Merge from trunk and resolve conflicts.
[bpt/emacs.git] / lisp / nxml / rng-valid.el
1 ;;; rng-valid.el --- real-time validation of XML using RELAX NG
2
3 ;; Copyright (C) 2003, 2007-2013 Free Software Foundation, Inc.
4
5 ;; Author: James Clark
6 ;; Keywords: XML, RelaxNG
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
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
108 (defface rng-error '((t (:inherit font-lock-warning-face)))
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
151 "Number of errors in the current buffer.
152 Always equal to number of overlays with category `rng-error'.")
153 (make-variable-buffer-local 'rng-error-count)
154
155 (defvar rng-message-overlay nil
156 "Overlay in this buffer whose `help-echo' property was last printed.
157 It is nil if none.")
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.
192 It is nil if there is no conditionally up-to-date region. The
193 conditionally up-to-date region must be such that for any cached
194 state S with position P in the conditionally up-to-date region,
195 if at some point it is determined that S becomes correct for P,
196 then all states with position >= P in the conditionally up to
197 date region must also then be correct and all errors between P
198 and the end of the region must then be correctly marked.")
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.
203 It is nil if there is no conditionally up-to-date region.
204 See the variable `rng-conditional-up-to-date-start'.")
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
223 RELAX NG schema. The mode-line indicates whether it is or not. Any
224 parts of the buffer that cause it not to be are considered errors and
225 are highlighted with face `rng-error'. A description of each error is
226 available as a tooltip. \\[rng-next-error] goes to the next error
227 after point. Clicking mouse-1 on the word `Invalid' in the mode-line
228 goes to the first error in the buffer. If the buffer changes, then it
229 will be automatically rechecked when Emacs becomes idle; the
230 rechecking will be paused whenever there is input pending.
231
232 By default, uses a vacuous schema that allows any well-formed XML
233 document. A schema can be specified explicitly using
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 (with-silent-modifications
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 (point-min)))
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))))
298
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 (setq rng-message-overlay-inhibit-point nil)
308 (with-silent-modifications
309 (rng-clear-cached-state start end))
310 ;; rng-validate-up-to-date-end holds the position before the change
311 ;; Adjust it to reflect the change.
312 (if (< start rng-validate-up-to-date-end)
313 (setq rng-validate-up-to-date-end
314 (if (<= (+ start pre-change-len) rng-validate-up-to-date-end)
315 (+ rng-validate-up-to-date-end
316 (- end start pre-change-len))
317 start)))
318 ;; Adjust the conditional zone
319 (cond (rng-conditional-up-to-date-start
320 (when (< rng-conditional-up-to-date-start end)
321 (if (< end rng-conditional-up-to-date-end)
322 (set-marker rng-conditional-up-to-date-start end)
323 (rng-clear-conditional-region))))
324 ((< end rng-validate-up-to-date-end)
325 (setq rng-conditional-up-to-date-end
326 (copy-marker rng-validate-up-to-date-end nil))
327 (setq rng-conditional-up-to-date-start
328 (copy-marker end t))))
329 ;; Adjust rng-validate-up-to-date-end
330 (if (< start rng-validate-up-to-date-end)
331 (setq rng-validate-up-to-date-end start))
332 ;; Must make rng-validate-up-to-date-end < point-max
333 ;; (unless the buffer is empty).
334 ;; otherwise rng-validate-prepare will say there's nothing to do.
335 (when (>= rng-validate-up-to-date-end (point-max))
336 (setq rng-validate-up-to-date-end
337 (if (< (point-min) (point-max))
338 (1- (point-max))
339 ;; Only widen if really necessary.
340 (save-restriction (widen) (max (point-min) (1- (point-max)))))))
341 ;; Arrange to revalidate
342 (rng-activate-timers)
343 ;; Need to do this after activating the timer
344 (force-mode-line-update))
345
346 (defun rng-compute-mode-line-string ()
347 (cond (rng-validate-timer
348 (concat " Validated:"
349 (number-to-string
350 ;; Use floor rather than round because we want
351 ;; to show 99% rather than 100% for changes near
352 ;; the end.
353 (floor (if (eq (buffer-size) 0)
354 0.0
355 (/ (* (- rng-validate-up-to-date-end (point-min))
356 100.0)
357 (- (point-max) (point-min))))))
358 "%%"))
359 ((> rng-error-count 0)
360 (concat " "
361 (propertize "Invalid"
362 'help-echo "mouse-1: go to first error"
363 'local-map (make-mode-line-mouse-map
364 'mouse-1
365 'rng-mouse-first-error))))
366 (t " Valid")))
367
368 (defun rng-cancel-timers ()
369 (let ((inhibit-quit t))
370 (when rng-validate-timer
371 (cancel-timer rng-validate-timer)
372 (setq rng-validate-timer nil))
373 (when rng-validate-quick-timer
374 (cancel-timer rng-validate-quick-timer)
375 (setq rng-validate-quick-timer nil))))
376
377 (defun rng-kill-timers ()
378 ;; rng-validate-timer and rng-validate-quick-timer have the
379 ;; permanent-local property, so that the timers can be
380 ;; canceled even after changing mode.
381 ;; This function takes care of canceling the timers and
382 ;; then killing the local variables.
383 (when (local-variable-p 'rng-validate-timer)
384 (when rng-validate-timer
385 (cancel-timer rng-validate-timer))
386 (kill-local-variable 'rng-validate-timer))
387 (when (local-variable-p 'rng-validate-quick-timer)
388 (when rng-validate-quick-timer
389 (cancel-timer rng-validate-quick-timer))
390 (kill-local-variable 'rng-validate-quick-timer)))
391
392 (defun rng-activate-timers ()
393 (unless rng-validate-timer
394 (let ((inhibit-quit t))
395 (setq rng-validate-timer
396 (run-with-idle-timer rng-validate-delay
397 t
398 'rng-validate-while-idle
399 (current-buffer)))
400 (setq rng-validate-quick-timer
401 (run-with-idle-timer rng-validate-quick-delay
402 t
403 'rng-validate-quick-while-idle
404 (current-buffer))))))
405
406 (defun rng-validate-clear ()
407 (rng-validate-mode 1 t))
408
409 ;; These two variables are dynamically bound and used
410 ;; to pass information between rng-validate-while-idle
411 ;; and rng-validate-while-idle-continue-p.
412
413 (defvar rng-validate-display-point nil)
414 (defvar rng-validate-display-modified-p nil)
415
416 (defun rng-validate-while-idle-continue-p ()
417 (and (not (input-pending-p))
418 ;; Fake rng-validate-up-to-date-end so that the mode line
419 ;; shows progress. Also use this to save point.
420 (let ((rng-validate-up-to-date-end (point)))
421 (goto-char rng-validate-display-point)
422 (when (not rng-validate-display-modified-p)
423 (restore-buffer-modified-p nil))
424 (force-mode-line-update)
425 (let ((continue (sit-for 0)))
426 (goto-char rng-validate-up-to-date-end)
427 continue))))
428
429 ;; Calling rng-do-some-validation once with a continue-p function, as
430 ;; opposed to calling it repeatedly, helps on initial validation of a
431 ;; large buffer with lots of errors. The overlays for errors will all
432 ;; get added when rng-do-some-validation returns and won't slow the
433 ;; validation process down.
434
435 (defun rng-validate-while-idle (buffer)
436 (when (buffer-live-p buffer) ; bug#13999
437 (with-current-buffer buffer
438 (if rng-validate-mode
439 (if (let ((rng-validate-display-point (point))
440 (rng-validate-display-modified-p (buffer-modified-p)))
441 (rng-do-some-validation 'rng-validate-while-idle-continue-p))
442 (force-mode-line-update)
443 (rng-validate-done))
444 ;; must have done kill-all-local-variables
445 (rng-kill-timers)))))
446
447 (defun rng-validate-quick-while-idle (buffer)
448 (when (buffer-live-p buffer) ; bug#13999
449 (with-current-buffer buffer
450 (if rng-validate-mode
451 (if (rng-do-some-validation)
452 (force-mode-line-update)
453 (rng-validate-done))
454 ;; must have done kill-all-local-variables
455 (rng-kill-timers)))))
456
457 (defun rng-validate-done ()
458 (when (or (not (current-message))
459 (rng-current-message-from-error-overlay-p))
460 (rng-error-overlay-message (or (rng-error-overlay-after (point))
461 (rng-error-overlay-after (1- (point))))))
462 (rng-cancel-timers)
463 (force-mode-line-update))
464
465 (defun rng-do-some-validation (&optional continue-p-function)
466 "Do some validation work. Return t if more to do, nil otherwise."
467 (save-excursion
468 (save-restriction
469 (widen)
470 (nxml-with-invisible-motion
471 (condition-case-unless-debug err
472 (and (rng-validate-prepare)
473 (let ((rng-dt-namespace-context-getter '(nxml-ns-get-context)))
474 (with-silent-modifications
475 (rng-do-some-validation-1 continue-p-function))))
476 ;; errors signaled from a function run by an idle timer
477 ;; are ignored; if we don't catch them, validation
478 ;; will get mysteriously stuck at a single place
479 (rng-compile-error
480 (message "Incorrect schema. %s" (nth 1 err))
481 (rng-validate-mode 0)
482 nil)
483 (error
484 (message "Internal error in rng-validate-mode triggered at buffer position %d. %s"
485 (point)
486 (error-message-string err))
487 (rng-validate-mode 0)
488 nil))))))
489
490 (defun rng-validate-prepare ()
491 "Prepare to do some validation, initializing point and the state.
492 Return t if there is work to do, nil otherwise."
493 (cond ((= rng-validate-up-to-date-end (point-min))
494 (rng-set-initial-state)
495 t)
496 ((= rng-validate-up-to-date-end (point-max))
497 nil)
498 (t (let ((state (get-text-property (1- rng-validate-up-to-date-end)
499 'rng-state)))
500 (cond (state
501 (rng-restore-state state)
502 (goto-char rng-validate-up-to-date-end))
503 (t
504 (let ((pos (previous-single-property-change
505 rng-validate-up-to-date-end
506 'rng-state)))
507 (cond (pos
508 (rng-restore-state
509 (or (get-text-property (1- pos) 'rng-state)
510 (error "Internal error: state null")))
511 (goto-char pos))
512 (t (rng-set-initial-state))))))))))
513
514 (defun rng-dtd-trivial-p (dtd)
515 "Check whether the current dtd is different from the trivial default."
516 (or (null dtd) (eq dtd xmltok-predefined-entity-alist)))
517
518 (defun rng-do-some-validation-1 (&optional continue-p-function)
519 (let ((limit (+ rng-validate-up-to-date-end
520 rng-validate-chunk-size))
521 (remove-start rng-validate-up-to-date-end)
522 (next-cache-point (+ (point) rng-state-cache-distance))
523 (continue t)
524 (xmltok-dtd rng-dtd)
525 have-remaining-chars
526 xmltok-type
527 xmltok-start
528 xmltok-name-colon
529 xmltok-name-end
530 xmltok-replacement
531 xmltok-attributes
532 xmltok-namespace-attributes
533 xmltok-dependent-regions
534 xmltok-errors)
535 (when (= (point) 1)
536 (let ((regions (xmltok-forward-prolog)))
537 (rng-clear-overlays 1 (point))
538 (while regions
539 (when (eq (aref (car regions) 0) 'encoding-name)
540 (rng-process-encoding-name (aref (car regions) 1)
541 (aref (car regions) 2)))
542 (setq regions (cdr regions))))
543 (unless (equal rng-dtd xmltok-dtd)
544 (rng-clear-conditional-region))
545 (setq rng-dtd xmltok-dtd))
546 (while continue
547 (setq have-remaining-chars (rng-forward))
548 (let ((pos (point)))
549 (setq continue
550 (and have-remaining-chars
551 (or (< pos limit)
552 (and continue-p-function
553 (funcall continue-p-function)
554 (setq limit (+ limit rng-validate-chunk-size))
555 t))))
556 (cond ((and rng-conditional-up-to-date-start
557 ;; > because we are getting the state from (1- pos)
558 (> pos rng-conditional-up-to-date-start)
559 (< pos rng-conditional-up-to-date-end)
560 (rng-state-matches-current (get-text-property (1- pos)
561 'rng-state)))
562 (when (< remove-start (1- pos))
563 (rng-clear-cached-state remove-start (1- pos)))
564 ;; sync up with cached validation state
565 (setq continue nil)
566 ;; do this before setting rng-validate-up-to-date-end
567 ;; in case we get a quit
568 (rng-mark-xmltok-errors)
569 (rng-mark-xmltok-dependent-regions)
570 (setq rng-validate-up-to-date-end
571 (marker-position rng-conditional-up-to-date-end))
572 (rng-clear-conditional-region)
573 (setq have-remaining-chars
574 (< rng-validate-up-to-date-end (point-max))))
575 ((or (>= pos next-cache-point)
576 (not continue))
577 (setq next-cache-point (+ pos rng-state-cache-distance))
578 (rng-clear-cached-state remove-start pos)
579 (when have-remaining-chars
580 (rng-cache-state (1- pos)))
581 (setq remove-start pos)
582 (unless continue
583 ;; if we have just blank chars skip to the end
584 (when have-remaining-chars
585 (skip-chars-forward " \t\r\n")
586 (when (= (point) (point-max))
587 (rng-clear-overlays pos (point))
588 (rng-clear-cached-state pos (point))
589 (setq have-remaining-chars nil)
590 (setq pos (point))))
591 (when (not have-remaining-chars)
592 (rng-process-end-document))
593 (rng-mark-xmltok-errors)
594 (rng-mark-xmltok-dependent-regions)
595 (setq rng-validate-up-to-date-end pos)
596 (when rng-conditional-up-to-date-end
597 (cond ((<= rng-conditional-up-to-date-end pos)
598 (rng-clear-conditional-region))
599 ((< rng-conditional-up-to-date-start pos)
600 (set-marker rng-conditional-up-to-date-start
601 pos)))))))))
602 have-remaining-chars))
603
604 (defun rng-clear-conditional-region ()
605 (when rng-conditional-up-to-date-start
606 (set-marker rng-conditional-up-to-date-start nil)
607 (setq rng-conditional-up-to-date-start nil))
608 (when rng-conditional-up-to-date-end
609 (set-marker rng-conditional-up-to-date-end nil)
610 (setq rng-conditional-up-to-date-end nil)))
611
612 (defun rng-clear-cached-state (start end)
613 "Clear cached state between START and END."
614 (remove-text-properties start end '(rng-state nil)))
615
616 (defun rng-cache-state (pos)
617 "Save the current state in a text property on the character at pos."
618 (put-text-property pos
619 (1+ pos)
620 'rng-state
621 (rng-get-state)))
622
623 (defun rng-state-matches-current (state)
624 (and state
625 (rng-match-state-equal (car state))
626 (nxml-ns-state-equal (nth 1 state))
627 (equal (nth 2 state) rng-open-elements)))
628
629 (defun rng-get-state ()
630 (list (rng-match-state)
631 (nxml-ns-state)
632 rng-open-elements))
633
634 (defun rng-restore-state (state)
635 (rng-set-match-state (car state))
636 (setq state (cdr state))
637 (nxml-ns-set-state (car state))
638 (setq rng-open-elements (cadr state))
639 (setq rng-pending-contents nil)
640 (setq rng-collecting-text (rng-match-text-typed-p)))
641
642 (defun rng-set-initial-state ()
643 (nxml-ns-init)
644 (rng-match-start-document)
645 (setq rng-open-elements nil)
646 (setq rng-pending-contents nil)
647 (goto-char (point-min)))
648
649 (defun rng-clear-overlays (beg end)
650 (unless rng-parsing-for-state
651 (let ((overlays (overlays-in beg end)))
652 (while overlays
653 (let* ((overlay (car overlays))
654 (category (overlay-get overlay 'category)))
655 (cond ((eq category 'rng-error)
656 (let ((inhibit-quit t))
657 (when (eq overlay rng-message-overlay)
658 (rng-error-overlay-message nil))
659 (delete-overlay overlay)
660 ;; rng-error-count could be nil
661 ;; if overlays left over from a previous use
662 ;; of rng-validate-mode that ended with a change of mode
663 (when rng-error-count
664 (setq rng-error-count (1- rng-error-count)))))
665 ((and (eq category 'rng-dependent)
666 (<= beg (overlay-start overlay)))
667 (delete-overlay overlay))))
668 (setq overlays (cdr overlays))))))
669
670 ;;; Dependent regions
671
672 (defun rng-mark-xmltok-dependent-regions ()
673 (while xmltok-dependent-regions
674 (apply 'rng-mark-xmltok-dependent-region
675 (car xmltok-dependent-regions))
676 (setq xmltok-dependent-regions
677 (cdr xmltok-dependent-regions))))
678
679 (defun rng-mark-xmltok-dependent-region (fun start end &rest args)
680 (let ((overlay (make-overlay start end nil t t)))
681 (overlay-put overlay 'category 'rng-dependent)
682 (overlay-put overlay 'rng-funargs (cons fun args))))
683
684 (put 'rng-dependent 'evaporate t)
685 (put 'rng-dependent 'modification-hooks '(rng-dependent-region-changed))
686 (put 'rng-dependent 'insert-behind-hooks '(rng-dependent-region-changed))
687
688 (defun rng-dependent-region-changed (overlay
689 after-p
690 change-start
691 change-end
692 &optional pre-change-length)
693 (when (and after-p
694 ;; Emacs sometimes appears to call deleted overlays
695 (overlay-start overlay)
696 (let ((funargs (overlay-get overlay 'rng-funargs)))
697 (save-match-data
698 (save-excursion
699 (save-restriction
700 (widen)
701 (apply (car funargs)
702 (append (list change-start
703 change-end
704 pre-change-length
705 (overlay-start overlay)
706 (overlay-end overlay))
707 (cdr funargs))))))))
708 (rng-after-change-function (overlay-start overlay)
709 change-end
710 (+ pre-change-length
711 (- (overlay-start overlay)
712 change-start)))
713 (delete-overlay overlay)))
714
715 ;;; Error state
716
717 (defun rng-mark-xmltok-errors ()
718 (while xmltok-errors
719 (let ((err (car xmltok-errors)))
720 (rng-mark-not-well-formed (xmltok-error-message err)
721 (xmltok-error-start err)
722 (xmltok-error-end err)))
723 (setq xmltok-errors (cdr xmltok-errors))))
724
725 (defun rng-mark-invalid (message beg end)
726 (rng-mark-error message beg end))
727
728 (defun rng-mark-not-well-formed (message beg end)
729 ;; Don't try to validate further
730 ;;(rng-set-match-state rng-not-allowed-ipattern)
731 (rng-mark-error message beg end))
732
733 (defun rng-mark-error (message beg end)
734 (unless rng-parsing-for-state
735 (let ((overlays (overlays-in beg end)))
736 (while (and overlays message)
737 (let ((o (car overlays)))
738 (when (and (eq (overlay-get o 'category) 'rng-error)
739 (= (overlay-start o) beg)
740 (= (overlay-end o) end))
741 (overlay-put o
742 'help-echo
743 (concat (overlay-get o 'help-echo)
744 "\n"
745 message))
746 (setq message nil)))
747 (setq overlays (cdr overlays))))
748 (when message
749 (let ((inhibit-quit t))
750 (setq rng-error-count (1+ rng-error-count))
751 (let ((overlay
752 (make-overlay beg end nil t
753 ;; Need to make the rear delimiter advance
754 ;; with the front delimiter when the overlay
755 ;; is empty, otherwise the front delimiter
756 ;; will move past the rear delimiter.
757 (= beg end))))
758 ;; Ensure when we have two overlapping messages, the help-echo
759 ;; of the one that starts first is shown
760 (overlay-put overlay 'priority beg)
761 (overlay-put overlay 'category 'rng-error)
762 (overlay-put overlay 'help-echo message))))))
763
764 (put 'rng-error 'face 'rng-error)
765 (put 'rng-error 'modification-hooks '(rng-error-modified))
766
767 ;; If we don't do this, then the front delimiter can move
768 ;; past the end delimiter.
769 (defun rng-error-modified (overlay after-p beg end &optional pre-change-len)
770 (when (and after-p
771 (overlay-start overlay) ; check not deleted
772 (>= (overlay-start overlay)
773 (overlay-end overlay)))
774 (let ((inhibit-quit t))
775 (delete-overlay overlay)
776 (setq rng-error-count (1- rng-error-count)))))
777
778 (defun rng-echo-area-clear-function ()
779 (setq rng-message-overlay-current nil))
780
781 ;;; Error navigation
782
783 (defun rng-maybe-echo-error-at-point ()
784 (when (or (not (current-message))
785 (rng-current-message-from-error-overlay-p))
786 (rng-error-overlay-message (rng-error-overlay-after (point)))))
787
788 (defun rng-error-overlay-after (pos)
789 (let ((overlays (overlays-in pos (1+ pos)))
790 (best nil))
791 (while overlays
792 (let ((overlay (car overlays)))
793 (when (and (eq (overlay-get overlay 'category)
794 'rng-error)
795 (or (not best)
796 (< (overlay-start best)
797 (overlay-start overlay))))
798 (setq best overlay)))
799 (setq overlays (cdr overlays)))
800 best))
801
802 (defun rng-first-error ()
803 "Go to the first validation error.
804 Turn on `rng-validate-mode' if it is not already on."
805 (interactive)
806 (or rng-validate-mode (rng-validate-mode))
807 (rng-do-some-validation)
808 (let ((err (rng-find-next-error-overlay (1- (point-min)))))
809 (if err
810 (rng-goto-error-overlay err)
811 (let ((pos (save-excursion
812 (goto-char (point-min))
813 (rng-next-error 1))))
814 (when pos
815 (goto-char pos))))))
816
817 (defun rng-mouse-first-error (event)
818 "Go to the first validation error from a mouse click."
819 (interactive "e")
820 (select-window (posn-window (event-start event)))
821 (rng-first-error))
822
823 (defun rng-next-error (arg)
824 "Go to the next validation error after point.
825 Turn on `rng-validate-mode' if it is not already on.
826 A prefix ARG specifies how many errors to move.
827 A negative ARG moves backwards. Just \\[universal-argument] as a prefix
828 means goto the first error."
829 (interactive "P")
830 (if (consp arg)
831 (rng-first-error)
832 (or rng-validate-mode (rng-validate-mode))
833 (setq arg (prefix-numeric-value arg))
834 (if (< arg 0)
835 (rng-previous-error-1 (- arg))
836 (rng-next-error-1 arg))))
837
838 (defun rng-previous-error (arg)
839 "Go to the previous validation error before point.
840 Turn on `rng-validate-mode' if it is not already on.
841 A prefix ARG specifies how many errors to move.
842 A negative ARG moves forwards. Just \\[universal-argument] as a prefix
843 means goto the first error."
844 (interactive "P")
845 (if (consp arg)
846 (rng-first-error)
847 (or rng-validate-mode (rng-validate-mode))
848 (setq arg (prefix-numeric-value arg))
849 (if (< arg 0)
850 (rng-next-error-1 (- arg))
851 (rng-previous-error-1 arg))))
852
853 (defun rng-next-error-1 (arg)
854 (let* ((pos (point))
855 err last-err)
856 (while (and (> arg 0)
857 (setq err (rng-find-next-error-overlay pos)))
858 (setq arg (1- arg))
859 (setq last-err err)
860 (setq pos (overlay-start err)))
861 (when (> arg 0)
862 (setq pos (max pos (1- rng-validate-up-to-date-end)))
863 (when (< rng-validate-up-to-date-end (point-max))
864 (message "Parsing...")
865 (while (let ((more-to-do (rng-do-some-validation)))
866 (while (and (> arg 0)
867 (setq err (rng-find-next-error-overlay pos)))
868 (setq arg (1- arg))
869 (setq last-err err)
870 (setq pos (overlay-start err)))
871 (when (and (> arg 0)
872 more-to-do
873 (< rng-validate-up-to-date-end (point-max)))
874 ;; Display percentage validated.
875 (force-mode-line-update)
876 (sit-for 0)
877 (setq pos
878 (max pos (1- rng-validate-up-to-date-end)))
879 t)))))
880 (if last-err
881 (rng-goto-error-overlay last-err)
882 (message "No more errors")
883 nil)))
884
885 (defun rng-previous-error-1 (arg)
886 (let* ((pos (point))
887 err last-err)
888 (while (and (> arg 0)
889 (setq err (rng-find-previous-error-overlay pos)))
890 (setq pos (overlay-start err))
891 (setq last-err err)
892 (setq arg (1- arg)))
893 (when (and (> arg 0)
894 (< rng-validate-up-to-date-end (min pos (point-max))))
895 (message "Parsing...")
896 (while (and (rng-do-some-validation)
897 (< rng-validate-up-to-date-end (min pos (point-max))))
898 (force-mode-line-update)
899 (sit-for 0))
900 (while (and (> arg 0)
901 (setq err (rng-find-previous-error-overlay pos)))
902 (setq pos (overlay-start err))
903 (setq last-err err)
904 (setq arg (1- arg))))
905 (if last-err
906 (rng-goto-error-overlay last-err)
907 (message "No previous errors")
908 nil)))
909
910 (defun rng-goto-error-overlay (err)
911 "Goto the start of error overlay ERR and print its message."
912 (goto-char (overlay-start err))
913 (setq rng-message-overlay-inhibit-point nil)
914 (rng-error-overlay-message err))
915
916 (defun rng-error-overlay-message (err)
917 (if err
918 (unless (or (and (eq rng-message-overlay-inhibit-point (point))
919 (eq rng-message-overlay err))
920 (= (point-max) 1))
921 (message "%s" (overlay-get err 'help-echo))
922 (setq rng-message-overlay-current t)
923 (setq rng-message-overlay-inhibit-point (point)))
924 (when (rng-current-message-from-error-overlay-p)
925 (message nil))
926 (setq rng-message-overlay-inhibit-point nil))
927 (setq rng-message-overlay err))
928
929 (defun rng-current-message-from-error-overlay-p ()
930 (and rng-message-overlay-current
931 rng-message-overlay
932 (equal (overlay-get rng-message-overlay 'help-echo)
933 (current-message))))
934
935 (defun rng-find-next-error-overlay (pos)
936 "Return the overlay for the next error starting after POS.
937 Return nil if there is no such overlay or it is out of date.
938 Do not do any additional validation."
939 (when rng-error-count
940 (let (done found overlays)
941 (while (not done)
942 (cond (overlays
943 (let ((overlay (car overlays)))
944 (setq overlays (cdr overlays))
945 (when (and (eq (overlay-get overlay 'category) 'rng-error)
946 ;; Is it the first?
947 (= (overlay-start overlay) pos)
948 ;; Is it up to date?
949 (<= (overlay-end overlay)
950 rng-validate-up-to-date-end))
951 (setq done t)
952 (setq found overlay))))
953 ((or (= pos (point-max))
954 (> (setq pos (next-overlay-change pos))
955 rng-validate-up-to-date-end))
956 (setq done t))
957 (t (setq overlays (overlays-in pos (1+ pos))))))
958 found)))
959
960 (defun rng-find-previous-error-overlay (pos)
961 "Return the overlay for the last error starting before POS.
962 Return nil if there is no such overlay or it is out of date.
963 Do not do any additional validation."
964 (when (and rng-error-count
965 (<= pos rng-validate-up-to-date-end))
966 (let (done found overlays)
967 (while (not done)
968 (cond (overlays
969 (let ((overlay (car overlays)))
970 (setq overlays (cdr overlays))
971 (when (and (eq (overlay-get overlay 'category) 'rng-error)
972 ;; Is it the first?
973 (= (overlay-start overlay) pos))
974 (setq done t)
975 (setq found overlay))))
976 ((= pos (point-min))
977 (setq done t))
978 (t
979 (setq pos (previous-overlay-change pos))
980 (setq overlays (overlays-in pos (1+ pos))))))
981 found)))
982
983 ;;; Parsing
984
985 (defun rng-forward (&optional limit)
986 "Move forward over one or more tokens updating the state.
987 If LIMIT is nil, stop after tags.
988 If LIMIT is non-nil, stop when end of last token parsed is >= LIMIT.
989 Return nil at end of buffer, t otherwise."
990 (let (type)
991 (while (progn
992 (setq type (xmltok-forward))
993 (rng-clear-overlays xmltok-start (point))
994 (let ((continue
995 (cond ((eq type 'start-tag)
996 (rng-process-start-tag 'start-tag)
997 nil)
998 ((eq type 'end-tag)
999 (rng-process-end-tag)
1000 nil)
1001 ((eq type 'empty-element)
1002 (rng-process-start-tag 'empty-element)
1003 nil)
1004 ((eq type 'space)
1005 (rng-process-text xmltok-start nil t)
1006 t)
1007 ((eq type 'data)
1008 (rng-process-text xmltok-start nil nil)
1009 t)
1010 ((memq type '(entity-ref char-ref))
1011 (cond (xmltok-replacement
1012 (rng-process-text xmltok-start
1013 nil
1014 'maybe
1015 xmltok-replacement))
1016 ((eq type 'char-ref)
1017 (rng-process-unknown-char))
1018 (t
1019 (rng-process-unknown-entity)))
1020 t)
1021 ((eq type 'cdata-section)
1022 (rng-process-text (+ xmltok-start 9) ; "<![CDATA["
1023 (- (point) 3) ; "]]>"
1024 'maybe)
1025 t)
1026 ((eq type 'partial-start-tag)
1027 (rng-process-start-tag 'partial-start-tag)
1028 t)
1029 ((eq type 'partial-empty-element)
1030 (rng-process-start-tag 'empty-element)
1031 t)
1032 ((eq type 'partial-end-tag)
1033 (rng-process-end-tag 'partial)
1034 t)
1035 (t type))))
1036 (if limit
1037 (< (point) limit)
1038 continue))))
1039 (and type t)))
1040
1041 (defun rng-process-start-tag (tag-type)
1042 "TAG-TYPE is `start-tag' for a start-tag, `empty-element' for
1043 an empty element. `partial-empty-element' should be passed
1044 as empty-element."
1045 (and rng-collecting-text (rng-flush-text))
1046 (setq rng-collecting-text nil)
1047 (setq rng-pending-contents nil)
1048 (rng-process-namespaces)
1049 (let ((tag (rng-process-tag-name)))
1050 (rng-process-attributes)
1051 ;; set the state appropriately
1052 (cond ((eq tag-type 'empty-element)
1053 (rng-process-start-tag-close)
1054 ;; deal with missing content with empty element
1055 (when (not (rng-match-empty-content))
1056 (rng-match-after)
1057 (rng-mark-start-tag-close "Empty content not allowed"))
1058 (nxml-ns-pop-state))
1059 ((eq tag-type 'start-tag)
1060 (rng-process-start-tag-close)
1061 (setq rng-collecting-text (rng-match-text-typed-p))
1062 (rng-push-tag tag))
1063 ((eq tag-type 'partial-start-tag)
1064 (rng-process-start-tag-close)
1065 (rng-match-after)
1066 (nxml-ns-pop-state)))))
1067
1068 (defun rng-process-namespaces ()
1069 (let ((nsatts xmltok-namespace-attributes)
1070 prefixes)
1071 (nxml-ns-push-state)
1072 (while nsatts
1073 (let* ((att (car nsatts))
1074 (value (xmltok-attribute-value att)))
1075 (when value
1076 (let ((ns (nxml-make-namespace value))
1077 (prefix (and (xmltok-attribute-prefix att)
1078 (xmltok-attribute-local-name att))))
1079 (cond ((member prefix prefixes)
1080 (rng-mark-invalid "Duplicate namespace declaration"
1081 (xmltok-attribute-name-start att)
1082 (xmltok-attribute-name-end att)))
1083 ((not prefix)
1084 (nxml-ns-set-default ns))
1085 (ns
1086 (nxml-ns-set-prefix prefix ns))
1087 (t
1088 ;; cannot have xmlns:foo=""
1089 (rng-mark-invalid "Namespace prefix cannot be undeclared"
1090 (1- (xmltok-attribute-value-start att))
1091 (1+ (xmltok-attribute-value-end att)))))
1092 (setq prefixes (cons prefix prefixes)))))
1093 (setq nsatts (cdr nsatts)))))
1094
1095 (defun rng-process-tag-name ()
1096 (let* ((prefix (xmltok-start-tag-prefix))
1097 (local-name (xmltok-start-tag-local-name))
1098 (name
1099 (if prefix
1100 (let ((ns (nxml-ns-get-prefix prefix)))
1101 (cond (ns (cons ns local-name))
1102 ((and (setq ns
1103 (rng-match-infer-start-tag-namespace
1104 local-name))
1105 (rng-match-start-tag-open (cons ns local-name)))
1106 (nxml-ns-set-prefix prefix ns)
1107 (rng-mark-start-tag-close "Missing xmlns:%s=\"%s\""
1108 prefix
1109 (nxml-namespace-name ns))
1110 nil)
1111 (t
1112 (rng-recover-bad-element-prefix)
1113 nil)))
1114 (cons (nxml-ns-get-default) local-name))))
1115 (when (and name
1116 (not (rng-match-start-tag-open name)))
1117 (unless (and (not (car name))
1118 (let ((ns (rng-match-infer-start-tag-namespace (cdr name))))
1119 (and ns
1120 (rng-match-start-tag-open (cons ns local-name))
1121 (progn
1122 (nxml-ns-set-default ns)
1123 ;; XXX need to check we don't have xmlns=""
1124 (rng-mark-start-tag-close "Missing xmlns=\"%s\""
1125 (nxml-namespace-name ns))
1126 t))))
1127 (rng-recover-start-tag-open name)))
1128 (cons prefix local-name)))
1129
1130 (defun rng-process-attributes ()
1131 (let ((atts xmltok-attributes)
1132 names)
1133 (while atts
1134 (let* ((att (car atts))
1135 (prefix (xmltok-attribute-prefix att))
1136 (local-name (xmltok-attribute-local-name att))
1137 (name
1138 (if prefix
1139 (let ((ns (nxml-ns-get-prefix prefix)))
1140 (and ns
1141 (cons ns local-name)))
1142 (cons nil local-name))))
1143 (cond ((not name)
1144 (rng-recover-bad-attribute-prefix att))
1145 ((member name names)
1146 (rng-recover-duplicate-attribute-name att))
1147 ((not (rng-match-attribute-name name))
1148 (rng-recover-attribute-name att))
1149 ((rng-match-text-typed-p)
1150 (let ((value (xmltok-attribute-value att)))
1151 (if value
1152 (or (rng-match-attribute-value value)
1153 (rng-recover-attribute-value att))
1154 (rng-match-after))))
1155 (t (or (rng-match-end-tag)
1156 (error "Internal error:\
1157 invalid on untyped attribute value"))))
1158 (setq names (cons name names)))
1159 (setq atts (cdr atts)))))
1160
1161 (defun rng-process-start-tag-close ()
1162 ;; deal with missing attributes
1163 (unless (rng-match-start-tag-close)
1164 (rng-mark-start-tag-close (rng-missing-attributes-message))
1165 (rng-match-ignore-attributes)))
1166
1167 (defun rng-mark-start-tag-close (&rest args)
1168 (when (not (eq xmltok-type 'partial-start-tag))
1169 (rng-mark-invalid (apply 'format args)
1170 (- (point)
1171 (if (eq xmltok-type 'empty-element)
1172 2
1173 1))
1174 (point))))
1175
1176 (defun rng-recover-bad-element-prefix ()
1177 (rng-mark-invalid "Prefix not declared"
1178 (1+ xmltok-start)
1179 xmltok-name-colon)
1180 (rng-match-unknown-start-tag-open))
1181
1182 (defun rng-recover-bad-attribute-prefix (att)
1183 (rng-mark-invalid "Prefix not declared"
1184 (xmltok-attribute-name-start att)
1185 (xmltok-attribute-name-colon att)))
1186
1187 (defun rng-recover-duplicate-attribute-name (att)
1188 (rng-mark-invalid "Duplicate attribute"
1189 (xmltok-attribute-name-start att)
1190 (xmltok-attribute-name-end att)))
1191
1192 (defun rng-recover-start-tag-open (name)
1193 (let ((required (rng-match-required-element-name)))
1194 (cond ((and required
1195 (rng-match-start-tag-open required)
1196 (rng-match-after)
1197 (rng-match-start-tag-open name))
1198 (rng-mark-invalid (concat "Missing element "
1199 (rng-quote-string
1200 (rng-name-to-string required)))
1201 xmltok-start
1202 (1+ xmltok-start)))
1203 ((and (rng-match-optionalize-elements)
1204 (rng-match-start-tag-open name))
1205 (rng-mark-invalid "Required elements missing"
1206 xmltok-start
1207 (1+ xmltok-start)))
1208 ((rng-match-out-of-context-start-tag-open name)
1209 (rng-mark-invalid "Element not allowed in this context"
1210 (1+ xmltok-start)
1211 xmltok-name-end))
1212 (t
1213 (rng-match-unknown-start-tag-open)
1214 (rng-mark-invalid "Unknown element"
1215 (1+ xmltok-start)
1216 xmltok-name-end)))))
1217
1218 (defun rng-recover-attribute-value (att)
1219 (let ((start (xmltok-attribute-value-start att))
1220 (end (xmltok-attribute-value-end att)))
1221 (if (= start end)
1222 (rng-mark-invalid "Empty attribute value invalid" start (1+ end))
1223 (rng-mark-invalid "Attribute value invalid" start end)))
1224 (rng-match-after))
1225
1226 (defun rng-recover-attribute-name (att)
1227 (rng-mark-invalid "Attribute not allowed"
1228 (xmltok-attribute-name-start att)
1229 (xmltok-attribute-name-end att)))
1230
1231 (defun rng-missing-attributes-message ()
1232 (let ((required-attributes
1233 (rng-match-required-attribute-names)))
1234 (cond ((not required-attributes)
1235 "Required attributes missing")
1236 ((not (cdr required-attributes))
1237 (concat "Missing attribute "
1238 (rng-quote-string
1239 (rng-name-to-string (car required-attributes) t))))
1240 (t
1241 (concat "Missing attributes "
1242 (mapconcat (lambda (nm)
1243 (rng-quote-string
1244 (rng-name-to-string nm t)))
1245 required-attributes
1246 ", "))))))
1247
1248 (defun rng-process-end-tag (&optional partial)
1249 (cond ((not rng-open-elements)
1250 (rng-mark-not-well-formed "Extra end-tag"
1251 xmltok-start
1252 (point)))
1253 ((or partial
1254 (equal (cons (xmltok-end-tag-prefix)
1255 (xmltok-end-tag-local-name))
1256 (car rng-open-elements)))
1257 (rng-end-element))
1258 (t (rng-recover-mismatched-end-tag))))
1259
1260 (defun rng-end-element ()
1261 (if rng-collecting-text
1262 (let ((contents (rng-contents-string)))
1263 (cond ((not contents) (rng-match-after))
1264 ((not (rng-match-element-value contents))
1265 (let* ((region (rng-contents-region)))
1266 (if (not region)
1267 (rng-mark-invalid "Empty content not allowed"
1268 xmltok-start
1269 (+ xmltok-start 2))
1270 (rng-mark-invalid "Invalid data"
1271 (car region)
1272 (cdr region))))
1273 (rng-match-after)))
1274 (setq rng-collecting-text nil)
1275 (setq rng-pending-contents nil))
1276 (unless (rng-match-end-tag)
1277 (rng-mark-invalid (rng-missing-element-message)
1278 xmltok-start
1279 (+ xmltok-start 2))
1280 (rng-match-after)))
1281 (nxml-ns-pop-state)
1282 (when (eq (car rng-open-elements) t)
1283 (rng-pop-tag))
1284 (rng-pop-tag))
1285
1286 (defun rng-missing-element-message ()
1287 (let ((element (rng-match-required-element-name)))
1288 (if element
1289 (concat "Missing element "
1290 (rng-quote-string (rng-name-to-string element)))
1291 "Required child elements missing")))
1292
1293 (defun rng-recover-mismatched-end-tag ()
1294 (let* ((name (cons (xmltok-end-tag-prefix)
1295 (xmltok-end-tag-local-name))))
1296 (cond ((member name (cdr rng-open-elements))
1297 (let* ((suppress-error (eq (car rng-open-elements) t))
1298 missing top)
1299 (while (progn
1300 (setq top (car rng-open-elements))
1301 (rng-pop-tag)
1302 (unless (eq top t)
1303 (setq missing (cons top missing))
1304 (nxml-ns-pop-state)
1305 (rng-match-after))
1306 (not (equal top name))))
1307 (unless suppress-error
1308 (rng-mark-missing-end-tags (cdr missing)))))
1309 ((rng-match-empty-before-p)
1310 (rng-mark-mismatched-end-tag)
1311 (rng-end-element))
1312 (t (rng-mark-mismatched-end-tag)
1313 (setq rng-open-elements
1314 (cons t rng-open-elements))))))
1315
1316 (defun rng-mark-missing-end-tags (missing)
1317 (rng-mark-not-well-formed
1318 (format "Missing end-tag%s %s"
1319 (if (null (cdr missing)) "" "s")
1320 (mapconcat (lambda (name)
1321 (rng-quote-string
1322 (if (car name)
1323 (concat (car name)
1324 ":"
1325 (cdr name))
1326 (cdr name))))
1327 missing
1328 ", "))
1329 xmltok-start
1330 (+ xmltok-start 2)))
1331
1332 (defun rng-mark-mismatched-end-tag ()
1333 (rng-mark-not-well-formed "Mismatched end-tag"
1334 (+ xmltok-start 2)
1335 xmltok-name-end))
1336
1337 (defun rng-push-tag (prefix-local-name)
1338 (setq rng-open-elements
1339 (cons prefix-local-name rng-open-elements)))
1340
1341 (defun rng-pop-tag ()
1342 (setq rng-open-elements (cdr rng-open-elements)))
1343
1344 (defun rng-contents-string ()
1345 (let ((contents rng-pending-contents))
1346 (cond ((not contents) "")
1347 ((memq nil contents) nil)
1348 ((not (cdr contents))
1349 (rng-segment-string (car contents)))
1350 (t (apply 'concat
1351 (nreverse (mapcar 'rng-segment-string
1352 contents)))))))
1353
1354 (defun rng-segment-string (segment)
1355 (or (car segment)
1356 (apply 'buffer-substring-no-properties
1357 (cdr segment))))
1358
1359 (defun rng-segment-blank-p (segment)
1360 (if (car segment)
1361 (rng-blank-p (car segment))
1362 (apply 'rng-region-blank-p
1363 (cdr segment))))
1364
1365 (defun rng-contents-region ()
1366 (if (null rng-pending-contents)
1367 nil
1368 (let* ((contents rng-pending-contents)
1369 (head (cdar contents))
1370 (start (car head))
1371 (end (cadr head)))
1372 (while (setq contents (cdr contents))
1373 (setq start (car (cdar contents))))
1374 (cons start end))))
1375
1376 (defun rng-process-text (start end whitespace &optional value)
1377 "Process characters between position START and END as text.
1378 END nil means point. WHITESPACE t means known to be whitespace, nil
1379 means known not to be, anything else means unknown whether whitespace
1380 or not. END must not be nil if WHITESPACE is neither t nor nil.
1381 VALUE is a string or nil; nil means the value is equal to the
1382 string between START and END."
1383 (cond (rng-collecting-text
1384 (setq rng-pending-contents (cons (list value start (or end (point)))
1385 rng-pending-contents)))
1386 ((not (or (and whitespace
1387 (or (eq whitespace t)
1388 (if value
1389 (rng-blank-p value)
1390 (rng-region-blank-p start end))))
1391 (rng-match-mixed-text)))
1392 (rng-mark-invalid "Text not allowed" start (or end (point))))))
1393
1394 (defun rng-process-unknown-char ()
1395 (when rng-collecting-text
1396 (setq rng-pending-contents
1397 (cons nil rng-pending-contents))))
1398
1399 (defun rng-process-unknown-entity ()
1400 (rng-process-unknown-char)
1401 (rng-match-optionalize-elements))
1402
1403 (defun rng-region-blank-p (beg end)
1404 (save-excursion
1405 (goto-char beg)
1406 (= (skip-chars-forward " \n\r\t" end)
1407 (- end beg))))
1408
1409 (defun rng-flush-text ()
1410 (while rng-pending-contents
1411 (let ((segment (car rng-pending-contents)))
1412 (unless (or (rng-segment-blank-p segment)
1413 (rng-match-mixed-text))
1414 (let ((region (cdr segment)))
1415 (rng-mark-invalid "In this context text cannot be mixed with elements"
1416 (car region)
1417 (cadr region)))))
1418 (setq rng-pending-contents (cdr rng-pending-contents))))
1419
1420 (defun rng-process-end-document ()
1421 ;; this is necessary to clear empty overlays at (point-max)
1422 (rng-clear-overlays (point) (point))
1423 (let ((start (save-excursion
1424 (skip-chars-backward " \t\r\n")
1425 (point))))
1426 (cond (rng-open-elements
1427 (unless (eq (car rng-open-elements) t)
1428 (rng-mark-not-well-formed "Missing end-tag"
1429 start
1430 (point))))
1431 ((not (rng-match-nullable-p))
1432 (rng-mark-not-well-formed "No document element"
1433 start
1434 (point))))))
1435
1436 (defun rng-process-encoding-name (beg end)
1437 (unless (let ((charset (buffer-substring-no-properties beg end)))
1438 (or (nxml-mime-charset-coding-system charset)
1439 (string= (downcase charset) "utf-16")))
1440 (rng-mark-not-well-formed "Unsupported encoding" beg end)))
1441
1442 (defun rng-name-to-string (name &optional attributep)
1443 (let ((ns (car name))
1444 (local-name (cdr name)))
1445 (if (or (not ns)
1446 (and (not attributep)
1447 (eq (nxml-ns-get-default) ns)))
1448 local-name
1449 (let ((prefix (nxml-ns-prefix-for ns)))
1450 (if prefix
1451 (concat prefix ":" local-name)
1452 (concat "{" (symbol-name ns) "}" local-name))))))
1453
1454 (provide 'rng-valid)
1455
1456 ;;; rng-valid.el ends here