1 ;;; nxml-mode.el --- a new XML mode -*- lexical-binding:t -*-
3 ;; Copyright (C) 2003-2004, 2007-2014 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-lib
))
40 ;; nxml-mode calls rng-nxml-mode-init, which is autoloaded from rng-nxml.
41 ;; So we might as well just require it and silence the compiler.
42 (provide 'nxml-mode
) ; avoid recursive require
48 "New XML editing mode."
51 (defgroup nxml-faces nil
52 "Faces for XML syntax highlighting."
55 (defcustom nxml-char-ref-display-glyph-flag t
56 "Non-nil means display glyph following character reference.
57 The glyph is displayed in face `nxml-glyph'. The abnormal hook
58 `nxml-glyph-set-functions' can be used to change the characters
59 for which glyphs are displayed."
63 (defcustom nxml-sexp-element-flag nil
64 "Non-nil means sexp commands treat an element as a single expression."
68 (defcustom nxml-slash-auto-complete-flag nil
69 "Non-nil means typing a slash automatically completes the end-tag.
70 This is used by `nxml-electric-slash'."
74 (defcustom nxml-child-indent
2
75 "Indentation for the children of an element relative to the start-tag.
76 This only applies when the line or lines containing the start-tag contains
77 nothing else other than that start-tag."
81 (defcustom nxml-attribute-indent
4
82 "Indentation for the attributes of an element relative to the start-tag.
83 This only applies when the first attribute of a tag starts a line.
84 In other cases, the first attribute on one line is indented the same
85 as the first attribute on the previous line."
89 (defcustom nxml-bind-meta-tab-to-complete-flag t
90 "Non-nil means to use nXML completion in \\[completion-at-point]."
94 (defcustom nxml-prefer-utf-16-to-utf-8-flag nil
95 "Non-nil means prefer UTF-16 to UTF-8 when saving a buffer.
96 This is used only when a buffer does not contain an encoding declaration
97 and when its current `buffer-file-coding-system' specifies neither UTF-16
102 (defcustom nxml-prefer-utf-16-little-to-big-endian-flag
(eq system-type
104 "Non-nil means prefer little-endian to big-endian byte-order for UTF-16.
105 This is used only for saving a buffer; when reading the byte-order is
106 auto-detected. It may be relevant both when there is no encoding declaration
107 and when the encoding declaration specifies `UTF-16'."
111 (defcustom nxml-default-buffer-file-coding-system nil
112 "Default value for `buffer-file-coding-system' for a buffer for a new file.
113 A value of nil means use the default value of `buffer-file-coding-system' as normal.
114 A buffer's `buffer-file-coding-system' affects what \\[nxml-insert-xml-declaration] inserts."
116 :type
'coding-system
)
118 (defcustom nxml-auto-insert-xml-declaration-flag nil
119 "Non-nil means automatically insert an XML declaration in a new file.
120 The XML declaration is inserted using `nxml-insert-xml-declaration'."
124 (defface nxml-delimited-data
125 '((t (:inherit font-lock-doc-face
)))
126 "Face used to highlight data enclosed between delimiters.
127 This is not used directly, but only via inheritance by other faces."
131 '((t (:inherit font-lock-builtin-face
)))
132 "Face used to highlight various names.
133 This includes element and attribute names, processing
134 instruction targets and the CDATA keyword in a CDATA section.
135 This is not used directly, but only via inheritance by other faces."
139 '((t (:inherit font-lock-constant-face
)))
140 "Face used to highlight character and entity references.
141 This is not used directly, but only via inheritance by other faces."
144 (defface nxml-delimiter
146 "Face used to highlight delimiters.
147 This is not used directly, but only via inheritance by other faces."
152 "Face used to highlight text."
155 (defface nxml-comment-content
156 '((t (:inherit font-lock-comment-face
)))
157 "Face used to highlight the content of comments."
160 (defface nxml-comment-delimiter
161 '((t (:inherit font-lock-comment-delimiter-face
)))
162 "Face used for the delimiters of comments, i.e., <!-- and -->."
165 (defface nxml-processing-instruction-delimiter
166 '((t (:inherit nxml-delimiter
)))
167 "Face used for the delimiters of processing instructions, i.e., <? and ?>."
170 (defface nxml-processing-instruction-target
171 '((t (:inherit font-lock-keyword-face
)))
172 "Face used for the target of processing instructions."
175 (defface nxml-processing-instruction-content
176 '((t (:inherit nxml-delimited-data
)))
177 "Face used for the content of processing instructions."
180 (defface nxml-cdata-section-delimiter
181 '((t (:inherit nxml-delimiter
)))
182 "Face used for the delimiters of CDATA sections, i.e., <![, [, and ]]>."
185 (defface nxml-cdata-section-CDATA
186 '((t (:inherit nxml-name
)))
187 "Face used for the CDATA keyword in CDATA sections."
190 (defface nxml-cdata-section-content
191 '((t (:inherit nxml-text
)))
192 "Face used for the content of CDATA sections."
195 (defface nxml-char-ref-number
196 '((t (:inherit nxml-ref
)))
197 "Face used for the number in character references.
198 This includes ths `x' in hex references."
201 (defface nxml-char-ref-delimiter
202 '((t (:inherit nxml-ref
)))
203 "Face used for the delimiters of character references, i.e., &# and ;."
206 (defface nxml-entity-ref-name
207 '((t (:inherit nxml-ref
)))
208 "Face used for the entity name in general entity references."
211 (defface nxml-entity-ref-delimiter
212 '((t (:inherit nxml-ref
)))
213 "Face used for the delimiters of entity references, i.e., & and ;."
216 (defface nxml-tag-delimiter
217 '((t (:inherit nxml-delimiter
)))
218 "Face used for the angle brackets delimiting tags.
219 `nxml-tag-slash' is used for slashes."
222 (defface nxml-tag-slash
223 '((t (:inherit nxml-tag-delimiter
)))
224 "Face used for slashes in tags, both in end-tags and empty-elements."
227 (defface nxml-element-prefix
228 '((t (:inherit nxml-name
)))
229 "Face used for the prefix of elements."
232 (defface nxml-element-colon
234 "Face used for the colon in element names."
237 (defface nxml-element-local-name
238 '((t (:inherit font-lock-function-name-face
)))
239 "Face used for the local name of elements."
242 (defface nxml-attribute-prefix
243 '((t (:inherit nxml-name
)))
244 "Face used for the prefix of attributes."
247 (defface nxml-attribute-colon
248 '((t (:inherit nxml-delimiter
)))
249 "Face used for the colon in attribute names."
252 (defface nxml-attribute-local-name
253 '((t (:inherit font-lock-variable-name-face
)))
254 "Face used for the local name of attributes."
257 (defface nxml-namespace-attribute-xmlns
258 '((t (:inherit nxml-attribute-prefix
)))
259 "Face used for `xmlns' in namespace attributes."
262 (defface nxml-namespace-attribute-colon
263 '((t (:inherit nxml-attribute-colon
)))
264 "Face used for the colon in namespace attributes."
267 (defface nxml-namespace-attribute-prefix
268 '((t (:inherit nxml-attribute-local-name
)))
269 "Face used for the prefix declared in namespace attributes."
272 (defface nxml-attribute-value
273 '((t (:inherit font-lock-string-face
)))
274 "Face used for the value of attributes."
277 (defface nxml-attribute-value-delimiter
278 '((t (:inherit nxml-attribute-value
)))
279 "Face used for the delimiters of attribute values."
282 (defface nxml-namespace-attribute-value
283 '((t (:inherit nxml-attribute-value
)))
284 "Face used for the value of namespace attributes."
287 (defface nxml-namespace-attribute-value-delimiter
288 '((t (:inherit nxml-attribute-value-delimiter
)))
289 "Face used for the delimiters of namespace attribute values."
292 (defface nxml-prolog-literal-delimiter
293 '((t (:inherit nxml-delimited-data
)))
294 "Face used for the delimiters of literals in the prolog."
297 (defface nxml-prolog-literal-content
298 '((t (:inherit nxml-delimited-data
)))
299 "Face used for the content of literals in the prolog."
302 (defface nxml-prolog-keyword
303 '((t (:inherit font-lock-keyword-face
)))
304 "Face used for keywords in the prolog."
307 (defface nxml-markup-declaration-delimiter
308 '((t (:inherit nxml-delimiter
)))
309 "Face used for the delimiters of markup declarations in the prolog.
310 The delimiters are <! and >."
314 '((t (:inherit nxml-name
)))
315 "Face used for # before a name in the prolog."
339 "Face used for glyph for char references."
344 (defvar nxml-parent-document nil
345 "The parent document for a part of a modular document.
346 Use `nxml-parent-document-set' to set it.")
347 (make-variable-buffer-local 'nxml-parent-document
)
348 (put 'nxml-parent-document
'safe-local-variable
'stringp
)
350 (defvar nxml-prolog-regions nil
351 "List of regions in the prolog to be fontified.
352 See the function `xmltok-forward-prolog' for more information.")
353 (make-variable-buffer-local 'nxml-prolog-regions
)
355 (defvar nxml-degraded nil
356 "Non-nil if currently operating in degraded mode.
357 Degraded mode is enabled when an internal error is encountered in the
358 fontification or after-change functions.")
359 (make-variable-buffer-local 'nxml-degraded
)
361 (defvar nxml-completion-hook nil
362 "Hook run by `nxml-complete'.
363 This hook is run until success.")
365 (defvar nxml-in-mixed-content-hook nil
366 "Hook to determine whether point is in mixed content.
367 The hook is called without arguments. It should return nil if it is
368 definitely not mixed; non-nil otherwise. The hook will be run until
369 one of the functions returns nil.")
371 (defvar nxml-mixed-scan-distance
4000
372 "Maximum distance from point to scan when checking for mixed content.")
374 (defvar nxml-end-tag-indent-scan-distance
4000
375 "Maximum distance from point to scan backwards when indenting end-tag.")
377 (defvar nxml-char-ref-extra-display t
378 "Non-nil means display extra information for character references.
379 The extra information consists of a tooltip with the character name
380 and, if `nxml-char-ref-display-glyph-flag' is non-nil, a glyph
381 corresponding to the referenced character following the character
383 (make-variable-buffer-local 'nxml-char-ref-extra-display
)
385 (defvar nxml-mode-map
386 (let ((map (make-sparse-keymap)))
387 (define-key map
"\M-\C-u" 'nxml-backward-up-element
)
388 (define-key map
"\M-\C-d" 'nxml-down-element
)
389 (define-key map
"\M-\C-n" 'nxml-forward-element
)
390 (define-key map
"\M-\C-p" 'nxml-backward-element
)
391 (define-key map
"\M-{" 'nxml-backward-paragraph
)
392 (define-key map
"\M-}" 'nxml-forward-paragraph
)
393 (define-key map
"\M-h" 'nxml-mark-paragraph
)
394 (define-key map
"\C-c\C-f" 'nxml-finish-element
)
395 (define-key map
"\C-c]" 'nxml-finish-element
)
396 (define-key map
"\C-c/" 'nxml-finish-element
)
397 (define-key map
"\C-c\C-m" 'nxml-split-element
)
398 (define-key map
"\C-c\C-b" 'nxml-balanced-close-start-tag-block
)
399 (define-key map
"\C-c\C-i" 'nxml-balanced-close-start-tag-inline
)
400 (define-key map
"\C-c\C-x" 'nxml-insert-xml-declaration
)
401 (define-key map
"\C-c\C-d" 'nxml-dynamic-markup-word
)
403 (define-key map
"\C-c\C-u" 'nxml-insert-named-char
)
404 (define-key map
"\C-c\C-o" nxml-outline-prefix-map
)
405 (define-key map
[S-mouse-2
] 'nxml-mouse-hide-direct-text-content
)
406 (define-key map
"/" 'nxml-electric-slash
)
407 (define-key map
"\M-\t" 'completion-at-point
)
409 "Keymap for nxml-mode.")
411 (defvar nxml-font-lock-keywords
412 '(nxml-fontify-matcher)
413 "Default font lock keywords for nxml-mode.")
415 (defsubst nxml-set-face
(start end face
)
416 (when (and face
(< start end
))
417 (font-lock-append-text-property start end
'face face
)))
419 (defun nxml-parent-document-set (parent-document)
420 "Set `nxml-parent-document' and inherit the DTD &c."
421 ;; FIXME: this does not work.
422 ;; the idea is that by inheriting some variables from the parent,
423 ;; `rng-validate-mode' will validate entities declared in the parent.
424 ;; alas, the most interesting variables (`rng-compile-table' et al)
425 ;; are circular and cannot be printed even with `print-circle'.
426 (interactive "fParent document")
427 (let (dtd current-schema current-schema-file-name compile-table
428 ipattern-table last-ipattern-index
)
429 (when (string= (file-truename parent-document
)
430 (file-truename buffer-file-name
))
431 (error "Parent document cannot be the same as the document"))
432 (with-current-buffer (find-file-noselect parent-document
)
434 current-schema rng-current-schema
435 current-schema-file-name rng-current-schema-file-name
436 compile-table rng-compile-table
437 ipattern-table rng-ipattern-table
438 last-ipattern-index rng-last-ipattern-index
439 parent-document buffer-file-name
))
441 rng-current-schema current-schema
442 rng-current-schema-file-name current-schema-file-name
443 rng-compile-table compile-table
444 rng-ipattern-table ipattern-table
445 rng-last-ipattern-index last-ipattern-index
446 nxml-parent-document parent-document
)
447 (message "Set parent document to %s" parent-document
)
448 (when rng-validate-mode
449 (rng-validate-while-idle (current-buffer)))))
452 (define-derived-mode nxml-mode text-mode
"nXML"
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 \\[nxml-finish-element] finishes the current element by inserting an end-tag.
459 C-c C-i closes a start-tag with `>' and then inserts a balancing end-tag
460 leaving point between the start-tag and end-tag.
461 \\[nxml-balanced-close-start-tag-block] is similar but for block rather than inline elements:
462 the start-tag, point, and end-tag are all left on separate lines.
463 If `nxml-slash-auto-complete-flag' is non-nil, then inserting a `</'
464 automatically inserts the rest of the end-tag.
466 \\[completion-at-point] performs completion on the symbol preceding point.
468 \\[nxml-dynamic-markup-word] uses the contents of the current buffer
469 to choose a tag to put around the word preceding point.
471 Sections of the document can be displayed in outline form. The
472 variable `nxml-section-element-name-regexp' controls when an element
473 is recognized as a section. The same key sequences that change
474 visibility in outline mode are used except that they start with C-c C-o
477 Validation is provided by the related minor-mode `rng-validate-mode'.
478 This also makes completion schema- and context- sensitive. Element
479 names, attribute names, attribute values and namespace URIs can all be
480 completed. By default, `rng-validate-mode' is automatically enabled.
481 You can toggle it using \\[rng-validate-mode] or change the default by
482 customizing `rng-nxml-auto-validate-flag'.
484 \\[indent-for-tab-command] indents the current line appropriately.
485 This can be customized using the variable `nxml-child-indent'
486 and the variable `nxml-attribute-indent'.
488 \\[nxml-insert-named-char] inserts a character reference using
489 the character's name (by default, the Unicode name).
490 \\[universal-argument] \\[nxml-insert-named-char] inserts the character directly.
492 The Emacs commands that normally operate on balanced expressions will
493 operate on XML markup items. Thus \\[forward-sexp] will move forward
494 across one markup item; \\[backward-sexp] will move backward across
495 one markup item; \\[kill-sexp] will kill the following markup item;
496 \\[mark-sexp] will mark the following markup item. By default, each
497 tag each treated as a single markup item; to make the complete element
498 be treated as a single markup item, set the variable
499 `nxml-sexp-element-flag' to t. For more details, see the function
500 `nxml-forward-balanced-item'.
502 \\[nxml-backward-up-element] and \\[nxml-down-element] move up and down the element structure.
504 Many aspects this mode can be customized using
505 \\[customize-group] nxml RET."
506 ;; (kill-all-local-variables)
507 (set (make-local-variable 'mode-line-process
) '((nxml-degraded "/degraded")))
508 ;; We'll determine the fill prefix ourselves
509 (make-local-variable 'adaptive-fill-mode
)
510 (setq adaptive-fill-mode nil
)
511 (make-local-variable 'forward-sexp-function
)
512 (setq forward-sexp-function
'nxml-forward-balanced-item
)
513 (make-local-variable 'indent-line-function
)
514 (setq indent-line-function
'nxml-indent-line
)
515 (make-local-variable 'fill-paragraph-function
)
516 (setq fill-paragraph-function
'nxml-do-fill-paragraph
)
518 ;; This doesn't seem to work too well;
519 ;; I think we should probably roll our own nxml-comment-dwim function.
520 (make-local-variable 'comment-indent-function
)
521 (setq comment-indent-function
'nxml-indent-line
)
522 (make-local-variable 'comment-start
)
523 (setq comment-start
"<!--")
524 (make-local-variable 'comment-start-skip
)
525 (setq comment-start-skip
"<!--[ \t\r\n]*")
526 (make-local-variable 'comment-end
)
527 (setq comment-end
"-->")
528 (make-local-variable 'comment-end-skip
)
529 (setq comment-end-skip
"[ \t\r\n]*-->")
530 (make-local-variable 'comment-line-break-function
)
531 (setq comment-line-break-function
'nxml-newline-and-indent
)
532 (use-local-map nxml-mode-map
)
536 (setq nxml-scan-end
(copy-marker (point-min) nil
))
537 (with-silent-modifications
538 (nxml-clear-inside (point-min) (point-max))
539 (nxml-with-invisible-motion
540 (nxml-scan-prolog)))))
541 (add-hook 'completion-at-point-functions
542 #'nxml-completion-at-point-function nil t
)
543 (setq-local syntax-propertize-function
#'nxml-after-change
)
544 (add-hook 'change-major-mode-hook
'nxml-cleanup nil t
)
546 ;; Emacs 23 handles the encoding attribute on the xml declaration
547 ;; transparently to nxml-mode, so there is no longer a need for the below
548 ;; hook. The hook also had the drawback of overriding explicit user
549 ;; instruction to save as some encoding other than utf-8.
550 ;;(add-hook 'write-contents-hooks 'nxml-prepare-to-save)
551 (when (not (and (buffer-file-name) (file-exists-p (buffer-file-name))))
552 (when (and nxml-default-buffer-file-coding-system
553 (not (local-variable-p 'buffer-file-coding-system
)))
554 (setq buffer-file-coding-system nxml-default-buffer-file-coding-system
))
555 (when nxml-auto-insert-xml-declaration-flag
556 (nxml-insert-xml-declaration)))
558 (setq font-lock-defaults
559 '(nxml-font-lock-keywords
560 t
; keywords-only; we highlight comments and strings here
561 nil
; font-lock-keywords-case-fold-search. XML is case sensitive
562 nil
; no special syntax table
563 nil
; no automatic syntactic fontification
564 (font-lock-extend-region-functions .
(nxml-extend-region))
565 (jit-lock-contextually . t
)
566 (font-lock-unfontify-region-function . nxml-unfontify-region
)))
569 (nxml-enable-unicode-char-name-sets))
571 (defun nxml-cleanup ()
572 "Clean up after nxml-mode."
573 ;; Disable associated minor modes.
574 (rng-validate-mode -
1)
575 ;; Clean up fontification.
578 (with-silent-modifications
579 (nxml-with-invisible-motion
580 (remove-text-properties (point-min) (point-max) '(face)))))
581 (remove-hook 'change-major-mode-hook
'nxml-cleanup t
))
583 (defun nxml-degrade (context err
)
584 (message "Internal nXML mode error in %s (%s), degrading"
586 (error-message-string err
))
588 (setq nxml-degraded t
)
589 (setq nxml-prolog-end
1)
593 (with-silent-modifications
594 (nxml-clear-inside (point-min) (point-max))))))
596 ;;; Change management
598 (defvar font-lock-beg
) (defvar font-lock-end
)
599 (defun nxml-debug-region (start end
)
601 (let ((font-lock-beg start
)
604 (goto-char font-lock-beg
)
605 (set-mark font-lock-end
)))
607 (defun nxml-after-change (start end
)
608 ;; Called via syntax-propertize-function.
609 (unless nxml-degraded
610 (nxml-with-degradation-on-error 'nxml-after-change
613 (nxml-with-invisible-motion
614 (nxml-after-change1 start end
))))))
616 (defun nxml-after-change1 (start end
)
617 "After-change bookkeeping.
618 Returns a cons cell containing a possibly-enlarged change region.
619 You must call `nxml-extend-region' on this expanded region to obtain
620 the full extent of the area needing refontification.
622 For bookkeeping, call this function even when fontification is
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))
631 (setq start
(point-min)))
633 (when (> end nxml-prolog-end
)
635 (nxml-move-tag-backwards (point-min))
637 (setq end
(max (nxml-scan-after-change start end
)
640 (nxml-debug-change "nxml-after-change1" start end
))
644 (defun nxml-insert-xml-declaration ()
645 "Insert an XML declaration at the beginning of buffer.
646 The XML declaration will declare an encoding depending on the buffer's
647 `buffer-file-coding-system'."
650 (if (and buffer-file-coding-system
651 (coding-system-p buffer-file-coding-system
)
652 (coding-system-get buffer-file-coding-system
654 buffer-file-coding-system
655 (nxml-choose-utf-coding-system))))
656 (goto-char (point-min))
657 (insert (format "<?xml version=\"1.0\" encoding=\"%s\"?>\n"
658 (nxml-coding-system-name coding-system
)))))
660 (defun nxml-prepare-to-save ()
661 (unless (and (not enable-multibyte-characters
)
662 (local-variable-p 'buffer-file-coding-system
)
663 buffer-file-coding-system
664 (or (eq (coding-system-type buffer-file-coding-system
) 5)
665 (eq buffer-file-coding-system
'no-conversion
)))
667 (setq buffer-file-coding-system
(nxml-select-coding-system))))
668 ;; nil from a function in `write-contents-hooks' means
669 ;; to continue and write the file as normal
672 (defun nxml-select-coding-system ()
673 (let* ((suitable-coding-systems
674 (find-coding-systems-region (point-min) (point-max)))
676 (goto-char (point-min))
677 (xmltok-get-declared-encoding-position)))
680 (buffer-substring-no-properties (car enc-pos
)
684 (if (string= (downcase enc-name
) "utf-16")
685 (nxml-choose-utf-16-coding-system)
686 (nxml-mime-charset-coding-system enc-name
)))
687 (enc-pos (nxml-choose-utf-coding-system)))))
688 ;; Make sure we have a coding-system
689 (unless coding-system
691 (and (not buffer-read-only
)
692 (nxml-choose-suitable-coding-system
693 suitable-coding-systems
)))
696 (format "Unknown encoding %s" enc-name
)
697 "XML declaration is not well-formed")))
698 (cond ((not coding-system
)
699 (error "%s" message
))
705 "Modify and save with encoding %s")
706 (nxml-coding-system-name coding-system
))
708 (nxml-fix-encoding-declaration enc-pos coding-system
))
709 (t (signal 'quit nil
)))))
710 ;; Make sure it can encode all the characters in the buffer
711 (unless (or (memq (coding-system-base coding-system
)
712 suitable-coding-systems
)
713 (equal suitable-coding-systems
'(undecided)))
715 (nxml-unsuitable-coding-system-message coding-system
718 (and (not buffer-read-only
)
719 (nxml-choose-suitable-coding-system
720 suitable-coding-systems
)))
721 (cond ((not coding-system
) (error "%s" message
))
722 ((y-or-n-p (concat message
723 (format ". Save with %s "
724 (nxml-coding-system-name
726 (nxml-fix-encoding-declaration enc-pos coding-system
))
727 (t (signal 'quit nil
)))))
728 ;; Merge the newline type of our existing encoding
729 (let ((current-eol-type
730 (coding-system-eol-type buffer-file-coding-system
)))
731 (when (and current-eol-type
(integerp current-eol-type
))
733 (coding-system-change-eol-conversion coding-system
737 (defun nxml-unsuitable-coding-system-message (coding-system &optional enc-name
)
738 (if (nxml-coding-system-unicode-p coding-system
)
739 "Cannot translate some characters to Unicode"
740 (format "Cannot encode some characters with %s"
742 (nxml-coding-system-name coding-system
)))))
744 (defconst nxml-utf-16-coding-systems
(and (coding-system-p 'utf-16-be
)
745 (coding-system-p 'utf-16-le
)
746 '(utf-16-be utf-16-le
)))
748 (defconst nxml-utf-coding-systems
(cons 'utf-8 nxml-utf-16-coding-systems
))
750 (defun nxml-coding-system-unicode-p (coding-system)
751 (nxml-coding-system-member (coding-system-base coding-system
)
752 nxml-utf-coding-systems
))
754 (defun nxml-coding-system-name (coding-system)
755 (setq coding-system
(coding-system-base coding-system
))
757 (if (nxml-coding-system-member coding-system nxml-utf-16-coding-systems
)
759 (or (coding-system-get coding-system
'mime-charset
)
762 (defun nxml-fix-encoding-declaration (enc-pos coding-system
)
763 (let ((charset (nxml-coding-system-name coding-system
)))
764 (cond ((consp enc-pos
)
765 (delete-region (car enc-pos
) (cdr enc-pos
))
766 (goto-char (car enc-pos
))
770 (insert " encoding=\"" charset ?
\"))
772 (goto-char (point-min))
773 (insert "<?xml version=\"1.0\" encoding=\""
776 (when (and (not enc-pos
)
777 (let ((case-fold-search t
))
778 (looking-at xmltok-bad-xml-decl-regexp
)))
779 (delete-region (point) (match-end 0)))))))
781 (defun nxml-choose-suitable-coding-system (suitable-coding-systems)
782 (let (ret coding-system
)
783 (if (and buffer-file-coding-system
784 (memq (coding-system-base buffer-file-coding-system
)
785 suitable-coding-systems
))
786 buffer-file-coding-system
787 (while (and suitable-coding-systems
(not ret
))
788 (setq coding-system
(car suitable-coding-systems
))
789 (if (coding-system-get coding-system
'mime-charset
)
790 (setq ret coding-system
)
791 (setq suitable-coding-systems
(cdr suitable-coding-systems
))))
794 (defun nxml-choose-utf-coding-system ()
795 (let ((cur (and (local-variable-p 'buffer-file-coding-system
)
796 buffer-file-coding-system
797 (coding-system-base buffer-file-coding-system
))))
798 (cond ((car (nxml-coding-system-member cur nxml-utf-coding-systems
)))
799 ((and nxml-prefer-utf-16-to-utf-8-flag
800 (coding-system-p 'utf-16-le
)
801 (coding-system-p 'utf-16-be
))
802 (if nxml-prefer-utf-16-little-to-big-endian-flag
807 (defun nxml-choose-utf-16-coding-system ()
808 (let ((cur (and (local-variable-p 'buffer-file-coding-system
)
809 buffer-file-coding-system
810 (coding-system-base buffer-file-coding-system
))))
811 (cond ((car (nxml-coding-system-member cur nxml-utf-16-coding-systems
)))
812 (nxml-prefer-utf-16-little-to-big-endian-flag
813 (and (coding-system-p 'utf-16-le
) 'utf-16-le
))
814 (t (and (coding-system-p 'utf-16-be
) 'utf-16-be
)))))
816 (defun nxml-coding-system-member (coding-system coding-systems
)
818 (while (and coding-systems
(not ret
))
819 (if (coding-system-equal coding-system
820 (car coding-systems
))
821 (setq ret coding-systems
)
822 (setq coding-systems
(cdr coding-systems
))))
827 (defun nxml-unfontify-region (start end
)
828 (font-lock-default-unfontify-region start end
)
829 (nxml-clear-char-ref-extra-display start end
))
831 (defun nxml-extend-region ()
832 "Extend the region to hold the minimum area we can fontify with nXML.
833 Called with `font-lock-beg' and `font-lock-end' dynamically bound."
834 (let ((start font-lock-beg
)
837 (nxml-debug-change "nxml-extend-region(input)" start end
)
839 (when (< start nxml-prolog-end
)
840 (setq start
(point-min)))
842 (cond ((<= end nxml-prolog-end
)
843 (setq end nxml-prolog-end
))
847 ;; some font-lock backends (like Emacs 22 jit-lock) snap
848 ;; the region to the beginning of the line no matter what
849 ;; we say here. To mitigate the resulting excess
850 ;; fontification, ignore leading whitespace.
851 (skip-syntax-forward " ")
853 ;; find the beginning of the previous tag
854 (when (not (equal (char-after) ?\
<))
855 (search-backward "<" nxml-prolog-end t
))
856 (nxml-ensure-scan-up-to-date)
857 (nxml-move-outside-backwards)
860 (while (< (point) end
)
861 (nxml-tokenize-forward))
865 (when (or (< start font-lock-beg
)
866 (> end font-lock-end
))
867 (setq font-lock-beg start
869 (nxml-debug-change "nxml-extend-region" start end
)
872 (defun nxml-fontify-matcher (bound)
873 "Called as font-lock keyword matcher."
875 (unless nxml-degraded
876 (nxml-debug-change "nxml-fontify-matcher" (point) bound
)
878 (when (< (point) nxml-prolog-end
)
879 ;; Prolog needs to be fontified in one go, and
880 ;; nxml-extend-region makes sure we start at BOB.
882 (nxml-fontify-prolog)
883 (goto-char nxml-prolog-end
))
886 (while (and (nxml-tokenize-forward)
887 (<= (point) bound
)) ; Intervals are open-ended.
888 (nxml-apply-fontify-rule)))
892 ;; Since we did the fontification internally, tell font-lock to not
893 ;; do anything itself.
896 (defun nxml-fontify-prolog ()
898 The buffer is assumed to be prepared for fontification.
899 This does not set the fontified property, but it does clear
900 faces appropriately."
901 (let ((regions nxml-prolog-regions
))
903 (let ((region (car regions
)))
904 (nxml-apply-fontify-rule (aref region
0)
907 (setq regions
(cdr regions
)))))
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 (token-start xmltok-start
))
1226 (unless (or (eq xmltok-type
'partial-start-tag
)
1227 (and (memq xmltok-type
'(start-tag
1229 partial-empty-element
))
1230 (>= token-end pos
)))
1231 (error "Not in a start-tag"))
1232 ;; Note that this insertion changes xmltok-start.
1234 (buffer-substring-no-properties (+ xmltok-start
1)
1235 (min xmltok-name-end
(point)))
1237 (if (eq block-or-inline
'inline
)
1239 (goto-char token-start
)
1240 (back-to-indentation)
1241 (if (= (point) token-start
)
1242 (let ((indent (current-column)))
1245 (indent-line-to indent
)
1248 (indent-line-to (+ nxml-child-indent indent
)))
1251 (defun nxml-finish-element ()
1252 "Finish the current element by inserting an end-tag."
1254 (nxml-finish-element-1 nil
))
1256 (defvar nxml-last-split-position nil
1257 "Position where `nxml-split-element' split the current element.")
1259 (defun nxml-split-element ()
1260 "Split the current element by inserting an end-tag and a start-tag.
1261 Point is left after the newly inserted start-tag. When repeated,
1262 split immediately before the previously inserted start-tag and leave
1265 (setq nxml-last-split-position
1266 (if (and (eq last-command this-command
)
1267 nxml-last-split-position
)
1269 (goto-char nxml-last-split-position
)
1270 (nxml-finish-element-1 t
))
1271 (nxml-finish-element-1 t
))))
1273 (defun nxml-finish-element-1 (startp)
1274 "Insert an end-tag for the current element and optionally a start-tag.
1275 The start-tag is inserted if STARTP is non-nil. Return the position
1276 of the inserted start-tag or nil if none was inserted."
1278 (let* ((token-end (nxml-token-before))
1281 (when (and (< (point) token-end
)
1284 processing-instruction
1289 (error "Point is inside a %s"
1290 (nxml-token-type-friendly-name xmltok-type
)))
1291 (nxml-scan-element-backward token-end t
)))
1294 (unless (eq xmltok-type
'start-tag
)
1295 (error "No matching start-tag"))
1296 (goto-char xmltok-start
)
1297 (back-to-indentation)
1298 (eq (point) xmltok-start
)))
1301 (goto-char start-tag-end
)
1302 (looking-at "[ \t\r\n]*$")))
1303 (start-tag-indent (save-excursion
1304 (goto-char xmltok-start
)
1306 (qname (xmltok-start-tag-qname))
1307 inserted-start-tag-pos
)
1308 (when (and starts-line ends-line
)
1309 ;; start-tag is on a line by itself
1310 ;; => put the end-tag on a line by itself
1313 (back-to-indentation)
1316 (indent-line-to start-tag-indent
))
1317 (insert "</" qname
">")
1321 (indent-line-to start-tag-indent
))
1322 (setq inserted-start-tag-pos
(point))
1323 (insert "<" qname
">")
1324 (when (and starts-line ends-line
)
1326 (indent-line-to (save-excursion
1327 (goto-char xmltok-start
)
1329 (back-to-indentation)
1330 (if (= (current-column)
1331 (+ start-tag-indent nxml-child-indent
))
1332 (+ start-tag-indent nxml-child-indent
)
1333 start-tag-indent
)))))
1334 inserted-start-tag-pos
))
1338 (defun nxml-indent-line ()
1339 "Indent current line as XML."
1340 (let* ((savep (point))
1341 (indent (condition-case nil
1344 (skip-chars-forward " \t")
1345 (if (>= (point) savep
) (setq savep nil
))
1346 (or (nxml-compute-indent) 0))
1348 (if (not (numberp indent
))
1349 ;; If something funny is used (e.g. `noindent'), return it.
1351 (if (< indent
0) (setq indent
0)) ;Just in case.
1353 (save-excursion (indent-line-to indent
))
1354 (indent-line-to indent
)))))
1356 (defun nxml-compute-indent ()
1357 "Return the indent for the line containing point."
1358 (or (nxml-compute-indent-from-matching-start-tag)
1359 (nxml-compute-indent-from-previous-line)))
1361 (defun nxml-compute-indent-from-matching-start-tag ()
1362 "Compute the indent for a line with an end-tag using the matching start-tag.
1363 When the line containing point ends with an end-tag and does not start
1364 in the middle of a token, return the indent of the line containing the
1365 matching start-tag, if there is one and it occurs at the beginning of
1366 its line. Otherwise return nil."
1368 (back-to-indentation)
1369 (let ((bol (point)))
1370 (let ((inhibit-field-text-motion t
))
1372 (skip-chars-backward " \t")
1373 (and (= (nxml-token-before) (point))
1374 (memq xmltok-type
'(end-tag partial-end-tag
))
1375 ;; start of line must not be inside a token
1376 (or (= xmltok-start bol
)
1380 (= xmltok-start bol
))
1381 (eq xmltok-type
'data
))
1383 (nxml-scan-element-backward
1387 nxml-end-tag-indent-scan-distance
))
1388 (nxml-scan-error nil
))
1389 (< xmltok-start bol
)
1391 (goto-char xmltok-start
)
1392 (skip-chars-backward " \t")
1394 (current-indentation)))))
1396 (defun nxml-compute-indent-from-previous-line ()
1397 "Compute the indent for a line using the indentation of a previous line."
1402 before-context after-context
)
1403 (back-to-indentation)
1406 ;; Move backwards until the start of a non-blank line that is
1407 ;; not inside a token.
1409 (when (= (forward-line -
1) -
1)
1411 (back-to-indentation)
1412 (if (looking-at "[ \t]*$")
1415 (setq prev-bol
(point)))
1417 (not (or (= xmltok-start
(point))
1418 (eq xmltok-type
'data
))))))
1420 ;; Now scan over tokens until the end of the line to be indented.
1421 ;; Determine the context before and after the beginning of the
1423 (while (< (point) eol
)
1424 (nxml-tokenize-forward)
1425 (cond ((<= bol xmltok-start
)
1427 (nxml-merge-indent-context-type after-context
)))
1428 ((and (<= (point) bol
)
1429 (not (and (eq xmltok-type
'partial-start-tag
)
1431 (setq before-context
1432 (nxml-merge-indent-context-type before-context
)))
1433 ((eq xmltok-type
'data
)
1434 (setq before-context
1435 (nxml-merge-indent-context-type before-context
))
1437 (nxml-merge-indent-context-type after-context
)))
1438 ;; If in the middle of a token that looks inline,
1439 ;; then indent relative to the previous non-blank line
1440 ((eq (nxml-merge-indent-context-type before-context
)
1442 (goto-char prev-bol
)
1443 (throw 'indent
(current-column)))
1446 (nxml-compute-indent-in-token bol
))))
1447 (skip-chars-forward " \t\r\n"))
1450 (* nxml-child-indent
1451 (+ (if (eq before-context
'start-tag
) 1 0)
1452 (if (eq after-context
'end-tag
) -
1 0))))))))
1454 (defun nxml-merge-indent-context-type (context)
1455 "Merge the indent context type CONTEXT with the token in `xmltok-type'.
1456 Return the merged indent context type. An indent context type is
1457 either nil or one of the symbols `start-tag', `end-tag', `markup',
1458 `comment', `mixed'."
1459 (cond ((memq xmltok-type
'(start-tag partial-start-tag
))
1460 (if (memq context
'(nil start-tag comment
))
1463 ((memq xmltok-type
'(end-tag partial-end-tag
))
1464 (if (memq context
'(nil end-tag comment
))
1467 ((eq xmltok-type
'comment
)
1468 (cond ((memq context
'(start-tag end-tag comment
))
1475 (defun nxml-compute-indent-in-token (pos)
1476 "Return the indent for a line that starts inside a token.
1477 POS is the position of the first non-whitespace character of the line.
1478 This expects the xmltok-* variables to be set up as by `xmltok-forward'."
1479 (cond ((memq xmltok-type
'(start-tag
1482 partial-empty-element
))
1483 (nxml-compute-indent-in-start-tag pos
))
1484 ((eq xmltok-type
'comment
)
1485 (nxml-compute-indent-in-delimited-token pos
"<!--" "-->"))
1486 ((eq xmltok-type
'cdata-section
)
1487 (nxml-compute-indent-in-delimited-token pos
"<![CDATA[" "]]>"))
1488 ((eq xmltok-type
'processing-instruction
)
1489 (nxml-compute-indent-in-delimited-token pos
"<?" "?>"))
1492 (if (and (= (forward-line -
1) 0)
1493 (< xmltok-start
(point)))
1494 (back-to-indentation)
1495 (goto-char xmltok-start
))
1498 (defun nxml-compute-indent-in-start-tag (pos)
1499 "Return the indent for a line that starts inside a start-tag.
1500 Also for a line that starts inside an empty element.
1501 POS is the position of the first non-whitespace character of the line.
1502 This expects the xmltok-* variables to be set up as by `xmltok-forward'."
1503 (let ((value-boundary (nxml-attribute-value-boundary pos
))
1506 ;; inside an attribute value
1507 (let ((value-start (car value-boundary
)))
1510 (if (< (point) value-start
)
1511 (goto-char value-start
)
1512 (back-to-indentation)))
1513 ;; outside an attribute value
1515 (while (and (= (forward-line -
1) 0)
1516 (nxml-attribute-value-boundary (point))))
1517 (cond ((<= (point) xmltok-start
)
1518 (goto-char xmltok-start
)
1519 (setq off nxml-attribute-indent
)
1520 (let ((atts (xmltok-merge-attributes)))
1522 (let* ((att (car atts
))
1523 (start (xmltok-attribute-name-start att
)))
1528 (back-to-indentation))))
1529 (+ (current-column) off
)))
1531 (defun nxml-attribute-value-boundary (pos)
1532 "Return a pair (START . END) if POS is inside an attribute value.
1533 Otherwise return nil. START and END are the positions of the start
1534 and end of the attribute value containing POS. This expects the
1535 xmltok-* variables to be set up as by `xmltok-forward'."
1536 (let ((atts (xmltok-merge-attributes))
1537 att value-start value-end value-boundary
)
1539 (setq att
(car atts
))
1540 (setq value-start
(xmltok-attribute-value-start att
))
1541 (setq value-end
(xmltok-attribute-value-end att
))
1542 (cond ((and value-start
(< pos value-start
))
1544 ((and value-start value-end
(<= pos value-end
))
1545 (setq value-boundary
(cons value-start value-end
))
1547 (t (setq atts
(cdr atts
)))))
1550 (defun nxml-compute-indent-in-delimited-token (pos open-delim close-delim
)
1551 "Return the indent for a line that starts inside a token with delimiters.
1552 OPEN-DELIM and CLOSE-DELIM are strings giving the opening and closing
1553 delimiters. POS is the position of the first non-whitespace character
1554 of the line. This expects the xmltok-* variables to be set up as by
1556 (cond ((let ((end (+ pos
(length close-delim
))))
1557 (and (<= end
(point-max))
1558 (string= (buffer-substring-no-properties pos end
)
1560 (goto-char xmltok-start
))
1564 (<= (point) xmltok-start
))
1565 (goto-char (+ xmltok-start
(length open-delim
)))
1566 (when (and (string= open-delim
"<!--")
1568 (goto-char (1+ (point)))))
1569 (t (back-to-indentation)))
1574 (defun nxml-complete ()
1575 "Perform completion on the symbol preceding point.
1577 Inserts as many characters as can be completed. However, if not even
1578 one character can be completed, then a buffer with the possibilities
1579 is popped up and the symbol is read from the minibuffer with
1580 completion. If the symbol is complete, then any characters that must
1581 follow the symbol are also inserted.
1583 The name space used for completion and what is treated as a symbol
1584 depends on the context. The contexts in which completion is performed
1585 depend on `nxml-completion-hook'."
1587 (unless (run-hook-with-args-until-success 'nxml-completion-hook
)
1588 ;; Eventually we will complete on entity names here.
1590 (message "Cannot complete in this context")))
1592 (defun nxml-completion-at-point-function ()
1593 "Call `nxml-complete' to perform completion at point."
1594 (when nxml-bind-meta-tab-to-complete-flag
1599 (defun nxml-forward-balanced-item (&optional arg
)
1600 "Move forward across one balanced item.
1601 With ARG, do it that many times. Negative arg -N means
1602 move backward across N balanced expressions.
1603 This is the equivalent of `forward-sexp' for XML.
1605 An element contains as items strings with no markup, tags, processing
1606 instructions, comments, CDATA sections, entity references and
1607 characters references. However, if the variable
1608 `nxml-sexp-element-flag' is non-nil, then an element is treated as a
1609 single markup item. A start-tag contains an element name followed by
1610 one or more attributes. An end-tag contains just an element name.
1611 An attribute value literals contains strings with no markup, entity
1612 references and character references. A processing instruction
1613 consists of a target and a content string. A comment or a CDATA
1614 section contains a single string. An entity reference contains a
1615 single name. A character reference contains a character number."
1617 (or arg
(setq arg
1))
1620 (nxml-forward-single-balanced-item)
1621 (> (setq arg
(1- arg
)) 0))))
1624 (nxml-backward-single-balanced-item)
1625 (< (setq arg
(1+ arg
)) 0))))))
1627 (defun nxml-forward-single-balanced-item ()
1629 (goto-char (let ((end (nxml-token-after)))
1631 (while (eq xmltok-type
'space
)
1633 (setq end
(nxml-token-after)))
1634 (cond ((/= (point) xmltok-start
)
1635 (nxml-scan-forward-within end
))
1636 ((and nxml-sexp-element-flag
1637 (eq xmltok-type
'start-tag
))
1638 ;; can't ever return nil here
1639 (nxml-scan-element-forward xmltok-start
))
1640 ((and nxml-sexp-element-flag
1642 '(end-tag partial-end-tag
)))
1643 (error "Already at end of element"))
1646 (goto-char (cadr err
))
1647 (apply 'error
(cddr err
)))))
1649 (defun nxml-backward-single-balanced-item ()
1651 (goto-char (let ((end (nxml-token-before)))
1653 (while (eq xmltok-type
'space
)
1654 (goto-char xmltok-start
)
1655 (setq end
(nxml-token-before)))
1656 (cond ((/= (point) end
)
1657 (nxml-scan-backward-within end
))
1658 ((and nxml-sexp-element-flag
1659 (eq xmltok-type
'end-tag
))
1660 ;; can't ever return nil here
1661 (nxml-scan-element-backward end
)
1663 ((and nxml-sexp-element-flag
1664 (eq xmltok-type
'start-tag
))
1665 (error "Already at start of element"))
1666 (t xmltok-start
)))))
1668 (goto-char (cadr err
))
1669 (apply 'error
(cddr err
)))))
1671 (defun nxml-scan-forward-within (end)
1672 (setq end
(- end
(nxml-end-delimiter-length xmltok-type
)))
1673 (when (<= end
(point))
1674 (error "Already at end of %s"
1675 (nxml-token-type-friendly-name xmltok-type
)))
1676 (cond ((memq xmltok-type
'(start-tag
1679 partial-empty-element
))
1680 (if (< (point) xmltok-name-end
)
1682 (let ((att (nxml-find-following-attribute)))
1683 (cond ((not att
) end
)
1684 ((and (xmltok-attribute-value-start att
)
1685 (<= (xmltok-attribute-value-start att
)
1687 (nxml-scan-forward-in-attribute-value att
))
1688 ((xmltok-attribute-value-end att
)
1689 (1+ (xmltok-attribute-value-end att
)))
1691 (goto-char (xmltok-attribute-name-end att
))
1692 (looking-at "[ \t\r\n]*="))
1694 (t (xmltok-attribute-name-end att
))))))
1695 ((and (eq xmltok-type
'processing-instruction
)
1696 (< (point) xmltok-name-end
))
1700 (defun nxml-scan-backward-within (_end)
1703 (nxml-start-delimiter-length xmltok-type
)))
1704 (when (<= (point) xmltok-start
)
1705 (error "Already at start of %s"
1706 (nxml-token-type-friendly-name xmltok-type
)))
1707 (cond ((memq xmltok-type
'(start-tag
1710 partial-empty-element
))
1711 (let ((att (nxml-find-preceding-attribute)))
1712 (cond ((not att
) xmltok-start
)
1713 ((and (xmltok-attribute-value-start att
)
1714 (<= (xmltok-attribute-value-start att
)
1717 (xmltok-attribute-value-end att
)))
1718 (nxml-scan-backward-in-attribute-value att
))
1719 (t (xmltok-attribute-name-start att
)))))
1720 ((and (eq xmltok-type
'processing-instruction
)
1721 (let ((content-start (save-excursion
1722 (goto-char xmltok-name-end
)
1723 (skip-chars-forward " \r\t\n")
1725 (and (< content-start
(point))
1729 (defun nxml-scan-forward-in-attribute-value (att)
1730 (when (= (point) (xmltok-attribute-value-end att
))
1731 (error "Already at end of attribute value"))
1732 (let ((refs (xmltok-attribute-refs att
))
1735 (setq ref
(car refs
))
1736 (if (< (point) (aref ref
2))
1739 (setq refs
(cdr refs
))))
1741 (xmltok-attribute-value-end att
))
1742 ((< (point) (aref ref
1))
1744 ((= (point) (aref ref
1))
1747 (let ((end (- (aref ref
2)
1748 (nxml-end-delimiter-length (aref ref
0)))))
1751 (error "Already at end of %s"
1752 (nxml-token-type-friendly-name (aref ref
0)))))))))
1754 (defun nxml-scan-backward-in-attribute-value (att)
1755 (when (= (point) (xmltok-attribute-value-start att
))
1756 (error "Already at start of attribute value"))
1757 (let ((refs (reverse (xmltok-attribute-refs att
)))
1760 (setq ref
(car refs
))
1761 (if (< (aref ref
1) (point))
1764 (setq refs
(cdr refs
))))
1766 (xmltok-attribute-value-start att
))
1767 ((< (aref ref
2) (point))
1769 ((= (point) (aref ref
2))
1772 (let ((start (+ (aref ref
1)
1773 (nxml-start-delimiter-length (aref ref
0)))))
1774 (if (< start
(point))
1776 (error "Already at start of %s"
1777 (nxml-token-type-friendly-name (aref ref
0)))))))))
1779 (defun nxml-find-following-attribute ()
1781 (atts (or xmltok-attributes xmltok-namespace-attributes
))
1782 (more-atts (and xmltok-attributes xmltok-namespace-attributes
)))
1784 (let* ((att (car atts
))
1785 (name-start (xmltok-attribute-name-start att
)))
1786 (cond ((and (<= name-start
(point))
1787 (xmltok-attribute-value-end att
)
1788 ;; <= because end is before quote
1789 (<= (point) (xmltok-attribute-value-end att
)))
1792 ((and (< (point) name-start
)
1795 (xmltok-attribute-name-start ret
))))
1797 (setq atts
(cdr atts
))
1799 (setq atts more-atts
)
1800 (setq more-atts nil
)))
1803 (defun nxml-find-preceding-attribute ()
1805 (atts (or xmltok-attributes xmltok-namespace-attributes
))
1806 (more-atts (and xmltok-attributes xmltok-namespace-attributes
)))
1808 (let* ((att (car atts
))
1809 (name-start (xmltok-attribute-name-start att
)))
1810 (cond ((and (< name-start
(point))
1811 (xmltok-attribute-value-end att
)
1812 ;; <= because end is before quote
1813 (<= (point) (xmltok-attribute-value-end att
)))
1816 ((and (< name-start
(point))
1818 (< (xmltok-attribute-name-start ret
)
1821 (setq atts
(cdr atts
))
1823 (setq atts more-atts
)
1824 (setq more-atts nil
)))
1827 (defun nxml-up-element (&optional arg
)
1829 (or arg
(setq arg
1))
1831 (nxml-backward-up-element (- arg
))
1833 (while (and (> arg
0)
1834 (< (point) (point-max)))
1835 (let ((token-end (nxml-token-after)))
1836 (goto-char (cond ((or (memq xmltok-type
'(end-tag
1838 (and (memq xmltok-type
1840 partial-empty-element
))
1841 (< xmltok-start
(point))))
1843 ((nxml-scan-element-forward
1844 (if (and (eq xmltok-type
'start-tag
)
1845 (= (point) xmltok-start
))
1849 (t (error "No parent element")))))
1850 (setq arg
(1- arg
)))
1852 (goto-char (cadr err
))
1853 (apply 'error
(cddr err
))))))
1855 (defun nxml-backward-up-element (&optional arg
)
1857 (or arg
(setq arg
1))
1859 (nxml-up-element (- arg
))
1861 (while (and (> arg
0)
1862 (< (point-min) (point)))
1863 (let ((token-end (nxml-token-before)))
1864 (goto-char (cond ((or (memq xmltok-type
'(start-tag
1866 (and (memq xmltok-type
1868 partial-empty-element
))
1869 (< (point) token-end
)))
1871 ((nxml-scan-element-backward
1872 (if (and (eq xmltok-type
'end-tag
)
1873 (= (point) token-end
))
1878 (t (error "No parent element")))))
1879 (setq arg
(1- arg
)))
1881 (goto-char (cadr err
))
1882 (apply 'error
(cddr err
))))))
1884 (defun nxml-down-element (&optional arg
)
1885 "Move forward down into the content of an element.
1886 With ARG, do this that many times.
1887 Negative ARG means move backward but still down."
1889 (or arg
(setq arg
1))
1891 (nxml-backward-down-element (- arg
))
1894 (let ((token-end (nxml-token-after)))
1896 (goto-char token-end
)
1898 (when (memq xmltok-type
'(nil end-tag partial-end-tag
))
1899 (error "No following start-tags in this element"))
1900 (not (memq xmltok-type
'(start-tag partial-start-tag
))))
1901 (nxml-tokenize-forward))
1903 (setq arg
(1- arg
)))))
1905 (defun nxml-backward-down-element (&optional arg
)
1907 (or arg
(setq arg
1))
1909 (nxml-down-element (- arg
))
1914 (goto-char xmltok-start
)
1916 (when (memq xmltok-type
'(start-tag
1920 (error "No preceding end-tags in this element"))
1921 (not (memq xmltok-type
'(end-tag partial-end-tag
))))
1922 (if (or (<= (point) nxml-prolog-end
)
1923 (not (search-backward "<" nxml-prolog-end t
)))
1924 (setq xmltok-type nil
)
1925 (nxml-move-outside-backwards)
1928 (setq arg
(1- arg
)))))
1930 (defun nxml-forward-element (&optional arg
)
1931 "Move forward over one element.
1932 With ARG, do it that many times.
1933 Negative ARG means move backward."
1935 (or arg
(setq arg
1))
1937 (nxml-backward-element (- arg
))
1939 (while (and (> arg
0)
1940 (< (point) (point-max)))
1942 (or (nxml-scan-element-forward (nxml-token-before))
1943 (error "No more elements")))
1944 (setq arg
(1- arg
)))
1946 (goto-char (cadr err
))
1947 (apply 'error
(cddr err
))))))
1949 (defun nxml-backward-element (&optional arg
)
1950 "Move backward over one element.
1951 With ARG, do it that many times.
1952 Negative ARG means move forward."
1954 (or arg
(setq arg
1))
1956 (nxml-forward-element (- arg
))
1958 (while (and (> arg
0)
1959 (< (point-min) (point)))
1961 (or (and (nxml-scan-element-backward (progn
1965 (error "No preceding elements")))
1966 (setq arg
(1- arg
)))
1968 (goto-char (cadr err
))
1969 (apply 'error
(cddr err
))))))
1971 (defun nxml-mark-token-after ()
1973 (push-mark (nxml-token-after) nil t
)
1974 (goto-char xmltok-start
)
1975 (message "Marked %s" xmltok-type
))
1979 (defun nxml-mark-paragraph ()
1980 "Put point at beginning of this paragraph, mark at end.
1981 The paragraph marked is the one that contains point or follows point."
1983 (nxml-forward-paragraph)
1985 (nxml-backward-paragraph))
1987 (defun nxml-forward-paragraph (&optional arg
)
1989 (or arg
(setq arg
1))
1991 (nxml-backward-paragraph (- arg
)))
1994 (while (and (nxml-forward-single-paragraph)
1995 (> (setq arg
(1- arg
)) 0))))))
1997 (defun nxml-backward-paragraph (&optional arg
)
1999 (or arg
(setq arg
1))
2001 (nxml-forward-paragraph (- arg
)))
2004 (let ((inhibit-field-text-motion t
))
2006 (while (and (nxml-backward-single-paragraph)
2007 (> (setq arg
(1- arg
)) 0))))))
2009 (defun nxml-forward-single-paragraph ()
2010 "Move forward over a single paragraph.
2011 Return nil at end of buffer, t otherwise."
2012 (let* ((token-end (nxml-token-after))
2013 (offset (- (point) xmltok-start
))
2015 (goto-char token-end
)
2016 (while (and (< (point) (point-max))
2018 (nxml-paragraph-end-pos had-data offset
))))
2019 (when (nxml-token-contains-data-p offset
)
2021 (nxml-tokenize-forward)
2023 (when pos
(goto-char pos
))))
2025 (defun nxml-backward-single-paragraph ()
2026 "Move backward over a single paragraph.
2027 Return nil at start of buffer, t otherwise."
2028 (let* ((token-end (nxml-token-before))
2029 (offset (- token-end
(point)))
2030 (last-tag-pos xmltok-start
)
2031 pos had-data last-data-pos
)
2032 (goto-char token-end
)
2033 (unless (setq pos
(nxml-paragraph-start-pos nil offset
))
2034 (setq had-data
(nxml-token-contains-data-p nil offset
))
2035 (goto-char xmltok-start
)
2036 (while (and (not pos
) (< (point-min) (point)))
2037 (cond ((search-backward "<" nxml-prolog-end t
)
2038 (nxml-move-outside-backwards)
2040 (while (< (point) last-tag-pos
)
2042 (when (and (not had-data
) (nxml-token-contains-data-p))
2044 (setq last-data-pos xmltok-start
))
2045 (let ((tem (nxml-paragraph-start-pos had-data
0)))
2046 (when tem
(setq pos tem
)))))
2047 (when (and (not had-data
) last-data-pos
(not pos
))
2050 (while (< (point) last-data-pos
)
2052 (let ((tem (nxml-paragraph-start-pos had-data
0)))
2053 (when tem
(setq pos tem
)))))
2054 (setq last-tag-pos
(point)))
2055 (t (goto-char (point-min))))))
2056 (when pos
(goto-char pos
))))
2058 (defun nxml-token-contains-data-p (&optional start end
)
2059 (setq start
(+ xmltok-start
(or start
0)))
2060 (setq end
(- (point) (or end
0)))
2061 (when (eq xmltok-type
'cdata-section
)
2062 (setq start
(max start
(+ xmltok-start
9)))
2063 (setq end
(min end
(- (point) 3))))
2064 (or (and (eq xmltok-type
'data
)
2065 (eq start xmltok-start
)
2067 (eq xmltok-type
'char-ref
)
2068 (and (memq xmltok-type
'(data cdata-section
))
2072 (re-search-forward "[^ \t\r\n]" end t
)))))
2074 (defun nxml-paragraph-end-pos (had-data offset
)
2075 "Return the position of the paragraph end if contained in the current token.
2076 Return nil if the current token does not contain the paragraph end.
2077 Only characters after OFFSET from the start of the token are eligible.
2078 HAD-DATA says whether there have been non-whitespace data characters yet."
2079 (cond ((not had-data
)
2080 (cond ((memq xmltok-type
'(data cdata-section
))
2082 (let ((end (point)))
2083 (goto-char (+ xmltok-start
2084 (max (if (eq xmltok-type
'cdata-section
)
2088 (and (re-search-forward "[^ \t\r\n]" end t
)
2089 (re-search-forward "^[ \t]*$" end t
)
2090 (match-beginning 0)))))
2091 ((and (eq xmltok-type
'comment
)
2092 (nxml-token-begins-line-p)
2093 (nxml-token-ends-line-p))
2095 (let ((end (point)))
2096 (goto-char (+ xmltok-start
(max 4 offset
)))
2097 (when (re-search-forward "[^ \t\r\n]" (- end
3) t
)
2098 (if (re-search-forward "^[ \t]*$" end t
)
2100 (goto-char (- end
3))
2101 (skip-chars-backward " \t")
2103 (beginning-of-line 2))
2105 ((memq xmltok-type
'(data space cdata-section
))
2107 (let ((end (point)))
2108 (goto-char (+ xmltok-start offset
))
2109 (and (re-search-forward "^[ \t]*$" end t
)
2110 (match-beginning 0)))))
2111 ((and (memq xmltok-type
'(start-tag
2115 processing-instruction
2117 (nxml-token-begins-line-p)
2118 (nxml-token-ends-line-p))
2120 (goto-char xmltok-start
)
2121 (skip-chars-backward " \t")
2123 ((and (eq xmltok-type
'end-tag
)
2124 (looking-at "[ \t]*$")
2125 (not (nxml-in-mixed-content-p t
)))
2127 (or (search-forward "\n" nil t
)
2130 (defun nxml-paragraph-start-pos (had-data offset
)
2131 "Return the position of the paragraph start if contained in the current token.
2132 Return nil if the current token does not contain the paragraph start.
2133 Only characters before OFFSET from the end of the token are eligible.
2134 HAD-DATA says whether there have been non-whitespace data characters yet."
2135 (cond ((not had-data
)
2136 (cond ((memq xmltok-type
'(data cdata-section
))
2138 (goto-char (- (point)
2139 (max (if (eq xmltok-type
'cdata-section
)
2143 (and (re-search-backward "[^ \t\r\n]" xmltok-start t
)
2144 (re-search-backward "^[ \t]*$" xmltok-start t
)
2145 (match-beginning 0))))
2146 ((and (eq xmltok-type
'comment
)
2147 (nxml-token-ends-line-p)
2148 (nxml-token-begins-line-p))
2150 (goto-char (- (point) (max 3 offset
)))
2151 (when (and (< (+ xmltok-start
4) (point))
2152 (re-search-backward "[^ \t\r\n]"
2155 (if (re-search-backward "^[ \t]*$" xmltok-start t
)
2157 (goto-char xmltok-start
)
2158 (if (looking-at "<!--[ \t]*\n")
2160 (skip-chars-backward " \t")
2162 ((memq xmltok-type
'(data space cdata-section
))
2164 (goto-char (- (point) offset
))
2165 (and (re-search-backward "^[ \t]*$" xmltok-start t
)
2166 (match-beginning 0))))
2167 ((and (memq xmltok-type
'(start-tag
2171 processing-instruction
2173 (nxml-token-ends-line-p)
2174 (nxml-token-begins-line-p))
2175 (or (search-forward "\n" nil t
)
2177 ((and (eq xmltok-type
'start-tag
)
2178 (nxml-token-begins-line-p)
2179 (not (save-excursion
2180 (goto-char xmltok-start
)
2181 (nxml-in-mixed-content-p nil
))))
2183 (goto-char xmltok-start
)
2184 (skip-chars-backward " \t")
2185 ;; include any blank line before
2186 (or (and (eq (char-before) ?
\n)
2188 (goto-char (1- (point)))
2189 (skip-chars-backward " \t")
2190 (and (bolp) (point))))
2193 (defun nxml-token-ends-line-p () (looking-at "[ \t]*$"))
2195 (defun nxml-token-begins-line-p ()
2197 (goto-char xmltok-start
)
2198 (skip-chars-backward " \t")
2201 (defun nxml-in-mixed-content-p (endp)
2202 "Return non-nil if point is in mixed content.
2203 Point must be after an end-tag or before a start-tag.
2204 ENDP is t in the former case, nil in the latter."
2205 (let (matching-tag-pos)
2206 (cond ((not (run-hook-with-args-until-failure
2207 'nxml-in-mixed-content-hook
))
2209 ;; See if the matching tag does not start or end a line.
2210 ((condition-case nil
2212 (setq matching-tag-pos
2215 (and (nxml-scan-element-backward (point))
2217 (nxml-scan-element-forward (point)))))
2218 (and matching-tag-pos
2220 (goto-char matching-tag-pos
)
2223 (skip-chars-backward " \t")
2225 (looking-at "[ \t]*$"))))))
2226 (nxml-scan-error nil
))
2228 ;; See if there's data at the same level.
2231 (setq start matching-tag-pos
2234 end matching-tag-pos
))
2238 (nxml-preceding-sibling-data-p))
2241 (nxml-following-sibling-data-p)))))
2243 ;; Otherwise, treat as not mixed
2246 (defun nxml-preceding-sibling-data-p ()
2247 "Return non-nil if there is a previous sibling that is data."
2248 (let ((lim (max (- (point) nxml-mixed-scan-distance
)
2254 (while (and (< lim
(point))
2259 (search-backward "<" lim t
)))
2260 (nxml-move-outside-backwards)
2263 (let ((prev-level level
))
2264 (cond ((eq xmltok-type
'end-tag
)
2265 (setq level
(1+ level
)))
2266 ((eq xmltok-type
'start-tag
)
2267 (setq level
(1- level
))))
2268 (when (eq prev-level
0)
2269 (while (and (< (point) end
) (not found
))
2271 (when (memq xmltok-type
'(data cdata-section char-ref
))
2272 (setq found t
)))))))))
2275 (defun nxml-following-sibling-data-p ()
2276 (let ((lim (min (+ (point) nxml-mixed-scan-distance
)
2282 (while (and (< (point) lim
)
2284 (nxml-tokenize-forward)
2286 (cond ((eq xmltok-type
'start-tag
)
2287 (setq level
(1+ level
)))
2288 ((eq xmltok-type
'end-tag
)
2289 (setq level
(1- level
)))
2291 (memq xmltok-type
'(data cdata-section char-ref
)))
2297 (defun nxml-do-fill-paragraph (arg)
2298 (let (fill-paragraph-function
2302 (nxml-forward-paragraph)
2304 (nxml-backward-paragraph)
2305 (skip-chars-forward " \t\r\n")
2306 (setq start
(point))
2308 (setq fill-prefix
(buffer-substring-no-properties (point) start
))
2309 (when (and (not (nxml-get-inside (point)))
2310 (looking-at "[ \t]*<!--"))
2311 (setq fill-prefix
(concat fill-prefix
" ")))
2312 (fill-region-as-paragraph start end arg
))
2313 (skip-line-prefix fill-prefix
)
2316 (defun nxml-newline-and-indent (soft)
2317 (delete-horizontal-space)
2318 (if soft
(insert-and-inherit ?
\n) (newline 1))
2324 (defvar nxml-dynamic-markup-prev-pos nil
)
2325 (defvar nxml-dynamic-markup-prev-lengths nil
)
2326 (defvar nxml-dynamic-markup-prev-found-marker nil
)
2327 (defvar nxml-dynamic-markup-prev-start-tags
(make-hash-table :test
'equal
))
2329 (defun nxml-dynamic-markup-word ()
2330 "Dynamically markup the word before point.
2331 This attempts to find a tag to put around the word before point based
2332 on the contents of the current buffer. The end-tag will be inserted at
2333 point. The start-tag will be inserted at or before the beginning of
2334 the word before point; the contents of the current buffer is used to
2337 It works in a similar way to \\[dabbrev-expand]. It searches first
2338 backwards from point, then forwards from point for an element whose
2339 content is a string which matches the contents of the buffer before
2340 point and which includes at least the word before point. It then
2341 copies the start- and end-tags from that element and uses them to
2342 surround the matching string before point.
2344 Repeating \\[nxml-dynamic-markup-word] immediately after successful
2345 \\[nxml-dynamic-markup-word] removes the previously inserted markup
2346 and attempts to find another possible way to do the markup."
2348 (let (search-start-pos)
2349 (if (and (integerp nxml-dynamic-markup-prev-pos
)
2350 (= nxml-dynamic-markup-prev-pos
(point))
2351 (eq last-command this-command
)
2352 nxml-dynamic-markup-prev-lengths
)
2353 (let* ((end-tag-open-pos
2354 (- nxml-dynamic-markup-prev-pos
2355 (nth 2 nxml-dynamic-markup-prev-lengths
)))
2356 (start-tag-close-pos
2358 (nth 1 nxml-dynamic-markup-prev-lengths
)))
2360 (- start-tag-close-pos
2361 (nth 0 nxml-dynamic-markup-prev-lengths
))))
2362 (delete-region end-tag-open-pos nxml-dynamic-markup-prev-pos
)
2363 (delete-region start-tag-open-pos start-tag-close-pos
)
2364 (setq search-start-pos
2365 (marker-position nxml-dynamic-markup-prev-found-marker
)))
2366 (clrhash nxml-dynamic-markup-prev-start-tags
))
2367 (setq nxml-dynamic-markup-prev-pos nil
)
2368 (setq nxml-dynamic-markup-prev-lengths nil
)
2369 (setq nxml-dynamic-markup-prev-found-marker nil
)
2372 (let* ((pos (point))
2375 (unless (< (point) pos
)
2376 (error "No word to markup"))
2377 (buffer-substring-no-properties (point) pos
)))
2378 (search (concat word
"</"))
2380 (when search-start-pos
2381 (goto-char search-start-pos
))
2382 (while (and (not done
)
2383 (or (and (< (point) pos
)
2384 (or (search-backward search nil t
)
2385 (progn (goto-char pos
) nil
)))
2386 (search-forward search nil t
)))
2387 (goto-char (- (match-end 0) 2))
2388 (setq done
(nxml-try-copy-markup pos
)))
2390 (error (if (zerop (hash-table-count
2391 nxml-dynamic-markup-prev-start-tags
))
2392 "No possible markup found for `%s'"
2393 "No more markup possibilities found for `%s'")
2396 (defun nxml-try-copy-markup (word-end-pos)
2398 (let ((end-tag-pos (point)))
2399 (when (and (not (nxml-get-inside end-tag-pos
))
2400 (search-backward "<" nil t
)
2401 (not (nxml-get-inside (point))))
2403 (when (and (eq xmltok-type
'start-tag
)
2404 (< (point) end-tag-pos
))
2405 (let* ((start-tag-close-pos (point))
2407 (buffer-substring-no-properties xmltok-start
2408 start-tag-close-pos
))
2412 (buffer-substring-no-properties start-tag-close-pos
2415 (goto-char word-end-pos
)
2417 (re-search-backward (concat
2418 (regexp-quote (car words
))
2422 (setq words
(cdr words
))
2423 (skip-chars-backward " \t\r\n"))
2424 (when (and (not words
)
2426 (skip-chars-forward " \t\r\n")
2427 (not (gethash (cons (point) start-tag
)
2428 nxml-dynamic-markup-prev-start-tags
)))
2429 (or (< end-tag-pos
(point))
2430 (< word-end-pos xmltok-start
)))
2431 (setq nxml-dynamic-markup-prev-found-marker
2432 (copy-marker end-tag-pos t
))
2433 (puthash (cons (point) start-tag
)
2435 nxml-dynamic-markup-prev-start-tags
)
2436 (setq nxml-dynamic-markup-prev-lengths
2437 (list (- start-tag-close-pos xmltok-start
)
2438 (- word-end-pos
(point))
2439 (+ (- xmltok-name-end xmltok-start
) 2)))
2440 (let ((name (xmltok-start-tag-qname)))
2442 (goto-char (+ word-end-pos
2443 (- start-tag-close-pos xmltok-start
)))
2444 (insert "</" name
">")
2445 (setq nxml-dynamic-markup-prev-pos
(point))))))))))
2450 (defvar nxml-char-name-ignore-case t
)
2452 (defvar nxml-char-name-alist nil
2453 "Alist of character names.
2454 Each member of the list has the form (NAME CODE . NAMESET),
2455 where NAME is a string naming a character, NAMESET is a symbol
2456 identifying a set of names and CODE is an integer specifying the
2457 Unicode scalar value of the named character.
2458 The NAME will only be used for completion if NAMESET has
2459 a non-nil `nxml-char-name-set-enabled' property.
2460 If NAMESET does does not have `nxml-char-name-set-defined' property,
2461 then it must have a `nxml-char-name-set-file' property and `load'
2462 will be applied to the value of this property if the nameset
2465 (defvar nxml-char-name-table
(make-hash-table :test
'eq
)
2466 "Hash table for mapping char codes to names.
2467 Each key is a Unicode scalar value.
2468 Each value is a list of pairs of the form (NAMESET . NAME),
2469 where NAMESET is a symbol identifying a set of names,
2470 and NAME is a string naming a character.")
2472 (defvar nxml-autoload-char-name-set-list nil
2473 "List of char namesets that can be autoloaded.")
2475 (defun nxml-enable-char-name-set (nameset)
2476 (put nameset
'nxml-char-name-set-enabled t
))
2478 (defun nxml-disable-char-name-set (nameset)
2479 (put nameset
'nxml-char-name-set-enabled nil
))
2481 (defun nxml-char-name-set-enabled-p (nameset)
2482 (get nameset
'nxml-char-name-set-enabled
))
2484 (defun nxml-autoload-char-name-set (nameset file
)
2485 (unless (memq nameset nxml-autoload-char-name-set-list
)
2486 (setq nxml-autoload-char-name-set-list
2487 (cons nameset nxml-autoload-char-name-set-list
)))
2488 (put nameset
'nxml-char-name-set-file file
))
2490 (defun nxml-define-char-name-set (nameset alist
)
2491 "Define a set of character names.
2492 NAMESET is a symbol identifying the set.
2493 ALIST is a list where each member has the form (NAME CODE),
2494 where NAME is a string naming a character and code is an
2495 integer giving the Unicode scalar value of the character."
2496 (when (get nameset
'nxml-char-name-set-defined
)
2497 (error "Nameset `%s' already defined" nameset
))
2500 (let* ((name-code (car iter
))
2501 (name (car name-code
))
2502 (code (cadr name-code
)))
2504 (cons (cons nameset name
)
2505 (gethash code nxml-char-name-table
))
2506 nxml-char-name-table
))
2507 (setcdr (cdr (car iter
)) nameset
)
2508 (setq iter
(cdr iter
))))
2509 (setq nxml-char-name-alist
2510 (nconc alist nxml-char-name-alist
))
2511 (put nameset
'nxml-char-name-set-defined t
))
2513 (defun nxml-get-char-name (code)
2514 (mapc 'nxml-maybe-load-char-name-set nxml-autoload-char-name-set-list
)
2515 (let ((names (gethash code nxml-char-name-table
))
2517 (while (and names
(not name
))
2518 (if (nxml-char-name-set-enabled-p (caar names
))
2519 (setq name
(cdar names
))
2520 (setq names
(cdr names
))))
2523 (defvar nxml-named-char-history nil
)
2525 (defun nxml-insert-named-char (arg)
2526 "Insert a character using its name.
2527 The name is read from the minibuffer.
2528 Normally, inserts the character as a numeric character reference.
2529 With a prefix argument, inserts the character directly."
2531 (mapc 'nxml-maybe-load-char-name-set nxml-autoload-char-name-set-list
)
2533 (let ((completion-ignore-case nxml-char-name-ignore-case
))
2534 (completing-read "Character name: "
2535 nxml-char-name-alist
2537 (get (cddr member
) 'nxml-char-name-set-enabled
))
2540 'nxml-named-char-history
)))
2541 (alist nxml-char-name-alist
)
2543 (while (and alist
(not code
))
2544 (setq elt
(assoc name alist
))
2545 (if (get (cddr elt
) 'nxml-char-name-set-enabled
)
2546 (setq code
(cadr elt
))
2547 (setq alist
(cdr (member elt alist
)))))
2550 (or (decode-char 'ucs code
)
2551 (error "Character %x is not supported by Emacs"
2553 (format "&#x%X;" code
))))))
2555 (defun nxml-maybe-load-char-name-set (sym)
2556 (when (and (get sym
'nxml-char-name-set-enabled
)
2557 (not (get sym
'nxml-char-name-set-defined
))
2558 (stringp (get sym
'nxml-char-name-set-file
)))
2559 (load (get sym
'nxml-char-name-set-file
))))
2561 (defun nxml-toggle-char-ref-extra-display (arg)
2562 "Toggle the display of extra information for character references."
2564 (let ((new (if (null arg
)
2565 (not nxml-char-ref-extra-display
)
2566 (> (prefix-numeric-value arg
) 0))))
2567 (when (not (eq new nxml-char-ref-extra-display
))
2568 (setq nxml-char-ref-extra-display new
)
2569 (font-lock-fontify-buffer))))
2571 (put 'nxml-char-ref
'evaporate t
)
2573 (defun nxml-char-ref-display-extra (start end n
)
2574 (when nxml-char-ref-extra-display
2575 (let ((name (nxml-get-char-name n
))
2576 (glyph-string (and nxml-char-ref-display-glyph-flag
2577 (nxml-glyph-display-string n
'nxml-glyph
)))
2579 (when (or name glyph-string
)
2580 (setq ov
(make-overlay start end nil t
))
2581 (overlay-put ov
'category
'nxml-char-ref
)
2583 (overlay-put ov
'help-echo name
))
2587 (propertize glyph-string
'face
'nxml-glyph
)))))))
2589 (defun nxml-clear-char-ref-extra-display (start end
)
2590 (let ((ov (overlays-in start end
)))
2592 (when (eq (overlay-get (car ov
) 'category
) 'nxml-char-ref
)
2593 (delete-overlay (car ov
)))
2594 (setq ov
(cdr ov
)))))
2597 (defun nxml-start-delimiter-length (type)
2598 (or (get type
'nxml-start-delimiter-length
)
2601 (put 'cdata-section
'nxml-start-delimiter-length
9)
2602 (put 'comment
'nxml-start-delimiter-length
4)
2603 (put 'processing-instruction
'nxml-start-delimiter-length
2)
2604 (put 'start-tag
'nxml-start-delimiter-length
1)
2605 (put 'empty-element
'nxml-start-delimiter-length
1)
2606 (put 'partial-empty-element
'nxml-start-delimiter-length
1)
2607 (put 'entity-ref
'nxml-start-delimiter-length
1)
2608 (put 'char-ref
'nxml-start-delimiter-length
2)
2610 (defun nxml-end-delimiter-length (type)
2611 (or (get type
'nxml-end-delimiter-length
)
2614 (put 'cdata-section
'nxml-end-delimiter-length
3)
2615 (put 'comment
'nxml-end-delimiter-length
3)
2616 (put 'processing-instruction
'nxml-end-delimiter-length
2)
2617 (put 'start-tag
'nxml-end-delimiter-length
1)
2618 (put 'empty-element
'nxml-end-delimiter-length
2)
2619 (put 'partial-empty-element
'nxml-end-delimiter-length
1)
2620 (put 'entity-ref
'nxml-end-delimiter-length
1)
2621 (put 'char-ref
'nxml-end-delimiter-length
1)
2623 (defun nxml-token-type-friendly-name (type)
2624 (or (get type
'nxml-friendly-name
)
2625 (symbol-name type
)))
2627 (put 'cdata-section
'nxml-friendly-name
"CDATA section")
2628 (put 'processing-instruction
'nxml-friendly-name
"processing instruction")
2629 (put 'entity-ref
'nxml-friendly-name
"entity reference")
2630 (put 'char-ref
'nxml-friendly-name
"character reference")
2633 (defalias 'xml-mode
'nxml-mode
)
2635 (provide 'nxml-mode
)
2637 ;;; nxml-mode.el ends here