1 ;;; nxml-mode.el --- a new XML mode
3 ;; Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; See nxml-rap.el for description of parsing strategy.
29 (when (featurep 'mucs
)
30 (error "nxml-mode is not compatible with Mule-UCS"))
32 (eval-when-compile (require 'cl
)) ; for assert
41 (declare-function rng-nxml-mode-init
"rng-nxml")
42 (declare-function nxml-enable-unicode-char-name-sets
"nxml-uchnm")
47 "New XML editing mode."
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 hook
58 `nxml-glyph-set-hook' can be used to customize for which characters
59 glyphs are displayed."
63 (defcustom nxml-mode-hook nil
64 "Hook run by command `nxml-mode'."
68 (defcustom nxml-sexp-element-flag nil
69 "*Non-nil means sexp commands treat an element as a single expression."
73 (defcustom nxml-slash-auto-complete-flag nil
74 "*Non-nil means typing a slash automatically completes the end-tag.
75 This is used by `nxml-electric-slash'."
79 (defcustom nxml-child-indent
2
80 "*Indentation for the children of an element relative to the start-tag.
81 This only applies when the line or lines containing the start-tag contains
82 nothing else other than that start-tag."
86 (defcustom nxml-attribute-indent
4
87 "*Indentation for the attributes of an element relative to the start-tag.
88 This only applies when the first attribute of a tag starts a line.
89 In other cases, the first attribute on one line is indented the same
90 as the first attribute on the previous line."
94 (defcustom nxml-bind-meta-tab-to-complete-flag
(not window-system
)
95 "*Non-nil means bind M-TAB in `nxml-mode-map' to `nxml-complete'.
96 C-return will be bound to `nxml-complete' in any case.
97 M-TAB gets swallowed by many window systems/managers, and
98 `documentation' will show M-TAB rather than C-return as the
99 binding for `nxml-complete' when both are bound. So it's better
100 to bind M-TAB only when it will work."
102 :set
(lambda (sym flag
)
103 (set-default sym flag
)
104 (when (and (boundp 'nxml-mode-map
) nxml-mode-map
)
105 (define-key nxml-mode-map
"\M-\t" (and flag
'nxml-complete
))))
108 (defcustom nxml-prefer-utf-16-to-utf-8-flag nil
109 "*Non-nil means prefer UTF-16 to UTF-8 when saving a buffer.
110 This is used only when a buffer does not contain an encoding declaration
111 and when its current `buffer-file-coding-system' specifies neither UTF-16
116 (defcustom nxml-prefer-utf-16-little-to-big-endian-flag
(eq system-type
118 "*Non-nil means prefer little-endian to big-endian byte-order for UTF-16.
119 This is used only for saving a buffer; when reading the byte-order is
120 auto-detected. It may be relevant both when there is no encoding declaration
121 and when the encoding declaration specifies `UTF-16'."
125 (defcustom nxml-default-buffer-file-coding-system nil
126 "*Default value for `buffer-file-coding-system' for a buffer for a new file.
127 A value of nil means use the default value of `buffer-file-coding-system' as normal.
128 A buffer's `buffer-file-coding-system' affects what \\[nxml-insert-xml-declaration] inserts."
130 :type
'coding-system
)
132 (defcustom nxml-auto-insert-xml-declaration-flag nil
133 "*Non-nil means automatically insert an XML declaration in a new file.
134 The XML declaration is inserted using `nxml-insert-xml-declaration'."
138 (defface nxml-delimited-data
139 '((t (:inherit font-lock-doc-face
)))
140 "Face used to highlight data enclosed between delimiters.
141 This is not used directly, but only via inheritance by other faces."
145 '((t (:inherit font-lock-builtin-face
)))
146 "Face used to highlight various names.
147 This includes element and attribute names, processing
148 instruction targets and the CDATA keyword in a CDATA section.
149 This is not used directly, but only via inheritance by other faces."
153 '((t (:inherit font-lock-constant-face
)))
154 "Face used to highlight character and entity references.
155 This is not used directly, but only via inheritance by other faces."
158 (defface nxml-delimiter
160 "Face used to highlight delimiters.
161 This is not used directly, but only via inheritance by other faces."
166 "Face used to highlight text."
169 (defface nxml-comment-content
170 '((t (:inherit font-lock-comment-face
)))
171 "Face used to highlight the content of comments."
174 (defface nxml-comment-delimiter
175 '((t (:inherit font-lock-comment-delimiter-face
)))
176 "Face used for the delimiters of comments, i.e <!-- and -->."
179 (defface nxml-processing-instruction-delimiter
180 '((t (:inherit nxml-delimiter
)))
181 "Face used for the delimiters of processing instructions, i.e <? and ?>."
184 (defface nxml-processing-instruction-target
185 '((t (:inherit font-lock-keyword-face
)))
186 "Face used for the target of processing instructions."
189 (defface nxml-processing-instruction-content
190 '((t (:inherit nxml-delimited-data
)))
191 "Face used for the content of processing instructions."
194 (defface nxml-cdata-section-delimiter
195 '((t (:inherit nxml-delimiter
)))
196 "Face used for the delimiters of CDATA sections, i.e <![, [, and ]]>."
199 (defface nxml-cdata-section-CDATA
200 '((t (:inherit nxml-name
)))
201 "Face used for the CDATA keyword in CDATA sections."
204 (defface nxml-cdata-section-content
205 '((t (:inherit nxml-text
)))
206 "Face used for the content of CDATA sections."
209 (defface nxml-char-ref-number
210 '((t (:inherit nxml-ref
)))
211 "Face used for the number in character references.
212 This includes ths `x' in hex references."
215 (defface nxml-char-ref-delimiter
216 '((t (:inherit nxml-ref
)))
217 "Face used for the delimiters of character references, i.e &# and ;."
220 (defface nxml-entity-ref-name
221 '((t (:inherit nxml-ref
)))
222 "Face used for the entity name in general entity references."
225 (defface nxml-entity-ref-delimiter
226 '((t (:inherit nxml-ref
)))
227 "Face used for the delimiters of entity references, i.e & and ;."
230 (defface nxml-tag-delimiter
231 '((t (:inherit nxml-delimiter
)))
232 "Face used for the angle brackets delimiting tags.
233 `nxml-tag-slash' is used for slashes."
236 (defface nxml-tag-slash
237 '((t (:inherit nxml-tag-delimiter
)))
238 "Face used for slashes in tags, both in end-tags and empty-elements."
241 (defface nxml-element-prefix
242 '((t (:inherit nxml-name
)))
243 "Face used for the prefix of elements."
246 (defface nxml-element-colon
248 "Face used for the colon in element names."
251 (defface nxml-element-local-name
252 '((t (:inherit font-lock-function-name-face
)))
253 "Face used for the local name of elements."
256 (defface nxml-attribute-prefix
257 '((t (:inherit nxml-name
)))
258 "Face used for the prefix of attributes."
261 (defface nxml-attribute-colon
262 '((t (:inherit nxml-delimiter
)))
263 "Face used for the colon in attribute names."
266 (defface nxml-attribute-local-name
267 '((t (:inherit font-lock-variable-name-face
)))
268 "Face used for the local name of attributes."
271 (defface nxml-namespace-attribute-xmlns
272 '((t (:inherit nxml-attribute-prefix
)))
273 "Face used for `xmlns' in namespace attributes."
276 (defface nxml-namespace-attribute-colon
277 '((t (:inherit nxml-attribute-colon
)))
278 "Face used for the colon in namespace attributes."
281 (defface nxml-namespace-attribute-prefix
282 '((t (:inherit nxml-attribute-local-name
)))
283 "Face used for the prefix declared in namespace attributes."
286 (defface nxml-attribute-value
287 '((t (:inherit font-lock-string-face
)))
288 "Face used for the value of attributes."
291 (defface nxml-attribute-value-delimiter
292 '((t (:inherit nxml-attribute-value
)))
293 "Face used for the delimiters of attribute values."
296 (defface nxml-namespace-attribute-value
297 '((t (:inherit nxml-attribute-value
)))
298 "Face used for the value of namespace attributes."
301 (defface nxml-namespace-attribute-value-delimiter
302 '((t (:inherit nxml-attribute-value-delimiter
)))
303 "Face used for the delimiters of namespace attribute values."
306 (defface nxml-prolog-literal-delimiter
307 '((t (:inherit nxml-delimited-data
)))
308 "Face used for the delimiters of literals in the prolog."
311 (defface nxml-prolog-literal-content
312 '((t (:inherit nxml-delimited-data
)))
313 "Face used for the content of literals in the prolog."
316 (defface nxml-prolog-keyword
317 '((t (:inherit font-lock-keyword-face
)))
318 "Face used for keywords in the prolog."
321 (defface nxml-markup-declaration-delimiter
322 '((t (:inherit nxml-delimiter
)))
323 "Face used for the delimiters of markup declarations in the prolog.
324 The delimiters are <! and >."
328 '((t (:inherit nxml-name
)))
329 "Face used for # before a name in the prolog."
353 "Face used for glyph for char references."
358 (defvar nxml-prolog-regions nil
359 "List of regions in the prolog to be fontified.
360 See the function `xmltok-forward-prolog' for more information.")
361 (make-variable-buffer-local 'nxml-prolog-regions
)
363 (defvar nxml-last-fontify-end nil
364 "Position where fontification last ended.
365 It is nil if the buffer changed since the last fontification.")
366 (make-variable-buffer-local 'nxml-last-fontify-end
)
368 (defvar nxml-degraded nil
369 "Non-nil if currently operating in degraded mode.
370 Degraded mode is enabled when an internal error is encountered in the
371 fontification or after-change functions.")
372 (make-variable-buffer-local 'nxml-degraded
)
374 (defvar nxml-completion-hook nil
375 "Hook run by `nxml-complete'.
376 This hook is run until success.")
378 (defvar nxml-in-mixed-content-hook nil
379 "Hook to determine whether point is in mixed content.
380 The hook is called without arguments. It should return nil if it is
381 definitely not mixed; non-nil otherwise. The hook will be run until
382 one of the functions returns nil.")
384 (defvar nxml-mixed-scan-distance
4000
385 "Maximum distance from point to scan when checking for mixed content.")
387 (defvar nxml-end-tag-indent-scan-distance
4000
388 "Maximum distance from point to scan backwards when indenting end-tag.")
390 (defvar nxml-char-ref-extra-display t
391 "Non-nil means display extra information for character references.
392 The extra information consists of a tooltip with the character name
393 and, if `nxml-char-ref-display-glyph-flag' is non-nil, a glyph
394 corresponding to the referenced character following the character
396 (make-variable-buffer-local 'nxml-char-ref-extra-display
)
398 (defvar nxml-mode-map
399 (let ((map (make-sparse-keymap)))
400 (define-key map
"\M-\C-u" 'nxml-backward-up-element
)
401 (define-key map
"\M-\C-d" 'nxml-down-element
)
402 (define-key map
"\M-\C-n" 'nxml-forward-element
)
403 (define-key map
"\M-\C-p" 'nxml-backward-element
)
404 (define-key map
"\M-{" 'nxml-backward-paragraph
)
405 (define-key map
"\M-}" 'nxml-forward-paragraph
)
406 (define-key map
"\M-h" 'nxml-mark-paragraph
)
407 (define-key map
"\C-c\C-f" 'nxml-finish-element
)
408 (define-key map
"\C-c\C-m" 'nxml-split-element
)
409 (define-key map
"\C-c\C-b" 'nxml-balanced-close-start-tag-block
)
410 (define-key map
"\C-c\C-i" 'nxml-balanced-close-start-tag-inline
)
411 (define-key map
"\C-c\C-x" 'nxml-insert-xml-declaration
)
412 (define-key map
"\C-c\C-d" 'nxml-dynamic-markup-word
)
414 (define-key map
"\C-c\C-u" 'nxml-insert-named-char
)
415 (define-key map
"\C-c\C-o" nxml-outline-prefix-map
)
416 (define-key map
[S-mouse-2
] 'nxml-mouse-hide-direct-text-content
)
417 (define-key map
"/" 'nxml-electric-slash
)
418 (define-key map
[C-return
] 'nxml-complete
)
419 (when nxml-bind-meta-tab-to-complete-flag
420 (define-key map
"\M-\t" 'nxml-complete
))
422 "Keymap for nxml-mode.")
424 (defvar nxml-font-lock-keywords
425 '(nxml-fontify-matcher)
426 "Default font lock keywords for nxml-mode.")
428 (defsubst nxml-set-face
(start end face
)
429 (when (and face
(< start end
))
430 (font-lock-append-text-property start end
'face face
)))
434 ;; We use C-c C-i instead of \\[nxml-balanced-close-start-tag-inline]
435 ;; because Emacs turns C-c C-i into C-c TAB which is hard to type and
437 "Major mode for editing XML.
439 \\[nxml-finish-element] finishes the current element by inserting an end-tag.
440 C-c C-i closes a start-tag with `>' and then inserts a balancing end-tag
441 leaving point between the start-tag and end-tag.
442 \\[nxml-balanced-close-start-tag-block] is similar but for block rather than inline elements:
443 the start-tag, point, and end-tag are all left on separate lines.
444 If `nxml-slash-auto-complete-flag' is non-nil, then inserting a `</'
445 automatically inserts the rest of the end-tag.
447 \\[nxml-complete] performs completion on the symbol preceding point.
449 \\[nxml-dynamic-markup-word] uses the contents of the current buffer
450 to choose a tag to put around the word preceding point.
452 Sections of the document can be displayed in outline form. The
453 variable `nxml-section-element-name-regexp' controls when an element
454 is recognized as a section. The same key sequences that change
455 visibility in outline mode are used except that they start with C-c C-o
458 Validation is provided by the related minor-mode `rng-validate-mode'.
459 This also makes completion schema- and context- sensitive. Element
460 names, attribute names, attribute values and namespace URIs can all be
461 completed. By default, `rng-validate-mode' is automatically enabled.
462 You can toggle it using \\[rng-validate-mode] or change the default by
463 customizing `rng-nxml-auto-validate-flag'.
465 \\[indent-for-tab-command] indents the current line appropriately.
466 This can be customized using the variable `nxml-child-indent'
467 and the variable `nxml-attribute-indent'.
469 \\[nxml-insert-named-char] inserts a character reference using
470 the character's name (by default, the Unicode name).
471 \\[universal-argument] \\[nxml-insert-named-char] inserts the character directly.
473 The Emacs commands that normally operate on balanced expressions will
474 operate on XML markup items. Thus \\[forward-sexp] will move forward
475 across one markup item; \\[backward-sexp] will move backward across
476 one markup item; \\[kill-sexp] will kill the following markup item;
477 \\[mark-sexp] will mark the following markup item. By default, each
478 tag each treated as a single markup item; to make the complete element
479 be treated as a single markup item, set the variable
480 `nxml-sexp-element-flag' to t. For more details, see the function
481 `nxml-forward-balanced-item'.
483 \\[nxml-backward-up-element] and \\[nxml-down-element] move up and down the element structure.
485 Many aspects this mode can be customized using
486 \\[customize-group] nxml RET."
488 (kill-all-local-variables)
489 (setq major-mode
'nxml-mode
)
490 (setq mode-name
"nXML")
491 (set (make-local-variable 'mode-line-process
) '((nxml-degraded "/degraded")))
492 ;; We'll determine the fill prefix ourselves
493 (make-local-variable 'adaptive-fill-mode
)
494 (setq adaptive-fill-mode nil
)
495 (make-local-variable 'forward-sexp-function
)
496 (setq forward-sexp-function
'nxml-forward-balanced-item
)
497 (make-local-variable 'indent-line-function
)
498 (setq indent-line-function
'nxml-indent-line
)
499 (make-local-variable 'fill-paragraph-function
)
500 (setq fill-paragraph-function
'nxml-do-fill-paragraph
)
502 ;; This doesn't seem to work too well;
503 ;; I think we should probably roll our own nxml-comment-dwim function.
504 (make-local-variable 'comment-indent-function
)
505 (setq comment-indent-function
'nxml-indent-line
)
506 (make-local-variable 'comment-start
)
507 (setq comment-start
"<!--")
508 (make-local-variable 'comment-start-skip
)
509 (setq comment-start-skip
"<!--[ \t\r\n]*")
510 (make-local-variable 'comment-end
)
511 (setq comment-end
"-->")
512 (make-local-variable 'comment-end-skip
)
513 (setq comment-end-skip
"[ \t\r\n]*-->")
514 (make-local-variable 'comment-line-break-function
)
515 (setq comment-line-break-function
'nxml-newline-and-indent
)
516 (use-local-map nxml-mode-map
)
520 (nxml-clear-dependent-regions (point-min) (point-max))
521 (setq nxml-scan-end
(copy-marker (point-min) nil
))
522 (nxml-with-unmodifying-text-property-changes
523 (nxml-clear-inside (point-min) (point-max))
524 (nxml-with-invisible-motion
525 (nxml-scan-prolog)))))
526 (add-hook 'after-change-functions
'nxml-after-change nil t
)
527 (add-hook 'change-major-mode-hook
'nxml-cleanup nil t
)
529 ;; Emacs 23 handles the encoding attribute on the xml declaration
530 ;; transparently to nxml-mode, so there is no longer a need for the below
531 ;; hook. The hook also had the drawback of overriding explicit user
532 ;; instruction to save as some encoding other than utf-8.
533 ;;; (add-hook 'write-contents-hooks 'nxml-prepare-to-save)
534 (when (not (and (buffer-file-name) (file-exists-p (buffer-file-name))))
535 (when (and nxml-default-buffer-file-coding-system
536 (not (local-variable-p 'buffer-file-coding-system
)))
537 (setq buffer-file-coding-system nxml-default-buffer-file-coding-system
))
538 (when nxml-auto-insert-xml-declaration-flag
539 (nxml-insert-xml-declaration)))
541 (setq font-lock-defaults
542 '(nxml-font-lock-keywords
543 t
; keywords-only; we highlight comments and strings here
544 nil
; font-lock-keywords-case-fold-search. XML is case sensitive
545 nil
; no special syntax table
546 nil
; no automatic syntactic fontification
547 (font-lock-extend-after-change-region-function
548 . nxml-extend-after-change-region
)
549 (font-lock-extend-region-functions .
(nxml-extend-region))
550 (jit-lock-contextually . t
)
551 (font-lock-unfontify-region-function . nxml-unfontify-region
)))
554 (nxml-enable-unicode-char-name-sets)
555 (run-mode-hooks 'nxml-mode-hook
))
557 (defun nxml-cleanup ()
558 "Clean up after nxml-mode."
559 ;; Disable associated minor modes.
560 (rng-validate-mode -
1)
561 ;; Clean up fontification.
564 (let ((inhibit-read-only t
)
566 (modified (buffer-modified-p)))
567 (nxml-with-invisible-motion
568 (remove-text-properties (point-min) (point-max) '(face)))
569 (set-buffer-modified-p modified
)))
570 (remove-hook 'change-major-mode-hook
'nxml-cleanup t
))
572 (defun nxml-degrade (context err
)
573 (message "Internal nXML mode error in %s (%s), degrading"
575 (error-message-string err
))
577 (setq nxml-degraded t
)
578 (setq nxml-prolog-end
1)
582 (nxml-with-unmodifying-text-property-changes
583 (nxml-clear-inside (point-min) (point-max))))))
585 ;;; Change management
587 (defun nxml-debug-region (start end
)
589 (let ((font-lock-beg start
)
592 (goto-char font-lock-beg
)
593 (set-mark font-lock-end
)))
595 (defun nxml-after-change (start end pre-change-length
)
596 ; In font-lock mode, nxml-after-change1 is called via
597 ; nxml-extend-after-change-region instead so that the updated
598 ; book-keeping information is available for fontification.
599 (unless (or font-lock-mode nxml-degraded
)
600 (nxml-with-degradation-on-error 'nxml-after-change
605 (nxml-with-invisible-motion
606 (nxml-with-unmodifying-text-property-changes
608 start end pre-change-length
)))))))))
610 (defun nxml-after-change1 (start end pre-change-length
)
611 "After-change bookkeeping.
612 Returns a cons cell containing a possibly-enlarged change region.
613 You must call `nxml-extend-region' on this expanded region to obtain
614 the full extent of the area needing refontification.
616 For bookkeeping, call this function even when fontification is
618 (let ((pre-change-end (+ start pre-change-length
)))
620 (nxml-adjust-start-for-dependent-regions start
623 ;; If the prolog might have changed, rescan the prolog
625 ;; Add 2 so as to include the < and following char that
626 ;; start the instance (document element), since changing
627 ;; these can change where the prolog ends.
628 (+ nxml-prolog-end
2))
629 ;; end must be extended to at least the end of the old prolog in
630 ;; case the new prolog is shorter
631 (when (< pre-change-end nxml-prolog-end
)
633 ;; don't let end get out of range even if pre-change-length
636 (+ end
(- nxml-prolog-end pre-change-end
)))))
638 (setq start
(point-min))))
640 (when (> end nxml-prolog-end
)
642 (nxml-move-tag-backwards (point-min))
644 (setq end
(max (nxml-scan-after-change start end
)
647 (nxml-debug-change "nxml-after-change1" start end
)
652 (defun nxml-insert-xml-declaration ()
653 "Insert an XML declaration at the beginning of buffer.
654 The XML declaration will declare an encoding depending on the buffer's
655 `buffer-file-coding-system'."
658 (if (and buffer-file-coding-system
659 (coding-system-p buffer-file-coding-system
)
660 (coding-system-get buffer-file-coding-system
662 buffer-file-coding-system
663 (nxml-choose-utf-coding-system))))
664 (goto-char (point-min))
665 (insert (format "<?xml version=\"1.0\" encoding=\"%s\"?>\n"
666 (nxml-coding-system-name coding-system
)))))
668 (defun nxml-prepare-to-save ()
669 (unless (and (not enable-multibyte-characters
)
670 (local-variable-p 'buffer-file-coding-system
)
671 buffer-file-coding-system
672 (or (eq (coding-system-type buffer-file-coding-system
) 5)
673 (eq buffer-file-coding-system
'no-conversion
)))
675 (setq buffer-file-coding-system
(nxml-select-coding-system))))
676 ;; nil from a function in `write-contents-hooks' means
677 ;; to continue and write the file as normal
680 (defun nxml-select-coding-system ()
681 (let* ((suitable-coding-systems
682 (find-coding-systems-region (point-min) (point-max)))
684 (goto-char (point-min))
685 (xmltok-get-declared-encoding-position)))
688 (buffer-substring-no-properties (car enc-pos
)
692 (if (string= (downcase enc-name
) "utf-16")
693 (nxml-choose-utf-16-coding-system)
694 (nxml-mime-charset-coding-system enc-name
)))
695 (enc-pos (nxml-choose-utf-coding-system)))))
696 ;; Make sure we have a coding-system
697 (unless coding-system
699 (and (not buffer-read-only
)
700 (nxml-choose-suitable-coding-system
701 suitable-coding-systems
)))
704 (format "Unknown encoding %s" enc-name
)
705 "XML declaration is not well-formed")))
706 (cond ((not coding-system
)
707 (error "%s" message
))
713 "Modify and save with encoding %s")
714 (nxml-coding-system-name coding-system
))
716 (nxml-fix-encoding-declaration enc-pos coding-system
))
717 (t (signal 'quit nil
)))))
718 ;; Make sure it can encode all the characters in the buffer
719 (unless (or (memq (coding-system-base coding-system
)
720 suitable-coding-systems
)
721 (equal suitable-coding-systems
'(undecided)))
723 (nxml-unsuitable-coding-system-message coding-system
726 (and (not buffer-read-only
)
727 (nxml-choose-suitable-coding-system
728 suitable-coding-systems
)))
729 (cond ((not coding-system
) (error "%s" message
))
730 ((y-or-n-p (concat message
731 (format ". Save with %s "
732 (nxml-coding-system-name
734 (nxml-fix-encoding-declaration enc-pos coding-system
))
735 (t (signal 'quit nil
)))))
736 ;; Merge the newline type of our existing encoding
737 (let ((current-eol-type
738 (coding-system-eol-type buffer-file-coding-system
)))
739 (when (and current-eol-type
(integerp current-eol-type
))
741 (coding-system-change-eol-conversion coding-system
745 (defun nxml-unsuitable-coding-system-message (coding-system &optional enc-name
)
746 (if (nxml-coding-system-unicode-p coding-system
)
747 "Cannot translate some characters to Unicode"
748 (format "Cannot encode some characters with %s"
750 (nxml-coding-system-name coding-system
)))))
752 (defconst nxml-utf-16-coding-systems
(and (coding-system-p 'utf-16-be
)
753 (coding-system-p 'utf-16-le
)
754 '(utf-16-be utf-16-le
)))
756 (defconst nxml-utf-coding-systems
(cons 'utf-8 nxml-utf-16-coding-systems
))
758 (defun nxml-coding-system-unicode-p (coding-system)
759 (nxml-coding-system-member (coding-system-base coding-system
)
760 nxml-utf-coding-systems
))
762 (defun nxml-coding-system-name (coding-system)
763 (setq coding-system
(coding-system-base coding-system
))
765 (if (nxml-coding-system-member coding-system nxml-utf-16-coding-systems
)
767 (or (coding-system-get coding-system
'mime-charset
)
770 (defun nxml-fix-encoding-declaration (enc-pos coding-system
)
771 (let ((charset (nxml-coding-system-name coding-system
)))
772 (cond ((consp enc-pos
)
773 (delete-region (car enc-pos
) (cdr enc-pos
))
774 (goto-char (car enc-pos
))
778 (insert " encoding=\"" charset ?
\"))
780 (goto-char (point-min))
781 (insert "<?xml version=\"1.0\" encoding=\""
784 (when (and (not enc-pos
)
785 (let ((case-fold-search t
))
786 (looking-at xmltok-bad-xml-decl-regexp
)))
787 (delete-region (point) (match-end 0)))))))
789 (defun nxml-choose-suitable-coding-system (suitable-coding-systems)
790 (let (ret coding-system
)
791 (if (and buffer-file-coding-system
792 (memq (coding-system-base buffer-file-coding-system
)
793 suitable-coding-systems
))
794 buffer-file-coding-system
795 (while (and suitable-coding-systems
(not ret
))
796 (setq coding-system
(car suitable-coding-systems
))
797 (if (coding-system-get coding-system
'mime-charset
)
798 (setq ret coding-system
)
799 (setq suitable-coding-systems
(cdr suitable-coding-systems
))))
802 (defun nxml-choose-utf-coding-system ()
803 (let ((cur (and (local-variable-p 'buffer-file-coding-system
)
804 buffer-file-coding-system
805 (coding-system-base buffer-file-coding-system
))))
806 (cond ((car (nxml-coding-system-member cur nxml-utf-coding-systems
)))
807 ((and nxml-prefer-utf-16-to-utf-8-flag
808 (coding-system-p 'utf-16-le
)
809 (coding-system-p 'utf-16-be
))
810 (if nxml-prefer-utf-16-little-to-big-endian-flag
815 (defun nxml-choose-utf-16-coding-system ()
816 (let ((cur (and (local-variable-p 'buffer-file-coding-system
)
817 buffer-file-coding-system
818 (coding-system-base buffer-file-coding-system
))))
819 (cond ((car (nxml-coding-system-member cur nxml-utf-16-coding-systems
)))
820 (nxml-prefer-utf-16-little-to-big-endian-flag
821 (and (coding-system-p 'utf-16-le
) 'utf-16-le
))
822 (t (and (coding-system-p 'utf-16-be
) 'utf-16-be
)))))
824 (defun nxml-coding-system-member (coding-system coding-systems
)
826 (while (and coding-systems
(not ret
))
827 (if (coding-system-equal coding-system
828 (car coding-systems
))
829 (setq ret coding-systems
)
830 (setq coding-systems
(cdr coding-systems
))))
835 (defun nxml-unfontify-region (start end
)
836 (font-lock-default-unfontify-region start end
)
837 (nxml-clear-char-ref-extra-display start end
))
839 (defvar font-lock-beg
) (defvar font-lock-end
)
840 (defun nxml-extend-region ()
841 "Extend the region to hold the minimum area we can fontify with nXML.
842 Called with `font-lock-beg' and `font-lock-end' dynamically bound."
843 (let ((start font-lock-beg
)
846 (nxml-debug-change "nxml-extend-region(input)" start end
)
848 (when (< start nxml-prolog-end
)
849 (setq start
(point-min)))
851 (cond ((<= end nxml-prolog-end
)
852 (setq end nxml-prolog-end
))
856 ;; some font-lock backends (like Emacs 22 jit-lock) snap
857 ;; the region to the beginning of the line no matter what
858 ;; we say here. To mitigate the resulting excess
859 ;; fontification, ignore leading whitespace.
860 (skip-syntax-forward " ")
862 ;; find the beginning of the previous tag
863 (when (not (equal (char-after) ?\
<))
864 (search-backward "<" nxml-prolog-end t
))
865 (nxml-ensure-scan-up-to-date)
866 (nxml-move-outside-backwards)
869 (while (< (point) end
)
870 (nxml-tokenize-forward))
874 (when (or (< start font-lock-beg
)
875 (> end font-lock-end
))
876 (setq font-lock-beg start
878 (nxml-debug-change "nxml-extend-region" start end
)
881 (defun nxml-extend-after-change-region (start end pre-change-length
)
882 (unless nxml-degraded
883 (setq nxml-last-fontify-end nil
)
884 (let ((region (nxml-with-degradation-on-error
885 'nxml-extend-after-change-region
890 (nxml-with-invisible-motion
891 (nxml-with-unmodifying-text-property-changes
892 (nxml-extend-after-change-region1
893 start end pre-change-length
)))))))))
894 (if (consp region
) region
))))
896 (defun nxml-extend-after-change-region1 (start end pre-change-length
)
897 (let* ((region (nxml-after-change1 start end pre-change-length
))
898 (font-lock-beg (car region
))
899 (font-lock-end (cdr region
)))
902 (cons font-lock-beg font-lock-end
)))
904 (defun nxml-fontify-matcher (bound)
905 "Called as font-lock keyword matcher."
907 (unless nxml-degraded
908 (nxml-debug-change "nxml-fontify-matcher" (point) bound
)
910 (when (< (point) nxml-prolog-end
)
911 ;; prolog needs to be fontified in one go, and
912 ;; nxml-extend-region makes sure we start at BOB.
914 (nxml-fontify-prolog)
915 (goto-char nxml-prolog-end
))
917 (let (xmltok-dependent-regions
919 (while (and (nxml-tokenize-forward)
920 (<= (point) bound
)) ; intervals are open-ended
921 (nxml-apply-fontify-rule)))
923 (setq nxml-last-fontify-end
(point)))
925 ;; Since we did the fontification internally, tell font-lock to not
926 ;; do anything itself.
929 (defun nxml-fontify-prolog ()
931 The buffer is assumed to be prepared for fontification.
932 This does not set the fontified property, but it does clear
933 faces appropriately."
934 (let ((regions nxml-prolog-regions
))
936 (let ((region (car regions
)))
937 (nxml-apply-fontify-rule (aref region
0)
940 (setq regions
(cdr regions
)))))
942 ;; Vectors identify a substring of the token to be highlighted in some face.
944 ;; Token types returned by xmltok-forward.
948 '([nil
1 nxml-tag-delimiter
]
949 [-
1 nil nxml-tag-delimiter
]
953 (put 'partial-start-tag
955 '([nil
1 nxml-tag-delimiter
]
961 '([nil
1 nxml-tag-delimiter
]
963 [-
1 nil nxml-tag-delimiter
]
964 (element-qname .
2)))
966 (put 'partial-end-tag
968 '([nil
1 nxml-tag-delimiter
]
970 (element-qname .
2)))
974 '([nil
1 nxml-tag-delimiter
]
975 [-
2 -
1 nxml-tag-slash
]
976 [-
1 nil nxml-tag-delimiter
]
980 (put 'partial-empty-element
982 '([nil
1 nxml-tag-delimiter
]
983 [-
1 nil nxml-tag-slash
]
989 '([nil
2 nxml-char-ref-delimiter
]
990 [2 -
1 nxml-char-ref-number
]
991 [-
1 nil nxml-char-ref-delimiter
]
996 '([nil
1 nxml-entity-ref-delimiter
]
997 [1 -
1 nxml-entity-ref-name
]
998 [-
1 nil nxml-entity-ref-delimiter
]))
1002 '([nil
4 nxml-comment-delimiter
]
1003 [4 -
3 nxml-comment-content
]
1004 [-
3 nil nxml-comment-delimiter
]))
1006 (put 'processing-instruction
1008 '([nil
2 nxml-processing-instruction-delimiter
]
1009 [-
2 nil nxml-processing-instruction-delimiter
]
1010 processing-instruction-content
))
1014 '([nil
3 nxml-cdata-section-delimiter
] ; <![
1015 [3 8 nxml-cdata-section-CDATA
] ; CDATA
1016 [8 9 nxml-cdata-section-delimiter
] ; [
1017 [9 -
3 nxml-cdata-section-content
] ; ]]>
1018 [-
3 nil nxml-cdata-section-delimiter
]))
1022 '([nil nil nxml-text
]))
1024 ;; Prolog region types in list returned by xmltok-forward-prolog.
1026 (put 'xml-declaration
1028 '([nil
2 nxml-processing-instruction-delimiter
]
1029 [2 5 nxml-processing-instruction-target
]
1030 [-
2 nil nxml-processing-instruction-delimiter
]))
1032 (put 'xml-declaration-attribute-name
1034 '([nil nil nxml-attribute-local-name
]))
1036 (put 'xml-declaration-attribute-value
1038 '([nil
1 nxml-attribute-value-delimiter
]
1039 [1 -
1 nxml-attribute-value
]
1040 [-
1 nil nxml-attribute-value-delimiter
]))
1042 (put 'processing-instruction-left
1044 '([nil
2 nxml-processing-instruction-delimiter
]
1045 [2 nil nxml-processing-instruction-target
]))
1047 (put 'processing-instruction-right
1049 '([nil -
2 nxml-processing-instruction-content
]
1050 [-
2 nil nxml-processing-instruction-delimiter
]))
1054 '([nil
1 nxml-prolog-literal-delimiter
]
1055 [1 -
1 nxml-prolog-literal-content
]
1056 [-
1 nil nxml-prolog-literal-delimiter
]))
1060 '([nil nil nxml-prolog-keyword
]))
1062 (put 'markup-declaration-open
1064 '([0 2 nxml-markup-declaration-delimiter
]
1065 [2 nil nxml-prolog-keyword
]))
1067 (put 'markup-declaration-close
1069 '([nil nil nxml-markup-declaration-delimiter
]))
1071 (put 'internal-subset-open
1073 '([nil nil nxml-markup-declaration-delimiter
]))
1075 (put 'internal-subset-close
1077 '([nil
1 nxml-markup-declaration-delimiter
]
1078 [-
1 nil nxml-markup-declaration-delimiter
]))
1083 [1 nil nxml-prolog-keyword
]))
1085 (defun nxml-apply-fontify-rule (&optional type start end
)
1086 (let ((rule (get (or type xmltok-type
) 'nxml-fontify-rule
)))
1087 (unless start
(setq start xmltok-start
))
1088 (unless end
(setq end
(point)))
1090 (let* ((action (car rule
)))
1091 (setq rule
(cdr rule
))
1092 (cond ((vectorp action
)
1093 (nxml-set-face (let ((offset (aref action
0)))
1094 (cond ((not offset
) start
)
1095 ((< offset
0) (+ end offset
))
1096 (t (+ start offset
))))
1097 (let ((offset (aref action
1)))
1098 (cond ((not offset
) end
)
1099 ((< offset
0) (+ end offset
))
1100 (t (+ start offset
))))
1102 ((and (consp action
)
1103 (eq (car action
) 'element-qname
))
1104 (when xmltok-name-end
; maybe nil in partial-end-tag case
1105 (nxml-fontify-qname (+ start
(cdr action
))
1108 'nxml-element-prefix
1110 'nxml-element-local-name
)))
1111 ((eq action
'attributes
)
1112 (nxml-fontify-attributes))
1113 ((eq action
'processing-instruction-content
)
1114 (nxml-set-face (+ start
2)
1116 'nxml-processing-instruction-target
)
1117 (nxml-set-face (save-excursion
1118 (goto-char xmltok-name-end
)
1119 (skip-chars-forward " \t\r\n")
1122 'nxml-processing-instruction-content
))
1123 ((eq action
'char-ref
)
1124 (nxml-char-ref-display-extra start
1126 (xmltok-char-number start end
)))
1127 (t (error "Invalid nxml-fontify-rule action %s" action
)))))))
1129 (defun nxml-fontify-attributes ()
1130 (while xmltok-namespace-attributes
1131 (nxml-fontify-attribute (car xmltok-namespace-attributes
)
1133 (setq xmltok-namespace-attributes
1134 (cdr xmltok-namespace-attributes
)))
1135 (while xmltok-attributes
1136 (nxml-fontify-attribute (car xmltok-attributes
))
1137 (setq xmltok-attributes
1138 (cdr xmltok-attributes
))))
1140 (defun nxml-fontify-attribute (att &optional namespace-declaration
)
1141 (if namespace-declaration
1142 (nxml-fontify-qname (xmltok-attribute-name-start att
)
1143 (xmltok-attribute-name-colon att
)
1144 (xmltok-attribute-name-end att
)
1145 'nxml-namespace-attribute-xmlns
1146 'nxml-namespace-attribute-colon
1147 'nxml-namespace-attribute-prefix
1148 'nxml-namespace-attribute-xmlns
)
1149 (nxml-fontify-qname (xmltok-attribute-name-start att
)
1150 (xmltok-attribute-name-colon att
)
1151 (xmltok-attribute-name-end att
)
1152 'nxml-attribute-prefix
1153 'nxml-attribute-colon
1154 'nxml-attribute-local-name
))
1155 (let ((start (xmltok-attribute-value-start att
))
1156 (end (xmltok-attribute-value-end att
))
1157 (refs (xmltok-attribute-refs att
))
1158 (delimiter-face (if namespace-declaration
1159 'nxml-namespace-attribute-value-delimiter
1160 'nxml-attribute-value-delimiter
))
1161 (value-face (if namespace-declaration
1162 'nxml-namespace-attribute-value
1163 'nxml-attribute-value
)))
1165 (nxml-set-face (1- start
) start delimiter-face
)
1166 (nxml-set-face end
(1+ end
) delimiter-face
)
1168 (let* ((ref (car refs
))
1169 (ref-type (aref ref
0))
1170 (ref-start (aref ref
1))
1171 (ref-end (aref ref
2)))
1172 (nxml-set-face start ref-start value-face
)
1173 (nxml-apply-fontify-rule ref-type ref-start ref-end
)
1174 (setq start ref-end
))
1175 (setq refs
(cdr refs
)))
1176 (nxml-set-face start end value-face
))))
1178 (defun nxml-fontify-qname (start
1186 (cond (colon (nxml-set-face start colon prefix-face
)
1187 (nxml-set-face colon
(1+ colon
) colon-face
)
1188 (nxml-set-face (1+ colon
) end local-name-face
))
1189 (t (nxml-set-face start end
(or unprefixed-face
1190 local-name-face
)))))
1194 (defun nxml-electric-slash (arg)
1197 With a prefix ARG, do nothing other than insert the slash.
1199 Otherwise, if `nxml-slash-auto-complete-flag' is non-nil, insert the
1200 rest of the end-tag or empty-element if the slash is potentially part
1201 of an end-tag or the close of an empty-element.
1203 If the slash is part of an end-tag that is the first non-whitespace
1204 on the line, reindent the line."
1206 (nxml-ensure-scan-up-to-date)
1207 (let* ((slash-pos (point))
1208 (end-tag-p (and (eq (char-before slash-pos
) ?
<)
1209 (not (nxml-get-inside slash-pos
))))
1210 (at-indentation (save-excursion
1211 (back-to-indentation)
1212 (eq (point) (1- slash-pos
)))))
1213 (self-insert-command (prefix-numeric-value arg
))
1215 (if nxml-slash-auto-complete-flag
1218 (let ((start-tag-end
1219 (nxml-scan-element-backward (1- slash-pos
) t
)))
1221 (insert (xmltok-start-tag-qname) ">")
1222 ;; copy the indentation of the start-tag
1223 (when (and at-indentation
1225 (goto-char xmltok-start
)
1226 (back-to-indentation)
1227 (eq (point) xmltok-start
)))
1229 (indent-line-to (save-excursion
1230 (goto-char xmltok-start
)
1231 (current-column)))))))
1232 (nxml-scan-error nil
))
1233 (when (and (eq (nxml-token-before) (point))
1234 (eq xmltok-type
'partial-empty-element
))
1236 (when (and end-tag-p at-indentation
)
1237 (nxml-indent-line))))))
1239 (defun nxml-balanced-close-start-tag-block ()
1240 "Close the start-tag before point with `>' and insert a balancing end-tag.
1241 Point is left between the start-tag and the end-tag.
1242 If there is nothing but whitespace before the `<' that opens the
1243 start-tag, then put point on a blank line, and put the end-tag on
1244 another line aligned with the start-tag."
1246 (nxml-balanced-close-start-tag 'block
))
1248 (defun nxml-balanced-close-start-tag-inline ()
1249 "Close the start-tag before point with `>' and insert a balancing end-tag.
1250 Point is left between the start-tag and the end-tag.
1251 No extra whitespace is inserted."
1253 (nxml-balanced-close-start-tag 'inline
))
1255 (defun nxml-balanced-close-start-tag (block-or-inline)
1256 (let ((token-end (nxml-token-before))
1258 (token-start xmltok-start
))
1259 (unless (or (eq xmltok-type
'partial-start-tag
)
1260 (and (memq xmltok-type
'(start-tag
1262 partial-empty-element
))
1263 (>= token-end pos
)))
1264 (error "Not in a start-tag"))
1265 ;; Note that this insertion changes xmltok-start.
1267 (buffer-substring-no-properties (+ xmltok-start
1)
1268 (min xmltok-name-end
(point)))
1270 (if (eq block-or-inline
'inline
)
1272 (goto-char token-start
)
1273 (back-to-indentation)
1274 (if (= (point) token-start
)
1275 (let ((indent (current-column)))
1278 (indent-line-to indent
)
1281 (indent-line-to (+ nxml-child-indent indent
)))
1284 (defun nxml-finish-element ()
1285 "Finish the current element by inserting an end-tag."
1287 (nxml-finish-element-1 nil
))
1289 (defvar nxml-last-split-position nil
1290 "Position where `nxml-split-element' split the current element.")
1292 (defun nxml-split-element ()
1293 "Split the current element by inserting an end-tag and a start-tag.
1294 Point is left after the newly inserted start-tag. When repeated,
1295 split immediately before the previously inserted start-tag and leave
1298 (setq nxml-last-split-position
1299 (if (and (eq last-command this-command
)
1300 nxml-last-split-position
)
1302 (goto-char nxml-last-split-position
)
1303 (nxml-finish-element-1 t
))
1304 (nxml-finish-element-1 t
))))
1306 (defun nxml-finish-element-1 (startp)
1307 "Insert an end-tag for the current element and optionally a start-tag.
1308 The start-tag is inserted if STARTP is non-nil. Return the position
1309 of the inserted start-tag or nil if none was inserted."
1311 (let* ((token-end (nxml-token-before))
1314 (when (and (< (point) token-end
)
1317 processing-instruction
1322 (error "Point is inside a %s"
1323 (nxml-token-type-friendly-name xmltok-type
)))
1324 (nxml-scan-element-backward token-end t
)))
1327 (unless (eq xmltok-type
'start-tag
)
1328 (error "No matching start-tag"))
1329 (goto-char xmltok-start
)
1330 (back-to-indentation)
1331 (eq (point) xmltok-start
)))
1334 (goto-char start-tag-end
)
1335 (looking-at "[ \t\r\n]*$")))
1336 (start-tag-indent (save-excursion
1337 (goto-char xmltok-start
)
1339 (qname (xmltok-start-tag-qname))
1340 inserted-start-tag-pos
)
1341 (when (and starts-line ends-line
)
1342 ;; start-tag is on a line by itself
1343 ;; => put the end-tag on a line by itself
1346 (back-to-indentation)
1349 (indent-line-to start-tag-indent
))
1350 (insert "</" qname
">")
1354 (indent-line-to start-tag-indent
))
1355 (setq inserted-start-tag-pos
(point))
1356 (insert "<" qname
">")
1357 (when (and starts-line ends-line
)
1359 (indent-line-to (save-excursion
1360 (goto-char xmltok-start
)
1362 (back-to-indentation)
1363 (if (= (current-column)
1364 (+ start-tag-indent nxml-child-indent
))
1365 (+ start-tag-indent nxml-child-indent
)
1366 start-tag-indent
)))))
1367 inserted-start-tag-pos
))
1371 (defun nxml-indent-line ()
1372 "Indent current line as XML."
1373 (let ((indent (nxml-compute-indent))
1374 (from-end (- (point-max) (point))))
1376 (/= indent
(current-indentation)))
1378 (let ((bol (point)))
1379 (skip-chars-forward " \t")
1380 (delete-region bol
(point)))
1382 (when (> (- (point-max) from-end
) (point))
1383 (goto-char (- (point-max) from-end
))))))
1385 (defun nxml-compute-indent ()
1386 "Return the indent for the line containing point."
1387 (or (nxml-compute-indent-from-matching-start-tag)
1388 (nxml-compute-indent-from-previous-line)))
1390 (defun nxml-compute-indent-from-matching-start-tag ()
1391 "Compute the indent for a line with an end-tag using the matching start-tag.
1392 When the line containing point ends with an end-tag and does not start
1393 in the middle of a token, return the indent of the line containing the
1394 matching start-tag, if there is one and it occurs at the beginning of
1395 its line. Otherwise return nil."
1397 (back-to-indentation)
1398 (let ((bol (point)))
1399 (let ((inhibit-field-text-motion t
))
1401 (skip-chars-backward " \t")
1402 (and (= (nxml-token-before) (point))
1403 (memq xmltok-type
'(end-tag partial-end-tag
))
1404 ;; start of line must not be inside a token
1405 (or (= xmltok-start bol
)
1409 (= xmltok-start bol
))
1410 (eq xmltok-type
'data
))
1412 (nxml-scan-element-backward
1416 nxml-end-tag-indent-scan-distance
))
1417 (nxml-scan-error nil
))
1418 (< xmltok-start bol
)
1420 (goto-char xmltok-start
)
1421 (skip-chars-backward " \t")
1423 (current-indentation)))))
1425 (defun nxml-compute-indent-from-previous-line ()
1426 "Compute the indent for a line using the indentation of a previous line."
1431 before-context after-context
)
1432 (back-to-indentation)
1435 ;; Move backwards until the start of a non-blank line that is
1436 ;; not inside a token.
1438 (when (= (forward-line -
1) -
1)
1440 (back-to-indentation)
1441 (if (looking-at "[ \t]*$")
1444 (setq prev-bol
(point)))
1446 (not (or (= xmltok-start
(point))
1447 (eq xmltok-type
'data
))))))
1449 ;; Now scan over tokens until the end of the line to be indented.
1450 ;; Determine the context before and after the beginning of the
1452 (while (< (point) eol
)
1453 (nxml-tokenize-forward)
1454 (cond ((<= bol xmltok-start
)
1456 (nxml-merge-indent-context-type after-context
)))
1457 ((and (<= (point) bol
)
1458 (not (and (eq xmltok-type
'partial-start-tag
)
1460 (setq before-context
1461 (nxml-merge-indent-context-type before-context
)))
1462 ((eq xmltok-type
'data
)
1463 (setq before-context
1464 (nxml-merge-indent-context-type before-context
))
1466 (nxml-merge-indent-context-type after-context
)))
1467 ;; If in the middle of a token that looks inline,
1468 ;; then indent relative to the previous non-blank line
1469 ((eq (nxml-merge-indent-context-type before-context
)
1471 (goto-char prev-bol
)
1472 (throw 'indent
(current-column)))
1475 (nxml-compute-indent-in-token bol
))))
1476 (skip-chars-forward " \t\r\n"))
1479 (* nxml-child-indent
1480 (+ (if (eq before-context
'start-tag
) 1 0)
1481 (if (eq after-context
'end-tag
) -
1 0))))))))
1483 (defun nxml-merge-indent-context-type (context)
1484 "Merge the indent context type CONTEXT with the token in `xmltok-type'.
1485 Return the merged indent context type. An indent context type is
1486 either nil or one of the symbols `start-tag', `end-tag', `markup',
1487 `comment', `mixed'."
1488 (cond ((memq xmltok-type
'(start-tag partial-start-tag
))
1489 (if (memq context
'(nil start-tag comment
))
1492 ((memq xmltok-type
'(end-tag partial-end-tag
))
1493 (if (memq context
'(nil end-tag comment
))
1496 ((eq xmltok-type
'comment
)
1497 (cond ((memq context
'(start-tag end-tag comment
))
1504 (defun nxml-compute-indent-in-token (pos)
1505 "Return the indent for a line that starts inside a token.
1506 POS is the position of the first non-whitespace character of the line.
1507 This expects the xmltok-* variables to be set up as by `xmltok-forward'."
1508 (cond ((memq xmltok-type
'(start-tag
1511 partial-empty-element
))
1512 (nxml-compute-indent-in-start-tag pos
))
1513 ((eq xmltok-type
'comment
)
1514 (nxml-compute-indent-in-delimited-token pos
"<!--" "-->"))
1515 ((eq xmltok-type
'cdata-section
)
1516 (nxml-compute-indent-in-delimited-token pos
"<![CDATA[" "]]>"))
1517 ((eq xmltok-type
'processing-instruction
)
1518 (nxml-compute-indent-in-delimited-token pos
"<?" "?>"))
1521 (if (and (= (forward-line -
1) 0)
1522 (< xmltok-start
(point)))
1523 (back-to-indentation)
1524 (goto-char xmltok-start
))
1527 (defun nxml-compute-indent-in-start-tag (pos)
1528 "Return the indent for a line that starts inside a start-tag.
1529 Also for a line that starts inside an empty element.
1530 POS is the position of the first non-whitespace character of the line.
1531 This expects the xmltok-* variables to be set up as by `xmltok-forward'."
1532 (let ((value-boundary (nxml-attribute-value-boundary pos
))
1535 ;; inside an attribute value
1536 (let ((value-start (car value-boundary
))
1537 (value-end (cdr value-boundary
)))
1540 (if (< (point) value-start
)
1541 (goto-char value-start
)
1542 (back-to-indentation)))
1543 ;; outside an attribute value
1545 (while (and (= (forward-line -
1) 0)
1546 (nxml-attribute-value-boundary (point))))
1547 (cond ((<= (point) xmltok-start
)
1548 (goto-char xmltok-start
)
1549 (setq off nxml-attribute-indent
)
1550 (let ((atts (xmltok-merge-attributes)))
1552 (let* ((att (car atts
))
1553 (start (xmltok-attribute-name-start att
)))
1558 (back-to-indentation))))
1559 (+ (current-column) off
)))
1561 (defun nxml-attribute-value-boundary (pos)
1562 "Return a pair (START . END) if POS is inside an attribute value.
1563 Otherwise return nil. START and END are the positions of the start
1564 and end of the attribute value containing POS. This expects the
1565 xmltok-* variables to be set up as by `xmltok-forward'."
1566 (let ((atts (xmltok-merge-attributes))
1567 att value-start value-end value-boundary
)
1569 (setq att
(car atts
))
1570 (setq value-start
(xmltok-attribute-value-start att
))
1571 (setq value-end
(xmltok-attribute-value-end att
))
1572 (cond ((and value-start
(< pos value-start
))
1574 ((and value-start value-end
(<= pos value-end
))
1575 (setq value-boundary
(cons value-start value-end
))
1577 (t (setq atts
(cdr atts
)))))
1580 (defun nxml-compute-indent-in-delimited-token (pos open-delim close-delim
)
1581 "Return the indent for a line that starts inside a token with delimiters.
1582 OPEN-DELIM and CLOSE-DELIM are strings giving the opening and closing
1583 delimiters. POS is the position of the first non-whitespace character
1584 of the line. This expects the xmltok-* variables to be set up as by
1586 (cond ((let ((end (+ pos
(length close-delim
))))
1587 (and (<= end
(point-max))
1588 (string= (buffer-substring-no-properties pos end
)
1590 (goto-char xmltok-start
))
1594 (<= (point) xmltok-start
))
1595 (goto-char (+ xmltok-start
(length open-delim
)))
1596 (when (and (string= open-delim
"<!--")
1598 (goto-char (1+ (point)))))
1599 (t (back-to-indentation)))
1604 (defun nxml-complete ()
1605 "Perform completion on the symbol preceding point.
1607 Inserts as many characters as can be completed. However, if not even
1608 one character can be completed, then a buffer with the possibilities
1609 is popped up and the symbol is read from the minibuffer with
1610 completion. If the symbol is complete, then any characters that must
1611 follow the symbol are also inserted.
1613 The name space used for completion and what is treated as a symbol
1614 depends on the context. The contexts in which completion is performed
1615 depend on `nxml-completion-hook'."
1617 (unless (run-hook-with-args-until-success 'nxml-completion-hook
)
1618 ;; Eventually we will complete on entity names here.
1620 (message "Cannot complete in this context")))
1624 (defun nxml-forward-balanced-item (&optional arg
)
1625 "Move forward across one balanced item.
1626 With ARG, do it that many times. Negative arg -N means
1627 move backward across N balanced expressions.
1628 This is the equivalent of `forward-sexp' for XML.
1630 An element contains as items strings with no markup, tags, processing
1631 instructions, comments, CDATA sections, entity references and
1632 characters references. However, if the variable
1633 `nxml-sexp-element-flag' is non-nil, then an element is treated as a
1634 single markup item. A start-tag contains an element name followed by
1635 one or more attributes. An end-tag contains just an element name.
1636 An attribute value literals contains strings with no markup, entity
1637 references and character references. A processing instruction
1638 consists of a target and a content string. A comment or a CDATA
1639 section contains a single string. An entity reference contains a
1640 single name. A character reference contains a character number."
1642 (or arg
(setq arg
1))
1645 (nxml-forward-single-balanced-item)
1646 (> (setq arg
(1- arg
)) 0))))
1649 (nxml-backward-single-balanced-item)
1650 (< (setq arg
(1+ arg
)) 0))))))
1652 (defun nxml-forward-single-balanced-item ()
1654 (goto-char (let ((end (nxml-token-after)))
1656 (while (eq xmltok-type
'space
)
1658 (setq end
(nxml-token-after)))
1659 (cond ((/= (point) xmltok-start
)
1660 (nxml-scan-forward-within end
))
1661 ((and nxml-sexp-element-flag
1662 (eq xmltok-type
'start-tag
))
1663 ;; can't ever return nil here
1664 (nxml-scan-element-forward xmltok-start
))
1665 ((and nxml-sexp-element-flag
1667 '(end-tag partial-end-tag
)))
1668 (error "Already at end of element"))
1671 (goto-char (cadr err
))
1672 (apply 'error
(cddr err
)))))
1674 (defun nxml-backward-single-balanced-item ()
1676 (goto-char (let ((end (nxml-token-before)))
1678 (while (eq xmltok-type
'space
)
1679 (goto-char xmltok-start
)
1680 (setq end
(nxml-token-before)))
1681 (cond ((/= (point) end
)
1682 (nxml-scan-backward-within end
))
1683 ((and nxml-sexp-element-flag
1684 (eq xmltok-type
'end-tag
))
1685 ;; can't ever return nil here
1686 (nxml-scan-element-backward end
)
1688 ((and nxml-sexp-element-flag
1689 (eq xmltok-type
'start-tag
))
1690 (error "Already at start of element"))
1691 (t xmltok-start
)))))
1693 (goto-char (cadr err
))
1694 (apply 'error
(cddr err
)))))
1696 (defun nxml-scan-forward-within (end)
1697 (setq end
(- end
(nxml-end-delimiter-length xmltok-type
)))
1698 (when (<= end
(point))
1699 (error "Already at end of %s"
1700 (nxml-token-type-friendly-name xmltok-type
)))
1701 (cond ((memq xmltok-type
'(start-tag
1704 partial-empty-element
))
1705 (if (< (point) xmltok-name-end
)
1707 (let ((att (nxml-find-following-attribute)))
1708 (cond ((not att
) end
)
1709 ((and (xmltok-attribute-value-start att
)
1710 (<= (xmltok-attribute-value-start att
)
1712 (nxml-scan-forward-in-attribute-value att
))
1713 ((xmltok-attribute-value-end att
)
1714 (1+ (xmltok-attribute-value-end att
)))
1716 (goto-char (xmltok-attribute-name-end att
))
1717 (looking-at "[ \t\r\n]*="))
1719 (t (xmltok-attribute-name-end att
))))))
1720 ((and (eq xmltok-type
'processing-instruction
)
1721 (< (point) xmltok-name-end
))
1725 (defun nxml-scan-backward-within (end)
1728 (nxml-start-delimiter-length xmltok-type
)))
1729 (when (<= (point) xmltok-start
)
1730 (error "Already at start of %s"
1731 (nxml-token-type-friendly-name xmltok-type
)))
1732 (cond ((memq xmltok-type
'(start-tag
1735 partial-empty-element
))
1736 (let ((att (nxml-find-preceding-attribute)))
1737 (cond ((not att
) xmltok-start
)
1738 ((and (xmltok-attribute-value-start att
)
1739 (<= (xmltok-attribute-value-start att
)
1742 (xmltok-attribute-value-end att
)))
1743 (nxml-scan-backward-in-attribute-value att
))
1744 (t (xmltok-attribute-name-start att
)))))
1745 ((and (eq xmltok-type
'processing-instruction
)
1746 (let ((content-start (save-excursion
1747 (goto-char xmltok-name-end
)
1748 (skip-chars-forward " \r\t\n")
1750 (and (< content-start
(point))
1754 (defun nxml-scan-forward-in-attribute-value (att)
1755 (when (= (point) (xmltok-attribute-value-end att
))
1756 (error "Already at end of attribute value"))
1757 (let ((refs (xmltok-attribute-refs att
))
1760 (setq ref
(car refs
))
1761 (if (< (point) (aref ref
2))
1764 (setq refs
(cdr refs
))))
1766 (xmltok-attribute-value-end att
))
1767 ((< (point) (aref ref
1))
1769 ((= (point) (aref ref
1))
1772 (let ((end (- (aref ref
2)
1773 (nxml-end-delimiter-length (aref ref
0)))))
1776 (error "Already at end of %s"
1777 (nxml-token-type-friendly-name (aref ref
0)))))))))
1779 (defun nxml-scan-backward-in-attribute-value (att)
1780 (when (= (point) (xmltok-attribute-value-start att
))
1781 (error "Already at start of attribute value"))
1782 (let ((refs (reverse (xmltok-attribute-refs att
)))
1785 (setq ref
(car refs
))
1786 (if (< (aref ref
1) (point))
1789 (setq refs
(cdr refs
))))
1791 (xmltok-attribute-value-start att
))
1792 ((< (aref ref
2) (point))
1794 ((= (point) (aref ref
2))
1797 (let ((start (+ (aref ref
1)
1798 (nxml-start-delimiter-length (aref ref
0)))))
1799 (if (< start
(point))
1801 (error "Already at start of %s"
1802 (nxml-token-type-friendly-name (aref ref
0)))))))))
1804 (defun nxml-find-following-attribute ()
1806 (atts (or xmltok-attributes xmltok-namespace-attributes
))
1807 (more-atts (and xmltok-attributes xmltok-namespace-attributes
)))
1809 (let* ((att (car atts
))
1810 (name-start (xmltok-attribute-name-start att
)))
1811 (cond ((and (<= name-start
(point))
1812 (xmltok-attribute-value-end att
)
1813 ;; <= because end is before quote
1814 (<= (point) (xmltok-attribute-value-end att
)))
1817 ((and (< (point) name-start
)
1820 (xmltok-attribute-name-start ret
))))
1822 (setq atts
(cdr atts
))
1824 (setq atts more-atts
)
1825 (setq more-atts nil
)))
1828 (defun nxml-find-preceding-attribute ()
1830 (atts (or xmltok-attributes xmltok-namespace-attributes
))
1831 (more-atts (and xmltok-attributes xmltok-namespace-attributes
)))
1833 (let* ((att (car atts
))
1834 (name-start (xmltok-attribute-name-start att
)))
1835 (cond ((and (< name-start
(point))
1836 (xmltok-attribute-value-end att
)
1837 ;; <= because end is before quote
1838 (<= (point) (xmltok-attribute-value-end att
)))
1841 ((and (< name-start
(point))
1843 (< (xmltok-attribute-name-start ret
)
1846 (setq atts
(cdr atts
))
1848 (setq atts more-atts
)
1849 (setq more-atts nil
)))
1852 (defun nxml-up-element (&optional arg
)
1854 (or arg
(setq arg
1))
1856 (nxml-backward-up-element (- arg
))
1858 (while (and (> arg
0)
1859 (< (point) (point-max)))
1860 (let ((token-end (nxml-token-after)))
1861 (goto-char (cond ((or (memq xmltok-type
'(end-tag
1863 (and (memq xmltok-type
1865 partial-empty-element
))
1866 (< xmltok-start
(point))))
1868 ((nxml-scan-element-forward
1869 (if (and (eq xmltok-type
'start-tag
)
1870 (= (point) xmltok-start
))
1874 (t (error "No parent element")))))
1875 (setq arg
(1- arg
)))
1877 (goto-char (cadr err
))
1878 (apply 'error
(cddr err
))))))
1880 (defun nxml-backward-up-element (&optional arg
)
1882 (or arg
(setq arg
1))
1884 (nxml-up-element (- arg
))
1886 (while (and (> arg
0)
1887 (< (point-min) (point)))
1888 (let ((token-end (nxml-token-before)))
1889 (goto-char (cond ((or (memq xmltok-type
'(start-tag
1891 (and (memq xmltok-type
1893 partial-empty-element
))
1894 (< (point) token-end
)))
1896 ((nxml-scan-element-backward
1897 (if (and (eq xmltok-type
'end-tag
)
1898 (= (point) token-end
))
1903 (t (error "No parent element")))))
1904 (setq arg
(1- arg
)))
1906 (goto-char (cadr err
))
1907 (apply 'error
(cddr err
))))))
1909 (defun nxml-down-element (&optional arg
)
1910 "Move forward down into the content of an element.
1911 With ARG, do this that many times.
1912 Negative ARG means move backward but still down."
1914 (or arg
(setq arg
1))
1916 (nxml-backward-down-element (- arg
))
1919 (let ((token-end (nxml-token-after)))
1921 (goto-char token-end
)
1923 (when (memq xmltok-type
'(nil end-tag partial-end-tag
))
1924 (error "No following start-tags in this element"))
1925 (not (memq xmltok-type
'(start-tag partial-start-tag
))))
1926 (nxml-tokenize-forward))
1928 (setq arg
(1- arg
)))))
1930 (defun nxml-backward-down-element (&optional arg
)
1932 (or arg
(setq arg
1))
1934 (nxml-down-element (- arg
))
1939 (goto-char xmltok-start
)
1941 (when (memq xmltok-type
'(start-tag
1945 (error "No preceding end-tags in this element"))
1946 (not (memq xmltok-type
'(end-tag partial-end-tag
))))
1947 (if (or (<= (point) nxml-prolog-end
)
1948 (not (search-backward "<" nxml-prolog-end t
)))
1949 (setq xmltok-type nil
)
1950 (nxml-move-outside-backwards)
1953 (setq arg
(1- arg
)))))
1955 (defun nxml-forward-element (&optional arg
)
1956 "Move forward over one element.
1957 With ARG, do it that many times.
1958 Negative ARG means move backward."
1960 (or arg
(setq arg
1))
1962 (nxml-backward-element (- arg
))
1964 (while (and (> arg
0)
1965 (< (point) (point-max)))
1967 (or (nxml-scan-element-forward (nxml-token-before))
1968 (error "No more elements")))
1969 (setq arg
(1- arg
)))
1971 (goto-char (cadr err
))
1972 (apply 'error
(cddr err
))))))
1974 (defun nxml-backward-element (&optional arg
)
1975 "Move backward over one element.
1976 With ARG, do it that many times.
1977 Negative ARG means move forward."
1979 (or arg
(setq arg
1))
1981 (nxml-forward-element (- arg
))
1983 (while (and (> arg
0)
1984 (< (point-min) (point)))
1986 (or (and (nxml-scan-element-backward (progn
1990 (error "No preceding elements")))
1991 (setq arg
(1- arg
)))
1993 (goto-char (cadr err
))
1994 (apply 'error
(cddr err
))))))
1996 (defun nxml-mark-token-after ()
1998 (push-mark (nxml-token-after) nil t
)
1999 (goto-char xmltok-start
)
2000 (message "Marked %s" xmltok-type
))
2004 (defun nxml-mark-paragraph ()
2005 "Put point at beginning of this paragraph, mark at end.
2006 The paragraph marked is the one that contains point or follows point."
2008 (nxml-forward-paragraph)
2010 (nxml-backward-paragraph))
2012 (defun nxml-forward-paragraph (&optional arg
)
2014 (or arg
(setq arg
1))
2016 (nxml-backward-paragraph (- arg
)))
2019 (while (and (nxml-forward-single-paragraph)
2020 (> (setq arg
(1- arg
)) 0))))))
2022 (defun nxml-backward-paragraph (&optional arg
)
2024 (or arg
(setq arg
1))
2026 (nxml-forward-paragraph (- arg
)))
2029 (let ((inhibit-field-text-motion t
))
2031 (while (and (nxml-backward-single-paragraph)
2032 (> (setq arg
(1- arg
)) 0))))))
2034 (defun nxml-forward-single-paragraph ()
2035 "Move forward over a single paragraph.
2036 Return nil at end of buffer, t otherwise."
2037 (let* ((token-end (nxml-token-after))
2038 (offset (- (point) xmltok-start
))
2040 (goto-char token-end
)
2041 (while (and (< (point) (point-max))
2043 (nxml-paragraph-end-pos had-data offset
))))
2044 (when (nxml-token-contains-data-p offset
)
2046 (nxml-tokenize-forward)
2048 (when pos
(goto-char pos
))))
2050 (defun nxml-backward-single-paragraph ()
2051 "Move backward over a single paragraph.
2052 Return nil at start of buffer, t otherwise."
2053 (let* ((token-end (nxml-token-before))
2054 (offset (- token-end
(point)))
2055 (last-tag-pos xmltok-start
)
2056 pos had-data last-data-pos
)
2057 (goto-char token-end
)
2058 (unless (setq pos
(nxml-paragraph-start-pos nil offset
))
2059 (setq had-data
(nxml-token-contains-data-p nil offset
))
2060 (goto-char xmltok-start
)
2061 (while (and (not pos
) (< (point-min) (point)))
2062 (cond ((search-backward "<" nxml-prolog-end t
)
2063 (nxml-move-outside-backwards)
2065 (while (< (point) last-tag-pos
)
2067 (when (and (not had-data
) (nxml-token-contains-data-p))
2069 (setq last-data-pos xmltok-start
))
2070 (let ((tem (nxml-paragraph-start-pos had-data
0)))
2071 (when tem
(setq pos tem
)))))
2072 (when (and (not had-data
) last-data-pos
(not pos
))
2075 (while (< (point) last-data-pos
)
2077 (let ((tem (nxml-paragraph-start-pos had-data
0)))
2078 (when tem
(setq pos tem
)))))
2079 (setq last-tag-pos
(point)))
2080 (t (goto-char (point-min))))))
2081 (when pos
(goto-char pos
))))
2083 (defun nxml-token-contains-data-p (&optional start end
)
2084 (setq start
(+ xmltok-start
(or start
0)))
2085 (setq end
(- (point) (or end
0)))
2086 (when (eq xmltok-type
'cdata-section
)
2087 (setq start
(max start
(+ xmltok-start
9)))
2088 (setq end
(min end
(- (point) 3))))
2089 (or (and (eq xmltok-type
'data
)
2090 (eq start xmltok-start
)
2092 (eq xmltok-type
'char-ref
)
2093 (and (memq xmltok-type
'(data cdata-section
))
2097 (re-search-forward "[^ \t\r\n]" end t
)))))
2099 (defun nxml-paragraph-end-pos (had-data offset
)
2100 "Return the position of the paragraph end if contained in the current token.
2101 Return nil if the current token does not contain the paragraph end.
2102 Only characters after OFFSET from the start of the token are eligible.
2103 HAD-DATA says whether there have been non-whitespace data characters yet."
2104 (cond ((not had-data
)
2105 (cond ((memq xmltok-type
'(data cdata-section
))
2107 (let ((end (point)))
2108 (goto-char (+ xmltok-start
2109 (max (if (eq xmltok-type
'cdata-section
)
2113 (and (re-search-forward "[^ \t\r\n]" end t
)
2114 (re-search-forward "^[ \t]*$" end t
)
2115 (match-beginning 0)))))
2116 ((and (eq xmltok-type
'comment
)
2117 (nxml-token-begins-line-p)
2118 (nxml-token-ends-line-p))
2120 (let ((end (point)))
2121 (goto-char (+ xmltok-start
(max 4 offset
)))
2122 (when (re-search-forward "[^ \t\r\n]" (- end
3) t
)
2123 (if (re-search-forward "^[ \t]*$" end t
)
2125 (goto-char (- end
3))
2126 (skip-chars-backward " \t")
2128 (beginning-of-line 2))
2130 ((memq xmltok-type
'(data space cdata-section
))
2132 (let ((end (point)))
2133 (goto-char (+ xmltok-start offset
))
2134 (and (re-search-forward "^[ \t]*$" end t
)
2135 (match-beginning 0)))))
2136 ((and (memq xmltok-type
'(start-tag
2140 processing-instruction
2142 (nxml-token-begins-line-p)
2143 (nxml-token-ends-line-p))
2145 (goto-char xmltok-start
)
2146 (skip-chars-backward " \t")
2148 ((and (eq xmltok-type
'end-tag
)
2149 (looking-at "[ \t]*$")
2150 (not (nxml-in-mixed-content-p t
)))
2152 (or (search-forward "\n" nil t
)
2155 (defun nxml-paragraph-start-pos (had-data offset
)
2156 "Return the position of the paragraph start if contained in the current token.
2157 Return nil if the current token does not contain the paragraph start.
2158 Only characters before OFFSET from the end of the token are eligible.
2159 HAD-DATA says whether there have been non-whitespace data characters yet."
2160 (cond ((not had-data
)
2161 (cond ((memq xmltok-type
'(data cdata-section
))
2163 (goto-char (- (point)
2164 (max (if (eq xmltok-type
'cdata-section
)
2168 (and (re-search-backward "[^ \t\r\n]" xmltok-start t
)
2169 (re-search-backward "^[ \t]*$" xmltok-start t
)
2170 (match-beginning 0))))
2171 ((and (eq xmltok-type
'comment
)
2172 (nxml-token-ends-line-p)
2173 (nxml-token-begins-line-p))
2175 (goto-char (- (point) (max 3 offset
)))
2176 (when (and (< (+ xmltok-start
4) (point))
2177 (re-search-backward "[^ \t\r\n]"
2180 (if (re-search-backward "^[ \t]*$" xmltok-start t
)
2182 (goto-char xmltok-start
)
2183 (if (looking-at "<!--[ \t]*\n")
2185 (skip-chars-backward " \t")
2187 ((memq xmltok-type
'(data space cdata-section
))
2189 (goto-char (- (point) offset
))
2190 (and (re-search-backward "^[ \t]*$" xmltok-start t
)
2191 (match-beginning 0))))
2192 ((and (memq xmltok-type
'(start-tag
2196 processing-instruction
2198 (nxml-token-ends-line-p)
2199 (nxml-token-begins-line-p))
2200 (or (search-forward "\n" nil t
)
2202 ((and (eq xmltok-type
'start-tag
)
2203 (nxml-token-begins-line-p)
2204 (not (save-excursion
2205 (goto-char xmltok-start
)
2206 (nxml-in-mixed-content-p nil
))))
2208 (goto-char xmltok-start
)
2209 (skip-chars-backward " \t")
2210 ;; include any blank line before
2211 (or (and (eq (char-before) ?
\n)
2213 (goto-char (1- (point)))
2214 (skip-chars-backward " \t")
2215 (and (bolp) (point))))
2218 (defun nxml-token-ends-line-p () (looking-at "[ \t]*$"))
2220 (defun nxml-token-begins-line-p ()
2222 (goto-char xmltok-start
)
2223 (skip-chars-backward " \t")
2226 (defun nxml-in-mixed-content-p (endp)
2227 "Return non-nil if point is in mixed content.
2228 Point must be after an end-tag or before a start-tag.
2229 ENDP is t in the former case, nil in the latter."
2230 (let (matching-tag-pos)
2231 (cond ((not (run-hook-with-args-until-failure
2232 'nxml-in-mixed-content-hook
))
2234 ;; See if the matching tag does not start or end a line.
2235 ((condition-case err
2237 (setq matching-tag-pos
2240 (and (nxml-scan-element-backward (point))
2242 (nxml-scan-element-forward (point)))))
2243 (and matching-tag-pos
2245 (goto-char matching-tag-pos
)
2248 (skip-chars-backward " \t")
2250 (looking-at "[ \t]*$"))))))
2251 (nxml-scan-error nil
))
2253 ;; See if there's data at the same level.
2256 (setq start matching-tag-pos
2259 end matching-tag-pos
))
2263 (nxml-preceding-sibling-data-p))
2266 (nxml-following-sibling-data-p)))))
2268 ;; Otherwise, treat as not mixed
2271 (defun nxml-preceding-sibling-data-p ()
2272 "Return non-nil if there is a previous sibling that is data."
2273 (let ((lim (max (- (point) nxml-mixed-scan-distance
)
2279 (while (and (< lim
(point))
2284 (search-backward "<" lim t
)))
2285 (nxml-move-outside-backwards)
2288 (let ((prev-level level
))
2289 (cond ((eq xmltok-type
'end-tag
)
2290 (setq level
(1+ level
)))
2291 ((eq xmltok-type
'start-tag
)
2292 (setq level
(1- level
))))
2293 (when (eq prev-level
0)
2294 (while (and (< (point) end
) (not found
))
2296 (when (memq xmltok-type
'(data cdata-section char-ref
))
2297 (setq found t
)))))))))
2300 (defun nxml-following-sibling-data-p ()
2301 (let ((lim (min (+ (point) nxml-mixed-scan-distance
)
2307 (while (and (< (point) lim
)
2309 (nxml-tokenize-forward)
2311 (cond ((eq xmltok-type
'start-tag
)
2312 (setq level
(1+ level
)))
2313 ((eq xmltok-type
'end-tag
)
2314 (setq level
(1- level
)))
2316 (memq xmltok-type
'(data cdata-section char-ref
)))
2322 (defun nxml-do-fill-paragraph (arg)
2323 (let (fill-paragraph-function
2327 (nxml-forward-paragraph)
2329 (nxml-backward-paragraph)
2330 (skip-chars-forward " \t\r\n")
2331 (setq start
(point))
2333 (setq fill-prefix
(buffer-substring-no-properties (point) start
))
2334 (when (and (not (nxml-get-inside (point)))
2335 (looking-at "[ \t]*<!--"))
2336 (setq fill-prefix
(concat fill-prefix
" ")))
2337 (fill-region-as-paragraph start end arg
))
2338 (skip-line-prefix fill-prefix
)
2341 (defun nxml-newline-and-indent (soft)
2342 (delete-horizontal-space)
2343 (if soft
(insert-and-inherit ?
\n) (newline 1))
2349 (defvar nxml-dynamic-markup-prev-pos nil
)
2350 (defvar nxml-dynamic-markup-prev-lengths nil
)
2351 (defvar nxml-dynamic-markup-prev-found-marker nil
)
2352 (defvar nxml-dynamic-markup-prev-start-tags
(make-hash-table :test
'equal
))
2354 (defun nxml-dynamic-markup-word ()
2355 "Dynamically markup the word before point.
2356 This attempts to find a tag to put around the word before point based
2357 on the contents of the current buffer. The end-tag will be inserted at
2358 point. The start-tag will be inserted at or before the beginning of
2359 the word before point; the contents of the current buffer is used to
2362 It works in a similar way to \\[dabbrev-expand]. It searches first
2363 backwards from point, then forwards from point for an element whose
2364 content is a string which matches the contents of the buffer before
2365 point and which includes at least the word before point. It then
2366 copies the start- and end-tags from that element and uses them to
2367 surround the matching string before point.
2369 Repeating \\[nxml-dynamic-markup-word] immediately after successful
2370 \\[nxml-dynamic-markup-word] removes the previously inserted markup
2371 and attempts to find another possible way to do the markup."
2373 (let (search-start-pos done
)
2374 (if (and (integerp nxml-dynamic-markup-prev-pos
)
2375 (= nxml-dynamic-markup-prev-pos
(point))
2376 (eq last-command this-command
)
2377 nxml-dynamic-markup-prev-lengths
)
2378 (let* ((end-tag-open-pos
2379 (- nxml-dynamic-markup-prev-pos
2380 (nth 2 nxml-dynamic-markup-prev-lengths
)))
2381 (start-tag-close-pos
2383 (nth 1 nxml-dynamic-markup-prev-lengths
)))
2385 (- start-tag-close-pos
2386 (nth 0 nxml-dynamic-markup-prev-lengths
))))
2387 (delete-region end-tag-open-pos nxml-dynamic-markup-prev-pos
)
2388 (delete-region start-tag-open-pos start-tag-close-pos
)
2389 (setq search-start-pos
2390 (marker-position nxml-dynamic-markup-prev-found-marker
)))
2391 (clrhash nxml-dynamic-markup-prev-start-tags
))
2392 (setq nxml-dynamic-markup-prev-pos nil
)
2393 (setq nxml-dynamic-markup-prev-lengths nil
)
2394 (setq nxml-dynamic-markup-prev-found-marker nil
)
2397 (let* ((pos (point))
2400 (unless (< (point) pos
)
2401 (error "No word to markup"))
2402 (buffer-substring-no-properties (point) pos
)))
2403 (search (concat word
"</"))
2405 (when search-start-pos
2406 (goto-char search-start-pos
))
2407 (while (and (not done
)
2408 (or (and (< (point) pos
)
2409 (or (search-backward search nil t
)
2410 (progn (goto-char pos
) nil
)))
2411 (search-forward search nil t
)))
2412 (goto-char (- (match-end 0) 2))
2413 (setq done
(nxml-try-copy-markup pos
)))
2415 (error (if (zerop (hash-table-count
2416 nxml-dynamic-markup-prev-start-tags
))
2417 "No possible markup found for `%s'"
2418 "No more markup possibilities found for `%s'")
2421 (defun nxml-try-copy-markup (word-end-pos)
2423 (let ((end-tag-pos (point)))
2424 (when (and (not (nxml-get-inside end-tag-pos
))
2425 (search-backward "<" nil t
)
2426 (not (nxml-get-inside (point))))
2428 (when (and (eq xmltok-type
'start-tag
)
2429 (< (point) end-tag-pos
))
2430 (let* ((start-tag-close-pos (point))
2432 (buffer-substring-no-properties xmltok-start
2433 start-tag-close-pos
))
2437 (buffer-substring-no-properties start-tag-close-pos
2440 (goto-char word-end-pos
)
2442 (re-search-backward (concat
2443 (regexp-quote (car words
))
2447 (setq words
(cdr words
))
2448 (skip-chars-backward " \t\r\n"))
2449 (when (and (not words
)
2451 (skip-chars-forward " \t\r\n")
2452 (not (gethash (cons (point) start-tag
)
2453 nxml-dynamic-markup-prev-start-tags
)))
2454 (or (< end-tag-pos
(point))
2455 (< word-end-pos xmltok-start
)))
2456 (setq nxml-dynamic-markup-prev-found-marker
2457 (copy-marker end-tag-pos t
))
2458 (puthash (cons (point) start-tag
)
2460 nxml-dynamic-markup-prev-start-tags
)
2461 (setq nxml-dynamic-markup-prev-lengths
2462 (list (- start-tag-close-pos xmltok-start
)
2463 (- word-end-pos
(point))
2464 (+ (- xmltok-name-end xmltok-start
) 2)))
2465 (let ((name (xmltok-start-tag-qname)))
2467 (goto-char (+ word-end-pos
2468 (- start-tag-close-pos xmltok-start
)))
2469 (insert "</" name
">")
2470 (setq nxml-dynamic-markup-prev-pos
(point))))))))))
2475 (defvar nxml-char-name-ignore-case t
)
2477 (defvar nxml-char-name-alist nil
2478 "Alist of character names.
2479 Each member of the list has the form (NAME CODE . NAMESET),
2480 where NAME is a string naming a character, NAMESET is a symbol
2481 identifying a set of names and CODE is an integer specifying the
2482 Unicode scalar value of the named character.
2483 The NAME will only be used for completion if NAMESET has
2484 a non-nil `nxml-char-name-set-enabled' property.
2485 If NAMESET does does not have `nxml-char-name-set-defined' property,
2486 then it must have a `nxml-char-name-set-file' property and `load'
2487 will be applied to the value of this property if the nameset
2490 (defvar nxml-char-name-table
(make-hash-table :test
'eq
)
2491 "Hash table for mapping char codes to names.
2492 Each key is a Unicode scalar value.
2493 Each value is a list of pairs of the form (NAMESET . NAME),
2494 where NAMESET is a symbol identifying a set of names,
2495 and NAME is a string naming a character.")
2497 (defvar nxml-autoload-char-name-set-list nil
2498 "List of char namesets that can be autoloaded.")
2500 (defun nxml-enable-char-name-set (nameset)
2501 (put nameset
'nxml-char-name-set-enabled t
))
2503 (defun nxml-disable-char-name-set (nameset)
2504 (put nameset
'nxml-char-name-set-enabled nil
))
2506 (defun nxml-char-name-set-enabled-p (nameset)
2507 (get nameset
'nxml-char-name-set-enabled
))
2509 (defun nxml-autoload-char-name-set (nameset file
)
2510 (unless (memq nameset nxml-autoload-char-name-set-list
)
2511 (setq nxml-autoload-char-name-set-list
2512 (cons nameset nxml-autoload-char-name-set-list
)))
2513 (put nameset
'nxml-char-name-set-file file
))
2515 (defun nxml-define-char-name-set (nameset alist
)
2516 "Define a set of character names.
2517 NAMESET is a symbol identifying the set.
2518 ALIST is a list where each member has the form (NAME CODE),
2519 where NAME is a string naming a character and code is an
2520 integer giving the Unicode scalar value of the character."
2521 (when (get nameset
'nxml-char-name-set-defined
)
2522 (error "Nameset `%s' already defined" nameset
))
2525 (let* ((name-code (car iter
))
2526 (name (car name-code
))
2527 (code (cadr name-code
)))
2529 (cons (cons nameset name
)
2530 (gethash code nxml-char-name-table
))
2531 nxml-char-name-table
))
2532 (setcdr (cdr (car iter
)) nameset
)
2533 (setq iter
(cdr iter
))))
2534 (setq nxml-char-name-alist
2535 (nconc alist nxml-char-name-alist
))
2536 (put nameset
'nxml-char-name-set-defined t
))
2538 (defun nxml-get-char-name (code)
2539 (mapc 'nxml-maybe-load-char-name-set nxml-autoload-char-name-set-list
)
2540 (let ((names (gethash code nxml-char-name-table
))
2542 (while (and names
(not name
))
2543 (if (nxml-char-name-set-enabled-p (caar names
))
2544 (setq name
(cdar names
))
2545 (setq names
(cdr names
))))
2548 (defvar nxml-named-char-history nil
)
2550 (defun nxml-insert-named-char (arg)
2551 "Insert a character using its name.
2552 The name is read from the minibuffer.
2553 Normally, inserts the character as a numeric character reference.
2554 With a prefix argument, inserts the character directly."
2556 (mapc 'nxml-maybe-load-char-name-set nxml-autoload-char-name-set-list
)
2558 (let ((completion-ignore-case nxml-char-name-ignore-case
))
2559 (completing-read "Character name: "
2560 nxml-char-name-alist
2562 (get (cddr member
) 'nxml-char-name-set-enabled
))
2565 'nxml-named-char-history
)))
2566 (alist nxml-char-name-alist
)
2568 (while (and alist
(not code
))
2569 (setq elt
(assoc name alist
))
2570 (if (get (cddr elt
) 'nxml-char-name-set-enabled
)
2571 (setq code
(cadr elt
))
2572 (setq alist
(cdr (member elt alist
)))))
2575 (or (decode-char 'ucs code
)
2576 (error "Character %x is not supported by Emacs"
2578 (format "&#x%X;" code
))))))
2580 (defun nxml-maybe-load-char-name-set (sym)
2581 (when (and (get sym
'nxml-char-name-set-enabled
)
2582 (not (get sym
'nxml-char-name-set-defined
))
2583 (stringp (get sym
'nxml-char-name-set-file
)))
2584 (load (get sym
'nxml-char-name-set-file
))))
2586 (defun nxml-toggle-char-ref-extra-display (arg)
2587 "Toggle the display of extra information for character references."
2589 (let ((new (if (null arg
)
2590 (not nxml-char-ref-extra-display
)
2591 (> (prefix-numeric-value arg
) 0))))
2592 (when (not (eq new nxml-char-ref-extra-display
))
2593 (setq nxml-char-ref-extra-display new
)
2594 (font-lock-fontify-buffer))))
2596 (put 'nxml-char-ref
'evaporate t
)
2598 (defun nxml-char-ref-display-extra (start end n
)
2599 (when nxml-char-ref-extra-display
2600 (let ((name (nxml-get-char-name n
))
2601 (glyph-string (and nxml-char-ref-display-glyph-flag
2602 (nxml-glyph-display-string n
'nxml-glyph
)))
2604 (when (or name glyph-string
)
2605 (setq ov
(make-overlay start end nil t
))
2606 (overlay-put ov
'category
'nxml-char-ref
)
2608 (overlay-put ov
'help-echo name
))
2612 (propertize glyph-string
'face
'nxml-glyph
)))))))
2614 (defun nxml-clear-char-ref-extra-display (start end
)
2615 (let ((ov (overlays-in start end
)))
2617 (when (eq (overlay-get (car ov
) 'category
) 'nxml-char-ref
)
2618 (delete-overlay (car ov
)))
2619 (setq ov
(cdr ov
)))))
2622 (defun nxml-start-delimiter-length (type)
2623 (or (get type
'nxml-start-delimiter-length
)
2626 (put 'cdata-section
'nxml-start-delimiter-length
9)
2627 (put 'comment
'nxml-start-delimiter-length
4)
2628 (put 'processing-instruction
'nxml-start-delimiter-length
2)
2629 (put 'start-tag
'nxml-start-delimiter-length
1)
2630 (put 'empty-element
'nxml-start-delimiter-length
1)
2631 (put 'partial-empty-element
'nxml-start-delimiter-length
1)
2632 (put 'entity-ref
'nxml-start-delimiter-length
1)
2633 (put 'char-ref
'nxml-start-delimiter-length
2)
2635 (defun nxml-end-delimiter-length (type)
2636 (or (get type
'nxml-end-delimiter-length
)
2639 (put 'cdata-section
'nxml-end-delimiter-length
3)
2640 (put 'comment
'nxml-end-delimiter-length
3)
2641 (put 'processing-instruction
'nxml-end-delimiter-length
2)
2642 (put 'start-tag
'nxml-end-delimiter-length
1)
2643 (put 'empty-element
'nxml-end-delimiter-length
2)
2644 (put 'partial-empty-element
'nxml-end-delimiter-length
1)
2645 (put 'entity-ref
'nxml-end-delimiter-length
1)
2646 (put 'char-ref
'nxml-end-delimiter-length
1)
2648 (defun nxml-token-type-friendly-name (type)
2649 (or (get type
'nxml-friendly-name
)
2650 (symbol-name type
)))
2652 (put 'cdata-section
'nxml-friendly-name
"CDATA section")
2653 (put 'processing-instruction
'nxml-friendly-name
"processing instruction")
2654 (put 'entity-ref
'nxml-friendly-name
"entity reference")
2655 (put 'char-ref
'nxml-friendly-name
"character reference")
2658 (defalias 'xml-mode
'nxml-mode
)
2660 (provide 'nxml-mode
)
2662 ;; arch-tag: 8603bc5f-1ef9-4021-b223-322fb2ca708e
2663 ;;; nxml-mode.el ends here