1 ;;; nxml-mode.el --- a new XML mode
3 ;; Copyright (C) 2003, 2004, 2007 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 3, or (at your option)
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 ;; To use this include rng-auto.el in your .emacs.
29 ;; See nxml-rap.el for description of parsing strategy.
31 ;; The font locking here is independent of font-lock.el. We want to
32 ;; do more sophisticated handling of changes and we want to use the
33 ;; same xmltok rather than regexps for parsing so that we parse
34 ;; consistently and correctly.
38 (when (featurep 'mucs)
39 (error "nxml-mode is not compatible with Mule-UCS"))
51 "New XML editing mode"
55 (defgroup nxml-highlighting-faces nil
56 "Faces for XML syntax highlighting."
58 :group 'font-lock-highlighting-faces)
60 (defcustom nxml-syntax-highlight-flag t
61 "*Non-nil means nxml-mode should perform syntax highlighting."
65 (defcustom nxml-char-ref-display-glyph-flag t
66 "*Non-nil means display glyph following character reference.
67 The glyph is displayed in `nxml-glyph-face'. The hook
68 `nxml-glyph-set-hook' can be used to customize for which characters
69 glyphs are displayed."
73 (defcustom nxml-mode-hook nil
74 "Hook run by command `nxml-mode'."
78 (defcustom nxml-sexp-element-flag nil
79 "*Non-nil means sexp commands treat an element as a single expression."
83 (defcustom nxml-slash-auto-complete-flag nil
84 "*Non-nil means typing a slash automatically completes the end-tag.
85 This is used by `nxml-electric-slash'."
89 (defcustom nxml-child-indent 2
90 "*Indentation for the children of an element relative to the start-tag.
91 This only applies when the line or lines containing the start-tag contains
92 nothing else other than that start-tag."
96 (defcustom nxml-attribute-indent 4
97 "*Indentation for the attributes of an element relative to the start-tag.
98 This only applies when the first attribute of a tag starts a line. In other
99 cases, the first attribute on one line is indented the same as the first
100 attribute on the previous line."
104 (defvar nxml-fontify-chunk-size 500)
106 (defcustom nxml-bind-meta-tab-to-complete-flag (not window-system)
107 "*Non-nil means bind M-TAB in `nxml-mode-map' to `nxml-complete'.
108 C-return will be bound to `nxml-complete' in any case.
109 M-TAB gets swallowed by many window systems/managers, and
110 `documentation' will show M-TAB rather than C-return as the
111 binding `rng-complete' when both are bound. So it's better
112 to bind M-TAB only when it will work."
114 :set (lambda (sym flag)
115 (set-default sym flag)
116 (when (and (boundp 'nxml-mode-map) nxml-mode-map)
117 (define-key nxml-mode-map "\M-\t" (and flag 'nxml-complete))))
120 (defcustom nxml-prefer-utf-16-to-utf-8-flag nil
121 "*Non-nil means prefer UTF-16 to UTF-8 when saving a buffer.
122 This is used only when a buffer does not contain an encoding declaration
123 and when its current `buffer-file-coding-system' specifies neither UTF-16
128 (defcustom nxml-prefer-utf-16-little-to-big-endian-flag (eq system-type
130 "*Non-nil means prefer little-endian to big-endian byte-order for UTF-16.
131 This is used only for saving a buffer; when reading the byte-order is
132 auto-detected. It may be relevant both when there is no encoding declaration
133 and when the encoding declaration specifies `UTF-16'."
137 (defcustom nxml-default-buffer-file-coding-system nil
138 "*Default value for `buffer-file-coding-system' for a buffer for a new file.
139 Nil means use the default value of `buffer-file-coding-system' as normal.
140 A buffer's `buffer-file-coding-system' affects what \\[nxml-insert-xml-declaration] inserts."
142 :type 'coding-system)
144 (defcustom nxml-auto-insert-xml-declaration-flag nil
145 "*Non-nil means automatically insert an XML declaration in a new file.
146 The XML declaration is inserted using `nxml-insert-xml-declaration'."
150 ;; The following are the colors we use with a light background.
151 ;; The two blues have the same hue but contrasting saturation/value.
152 ;; The hue of the green is 120 degrees different from that of the
153 ;; blue. The red used for highlighting errors is 120 degrees
154 ;; different again. We use the light blue only for refs and
155 ;; delimiters, since these are short (long stretches in a light color
156 ;; would be too hard to read). The dark blue is closest to black
157 ;; (which we use by default for text), so we use it for attribute
158 ;; values, which are similar to text.
160 (defconst nxml-light-blue-color "#9292C9") ; hue 240
161 (defconst nxml-dark-blue-color "#3A3A7B") ; hue 240
162 (defconst nxml-green-color "#257A25") ; hue 120
164 ;; Similar principles apply with a dark background. However,
165 ;; we switch green and blue, because darker blues are very hard to
166 ;; read (for me anyway) on a dark background.
168 (defconst nxml-sky-blue-color "#ACACFC") ; hue 240
169 (defconst nxml-dark-green-color "#00AD00") ; hue 120
170 (defconst nxml-light-green-color "#70F170") ; hue 120
172 (defface nxml-delimited-data-face
173 `((((class color) (background light)) (:foreground ,nxml-dark-blue-color))
174 (((class color) (background dark)) (:foreground ,nxml-light-green-color)))
175 "Face used to highlight data enclosed between delimiters.
176 By default, this is inherited by `nxml-attribute-value-face'
177 and `nxml-processing-instruction-content-face'."
178 :group 'nxml-highlighting-faces)
180 (defface nxml-name-face
181 `((((class color) (background light)) (:foreground ,nxml-green-color))
182 (((class color) (background dark)) (:foreground ,nxml-sky-blue-color)))
183 "Face used to highlight various names.
184 This includes element and attribute names, processing
185 instruction targets and the CDATA keyword in a CDATA section.
186 This is not used directly, but only via inheritance by other faces."
187 :group 'nxml-highlighting-faces)
189 (defface nxml-ref-face
190 `((((class color) (background light)) (:foreground ,nxml-light-blue-color))
191 (((class color) (background dark)) (:foreground ,nxml-dark-green-color)))
192 "Face used to highlight character and entity references.
193 This is not used directly, but only via inheritance by other faces."
194 :group 'nxml-highlighting-faces)
196 (defface nxml-delimiter-face
197 `((((class color) (background light)) (:foreground ,nxml-light-blue-color))
198 (((class color) (background dark)) (:foreground ,nxml-dark-green-color))
200 "Face used to highlight delimiters.
201 This is not used directly, but only via inheritance by other faces."
202 :group 'nxml-highlighting-faces)
204 (defface nxml-text-face
206 "Face used to highlight text."
207 :group 'nxml-highlighting-faces)
209 (defface nxml-comment-content-face
211 "Face used to highlight the content of comments."
212 :group 'nxml-highlighting-faces)
214 (defface nxml-comment-delimiter-face
215 '((t (:inherit nxml-delimiter-face)))
216 "Face used for the delimiters of comments, i.e <!-- and -->."
217 :group 'nxml-highlighting-faces)
219 (defface nxml-processing-instruction-delimiter-face
220 '((t (:inherit nxml-delimiter-face)))
221 "Face used for the delimiters of processing instructions, i.e <? and ?>."
222 :group 'nxml-highlighting-faces)
224 (defface nxml-processing-instruction-target-face
225 '((t (:inherit nxml-name-face)))
226 "Face used for the target of processing instructions."
227 :group 'nxml-highlighting-faces)
229 (defface nxml-processing-instruction-content-face
230 '((t (:inherit nxml-delimited-data-face)))
231 "Face used for the content of processing instructions."
232 :group 'nxml-highlighting-faces)
234 (defface nxml-cdata-section-delimiter-face
235 '((t (:inherit nxml-delimiter-face)))
236 "Face used for the delimiters of CDATA sections, i.e <![, [, and ]]>."
237 :group 'nxml-highlighting-faces)
239 (defface nxml-cdata-section-CDATA-face
240 '((t (:inherit nxml-name-face)))
241 "Face used for the CDATA keyword in CDATA sections."
242 :group 'nxml-highlighting-faces)
244 (defface nxml-cdata-section-content-face
245 '((t (:inherit nxml-text-face)))
246 "Face used for the content of CDATA sections."
247 :group 'nxml-highlighting-faces)
249 (defface nxml-char-ref-number-face
250 '((t (:inherit nxml-ref-face)))
251 "Face used for the number in character references.
252 This includes ths `x' in hex references."
253 :group 'nxml-highlighting-faces)
255 (defface nxml-char-ref-delimiter-face
256 '((t (:inherit nxml-ref-face)))
257 "Face used for the delimiters of character references, i.e &# and ;."
258 :group 'nxml-highlighting-faces)
260 (defface nxml-entity-ref-name-face
261 '((t (:inherit nxml-ref-face)))
262 "Face used for the entity name in general entity references."
263 :group 'nxml-highlighting-faces)
265 (defface nxml-entity-ref-delimiter-face
266 '((t (:inherit nxml-ref-face)))
267 "Face used for the delimiters of entity references, i.e & and ;."
268 :group 'nxml-highlighting-faces)
270 (defface nxml-tag-delimiter-face
271 '((t (:inherit nxml-delimiter-face)))
272 "Face used for the angle brackets delimiting tags.
273 `nxml-tag-slash-face' is used for slashes."
274 :group 'nxml-highlighting-faces)
276 (defface nxml-tag-slash-face
277 '((t (:inherit nxml-name-face)))
278 "Face used for slashes in tags, both in end-tags and empty-elements."
279 :group 'nxml-highlighting-faces)
281 (defface nxml-element-prefix-face
282 '((t (:inherit nxml-name-face)))
283 "Face used for the prefix of elements."
284 :group 'nxml-highlighting-faces)
286 (defface nxml-element-colon-face
287 '((t (:inherit nxml-name-face)))
288 "Face used for the colon in element names."
289 :group 'nxml-highlighting-faces)
291 (defface nxml-element-local-name-face
292 '((t (:inherit nxml-name-face)))
293 "Face used for the local name of elements."
294 :group 'nxml-highlighting-faces)
296 (defface nxml-attribute-prefix-face
297 '((t (:inherit nxml-name-face)))
298 "Face used for the prefix of attributes."
299 :group 'nxml-highlighting-faces)
301 (defface nxml-attribute-colon-face
302 '((t (:inherit nxml-name-face)))
303 "Face used for the colon in attribute names."
304 :group 'nxml-highlighting-faces)
306 (defface nxml-attribute-local-name-face
307 '((t (:inherit nxml-name-face)))
308 "Face used for the local name of attributes."
309 :group 'nxml-highlighting-faces)
311 (defface nxml-namespace-attribute-xmlns-face
312 '((t (:inherit nxml-name-face)))
313 "Face used for `xmlns' in namespace attributes."
314 :group 'nxml-highlighting-faces)
316 (defface nxml-namespace-attribute-colon-face
317 '((t (:inherit nxml-name-face)))
318 "Face used for the colon in namespace attributes."
319 :group 'nxml-highlighting-faces)
321 (defface nxml-namespace-attribute-prefix-face
322 '((t (:inherit nxml-name-face)))
323 "Face used for the prefix declared in namespace attributes."
324 :group 'nxml-highlighting-faces)
326 (defface nxml-attribute-value-face
327 '((t (:inherit nxml-delimited-data-face)))
328 "Face used for the value of attributes."
329 :group 'nxml-highlighting-faces)
331 (defface nxml-attribute-value-delimiter-face
332 '((t (:inherit nxml-delimiter-face)))
333 "Face used for the delimiters of attribute values."
334 :group 'nxml-highlighting-faces)
336 (defface nxml-namespace-attribute-value-face
337 '((t (:inherit nxml-attribute-value-face)))
338 "Face used for the value of namespace attributes."
339 :group 'nxml-highlighting-faces)
341 (defface nxml-namespace-attribute-value-delimiter-face
342 '((t (:inherit nxml-attribute-value-delimiter-face)))
343 "Face used for the delimiters of namespace attribute values."
344 :group 'nxml-highlighting-faces)
346 (defface nxml-prolog-literal-delimiter-face
347 '((t (:inherit nxml-delimiter-face)))
348 "Face used for the delimiters of literals in the prolog."
349 :group 'nxml-highlighting-faces)
351 (defface nxml-prolog-literal-content-face
352 '((t (:inherit nxml-delimited-data-face)))
353 "Face used for the content of literals in the prolog."
354 :group 'nxml-highlighting-faces)
356 (defface nxml-prolog-keyword-face
357 '((t (:inherit nxml-name-face)))
358 "Face used for keywords in the prolog."
359 :group 'nxml-highlighting-faces)
361 (defface nxml-markup-declaration-delimiter-face
362 '((t (:inherit nxml-delimiter-face)))
363 "Face used for the delimiters of markup declarations in the prolog.
364 The delimiters are <! and >."
365 :group 'nxml-highlighting-faces)
367 (defface nxml-hash-face
368 '((t (:inherit nxml-name-face)))
369 "Face used for # before a name in the prolog."
370 :group 'nxml-highlighting-faces)
372 (defface nxml-glyph-face
393 "Face used for glyph for char references."
394 :group 'nxml-highlighting-faces)
398 ;; This is initialized in rng-auto.el.
399 (defvar nxml-version nil
400 "*The version of nxml-mode that is being used.")
402 (defvar nxml-prolog-regions nil
403 "List of regions in the prolog to be fontified.
404 See the function `xmltok-forward-prolog' for more information.")
405 (make-variable-buffer-local 'nxml-prolog-regions)
407 (defvar nxml-last-fontify-end nil
408 "Position where fontification last ended.
409 Nil if the buffer changed since the last fontification.")
410 (make-variable-buffer-local 'nxml-last-fontify-end)
412 (defvar nxml-degraded nil
413 "Non-nil if currently operating in degraded mode.
414 Degraded mode is enabled when an internal error is encountered in the
415 fontification or after-change functions.")
416 (make-variable-buffer-local 'nxml-degraded)
418 (defvar nxml-completion-hook nil
419 "Hook run by `nxml-complete'.
420 This hook is run until success.")
422 (defvar nxml-in-mixed-content-hook nil
423 "Hook to determine whether point is in mixed content.
424 The hook is called without arguments. It should return nil if it is
425 definitely not mixed; non-nil otherwise. The hook will be run until
426 one of the functions returns nil.")
428 (defvar nxml-mixed-scan-distance 4000
429 "Maximum distance from point to scan when checking for mixed content.")
431 (defvar nxml-end-tag-indent-scan-distance 4000
432 "Maximum distance from point to scan backwards when indenting end-tag.")
434 (defvar nxml-char-ref-extra-display t
435 "Non-nil means display extra information for character references.
436 The extra information consists of a tooltip with the character name
437 and, if `nxml-char-ref-display-glyph-flag' is non-nil, a glyph
438 corresponding to the referenced character following the character
440 (make-variable-buffer-local 'nxml-char-ref-extra-display)
442 (defvar nxml-mode-map
443 (let ((map (make-sparse-keymap)))
444 (define-key map "\M-\C-u" 'nxml-backward-up-element)
445 (define-key map "\M-\C-d" 'nxml-down-element)
446 (define-key map "\M-\C-n" 'nxml-forward-element)
447 (define-key map "\M-\C-p" 'nxml-backward-element)
448 (define-key map "\M-{" 'nxml-backward-paragraph)
449 (define-key map "\M-}" 'nxml-forward-paragraph)
450 (define-key map "\M-h" 'nxml-mark-paragraph)
451 (define-key map "\C-c\C-f" 'nxml-finish-element)
452 (define-key map "\C-c\C-m" 'nxml-split-element)
453 (define-key map "\C-c\C-b" 'nxml-balanced-close-start-tag-block)
454 (define-key map "\C-c\C-i" 'nxml-balanced-close-start-tag-inline)
455 (define-key map "\C-c\C-x" 'nxml-insert-xml-declaration)
456 (define-key map "\C-c\C-d" 'nxml-dynamic-markup-word)
458 (define-key map "\C-c\C-u" 'nxml-insert-named-char)
459 (define-key map "\C-c\C-o" nxml-outline-prefix-map)
460 (define-key map [S-mouse-2] 'nxml-mouse-hide-direct-text-content)
461 (define-key map "/" 'nxml-electric-slash)
462 (define-key map [C-return] 'nxml-complete)
463 (when nxml-bind-meta-tab-to-complete-flag
464 (define-key map "\M-\t" 'nxml-complete))
466 "Keymap for nxml-mode.")
468 (defsubst nxml-set-face (start end face)
469 (when (and face (< start end))
470 (put-text-property start end 'face face)))
472 (defun nxml-clear-face (start end)
473 (remove-text-properties start end '(face nil))
474 (nxml-clear-char-ref-extra-display start end))
476 (defsubst nxml-set-fontified (start end)
477 (put-text-property start end 'fontified t))
479 (defsubst nxml-clear-fontified (start end)
480 (remove-text-properties start end '(fontified nil)))
484 ;; We use C-c C-i instead of \\[nxml-balanced-close-start-tag-inline]
485 ;; because Emacs turns C-c C-i into C-c TAB which is hard to type and
487 "Major mode for editing XML.
489 Syntax highlighting is performed unless the variable
490 `nxml-syntax-highlight-flag' is nil.
492 \\[nxml-finish-element] finishes the current element by inserting an end-tag.
493 C-c C-i closes a start-tag with `>' and then inserts a balancing end-tag
494 leaving point between the start-tag and end-tag.
495 \\[nxml-balanced-close-start-tag-block] is similar but for block rather than inline elements:
496 the start-tag, point, and end-tag are all left on separate lines.
497 If `nxml-slash-auto-complete-flag' is non-nil, then inserting a `</'
498 automatically inserts the rest of the end-tag.
500 \\[nxml-complete] performs completion on the symbol preceding point.
502 \\[nxml-dynamic-markup-word] uses the contents of the current buffer
503 to choose a tag to put around the word preceding point.
505 Sections of the document can be displayed in outline form. The
506 variable `nxml-section-element-name-regexp' controls when an element
507 is recognized as a section. The same key sequences that change
508 visibility in outline mode are used except that they start with C-c C-o
511 Validation is provided by the related minor-mode `rng-validate-mode'.
512 This also makes completion schema- and context- sensitive. Element
513 names, attribute names, attribute values and namespace URIs can all be
514 completed. By default, `rng-validate-mode' is automatically enabled by
515 `rng-nxml-mode-init' which is normally added to `nxml-mode-hook'. You
516 can toggle it using \\[rng-validate-mode].
518 \\[indent-for-tab-command] indents the current line appropriately.
519 This can be customized using the variable `nxml-child-indent'
520 and the variable `nxml-attribute-indent'.
522 \\[nxml-insert-named-char] inserts a character reference using
523 the character's name (by default, the Unicode name). \\[universal-argument] \\[nxml-insert-named-char]
524 inserts the character directly.
526 The Emacs commands that normally operate on balanced expressions will
527 operate on XML markup items. Thus \\[forward-sexp] will move forward
528 across one markup item; \\[backward-sexp] will move backward across
529 one markup item; \\[kill-sexp] will kill the following markup item;
530 \\[mark-sexp] will mark the following markup item. By default, each
531 tag each treated as a single markup item; to make the complete element
532 be treated as a single markup item, set the variable
533 `nxml-sexp-element-flag' to t. For more details, see the function
534 `nxml-forward-balanced-item'.
536 \\[nxml-backward-up-element] and \\[nxml-down-element] move up and down the element structure.
538 Many aspects this mode can be customized using
539 \\[customize-group] nxml RET."
541 (kill-all-local-variables)
542 (setq major-mode 'nxml-mode)
543 (setq mode-name "nXML")
544 ;; We'll determine the fill prefix ourselves
545 (make-local-variable 'adaptive-fill-mode)
546 (setq adaptive-fill-mode nil)
547 (make-local-variable 'forward-sexp-function)
548 (setq forward-sexp-function 'nxml-forward-balanced-item)
549 (make-local-variable 'indent-line-function)
550 (setq indent-line-function 'nxml-indent-line)
551 (make-local-variable 'fill-paragraph-function)
552 (setq fill-paragraph-function 'nxml-do-fill-paragraph)
554 ;; This doesn't seem to work too well;
555 ;; I think we should probably roll our own nxml-comment-dwim function.
556 (make-local-variable 'comment-indent-function)
557 (setq comment-indent-function 'nxml-indent-line)
558 (make-local-variable 'comment-start)
559 (setq comment-start "<!--")
560 (make-local-variable 'comment-start-skip)
561 (setq comment-start-skip "<!--[ \t\r\n]*")
562 (make-local-variable 'comment-end)
563 (setq comment-end "-->")
564 (make-local-variable 'comment-end-skip)
565 (setq comment-end-skip "[ \t\r\n]*-->")
566 (make-local-variable 'comment-line-break-function)
567 (setq comment-line-break-function 'nxml-newline-and-indent)
568 (use-local-map nxml-mode-map)
572 (nxml-clear-dependent-regions (point-min) (point-max))
573 (setq nxml-scan-end (copy-marker (point-min) nil))
574 (nxml-with-unmodifying-text-property-changes
575 (when nxml-syntax-highlight-flag
576 (nxml-clear-fontified (point-min) (point-max)))
577 (nxml-clear-inside (point-min) (point-max))
578 (nxml-with-invisible-motion
579 (nxml-scan-prolog)))))
580 (when nxml-syntax-highlight-flag
581 (add-hook 'fontification-functions 'nxml-fontify nil t))
582 (add-hook 'after-change-functions 'nxml-after-change nil t)
583 (add-hook 'write-contents-hooks 'nxml-prepare-to-save)
584 (when (not (and (buffer-file-name) (file-exists-p (buffer-file-name))))
585 (when (and nxml-default-buffer-file-coding-system
586 (not (local-variable-p 'buffer-file-coding-system)))
587 (setq buffer-file-coding-system nxml-default-buffer-file-coding-system))
588 (when nxml-auto-insert-xml-declaration-flag
589 (nxml-insert-xml-declaration)))
590 (run-hooks 'nxml-mode-hook))
592 (defun nxml-degrade (context err)
593 (message "Internal nXML mode error in %s (%s), degrading"
595 (error-message-string err))
597 (setq nxml-degraded t)
598 (setq nxml-prolog-end 1)
602 (nxml-with-unmodifying-text-property-changes
603 (nxml-clear-face (point-min) (point-max))
604 (nxml-set-fontified (point-min) (point-max))
605 (nxml-clear-inside (point-min) (point-max)))
606 (setq mode-name "nXML/degraded"))))
608 ;;; Change management
610 (defun nxml-after-change (start end pre-change-length)
611 ;; Work around bug in insert-file-contents.
612 (when (> end (1+ (buffer-size)))
614 (setq end (1+ (buffer-size))))
615 (unless nxml-degraded
621 (nxml-with-invisible-motion
622 (nxml-with-unmodifying-text-property-changes
623 (nxml-after-change1 start end pre-change-length))))))
625 (nxml-degrade 'nxml-after-change err)))))
627 (defun nxml-after-change1 (start end pre-change-length)
628 (setq nxml-last-fontify-end nil)
629 (let ((pre-change-end (+ start pre-change-length)))
631 (nxml-adjust-start-for-dependent-regions start
635 ;; Add 2 so as to include the < and following char
636 ;; that start the instance, since changing these
637 ;; can change where the prolog ends.
638 (+ nxml-prolog-end 2))
639 ;; end must be extended to at least the end of the old prolog
640 (when (< pre-change-end nxml-prolog-end)
642 ;; don't let end get out of range even if pre-change-length
645 (+ end (- nxml-prolog-end pre-change-end)))))
647 (cond ((<= end nxml-prolog-end)
648 (setq end nxml-prolog-end)
650 ;; This is so that Emacs redisplay works
651 (setq start (line-beginning-position)))
652 ((and (<= start nxml-scan-end)
653 (> start (point-min))
654 (nxml-get-inside (1- start)))
655 ;; The closing delimiter might have been removed.
656 ;; So we may need to redisplay from the beginning
658 (goto-char (1- start))
659 (nxml-move-outside-backwards)
660 ;; This is so that Emacs redisplay works
661 (setq start (line-beginning-position))
662 (setq end (max (nxml-scan-after-change (point) end)
666 ;; This is both for redisplay and to move back
667 ;; past any incomplete opening delimiters
668 (setq start (line-beginning-position))
669 (setq end (max (nxml-scan-after-change start end)
671 (when nxml-syntax-highlight-flag
673 ;; Must clear at least one char so as to trigger redisplay.
674 (cond ((< start (point-max))
675 (setq end (1+ start)))
677 (setq end (point-max))
679 (setq start (line-beginning-position)))))
680 (nxml-clear-fontified start end)))
684 (defun nxml-insert-xml-declaration ()
685 "Insert an XML declaration at the beginning of buffer.
686 The XML declaration will declare an encoding depending on the buffer's
687 `buffer-file-coding-system'."
690 (if (and buffer-file-coding-system
691 (coding-system-p buffer-file-coding-system)
692 (coding-system-get buffer-file-coding-system
694 buffer-file-coding-system
695 (nxml-choose-utf-coding-system))))
696 (goto-char (point-min))
697 (insert (format "<?xml version=\"1.0\" encoding=\"%s\"?>\n"
698 (nxml-coding-system-name coding-system)))))
700 (defun nxml-prepare-to-save ()
701 (unless (and (not enable-multibyte-characters)
702 (local-variable-p 'buffer-file-coding-system)
703 buffer-file-coding-system
704 (or (eq (coding-system-type buffer-file-coding-system) 5)
705 (eq buffer-file-coding-system 'no-conversion)))
707 (setq buffer-file-coding-system (nxml-select-coding-system))))
708 ;; nil from a function in `write-contents-hooks' means
709 ;; to continue and write the file as normal
712 (defun nxml-select-coding-system ()
713 (let* ((suitable-coding-systems
714 (find-coding-systems-region (point-min) (point-max)))
716 (goto-char (point-min))
717 (xmltok-get-declared-encoding-position)))
720 (buffer-substring-no-properties (car enc-pos)
724 (if (string= (downcase enc-name) "utf-16")
725 (nxml-choose-utf-16-coding-system)
726 (nxml-mime-charset-coding-system enc-name)))
727 (enc-pos (nxml-choose-utf-coding-system)))))
728 ;; Make sure we have a coding-system
729 (unless coding-system
731 (and (not buffer-read-only)
732 (nxml-choose-suitable-coding-system
733 suitable-coding-systems)))
736 (format "Unknown encoding %s" enc-name)
737 "XML declaration is not well-formed")))
738 (cond ((not coding-system)
739 (error "%s" message))
745 "Modify and save with encoding %s")
746 (nxml-coding-system-name coding-system))
748 (nxml-fix-encoding-declaration enc-pos coding-system))
749 (t (signal 'quit nil)))))
750 ;; Make sure it can encode all the characters in the buffer
751 (unless (or (memq (coding-system-base coding-system)
752 suitable-coding-systems)
753 (equal suitable-coding-systems '(undecided)))
755 (nxml-unsuitable-coding-system-message coding-system
758 (and (not buffer-read-only)
759 (nxml-choose-suitable-coding-system
760 suitable-coding-systems)))
761 (cond ((not coding-system) (error "%s" message))
762 ((y-or-n-p (concat message
763 (format ". Save with %s "
764 (nxml-coding-system-name
766 (nxml-fix-encoding-declaration enc-pos coding-system))
767 (t (signal 'quit nil)))))
768 ;; Merge the newline type of our existing encoding
769 (let ((current-eol-type
770 (coding-system-eol-type buffer-file-coding-system)))
771 (when (and current-eol-type (integerp current-eol-type))
773 (coding-system-change-eol-conversion coding-system
777 (defun nxml-unsuitable-coding-system-message (coding-system &optional enc-name)
778 (if (nxml-coding-system-unicode-p coding-system)
779 "Cannot translate some characters to Unicode"
780 (format "Cannot encode some characters with %s"
782 (nxml-coding-system-name coding-system)))))
784 (defconst nxml-utf-16-coding-systems (and (coding-system-p 'utf-16-be)
785 (coding-system-p 'utf-16-le)
786 '(utf-16-be utf-16-le)))
788 (defconst nxml-utf-coding-systems (cons 'utf-8 nxml-utf-16-coding-systems))
790 (defun nxml-coding-system-unicode-p (coding-system)
791 (nxml-coding-system-member (coding-system-base coding-system)
792 nxml-utf-coding-systems))
794 (defun nxml-coding-system-name (coding-system)
795 (setq coding-system (coding-system-base coding-system))
797 (if (nxml-coding-system-member coding-system nxml-utf-16-coding-systems)
799 (or (coding-system-get coding-system 'mime-charset)
802 (defun nxml-fix-encoding-declaration (enc-pos coding-system)
803 (let ((charset (nxml-coding-system-name coding-system)))
804 (cond ((consp enc-pos)
805 (delete-region (car enc-pos) (cdr enc-pos))
806 (goto-char (car enc-pos))
810 (insert " encoding=\"" charset ?\"))
812 (goto-char (point-min))
813 (insert "<?xml version=\"1.0\" encoding=\""
816 (when (and (not enc-pos)
817 (let ((case-fold-search t))
818 (looking-at xmltok-bad-xml-decl-regexp)))
819 (delete-region (point) (match-end 0)))))))
821 (defun nxml-choose-suitable-coding-system (suitable-coding-systems)
822 (let (ret coding-system)
823 (if (and buffer-file-coding-system
824 (memq (coding-system-base buffer-file-coding-system)
825 suitable-coding-systems))
826 buffer-file-coding-system
827 (while (and suitable-coding-systems (not ret))
828 (setq coding-system (car suitable-coding-systems))
829 (if (coding-system-get coding-system 'mime-charset)
830 (setq ret coding-system)
831 (setq suitable-coding-systems (cdr suitable-coding-systems))))
834 (defun nxml-choose-utf-coding-system ()
835 (let ((cur (and (local-variable-p 'buffer-file-coding-system)
836 buffer-file-coding-system
837 (coding-system-base buffer-file-coding-system))))
838 (cond ((car (nxml-coding-system-member cur nxml-utf-coding-systems)))
839 ((and nxml-prefer-utf-16-to-utf-8-flag
840 (coding-system-p 'utf-16-le)
841 (coding-system-p 'utf-16-be))
842 (if nxml-prefer-utf-16-little-to-big-endian-flag
847 (defun nxml-choose-utf-16-coding-system ()
848 (let ((cur (and (local-variable-p 'buffer-file-coding-system)
849 buffer-file-coding-system
850 (coding-system-base buffer-file-coding-system))))
851 (cond ((car (nxml-coding-system-member cur nxml-utf-16-coding-systems)))
852 (nxml-prefer-utf-16-little-to-big-endian-flag
853 (and (coding-system-p 'utf-16-le) 'utf-16-le))
854 (t (and (coding-system-p 'utf-16-be) 'utf-16-be)))))
856 (defun nxml-coding-system-member (coding-system coding-systems)
858 (while (and coding-systems (not ret))
859 (if (coding-system-equal coding-system
860 (car coding-systems))
861 (setq ret coding-systems)
862 (setq coding-systems (cdr coding-systems))))
867 (defun nxml-fontify (start)
873 (nxml-with-invisible-motion
874 (nxml-with-unmodifying-text-property-changes
875 (if (or nxml-degraded
876 ;; just in case we get called in the wrong buffer
877 (not nxml-prolog-end))
878 (nxml-set-fontified start (point-max))
879 (nxml-fontify1 start)))))))
881 (nxml-degrade 'nxml-fontify err))))
883 (defun nxml-fontify1 (start)
884 (cond ((< start nxml-prolog-end)
885 (nxml-fontify-prolog)
886 (nxml-set-fontified (point-min)
890 (when (not (eq nxml-last-fontify-end start))
891 (when (not (equal (char-after) ?\<))
892 (search-backward "<" nxml-prolog-end t))
893 (nxml-ensure-scan-up-to-date)
894 (nxml-move-outside-backwards))
895 (let ((start (point)))
896 (nxml-do-fontify (min (point-max)
897 (+ start nxml-fontify-chunk-size)))
898 (setq nxml-last-fontify-end (point))
899 (nxml-set-fontified start nxml-last-fontify-end)))))
901 (defun nxml-fontify-buffer ()
906 (nxml-with-invisible-motion
907 (goto-char (point-min))
908 (nxml-with-unmodifying-text-property-changes
909 (nxml-fontify-prolog)
910 (goto-char nxml-prolog-end)
911 (nxml-do-fontify))))))
913 (defun nxml-fontify-prolog ()
915 The buffer is assumed to be prepared for fontification.
916 This does not set the fontified property, but it does clear
917 faces appropriately."
918 (let ((regions nxml-prolog-regions))
919 (nxml-clear-face (point-min) nxml-prolog-end)
921 (let ((region (car regions)))
922 (nxml-apply-fontify-rule (aref region 0)
925 (setq regions (cdr regions)))))
927 (defun nxml-do-fontify (&optional bound)
928 "Fontify at least as far as bound.
929 Leave point after last fontified position."
930 (unless bound (setq bound (point-max)))
931 (let (xmltok-dependent-regions
933 (while (and (< (point) bound)
934 (nxml-tokenize-forward))
935 (nxml-clear-face xmltok-start (point))
936 (nxml-apply-fontify-rule))))
938 ;; Vectors identify a substring of the token to be highlighted in some face.
940 ;; Token types returned by xmltok-forward.
944 '([nil 1 nxml-tag-delimiter-face]
945 [-1 nil nxml-tag-delimiter-face]
949 (put 'partial-start-tag
951 '([nil 1 nxml-tag-delimiter-face]
957 '([nil 1 nxml-tag-delimiter-face]
958 [1 2 nxml-tag-slash-face]
959 [-1 nil nxml-tag-delimiter-face]
960 (element-qname . 2)))
962 (put 'partial-end-tag
964 '([nil 1 nxml-tag-delimiter-face]
965 [1 2 nxml-tag-slash-face]
966 (element-qname . 2)))
970 '([nil 1 nxml-tag-delimiter-face]
971 [-2 -1 nxml-tag-slash-face]
972 [-1 nil nxml-tag-delimiter-face]
976 (put 'partial-empty-element
978 '([nil 1 nxml-tag-delimiter-face]
979 [-1 nil nxml-tag-slash-face]
985 '([nil 2 nxml-char-ref-delimiter-face]
986 [2 -1 nxml-char-ref-number-face]
987 [-1 nil nxml-char-ref-delimiter-face]
992 '([nil 1 nxml-entity-ref-delimiter-face]
993 [1 -1 nxml-entity-ref-name-face]
994 [-1 nil nxml-entity-ref-delimiter-face]))
998 '([nil 4 nxml-comment-delimiter-face]
999 [4 -3 nxml-comment-content-face]
1000 [-3 nil nxml-comment-delimiter-face]))
1002 (put 'processing-instruction
1004 '([nil 2 nxml-processing-instruction-delimiter-face]
1005 [-2 nil nxml-processing-instruction-delimiter-face]
1006 processing-instruction-content))
1010 '([nil 3 nxml-cdata-section-delimiter-face] ; <![
1011 [3 8 nxml-cdata-section-CDATA-face] ; CDATA
1012 [8 9 nxml-cdata-section-delimiter-face] ; [
1013 [9 -3 nxml-cdata-section-content-face] ; ]]>
1014 [-3 nil nxml-cdata-section-delimiter-face]))
1018 '([nil nil nxml-text-face]))
1020 ;; Prolog region types in list returned by xmltok-forward-prolog.
1022 (put 'xml-declaration
1024 '([nil 2 nxml-processing-instruction-delimiter-face]
1025 [2 5 nxml-processing-instruction-target-face]
1026 [-2 nil nxml-processing-instruction-delimiter-face]))
1028 (put 'xml-declaration-attribute-name
1030 '([nil nil nxml-attribute-local-name-face]))
1032 (put 'xml-declaration-attribute-value
1034 '([nil 1 nxml-attribute-value-delimiter-face]
1035 [1 -1 nxml-attribute-value-face]
1036 [-1 nil nxml-attribute-value-delimiter-face]))
1038 (put 'processing-instruction-left
1040 '([nil 2 nxml-processing-instruction-delimiter-face]
1041 [2 nil nxml-processing-instruction-target-face]))
1043 (put 'processing-instruction-right
1045 '([nil -2 nxml-processing-instruction-content-face]
1046 [-2 nil nxml-processing-instruction-delimiter-face]))
1050 '([nil 1 nxml-prolog-literal-delimiter-face]
1051 [1 -1 nxml-prolog-literal-content-face]
1052 [-1 nil nxml-prolog-literal-delimiter-face]))
1056 '([nil nil nxml-prolog-keyword-face]))
1058 (put 'markup-declaration-open
1060 '([0 2 nxml-markup-declaration-delimiter-face]
1061 [2 nil nxml-prolog-keyword-face]))
1063 (put 'markup-declaration-close
1065 '([nil nil nxml-markup-declaration-delimiter-face]))
1067 (put 'internal-subset-open
1069 '([nil nil nxml-markup-declaration-delimiter-face]))
1071 (put 'internal-subset-close
1073 '([nil 1 nxml-markup-declaration-delimiter-face]
1074 [-1 nil nxml-markup-declaration-delimiter-face]))
1078 '([nil 1 nxml-hash-face]
1079 [1 nil nxml-prolog-keyword-face]))
1081 (defun nxml-apply-fontify-rule (&optional type start end)
1082 (let ((rule (get (or type xmltok-type) 'nxml-fontify-rule)))
1083 (unless start (setq start xmltok-start))
1084 (unless end (setq end (point)))
1086 (let* ((action (car rule)))
1087 (setq rule (cdr rule))
1088 (cond ((vectorp action)
1089 (nxml-set-face (let ((offset (aref action 0)))
1090 (cond ((not offset) start)
1091 ((< offset 0) (+ end offset))
1092 (t (+ start offset))))
1093 (let ((offset (aref action 1)))
1094 (cond ((not offset) end)
1095 ((< offset 0) (+ end offset))
1096 (t (+ start offset))))
1098 ((and (consp action)
1099 (eq (car action) 'element-qname))
1100 (when xmltok-name-end ; maybe nil in partial-end-tag case
1101 (nxml-fontify-qname (+ start (cdr action))
1104 'nxml-element-prefix-face
1105 'nxml-element-colon-face
1106 'nxml-element-local-name-face)))
1107 ((eq action 'attributes)
1108 (nxml-fontify-attributes))
1109 ((eq action 'processing-instruction-content)
1110 (nxml-set-face (+ start 2)
1112 'nxml-processing-instruction-target-face)
1113 (nxml-set-face (save-excursion
1114 (goto-char xmltok-name-end)
1115 (skip-chars-forward " \t\r\n")
1118 'nxml-processing-instruction-content-face))
1119 ((eq action 'char-ref)
1120 (nxml-char-ref-display-extra start
1122 (xmltok-char-number start end)))
1123 (t (error "Invalid nxml-fontify-rule action %s" action)))))))
1125 (defun nxml-fontify-attributes ()
1126 (while xmltok-namespace-attributes
1127 (nxml-fontify-attribute (car xmltok-namespace-attributes)
1129 (setq xmltok-namespace-attributes
1130 (cdr xmltok-namespace-attributes)))
1131 (while xmltok-attributes
1132 (nxml-fontify-attribute (car xmltok-attributes))
1133 (setq xmltok-attributes
1134 (cdr xmltok-attributes))))
1136 (defun nxml-fontify-attribute (att &optional namespace-declaration)
1137 (if namespace-declaration
1138 (nxml-fontify-qname (xmltok-attribute-name-start att)
1139 (xmltok-attribute-name-colon att)
1140 (xmltok-attribute-name-end att)
1141 'nxml-namespace-attribute-xmlns-face
1142 'nxml-namespace-attribute-colon-face
1143 'nxml-namespace-attribute-prefix-face
1144 'nxml-namespace-attribute-xmlns-face)
1145 (nxml-fontify-qname (xmltok-attribute-name-start att)
1146 (xmltok-attribute-name-colon att)
1147 (xmltok-attribute-name-end att)
1148 'nxml-attribute-prefix-face
1149 'nxml-attribute-colon-face
1150 'nxml-attribute-local-name-face))
1151 (let ((start (xmltok-attribute-value-start att))
1152 (end (xmltok-attribute-value-end att))
1153 (refs (xmltok-attribute-refs att))
1154 (delimiter-face (if namespace-declaration
1155 'nxml-namespace-attribute-value-delimiter-face
1156 'nxml-attribute-value-delimiter-face))
1157 (value-face (if namespace-declaration
1158 'nxml-namespace-attribute-value-face
1159 'nxml-attribute-value-face)))
1161 (nxml-set-face (1- start) start delimiter-face)
1162 (nxml-set-face end (1+ end) delimiter-face)
1164 (let* ((ref (car refs))
1165 (ref-type (aref ref 0))
1166 (ref-start (aref ref 1))
1167 (ref-end (aref ref 2)))
1168 (nxml-set-face start ref-start value-face)
1169 (nxml-apply-fontify-rule ref-type ref-start ref-end)
1170 (setq start ref-end))
1171 (setq refs (cdr refs)))
1172 (nxml-set-face start end value-face))))
1174 (defun nxml-fontify-qname (start
1182 (cond (colon (nxml-set-face start colon prefix-face)
1183 (nxml-set-face colon (1+ colon) colon-face)
1184 (nxml-set-face (1+ colon) end local-name-face))
1185 (t (nxml-set-face start end (or unprefixed-face
1186 local-name-face)))))
1190 (defun nxml-electric-slash (arg)
1193 With a prefix ARG, do nothing other than insert the slash.
1195 Otherwise, if `nxml-slash-auto-complete-flag' is non-nil, insert the
1196 rest of the end-tag or empty-element if the slash is potentially part
1197 of an end-tag or the close of an empty-element.
1199 If the slash is part of an end-tag that is the first non-whitespace
1200 on the line, reindent the line."
1202 (nxml-ensure-scan-up-to-date)
1203 (let* ((slash-pos (point))
1204 (end-tag-p (and (eq (char-before slash-pos) ?<)
1205 (not (nxml-get-inside slash-pos))))
1206 (at-indentation (save-excursion
1207 (back-to-indentation)
1208 (eq (point) (1- slash-pos)))))
1209 (self-insert-command (prefix-numeric-value arg))
1211 (if nxml-slash-auto-complete-flag
1214 (let ((start-tag-end
1215 (nxml-scan-element-backward (1- slash-pos) t)))
1217 (insert (xmltok-start-tag-qname) ">")
1218 ;; copy the indentation of the start-tag
1219 (when (and at-indentation
1221 (goto-char xmltok-start)
1222 (back-to-indentation)
1223 (eq (point) xmltok-start)))
1225 (indent-line-to (save-excursion
1226 (goto-char xmltok-start)
1227 (current-column)))))))
1228 (nxml-scan-error nil))
1229 (when (and (eq (nxml-token-before) (point))
1230 (eq xmltok-type 'partial-empty-element))
1232 (when (and end-tag-p at-indentation)
1233 (nxml-indent-line))))))
1235 (defun nxml-balanced-close-start-tag-block ()
1236 "Close the start-tag before point with `>' and insert a balancing end-tag.
1237 Point is left between the start-tag and the end-tag.
1238 If there is nothing but whitespace before the `<' that opens the
1239 start-tag, then put point on a blank line, and put the end-tag on
1240 another line aligned with the start-tag."
1242 (nxml-balanced-close-start-tag 'block))
1244 (defun nxml-balanced-close-start-tag-inline ()
1245 "Close the start-tag before point with `>' and insert a balancing end-tag.
1246 Point is left between the start-tag and the end-tag.
1247 No extra whitespace is inserted."
1249 (nxml-balanced-close-start-tag 'inline))
1251 (defun nxml-balanced-close-start-tag (block-or-inline)
1252 (let ((token-end (nxml-token-before))
1254 (unless (or (eq xmltok-type 'partial-start-tag)
1255 (and (memq xmltok-type '(start-tag
1257 partial-empty-element))
1258 (>= token-end pos)))
1259 (error "Not in a start-tag"))
1261 (buffer-substring-no-properties (+ xmltok-start 1)
1262 (min xmltok-name-end (point)))
1264 (if (eq block-or-inline 'inline)
1266 (goto-char xmltok-start)
1267 (back-to-indentation)
1268 (if (= (point) xmltok-start)
1269 (let ((indent (current-column)))
1272 (indent-line-to indent)
1275 (indent-line-to (+ nxml-child-indent indent)))
1278 (defun nxml-finish-element ()
1279 "Finish the current element by inserting an end-tag."
1281 (nxml-finish-element-1 nil))
1283 (defvar nxml-last-split-position nil
1284 "Position where `nxml-split-element' split the current element.")
1286 (defun nxml-split-element ()
1287 "Split the current element by inserting an end-tag and a start-tag.
1288 Point is left after the newly inserted start-tag. When repeated,
1289 split immediately before the previously inserted start-tag and leave
1292 (setq nxml-last-split-position
1293 (if (and (eq last-command this-command)
1294 nxml-last-split-position)
1296 (goto-char nxml-last-split-position)
1297 (nxml-finish-element-1 t))
1298 (nxml-finish-element-1 t))))
1300 (defun nxml-finish-element-1 (startp)
1301 "Insert an end-tag for the current element and optionally a start-tag.
1302 The start-tag is inserted if STARTP is non-nil. Return the position
1303 of the inserted start-tag or nil if none was inserted."
1305 (let* ((token-end (nxml-token-before))
1308 (when (and (< (point) token-end)
1311 processing-instruction
1316 (error "Point is inside a %s"
1317 (nxml-token-type-friendly-name xmltok-type)))
1318 (nxml-scan-element-backward token-end t)))
1321 (unless (eq xmltok-type 'start-tag)
1322 (error "No matching start-tag"))
1323 (goto-char xmltok-start)
1324 (back-to-indentation)
1325 (eq (point) xmltok-start)))
1328 (goto-char start-tag-end)
1329 (looking-at "[ \t\r\n]*$")))
1330 (start-tag-indent (save-excursion
1331 (goto-char xmltok-start)
1333 (qname (xmltok-start-tag-qname))
1334 inserted-start-tag-pos)
1335 (when (and starts-line ends-line)
1336 ;; start-tag is on a line by itself
1337 ;; => put the end-tag on a line by itself
1340 (back-to-indentation)
1343 (indent-line-to start-tag-indent))
1344 (insert "</" qname ">")
1348 (indent-line-to start-tag-indent))
1349 (setq inserted-start-tag-pos (point))
1350 (insert "<" qname ">")
1351 (when (and starts-line ends-line)
1353 (indent-line-to (save-excursion
1354 (goto-char xmltok-start)
1356 (back-to-indentation)
1357 (if (= (current-column)
1358 (+ start-tag-indent nxml-child-indent))
1359 (+ start-tag-indent nxml-child-indent)
1360 start-tag-indent)))))
1361 inserted-start-tag-pos))
1365 (defun nxml-indent-line ()
1366 "Indent current line as XML."
1367 (let ((indent (nxml-compute-indent))
1368 (from-end (- (point-max) (point))))
1371 (let ((bol (point)))
1372 (skip-chars-forward " \t")
1373 (delete-region bol (point)))
1375 (when (> (- (point-max) from-end) (point))
1376 (goto-char (- (point-max) from-end))))))
1378 (defun nxml-compute-indent ()
1379 "Return the indent for the line containing point."
1380 (or (nxml-compute-indent-from-matching-start-tag)
1381 (nxml-compute-indent-from-previous-line)))
1383 (defun nxml-compute-indent-from-matching-start-tag ()
1384 "Compute the indent for a line with an end-tag using the matching start-tag.
1385 When the line containing point ends with an end-tag and does not start
1386 in the middle of a token, return the indent of the line containing the
1387 matching start-tag, if there is one and it occurs at the beginning of
1388 its line. Otherwise return nil."
1390 (back-to-indentation)
1391 (let ((bol (point)))
1392 (let ((inhibit-field-text-motion t))
1394 (skip-chars-backward " \t")
1395 (and (= (nxml-token-before) (point))
1396 (memq xmltok-type '(end-tag partial-end-tag))
1397 ;; start of line must not be inside a token
1398 (or (= xmltok-start bol)
1402 (= xmltok-start bol))
1403 (eq xmltok-type 'data))
1405 (nxml-scan-element-backward
1409 nxml-end-tag-indent-scan-distance))
1410 (nxml-scan-error nil))
1411 (< xmltok-start bol)
1413 (goto-char xmltok-start)
1414 (skip-chars-backward " \t")
1416 (current-indentation)))))
1418 (defun nxml-compute-indent-from-previous-line ()
1419 "Compute the indent for a line using the indentation of a previous line."
1424 before-context after-context)
1425 (back-to-indentation)
1428 ;; Move backwards until the start of a non-blank line that is
1429 ;; not inside a token.
1431 (when (= (forward-line -1) -1)
1433 (back-to-indentation)
1434 (if (looking-at "[ \t]*$")
1437 (setq prev-bol (point)))
1439 (not (or (= xmltok-start (point))
1440 (eq xmltok-type 'data))))))
1442 ;; Now scan over tokens until the end of the line to be indented.
1443 ;; Determine the context before and after the beginning of the
1445 (while (< (point) eol)
1446 (nxml-tokenize-forward)
1447 (cond ((<= bol xmltok-start)
1449 (nxml-merge-indent-context-type after-context)))
1450 ((and (<= (point) bol)
1451 (not (and (eq xmltok-type 'partial-start-tag)
1453 (setq before-context
1454 (nxml-merge-indent-context-type before-context)))
1455 ((eq xmltok-type 'data)
1456 (setq before-context
1457 (nxml-merge-indent-context-type before-context))
1459 (nxml-merge-indent-context-type after-context)))
1460 ;; If in the middle of a token that looks inline,
1461 ;; then indent relative to the previous non-blank line
1462 ((eq (nxml-merge-indent-context-type before-context)
1464 (goto-char prev-bol)
1465 (throw 'indent (current-column)))
1468 (nxml-compute-indent-in-token bol))))
1469 (skip-chars-forward " \t\r\n"))
1472 (* nxml-child-indent
1473 (+ (if (eq before-context 'start-tag) 1 0)
1474 (if (eq after-context 'end-tag) -1 0))))))))
1476 (defun nxml-merge-indent-context-type (context)
1477 "Merge the indent context type CONTEXT with the token in `xmltok-type'.
1478 Return the merged indent context type. An indent context type is
1479 either nil or one of the symbols start-tag, end-tag, markup, comment,
1481 (cond ((memq xmltok-type '(start-tag partial-start-tag))
1482 (if (memq context '(nil start-tag comment))
1485 ((memq xmltok-type '(end-tag partial-end-tag))
1486 (if (memq context '(nil end-tag comment))
1489 ((eq xmltok-type 'comment)
1490 (cond ((memq context '(start-tag end-tag comment))
1497 (defun nxml-compute-indent-in-token (pos)
1498 "Return the indent for a line that starts inside a token.
1499 POS is the position of the first non-whitespace character of the line.
1500 This expects the xmltok-* variables to be set up as by `xmltok-forward'."
1501 (cond ((memq xmltok-type '(start-tag
1504 partial-empty-element))
1505 (nxml-compute-indent-in-start-tag pos))
1506 ((eq xmltok-type 'comment)
1507 (nxml-compute-indent-in-delimited-token pos "<!--" "-->"))
1508 ((eq xmltok-type 'cdata-section)
1509 (nxml-compute-indent-in-delimited-token pos "<![CDATA[" "]]>"))
1510 ((eq xmltok-type 'processing-instruction)
1511 (nxml-compute-indent-in-delimited-token pos "<?" "?>"))
1514 (if (and (= (forward-line -1) 0)
1515 (< xmltok-start (point)))
1516 (back-to-indentation)
1517 (goto-char xmltok-start))
1520 (defun nxml-compute-indent-in-start-tag (pos)
1521 "Return the indent for a line that starts inside a start-tag.
1522 Also for a line that starts inside an empty element.
1523 POS is the position of the first non-whitespace character of the line.
1524 This expects the xmltok-* variables to be set up as by `xmltok-forward'."
1525 (let ((value-boundary (nxml-attribute-value-boundary pos))
1528 ;; inside an attribute value
1529 (let ((value-start (car value-boundary))
1530 (value-end (cdr value-boundary)))
1533 (if (< (point) value-start)
1534 (goto-char value-start)
1535 (back-to-indentation)))
1536 ;; outside an attribute value
1538 (while (and (= (forward-line -1) 0)
1539 (nxml-attribute-value-boundary (point))))
1540 (cond ((<= (point) xmltok-start)
1541 (goto-char xmltok-start)
1542 (setq off nxml-attribute-indent)
1543 (let ((atts (xmltok-merge-attributes)))
1545 (let* ((att (car atts))
1546 (start (xmltok-attribute-name-start att)))
1551 (back-to-indentation))))
1552 (+ (current-column) off)))
1554 (defun nxml-attribute-value-boundary (pos)
1555 "Return a pair (START . END) if POS is inside an attribute value.
1556 Otherwise return nil. START and END are the positions of the start
1557 and end of the attribute value containing POS. This expects the
1558 xmltok-* variables to be set up as by `xmltok-forward'."
1559 (let ((atts (xmltok-merge-attributes))
1560 att value-start value-end value-boundary)
1562 (setq att (car atts))
1563 (setq value-start (xmltok-attribute-value-start att))
1564 (setq value-end (xmltok-attribute-value-end att))
1565 (cond ((and value-start (< pos value-start))
1567 ((and value-start value-end (<= pos value-end))
1568 (setq value-boundary (cons value-start value-end))
1570 (t (setq atts (cdr atts)))))
1573 (defun nxml-compute-indent-in-delimited-token (pos open-delim close-delim)
1574 "Return the indent for a line that starts inside a token with delimiters.
1575 OPEN-DELIM and CLOSE-DELIM are strings giving the opening and closing
1576 delimiters. POS is the position of the first non-whitespace character
1577 of the line. This expects the xmltok-* variables to be set up as by
1579 (cond ((let ((end (+ pos (length close-delim))))
1580 (and (<= end (point-max))
1581 (string= (buffer-substring-no-properties pos end)
1583 (goto-char xmltok-start))
1587 (<= (point) xmltok-start))
1588 (goto-char (+ xmltok-start (length open-delim)))
1589 (when (and (string= open-delim "<!--")
1591 (goto-char (1+ (point)))))
1592 (t (back-to-indentation)))
1597 (defun nxml-complete ()
1598 "Perform completion on the symbol preceding point.
1600 Inserts as many characters as can be completed. However, if not even
1601 one character can be completed, then a buffer with the possibilities
1602 is popped up and the symbol is read from the minibuffer with
1603 completion. If the symbol is complete, then any characters that must
1604 follow the symbol are also inserted.
1606 The name space used for completion and what is treated as a symbol
1607 depends on the context. The contexts in which completion is performed
1608 depend on `nxml-completion-hook'."
1610 (unless (run-hook-with-args-until-success 'nxml-completion-hook)
1611 ;; Eventually we will complete on entity names here.
1613 (message "Cannot complete in this context")))
1617 (defun nxml-forward-balanced-item (&optional arg)
1618 "Move forward across one balanced item.
1619 With ARG, do it that many times. Negative arg -N means
1620 move backward across N balanced expressions.
1621 This is the equivalent of `forward-sexp' for XML.
1623 An element contains as items strings with no markup, tags, processing
1624 instructions, comments, CDATA sections, entity references and
1625 characters references. However, if the variable
1626 `nxml-sexp-element-flag' is non-nil, then an element is treated as a
1627 single markup item. A start-tag contains an element name followed by
1628 one or more attributes. An end-tag contains just an element name. An
1629 attribute value literals contains strings with no markup, entity
1630 references and character references. A processing instruction
1631 consists of a target and a content string. A comment or a CDATA
1632 section contains a single string. An entity reference contains a
1633 single name. A character reference contains a character number."
1635 (or arg (setq arg 1))
1638 (nxml-forward-single-balanced-item)
1639 (> (setq arg (1- arg)) 0))))
1642 (nxml-backward-single-balanced-item)
1643 (< (setq arg (1+ arg)) 0))))))
1645 (defun nxml-forward-single-balanced-item ()
1647 (goto-char (let ((end (nxml-token-after)))
1649 (while (eq xmltok-type 'space)
1651 (setq end (nxml-token-after)))
1652 (cond ((/= (point) xmltok-start)
1653 (nxml-scan-forward-within end))
1654 ((and nxml-sexp-element-flag
1655 (eq xmltok-type 'start-tag))
1656 ;; can't ever return nil here
1657 (nxml-scan-element-forward xmltok-start))
1658 ((and nxml-sexp-element-flag
1660 '(end-tag partial-end-tag)))
1661 (error "Already at end of element"))
1664 (goto-char (cadr err))
1665 (apply 'error (cddr err)))))
1667 (defun nxml-backward-single-balanced-item ()
1669 (goto-char (let ((end (nxml-token-before)))
1671 (while (eq xmltok-type 'space)
1672 (goto-char xmltok-start)
1673 (setq end (nxml-token-before)))
1674 (cond ((/= (point) end)
1675 (nxml-scan-backward-within end))
1676 ((and nxml-sexp-element-flag
1677 (eq xmltok-type 'end-tag))
1678 ;; can't ever return nil here
1679 (nxml-scan-element-backward end)
1681 ((and nxml-sexp-element-flag
1682 (eq xmltok-type 'start-tag))
1683 (error "Already at start of element"))
1684 (t xmltok-start)))))
1686 (goto-char (cadr err))
1687 (apply 'error (cddr err)))))
1689 (defun nxml-scan-forward-within (end)
1690 (setq end (- end (nxml-end-delimiter-length xmltok-type)))
1691 (when (<= end (point))
1692 (error "Already at end of %s"
1693 (nxml-token-type-friendly-name xmltok-type)))
1694 (cond ((memq xmltok-type '(start-tag
1697 partial-empty-element))
1698 (if (< (point) xmltok-name-end)
1700 (let ((att (nxml-find-following-attribute)))
1701 (cond ((not att) end)
1702 ((and (xmltok-attribute-value-start att)
1703 (<= (xmltok-attribute-value-start att)
1705 (nxml-scan-forward-in-attribute-value att))
1706 ((xmltok-attribute-value-end att)
1707 (1+ (xmltok-attribute-value-end att)))
1709 (goto-char (xmltok-attribute-name-end att))
1710 (looking-at "[ \t\r\n]*="))
1712 (t (xmltok-attribute-name-end att))))))
1713 ((and (eq xmltok-type 'processing-instruction)
1714 (< (point) xmltok-name-end))
1718 (defun nxml-scan-backward-within (end)
1721 (nxml-start-delimiter-length xmltok-type)))
1722 (when (<= (point) xmltok-start)
1723 (error "Already at start of %s"
1724 (nxml-token-type-friendly-name xmltok-type)))
1725 (cond ((memq xmltok-type '(start-tag
1728 partial-empty-element))
1729 (let ((att (nxml-find-preceding-attribute)))
1730 (cond ((not att) xmltok-start)
1731 ((and (xmltok-attribute-value-start att)
1732 (<= (xmltok-attribute-value-start att)
1735 (xmltok-attribute-value-end att)))
1736 (nxml-scan-backward-in-attribute-value att))
1737 (t (xmltok-attribute-name-start att)))))
1738 ((and (eq xmltok-type 'processing-instruction)
1739 (let ((content-start (save-excursion
1740 (goto-char xmltok-name-end)
1741 (skip-chars-forward " \r\t\n")
1743 (and (< content-start (point))
1747 (defun nxml-scan-forward-in-attribute-value (att)
1748 (when (= (point) (xmltok-attribute-value-end att))
1749 (error "Already at end of attribute value"))
1750 (let ((refs (xmltok-attribute-refs att))
1753 (setq ref (car refs))
1754 (if (< (point) (aref ref 2))
1757 (setq refs (cdr refs))))
1759 (xmltok-attribute-value-end att))
1760 ((< (point) (aref ref 1))
1762 ((= (point) (aref ref 1))
1765 (let ((end (- (aref ref 2)
1766 (nxml-end-delimiter-length (aref ref 0)))))
1769 (error "Already at end of %s"
1770 (nxml-token-type-friendly-name (aref ref 0)))))))))
1772 (defun nxml-scan-backward-in-attribute-value (att)
1773 (when (= (point) (xmltok-attribute-value-start att))
1774 (error "Already at start of attribute value"))
1775 (let ((refs (reverse (xmltok-attribute-refs att)))
1778 (setq ref (car refs))
1779 (if (< (aref ref 1) (point))
1782 (setq refs (cdr refs))))
1784 (xmltok-attribute-value-start att))
1785 ((< (aref ref 2) (point))
1787 ((= (point) (aref ref 2))
1790 (let ((start (+ (aref ref 1)
1791 (nxml-start-delimiter-length (aref ref 0)))))
1792 (if (< start (point))
1794 (error "Already at start of %s"
1795 (nxml-token-type-friendly-name (aref ref 0)))))))))
1797 (defun nxml-find-following-attribute ()
1799 (atts (or xmltok-attributes xmltok-namespace-attributes))
1800 (more-atts (and xmltok-attributes xmltok-namespace-attributes)))
1802 (let* ((att (car atts))
1803 (name-start (xmltok-attribute-name-start att)))
1804 (cond ((and (<= name-start (point))
1805 (xmltok-attribute-value-end att)
1806 ;; <= because end is before quote
1807 (<= (point) (xmltok-attribute-value-end att)))
1810 ((and (< (point) name-start)
1813 (xmltok-attribute-name-start ret))))
1815 (setq atts (cdr atts))
1817 (setq atts more-atts)
1818 (setq more-atts nil)))
1821 (defun nxml-find-preceding-attribute ()
1823 (atts (or xmltok-attributes xmltok-namespace-attributes))
1824 (more-atts (and xmltok-attributes xmltok-namespace-attributes)))
1826 (let* ((att (car atts))
1827 (name-start (xmltok-attribute-name-start att)))
1828 (cond ((and (< name-start (point))
1829 (xmltok-attribute-value-end att)
1830 ;; <= because end is before quote
1831 (<= (point) (xmltok-attribute-value-end att)))
1834 ((and (< name-start (point))
1836 (< (xmltok-attribute-name-start ret)
1839 (setq atts (cdr atts))
1841 (setq atts more-atts)
1842 (setq more-atts nil)))
1845 (defun nxml-up-element (&optional arg)
1847 (or arg (setq arg 1))
1849 (nxml-backward-up-element (- arg))
1851 (while (and (> arg 0)
1852 (< (point) (point-max)))
1853 (let ((token-end (nxml-token-after)))
1854 (goto-char (cond ((or (memq xmltok-type '(end-tag
1856 (and (memq xmltok-type
1858 partial-empty-element))
1859 (< xmltok-start (point))))
1861 ((nxml-scan-element-forward
1862 (if (and (eq xmltok-type 'start-tag)
1863 (= (point) xmltok-start))
1867 (t (error "No parent element")))))
1868 (setq arg (1- arg)))
1870 (goto-char (cadr err))
1871 (apply 'error (cddr err))))))
1873 (defun nxml-backward-up-element (&optional arg)
1875 (or arg (setq arg 1))
1877 (nxml-up-element (- arg))
1879 (while (and (> arg 0)
1880 (< (point-min) (point)))
1881 (let ((token-end (nxml-token-before)))
1882 (goto-char (cond ((or (memq xmltok-type '(start-tag
1884 (and (memq xmltok-type
1886 partial-empty-element))
1887 (< (point) token-end)))
1889 ((nxml-scan-element-backward
1890 (if (and (eq xmltok-type 'end-tag)
1891 (= (point) token-end))
1896 (t (error "No parent element")))))
1897 (setq arg (1- arg)))
1899 (goto-char (cadr err))
1900 (apply 'error (cddr err))))))
1902 (defun nxml-down-element (&optional arg)
1903 "Move forward down into the content of an element.
1904 With ARG, do this that many times.
1905 Negative ARG means move backward but still down."
1907 (or arg (setq arg 1))
1909 (nxml-backward-down-element (- arg))
1912 (let ((token-end (nxml-token-after)))
1914 (goto-char token-end)
1916 (when (memq xmltok-type '(nil end-tag partial-end-tag))
1917 (error "No following start-tags in this element"))
1918 (not (memq xmltok-type '(start-tag partial-start-tag))))
1919 (nxml-tokenize-forward))
1921 (setq arg (1- arg)))))
1923 (defun nxml-backward-down-element (&optional arg)
1925 (or arg (setq arg 1))
1927 (nxml-down-element (- arg))
1932 (goto-char xmltok-start)
1934 (when (memq xmltok-type '(start-tag
1938 (error "No preceding end-tags in this element"))
1939 (not (memq xmltok-type '(end-tag partial-end-tag))))
1940 (if (or (<= (point) nxml-prolog-end)
1941 (not (search-backward "<" nxml-prolog-end t)))
1942 (setq xmltok-type nil)
1943 (nxml-move-outside-backwards)
1946 (setq arg (1- arg)))))
1948 (defun nxml-forward-element (&optional arg)
1949 "Move forward over one element.
1950 With ARG, do it that many times.
1951 Negative ARG means move backward."
1953 (or arg (setq arg 1))
1955 (nxml-backward-element (- arg))
1957 (while (and (> arg 0)
1958 (< (point) (point-max)))
1960 (or (nxml-scan-element-forward (nxml-token-before))
1961 (error "No more elements")))
1962 (setq arg (1- arg)))
1964 (goto-char (cadr err))
1965 (apply 'error (cddr err))))))
1967 (defun nxml-backward-element (&optional arg)
1968 "Move backward over one element.
1969 With ARG, do it that many times.
1970 Negative ARG means move forward."
1972 (or arg (setq arg 1))
1974 (nxml-forward-element (- arg))
1976 (while (and (> arg 0)
1977 (< (point-min) (point)))
1979 (or (and (nxml-scan-element-backward (progn
1983 (error "No preceding elements")))
1984 (setq arg (1- arg)))
1986 (goto-char (cadr err))
1987 (apply 'error (cddr err))))))
1989 (defun nxml-mark-token-after ()
1991 (push-mark (nxml-token-after) nil t)
1992 (goto-char xmltok-start)
1993 (message "Marked %s" xmltok-type))
1997 (defun nxml-mark-paragraph ()
1998 "Put point at beginning of this paragraph, mark at end.
1999 The paragraph marked is the one that contains point or follows point."
2001 (nxml-forward-paragraph)
2003 (nxml-backward-paragraph))
2005 (defun nxml-forward-paragraph (&optional arg)
2007 (or arg (setq arg 1))
2009 (nxml-backward-paragraph (- arg)))
2012 (while (and (nxml-forward-single-paragraph)
2013 (> (setq arg (1- arg)) 0))))))
2015 (defun nxml-backward-paragraph (&optional arg)
2017 (or arg (setq arg 1))
2019 (nxml-forward-paragraph (- arg)))
2022 (let ((inhibit-field-text-motion t))
2024 (while (and (nxml-backward-single-paragraph)
2025 (> (setq arg (1- arg)) 0))))))
2027 (defun nxml-forward-single-paragraph ()
2028 "Move forward over a single paragraph.
2029 Return nil at end of buffer, t otherwise."
2030 (let* ((token-end (nxml-token-after))
2031 (offset (- (point) xmltok-start))
2033 (goto-char token-end)
2034 (while (and (< (point) (point-max))
2036 (nxml-paragraph-end-pos had-data offset))))
2037 (when (nxml-token-contains-data-p offset)
2039 (nxml-tokenize-forward)
2041 (when pos (goto-char pos))))
2043 (defun nxml-backward-single-paragraph ()
2044 "Move backward over a single paragraph.
2045 Return nil at start of buffer, t otherwise."
2046 (let* ((token-end (nxml-token-before))
2047 (offset (- token-end (point)))
2048 (last-tag-pos xmltok-start)
2049 pos had-data last-data-pos)
2050 (goto-char token-end)
2051 (unless (setq pos (nxml-paragraph-start-pos nil offset))
2052 (setq had-data (nxml-token-contains-data-p nil offset))
2053 (goto-char xmltok-start)
2054 (while (and (not pos) (< (point-min) (point)))
2055 (cond ((search-backward "<" nxml-prolog-end t)
2056 (nxml-move-outside-backwards)
2058 (while (< (point) last-tag-pos)
2060 (when (and (not had-data) (nxml-token-contains-data-p))
2062 (setq last-data-pos xmltok-start))
2063 (let ((tem (nxml-paragraph-start-pos had-data 0)))
2064 (when tem (setq pos tem)))))
2065 (when (and (not had-data) last-data-pos (not pos))
2068 (while (< (point) last-data-pos)
2070 (let ((tem (nxml-paragraph-start-pos had-data 0)))
2071 (when tem (setq pos tem)))))
2072 (setq last-tag-pos (point)))
2073 (t (goto-char (point-min))))))
2074 (when pos (goto-char pos))))
2076 (defun nxml-token-contains-data-p (&optional start end)
2077 (setq start (+ xmltok-start (or start 0)))
2078 (setq end (- (point) (or end 0)))
2079 (when (eq xmltok-type 'cdata-section)
2080 (setq start (max start (+ xmltok-start 9)))
2081 (setq end (min end (- (point) 3))))
2082 (or (and (eq xmltok-type 'data)
2083 (eq start xmltok-start)
2085 (eq xmltok-type 'char-ref)
2086 (and (memq xmltok-type '(data cdata-section))
2090 (re-search-forward "[^ \t\r\n]" end t)))))
2092 (defun nxml-paragraph-end-pos (had-data offset)
2093 "Return the position of the paragraph end if contained in the current token.
2094 Return nil if the current token does not contain the paragraph end.
2095 Only characters after OFFSET from the start of the token are eligible.
2096 HAD-DATA says whether there have been non-whitespace data characters yet."
2097 (cond ((not had-data)
2098 (cond ((memq xmltok-type '(data cdata-section))
2100 (let ((end (point)))
2101 (goto-char (+ xmltok-start
2102 (max (if (eq xmltok-type 'cdata-section)
2106 (and (re-search-forward "[^ \t\r\n]" end t)
2107 (re-search-forward "^[ \t]*$" end t)
2108 (match-beginning 0)))))
2109 ((and (eq xmltok-type 'comment)
2110 (nxml-token-begins-line-p)
2111 (nxml-token-ends-line-p))
2113 (let ((end (point)))
2114 (goto-char (+ xmltok-start (max 4 offset)))
2115 (when (re-search-forward "[^ \t\r\n]" (- end 3) t)
2116 (if (re-search-forward "^[ \t]*$" end t)
2118 (goto-char (- end 3))
2119 (skip-chars-backward " \t")
2121 (beginning-of-line 2))
2123 ((memq xmltok-type '(data space cdata-section))
2125 (let ((end (point)))
2126 (goto-char (+ xmltok-start offset))
2127 (and (re-search-forward "^[ \t]*$" end t)
2128 (match-beginning 0)))))
2129 ((and (memq xmltok-type '(start-tag
2133 processing-instruction
2135 (nxml-token-begins-line-p)
2136 (nxml-token-ends-line-p))
2138 (goto-char xmltok-start)
2139 (skip-chars-backward " \t")
2141 ((and (eq xmltok-type 'end-tag)
2142 (looking-at "[ \t]*$")
2143 (not (nxml-in-mixed-content-p t)))
2145 (or (search-forward "\n" nil t)
2148 (defun nxml-paragraph-start-pos (had-data offset)
2149 "Return the position of the paragraph start if contained in the current token.
2150 Return nil if the current token does not contain the paragraph start.
2151 Only characters before OFFSET from the end of the token are eligible.
2152 HAD-DATA says whether there have been non-whitespace data characters yet."
2153 (cond ((not had-data)
2154 (cond ((memq xmltok-type '(data cdata-section))
2156 (goto-char (- (point)
2157 (max (if (eq xmltok-type 'cdata-section)
2161 (and (re-search-backward "[^ \t\r\n]" xmltok-start t)
2162 (re-search-backward "^[ \t]*$" xmltok-start t)
2163 (match-beginning 0))))
2164 ((and (eq xmltok-type 'comment)
2165 (nxml-token-ends-line-p)
2166 (nxml-token-begins-line-p))
2168 (goto-char (- (point) (max 3 offset)))
2169 (when (and (< (+ xmltok-start 4) (point))
2170 (re-search-backward "[^ \t\r\n]"
2173 (if (re-search-backward "^[ \t]*$" xmltok-start t)
2175 (goto-char xmltok-start)
2176 (if (looking-at "<!--[ \t]*\n")
2178 (skip-chars-backward " \t")
2180 ((memq xmltok-type '(data space cdata-section))
2182 (goto-char (- (point) offset))
2183 (and (re-search-backward "^[ \t]*$" xmltok-start t)
2184 (match-beginning 0))))
2185 ((and (memq xmltok-type '(start-tag
2189 processing-instruction
2191 (nxml-token-ends-line-p)
2192 (nxml-token-begins-line-p))
2193 (or (search-forward "\n" nil t)
2195 ((and (eq xmltok-type 'start-tag)
2196 (nxml-token-begins-line-p)
2197 (not (save-excursion
2198 (goto-char xmltok-start)
2199 (nxml-in-mixed-content-p nil))))
2201 (goto-char xmltok-start)
2202 (skip-chars-backward " \t")
2203 ;; include any blank line before
2204 (or (and (eq (char-before) ?\n)
2206 (goto-char (1- (point)))
2207 (skip-chars-backward " \t")
2208 (and (bolp) (point))))
2211 (defun nxml-token-ends-line-p () (looking-at "[ \t]*$"))
2213 (defun nxml-token-begins-line-p ()
2215 (goto-char xmltok-start)
2216 (skip-chars-backward " \t")
2219 (defun nxml-in-mixed-content-p (endp)
2220 "Return non-nil if point is in mixed content.
2221 Point must be after an end-tag or before a start-tag.
2222 ENDP is t in the former case, nil in the latter."
2223 (let (matching-tag-pos)
2224 (cond ((not (run-hook-with-args-until-failure
2225 'nxml-in-mixed-content-hook))
2227 ;; See if the matching tag does not start or end a line.
2228 ((condition-case err
2230 (setq matching-tag-pos
2233 (and (nxml-scan-element-backward (point))
2235 (nxml-scan-element-forward (point)))))
2236 (and matching-tag-pos
2238 (goto-char matching-tag-pos)
2241 (skip-chars-backward " \t")
2243 (looking-at "[ \t]*$"))))))
2244 (nxml-scan-error nil))
2246 ;; See if there's data at the same level.
2249 (setq start matching-tag-pos
2252 end matching-tag-pos))
2256 (nxml-preceding-sibling-data-p))
2259 (nxml-following-sibling-data-p)))))
2261 ;; Otherwise, treat as not mixed
2264 (defun nxml-preceding-sibling-data-p ()
2265 "Return non-nil if there is a previous sibling that is data."
2266 (let ((lim (max (- (point) nxml-mixed-scan-distance)
2272 (while (and (< lim (point))
2277 (search-backward "<" lim t)))
2278 (nxml-move-outside-backwards)
2281 (let ((prev-level level))
2282 (cond ((eq xmltok-type 'end-tag)
2283 (setq level (1+ level)))
2284 ((eq xmltok-type 'start-tag)
2285 (setq level (1- level))))
2286 (when (eq prev-level 0)
2287 (while (and (< (point) end) (not found))
2289 (when (memq xmltok-type '(data cdata-section char-ref))
2290 (setq found t)))))))))
2293 (defun nxml-following-sibling-data-p ()
2294 (let ((lim (min (+ (point) nxml-mixed-scan-distance)
2300 (while (and (< (point) lim)
2302 (nxml-tokenize-forward)
2304 (cond ((eq xmltok-type 'start-tag)
2305 (setq level (1+ level)))
2306 ((eq xmltok-type 'end-tag)
2307 (setq level (1- level)))
2309 (memq xmltok-type '(data cdata-section char-ref)))
2315 (defun nxml-do-fill-paragraph (arg)
2316 (let (fill-paragraph-function
2320 (nxml-forward-paragraph)
2322 (nxml-backward-paragraph)
2323 (skip-chars-forward " \t\r\n")
2324 (setq start (point))
2326 (setq fill-prefix (buffer-substring-no-properties (point) start))
2327 (when (and (not (nxml-get-inside (point)))
2328 (looking-at "[ \t]*<!--"))
2329 (setq fill-prefix (concat fill-prefix " ")))
2330 (fill-region-as-paragraph start end arg))
2331 (skip-line-prefix fill-prefix)
2334 (defun nxml-newline-and-indent (soft)
2335 (delete-horizontal-space)
2336 (if soft (insert-and-inherit ?\n) (newline 1))
2342 (defvar nxml-dynamic-markup-prev-pos nil)
2343 (defvar nxml-dynamic-markup-prev-lengths nil)
2344 (defvar nxml-dynamic-markup-prev-found-marker nil)
2345 (defvar nxml-dynamic-markup-prev-start-tags (make-hash-table :test 'equal))
2347 (defun nxml-dynamic-markup-word ()
2348 "Dynamically markup the word before point.
2349 This attempts to find a tag to put around the word before point based
2350 on the contents of the current buffer. The end-tag will be inserted at
2351 point. The start-tag will be inserted at or before the beginning of
2352 the word before point; the contents of the current buffer is used to
2355 It works in a similar way to \\[dabbrev-expand]. It searches first
2356 backwards from point, then forwards from point for an element whose
2357 content is a string which matches the contents of the buffer before
2358 point and which includes at least the word before point. It then
2359 copies the start- and end-tags from that element and uses them to
2360 surround the matching string before point.
2362 Repeating \\[nxml-dynamic-markup-word] immediately after successful
2363 \\[nxml-dynamic-markup-word] removes the previously inserted markup
2364 and attempts to find another possible way to do the markup."
2366 (let (search-start-pos done)
2367 (if (and (integerp nxml-dynamic-markup-prev-pos)
2368 (= nxml-dynamic-markup-prev-pos (point))
2369 (eq last-command this-command)
2370 nxml-dynamic-markup-prev-lengths)
2371 (let* ((end-tag-open-pos
2372 (- nxml-dynamic-markup-prev-pos
2373 (nth 2 nxml-dynamic-markup-prev-lengths)))
2374 (start-tag-close-pos
2376 (nth 1 nxml-dynamic-markup-prev-lengths)))
2378 (- start-tag-close-pos
2379 (nth 0 nxml-dynamic-markup-prev-lengths))))
2380 (delete-region end-tag-open-pos nxml-dynamic-markup-prev-pos)
2381 (delete-region start-tag-open-pos start-tag-close-pos)
2382 (setq search-start-pos
2383 (marker-position nxml-dynamic-markup-prev-found-marker)))
2384 (clrhash nxml-dynamic-markup-prev-start-tags))
2385 (setq nxml-dynamic-markup-prev-pos nil)
2386 (setq nxml-dynamic-markup-prev-lengths nil)
2387 (setq nxml-dynamic-markup-prev-found-marker nil)
2390 (let* ((pos (point))
2393 (unless (< (point) pos)
2394 (error "No word to markup"))
2395 (buffer-substring-no-properties (point) pos)))
2396 (search (concat word "</"))
2398 (when search-start-pos
2399 (goto-char search-start-pos))
2400 (while (and (not done)
2401 (or (and (< (point) pos)
2402 (or (search-backward search nil t)
2403 (progn (goto-char pos) nil)))
2404 (search-forward search nil t)))
2405 (goto-char (- (match-end 0) 2))
2406 (setq done (nxml-try-copy-markup pos)))
2408 (error (if (zerop (hash-table-count
2409 nxml-dynamic-markup-prev-start-tags))
2410 "No possible markup found for `%s'"
2411 "No more markup possibilities found for `%s'")
2414 (defun nxml-try-copy-markup (word-end-pos)
2416 (let ((end-tag-pos (point)))
2417 (when (and (not (nxml-get-inside end-tag-pos))
2418 (search-backward "<" nil t)
2419 (not (nxml-get-inside (point))))
2421 (when (and (eq xmltok-type 'start-tag)
2422 (< (point) end-tag-pos))
2423 (let* ((start-tag-close-pos (point))
2425 (buffer-substring-no-properties xmltok-start
2426 start-tag-close-pos))
2430 (buffer-substring-no-properties start-tag-close-pos
2433 (goto-char word-end-pos)
2435 (re-search-backward (concat
2436 (regexp-quote (car words))
2440 (setq words (cdr words))
2441 (skip-chars-backward " \t\r\n"))
2442 (when (and (not words)
2444 (skip-chars-forward " \t\r\n")
2445 (not (gethash (cons (point) start-tag)
2446 nxml-dynamic-markup-prev-start-tags)))
2447 (or (< end-tag-pos (point))
2448 (< word-end-pos xmltok-start)))
2449 (setq nxml-dynamic-markup-prev-found-marker
2450 (copy-marker end-tag-pos t))
2451 (puthash (cons (point) start-tag)
2453 nxml-dynamic-markup-prev-start-tags)
2454 (setq nxml-dynamic-markup-prev-lengths
2455 (list (- start-tag-close-pos xmltok-start)
2456 (- word-end-pos (point))
2457 (+ (- xmltok-name-end xmltok-start) 2)))
2458 (let ((name (xmltok-start-tag-qname)))
2460 (goto-char (+ word-end-pos
2461 (- start-tag-close-pos xmltok-start)))
2462 (insert "</" name ">")
2463 (setq nxml-dynamic-markup-prev-pos (point))))))))))
2468 (defvar nxml-char-name-ignore-case nil)
2470 (defvar nxml-char-name-alist nil
2471 "Alist of character names.
2472 Each member of the list has the form (NAME CODE . NAMESET),
2473 where NAME is a string naming a character, NAMESET is a symbol
2474 identifying a set of names and CODE is an integer specifying the
2475 Unicode scalar value of the named character.
2476 The NAME will only be used for completion if NAMESET has
2477 a non-nil `nxml-char-name-set-enabled' property.
2478 If NAMESET does does not have `nxml-char-name-set-defined' property,
2479 then it must have a `nxml-char-name-set-file' property and `load'
2480 will be applied to the value of this property if the nameset
2483 (defvar nxml-char-name-table (make-hash-table :test 'eq)
2484 "Hash table for mapping char codes to names.
2485 Each key is a Unicode scalar value.
2486 Each value is a list of pairs of the form (NAMESET . NAME),
2487 where NAMESET is a symbol identifying a set of names,
2488 and NAME is a string naming a character.")
2490 (defvar nxml-autoload-char-name-set-list nil
2491 "List of char namesets that can be autoloaded.")
2493 (defun nxml-enable-char-name-set (nameset)
2494 (put nameset 'nxml-char-name-set-enabled t))
2496 (defun nxml-disable-char-name-set (nameset)
2497 (put nameset 'nxml-char-name-set-enabled nil))
2499 (defun nxml-char-name-set-enabled-p (nameset)
2500 (get nameset 'nxml-char-name-set-enabled))
2502 (defun nxml-autoload-char-name-set (nameset file)
2503 (unless (memq nameset nxml-autoload-char-name-set-list)
2504 (setq nxml-autoload-char-name-set-list
2505 (cons nameset nxml-autoload-char-name-set-list)))
2506 (put nameset 'nxml-char-name-set-file file))
2508 (defun nxml-define-char-name-set (nameset alist)
2509 "Define a set of character names.
2510 NAMESET is a symbol identifying the set.
2511 Alist is a list where each member has the form (NAME CODE),
2512 where NAME is a string naming a character and code
2513 is an integer giving the Unicode scalar value of the character."
2514 (when (get nameset 'nxml-char-name-set-defined)
2515 (error "Nameset `%s' already defined" nameset))
2518 (let* ((name-code (car iter))
2519 (name (car name-code))
2520 (code (cadr name-code)))
2522 (cons (cons nameset name)
2523 (gethash code nxml-char-name-table))
2524 nxml-char-name-table))
2525 (setcdr (cdr (car iter)) nameset)
2526 (setq iter (cdr iter))))
2527 (setq nxml-char-name-alist
2528 (nconc alist nxml-char-name-alist))
2529 (put nameset 'nxml-char-name-set-defined t))
2531 (defun nxml-get-char-name (code)
2532 (mapc 'nxml-maybe-load-char-name-set nxml-autoload-char-name-set-list)
2533 (let ((names (gethash code nxml-char-name-table))
2535 (while (and names (not name))
2536 (if (nxml-char-name-set-enabled-p (caar names))
2537 (setq name (cdar names))
2538 (setq names (cdr names))))
2541 (defvar nxml-named-char-history nil)
2543 (defun nxml-insert-named-char (arg)
2544 "Insert a character using its name.
2545 The name is read from the minibuffer.
2546 Normally, inserts the character as a numeric character reference.
2547 With a prefix argument, inserts the character directly."
2549 (mapc 'nxml-maybe-load-char-name-set nxml-autoload-char-name-set-list)
2551 (let ((completion-ignore-case nxml-char-name-ignore-case))
2552 (completing-read "Character name: "
2553 nxml-char-name-alist
2555 (get (cddr member) 'nxml-char-name-set-enabled))
2558 'nxml-named-char-history)))
2559 (alist nxml-char-name-alist)
2561 (while (and alist (not code))
2562 (setq elt (assoc name alist))
2563 (if (get (cddr elt) 'nxml-char-name-set-enabled)
2564 (setq code (cadr elt))
2565 (setq alist (cdr (member elt alist)))))
2568 (or (decode-char 'ucs code)
2569 (error "Character %x is not supported by Emacs"
2571 (format "&#x%X;" code))))))
2573 (defun nxml-maybe-load-char-name-set (sym)
2574 (when (and (get sym 'nxml-char-name-set-enabled)
2575 (not (get sym 'nxml-char-name-set-defined))
2576 (stringp (get sym 'nxml-char-name-set-file)))
2577 (load (get sym 'nxml-char-name-set-file))))
2579 (defun nxml-toggle-char-ref-extra-display (arg)
2580 "*Toggle the display of extra information for character references."
2582 (let ((new (if (null arg)
2583 (not nxml-char-ref-extra-display)
2584 (> (prefix-numeric-value arg) 0))))
2585 (when (not (eq new nxml-char-ref-extra-display))
2586 (setq nxml-char-ref-extra-display new)
2590 (if nxml-char-ref-extra-display
2591 (nxml-with-unmodifying-text-property-changes
2592 (nxml-clear-fontified (point-min) (point-max)))
2593 (nxml-clear-char-ref-extra-display (point-min) (point-max))))))))
2595 (put 'nxml-char-ref 'evaporate t)
2597 (defun nxml-char-ref-display-extra (start end n)
2598 (when nxml-char-ref-extra-display
2599 (let ((name (nxml-get-char-name n))
2600 (glyph-string (and nxml-char-ref-display-glyph-flag
2601 (nxml-glyph-display-string n 'nxml-glyph-face)))
2603 (when (or name glyph-string)
2604 (setq ov (make-overlay start end nil t))
2605 (overlay-put ov 'category 'nxml-char-ref)
2607 (overlay-put ov 'help-echo name))
2611 (propertize glyph-string 'face 'nxml-glyph-face)))))))
2613 (defun nxml-clear-char-ref-extra-display (start end)
2614 (let ((ov (overlays-in start end)))
2616 (when (eq (overlay-get (car ov) 'category) 'nxml-char-ref)
2617 (delete-overlay (car ov)))
2618 (setq ov (cdr ov)))))
2622 (defun nxml-version ()
2623 "Show the version of nXML mode that is being used."
2626 (message "nXML mode version %s" nxml-version)
2627 (message "nXML mode version unknown")))
2630 (defun nxml-start-delimiter-length (type)
2631 (or (get type 'nxml-start-delimiter-length)
2634 (put 'cdata-section 'nxml-start-delimiter-length 9)
2635 (put 'comment 'nxml-start-delimiter-length 4)
2636 (put 'processing-instruction 'nxml-start-delimiter-length 2)
2637 (put 'start-tag 'nxml-start-delimiter-length 1)
2638 (put 'empty-element 'nxml-start-delimiter-length 1)
2639 (put 'partial-empty-element 'nxml-start-delimiter-length 1)
2640 (put 'entity-ref 'nxml-start-delimiter-length 1)
2641 (put 'char-ref 'nxml-start-delimiter-length 2)
2643 (defun nxml-end-delimiter-length (type)
2644 (or (get type 'nxml-end-delimiter-length)
2647 (put 'cdata-section 'nxml-end-delimiter-length 3)
2648 (put 'comment 'nxml-end-delimiter-length 3)
2649 (put 'processing-instruction 'nxml-end-delimiter-length 2)
2650 (put 'start-tag 'nxml-end-delimiter-length 1)
2651 (put 'empty-element 'nxml-end-delimiter-length 2)
2652 (put 'partial-empty-element 'nxml-end-delimiter-length 1)
2653 (put 'entity-ref 'nxml-end-delimiter-length 1)
2654 (put 'char-ref 'nxml-end-delimiter-length 1)
2656 (defun nxml-token-type-friendly-name (type)
2657 (or (get type 'nxml-friendly-name)
2658 (symbol-name type)))
2660 (put 'cdata-section 'nxml-friendly-name "CDATA section")
2661 (put 'processing-instruction 'nxml-friendly-name "processing instruction")
2662 (put 'entity-ref 'nxml-friendly-name "entity reference")
2663 (put 'char-ref 'nxml-friendly-name "character reference")
2665 (provide 'nxml-mode)
2667 ;; arch-tag: 8603bc5f-1ef9-4021-b223-322fb2ca708e
2668 ;;; nxml-mode.el ends here