rng-valid.el 50 KB

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