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