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