NS equivalents of xterm.c and w32term.c changes
[emacs.git] / lisp / nxml / nxml-mode.el
blob6c5c85b2fcc879d1ece8b96a72502db962926ef9
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 (use-local-map nxml-mode-map)
550 (save-excursion
551 (save-restriction
552 (widen)
553 (setq nxml-scan-end (copy-marker (point-min) nil))
554 (with-silent-modifications
555 (nxml-clear-inside (point-min) (point-max))
556 (nxml-with-invisible-motion
557 (nxml-scan-prolog)))))
558 (add-hook 'completion-at-point-functions
559 #'nxml-completion-at-point-function nil t)
560 (setq-local syntax-propertize-function #'nxml-after-change)
561 (add-hook 'change-major-mode-hook 'nxml-cleanup nil t)
563 ;; Emacs 23 handles the encoding attribute on the xml declaration
564 ;; transparently to nxml-mode, so there is no longer a need for the below
565 ;; hook. The hook also had the drawback of overriding explicit user
566 ;; instruction to save as some encoding other than utf-8.
567 ;;(add-hook 'write-contents-hooks 'nxml-prepare-to-save)
568 (when (not (and (buffer-file-name) (file-exists-p (buffer-file-name))))
569 (when (and nxml-default-buffer-file-coding-system
570 (not (local-variable-p 'buffer-file-coding-system)))
571 (setq buffer-file-coding-system nxml-default-buffer-file-coding-system))
572 (when nxml-auto-insert-xml-declaration-flag
573 (nxml-insert-xml-declaration)))
575 (setq font-lock-defaults
576 '(nxml-font-lock-keywords
577 t ; keywords-only; we highlight comments and strings here
578 nil ; font-lock-keywords-case-fold-search. XML is case sensitive
579 nil ; no special syntax table
580 nil ; no automatic syntactic fontification
581 (font-lock-extend-region-functions . (nxml-extend-region))
582 (jit-lock-contextually . t)
583 (font-lock-unfontify-region-function . nxml-unfontify-region)))
585 (rng-nxml-mode-init)
586 (nxml-enable-unicode-char-name-sets))
588 (defun nxml-cleanup ()
589 "Clean up after nxml-mode."
590 ;; Disable associated minor modes.
591 (rng-validate-mode -1)
592 ;; Clean up fontification.
593 (save-excursion
594 (widen)
595 (with-silent-modifications
596 (nxml-with-invisible-motion
597 (remove-text-properties (point-min) (point-max) '(face)))))
598 (remove-hook 'change-major-mode-hook 'nxml-cleanup t))
600 (defun nxml-degrade (context err)
601 (message "Internal nXML mode error in %s (%s), degrading"
602 context
603 (error-message-string err))
604 (ding)
605 (setq nxml-degraded t)
606 (setq nxml-prolog-end 1)
607 (save-excursion
608 (save-restriction
609 (widen)
610 (with-silent-modifications
611 (nxml-clear-inside (point-min) (point-max))))))
613 ;;; Change management
615 (defvar font-lock-beg) (defvar font-lock-end)
616 (defun nxml-debug-region (start end)
617 (interactive "r")
618 (let ((font-lock-beg start)
619 (font-lock-end end))
620 (nxml-extend-region)
621 (goto-char font-lock-beg)
622 (set-mark font-lock-end)))
624 (defun nxml-after-change (start end)
625 ;; Called via syntax-propertize-function.
626 (unless nxml-degraded
627 (nxml-with-degradation-on-error 'nxml-after-change
628 (save-restriction
629 (widen)
630 (nxml-with-invisible-motion
631 (nxml-after-change1 start end))))))
633 (defun nxml-after-change1 (start end)
634 "After-change bookkeeping.
635 Returns a cons cell containing a possibly-enlarged change region.
636 You must call `nxml-extend-region' on this expanded region to obtain
637 the full extent of the area needing refontification.
639 For bookkeeping, call this function even when fontification is
640 disabled."
641 ;; If the prolog might have changed, rescan the prolog.
642 (when (<= start
643 ;; Add 2 so as to include the < and following char that
644 ;; start the instance (document element), since changing
645 ;; these can change where the prolog ends.
646 (+ nxml-prolog-end 2))
647 (nxml-scan-prolog)
648 (setq start (point-min)))
650 (when (> end nxml-prolog-end)
651 (goto-char start)
652 (nxml-move-tag-backwards (point-min))
653 (setq start (point))
654 (setq end (max (nxml-scan-after-change start end)
655 end)))
657 (nxml-debug-change "nxml-after-change1" start end))
659 ;;; Encodings
661 (defun nxml-insert-xml-declaration ()
662 "Insert an XML declaration at the beginning of buffer.
663 The XML declaration will declare an encoding depending on the buffer's
664 `buffer-file-coding-system'."
665 (interactive "*")
666 (let ((coding-system
667 (if (and buffer-file-coding-system
668 (coding-system-p buffer-file-coding-system)
669 (coding-system-get buffer-file-coding-system
670 'mime-charset))
671 buffer-file-coding-system
672 (nxml-choose-utf-coding-system))))
673 (goto-char (point-min))
674 (insert (format "<?xml version=\"1.0\" encoding=\"%s\"?>\n"
675 (nxml-coding-system-name coding-system)))))
677 (defun nxml-prepare-to-save ()
678 (unless (and (not enable-multibyte-characters)
679 (local-variable-p 'buffer-file-coding-system)
680 buffer-file-coding-system
681 (or (eq (coding-system-type buffer-file-coding-system) 5)
682 (eq buffer-file-coding-system 'no-conversion)))
683 (save-excursion
684 (setq buffer-file-coding-system (nxml-select-coding-system))))
685 ;; nil from a function in `write-contents-hooks' means
686 ;; to continue and write the file as normal
687 nil)
689 (defun nxml-select-coding-system ()
690 (let* ((suitable-coding-systems
691 (find-coding-systems-region (point-min) (point-max)))
692 (enc-pos (progn
693 (goto-char (point-min))
694 (xmltok-get-declared-encoding-position)))
695 (enc-name
696 (and (consp enc-pos)
697 (buffer-substring-no-properties (car enc-pos)
698 (cdr enc-pos))))
699 (coding-system
700 (cond (enc-name
701 (if (string= (downcase enc-name) "utf-16")
702 (nxml-choose-utf-16-coding-system)
703 (nxml-mime-charset-coding-system enc-name)))
704 (enc-pos (nxml-choose-utf-coding-system)))))
705 ;; Make sure we have a coding-system
706 (unless coding-system
707 (setq coding-system
708 (and (not buffer-read-only)
709 (nxml-choose-suitable-coding-system
710 suitable-coding-systems)))
711 (let ((message
712 (if enc-name
713 (format "Unknown encoding %s" enc-name)
714 "XML declaration is not well-formed")))
715 (cond ((not coding-system)
716 (error "%s" message))
717 ((y-or-n-p
718 (concat message
719 ". "
720 (format (if enc-name
721 "Save with %s"
722 "Modify and save with encoding %s")
723 (nxml-coding-system-name coding-system))
724 " "))
725 (nxml-fix-encoding-declaration enc-pos coding-system))
726 (t (signal 'quit nil)))))
727 ;; Make sure it can encode all the characters in the buffer
728 (unless (or (memq (coding-system-base coding-system)
729 suitable-coding-systems)
730 (equal suitable-coding-systems '(undecided)))
731 (let ((message
732 (nxml-unsuitable-coding-system-message coding-system
733 enc-name)))
734 (setq coding-system
735 (and (not buffer-read-only)
736 (nxml-choose-suitable-coding-system
737 suitable-coding-systems)))
738 (cond ((not coding-system) (error "%s" message))
739 ((y-or-n-p (concat message
740 (format ". Save with %s "
741 (nxml-coding-system-name
742 coding-system))))
743 (nxml-fix-encoding-declaration enc-pos coding-system))
744 (t (signal 'quit nil)))))
745 ;; Merge the newline type of our existing encoding
746 (let ((current-eol-type
747 (coding-system-eol-type buffer-file-coding-system)))
748 (when (and current-eol-type (integerp current-eol-type))
749 (setq coding-system
750 (coding-system-change-eol-conversion coding-system
751 current-eol-type))))
752 coding-system))
754 (defun nxml-unsuitable-coding-system-message (coding-system &optional enc-name)
755 (if (nxml-coding-system-unicode-p coding-system)
756 "Cannot translate some characters to Unicode"
757 (format "Cannot encode some characters with %s"
758 (or enc-name
759 (nxml-coding-system-name coding-system)))))
761 (defconst nxml-utf-16-coding-systems (and (coding-system-p 'utf-16-be)
762 (coding-system-p 'utf-16-le)
763 '(utf-16-be utf-16-le)))
765 (defconst nxml-utf-coding-systems (cons 'utf-8 nxml-utf-16-coding-systems))
767 (defun nxml-coding-system-unicode-p (coding-system)
768 (nxml-coding-system-member (coding-system-base coding-system)
769 nxml-utf-coding-systems))
771 (defun nxml-coding-system-name (coding-system)
772 (setq coding-system (coding-system-base coding-system))
773 (symbol-name
774 (if (nxml-coding-system-member coding-system nxml-utf-16-coding-systems)
775 'utf-16
776 (or (coding-system-get coding-system 'mime-charset)
777 coding-system))))
779 (defun nxml-fix-encoding-declaration (enc-pos coding-system)
780 (let ((charset (nxml-coding-system-name coding-system)))
781 (cond ((consp enc-pos)
782 (delete-region (car enc-pos) (cdr enc-pos))
783 (goto-char (car enc-pos))
784 (insert charset))
785 ((integerp enc-pos)
786 (goto-char enc-pos)
787 (insert " encoding=\"" charset ?\"))
789 (goto-char (point-min))
790 (insert "<?xml version=\"1.0\" encoding=\""
791 charset
792 "\"?>\n")
793 (when (and (not enc-pos)
794 (let ((case-fold-search t))
795 (looking-at xmltok-bad-xml-decl-regexp)))
796 (delete-region (point) (match-end 0)))))))
798 (defun nxml-choose-suitable-coding-system (suitable-coding-systems)
799 (let (ret coding-system)
800 (if (and buffer-file-coding-system
801 (memq (coding-system-base buffer-file-coding-system)
802 suitable-coding-systems))
803 buffer-file-coding-system
804 (while (and suitable-coding-systems (not ret))
805 (setq coding-system (car suitable-coding-systems))
806 (if (coding-system-get coding-system 'mime-charset)
807 (setq ret coding-system)
808 (setq suitable-coding-systems (cdr suitable-coding-systems))))
809 ret)))
811 (defun nxml-choose-utf-coding-system ()
812 (let ((cur (and (local-variable-p 'buffer-file-coding-system)
813 buffer-file-coding-system
814 (coding-system-base buffer-file-coding-system))))
815 (cond ((car (nxml-coding-system-member cur nxml-utf-coding-systems)))
816 ((and nxml-prefer-utf-16-to-utf-8-flag
817 (coding-system-p 'utf-16-le)
818 (coding-system-p 'utf-16-be))
819 (if nxml-prefer-utf-16-little-to-big-endian-flag
820 'utf-16-le
821 'utf-16-be))
822 (t 'utf-8))))
824 (defun nxml-choose-utf-16-coding-system ()
825 (let ((cur (and (local-variable-p 'buffer-file-coding-system)
826 buffer-file-coding-system
827 (coding-system-base buffer-file-coding-system))))
828 (cond ((car (nxml-coding-system-member cur nxml-utf-16-coding-systems)))
829 (nxml-prefer-utf-16-little-to-big-endian-flag
830 (and (coding-system-p 'utf-16-le) 'utf-16-le))
831 (t (and (coding-system-p 'utf-16-be) 'utf-16-be)))))
833 (defun nxml-coding-system-member (coding-system coding-systems)
834 (let (ret)
835 (while (and coding-systems (not ret))
836 (if (coding-system-equal coding-system
837 (car coding-systems))
838 (setq ret coding-systems)
839 (setq coding-systems (cdr coding-systems))))
840 ret))
842 ;;; Fontification
844 (defun nxml-unfontify-region (start end)
845 (font-lock-default-unfontify-region start end)
846 (nxml-clear-char-ref-extra-display start end))
848 (defun nxml-extend-region ()
849 "Extend the region to hold the minimum area we can fontify with nXML.
850 Called with `font-lock-beg' and `font-lock-end' dynamically bound."
851 (let ((start font-lock-beg)
852 (end font-lock-end))
854 (nxml-debug-change "nxml-extend-region(input)" start end)
856 (when (< start nxml-prolog-end)
857 (setq start (point-min)))
859 (cond ((<= end nxml-prolog-end)
860 (setq end nxml-prolog-end))
863 (goto-char start)
864 ;; some font-lock backends (like Emacs 22 jit-lock) snap
865 ;; the region to the beginning of the line no matter what
866 ;; we say here. To mitigate the resulting excess
867 ;; fontification, ignore leading whitespace.
868 (skip-syntax-forward " ")
870 ;; find the beginning of the previous tag
871 (when (not (equal (char-after) ?\<))
872 (search-backward "<" nxml-prolog-end t))
873 (nxml-ensure-scan-up-to-date)
874 (nxml-move-outside-backwards)
875 (setq start (point))
877 (while (< (point) end)
878 (nxml-tokenize-forward))
880 (setq end (point))))
882 (when (or (< start font-lock-beg)
883 (> end font-lock-end))
884 (setq font-lock-beg start
885 font-lock-end end)
886 (nxml-debug-change "nxml-extend-region" start end)
887 t)))
889 (defun nxml-fontify-matcher (bound)
890 "Called as font-lock keyword matcher."
891 (syntax-propertize bound)
892 (unless nxml-degraded
893 (nxml-debug-change "nxml-fontify-matcher" (point) bound)
895 (when (< (point) nxml-prolog-end)
896 ;; Prolog needs to be fontified in one go, and
897 ;; nxml-extend-region makes sure we start at BOB.
898 (cl-assert (bobp))
899 (nxml-fontify-prolog)
900 (goto-char nxml-prolog-end))
902 (let (xmltok-errors)
903 (while (and (nxml-tokenize-forward)
904 (<= (point) bound)) ; Intervals are open-ended.
905 (nxml-apply-fontify-rule)))
909 ;; Since we did the fontification internally, tell font-lock to not
910 ;; do anything itself.
911 nil)
913 (defun nxml-fontify-prolog ()
914 "Fontify the prolog.
915 The buffer is assumed to be prepared for fontification.
916 This does not set the fontified property, but it does clear
917 faces appropriately."
918 (let ((regions nxml-prolog-regions))
919 (while regions
920 (let ((region (car regions)))
921 (nxml-apply-fontify-rule (aref region 0)
922 (aref region 1)
923 (aref region 2)))
924 (setq regions (cdr regions)))))
926 ;; Vectors identify a substring of the token to be highlighted in some face.
928 ;; Token types returned by xmltok-forward.
930 (put 'start-tag
931 'nxml-fontify-rule
932 '([nil 1 nxml-tag-delimiter]
933 [-1 nil nxml-tag-delimiter]
934 (element-qname . 1)
935 attributes))
937 (put 'partial-start-tag
938 'nxml-fontify-rule
939 '([nil 1 nxml-tag-delimiter]
940 (element-qname . 1)
941 attributes))
943 (put 'end-tag
944 'nxml-fontify-rule
945 '([nil 1 nxml-tag-delimiter]
946 [1 2 nxml-tag-slash]
947 [-1 nil nxml-tag-delimiter]
948 (element-qname . 2)))
950 (put 'partial-end-tag
951 'nxml-fontify-rule
952 '([nil 1 nxml-tag-delimiter]
953 [1 2 nxml-tag-slash]
954 (element-qname . 2)))
956 (put 'empty-element
957 'nxml-fontify-rule
958 '([nil 1 nxml-tag-delimiter]
959 [-2 -1 nxml-tag-slash]
960 [-1 nil nxml-tag-delimiter]
961 (element-qname . 1)
962 attributes))
964 (put 'partial-empty-element
965 'nxml-fontify-rule
966 '([nil 1 nxml-tag-delimiter]
967 [-1 nil nxml-tag-slash]
968 (element-qname . 1)
969 attributes))
971 (put 'char-ref
972 'nxml-fontify-rule
973 '([nil 2 nxml-char-ref-delimiter]
974 [2 -1 nxml-char-ref-number]
975 [-1 nil nxml-char-ref-delimiter]
976 char-ref))
978 (put 'entity-ref
979 'nxml-fontify-rule
980 '([nil 1 nxml-entity-ref-delimiter]
981 [1 -1 nxml-entity-ref-name]
982 [-1 nil nxml-entity-ref-delimiter]))
984 (put 'comment
985 'nxml-fontify-rule
986 '([nil 4 nxml-comment-delimiter]
987 [4 -3 nxml-comment-content]
988 [-3 nil nxml-comment-delimiter]))
990 (put 'processing-instruction
991 'nxml-fontify-rule
992 '([nil 2 nxml-processing-instruction-delimiter]
993 [-2 nil nxml-processing-instruction-delimiter]
994 processing-instruction-content))
996 (put 'cdata-section
997 'nxml-fontify-rule
998 '([nil 3 nxml-cdata-section-delimiter] ; <![
999 [3 8 nxml-cdata-section-CDATA] ; CDATA
1000 [8 9 nxml-cdata-section-delimiter] ; [
1001 [9 -3 nxml-cdata-section-content] ; ]]>
1002 [-3 nil nxml-cdata-section-delimiter]))
1004 (put 'data
1005 'nxml-fontify-rule
1006 '([nil nil nxml-text]))
1008 ;; Prolog region types in list returned by xmltok-forward-prolog.
1010 (put 'xml-declaration
1011 'nxml-fontify-rule
1012 '([nil 2 nxml-processing-instruction-delimiter]
1013 [2 5 nxml-processing-instruction-target]
1014 [-2 nil nxml-processing-instruction-delimiter]))
1016 (put 'xml-declaration-attribute-name
1017 'nxml-fontify-rule
1018 '([nil nil nxml-attribute-local-name]))
1020 (put 'xml-declaration-attribute-value
1021 'nxml-fontify-rule
1022 '([nil 1 nxml-attribute-value-delimiter]
1023 [1 -1 nxml-attribute-value]
1024 [-1 nil nxml-attribute-value-delimiter]))
1026 (put 'processing-instruction-left
1027 'nxml-fontify-rule
1028 '([nil 2 nxml-processing-instruction-delimiter]
1029 [2 nil nxml-processing-instruction-target]))
1031 (put 'processing-instruction-right
1032 'nxml-fontify-rule
1033 '([nil -2 nxml-processing-instruction-content]
1034 [-2 nil nxml-processing-instruction-delimiter]))
1036 (put 'literal
1037 'nxml-fontify-rule
1038 '([nil 1 nxml-prolog-literal-delimiter]
1039 [1 -1 nxml-prolog-literal-content]
1040 [-1 nil nxml-prolog-literal-delimiter]))
1042 (put 'keyword
1043 'nxml-fontify-rule
1044 '([nil nil nxml-prolog-keyword]))
1046 (put 'markup-declaration-open
1047 'nxml-fontify-rule
1048 '([0 2 nxml-markup-declaration-delimiter]
1049 [2 nil nxml-prolog-keyword]))
1051 (put 'markup-declaration-close
1052 'nxml-fontify-rule
1053 '([nil nil nxml-markup-declaration-delimiter]))
1055 (put 'internal-subset-open
1056 'nxml-fontify-rule
1057 '([nil nil nxml-markup-declaration-delimiter]))
1059 (put 'internal-subset-close
1060 'nxml-fontify-rule
1061 '([nil 1 nxml-markup-declaration-delimiter]
1062 [-1 nil nxml-markup-declaration-delimiter]))
1064 (put 'hash-name
1065 'nxml-fontify-rule
1066 '([nil 1 nxml-hash]
1067 [1 nil nxml-prolog-keyword]))
1069 (defun nxml-apply-fontify-rule (&optional type start end)
1070 (let ((rule (get (or type xmltok-type) 'nxml-fontify-rule)))
1071 (unless start (setq start xmltok-start))
1072 (unless end (setq end (point)))
1073 (while rule
1074 (let* ((action (car rule)))
1075 (setq rule (cdr rule))
1076 (cond ((vectorp action)
1077 (nxml-set-face (let ((offset (aref action 0)))
1078 (cond ((not offset) start)
1079 ((< offset 0) (+ end offset))
1080 (t (+ start offset))))
1081 (let ((offset (aref action 1)))
1082 (cond ((not offset) end)
1083 ((< offset 0) (+ end offset))
1084 (t (+ start offset))))
1085 (aref action 2)))
1086 ((and (consp action)
1087 (eq (car action) 'element-qname))
1088 (when xmltok-name-end ; maybe nil in partial-end-tag case
1089 (nxml-fontify-qname (+ start (cdr action))
1090 xmltok-name-colon
1091 xmltok-name-end
1092 'nxml-element-prefix
1093 'nxml-element-colon
1094 'nxml-element-local-name)))
1095 ((eq action 'attributes)
1096 (nxml-fontify-attributes))
1097 ((eq action 'processing-instruction-content)
1098 (nxml-set-face (+ start 2)
1099 xmltok-name-end
1100 'nxml-processing-instruction-target)
1101 (nxml-set-face (save-excursion
1102 (goto-char xmltok-name-end)
1103 (skip-chars-forward " \t\r\n")
1104 (point))
1105 (- end 2)
1106 'nxml-processing-instruction-content))
1107 ((eq action 'char-ref)
1108 (nxml-char-ref-display-extra start
1110 (xmltok-char-number start end)))
1111 (t (error "Invalid nxml-fontify-rule action %s" action)))))))
1113 (defun nxml-fontify-attributes ()
1114 (while xmltok-namespace-attributes
1115 (nxml-fontify-attribute (car xmltok-namespace-attributes)
1116 'namespace)
1117 (setq xmltok-namespace-attributes
1118 (cdr xmltok-namespace-attributes)))
1119 (while xmltok-attributes
1120 (nxml-fontify-attribute (car xmltok-attributes))
1121 (setq xmltok-attributes
1122 (cdr xmltok-attributes))))
1124 (defun nxml-fontify-attribute (att &optional namespace-declaration)
1125 (if namespace-declaration
1126 (nxml-fontify-qname (xmltok-attribute-name-start att)
1127 (xmltok-attribute-name-colon att)
1128 (xmltok-attribute-name-end att)
1129 'nxml-namespace-attribute-xmlns
1130 'nxml-namespace-attribute-colon
1131 'nxml-namespace-attribute-prefix
1132 'nxml-namespace-attribute-xmlns)
1133 (nxml-fontify-qname (xmltok-attribute-name-start att)
1134 (xmltok-attribute-name-colon att)
1135 (xmltok-attribute-name-end att)
1136 'nxml-attribute-prefix
1137 'nxml-attribute-colon
1138 'nxml-attribute-local-name))
1139 (let ((start (xmltok-attribute-value-start att))
1140 (end (xmltok-attribute-value-end att))
1141 (refs (xmltok-attribute-refs att))
1142 (delimiter-face (if namespace-declaration
1143 'nxml-namespace-attribute-value-delimiter
1144 'nxml-attribute-value-delimiter))
1145 (value-face (if namespace-declaration
1146 'nxml-namespace-attribute-value
1147 'nxml-attribute-value)))
1148 (when start
1149 (nxml-set-face (1- start) start delimiter-face)
1150 (nxml-set-face end (1+ end) delimiter-face)
1151 (while refs
1152 (let* ((ref (car refs))
1153 (ref-type (aref ref 0))
1154 (ref-start (aref ref 1))
1155 (ref-end (aref ref 2)))
1156 (nxml-set-face start ref-start value-face)
1157 (nxml-apply-fontify-rule ref-type ref-start ref-end)
1158 (setq start ref-end))
1159 (setq refs (cdr refs)))
1160 (nxml-set-face start end value-face))))
1162 (defun nxml-fontify-qname (start
1163 colon
1165 prefix-face
1166 colon-face
1167 local-name-face
1168 &optional
1169 unprefixed-face)
1170 (cond (colon (nxml-set-face start colon prefix-face)
1171 (nxml-set-face colon (1+ colon) colon-face)
1172 (nxml-set-face (1+ colon) end local-name-face))
1173 (t (nxml-set-face start end (or unprefixed-face
1174 local-name-face)))))
1176 ;;; Editing
1178 (defun nxml-electric-slash (arg)
1179 "Insert a slash.
1181 With a prefix ARG, do nothing other than insert the slash.
1183 Otherwise, if `nxml-slash-auto-complete-flag' is non-nil, insert the
1184 rest of the end-tag or empty-element if the slash is potentially part
1185 of an end-tag or the close of an empty-element.
1187 If the slash is part of an end-tag that is the first non-whitespace
1188 on the line, reindent the line."
1189 (interactive "*P")
1190 (nxml-ensure-scan-up-to-date)
1191 (let* ((slash-pos (point))
1192 (end-tag-p (and (eq (char-before slash-pos) ?<)
1193 (not (nxml-get-inside slash-pos))))
1194 (at-indentation (save-excursion
1195 (back-to-indentation)
1196 (eq (point) (1- slash-pos)))))
1197 (self-insert-command (prefix-numeric-value arg))
1198 (unless arg
1199 (if nxml-slash-auto-complete-flag
1200 (if end-tag-p
1201 (condition-case nil
1202 (let ((start-tag-end
1203 (nxml-scan-element-backward (1- slash-pos) t)))
1204 (when start-tag-end
1205 (insert (xmltok-start-tag-qname) ">")
1206 ;; copy the indentation of the start-tag
1207 (when (and at-indentation
1208 (save-excursion
1209 (goto-char xmltok-start)
1210 (back-to-indentation)
1211 (eq (point) xmltok-start)))
1212 (save-excursion
1213 (indent-line-to (save-excursion
1214 (goto-char xmltok-start)
1215 (current-column)))))))
1216 (nxml-scan-error nil))
1217 (when (and (eq (nxml-token-before) (point))
1218 (eq xmltok-type 'partial-empty-element))
1219 (insert ">"))))
1220 (when (and end-tag-p at-indentation)
1221 (nxml-indent-line)))))
1223 (defun nxml-balanced-close-start-tag-block ()
1224 "Close the start-tag before point with `>' and insert a balancing end-tag.
1225 Point is left between the start-tag and the end-tag.
1226 If there is nothing but whitespace before the `<' that opens the
1227 start-tag, then put point on a blank line, and put the end-tag on
1228 another line aligned with the start-tag."
1229 (interactive "*")
1230 (nxml-balanced-close-start-tag 'block))
1232 (defun nxml-balanced-close-start-tag-inline ()
1233 "Close the start-tag before point with `>' and insert a balancing end-tag.
1234 Point is left between the start-tag and the end-tag.
1235 No extra whitespace is inserted."
1236 (interactive "*")
1237 (nxml-balanced-close-start-tag 'inline))
1239 (defun nxml-balanced-close-start-tag (block-or-inline)
1240 (let ((token-end (nxml-token-before))
1241 (pos (1+ (point)))
1242 (token-start xmltok-start))
1243 (unless (or (eq xmltok-type 'partial-start-tag)
1244 (and (memq xmltok-type '(start-tag
1245 empty-element
1246 partial-empty-element))
1247 (>= token-end pos)))
1248 (error "Not in a start-tag"))
1249 ;; Note that this insertion changes xmltok-start.
1250 (insert "></"
1251 (buffer-substring-no-properties (+ xmltok-start 1)
1252 (min xmltok-name-end (point)))
1253 ">")
1254 (if (eq block-or-inline 'inline)
1255 (goto-char pos)
1256 (goto-char token-start)
1257 (back-to-indentation)
1258 (if (= (point) token-start)
1259 (let ((indent (current-column)))
1260 (goto-char pos)
1261 (insert "\n")
1262 (indent-line-to indent)
1263 (goto-char pos)
1264 (insert "\n")
1265 (indent-line-to (+ nxml-child-indent indent)))
1266 (goto-char pos)))))
1268 (defun nxml-finish-element ()
1269 "Finish the current element by inserting an end-tag."
1270 (interactive "*")
1271 (nxml-finish-element-1 nil))
1273 (defvar nxml-last-split-position nil
1274 "Position where `nxml-split-element' split the current element.")
1276 (defun nxml-split-element ()
1277 "Split the current element by inserting an end-tag and a start-tag.
1278 Point is left after the newly inserted start-tag. When repeated,
1279 split immediately before the previously inserted start-tag and leave
1280 point unchanged."
1281 (interactive "*")
1282 (setq nxml-last-split-position
1283 (if (and (eq last-command this-command)
1284 nxml-last-split-position)
1285 (save-excursion
1286 (goto-char nxml-last-split-position)
1287 (nxml-finish-element-1 t))
1288 (nxml-finish-element-1 t))))
1290 (defun nxml-finish-element-1 (startp)
1291 "Insert an end-tag for the current element and optionally a start-tag.
1292 The start-tag is inserted if STARTP is non-nil. Return the position
1293 of the inserted start-tag or nil if none was inserted."
1294 (interactive "*")
1295 (let* ((token-end (nxml-token-before))
1296 (start-tag-end
1297 (save-excursion
1298 (when (and (< (point) token-end)
1299 (memq xmltok-type
1300 '(cdata-section
1301 processing-instruction
1302 comment
1303 start-tag
1304 end-tag
1305 empty-element)))
1306 (error "Point is inside a %s"
1307 (nxml-token-type-friendly-name xmltok-type)))
1308 (nxml-scan-element-backward token-end t)))
1309 (starts-line
1310 (save-excursion
1311 (unless (eq xmltok-type 'start-tag)
1312 (error "No matching start-tag"))
1313 (goto-char xmltok-start)
1314 (back-to-indentation)
1315 (eq (point) xmltok-start)))
1316 (ends-line
1317 (save-excursion
1318 (goto-char start-tag-end)
1319 (looking-at "[ \t\r\n]*$")))
1320 (start-tag-indent (save-excursion
1321 (goto-char xmltok-start)
1322 (current-column)))
1323 (qname (xmltok-start-tag-qname))
1324 inserted-start-tag-pos)
1325 (when (and starts-line ends-line)
1326 ;; start-tag is on a line by itself
1327 ;; => put the end-tag on a line by itself
1328 (unless (<= (point)
1329 (save-excursion
1330 (back-to-indentation)
1331 (point)))
1332 (insert "\n"))
1333 (indent-line-to start-tag-indent))
1334 (insert "</" qname ">")
1335 (when startp
1336 (when starts-line
1337 (insert "\n")
1338 (indent-line-to start-tag-indent))
1339 (setq inserted-start-tag-pos (point))
1340 (insert "<" qname ">")
1341 (when (and starts-line ends-line)
1342 (insert "\n")
1343 (indent-line-to (save-excursion
1344 (goto-char xmltok-start)
1345 (forward-line 1)
1346 (back-to-indentation)
1347 (if (= (current-column)
1348 (+ start-tag-indent nxml-child-indent))
1349 (+ start-tag-indent nxml-child-indent)
1350 start-tag-indent)))))
1351 inserted-start-tag-pos))
1353 ;;; Indentation
1355 (defun nxml-indent-line ()
1356 "Indent current line as XML."
1357 (let* ((savep (point))
1358 (indent (condition-case nil
1359 (save-excursion
1360 (forward-line 0)
1361 (skip-chars-forward " \t")
1362 (if (>= (point) savep) (setq savep nil))
1363 (or (nxml-compute-indent) 0))
1364 (error 0))))
1365 (if (not (numberp indent))
1366 ;; If something funny is used (e.g. `noindent'), return it.
1367 indent
1368 (if (< indent 0) (setq indent 0)) ;Just in case.
1369 (if savep
1370 (save-excursion (indent-line-to indent))
1371 (indent-line-to indent)))))
1373 (defun nxml-compute-indent ()
1374 "Return the indent for the line containing point."
1375 (or (nxml-compute-indent-from-matching-start-tag)
1376 (nxml-compute-indent-from-previous-line)))
1378 (defun nxml-compute-indent-from-matching-start-tag ()
1379 "Compute the indent for a line with an end-tag using the matching start-tag.
1380 When the line containing point ends with an end-tag and does not start
1381 in the middle of a token, return the indent of the line containing the
1382 matching start-tag, if there is one and it occurs at the beginning of
1383 its line. Otherwise return nil."
1384 (save-excursion
1385 (back-to-indentation)
1386 (let ((bol (point)))
1387 (let ((inhibit-field-text-motion t))
1388 (end-of-line))
1389 (skip-chars-backward " \t")
1390 (and (= (nxml-token-before) (point))
1391 (memq xmltok-type '(end-tag partial-end-tag))
1392 ;; start of line must not be inside a token
1393 (or (= xmltok-start bol)
1394 (save-excursion
1395 (goto-char bol)
1396 (nxml-token-after)
1397 (= xmltok-start bol))
1398 (eq xmltok-type 'data))
1399 (condition-case nil
1400 (nxml-scan-element-backward
1401 (point)
1403 (- (point)
1404 nxml-end-tag-indent-scan-distance))
1405 (nxml-scan-error nil))
1406 (< xmltok-start bol)
1407 (progn
1408 (goto-char xmltok-start)
1409 (skip-chars-backward " \t")
1410 (bolp))
1411 (current-indentation)))))
1413 (defun nxml-compute-indent-from-previous-line ()
1414 "Compute the indent for a line using the indentation of a previous line."
1415 (save-excursion
1416 (end-of-line)
1417 (let ((eol (point))
1418 bol prev-bol ref
1419 before-context after-context)
1420 (back-to-indentation)
1421 (setq bol (point))
1422 (catch 'indent
1423 ;; Move backwards until the start of a non-blank line that is
1424 ;; not inside a token.
1425 (while (progn
1426 (when (= (forward-line -1) -1)
1427 (throw 'indent 0))
1428 (back-to-indentation)
1429 (if (looking-at "[ \t]*$")
1431 (or prev-bol
1432 (setq prev-bol (point)))
1433 (nxml-token-after)
1434 (not (or (= xmltok-start (point))
1435 (eq xmltok-type 'data))))))
1436 (setq ref (point))
1437 ;; Now scan over tokens until the end of the line to be indented.
1438 ;; Determine the context before and after the beginning of the
1439 ;; line.
1440 (while (< (point) eol)
1441 (nxml-tokenize-forward)
1442 (cond ((<= bol xmltok-start)
1443 (setq after-context
1444 (nxml-merge-indent-context-type after-context)))
1445 ((and (<= (point) bol)
1446 (not (and (eq xmltok-type 'partial-start-tag)
1447 (= (point) bol))))
1448 (setq before-context
1449 (nxml-merge-indent-context-type before-context)))
1450 ((eq xmltok-type 'data)
1451 (setq before-context
1452 (nxml-merge-indent-context-type before-context))
1453 (setq after-context
1454 (nxml-merge-indent-context-type after-context)))
1455 ;; If in the middle of a token that looks inline,
1456 ;; then indent relative to the previous non-blank line
1457 ((eq (nxml-merge-indent-context-type before-context)
1458 'mixed)
1459 (goto-char prev-bol)
1460 (throw 'indent (current-column)))
1462 (throw 'indent
1463 (nxml-compute-indent-in-token bol))))
1464 (skip-chars-forward " \t\r\n"))
1465 (goto-char ref)
1466 (+ (current-column)
1467 (* nxml-child-indent
1468 (+ (if (eq before-context 'start-tag) 1 0)
1469 (if (eq after-context 'end-tag) -1 0))))))))
1471 (defun nxml-merge-indent-context-type (context)
1472 "Merge the indent context type CONTEXT with the token in `xmltok-type'.
1473 Return the merged indent context type. An indent context type is
1474 either nil or one of the symbols `start-tag', `end-tag', `markup',
1475 `comment', `mixed'."
1476 (cond ((memq xmltok-type '(start-tag partial-start-tag))
1477 (if (memq context '(nil start-tag comment))
1478 'start-tag
1479 'mixed))
1480 ((memq xmltok-type '(end-tag partial-end-tag))
1481 (if (memq context '(nil end-tag comment))
1482 'end-tag
1483 'mixed))
1484 ((eq xmltok-type 'comment)
1485 (cond ((memq context '(start-tag end-tag comment))
1486 context)
1487 (context 'mixed)
1488 (t 'comment)))
1489 (context 'mixed)
1490 (t 'markup)))
1492 (defun nxml-compute-indent-in-token (pos)
1493 "Return the indent for a line that starts inside a token.
1494 POS is the position of the first non-whitespace character of the line.
1495 This expects the xmltok-* variables to be set up as by `xmltok-forward'."
1496 (cond ((memq xmltok-type '(start-tag
1497 partial-start-tag
1498 empty-element
1499 partial-empty-element))
1500 (nxml-compute-indent-in-start-tag pos))
1501 ((eq xmltok-type 'comment)
1502 (nxml-compute-indent-in-delimited-token pos "<!--" "-->"))
1503 ((eq xmltok-type 'cdata-section)
1504 (nxml-compute-indent-in-delimited-token pos "<![CDATA[" "]]>"))
1505 ((eq xmltok-type 'processing-instruction)
1506 (nxml-compute-indent-in-delimited-token pos "<?" "?>"))
1508 (goto-char pos)
1509 (if (and (= (forward-line -1) 0)
1510 (< xmltok-start (point)))
1511 (back-to-indentation)
1512 (goto-char xmltok-start))
1513 (current-column))))
1515 (defun nxml-compute-indent-in-start-tag (pos)
1516 "Return the indent for a line that starts inside a start-tag.
1517 Also for a line that starts inside an empty element.
1518 POS is the position of the first non-whitespace character of the line.
1519 This expects the xmltok-* variables to be set up as by `xmltok-forward'."
1520 (let ((value-boundary (nxml-attribute-value-boundary pos))
1521 (off 0))
1522 (if value-boundary
1523 ;; inside an attribute value
1524 (let ((value-start (car value-boundary)))
1525 (goto-char pos)
1526 (forward-line -1)
1527 (if (< (point) value-start)
1528 (goto-char value-start)
1529 (back-to-indentation)))
1530 ;; outside an attribute value
1531 (goto-char pos)
1532 (while (and (= (forward-line -1) 0)
1533 (nxml-attribute-value-boundary (point))))
1534 (cond ((<= (point) xmltok-start)
1535 (goto-char xmltok-start)
1536 (setq off nxml-attribute-indent)
1537 (let ((atts (xmltok-merge-attributes)))
1538 (when atts
1539 (let* ((att (car atts))
1540 (start (xmltok-attribute-name-start att)))
1541 (when (< start pos)
1542 (goto-char start)
1543 (setq off 0))))))
1545 (back-to-indentation))))
1546 (+ (current-column) off)))
1548 (defun nxml-attribute-value-boundary (pos)
1549 "Return a pair (START . END) if POS is inside an attribute value.
1550 Otherwise return nil. START and END are the positions of the start
1551 and end of the attribute value containing POS. This expects the
1552 xmltok-* variables to be set up as by `xmltok-forward'."
1553 (let ((atts (xmltok-merge-attributes))
1554 att value-start value-end value-boundary)
1555 (while atts
1556 (setq att (car atts))
1557 (setq value-start (xmltok-attribute-value-start att))
1558 (setq value-end (xmltok-attribute-value-end att))
1559 (cond ((and value-start (< pos value-start))
1560 (setq atts nil))
1561 ((and value-start value-end (<= pos value-end))
1562 (setq value-boundary (cons value-start value-end))
1563 (setq atts nil))
1564 (t (setq atts (cdr atts)))))
1565 value-boundary))
1567 (defun nxml-compute-indent-in-delimited-token (pos open-delim close-delim)
1568 "Return the indent for a line that starts inside a token with delimiters.
1569 OPEN-DELIM and CLOSE-DELIM are strings giving the opening and closing
1570 delimiters. POS is the position of the first non-whitespace character
1571 of the line. This expects the xmltok-* variables to be set up as by
1572 `xmltok-forward'."
1573 (cond ((let ((end (+ pos (length close-delim))))
1574 (and (<= end (point-max))
1575 (string= (buffer-substring-no-properties pos end)
1576 close-delim)))
1577 (goto-char xmltok-start))
1578 ((progn
1579 (goto-char pos)
1580 (forward-line -1)
1581 (<= (point) xmltok-start))
1582 (goto-char (+ xmltok-start (length open-delim)))
1583 (when (and (string= open-delim "<!--")
1584 (looking-at " "))
1585 (goto-char (1+ (point)))))
1586 (t (back-to-indentation)))
1587 (current-column))
1589 ;;; Completion
1591 (defun nxml-complete ()
1592 "Perform completion on the symbol preceding point.
1594 Inserts as many characters as can be completed. However, if not even
1595 one character can be completed, then a buffer with the possibilities
1596 is popped up and the symbol is read from the minibuffer with
1597 completion. If the symbol is complete, then any characters that must
1598 follow the symbol are also inserted.
1600 The name space used for completion and what is treated as a symbol
1601 depends on the context. The contexts in which completion is performed
1602 depend on `nxml-completion-hook'."
1603 (interactive)
1604 (unless (run-hook-with-args-until-success 'nxml-completion-hook)
1605 ;; Eventually we will complete on entity names here.
1606 (ding)
1607 (message "Cannot complete in this context")))
1609 (defun nxml-completion-at-point-function ()
1610 "Call `nxml-complete' to perform completion at point."
1611 (when nxml-bind-meta-tab-to-complete-flag
1612 #'nxml-complete))
1614 ;;; Movement
1616 (defun nxml-forward-balanced-item (&optional arg)
1617 "Move forward across one balanced item.
1618 With ARG, do it that many times. Negative arg -N means
1619 move backward across N balanced expressions.
1620 This is the equivalent of `forward-sexp' for XML.
1622 An element contains as items strings with no markup, tags, processing
1623 instructions, comments, CDATA sections, entity references and
1624 characters references. However, if the variable
1625 `nxml-sexp-element-flag' is non-nil, then an element is treated as a
1626 single markup item. A start-tag contains an element name followed by
1627 one or more attributes. An end-tag contains just an element name.
1628 An attribute value literals contains strings with no markup, entity
1629 references and character references. A processing instruction
1630 consists of a target and a content string. A comment or a CDATA
1631 section contains a single string. An entity reference contains a
1632 single name. A character reference contains a character number."
1633 (interactive "p")
1634 (or arg (setq arg 1))
1635 (cond ((> arg 0)
1636 (while (progn
1637 (nxml-forward-single-balanced-item)
1638 (> (setq arg (1- arg)) 0))))
1639 ((< arg 0)
1640 (while (progn
1641 (nxml-backward-single-balanced-item)
1642 (< (setq arg (1+ arg)) 0))))))
1644 (defun nxml-forward-single-balanced-item ()
1645 (condition-case err
1646 (goto-char (let ((end (nxml-token-after)))
1647 (save-excursion
1648 (while (eq xmltok-type 'space)
1649 (goto-char end)
1650 (setq end (nxml-token-after)))
1651 (cond ((/= (point) xmltok-start)
1652 (nxml-scan-forward-within end))
1653 ((and nxml-sexp-element-flag
1654 (eq xmltok-type 'start-tag))
1655 ;; can't ever return nil here
1656 (nxml-scan-element-forward xmltok-start))
1657 ((and nxml-sexp-element-flag
1658 (memq xmltok-type
1659 '(end-tag partial-end-tag)))
1660 (error "Already at end of element"))
1661 (t end)))))
1662 (nxml-scan-error
1663 (goto-char (cadr err))
1664 (apply 'error (cddr err)))))
1666 (defun nxml-backward-single-balanced-item ()
1667 (condition-case err
1668 (goto-char (let ((end (nxml-token-before)))
1669 (save-excursion
1670 (while (eq xmltok-type 'space)
1671 (goto-char xmltok-start)
1672 (setq end (nxml-token-before)))
1673 (cond ((/= (point) end)
1674 (nxml-scan-backward-within end))
1675 ((and nxml-sexp-element-flag
1676 (eq xmltok-type 'end-tag))
1677 ;; can't ever return nil here
1678 (nxml-scan-element-backward end)
1679 xmltok-start)
1680 ((and nxml-sexp-element-flag
1681 (eq xmltok-type 'start-tag))
1682 (error "Already at start of element"))
1683 (t xmltok-start)))))
1684 (nxml-scan-error
1685 (goto-char (cadr err))
1686 (apply 'error (cddr err)))))
1688 (defun nxml-scan-forward-within (end)
1689 (setq end (- end (nxml-end-delimiter-length xmltok-type)))
1690 (when (<= end (point))
1691 (error "Already at end of %s"
1692 (nxml-token-type-friendly-name xmltok-type)))
1693 (cond ((memq xmltok-type '(start-tag
1694 empty-element
1695 partial-start-tag
1696 partial-empty-element))
1697 (if (< (point) xmltok-name-end)
1698 xmltok-name-end
1699 (let ((att (nxml-find-following-attribute)))
1700 (cond ((not att) end)
1701 ((and (xmltok-attribute-value-start att)
1702 (<= (xmltok-attribute-value-start att)
1703 (point)))
1704 (nxml-scan-forward-in-attribute-value att))
1705 ((xmltok-attribute-value-end att)
1706 (1+ (xmltok-attribute-value-end att)))
1707 ((save-excursion
1708 (goto-char (xmltok-attribute-name-end att))
1709 (looking-at "[ \t\r\n]*="))
1710 (match-end 0))
1711 (t (xmltok-attribute-name-end att))))))
1712 ((and (eq xmltok-type 'processing-instruction)
1713 (< (point) xmltok-name-end))
1714 xmltok-name-end)
1715 (t end)))
1717 (defun nxml-scan-backward-within (_end)
1718 (setq xmltok-start
1719 (+ xmltok-start
1720 (nxml-start-delimiter-length xmltok-type)))
1721 (when (<= (point) xmltok-start)
1722 (error "Already at start of %s"
1723 (nxml-token-type-friendly-name xmltok-type)))
1724 (cond ((memq xmltok-type '(start-tag
1725 empty-element
1726 partial-start-tag
1727 partial-empty-element))
1728 (let ((att (nxml-find-preceding-attribute)))
1729 (cond ((not att) xmltok-start)
1730 ((and (xmltok-attribute-value-start att)
1731 (<= (xmltok-attribute-value-start att)
1732 (point))
1733 (<= (point)
1734 (xmltok-attribute-value-end att)))
1735 (nxml-scan-backward-in-attribute-value att))
1736 (t (xmltok-attribute-name-start att)))))
1737 ((and (eq xmltok-type 'processing-instruction)
1738 (let ((content-start (save-excursion
1739 (goto-char xmltok-name-end)
1740 (skip-chars-forward " \r\t\n")
1741 (point))))
1742 (and (< content-start (point))
1743 content-start))))
1744 (t xmltok-start)))
1746 (defun nxml-scan-forward-in-attribute-value (att)
1747 (when (= (point) (xmltok-attribute-value-end att))
1748 (error "Already at end of attribute value"))
1749 (let ((refs (xmltok-attribute-refs att))
1750 ref)
1751 (while refs
1752 (setq ref (car refs))
1753 (if (< (point) (aref ref 2))
1754 (setq refs nil)
1755 (setq ref nil)
1756 (setq refs (cdr refs))))
1757 (cond ((not ref)
1758 (xmltok-attribute-value-end att))
1759 ((< (point) (aref ref 1))
1760 (aref ref 1))
1761 ((= (point) (aref ref 1))
1762 (aref ref 2))
1764 (let ((end (- (aref ref 2)
1765 (nxml-end-delimiter-length (aref ref 0)))))
1766 (if (< (point) end)
1768 (error "Already at end of %s"
1769 (nxml-token-type-friendly-name (aref ref 0)))))))))
1771 (defun nxml-scan-backward-in-attribute-value (att)
1772 (when (= (point) (xmltok-attribute-value-start att))
1773 (error "Already at start of attribute value"))
1774 (let ((refs (reverse (xmltok-attribute-refs att)))
1775 ref)
1776 (while refs
1777 (setq ref (car refs))
1778 (if (< (aref ref 1) (point))
1779 (setq refs nil)
1780 (setq ref nil)
1781 (setq refs (cdr refs))))
1782 (cond ((not ref)
1783 (xmltok-attribute-value-start att))
1784 ((< (aref ref 2) (point))
1785 (aref ref 2))
1786 ((= (point) (aref ref 2))
1787 (aref ref 1))
1789 (let ((start (+ (aref ref 1)
1790 (nxml-start-delimiter-length (aref ref 0)))))
1791 (if (< start (point))
1792 start
1793 (error "Already at start of %s"
1794 (nxml-token-type-friendly-name (aref ref 0)))))))))
1796 (defun nxml-find-following-attribute ()
1797 (let ((ret nil)
1798 (atts (or xmltok-attributes xmltok-namespace-attributes))
1799 (more-atts (and xmltok-attributes xmltok-namespace-attributes)))
1800 (while atts
1801 (let* ((att (car atts))
1802 (name-start (xmltok-attribute-name-start att)))
1803 (cond ((and (<= name-start (point))
1804 (xmltok-attribute-value-end att)
1805 ;; <= because end is before quote
1806 (<= (point) (xmltok-attribute-value-end att)))
1807 (setq atts nil)
1808 (setq ret att))
1809 ((and (< (point) name-start)
1810 (or (not ret)
1811 (< name-start
1812 (xmltok-attribute-name-start ret))))
1813 (setq ret att))))
1814 (setq atts (cdr atts))
1815 (unless atts
1816 (setq atts more-atts)
1817 (setq more-atts nil)))
1818 ret))
1820 (defun nxml-find-preceding-attribute ()
1821 (let ((ret nil)
1822 (atts (or xmltok-attributes xmltok-namespace-attributes))
1823 (more-atts (and xmltok-attributes xmltok-namespace-attributes)))
1824 (while atts
1825 (let* ((att (car atts))
1826 (name-start (xmltok-attribute-name-start att)))
1827 (cond ((and (< name-start (point))
1828 (xmltok-attribute-value-end att)
1829 ;; <= because end is before quote
1830 (<= (point) (xmltok-attribute-value-end att)))
1831 (setq atts nil)
1832 (setq ret att))
1833 ((and (< name-start (point))
1834 (or (not ret)
1835 (< (xmltok-attribute-name-start ret)
1836 name-start)))
1837 (setq ret att))))
1838 (setq atts (cdr atts))
1839 (unless atts
1840 (setq atts more-atts)
1841 (setq more-atts nil)))
1842 ret))
1844 (defun nxml-up-element (&optional arg)
1845 (interactive "p")
1846 (or arg (setq arg 1))
1847 (if (< arg 0)
1848 (nxml-backward-up-element (- arg))
1849 (condition-case err
1850 (while (and (> arg 0)
1851 (< (point) (point-max)))
1852 (let ((token-end (nxml-token-after)))
1853 (goto-char (cond ((or (memq xmltok-type '(end-tag
1854 partial-end-tag))
1855 (and (memq xmltok-type
1856 '(empty-element
1857 partial-empty-element))
1858 (< xmltok-start (point))))
1859 token-end)
1860 ((nxml-scan-element-forward
1861 (if (and (eq xmltok-type 'start-tag)
1862 (= (point) xmltok-start))
1863 xmltok-start
1864 token-end)
1866 (t (error "No parent element")))))
1867 (setq arg (1- arg)))
1868 (nxml-scan-error
1869 (goto-char (cadr err))
1870 (apply 'error (cddr err))))))
1872 (defun nxml-backward-up-element (&optional arg)
1873 (interactive "p")
1874 (or arg (setq arg 1))
1875 (if (< arg 0)
1876 (nxml-up-element (- arg))
1877 (condition-case err
1878 (while (and (> arg 0)
1879 (< (point-min) (point)))
1880 (let ((token-end (nxml-token-before)))
1881 (goto-char (cond ((or (memq xmltok-type '(start-tag
1882 partial-start-tag))
1883 (and (memq xmltok-type
1884 '(empty-element
1885 partial-empty-element))
1886 (< (point) token-end)))
1887 xmltok-start)
1888 ((nxml-scan-element-backward
1889 (if (and (eq xmltok-type 'end-tag)
1890 (= (point) token-end))
1891 token-end
1892 xmltok-start)
1894 xmltok-start)
1895 (t (error "No parent element")))))
1896 (setq arg (1- arg)))
1897 (nxml-scan-error
1898 (goto-char (cadr err))
1899 (apply 'error (cddr err))))))
1901 (defun nxml-down-element (&optional arg)
1902 "Move forward down into the content of an element.
1903 With ARG, do this that many times.
1904 Negative ARG means move backward but still down."
1905 (interactive "p")
1906 (or arg (setq arg 1))
1907 (if (< arg 0)
1908 (nxml-backward-down-element (- arg))
1909 (while (> arg 0)
1910 (goto-char
1911 (let ((token-end (nxml-token-after)))
1912 (save-excursion
1913 (goto-char token-end)
1914 (while (progn
1915 (when (memq xmltok-type '(nil end-tag partial-end-tag))
1916 (error "No following start-tags in this element"))
1917 (not (memq xmltok-type '(start-tag partial-start-tag))))
1918 (nxml-tokenize-forward))
1919 (point))))
1920 (setq arg (1- arg)))))
1922 (defun nxml-backward-down-element (&optional arg)
1923 (interactive "p")
1924 (or arg (setq arg 1))
1925 (if (< arg 0)
1926 (nxml-down-element (- arg))
1927 (while (> arg 0)
1928 (goto-char
1929 (save-excursion
1930 (nxml-token-before)
1931 (goto-char xmltok-start)
1932 (while (progn
1933 (when (memq xmltok-type '(start-tag
1934 partial-start-tag
1935 prolog
1936 nil))
1937 (error "No preceding end-tags in this element"))
1938 (not (memq xmltok-type '(end-tag partial-end-tag))))
1939 (if (or (<= (point) nxml-prolog-end)
1940 (not (search-backward "<" nxml-prolog-end t)))
1941 (setq xmltok-type nil)
1942 (nxml-move-outside-backwards)
1943 (xmltok-forward)))
1944 xmltok-start))
1945 (setq arg (1- arg)))))
1947 (defun nxml-forward-element (&optional arg)
1948 "Move forward over one element.
1949 With ARG, do it that many times.
1950 Negative ARG means move backward."
1951 (interactive "p")
1952 (or arg (setq arg 1))
1953 (if (< arg 0)
1954 (nxml-backward-element (- arg))
1955 (condition-case err
1956 (while (and (> arg 0)
1957 (< (point) (point-max)))
1958 (goto-char
1959 (or (nxml-scan-element-forward (nxml-token-before))
1960 (error "No more elements")))
1961 (setq arg (1- arg)))
1962 (nxml-scan-error
1963 (goto-char (cadr err))
1964 (apply 'error (cddr err))))))
1966 (defun nxml-backward-element (&optional arg)
1967 "Move backward over one element.
1968 With ARG, do it that many times.
1969 Negative ARG means move forward."
1970 (interactive "p")
1971 (or arg (setq arg 1))
1972 (if (< arg 0)
1973 (nxml-forward-element (- arg))
1974 (condition-case err
1975 (while (and (> arg 0)
1976 (< (point-min) (point)))
1977 (goto-char
1978 (or (and (nxml-scan-element-backward (progn
1979 (nxml-token-after)
1980 xmltok-start))
1981 xmltok-start)
1982 (error "No preceding elements")))
1983 (setq arg (1- arg)))
1984 (nxml-scan-error
1985 (goto-char (cadr err))
1986 (apply 'error (cddr err))))))
1988 (defun nxml-mark-token-after ()
1989 (interactive)
1990 (push-mark (nxml-token-after) nil t)
1991 (goto-char xmltok-start)
1992 (message "Marked %s" xmltok-type))
1994 ;;; Paragraphs
1996 (defun nxml-mark-paragraph ()
1997 "Put point at beginning of this paragraph, mark at end.
1998 The paragraph marked is the one that contains point or follows point."
1999 (interactive)
2000 (nxml-forward-paragraph)
2001 (push-mark nil t t)
2002 (nxml-backward-paragraph))
2004 (defun nxml-forward-paragraph (&optional arg)
2005 (interactive "p")
2006 (or arg (setq arg 1))
2007 (cond ((< arg 0)
2008 (nxml-backward-paragraph (- arg)))
2009 ((> arg 0)
2010 (forward-line 0)
2011 (while (and (nxml-forward-single-paragraph)
2012 (> (setq arg (1- arg)) 0))))))
2014 (defun nxml-backward-paragraph (&optional arg)
2015 (interactive "p")
2016 (or arg (setq arg 1))
2017 (cond ((< arg 0)
2018 (nxml-forward-paragraph (- arg)))
2019 ((> arg 0)
2020 (unless (bolp)
2021 (let ((inhibit-field-text-motion t))
2022 (end-of-line)))
2023 (while (and (nxml-backward-single-paragraph)
2024 (> (setq arg (1- arg)) 0))))))
2026 (defun nxml-forward-single-paragraph ()
2027 "Move forward over a single paragraph.
2028 Return nil at end of buffer, t otherwise."
2029 (let* ((token-end (nxml-token-after))
2030 (offset (- (point) xmltok-start))
2031 pos had-data)
2032 (goto-char token-end)
2033 (while (and (< (point) (point-max))
2034 (not (setq pos
2035 (nxml-paragraph-end-pos had-data offset))))
2036 (when (nxml-token-contains-data-p offset)
2037 (setq had-data t))
2038 (nxml-tokenize-forward)
2039 (setq offset 0))
2040 (when pos (goto-char pos))))
2042 (defun nxml-backward-single-paragraph ()
2043 "Move backward over a single paragraph.
2044 Return nil at start of buffer, t otherwise."
2045 (let* ((token-end (nxml-token-before))
2046 (offset (- token-end (point)))
2047 (last-tag-pos xmltok-start)
2048 pos had-data last-data-pos)
2049 (goto-char token-end)
2050 (unless (setq pos (nxml-paragraph-start-pos nil offset))
2051 (setq had-data (nxml-token-contains-data-p nil offset))
2052 (goto-char xmltok-start)
2053 (while (and (not pos) (< (point-min) (point)))
2054 (cond ((search-backward "<" nxml-prolog-end t)
2055 (nxml-move-outside-backwards)
2056 (save-excursion
2057 (while (< (point) last-tag-pos)
2058 (xmltok-forward)
2059 (when (and (not had-data) (nxml-token-contains-data-p))
2060 (setq pos nil)
2061 (setq last-data-pos xmltok-start))
2062 (let ((tem (nxml-paragraph-start-pos had-data 0)))
2063 (when tem (setq pos tem)))))
2064 (when (and (not had-data) last-data-pos (not pos))
2065 (setq had-data t)
2066 (save-excursion
2067 (while (< (point) last-data-pos)
2068 (xmltok-forward))
2069 (let ((tem (nxml-paragraph-start-pos had-data 0)))
2070 (when tem (setq pos tem)))))
2071 (setq last-tag-pos (point)))
2072 (t (goto-char (point-min))))))
2073 (when pos (goto-char pos))))
2075 (defun nxml-token-contains-data-p (&optional start end)
2076 (setq start (+ xmltok-start (or start 0)))
2077 (setq end (- (point) (or end 0)))
2078 (when (eq xmltok-type 'cdata-section)
2079 (setq start (max start (+ xmltok-start 9)))
2080 (setq end (min end (- (point) 3))))
2081 (or (and (eq xmltok-type 'data)
2082 (eq start xmltok-start)
2083 (eq end (point)))
2084 (eq xmltok-type 'char-ref)
2085 (and (memq xmltok-type '(data cdata-section))
2086 (< start end)
2087 (save-excursion
2088 (goto-char start)
2089 (re-search-forward "[^ \t\r\n]" end t)))))
2091 (defun nxml-paragraph-end-pos (had-data offset)
2092 "Return the position of the paragraph end if contained in the current token.
2093 Return nil if the current token does not contain the paragraph end.
2094 Only characters after OFFSET from the start of the token are eligible.
2095 HAD-DATA says whether there have been non-whitespace data characters yet."
2096 (cond ((not had-data)
2097 (cond ((memq xmltok-type '(data cdata-section))
2098 (save-excursion
2099 (let ((end (point)))
2100 (goto-char (+ xmltok-start
2101 (max (if (eq xmltok-type 'cdata-section)
2104 offset)))
2105 (and (re-search-forward "[^ \t\r\n]" end t)
2106 (re-search-forward "^[ \t]*$" end t)
2107 (match-beginning 0)))))
2108 ((and (eq xmltok-type 'comment)
2109 (nxml-token-begins-line-p)
2110 (nxml-token-ends-line-p))
2111 (save-excursion
2112 (let ((end (point)))
2113 (goto-char (+ xmltok-start (max 4 offset)))
2114 (when (re-search-forward "[^ \t\r\n]" (- end 3) t)
2115 (if (re-search-forward "^[ \t]*$" end t)
2116 (match-beginning 0)
2117 (goto-char (- end 3))
2118 (skip-chars-backward " \t")
2119 (unless (bolp)
2120 (beginning-of-line 2))
2121 (point))))))))
2122 ((memq xmltok-type '(data space cdata-section))
2123 (save-excursion
2124 (let ((end (point)))
2125 (goto-char (+ xmltok-start offset))
2126 (and (re-search-forward "^[ \t]*$" end t)
2127 (match-beginning 0)))))
2128 ((and (memq xmltok-type '(start-tag
2129 end-tag
2130 empty-element
2131 comment
2132 processing-instruction
2133 entity-ref))
2134 (nxml-token-begins-line-p)
2135 (nxml-token-ends-line-p))
2136 (save-excursion
2137 (goto-char xmltok-start)
2138 (skip-chars-backward " \t")
2139 (point)))
2140 ((and (eq xmltok-type 'end-tag)
2141 (looking-at "[ \t]*$")
2142 (not (nxml-in-mixed-content-p t)))
2143 (save-excursion
2144 (or (search-forward "\n" nil t)
2145 (point-max))))))
2147 (defun nxml-paragraph-start-pos (had-data offset)
2148 "Return the position of the paragraph start if contained in the current token.
2149 Return nil if the current token does not contain the paragraph start.
2150 Only characters before OFFSET from the end of the token are eligible.
2151 HAD-DATA says whether there have been non-whitespace data characters yet."
2152 (cond ((not had-data)
2153 (cond ((memq xmltok-type '(data cdata-section))
2154 (save-excursion
2155 (goto-char (- (point)
2156 (max (if (eq xmltok-type 'cdata-section)
2159 offset)))
2160 (and (re-search-backward "[^ \t\r\n]" xmltok-start t)
2161 (re-search-backward "^[ \t]*$" xmltok-start t)
2162 (match-beginning 0))))
2163 ((and (eq xmltok-type 'comment)
2164 (nxml-token-ends-line-p)
2165 (nxml-token-begins-line-p))
2166 (save-excursion
2167 (goto-char (- (point) (max 3 offset)))
2168 (when (and (< (+ xmltok-start 4) (point))
2169 (re-search-backward "[^ \t\r\n]"
2170 (+ xmltok-start 4)
2172 (if (re-search-backward "^[ \t]*$" xmltok-start t)
2173 (match-beginning 0)
2174 (goto-char xmltok-start)
2175 (if (looking-at "<!--[ \t]*\n")
2176 (match-end 0)
2177 (skip-chars-backward " \t")
2178 (point))))))))
2179 ((memq xmltok-type '(data space cdata-section))
2180 (save-excursion
2181 (goto-char (- (point) offset))
2182 (and (re-search-backward "^[ \t]*$" xmltok-start t)
2183 (match-beginning 0))))
2184 ((and (memq xmltok-type '(start-tag
2185 end-tag
2186 empty-element
2187 comment
2188 processing-instruction
2189 entity-ref))
2190 (nxml-token-ends-line-p)
2191 (nxml-token-begins-line-p))
2192 (or (search-forward "\n" nil t)
2193 (point-max)))
2194 ((and (eq xmltok-type 'start-tag)
2195 (nxml-token-begins-line-p)
2196 (not (save-excursion
2197 (goto-char xmltok-start)
2198 (nxml-in-mixed-content-p nil))))
2199 (save-excursion
2200 (goto-char xmltok-start)
2201 (skip-chars-backward " \t")
2202 ;; include any blank line before
2203 (or (and (eq (char-before) ?\n)
2204 (save-excursion
2205 (goto-char (1- (point)))
2206 (skip-chars-backward " \t")
2207 (and (bolp) (point))))
2208 (point))))))
2210 (defun nxml-token-ends-line-p () (looking-at "[ \t]*$"))
2212 (defun nxml-token-begins-line-p ()
2213 (save-excursion
2214 (goto-char xmltok-start)
2215 (skip-chars-backward " \t")
2216 (bolp)))
2218 (defun nxml-in-mixed-content-p (endp)
2219 "Return non-nil if point is in mixed content.
2220 Point must be after an end-tag or before a start-tag.
2221 ENDP is t in the former case, nil in the latter."
2222 (let (matching-tag-pos)
2223 (cond ((not (run-hook-with-args-until-failure
2224 'nxml-in-mixed-content-hook))
2225 nil)
2226 ;; See if the matching tag does not start or end a line.
2227 ((condition-case nil
2228 (progn
2229 (setq matching-tag-pos
2230 (xmltok-save
2231 (if endp
2232 (and (nxml-scan-element-backward (point))
2233 xmltok-start)
2234 (nxml-scan-element-forward (point)))))
2235 (and matching-tag-pos
2236 (save-excursion
2237 (goto-char matching-tag-pos)
2238 (not (if endp
2239 (progn
2240 (skip-chars-backward " \t")
2241 (bolp))
2242 (looking-at "[ \t]*$"))))))
2243 (nxml-scan-error nil))
2245 ;; See if there's data at the same level.
2246 ((let (start end)
2247 (if endp
2248 (setq start matching-tag-pos
2249 end (point))
2250 (setq start (point)
2251 end matching-tag-pos))
2252 (save-excursion
2253 (or (when start
2254 (goto-char start)
2255 (nxml-preceding-sibling-data-p))
2256 (when end
2257 (goto-char end)
2258 (nxml-following-sibling-data-p)))))
2260 ;; Otherwise, treat as not mixed
2261 (t nil))))
2263 (defun nxml-preceding-sibling-data-p ()
2264 "Return non-nil if there is a previous sibling that is data."
2265 (let ((lim (max (- (point) nxml-mixed-scan-distance)
2266 nxml-prolog-end))
2267 (level 0)
2268 found end)
2269 (xmltok-save
2270 (save-excursion
2271 (while (and (< lim (point))
2272 (>= level 0)
2273 (not found)
2274 (progn
2275 (setq end (point))
2276 (search-backward "<" lim t)))
2277 (nxml-move-outside-backwards)
2278 (save-excursion
2279 (xmltok-forward)
2280 (let ((prev-level level))
2281 (cond ((eq xmltok-type 'end-tag)
2282 (setq level (1+ level)))
2283 ((eq xmltok-type 'start-tag)
2284 (setq level (1- level))))
2285 (when (eq prev-level 0)
2286 (while (and (< (point) end) (not found))
2287 (xmltok-forward)
2288 (when (memq xmltok-type '(data cdata-section char-ref))
2289 (setq found t)))))))))
2290 found))
2292 (defun nxml-following-sibling-data-p ()
2293 (let ((lim (min (+ (point) nxml-mixed-scan-distance)
2294 (point-max)))
2295 (level 0)
2296 found)
2297 (xmltok-save
2298 (save-excursion
2299 (while (and (< (point) lim)
2300 (>= level 0)
2301 (nxml-tokenize-forward)
2302 (not found))
2303 (cond ((eq xmltok-type 'start-tag)
2304 (setq level (1+ level)))
2305 ((eq xmltok-type 'end-tag)
2306 (setq level (1- level)))
2307 ((and (eq level 0)
2308 (memq xmltok-type '(data cdata-section char-ref)))
2309 (setq found t))))))
2310 found))
2312 ;;; Filling
2314 (defun nxml-do-fill-paragraph (arg)
2315 (let (fill-paragraph-function
2316 fill-prefix
2317 start end)
2318 (save-excursion
2319 (nxml-forward-paragraph)
2320 (setq end (point))
2321 (nxml-backward-paragraph)
2322 (skip-chars-forward " \t\r\n")
2323 (setq start (point))
2324 (beginning-of-line)
2325 (setq fill-prefix (buffer-substring-no-properties (point) start))
2326 (when (and (not (nxml-get-inside (point)))
2327 (looking-at "[ \t]*<!--"))
2328 (setq fill-prefix (concat fill-prefix " ")))
2329 (fill-region-as-paragraph start end arg))
2330 (skip-line-prefix fill-prefix)
2331 fill-prefix))
2333 (defun nxml-newline-and-indent (soft)
2334 (delete-horizontal-space)
2335 (if soft (insert-and-inherit ?\n) (newline 1))
2336 (nxml-indent-line))
2339 ;;; Dynamic markup
2341 (defvar nxml-dynamic-markup-prev-pos nil)
2342 (defvar nxml-dynamic-markup-prev-lengths nil)
2343 (defvar nxml-dynamic-markup-prev-found-marker nil)
2344 (defvar nxml-dynamic-markup-prev-start-tags (make-hash-table :test 'equal))
2346 (defun nxml-dynamic-markup-word ()
2347 "Dynamically markup the word before point.
2348 This attempts to find a tag to put around the word before point based
2349 on the contents of the current buffer. The end-tag will be inserted at
2350 point. The start-tag will be inserted at or before the beginning of
2351 the word before point; the contents of the current buffer is used to
2352 decide where.
2354 It works in a similar way to \\[dabbrev-expand]. It searches first
2355 backwards from point, then forwards from point for an element whose
2356 content is a string which matches the contents of the buffer before
2357 point and which includes at least the word before point. It then
2358 copies the start- and end-tags from that element and uses them to
2359 surround the matching string before point.
2361 Repeating \\[nxml-dynamic-markup-word] immediately after successful
2362 \\[nxml-dynamic-markup-word] removes the previously inserted markup
2363 and attempts to find another possible way to do the markup."
2364 (interactive "*")
2365 (let (search-start-pos)
2366 (if (and (integerp nxml-dynamic-markup-prev-pos)
2367 (= nxml-dynamic-markup-prev-pos (point))
2368 (eq last-command this-command)
2369 nxml-dynamic-markup-prev-lengths)
2370 (let* ((end-tag-open-pos
2371 (- nxml-dynamic-markup-prev-pos
2372 (nth 2 nxml-dynamic-markup-prev-lengths)))
2373 (start-tag-close-pos
2374 (- end-tag-open-pos
2375 (nth 1 nxml-dynamic-markup-prev-lengths)))
2376 (start-tag-open-pos
2377 (- start-tag-close-pos
2378 (nth 0 nxml-dynamic-markup-prev-lengths))))
2379 (delete-region end-tag-open-pos nxml-dynamic-markup-prev-pos)
2380 (delete-region start-tag-open-pos start-tag-close-pos)
2381 (setq search-start-pos
2382 (marker-position nxml-dynamic-markup-prev-found-marker)))
2383 (clrhash nxml-dynamic-markup-prev-start-tags))
2384 (setq nxml-dynamic-markup-prev-pos nil)
2385 (setq nxml-dynamic-markup-prev-lengths nil)
2386 (setq nxml-dynamic-markup-prev-found-marker nil)
2387 (goto-char
2388 (save-excursion
2389 (let* ((pos (point))
2390 (word (progn
2391 (backward-word 1)
2392 (unless (< (point) pos)
2393 (error "No word to markup"))
2394 (buffer-substring-no-properties (point) pos)))
2395 (search (concat word "</"))
2396 done)
2397 (when search-start-pos
2398 (goto-char search-start-pos))
2399 (while (and (not done)
2400 (or (and (< (point) pos)
2401 (or (search-backward search nil t)
2402 (progn (goto-char pos) nil)))
2403 (search-forward search nil t)))
2404 (goto-char (- (match-end 0) 2))
2405 (setq done (nxml-try-copy-markup pos)))
2406 (or done
2407 (error (if (zerop (hash-table-count
2408 nxml-dynamic-markup-prev-start-tags))
2409 "No possible markup found for `%s'"
2410 "No more markup possibilities found for `%s'")
2411 word)))))))
2413 (defun nxml-try-copy-markup (word-end-pos)
2414 (save-excursion
2415 (let ((end-tag-pos (point)))
2416 (when (and (not (nxml-get-inside end-tag-pos))
2417 (search-backward "<" nil t)
2418 (not (nxml-get-inside (point))))
2419 (xmltok-forward)
2420 (when (and (eq xmltok-type 'start-tag)
2421 (< (point) end-tag-pos))
2422 (let* ((start-tag-close-pos (point))
2423 (start-tag
2424 (buffer-substring-no-properties xmltok-start
2425 start-tag-close-pos))
2426 (words
2427 (nreverse
2428 (split-string
2429 (buffer-substring-no-properties start-tag-close-pos
2430 end-tag-pos)
2431 "[ \t\r\n]+"))))
2432 (goto-char word-end-pos)
2433 (while (and words
2434 (re-search-backward (concat
2435 (regexp-quote (car words))
2436 "\\=")
2439 (setq words (cdr words))
2440 (skip-chars-backward " \t\r\n"))
2441 (when (and (not words)
2442 (progn
2443 (skip-chars-forward " \t\r\n")
2444 (not (gethash (cons (point) start-tag)
2445 nxml-dynamic-markup-prev-start-tags)))
2446 (or (< end-tag-pos (point))
2447 (< word-end-pos xmltok-start)))
2448 (setq nxml-dynamic-markup-prev-found-marker
2449 (copy-marker end-tag-pos t))
2450 (puthash (cons (point) start-tag)
2452 nxml-dynamic-markup-prev-start-tags)
2453 (setq nxml-dynamic-markup-prev-lengths
2454 (list (- start-tag-close-pos xmltok-start)
2455 (- word-end-pos (point))
2456 (+ (- xmltok-name-end xmltok-start) 2)))
2457 (let ((name (xmltok-start-tag-qname)))
2458 (insert start-tag)
2459 (goto-char (+ word-end-pos
2460 (- start-tag-close-pos xmltok-start)))
2461 (insert "</" name ">")
2462 (setq nxml-dynamic-markup-prev-pos (point))))))))))
2465 ;;; Character names
2467 (defvar nxml-char-name-ignore-case t)
2469 (defvar nxml-char-name-alist nil
2470 "Alist of character names.
2471 Each member of the list has the form (NAME CODE . NAMESET),
2472 where NAME is a string naming a character, NAMESET is a symbol
2473 identifying a set of names and CODE is an integer specifying the
2474 Unicode scalar value of the named character.
2475 The NAME will only be used for completion if NAMESET has
2476 a non-nil `nxml-char-name-set-enabled' property.
2477 If NAMESET does does not have `nxml-char-name-set-defined' property,
2478 then it must have a `nxml-char-name-set-file' property and `load'
2479 will be applied to the value of this property if the nameset
2480 is enabled.")
2482 (defvar nxml-char-name-table (make-hash-table :test 'eq)
2483 "Hash table for mapping char codes to names.
2484 Each key is a Unicode scalar value.
2485 Each value is a list of pairs of the form (NAMESET . NAME),
2486 where NAMESET is a symbol identifying a set of names,
2487 and NAME is a string naming a character.")
2489 (defvar nxml-autoload-char-name-set-list nil
2490 "List of char namesets that can be autoloaded.")
2492 (defun nxml-enable-char-name-set (nameset)
2493 (put nameset 'nxml-char-name-set-enabled t))
2495 (defun nxml-disable-char-name-set (nameset)
2496 (put nameset 'nxml-char-name-set-enabled nil))
2498 (defun nxml-char-name-set-enabled-p (nameset)
2499 (get nameset 'nxml-char-name-set-enabled))
2501 (defun nxml-autoload-char-name-set (nameset file)
2502 (unless (memq nameset nxml-autoload-char-name-set-list)
2503 (setq nxml-autoload-char-name-set-list
2504 (cons nameset nxml-autoload-char-name-set-list)))
2505 (put nameset 'nxml-char-name-set-file file))
2507 (defun nxml-define-char-name-set (nameset alist)
2508 "Define a set of character names.
2509 NAMESET is a symbol identifying the set.
2510 ALIST is a list where each member has the form (NAME CODE),
2511 where NAME is a string naming a character and code is an
2512 integer giving the Unicode scalar value of the character."
2513 (when (get nameset 'nxml-char-name-set-defined)
2514 (error "Nameset `%s' already defined" nameset))
2515 (let ((iter alist))
2516 (while iter
2517 (let* ((name-code (car iter))
2518 (name (car name-code))
2519 (code (cadr name-code)))
2520 (puthash code
2521 (cons (cons nameset name)
2522 (gethash code nxml-char-name-table))
2523 nxml-char-name-table))
2524 (setcdr (cdr (car iter)) nameset)
2525 (setq iter (cdr iter))))
2526 (setq nxml-char-name-alist
2527 (nconc alist nxml-char-name-alist))
2528 (put nameset 'nxml-char-name-set-defined t))
2530 (defun nxml-get-char-name (code)
2531 (mapc 'nxml-maybe-load-char-name-set nxml-autoload-char-name-set-list)
2532 (let ((names (gethash code nxml-char-name-table))
2533 name)
2534 (while (and names (not name))
2535 (if (nxml-char-name-set-enabled-p (caar names))
2536 (setq name (cdar names))
2537 (setq names (cdr names))))
2538 name))
2540 (defvar nxml-named-char-history nil)
2542 (defun nxml-insert-named-char (arg)
2543 "Insert a character using its name.
2544 The name is read from the minibuffer.
2545 Normally, inserts the character as a numeric character reference.
2546 With a prefix argument, inserts the character directly."
2547 (interactive "*P")
2548 (mapc 'nxml-maybe-load-char-name-set nxml-autoload-char-name-set-list)
2549 (let ((name
2550 (let ((completion-ignore-case nxml-char-name-ignore-case))
2551 (completing-read "Character name: "
2552 nxml-char-name-alist
2553 (lambda (member)
2554 (get (cddr member) 'nxml-char-name-set-enabled))
2557 'nxml-named-char-history)))
2558 (alist nxml-char-name-alist)
2559 elt code)
2560 (while (and alist (not code))
2561 (setq elt (assoc name alist))
2562 (if (get (cddr elt) 'nxml-char-name-set-enabled)
2563 (setq code (cadr elt))
2564 (setq alist (cdr (member elt alist)))))
2565 (when code
2566 (insert (if arg
2567 (or (decode-char 'ucs code)
2568 (error "Character %x is not supported by Emacs"
2569 code))
2570 (format "&#x%X;" code))))))
2572 (defun nxml-maybe-load-char-name-set (sym)
2573 (when (and (get sym 'nxml-char-name-set-enabled)
2574 (not (get sym 'nxml-char-name-set-defined))
2575 (stringp (get sym 'nxml-char-name-set-file)))
2576 (load (get sym 'nxml-char-name-set-file))))
2578 (defun nxml-toggle-char-ref-extra-display (arg)
2579 "Toggle the display of extra information for character references."
2580 (interactive "P")
2581 (let ((new (if (null arg)
2582 (not nxml-char-ref-extra-display)
2583 (> (prefix-numeric-value arg) 0))))
2584 (when (not (eq new nxml-char-ref-extra-display))
2585 (setq nxml-char-ref-extra-display new)
2586 (font-lock-flush))))
2588 (put 'nxml-char-ref 'evaporate t)
2590 (defun nxml-char-ref-display-extra (start end n)
2591 (when nxml-char-ref-extra-display
2592 (let ((name (nxml-get-char-name n))
2593 (glyph-string (and nxml-char-ref-display-glyph-flag
2594 (nxml-glyph-display-string n 'nxml-glyph)))
2596 (when (or name glyph-string)
2597 (setq ov (make-overlay start end nil t))
2598 (overlay-put ov 'category 'nxml-char-ref)
2599 (when name
2600 (overlay-put ov 'help-echo name))
2601 (when glyph-string
2602 (overlay-put ov
2603 'after-string
2604 (propertize glyph-string 'face 'nxml-glyph)))))))
2606 (defun nxml-clear-char-ref-extra-display (start end)
2607 (let ((ov (overlays-in start end)))
2608 (while ov
2609 (when (eq (overlay-get (car ov) 'category) 'nxml-char-ref)
2610 (delete-overlay (car ov)))
2611 (setq ov (cdr ov)))))
2614 (defun nxml-start-delimiter-length (type)
2615 (or (get type 'nxml-start-delimiter-length)
2618 (put 'cdata-section 'nxml-start-delimiter-length 9)
2619 (put 'comment 'nxml-start-delimiter-length 4)
2620 (put 'processing-instruction 'nxml-start-delimiter-length 2)
2621 (put 'start-tag 'nxml-start-delimiter-length 1)
2622 (put 'empty-element 'nxml-start-delimiter-length 1)
2623 (put 'partial-empty-element 'nxml-start-delimiter-length 1)
2624 (put 'entity-ref 'nxml-start-delimiter-length 1)
2625 (put 'char-ref 'nxml-start-delimiter-length 2)
2627 (defun nxml-end-delimiter-length (type)
2628 (or (get type 'nxml-end-delimiter-length)
2631 (put 'cdata-section 'nxml-end-delimiter-length 3)
2632 (put 'comment 'nxml-end-delimiter-length 3)
2633 (put 'processing-instruction 'nxml-end-delimiter-length 2)
2634 (put 'start-tag 'nxml-end-delimiter-length 1)
2635 (put 'empty-element 'nxml-end-delimiter-length 2)
2636 (put 'partial-empty-element 'nxml-end-delimiter-length 1)
2637 (put 'entity-ref 'nxml-end-delimiter-length 1)
2638 (put 'char-ref 'nxml-end-delimiter-length 1)
2640 (defun nxml-token-type-friendly-name (type)
2641 (or (get type 'nxml-friendly-name)
2642 (symbol-name type)))
2644 (put 'cdata-section 'nxml-friendly-name "CDATA section")
2645 (put 'processing-instruction 'nxml-friendly-name "processing instruction")
2646 (put 'entity-ref 'nxml-friendly-name "entity reference")
2647 (put 'char-ref 'nxml-friendly-name "character reference")
2649 ;; Only do this in loaddefs, so that if someone defines a different
2650 ;; alias in .emacs, loading this file afterwards does not clobber it.
2651 ;;;###autoload(defalias 'xml-mode 'nxml-mode)
2653 (provide 'nxml-mode)
2655 ;;; nxml-mode.el ends here