* lisp/textmodes/tex-mode.el: Use lexical-binding
[emacs.git] / lisp / nxml / nxml-mode.el
blob0e2fca349a5c85f3ecb345fb666ad5937dff3e39
1 ;;; nxml-mode.el --- a new XML mode -*- lexical-binding:t -*-
3 ;; Copyright (C) 2003-2004, 2007-2015 Free Software Foundation, Inc.
5 ;; Author: James Clark
6 ;; Keywords: wp, hypermedia, languages, XML
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; See nxml-rap.el for description of parsing strategy.
27 ;;; Code:
29 (when (featurep 'mucs)
30 (error "nxml-mode is not compatible with Mule-UCS"))
32 (eval-when-compile (require 'cl-lib))
34 (require 'xmltok)
35 (require 'nxml-enc)
36 (require 'nxml-glyph)
37 (require 'nxml-util)
38 (require 'nxml-rap)
39 (require 'nxml-outln)
40 ;; nxml-mode calls rng-nxml-mode-init, which is autoloaded from rng-nxml.
41 ;; So we might as well just require it and silence the compiler.
42 (provide 'nxml-mode) ; avoid recursive require
43 (require 'rng-nxml)
45 ;;; Customization
47 (defgroup nxml nil
48 "New XML editing mode."
49 :link '(custom-manual "(nxml-mode) Top")
50 :group 'languages)
52 (defgroup nxml-faces nil
53 "Faces for XML syntax highlighting."
54 :group 'nxml)
56 (defcustom nxml-char-ref-display-glyph-flag t
57 "Non-nil means display glyph following character reference.
58 The glyph is displayed in face `nxml-glyph'. The abnormal hook
59 `nxml-glyph-set-functions' can be used to change the characters
60 for which glyphs are displayed."
61 :group 'nxml
62 :type 'boolean)
64 (defcustom nxml-sexp-element-flag nil
65 "Non-nil means sexp commands treat an element as a single expression."
66 :group 'nxml
67 :type 'boolean)
69 (defcustom nxml-slash-auto-complete-flag nil
70 "Non-nil means typing a slash automatically completes the end-tag.
71 This is used by `nxml-electric-slash'."
72 :group 'nxml
73 :type 'boolean)
75 (defcustom nxml-child-indent 2
76 "Indentation for the children of an element relative to the start-tag.
77 This only applies when the line or lines containing the start-tag contains
78 nothing else other than that start-tag."
79 :group 'nxml
80 :type 'integer)
82 (defcustom nxml-attribute-indent 4
83 "Indentation for the attributes of an element relative to the start-tag.
84 This only applies when the first attribute of a tag starts a line.
85 In other cases, the first attribute on one line is indented the same
86 as the first attribute on the previous line."
87 :group 'nxml
88 :type 'integer)
90 (defcustom nxml-bind-meta-tab-to-complete-flag t
91 "Non-nil means to use nXML completion in \\[completion-at-point]."
92 :group 'nxml
93 :type 'boolean)
95 (defcustom nxml-prefer-utf-16-to-utf-8-flag nil
96 "Non-nil means prefer UTF-16 to UTF-8 when saving a buffer.
97 This is used only when a buffer does not contain an encoding declaration
98 and when its current `buffer-file-coding-system' specifies neither UTF-16
99 nor UTF-8."
100 :group 'nxml
101 :type 'boolean)
103 (defcustom nxml-prefer-utf-16-little-to-big-endian-flag (eq system-type
104 'windows-nt)
105 "Non-nil means prefer little-endian to big-endian byte-order for UTF-16.
106 This is used only for saving a buffer; when reading the byte-order is
107 auto-detected. It may be relevant both when there is no encoding declaration
108 and when the encoding declaration specifies `UTF-16'."
109 :group 'nxml
110 :type 'boolean)
112 (defcustom nxml-default-buffer-file-coding-system nil
113 "Default value for `buffer-file-coding-system' for a buffer for a new file.
114 A value of nil means use the default value of `buffer-file-coding-system' as normal.
115 A buffer's `buffer-file-coding-system' affects what \\[nxml-insert-xml-declaration] inserts."
116 :group 'nxml
117 :type 'coding-system)
119 (defcustom nxml-auto-insert-xml-declaration-flag nil
120 "Non-nil means automatically insert an XML declaration in a new file.
121 The XML declaration is inserted using `nxml-insert-xml-declaration'."
122 :group 'nxml
123 :type 'boolean)
125 (defface nxml-delimited-data
126 '((t (:inherit font-lock-doc-face)))
127 "Face used to highlight data enclosed between delimiters.
128 This is not used directly, but only via inheritance by other faces."
129 :group 'nxml-faces)
131 (defface nxml-name
132 '((t (:inherit font-lock-builtin-face)))
133 "Face used to highlight various names.
134 This includes element and attribute names, processing
135 instruction targets and the CDATA keyword in a CDATA section.
136 This is not used directly, but only via inheritance by other faces."
137 :group 'nxml-faces)
139 (defface nxml-ref
140 '((t (:inherit font-lock-constant-face)))
141 "Face used to highlight character and entity references.
142 This is not used directly, but only via inheritance by other faces."
143 :group 'nxml-faces)
145 (defface nxml-delimiter
147 "Face used to highlight delimiters.
148 This is not used directly, but only via inheritance by other faces."
149 :group 'nxml-faces)
151 (defface nxml-text
153 "Face used to highlight text."
154 :group 'nxml-faces)
156 (defface nxml-comment-content
157 '((t (:inherit font-lock-comment-face)))
158 "Face used to highlight the content of comments."
159 :group 'nxml-faces)
161 (defface nxml-comment-delimiter
162 '((t (:inherit font-lock-comment-delimiter-face)))
163 "Face used for the delimiters of comments, i.e., <!-- and -->."
164 :group 'nxml-faces)
166 (defface nxml-processing-instruction-delimiter
167 '((t (:inherit nxml-delimiter)))
168 "Face used for the delimiters of processing instructions, i.e., <? and ?>."
169 :group 'nxml-faces)
171 (defface nxml-processing-instruction-target
172 '((t (:inherit font-lock-keyword-face)))
173 "Face used for the target of processing instructions."
174 :group 'nxml-faces)
176 (defface nxml-processing-instruction-content
177 '((t (:inherit nxml-delimited-data)))
178 "Face used for the content of processing instructions."
179 :group 'nxml-faces)
181 (defface nxml-cdata-section-delimiter
182 '((t (:inherit nxml-delimiter)))
183 "Face used for the delimiters of CDATA sections, i.e., <![, [, and ]]>."
184 :group 'nxml-faces)
186 (defface nxml-cdata-section-CDATA
187 '((t (:inherit nxml-name)))
188 "Face used for the CDATA keyword in CDATA sections."
189 :group 'nxml-faces)
191 (defface nxml-cdata-section-content
192 '((t (:inherit nxml-text)))
193 "Face used for the content of CDATA sections."
194 :group 'nxml-faces)
196 (defface nxml-char-ref-number
197 '((t (:inherit nxml-ref)))
198 "Face used for the number in character references.
199 This includes ths `x' in hex references."
200 :group 'nxml-faces)
202 (defface nxml-char-ref-delimiter
203 '((t (:inherit nxml-ref)))
204 "Face used for the delimiters of character references, i.e., &# and ;."
205 :group 'nxml-faces)
207 (defface nxml-entity-ref-name
208 '((t (:inherit nxml-ref)))
209 "Face used for the entity name in general entity references."
210 :group 'nxml-faces)
212 (defface nxml-entity-ref-delimiter
213 '((t (:inherit nxml-ref)))
214 "Face used for the delimiters of entity references, i.e., & and ;."
215 :group 'nxml-faces)
217 (defface nxml-tag-delimiter
218 '((t (:inherit nxml-delimiter)))
219 "Face used for the angle brackets delimiting tags.
220 `nxml-tag-slash' is used for slashes."
221 :group 'nxml-faces)
223 (defface nxml-tag-slash
224 '((t (:inherit nxml-tag-delimiter)))
225 "Face used for slashes in tags, both in end-tags and empty-elements."
226 :group 'nxml-faces)
228 (defface nxml-element-prefix
229 '((t (:inherit nxml-name)))
230 "Face used for the prefix of elements."
231 :group 'nxml-faces)
233 (defface nxml-element-colon
235 "Face used for the colon in element names."
236 :group 'nxml-faces)
238 (defface nxml-element-local-name
239 '((t (:inherit font-lock-function-name-face)))
240 "Face used for the local name of elements."
241 :group 'nxml-faces)
243 (defface nxml-attribute-prefix
244 '((t (:inherit nxml-name)))
245 "Face used for the prefix of attributes."
246 :group 'nxml-faces)
248 (defface nxml-attribute-colon
249 '((t (:inherit nxml-delimiter)))
250 "Face used for the colon in attribute names."
251 :group 'nxml-faces)
253 (defface nxml-attribute-local-name
254 '((t (:inherit font-lock-variable-name-face)))
255 "Face used for the local name of attributes."
256 :group 'nxml-faces)
258 (defface nxml-namespace-attribute-xmlns
259 '((t (:inherit nxml-attribute-prefix)))
260 "Face used for `xmlns' in namespace attributes."
261 :group 'nxml-faces)
263 (defface nxml-namespace-attribute-colon
264 '((t (:inherit nxml-attribute-colon)))
265 "Face used for the colon in namespace attributes."
266 :group 'nxml-faces)
268 (defface nxml-namespace-attribute-prefix
269 '((t (:inherit nxml-attribute-local-name)))
270 "Face used for the prefix declared in namespace attributes."
271 :group 'nxml-faces)
273 (defface nxml-attribute-value
274 '((t (:inherit font-lock-string-face)))
275 "Face used for the value of attributes."
276 :group 'nxml-faces)
278 (defface nxml-attribute-value-delimiter
279 '((t (:inherit nxml-attribute-value)))
280 "Face used for the delimiters of attribute values."
281 :group 'nxml-faces)
283 (defface nxml-namespace-attribute-value
284 '((t (:inherit nxml-attribute-value)))
285 "Face used for the value of namespace attributes."
286 :group 'nxml-faces)
288 (defface nxml-namespace-attribute-value-delimiter
289 '((t (:inherit nxml-attribute-value-delimiter)))
290 "Face used for the delimiters of namespace attribute values."
291 :group 'nxml-faces)
293 (defface nxml-prolog-literal-delimiter
294 '((t (:inherit nxml-delimited-data)))
295 "Face used for the delimiters of literals in the prolog."
296 :group 'nxml-faces)
298 (defface nxml-prolog-literal-content
299 '((t (:inherit nxml-delimited-data)))
300 "Face used for the content of literals in the prolog."
301 :group 'nxml-faces)
303 (defface nxml-prolog-keyword
304 '((t (:inherit font-lock-keyword-face)))
305 "Face used for keywords in the prolog."
306 :group 'nxml-faces)
308 (defface nxml-markup-declaration-delimiter
309 '((t (:inherit nxml-delimiter)))
310 "Face used for the delimiters of markup declarations in the prolog.
311 The delimiters are <! and >."
312 :group 'nxml-faces)
314 (defface nxml-hash
315 '((t (:inherit nxml-name)))
316 "Face used for # before a name in the prolog."
317 :group 'nxml-faces)
319 (defface nxml-glyph
320 '((((type x))
321 (:family
322 "misc-fixed"
323 :background
324 "light grey"
325 :foreground
326 "black"
327 :weight
328 normal
329 :slant
330 normal))
332 (:background
333 "light grey"
334 :foreground
335 "black"
336 :weight
337 normal
338 :slant
339 normal)))
340 "Face used for glyph for char references."
341 :group 'nxml-faces)
343 ;;; Global variables
345 (defvar nxml-parent-document nil
346 "The parent document for a part of a modular document.
347 Use `nxml-parent-document-set' to set it.")
348 (make-variable-buffer-local 'nxml-parent-document)
349 (put 'nxml-parent-document 'safe-local-variable 'stringp)
351 (defvar nxml-prolog-regions nil
352 "List of regions in the prolog to be fontified.
353 See the function `xmltok-forward-prolog' for more information.")
354 (make-variable-buffer-local 'nxml-prolog-regions)
356 (defvar nxml-degraded nil
357 "Non-nil if currently operating in degraded mode.
358 Degraded mode is enabled when an internal error is encountered in the
359 fontification or after-change functions.")
360 (make-variable-buffer-local 'nxml-degraded)
362 (defvar nxml-completion-hook nil
363 "Hook run by `nxml-complete'.
364 This hook is run until success.")
366 (defvar nxml-in-mixed-content-hook nil
367 "Hook to determine whether point is in mixed content.
368 The hook is called without arguments. It should return nil if it is
369 definitely not mixed; non-nil otherwise. The hook will be run until
370 one of the functions returns nil.")
372 (defvar nxml-mixed-scan-distance 4000
373 "Maximum distance from point to scan when checking for mixed content.")
375 (defvar nxml-end-tag-indent-scan-distance 4000
376 "Maximum distance from point to scan backwards when indenting end-tag.")
378 (defvar nxml-char-ref-extra-display t
379 "Non-nil means display extra information for character references.
380 The extra information consists of a tooltip with the character name
381 and, if `nxml-char-ref-display-glyph-flag' is non-nil, a glyph
382 corresponding to the referenced character following the character
383 reference.")
384 (make-variable-buffer-local 'nxml-char-ref-extra-display)
386 (defvar nxml-mode-map
387 (let ((map (make-sparse-keymap)))
388 (define-key map "\M-\C-u" 'nxml-backward-up-element)
389 (define-key map "\M-\C-d" 'nxml-down-element)
390 (define-key map "\M-\C-n" 'nxml-forward-element)
391 (define-key map "\M-\C-p" 'nxml-backward-element)
392 (define-key map "\M-{" 'nxml-backward-paragraph)
393 (define-key map "\M-}" 'nxml-forward-paragraph)
394 (define-key map "\M-h" 'nxml-mark-paragraph)
395 (define-key map "\C-c\C-f" 'nxml-finish-element)
396 (define-key map "\C-c]" 'nxml-finish-element)
397 (define-key map "\C-c/" 'nxml-finish-element)
398 (define-key map "\C-c\C-m" 'nxml-split-element)
399 (define-key map "\C-c\C-b" 'nxml-balanced-close-start-tag-block)
400 (define-key map "\C-c\C-i" 'nxml-balanced-close-start-tag-inline)
401 (define-key map "\C-c\C-x" 'nxml-insert-xml-declaration)
402 (define-key map "\C-c\C-d" 'nxml-dynamic-markup-word)
403 ;; u is for Unicode
404 (define-key map "\C-c\C-u" 'nxml-insert-named-char)
405 (define-key map "\C-c\C-o" nxml-outline-prefix-map)
406 (define-key map [S-mouse-2] 'nxml-mouse-hide-direct-text-content)
407 (define-key map "/" 'nxml-electric-slash)
408 (define-key map "\M-\t" 'completion-at-point)
409 map)
410 "Keymap for nxml-mode.")
412 (defvar nxml-font-lock-keywords
413 '(nxml-fontify-matcher)
414 "Default font lock keywords for nxml-mode.")
416 (defsubst nxml-set-face (start end face)
417 (when (and face (< start end))
418 (font-lock-append-text-property start end 'face face)))
420 (defun nxml-parent-document-set (parent-document)
421 "Set `nxml-parent-document' and inherit the DTD &c."
422 ;; FIXME: this does not work.
423 ;; the idea is that by inheriting some variables from the parent,
424 ;; `rng-validate-mode' will validate entities declared in the parent.
425 ;; alas, the most interesting variables (`rng-compile-table' et al)
426 ;; are circular and cannot be printed even with `print-circle'.
427 (interactive "fParent document")
428 (let (dtd current-schema current-schema-file-name compile-table
429 ipattern-table last-ipattern-index)
430 (when (string= (file-truename parent-document)
431 (file-truename buffer-file-name))
432 (error "Parent document cannot be the same as the document"))
433 (with-current-buffer (find-file-noselect parent-document)
434 (setq dtd rng-dtd
435 current-schema rng-current-schema
436 current-schema-file-name rng-current-schema-file-name
437 compile-table rng-compile-table
438 ipattern-table rng-ipattern-table
439 last-ipattern-index rng-last-ipattern-index
440 parent-document buffer-file-name))
441 (setq rng-dtd dtd
442 rng-current-schema current-schema
443 rng-current-schema-file-name current-schema-file-name
444 rng-compile-table compile-table
445 rng-ipattern-table ipattern-table
446 rng-last-ipattern-index last-ipattern-index
447 nxml-parent-document parent-document)
448 (message "Set parent document to %s" parent-document)
449 (when rng-validate-mode
450 (rng-validate-while-idle (current-buffer)))))
452 (defvar tildify-space-string)
453 (defvar tildify-foreach-region-function)
455 ;;;###autoload
456 (define-derived-mode nxml-mode text-mode "nXML"
457 ;; We use C-c C-i instead of \\[nxml-balanced-close-start-tag-inline]
458 ;; because Emacs turns C-c C-i into C-c TAB which is hard to type and
459 ;; not mnemonic.
460 "Major mode for editing XML.
462 \\[nxml-finish-element] finishes the current element by inserting an end-tag.
463 C-c C-i closes a start-tag with `>' and then inserts a balancing end-tag
464 leaving point between the start-tag and end-tag.
465 \\[nxml-balanced-close-start-tag-block] is similar but for block rather than inline elements:
466 the start-tag, point, and end-tag are all left on separate lines.
467 If `nxml-slash-auto-complete-flag' is non-nil, then inserting a `</'
468 automatically inserts the rest of the end-tag.
470 \\[completion-at-point] performs completion on the symbol preceding point.
472 \\[nxml-dynamic-markup-word] uses the contents of the current buffer
473 to choose a tag to put around the word preceding point.
475 Sections of the document can be displayed in outline form. The
476 variable `nxml-section-element-name-regexp' controls when an element
477 is recognized as a section. The same key sequences that change
478 visibility in outline mode are used except that they start with C-c C-o
479 instead of C-c.
481 Validation is provided by the related minor-mode `rng-validate-mode'.
482 This also makes completion schema- and context- sensitive. Element
483 names, attribute names, attribute values and namespace URIs can all be
484 completed. By default, `rng-validate-mode' is automatically enabled.
485 You can toggle it using \\[rng-validate-mode] or change the default by
486 customizing `rng-nxml-auto-validate-flag'.
488 \\[indent-for-tab-command] indents the current line appropriately.
489 This can be customized using the variable `nxml-child-indent'
490 and the variable `nxml-attribute-indent'.
492 \\[nxml-insert-named-char] inserts a character reference using
493 the character's name (by default, the Unicode name).
494 \\[universal-argument] \\[nxml-insert-named-char] inserts the character directly.
496 The Emacs commands that normally operate on balanced expressions will
497 operate on XML markup items. Thus \\[forward-sexp] will move forward
498 across one markup item; \\[backward-sexp] will move backward across
499 one markup item; \\[kill-sexp] will kill the following markup item;
500 \\[mark-sexp] will mark the following markup item. By default, each
501 tag each treated as a single markup item; to make the complete element
502 be treated as a single markup item, set the variable
503 `nxml-sexp-element-flag' to t. For more details, see the function
504 `nxml-forward-balanced-item'.
506 \\[nxml-backward-up-element] and \\[nxml-down-element] move up and down the element structure.
508 Many aspects this mode can be customized using
509 \\[customize-group] nxml RET."
510 ;; (kill-all-local-variables)
511 ;; If encoding does not allow non-break space character, use reference.
512 ;; FIXME: This duplicates code from sgml-mode, perhaps derive from it?
513 ;; FIXME: Perhaps use &nbsp; if possible (e.g. XHTML)?
514 (setq-local tildify-space-string
515 (if (equal (decode-coding-string
516 (encode-coding-string " " buffer-file-coding-system)
517 buffer-file-coding-system) " ")
518 " " "&#160;"))
519 ;; FIXME: Use the fact that we're parsing the document already
520 ;; rather than using regex-based filtering.
521 (setq-local tildify-foreach-region-function
522 (apply-partially 'tildify-foreach-ignore-environments
523 '(("<! *--" . "-- *>") ("<" . ">"))))
524 (set (make-local-variable 'mode-line-process) '((nxml-degraded "/degraded")))
525 ;; We'll determine the fill prefix ourselves
526 (make-local-variable 'adaptive-fill-mode)
527 (setq adaptive-fill-mode nil)
528 (make-local-variable 'forward-sexp-function)
529 (setq forward-sexp-function 'nxml-forward-balanced-item)
530 (make-local-variable 'indent-line-function)
531 (setq indent-line-function 'nxml-indent-line)
532 (make-local-variable 'fill-paragraph-function)
533 (setq fill-paragraph-function 'nxml-do-fill-paragraph)
534 ;; Comment support
535 ;; This doesn't seem to work too well;
536 ;; I think we should probably roll our own nxml-comment-dwim function.
537 (make-local-variable 'comment-indent-function)
538 (setq comment-indent-function 'nxml-indent-line)
539 (make-local-variable 'comment-start)
540 (setq comment-start "<!--")
541 (make-local-variable 'comment-start-skip)
542 (setq comment-start-skip "<!--[ \t\r\n]*")
543 (make-local-variable 'comment-end)
544 (setq comment-end "-->")
545 (make-local-variable 'comment-end-skip)
546 (setq comment-end-skip "[ \t\r\n]*-->")
547 (make-local-variable 'comment-line-break-function)
548 (setq comment-line-break-function 'nxml-newline-and-indent)
549 (setq-local comment-quote-nested-function 'nxml-comment-quote-nested)
550 (use-local-map nxml-mode-map)
551 (save-excursion
552 (save-restriction
553 (widen)
554 (setq nxml-scan-end (copy-marker (point-min) nil))
555 (with-silent-modifications
556 (nxml-clear-inside (point-min) (point-max))
557 (nxml-with-invisible-motion
558 (nxml-scan-prolog)))))
559 (add-hook 'completion-at-point-functions
560 #'nxml-completion-at-point-function nil t)
561 (setq-local syntax-propertize-function #'nxml-after-change)
562 (add-hook 'change-major-mode-hook 'nxml-cleanup nil t)
564 ;; Emacs 23 handles the encoding attribute on the xml declaration
565 ;; transparently to nxml-mode, so there is no longer a need for the below
566 ;; hook. The hook also had the drawback of overriding explicit user
567 ;; instruction to save as some encoding other than utf-8.
568 ;;(add-hook 'write-contents-hooks 'nxml-prepare-to-save)
569 (when (not (and (buffer-file-name) (file-exists-p (buffer-file-name))))
570 (when (and nxml-default-buffer-file-coding-system
571 (not (local-variable-p 'buffer-file-coding-system)))
572 (setq buffer-file-coding-system nxml-default-buffer-file-coding-system))
573 (when nxml-auto-insert-xml-declaration-flag
574 (nxml-insert-xml-declaration)))
576 (setq font-lock-defaults
577 '(nxml-font-lock-keywords
578 t ; keywords-only; we highlight comments and strings here
579 nil ; font-lock-keywords-case-fold-search. XML is case sensitive
580 nil ; no special syntax table
581 nil ; no automatic syntactic fontification
582 (font-lock-extend-region-functions . (nxml-extend-region))
583 (jit-lock-contextually . t)
584 (font-lock-unfontify-region-function . nxml-unfontify-region)))
586 (rng-nxml-mode-init)
587 (nxml-enable-unicode-char-name-sets))
589 (defun nxml-cleanup ()
590 "Clean up after nxml-mode."
591 ;; Disable associated minor modes.
592 (rng-validate-mode -1)
593 ;; Clean up fontification.
594 (save-excursion
595 (widen)
596 (with-silent-modifications
597 (nxml-with-invisible-motion
598 (remove-text-properties (point-min) (point-max) '(face)))))
599 (remove-hook 'change-major-mode-hook 'nxml-cleanup t))
601 (defun nxml-degrade (context err)
602 (message "Internal nXML mode error in %s (%s), degrading"
603 context
604 (error-message-string err))
605 (ding)
606 (setq nxml-degraded t)
607 (setq nxml-prolog-end 1)
608 (save-excursion
609 (save-restriction
610 (widen)
611 (with-silent-modifications
612 (nxml-clear-inside (point-min) (point-max))))))
614 ;;; Change management
616 (defvar font-lock-beg) (defvar font-lock-end)
617 (defun nxml-debug-region (start end)
618 (interactive "r")
619 (let ((font-lock-beg start)
620 (font-lock-end end))
621 (nxml-extend-region)
622 (goto-char font-lock-beg)
623 (set-mark font-lock-end)))
625 (defun nxml-after-change (start end)
626 ;; Called via syntax-propertize-function.
627 (unless nxml-degraded
628 (nxml-with-degradation-on-error 'nxml-after-change
629 (save-restriction
630 (widen)
631 (nxml-with-invisible-motion
632 (nxml-after-change1 start end))))))
634 (defun nxml-after-change1 (start end)
635 "After-change bookkeeping.
636 Returns a cons cell containing a possibly-enlarged change region.
637 You must call `nxml-extend-region' on this expanded region to obtain
638 the full extent of the area needing refontification.
640 For bookkeeping, call this function even when fontification is
641 disabled."
642 ;; If the prolog might have changed, rescan the prolog.
643 (when (<= start
644 ;; Add 2 so as to include the < and following char that
645 ;; start the instance (document element), since changing
646 ;; these can change where the prolog ends.
647 (+ nxml-prolog-end 2))
648 (nxml-scan-prolog)
649 (setq start (point-min)))
651 (when (> end nxml-prolog-end)
652 (goto-char start)
653 (nxml-move-tag-backwards (point-min))
654 (setq start (point))
655 (setq end (max (nxml-scan-after-change start end)
656 end)))
658 (nxml-debug-change "nxml-after-change1" start end))
660 ;;; Encodings
662 (defun nxml-insert-xml-declaration ()
663 "Insert an XML declaration at the beginning of buffer.
664 The XML declaration will declare an encoding depending on the buffer's
665 `buffer-file-coding-system'."
666 (interactive "*")
667 (let ((coding-system
668 (if (and buffer-file-coding-system
669 (coding-system-p buffer-file-coding-system)
670 (coding-system-get buffer-file-coding-system
671 'mime-charset))
672 buffer-file-coding-system
673 (nxml-choose-utf-coding-system))))
674 (goto-char (point-min))
675 (insert (format "<?xml version=\"1.0\" encoding=\"%s\"?>\n"
676 (nxml-coding-system-name coding-system)))))
678 (defun nxml-prepare-to-save ()
679 (unless (and (not enable-multibyte-characters)
680 (local-variable-p 'buffer-file-coding-system)
681 buffer-file-coding-system
682 (or (eq (coding-system-type buffer-file-coding-system) 5)
683 (eq buffer-file-coding-system 'no-conversion)))
684 (save-excursion
685 (setq buffer-file-coding-system (nxml-select-coding-system))))
686 ;; nil from a function in `write-contents-hooks' means
687 ;; to continue and write the file as normal
688 nil)
690 (defun nxml-select-coding-system ()
691 (let* ((suitable-coding-systems
692 (find-coding-systems-region (point-min) (point-max)))
693 (enc-pos (progn
694 (goto-char (point-min))
695 (xmltok-get-declared-encoding-position)))
696 (enc-name
697 (and (consp enc-pos)
698 (buffer-substring-no-properties (car enc-pos)
699 (cdr enc-pos))))
700 (coding-system
701 (cond (enc-name
702 (if (string= (downcase enc-name) "utf-16")
703 (nxml-choose-utf-16-coding-system)
704 (nxml-mime-charset-coding-system enc-name)))
705 (enc-pos (nxml-choose-utf-coding-system)))))
706 ;; Make sure we have a coding-system
707 (unless coding-system
708 (setq coding-system
709 (and (not buffer-read-only)
710 (nxml-choose-suitable-coding-system
711 suitable-coding-systems)))
712 (let ((message
713 (if enc-name
714 (format "Unknown encoding %s" enc-name)
715 "XML declaration is not well-formed")))
716 (cond ((not coding-system)
717 (error "%s" message))
718 ((y-or-n-p
719 (concat message
720 ". "
721 (format (if enc-name
722 "Save with %s"
723 "Modify and save with encoding %s")
724 (nxml-coding-system-name coding-system))
725 " "))
726 (nxml-fix-encoding-declaration enc-pos coding-system))
727 (t (signal 'quit nil)))))
728 ;; Make sure it can encode all the characters in the buffer
729 (unless (or (memq (coding-system-base coding-system)
730 suitable-coding-systems)
731 (equal suitable-coding-systems '(undecided)))
732 (let ((message
733 (nxml-unsuitable-coding-system-message coding-system
734 enc-name)))
735 (setq coding-system
736 (and (not buffer-read-only)
737 (nxml-choose-suitable-coding-system
738 suitable-coding-systems)))
739 (cond ((not coding-system) (error "%s" message))
740 ((y-or-n-p (concat message
741 (format ". Save with %s "
742 (nxml-coding-system-name
743 coding-system))))
744 (nxml-fix-encoding-declaration enc-pos coding-system))
745 (t (signal 'quit nil)))))
746 ;; Merge the newline type of our existing encoding
747 (let ((current-eol-type
748 (coding-system-eol-type buffer-file-coding-system)))
749 (when (and current-eol-type (integerp current-eol-type))
750 (setq coding-system
751 (coding-system-change-eol-conversion coding-system
752 current-eol-type))))
753 coding-system))
755 (defun nxml-unsuitable-coding-system-message (coding-system &optional enc-name)
756 (if (nxml-coding-system-unicode-p coding-system)
757 "Cannot translate some characters to Unicode"
758 (format "Cannot encode some characters with %s"
759 (or enc-name
760 (nxml-coding-system-name coding-system)))))
762 (defconst nxml-utf-16-coding-systems (and (coding-system-p 'utf-16-be)
763 (coding-system-p 'utf-16-le)
764 '(utf-16-be utf-16-le)))
766 (defconst nxml-utf-coding-systems (cons 'utf-8 nxml-utf-16-coding-systems))
768 (defun nxml-coding-system-unicode-p (coding-system)
769 (nxml-coding-system-member (coding-system-base coding-system)
770 nxml-utf-coding-systems))
772 (defun nxml-coding-system-name (coding-system)
773 (setq coding-system (coding-system-base coding-system))
774 (symbol-name
775 (if (nxml-coding-system-member coding-system nxml-utf-16-coding-systems)
776 'utf-16
777 (or (coding-system-get coding-system 'mime-charset)
778 coding-system))))
780 (defun nxml-fix-encoding-declaration (enc-pos coding-system)
781 (let ((charset (nxml-coding-system-name coding-system)))
782 (cond ((consp enc-pos)
783 (delete-region (car enc-pos) (cdr enc-pos))
784 (goto-char (car enc-pos))
785 (insert charset))
786 ((integerp enc-pos)
787 (goto-char enc-pos)
788 (insert " encoding=\"" charset ?\"))
790 (goto-char (point-min))
791 (insert "<?xml version=\"1.0\" encoding=\""
792 charset
793 "\"?>\n")
794 (when (and (not enc-pos)
795 (let ((case-fold-search t))
796 (looking-at xmltok-bad-xml-decl-regexp)))
797 (delete-region (point) (match-end 0)))))))
799 (defun nxml-choose-suitable-coding-system (suitable-coding-systems)
800 (let (ret coding-system)
801 (if (and buffer-file-coding-system
802 (memq (coding-system-base buffer-file-coding-system)
803 suitable-coding-systems))
804 buffer-file-coding-system
805 (while (and suitable-coding-systems (not ret))
806 (setq coding-system (car suitable-coding-systems))
807 (if (coding-system-get coding-system 'mime-charset)
808 (setq ret coding-system)
809 (setq suitable-coding-systems (cdr suitable-coding-systems))))
810 ret)))
812 (defun nxml-choose-utf-coding-system ()
813 (let ((cur (and (local-variable-p 'buffer-file-coding-system)
814 buffer-file-coding-system
815 (coding-system-base buffer-file-coding-system))))
816 (cond ((car (nxml-coding-system-member cur nxml-utf-coding-systems)))
817 ((and nxml-prefer-utf-16-to-utf-8-flag
818 (coding-system-p 'utf-16-le)
819 (coding-system-p 'utf-16-be))
820 (if nxml-prefer-utf-16-little-to-big-endian-flag
821 'utf-16-le
822 'utf-16-be))
823 (t 'utf-8))))
825 (defun nxml-choose-utf-16-coding-system ()
826 (let ((cur (and (local-variable-p 'buffer-file-coding-system)
827 buffer-file-coding-system
828 (coding-system-base buffer-file-coding-system))))
829 (cond ((car (nxml-coding-system-member cur nxml-utf-16-coding-systems)))
830 (nxml-prefer-utf-16-little-to-big-endian-flag
831 (and (coding-system-p 'utf-16-le) 'utf-16-le))
832 (t (and (coding-system-p 'utf-16-be) 'utf-16-be)))))
834 (defun nxml-coding-system-member (coding-system coding-systems)
835 (let (ret)
836 (while (and coding-systems (not ret))
837 (if (coding-system-equal coding-system
838 (car coding-systems))
839 (setq ret coding-systems)
840 (setq coding-systems (cdr coding-systems))))
841 ret))
843 ;;; Fontification
845 (defun nxml-unfontify-region (start end)
846 (font-lock-default-unfontify-region start end)
847 (nxml-clear-char-ref-extra-display start end))
849 (defun nxml-extend-region ()
850 "Extend the region to hold the minimum area we can fontify with nXML.
851 Called with `font-lock-beg' and `font-lock-end' dynamically bound."
852 (let ((start font-lock-beg)
853 (end font-lock-end))
855 (nxml-debug-change "nxml-extend-region(input)" start end)
857 (when (< start nxml-prolog-end)
858 (setq start (point-min)))
860 (cond ((<= end nxml-prolog-end)
861 (setq end nxml-prolog-end))
864 (goto-char start)
865 ;; some font-lock backends (like Emacs 22 jit-lock) snap
866 ;; the region to the beginning of the line no matter what
867 ;; we say here. To mitigate the resulting excess
868 ;; fontification, ignore leading whitespace.
869 (skip-syntax-forward " ")
871 ;; find the beginning of the previous tag
872 (when (not (equal (char-after) ?\<))
873 (search-backward "<" nxml-prolog-end t))
874 (nxml-ensure-scan-up-to-date)
875 (nxml-move-outside-backwards)
876 (setq start (point))
878 (while (< (point) end)
879 (nxml-tokenize-forward))
881 (setq end (point))))
883 (when (or (< start font-lock-beg)
884 (> end font-lock-end))
885 (setq font-lock-beg start
886 font-lock-end end)
887 (nxml-debug-change "nxml-extend-region" start end)
888 t)))
890 (defun nxml-fontify-matcher (bound)
891 "Called as font-lock keyword matcher."
892 (syntax-propertize bound)
893 (unless nxml-degraded
894 (nxml-debug-change "nxml-fontify-matcher" (point) bound)
896 (when (< (point) nxml-prolog-end)
897 ;; Prolog needs to be fontified in one go, and
898 ;; nxml-extend-region makes sure we start at BOB.
899 (cl-assert (bobp))
900 (nxml-fontify-prolog)
901 (goto-char nxml-prolog-end))
903 (let (xmltok-errors)
904 (while (and (nxml-tokenize-forward)
905 (<= (point) bound)) ; Intervals are open-ended.
906 (nxml-apply-fontify-rule)))
910 ;; Since we did the fontification internally, tell font-lock to not
911 ;; do anything itself.
912 nil)
914 (defun nxml-fontify-prolog ()
915 "Fontify the prolog.
916 The buffer is assumed to be prepared for fontification.
917 This does not set the fontified property, but it does clear
918 faces appropriately."
919 (let ((regions nxml-prolog-regions))
920 (while regions
921 (let ((region (car regions)))
922 (nxml-apply-fontify-rule (aref region 0)
923 (aref region 1)
924 (aref region 2)))
925 (setq regions (cdr regions)))))
927 ;; Vectors identify a substring of the token to be highlighted in some face.
929 ;; Token types returned by xmltok-forward.
931 (put 'start-tag
932 'nxml-fontify-rule
933 '([nil 1 nxml-tag-delimiter]
934 [-1 nil nxml-tag-delimiter]
935 (element-qname . 1)
936 attributes))
938 (put 'partial-start-tag
939 'nxml-fontify-rule
940 '([nil 1 nxml-tag-delimiter]
941 (element-qname . 1)
942 attributes))
944 (put 'end-tag
945 'nxml-fontify-rule
946 '([nil 1 nxml-tag-delimiter]
947 [1 2 nxml-tag-slash]
948 [-1 nil nxml-tag-delimiter]
949 (element-qname . 2)))
951 (put 'partial-end-tag
952 'nxml-fontify-rule
953 '([nil 1 nxml-tag-delimiter]
954 [1 2 nxml-tag-slash]
955 (element-qname . 2)))
957 (put 'empty-element
958 'nxml-fontify-rule
959 '([nil 1 nxml-tag-delimiter]
960 [-2 -1 nxml-tag-slash]
961 [-1 nil nxml-tag-delimiter]
962 (element-qname . 1)
963 attributes))
965 (put 'partial-empty-element
966 'nxml-fontify-rule
967 '([nil 1 nxml-tag-delimiter]
968 [-1 nil nxml-tag-slash]
969 (element-qname . 1)
970 attributes))
972 (put 'char-ref
973 'nxml-fontify-rule
974 '([nil 2 nxml-char-ref-delimiter]
975 [2 -1 nxml-char-ref-number]
976 [-1 nil nxml-char-ref-delimiter]
977 char-ref))
979 (put 'entity-ref
980 'nxml-fontify-rule
981 '([nil 1 nxml-entity-ref-delimiter]
982 [1 -1 nxml-entity-ref-name]
983 [-1 nil nxml-entity-ref-delimiter]))
985 (put 'comment
986 'nxml-fontify-rule
987 '([nil 4 nxml-comment-delimiter]
988 [4 -3 nxml-comment-content]
989 [-3 nil nxml-comment-delimiter]))
991 (put 'processing-instruction
992 'nxml-fontify-rule
993 '([nil 2 nxml-processing-instruction-delimiter]
994 [-2 nil nxml-processing-instruction-delimiter]
995 processing-instruction-content))
997 (put 'cdata-section
998 'nxml-fontify-rule
999 '([nil 3 nxml-cdata-section-delimiter] ; <![
1000 [3 8 nxml-cdata-section-CDATA] ; CDATA
1001 [8 9 nxml-cdata-section-delimiter] ; [
1002 [9 -3 nxml-cdata-section-content] ; ]]>
1003 [-3 nil nxml-cdata-section-delimiter]))
1005 (put 'data
1006 'nxml-fontify-rule
1007 '([nil nil nxml-text]))
1009 ;; Prolog region types in list returned by xmltok-forward-prolog.
1011 (put 'xml-declaration
1012 'nxml-fontify-rule
1013 '([nil 2 nxml-processing-instruction-delimiter]
1014 [2 5 nxml-processing-instruction-target]
1015 [-2 nil nxml-processing-instruction-delimiter]))
1017 (put 'xml-declaration-attribute-name
1018 'nxml-fontify-rule
1019 '([nil nil nxml-attribute-local-name]))
1021 (put 'xml-declaration-attribute-value
1022 'nxml-fontify-rule
1023 '([nil 1 nxml-attribute-value-delimiter]
1024 [1 -1 nxml-attribute-value]
1025 [-1 nil nxml-attribute-value-delimiter]))
1027 (put 'processing-instruction-left
1028 'nxml-fontify-rule
1029 '([nil 2 nxml-processing-instruction-delimiter]
1030 [2 nil nxml-processing-instruction-target]))
1032 (put 'processing-instruction-right
1033 'nxml-fontify-rule
1034 '([nil -2 nxml-processing-instruction-content]
1035 [-2 nil nxml-processing-instruction-delimiter]))
1037 (put 'literal
1038 'nxml-fontify-rule
1039 '([nil 1 nxml-prolog-literal-delimiter]
1040 [1 -1 nxml-prolog-literal-content]
1041 [-1 nil nxml-prolog-literal-delimiter]))
1043 (put 'keyword
1044 'nxml-fontify-rule
1045 '([nil nil nxml-prolog-keyword]))
1047 (put 'markup-declaration-open
1048 'nxml-fontify-rule
1049 '([0 2 nxml-markup-declaration-delimiter]
1050 [2 nil nxml-prolog-keyword]))
1052 (put 'markup-declaration-close
1053 'nxml-fontify-rule
1054 '([nil nil nxml-markup-declaration-delimiter]))
1056 (put 'internal-subset-open
1057 'nxml-fontify-rule
1058 '([nil nil nxml-markup-declaration-delimiter]))
1060 (put 'internal-subset-close
1061 'nxml-fontify-rule
1062 '([nil 1 nxml-markup-declaration-delimiter]
1063 [-1 nil nxml-markup-declaration-delimiter]))
1065 (put 'hash-name
1066 'nxml-fontify-rule
1067 '([nil 1 nxml-hash]
1068 [1 nil nxml-prolog-keyword]))
1070 (defun nxml-apply-fontify-rule (&optional type start end)
1071 (let ((rule (get (or type xmltok-type) 'nxml-fontify-rule)))
1072 (unless start (setq start xmltok-start))
1073 (unless end (setq end (point)))
1074 (while rule
1075 (let* ((action (car rule)))
1076 (setq rule (cdr rule))
1077 (cond ((vectorp action)
1078 (nxml-set-face (let ((offset (aref action 0)))
1079 (cond ((not offset) start)
1080 ((< offset 0) (+ end offset))
1081 (t (+ start offset))))
1082 (let ((offset (aref action 1)))
1083 (cond ((not offset) end)
1084 ((< offset 0) (+ end offset))
1085 (t (+ start offset))))
1086 (aref action 2)))
1087 ((and (consp action)
1088 (eq (car action) 'element-qname))
1089 (when xmltok-name-end ; maybe nil in partial-end-tag case
1090 (nxml-fontify-qname (+ start (cdr action))
1091 xmltok-name-colon
1092 xmltok-name-end
1093 'nxml-element-prefix
1094 'nxml-element-colon
1095 'nxml-element-local-name)))
1096 ((eq action 'attributes)
1097 (nxml-fontify-attributes))
1098 ((eq action 'processing-instruction-content)
1099 (nxml-set-face (+ start 2)
1100 xmltok-name-end
1101 'nxml-processing-instruction-target)
1102 (nxml-set-face (save-excursion
1103 (goto-char xmltok-name-end)
1104 (skip-chars-forward " \t\r\n")
1105 (point))
1106 (- end 2)
1107 'nxml-processing-instruction-content))
1108 ((eq action 'char-ref)
1109 (nxml-char-ref-display-extra start
1111 (xmltok-char-number start end)))
1112 (t (error "Invalid nxml-fontify-rule action %s" action)))))))
1114 (defun nxml-fontify-attributes ()
1115 (while xmltok-namespace-attributes
1116 (nxml-fontify-attribute (car xmltok-namespace-attributes)
1117 'namespace)
1118 (setq xmltok-namespace-attributes
1119 (cdr xmltok-namespace-attributes)))
1120 (while xmltok-attributes
1121 (nxml-fontify-attribute (car xmltok-attributes))
1122 (setq xmltok-attributes
1123 (cdr xmltok-attributes))))
1125 (defun nxml-fontify-attribute (att &optional namespace-declaration)
1126 (if namespace-declaration
1127 (nxml-fontify-qname (xmltok-attribute-name-start att)
1128 (xmltok-attribute-name-colon att)
1129 (xmltok-attribute-name-end att)
1130 'nxml-namespace-attribute-xmlns
1131 'nxml-namespace-attribute-colon
1132 'nxml-namespace-attribute-prefix
1133 'nxml-namespace-attribute-xmlns)
1134 (nxml-fontify-qname (xmltok-attribute-name-start att)
1135 (xmltok-attribute-name-colon att)
1136 (xmltok-attribute-name-end att)
1137 'nxml-attribute-prefix
1138 'nxml-attribute-colon
1139 'nxml-attribute-local-name))
1140 (let ((start (xmltok-attribute-value-start att))
1141 (end (xmltok-attribute-value-end att))
1142 (refs (xmltok-attribute-refs att))
1143 (delimiter-face (if namespace-declaration
1144 'nxml-namespace-attribute-value-delimiter
1145 'nxml-attribute-value-delimiter))
1146 (value-face (if namespace-declaration
1147 'nxml-namespace-attribute-value
1148 'nxml-attribute-value)))
1149 (when start
1150 (nxml-set-face (1- start) start delimiter-face)
1151 (nxml-set-face end (1+ end) delimiter-face)
1152 (while refs
1153 (let* ((ref (car refs))
1154 (ref-type (aref ref 0))
1155 (ref-start (aref ref 1))
1156 (ref-end (aref ref 2)))
1157 (nxml-set-face start ref-start value-face)
1158 (nxml-apply-fontify-rule ref-type ref-start ref-end)
1159 (setq start ref-end))
1160 (setq refs (cdr refs)))
1161 (nxml-set-face start end value-face))))
1163 (defun nxml-fontify-qname (start
1164 colon
1166 prefix-face
1167 colon-face
1168 local-name-face
1169 &optional
1170 unprefixed-face)
1171 (cond (colon (nxml-set-face start colon prefix-face)
1172 (nxml-set-face colon (1+ colon) colon-face)
1173 (nxml-set-face (1+ colon) end local-name-face))
1174 (t (nxml-set-face start end (or unprefixed-face
1175 local-name-face)))))
1177 ;;; Editing
1179 (defun nxml-electric-slash (arg)
1180 "Insert a slash.
1182 With a prefix ARG, do nothing other than insert the slash.
1184 Otherwise, if `nxml-slash-auto-complete-flag' is non-nil, insert the
1185 rest of the end-tag or empty-element if the slash is potentially part
1186 of an end-tag or the close of an empty-element.
1188 If the slash is part of an end-tag that is the first non-whitespace
1189 on the line, reindent the line."
1190 (interactive "*P")
1191 (nxml-ensure-scan-up-to-date)
1192 (let* ((slash-pos (point))
1193 (end-tag-p (and (eq (char-before slash-pos) ?<)
1194 (not (nxml-get-inside slash-pos))))
1195 (at-indentation (save-excursion
1196 (back-to-indentation)
1197 (eq (point) (1- slash-pos)))))
1198 (self-insert-command (prefix-numeric-value arg))
1199 (unless arg
1200 (if nxml-slash-auto-complete-flag
1201 (if end-tag-p
1202 (condition-case nil
1203 (let ((start-tag-end
1204 (nxml-scan-element-backward (1- slash-pos) t)))
1205 (when start-tag-end
1206 (insert (xmltok-start-tag-qname) ">")
1207 ;; copy the indentation of the start-tag
1208 (when (and at-indentation
1209 (save-excursion
1210 (goto-char xmltok-start)
1211 (back-to-indentation)
1212 (eq (point) xmltok-start)))
1213 (save-excursion
1214 (indent-line-to (save-excursion
1215 (goto-char xmltok-start)
1216 (current-column)))))))
1217 (nxml-scan-error nil))
1218 (when (and (eq (nxml-token-before) (point))
1219 (eq xmltok-type 'partial-empty-element))
1220 (insert ">"))))
1221 (when (and end-tag-p at-indentation)
1222 (nxml-indent-line)))))
1224 (defun nxml-balanced-close-start-tag-block ()
1225 "Close the start-tag before point with `>' and insert a balancing end-tag.
1226 Point is left between the start-tag and the end-tag.
1227 If there is nothing but whitespace before the `<' that opens the
1228 start-tag, then put point on a blank line, and put the end-tag on
1229 another line aligned with the start-tag."
1230 (interactive "*")
1231 (nxml-balanced-close-start-tag 'block))
1233 (defun nxml-balanced-close-start-tag-inline ()
1234 "Close the start-tag before point with `>' and insert a balancing end-tag.
1235 Point is left between the start-tag and the end-tag.
1236 No extra whitespace is inserted."
1237 (interactive "*")
1238 (nxml-balanced-close-start-tag 'inline))
1240 (defun nxml-balanced-close-start-tag (block-or-inline)
1241 (let ((token-end (nxml-token-before))
1242 (pos (1+ (point)))
1243 (token-start xmltok-start))
1244 (unless (or (eq xmltok-type 'partial-start-tag)
1245 (and (memq xmltok-type '(start-tag
1246 empty-element
1247 partial-empty-element))
1248 (>= token-end pos)))
1249 (error "Not in a start-tag"))
1250 ;; Note that this insertion changes xmltok-start.
1251 (insert "></"
1252 (buffer-substring-no-properties (+ xmltok-start 1)
1253 (min xmltok-name-end (point)))
1254 ">")
1255 (if (eq block-or-inline 'inline)
1256 (goto-char pos)
1257 (goto-char token-start)
1258 (back-to-indentation)
1259 (if (= (point) token-start)
1260 (let ((indent (current-column)))
1261 (goto-char pos)
1262 (insert "\n")
1263 (indent-line-to indent)
1264 (goto-char pos)
1265 (insert "\n")
1266 (indent-line-to (+ nxml-child-indent indent)))
1267 (goto-char pos)))))
1269 (defun nxml-finish-element ()
1270 "Finish the current element by inserting an end-tag."
1271 (interactive "*")
1272 (nxml-finish-element-1 nil))
1274 (defvar nxml-last-split-position nil
1275 "Position where `nxml-split-element' split the current element.")
1277 (defun nxml-split-element ()
1278 "Split the current element by inserting an end-tag and a start-tag.
1279 Point is left after the newly inserted start-tag. When repeated,
1280 split immediately before the previously inserted start-tag and leave
1281 point unchanged."
1282 (interactive "*")
1283 (setq nxml-last-split-position
1284 (if (and (eq last-command this-command)
1285 nxml-last-split-position)
1286 (save-excursion
1287 (goto-char nxml-last-split-position)
1288 (nxml-finish-element-1 t))
1289 (nxml-finish-element-1 t))))
1291 (defun nxml-finish-element-1 (startp)
1292 "Insert an end-tag for the current element and optionally a start-tag.
1293 The start-tag is inserted if STARTP is non-nil. Return the position
1294 of the inserted start-tag or nil if none was inserted."
1295 (interactive "*")
1296 (let* ((token-end (nxml-token-before))
1297 (start-tag-end
1298 (save-excursion
1299 (when (and (< (point) token-end)
1300 (memq xmltok-type
1301 '(cdata-section
1302 processing-instruction
1303 comment
1304 start-tag
1305 end-tag
1306 empty-element)))
1307 (error "Point is inside a %s"
1308 (nxml-token-type-friendly-name xmltok-type)))
1309 (nxml-scan-element-backward token-end t)))
1310 (starts-line
1311 (save-excursion
1312 (unless (eq xmltok-type 'start-tag)
1313 (error "No matching start-tag"))
1314 (goto-char xmltok-start)
1315 (back-to-indentation)
1316 (eq (point) xmltok-start)))
1317 (ends-line
1318 (save-excursion
1319 (goto-char start-tag-end)
1320 (looking-at "[ \t\r\n]*$")))
1321 (start-tag-indent (save-excursion
1322 (goto-char xmltok-start)
1323 (current-column)))
1324 (qname (xmltok-start-tag-qname))
1325 inserted-start-tag-pos)
1326 (when (and starts-line ends-line)
1327 ;; start-tag is on a line by itself
1328 ;; => put the end-tag on a line by itself
1329 (unless (<= (point)
1330 (save-excursion
1331 (back-to-indentation)
1332 (point)))
1333 (insert "\n"))
1334 (indent-line-to start-tag-indent))
1335 (insert "</" qname ">")
1336 (when startp
1337 (when starts-line
1338 (insert "\n")
1339 (indent-line-to start-tag-indent))
1340 (setq inserted-start-tag-pos (point))
1341 (insert "<" qname ">")
1342 (when (and starts-line ends-line)
1343 (insert "\n")
1344 (indent-line-to (save-excursion
1345 (goto-char xmltok-start)
1346 (forward-line 1)
1347 (back-to-indentation)
1348 (if (= (current-column)
1349 (+ start-tag-indent nxml-child-indent))
1350 (+ start-tag-indent nxml-child-indent)
1351 start-tag-indent)))))
1352 inserted-start-tag-pos))
1354 (defun nxml-comment-quote-nested (_cs _ce unp)
1355 "Quote nested comments in buffer.
1356 See `comment-quote-nested-function' for more information."
1357 (goto-char (point-min))
1358 (save-match-data
1359 (while (re-search-forward "-[\\]*-" nil t)
1360 (goto-char (match-beginning 0))
1361 (forward-char 1)
1362 (if unp
1363 (delete-char 1)
1364 (insert "\\")))))
1366 ;;; Indentation
1368 (defun nxml-indent-line ()
1369 "Indent current line as XML."
1370 (let* ((savep (point))
1371 (indent (condition-case nil
1372 (save-excursion
1373 (forward-line 0)
1374 (skip-chars-forward " \t")
1375 (if (>= (point) savep) (setq savep nil))
1376 (or (nxml-compute-indent) 0))
1377 (error 0))))
1378 (if (not (numberp indent))
1379 ;; If something funny is used (e.g. `noindent'), return it.
1380 indent
1381 (if (< indent 0) (setq indent 0)) ;Just in case.
1382 (if savep
1383 (save-excursion (indent-line-to indent))
1384 (indent-line-to indent)))))
1386 (defun nxml-compute-indent ()
1387 "Return the indent for the line containing point."
1388 (or (nxml-compute-indent-from-matching-start-tag)
1389 (nxml-compute-indent-from-previous-line)))
1391 (defun nxml-compute-indent-from-matching-start-tag ()
1392 "Compute the indent for a line with an end-tag using the matching start-tag.
1393 When the line containing point ends with an end-tag and does not start
1394 in the middle of a token, return the indent of the line containing the
1395 matching start-tag, if there is one and it occurs at the beginning of
1396 its line. Otherwise return nil."
1397 (save-excursion
1398 (back-to-indentation)
1399 (let ((bol (point)))
1400 (let ((inhibit-field-text-motion t))
1401 (end-of-line))
1402 (skip-chars-backward " \t")
1403 (and (= (nxml-token-before) (point))
1404 (memq xmltok-type '(end-tag partial-end-tag))
1405 ;; start of line must not be inside a token
1406 (or (= xmltok-start bol)
1407 (save-excursion
1408 (goto-char bol)
1409 (nxml-token-after)
1410 (= xmltok-start bol))
1411 (eq xmltok-type 'data))
1412 (condition-case nil
1413 (nxml-scan-element-backward
1414 (point)
1416 (- (point)
1417 nxml-end-tag-indent-scan-distance))
1418 (nxml-scan-error nil))
1419 (< xmltok-start bol)
1420 (progn
1421 (goto-char xmltok-start)
1422 (skip-chars-backward " \t")
1423 (bolp))
1424 (current-indentation)))))
1426 (defun nxml-compute-indent-from-previous-line ()
1427 "Compute the indent for a line using the indentation of a previous line."
1428 (save-excursion
1429 (end-of-line)
1430 (let ((eol (point))
1431 bol prev-bol ref
1432 before-context after-context)
1433 (back-to-indentation)
1434 (setq bol (point))
1435 (catch 'indent
1436 ;; Move backwards until the start of a non-blank line that is
1437 ;; not inside a token.
1438 (while (progn
1439 (when (= (forward-line -1) -1)
1440 (throw 'indent 0))
1441 (back-to-indentation)
1442 (if (looking-at "[ \t]*$")
1444 (or prev-bol
1445 (setq prev-bol (point)))
1446 (nxml-token-after)
1447 (not (or (= xmltok-start (point))
1448 (eq xmltok-type 'data))))))
1449 (setq ref (point))
1450 ;; Now scan over tokens until the end of the line to be indented.
1451 ;; Determine the context before and after the beginning of the
1452 ;; line.
1453 (while (< (point) eol)
1454 (nxml-tokenize-forward)
1455 (cond ((<= bol xmltok-start)
1456 (setq after-context
1457 (nxml-merge-indent-context-type after-context)))
1458 ((and (<= (point) bol)
1459 (not (and (eq xmltok-type 'partial-start-tag)
1460 (= (point) bol))))
1461 (setq before-context
1462 (nxml-merge-indent-context-type before-context)))
1463 ((eq xmltok-type 'data)
1464 (setq before-context
1465 (nxml-merge-indent-context-type before-context))
1466 (setq after-context
1467 (nxml-merge-indent-context-type after-context)))
1468 ;; If in the middle of a token that looks inline,
1469 ;; then indent relative to the previous non-blank line
1470 ((eq (nxml-merge-indent-context-type before-context)
1471 'mixed)
1472 (goto-char prev-bol)
1473 (throw 'indent (current-column)))
1475 (throw 'indent
1476 (nxml-compute-indent-in-token bol))))
1477 (skip-chars-forward " \t\r\n"))
1478 (goto-char ref)
1479 (+ (current-column)
1480 (* nxml-child-indent
1481 (+ (if (eq before-context 'start-tag) 1 0)
1482 (if (eq after-context 'end-tag) -1 0))))))))
1484 (defun nxml-merge-indent-context-type (context)
1485 "Merge the indent context type CONTEXT with the token in `xmltok-type'.
1486 Return the merged indent context type. An indent context type is
1487 either nil or one of the symbols `start-tag', `end-tag', `markup',
1488 `comment', `mixed'."
1489 (cond ((memq xmltok-type '(start-tag partial-start-tag))
1490 (if (memq context '(nil start-tag comment))
1491 'start-tag
1492 'mixed))
1493 ((memq xmltok-type '(end-tag partial-end-tag))
1494 (if (memq context '(nil end-tag comment))
1495 'end-tag
1496 'mixed))
1497 ((eq xmltok-type 'comment)
1498 (cond ((memq context '(start-tag end-tag comment))
1499 context)
1500 (context 'mixed)
1501 (t 'comment)))
1502 (context 'mixed)
1503 (t 'markup)))
1505 (defun nxml-compute-indent-in-token (pos)
1506 "Return the indent for a line that starts inside a token.
1507 POS is the position of the first non-whitespace character of the line.
1508 This expects the xmltok-* variables to be set up as by `xmltok-forward'."
1509 (cond ((memq xmltok-type '(start-tag
1510 partial-start-tag
1511 empty-element
1512 partial-empty-element))
1513 (nxml-compute-indent-in-start-tag pos))
1514 ((eq xmltok-type 'comment)
1515 (nxml-compute-indent-in-delimited-token pos "<!--" "-->"))
1516 ((eq xmltok-type 'cdata-section)
1517 (nxml-compute-indent-in-delimited-token pos "<![CDATA[" "]]>"))
1518 ((eq xmltok-type 'processing-instruction)
1519 (nxml-compute-indent-in-delimited-token pos "<?" "?>"))
1521 (goto-char pos)
1522 (if (and (= (forward-line -1) 0)
1523 (< xmltok-start (point)))
1524 (back-to-indentation)
1525 (goto-char xmltok-start))
1526 (current-column))))
1528 (defun nxml-compute-indent-in-start-tag (pos)
1529 "Return the indent for a line that starts inside a start-tag.
1530 Also for a line that starts inside an empty element.
1531 POS is the position of the first non-whitespace character of the line.
1532 This expects the xmltok-* variables to be set up as by `xmltok-forward'."
1533 (let ((value-boundary (nxml-attribute-value-boundary pos))
1534 (off 0))
1535 (if value-boundary
1536 ;; inside an attribute value
1537 (let ((value-start (car value-boundary)))
1538 (goto-char pos)
1539 (forward-line -1)
1540 (if (< (point) value-start)
1541 (goto-char value-start)
1542 (back-to-indentation)))
1543 ;; outside an attribute value
1544 (goto-char pos)
1545 (while (and (= (forward-line -1) 0)
1546 (nxml-attribute-value-boundary (point))))
1547 (cond ((<= (point) xmltok-start)
1548 (goto-char xmltok-start)
1549 (setq off nxml-attribute-indent)
1550 (let ((atts (xmltok-merge-attributes)))
1551 (when atts
1552 (let* ((att (car atts))
1553 (start (xmltok-attribute-name-start att)))
1554 (when (< start pos)
1555 (goto-char start)
1556 (setq off 0))))))
1558 (back-to-indentation))))
1559 (+ (current-column) off)))
1561 (defun nxml-attribute-value-boundary (pos)
1562 "Return a pair (START . END) if POS is inside an attribute value.
1563 Otherwise return nil. START and END are the positions of the start
1564 and end of the attribute value containing POS. This expects the
1565 xmltok-* variables to be set up as by `xmltok-forward'."
1566 (let ((atts (xmltok-merge-attributes))
1567 att value-start value-end value-boundary)
1568 (while atts
1569 (setq att (car atts))
1570 (setq value-start (xmltok-attribute-value-start att))
1571 (setq value-end (xmltok-attribute-value-end att))
1572 (cond ((and value-start (< pos value-start))
1573 (setq atts nil))
1574 ((and value-start value-end (<= pos value-end))
1575 (setq value-boundary (cons value-start value-end))
1576 (setq atts nil))
1577 (t (setq atts (cdr atts)))))
1578 value-boundary))
1580 (defun nxml-compute-indent-in-delimited-token (pos open-delim close-delim)
1581 "Return the indent for a line that starts inside a token with delimiters.
1582 OPEN-DELIM and CLOSE-DELIM are strings giving the opening and closing
1583 delimiters. POS is the position of the first non-whitespace character
1584 of the line. This expects the xmltok-* variables to be set up as by
1585 `xmltok-forward'."
1586 (cond ((let ((end (+ pos (length close-delim))))
1587 (and (<= end (point-max))
1588 (string= (buffer-substring-no-properties pos end)
1589 close-delim)))
1590 (goto-char xmltok-start))
1591 ((progn
1592 (goto-char pos)
1593 (forward-line -1)
1594 (<= (point) xmltok-start))
1595 (goto-char (+ xmltok-start (length open-delim)))
1596 (when (and (string= open-delim "<!--")
1597 (looking-at " "))
1598 (goto-char (1+ (point)))))
1599 (t (back-to-indentation)))
1600 (current-column))
1602 ;;; Completion
1604 (defun nxml-complete ()
1605 "Perform completion on the symbol preceding point.
1607 Inserts as many characters as can be completed. However, if not even
1608 one character can be completed, then a buffer with the possibilities
1609 is popped up and the symbol is read from the minibuffer with
1610 completion. If the symbol is complete, then any characters that must
1611 follow the symbol are also inserted.
1613 The name space used for completion and what is treated as a symbol
1614 depends on the context. The contexts in which completion is performed
1615 depend on `nxml-completion-hook'."
1616 (interactive)
1617 (unless (run-hook-with-args-until-success 'nxml-completion-hook)
1618 ;; Eventually we will complete on entity names here.
1619 (ding)
1620 (message "Cannot complete in this context")))
1622 (defun nxml-completion-at-point-function ()
1623 "Call `nxml-complete' to perform completion at point."
1624 (when nxml-bind-meta-tab-to-complete-flag
1625 #'nxml-complete))
1627 ;;; Movement
1629 (defun nxml-forward-balanced-item (&optional arg)
1630 "Move forward across one balanced item.
1631 With ARG, do it that many times. Negative arg -N means
1632 move backward across N balanced expressions.
1633 This is the equivalent of `forward-sexp' for XML.
1635 An element contains as items strings with no markup, tags, processing
1636 instructions, comments, CDATA sections, entity references and
1637 characters references. However, if the variable
1638 `nxml-sexp-element-flag' is non-nil, then an element is treated as a
1639 single markup item. A start-tag contains an element name followed by
1640 one or more attributes. An end-tag contains just an element name.
1641 An attribute value literals contains strings with no markup, entity
1642 references and character references. A processing instruction
1643 consists of a target and a content string. A comment or a CDATA
1644 section contains a single string. An entity reference contains a
1645 single name. A character reference contains a character number."
1646 (interactive "p")
1647 (or arg (setq arg 1))
1648 (cond ((> arg 0)
1649 (while (progn
1650 (nxml-forward-single-balanced-item)
1651 (> (setq arg (1- arg)) 0))))
1652 ((< arg 0)
1653 (while (progn
1654 (nxml-backward-single-balanced-item)
1655 (< (setq arg (1+ arg)) 0))))))
1657 (defun nxml-forward-single-balanced-item ()
1658 (condition-case err
1659 (goto-char (let ((end (nxml-token-after)))
1660 (save-excursion
1661 (while (eq xmltok-type 'space)
1662 (goto-char end)
1663 (setq end (nxml-token-after)))
1664 (cond ((/= (point) xmltok-start)
1665 (nxml-scan-forward-within end))
1666 ((and nxml-sexp-element-flag
1667 (eq xmltok-type 'start-tag))
1668 ;; can't ever return nil here
1669 (nxml-scan-element-forward xmltok-start))
1670 ((and nxml-sexp-element-flag
1671 (memq xmltok-type
1672 '(end-tag partial-end-tag)))
1673 (error "Already at end of element"))
1674 (t end)))))
1675 (nxml-scan-error
1676 (goto-char (cadr err))
1677 (apply 'error (cddr err)))))
1679 (defun nxml-backward-single-balanced-item ()
1680 (condition-case err
1681 (goto-char (let ((end (nxml-token-before)))
1682 (save-excursion
1683 (while (eq xmltok-type 'space)
1684 (goto-char xmltok-start)
1685 (setq end (nxml-token-before)))
1686 (cond ((/= (point) end)
1687 (nxml-scan-backward-within end))
1688 ((and nxml-sexp-element-flag
1689 (eq xmltok-type 'end-tag))
1690 ;; can't ever return nil here
1691 (nxml-scan-element-backward end)
1692 xmltok-start)
1693 ((and nxml-sexp-element-flag
1694 (eq xmltok-type 'start-tag))
1695 (error "Already at start of element"))
1696 (t xmltok-start)))))
1697 (nxml-scan-error
1698 (goto-char (cadr err))
1699 (apply 'error (cddr err)))))
1701 (defun nxml-scan-forward-within (end)
1702 (setq end (- end (nxml-end-delimiter-length xmltok-type)))
1703 (when (<= end (point))
1704 (error "Already at end of %s"
1705 (nxml-token-type-friendly-name xmltok-type)))
1706 (cond ((memq xmltok-type '(start-tag
1707 empty-element
1708 partial-start-tag
1709 partial-empty-element))
1710 (if (< (point) xmltok-name-end)
1711 xmltok-name-end
1712 (let ((att (nxml-find-following-attribute)))
1713 (cond ((not att) end)
1714 ((and (xmltok-attribute-value-start att)
1715 (<= (xmltok-attribute-value-start att)
1716 (point)))
1717 (nxml-scan-forward-in-attribute-value att))
1718 ((xmltok-attribute-value-end att)
1719 (1+ (xmltok-attribute-value-end att)))
1720 ((save-excursion
1721 (goto-char (xmltok-attribute-name-end att))
1722 (looking-at "[ \t\r\n]*="))
1723 (match-end 0))
1724 (t (xmltok-attribute-name-end att))))))
1725 ((and (eq xmltok-type 'processing-instruction)
1726 (< (point) xmltok-name-end))
1727 xmltok-name-end)
1728 (t end)))
1730 (defun nxml-scan-backward-within (_end)
1731 (setq xmltok-start
1732 (+ xmltok-start
1733 (nxml-start-delimiter-length xmltok-type)))
1734 (when (<= (point) xmltok-start)
1735 (error "Already at start of %s"
1736 (nxml-token-type-friendly-name xmltok-type)))
1737 (cond ((memq xmltok-type '(start-tag
1738 empty-element
1739 partial-start-tag
1740 partial-empty-element))
1741 (let ((att (nxml-find-preceding-attribute)))
1742 (cond ((not att) xmltok-start)
1743 ((and (xmltok-attribute-value-start att)
1744 (<= (xmltok-attribute-value-start att)
1745 (point))
1746 (<= (point)
1747 (xmltok-attribute-value-end att)))
1748 (nxml-scan-backward-in-attribute-value att))
1749 (t (xmltok-attribute-name-start att)))))
1750 ((and (eq xmltok-type 'processing-instruction)
1751 (let ((content-start (save-excursion
1752 (goto-char xmltok-name-end)
1753 (skip-chars-forward " \r\t\n")
1754 (point))))
1755 (and (< content-start (point))
1756 content-start))))
1757 (t xmltok-start)))
1759 (defun nxml-scan-forward-in-attribute-value (att)
1760 (when (= (point) (xmltok-attribute-value-end att))
1761 (error "Already at end of attribute value"))
1762 (let ((refs (xmltok-attribute-refs att))
1763 ref)
1764 (while refs
1765 (setq ref (car refs))
1766 (if (< (point) (aref ref 2))
1767 (setq refs nil)
1768 (setq ref nil)
1769 (setq refs (cdr refs))))
1770 (cond ((not ref)
1771 (xmltok-attribute-value-end att))
1772 ((< (point) (aref ref 1))
1773 (aref ref 1))
1774 ((= (point) (aref ref 1))
1775 (aref ref 2))
1777 (let ((end (- (aref ref 2)
1778 (nxml-end-delimiter-length (aref ref 0)))))
1779 (if (< (point) end)
1781 (error "Already at end of %s"
1782 (nxml-token-type-friendly-name (aref ref 0)))))))))
1784 (defun nxml-scan-backward-in-attribute-value (att)
1785 (when (= (point) (xmltok-attribute-value-start att))
1786 (error "Already at start of attribute value"))
1787 (let ((refs (reverse (xmltok-attribute-refs att)))
1788 ref)
1789 (while refs
1790 (setq ref (car refs))
1791 (if (< (aref ref 1) (point))
1792 (setq refs nil)
1793 (setq ref nil)
1794 (setq refs (cdr refs))))
1795 (cond ((not ref)
1796 (xmltok-attribute-value-start att))
1797 ((< (aref ref 2) (point))
1798 (aref ref 2))
1799 ((= (point) (aref ref 2))
1800 (aref ref 1))
1802 (let ((start (+ (aref ref 1)
1803 (nxml-start-delimiter-length (aref ref 0)))))
1804 (if (< start (point))
1805 start
1806 (error "Already at start of %s"
1807 (nxml-token-type-friendly-name (aref ref 0)))))))))
1809 (defun nxml-find-following-attribute ()
1810 (let ((ret nil)
1811 (atts (or xmltok-attributes xmltok-namespace-attributes))
1812 (more-atts (and xmltok-attributes xmltok-namespace-attributes)))
1813 (while atts
1814 (let* ((att (car atts))
1815 (name-start (xmltok-attribute-name-start att)))
1816 (cond ((and (<= name-start (point))
1817 (xmltok-attribute-value-end att)
1818 ;; <= because end is before quote
1819 (<= (point) (xmltok-attribute-value-end att)))
1820 (setq atts nil)
1821 (setq ret att))
1822 ((and (< (point) name-start)
1823 (or (not ret)
1824 (< name-start
1825 (xmltok-attribute-name-start ret))))
1826 (setq ret att))))
1827 (setq atts (cdr atts))
1828 (unless atts
1829 (setq atts more-atts)
1830 (setq more-atts nil)))
1831 ret))
1833 (defun nxml-find-preceding-attribute ()
1834 (let ((ret nil)
1835 (atts (or xmltok-attributes xmltok-namespace-attributes))
1836 (more-atts (and xmltok-attributes xmltok-namespace-attributes)))
1837 (while atts
1838 (let* ((att (car atts))
1839 (name-start (xmltok-attribute-name-start att)))
1840 (cond ((and (< name-start (point))
1841 (xmltok-attribute-value-end att)
1842 ;; <= because end is before quote
1843 (<= (point) (xmltok-attribute-value-end att)))
1844 (setq atts nil)
1845 (setq ret att))
1846 ((and (< name-start (point))
1847 (or (not ret)
1848 (< (xmltok-attribute-name-start ret)
1849 name-start)))
1850 (setq ret att))))
1851 (setq atts (cdr atts))
1852 (unless atts
1853 (setq atts more-atts)
1854 (setq more-atts nil)))
1855 ret))
1857 (defun nxml-up-element (&optional arg)
1858 (interactive "p")
1859 (or arg (setq arg 1))
1860 (if (< arg 0)
1861 (nxml-backward-up-element (- arg))
1862 (condition-case err
1863 (while (and (> arg 0)
1864 (< (point) (point-max)))
1865 (let ((token-end (nxml-token-after)))
1866 (goto-char (cond ((or (memq xmltok-type '(end-tag
1867 partial-end-tag))
1868 (and (memq xmltok-type
1869 '(empty-element
1870 partial-empty-element))
1871 (< xmltok-start (point))))
1872 token-end)
1873 ((nxml-scan-element-forward
1874 (if (and (eq xmltok-type 'start-tag)
1875 (= (point) xmltok-start))
1876 xmltok-start
1877 token-end)
1879 (t (error "No parent element")))))
1880 (setq arg (1- arg)))
1881 (nxml-scan-error
1882 (goto-char (cadr err))
1883 (apply 'error (cddr err))))))
1885 (defun nxml-backward-up-element (&optional arg)
1886 (interactive "p")
1887 (or arg (setq arg 1))
1888 (if (< arg 0)
1889 (nxml-up-element (- arg))
1890 (condition-case err
1891 (while (and (> arg 0)
1892 (< (point-min) (point)))
1893 (let ((token-end (nxml-token-before)))
1894 (goto-char (cond ((or (memq xmltok-type '(start-tag
1895 partial-start-tag))
1896 (and (memq xmltok-type
1897 '(empty-element
1898 partial-empty-element))
1899 (< (point) token-end)))
1900 xmltok-start)
1901 ((nxml-scan-element-backward
1902 (if (and (eq xmltok-type 'end-tag)
1903 (= (point) token-end))
1904 token-end
1905 xmltok-start)
1907 xmltok-start)
1908 (t (error "No parent element")))))
1909 (setq arg (1- arg)))
1910 (nxml-scan-error
1911 (goto-char (cadr err))
1912 (apply 'error (cddr err))))))
1914 (defun nxml-down-element (&optional arg)
1915 "Move forward down into the content of an element.
1916 With ARG, do this that many times.
1917 Negative ARG means move backward but still down."
1918 (interactive "p")
1919 (or arg (setq arg 1))
1920 (if (< arg 0)
1921 (nxml-backward-down-element (- arg))
1922 (while (> arg 0)
1923 (goto-char
1924 (let ((token-end (nxml-token-after)))
1925 (save-excursion
1926 (goto-char token-end)
1927 (while (progn
1928 (when (memq xmltok-type '(nil end-tag partial-end-tag))
1929 (error "No following start-tags in this element"))
1930 (not (memq xmltok-type '(start-tag partial-start-tag))))
1931 (nxml-tokenize-forward))
1932 (point))))
1933 (setq arg (1- arg)))))
1935 (defun nxml-backward-down-element (&optional arg)
1936 (interactive "p")
1937 (or arg (setq arg 1))
1938 (if (< arg 0)
1939 (nxml-down-element (- arg))
1940 (while (> arg 0)
1941 (goto-char
1942 (save-excursion
1943 (nxml-token-before)
1944 (goto-char xmltok-start)
1945 (while (progn
1946 (when (memq xmltok-type '(start-tag
1947 partial-start-tag
1948 prolog
1949 nil))
1950 (error "No preceding end-tags in this element"))
1951 (not (memq xmltok-type '(end-tag partial-end-tag))))
1952 (if (or (<= (point) nxml-prolog-end)
1953 (not (search-backward "<" nxml-prolog-end t)))
1954 (setq xmltok-type nil)
1955 (nxml-move-outside-backwards)
1956 (xmltok-forward)))
1957 xmltok-start))
1958 (setq arg (1- arg)))))
1960 (defun nxml-forward-element (&optional arg)
1961 "Move forward over one element.
1962 With ARG, do it that many times.
1963 Negative ARG means move backward."
1964 (interactive "p")
1965 (or arg (setq arg 1))
1966 (if (< arg 0)
1967 (nxml-backward-element (- arg))
1968 (condition-case err
1969 (while (and (> arg 0)
1970 (< (point) (point-max)))
1971 (goto-char
1972 (or (nxml-scan-element-forward (nxml-token-before))
1973 (error "No more elements")))
1974 (setq arg (1- arg)))
1975 (nxml-scan-error
1976 (goto-char (cadr err))
1977 (apply 'error (cddr err))))))
1979 (defun nxml-backward-element (&optional arg)
1980 "Move backward over one element.
1981 With ARG, do it that many times.
1982 Negative ARG means move forward."
1983 (interactive "p")
1984 (or arg (setq arg 1))
1985 (if (< arg 0)
1986 (nxml-forward-element (- arg))
1987 (condition-case err
1988 (while (and (> arg 0)
1989 (< (point-min) (point)))
1990 (goto-char
1991 (or (and (nxml-scan-element-backward (progn
1992 (nxml-token-after)
1993 xmltok-start))
1994 xmltok-start)
1995 (error "No preceding elements")))
1996 (setq arg (1- arg)))
1997 (nxml-scan-error
1998 (goto-char (cadr err))
1999 (apply 'error (cddr err))))))
2001 (defun nxml-mark-token-after ()
2002 (interactive)
2003 (push-mark (nxml-token-after) nil t)
2004 (goto-char xmltok-start)
2005 (message "Marked %s" xmltok-type))
2007 ;;; Paragraphs
2009 (defun nxml-mark-paragraph ()
2010 "Put point at beginning of this paragraph, mark at end.
2011 The paragraph marked is the one that contains point or follows point."
2012 (interactive)
2013 (nxml-forward-paragraph)
2014 (push-mark nil t t)
2015 (nxml-backward-paragraph))
2017 (defun nxml-forward-paragraph (&optional arg)
2018 (interactive "p")
2019 (or arg (setq arg 1))
2020 (cond ((< arg 0)
2021 (nxml-backward-paragraph (- arg)))
2022 ((> arg 0)
2023 (forward-line 0)
2024 (while (and (nxml-forward-single-paragraph)
2025 (> (setq arg (1- arg)) 0))))))
2027 (defun nxml-backward-paragraph (&optional arg)
2028 (interactive "p")
2029 (or arg (setq arg 1))
2030 (cond ((< arg 0)
2031 (nxml-forward-paragraph (- arg)))
2032 ((> arg 0)
2033 (unless (bolp)
2034 (let ((inhibit-field-text-motion t))
2035 (end-of-line)))
2036 (while (and (nxml-backward-single-paragraph)
2037 (> (setq arg (1- arg)) 0))))))
2039 (defun nxml-forward-single-paragraph ()
2040 "Move forward over a single paragraph.
2041 Return nil at end of buffer, t otherwise."
2042 (let* ((token-end (nxml-token-after))
2043 (offset (- (point) xmltok-start))
2044 pos had-data)
2045 (goto-char token-end)
2046 (while (and (< (point) (point-max))
2047 (not (setq pos
2048 (nxml-paragraph-end-pos had-data offset))))
2049 (when (nxml-token-contains-data-p offset)
2050 (setq had-data t))
2051 (nxml-tokenize-forward)
2052 (setq offset 0))
2053 (when pos (goto-char pos))))
2055 (defun nxml-backward-single-paragraph ()
2056 "Move backward over a single paragraph.
2057 Return nil at start of buffer, t otherwise."
2058 (let* ((token-end (nxml-token-before))
2059 (offset (- token-end (point)))
2060 (last-tag-pos xmltok-start)
2061 pos had-data last-data-pos)
2062 (goto-char token-end)
2063 (unless (setq pos (nxml-paragraph-start-pos nil offset))
2064 (setq had-data (nxml-token-contains-data-p nil offset))
2065 (goto-char xmltok-start)
2066 (while (and (not pos) (< (point-min) (point)))
2067 (cond ((search-backward "<" nxml-prolog-end t)
2068 (nxml-move-outside-backwards)
2069 (save-excursion
2070 (while (< (point) last-tag-pos)
2071 (xmltok-forward)
2072 (when (and (not had-data) (nxml-token-contains-data-p))
2073 (setq pos nil)
2074 (setq last-data-pos xmltok-start))
2075 (let ((tem (nxml-paragraph-start-pos had-data 0)))
2076 (when tem (setq pos tem)))))
2077 (when (and (not had-data) last-data-pos (not pos))
2078 (setq had-data t)
2079 (save-excursion
2080 (while (< (point) last-data-pos)
2081 (xmltok-forward))
2082 (let ((tem (nxml-paragraph-start-pos had-data 0)))
2083 (when tem (setq pos tem)))))
2084 (setq last-tag-pos (point)))
2085 (t (goto-char (point-min))))))
2086 (when pos (goto-char pos))))
2088 (defun nxml-token-contains-data-p (&optional start end)
2089 (setq start (+ xmltok-start (or start 0)))
2090 (setq end (- (point) (or end 0)))
2091 (when (eq xmltok-type 'cdata-section)
2092 (setq start (max start (+ xmltok-start 9)))
2093 (setq end (min end (- (point) 3))))
2094 (or (and (eq xmltok-type 'data)
2095 (eq start xmltok-start)
2096 (eq end (point)))
2097 (eq xmltok-type 'char-ref)
2098 (and (memq xmltok-type '(data cdata-section))
2099 (< start end)
2100 (save-excursion
2101 (goto-char start)
2102 (re-search-forward "[^ \t\r\n]" end t)))))
2104 (defun nxml-paragraph-end-pos (had-data offset)
2105 "Return the position of the paragraph end if contained in the current token.
2106 Return nil if the current token does not contain the paragraph end.
2107 Only characters after OFFSET from the start of the token are eligible.
2108 HAD-DATA says whether there have been non-whitespace data characters yet."
2109 (cond ((not had-data)
2110 (cond ((memq xmltok-type '(data cdata-section))
2111 (save-excursion
2112 (let ((end (point)))
2113 (goto-char (+ xmltok-start
2114 (max (if (eq xmltok-type 'cdata-section)
2117 offset)))
2118 (and (re-search-forward "[^ \t\r\n]" end t)
2119 (re-search-forward "^[ \t]*$" end t)
2120 (match-beginning 0)))))
2121 ((and (eq xmltok-type 'comment)
2122 (nxml-token-begins-line-p)
2123 (nxml-token-ends-line-p))
2124 (save-excursion
2125 (let ((end (point)))
2126 (goto-char (+ xmltok-start (max 4 offset)))
2127 (when (re-search-forward "[^ \t\r\n]" (- end 3) t)
2128 (if (re-search-forward "^[ \t]*$" end t)
2129 (match-beginning 0)
2130 (goto-char (- end 3))
2131 (skip-chars-backward " \t")
2132 (unless (bolp)
2133 (beginning-of-line 2))
2134 (point))))))))
2135 ((memq xmltok-type '(data space cdata-section))
2136 (save-excursion
2137 (let ((end (point)))
2138 (goto-char (+ xmltok-start offset))
2139 (and (re-search-forward "^[ \t]*$" end t)
2140 (match-beginning 0)))))
2141 ((and (memq xmltok-type '(start-tag
2142 end-tag
2143 empty-element
2144 comment
2145 processing-instruction
2146 entity-ref))
2147 (nxml-token-begins-line-p)
2148 (nxml-token-ends-line-p))
2149 (save-excursion
2150 (goto-char xmltok-start)
2151 (skip-chars-backward " \t")
2152 (point)))
2153 ((and (eq xmltok-type 'end-tag)
2154 (looking-at "[ \t]*$")
2155 (not (nxml-in-mixed-content-p t)))
2156 (save-excursion
2157 (or (search-forward "\n" nil t)
2158 (point-max))))))
2160 (defun nxml-paragraph-start-pos (had-data offset)
2161 "Return the position of the paragraph start if contained in the current token.
2162 Return nil if the current token does not contain the paragraph start.
2163 Only characters before OFFSET from the end of the token are eligible.
2164 HAD-DATA says whether there have been non-whitespace data characters yet."
2165 (cond ((not had-data)
2166 (cond ((memq xmltok-type '(data cdata-section))
2167 (save-excursion
2168 (goto-char (- (point)
2169 (max (if (eq xmltok-type 'cdata-section)
2172 offset)))
2173 (and (re-search-backward "[^ \t\r\n]" xmltok-start t)
2174 (re-search-backward "^[ \t]*$" xmltok-start t)
2175 (match-beginning 0))))
2176 ((and (eq xmltok-type 'comment)
2177 (nxml-token-ends-line-p)
2178 (nxml-token-begins-line-p))
2179 (save-excursion
2180 (goto-char (- (point) (max 3 offset)))
2181 (when (and (< (+ xmltok-start 4) (point))
2182 (re-search-backward "[^ \t\r\n]"
2183 (+ xmltok-start 4)
2185 (if (re-search-backward "^[ \t]*$" xmltok-start t)
2186 (match-beginning 0)
2187 (goto-char xmltok-start)
2188 (if (looking-at "<!--[ \t]*\n")
2189 (match-end 0)
2190 (skip-chars-backward " \t")
2191 (point))))))))
2192 ((memq xmltok-type '(data space cdata-section))
2193 (save-excursion
2194 (goto-char (- (point) offset))
2195 (and (re-search-backward "^[ \t]*$" xmltok-start t)
2196 (match-beginning 0))))
2197 ((and (memq xmltok-type '(start-tag
2198 end-tag
2199 empty-element
2200 comment
2201 processing-instruction
2202 entity-ref))
2203 (nxml-token-ends-line-p)
2204 (nxml-token-begins-line-p))
2205 (or (search-forward "\n" nil t)
2206 (point-max)))
2207 ((and (eq xmltok-type 'start-tag)
2208 (nxml-token-begins-line-p)
2209 (not (save-excursion
2210 (goto-char xmltok-start)
2211 (nxml-in-mixed-content-p nil))))
2212 (save-excursion
2213 (goto-char xmltok-start)
2214 (skip-chars-backward " \t")
2215 ;; include any blank line before
2216 (or (and (eq (char-before) ?\n)
2217 (save-excursion
2218 (goto-char (1- (point)))
2219 (skip-chars-backward " \t")
2220 (and (bolp) (point))))
2221 (point))))))
2223 (defun nxml-token-ends-line-p () (looking-at "[ \t]*$"))
2225 (defun nxml-token-begins-line-p ()
2226 (save-excursion
2227 (goto-char xmltok-start)
2228 (skip-chars-backward " \t")
2229 (bolp)))
2231 (defun nxml-in-mixed-content-p (endp)
2232 "Return non-nil if point is in mixed content.
2233 Point must be after an end-tag or before a start-tag.
2234 ENDP is t in the former case, nil in the latter."
2235 (let (matching-tag-pos)
2236 (cond ((not (run-hook-with-args-until-failure
2237 'nxml-in-mixed-content-hook))
2238 nil)
2239 ;; See if the matching tag does not start or end a line.
2240 ((condition-case nil
2241 (progn
2242 (setq matching-tag-pos
2243 (xmltok-save
2244 (if endp
2245 (and (nxml-scan-element-backward (point))
2246 xmltok-start)
2247 (nxml-scan-element-forward (point)))))
2248 (and matching-tag-pos
2249 (save-excursion
2250 (goto-char matching-tag-pos)
2251 (not (if endp
2252 (progn
2253 (skip-chars-backward " \t")
2254 (bolp))
2255 (looking-at "[ \t]*$"))))))
2256 (nxml-scan-error nil))
2258 ;; See if there's data at the same level.
2259 ((let (start end)
2260 (if endp
2261 (setq start matching-tag-pos
2262 end (point))
2263 (setq start (point)
2264 end matching-tag-pos))
2265 (save-excursion
2266 (or (when start
2267 (goto-char start)
2268 (nxml-preceding-sibling-data-p))
2269 (when end
2270 (goto-char end)
2271 (nxml-following-sibling-data-p)))))
2273 ;; Otherwise, treat as not mixed
2274 (t nil))))
2276 (defun nxml-preceding-sibling-data-p ()
2277 "Return non-nil if there is a previous sibling that is data."
2278 (let ((lim (max (- (point) nxml-mixed-scan-distance)
2279 nxml-prolog-end))
2280 (level 0)
2281 found end)
2282 (xmltok-save
2283 (save-excursion
2284 (while (and (< lim (point))
2285 (>= level 0)
2286 (not found)
2287 (progn
2288 (setq end (point))
2289 (search-backward "<" lim t)))
2290 (nxml-move-outside-backwards)
2291 (save-excursion
2292 (xmltok-forward)
2293 (let ((prev-level level))
2294 (cond ((eq xmltok-type 'end-tag)
2295 (setq level (1+ level)))
2296 ((eq xmltok-type 'start-tag)
2297 (setq level (1- level))))
2298 (when (eq prev-level 0)
2299 (while (and (< (point) end) (not found))
2300 (xmltok-forward)
2301 (when (memq xmltok-type '(data cdata-section char-ref))
2302 (setq found t)))))))))
2303 found))
2305 (defun nxml-following-sibling-data-p ()
2306 (let ((lim (min (+ (point) nxml-mixed-scan-distance)
2307 (point-max)))
2308 (level 0)
2309 found)
2310 (xmltok-save
2311 (save-excursion
2312 (while (and (< (point) lim)
2313 (>= level 0)
2314 (nxml-tokenize-forward)
2315 (not found))
2316 (cond ((eq xmltok-type 'start-tag)
2317 (setq level (1+ level)))
2318 ((eq xmltok-type 'end-tag)
2319 (setq level (1- level)))
2320 ((and (eq level 0)
2321 (memq xmltok-type '(data cdata-section char-ref)))
2322 (setq found t))))))
2323 found))
2325 ;;; Filling
2327 (defun nxml-do-fill-paragraph (arg)
2328 (let (fill-paragraph-function
2329 fill-prefix
2330 start end)
2331 (save-excursion
2332 (nxml-forward-paragraph)
2333 (setq end (point))
2334 (nxml-backward-paragraph)
2335 (skip-chars-forward " \t\r\n")
2336 (setq start (point))
2337 (beginning-of-line)
2338 (setq fill-prefix (buffer-substring-no-properties (point) start))
2339 (when (and (not (nxml-get-inside (point)))
2340 (looking-at "[ \t]*<!--"))
2341 (setq fill-prefix (concat fill-prefix " ")))
2342 (fill-region-as-paragraph start end arg))
2343 (skip-line-prefix fill-prefix)
2344 fill-prefix))
2346 (defun nxml-newline-and-indent (soft)
2347 (delete-horizontal-space)
2348 (if soft (insert-and-inherit ?\n) (newline 1))
2349 (nxml-indent-line))
2352 ;;; Dynamic markup
2354 (defvar nxml-dynamic-markup-prev-pos nil)
2355 (defvar nxml-dynamic-markup-prev-lengths nil)
2356 (defvar nxml-dynamic-markup-prev-found-marker nil)
2357 (defvar nxml-dynamic-markup-prev-start-tags (make-hash-table :test 'equal))
2359 (defun nxml-dynamic-markup-word ()
2360 "Dynamically markup the word before point.
2361 This attempts to find a tag to put around the word before point based
2362 on the contents of the current buffer. The end-tag will be inserted at
2363 point. The start-tag will be inserted at or before the beginning of
2364 the word before point; the contents of the current buffer is used to
2365 decide where.
2367 It works in a similar way to \\[dabbrev-expand]. It searches first
2368 backwards from point, then forwards from point for an element whose
2369 content is a string which matches the contents of the buffer before
2370 point and which includes at least the word before point. It then
2371 copies the start- and end-tags from that element and uses them to
2372 surround the matching string before point.
2374 Repeating \\[nxml-dynamic-markup-word] immediately after successful
2375 \\[nxml-dynamic-markup-word] removes the previously inserted markup
2376 and attempts to find another possible way to do the markup."
2377 (interactive "*")
2378 (let (search-start-pos)
2379 (if (and (integerp nxml-dynamic-markup-prev-pos)
2380 (= nxml-dynamic-markup-prev-pos (point))
2381 (eq last-command this-command)
2382 nxml-dynamic-markup-prev-lengths)
2383 (let* ((end-tag-open-pos
2384 (- nxml-dynamic-markup-prev-pos
2385 (nth 2 nxml-dynamic-markup-prev-lengths)))
2386 (start-tag-close-pos
2387 (- end-tag-open-pos
2388 (nth 1 nxml-dynamic-markup-prev-lengths)))
2389 (start-tag-open-pos
2390 (- start-tag-close-pos
2391 (nth 0 nxml-dynamic-markup-prev-lengths))))
2392 (delete-region end-tag-open-pos nxml-dynamic-markup-prev-pos)
2393 (delete-region start-tag-open-pos start-tag-close-pos)
2394 (setq search-start-pos
2395 (marker-position nxml-dynamic-markup-prev-found-marker)))
2396 (clrhash nxml-dynamic-markup-prev-start-tags))
2397 (setq nxml-dynamic-markup-prev-pos nil)
2398 (setq nxml-dynamic-markup-prev-lengths nil)
2399 (setq nxml-dynamic-markup-prev-found-marker nil)
2400 (goto-char
2401 (save-excursion
2402 (let* ((pos (point))
2403 (word (progn
2404 (backward-word 1)
2405 (unless (< (point) pos)
2406 (error "No word to markup"))
2407 (buffer-substring-no-properties (point) pos)))
2408 (search (concat word "</"))
2409 done)
2410 (when search-start-pos
2411 (goto-char search-start-pos))
2412 (while (and (not done)
2413 (or (and (< (point) pos)
2414 (or (search-backward search nil t)
2415 (progn (goto-char pos) nil)))
2416 (search-forward search nil t)))
2417 (goto-char (- (match-end 0) 2))
2418 (setq done (nxml-try-copy-markup pos)))
2419 (or done
2420 (error (if (zerop (hash-table-count
2421 nxml-dynamic-markup-prev-start-tags))
2422 "No possible markup found for `%s'"
2423 "No more markup possibilities found for `%s'")
2424 word)))))))
2426 (defun nxml-try-copy-markup (word-end-pos)
2427 (save-excursion
2428 (let ((end-tag-pos (point)))
2429 (when (and (not (nxml-get-inside end-tag-pos))
2430 (search-backward "<" nil t)
2431 (not (nxml-get-inside (point))))
2432 (xmltok-forward)
2433 (when (and (eq xmltok-type 'start-tag)
2434 (< (point) end-tag-pos))
2435 (let* ((start-tag-close-pos (point))
2436 (start-tag
2437 (buffer-substring-no-properties xmltok-start
2438 start-tag-close-pos))
2439 (words
2440 (nreverse
2441 (split-string
2442 (buffer-substring-no-properties start-tag-close-pos
2443 end-tag-pos)
2444 "[ \t\r\n]+"))))
2445 (goto-char word-end-pos)
2446 (while (and words
2447 (re-search-backward (concat
2448 (regexp-quote (car words))
2449 "\\=")
2452 (setq words (cdr words))
2453 (skip-chars-backward " \t\r\n"))
2454 (when (and (not words)
2455 (progn
2456 (skip-chars-forward " \t\r\n")
2457 (not (gethash (cons (point) start-tag)
2458 nxml-dynamic-markup-prev-start-tags)))
2459 (or (< end-tag-pos (point))
2460 (< word-end-pos xmltok-start)))
2461 (setq nxml-dynamic-markup-prev-found-marker
2462 (copy-marker end-tag-pos t))
2463 (puthash (cons (point) start-tag)
2465 nxml-dynamic-markup-prev-start-tags)
2466 (setq nxml-dynamic-markup-prev-lengths
2467 (list (- start-tag-close-pos xmltok-start)
2468 (- word-end-pos (point))
2469 (+ (- xmltok-name-end xmltok-start) 2)))
2470 (let ((name (xmltok-start-tag-qname)))
2471 (insert start-tag)
2472 (goto-char (+ word-end-pos
2473 (- start-tag-close-pos xmltok-start)))
2474 (insert "</" name ">")
2475 (setq nxml-dynamic-markup-prev-pos (point))))))))))
2478 ;;; Character names
2480 (defvar nxml-char-name-ignore-case t)
2482 (defvar nxml-char-name-alist nil
2483 "Alist of character names.
2484 Each member of the list has the form (NAME CODE . NAMESET),
2485 where NAME is a string naming a character, NAMESET is a symbol
2486 identifying a set of names and CODE is an integer specifying the
2487 Unicode scalar value of the named character.
2488 The NAME will only be used for completion if NAMESET has
2489 a non-nil `nxml-char-name-set-enabled' property.
2490 If NAMESET does does not have `nxml-char-name-set-defined' property,
2491 then it must have a `nxml-char-name-set-file' property and `load'
2492 will be applied to the value of this property if the nameset
2493 is enabled.")
2495 (defvar nxml-char-name-table (make-hash-table :test 'eq)
2496 "Hash table for mapping char codes to names.
2497 Each key is a Unicode scalar value.
2498 Each value is a list of pairs of the form (NAMESET . NAME),
2499 where NAMESET is a symbol identifying a set of names,
2500 and NAME is a string naming a character.")
2502 (defvar nxml-autoload-char-name-set-list nil
2503 "List of char namesets that can be autoloaded.")
2505 (defun nxml-enable-char-name-set (nameset)
2506 (put nameset 'nxml-char-name-set-enabled t))
2508 (defun nxml-disable-char-name-set (nameset)
2509 (put nameset 'nxml-char-name-set-enabled nil))
2511 (defun nxml-char-name-set-enabled-p (nameset)
2512 (get nameset 'nxml-char-name-set-enabled))
2514 (defun nxml-autoload-char-name-set (nameset file)
2515 (unless (memq nameset nxml-autoload-char-name-set-list)
2516 (setq nxml-autoload-char-name-set-list
2517 (cons nameset nxml-autoload-char-name-set-list)))
2518 (put nameset 'nxml-char-name-set-file file))
2520 (defun nxml-define-char-name-set (nameset alist)
2521 "Define a set of character names.
2522 NAMESET is a symbol identifying the set.
2523 ALIST is a list where each member has the form (NAME CODE),
2524 where NAME is a string naming a character and code is an
2525 integer giving the Unicode scalar value of the character."
2526 (when (get nameset 'nxml-char-name-set-defined)
2527 (error "Nameset `%s' already defined" nameset))
2528 (let ((iter alist))
2529 (while iter
2530 (let* ((name-code (car iter))
2531 (name (car name-code))
2532 (code (cadr name-code)))
2533 (puthash code
2534 (cons (cons nameset name)
2535 (gethash code nxml-char-name-table))
2536 nxml-char-name-table))
2537 (setcdr (cdr (car iter)) nameset)
2538 (setq iter (cdr iter))))
2539 (setq nxml-char-name-alist
2540 (nconc alist nxml-char-name-alist))
2541 (put nameset 'nxml-char-name-set-defined t))
2543 (defun nxml-get-char-name (code)
2544 (mapc 'nxml-maybe-load-char-name-set nxml-autoload-char-name-set-list)
2545 (let ((names (gethash code nxml-char-name-table))
2546 name)
2547 (while (and names (not name))
2548 (if (nxml-char-name-set-enabled-p (caar names))
2549 (setq name (cdar names))
2550 (setq names (cdr names))))
2551 name))
2553 (defvar nxml-named-char-history nil)
2555 (defun nxml-insert-named-char (arg)
2556 "Insert a character using its name.
2557 The name is read from the minibuffer.
2558 Normally, inserts the character as a numeric character reference.
2559 With a prefix argument, inserts the character directly."
2560 (interactive "*P")
2561 (mapc 'nxml-maybe-load-char-name-set nxml-autoload-char-name-set-list)
2562 (let ((name
2563 (let ((completion-ignore-case nxml-char-name-ignore-case))
2564 (completing-read "Character name: "
2565 nxml-char-name-alist
2566 (lambda (member)
2567 (get (cddr member) 'nxml-char-name-set-enabled))
2570 'nxml-named-char-history)))
2571 (alist nxml-char-name-alist)
2572 elt code)
2573 (while (and alist (not code))
2574 (setq elt (assoc name alist))
2575 (if (get (cddr elt) 'nxml-char-name-set-enabled)
2576 (setq code (cadr elt))
2577 (setq alist (cdr (member elt alist)))))
2578 (when code
2579 (insert (if arg
2580 (or (decode-char 'ucs code)
2581 (error "Character %x is not supported by Emacs"
2582 code))
2583 (format "&#x%X;" code))))))
2585 (defun nxml-maybe-load-char-name-set (sym)
2586 (when (and (get sym 'nxml-char-name-set-enabled)
2587 (not (get sym 'nxml-char-name-set-defined))
2588 (stringp (get sym 'nxml-char-name-set-file)))
2589 (load (get sym 'nxml-char-name-set-file))))
2591 (defun nxml-toggle-char-ref-extra-display (arg)
2592 "Toggle the display of extra information for character references."
2593 (interactive "P")
2594 (let ((new (if (null arg)
2595 (not nxml-char-ref-extra-display)
2596 (> (prefix-numeric-value arg) 0))))
2597 (when (not (eq new nxml-char-ref-extra-display))
2598 (setq nxml-char-ref-extra-display new)
2599 (font-lock-flush))))
2601 (put 'nxml-char-ref 'evaporate t)
2603 (defun nxml-char-ref-display-extra (start end n)
2604 (when nxml-char-ref-extra-display
2605 (let ((name (nxml-get-char-name n))
2606 (glyph-string (and nxml-char-ref-display-glyph-flag
2607 (nxml-glyph-display-string n 'nxml-glyph)))
2609 (when (or name glyph-string)
2610 (setq ov (make-overlay start end nil t))
2611 (overlay-put ov 'category 'nxml-char-ref)
2612 (when name
2613 (overlay-put ov 'help-echo name))
2614 (when glyph-string
2615 (overlay-put ov
2616 'after-string
2617 (propertize glyph-string 'face 'nxml-glyph)))))))
2619 (defun nxml-clear-char-ref-extra-display (start end)
2620 (let ((ov (overlays-in start end)))
2621 (while ov
2622 (when (eq (overlay-get (car ov) 'category) 'nxml-char-ref)
2623 (delete-overlay (car ov)))
2624 (setq ov (cdr ov)))))
2627 (defun nxml-start-delimiter-length (type)
2628 (or (get type 'nxml-start-delimiter-length)
2631 (put 'cdata-section 'nxml-start-delimiter-length 9)
2632 (put 'comment 'nxml-start-delimiter-length 4)
2633 (put 'processing-instruction 'nxml-start-delimiter-length 2)
2634 (put 'start-tag 'nxml-start-delimiter-length 1)
2635 (put 'empty-element 'nxml-start-delimiter-length 1)
2636 (put 'partial-empty-element 'nxml-start-delimiter-length 1)
2637 (put 'entity-ref 'nxml-start-delimiter-length 1)
2638 (put 'char-ref 'nxml-start-delimiter-length 2)
2640 (defun nxml-end-delimiter-length (type)
2641 (or (get type 'nxml-end-delimiter-length)
2644 (put 'cdata-section 'nxml-end-delimiter-length 3)
2645 (put 'comment 'nxml-end-delimiter-length 3)
2646 (put 'processing-instruction 'nxml-end-delimiter-length 2)
2647 (put 'start-tag 'nxml-end-delimiter-length 1)
2648 (put 'empty-element 'nxml-end-delimiter-length 2)
2649 (put 'partial-empty-element 'nxml-end-delimiter-length 1)
2650 (put 'entity-ref 'nxml-end-delimiter-length 1)
2651 (put 'char-ref 'nxml-end-delimiter-length 1)
2653 (defun nxml-token-type-friendly-name (type)
2654 (or (get type 'nxml-friendly-name)
2655 (symbol-name type)))
2657 (put 'cdata-section 'nxml-friendly-name "CDATA section")
2658 (put 'processing-instruction 'nxml-friendly-name "processing instruction")
2659 (put 'entity-ref 'nxml-friendly-name "entity reference")
2660 (put 'char-ref 'nxml-friendly-name "character reference")
2662 ;; Only do this in loaddefs, so that if someone defines a different
2663 ;; alias in .emacs, loading this file afterwards does not clobber it.
2664 ;;;###autoload(defalias 'xml-mode 'nxml-mode)
2666 (provide 'nxml-mode)
2668 ;;; nxml-mode.el ends here