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