1 ;;; nxml-mode.el --- a new XML mode
3 ;; Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010 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 of the License, or
13 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
25 ;; See nxml-rap.el for description of parsing strategy.
29 (when (featurep 'mucs)
30 (error "nxml-mode is not compatible with Mule-UCS"))
32 (eval-when-compile (require 'cl)) ; for assert
41 (declare-function rng-nxml-mode-init "rng-nxml")
42 (declare-function nxml-enable-unicode-char-name-sets "nxml-uchnm")
47 "New XML editing mode."
50 (defgroup nxml-faces nil
51 "Faces for XML syntax highlighting."
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."
62 (defcustom nxml-mode-hook nil
63 "Hook run by command `nxml-mode'."
67 (defcustom nxml-sexp-element-flag nil
68 "*Non-nil means sexp commands treat an element as a single expression."
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'."
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."
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."
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."
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))))
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
115 (defcustom nxml-prefer-utf-16-little-to-big-endian-flag (eq system-type
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'."
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."
129 :type 'coding-system)
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'."
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."
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."
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."
157 (defface nxml-delimiter
159 "Face used to highlight delimiters.
160 This is not used directly, but only via inheritance by other faces."
165 "Face used to highlight text."
168 (defface nxml-comment-content
169 '((t (:inherit font-lock-comment-face)))
170 "Face used to highlight the content of comments."
173 (defface nxml-comment-delimiter
174 '((t (:inherit font-lock-comment-delimiter-face)))
175 "Face used for the delimiters of comments, i.e <!-- and -->."
178 (defface nxml-processing-instruction-delimiter
179 '((t (:inherit nxml-delimiter)))
180 "Face used for the delimiters of processing instructions, i.e <? and ?>."
183 (defface nxml-processing-instruction-target
184 '((t (:inherit font-lock-keyword-face)))
185 "Face used for the target of processing instructions."
188 (defface nxml-processing-instruction-content
189 '((t (:inherit nxml-delimited-data)))
190 "Face used for the content of processing instructions."
193 (defface nxml-cdata-section-delimiter
194 '((t (:inherit nxml-delimiter)))
195 "Face used for the delimiters of CDATA sections, i.e <![, [, and ]]>."
198 (defface nxml-cdata-section-CDATA
199 '((t (:inherit nxml-name)))
200 "Face used for the CDATA keyword in CDATA sections."
203 (defface nxml-cdata-section-content
204 '((t (:inherit nxml-text)))
205 "Face used for the content of CDATA sections."
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."
214 (defface nxml-char-ref-delimiter
215 '((t (:inherit nxml-ref)))
216 "Face used for the delimiters of character references, i.e &# and ;."
219 (defface nxml-entity-ref-name
220 '((t (:inherit nxml-ref)))
221 "Face used for the entity name in general entity references."
224 (defface nxml-entity-ref-delimiter
225 '((t (:inherit nxml-ref)))
226 "Face used for the delimiters of entity references, i.e & and ;."
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."
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."
240 (defface nxml-element-prefix
241 '((t (:inherit nxml-name)))
242 "Face used for the prefix of elements."
245 (defface nxml-element-colon
247 "Face used for the colon in element names."
250 (defface nxml-element-local-name
251 '((t (:inherit font-lock-function-name-face)))
252 "Face used for the local name of elements."
255 (defface nxml-attribute-prefix
256 '((t (:inherit nxml-name)))
257 "Face used for the prefix of attributes."
260 (defface nxml-attribute-colon
261 '((t (:inherit nxml-delimiter)))
262 "Face used for the colon in attribute names."
265 (defface nxml-attribute-local-name
266 '((t (:inherit font-lock-variable-name-face)))
267 "Face used for the local name of attributes."
270 (defface nxml-namespace-attribute-xmlns
271 '((t (:inherit nxml-attribute-prefix)))
272 "Face used for `xmlns' in namespace attributes."
275 (defface nxml-namespace-attribute-colon
276 '((t (:inherit nxml-attribute-colon)))
277 "Face used for the colon in namespace attributes."
280 (defface nxml-namespace-attribute-prefix
281 '((t (:inherit nxml-attribute-local-name)))
282 "Face used for the prefix declared in namespace attributes."
285 (defface nxml-attribute-value
286 '((t (:inherit font-lock-string-face)))
287 "Face used for the value of attributes."
290 (defface nxml-attribute-value-delimiter
291 '((t (:inherit nxml-attribute-value)))
292 "Face used for the delimiters of attribute values."
295 (defface nxml-namespace-attribute-value
296 '((t (:inherit nxml-attribute-value)))
297 "Face used for the value of namespace attributes."
300 (defface nxml-namespace-attribute-value-delimiter
301 '((t (:inherit nxml-attribute-value-delimiter)))
302 "Face used for the delimiters of namespace attribute values."
305 (defface nxml-prolog-literal-delimiter
306 '((t (:inherit nxml-delimited-data)))
307 "Face used for the delimiters of literals in the prolog."
310 (defface nxml-prolog-literal-content
311 '((t (:inherit nxml-delimited-data)))
312 "Face used for the content of literals in the prolog."
315 (defface nxml-prolog-keyword
316 '((t (:inherit font-lock-keyword-face)))
317 "Face used for keywords in the prolog."
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 >."
327 '((t (:inherit nxml-name)))
328 "Face used for # before a name in the prolog."
352 "Face used for glyph for char references."
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)
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)
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)
373 (defvar nxml-completion-hook nil
374 "Hook run by `nxml-complete'.
375 This hook is run until success.")
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.")
383 (defvar nxml-mixed-scan-distance 4000
384 "Maximum distance from point to scan when checking for mixed content.")
386 (defvar nxml-end-tag-indent-scan-distance 4000
387 "Maximum distance from point to scan backwards when indenting end-tag.")
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
395 (make-variable-buffer-local 'nxml-char-ref-extra-display)
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/" 'nxml-finish-element)
409 (define-key map "\C-c\C-m" 'nxml-split-element)
410 (define-key map "\C-c\C-b" 'nxml-balanced-close-start-tag-block)
411 (define-key map "\C-c\C-i" 'nxml-balanced-close-start-tag-inline)
412 (define-key map "\C-c\C-x" 'nxml-insert-xml-declaration)
413 (define-key map "\C-c\C-d" 'nxml-dynamic-markup-word)
415 (define-key map "\C-c\C-u" 'nxml-insert-named-char)
416 (define-key map "\C-c\C-o" nxml-outline-prefix-map)
417 (define-key map [S-mouse-2] 'nxml-mouse-hide-direct-text-content)
418 (define-key map "/" 'nxml-electric-slash)
419 (define-key map [C-return] 'nxml-complete)
420 (when nxml-bind-meta-tab-to-complete-flag
421 (define-key map "\M-\t" 'nxml-complete))
423 "Keymap for nxml-mode.")
425 (defvar nxml-font-lock-keywords
426 '(nxml-fontify-matcher)
427 "Default font lock keywords for nxml-mode.")
429 (defsubst nxml-set-face (start end face)
430 (when (and face (< start end))
431 (font-lock-append-text-property start end 'face face)))
435 ;; We use C-c C-i instead of \\[nxml-balanced-close-start-tag-inline]
436 ;; because Emacs turns C-c C-i into C-c TAB which is hard to type and
438 "Major mode for editing XML.
440 \\[nxml-finish-element] finishes the current element by inserting an end-tag.
441 C-c C-i closes a start-tag with `>' and then inserts a balancing end-tag
442 leaving point between the start-tag and end-tag.
443 \\[nxml-balanced-close-start-tag-block] is similar but for block rather than inline elements:
444 the start-tag, point, and end-tag are all left on separate lines.
445 If `nxml-slash-auto-complete-flag' is non-nil, then inserting a `</'
446 automatically inserts the rest of the end-tag.
448 \\[nxml-complete] performs completion on the symbol preceding point.
450 \\[nxml-dynamic-markup-word] uses the contents of the current buffer
451 to choose a tag to put around the word preceding point.
453 Sections of the document can be displayed in outline form. The
454 variable `nxml-section-element-name-regexp' controls when an element
455 is recognized as a section. The same key sequences that change
456 visibility in outline mode are used except that they start with C-c C-o
459 Validation is provided by the related minor-mode `rng-validate-mode'.
460 This also makes completion schema- and context- sensitive. Element
461 names, attribute names, attribute values and namespace URIs can all be
462 completed. By default, `rng-validate-mode' is automatically enabled.
463 You can toggle it using \\[rng-validate-mode] or change the default by
464 customizing `rng-nxml-auto-validate-flag'.
466 \\[indent-for-tab-command] indents the current line appropriately.
467 This can be customized using the variable `nxml-child-indent'
468 and the variable `nxml-attribute-indent'.
470 \\[nxml-insert-named-char] inserts a character reference using
471 the character's name (by default, the Unicode name).
472 \\[universal-argument] \\[nxml-insert-named-char] inserts the character directly.
474 The Emacs commands that normally operate on balanced expressions will
475 operate on XML markup items. Thus \\[forward-sexp] will move forward
476 across one markup item; \\[backward-sexp] will move backward across
477 one markup item; \\[kill-sexp] will kill the following markup item;
478 \\[mark-sexp] will mark the following markup item. By default, each
479 tag each treated as a single markup item; to make the complete element
480 be treated as a single markup item, set the variable
481 `nxml-sexp-element-flag' to t. For more details, see the function
482 `nxml-forward-balanced-item'.
484 \\[nxml-backward-up-element] and \\[nxml-down-element] move up and down the element structure.
486 Many aspects this mode can be customized using
487 \\[customize-group] nxml RET."
489 (kill-all-local-variables)
490 (setq major-mode 'nxml-mode)
491 (setq mode-name "nXML")
492 (set (make-local-variable 'mode-line-process) '((nxml-degraded "/degraded")))
493 ;; We'll determine the fill prefix ourselves
494 (make-local-variable 'adaptive-fill-mode)
495 (setq adaptive-fill-mode nil)
496 (make-local-variable 'forward-sexp-function)
497 (setq forward-sexp-function 'nxml-forward-balanced-item)
498 (make-local-variable 'indent-line-function)
499 (setq indent-line-function 'nxml-indent-line)
500 (make-local-variable 'fill-paragraph-function)
501 (setq fill-paragraph-function 'nxml-do-fill-paragraph)
503 ;; This doesn't seem to work too well;
504 ;; I think we should probably roll our own nxml-comment-dwim function.
505 (make-local-variable 'comment-indent-function)
506 (setq comment-indent-function 'nxml-indent-line)
507 (make-local-variable 'comment-start)
508 (setq comment-start "<!--")
509 (make-local-variable 'comment-start-skip)
510 (setq comment-start-skip "<!--[ \t\r\n]*")
511 (make-local-variable 'comment-end)
512 (setq comment-end "-->")
513 (make-local-variable 'comment-end-skip)
514 (setq comment-end-skip "[ \t\r\n]*-->")
515 (make-local-variable 'comment-line-break-function)
516 (setq comment-line-break-function 'nxml-newline-and-indent)
517 (use-local-map nxml-mode-map)
521 (nxml-clear-dependent-regions (point-min) (point-max))
522 (setq nxml-scan-end (copy-marker (point-min) nil))
523 (nxml-with-unmodifying-text-property-changes
524 (nxml-clear-inside (point-min) (point-max))
525 (nxml-with-invisible-motion
526 (nxml-scan-prolog)))))
527 (add-hook 'after-change-functions 'nxml-after-change nil t)
528 (add-hook 'change-major-mode-hook 'nxml-cleanup nil t)
530 ;; Emacs 23 handles the encoding attribute on the xml declaration
531 ;; transparently to nxml-mode, so there is no longer a need for the below
532 ;; hook. The hook also had the drawback of overriding explicit user
533 ;; instruction to save as some encoding other than utf-8.
534 ;;; (add-hook 'write-contents-hooks 'nxml-prepare-to-save)
535 (when (not (and (buffer-file-name) (file-exists-p (buffer-file-name))))
536 (when (and nxml-default-buffer-file-coding-system
537 (not (local-variable-p 'buffer-file-coding-system)))
538 (setq buffer-file-coding-system nxml-default-buffer-file-coding-system))
539 (when nxml-auto-insert-xml-declaration-flag
540 (nxml-insert-xml-declaration)))
542 (setq font-lock-defaults
543 '(nxml-font-lock-keywords
544 t ; keywords-only; we highlight comments and strings here
545 nil ; font-lock-keywords-case-fold-search. XML is case sensitive
546 nil ; no special syntax table
547 nil ; no automatic syntactic fontification
548 (font-lock-extend-after-change-region-function
549 . nxml-extend-after-change-region)
550 (font-lock-extend-region-functions . (nxml-extend-region))
551 (jit-lock-contextually . t)
552 (font-lock-unfontify-region-function . nxml-unfontify-region)))
555 (nxml-enable-unicode-char-name-sets)
556 (run-mode-hooks 'nxml-mode-hook))
558 (defun nxml-cleanup ()
559 "Clean up after nxml-mode."
560 ;; Disable associated minor modes.
561 (rng-validate-mode -1)
562 ;; Clean up fontification.
565 (let ((inhibit-read-only t)
567 (modified (buffer-modified-p)))
568 (nxml-with-invisible-motion
569 (remove-text-properties (point-min) (point-max) '(face)))
570 (set-buffer-modified-p modified)))
571 (remove-hook 'change-major-mode-hook 'nxml-cleanup t))
573 (defun nxml-degrade (context err)
574 (message "Internal nXML mode error in %s (%s), degrading"
576 (error-message-string err))
578 (setq nxml-degraded t)
579 (setq nxml-prolog-end 1)
583 (nxml-with-unmodifying-text-property-changes
584 (nxml-clear-inside (point-min) (point-max))))))
586 ;;; Change management
588 (defun nxml-debug-region (start end)
590 (let ((font-lock-beg start)
593 (goto-char font-lock-beg)
594 (set-mark font-lock-end)))
596 (defun nxml-after-change (start end pre-change-length)
597 ; In font-lock mode, nxml-after-change1 is called via
598 ; nxml-extend-after-change-region instead so that the updated
599 ; book-keeping information is available for fontification.
600 (unless (or font-lock-mode nxml-degraded)
601 (nxml-with-degradation-on-error 'nxml-after-change
606 (nxml-with-invisible-motion
607 (nxml-with-unmodifying-text-property-changes
609 start end pre-change-length)))))))))
611 (defun nxml-after-change1 (start end pre-change-length)
612 "After-change bookkeeping.
613 Returns a cons cell containing a possibly-enlarged change region.
614 You must call `nxml-extend-region' on this expanded region to obtain
615 the full extent of the area needing refontification.
617 For bookkeeping, call this function even when fontification is
619 (let ((pre-change-end (+ start pre-change-length)))
621 (nxml-adjust-start-for-dependent-regions start
624 ;; If the prolog might have changed, rescan the prolog
626 ;; Add 2 so as to include the < and following char that
627 ;; start the instance (document element), since changing
628 ;; these can change where the prolog ends.
629 (+ nxml-prolog-end 2))
630 ;; end must be extended to at least the end of the old prolog in
631 ;; case the new prolog is shorter
632 (when (< pre-change-end nxml-prolog-end)
634 ;; don't let end get out of range even if pre-change-length
637 (+ end (- nxml-prolog-end pre-change-end)))))
639 (setq start (point-min))))
641 (when (> end nxml-prolog-end)
643 (nxml-move-tag-backwards (point-min))
645 (setq end (max (nxml-scan-after-change start end)
648 (nxml-debug-change "nxml-after-change1" start end)
653 (defun nxml-insert-xml-declaration ()
654 "Insert an XML declaration at the beginning of buffer.
655 The XML declaration will declare an encoding depending on the buffer's
656 `buffer-file-coding-system'."
659 (if (and buffer-file-coding-system
660 (coding-system-p buffer-file-coding-system)
661 (coding-system-get buffer-file-coding-system
663 buffer-file-coding-system
664 (nxml-choose-utf-coding-system))))
665 (goto-char (point-min))
666 (insert (format "<?xml version=\"1.0\" encoding=\"%s\"?>\n"
667 (nxml-coding-system-name coding-system)))))
669 (defun nxml-prepare-to-save ()
670 (unless (and (not enable-multibyte-characters)
671 (local-variable-p 'buffer-file-coding-system)
672 buffer-file-coding-system
673 (or (eq (coding-system-type buffer-file-coding-system) 5)
674 (eq buffer-file-coding-system 'no-conversion)))
676 (setq buffer-file-coding-system (nxml-select-coding-system))))
677 ;; nil from a function in `write-contents-hooks' means
678 ;; to continue and write the file as normal
681 (defun nxml-select-coding-system ()
682 (let* ((suitable-coding-systems
683 (find-coding-systems-region (point-min) (point-max)))
685 (goto-char (point-min))
686 (xmltok-get-declared-encoding-position)))
689 (buffer-substring-no-properties (car enc-pos)
693 (if (string= (downcase enc-name) "utf-16")
694 (nxml-choose-utf-16-coding-system)
695 (nxml-mime-charset-coding-system enc-name)))
696 (enc-pos (nxml-choose-utf-coding-system)))))
697 ;; Make sure we have a coding-system
698 (unless coding-system
700 (and (not buffer-read-only)
701 (nxml-choose-suitable-coding-system
702 suitable-coding-systems)))
705 (format "Unknown encoding %s" enc-name)
706 "XML declaration is not well-formed")))
707 (cond ((not coding-system)
708 (error "%s" message))
714 "Modify and save with encoding %s")
715 (nxml-coding-system-name coding-system))
717 (nxml-fix-encoding-declaration enc-pos coding-system))
718 (t (signal 'quit nil)))))
719 ;; Make sure it can encode all the characters in the buffer
720 (unless (or (memq (coding-system-base coding-system)
721 suitable-coding-systems)
722 (equal suitable-coding-systems '(undecided)))
724 (nxml-unsuitable-coding-system-message coding-system
727 (and (not buffer-read-only)
728 (nxml-choose-suitable-coding-system
729 suitable-coding-systems)))
730 (cond ((not coding-system) (error "%s" message))
731 ((y-or-n-p (concat message
732 (format ". Save with %s "
733 (nxml-coding-system-name
735 (nxml-fix-encoding-declaration enc-pos coding-system))
736 (t (signal 'quit nil)))))
737 ;; Merge the newline type of our existing encoding
738 (let ((current-eol-type
739 (coding-system-eol-type buffer-file-coding-system)))
740 (when (and current-eol-type (integerp current-eol-type))
742 (coding-system-change-eol-conversion coding-system
746 (defun nxml-unsuitable-coding-system-message (coding-system &optional enc-name)
747 (if (nxml-coding-system-unicode-p coding-system)
748 "Cannot translate some characters to Unicode"
749 (format "Cannot encode some characters with %s"
751 (nxml-coding-system-name coding-system)))))
753 (defconst nxml-utf-16-coding-systems (and (coding-system-p 'utf-16-be)
754 (coding-system-p 'utf-16-le)
755 '(utf-16-be utf-16-le)))
757 (defconst nxml-utf-coding-systems (cons 'utf-8 nxml-utf-16-coding-systems))
759 (defun nxml-coding-system-unicode-p (coding-system)
760 (nxml-coding-system-member (coding-system-base coding-system)
761 nxml-utf-coding-systems))
763 (defun nxml-coding-system-name (coding-system)
764 (setq coding-system (coding-system-base coding-system))
766 (if (nxml-coding-system-member coding-system nxml-utf-16-coding-systems)
768 (or (coding-system-get coding-system 'mime-charset)
771 (defun nxml-fix-encoding-declaration (enc-pos coding-system)
772 (let ((charset (nxml-coding-system-name coding-system)))
773 (cond ((consp enc-pos)
774 (delete-region (car enc-pos) (cdr enc-pos))
775 (goto-char (car enc-pos))
779 (insert " encoding=\"" charset ?\"))
781 (goto-char (point-min))
782 (insert "<?xml version=\"1.0\" encoding=\""
785 (when (and (not enc-pos)
786 (let ((case-fold-search t))
787 (looking-at xmltok-bad-xml-decl-regexp)))
788 (delete-region (point) (match-end 0)))))))
790 (defun nxml-choose-suitable-coding-system (suitable-coding-systems)
791 (let (ret coding-system)
792 (if (and buffer-file-coding-system
793 (memq (coding-system-base buffer-file-coding-system)
794 suitable-coding-systems))
795 buffer-file-coding-system
796 (while (and suitable-coding-systems (not ret))
797 (setq coding-system (car suitable-coding-systems))
798 (if (coding-system-get coding-system 'mime-charset)
799 (setq ret coding-system)
800 (setq suitable-coding-systems (cdr suitable-coding-systems))))
803 (defun nxml-choose-utf-coding-system ()
804 (let ((cur (and (local-variable-p 'buffer-file-coding-system)
805 buffer-file-coding-system
806 (coding-system-base buffer-file-coding-system))))
807 (cond ((car (nxml-coding-system-member cur nxml-utf-coding-systems)))
808 ((and nxml-prefer-utf-16-to-utf-8-flag
809 (coding-system-p 'utf-16-le)
810 (coding-system-p 'utf-16-be))
811 (if nxml-prefer-utf-16-little-to-big-endian-flag
816 (defun nxml-choose-utf-16-coding-system ()
817 (let ((cur (and (local-variable-p 'buffer-file-coding-system)
818 buffer-file-coding-system
819 (coding-system-base buffer-file-coding-system))))
820 (cond ((car (nxml-coding-system-member cur nxml-utf-16-coding-systems)))
821 (nxml-prefer-utf-16-little-to-big-endian-flag
822 (and (coding-system-p 'utf-16-le) 'utf-16-le))
823 (t (and (coding-system-p 'utf-16-be) 'utf-16-be)))))
825 (defun nxml-coding-system-member (coding-system coding-systems)
827 (while (and coding-systems (not ret))
828 (if (coding-system-equal coding-system
829 (car coding-systems))
830 (setq ret coding-systems)
831 (setq coding-systems (cdr coding-systems))))
836 (defun nxml-unfontify-region (start end)
837 (font-lock-default-unfontify-region start end)
838 (nxml-clear-char-ref-extra-display start end))
840 (defvar font-lock-beg) (defvar font-lock-end)
841 (defun nxml-extend-region ()
842 "Extend the region to hold the minimum area we can fontify with nXML.
843 Called with `font-lock-beg' and `font-lock-end' dynamically bound."
844 (let ((start font-lock-beg)
847 (nxml-debug-change "nxml-extend-region(input)" start end)
849 (when (< start nxml-prolog-end)
850 (setq start (point-min)))
852 (cond ((<= end nxml-prolog-end)
853 (setq end nxml-prolog-end))
857 ;; some font-lock backends (like Emacs 22 jit-lock) snap
858 ;; the region to the beginning of the line no matter what
859 ;; we say here. To mitigate the resulting excess
860 ;; fontification, ignore leading whitespace.
861 (skip-syntax-forward " ")
863 ;; find the beginning of the previous tag
864 (when (not (equal (char-after) ?\<))
865 (search-backward "<" nxml-prolog-end t))
866 (nxml-ensure-scan-up-to-date)
867 (nxml-move-outside-backwards)
870 (while (< (point) end)
871 (nxml-tokenize-forward))
875 (when (or (< start font-lock-beg)
876 (> end font-lock-end))
877 (setq font-lock-beg start
879 (nxml-debug-change "nxml-extend-region" start end)
882 (defun nxml-extend-after-change-region (start end pre-change-length)
883 (unless nxml-degraded
884 (setq nxml-last-fontify-end nil)
885 (let ((region (nxml-with-degradation-on-error
886 'nxml-extend-after-change-region
891 (nxml-with-invisible-motion
892 (nxml-with-unmodifying-text-property-changes
893 (nxml-extend-after-change-region1
894 start end pre-change-length)))))))))
895 (if (consp region) region))))
897 (defun nxml-extend-after-change-region1 (start end pre-change-length)
898 (let* ((region (nxml-after-change1 start end pre-change-length))
899 (font-lock-beg (car region))
900 (font-lock-end (cdr region)))
903 (cons font-lock-beg font-lock-end)))
905 (defun nxml-fontify-matcher (bound)
906 "Called as font-lock keyword matcher."
908 (unless nxml-degraded
909 (nxml-debug-change "nxml-fontify-matcher" (point) bound)
911 (when (< (point) nxml-prolog-end)
912 ;; prolog needs to be fontified in one go, and
913 ;; nxml-extend-region makes sure we start at BOB.
915 (nxml-fontify-prolog)
916 (goto-char nxml-prolog-end))
918 (let (xmltok-dependent-regions
920 (while (and (nxml-tokenize-forward)
921 (<= (point) bound)) ; intervals are open-ended
922 (nxml-apply-fontify-rule)))
924 (setq nxml-last-fontify-end (point)))
926 ;; Since we did the fontification internally, tell font-lock to not
927 ;; do anything itself.
930 (defun nxml-fontify-prolog ()
932 The buffer is assumed to be prepared for fontification.
933 This does not set the fontified property, but it does clear
934 faces appropriately."
935 (let ((regions nxml-prolog-regions))
937 (let ((region (car regions)))
938 (nxml-apply-fontify-rule (aref region 0)
941 (setq regions (cdr regions)))))
943 ;; Vectors identify a substring of the token to be highlighted in some face.
945 ;; Token types returned by xmltok-forward.
949 '([nil 1 nxml-tag-delimiter]
950 [-1 nil nxml-tag-delimiter]
954 (put 'partial-start-tag
956 '([nil 1 nxml-tag-delimiter]
962 '([nil 1 nxml-tag-delimiter]
964 [-1 nil nxml-tag-delimiter]
965 (element-qname . 2)))
967 (put 'partial-end-tag
969 '([nil 1 nxml-tag-delimiter]
971 (element-qname . 2)))
975 '([nil 1 nxml-tag-delimiter]
976 [-2 -1 nxml-tag-slash]
977 [-1 nil nxml-tag-delimiter]
981 (put 'partial-empty-element
983 '([nil 1 nxml-tag-delimiter]
984 [-1 nil nxml-tag-slash]
990 '([nil 2 nxml-char-ref-delimiter]
991 [2 -1 nxml-char-ref-number]
992 [-1 nil nxml-char-ref-delimiter]
997 '([nil 1 nxml-entity-ref-delimiter]
998 [1 -1 nxml-entity-ref-name]
999 [-1 nil nxml-entity-ref-delimiter]))
1003 '([nil 4 nxml-comment-delimiter]
1004 [4 -3 nxml-comment-content]
1005 [-3 nil nxml-comment-delimiter]))
1007 (put 'processing-instruction
1009 '([nil 2 nxml-processing-instruction-delimiter]
1010 [-2 nil nxml-processing-instruction-delimiter]
1011 processing-instruction-content))
1015 '([nil 3 nxml-cdata-section-delimiter] ; <![
1016 [3 8 nxml-cdata-section-CDATA] ; CDATA
1017 [8 9 nxml-cdata-section-delimiter] ; [
1018 [9 -3 nxml-cdata-section-content] ; ]]>
1019 [-3 nil nxml-cdata-section-delimiter]))
1023 '([nil nil nxml-text]))
1025 ;; Prolog region types in list returned by xmltok-forward-prolog.
1027 (put 'xml-declaration
1029 '([nil 2 nxml-processing-instruction-delimiter]
1030 [2 5 nxml-processing-instruction-target]
1031 [-2 nil nxml-processing-instruction-delimiter]))
1033 (put 'xml-declaration-attribute-name
1035 '([nil nil nxml-attribute-local-name]))
1037 (put 'xml-declaration-attribute-value
1039 '([nil 1 nxml-attribute-value-delimiter]
1040 [1 -1 nxml-attribute-value]
1041 [-1 nil nxml-attribute-value-delimiter]))
1043 (put 'processing-instruction-left
1045 '([nil 2 nxml-processing-instruction-delimiter]
1046 [2 nil nxml-processing-instruction-target]))
1048 (put 'processing-instruction-right
1050 '([nil -2 nxml-processing-instruction-content]
1051 [-2 nil nxml-processing-instruction-delimiter]))
1055 '([nil 1 nxml-prolog-literal-delimiter]
1056 [1 -1 nxml-prolog-literal-content]
1057 [-1 nil nxml-prolog-literal-delimiter]))
1061 '([nil nil nxml-prolog-keyword]))
1063 (put 'markup-declaration-open
1065 '([0 2 nxml-markup-declaration-delimiter]
1066 [2 nil nxml-prolog-keyword]))
1068 (put 'markup-declaration-close
1070 '([nil nil nxml-markup-declaration-delimiter]))
1072 (put 'internal-subset-open
1074 '([nil nil nxml-markup-declaration-delimiter]))
1076 (put 'internal-subset-close
1078 '([nil 1 nxml-markup-declaration-delimiter]
1079 [-1 nil nxml-markup-declaration-delimiter]))
1084 [1 nil nxml-prolog-keyword]))
1086 (defun nxml-apply-fontify-rule (&optional type start end)
1087 (let ((rule (get (or type xmltok-type) 'nxml-fontify-rule)))
1088 (unless start (setq start xmltok-start))
1089 (unless end (setq end (point)))
1091 (let* ((action (car rule)))
1092 (setq rule (cdr rule))
1093 (cond ((vectorp action)
1094 (nxml-set-face (let ((offset (aref action 0)))
1095 (cond ((not offset) start)
1096 ((< offset 0) (+ end offset))
1097 (t (+ start offset))))
1098 (let ((offset (aref action 1)))
1099 (cond ((not offset) end)
1100 ((< offset 0) (+ end offset))
1101 (t (+ start offset))))
1103 ((and (consp action)
1104 (eq (car action) 'element-qname))
1105 (when xmltok-name-end ; maybe nil in partial-end-tag case
1106 (nxml-fontify-qname (+ start (cdr action))
1109 'nxml-element-prefix
1111 'nxml-element-local-name)))
1112 ((eq action 'attributes)
1113 (nxml-fontify-attributes))
1114 ((eq action 'processing-instruction-content)
1115 (nxml-set-face (+ start 2)
1117 'nxml-processing-instruction-target)
1118 (nxml-set-face (save-excursion
1119 (goto-char xmltok-name-end)
1120 (skip-chars-forward " \t\r\n")
1123 'nxml-processing-instruction-content))
1124 ((eq action 'char-ref)
1125 (nxml-char-ref-display-extra start
1127 (xmltok-char-number start end)))
1128 (t (error "Invalid nxml-fontify-rule action %s" action)))))))
1130 (defun nxml-fontify-attributes ()
1131 (while xmltok-namespace-attributes
1132 (nxml-fontify-attribute (car xmltok-namespace-attributes)
1134 (setq xmltok-namespace-attributes
1135 (cdr xmltok-namespace-attributes)))
1136 (while xmltok-attributes
1137 (nxml-fontify-attribute (car xmltok-attributes))
1138 (setq xmltok-attributes
1139 (cdr xmltok-attributes))))
1141 (defun nxml-fontify-attribute (att &optional namespace-declaration)
1142 (if namespace-declaration
1143 (nxml-fontify-qname (xmltok-attribute-name-start att)
1144 (xmltok-attribute-name-colon att)
1145 (xmltok-attribute-name-end att)
1146 'nxml-namespace-attribute-xmlns
1147 'nxml-namespace-attribute-colon
1148 'nxml-namespace-attribute-prefix
1149 'nxml-namespace-attribute-xmlns)
1150 (nxml-fontify-qname (xmltok-attribute-name-start att)
1151 (xmltok-attribute-name-colon att)
1152 (xmltok-attribute-name-end att)
1153 'nxml-attribute-prefix
1154 'nxml-attribute-colon
1155 'nxml-attribute-local-name))
1156 (let ((start (xmltok-attribute-value-start att))
1157 (end (xmltok-attribute-value-end att))
1158 (refs (xmltok-attribute-refs att))
1159 (delimiter-face (if namespace-declaration
1160 'nxml-namespace-attribute-value-delimiter
1161 'nxml-attribute-value-delimiter))
1162 (value-face (if namespace-declaration
1163 'nxml-namespace-attribute-value
1164 'nxml-attribute-value)))
1166 (nxml-set-face (1- start) start delimiter-face)
1167 (nxml-set-face end (1+ end) delimiter-face)
1169 (let* ((ref (car refs))
1170 (ref-type (aref ref 0))
1171 (ref-start (aref ref 1))
1172 (ref-end (aref ref 2)))
1173 (nxml-set-face start ref-start value-face)
1174 (nxml-apply-fontify-rule ref-type ref-start ref-end)
1175 (setq start ref-end))
1176 (setq refs (cdr refs)))
1177 (nxml-set-face start end value-face))))
1179 (defun nxml-fontify-qname (start
1187 (cond (colon (nxml-set-face start colon prefix-face)
1188 (nxml-set-face colon (1+ colon) colon-face)
1189 (nxml-set-face (1+ colon) end local-name-face))
1190 (t (nxml-set-face start end (or unprefixed-face
1191 local-name-face)))))
1195 (defun nxml-electric-slash (arg)
1198 With a prefix ARG, do nothing other than insert the slash.
1200 Otherwise, if `nxml-slash-auto-complete-flag' is non-nil, insert the
1201 rest of the end-tag or empty-element if the slash is potentially part
1202 of an end-tag or the close of an empty-element.
1204 If the slash is part of an end-tag that is the first non-whitespace
1205 on the line, reindent the line."
1207 (nxml-ensure-scan-up-to-date)
1208 (let* ((slash-pos (point))
1209 (end-tag-p (and (eq (char-before slash-pos) ?<)
1210 (not (nxml-get-inside slash-pos))))
1211 (at-indentation (save-excursion
1212 (back-to-indentation)
1213 (eq (point) (1- slash-pos)))))
1214 (self-insert-command (prefix-numeric-value arg))
1216 (if nxml-slash-auto-complete-flag
1219 (let ((start-tag-end
1220 (nxml-scan-element-backward (1- slash-pos) t)))
1222 (insert (xmltok-start-tag-qname) ">")
1223 ;; copy the indentation of the start-tag
1224 (when (and at-indentation
1226 (goto-char xmltok-start)
1227 (back-to-indentation)
1228 (eq (point) xmltok-start)))
1230 (indent-line-to (save-excursion
1231 (goto-char xmltok-start)
1232 (current-column)))))))
1233 (nxml-scan-error nil))
1234 (when (and (eq (nxml-token-before) (point))
1235 (eq xmltok-type 'partial-empty-element))
1237 (when (and end-tag-p at-indentation)
1238 (nxml-indent-line))))))
1240 (defun nxml-balanced-close-start-tag-block ()
1241 "Close the start-tag before point with `>' and insert a balancing end-tag.
1242 Point is left between the start-tag and the end-tag.
1243 If there is nothing but whitespace before the `<' that opens the
1244 start-tag, then put point on a blank line, and put the end-tag on
1245 another line aligned with the start-tag."
1247 (nxml-balanced-close-start-tag 'block))
1249 (defun nxml-balanced-close-start-tag-inline ()
1250 "Close the start-tag before point with `>' and insert a balancing end-tag.
1251 Point is left between the start-tag and the end-tag.
1252 No extra whitespace is inserted."
1254 (nxml-balanced-close-start-tag 'inline))
1256 (defun nxml-balanced-close-start-tag (block-or-inline)
1257 (let ((token-end (nxml-token-before))
1259 (token-start xmltok-start))
1260 (unless (or (eq xmltok-type 'partial-start-tag)
1261 (and (memq xmltok-type '(start-tag
1263 partial-empty-element))
1264 (>= token-end pos)))
1265 (error "Not in a start-tag"))
1266 ;; Note that this insertion changes xmltok-start.
1268 (buffer-substring-no-properties (+ xmltok-start 1)
1269 (min xmltok-name-end (point)))
1271 (if (eq block-or-inline 'inline)
1273 (goto-char token-start)
1274 (back-to-indentation)
1275 (if (= (point) token-start)
1276 (let ((indent (current-column)))
1279 (indent-line-to indent)
1282 (indent-line-to (+ nxml-child-indent indent)))
1285 (defun nxml-finish-element ()
1286 "Finish the current element by inserting an end-tag."
1288 (nxml-finish-element-1 nil))
1290 (defvar nxml-last-split-position nil
1291 "Position where `nxml-split-element' split the current element.")
1293 (defun nxml-split-element ()
1294 "Split the current element by inserting an end-tag and a start-tag.
1295 Point is left after the newly inserted start-tag. When repeated,
1296 split immediately before the previously inserted start-tag and leave
1299 (setq nxml-last-split-position
1300 (if (and (eq last-command this-command)
1301 nxml-last-split-position)
1303 (goto-char nxml-last-split-position)
1304 (nxml-finish-element-1 t))
1305 (nxml-finish-element-1 t))))
1307 (defun nxml-finish-element-1 (startp)
1308 "Insert an end-tag for the current element and optionally a start-tag.
1309 The start-tag is inserted if STARTP is non-nil. Return the position
1310 of the inserted start-tag or nil if none was inserted."
1312 (let* ((token-end (nxml-token-before))
1315 (when (and (< (point) token-end)
1318 processing-instruction
1323 (error "Point is inside a %s"
1324 (nxml-token-type-friendly-name xmltok-type)))
1325 (nxml-scan-element-backward token-end t)))
1328 (unless (eq xmltok-type 'start-tag)
1329 (error "No matching start-tag"))
1330 (goto-char xmltok-start)
1331 (back-to-indentation)
1332 (eq (point) xmltok-start)))
1335 (goto-char start-tag-end)
1336 (looking-at "[ \t\r\n]*$")))
1337 (start-tag-indent (save-excursion
1338 (goto-char xmltok-start)
1340 (qname (xmltok-start-tag-qname))
1341 inserted-start-tag-pos)
1342 (when (and starts-line ends-line)
1343 ;; start-tag is on a line by itself
1344 ;; => put the end-tag on a line by itself
1347 (back-to-indentation)
1350 (indent-line-to start-tag-indent))
1351 (insert "</" qname ">")
1355 (indent-line-to start-tag-indent))
1356 (setq inserted-start-tag-pos (point))
1357 (insert "<" qname ">")
1358 (when (and starts-line ends-line)
1360 (indent-line-to (save-excursion
1361 (goto-char xmltok-start)
1363 (back-to-indentation)
1364 (if (= (current-column)
1365 (+ start-tag-indent nxml-child-indent))
1366 (+ start-tag-indent nxml-child-indent)
1367 start-tag-indent)))))
1368 inserted-start-tag-pos))
1372 (defun nxml-indent-line ()
1373 "Indent current line as XML."
1374 (let* ((savep (point))
1375 (indent (condition-case nil
1378 (skip-chars-forward " \t")
1379 (if (>= (point) savep) (setq savep nil))
1380 (or (nxml-compute-indent) 0))
1382 (if (not (numberp indent))
1383 ;; If something funny is used (e.g. `noindent'), return it.
1385 (if (< indent 0) (setq indent 0)) ;Just in case.
1387 (save-excursion (indent-line-to indent))
1388 (indent-line-to indent)))))
1390 (defun nxml-compute-indent ()
1391 "Return the indent for the line containing point."
1392 (or (nxml-compute-indent-from-matching-start-tag)
1393 (nxml-compute-indent-from-previous-line)))
1395 (defun nxml-compute-indent-from-matching-start-tag ()
1396 "Compute the indent for a line with an end-tag using the matching start-tag.
1397 When the line containing point ends with an end-tag and does not start
1398 in the middle of a token, return the indent of the line containing the
1399 matching start-tag, if there is one and it occurs at the beginning of
1400 its line. Otherwise return nil."
1402 (back-to-indentation)
1403 (let ((bol (point)))
1404 (let ((inhibit-field-text-motion t))
1406 (skip-chars-backward " \t")
1407 (and (= (nxml-token-before) (point))
1408 (memq xmltok-type '(end-tag partial-end-tag))
1409 ;; start of line must not be inside a token
1410 (or (= xmltok-start bol)
1414 (= xmltok-start bol))
1415 (eq xmltok-type 'data))
1417 (nxml-scan-element-backward
1421 nxml-end-tag-indent-scan-distance))
1422 (nxml-scan-error nil))
1423 (< xmltok-start bol)
1425 (goto-char xmltok-start)
1426 (skip-chars-backward " \t")
1428 (current-indentation)))))
1430 (defun nxml-compute-indent-from-previous-line ()
1431 "Compute the indent for a line using the indentation of a previous line."
1436 before-context after-context)
1437 (back-to-indentation)
1440 ;; Move backwards until the start of a non-blank line that is
1441 ;; not inside a token.
1443 (when (= (forward-line -1) -1)
1445 (back-to-indentation)
1446 (if (looking-at "[ \t]*$")
1449 (setq prev-bol (point)))
1451 (not (or (= xmltok-start (point))
1452 (eq xmltok-type 'data))))))
1454 ;; Now scan over tokens until the end of the line to be indented.
1455 ;; Determine the context before and after the beginning of the
1457 (while (< (point) eol)
1458 (nxml-tokenize-forward)
1459 (cond ((<= bol xmltok-start)
1461 (nxml-merge-indent-context-type after-context)))
1462 ((and (<= (point) bol)
1463 (not (and (eq xmltok-type 'partial-start-tag)
1465 (setq before-context
1466 (nxml-merge-indent-context-type before-context)))
1467 ((eq xmltok-type 'data)
1468 (setq before-context
1469 (nxml-merge-indent-context-type before-context))
1471 (nxml-merge-indent-context-type after-context)))
1472 ;; If in the middle of a token that looks inline,
1473 ;; then indent relative to the previous non-blank line
1474 ((eq (nxml-merge-indent-context-type before-context)
1476 (goto-char prev-bol)
1477 (throw 'indent (current-column)))
1480 (nxml-compute-indent-in-token bol))))
1481 (skip-chars-forward " \t\r\n"))
1484 (* nxml-child-indent
1485 (+ (if (eq before-context 'start-tag) 1 0)
1486 (if (eq after-context 'end-tag) -1 0))))))))
1488 (defun nxml-merge-indent-context-type (context)
1489 "Merge the indent context type CONTEXT with the token in `xmltok-type'.
1490 Return the merged indent context type. An indent context type is
1491 either nil or one of the symbols `start-tag', `end-tag', `markup',
1492 `comment', `mixed'."
1493 (cond ((memq xmltok-type '(start-tag partial-start-tag))
1494 (if (memq context '(nil start-tag comment))
1497 ((memq xmltok-type '(end-tag partial-end-tag))
1498 (if (memq context '(nil end-tag comment))
1501 ((eq xmltok-type 'comment)
1502 (cond ((memq context '(start-tag end-tag comment))
1509 (defun nxml-compute-indent-in-token (pos)
1510 "Return the indent for a line that starts inside a token.
1511 POS is the position of the first non-whitespace character of the line.
1512 This expects the xmltok-* variables to be set up as by `xmltok-forward'."
1513 (cond ((memq xmltok-type '(start-tag
1516 partial-empty-element))
1517 (nxml-compute-indent-in-start-tag pos))
1518 ((eq xmltok-type 'comment)
1519 (nxml-compute-indent-in-delimited-token pos "<!--" "-->"))
1520 ((eq xmltok-type 'cdata-section)
1521 (nxml-compute-indent-in-delimited-token pos "<![CDATA[" "]]>"))
1522 ((eq xmltok-type 'processing-instruction)
1523 (nxml-compute-indent-in-delimited-token pos "<?" "?>"))
1526 (if (and (= (forward-line -1) 0)
1527 (< xmltok-start (point)))
1528 (back-to-indentation)
1529 (goto-char xmltok-start))
1532 (defun nxml-compute-indent-in-start-tag (pos)
1533 "Return the indent for a line that starts inside a start-tag.
1534 Also for a line that starts inside an empty element.
1535 POS is the position of the first non-whitespace character of the line.
1536 This expects the xmltok-* variables to be set up as by `xmltok-forward'."
1537 (let ((value-boundary (nxml-attribute-value-boundary pos))
1540 ;; inside an attribute value
1541 (let ((value-start (car value-boundary))
1542 (value-end (cdr value-boundary)))
1545 (if (< (point) value-start)
1546 (goto-char value-start)
1547 (back-to-indentation)))
1548 ;; outside an attribute value
1550 (while (and (= (forward-line -1) 0)
1551 (nxml-attribute-value-boundary (point))))
1552 (cond ((<= (point) xmltok-start)
1553 (goto-char xmltok-start)
1554 (setq off nxml-attribute-indent)
1555 (let ((atts (xmltok-merge-attributes)))
1557 (let* ((att (car atts))
1558 (start (xmltok-attribute-name-start att)))
1563 (back-to-indentation))))
1564 (+ (current-column) off)))
1566 (defun nxml-attribute-value-boundary (pos)
1567 "Return a pair (START . END) if POS is inside an attribute value.
1568 Otherwise return nil. START and END are the positions of the start
1569 and end of the attribute value containing POS. This expects the
1570 xmltok-* variables to be set up as by `xmltok-forward'."
1571 (let ((atts (xmltok-merge-attributes))
1572 att value-start value-end value-boundary)
1574 (setq att (car atts))
1575 (setq value-start (xmltok-attribute-value-start att))
1576 (setq value-end (xmltok-attribute-value-end att))
1577 (cond ((and value-start (< pos value-start))
1579 ((and value-start value-end (<= pos value-end))
1580 (setq value-boundary (cons value-start value-end))
1582 (t (setq atts (cdr atts)))))
1585 (defun nxml-compute-indent-in-delimited-token (pos open-delim close-delim)
1586 "Return the indent for a line that starts inside a token with delimiters.
1587 OPEN-DELIM and CLOSE-DELIM are strings giving the opening and closing
1588 delimiters. POS is the position of the first non-whitespace character
1589 of the line. This expects the xmltok-* variables to be set up as by
1591 (cond ((let ((end (+ pos (length close-delim))))
1592 (and (<= end (point-max))
1593 (string= (buffer-substring-no-properties pos end)
1595 (goto-char xmltok-start))
1599 (<= (point) xmltok-start))
1600 (goto-char (+ xmltok-start (length open-delim)))
1601 (when (and (string= open-delim "<!--")
1603 (goto-char (1+ (point)))))
1604 (t (back-to-indentation)))
1609 (defun nxml-complete ()
1610 "Perform completion on the symbol preceding point.
1612 Inserts as many characters as can be completed. However, if not even
1613 one character can be completed, then a buffer with the possibilities
1614 is popped up and the symbol is read from the minibuffer with
1615 completion. If the symbol is complete, then any characters that must
1616 follow the symbol are also inserted.
1618 The name space used for completion and what is treated as a symbol
1619 depends on the context. The contexts in which completion is performed
1620 depend on `nxml-completion-hook'."
1622 (unless (run-hook-with-args-until-success 'nxml-completion-hook)
1623 ;; Eventually we will complete on entity names here.
1625 (message "Cannot complete in this context")))
1629 (defun nxml-forward-balanced-item (&optional arg)
1630 "Move forward across one balanced item.
1631 With ARG, do it that many times. Negative arg -N means
1632 move backward across N balanced expressions.
1633 This is the equivalent of `forward-sexp' for XML.
1635 An element contains as items strings with no markup, tags, processing
1636 instructions, comments, CDATA sections, entity references and
1637 characters references. However, if the variable
1638 `nxml-sexp-element-flag' is non-nil, then an element is treated as a
1639 single markup item. A start-tag contains an element name followed by
1640 one or more attributes. An end-tag contains just an element name.
1641 An attribute value literals contains strings with no markup, entity
1642 references and character references. A processing instruction
1643 consists of a target and a content string. A comment or a CDATA
1644 section contains a single string. An entity reference contains a
1645 single name. A character reference contains a character number."
1647 (or arg (setq arg 1))
1650 (nxml-forward-single-balanced-item)
1651 (> (setq arg (1- arg)) 0))))
1654 (nxml-backward-single-balanced-item)
1655 (< (setq arg (1+ arg)) 0))))))
1657 (defun nxml-forward-single-balanced-item ()
1659 (goto-char (let ((end (nxml-token-after)))
1661 (while (eq xmltok-type 'space)
1663 (setq end (nxml-token-after)))
1664 (cond ((/= (point) xmltok-start)
1665 (nxml-scan-forward-within end))
1666 ((and nxml-sexp-element-flag
1667 (eq xmltok-type 'start-tag))
1668 ;; can't ever return nil here
1669 (nxml-scan-element-forward xmltok-start))
1670 ((and nxml-sexp-element-flag
1672 '(end-tag partial-end-tag)))
1673 (error "Already at end of element"))
1676 (goto-char (cadr err))
1677 (apply 'error (cddr err)))))
1679 (defun nxml-backward-single-balanced-item ()
1681 (goto-char (let ((end (nxml-token-before)))
1683 (while (eq xmltok-type 'space)
1684 (goto-char xmltok-start)
1685 (setq end (nxml-token-before)))
1686 (cond ((/= (point) end)
1687 (nxml-scan-backward-within end))
1688 ((and nxml-sexp-element-flag
1689 (eq xmltok-type 'end-tag))
1690 ;; can't ever return nil here
1691 (nxml-scan-element-backward end)
1693 ((and nxml-sexp-element-flag
1694 (eq xmltok-type 'start-tag))
1695 (error "Already at start of element"))
1696 (t xmltok-start)))))
1698 (goto-char (cadr err))
1699 (apply 'error (cddr err)))))
1701 (defun nxml-scan-forward-within (end)
1702 (setq end (- end (nxml-end-delimiter-length xmltok-type)))
1703 (when (<= end (point))
1704 (error "Already at end of %s"
1705 (nxml-token-type-friendly-name xmltok-type)))
1706 (cond ((memq xmltok-type '(start-tag
1709 partial-empty-element))
1710 (if (< (point) xmltok-name-end)
1712 (let ((att (nxml-find-following-attribute)))
1713 (cond ((not att) end)
1714 ((and (xmltok-attribute-value-start att)
1715 (<= (xmltok-attribute-value-start att)
1717 (nxml-scan-forward-in-attribute-value att))
1718 ((xmltok-attribute-value-end att)
1719 (1+ (xmltok-attribute-value-end att)))
1721 (goto-char (xmltok-attribute-name-end att))
1722 (looking-at "[ \t\r\n]*="))
1724 (t (xmltok-attribute-name-end att))))))
1725 ((and (eq xmltok-type 'processing-instruction)
1726 (< (point) xmltok-name-end))
1730 (defun nxml-scan-backward-within (end)
1733 (nxml-start-delimiter-length xmltok-type)))
1734 (when (<= (point) xmltok-start)
1735 (error "Already at start of %s"
1736 (nxml-token-type-friendly-name xmltok-type)))
1737 (cond ((memq xmltok-type '(start-tag
1740 partial-empty-element))
1741 (let ((att (nxml-find-preceding-attribute)))
1742 (cond ((not att) xmltok-start)
1743 ((and (xmltok-attribute-value-start att)
1744 (<= (xmltok-attribute-value-start att)
1747 (xmltok-attribute-value-end att)))
1748 (nxml-scan-backward-in-attribute-value att))
1749 (t (xmltok-attribute-name-start att)))))
1750 ((and (eq xmltok-type 'processing-instruction)
1751 (let ((content-start (save-excursion
1752 (goto-char xmltok-name-end)
1753 (skip-chars-forward " \r\t\n")
1755 (and (< content-start (point))
1759 (defun nxml-scan-forward-in-attribute-value (att)
1760 (when (= (point) (xmltok-attribute-value-end att))
1761 (error "Already at end of attribute value"))
1762 (let ((refs (xmltok-attribute-refs att))
1765 (setq ref (car refs))
1766 (if (< (point) (aref ref 2))
1769 (setq refs (cdr refs))))
1771 (xmltok-attribute-value-end att))
1772 ((< (point) (aref ref 1))
1774 ((= (point) (aref ref 1))
1777 (let ((end (- (aref ref 2)
1778 (nxml-end-delimiter-length (aref ref 0)))))
1781 (error "Already at end of %s"
1782 (nxml-token-type-friendly-name (aref ref 0)))))))))
1784 (defun nxml-scan-backward-in-attribute-value (att)
1785 (when (= (point) (xmltok-attribute-value-start att))
1786 (error "Already at start of attribute value"))
1787 (let ((refs (reverse (xmltok-attribute-refs att)))
1790 (setq ref (car refs))
1791 (if (< (aref ref 1) (point))
1794 (setq refs (cdr refs))))
1796 (xmltok-attribute-value-start att))
1797 ((< (aref ref 2) (point))
1799 ((= (point) (aref ref 2))
1802 (let ((start (+ (aref ref 1)
1803 (nxml-start-delimiter-length (aref ref 0)))))
1804 (if (< start (point))
1806 (error "Already at start of %s"
1807 (nxml-token-type-friendly-name (aref ref 0)))))))))
1809 (defun nxml-find-following-attribute ()
1811 (atts (or xmltok-attributes xmltok-namespace-attributes))
1812 (more-atts (and xmltok-attributes xmltok-namespace-attributes)))
1814 (let* ((att (car atts))
1815 (name-start (xmltok-attribute-name-start att)))
1816 (cond ((and (<= name-start (point))
1817 (xmltok-attribute-value-end att)
1818 ;; <= because end is before quote
1819 (<= (point) (xmltok-attribute-value-end att)))
1822 ((and (< (point) name-start)
1825 (xmltok-attribute-name-start ret))))
1827 (setq atts (cdr atts))
1829 (setq atts more-atts)
1830 (setq more-atts nil)))
1833 (defun nxml-find-preceding-attribute ()
1835 (atts (or xmltok-attributes xmltok-namespace-attributes))
1836 (more-atts (and xmltok-attributes xmltok-namespace-attributes)))
1838 (let* ((att (car atts))
1839 (name-start (xmltok-attribute-name-start att)))
1840 (cond ((and (< name-start (point))
1841 (xmltok-attribute-value-end att)
1842 ;; <= because end is before quote
1843 (<= (point) (xmltok-attribute-value-end att)))
1846 ((and (< name-start (point))
1848 (< (xmltok-attribute-name-start ret)
1851 (setq atts (cdr atts))
1853 (setq atts more-atts)
1854 (setq more-atts nil)))
1857 (defun nxml-up-element (&optional arg)
1859 (or arg (setq arg 1))
1861 (nxml-backward-up-element (- arg))
1863 (while (and (> arg 0)
1864 (< (point) (point-max)))
1865 (let ((token-end (nxml-token-after)))
1866 (goto-char (cond ((or (memq xmltok-type '(end-tag
1868 (and (memq xmltok-type
1870 partial-empty-element))
1871 (< xmltok-start (point))))
1873 ((nxml-scan-element-forward
1874 (if (and (eq xmltok-type 'start-tag)
1875 (= (point) xmltok-start))
1879 (t (error "No parent element")))))
1880 (setq arg (1- arg)))
1882 (goto-char (cadr err))
1883 (apply 'error (cddr err))))))
1885 (defun nxml-backward-up-element (&optional arg)
1887 (or arg (setq arg 1))
1889 (nxml-up-element (- arg))
1891 (while (and (> arg 0)
1892 (< (point-min) (point)))
1893 (let ((token-end (nxml-token-before)))
1894 (goto-char (cond ((or (memq xmltok-type '(start-tag
1896 (and (memq xmltok-type
1898 partial-empty-element))
1899 (< (point) token-end)))
1901 ((nxml-scan-element-backward
1902 (if (and (eq xmltok-type 'end-tag)
1903 (= (point) token-end))
1908 (t (error "No parent element")))))
1909 (setq arg (1- arg)))
1911 (goto-char (cadr err))
1912 (apply 'error (cddr err))))))
1914 (defun nxml-down-element (&optional arg)
1915 "Move forward down into the content of an element.
1916 With ARG, do this that many times.
1917 Negative ARG means move backward but still down."
1919 (or arg (setq arg 1))
1921 (nxml-backward-down-element (- arg))
1924 (let ((token-end (nxml-token-after)))
1926 (goto-char token-end)
1928 (when (memq xmltok-type '(nil end-tag partial-end-tag))
1929 (error "No following start-tags in this element"))
1930 (not (memq xmltok-type '(start-tag partial-start-tag))))
1931 (nxml-tokenize-forward))
1933 (setq arg (1- arg)))))
1935 (defun nxml-backward-down-element (&optional arg)
1937 (or arg (setq arg 1))
1939 (nxml-down-element (- arg))
1944 (goto-char xmltok-start)
1946 (when (memq xmltok-type '(start-tag
1950 (error "No preceding end-tags in this element"))
1951 (not (memq xmltok-type '(end-tag partial-end-tag))))
1952 (if (or (<= (point) nxml-prolog-end)
1953 (not (search-backward "<" nxml-prolog-end t)))
1954 (setq xmltok-type nil)
1955 (nxml-move-outside-backwards)
1958 (setq arg (1- arg)))))
1960 (defun nxml-forward-element (&optional arg)
1961 "Move forward over one element.
1962 With ARG, do it that many times.
1963 Negative ARG means move backward."
1965 (or arg (setq arg 1))
1967 (nxml-backward-element (- arg))
1969 (while (and (> arg 0)
1970 (< (point) (point-max)))
1972 (or (nxml-scan-element-forward (nxml-token-before))
1973 (error "No more elements")))
1974 (setq arg (1- arg)))
1976 (goto-char (cadr err))
1977 (apply 'error (cddr err))))))
1979 (defun nxml-backward-element (&optional arg)
1980 "Move backward over one element.
1981 With ARG, do it that many times.
1982 Negative ARG means move forward."
1984 (or arg (setq arg 1))
1986 (nxml-forward-element (- arg))
1988 (while (and (> arg 0)
1989 (< (point-min) (point)))
1991 (or (and (nxml-scan-element-backward (progn
1995 (error "No preceding elements")))
1996 (setq arg (1- arg)))
1998 (goto-char (cadr err))
1999 (apply 'error (cddr err))))))
2001 (defun nxml-mark-token-after ()
2003 (push-mark (nxml-token-after) nil t)
2004 (goto-char xmltok-start)
2005 (message "Marked %s" xmltok-type))
2009 (defun nxml-mark-paragraph ()
2010 "Put point at beginning of this paragraph, mark at end.
2011 The paragraph marked is the one that contains point or follows point."
2013 (nxml-forward-paragraph)
2015 (nxml-backward-paragraph))
2017 (defun nxml-forward-paragraph (&optional arg)
2019 (or arg (setq arg 1))
2021 (nxml-backward-paragraph (- arg)))
2024 (while (and (nxml-forward-single-paragraph)
2025 (> (setq arg (1- arg)) 0))))))
2027 (defun nxml-backward-paragraph (&optional arg)
2029 (or arg (setq arg 1))
2031 (nxml-forward-paragraph (- arg)))
2034 (let ((inhibit-field-text-motion t))
2036 (while (and (nxml-backward-single-paragraph)
2037 (> (setq arg (1- arg)) 0))))))
2039 (defun nxml-forward-single-paragraph ()
2040 "Move forward over a single paragraph.
2041 Return nil at end of buffer, t otherwise."
2042 (let* ((token-end (nxml-token-after))
2043 (offset (- (point) xmltok-start))
2045 (goto-char token-end)
2046 (while (and (< (point) (point-max))
2048 (nxml-paragraph-end-pos had-data offset))))
2049 (when (nxml-token-contains-data-p offset)
2051 (nxml-tokenize-forward)
2053 (when pos (goto-char pos))))
2055 (defun nxml-backward-single-paragraph ()
2056 "Move backward over a single paragraph.
2057 Return nil at start of buffer, t otherwise."
2058 (let* ((token-end (nxml-token-before))
2059 (offset (- token-end (point)))
2060 (last-tag-pos xmltok-start)
2061 pos had-data last-data-pos)
2062 (goto-char token-end)
2063 (unless (setq pos (nxml-paragraph-start-pos nil offset))
2064 (setq had-data (nxml-token-contains-data-p nil offset))
2065 (goto-char xmltok-start)
2066 (while (and (not pos) (< (point-min) (point)))
2067 (cond ((search-backward "<" nxml-prolog-end t)
2068 (nxml-move-outside-backwards)
2070 (while (< (point) last-tag-pos)
2072 (when (and (not had-data) (nxml-token-contains-data-p))
2074 (setq last-data-pos xmltok-start))
2075 (let ((tem (nxml-paragraph-start-pos had-data 0)))
2076 (when tem (setq pos tem)))))
2077 (when (and (not had-data) last-data-pos (not pos))
2080 (while (< (point) last-data-pos)
2082 (let ((tem (nxml-paragraph-start-pos had-data 0)))
2083 (when tem (setq pos tem)))))
2084 (setq last-tag-pos (point)))
2085 (t (goto-char (point-min))))))
2086 (when pos (goto-char pos))))
2088 (defun nxml-token-contains-data-p (&optional start end)
2089 (setq start (+ xmltok-start (or start 0)))
2090 (setq end (- (point) (or end 0)))
2091 (when (eq xmltok-type 'cdata-section)
2092 (setq start (max start (+ xmltok-start 9)))
2093 (setq end (min end (- (point) 3))))
2094 (or (and (eq xmltok-type 'data)
2095 (eq start xmltok-start)
2097 (eq xmltok-type 'char-ref)
2098 (and (memq xmltok-type '(data cdata-section))
2102 (re-search-forward "[^ \t\r\n]" end t)))))
2104 (defun nxml-paragraph-end-pos (had-data offset)
2105 "Return the position of the paragraph end if contained in the current token.
2106 Return nil if the current token does not contain the paragraph end.
2107 Only characters after OFFSET from the start of the token are eligible.
2108 HAD-DATA says whether there have been non-whitespace data characters yet."
2109 (cond ((not had-data)
2110 (cond ((memq xmltok-type '(data cdata-section))
2112 (let ((end (point)))
2113 (goto-char (+ xmltok-start
2114 (max (if (eq xmltok-type 'cdata-section)
2118 (and (re-search-forward "[^ \t\r\n]" end t)
2119 (re-search-forward "^[ \t]*$" end t)
2120 (match-beginning 0)))))
2121 ((and (eq xmltok-type 'comment)
2122 (nxml-token-begins-line-p)
2123 (nxml-token-ends-line-p))
2125 (let ((end (point)))
2126 (goto-char (+ xmltok-start (max 4 offset)))
2127 (when (re-search-forward "[^ \t\r\n]" (- end 3) t)
2128 (if (re-search-forward "^[ \t]*$" end t)
2130 (goto-char (- end 3))
2131 (skip-chars-backward " \t")
2133 (beginning-of-line 2))
2135 ((memq xmltok-type '(data space cdata-section))
2137 (let ((end (point)))
2138 (goto-char (+ xmltok-start offset))
2139 (and (re-search-forward "^[ \t]*$" end t)
2140 (match-beginning 0)))))
2141 ((and (memq xmltok-type '(start-tag
2145 processing-instruction
2147 (nxml-token-begins-line-p)
2148 (nxml-token-ends-line-p))
2150 (goto-char xmltok-start)
2151 (skip-chars-backward " \t")
2153 ((and (eq xmltok-type 'end-tag)
2154 (looking-at "[ \t]*$")
2155 (not (nxml-in-mixed-content-p t)))
2157 (or (search-forward "\n" nil t)
2160 (defun nxml-paragraph-start-pos (had-data offset)
2161 "Return the position of the paragraph start if contained in the current token.
2162 Return nil if the current token does not contain the paragraph start.
2163 Only characters before OFFSET from the end of the token are eligible.
2164 HAD-DATA says whether there have been non-whitespace data characters yet."
2165 (cond ((not had-data)
2166 (cond ((memq xmltok-type '(data cdata-section))
2168 (goto-char (- (point)
2169 (max (if (eq xmltok-type 'cdata-section)
2173 (and (re-search-backward "[^ \t\r\n]" xmltok-start t)
2174 (re-search-backward "^[ \t]*$" xmltok-start t)
2175 (match-beginning 0))))
2176 ((and (eq xmltok-type 'comment)
2177 (nxml-token-ends-line-p)
2178 (nxml-token-begins-line-p))
2180 (goto-char (- (point) (max 3 offset)))
2181 (when (and (< (+ xmltok-start 4) (point))
2182 (re-search-backward "[^ \t\r\n]"
2185 (if (re-search-backward "^[ \t]*$" xmltok-start t)
2187 (goto-char xmltok-start)
2188 (if (looking-at "<!--[ \t]*\n")
2190 (skip-chars-backward " \t")
2192 ((memq xmltok-type '(data space cdata-section))
2194 (goto-char (- (point) offset))
2195 (and (re-search-backward "^[ \t]*$" xmltok-start t)
2196 (match-beginning 0))))
2197 ((and (memq xmltok-type '(start-tag
2201 processing-instruction
2203 (nxml-token-ends-line-p)
2204 (nxml-token-begins-line-p))
2205 (or (search-forward "\n" nil t)
2207 ((and (eq xmltok-type 'start-tag)
2208 (nxml-token-begins-line-p)
2209 (not (save-excursion
2210 (goto-char xmltok-start)
2211 (nxml-in-mixed-content-p nil))))
2213 (goto-char xmltok-start)
2214 (skip-chars-backward " \t")
2215 ;; include any blank line before
2216 (or (and (eq (char-before) ?\n)
2218 (goto-char (1- (point)))
2219 (skip-chars-backward " \t")
2220 (and (bolp) (point))))
2223 (defun nxml-token-ends-line-p () (looking-at "[ \t]*$"))
2225 (defun nxml-token-begins-line-p ()
2227 (goto-char xmltok-start)
2228 (skip-chars-backward " \t")
2231 (defun nxml-in-mixed-content-p (endp)
2232 "Return non-nil if point is in mixed content.
2233 Point must be after an end-tag or before a start-tag.
2234 ENDP is t in the former case, nil in the latter."
2235 (let (matching-tag-pos)
2236 (cond ((not (run-hook-with-args-until-failure
2237 'nxml-in-mixed-content-hook))
2239 ;; See if the matching tag does not start or end a line.
2240 ((condition-case err
2242 (setq matching-tag-pos
2245 (and (nxml-scan-element-backward (point))
2247 (nxml-scan-element-forward (point)))))
2248 (and matching-tag-pos
2250 (goto-char matching-tag-pos)
2253 (skip-chars-backward " \t")
2255 (looking-at "[ \t]*$"))))))
2256 (nxml-scan-error nil))
2258 ;; See if there's data at the same level.
2261 (setq start matching-tag-pos
2264 end matching-tag-pos))
2268 (nxml-preceding-sibling-data-p))
2271 (nxml-following-sibling-data-p)))))
2273 ;; Otherwise, treat as not mixed
2276 (defun nxml-preceding-sibling-data-p ()
2277 "Return non-nil if there is a previous sibling that is data."
2278 (let ((lim (max (- (point) nxml-mixed-scan-distance)
2284 (while (and (< lim (point))
2289 (search-backward "<" lim t)))
2290 (nxml-move-outside-backwards)
2293 (let ((prev-level level))
2294 (cond ((eq xmltok-type 'end-tag)
2295 (setq level (1+ level)))
2296 ((eq xmltok-type 'start-tag)
2297 (setq level (1- level))))
2298 (when (eq prev-level 0)
2299 (while (and (< (point) end) (not found))
2301 (when (memq xmltok-type '(data cdata-section char-ref))
2302 (setq found t)))))))))
2305 (defun nxml-following-sibling-data-p ()
2306 (let ((lim (min (+ (point) nxml-mixed-scan-distance)
2312 (while (and (< (point) lim)
2314 (nxml-tokenize-forward)
2316 (cond ((eq xmltok-type 'start-tag)
2317 (setq level (1+ level)))
2318 ((eq xmltok-type 'end-tag)
2319 (setq level (1- level)))
2321 (memq xmltok-type '(data cdata-section char-ref)))
2327 (defun nxml-do-fill-paragraph (arg)
2328 (let (fill-paragraph-function
2332 (nxml-forward-paragraph)
2334 (nxml-backward-paragraph)
2335 (skip-chars-forward " \t\r\n")
2336 (setq start (point))
2338 (setq fill-prefix (buffer-substring-no-properties (point) start))
2339 (when (and (not (nxml-get-inside (point)))
2340 (looking-at "[ \t]*<!--"))
2341 (setq fill-prefix (concat fill-prefix " ")))
2342 (fill-region-as-paragraph start end arg))
2343 (skip-line-prefix fill-prefix)
2346 (defun nxml-newline-and-indent (soft)
2347 (delete-horizontal-space)
2348 (if soft (insert-and-inherit ?\n) (newline 1))
2354 (defvar nxml-dynamic-markup-prev-pos nil)
2355 (defvar nxml-dynamic-markup-prev-lengths nil)
2356 (defvar nxml-dynamic-markup-prev-found-marker nil)
2357 (defvar nxml-dynamic-markup-prev-start-tags (make-hash-table :test 'equal))
2359 (defun nxml-dynamic-markup-word ()
2360 "Dynamically markup the word before point.
2361 This attempts to find a tag to put around the word before point based
2362 on the contents of the current buffer. The end-tag will be inserted at
2363 point. The start-tag will be inserted at or before the beginning of
2364 the word before point; the contents of the current buffer is used to
2367 It works in a similar way to \\[dabbrev-expand]. It searches first
2368 backwards from point, then forwards from point for an element whose
2369 content is a string which matches the contents of the buffer before
2370 point and which includes at least the word before point. It then
2371 copies the start- and end-tags from that element and uses them to
2372 surround the matching string before point.
2374 Repeating \\[nxml-dynamic-markup-word] immediately after successful
2375 \\[nxml-dynamic-markup-word] removes the previously inserted markup
2376 and attempts to find another possible way to do the markup."
2378 (let (search-start-pos done)
2379 (if (and (integerp nxml-dynamic-markup-prev-pos)
2380 (= nxml-dynamic-markup-prev-pos (point))
2381 (eq last-command this-command)
2382 nxml-dynamic-markup-prev-lengths)
2383 (let* ((end-tag-open-pos
2384 (- nxml-dynamic-markup-prev-pos
2385 (nth 2 nxml-dynamic-markup-prev-lengths)))
2386 (start-tag-close-pos
2388 (nth 1 nxml-dynamic-markup-prev-lengths)))
2390 (- start-tag-close-pos
2391 (nth 0 nxml-dynamic-markup-prev-lengths))))
2392 (delete-region end-tag-open-pos nxml-dynamic-markup-prev-pos)
2393 (delete-region start-tag-open-pos start-tag-close-pos)
2394 (setq search-start-pos
2395 (marker-position nxml-dynamic-markup-prev-found-marker)))
2396 (clrhash nxml-dynamic-markup-prev-start-tags))
2397 (setq nxml-dynamic-markup-prev-pos nil)
2398 (setq nxml-dynamic-markup-prev-lengths nil)
2399 (setq nxml-dynamic-markup-prev-found-marker nil)
2402 (let* ((pos (point))
2405 (unless (< (point) pos)
2406 (error "No word to markup"))
2407 (buffer-substring-no-properties (point) pos)))
2408 (search (concat word "</"))
2410 (when search-start-pos
2411 (goto-char search-start-pos))
2412 (while (and (not done)
2413 (or (and (< (point) pos)
2414 (or (search-backward search nil t)
2415 (progn (goto-char pos) nil)))
2416 (search-forward search nil t)))
2417 (goto-char (- (match-end 0) 2))
2418 (setq done (nxml-try-copy-markup pos)))
2420 (error (if (zerop (hash-table-count
2421 nxml-dynamic-markup-prev-start-tags))
2422 "No possible markup found for `%s'"
2423 "No more markup possibilities found for `%s'")
2426 (defun nxml-try-copy-markup (word-end-pos)
2428 (let ((end-tag-pos (point)))
2429 (when (and (not (nxml-get-inside end-tag-pos))
2430 (search-backward "<" nil t)
2431 (not (nxml-get-inside (point))))
2433 (when (and (eq xmltok-type 'start-tag)
2434 (< (point) end-tag-pos))
2435 (let* ((start-tag-close-pos (point))
2437 (buffer-substring-no-properties xmltok-start
2438 start-tag-close-pos))
2442 (buffer-substring-no-properties start-tag-close-pos
2445 (goto-char word-end-pos)
2447 (re-search-backward (concat
2448 (regexp-quote (car words))
2452 (setq words (cdr words))
2453 (skip-chars-backward " \t\r\n"))
2454 (when (and (not words)
2456 (skip-chars-forward " \t\r\n")
2457 (not (gethash (cons (point) start-tag)
2458 nxml-dynamic-markup-prev-start-tags)))
2459 (or (< end-tag-pos (point))
2460 (< word-end-pos xmltok-start)))
2461 (setq nxml-dynamic-markup-prev-found-marker
2462 (copy-marker end-tag-pos t))
2463 (puthash (cons (point) start-tag)
2465 nxml-dynamic-markup-prev-start-tags)
2466 (setq nxml-dynamic-markup-prev-lengths
2467 (list (- start-tag-close-pos xmltok-start)
2468 (- word-end-pos (point))
2469 (+ (- xmltok-name-end xmltok-start) 2)))
2470 (let ((name (xmltok-start-tag-qname)))
2472 (goto-char (+ word-end-pos
2473 (- start-tag-close-pos xmltok-start)))
2474 (insert "</" name ">")
2475 (setq nxml-dynamic-markup-prev-pos (point))))))))))
2480 (defvar nxml-char-name-ignore-case t)
2482 (defvar nxml-char-name-alist nil
2483 "Alist of character names.
2484 Each member of the list has the form (NAME CODE . NAMESET),
2485 where NAME is a string naming a character, NAMESET is a symbol
2486 identifying a set of names and CODE is an integer specifying the
2487 Unicode scalar value of the named character.
2488 The NAME will only be used for completion if NAMESET has
2489 a non-nil `nxml-char-name-set-enabled' property.
2490 If NAMESET does does not have `nxml-char-name-set-defined' property,
2491 then it must have a `nxml-char-name-set-file' property and `load'
2492 will be applied to the value of this property if the nameset
2495 (defvar nxml-char-name-table (make-hash-table :test 'eq)
2496 "Hash table for mapping char codes to names.
2497 Each key is a Unicode scalar value.
2498 Each value is a list of pairs of the form (NAMESET . NAME),
2499 where NAMESET is a symbol identifying a set of names,
2500 and NAME is a string naming a character.")
2502 (defvar nxml-autoload-char-name-set-list nil
2503 "List of char namesets that can be autoloaded.")
2505 (defun nxml-enable-char-name-set (nameset)
2506 (put nameset 'nxml-char-name-set-enabled t))
2508 (defun nxml-disable-char-name-set (nameset)
2509 (put nameset 'nxml-char-name-set-enabled nil))
2511 (defun nxml-char-name-set-enabled-p (nameset)
2512 (get nameset 'nxml-char-name-set-enabled))
2514 (defun nxml-autoload-char-name-set (nameset file)
2515 (unless (memq nameset nxml-autoload-char-name-set-list)
2516 (setq nxml-autoload-char-name-set-list
2517 (cons nameset nxml-autoload-char-name-set-list)))
2518 (put nameset 'nxml-char-name-set-file file))
2520 (defun nxml-define-char-name-set (nameset alist)
2521 "Define a set of character names.
2522 NAMESET is a symbol identifying the set.
2523 ALIST is a list where each member has the form (NAME CODE),
2524 where NAME is a string naming a character and code is an
2525 integer giving the Unicode scalar value of the character."
2526 (when (get nameset 'nxml-char-name-set-defined)
2527 (error "Nameset `%s' already defined" nameset))
2530 (let* ((name-code (car iter))
2531 (name (car name-code))
2532 (code (cadr name-code)))
2534 (cons (cons nameset name)
2535 (gethash code nxml-char-name-table))
2536 nxml-char-name-table))
2537 (setcdr (cdr (car iter)) nameset)
2538 (setq iter (cdr iter))))
2539 (setq nxml-char-name-alist
2540 (nconc alist nxml-char-name-alist))
2541 (put nameset 'nxml-char-name-set-defined t))
2543 (defun nxml-get-char-name (code)
2544 (mapc 'nxml-maybe-load-char-name-set nxml-autoload-char-name-set-list)
2545 (let ((names (gethash code nxml-char-name-table))
2547 (while (and names (not name))
2548 (if (nxml-char-name-set-enabled-p (caar names))
2549 (setq name (cdar names))
2550 (setq names (cdr names))))
2553 (defvar nxml-named-char-history nil)
2555 (defun nxml-insert-named-char (arg)
2556 "Insert a character using its name.
2557 The name is read from the minibuffer.
2558 Normally, inserts the character as a numeric character reference.
2559 With a prefix argument, inserts the character directly."
2561 (mapc 'nxml-maybe-load-char-name-set nxml-autoload-char-name-set-list)
2563 (let ((completion-ignore-case nxml-char-name-ignore-case))
2564 (completing-read "Character name: "
2565 nxml-char-name-alist
2567 (get (cddr member) 'nxml-char-name-set-enabled))
2570 'nxml-named-char-history)))
2571 (alist nxml-char-name-alist)
2573 (while (and alist (not code))
2574 (setq elt (assoc name alist))
2575 (if (get (cddr elt) 'nxml-char-name-set-enabled)
2576 (setq code (cadr elt))
2577 (setq alist (cdr (member elt alist)))))
2580 (or (decode-char 'ucs code)
2581 (error "Character %x is not supported by Emacs"
2583 (format "&#x%X;" code))))))
2585 (defun nxml-maybe-load-char-name-set (sym)
2586 (when (and (get sym 'nxml-char-name-set-enabled)
2587 (not (get sym 'nxml-char-name-set-defined))
2588 (stringp (get sym 'nxml-char-name-set-file)))
2589 (load (get sym 'nxml-char-name-set-file))))
2591 (defun nxml-toggle-char-ref-extra-display (arg)
2592 "Toggle the display of extra information for character references."
2594 (let ((new (if (null arg)
2595 (not nxml-char-ref-extra-display)
2596 (> (prefix-numeric-value arg) 0))))
2597 (when (not (eq new nxml-char-ref-extra-display))
2598 (setq nxml-char-ref-extra-display new)
2599 (font-lock-fontify-buffer))))
2601 (put 'nxml-char-ref 'evaporate t)
2603 (defun nxml-char-ref-display-extra (start end n)
2604 (when nxml-char-ref-extra-display
2605 (let ((name (nxml-get-char-name n))
2606 (glyph-string (and nxml-char-ref-display-glyph-flag
2607 (nxml-glyph-display-string n 'nxml-glyph)))
2609 (when (or name glyph-string)
2610 (setq ov (make-overlay start end nil t))
2611 (overlay-put ov 'category 'nxml-char-ref)
2613 (overlay-put ov 'help-echo name))
2617 (propertize glyph-string 'face 'nxml-glyph)))))))
2619 (defun nxml-clear-char-ref-extra-display (start end)
2620 (let ((ov (overlays-in start end)))
2622 (when (eq (overlay-get (car ov) 'category) 'nxml-char-ref)
2623 (delete-overlay (car ov)))
2624 (setq ov (cdr ov)))))
2627 (defun nxml-start-delimiter-length (type)
2628 (or (get type 'nxml-start-delimiter-length)
2631 (put 'cdata-section 'nxml-start-delimiter-length 9)
2632 (put 'comment 'nxml-start-delimiter-length 4)
2633 (put 'processing-instruction 'nxml-start-delimiter-length 2)
2634 (put 'start-tag 'nxml-start-delimiter-length 1)
2635 (put 'empty-element 'nxml-start-delimiter-length 1)
2636 (put 'partial-empty-element 'nxml-start-delimiter-length 1)
2637 (put 'entity-ref 'nxml-start-delimiter-length 1)
2638 (put 'char-ref 'nxml-start-delimiter-length 2)
2640 (defun nxml-end-delimiter-length (type)
2641 (or (get type 'nxml-end-delimiter-length)
2644 (put 'cdata-section 'nxml-end-delimiter-length 3)
2645 (put 'comment 'nxml-end-delimiter-length 3)
2646 (put 'processing-instruction 'nxml-end-delimiter-length 2)
2647 (put 'start-tag 'nxml-end-delimiter-length 1)
2648 (put 'empty-element 'nxml-end-delimiter-length 2)
2649 (put 'partial-empty-element 'nxml-end-delimiter-length 1)
2650 (put 'entity-ref 'nxml-end-delimiter-length 1)
2651 (put 'char-ref 'nxml-end-delimiter-length 1)
2653 (defun nxml-token-type-friendly-name (type)
2654 (or (get type 'nxml-friendly-name)
2655 (symbol-name type)))
2657 (put 'cdata-section 'nxml-friendly-name "CDATA section")
2658 (put 'processing-instruction 'nxml-friendly-name "processing instruction")
2659 (put 'entity-ref 'nxml-friendly-name "entity reference")
2660 (put 'char-ref 'nxml-friendly-name "character reference")
2663 (defalias 'xml-mode 'nxml-mode)
2665 (provide 'nxml-mode)
2667 ;; arch-tag: 8603bc5f-1ef9-4021-b223-322fb2ca708e
2668 ;;; nxml-mode.el ends here