Rename `nsm-security-level' to `network-security-level'
[emacs.git] / lisp / nxml / rng-maint.el
blobd143cd428edae10bf05ab5d250210c2268b9d57e
1 ;;; rng-maint.el --- commands for RELAX NG maintainers
3 ;; Copyright (C) 2003, 2007-2014 Free Software Foundation, Inc.
5 ;; Author: James Clark
6 ;; Keywords: wp, hypermedia, languages, XML, RelaxNG
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;;; Code:
27 (require 'xmltok)
28 (require 'nxml-mode)
29 (require 'texnfo-upd)
31 (defvar rng-dir (file-name-directory load-file-name))
33 ;;; Conversion from XML to texinfo.
34 ;; This is all a hack and is just enough to make the conversion work.
35 ;; It's not intended for public use.
37 (defvar rng-manual-base "nxml-mode")
38 (defvar rng-manual-xml (concat rng-manual-base ".xml"))
39 (defvar rng-manual-texi (concat rng-manual-base ".texi"))
40 (defvar rng-manual-info (concat rng-manual-base ".info"))
42 (defun rng-format-manual ()
43 "Create manual.texi from manual.xml."
44 (interactive)
45 (let ((xml-buf (find-file-noselect (expand-file-name rng-manual-xml
46 rng-dir)))
47 (texi-buf (find-file-noselect (expand-file-name rng-manual-texi
48 rng-dir))))
49 (with-current-buffer texi-buf
50 (erase-buffer)
51 (let ((standard-output texi-buf))
52 (princ (format "\\input texinfo @c -*- texinfo -*-\n\
53 @c %%**start of header\n\
54 @setfilename %s\n\
55 @settitle \n\
56 @c %%**end of header\n" rng-manual-info))
57 (set-buffer xml-buf)
58 (goto-char (point-min))
59 (xmltok-save
60 (xmltok-forward-prolog)
61 (rng-process-tokens))
62 (princ "\n@bye\n"))
63 (set-buffer texi-buf)
64 (rng-manual-fixup)
65 (texinfo-insert-node-lines (point-min) (point-max) t)
66 (texinfo-all-menus-update)
67 (save-buffer))))
69 (defun rng-manual-fixup ()
70 (goto-char (point-min))
71 (search-forward "@top ")
72 (let ((pos (point)))
73 (search-forward "\n")
74 (let ((title (buffer-substring-no-properties pos (1- (point)))))
75 (goto-char (point-min))
76 (search-forward "@settitle ")
77 (insert title)
78 (search-forward "@node")
79 (goto-char (match-beginning 0))
80 (insert "@dircategory Emacs\n"
81 "@direntry\n* "
82 title
83 ": ("
84 rng-manual-info
85 ").\n@end direntry\n\n"))))
87 (defvar rng-manual-inline-elements '(kbd key samp code var emph uref point))
89 (defun rng-process-tokens ()
90 (let ((section-depth 0)
91 ;; stack of per-element space treatment
92 ;; t means keep, nil means discard, fill means no blank lines
93 (keep-space-stack (list nil))
94 (ignore-following-newline nil)
95 (want-blank-line nil)
96 name startp endp data keep-space-for-children)
97 (while (xmltok-forward)
98 (cond ((memq xmltok-type '(start-tag empty-element end-tag))
99 (setq startp (memq xmltok-type '(start-tag empty-element)))
100 (setq endp (memq xmltok-type '(end-tag empty-element)))
101 (setq name (intern (if startp
102 (xmltok-start-tag-qname)
103 (xmltok-end-tag-qname))))
104 (setq keep-space-for-children nil)
105 (setq ignore-following-newline nil)
106 (cond ((memq name rng-manual-inline-elements)
107 (when startp
108 (when want-blank-line
109 (rng-manual-output-force-blank-line)
110 (when (eq want-blank-line 'noindent)
111 (princ "@noindent\n"))
112 (setq want-blank-line nil))
113 (setq keep-space-for-children t)
114 (princ (format "@%s{" name)))
115 (when endp (princ "}")))
116 ((eq name 'ulist)
117 (when startp
118 (rng-manual-output-force-blank-line)
119 (setq want-blank-line nil)
120 (princ "@itemize @bullet\n"))
121 (when endp
122 (rng-manual-output-force-new-line)
123 (setq want-blank-line 'noindent)
124 (princ "@end itemize\n")))
125 ((eq name 'item)
126 (rng-manual-output-force-new-line)
127 (setq want-blank-line endp)
128 (when startp (princ "@item\n")))
129 ((memq name '(example display))
130 (when startp
131 (setq ignore-following-newline t)
132 (rng-manual-output-force-blank-line)
133 (setq want-blank-line nil)
134 (setq keep-space-for-children t)
135 (princ (format "@%s\n" name)))
136 (when endp
137 (rng-manual-output-force-new-line)
138 (setq want-blank-line 'noindent)
139 (princ (format "@end %s\n" name))))
140 ((eq name 'para)
141 (rng-manual-output-force-new-line)
142 (when startp
143 (when want-blank-line
144 (setq want-blank-line t))
145 (setq keep-space-for-children 'fill))
146 (when endp (setq want-blank-line t)))
147 ((eq name 'section)
148 (when startp
149 (rng-manual-output-force-blank-line)
150 (when (eq section-depth 0)
151 (princ "@node Top\n"))
152 (princ "@")
153 (princ (nth section-depth '(top
154 chapter
155 section
156 subsection
157 subsubsection)))
158 (princ " ")
159 (setq want-blank-line nil)
160 (setq section-depth (1+ section-depth)))
161 (when endp
162 (rng-manual-output-force-new-line)
163 (setq want-blank-line nil)
164 (setq section-depth (1- section-depth))))
165 ((eq name 'title)
166 (when startp
167 (setq keep-space-for-children 'fill))
168 (when endp
169 (setq want-blank-line t)
170 (princ "\n"))))
171 (when startp
172 (setq keep-space-stack (cons keep-space-for-children
173 keep-space-stack)))
174 (when endp
175 (setq keep-space-stack (cdr keep-space-stack))))
176 ((memq xmltok-type '(data
177 space
178 char-ref
179 entity-ref
180 cdata-section))
181 (setq data nil)
182 (cond ((memq xmltok-type '(data space))
183 (setq data (buffer-substring-no-properties xmltok-start
184 (point))))
185 ((and (memq xmltok-type '(char-ref entity-ref))
186 xmltok-replacement)
187 (setq data xmltok-replacement))
188 ((eq xmltok-type 'cdata-section)
189 (setq data
190 (buffer-substring-no-properties (+ xmltok-start 9)
191 (- (point) 3)))))
192 (when (and data (car keep-space-stack))
193 (setq data (replace-regexp-in-string "[@{}]"
194 "@\\&"
195 data
197 (when ignore-following-newline
198 (setq data (replace-regexp-in-string "\\`\n" "" data t)))
199 (setq ignore-following-newline nil)
200 ;; (when (eq (car keep-space-stack) 'fill)
201 ;; (setq data (replace-regexp-in-string "\n" " " data t)))
202 (when (eq want-blank-line 'noindent)
203 (setq data (replace-regexp-in-string "\\`\n*" "" data t)))
204 (when (> (length data) 0)
205 (when want-blank-line
206 (rng-manual-output-force-blank-line)
207 (when (eq want-blank-line 'noindent)
208 (princ "@noindent\n"))
209 (setq want-blank-line nil))
210 (princ data))))
211 ))))
213 (defun rng-manual-output-force-new-line ()
214 (with-current-buffer standard-output
215 (unless (eq (char-before) ?\n)
216 (insert ?\n))))
218 (defun rng-manual-output-force-blank-line ()
219 (with-current-buffer standard-output
220 (if (eq (char-before) ?\n)
221 (unless (eq (char-before (1- (point))) ?\n)
222 (insert ?\n))
223 (insert "\n\n"))))
225 ;;; Timing
227 (defun rng-time-function (function &rest args)
228 (let* ((start (current-time))
229 (val (apply function args))
230 (end (current-time)))
231 (message "%s ran in %g seconds"
232 function
233 (float-time (time-subtract end start)))
234 val))
236 (defun rng-time-tokenize-buffer ()
237 (interactive)
238 (rng-time-function 'rng-tokenize-buffer))
240 (defun rng-tokenize-buffer ()
241 (save-excursion
242 (goto-char (point-min))
243 (xmltok-save
244 (xmltok-forward-prolog)
245 (while (xmltok-forward)))))
247 (defun rng-time-validate-buffer ()
248 (interactive)
249 (rng-time-function 'rng-validate-buffer))
251 (defvar rng-error-count)
252 (defvar rng-validate-up-to-date-end)
253 (declare-function rng-clear-cached-state "rng-valid" (start end))
254 (declare-function rng-clear-overlays "rng-valid" (beg end))
255 (declare-function rng-clear-conditional-region "rng-valid" ())
256 (declare-function rng-do-some-validation "rng-valid"
257 (&optional continue-p-function))
259 (defun rng-validate-buffer ()
260 (save-restriction
261 (widen)
262 (with-silent-modifications
263 (rng-clear-cached-state (point-min) (point-max)))
264 ;; 1+ to clear empty overlays at (point-max)
265 (rng-clear-overlays (point-min) (1+ (point-max))))
266 (setq rng-validate-up-to-date-end 1)
267 (rng-clear-conditional-region)
268 (setq rng-error-count 0)
269 (while (rng-do-some-validation
270 (lambda () t))))
272 ;;; rng-maint.el ends here