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