Mercurial > emacs
annotate lisp/nxml/rng-valid.el @ 102686:6ff0334666aa
Jari Aalto <jari.aalto at cante.net>
(display-time-default-load-average): Doc fix.
author | Glenn Morris <rgm@gnu.org> |
---|---|
date | Sat, 21 Mar 2009 18:53:14 +0000 |
parents | a9dc0e7c3f2b |
children | cbe5e21da2e4 |
rev | line source |
---|---|
86361 | 1 ;;; rng-valid.el --- real-time validation of XML using RELAX NG |
2 | |
100908 | 3 ;; Copyright (C) 2003, 2007, 2008, 2009 Free Software Foundation, Inc. |
86361 | 4 |
5 ;; Author: James Clark | |
6 ;; Keywords: XML, RelaxNG | |
7 | |
86555 | 8 ;; This file is part of GNU Emacs. |
9 | |
94666
d495d4d5452f
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
87665
diff
changeset
|
10 ;; GNU Emacs is free software: you can redistribute it and/or modify |
86555 | 11 ;; it under the terms of the GNU General Public License as published by |
94666
d495d4d5452f
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
87665
diff
changeset
|
12 ;; the Free Software Foundation, either version 3 of the License, or |
d495d4d5452f
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
87665
diff
changeset
|
13 ;; (at your option) any later version. |
86361 | 14 |
86555 | 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. | |
86361 | 19 |
86555 | 20 ;; You should have received a copy of the GNU General Public License |
94666
d495d4d5452f
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
87665
diff
changeset
|
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
86361 | 22 |
23 ;;; Commentary: | |
24 | |
25 ;; For usage information, see the documentation for rng-validate-mode. | |
26 ;; | |
27 ;; This file provides a minor mode that continually validates a buffer | |
28 ;; against a RELAX NG schema. The validation state is used to support | |
29 ;; schema-sensitive editing as well as validation. Validation is | |
30 ;; performed while Emacs is idle. XML parsing is done using | |
31 ;; xmltok.el. This file is responsible for checking that end-tags | |
32 ;; match their start-tags. Namespace processing is handled by | |
33 ;; nxml-ns.el. The RELAX NG Compact Syntax schema is parsed into | |
34 ;; internal form by rng-cmpct.el. This internal form is described by | |
35 ;; rng-pttrn.el. Validation of the document by matching against this | |
36 ;; internal form is done by rng-match.el. Handling of W3C XML Schema | |
37 ;; datatypes is delegated by rng-match.el to rng-xsd.el. The minor | |
38 ;; mode is intended to be used in conjunction with the nxml major | |
39 ;; mode, but does not have to be. | |
40 ;; | |
41 ;; The major responsibility of this file is to allow validation to | |
42 ;; happen incrementally. If a buffer has been validated and is then | |
43 ;; changed, we can often revalidate it without having to completely | |
44 ;; parse and validate it from start to end. As we parse and validate | |
45 ;; the buffer, we periodically cache the state. The state has three | |
46 ;; components: the stack of open elements, the namespace processing | |
47 ;; state and the RELAX NG validation state. The state is cached as the | |
48 ;; value of the rng-state text property on the closing greater-than of | |
49 ;; tags (but at intervals, not on every tag). We keep track of the | |
50 ;; position up to which cached state is known to be correct by adding | |
51 ;; a function to the buffer's after-change-functions. This is stored | |
52 ;; in the rng-validate-up-to-date-end variable. The first way in | |
53 ;; which we make validation incremental is obvious: we start | |
54 ;; validation from the first cached state before | |
55 ;; rng-validate-up-to-date-end. | |
56 ;; | |
57 ;; To make this work efficiently, we have to be able to copy the | |
58 ;; current parsing and validation state efficiently. We do this by | |
59 ;; minimizing destructive changes to the objects storing the state. | |
60 ;; When state is changed, we use the old state to create new objects | |
61 ;; representing the new state rather than destructively modifying the | |
62 ;; objects representing the old state. Copying the state is just a | |
63 ;; matter of making a list of three objects, one for each component of | |
64 ;; the state; the three objects themselves can be shared and do not | |
65 ;; need to be copied. | |
66 ;; | |
67 ;; There's one other idea that is used to make validation incremental. | |
68 ;; Suppose we have a buffer that's 4000 bytes long and suppose we | |
69 ;; validated it, caching state at positions 1000, 2000 and 3000. Now | |
70 ;; suppose we make a change at position 1500 inserting 100 characters. | |
71 ;; rng-validate-up-to-date-end will be changed to 1500. When Emacs | |
72 ;; becomes idle and we revalidate, validation will restart using the | |
73 ;; cached state at position 1000. However, we take advantage of the | |
74 ;; cached state beyond rng-validate-up-to-date-end as follows. When | |
75 ;; our validation reaches position 2100 (the current position of the | |
76 ;; character that was at 2000), we compare our current state with the | |
77 ;; cached state. If they are the same, then we can stop parsing | |
78 ;; immediately and set rng-validate-up-to-date-end to the end of the | |
79 ;; buffer: we already know that the state cached at position 3100 is | |
80 ;; correct. If they are not the same, then we have to continue | |
81 ;; parsing. After the change, but before revalidation, we call the | |
82 ;; region from 1600 to the end of the buffer "conditionally | |
83 ;; up-to-date". | |
84 ;; | |
85 ;; As well as the cached parsing and validation state, we also keep | |
86 ;; track of the errors in the file. Errors are stored as overlays | |
87 ;; with a category of rng-error. The number of such overlays in the | |
88 ;; buffer must always be equal to rng-error-count. | |
89 | |
90 ;;; Code: | |
91 | |
92 (require 'xmltok) | |
93 (require 'nxml-enc) | |
94 (require 'nxml-util) | |
95 (require 'nxml-ns) | |
96 (require 'rng-match) | |
97 (require 'rng-util) | |
98 (require 'rng-loc) | |
99 | |
100 ;;; Customizable variables | |
101 | |
102 (defgroup relax-ng nil | |
103 "Validation of XML using RELAX NG." | |
104 :group 'wp | |
105 :group 'nxml | |
106 :group 'languages) | |
107 | |
87346
c5910db8e06e
(rng-error): Rename from rng-error-face.
Jason Rumney <jasonr@gnu.org>
parents:
86555
diff
changeset
|
108 (defface rng-error '((t (:inherit font-lock-warning-face))) |
86361 | 109 "Face for highlighting XML errors." |
110 :group 'relax-ng) | |
111 | |
112 (defcustom rng-state-cache-distance 2000 | |
113 "*Distance in characters between each parsing and validation state cache." | |
114 :type 'integer | |
115 :group 'relax-ng) | |
116 | |
117 (defcustom rng-validate-chunk-size 8000 | |
118 "*Number of characters in a RELAX NG validation chunk. | |
119 A validation chunk will be the smallest chunk that is at least this | |
120 size and ends with a tag. After validating a chunk, validation will | |
121 continue only if Emacs is still idle." | |
122 :type 'integer | |
123 :group 'relax-ng) | |
124 | |
125 (defcustom rng-validate-delay 1.5 | |
126 "*Time in seconds that Emacs must be idle before starting a full validation. | |
127 A full validation continues until either validation is up to date | |
128 or Emacs is no longer idle." | |
129 :type 'number | |
130 :group 'relax-ng) | |
131 | |
132 (defcustom rng-validate-quick-delay 0.3 | |
133 "*Time in seconds that Emacs must be idle before starting a quick validation. | |
134 A quick validation validates at most one chunk." | |
135 :type 'number | |
136 :group 'relax-ng) | |
137 | |
138 ;; Global variables | |
139 | |
140 (defvar rng-validate-timer nil) | |
141 (make-variable-buffer-local 'rng-validate-timer) | |
142 ;; ensure that we can cancel the timer even after a kill-all-local-variables | |
143 (put 'rng-validate-timer 'permanent-local t) | |
144 | |
145 (defvar rng-validate-quick-timer nil) | |
146 (make-variable-buffer-local 'rng-validate-quick-timer) | |
147 ;; ensure that we can cancel the timer even after a kill-all-local-variables | |
148 (put 'rng-validate-quick-timer 'permanent-local t) | |
149 | |
150 (defvar rng-error-count nil | |
96496
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
151 "Number of errors in the current buffer. |
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
152 Always equal to number of overlays with category `rng-error'.") |
86361 | 153 (make-variable-buffer-local 'rng-error-count) |
154 | |
155 (defvar rng-message-overlay nil | |
96496
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
156 "Overlay in this buffer whose `help-echo' property was last printed. |
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
157 It is nil if none.") |
86361 | 158 (make-variable-buffer-local 'rng-message-overlay) |
159 | |
160 (defvar rng-message-overlay-inhibit-point nil | |
161 "Position at which message from overlay should be inhibited. | |
162 If point is equal to this and the error overlay around | |
163 point is `rng-message-overlay', then the `help-echo' property | |
164 of the error overlay should not be printed with `message'.") | |
165 (make-variable-buffer-local 'rng-message-overlay-inhibit-point) | |
166 | |
167 (defvar rng-message-overlay-current nil | |
168 "Non-nil if `rng-message-overlay' is still the current message.") | |
169 (make-variable-buffer-local 'rng-message-overlay-current) | |
170 | |
171 (defvar rng-open-elements nil | |
172 "Stack of names of open elements represented as a list. | |
173 Each member of the list is either t or a (PREFIX . LOCAL-NAME) pair. | |
174 \(PREFIX . LOCAL-NAME) is pushed for a start-tag; t is pushed | |
175 for a mismatched end-tag.") | |
176 | |
177 (defvar rng-pending-contents nil | |
178 "Text content of current element that has yet to be processed. | |
179 Value is a list of segments (VALUE START END) positions in reverse | |
180 order. VALUE is a string or nil. If VALUE is nil, then the value is | |
181 the string between START and END. A segment can also be nil | |
182 indicating an unresolvable entity or character reference.") | |
183 | |
184 (defvar rng-collecting-text nil) | |
185 | |
186 (defvar rng-validate-up-to-date-end nil | |
187 "Last position where validation is known to be up to date.") | |
188 (make-variable-buffer-local 'rng-validate-up-to-date-end) | |
189 | |
190 (defvar rng-conditional-up-to-date-start nil | |
191 "Marker for the start of the conditionally up-to-date region. | |
96496
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
192 It is nil if there is no conditionally up-to-date region. The |
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
193 conditionally up-to-date region must be such that for any cached |
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
194 state S with position P in the conditionally up-to-date region, |
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
195 if at some point it is determined that S becomes correct for P, |
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
196 then all states with position >= P in the conditionally up to |
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
197 date region must also then be correct and all errors between P |
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
198 and the end of the region must then be correctly marked.") |
86361 | 199 (make-variable-buffer-local 'rng-conditional-up-to-date-start) |
200 | |
201 (defvar rng-conditional-up-to-date-end nil | |
202 "Marker for the end of the conditionally up-to-date region. | |
96496
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
203 It is nil if there is no conditionally up-to-date region. |
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
204 See the variable `rng-conditional-up-to-date-start'.") |
86361 | 205 (make-variable-buffer-local 'rng-conditional-up-to-date-end) |
206 | |
207 (defvar rng-parsing-for-state nil | |
208 "Non-nil means we are currently parsing just to compute the state. | |
209 Should be dynamically bound.") | |
210 | |
211 (defvar rng-validate-mode nil) | |
212 (make-variable-buffer-local 'rng-validate-mode) | |
213 | |
214 (defvar rng-dtd nil) | |
215 (make-variable-buffer-local 'rng-dtd) | |
216 | |
217 ;;;###autoload | |
218 (defun rng-validate-mode (&optional arg no-change-schema) | |
219 "Minor mode performing continual validation against a RELAX NG schema. | |
220 | |
221 Checks whether the buffer is a well-formed XML 1.0 document, | |
222 conforming to the XML Namespaces Recommendation and valid against a | |
96496
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
223 RELAX NG schema. The mode-line indicates whether it is or not. Any |
86361 | 224 parts of the buffer that cause it not to be are considered errors and |
96496
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
225 are highlighted with face `rng-error'. A description of each error is |
86361 | 226 available as a tooltip. \\[rng-next-error] goes to the next error |
96496
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
227 after point. Clicking mouse-1 on the word `Invalid' in the mode-line |
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
228 goes to the first error in the buffer. If the buffer changes, then it |
86361 | 229 will be automatically rechecked when Emacs becomes idle; the |
96496
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
230 rechecking will be paused whenever there is input pending. |
86361 | 231 |
232 By default, uses a vacuous schema that allows any well-formed XML | |
96496
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
233 document. A schema can be specified explictly using |
86361 | 234 \\[rng-set-schema-file-and-validate], or implicitly based on the buffer's |
235 file name or on the root element name. In each case the schema must | |
236 be a RELAX NG schema using the compact schema \(such schemas | |
237 conventionally have a suffix of `.rnc'). The variable | |
238 `rng-schema-locating-files' specifies files containing rules | |
239 to use for finding the schema." | |
240 (interactive "P") | |
241 (setq rng-validate-mode | |
242 (if (null arg) | |
243 (not rng-validate-mode) | |
244 (> (prefix-numeric-value arg) 0))) | |
245 (save-restriction | |
246 (widen) | |
247 (nxml-with-unmodifying-text-property-changes | |
248 (rng-clear-cached-state (point-min) (point-max))) | |
249 ;; 1+ to clear empty overlays at (point-max) | |
250 (rng-clear-overlays (point-min) (1+ (point-max)))) | |
251 (setq rng-validate-up-to-date-end 1) | |
252 (rng-clear-conditional-region) | |
253 (setq rng-error-count 0) | |
254 ;; do this here to avoid infinite loop if we set the schema | |
255 (remove-hook 'rng-schema-change-hook 'rng-validate-clear t) | |
256 (cond (rng-validate-mode | |
257 (unwind-protect | |
258 (save-excursion | |
259 ;; An error can change the current buffer | |
260 (when (or (not rng-current-schema) | |
261 (and (eq rng-current-schema rng-any-element) | |
262 (not no-change-schema))) | |
263 (rng-auto-set-schema t))) | |
264 (unless rng-current-schema (rng-set-schema-file-1 nil)) | |
265 (add-hook 'rng-schema-change-hook 'rng-validate-clear nil t) | |
266 (add-hook 'after-change-functions 'rng-after-change-function nil t) | |
267 (add-hook 'kill-buffer-hook 'rng-kill-timers nil t) | |
268 (add-hook 'echo-area-clear-hook 'rng-echo-area-clear-function nil t) | |
269 (add-hook 'post-command-hook 'rng-maybe-echo-error-at-point nil t) | |
270 (rng-match-init-buffer) | |
271 (rng-activate-timers) | |
272 ;; Start validating right away if the buffer is visible. | |
273 ;; If it's not visible, don't do this, because the user | |
274 ;; won't get any progress indication. When the user finds | |
275 ;; a new file, then the buffer won't be visible | |
276 ;; when this is invoked. | |
277 (when (get-buffer-window (current-buffer) 'visible) | |
278 (rng-validate-while-idle (current-buffer))))) | |
279 (t | |
280 (rng-cancel-timers) | |
281 (force-mode-line-update) | |
282 (remove-hook 'kill-buffer-hook 'rng-cancel-timers t) | |
283 (remove-hook 'post-command-hook 'rng-maybe-echo-error-at-point t) | |
284 (remove-hook 'echo-area-clear-hook 'rng-echo-area-clear-function t) | |
285 (remove-hook 'after-change-functions 'rng-after-change-function t)))) | |
286 | |
287 (defun rng-set-schema-file-and-validate (filename) | |
288 "Sets the schema and turns on `rng-validate-mode' if not already on. | |
289 The schema is set like `rng-set-schema'." | |
290 (interactive "fSchema file: ") | |
291 (rng-set-schema-file filename) | |
292 (or rng-validate-mode (rng-validate-mode))) | |
293 | |
294 (defun rng-set-document-type-and-validate (type-id) | |
295 (interactive (list (rng-read-type-id))) | |
296 (and (rng-set-document-type type-id) | |
297 (or rng-validate-mode (rng-validate-mode)))) | |
96496
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
298 |
86361 | 299 (defun rng-auto-set-schema-and-validate () |
300 "Set the schema for this buffer automatically and turn on `rng-validate-mode'. | |
301 The schema is set like `rng-auto-set-schema'." | |
302 (interactive) | |
303 (rng-auto-set-schema) | |
304 (or rng-validate-mode (rng-validate-mode))) | |
305 | |
306 (defun rng-after-change-function (start end pre-change-len) | |
307 ;; Work around bug in insert-file-contents. | |
308 (when (> end (1+ (buffer-size))) | |
309 (setq start 1) | |
310 (setq end (1+ (buffer-size)))) | |
311 (setq rng-message-overlay-inhibit-point nil) | |
312 (nxml-with-unmodifying-text-property-changes | |
313 (rng-clear-cached-state start end)) | |
314 ;; rng-validate-up-to-date-end holds the position before the change | |
315 ;; Adjust it to reflect the change. | |
316 (if (< start rng-validate-up-to-date-end) | |
317 (setq rng-validate-up-to-date-end | |
318 (if (<= (+ start pre-change-len) rng-validate-up-to-date-end) | |
319 (+ rng-validate-up-to-date-end | |
320 (- end start pre-change-len)) | |
321 start))) | |
322 ;; Adjust the conditional zone | |
323 (cond (rng-conditional-up-to-date-start | |
324 (when (< rng-conditional-up-to-date-start end) | |
325 (if (< end rng-conditional-up-to-date-end) | |
326 (set-marker rng-conditional-up-to-date-start end) | |
327 (rng-clear-conditional-region)))) | |
328 ((< end rng-validate-up-to-date-end) | |
329 (setq rng-conditional-up-to-date-end | |
330 (copy-marker rng-validate-up-to-date-end nil)) | |
331 (setq rng-conditional-up-to-date-start | |
332 (copy-marker end t)))) | |
333 ;; Adjust rng-validate-up-to-date-end | |
334 (if (< start rng-validate-up-to-date-end) | |
335 (setq rng-validate-up-to-date-end start)) | |
336 ;; Must make rng-validate-up-to-date-end < point-max | |
337 ;; (unless the buffer is empty). | |
338 ;; otherwise validate-prepare will say there's nothing to do. | |
339 ;; Don't use (point-max) because we may be narrowed. | |
340 (if (> rng-validate-up-to-date-end (buffer-size)) | |
341 (setq rng-validate-up-to-date-end | |
342 (max 1 (1- rng-validate-up-to-date-end)))) | |
343 ;; Arrange to revalidate | |
344 (rng-activate-timers) | |
345 ;; Need to do this after activating the timer | |
346 (force-mode-line-update)) | |
347 | |
348 (defun rng-compute-mode-line-string () | |
349 (cond (rng-validate-timer | |
350 (concat " Validated:" | |
351 (number-to-string | |
352 ;; Use floor rather than round because we want | |
353 ;; to show 99% rather than 100% for changes near | |
354 ;; the end. | |
355 (floor (if (eq (buffer-size) 0) | |
356 0.0 | |
357 (/ (* (- rng-validate-up-to-date-end 1) 100.0) | |
358 (buffer-size))))) | |
359 "%%")) | |
360 ((> rng-error-count 0) | |
361 (concat " " | |
362 (propertize "Invalid" | |
363 'help-echo "mouse-1: go to first error" | |
364 'local-map (make-mode-line-mouse-map | |
365 'mouse-1 | |
366 'rng-mouse-first-error)))) | |
367 (t " Valid"))) | |
96496
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
368 |
86361 | 369 (defun rng-cancel-timers () |
370 (let ((inhibit-quit t)) | |
371 (when rng-validate-timer | |
372 (cancel-timer rng-validate-timer) | |
373 (setq rng-validate-timer nil)) | |
374 (when rng-validate-quick-timer | |
375 (cancel-timer rng-validate-quick-timer) | |
376 (setq rng-validate-quick-timer nil)))) | |
377 | |
378 (defun rng-kill-timers () | |
379 ;; rng-validate-timer and rng-validate-quick-timer have the | |
380 ;; permanent-local property, so that the timers can be | |
381 ;; cancelled even after changing mode. | |
382 ;; This function takes care of cancelling the timers and | |
383 ;; then killing the local variables. | |
384 (when (local-variable-p 'rng-validate-timer) | |
385 (when rng-validate-timer | |
386 (cancel-timer rng-validate-timer)) | |
387 (kill-local-variable 'rng-validate-timer)) | |
388 (when (local-variable-p 'rng-validate-quick-timer) | |
389 (when rng-validate-quick-timer | |
390 (cancel-timer rng-validate-quick-timer)) | |
391 (kill-local-variable 'rng-validate-quick-timer))) | |
96496
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
392 |
86361 | 393 (defun rng-activate-timers () |
394 (unless rng-validate-timer | |
395 (let ((inhibit-quit t)) | |
396 (setq rng-validate-timer | |
397 (run-with-idle-timer rng-validate-delay | |
398 t | |
399 'rng-validate-while-idle | |
400 (current-buffer))) | |
401 (setq rng-validate-quick-timer | |
402 (run-with-idle-timer rng-validate-quick-delay | |
403 t | |
404 'rng-validate-quick-while-idle | |
405 (current-buffer)))))) | |
406 | |
407 (defun rng-validate-clear () | |
408 (rng-validate-mode 1 t)) | |
409 | |
410 ;; These two variables are dynamically bound and used | |
411 ;; to pass information between rng-validate-while-idle | |
412 ;; and rng-validate-while-idle-continue-p. | |
413 | |
414 (defvar rng-validate-display-point nil) | |
415 (defvar rng-validate-display-modified-p nil) | |
416 | |
417 (defun rng-validate-while-idle-continue-p () | |
418 ;; input-pending-p and sit-for run timers that are | |
419 ;; ripe. Binding timer-idle-list to nil prevents | |
420 ;; this. If we don't do this, then any ripe timers | |
421 ;; will get run, and we won't get any chance to | |
422 ;; validate until Emacs becomes idle again or until | |
423 ;; the other lower priority timers finish (which | |
424 ;; can take a very long time in the case of | |
425 ;; jit-lock). | |
426 (let ((timer-idle-list nil)) | |
427 (and (not (input-pending-p)) | |
428 ;; Fake rng-validate-up-to-date-end so that the mode line | |
429 ;; shows progress. Also use this to save point. | |
430 (let ((rng-validate-up-to-date-end (point))) | |
431 (goto-char rng-validate-display-point) | |
432 (when (not rng-validate-display-modified-p) | |
433 (restore-buffer-modified-p nil)) | |
434 (force-mode-line-update) | |
435 (let ((continue (sit-for 0))) | |
436 (goto-char rng-validate-up-to-date-end) | |
437 continue))))) | |
438 | |
439 ;; Calling rng-do-some-validation once with a continue-p function, as | |
440 ;; opposed to calling it repeatedly, helps on initial validation of a | |
441 ;; large buffer with lots of errors. The overlays for errors will all | |
442 ;; get added when rng-do-some-validation returns and won't slow the | |
443 ;; validation process down. | |
444 | |
445 (defun rng-validate-while-idle (buffer) | |
446 (with-current-buffer buffer | |
447 (if rng-validate-mode | |
448 (if (let ((rng-validate-display-point (point)) | |
449 (rng-validate-display-modified-p (buffer-modified-p))) | |
450 (rng-do-some-validation 'rng-validate-while-idle-continue-p)) | |
451 (force-mode-line-update) | |
452 (rng-validate-done)) | |
453 ;; must have done kill-all-local-variables | |
454 (rng-kill-timers)))) | |
455 | |
456 (defun rng-validate-quick-while-idle (buffer) | |
457 (with-current-buffer buffer | |
458 (if rng-validate-mode | |
459 (if (rng-do-some-validation) | |
460 (force-mode-line-update) | |
461 (rng-validate-done)) | |
462 ;; must have done kill-all-local-variables | |
463 (rng-kill-timers)))) | |
464 | |
465 (defun rng-validate-done () | |
466 (when (or (not (current-message)) | |
467 (rng-current-message-from-error-overlay-p)) | |
96496
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
468 (rng-error-overlay-message (or (rng-error-overlay-after (point)) |
86361 | 469 (rng-error-overlay-after (1- (point)))))) |
470 (rng-cancel-timers) | |
471 (force-mode-line-update)) | |
472 | |
473 (defun rng-do-some-validation (&optional continue-p-function) | |
96496
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
474 "Do some validation work. Return t if more to do, nil otherwise." |
86361 | 475 (save-excursion |
476 (save-restriction | |
477 (widen) | |
478 (nxml-with-invisible-motion | |
479 (condition-case err | |
480 (and (rng-validate-prepare) | |
481 (let ((rng-dt-namespace-context-getter '(nxml-ns-get-context))) | |
482 (nxml-with-unmodifying-text-property-changes | |
483 (rng-do-some-validation-1 continue-p-function)))) | |
96361
a99299e4d2de
American English spelling fix.
Glenn Morris <rgm@gnu.org>
parents:
94666
diff
changeset
|
484 ;; errors signaled from a function run by an idle timer |
86361 | 485 ;; are ignored; if we don't catch them, validation |
486 ;; will get mysteriously stuck at a single place | |
487 (rng-compile-error | |
488 (message "Incorrect schema. %s" (nth 1 err)) | |
489 (rng-validate-mode 0) | |
490 nil) | |
491 (error | |
492 (message "Internal error in rng-validate-mode triggered at buffer position %d. %s" | |
493 (point) | |
494 (error-message-string err)) | |
495 (rng-validate-mode 0) | |
496 nil)))))) | |
497 | |
498 (defun rng-validate-prepare () | |
499 "Prepare to do some validation, initializing point and the state. | |
500 Return t if there is work to do, nil otherwise." | |
501 (cond ((= rng-validate-up-to-date-end (point-min)) | |
502 (rng-set-initial-state) | |
503 t) | |
504 ((= rng-validate-up-to-date-end (point-max)) | |
505 nil) | |
506 (t (let ((state (get-text-property (1- rng-validate-up-to-date-end) | |
507 'rng-state))) | |
508 (cond (state | |
509 (rng-restore-state state) | |
510 (goto-char rng-validate-up-to-date-end)) | |
511 (t | |
512 (let ((pos (previous-single-property-change | |
513 rng-validate-up-to-date-end | |
514 'rng-state))) | |
515 (cond (pos | |
516 (rng-restore-state | |
517 (or (get-text-property (1- pos) 'rng-state) | |
518 (error "Internal error: state null"))) | |
519 (goto-char pos)) | |
520 (t (rng-set-initial-state)))))))))) | |
521 | |
522 | |
523 (defun rng-do-some-validation-1 (&optional continue-p-function) | |
524 (let ((limit (+ rng-validate-up-to-date-end | |
525 rng-validate-chunk-size)) | |
526 (remove-start rng-validate-up-to-date-end) | |
527 (next-cache-point (+ (point) rng-state-cache-distance)) | |
528 (continue t) | |
529 (xmltok-dtd rng-dtd) | |
530 have-remaining-chars | |
531 xmltok-type | |
532 xmltok-start | |
533 xmltok-name-colon | |
534 xmltok-name-end | |
535 xmltok-replacement | |
536 xmltok-attributes | |
537 xmltok-namespace-attributes | |
538 xmltok-dependent-regions | |
539 xmltok-errors) | |
540 (when (= (point) 1) | |
541 (let ((regions (xmltok-forward-prolog))) | |
542 (rng-clear-overlays 1 (point)) | |
543 (while regions | |
544 (when (eq (aref (car regions) 0) 'encoding-name) | |
545 (rng-process-encoding-name (aref (car regions) 1) | |
546 (aref (car regions) 2))) | |
547 (setq regions (cdr regions)))) | |
548 (unless (equal rng-dtd xmltok-dtd) | |
549 (rng-clear-conditional-region)) | |
550 (setq rng-dtd xmltok-dtd)) | |
551 (while continue | |
552 (setq have-remaining-chars (rng-forward)) | |
553 (let ((pos (point))) | |
554 (setq continue | |
555 (and have-remaining-chars | |
556 (or (< pos limit) | |
557 (and continue-p-function | |
558 (funcall continue-p-function) | |
559 (setq limit (+ limit rng-validate-chunk-size)) | |
560 t)))) | |
561 (cond ((and rng-conditional-up-to-date-start | |
562 ;; > because we are getting the state from (1- pos) | |
563 (> pos rng-conditional-up-to-date-start) | |
564 (< pos rng-conditional-up-to-date-end) | |
565 (rng-state-matches-current (get-text-property (1- pos) | |
566 'rng-state))) | |
567 (when (< remove-start (1- pos)) | |
568 (rng-clear-cached-state remove-start (1- pos))) | |
569 ;; sync up with cached validation state | |
570 (setq continue nil) | |
571 ;; do this before settting rng-validate-up-to-date-end | |
572 ;; in case we get a quit | |
573 (rng-mark-xmltok-errors) | |
574 (rng-mark-xmltok-dependent-regions) | |
575 (setq rng-validate-up-to-date-end | |
576 (marker-position rng-conditional-up-to-date-end)) | |
577 (rng-clear-conditional-region) | |
578 (setq have-remaining-chars | |
579 (< rng-validate-up-to-date-end (point-max)))) | |
580 ((or (>= pos next-cache-point) | |
581 (not continue)) | |
582 (setq next-cache-point (+ pos rng-state-cache-distance)) | |
583 (rng-clear-cached-state remove-start pos) | |
584 (when have-remaining-chars | |
585 (rng-cache-state (1- pos))) | |
586 (setq remove-start pos) | |
587 (unless continue | |
588 ;; if we have just blank chars skip to the end | |
589 (when have-remaining-chars | |
590 (skip-chars-forward " \t\r\n") | |
591 (when (= (point) (point-max)) | |
592 (rng-clear-overlays pos (point)) | |
593 (rng-clear-cached-state pos (point)) | |
594 (setq have-remaining-chars nil) | |
595 (setq pos (point)))) | |
596 (when (not have-remaining-chars) | |
597 (rng-process-end-document)) | |
598 (rng-mark-xmltok-errors) | |
599 (rng-mark-xmltok-dependent-regions) | |
600 (setq rng-validate-up-to-date-end pos) | |
601 (when rng-conditional-up-to-date-end | |
602 (cond ((<= rng-conditional-up-to-date-end pos) | |
603 (rng-clear-conditional-region)) | |
604 ((< rng-conditional-up-to-date-start pos) | |
605 (set-marker rng-conditional-up-to-date-start | |
606 pos))))))))) | |
607 have-remaining-chars)) | |
96496
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
608 |
86361 | 609 (defun rng-clear-conditional-region () |
610 (when rng-conditional-up-to-date-start | |
611 (set-marker rng-conditional-up-to-date-start nil) | |
612 (setq rng-conditional-up-to-date-start nil)) | |
613 (when rng-conditional-up-to-date-end | |
614 (set-marker rng-conditional-up-to-date-end nil) | |
615 (setq rng-conditional-up-to-date-end nil))) | |
616 | |
617 (defun rng-clear-cached-state (start end) | |
618 "Clear cached state between START and END." | |
619 (remove-text-properties start end '(rng-state nil))) | |
620 | |
621 (defun rng-cache-state (pos) | |
622 "Save the current state in a text property on the character at pos." | |
623 (put-text-property pos | |
624 (1+ pos) | |
625 'rng-state | |
626 (rng-get-state))) | |
627 | |
628 (defun rng-state-matches-current (state) | |
629 (and state | |
630 (rng-match-state-equal (car state)) | |
631 (nxml-ns-state-equal (nth 1 state)) | |
632 (equal (nth 2 state) rng-open-elements))) | |
633 | |
634 (defun rng-get-state () | |
635 (list (rng-match-state) | |
636 (nxml-ns-state) | |
637 rng-open-elements)) | |
638 | |
639 (defun rng-restore-state (state) | |
640 (rng-set-match-state (car state)) | |
641 (setq state (cdr state)) | |
642 (nxml-ns-set-state (car state)) | |
643 (setq rng-open-elements (cadr state)) | |
644 (setq rng-pending-contents nil) | |
645 (setq rng-collecting-text (rng-match-text-typed-p))) | |
646 | |
647 (defun rng-set-initial-state () | |
648 (nxml-ns-init) | |
649 (rng-match-start-document) | |
650 (setq rng-open-elements nil) | |
651 (setq rng-pending-contents nil) | |
652 (goto-char (point-min))) | |
653 | |
654 (defun rng-clear-overlays (beg end) | |
655 (unless rng-parsing-for-state | |
656 (let ((overlays (overlays-in beg end))) | |
657 (while overlays | |
658 (let* ((overlay (car overlays)) | |
659 (category (overlay-get overlay 'category))) | |
660 (cond ((eq category 'rng-error) | |
661 (let ((inhibit-quit t)) | |
662 (when (eq overlay rng-message-overlay) | |
663 (rng-error-overlay-message nil)) | |
664 (delete-overlay overlay) | |
665 ;; rng-error-count could be nil | |
666 ;; if overlays left over from a previous use | |
667 ;; of rng-validate-mode that ended with a change of mode | |
668 (when rng-error-count | |
669 (setq rng-error-count (1- rng-error-count))))) | |
670 ((and (eq category 'rng-dependent) | |
671 (<= beg (overlay-start overlay))) | |
672 (delete-overlay overlay)))) | |
673 (setq overlays (cdr overlays)))))) | |
674 | |
675 ;;; Dependent regions | |
676 | |
677 (defun rng-mark-xmltok-dependent-regions () | |
678 (while xmltok-dependent-regions | |
679 (apply 'rng-mark-xmltok-dependent-region | |
680 (car xmltok-dependent-regions)) | |
681 (setq xmltok-dependent-regions | |
682 (cdr xmltok-dependent-regions)))) | |
683 | |
684 (defun rng-mark-xmltok-dependent-region (fun start end &rest args) | |
685 (let ((overlay (make-overlay start end nil t t))) | |
686 (overlay-put overlay 'category 'rng-dependent) | |
687 (overlay-put overlay 'rng-funargs (cons fun args)))) | |
688 | |
689 (put 'rng-dependent 'evaporate t) | |
690 (put 'rng-dependent 'modification-hooks '(rng-dependent-region-changed)) | |
691 (put 'rng-dependent 'insert-behind-hooks '(rng-dependent-region-changed)) | |
692 | |
693 (defun rng-dependent-region-changed (overlay | |
694 after-p | |
695 change-start | |
696 change-end | |
697 &optional pre-change-length) | |
698 (when (and after-p | |
699 ;; Emacs sometimes appears to call deleted overlays | |
700 (overlay-start overlay) | |
701 (let ((funargs (overlay-get overlay 'rng-funargs))) | |
702 (save-match-data | |
703 (save-excursion | |
704 (save-restriction | |
705 (widen) | |
706 (apply (car funargs) | |
707 (append (list change-start | |
708 change-end | |
709 pre-change-length | |
710 (overlay-start overlay) | |
711 (overlay-end overlay)) | |
712 (cdr funargs)))))))) | |
713 (rng-after-change-function (overlay-start overlay) | |
714 change-end | |
715 (+ pre-change-length | |
716 (- (overlay-start overlay) | |
717 change-start))) | |
718 (delete-overlay overlay))) | |
719 | |
720 ;;; Error state | |
721 | |
722 (defun rng-mark-xmltok-errors () | |
723 (while xmltok-errors | |
724 (let ((err (car xmltok-errors))) | |
725 (rng-mark-not-well-formed (xmltok-error-message err) | |
726 (xmltok-error-start err) | |
727 (xmltok-error-end err))) | |
728 (setq xmltok-errors (cdr xmltok-errors)))) | |
729 | |
730 (defun rng-mark-invalid (message beg end) | |
731 (rng-mark-error message beg end)) | |
732 | |
733 (defun rng-mark-not-well-formed (message beg end) | |
734 ;; Don't try to validate further | |
735 ;;(rng-set-match-state rng-not-allowed-ipattern) | |
736 (rng-mark-error message beg end)) | |
737 | |
738 (defun rng-mark-error (message beg end) | |
739 (unless rng-parsing-for-state | |
740 (let ((overlays (overlays-in beg end))) | |
741 (while (and overlays message) | |
742 (let ((o (car overlays))) | |
743 (when (and (eq (overlay-get o 'category) 'rng-error) | |
744 (= (overlay-start o) beg) | |
745 (= (overlay-end o) end)) | |
746 (overlay-put o | |
747 'help-echo | |
748 (concat (overlay-get o 'help-echo) | |
749 "\n" | |
750 message)) | |
751 (setq message nil))) | |
752 (setq overlays (cdr overlays)))) | |
753 (when message | |
754 (let ((inhibit-quit t)) | |
755 (setq rng-error-count (1+ rng-error-count)) | |
756 (let ((overlay | |
757 (make-overlay beg end nil t | |
758 ;; Need to make the rear delimiter advance | |
759 ;; with the front delimiter when the overlay | |
760 ;; is empty, otherwise the front delimiter | |
761 ;; will move past the rear delimiter. | |
762 (= beg end)))) | |
763 ;; Ensure when we have two overlapping messages, the help-echo | |
764 ;; of the one that starts first is shown | |
765 (overlay-put overlay 'priority beg) | |
766 (overlay-put overlay 'category 'rng-error) | |
767 (overlay-put overlay 'help-echo message)))))) | |
768 | |
87346
c5910db8e06e
(rng-error): Rename from rng-error-face.
Jason Rumney <jasonr@gnu.org>
parents:
86555
diff
changeset
|
769 (put 'rng-error 'face 'rng-error) |
86361 | 770 (put 'rng-error 'modification-hooks '(rng-error-modified)) |
771 | |
772 ;; If we don't do this, then the front delimiter can move | |
773 ;; past the end delimiter. | |
774 (defun rng-error-modified (overlay after-p beg end &optional pre-change-len) | |
775 (when (and after-p | |
776 (overlay-start overlay) ; check not deleted | |
777 (>= (overlay-start overlay) | |
778 (overlay-end overlay))) | |
779 (let ((inhibit-quit t)) | |
780 (delete-overlay overlay) | |
781 (setq rng-error-count (1- rng-error-count))))) | |
782 | |
783 (defun rng-echo-area-clear-function () | |
784 (setq rng-message-overlay-current nil)) | |
785 | |
786 ;;; Error navigation | |
96496
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
787 |
86361 | 788 (defun rng-maybe-echo-error-at-point () |
789 (when (or (not (current-message)) | |
790 (rng-current-message-from-error-overlay-p)) | |
791 (rng-error-overlay-message (rng-error-overlay-after (point))))) | |
792 | |
793 (defun rng-error-overlay-after (pos) | |
794 (let ((overlays (overlays-in pos (1+ pos))) | |
795 (best nil)) | |
796 (while overlays | |
797 (let ((overlay (car overlays))) | |
798 (when (and (eq (overlay-get overlay 'category) | |
799 'rng-error) | |
800 (or (not best) | |
801 (< (overlay-start best) | |
802 (overlay-start overlay)))) | |
803 (setq best overlay))) | |
804 (setq overlays (cdr overlays))) | |
805 best)) | |
806 | |
807 (defun rng-first-error () | |
808 "Go to the first validation error. | |
809 Turn on `rng-validate-mode' if it is not already on." | |
810 (interactive) | |
811 (or rng-validate-mode (rng-validate-mode)) | |
812 (when (and (eq rng-validate-up-to-date-end 1) | |
813 (< rng-validate-up-to-date-end (point-max))) | |
814 (rng-do-some-validation)) | |
815 (let ((err (rng-find-next-error-overlay (1- (point-min))))) | |
816 (if err | |
817 (rng-goto-error-overlay err) | |
818 (let ((pos (save-excursion | |
819 (goto-char (point-min)) | |
820 (rng-next-error 1)))) | |
821 (when pos | |
822 (goto-char pos)))))) | |
823 | |
824 (defun rng-mouse-first-error (event) | |
825 "Go to the first validation error from a mouse click." | |
826 (interactive "e") | |
827 (select-window (posn-window (event-start event))) | |
828 (rng-first-error)) | |
829 | |
830 (defun rng-next-error (arg) | |
831 "Go to the next validation error after point. | |
832 Turn on `rng-validate-mode' if it is not already on. | |
96496
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
833 A prefix ARG specifies how many errors to move. |
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
834 A negative ARG moves backwards. Just \\[universal-argument] as a prefix |
86361 | 835 means goto the first error." |
836 (interactive "P") | |
837 (if (consp arg) | |
838 (rng-first-error) | |
839 (or rng-validate-mode (rng-validate-mode)) | |
840 (setq arg (prefix-numeric-value arg)) | |
841 (if (< arg 0) | |
842 (rng-previous-error-1 (- arg)) | |
843 (rng-next-error-1 arg)))) | |
844 | |
845 (defun rng-previous-error (arg) | |
846 "Go to the previous validation error before point. | |
847 Turn on `rng-validate-mode' if it is not already on. | |
96496
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
848 A prefix ARG specifies how many errors to move. |
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
849 A negative ARG moves forwards. Just \\[universal-argument] as a prefix |
86361 | 850 means goto the first error." |
851 (interactive "P") | |
852 (if (consp arg) | |
853 (rng-first-error) | |
854 (or rng-validate-mode (rng-validate-mode)) | |
855 (setq arg (prefix-numeric-value arg)) | |
856 (if (< arg 0) | |
857 (rng-next-error-1 (- arg)) | |
858 (rng-previous-error-1 arg)))) | |
859 | |
860 (defun rng-next-error-1 (arg) | |
861 (let* ((pos (point)) | |
862 err last-err) | |
863 (while (and (> arg 0) | |
864 (setq err (rng-find-next-error-overlay pos))) | |
865 (setq arg (1- arg)) | |
866 (setq last-err err) | |
867 (setq pos (overlay-start err))) | |
868 (when (> arg 0) | |
96496
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
869 (setq pos (max pos (1- rng-validate-up-to-date-end))) |
86361 | 870 (when (< rng-validate-up-to-date-end (point-max)) |
871 (message "Parsing...") | |
872 (while (let ((more-to-do (rng-do-some-validation))) | |
873 (while (and (> arg 0) | |
874 (setq err (rng-find-next-error-overlay pos))) | |
875 (setq arg (1- arg)) | |
876 (setq last-err err) | |
877 (setq pos (overlay-start err))) | |
878 (when (and (> arg 0) | |
879 more-to-do | |
880 (< rng-validate-up-to-date-end (point-max))) | |
881 ;; Display percentage validated. | |
882 (force-mode-line-update) | |
883 ;; Force redisplay but don't allow idle timers to run. | |
884 (let ((timer-idle-list nil)) | |
885 (sit-for 0)) | |
886 (setq pos | |
887 (max pos (1- rng-validate-up-to-date-end))) | |
888 t))))) | |
889 (if last-err | |
890 (rng-goto-error-overlay last-err) | |
891 (message "No more errors") | |
892 nil))) | |
893 | |
894 (defun rng-previous-error-1 (arg) | |
895 (let* ((pos (point)) | |
896 err last-err) | |
897 (while (and (> arg 0) | |
898 (setq err (rng-find-previous-error-overlay pos))) | |
899 (setq pos (overlay-start err)) | |
900 (setq last-err err) | |
901 (setq arg (1- arg))) | |
902 (when (and (> arg 0) | |
903 (< rng-validate-up-to-date-end (min pos (point-max)))) | |
904 (message "Parsing...") | |
905 (while (and (rng-do-some-validation) | |
906 (< rng-validate-up-to-date-end (min pos (point-max)))) | |
907 (force-mode-line-update) | |
908 ;; Force redisplay but don't allow idle timers to run. | |
909 (let ((timer-idle-list nil)) | |
910 (sit-for 0))) | |
911 (while (and (> arg 0) | |
912 (setq err (rng-find-previous-error-overlay pos))) | |
913 (setq pos (overlay-start err)) | |
914 (setq last-err err) | |
915 (setq arg (1- arg)))) | |
916 (if last-err | |
917 (rng-goto-error-overlay last-err) | |
918 (message "No previous errors") | |
919 nil))) | |
96496
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
920 |
86361 | 921 (defun rng-goto-error-overlay (err) |
922 "Goto the start of error overlay ERR and print its message." | |
923 (goto-char (overlay-start err)) | |
924 (setq rng-message-overlay-inhibit-point nil) | |
925 (rng-error-overlay-message err)) | |
926 | |
927 (defun rng-error-overlay-message (err) | |
928 (if err | |
929 (unless (or (and (eq rng-message-overlay-inhibit-point (point)) | |
930 (eq rng-message-overlay err)) | |
931 (= (point-max) 1)) | |
932 (message "%s" (overlay-get err 'help-echo)) | |
933 (setq rng-message-overlay-current t) | |
934 (setq rng-message-overlay-inhibit-point (point))) | |
935 (when (rng-current-message-from-error-overlay-p) | |
936 (message nil)) | |
937 (setq rng-message-overlay-inhibit-point nil)) | |
938 (setq rng-message-overlay err)) | |
939 | |
940 (defun rng-current-message-from-error-overlay-p () | |
941 (and rng-message-overlay-current | |
942 rng-message-overlay | |
943 (equal (overlay-get rng-message-overlay 'help-echo) | |
944 (current-message)))) | |
945 | |
946 (defun rng-find-next-error-overlay (pos) | |
947 "Return the overlay for the next error starting after POS. | |
948 Return nil if there is no such overlay or it is out of date. | |
949 Do not do any additional validation." | |
950 (when rng-error-count | |
951 (let (done found overlays) | |
952 (while (not done) | |
953 (cond (overlays | |
954 (let ((overlay (car overlays))) | |
955 (setq overlays (cdr overlays)) | |
956 (when (and (eq (overlay-get overlay 'category) 'rng-error) | |
957 ;; Is it the first? | |
958 (= (overlay-start overlay) pos) | |
959 ;; Is it up to date? | |
960 (<= (overlay-end overlay) | |
961 rng-validate-up-to-date-end)) | |
962 (setq done t) | |
963 (setq found overlay)))) | |
964 ((or (= pos (point-max)) | |
965 (> (setq pos (next-overlay-change pos)) | |
966 rng-validate-up-to-date-end)) | |
967 (setq done t)) | |
968 (t (setq overlays (overlays-in pos (1+ pos)))))) | |
969 found))) | |
970 | |
971 (defun rng-find-previous-error-overlay (pos) | |
972 "Return the overlay for the last error starting before POS. | |
973 Return nil if there is no such overlay or it is out of date. | |
974 Do not do any additional validation." | |
975 (when (and rng-error-count | |
976 (<= pos rng-validate-up-to-date-end)) | |
977 (let (done found overlays) | |
978 (while (not done) | |
979 (cond (overlays | |
980 (let ((overlay (car overlays))) | |
981 (setq overlays (cdr overlays)) | |
982 (when (and (eq (overlay-get overlay 'category) 'rng-error) | |
983 ;; Is it the first? | |
984 (= (overlay-start overlay) pos)) | |
985 (setq done t) | |
986 (setq found overlay)))) | |
987 ((= pos (point-min)) | |
988 (setq done t)) | |
989 (t | |
990 (setq pos (previous-overlay-change pos)) | |
991 (setq overlays (overlays-in pos (1+ pos)))))) | |
992 found))) | |
993 | |
994 ;;; Parsing | |
995 | |
996 (defun rng-forward (&optional limit) | |
997 "Move forward over one or more tokens updating the state. | |
998 If LIMIT is nil, stop after tags. | |
999 If LIMIT is non-nil, stop when end of last token parsed is >= LIMIT. | |
1000 Return nil at end of buffer, t otherwise." | |
1001 (let (type) | |
1002 (while (progn | |
1003 (setq type (xmltok-forward)) | |
1004 (rng-clear-overlays xmltok-start (point)) | |
1005 (let ((continue | |
1006 (cond ((eq type 'start-tag) | |
1007 (rng-process-start-tag 'start-tag) | |
1008 nil) | |
1009 ((eq type 'end-tag) | |
1010 (rng-process-end-tag) | |
1011 nil) | |
1012 ((eq type 'empty-element) | |
1013 (rng-process-start-tag 'empty-element) | |
1014 nil) | |
1015 ((eq type 'space) | |
1016 (rng-process-text xmltok-start nil t) | |
1017 t) | |
1018 ((eq type 'data) | |
1019 (rng-process-text xmltok-start nil nil) | |
1020 t) | |
1021 ((memq type '(entity-ref char-ref)) | |
1022 (cond (xmltok-replacement | |
1023 (rng-process-text xmltok-start | |
1024 nil | |
1025 'maybe | |
1026 xmltok-replacement)) | |
1027 ((eq type 'char-ref) | |
1028 (rng-process-unknown-char)) | |
1029 (t | |
1030 (rng-process-unknown-entity))) | |
1031 t) | |
1032 ((eq type 'cdata-section) | |
1033 (rng-process-text (+ xmltok-start 9) ; "<![CDATA[" | |
1034 (- (point) 3) ; "]]>" | |
1035 'maybe) | |
1036 t) | |
1037 ((eq type 'partial-start-tag) | |
1038 (rng-process-start-tag 'partial-start-tag) | |
1039 t) | |
1040 ((eq type 'partial-empty-element) | |
1041 (rng-process-start-tag 'empty-element) | |
1042 t) | |
1043 ((eq type 'partial-end-tag) | |
1044 (rng-process-end-tag 'partial) | |
1045 t) | |
1046 (t type)))) | |
1047 (if limit | |
1048 (< (point) limit) | |
1049 continue)))) | |
1050 (and type t))) | |
1051 | |
1052 (defun rng-process-start-tag (tag-type) | |
1053 "TAG-TYPE is `start-tag' for a start-tag, `empty-element' for | |
96496
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
1054 an empty element. `partial-empty-element' should be passed |
86361 | 1055 as empty-element." |
1056 (and rng-collecting-text (rng-flush-text)) | |
1057 (setq rng-collecting-text nil) | |
1058 (setq rng-pending-contents nil) | |
1059 (rng-process-namespaces) | |
1060 (let ((tag (rng-process-tag-name))) | |
1061 (rng-process-attributes) | |
1062 ;; set the state appropriately | |
1063 (cond ((eq tag-type 'empty-element) | |
1064 (rng-process-start-tag-close) | |
1065 ;; deal with missing content with empty element | |
1066 (when (not (rng-match-empty-content)) | |
1067 (rng-match-after) | |
1068 (rng-mark-start-tag-close "Empty content not allowed")) | |
1069 (nxml-ns-pop-state)) | |
1070 ((eq tag-type 'start-tag) | |
1071 (rng-process-start-tag-close) | |
1072 (setq rng-collecting-text (rng-match-text-typed-p)) | |
1073 (rng-push-tag tag)) | |
1074 ((eq tag-type 'partial-start-tag) | |
1075 (rng-process-start-tag-close) | |
1076 (rng-match-after) | |
1077 (nxml-ns-pop-state))))) | |
1078 | |
1079 (defun rng-process-namespaces () | |
1080 (let ((nsatts xmltok-namespace-attributes) | |
1081 prefixes) | |
1082 (nxml-ns-push-state) | |
1083 (while nsatts | |
1084 (let* ((att (car nsatts)) | |
1085 (value (xmltok-attribute-value att))) | |
1086 (when value | |
1087 (let ((ns (nxml-make-namespace value)) | |
1088 (prefix (and (xmltok-attribute-prefix att) | |
1089 (xmltok-attribute-local-name att)))) | |
1090 (cond ((member prefix prefixes) | |
1091 (rng-mark-invalid "Duplicate namespace declaration" | |
1092 (xmltok-attribute-name-start att) | |
1093 (xmltok-attribute-name-end att))) | |
1094 ((not prefix) | |
1095 (nxml-ns-set-default ns)) | |
1096 (ns | |
1097 (nxml-ns-set-prefix prefix ns)) | |
1098 (t | |
1099 ;; cannot have xmlns:foo="" | |
1100 (rng-mark-invalid "Namespace prefix cannot be undeclared" | |
1101 (1- (xmltok-attribute-value-start att)) | |
1102 (1+ (xmltok-attribute-value-end att))))) | |
1103 (setq prefixes (cons prefix prefixes))))) | |
1104 (setq nsatts (cdr nsatts))))) | |
1105 | |
1106 (defun rng-process-tag-name () | |
1107 (let* ((prefix (xmltok-start-tag-prefix)) | |
1108 (local-name (xmltok-start-tag-local-name)) | |
1109 (name | |
1110 (if prefix | |
1111 (let ((ns (nxml-ns-get-prefix prefix))) | |
1112 (cond (ns (cons ns local-name)) | |
1113 ((and (setq ns | |
1114 (rng-match-infer-start-tag-namespace | |
1115 local-name)) | |
1116 (rng-match-start-tag-open (cons ns local-name))) | |
1117 (nxml-ns-set-prefix prefix ns) | |
1118 (rng-mark-start-tag-close "Missing xmlns:%s=\"%s\"" | |
1119 prefix | |
1120 (nxml-namespace-name ns)) | |
1121 nil) | |
1122 (t | |
1123 (rng-recover-bad-element-prefix) | |
1124 nil))) | |
1125 (cons (nxml-ns-get-default) local-name)))) | |
1126 (when (and name | |
1127 (not (rng-match-start-tag-open name))) | |
1128 (unless (and (not (car name)) | |
1129 (let ((ns (rng-match-infer-start-tag-namespace (cdr name)))) | |
1130 (and ns | |
1131 (rng-match-start-tag-open (cons ns local-name)) | |
1132 (progn | |
1133 (nxml-ns-set-default ns) | |
1134 ;; XXX need to check we don't have xmlns="" | |
1135 (rng-mark-start-tag-close "Missing xmlns=\"%s\"" | |
1136 (nxml-namespace-name ns)) | |
1137 t)))) | |
1138 (rng-recover-start-tag-open name))) | |
1139 (cons prefix local-name))) | |
1140 | |
1141 (defun rng-process-attributes () | |
1142 (let ((atts xmltok-attributes) | |
1143 names) | |
1144 (while atts | |
1145 (let* ((att (car atts)) | |
1146 (prefix (xmltok-attribute-prefix att)) | |
1147 (local-name (xmltok-attribute-local-name att)) | |
1148 (name | |
1149 (if prefix | |
1150 (let ((ns (nxml-ns-get-prefix prefix))) | |
1151 (and ns | |
1152 (cons ns local-name))) | |
1153 (cons nil local-name)))) | |
1154 (cond ((not name) | |
1155 (rng-recover-bad-attribute-prefix att)) | |
1156 ((member name names) | |
1157 (rng-recover-duplicate-attribute-name att)) | |
1158 ((not (rng-match-attribute-name name)) | |
1159 (rng-recover-attribute-name att)) | |
1160 ((rng-match-text-typed-p) | |
1161 (let ((value (xmltok-attribute-value att))) | |
1162 (if value | |
1163 (or (rng-match-attribute-value value) | |
1164 (rng-recover-attribute-value att)) | |
1165 (rng-match-after)))) | |
1166 (t (or (rng-match-end-tag) | |
1167 (error "Internal error:\ | |
1168 invalid on untyped attribute value")))) | |
1169 (setq names (cons name names))) | |
1170 (setq atts (cdr atts))))) | |
1171 | |
1172 (defun rng-process-start-tag-close () | |
1173 ;; deal with missing attributes | |
1174 (unless (rng-match-start-tag-close) | |
1175 (rng-mark-start-tag-close (rng-missing-attributes-message)) | |
1176 (rng-match-ignore-attributes))) | |
1177 | |
1178 (defun rng-mark-start-tag-close (&rest args) | |
1179 (when (not (eq xmltok-type 'partial-start-tag)) | |
1180 (rng-mark-invalid (apply 'format args) | |
1181 (- (point) | |
1182 (if (eq xmltok-type 'empty-element) | |
1183 2 | |
1184 1)) | |
1185 (point)))) | |
1186 | |
1187 (defun rng-recover-bad-element-prefix () | |
1188 (rng-mark-invalid "Prefix not declared" | |
1189 (1+ xmltok-start) | |
1190 xmltok-name-colon) | |
1191 (rng-match-unknown-start-tag-open)) | |
1192 | |
1193 (defun rng-recover-bad-attribute-prefix (att) | |
1194 (rng-mark-invalid "Prefix not declared" | |
1195 (xmltok-attribute-name-start att) | |
1196 (xmltok-attribute-name-colon att))) | |
1197 | |
1198 (defun rng-recover-duplicate-attribute-name (att) | |
1199 (rng-mark-invalid "Duplicate attribute" | |
1200 (xmltok-attribute-name-start att) | |
1201 (xmltok-attribute-name-end att))) | |
1202 | |
1203 (defun rng-recover-start-tag-open (name) | |
1204 (let ((required (rng-match-required-element-name))) | |
1205 (cond ((and required | |
1206 (rng-match-start-tag-open required) | |
1207 (rng-match-after) | |
1208 (rng-match-start-tag-open name)) | |
1209 (rng-mark-invalid (concat "Missing element " | |
1210 (rng-quote-string | |
1211 (rng-name-to-string required))) | |
1212 xmltok-start | |
1213 (1+ xmltok-start))) | |
1214 ((and (rng-match-optionalize-elements) | |
1215 (rng-match-start-tag-open name)) | |
1216 (rng-mark-invalid "Required elements missing" | |
1217 xmltok-start | |
1218 (1+ xmltok-start))) | |
1219 ((rng-match-out-of-context-start-tag-open name) | |
1220 (rng-mark-invalid "Element not allowed in this context" | |
1221 (1+ xmltok-start) | |
1222 xmltok-name-end)) | |
1223 (t | |
1224 (rng-match-unknown-start-tag-open) | |
1225 (rng-mark-invalid "Unknown element" | |
1226 (1+ xmltok-start) | |
1227 xmltok-name-end))))) | |
1228 | |
1229 (defun rng-recover-attribute-value (att) | |
1230 (let ((start (xmltok-attribute-value-start att)) | |
1231 (end (xmltok-attribute-value-end att))) | |
1232 (if (= start end) | |
1233 (rng-mark-invalid "Empty attribute value invalid" start (1+ end)) | |
1234 (rng-mark-invalid "Attribute value invalid" start end))) | |
1235 (rng-match-after)) | |
1236 | |
1237 (defun rng-recover-attribute-name (att) | |
1238 (rng-mark-invalid "Attribute not allowed" | |
1239 (xmltok-attribute-name-start att) | |
1240 (xmltok-attribute-name-end att))) | |
1241 | |
1242 (defun rng-missing-attributes-message () | |
1243 (let ((required-attributes | |
1244 (rng-match-required-attribute-names))) | |
1245 (cond ((not required-attributes) | |
1246 "Required attributes missing") | |
1247 ((not (cdr required-attributes)) | |
1248 (concat "Missing attribute " | |
1249 (rng-quote-string | |
1250 (rng-name-to-string (car required-attributes) t)))) | |
1251 (t | |
1252 (concat "Missing attributes " | |
1253 (mapconcat (lambda (nm) | |
1254 (rng-quote-string | |
1255 (rng-name-to-string nm t))) | |
1256 required-attributes | |
1257 ", ")))))) | |
96496
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
1258 |
86361 | 1259 (defun rng-process-end-tag (&optional partial) |
1260 (cond ((not rng-open-elements) | |
1261 (rng-mark-not-well-formed "Extra end-tag" | |
1262 xmltok-start | |
1263 (point))) | |
1264 ((or partial | |
1265 (equal (cons (xmltok-end-tag-prefix) | |
1266 (xmltok-end-tag-local-name)) | |
1267 (car rng-open-elements))) | |
1268 (rng-end-element)) | |
1269 (t (rng-recover-mismatched-end-tag)))) | |
1270 | |
1271 (defun rng-end-element () | |
1272 (if rng-collecting-text | |
1273 (let ((contents (rng-contents-string))) | |
1274 (cond ((not contents) (rng-match-after)) | |
1275 ((not (rng-match-element-value contents)) | |
1276 (let* ((region (rng-contents-region))) | |
1277 (if (not region) | |
1278 (rng-mark-invalid "Empty content not allowed" | |
1279 xmltok-start | |
1280 (+ xmltok-start 2)) | |
1281 (rng-mark-invalid "Invalid data" | |
1282 (car region) | |
1283 (cdr region)))) | |
1284 (rng-match-after))) | |
1285 (setq rng-collecting-text nil) | |
1286 (setq rng-pending-contents nil)) | |
1287 (unless (rng-match-end-tag) | |
1288 (rng-mark-invalid (rng-missing-element-message) | |
1289 xmltok-start | |
1290 (+ xmltok-start 2)) | |
1291 (rng-match-after))) | |
1292 (nxml-ns-pop-state) | |
1293 (when (eq (car rng-open-elements) t) | |
1294 (rng-pop-tag)) | |
1295 (rng-pop-tag)) | |
1296 | |
1297 (defun rng-missing-element-message () | |
1298 (let ((element (rng-match-required-element-name))) | |
1299 (if element | |
1300 (concat "Missing element " | |
1301 (rng-quote-string (rng-name-to-string element))) | |
1302 "Required child elements missing"))) | |
1303 | |
1304 (defun rng-recover-mismatched-end-tag () | |
1305 (let* ((name (cons (xmltok-end-tag-prefix) | |
1306 (xmltok-end-tag-local-name)))) | |
1307 (cond ((member name (cdr rng-open-elements)) | |
1308 (let* ((suppress-error (eq (car rng-open-elements) t)) | |
1309 missing top) | |
1310 (while (progn | |
1311 (setq top (car rng-open-elements)) | |
1312 (rng-pop-tag) | |
1313 (unless (eq top t) | |
1314 (setq missing (cons top missing)) | |
1315 (nxml-ns-pop-state) | |
1316 (rng-match-after)) | |
1317 (not (equal top name)))) | |
1318 (unless suppress-error | |
1319 (rng-mark-missing-end-tags (cdr missing))))) | |
1320 ((rng-match-empty-before-p) | |
1321 (rng-mark-mismatched-end-tag) | |
1322 (rng-end-element)) | |
1323 (t (rng-mark-mismatched-end-tag) | |
1324 (setq rng-open-elements | |
1325 (cons t rng-open-elements)))))) | |
1326 | |
1327 (defun rng-mark-missing-end-tags (missing) | |
1328 (rng-mark-not-well-formed | |
1329 (format "Missing end-tag%s %s" | |
1330 (if (null (cdr missing)) "" "s") | |
1331 (mapconcat (lambda (name) | |
1332 (rng-quote-string | |
1333 (if (car name) | |
1334 (concat (car name) | |
1335 ":" | |
1336 (cdr name)) | |
1337 (cdr name)))) | |
1338 missing | |
1339 ", ")) | |
1340 xmltok-start | |
1341 (+ xmltok-start 2))) | |
1342 | |
1343 (defun rng-mark-mismatched-end-tag () | |
1344 (rng-mark-not-well-formed "Mismatched end-tag" | |
1345 (+ xmltok-start 2) | |
1346 xmltok-name-end)) | |
1347 | |
1348 (defun rng-push-tag (prefix-local-name) | |
1349 (setq rng-open-elements | |
1350 (cons prefix-local-name rng-open-elements))) | |
1351 | |
1352 (defun rng-pop-tag () | |
1353 (setq rng-open-elements (cdr rng-open-elements))) | |
1354 | |
1355 (defun rng-contents-string () | |
1356 (let ((contents rng-pending-contents)) | |
1357 (cond ((not contents) "") | |
1358 ((memq nil contents) nil) | |
1359 ((not (cdr contents)) | |
1360 (rng-segment-string (car contents))) | |
1361 (t (apply 'concat | |
1362 (nreverse (mapcar 'rng-segment-string | |
1363 contents))))))) | |
1364 | |
1365 (defun rng-segment-string (segment) | |
1366 (or (car segment) | |
1367 (apply 'buffer-substring-no-properties | |
1368 (cdr segment)))) | |
1369 | |
1370 (defun rng-segment-blank-p (segment) | |
1371 (if (car segment) | |
1372 (rng-blank-p (car segment)) | |
1373 (apply 'rng-region-blank-p | |
1374 (cdr segment)))) | |
1375 | |
1376 (defun rng-contents-region () | |
1377 (if (null rng-pending-contents) | |
1378 nil | |
1379 (let* ((contents rng-pending-contents) | |
1380 (head (cdar contents)) | |
1381 (start (car head)) | |
1382 (end (cadr head))) | |
1383 (while (setq contents (cdr contents)) | |
1384 (setq start (car (cdar contents)))) | |
1385 (cons start end)))) | |
1386 | |
1387 (defun rng-process-text (start end whitespace &optional value) | |
1388 "Process characters between position START and END as text. | |
96496
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
1389 END nil means point. WHITESPACE t means known to be whitespace, nil |
86361 | 1390 means known not to be, anything else means unknown whether whitespace |
96496
e374c747704b
Fix typos, and general docstring cleanup.
Juanma Barranquero <lekktu@gmail.com>
parents:
96361
diff
changeset
|
1391 or not. END must not be nil if WHITESPACE is neither t nor nil. |
86361 | 1392 VALUE is a string or nil; nil means the value is equal to the |
1393 string between START and END." | |
1394 (cond (rng-collecting-text | |
1395 (setq rng-pending-contents (cons (list value start (or end (point))) | |
1396 rng-pending-contents))) | |
1397 ((not (or (and whitespace | |
1398 (or (eq whitespace t) | |
1399 (if value | |
1400 (rng-blank-p value) | |
1401 (rng-region-blank-p start end)))) | |
1402 (rng-match-mixed-text))) | |
1403 (rng-mark-invalid "Text not allowed" start (or end (point)))))) | |
1404 | |
1405 (defun rng-process-unknown-char () | |
1406 (when rng-collecting-text | |
1407 (setq rng-pending-contents | |
1408 (cons nil rng-pending-contents)))) | |
1409 | |
1410 (defun rng-process-unknown-entity () | |
1411 (rng-process-unknown-char) | |
1412 (rng-match-optionalize-elements)) | |
1413 | |
1414 (defun rng-region-blank-p (beg end) | |
1415 (save-excursion | |
1416 (goto-char beg) | |
1417 (= (skip-chars-forward " \n\r\t" end) | |
1418 (- end beg)))) | |
1419 | |
1420 (defun rng-flush-text () | |
1421 (while rng-pending-contents | |
1422 (let ((segment (car rng-pending-contents))) | |
1423 (unless (or (rng-segment-blank-p segment) | |
1424 (rng-match-mixed-text)) | |
1425 (let ((region (cdr segment))) | |
1426 (rng-mark-invalid "In this context text cannot be mixed with elements" | |
1427 (car region) | |
1428 (cadr region))))) | |
1429 (setq rng-pending-contents (cdr rng-pending-contents)))) | |
1430 | |
1431 (defun rng-process-end-document () | |
1432 ;; this is necessary to clear empty overlays at (point-max) | |
1433 (rng-clear-overlays (point) (point)) | |
1434 (let ((start (save-excursion | |
1435 (skip-chars-backward " \t\r\n") | |
1436 (point)))) | |
1437 (cond (rng-open-elements | |
1438 (unless (eq (car rng-open-elements) t) | |
1439 (rng-mark-not-well-formed "Missing end-tag" | |
1440 start | |
1441 (point)))) | |
1442 ((not (rng-match-nullable-p)) | |
1443 (rng-mark-not-well-formed "No document element" | |
1444 start | |
1445 (point)))))) | |
1446 | |
1447 (defun rng-process-encoding-name (beg end) | |
1448 (unless (let ((charset (buffer-substring-no-properties beg end))) | |
1449 (or (nxml-mime-charset-coding-system charset) | |
1450 (string= (downcase charset) "utf-16"))) | |
1451 (rng-mark-not-well-formed "Unsupported encoding" beg end))) | |
1452 | |
1453 (defun rng-name-to-string (name &optional attributep) | |
1454 (let ((ns (car name)) | |
1455 (local-name (cdr name))) | |
1456 (if (or (not ns) | |
1457 (and (not attributep) | |
1458 (eq (nxml-ns-get-default) ns))) | |
1459 local-name | |
1460 (let ((prefix (nxml-ns-prefix-for ns))) | |
1461 (if prefix | |
1462 (concat prefix ":" local-name) | |
1463 (concat "{" (symbol-name ns) "}" local-name)))))) | |
1464 | |
1465 (provide 'rng-valid) | |
1466 | |
86379 | 1467 ;; arch-tag: 7dd846d3-519d-4a6d-8107-4ff0024a60ef |
86361 | 1468 ;;; rng-valid.el ends here |