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