1 ;;; nxml-mode.el --- a new XML mode
3 ;; Copyright (C) 2003, 2004, 2007, 2008 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 ;; See nxml-rap.el for description of parsing strategy.
29 ;; The font locking here is independent of font-lock.el. We want to
30 ;; do more sophisticated handling of changes and we want to use the
31 ;; same xmltok rather than regexps for parsing so that we parse
32 ;; consistently and correctly.
36 (when (featurep 'mucs)
37 (error "nxml-mode is not compatible with Mule-UCS"))
46 (declare-function rng-nxml-mode-init "rng-nxml")
47 (declare-function nxml-enable-unicode-char-name-sets "nxml-uchnm")
52 "New XML editing mode"
56 (defgroup nxml-faces nil
57 "Faces for XML syntax highlighting."
59 :group 'font-lock-faces)
61 (defcustom nxml-syntax-highlight-flag t
62 "*Non-nil means nxml-mode should perform syntax highlighting."
66 (defcustom nxml-char-ref-display-glyph-flag t
67 "*Non-nil means display glyph following character reference.
68 The glyph is displayed in face `nxml-glyph'. The hook
69 `nxml-glyph-set-hook' can be used to customize for which characters
70 glyphs are displayed."
74 (defcustom nxml-mode-hook nil
75 "Hook run by command `nxml-mode'."
79 (defcustom nxml-sexp-element-flag nil
80 "*Non-nil means sexp commands treat an element as a single expression."
84 (defcustom nxml-slash-auto-complete-flag nil
85 "*Non-nil means typing a slash automatically completes the end-tag.
86 This is used by `nxml-electric-slash'."
90 (defcustom nxml-child-indent 2
91 "*Indentation for the children of an element relative to the start-tag.
92 This only applies when the line or lines containing the start-tag contains
93 nothing else other than that start-tag."
97 (defcustom nxml-attribute-indent 4
98 "*Indentation for the attributes of an element relative to the start-tag.
99 This only applies when the first attribute of a tag starts a line. In other
100 cases, the first attribute on one line is indented the same as the first
101 attribute on the previous line."
105 (defvar nxml-fontify-chunk-size 500)
107 (defcustom nxml-bind-meta-tab-to-complete-flag (not window-system)
108 "*Non-nil means bind M-TAB in `nxml-mode-map' to `nxml-complete'.
109 C-return will be bound to `nxml-complete' in any case.
110 M-TAB gets swallowed by many window systems/managers, and
111 `documentation' will show M-TAB rather than C-return as the
112 binding `rng-complete' when both are bound. So it's better
113 to bind M-TAB only when it will work."
115 :set (lambda (sym flag)
116 (set-default sym flag)
117 (when (and (boundp 'nxml-mode-map) nxml-mode-map)
118 (define-key nxml-mode-map "\M-\t" (and flag 'nxml-complete))))
121 (defcustom nxml-prefer-utf-16-to-utf-8-flag nil
122 "*Non-nil means prefer UTF-16 to UTF-8 when saving a buffer.
123 This is used only when a buffer does not contain an encoding declaration
124 and when its current `buffer-file-coding-system' specifies neither UTF-16
129 (defcustom nxml-prefer-utf-16-little-to-big-endian-flag (eq system-type
131 "*Non-nil means prefer little-endian to big-endian byte-order for UTF-16.
132 This is used only for saving a buffer; when reading the byte-order is
133 auto-detected. It may be relevant both when there is no encoding declaration
134 and when the encoding declaration specifies `UTF-16'."
138 (defcustom nxml-default-buffer-file-coding-system nil
139 "*Default value for `buffer-file-coding-system' for a buffer for a new file.
140 Nil means use the default value of `buffer-file-coding-system' as normal.
141 A buffer's `buffer-file-coding-system' affects what \\[nxml-insert-xml-declaration] inserts."
143 :type 'coding-system)
145 (defcustom nxml-auto-insert-xml-declaration-flag nil
146 "*Non-nil means automatically insert an XML declaration in a new file.
147 The XML declaration is inserted using `nxml-insert-xml-declaration'."
151 (defface nxml-delimited-data
152 '((t (:inherit font-lock-doc-face)))
153 "Face used to highlight data enclosed between delimiters.
154 This is not used directly, but only via inheritance by other faces."
158 '((t (:inherit font-lock-builtin-face)))
159 "Face used to highlight various names.
160 This includes element and attribute names, processing
161 instruction targets and the CDATA keyword in a CDATA section.
162 This is not used directly, but only via inheritance by other faces."
166 '((t (:inherit font-lock-constant-face)))
167 "Face used to highlight character and entity references.
168 This is not used directly, but only via inheritance by other faces."
171 (defface nxml-delimiter
173 "Face used to highlight delimiters.
174 This is not used directly, but only via inheritance by other faces."
179 "Face used to highlight text."
182 (defface nxml-comment-content
183 '((t (:inherit font-lock-comment-face)))
184 "Face used to highlight the content of comments."
187 (defface nxml-comment-delimiter
188 '((t (:inherit font-lock-comment-delimiter-face)))
189 "Face used for the delimiters of comments, i.e <!-- and -->."
192 (defface nxml-processing-instruction-delimiter
193 '((t (:inherit nxml-delimiter)))
194 "Face used for the delimiters of processing instructions, i.e <? and ?>."
197 (defface nxml-processing-instruction-target
198 '((t (:inherit font-lock-keyword-face)))
199 "Face used for the target of processing instructions."
202 (defface nxml-processing-instruction-content
203 '((t (:inherit nxml-delimited-data)))
204 "Face used for the content of processing instructions."
207 (defface nxml-cdata-section-delimiter
208 '((t (:inherit nxml-delimiter)))
209 "Face used for the delimiters of CDATA sections, i.e <![, [, and ]]>."
212 (defface nxml-cdata-section-CDATA
213 '((t (:inherit nxml-name)))
214 "Face used for the CDATA keyword in CDATA sections."
217 (defface nxml-cdata-section-content
218 '((t (:inherit nxml-text)))
219 "Face used for the content of CDATA sections."
222 (defface nxml-char-ref-number
223 '((t (:inherit nxml-ref)))
224 "Face used for the number in character references.
225 This includes ths `x' in hex references."
228 (defface nxml-char-ref-delimiter
229 '((t (:inherit nxml-ref)))
230 "Face used for the delimiters of character references, i.e &# and ;."
233 (defface nxml-entity-ref-name
234 '((t (:inherit nxml-ref)))
235 "Face used for the entity name in general entity references."
238 (defface nxml-entity-ref-delimiter
239 '((t (:inherit nxml-ref)))
240 "Face used for the delimiters of entity references, i.e & and ;."
243 (defface nxml-tag-delimiter
244 '((t (:inherit nxml-delimiter)))
245 "Face used for the angle brackets delimiting tags.
246 `nxml-tag-slash' is used for slashes."
249 (defface nxml-tag-slash
250 '((t (:inherit nxml-tag-delimiter)))
251 "Face used for slashes in tags, both in end-tags and empty-elements."
254 (defface nxml-element-prefix
255 '((t (:inherit nxml-name)))
256 "Face used for the prefix of elements."
259 (defface nxml-element-colon
261 "Face used for the colon in element names."
264 (defface nxml-element-local-name
265 '((t (:inherit font-lock-function-name-face)))
266 "Face used for the local name of elements."
269 (defface nxml-attribute-prefix
270 '((t (:inherit nxml-name)))
271 "Face used for the prefix of attributes."
274 (defface nxml-attribute-colon
275 '((t (:inherit nxml-delimiter)))
276 "Face used for the colon in attribute names."
279 (defface nxml-attribute-local-name
280 '((t (:inherit font-lock-variable-name-face)))
281 "Face used for the local name of attributes."
284 (defface nxml-namespace-attribute-xmlns
285 '((t (:inherit nxml-attribute-prefix)))
286 "Face used for `xmlns' in namespace attributes."
289 (defface nxml-namespace-attribute-colon
290 '((t (:inherit nxml-attribute-colon)))
291 "Face used for the colon in namespace attributes."
294 (defface nxml-namespace-attribute-prefix
295 '((t (:inherit nxml-attribute-local-name)))
296 "Face used for the prefix declared in namespace attributes."
299 (defface nxml-attribute-value
300 '((t (:inherit font-lock-string-face)))
301 "Face used for the value of attributes."
304 (defface nxml-attribute-value-delimiter
305 '((t (:inherit nxml-attribute-value)))
306 "Face used for the delimiters of attribute values."
309 (defface nxml-namespace-attribute-value
310 '((t (:inherit nxml-attribute-value)))
311 "Face used for the value of namespace attributes."
314 (defface nxml-namespace-attribute-value-delimiter
315 '((t (:inherit nxml-attribute-value-delimiter)))
316 "Face used for the delimiters of namespace attribute values."
319 (defface nxml-prolog-literal-delimiter
320 '((t (:inherit nxml-delimited-data)))
321 "Face used for the delimiters of literals in the prolog."
324 (defface nxml-prolog-literal-content
325 '((t (:inherit nxml-delimited-data)))
326 "Face used for the content of literals in the prolog."
329 (defface nxml-prolog-keyword
330 '((t (:inherit font-lock-keyword-face)))
331 "Face used for keywords in the prolog."
334 (defface nxml-markup-declaration-delimiter
335 '((t (:inherit nxml-delimiter)))
336 "Face used for the delimiters of markup declarations in the prolog.
337 The delimiters are <! and >."
341 '((t (:inherit nxml-name)))
342 "Face used for # before a name in the prolog."
366 "Face used for glyph for char references."
371 (defvar nxml-prolog-regions nil
372 "List of regions in the prolog to be fontified.
373 See the function `xmltok-forward-prolog' for more information.")
374 (make-variable-buffer-local 'nxml-prolog-regions)
376 (defvar nxml-last-fontify-end nil
377 "Position where fontification last ended.
378 Nil if the buffer changed since the last fontification.")
379 (make-variable-buffer-local 'nxml-last-fontify-end)
381 (defvar nxml-degraded nil
382 "Non-nil if currently operating in degraded mode.
383 Degraded mode is enabled when an internal error is encountered in the
384 fontification or after-change functions.")
385 (make-variable-buffer-local 'nxml-degraded)
387 (defvar nxml-completion-hook nil
388 "Hook run by `nxml-complete'.
389 This hook is run until success.")
391 (defvar nxml-in-mixed-content-hook nil
392 "Hook to determine whether point is in mixed content.
393 The hook is called without arguments. It should return nil if it is
394 definitely not mixed; non-nil otherwise. The hook will be run until
395 one of the functions returns nil.")
397 (defvar nxml-mixed-scan-distance 4000
398 "Maximum distance from point to scan when checking for mixed content.")
400 (defvar nxml-end-tag-indent-scan-distance 4000
401 "Maximum distance from point to scan backwards when indenting end-tag.")
403 (defvar nxml-char-ref-extra-display t
404 "Non-nil means display extra information for character references.
405 The extra information consists of a tooltip with the character name
406 and, if `nxml-char-ref-display-glyph-flag' is non-nil, a glyph
407 corresponding to the referenced character following the character
409 (make-variable-buffer-local 'nxml-char-ref-extra-display)
411 (defvar nxml-mode-map
412 (let ((map (make-sparse-keymap)))
413 (define-key map "\M-\C-u" 'nxml-backward-up-element)
414 (define-key map "\M-\C-d" 'nxml-down-element)
415 (define-key map "\M-\C-n" 'nxml-forward-element)
416 (define-key map "\M-\C-p" 'nxml-backward-element)
417 (define-key map "\M-{" 'nxml-backward-paragraph)
418 (define-key map "\M-}" 'nxml-forward-paragraph)
419 (define-key map "\M-h" 'nxml-mark-paragraph)
420 (define-key map "\C-c\C-f" 'nxml-finish-element)
421 (define-key map "\C-c\C-m" 'nxml-split-element)
422 (define-key map "\C-c\C-b" 'nxml-balanced-close-start-tag-block)
423 (define-key map "\C-c\C-i" 'nxml-balanced-close-start-tag-inline)
424 (define-key map "\C-c\C-x" 'nxml-insert-xml-declaration)
425 (define-key map "\C-c\C-d" 'nxml-dynamic-markup-word)
427 (define-key map "\C-c\C-u" 'nxml-insert-named-char)
428 (define-key map "\C-c\C-o" nxml-outline-prefix-map)
429 (define-key map [S-mouse-2] 'nxml-mouse-hide-direct-text-content)
430 (define-key map "/" 'nxml-electric-slash)
431 (define-key map [C-return] 'nxml-complete)
432 (when nxml-bind-meta-tab-to-complete-flag
433 (define-key map "\M-\t" 'nxml-complete))
435 "Keymap for nxml-mode.")
437 (defsubst nxml-set-face (start end face)
438 (when (and face (< start end))
439 (put-text-property start end 'face face)))
441 (defun nxml-clear-face (start end)
442 (remove-text-properties start end '(face nil))
443 (nxml-clear-char-ref-extra-display start end))
445 (defsubst nxml-set-fontified (start end)
446 (put-text-property start end 'fontified t))
448 (defsubst nxml-clear-fontified (start end)
449 (remove-text-properties start end '(fontified nil)))
453 ;; We use C-c C-i instead of \\[nxml-balanced-close-start-tag-inline]
454 ;; because Emacs turns C-c C-i into C-c TAB which is hard to type and
456 "Major mode for editing XML.
458 Syntax highlighting is performed unless the variable
459 `nxml-syntax-highlight-flag' is nil.
461 \\[nxml-finish-element] finishes the current element by inserting an end-tag.
462 C-c C-i closes a start-tag with `>' and then inserts a balancing end-tag
463 leaving point between the start-tag and end-tag.
464 \\[nxml-balanced-close-start-tag-block] is similar but for block rather than inline elements:
465 the start-tag, point, and end-tag are all left on separate lines.
466 If `nxml-slash-auto-complete-flag' is non-nil, then inserting a `</'
467 automatically inserts the rest of the end-tag.
469 \\[nxml-complete] performs completion on the symbol preceding point.
471 \\[nxml-dynamic-markup-word] uses the contents of the current buffer
472 to choose a tag to put around the word preceding point.
474 Sections of the document can be displayed in outline form. The
475 variable `nxml-section-element-name-regexp' controls when an element
476 is recognized as a section. The same key sequences that change
477 visibility in outline mode are used except that they start with C-c C-o
480 Validation is provided by the related minor-mode `rng-validate-mode'.
481 This also makes completion schema- and context- sensitive. Element
482 names, attribute names, attribute values and namespace URIs can all be
483 completed. By default, `rng-validate-mode' is automatically enabled. You
484 can toggle it using \\[rng-validate-mode] or change the default by
485 customizing `rng-nxml-auto-validate-flag'.
487 \\[indent-for-tab-command] indents the current line appropriately.
488 This can be customized using the variable `nxml-child-indent'
489 and the variable `nxml-attribute-indent'.
491 \\[nxml-insert-named-char] inserts a character reference using
492 the character's name (by default, the Unicode name). \\[universal-argument] \\[nxml-insert-named-char]
493 inserts the character directly.
495 The Emacs commands that normally operate on balanced expressions will
496 operate on XML markup items. Thus \\[forward-sexp] will move forward
497 across one markup item; \\[backward-sexp] will move backward across
498 one markup item; \\[kill-sexp] will kill the following markup item;
499 \\[mark-sexp] will mark the following markup item. By default, each
500 tag each treated as a single markup item; to make the complete element
501 be treated as a single markup item, set the variable
502 `nxml-sexp-element-flag' to t. For more details, see the function
503 `nxml-forward-balanced-item'.
505 \\[nxml-backward-up-element] and \\[nxml-down-element] move up and down the element structure.
507 Many aspects this mode can be customized using
508 \\[customize-group] nxml RET."
510 (kill-all-local-variables)
511 (setq major-mode 'nxml-mode)
512 (setq mode-name "nXML")
513 (set (make-local-variable 'mode-line-process) '((nxml-degraded "/degraded")))
514 ;; We'll determine the fill prefix ourselves
515 (make-local-variable 'adaptive-fill-mode)
516 (setq adaptive-fill-mode nil)
517 (make-local-variable 'forward-sexp-function)
518 (setq forward-sexp-function 'nxml-forward-balanced-item)
519 (make-local-variable 'indent-line-function)
520 (setq indent-line-function 'nxml-indent-line)
521 (make-local-variable 'fill-paragraph-function)
522 (setq fill-paragraph-function 'nxml-do-fill-paragraph)
524 ;; This doesn't seem to work too well;
525 ;; I think we should probably roll our own nxml-comment-dwim function.
526 (make-local-variable 'comment-indent-function)
527 (setq comment-indent-function 'nxml-indent-line)
528 (make-local-variable 'comment-start)
529 (setq comment-start "<!--")
530 (make-local-variable 'comment-start-skip)
531 (setq comment-start-skip "<!--[ \t\r\n]*")
532 (make-local-variable 'comment-end)
533 (setq comment-end "-->")
534 (make-local-variable 'comment-end-skip)
535 (setq comment-end-skip "[ \t\r\n]*-->")
536 (make-local-variable 'comment-line-break-function)
537 (setq comment-line-break-function 'nxml-newline-and-indent)
538 (use-local-map nxml-mode-map)
542 (nxml-clear-dependent-regions (point-min) (point-max))
543 (setq nxml-scan-end (copy-marker (point-min) nil))
544 (nxml-with-unmodifying-text-property-changes
545 (when nxml-syntax-highlight-flag
546 (nxml-clear-fontified (point-min) (point-max)))
547 (nxml-clear-inside (point-min) (point-max))
548 (nxml-with-invisible-motion
549 (nxml-scan-prolog)))))
550 (when nxml-syntax-highlight-flag
551 (add-hook 'fontification-functions 'nxml-fontify nil t))
552 (add-hook 'after-change-functions 'nxml-after-change nil t)
553 (add-hook 'write-contents-hooks 'nxml-prepare-to-save)
554 (when (not (and (buffer-file-name) (file-exists-p (buffer-file-name))))
555 (when (and nxml-default-buffer-file-coding-system
556 (not (local-variable-p 'buffer-file-coding-system)))
557 (setq buffer-file-coding-system nxml-default-buffer-file-coding-system))
558 (when nxml-auto-insert-xml-declaration-flag
559 (nxml-insert-xml-declaration)))
561 (nxml-enable-unicode-char-name-sets)
562 (run-hooks 'nxml-mode-hook))
564 (defun nxml-degrade (context err)
565 (message "Internal nXML mode error in %s (%s), degrading"
567 (error-message-string err))
569 (setq nxml-degraded t)
570 (setq nxml-prolog-end 1)
574 (nxml-with-unmodifying-text-property-changes
575 (nxml-clear-face (point-min) (point-max))
576 (nxml-set-fontified (point-min) (point-max))
577 (nxml-clear-inside (point-min) (point-max))))))
579 ;;; Change management
581 (defun nxml-after-change (start end pre-change-length)
582 ;; Work around bug in insert-file-contents.
583 (when (> end (1+ (buffer-size)))
585 (setq end (1+ (buffer-size))))
586 (unless nxml-degraded
592 (nxml-with-invisible-motion
593 (nxml-with-unmodifying-text-property-changes
594 (nxml-after-change1 start end pre-change-length))))))
596 (nxml-degrade 'nxml-after-change err)))))
598 (defun nxml-after-change1 (start end pre-change-length)
599 (setq nxml-last-fontify-end nil)
600 (let ((pre-change-end (+ start pre-change-length)))
602 (nxml-adjust-start-for-dependent-regions start
606 ;; Add 2 so as to include the < and following char
607 ;; that start the instance, since changing these
608 ;; can change where the prolog ends.
609 (+ nxml-prolog-end 2))
610 ;; end must be extended to at least the end of the old prolog
611 (when (< pre-change-end nxml-prolog-end)
613 ;; don't let end get out of range even if pre-change-length
616 (+ end (- nxml-prolog-end pre-change-end)))))
618 (cond ((<= end nxml-prolog-end)
619 (setq end nxml-prolog-end)
621 ;; This is so that Emacs redisplay works
622 (setq start (line-beginning-position)))
623 ((and (<= start nxml-scan-end)
624 (> start (point-min))
625 (nxml-get-inside (1- start)))
626 ;; The closing delimiter might have been removed.
627 ;; So we may need to redisplay from the beginning
629 (goto-char (1- start))
630 (nxml-move-outside-backwards)
631 ;; This is so that Emacs redisplay works
632 (setq start (line-beginning-position))
633 (setq end (max (nxml-scan-after-change (point) end)
637 ;; This is both for redisplay and to move back
638 ;; past any incomplete opening delimiters
639 (setq start (line-beginning-position))
640 (setq end (max (nxml-scan-after-change start end)
642 (when nxml-syntax-highlight-flag
644 ;; Must clear at least one char so as to trigger redisplay.
645 (cond ((< start (point-max))
646 (setq end (1+ start)))
648 (setq end (point-max))
650 (setq start (line-beginning-position)))))
651 (nxml-clear-fontified start end)))
655 (defun nxml-insert-xml-declaration ()
656 "Insert an XML declaration at the beginning of buffer.
657 The XML declaration will declare an encoding depending on the buffer's
658 `buffer-file-coding-system'."
661 (if (and buffer-file-coding-system
662 (coding-system-p buffer-file-coding-system)
663 (coding-system-get buffer-file-coding-system
665 buffer-file-coding-system
666 (nxml-choose-utf-coding-system))))
667 (goto-char (point-min))
668 (insert (format "<?xml version=\"1.0\" encoding=\"%s\"?>\n"
669 (nxml-coding-system-name coding-system)))))
671 (defun nxml-prepare-to-save ()
672 (unless (and (not enable-multibyte-characters)
673 (local-variable-p 'buffer-file-coding-system)
674 buffer-file-coding-system
675 (or (eq (coding-system-type buffer-file-coding-system) 5)
676 (eq buffer-file-coding-system 'no-conversion)))
678 (setq buffer-file-coding-system (nxml-select-coding-system))))
679 ;; nil from a function in `write-contents-hooks' means
680 ;; to continue and write the file as normal
683 (defun nxml-select-coding-system ()
684 (let* ((suitable-coding-systems
685 (find-coding-systems-region (point-min) (point-max)))
687 (goto-char (point-min))
688 (xmltok-get-declared-encoding-position)))
691 (buffer-substring-no-properties (car enc-pos)
695 (if (string= (downcase enc-name) "utf-16")
696 (nxml-choose-utf-16-coding-system)
697 (nxml-mime-charset-coding-system enc-name)))
698 (enc-pos (nxml-choose-utf-coding-system)))))
699 ;; Make sure we have a coding-system
700 (unless coding-system
702 (and (not buffer-read-only)
703 (nxml-choose-suitable-coding-system
704 suitable-coding-systems)))
707 (format "Unknown encoding %s" enc-name)
708 "XML declaration is not well-formed")))
709 (cond ((not coding-system)
710 (error "%s" message))
716 "Modify and save with encoding %s")
717 (nxml-coding-system-name coding-system))
719 (nxml-fix-encoding-declaration enc-pos coding-system))
720 (t (signal 'quit nil)))))
721 ;; Make sure it can encode all the characters in the buffer
722 (unless (or (memq (coding-system-base coding-system)
723 suitable-coding-systems)
724 (equal suitable-coding-systems '(undecided)))
726 (nxml-unsuitable-coding-system-message coding-system
729 (and (not buffer-read-only)
730 (nxml-choose-suitable-coding-system
731 suitable-coding-systems)))
732 (cond ((not coding-system) (error "%s" message))
733 ((y-or-n-p (concat message
734 (format ". Save with %s "
735 (nxml-coding-system-name
737 (nxml-fix-encoding-declaration enc-pos coding-system))
738 (t (signal 'quit nil)))))
739 ;; Merge the newline type of our existing encoding
740 (let ((current-eol-type
741 (coding-system-eol-type buffer-file-coding-system)))
742 (when (and current-eol-type (integerp current-eol-type))
744 (coding-system-change-eol-conversion coding-system
748 (defun nxml-unsuitable-coding-system-message (coding-system &optional enc-name)
749 (if (nxml-coding-system-unicode-p coding-system)
750 "Cannot translate some characters to Unicode"
751 (format "Cannot encode some characters with %s"
753 (nxml-coding-system-name coding-system)))))
755 (defconst nxml-utf-16-coding-systems (and (coding-system-p 'utf-16-be)
756 (coding-system-p 'utf-16-le)
757 '(utf-16-be utf-16-le)))
759 (defconst nxml-utf-coding-systems (cons 'utf-8 nxml-utf-16-coding-systems))
761 (defun nxml-coding-system-unicode-p (coding-system)
762 (nxml-coding-system-member (coding-system-base coding-system)
763 nxml-utf-coding-systems))
765 (defun nxml-coding-system-name (coding-system)
766 (setq coding-system (coding-system-base coding-system))
768 (if (nxml-coding-system-member coding-system nxml-utf-16-coding-systems)
770 (or (coding-system-get coding-system 'mime-charset)
773 (defun nxml-fix-encoding-declaration (enc-pos coding-system)
774 (let ((charset (nxml-coding-system-name coding-system)))
775 (cond ((consp enc-pos)
776 (delete-region (car enc-pos) (cdr enc-pos))
777 (goto-char (car enc-pos))
781 (insert " encoding=\"" charset ?\"))
783 (goto-char (point-min))
784 (insert "<?xml version=\"1.0\" encoding=\""
787 (when (and (not enc-pos)
788 (let ((case-fold-search t))
789 (looking-at xmltok-bad-xml-decl-regexp)))
790 (delete-region (point) (match-end 0)))))))
792 (defun nxml-choose-suitable-coding-system (suitable-coding-systems)
793 (let (ret coding-system)
794 (if (and buffer-file-coding-system
795 (memq (coding-system-base buffer-file-coding-system)
796 suitable-coding-systems))
797 buffer-file-coding-system
798 (while (and suitable-coding-systems (not ret))
799 (setq coding-system (car suitable-coding-systems))
800 (if (coding-system-get coding-system 'mime-charset)
801 (setq ret coding-system)
802 (setq suitable-coding-systems (cdr suitable-coding-systems))))
805 (defun nxml-choose-utf-coding-system ()
806 (let ((cur (and (local-variable-p 'buffer-file-coding-system)
807 buffer-file-coding-system
808 (coding-system-base buffer-file-coding-system))))
809 (cond ((car (nxml-coding-system-member cur nxml-utf-coding-systems)))
810 ((and nxml-prefer-utf-16-to-utf-8-flag
811 (coding-system-p 'utf-16-le)
812 (coding-system-p 'utf-16-be))
813 (if nxml-prefer-utf-16-little-to-big-endian-flag
818 (defun nxml-choose-utf-16-coding-system ()
819 (let ((cur (and (local-variable-p 'buffer-file-coding-system)
820 buffer-file-coding-system
821 (coding-system-base buffer-file-coding-system))))
822 (cond ((car (nxml-coding-system-member cur nxml-utf-16-coding-systems)))
823 (nxml-prefer-utf-16-little-to-big-endian-flag
824 (and (coding-system-p 'utf-16-le) 'utf-16-le))
825 (t (and (coding-system-p 'utf-16-be) 'utf-16-be)))))
827 (defun nxml-coding-system-member (coding-system coding-systems)
829 (while (and coding-systems (not ret))
830 (if (coding-system-equal coding-system
831 (car coding-systems))
832 (setq ret coding-systems)
833 (setq coding-systems (cdr coding-systems))))
838 (defun nxml-fontify (start)
844 (nxml-with-invisible-motion
845 (nxml-with-unmodifying-text-property-changes
846 (if (or nxml-degraded
847 ;; just in case we get called in the wrong buffer
848 (not nxml-prolog-end))
849 (nxml-set-fontified start (point-max))
850 (nxml-fontify1 start)))))))
852 (nxml-degrade 'nxml-fontify err))))
854 (defun nxml-fontify1 (start)
855 (cond ((< start nxml-prolog-end)
856 (nxml-fontify-prolog)
857 (nxml-set-fontified (point-min)
861 (when (not (eq nxml-last-fontify-end start))
862 (when (not (equal (char-after) ?\<))
863 (search-backward "<" nxml-prolog-end t))
864 (nxml-ensure-scan-up-to-date)
865 (nxml-move-outside-backwards))
866 (let ((start (point)))
867 (nxml-do-fontify (min (point-max)
868 (+ start nxml-fontify-chunk-size)))
869 (setq nxml-last-fontify-end (point))
870 (nxml-set-fontified start nxml-last-fontify-end)))))
872 (defun nxml-fontify-buffer ()
877 (nxml-with-invisible-motion
878 (goto-char (point-min))
879 (nxml-with-unmodifying-text-property-changes
880 (nxml-fontify-prolog)
881 (goto-char nxml-prolog-end)
882 (nxml-do-fontify))))))
884 (defun nxml-fontify-prolog ()
886 The buffer is assumed to be prepared for fontification.
887 This does not set the fontified property, but it does clear
888 faces appropriately."
889 (let ((regions nxml-prolog-regions))
890 (nxml-clear-face (point-min) nxml-prolog-end)
892 (let ((region (car regions)))
893 (nxml-apply-fontify-rule (aref region 0)
896 (setq regions (cdr regions)))))
898 (defun nxml-do-fontify (&optional bound)
899 "Fontify at least as far as bound.
900 Leave point after last fontified position."
901 (unless bound (setq bound (point-max)))
902 (let (xmltok-dependent-regions
904 (while (and (< (point) bound)
905 (nxml-tokenize-forward))
906 (nxml-clear-face xmltok-start (point))
907 (nxml-apply-fontify-rule))))
909 ;; Vectors identify a substring of the token to be highlighted in some face.
911 ;; Token types returned by xmltok-forward.
915 '([nil 1 nxml-tag-delimiter]
916 [-1 nil nxml-tag-delimiter]
920 (put 'partial-start-tag
922 '([nil 1 nxml-tag-delimiter]
928 '([nil 1 nxml-tag-delimiter]
930 [-1 nil nxml-tag-delimiter]
931 (element-qname . 2)))
933 (put 'partial-end-tag
935 '([nil 1 nxml-tag-delimiter]
937 (element-qname . 2)))
941 '([nil 1 nxml-tag-delimiter]
942 [-2 -1 nxml-tag-slash]
943 [-1 nil nxml-tag-delimiter]
947 (put 'partial-empty-element
949 '([nil 1 nxml-tag-delimiter]
950 [-1 nil nxml-tag-slash]
956 '([nil 2 nxml-char-ref-delimiter]
957 [2 -1 nxml-char-ref-number]
958 [-1 nil nxml-char-ref-delimiter]
963 '([nil 1 nxml-entity-ref-delimiter]
964 [1 -1 nxml-entity-ref-name]
965 [-1 nil nxml-entity-ref-delimiter]))
969 '([nil 4 nxml-comment-delimiter]
970 [4 -3 nxml-comment-content]
971 [-3 nil nxml-comment-delimiter]))
973 (put 'processing-instruction
975 '([nil 2 nxml-processing-instruction-delimiter]
976 [-2 nil nxml-processing-instruction-delimiter]
977 processing-instruction-content))
981 '([nil 3 nxml-cdata-section-delimiter] ; <![
982 [3 8 nxml-cdata-section-CDATA] ; CDATA
983 [8 9 nxml-cdata-section-delimiter] ; [
984 [9 -3 nxml-cdata-section-content] ; ]]>
985 [-3 nil nxml-cdata-section-delimiter]))
989 '([nil nil nxml-text]))
991 ;; Prolog region types in list returned by xmltok-forward-prolog.
993 (put 'xml-declaration
995 '([nil 2 nxml-processing-instruction-delimiter]
996 [2 5 nxml-processing-instruction-target]
997 [-2 nil nxml-processing-instruction-delimiter]))
999 (put 'xml-declaration-attribute-name
1001 '([nil nil nxml-attribute-local-name]))
1003 (put 'xml-declaration-attribute-value
1005 '([nil 1 nxml-attribute-value-delimiter]
1006 [1 -1 nxml-attribute-value]
1007 [-1 nil nxml-attribute-value-delimiter]))
1009 (put 'processing-instruction-left
1011 '([nil 2 nxml-processing-instruction-delimiter]
1012 [2 nil nxml-processing-instruction-target]))
1014 (put 'processing-instruction-right
1016 '([nil -2 nxml-processing-instruction-content]
1017 [-2 nil nxml-processing-instruction-delimiter]))
1021 '([nil 1 nxml-prolog-literal-delimiter]
1022 [1 -1 nxml-prolog-literal-content]
1023 [-1 nil nxml-prolog-literal-delimiter]))
1027 '([nil nil nxml-prolog-keyword]))
1029 (put 'markup-declaration-open
1031 '([0 2 nxml-markup-declaration-delimiter]
1032 [2 nil nxml-prolog-keyword]))
1034 (put 'markup-declaration-close
1036 '([nil nil nxml-markup-declaration-delimiter]))
1038 (put 'internal-subset-open
1040 '([nil nil nxml-markup-declaration-delimiter]))
1042 (put 'internal-subset-close
1044 '([nil 1 nxml-markup-declaration-delimiter]
1045 [-1 nil nxml-markup-declaration-delimiter]))
1050 [1 nil nxml-prolog-keyword]))
1052 (defun nxml-apply-fontify-rule (&optional type start end)
1053 (let ((rule (get (or type xmltok-type) 'nxml-fontify-rule)))
1054 (unless start (setq start xmltok-start))
1055 (unless end (setq end (point)))
1057 (let* ((action (car rule)))
1058 (setq rule (cdr rule))
1059 (cond ((vectorp action)
1060 (nxml-set-face (let ((offset (aref action 0)))
1061 (cond ((not offset) start)
1062 ((< offset 0) (+ end offset))
1063 (t (+ start offset))))
1064 (let ((offset (aref action 1)))
1065 (cond ((not offset) end)
1066 ((< offset 0) (+ end offset))
1067 (t (+ start offset))))
1069 ((and (consp action)
1070 (eq (car action) 'element-qname))
1071 (when xmltok-name-end ; maybe nil in partial-end-tag case
1072 (nxml-fontify-qname (+ start (cdr action))
1075 'nxml-element-prefix
1077 'nxml-element-local-name)))
1078 ((eq action 'attributes)
1079 (nxml-fontify-attributes))
1080 ((eq action 'processing-instruction-content)
1081 (nxml-set-face (+ start 2)
1083 'nxml-processing-instruction-target)
1084 (nxml-set-face (save-excursion
1085 (goto-char xmltok-name-end)
1086 (skip-chars-forward " \t\r\n")
1089 'nxml-processing-instruction-content))
1090 ((eq action 'char-ref)
1091 (nxml-char-ref-display-extra start
1093 (xmltok-char-number start end)))
1094 (t (error "Invalid nxml-fontify-rule action %s" action)))))))
1096 (defun nxml-fontify-attributes ()
1097 (while xmltok-namespace-attributes
1098 (nxml-fontify-attribute (car xmltok-namespace-attributes)
1100 (setq xmltok-namespace-attributes
1101 (cdr xmltok-namespace-attributes)))
1102 (while xmltok-attributes
1103 (nxml-fontify-attribute (car xmltok-attributes))
1104 (setq xmltok-attributes
1105 (cdr xmltok-attributes))))
1107 (defun nxml-fontify-attribute (att &optional namespace-declaration)
1108 (if namespace-declaration
1109 (nxml-fontify-qname (xmltok-attribute-name-start att)
1110 (xmltok-attribute-name-colon att)
1111 (xmltok-attribute-name-end att)
1112 'nxml-namespace-attribute-xmlns
1113 'nxml-namespace-attribute-colon
1114 'nxml-namespace-attribute-prefix
1115 'nxml-namespace-attribute-xmlns)
1116 (nxml-fontify-qname (xmltok-attribute-name-start att)
1117 (xmltok-attribute-name-colon att)
1118 (xmltok-attribute-name-end att)
1119 'nxml-attribute-prefix
1120 'nxml-attribute-colon
1121 'nxml-attribute-local-name))
1122 (let ((start (xmltok-attribute-value-start att))
1123 (end (xmltok-attribute-value-end att))
1124 (refs (xmltok-attribute-refs att))
1125 (delimiter-face (if namespace-declaration
1126 'nxml-namespace-attribute-value-delimiter
1127 'nxml-attribute-value-delimiter))
1128 (value-face (if namespace-declaration
1129 'nxml-namespace-attribute-value
1130 'nxml-attribute-value)))
1132 (nxml-set-face (1- start) start delimiter-face)
1133 (nxml-set-face end (1+ end) delimiter-face)
1135 (let* ((ref (car refs))
1136 (ref-type (aref ref 0))
1137 (ref-start (aref ref 1))
1138 (ref-end (aref ref 2)))
1139 (nxml-set-face start ref-start value-face)
1140 (nxml-apply-fontify-rule ref-type ref-start ref-end)
1141 (setq start ref-end))
1142 (setq refs (cdr refs)))
1143 (nxml-set-face start end value-face))))
1145 (defun nxml-fontify-qname (start
1153 (cond (colon (nxml-set-face start colon prefix-face)
1154 (nxml-set-face colon (1+ colon) colon-face)
1155 (nxml-set-face (1+ colon) end local-name-face))
1156 (t (nxml-set-face start end (or unprefixed-face
1157 local-name-face)))))
1161 (defun nxml-electric-slash (arg)
1164 With a prefix ARG, do nothing other than insert the slash.
1166 Otherwise, if `nxml-slash-auto-complete-flag' is non-nil, insert the
1167 rest of the end-tag or empty-element if the slash is potentially part
1168 of an end-tag or the close of an empty-element.
1170 If the slash is part of an end-tag that is the first non-whitespace
1171 on the line, reindent the line."
1173 (nxml-ensure-scan-up-to-date)
1174 (let* ((slash-pos (point))
1175 (end-tag-p (and (eq (char-before slash-pos) ?<)
1176 (not (nxml-get-inside slash-pos))))
1177 (at-indentation (save-excursion
1178 (back-to-indentation)
1179 (eq (point) (1- slash-pos)))))
1180 (self-insert-command (prefix-numeric-value arg))
1182 (if nxml-slash-auto-complete-flag
1185 (let ((start-tag-end
1186 (nxml-scan-element-backward (1- slash-pos) t)))
1188 (insert (xmltok-start-tag-qname) ">")
1189 ;; copy the indentation of the start-tag
1190 (when (and at-indentation
1192 (goto-char xmltok-start)
1193 (back-to-indentation)
1194 (eq (point) xmltok-start)))
1196 (indent-line-to (save-excursion
1197 (goto-char xmltok-start)
1198 (current-column)))))))
1199 (nxml-scan-error nil))
1200 (when (and (eq (nxml-token-before) (point))
1201 (eq xmltok-type 'partial-empty-element))
1203 (when (and end-tag-p at-indentation)
1204 (nxml-indent-line))))))
1206 (defun nxml-balanced-close-start-tag-block ()
1207 "Close the start-tag before point with `>' and insert a balancing end-tag.
1208 Point is left between the start-tag and the end-tag.
1209 If there is nothing but whitespace before the `<' that opens the
1210 start-tag, then put point on a blank line, and put the end-tag on
1211 another line aligned with the start-tag."
1213 (nxml-balanced-close-start-tag 'block))
1215 (defun nxml-balanced-close-start-tag-inline ()
1216 "Close the start-tag before point with `>' and insert a balancing end-tag.
1217 Point is left between the start-tag and the end-tag.
1218 No extra whitespace is inserted."
1220 (nxml-balanced-close-start-tag 'inline))
1222 (defun nxml-balanced-close-start-tag (block-or-inline)
1223 (let ((token-end (nxml-token-before))
1225 (unless (or (eq xmltok-type 'partial-start-tag)
1226 (and (memq xmltok-type '(start-tag
1228 partial-empty-element))
1229 (>= token-end pos)))
1230 (error "Not in a start-tag"))
1232 (buffer-substring-no-properties (+ xmltok-start 1)
1233 (min xmltok-name-end (point)))
1235 (if (eq block-or-inline 'inline)
1237 (goto-char xmltok-start)
1238 (back-to-indentation)
1239 (if (= (point) xmltok-start)
1240 (let ((indent (current-column)))
1243 (indent-line-to indent)
1246 (indent-line-to (+ nxml-child-indent indent)))
1249 (defun nxml-finish-element ()
1250 "Finish the current element by inserting an end-tag."
1252 (nxml-finish-element-1 nil))
1254 (defvar nxml-last-split-position nil
1255 "Position where `nxml-split-element' split the current element.")
1257 (defun nxml-split-element ()
1258 "Split the current element by inserting an end-tag and a start-tag.
1259 Point is left after the newly inserted start-tag. When repeated,
1260 split immediately before the previously inserted start-tag and leave
1263 (setq nxml-last-split-position
1264 (if (and (eq last-command this-command)
1265 nxml-last-split-position)
1267 (goto-char nxml-last-split-position)
1268 (nxml-finish-element-1 t))
1269 (nxml-finish-element-1 t))))
1271 (defun nxml-finish-element-1 (startp)
1272 "Insert an end-tag for the current element and optionally a start-tag.
1273 The start-tag is inserted if STARTP is non-nil. Return the position
1274 of the inserted start-tag or nil if none was inserted."
1276 (let* ((token-end (nxml-token-before))
1279 (when (and (< (point) token-end)
1282 processing-instruction
1287 (error "Point is inside a %s"
1288 (nxml-token-type-friendly-name xmltok-type)))
1289 (nxml-scan-element-backward token-end t)))
1292 (unless (eq xmltok-type 'start-tag)
1293 (error "No matching start-tag"))
1294 (goto-char xmltok-start)
1295 (back-to-indentation)
1296 (eq (point) xmltok-start)))
1299 (goto-char start-tag-end)
1300 (looking-at "[ \t\r\n]*$")))
1301 (start-tag-indent (save-excursion
1302 (goto-char xmltok-start)
1304 (qname (xmltok-start-tag-qname))
1305 inserted-start-tag-pos)
1306 (when (and starts-line ends-line)
1307 ;; start-tag is on a line by itself
1308 ;; => put the end-tag on a line by itself
1311 (back-to-indentation)
1314 (indent-line-to start-tag-indent))
1315 (insert "</" qname ">")
1319 (indent-line-to start-tag-indent))
1320 (setq inserted-start-tag-pos (point))
1321 (insert "<" qname ">")
1322 (when (and starts-line ends-line)
1324 (indent-line-to (save-excursion
1325 (goto-char xmltok-start)
1327 (back-to-indentation)
1328 (if (= (current-column)
1329 (+ start-tag-indent nxml-child-indent))
1330 (+ start-tag-indent nxml-child-indent)
1331 start-tag-indent)))))
1332 inserted-start-tag-pos))
1336 (defun nxml-indent-line ()
1337 "Indent current line as XML."
1338 (let ((indent (nxml-compute-indent))
1339 (from-end (- (point-max) (point))))
1342 (let ((bol (point)))
1343 (skip-chars-forward " \t")
1344 (delete-region bol (point)))
1346 (when (> (- (point-max) from-end) (point))
1347 (goto-char (- (point-max) from-end))))))
1349 (defun nxml-compute-indent ()
1350 "Return the indent for the line containing point."
1351 (or (nxml-compute-indent-from-matching-start-tag)
1352 (nxml-compute-indent-from-previous-line)))
1354 (defun nxml-compute-indent-from-matching-start-tag ()
1355 "Compute the indent for a line with an end-tag using the matching start-tag.
1356 When the line containing point ends with an end-tag and does not start
1357 in the middle of a token, return the indent of the line containing the
1358 matching start-tag, if there is one and it occurs at the beginning of
1359 its line. Otherwise return nil."
1361 (back-to-indentation)
1362 (let ((bol (point)))
1363 (let ((inhibit-field-text-motion t))
1365 (skip-chars-backward " \t")
1366 (and (= (nxml-token-before) (point))
1367 (memq xmltok-type '(end-tag partial-end-tag))
1368 ;; start of line must not be inside a token
1369 (or (= xmltok-start bol)
1373 (= xmltok-start bol))
1374 (eq xmltok-type 'data))
1376 (nxml-scan-element-backward
1380 nxml-end-tag-indent-scan-distance))
1381 (nxml-scan-error nil))
1382 (< xmltok-start bol)
1384 (goto-char xmltok-start)
1385 (skip-chars-backward " \t")
1387 (current-indentation)))))
1389 (defun nxml-compute-indent-from-previous-line ()
1390 "Compute the indent for a line using the indentation of a previous line."
1395 before-context after-context)
1396 (back-to-indentation)
1399 ;; Move backwards until the start of a non-blank line that is
1400 ;; not inside a token.
1402 (when (= (forward-line -1) -1)
1404 (back-to-indentation)
1405 (if (looking-at "[ \t]*$")
1408 (setq prev-bol (point)))
1410 (not (or (= xmltok-start (point))
1411 (eq xmltok-type 'data))))))
1413 ;; Now scan over tokens until the end of the line to be indented.
1414 ;; Determine the context before and after the beginning of the
1416 (while (< (point) eol)
1417 (nxml-tokenize-forward)
1418 (cond ((<= bol xmltok-start)
1420 (nxml-merge-indent-context-type after-context)))
1421 ((and (<= (point) bol)
1422 (not (and (eq xmltok-type 'partial-start-tag)
1424 (setq before-context
1425 (nxml-merge-indent-context-type before-context)))
1426 ((eq xmltok-type 'data)
1427 (setq before-context
1428 (nxml-merge-indent-context-type before-context))
1430 (nxml-merge-indent-context-type after-context)))
1431 ;; If in the middle of a token that looks inline,
1432 ;; then indent relative to the previous non-blank line
1433 ((eq (nxml-merge-indent-context-type before-context)
1435 (goto-char prev-bol)
1436 (throw 'indent (current-column)))
1439 (nxml-compute-indent-in-token bol))))
1440 (skip-chars-forward " \t\r\n"))
1443 (* nxml-child-indent
1444 (+ (if (eq before-context 'start-tag) 1 0)
1445 (if (eq after-context 'end-tag) -1 0))))))))
1447 (defun nxml-merge-indent-context-type (context)
1448 "Merge the indent context type CONTEXT with the token in `xmltok-type'.
1449 Return the merged indent context type. An indent context type is
1450 either nil or one of the symbols start-tag, end-tag, markup, comment,
1452 (cond ((memq xmltok-type '(start-tag partial-start-tag))
1453 (if (memq context '(nil start-tag comment))
1456 ((memq xmltok-type '(end-tag partial-end-tag))
1457 (if (memq context '(nil end-tag comment))
1460 ((eq xmltok-type 'comment)
1461 (cond ((memq context '(start-tag end-tag comment))
1468 (defun nxml-compute-indent-in-token (pos)
1469 "Return the indent for a line that starts inside a token.
1470 POS is the position of the first non-whitespace character of the line.
1471 This expects the xmltok-* variables to be set up as by `xmltok-forward'."
1472 (cond ((memq xmltok-type '(start-tag
1475 partial-empty-element))
1476 (nxml-compute-indent-in-start-tag pos))
1477 ((eq xmltok-type 'comment)
1478 (nxml-compute-indent-in-delimited-token pos "<!--" "-->"))
1479 ((eq xmltok-type 'cdata-section)
1480 (nxml-compute-indent-in-delimited-token pos "<![CDATA[" "]]>"))
1481 ((eq xmltok-type 'processing-instruction)
1482 (nxml-compute-indent-in-delimited-token pos "<?" "?>"))
1485 (if (and (= (forward-line -1) 0)
1486 (< xmltok-start (point)))
1487 (back-to-indentation)
1488 (goto-char xmltok-start))
1491 (defun nxml-compute-indent-in-start-tag (pos)
1492 "Return the indent for a line that starts inside a start-tag.
1493 Also for a line that starts inside an empty element.
1494 POS is the position of the first non-whitespace character of the line.
1495 This expects the xmltok-* variables to be set up as by `xmltok-forward'."
1496 (let ((value-boundary (nxml-attribute-value-boundary pos))
1499 ;; inside an attribute value
1500 (let ((value-start (car value-boundary))
1501 (value-end (cdr value-boundary)))
1504 (if (< (point) value-start)
1505 (goto-char value-start)
1506 (back-to-indentation)))
1507 ;; outside an attribute value
1509 (while (and (= (forward-line -1) 0)
1510 (nxml-attribute-value-boundary (point))))
1511 (cond ((<= (point) xmltok-start)
1512 (goto-char xmltok-start)
1513 (setq off nxml-attribute-indent)
1514 (let ((atts (xmltok-merge-attributes)))
1516 (let* ((att (car atts))
1517 (start (xmltok-attribute-name-start att)))
1522 (back-to-indentation))))
1523 (+ (current-column) off)))
1525 (defun nxml-attribute-value-boundary (pos)
1526 "Return a pair (START . END) if POS is inside an attribute value.
1527 Otherwise return nil. START and END are the positions of the start
1528 and end of the attribute value containing POS. This expects the
1529 xmltok-* variables to be set up as by `xmltok-forward'."
1530 (let ((atts (xmltok-merge-attributes))
1531 att value-start value-end value-boundary)
1533 (setq att (car atts))
1534 (setq value-start (xmltok-attribute-value-start att))
1535 (setq value-end (xmltok-attribute-value-end att))
1536 (cond ((and value-start (< pos value-start))
1538 ((and value-start value-end (<= pos value-end))
1539 (setq value-boundary (cons value-start value-end))
1541 (t (setq atts (cdr atts)))))
1544 (defun nxml-compute-indent-in-delimited-token (pos open-delim close-delim)
1545 "Return the indent for a line that starts inside a token with delimiters.
1546 OPEN-DELIM and CLOSE-DELIM are strings giving the opening and closing
1547 delimiters. POS is the position of the first non-whitespace character
1548 of the line. This expects the xmltok-* variables to be set up as by
1550 (cond ((let ((end (+ pos (length close-delim))))
1551 (and (<= end (point-max))
1552 (string= (buffer-substring-no-properties pos end)
1554 (goto-char xmltok-start))
1558 (<= (point) xmltok-start))
1559 (goto-char (+ xmltok-start (length open-delim)))
1560 (when (and (string= open-delim "<!--")
1562 (goto-char (1+ (point)))))
1563 (t (back-to-indentation)))
1568 (defun nxml-complete ()
1569 "Perform completion on the symbol preceding point.
1571 Inserts as many characters as can be completed. However, if not even
1572 one character can be completed, then a buffer with the possibilities
1573 is popped up and the symbol is read from the minibuffer with
1574 completion. If the symbol is complete, then any characters that must
1575 follow the symbol are also inserted.
1577 The name space used for completion and what is treated as a symbol
1578 depends on the context. The contexts in which completion is performed
1579 depend on `nxml-completion-hook'."
1581 (unless (run-hook-with-args-until-success 'nxml-completion-hook)
1582 ;; Eventually we will complete on entity names here.
1584 (message "Cannot complete in this context")))
1588 (defun nxml-forward-balanced-item (&optional arg)
1589 "Move forward across one balanced item.
1590 With ARG, do it that many times. Negative arg -N means
1591 move backward across N balanced expressions.
1592 This is the equivalent of `forward-sexp' for XML.
1594 An element contains as items strings with no markup, tags, processing
1595 instructions, comments, CDATA sections, entity references and
1596 characters references. However, if the variable
1597 `nxml-sexp-element-flag' is non-nil, then an element is treated as a
1598 single markup item. A start-tag contains an element name followed by
1599 one or more attributes. An end-tag contains just an element name. An
1600 attribute value literals contains strings with no markup, entity
1601 references and character references. A processing instruction
1602 consists of a target and a content string. A comment or a CDATA
1603 section contains a single string. An entity reference contains a
1604 single name. A character reference contains a character number."
1606 (or arg (setq arg 1))
1609 (nxml-forward-single-balanced-item)
1610 (> (setq arg (1- arg)) 0))))
1613 (nxml-backward-single-balanced-item)
1614 (< (setq arg (1+ arg)) 0))))))
1616 (defun nxml-forward-single-balanced-item ()
1618 (goto-char (let ((end (nxml-token-after)))
1620 (while (eq xmltok-type 'space)
1622 (setq end (nxml-token-after)))
1623 (cond ((/= (point) xmltok-start)
1624 (nxml-scan-forward-within end))
1625 ((and nxml-sexp-element-flag
1626 (eq xmltok-type 'start-tag))
1627 ;; can't ever return nil here
1628 (nxml-scan-element-forward xmltok-start))
1629 ((and nxml-sexp-element-flag
1631 '(end-tag partial-end-tag)))
1632 (error "Already at end of element"))
1635 (goto-char (cadr err))
1636 (apply 'error (cddr err)))))
1638 (defun nxml-backward-single-balanced-item ()
1640 (goto-char (let ((end (nxml-token-before)))
1642 (while (eq xmltok-type 'space)
1643 (goto-char xmltok-start)
1644 (setq end (nxml-token-before)))
1645 (cond ((/= (point) end)
1646 (nxml-scan-backward-within end))
1647 ((and nxml-sexp-element-flag
1648 (eq xmltok-type 'end-tag))
1649 ;; can't ever return nil here
1650 (nxml-scan-element-backward end)
1652 ((and nxml-sexp-element-flag
1653 (eq xmltok-type 'start-tag))
1654 (error "Already at start of element"))
1655 (t xmltok-start)))))
1657 (goto-char (cadr err))
1658 (apply 'error (cddr err)))))
1660 (defun nxml-scan-forward-within (end)
1661 (setq end (- end (nxml-end-delimiter-length xmltok-type)))
1662 (when (<= end (point))
1663 (error "Already at end of %s"
1664 (nxml-token-type-friendly-name xmltok-type)))
1665 (cond ((memq xmltok-type '(start-tag
1668 partial-empty-element))
1669 (if (< (point) xmltok-name-end)
1671 (let ((att (nxml-find-following-attribute)))
1672 (cond ((not att) end)
1673 ((and (xmltok-attribute-value-start att)
1674 (<= (xmltok-attribute-value-start att)
1676 (nxml-scan-forward-in-attribute-value att))
1677 ((xmltok-attribute-value-end att)
1678 (1+ (xmltok-attribute-value-end att)))
1680 (goto-char (xmltok-attribute-name-end att))
1681 (looking-at "[ \t\r\n]*="))
1683 (t (xmltok-attribute-name-end att))))))
1684 ((and (eq xmltok-type 'processing-instruction)
1685 (< (point) xmltok-name-end))
1689 (defun nxml-scan-backward-within (end)
1692 (nxml-start-delimiter-length xmltok-type)))
1693 (when (<= (point) xmltok-start)
1694 (error "Already at start of %s"
1695 (nxml-token-type-friendly-name xmltok-type)))
1696 (cond ((memq xmltok-type '(start-tag
1699 partial-empty-element))
1700 (let ((att (nxml-find-preceding-attribute)))
1701 (cond ((not att) xmltok-start)
1702 ((and (xmltok-attribute-value-start att)
1703 (<= (xmltok-attribute-value-start att)
1706 (xmltok-attribute-value-end att)))
1707 (nxml-scan-backward-in-attribute-value att))
1708 (t (xmltok-attribute-name-start att)))))
1709 ((and (eq xmltok-type 'processing-instruction)
1710 (let ((content-start (save-excursion
1711 (goto-char xmltok-name-end)
1712 (skip-chars-forward " \r\t\n")
1714 (and (< content-start (point))
1718 (defun nxml-scan-forward-in-attribute-value (att)
1719 (when (= (point) (xmltok-attribute-value-end att))
1720 (error "Already at end of attribute value"))
1721 (let ((refs (xmltok-attribute-refs att))
1724 (setq ref (car refs))
1725 (if (< (point) (aref ref 2))
1728 (setq refs (cdr refs))))
1730 (xmltok-attribute-value-end att))
1731 ((< (point) (aref ref 1))
1733 ((= (point) (aref ref 1))
1736 (let ((end (- (aref ref 2)
1737 (nxml-end-delimiter-length (aref ref 0)))))
1740 (error "Already at end of %s"
1741 (nxml-token-type-friendly-name (aref ref 0)))))))))
1743 (defun nxml-scan-backward-in-attribute-value (att)
1744 (when (= (point) (xmltok-attribute-value-start att))
1745 (error "Already at start of attribute value"))
1746 (let ((refs (reverse (xmltok-attribute-refs att)))
1749 (setq ref (car refs))
1750 (if (< (aref ref 1) (point))
1753 (setq refs (cdr refs))))
1755 (xmltok-attribute-value-start att))
1756 ((< (aref ref 2) (point))
1758 ((= (point) (aref ref 2))
1761 (let ((start (+ (aref ref 1)
1762 (nxml-start-delimiter-length (aref ref 0)))))
1763 (if (< start (point))
1765 (error "Already at start of %s"
1766 (nxml-token-type-friendly-name (aref ref 0)))))))))
1768 (defun nxml-find-following-attribute ()
1770 (atts (or xmltok-attributes xmltok-namespace-attributes))
1771 (more-atts (and xmltok-attributes xmltok-namespace-attributes)))
1773 (let* ((att (car atts))
1774 (name-start (xmltok-attribute-name-start att)))
1775 (cond ((and (<= name-start (point))
1776 (xmltok-attribute-value-end att)
1777 ;; <= because end is before quote
1778 (<= (point) (xmltok-attribute-value-end att)))
1781 ((and (< (point) name-start)
1784 (xmltok-attribute-name-start ret))))
1786 (setq atts (cdr atts))
1788 (setq atts more-atts)
1789 (setq more-atts nil)))
1792 (defun nxml-find-preceding-attribute ()
1794 (atts (or xmltok-attributes xmltok-namespace-attributes))
1795 (more-atts (and xmltok-attributes xmltok-namespace-attributes)))
1797 (let* ((att (car atts))
1798 (name-start (xmltok-attribute-name-start att)))
1799 (cond ((and (< name-start (point))
1800 (xmltok-attribute-value-end att)
1801 ;; <= because end is before quote
1802 (<= (point) (xmltok-attribute-value-end att)))
1805 ((and (< name-start (point))
1807 (< (xmltok-attribute-name-start ret)
1810 (setq atts (cdr atts))
1812 (setq atts more-atts)
1813 (setq more-atts nil)))
1816 (defun nxml-up-element (&optional arg)
1818 (or arg (setq arg 1))
1820 (nxml-backward-up-element (- arg))
1822 (while (and (> arg 0)
1823 (< (point) (point-max)))
1824 (let ((token-end (nxml-token-after)))
1825 (goto-char (cond ((or (memq xmltok-type '(end-tag
1827 (and (memq xmltok-type
1829 partial-empty-element))
1830 (< xmltok-start (point))))
1832 ((nxml-scan-element-forward
1833 (if (and (eq xmltok-type 'start-tag)
1834 (= (point) xmltok-start))
1838 (t (error "No parent element")))))
1839 (setq arg (1- arg)))
1841 (goto-char (cadr err))
1842 (apply 'error (cddr err))))))
1844 (defun nxml-backward-up-element (&optional arg)
1846 (or arg (setq arg 1))
1848 (nxml-up-element (- arg))
1850 (while (and (> arg 0)
1851 (< (point-min) (point)))
1852 (let ((token-end (nxml-token-before)))
1853 (goto-char (cond ((or (memq xmltok-type '(start-tag
1855 (and (memq xmltok-type
1857 partial-empty-element))
1858 (< (point) token-end)))
1860 ((nxml-scan-element-backward
1861 (if (and (eq xmltok-type 'end-tag)
1862 (= (point) token-end))
1867 (t (error "No parent element")))))
1868 (setq arg (1- arg)))
1870 (goto-char (cadr err))
1871 (apply 'error (cddr err))))))
1873 (defun nxml-down-element (&optional arg)
1874 "Move forward down into the content of an element.
1875 With ARG, do this that many times.
1876 Negative ARG means move backward but still down."
1878 (or arg (setq arg 1))
1880 (nxml-backward-down-element (- arg))
1883 (let ((token-end (nxml-token-after)))
1885 (goto-char token-end)
1887 (when (memq xmltok-type '(nil end-tag partial-end-tag))
1888 (error "No following start-tags in this element"))
1889 (not (memq xmltok-type '(start-tag partial-start-tag))))
1890 (nxml-tokenize-forward))
1892 (setq arg (1- arg)))))
1894 (defun nxml-backward-down-element (&optional arg)
1896 (or arg (setq arg 1))
1898 (nxml-down-element (- arg))
1903 (goto-char xmltok-start)
1905 (when (memq xmltok-type '(start-tag
1909 (error "No preceding end-tags in this element"))
1910 (not (memq xmltok-type '(end-tag partial-end-tag))))
1911 (if (or (<= (point) nxml-prolog-end)
1912 (not (search-backward "<" nxml-prolog-end t)))
1913 (setq xmltok-type nil)
1914 (nxml-move-outside-backwards)
1917 (setq arg (1- arg)))))
1919 (defun nxml-forward-element (&optional arg)
1920 "Move forward over one element.
1921 With ARG, do it that many times.
1922 Negative ARG means move backward."
1924 (or arg (setq arg 1))
1926 (nxml-backward-element (- arg))
1928 (while (and (> arg 0)
1929 (< (point) (point-max)))
1931 (or (nxml-scan-element-forward (nxml-token-before))
1932 (error "No more elements")))
1933 (setq arg (1- arg)))
1935 (goto-char (cadr err))
1936 (apply 'error (cddr err))))))
1938 (defun nxml-backward-element (&optional arg)
1939 "Move backward over one element.
1940 With ARG, do it that many times.
1941 Negative ARG means move forward."
1943 (or arg (setq arg 1))
1945 (nxml-forward-element (- arg))
1947 (while (and (> arg 0)
1948 (< (point-min) (point)))
1950 (or (and (nxml-scan-element-backward (progn
1954 (error "No preceding elements")))
1955 (setq arg (1- arg)))
1957 (goto-char (cadr err))
1958 (apply 'error (cddr err))))))
1960 (defun nxml-mark-token-after ()
1962 (push-mark (nxml-token-after) nil t)
1963 (goto-char xmltok-start)
1964 (message "Marked %s" xmltok-type))
1968 (defun nxml-mark-paragraph ()
1969 "Put point at beginning of this paragraph, mark at end.
1970 The paragraph marked is the one that contains point or follows point."
1972 (nxml-forward-paragraph)
1974 (nxml-backward-paragraph))
1976 (defun nxml-forward-paragraph (&optional arg)
1978 (or arg (setq arg 1))
1980 (nxml-backward-paragraph (- arg)))
1983 (while (and (nxml-forward-single-paragraph)
1984 (> (setq arg (1- arg)) 0))))))
1986 (defun nxml-backward-paragraph (&optional arg)
1988 (or arg (setq arg 1))
1990 (nxml-forward-paragraph (- arg)))
1993 (let ((inhibit-field-text-motion t))
1995 (while (and (nxml-backward-single-paragraph)
1996 (> (setq arg (1- arg)) 0))))))
1998 (defun nxml-forward-single-paragraph ()
1999 "Move forward over a single paragraph.
2000 Return nil at end of buffer, t otherwise."
2001 (let* ((token-end (nxml-token-after))
2002 (offset (- (point) xmltok-start))
2004 (goto-char token-end)
2005 (while (and (< (point) (point-max))
2007 (nxml-paragraph-end-pos had-data offset))))
2008 (when (nxml-token-contains-data-p offset)
2010 (nxml-tokenize-forward)
2012 (when pos (goto-char pos))))
2014 (defun nxml-backward-single-paragraph ()
2015 "Move backward over a single paragraph.
2016 Return nil at start of buffer, t otherwise."
2017 (let* ((token-end (nxml-token-before))
2018 (offset (- token-end (point)))
2019 (last-tag-pos xmltok-start)
2020 pos had-data last-data-pos)
2021 (goto-char token-end)
2022 (unless (setq pos (nxml-paragraph-start-pos nil offset))
2023 (setq had-data (nxml-token-contains-data-p nil offset))
2024 (goto-char xmltok-start)
2025 (while (and (not pos) (< (point-min) (point)))
2026 (cond ((search-backward "<" nxml-prolog-end t)
2027 (nxml-move-outside-backwards)
2029 (while (< (point) last-tag-pos)
2031 (when (and (not had-data) (nxml-token-contains-data-p))
2033 (setq last-data-pos xmltok-start))
2034 (let ((tem (nxml-paragraph-start-pos had-data 0)))
2035 (when tem (setq pos tem)))))
2036 (when (and (not had-data) last-data-pos (not pos))
2039 (while (< (point) last-data-pos)
2041 (let ((tem (nxml-paragraph-start-pos had-data 0)))
2042 (when tem (setq pos tem)))))
2043 (setq last-tag-pos (point)))
2044 (t (goto-char (point-min))))))
2045 (when pos (goto-char pos))))
2047 (defun nxml-token-contains-data-p (&optional start end)
2048 (setq start (+ xmltok-start (or start 0)))
2049 (setq end (- (point) (or end 0)))
2050 (when (eq xmltok-type 'cdata-section)
2051 (setq start (max start (+ xmltok-start 9)))
2052 (setq end (min end (- (point) 3))))
2053 (or (and (eq xmltok-type 'data)
2054 (eq start xmltok-start)
2056 (eq xmltok-type 'char-ref)
2057 (and (memq xmltok-type '(data cdata-section))
2061 (re-search-forward "[^ \t\r\n]" end t)))))
2063 (defun nxml-paragraph-end-pos (had-data offset)
2064 "Return the position of the paragraph end if contained in the current token.
2065 Return nil if the current token does not contain the paragraph end.
2066 Only characters after OFFSET from the start of the token are eligible.
2067 HAD-DATA says whether there have been non-whitespace data characters yet."
2068 (cond ((not had-data)
2069 (cond ((memq xmltok-type '(data cdata-section))
2071 (let ((end (point)))
2072 (goto-char (+ xmltok-start
2073 (max (if (eq xmltok-type 'cdata-section)
2077 (and (re-search-forward "[^ \t\r\n]" end t)
2078 (re-search-forward "^[ \t]*$" end t)
2079 (match-beginning 0)))))
2080 ((and (eq xmltok-type 'comment)
2081 (nxml-token-begins-line-p)
2082 (nxml-token-ends-line-p))
2084 (let ((end (point)))
2085 (goto-char (+ xmltok-start (max 4 offset)))
2086 (when (re-search-forward "[^ \t\r\n]" (- end 3) t)
2087 (if (re-search-forward "^[ \t]*$" end t)
2089 (goto-char (- end 3))
2090 (skip-chars-backward " \t")
2092 (beginning-of-line 2))
2094 ((memq xmltok-type '(data space cdata-section))
2096 (let ((end (point)))
2097 (goto-char (+ xmltok-start offset))
2098 (and (re-search-forward "^[ \t]*$" end t)
2099 (match-beginning 0)))))
2100 ((and (memq xmltok-type '(start-tag
2104 processing-instruction
2106 (nxml-token-begins-line-p)
2107 (nxml-token-ends-line-p))
2109 (goto-char xmltok-start)
2110 (skip-chars-backward " \t")
2112 ((and (eq xmltok-type 'end-tag)
2113 (looking-at "[ \t]*$")
2114 (not (nxml-in-mixed-content-p t)))
2116 (or (search-forward "\n" nil t)
2119 (defun nxml-paragraph-start-pos (had-data offset)
2120 "Return the position of the paragraph start if contained in the current token.
2121 Return nil if the current token does not contain the paragraph start.
2122 Only characters before OFFSET from the end of the token are eligible.
2123 HAD-DATA says whether there have been non-whitespace data characters yet."
2124 (cond ((not had-data)
2125 (cond ((memq xmltok-type '(data cdata-section))
2127 (goto-char (- (point)
2128 (max (if (eq xmltok-type 'cdata-section)
2132 (and (re-search-backward "[^ \t\r\n]" xmltok-start t)
2133 (re-search-backward "^[ \t]*$" xmltok-start t)
2134 (match-beginning 0))))
2135 ((and (eq xmltok-type 'comment)
2136 (nxml-token-ends-line-p)
2137 (nxml-token-begins-line-p))
2139 (goto-char (- (point) (max 3 offset)))
2140 (when (and (< (+ xmltok-start 4) (point))
2141 (re-search-backward "[^ \t\r\n]"
2144 (if (re-search-backward "^[ \t]*$" xmltok-start t)
2146 (goto-char xmltok-start)
2147 (if (looking-at "<!--[ \t]*\n")
2149 (skip-chars-backward " \t")
2151 ((memq xmltok-type '(data space cdata-section))
2153 (goto-char (- (point) offset))
2154 (and (re-search-backward "^[ \t]*$" xmltok-start t)
2155 (match-beginning 0))))
2156 ((and (memq xmltok-type '(start-tag
2160 processing-instruction
2162 (nxml-token-ends-line-p)
2163 (nxml-token-begins-line-p))
2164 (or (search-forward "\n" nil t)
2166 ((and (eq xmltok-type 'start-tag)
2167 (nxml-token-begins-line-p)
2168 (not (save-excursion
2169 (goto-char xmltok-start)
2170 (nxml-in-mixed-content-p nil))))
2172 (goto-char xmltok-start)
2173 (skip-chars-backward " \t")
2174 ;; include any blank line before
2175 (or (and (eq (char-before) ?\n)
2177 (goto-char (1- (point)))
2178 (skip-chars-backward " \t")
2179 (and (bolp) (point))))
2182 (defun nxml-token-ends-line-p () (looking-at "[ \t]*$"))
2184 (defun nxml-token-begins-line-p ()
2186 (goto-char xmltok-start)
2187 (skip-chars-backward " \t")
2190 (defun nxml-in-mixed-content-p (endp)
2191 "Return non-nil if point is in mixed content.
2192 Point must be after an end-tag or before a start-tag.
2193 ENDP is t in the former case, nil in the latter."
2194 (let (matching-tag-pos)
2195 (cond ((not (run-hook-with-args-until-failure
2196 'nxml-in-mixed-content-hook))
2198 ;; See if the matching tag does not start or end a line.
2199 ((condition-case err
2201 (setq matching-tag-pos
2204 (and (nxml-scan-element-backward (point))
2206 (nxml-scan-element-forward (point)))))
2207 (and matching-tag-pos
2209 (goto-char matching-tag-pos)
2212 (skip-chars-backward " \t")
2214 (looking-at "[ \t]*$"))))))
2215 (nxml-scan-error nil))
2217 ;; See if there's data at the same level.
2220 (setq start matching-tag-pos
2223 end matching-tag-pos))
2227 (nxml-preceding-sibling-data-p))
2230 (nxml-following-sibling-data-p)))))
2232 ;; Otherwise, treat as not mixed
2235 (defun nxml-preceding-sibling-data-p ()
2236 "Return non-nil if there is a previous sibling that is data."
2237 (let ((lim (max (- (point) nxml-mixed-scan-distance)
2243 (while (and (< lim (point))
2248 (search-backward "<" lim t)))
2249 (nxml-move-outside-backwards)
2252 (let ((prev-level level))
2253 (cond ((eq xmltok-type 'end-tag)
2254 (setq level (1+ level)))
2255 ((eq xmltok-type 'start-tag)
2256 (setq level (1- level))))
2257 (when (eq prev-level 0)
2258 (while (and (< (point) end) (not found))
2260 (when (memq xmltok-type '(data cdata-section char-ref))
2261 (setq found t)))))))))
2264 (defun nxml-following-sibling-data-p ()
2265 (let ((lim (min (+ (point) nxml-mixed-scan-distance)
2271 (while (and (< (point) lim)
2273 (nxml-tokenize-forward)
2275 (cond ((eq xmltok-type 'start-tag)
2276 (setq level (1+ level)))
2277 ((eq xmltok-type 'end-tag)
2278 (setq level (1- level)))
2280 (memq xmltok-type '(data cdata-section char-ref)))
2286 (defun nxml-do-fill-paragraph (arg)
2287 (let (fill-paragraph-function
2291 (nxml-forward-paragraph)
2293 (nxml-backward-paragraph)
2294 (skip-chars-forward " \t\r\n")
2295 (setq start (point))
2297 (setq fill-prefix (buffer-substring-no-properties (point) start))
2298 (when (and (not (nxml-get-inside (point)))
2299 (looking-at "[ \t]*<!--"))
2300 (setq fill-prefix (concat fill-prefix " ")))
2301 (fill-region-as-paragraph start end arg))
2302 (skip-line-prefix fill-prefix)
2305 (defun nxml-newline-and-indent (soft)
2306 (delete-horizontal-space)
2307 (if soft (insert-and-inherit ?\n) (newline 1))
2313 (defvar nxml-dynamic-markup-prev-pos nil)
2314 (defvar nxml-dynamic-markup-prev-lengths nil)
2315 (defvar nxml-dynamic-markup-prev-found-marker nil)
2316 (defvar nxml-dynamic-markup-prev-start-tags (make-hash-table :test 'equal))
2318 (defun nxml-dynamic-markup-word ()
2319 "Dynamically markup the word before point.
2320 This attempts to find a tag to put around the word before point based
2321 on the contents of the current buffer. The end-tag will be inserted at
2322 point. The start-tag will be inserted at or before the beginning of
2323 the word before point; the contents of the current buffer is used to
2326 It works in a similar way to \\[dabbrev-expand]. It searches first
2327 backwards from point, then forwards from point for an element whose
2328 content is a string which matches the contents of the buffer before
2329 point and which includes at least the word before point. It then
2330 copies the start- and end-tags from that element and uses them to
2331 surround the matching string before point.
2333 Repeating \\[nxml-dynamic-markup-word] immediately after successful
2334 \\[nxml-dynamic-markup-word] removes the previously inserted markup
2335 and attempts to find another possible way to do the markup."
2337 (let (search-start-pos done)
2338 (if (and (integerp nxml-dynamic-markup-prev-pos)
2339 (= nxml-dynamic-markup-prev-pos (point))
2340 (eq last-command this-command)
2341 nxml-dynamic-markup-prev-lengths)
2342 (let* ((end-tag-open-pos
2343 (- nxml-dynamic-markup-prev-pos
2344 (nth 2 nxml-dynamic-markup-prev-lengths)))
2345 (start-tag-close-pos
2347 (nth 1 nxml-dynamic-markup-prev-lengths)))
2349 (- start-tag-close-pos
2350 (nth 0 nxml-dynamic-markup-prev-lengths))))
2351 (delete-region end-tag-open-pos nxml-dynamic-markup-prev-pos)
2352 (delete-region start-tag-open-pos start-tag-close-pos)
2353 (setq search-start-pos
2354 (marker-position nxml-dynamic-markup-prev-found-marker)))
2355 (clrhash nxml-dynamic-markup-prev-start-tags))
2356 (setq nxml-dynamic-markup-prev-pos nil)
2357 (setq nxml-dynamic-markup-prev-lengths nil)
2358 (setq nxml-dynamic-markup-prev-found-marker nil)
2361 (let* ((pos (point))
2364 (unless (< (point) pos)
2365 (error "No word to markup"))
2366 (buffer-substring-no-properties (point) pos)))
2367 (search (concat word "</"))
2369 (when search-start-pos
2370 (goto-char search-start-pos))
2371 (while (and (not done)
2372 (or (and (< (point) pos)
2373 (or (search-backward search nil t)
2374 (progn (goto-char pos) nil)))
2375 (search-forward search nil t)))
2376 (goto-char (- (match-end 0) 2))
2377 (setq done (nxml-try-copy-markup pos)))
2379 (error (if (zerop (hash-table-count
2380 nxml-dynamic-markup-prev-start-tags))
2381 "No possible markup found for `%s'"
2382 "No more markup possibilities found for `%s'")
2385 (defun nxml-try-copy-markup (word-end-pos)
2387 (let ((end-tag-pos (point)))
2388 (when (and (not (nxml-get-inside end-tag-pos))
2389 (search-backward "<" nil t)
2390 (not (nxml-get-inside (point))))
2392 (when (and (eq xmltok-type 'start-tag)
2393 (< (point) end-tag-pos))
2394 (let* ((start-tag-close-pos (point))
2396 (buffer-substring-no-properties xmltok-start
2397 start-tag-close-pos))
2401 (buffer-substring-no-properties start-tag-close-pos
2404 (goto-char word-end-pos)
2406 (re-search-backward (concat
2407 (regexp-quote (car words))
2411 (setq words (cdr words))
2412 (skip-chars-backward " \t\r\n"))
2413 (when (and (not words)
2415 (skip-chars-forward " \t\r\n")
2416 (not (gethash (cons (point) start-tag)
2417 nxml-dynamic-markup-prev-start-tags)))
2418 (or (< end-tag-pos (point))
2419 (< word-end-pos xmltok-start)))
2420 (setq nxml-dynamic-markup-prev-found-marker
2421 (copy-marker end-tag-pos t))
2422 (puthash (cons (point) start-tag)
2424 nxml-dynamic-markup-prev-start-tags)
2425 (setq nxml-dynamic-markup-prev-lengths
2426 (list (- start-tag-close-pos xmltok-start)
2427 (- word-end-pos (point))
2428 (+ (- xmltok-name-end xmltok-start) 2)))
2429 (let ((name (xmltok-start-tag-qname)))
2431 (goto-char (+ word-end-pos
2432 (- start-tag-close-pos xmltok-start)))
2433 (insert "</" name ">")
2434 (setq nxml-dynamic-markup-prev-pos (point))))))))))
2439 (defvar nxml-char-name-ignore-case t)
2441 (defvar nxml-char-name-alist nil
2442 "Alist of character names.
2443 Each member of the list has the form (NAME CODE . NAMESET),
2444 where NAME is a string naming a character, NAMESET is a symbol
2445 identifying a set of names and CODE is an integer specifying the
2446 Unicode scalar value of the named character.
2447 The NAME will only be used for completion if NAMESET has
2448 a non-nil `nxml-char-name-set-enabled' property.
2449 If NAMESET does does not have `nxml-char-name-set-defined' property,
2450 then it must have a `nxml-char-name-set-file' property and `load'
2451 will be applied to the value of this property if the nameset
2454 (defvar nxml-char-name-table (make-hash-table :test 'eq)
2455 "Hash table for mapping char codes to names.
2456 Each key is a Unicode scalar value.
2457 Each value is a list of pairs of the form (NAMESET . NAME),
2458 where NAMESET is a symbol identifying a set of names,
2459 and NAME is a string naming a character.")
2461 (defvar nxml-autoload-char-name-set-list nil
2462 "List of char namesets that can be autoloaded.")
2464 (defun nxml-enable-char-name-set (nameset)
2465 (put nameset 'nxml-char-name-set-enabled t))
2467 (defun nxml-disable-char-name-set (nameset)
2468 (put nameset 'nxml-char-name-set-enabled nil))
2470 (defun nxml-char-name-set-enabled-p (nameset)
2471 (get nameset 'nxml-char-name-set-enabled))
2473 (defun nxml-autoload-char-name-set (nameset file)
2474 (unless (memq nameset nxml-autoload-char-name-set-list)
2475 (setq nxml-autoload-char-name-set-list
2476 (cons nameset nxml-autoload-char-name-set-list)))
2477 (put nameset 'nxml-char-name-set-file file))
2479 (defun nxml-define-char-name-set (nameset alist)
2480 "Define a set of character names.
2481 NAMESET is a symbol identifying the set.
2482 Alist is a list where each member has the form (NAME CODE),
2483 where NAME is a string naming a character and code
2484 is an integer giving the Unicode scalar value of the character."
2485 (when (get nameset 'nxml-char-name-set-defined)
2486 (error "Nameset `%s' already defined" nameset))
2489 (let* ((name-code (car iter))
2490 (name (car name-code))
2491 (code (cadr name-code)))
2493 (cons (cons nameset name)
2494 (gethash code nxml-char-name-table))
2495 nxml-char-name-table))
2496 (setcdr (cdr (car iter)) nameset)
2497 (setq iter (cdr iter))))
2498 (setq nxml-char-name-alist
2499 (nconc alist nxml-char-name-alist))
2500 (put nameset 'nxml-char-name-set-defined t))
2502 (defun nxml-get-char-name (code)
2503 (mapc 'nxml-maybe-load-char-name-set nxml-autoload-char-name-set-list)
2504 (let ((names (gethash code nxml-char-name-table))
2506 (while (and names (not name))
2507 (if (nxml-char-name-set-enabled-p (caar names))
2508 (setq name (cdar names))
2509 (setq names (cdr names))))
2512 (defvar nxml-named-char-history nil)
2514 (defun nxml-insert-named-char (arg)
2515 "Insert a character using its name.
2516 The name is read from the minibuffer.
2517 Normally, inserts the character as a numeric character reference.
2518 With a prefix argument, inserts the character directly."
2520 (mapc 'nxml-maybe-load-char-name-set nxml-autoload-char-name-set-list)
2522 (let ((completion-ignore-case nxml-char-name-ignore-case))
2523 (completing-read "Character name: "
2524 nxml-char-name-alist
2526 (get (cddr member) 'nxml-char-name-set-enabled))
2529 'nxml-named-char-history)))
2530 (alist nxml-char-name-alist)
2532 (while (and alist (not code))
2533 (setq elt (assoc name alist))
2534 (if (get (cddr elt) 'nxml-char-name-set-enabled)
2535 (setq code (cadr elt))
2536 (setq alist (cdr (member elt alist)))))
2539 (or (decode-char 'ucs code)
2540 (error "Character %x is not supported by Emacs"
2542 (format "&#x%X;" code))))))
2544 (defun nxml-maybe-load-char-name-set (sym)
2545 (when (and (get sym 'nxml-char-name-set-enabled)
2546 (not (get sym 'nxml-char-name-set-defined))
2547 (stringp (get sym 'nxml-char-name-set-file)))
2548 (load (get sym 'nxml-char-name-set-file))))
2550 (defun nxml-toggle-char-ref-extra-display (arg)
2551 "*Toggle the display of extra information for character references."
2553 (let ((new (if (null arg)
2554 (not nxml-char-ref-extra-display)
2555 (> (prefix-numeric-value arg) 0))))
2556 (when (not (eq new nxml-char-ref-extra-display))
2557 (setq nxml-char-ref-extra-display new)
2561 (if nxml-char-ref-extra-display
2562 (nxml-with-unmodifying-text-property-changes
2563 (nxml-clear-fontified (point-min) (point-max)))
2564 (nxml-clear-char-ref-extra-display (point-min) (point-max))))))))
2566 (put 'nxml-char-ref 'evaporate t)
2568 (defun nxml-char-ref-display-extra (start end n)
2569 (when nxml-char-ref-extra-display
2570 (let ((name (nxml-get-char-name n))
2571 (glyph-string (and nxml-char-ref-display-glyph-flag
2572 (nxml-glyph-display-string n 'nxml-glyph)))
2574 (when (or name glyph-string)
2575 (setq ov (make-overlay start end nil t))
2576 (overlay-put ov 'category 'nxml-char-ref)
2578 (overlay-put ov 'help-echo name))
2582 (propertize glyph-string 'face 'nxml-glyph)))))))
2584 (defun nxml-clear-char-ref-extra-display (start end)
2585 (let ((ov (overlays-in start end)))
2587 (when (eq (overlay-get (car ov) 'category) 'nxml-char-ref)
2588 (delete-overlay (car ov)))
2589 (setq ov (cdr ov)))))
2592 (defun nxml-start-delimiter-length (type)
2593 (or (get type 'nxml-start-delimiter-length)
2596 (put 'cdata-section 'nxml-start-delimiter-length 9)
2597 (put 'comment 'nxml-start-delimiter-length 4)
2598 (put 'processing-instruction 'nxml-start-delimiter-length 2)
2599 (put 'start-tag 'nxml-start-delimiter-length 1)
2600 (put 'empty-element 'nxml-start-delimiter-length 1)
2601 (put 'partial-empty-element 'nxml-start-delimiter-length 1)
2602 (put 'entity-ref 'nxml-start-delimiter-length 1)
2603 (put 'char-ref 'nxml-start-delimiter-length 2)
2605 (defun nxml-end-delimiter-length (type)
2606 (or (get type 'nxml-end-delimiter-length)
2609 (put 'cdata-section 'nxml-end-delimiter-length 3)
2610 (put 'comment 'nxml-end-delimiter-length 3)
2611 (put 'processing-instruction 'nxml-end-delimiter-length 2)
2612 (put 'start-tag 'nxml-end-delimiter-length 1)
2613 (put 'empty-element 'nxml-end-delimiter-length 2)
2614 (put 'partial-empty-element 'nxml-end-delimiter-length 1)
2615 (put 'entity-ref 'nxml-end-delimiter-length 1)
2616 (put 'char-ref 'nxml-end-delimiter-length 1)
2618 (defun nxml-token-type-friendly-name (type)
2619 (or (get type 'nxml-friendly-name)
2620 (symbol-name type)))
2622 (put 'cdata-section 'nxml-friendly-name "CDATA section")
2623 (put 'processing-instruction 'nxml-friendly-name "processing instruction")
2624 (put 'entity-ref 'nxml-friendly-name "entity reference")
2625 (put 'char-ref 'nxml-friendly-name "character reference")
2627 (provide 'nxml-mode)
2629 ;; arch-tag: 8603bc5f-1ef9-4021-b223-322fb2ca708e
2630 ;;; nxml-mode.el ends here