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