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