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