1 ;;; nxml-mode.el --- a new XML mode
3 ;; Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; See nxml-rap.el for description of parsing strategy.
29 (when (featurep 'mucs
)
30 (error "nxml-mode is not compatible with Mule-UCS"))
32 (eval-when-compile (require 'cl
)) ; for assert
41 (declare-function rng-nxml-mode-init
"rng-nxml")
42 (declare-function nxml-enable-unicode-char-name-sets
"nxml-uchnm")
47 "New XML editing mode."
51 (defgroup nxml-faces nil
52 "Faces for XML syntax highlighting."
54 :group
'font-lock-faces
)
56 (defcustom nxml-char-ref-display-glyph-flag t
57 "*Non-nil means display glyph following character reference.
58 The glyph is displayed in face `nxml-glyph'. The hook
59 `nxml-glyph-set-hook' can be used to customize for which characters
60 glyphs are displayed."
64 (defcustom nxml-mode-hook nil
65 "Hook run by command `nxml-mode'."
69 (defcustom nxml-sexp-element-flag nil
70 "*Non-nil means sexp commands treat an element as a single expression."
74 (defcustom nxml-slash-auto-complete-flag nil
75 "*Non-nil means typing a slash automatically completes the end-tag.
76 This is used by `nxml-electric-slash'."
80 (defcustom nxml-child-indent
2
81 "*Indentation for the children of an element relative to the start-tag.
82 This only applies when the line or lines containing the start-tag contains
83 nothing else other than that start-tag."
87 (defcustom nxml-attribute-indent
4
88 "*Indentation for the attributes of an element relative to the start-tag.
89 This only applies when the first attribute of a tag starts a line.
90 In other cases, the first attribute on one line is indented the same
91 as the first attribute on the previous line."
95 (defcustom nxml-bind-meta-tab-to-complete-flag
(not window-system
)
96 "*Non-nil means bind M-TAB in `nxml-mode-map' to `nxml-complete'.
97 C-return will be bound to `nxml-complete' in any case.
98 M-TAB gets swallowed by many window systems/managers, and
99 `documentation' will show M-TAB rather than C-return as the
100 binding for `nxml-complete' when both are bound. So it's better
101 to bind M-TAB only when it will work."
103 :set
(lambda (sym flag
)
104 (set-default sym flag
)
105 (when (and (boundp 'nxml-mode-map
) nxml-mode-map
)
106 (define-key nxml-mode-map
"\M-\t" (and flag
'nxml-complete
))))
109 (defcustom nxml-prefer-utf-16-to-utf-8-flag nil
110 "*Non-nil means prefer UTF-16 to UTF-8 when saving a buffer.
111 This is used only when a buffer does not contain an encoding declaration
112 and when its current `buffer-file-coding-system' specifies neither UTF-16
117 (defcustom nxml-prefer-utf-16-little-to-big-endian-flag
(eq system-type
119 "*Non-nil means prefer little-endian to big-endian byte-order for UTF-16.
120 This is used only for saving a buffer; when reading the byte-order is
121 auto-detected. It may be relevant both when there is no encoding declaration
122 and when the encoding declaration specifies `UTF-16'."
126 (defcustom nxml-default-buffer-file-coding-system nil
127 "*Default value for `buffer-file-coding-system' for a buffer for a new file.
128 A value of nil means use the default value of `buffer-file-coding-system' as normal.
129 A buffer's `buffer-file-coding-system' affects what \\[nxml-insert-xml-declaration] inserts."
131 :type
'coding-system
)
133 (defcustom nxml-auto-insert-xml-declaration-flag nil
134 "*Non-nil means automatically insert an XML declaration in a new file.
135 The XML declaration is inserted using `nxml-insert-xml-declaration'."
139 (defface nxml-delimited-data
140 '((t (:inherit font-lock-doc-face
)))
141 "Face used to highlight data enclosed between delimiters.
142 This is not used directly, but only via inheritance by other faces."
146 '((t (:inherit font-lock-builtin-face
)))
147 "Face used to highlight various names.
148 This includes element and attribute names, processing
149 instruction targets and the CDATA keyword in a CDATA section.
150 This is not used directly, but only via inheritance by other faces."
154 '((t (:inherit font-lock-constant-face
)))
155 "Face used to highlight character and entity references.
156 This is not used directly, but only via inheritance by other faces."
159 (defface nxml-delimiter
161 "Face used to highlight delimiters.
162 This is not used directly, but only via inheritance by other faces."
167 "Face used to highlight text."
170 (defface nxml-comment-content
171 '((t (:inherit font-lock-comment-face
)))
172 "Face used to highlight the content of comments."
175 (defface nxml-comment-delimiter
176 '((t (:inherit font-lock-comment-delimiter-face
)))
177 "Face used for the delimiters of comments, i.e <!-- and -->."
180 (defface nxml-processing-instruction-delimiter
181 '((t (:inherit nxml-delimiter
)))
182 "Face used for the delimiters of processing instructions, i.e <? and ?>."
185 (defface nxml-processing-instruction-target
186 '((t (:inherit font-lock-keyword-face
)))
187 "Face used for the target of processing instructions."
190 (defface nxml-processing-instruction-content
191 '((t (:inherit nxml-delimited-data
)))
192 "Face used for the content of processing instructions."
195 (defface nxml-cdata-section-delimiter
196 '((t (:inherit nxml-delimiter
)))
197 "Face used for the delimiters of CDATA sections, i.e <![, [, and ]]>."
200 (defface nxml-cdata-section-CDATA
201 '((t (:inherit nxml-name
)))
202 "Face used for the CDATA keyword in CDATA sections."
205 (defface nxml-cdata-section-content
206 '((t (:inherit nxml-text
)))
207 "Face used for the content of CDATA sections."
210 (defface nxml-char-ref-number
211 '((t (:inherit nxml-ref
)))
212 "Face used for the number in character references.
213 This includes ths `x' in hex references."
216 (defface nxml-char-ref-delimiter
217 '((t (:inherit nxml-ref
)))
218 "Face used for the delimiters of character references, i.e &# and ;."
221 (defface nxml-entity-ref-name
222 '((t (:inherit nxml-ref
)))
223 "Face used for the entity name in general entity references."
226 (defface nxml-entity-ref-delimiter
227 '((t (:inherit nxml-ref
)))
228 "Face used for the delimiters of entity references, i.e & and ;."
231 (defface nxml-tag-delimiter
232 '((t (:inherit nxml-delimiter
)))
233 "Face used for the angle brackets delimiting tags.
234 `nxml-tag-slash' is used for slashes."
237 (defface nxml-tag-slash
238 '((t (:inherit nxml-tag-delimiter
)))
239 "Face used for slashes in tags, both in end-tags and empty-elements."
242 (defface nxml-element-prefix
243 '((t (:inherit nxml-name
)))
244 "Face used for the prefix of elements."
247 (defface nxml-element-colon
249 "Face used for the colon in element names."
252 (defface nxml-element-local-name
253 '((t (:inherit font-lock-function-name-face
)))
254 "Face used for the local name of elements."
257 (defface nxml-attribute-prefix
258 '((t (:inherit nxml-name
)))
259 "Face used for the prefix of attributes."
262 (defface nxml-attribute-colon
263 '((t (:inherit nxml-delimiter
)))
264 "Face used for the colon in attribute names."
267 (defface nxml-attribute-local-name
268 '((t (:inherit font-lock-variable-name-face
)))
269 "Face used for the local name of attributes."
272 (defface nxml-namespace-attribute-xmlns
273 '((t (:inherit nxml-attribute-prefix
)))
274 "Face used for `xmlns' in namespace attributes."
277 (defface nxml-namespace-attribute-colon
278 '((t (:inherit nxml-attribute-colon
)))
279 "Face used for the colon in namespace attributes."
282 (defface nxml-namespace-attribute-prefix
283 '((t (:inherit nxml-attribute-local-name
)))
284 "Face used for the prefix declared in namespace attributes."
287 (defface nxml-attribute-value
288 '((t (:inherit font-lock-string-face
)))
289 "Face used for the value of attributes."
292 (defface nxml-attribute-value-delimiter
293 '((t (:inherit nxml-attribute-value
)))
294 "Face used for the delimiters of attribute values."
297 (defface nxml-namespace-attribute-value
298 '((t (:inherit nxml-attribute-value
)))
299 "Face used for the value of namespace attributes."
302 (defface nxml-namespace-attribute-value-delimiter
303 '((t (:inherit nxml-attribute-value-delimiter
)))
304 "Face used for the delimiters of namespace attribute values."
307 (defface nxml-prolog-literal-delimiter
308 '((t (:inherit nxml-delimited-data
)))
309 "Face used for the delimiters of literals in the prolog."
312 (defface nxml-prolog-literal-content
313 '((t (:inherit nxml-delimited-data
)))
314 "Face used for the content of literals in the prolog."
317 (defface nxml-prolog-keyword
318 '((t (:inherit font-lock-keyword-face
)))
319 "Face used for keywords in the prolog."
322 (defface nxml-markup-declaration-delimiter
323 '((t (:inherit nxml-delimiter
)))
324 "Face used for the delimiters of markup declarations in the prolog.
325 The delimiters are <! and >."
329 '((t (:inherit nxml-name
)))
330 "Face used for # before a name in the prolog."
354 "Face used for glyph for char references."
359 (defvar nxml-prolog-regions nil
360 "List of regions in the prolog to be fontified.
361 See the function `xmltok-forward-prolog' for more information.")
362 (make-variable-buffer-local 'nxml-prolog-regions
)
364 (defvar nxml-last-fontify-end nil
365 "Position where fontification last ended.
366 It is nil if the buffer changed since the last fontification.")
367 (make-variable-buffer-local 'nxml-last-fontify-end
)
369 (defvar nxml-degraded nil
370 "Non-nil if currently operating in degraded mode.
371 Degraded mode is enabled when an internal error is encountered in the
372 fontification or after-change functions.")
373 (make-variable-buffer-local 'nxml-degraded
)
375 (defvar nxml-completion-hook nil
376 "Hook run by `nxml-complete'.
377 This hook is run until success.")
379 (defvar nxml-in-mixed-content-hook nil
380 "Hook to determine whether point is in mixed content.
381 The hook is called without arguments. It should return nil if it is
382 definitely not mixed; non-nil otherwise. The hook will be run until
383 one of the functions returns nil.")
385 (defvar nxml-mixed-scan-distance
4000
386 "Maximum distance from point to scan when checking for mixed content.")
388 (defvar nxml-end-tag-indent-scan-distance
4000
389 "Maximum distance from point to scan backwards when indenting end-tag.")
391 (defvar nxml-char-ref-extra-display t
392 "Non-nil means display extra information for character references.
393 The extra information consists of a tooltip with the character name
394 and, if `nxml-char-ref-display-glyph-flag' is non-nil, a glyph
395 corresponding to the referenced character following the character
397 (make-variable-buffer-local 'nxml-char-ref-extra-display
)
399 (defvar nxml-mode-map
400 (let ((map (make-sparse-keymap)))
401 (define-key map
"\M-\C-u" 'nxml-backward-up-element
)
402 (define-key map
"\M-\C-d" 'nxml-down-element
)
403 (define-key map
"\M-\C-n" 'nxml-forward-element
)
404 (define-key map
"\M-\C-p" 'nxml-backward-element
)
405 (define-key map
"\M-{" 'nxml-backward-paragraph
)
406 (define-key map
"\M-}" 'nxml-forward-paragraph
)
407 (define-key map
"\M-h" 'nxml-mark-paragraph
)
408 (define-key map
"\C-c\C-f" 'nxml-finish-element
)
409 (define-key map
"\C-c\C-m" 'nxml-split-element
)
410 (define-key map
"\C-c\C-b" 'nxml-balanced-close-start-tag-block
)
411 (define-key map
"\C-c\C-i" 'nxml-balanced-close-start-tag-inline
)
412 (define-key map
"\C-c\C-x" 'nxml-insert-xml-declaration
)
413 (define-key map
"\C-c\C-d" 'nxml-dynamic-markup-word
)
415 (define-key map
"\C-c\C-u" 'nxml-insert-named-char
)
416 (define-key map
"\C-c\C-o" nxml-outline-prefix-map
)
417 (define-key map
[S-mouse-2
] 'nxml-mouse-hide-direct-text-content
)
418 (define-key map
"/" 'nxml-electric-slash
)
419 (define-key map
[C-return
] 'nxml-complete
)
420 (when nxml-bind-meta-tab-to-complete-flag
421 (define-key map
"\M-\t" 'nxml-complete
))
423 "Keymap for nxml-mode.")
425 (defvar nxml-font-lock-keywords
426 '(nxml-fontify-matcher)
427 "Default font lock keywords for nxml-mode.")
429 (defsubst nxml-set-face
(start end face
)
430 (when (and face
(< start end
))
431 (font-lock-append-text-property start end
'face face
)))
435 ;; We use C-c C-i instead of \\[nxml-balanced-close-start-tag-inline]
436 ;; because Emacs turns C-c C-i into C-c TAB which is hard to type and
438 "Major mode for editing XML.
440 \\[nxml-finish-element] finishes the current element by inserting an end-tag.
441 C-c C-i closes a start-tag with `>' and then inserts a balancing end-tag
442 leaving point between the start-tag and end-tag.
443 \\[nxml-balanced-close-start-tag-block] is similar but for block rather than inline elements:
444 the start-tag, point, and end-tag are all left on separate lines.
445 If `nxml-slash-auto-complete-flag' is non-nil, then inserting a `</'
446 automatically inserts the rest of the end-tag.
448 \\[nxml-complete] performs completion on the symbol preceding point.
450 \\[nxml-dynamic-markup-word] uses the contents of the current buffer
451 to choose a tag to put around the word preceding point.
453 Sections of the document can be displayed in outline form. The
454 variable `nxml-section-element-name-regexp' controls when an element
455 is recognized as a section. The same key sequences that change
456 visibility in outline mode are used except that they start with C-c C-o
459 Validation is provided by the related minor-mode `rng-validate-mode'.
460 This also makes completion schema- and context- sensitive. Element
461 names, attribute names, attribute values and namespace URIs can all be
462 completed. By default, `rng-validate-mode' is automatically enabled.
463 You can toggle it using \\[rng-validate-mode] or change the default by
464 customizing `rng-nxml-auto-validate-flag'.
466 \\[indent-for-tab-command] indents the current line appropriately.
467 This can be customized using the variable `nxml-child-indent'
468 and the variable `nxml-attribute-indent'.
470 \\[nxml-insert-named-char] inserts a character reference using
471 the character's name (by default, the Unicode name).
472 \\[universal-argument] \\[nxml-insert-named-char] inserts the character directly.
474 The Emacs commands that normally operate on balanced expressions will
475 operate on XML markup items. Thus \\[forward-sexp] will move forward
476 across one markup item; \\[backward-sexp] will move backward across
477 one markup item; \\[kill-sexp] will kill the following markup item;
478 \\[mark-sexp] will mark the following markup item. By default, each
479 tag each treated as a single markup item; to make the complete element
480 be treated as a single markup item, set the variable
481 `nxml-sexp-element-flag' to t. For more details, see the function
482 `nxml-forward-balanced-item'.
484 \\[nxml-backward-up-element] and \\[nxml-down-element] move up and down the element structure.
486 Many aspects this mode can be customized using
487 \\[customize-group] nxml RET."
489 (kill-all-local-variables)
490 (setq major-mode
'nxml-mode
)
491 (setq mode-name
"nXML")
492 (set (make-local-variable 'mode-line-process
) '((nxml-degraded "/degraded")))
493 ;; We'll determine the fill prefix ourselves
494 (make-local-variable 'adaptive-fill-mode
)
495 (setq adaptive-fill-mode nil
)
496 (make-local-variable 'forward-sexp-function
)
497 (setq forward-sexp-function
'nxml-forward-balanced-item
)
498 (make-local-variable 'indent-line-function
)
499 (setq indent-line-function
'nxml-indent-line
)
500 (make-local-variable 'fill-paragraph-function
)
501 (setq fill-paragraph-function
'nxml-do-fill-paragraph
)
503 ;; This doesn't seem to work too well;
504 ;; I think we should probably roll our own nxml-comment-dwim function.
505 (make-local-variable 'comment-indent-function
)
506 (setq comment-indent-function
'nxml-indent-line
)
507 (make-local-variable 'comment-start
)
508 (setq comment-start
"<!--")
509 (make-local-variable 'comment-start-skip
)
510 (setq comment-start-skip
"<!--[ \t\r\n]*")
511 (make-local-variable 'comment-end
)
512 (setq comment-end
"-->")
513 (make-local-variable 'comment-end-skip
)
514 (setq comment-end-skip
"[ \t\r\n]*-->")
515 (make-local-variable 'comment-line-break-function
)
516 (setq comment-line-break-function
'nxml-newline-and-indent
)
517 (use-local-map nxml-mode-map
)
521 (nxml-clear-dependent-regions (point-min) (point-max))
522 (setq nxml-scan-end
(copy-marker (point-min) nil
))
523 (nxml-with-unmodifying-text-property-changes
524 (nxml-clear-inside (point-min) (point-max))
525 (nxml-with-invisible-motion
526 (nxml-scan-prolog)))))
527 (add-hook 'after-change-functions
'nxml-after-change nil t
)
528 (add-hook 'change-major-mode-hook
'nxml-cleanup nil t
)
530 ;; Emacs 23 handles the encoding attribute on the xml declaration
531 ;; transparently to nxml-mode, so there is no longer a need for the below
532 ;; hook. The hook also had the drawback of overriding explicit user
533 ;; instruction to save as some encoding other than utf-8.
534 ;;; (add-hook 'write-contents-hooks 'nxml-prepare-to-save)
535 (when (not (and (buffer-file-name) (file-exists-p (buffer-file-name))))
536 (when (and nxml-default-buffer-file-coding-system
537 (not (local-variable-p 'buffer-file-coding-system
)))
538 (setq buffer-file-coding-system nxml-default-buffer-file-coding-system
))
539 (when nxml-auto-insert-xml-declaration-flag
540 (nxml-insert-xml-declaration)))
542 (setq font-lock-defaults
543 '(nxml-font-lock-keywords
544 t
; keywords-only; we highlight comments and strings here
545 nil
; font-lock-keywords-case-fold-search. XML is case sensitive
546 nil
; no special syntax table
547 nil
; no automatic syntactic fontification
548 (font-lock-extend-after-change-region-function
549 . nxml-extend-after-change-region
)
550 (font-lock-extend-region-functions .
(nxml-extend-region))
551 (jit-lock-contextually . t
)
552 (font-lock-unfontify-region-function . nxml-unfontify-region
)))
555 (nxml-enable-unicode-char-name-sets)
556 (run-mode-hooks 'nxml-mode-hook
))
558 (defun nxml-cleanup ()
559 "Clean up after nxml-mode."
560 ;; Disable associated minor modes.
561 (rng-validate-mode -
1)
562 ;; Clean up fontification.
565 (let ((inhibit-read-only t
)
567 (modified (buffer-modified-p)))
568 (nxml-with-invisible-motion
569 (remove-text-properties (point-min) (point-max) '(face)))
570 (set-buffer-modified-p modified
)))
571 (remove-hook 'change-major-mode-hook
'nxml-cleanup t
))
573 (defun nxml-degrade (context err
)
574 (message "Internal nXML mode error in %s (%s), degrading"
576 (error-message-string err
))
578 (setq nxml-degraded t
)
579 (setq nxml-prolog-end
1)
583 (nxml-with-unmodifying-text-property-changes
584 (nxml-clear-inside (point-min) (point-max))))))
586 ;;; Change management
588 (defun nxml-debug-region (start end
)
590 (let ((font-lock-beg start
)
593 (goto-char font-lock-beg
)
594 (set-mark font-lock-end
)))
596 (defun nxml-after-change (start end pre-change-length
)
597 ; In font-lock mode, nxml-after-change1 is called via
598 ; nxml-extend-after-change-region instead so that the updated
599 ; book-keeping information is available for fontification.
600 (unless (or font-lock-mode nxml-degraded
)
601 (nxml-with-degradation-on-error 'nxml-after-change
606 (nxml-with-invisible-motion
607 (nxml-with-unmodifying-text-property-changes
609 start end pre-change-length
)))))))))
611 (defun nxml-after-change1 (start end pre-change-length
)
612 "After-change bookkeeping.
613 Returns a cons cell containing a possibly-enlarged change region.
614 You must call `nxml-extend-region' on this expanded region to obtain
615 the full extent of the area needing refontification.
617 For bookkeeping, call this function even when fontification is
619 (let ((pre-change-end (+ start pre-change-length
)))
621 (nxml-adjust-start-for-dependent-regions start
624 ;; If the prolog might have changed, rescan the prolog
626 ;; Add 2 so as to include the < and following char that
627 ;; start the instance (document element), since changing
628 ;; these can change where the prolog ends.
629 (+ nxml-prolog-end
2))
630 ;; end must be extended to at least the end of the old prolog in
631 ;; case the new prolog is shorter
632 (when (< pre-change-end nxml-prolog-end
)
634 ;; don't let end get out of range even if pre-change-length
637 (+ end
(- nxml-prolog-end pre-change-end
)))))
639 (setq start
(point-min))))
641 (when (> end nxml-prolog-end
)
643 (nxml-move-tag-backwards (point-min))
645 (setq end
(max (nxml-scan-after-change start end
)
648 (nxml-debug-change "nxml-after-change1" start end
)
653 (defun nxml-insert-xml-declaration ()
654 "Insert an XML declaration at the beginning of buffer.
655 The XML declaration will declare an encoding depending on the buffer's
656 `buffer-file-coding-system'."
659 (if (and buffer-file-coding-system
660 (coding-system-p buffer-file-coding-system
)
661 (coding-system-get buffer-file-coding-system
663 buffer-file-coding-system
664 (nxml-choose-utf-coding-system))))
665 (goto-char (point-min))
666 (insert (format "<?xml version=\"1.0\" encoding=\"%s\"?>\n"
667 (nxml-coding-system-name coding-system
)))))
669 (defun nxml-prepare-to-save ()
670 (unless (and (not enable-multibyte-characters
)
671 (local-variable-p 'buffer-file-coding-system
)
672 buffer-file-coding-system
673 (or (eq (coding-system-type buffer-file-coding-system
) 5)
674 (eq buffer-file-coding-system
'no-conversion
)))
676 (setq buffer-file-coding-system
(nxml-select-coding-system))))
677 ;; nil from a function in `write-contents-hooks' means
678 ;; to continue and write the file as normal
681 (defun nxml-select-coding-system ()
682 (let* ((suitable-coding-systems
683 (find-coding-systems-region (point-min) (point-max)))
685 (goto-char (point-min))
686 (xmltok-get-declared-encoding-position)))
689 (buffer-substring-no-properties (car enc-pos
)
693 (if (string= (downcase enc-name
) "utf-16")
694 (nxml-choose-utf-16-coding-system)
695 (nxml-mime-charset-coding-system enc-name
)))
696 (enc-pos (nxml-choose-utf-coding-system)))))
697 ;; Make sure we have a coding-system
698 (unless coding-system
700 (and (not buffer-read-only
)
701 (nxml-choose-suitable-coding-system
702 suitable-coding-systems
)))
705 (format "Unknown encoding %s" enc-name
)
706 "XML declaration is not well-formed")))
707 (cond ((not coding-system
)
708 (error "%s" message
))
714 "Modify and save with encoding %s")
715 (nxml-coding-system-name coding-system
))
717 (nxml-fix-encoding-declaration enc-pos coding-system
))
718 (t (signal 'quit nil
)))))
719 ;; Make sure it can encode all the characters in the buffer
720 (unless (or (memq (coding-system-base coding-system
)
721 suitable-coding-systems
)
722 (equal suitable-coding-systems
'(undecided)))
724 (nxml-unsuitable-coding-system-message coding-system
727 (and (not buffer-read-only
)
728 (nxml-choose-suitable-coding-system
729 suitable-coding-systems
)))
730 (cond ((not coding-system
) (error "%s" message
))
731 ((y-or-n-p (concat message
732 (format ". Save with %s "
733 (nxml-coding-system-name
735 (nxml-fix-encoding-declaration enc-pos coding-system
))
736 (t (signal 'quit nil
)))))
737 ;; Merge the newline type of our existing encoding
738 (let ((current-eol-type
739 (coding-system-eol-type buffer-file-coding-system
)))
740 (when (and current-eol-type
(integerp current-eol-type
))
742 (coding-system-change-eol-conversion coding-system
746 (defun nxml-unsuitable-coding-system-message (coding-system &optional enc-name
)
747 (if (nxml-coding-system-unicode-p coding-system
)
748 "Cannot translate some characters to Unicode"
749 (format "Cannot encode some characters with %s"
751 (nxml-coding-system-name coding-system
)))))
753 (defconst nxml-utf-16-coding-systems
(and (coding-system-p 'utf-16-be
)
754 (coding-system-p 'utf-16-le
)
755 '(utf-16-be utf-16-le
)))
757 (defconst nxml-utf-coding-systems
(cons 'utf-8 nxml-utf-16-coding-systems
))
759 (defun nxml-coding-system-unicode-p (coding-system)
760 (nxml-coding-system-member (coding-system-base coding-system
)
761 nxml-utf-coding-systems
))
763 (defun nxml-coding-system-name (coding-system)
764 (setq coding-system
(coding-system-base coding-system
))
766 (if (nxml-coding-system-member coding-system nxml-utf-16-coding-systems
)
768 (or (coding-system-get coding-system
'mime-charset
)
771 (defun nxml-fix-encoding-declaration (enc-pos coding-system
)
772 (let ((charset (nxml-coding-system-name coding-system
)))
773 (cond ((consp enc-pos
)
774 (delete-region (car enc-pos
) (cdr enc-pos
))
775 (goto-char (car enc-pos
))
779 (insert " encoding=\"" charset ?
\"))
781 (goto-char (point-min))
782 (insert "<?xml version=\"1.0\" encoding=\""
785 (when (and (not enc-pos
)
786 (let ((case-fold-search t
))
787 (looking-at xmltok-bad-xml-decl-regexp
)))
788 (delete-region (point) (match-end 0)))))))
790 (defun nxml-choose-suitable-coding-system (suitable-coding-systems)
791 (let (ret coding-system
)
792 (if (and buffer-file-coding-system
793 (memq (coding-system-base buffer-file-coding-system
)
794 suitable-coding-systems
))
795 buffer-file-coding-system
796 (while (and suitable-coding-systems
(not ret
))
797 (setq coding-system
(car suitable-coding-systems
))
798 (if (coding-system-get coding-system
'mime-charset
)
799 (setq ret coding-system
)
800 (setq suitable-coding-systems
(cdr suitable-coding-systems
))))
803 (defun nxml-choose-utf-coding-system ()
804 (let ((cur (and (local-variable-p 'buffer-file-coding-system
)
805 buffer-file-coding-system
806 (coding-system-base buffer-file-coding-system
))))
807 (cond ((car (nxml-coding-system-member cur nxml-utf-coding-systems
)))
808 ((and nxml-prefer-utf-16-to-utf-8-flag
809 (coding-system-p 'utf-16-le
)
810 (coding-system-p 'utf-16-be
))
811 (if nxml-prefer-utf-16-little-to-big-endian-flag
816 (defun nxml-choose-utf-16-coding-system ()
817 (let ((cur (and (local-variable-p 'buffer-file-coding-system
)
818 buffer-file-coding-system
819 (coding-system-base buffer-file-coding-system
))))
820 (cond ((car (nxml-coding-system-member cur nxml-utf-16-coding-systems
)))
821 (nxml-prefer-utf-16-little-to-big-endian-flag
822 (and (coding-system-p 'utf-16-le
) 'utf-16-le
))
823 (t (and (coding-system-p 'utf-16-be
) 'utf-16-be
)))))
825 (defun nxml-coding-system-member (coding-system coding-systems
)
827 (while (and coding-systems
(not ret
))
828 (if (coding-system-equal coding-system
829 (car coding-systems
))
830 (setq ret coding-systems
)
831 (setq coding-systems
(cdr coding-systems
))))
836 (defun nxml-unfontify-region (start end
)
837 (font-lock-default-unfontify-region start end
)
838 (nxml-clear-char-ref-extra-display start end
))
840 (defvar font-lock-beg
) (defvar font-lock-end
)
841 (defun nxml-extend-region ()
842 "Extend the region to hold the minimum area we can fontify with nXML.
843 Called with `font-lock-beg' and `font-lock-end' dynamically bound."
844 (let ((start font-lock-beg
)
847 (nxml-debug-change "nxml-extend-region(input)" start end
)
849 (when (< start nxml-prolog-end
)
850 (setq start
(point-min)))
852 (cond ((<= end nxml-prolog-end
)
853 (setq end nxml-prolog-end
))
857 ;; some font-lock backends (like Emacs 22 jit-lock) snap
858 ;; the region to the beginning of the line no matter what
859 ;; we say here. To mitigate the resulting excess
860 ;; fontification, ignore leading whitespace.
861 (skip-syntax-forward " ")
863 ;; find the beginning of the previous tag
864 (when (not (equal (char-after) ?\
<))
865 (search-backward "<" nxml-prolog-end t
))
866 (nxml-ensure-scan-up-to-date)
867 (nxml-move-outside-backwards)
870 (while (< (point) end
)
871 (nxml-tokenize-forward))
875 (when (or (< start font-lock-beg
)
876 (> end font-lock-end
))
877 (setq font-lock-beg start
879 (nxml-debug-change "nxml-extend-region" start end
)
882 (defun nxml-extend-after-change-region (start end pre-change-length
)
883 (unless nxml-degraded
884 (setq nxml-last-fontify-end nil
)
885 (let ((region (nxml-with-degradation-on-error
886 'nxml-extend-after-change-region
891 (nxml-with-invisible-motion
892 (nxml-with-unmodifying-text-property-changes
893 (nxml-extend-after-change-region1
894 start end pre-change-length
)))))))))
895 (if (consp region
) region
))))
897 (defun nxml-extend-after-change-region1 (start end pre-change-length
)
898 (let* ((region (nxml-after-change1 start end pre-change-length
))
899 (font-lock-beg (car region
))
900 (font-lock-end (cdr region
)))
903 (cons font-lock-beg font-lock-end
)))
905 (defun nxml-fontify-matcher (bound)
906 "Called as font-lock keyword matcher."
908 (unless nxml-degraded
909 (nxml-debug-change "nxml-fontify-matcher" (point) bound
)
911 (when (< (point) nxml-prolog-end
)
912 ;; prolog needs to be fontified in one go, and
913 ;; nxml-extend-region makes sure we start at BOB.
915 (nxml-fontify-prolog)
916 (goto-char nxml-prolog-end
))
918 (let (xmltok-dependent-regions
920 (while (and (nxml-tokenize-forward)
921 (<= (point) bound
)) ; intervals are open-ended
922 (nxml-apply-fontify-rule)))
924 (setq nxml-last-fontify-end
(point)))
926 ;; Since we did the fontification internally, tell font-lock to not
927 ;; do anything itself.
930 (defun nxml-fontify-prolog ()
932 The buffer is assumed to be prepared for fontification.
933 This does not set the fontified property, but it does clear
934 faces appropriately."
935 (let ((regions nxml-prolog-regions
))
937 (let ((region (car regions
)))
938 (nxml-apply-fontify-rule (aref region
0)
941 (setq regions
(cdr regions
)))))
943 ;; Vectors identify a substring of the token to be highlighted in some face.
945 ;; Token types returned by xmltok-forward.
949 '([nil
1 nxml-tag-delimiter
]
950 [-
1 nil nxml-tag-delimiter
]
954 (put 'partial-start-tag
956 '([nil
1 nxml-tag-delimiter
]
962 '([nil
1 nxml-tag-delimiter
]
964 [-
1 nil nxml-tag-delimiter
]
965 (element-qname .
2)))
967 (put 'partial-end-tag
969 '([nil
1 nxml-tag-delimiter
]
971 (element-qname .
2)))
975 '([nil
1 nxml-tag-delimiter
]
976 [-
2 -
1 nxml-tag-slash
]
977 [-
1 nil nxml-tag-delimiter
]
981 (put 'partial-empty-element
983 '([nil
1 nxml-tag-delimiter
]
984 [-
1 nil nxml-tag-slash
]
990 '([nil
2 nxml-char-ref-delimiter
]
991 [2 -
1 nxml-char-ref-number
]
992 [-
1 nil nxml-char-ref-delimiter
]
997 '([nil
1 nxml-entity-ref-delimiter
]
998 [1 -
1 nxml-entity-ref-name
]
999 [-
1 nil nxml-entity-ref-delimiter
]))
1003 '([nil
4 nxml-comment-delimiter
]
1004 [4 -
3 nxml-comment-content
]
1005 [-
3 nil nxml-comment-delimiter
]))
1007 (put 'processing-instruction
1009 '([nil
2 nxml-processing-instruction-delimiter
]
1010 [-
2 nil nxml-processing-instruction-delimiter
]
1011 processing-instruction-content
))
1015 '([nil
3 nxml-cdata-section-delimiter
] ; <![
1016 [3 8 nxml-cdata-section-CDATA
] ; CDATA
1017 [8 9 nxml-cdata-section-delimiter
] ; [
1018 [9 -
3 nxml-cdata-section-content
] ; ]]>
1019 [-
3 nil nxml-cdata-section-delimiter
]))
1023 '([nil nil nxml-text
]))
1025 ;; Prolog region types in list returned by xmltok-forward-prolog.
1027 (put 'xml-declaration
1029 '([nil
2 nxml-processing-instruction-delimiter
]
1030 [2 5 nxml-processing-instruction-target
]
1031 [-
2 nil nxml-processing-instruction-delimiter
]))
1033 (put 'xml-declaration-attribute-name
1035 '([nil nil nxml-attribute-local-name
]))
1037 (put 'xml-declaration-attribute-value
1039 '([nil
1 nxml-attribute-value-delimiter
]
1040 [1 -
1 nxml-attribute-value
]
1041 [-
1 nil nxml-attribute-value-delimiter
]))
1043 (put 'processing-instruction-left
1045 '([nil
2 nxml-processing-instruction-delimiter
]
1046 [2 nil nxml-processing-instruction-target
]))
1048 (put 'processing-instruction-right
1050 '([nil -
2 nxml-processing-instruction-content
]
1051 [-
2 nil nxml-processing-instruction-delimiter
]))
1055 '([nil
1 nxml-prolog-literal-delimiter
]
1056 [1 -
1 nxml-prolog-literal-content
]
1057 [-
1 nil nxml-prolog-literal-delimiter
]))
1061 '([nil nil nxml-prolog-keyword
]))
1063 (put 'markup-declaration-open
1065 '([0 2 nxml-markup-declaration-delimiter
]
1066 [2 nil nxml-prolog-keyword
]))
1068 (put 'markup-declaration-close
1070 '([nil nil nxml-markup-declaration-delimiter
]))
1072 (put 'internal-subset-open
1074 '([nil nil nxml-markup-declaration-delimiter
]))
1076 (put 'internal-subset-close
1078 '([nil
1 nxml-markup-declaration-delimiter
]
1079 [-
1 nil nxml-markup-declaration-delimiter
]))
1084 [1 nil nxml-prolog-keyword
]))
1086 (defun nxml-apply-fontify-rule (&optional type start end
)
1087 (let ((rule (get (or type xmltok-type
) 'nxml-fontify-rule
)))
1088 (unless start
(setq start xmltok-start
))
1089 (unless end
(setq end
(point)))
1091 (let* ((action (car rule
)))
1092 (setq rule
(cdr rule
))
1093 (cond ((vectorp action
)
1094 (nxml-set-face (let ((offset (aref action
0)))
1095 (cond ((not offset
) start
)
1096 ((< offset
0) (+ end offset
))
1097 (t (+ start offset
))))
1098 (let ((offset (aref action
1)))
1099 (cond ((not offset
) end
)
1100 ((< offset
0) (+ end offset
))
1101 (t (+ start offset
))))
1103 ((and (consp action
)
1104 (eq (car action
) 'element-qname
))
1105 (when xmltok-name-end
; maybe nil in partial-end-tag case
1106 (nxml-fontify-qname (+ start
(cdr action
))
1109 'nxml-element-prefix
1111 'nxml-element-local-name
)))
1112 ((eq action
'attributes
)
1113 (nxml-fontify-attributes))
1114 ((eq action
'processing-instruction-content
)
1115 (nxml-set-face (+ start
2)
1117 'nxml-processing-instruction-target
)
1118 (nxml-set-face (save-excursion
1119 (goto-char xmltok-name-end
)
1120 (skip-chars-forward " \t\r\n")
1123 'nxml-processing-instruction-content
))
1124 ((eq action
'char-ref
)
1125 (nxml-char-ref-display-extra start
1127 (xmltok-char-number start end
)))
1128 (t (error "Invalid nxml-fontify-rule action %s" action
)))))))
1130 (defun nxml-fontify-attributes ()
1131 (while xmltok-namespace-attributes
1132 (nxml-fontify-attribute (car xmltok-namespace-attributes
)
1134 (setq xmltok-namespace-attributes
1135 (cdr xmltok-namespace-attributes
)))
1136 (while xmltok-attributes
1137 (nxml-fontify-attribute (car xmltok-attributes
))
1138 (setq xmltok-attributes
1139 (cdr xmltok-attributes
))))
1141 (defun nxml-fontify-attribute (att &optional namespace-declaration
)
1142 (if namespace-declaration
1143 (nxml-fontify-qname (xmltok-attribute-name-start att
)
1144 (xmltok-attribute-name-colon att
)
1145 (xmltok-attribute-name-end att
)
1146 'nxml-namespace-attribute-xmlns
1147 'nxml-namespace-attribute-colon
1148 'nxml-namespace-attribute-prefix
1149 'nxml-namespace-attribute-xmlns
)
1150 (nxml-fontify-qname (xmltok-attribute-name-start att
)
1151 (xmltok-attribute-name-colon att
)
1152 (xmltok-attribute-name-end att
)
1153 'nxml-attribute-prefix
1154 'nxml-attribute-colon
1155 'nxml-attribute-local-name
))
1156 (let ((start (xmltok-attribute-value-start att
))
1157 (end (xmltok-attribute-value-end att
))
1158 (refs (xmltok-attribute-refs att
))
1159 (delimiter-face (if namespace-declaration
1160 'nxml-namespace-attribute-value-delimiter
1161 'nxml-attribute-value-delimiter
))
1162 (value-face (if namespace-declaration
1163 'nxml-namespace-attribute-value
1164 'nxml-attribute-value
)))
1166 (nxml-set-face (1- start
) start delimiter-face
)
1167 (nxml-set-face end
(1+ end
) delimiter-face
)
1169 (let* ((ref (car refs
))
1170 (ref-type (aref ref
0))
1171 (ref-start (aref ref
1))
1172 (ref-end (aref ref
2)))
1173 (nxml-set-face start ref-start value-face
)
1174 (nxml-apply-fontify-rule ref-type ref-start ref-end
)
1175 (setq start ref-end
))
1176 (setq refs
(cdr refs
)))
1177 (nxml-set-face start end value-face
))))
1179 (defun nxml-fontify-qname (start
1187 (cond (colon (nxml-set-face start colon prefix-face
)
1188 (nxml-set-face colon
(1+ colon
) colon-face
)
1189 (nxml-set-face (1+ colon
) end local-name-face
))
1190 (t (nxml-set-face start end
(or unprefixed-face
1191 local-name-face
)))))
1195 (defun nxml-electric-slash (arg)
1198 With a prefix ARG, do nothing other than insert the slash.
1200 Otherwise, if `nxml-slash-auto-complete-flag' is non-nil, insert the
1201 rest of the end-tag or empty-element if the slash is potentially part
1202 of an end-tag or the close of an empty-element.
1204 If the slash is part of an end-tag that is the first non-whitespace
1205 on the line, reindent the line."
1207 (nxml-ensure-scan-up-to-date)
1208 (let* ((slash-pos (point))
1209 (end-tag-p (and (eq (char-before slash-pos
) ?
<)
1210 (not (nxml-get-inside slash-pos
))))
1211 (at-indentation (save-excursion
1212 (back-to-indentation)
1213 (eq (point) (1- slash-pos
)))))
1214 (self-insert-command (prefix-numeric-value arg
))
1216 (if nxml-slash-auto-complete-flag
1219 (let ((start-tag-end
1220 (nxml-scan-element-backward (1- slash-pos
) t
)))
1222 (insert (xmltok-start-tag-qname) ">")
1223 ;; copy the indentation of the start-tag
1224 (when (and at-indentation
1226 (goto-char xmltok-start
)
1227 (back-to-indentation)
1228 (eq (point) xmltok-start
)))
1230 (indent-line-to (save-excursion
1231 (goto-char xmltok-start
)
1232 (current-column)))))))
1233 (nxml-scan-error nil
))
1234 (when (and (eq (nxml-token-before) (point))
1235 (eq xmltok-type
'partial-empty-element
))
1237 (when (and end-tag-p at-indentation
)
1238 (nxml-indent-line))))))
1240 (defun nxml-balanced-close-start-tag-block ()
1241 "Close the start-tag before point with `>' and insert a balancing end-tag.
1242 Point is left between the start-tag and the end-tag.
1243 If there is nothing but whitespace before the `<' that opens the
1244 start-tag, then put point on a blank line, and put the end-tag on
1245 another line aligned with the start-tag."
1247 (nxml-balanced-close-start-tag 'block
))
1249 (defun nxml-balanced-close-start-tag-inline ()
1250 "Close the start-tag before point with `>' and insert a balancing end-tag.
1251 Point is left between the start-tag and the end-tag.
1252 No extra whitespace is inserted."
1254 (nxml-balanced-close-start-tag 'inline
))
1256 (defun nxml-balanced-close-start-tag (block-or-inline)
1257 (let ((token-end (nxml-token-before))
1259 (token-start xmltok-start
))
1260 (unless (or (eq xmltok-type
'partial-start-tag
)
1261 (and (memq xmltok-type
'(start-tag
1263 partial-empty-element
))
1264 (>= token-end pos
)))
1265 (error "Not in a start-tag"))
1266 ;; Note that this insertion changes xmltok-start.
1268 (buffer-substring-no-properties (+ xmltok-start
1)
1269 (min xmltok-name-end
(point)))
1271 (if (eq block-or-inline
'inline
)
1273 (goto-char token-start
)
1274 (back-to-indentation)
1275 (if (= (point) token-start
)
1276 (let ((indent (current-column)))
1279 (indent-line-to indent
)
1282 (indent-line-to (+ nxml-child-indent indent
)))
1285 (defun nxml-finish-element ()
1286 "Finish the current element by inserting an end-tag."
1288 (nxml-finish-element-1 nil
))
1290 (defvar nxml-last-split-position nil
1291 "Position where `nxml-split-element' split the current element.")
1293 (defun nxml-split-element ()
1294 "Split the current element by inserting an end-tag and a start-tag.
1295 Point is left after the newly inserted start-tag. When repeated,
1296 split immediately before the previously inserted start-tag and leave
1299 (setq nxml-last-split-position
1300 (if (and (eq last-command this-command
)
1301 nxml-last-split-position
)
1303 (goto-char nxml-last-split-position
)
1304 (nxml-finish-element-1 t
))
1305 (nxml-finish-element-1 t
))))
1307 (defun nxml-finish-element-1 (startp)
1308 "Insert an end-tag for the current element and optionally a start-tag.
1309 The start-tag is inserted if STARTP is non-nil. Return the position
1310 of the inserted start-tag or nil if none was inserted."
1312 (let* ((token-end (nxml-token-before))
1315 (when (and (< (point) token-end
)
1318 processing-instruction
1323 (error "Point is inside a %s"
1324 (nxml-token-type-friendly-name xmltok-type
)))
1325 (nxml-scan-element-backward token-end t
)))
1328 (unless (eq xmltok-type
'start-tag
)
1329 (error "No matching start-tag"))
1330 (goto-char xmltok-start
)
1331 (back-to-indentation)
1332 (eq (point) xmltok-start
)))
1335 (goto-char start-tag-end
)
1336 (looking-at "[ \t\r\n]*$")))
1337 (start-tag-indent (save-excursion
1338 (goto-char xmltok-start
)
1340 (qname (xmltok-start-tag-qname))
1341 inserted-start-tag-pos
)
1342 (when (and starts-line ends-line
)
1343 ;; start-tag is on a line by itself
1344 ;; => put the end-tag on a line by itself
1347 (back-to-indentation)
1350 (indent-line-to start-tag-indent
))
1351 (insert "</" qname
">")
1355 (indent-line-to start-tag-indent
))
1356 (setq inserted-start-tag-pos
(point))
1357 (insert "<" qname
">")
1358 (when (and starts-line ends-line
)
1360 (indent-line-to (save-excursion
1361 (goto-char xmltok-start
)
1363 (back-to-indentation)
1364 (if (= (current-column)
1365 (+ start-tag-indent nxml-child-indent
))
1366 (+ start-tag-indent nxml-child-indent
)
1367 start-tag-indent
)))))
1368 inserted-start-tag-pos
))
1372 (defun nxml-indent-line ()
1373 "Indent current line as XML."
1374 (let ((indent (nxml-compute-indent))
1375 (from-end (- (point-max) (point))))
1377 (/= indent
(current-indentation)))
1379 (let ((bol (point)))
1380 (skip-chars-forward " \t")
1381 (delete-region bol
(point)))
1383 (when (> (- (point-max) from-end
) (point))
1384 (goto-char (- (point-max) from-end
))))))
1386 (defun nxml-compute-indent ()
1387 "Return the indent for the line containing point."
1388 (or (nxml-compute-indent-from-matching-start-tag)
1389 (nxml-compute-indent-from-previous-line)))
1391 (defun nxml-compute-indent-from-matching-start-tag ()
1392 "Compute the indent for a line with an end-tag using the matching start-tag.
1393 When the line containing point ends with an end-tag and does not start
1394 in the middle of a token, return the indent of the line containing the
1395 matching start-tag, if there is one and it occurs at the beginning of
1396 its line. Otherwise return nil."
1398 (back-to-indentation)
1399 (let ((bol (point)))
1400 (let ((inhibit-field-text-motion t
))
1402 (skip-chars-backward " \t")
1403 (and (= (nxml-token-before) (point))
1404 (memq xmltok-type
'(end-tag partial-end-tag
))
1405 ;; start of line must not be inside a token
1406 (or (= xmltok-start bol
)
1410 (= xmltok-start bol
))
1411 (eq xmltok-type
'data
))
1413 (nxml-scan-element-backward
1417 nxml-end-tag-indent-scan-distance
))
1418 (nxml-scan-error nil
))
1419 (< xmltok-start bol
)
1421 (goto-char xmltok-start
)
1422 (skip-chars-backward " \t")
1424 (current-indentation)))))
1426 (defun nxml-compute-indent-from-previous-line ()
1427 "Compute the indent for a line using the indentation of a previous line."
1432 before-context after-context
)
1433 (back-to-indentation)
1436 ;; Move backwards until the start of a non-blank line that is
1437 ;; not inside a token.
1439 (when (= (forward-line -
1) -
1)
1441 (back-to-indentation)
1442 (if (looking-at "[ \t]*$")
1445 (setq prev-bol
(point)))
1447 (not (or (= xmltok-start
(point))
1448 (eq xmltok-type
'data
))))))
1450 ;; Now scan over tokens until the end of the line to be indented.
1451 ;; Determine the context before and after the beginning of the
1453 (while (< (point) eol
)
1454 (nxml-tokenize-forward)
1455 (cond ((<= bol xmltok-start
)
1457 (nxml-merge-indent-context-type after-context
)))
1458 ((and (<= (point) bol
)
1459 (not (and (eq xmltok-type
'partial-start-tag
)
1461 (setq before-context
1462 (nxml-merge-indent-context-type before-context
)))
1463 ((eq xmltok-type
'data
)
1464 (setq before-context
1465 (nxml-merge-indent-context-type before-context
))
1467 (nxml-merge-indent-context-type after-context
)))
1468 ;; If in the middle of a token that looks inline,
1469 ;; then indent relative to the previous non-blank line
1470 ((eq (nxml-merge-indent-context-type before-context
)
1472 (goto-char prev-bol
)
1473 (throw 'indent
(current-column)))
1476 (nxml-compute-indent-in-token bol
))))
1477 (skip-chars-forward " \t\r\n"))
1480 (* nxml-child-indent
1481 (+ (if (eq before-context
'start-tag
) 1 0)
1482 (if (eq after-context
'end-tag
) -
1 0))))))))
1484 (defun nxml-merge-indent-context-type (context)
1485 "Merge the indent context type CONTEXT with the token in `xmltok-type'.
1486 Return the merged indent context type. An indent context type is
1487 either nil or one of the symbols `start-tag', `end-tag', `markup',
1488 `comment', `mixed'."
1489 (cond ((memq xmltok-type
'(start-tag partial-start-tag
))
1490 (if (memq context
'(nil start-tag comment
))
1493 ((memq xmltok-type
'(end-tag partial-end-tag
))
1494 (if (memq context
'(nil end-tag comment
))
1497 ((eq xmltok-type
'comment
)
1498 (cond ((memq context
'(start-tag end-tag comment
))
1505 (defun nxml-compute-indent-in-token (pos)
1506 "Return the indent for a line that starts inside a token.
1507 POS is the position of the first non-whitespace character of the line.
1508 This expects the xmltok-* variables to be set up as by `xmltok-forward'."
1509 (cond ((memq xmltok-type
'(start-tag
1512 partial-empty-element
))
1513 (nxml-compute-indent-in-start-tag pos
))
1514 ((eq xmltok-type
'comment
)
1515 (nxml-compute-indent-in-delimited-token pos
"<!--" "-->"))
1516 ((eq xmltok-type
'cdata-section
)
1517 (nxml-compute-indent-in-delimited-token pos
"<![CDATA[" "]]>"))
1518 ((eq xmltok-type
'processing-instruction
)
1519 (nxml-compute-indent-in-delimited-token pos
"<?" "?>"))
1522 (if (and (= (forward-line -
1) 0)
1523 (< xmltok-start
(point)))
1524 (back-to-indentation)
1525 (goto-char xmltok-start
))
1528 (defun nxml-compute-indent-in-start-tag (pos)
1529 "Return the indent for a line that starts inside a start-tag.
1530 Also for a line that starts inside an empty element.
1531 POS is the position of the first non-whitespace character of the line.
1532 This expects the xmltok-* variables to be set up as by `xmltok-forward'."
1533 (let ((value-boundary (nxml-attribute-value-boundary pos
))
1536 ;; inside an attribute value
1537 (let ((value-start (car value-boundary
))
1538 (value-end (cdr value-boundary
)))
1541 (if (< (point) value-start
)
1542 (goto-char value-start
)
1543 (back-to-indentation)))
1544 ;; outside an attribute value
1546 (while (and (= (forward-line -
1) 0)
1547 (nxml-attribute-value-boundary (point))))
1548 (cond ((<= (point) xmltok-start
)
1549 (goto-char xmltok-start
)
1550 (setq off nxml-attribute-indent
)
1551 (let ((atts (xmltok-merge-attributes)))
1553 (let* ((att (car atts
))
1554 (start (xmltok-attribute-name-start att
)))
1559 (back-to-indentation))))
1560 (+ (current-column) off
)))
1562 (defun nxml-attribute-value-boundary (pos)
1563 "Return a pair (START . END) if POS is inside an attribute value.
1564 Otherwise return nil. START and END are the positions of the start
1565 and end of the attribute value containing POS. This expects the
1566 xmltok-* variables to be set up as by `xmltok-forward'."
1567 (let ((atts (xmltok-merge-attributes))
1568 att value-start value-end value-boundary
)
1570 (setq att
(car atts
))
1571 (setq value-start
(xmltok-attribute-value-start att
))
1572 (setq value-end
(xmltok-attribute-value-end att
))
1573 (cond ((and value-start
(< pos value-start
))
1575 ((and value-start value-end
(<= pos value-end
))
1576 (setq value-boundary
(cons value-start value-end
))
1578 (t (setq atts
(cdr atts
)))))
1581 (defun nxml-compute-indent-in-delimited-token (pos open-delim close-delim
)
1582 "Return the indent for a line that starts inside a token with delimiters.
1583 OPEN-DELIM and CLOSE-DELIM are strings giving the opening and closing
1584 delimiters. POS is the position of the first non-whitespace character
1585 of the line. This expects the xmltok-* variables to be set up as by
1587 (cond ((let ((end (+ pos
(length close-delim
))))
1588 (and (<= end
(point-max))
1589 (string= (buffer-substring-no-properties pos end
)
1591 (goto-char xmltok-start
))
1595 (<= (point) xmltok-start
))
1596 (goto-char (+ xmltok-start
(length open-delim
)))
1597 (when (and (string= open-delim
"<!--")
1599 (goto-char (1+ (point)))))
1600 (t (back-to-indentation)))
1605 (defun nxml-complete ()
1606 "Perform completion on the symbol preceding point.
1608 Inserts as many characters as can be completed. However, if not even
1609 one character can be completed, then a buffer with the possibilities
1610 is popped up and the symbol is read from the minibuffer with
1611 completion. If the symbol is complete, then any characters that must
1612 follow the symbol are also inserted.
1614 The name space used for completion and what is treated as a symbol
1615 depends on the context. The contexts in which completion is performed
1616 depend on `nxml-completion-hook'."
1618 (unless (run-hook-with-args-until-success 'nxml-completion-hook
)
1619 ;; Eventually we will complete on entity names here.
1621 (message "Cannot complete in this context")))
1625 (defun nxml-forward-balanced-item (&optional arg
)
1626 "Move forward across one balanced item.
1627 With ARG, do it that many times. Negative arg -N means
1628 move backward across N balanced expressions.
1629 This is the equivalent of `forward-sexp' for XML.
1631 An element contains as items strings with no markup, tags, processing
1632 instructions, comments, CDATA sections, entity references and
1633 characters references. However, if the variable
1634 `nxml-sexp-element-flag' is non-nil, then an element is treated as a
1635 single markup item. A start-tag contains an element name followed by
1636 one or more attributes. An end-tag contains just an element name.
1637 An attribute value literals contains strings with no markup, entity
1638 references and character references. A processing instruction
1639 consists of a target and a content string. A comment or a CDATA
1640 section contains a single string. An entity reference contains a
1641 single name. A character reference contains a character number."
1643 (or arg
(setq arg
1))
1646 (nxml-forward-single-balanced-item)
1647 (> (setq arg
(1- arg
)) 0))))
1650 (nxml-backward-single-balanced-item)
1651 (< (setq arg
(1+ arg
)) 0))))))
1653 (defun nxml-forward-single-balanced-item ()
1655 (goto-char (let ((end (nxml-token-after)))
1657 (while (eq xmltok-type
'space
)
1659 (setq end
(nxml-token-after)))
1660 (cond ((/= (point) xmltok-start
)
1661 (nxml-scan-forward-within end
))
1662 ((and nxml-sexp-element-flag
1663 (eq xmltok-type
'start-tag
))
1664 ;; can't ever return nil here
1665 (nxml-scan-element-forward xmltok-start
))
1666 ((and nxml-sexp-element-flag
1668 '(end-tag partial-end-tag
)))
1669 (error "Already at end of element"))
1672 (goto-char (cadr err
))
1673 (apply 'error
(cddr err
)))))
1675 (defun nxml-backward-single-balanced-item ()
1677 (goto-char (let ((end (nxml-token-before)))
1679 (while (eq xmltok-type
'space
)
1680 (goto-char xmltok-start
)
1681 (setq end
(nxml-token-before)))
1682 (cond ((/= (point) end
)
1683 (nxml-scan-backward-within end
))
1684 ((and nxml-sexp-element-flag
1685 (eq xmltok-type
'end-tag
))
1686 ;; can't ever return nil here
1687 (nxml-scan-element-backward end
)
1689 ((and nxml-sexp-element-flag
1690 (eq xmltok-type
'start-tag
))
1691 (error "Already at start of element"))
1692 (t xmltok-start
)))))
1694 (goto-char (cadr err
))
1695 (apply 'error
(cddr err
)))))
1697 (defun nxml-scan-forward-within (end)
1698 (setq end
(- end
(nxml-end-delimiter-length xmltok-type
)))
1699 (when (<= end
(point))
1700 (error "Already at end of %s"
1701 (nxml-token-type-friendly-name xmltok-type
)))
1702 (cond ((memq xmltok-type
'(start-tag
1705 partial-empty-element
))
1706 (if (< (point) xmltok-name-end
)
1708 (let ((att (nxml-find-following-attribute)))
1709 (cond ((not att
) end
)
1710 ((and (xmltok-attribute-value-start att
)
1711 (<= (xmltok-attribute-value-start att
)
1713 (nxml-scan-forward-in-attribute-value att
))
1714 ((xmltok-attribute-value-end att
)
1715 (1+ (xmltok-attribute-value-end att
)))
1717 (goto-char (xmltok-attribute-name-end att
))
1718 (looking-at "[ \t\r\n]*="))
1720 (t (xmltok-attribute-name-end att
))))))
1721 ((and (eq xmltok-type
'processing-instruction
)
1722 (< (point) xmltok-name-end
))
1726 (defun nxml-scan-backward-within (end)
1729 (nxml-start-delimiter-length xmltok-type
)))
1730 (when (<= (point) xmltok-start
)
1731 (error "Already at start of %s"
1732 (nxml-token-type-friendly-name xmltok-type
)))
1733 (cond ((memq xmltok-type
'(start-tag
1736 partial-empty-element
))
1737 (let ((att (nxml-find-preceding-attribute)))
1738 (cond ((not att
) xmltok-start
)
1739 ((and (xmltok-attribute-value-start att
)
1740 (<= (xmltok-attribute-value-start att
)
1743 (xmltok-attribute-value-end att
)))
1744 (nxml-scan-backward-in-attribute-value att
))
1745 (t (xmltok-attribute-name-start att
)))))
1746 ((and (eq xmltok-type
'processing-instruction
)
1747 (let ((content-start (save-excursion
1748 (goto-char xmltok-name-end
)
1749 (skip-chars-forward " \r\t\n")
1751 (and (< content-start
(point))
1755 (defun nxml-scan-forward-in-attribute-value (att)
1756 (when (= (point) (xmltok-attribute-value-end att
))
1757 (error "Already at end of attribute value"))
1758 (let ((refs (xmltok-attribute-refs att
))
1761 (setq ref
(car refs
))
1762 (if (< (point) (aref ref
2))
1765 (setq refs
(cdr refs
))))
1767 (xmltok-attribute-value-end att
))
1768 ((< (point) (aref ref
1))
1770 ((= (point) (aref ref
1))
1773 (let ((end (- (aref ref
2)
1774 (nxml-end-delimiter-length (aref ref
0)))))
1777 (error "Already at end of %s"
1778 (nxml-token-type-friendly-name (aref ref
0)))))))))
1780 (defun nxml-scan-backward-in-attribute-value (att)
1781 (when (= (point) (xmltok-attribute-value-start att
))
1782 (error "Already at start of attribute value"))
1783 (let ((refs (reverse (xmltok-attribute-refs att
)))
1786 (setq ref
(car refs
))
1787 (if (< (aref ref
1) (point))
1790 (setq refs
(cdr refs
))))
1792 (xmltok-attribute-value-start att
))
1793 ((< (aref ref
2) (point))
1795 ((= (point) (aref ref
2))
1798 (let ((start (+ (aref ref
1)
1799 (nxml-start-delimiter-length (aref ref
0)))))
1800 (if (< start
(point))
1802 (error "Already at start of %s"
1803 (nxml-token-type-friendly-name (aref ref
0)))))))))
1805 (defun nxml-find-following-attribute ()
1807 (atts (or xmltok-attributes xmltok-namespace-attributes
))
1808 (more-atts (and xmltok-attributes xmltok-namespace-attributes
)))
1810 (let* ((att (car atts
))
1811 (name-start (xmltok-attribute-name-start att
)))
1812 (cond ((and (<= name-start
(point))
1813 (xmltok-attribute-value-end att
)
1814 ;; <= because end is before quote
1815 (<= (point) (xmltok-attribute-value-end att
)))
1818 ((and (< (point) name-start
)
1821 (xmltok-attribute-name-start ret
))))
1823 (setq atts
(cdr atts
))
1825 (setq atts more-atts
)
1826 (setq more-atts nil
)))
1829 (defun nxml-find-preceding-attribute ()
1831 (atts (or xmltok-attributes xmltok-namespace-attributes
))
1832 (more-atts (and xmltok-attributes xmltok-namespace-attributes
)))
1834 (let* ((att (car atts
))
1835 (name-start (xmltok-attribute-name-start att
)))
1836 (cond ((and (< name-start
(point))
1837 (xmltok-attribute-value-end att
)
1838 ;; <= because end is before quote
1839 (<= (point) (xmltok-attribute-value-end att
)))
1842 ((and (< name-start
(point))
1844 (< (xmltok-attribute-name-start ret
)
1847 (setq atts
(cdr atts
))
1849 (setq atts more-atts
)
1850 (setq more-atts nil
)))
1853 (defun nxml-up-element (&optional arg
)
1855 (or arg
(setq arg
1))
1857 (nxml-backward-up-element (- arg
))
1859 (while (and (> arg
0)
1860 (< (point) (point-max)))
1861 (let ((token-end (nxml-token-after)))
1862 (goto-char (cond ((or (memq xmltok-type
'(end-tag
1864 (and (memq xmltok-type
1866 partial-empty-element
))
1867 (< xmltok-start
(point))))
1869 ((nxml-scan-element-forward
1870 (if (and (eq xmltok-type
'start-tag
)
1871 (= (point) xmltok-start
))
1875 (t (error "No parent element")))))
1876 (setq arg
(1- arg
)))
1878 (goto-char (cadr err
))
1879 (apply 'error
(cddr err
))))))
1881 (defun nxml-backward-up-element (&optional arg
)
1883 (or arg
(setq arg
1))
1885 (nxml-up-element (- arg
))
1887 (while (and (> arg
0)
1888 (< (point-min) (point)))
1889 (let ((token-end (nxml-token-before)))
1890 (goto-char (cond ((or (memq xmltok-type
'(start-tag
1892 (and (memq xmltok-type
1894 partial-empty-element
))
1895 (< (point) token-end
)))
1897 ((nxml-scan-element-backward
1898 (if (and (eq xmltok-type
'end-tag
)
1899 (= (point) token-end
))
1904 (t (error "No parent element")))))
1905 (setq arg
(1- arg
)))
1907 (goto-char (cadr err
))
1908 (apply 'error
(cddr err
))))))
1910 (defun nxml-down-element (&optional arg
)
1911 "Move forward down into the content of an element.
1912 With ARG, do this that many times.
1913 Negative ARG means move backward but still down."
1915 (or arg
(setq arg
1))
1917 (nxml-backward-down-element (- arg
))
1920 (let ((token-end (nxml-token-after)))
1922 (goto-char token-end
)
1924 (when (memq xmltok-type
'(nil end-tag partial-end-tag
))
1925 (error "No following start-tags in this element"))
1926 (not (memq xmltok-type
'(start-tag partial-start-tag
))))
1927 (nxml-tokenize-forward))
1929 (setq arg
(1- arg
)))))
1931 (defun nxml-backward-down-element (&optional arg
)
1933 (or arg
(setq arg
1))
1935 (nxml-down-element (- arg
))
1940 (goto-char xmltok-start
)
1942 (when (memq xmltok-type
'(start-tag
1946 (error "No preceding end-tags in this element"))
1947 (not (memq xmltok-type
'(end-tag partial-end-tag
))))
1948 (if (or (<= (point) nxml-prolog-end
)
1949 (not (search-backward "<" nxml-prolog-end t
)))
1950 (setq xmltok-type nil
)
1951 (nxml-move-outside-backwards)
1954 (setq arg
(1- arg
)))))
1956 (defun nxml-forward-element (&optional arg
)
1957 "Move forward over one element.
1958 With ARG, do it that many times.
1959 Negative ARG means move backward."
1961 (or arg
(setq arg
1))
1963 (nxml-backward-element (- arg
))
1965 (while (and (> arg
0)
1966 (< (point) (point-max)))
1968 (or (nxml-scan-element-forward (nxml-token-before))
1969 (error "No more elements")))
1970 (setq arg
(1- arg
)))
1972 (goto-char (cadr err
))
1973 (apply 'error
(cddr err
))))))
1975 (defun nxml-backward-element (&optional arg
)
1976 "Move backward over one element.
1977 With ARG, do it that many times.
1978 Negative ARG means move forward."
1980 (or arg
(setq arg
1))
1982 (nxml-forward-element (- arg
))
1984 (while (and (> arg
0)
1985 (< (point-min) (point)))
1987 (or (and (nxml-scan-element-backward (progn
1991 (error "No preceding elements")))
1992 (setq arg
(1- arg
)))
1994 (goto-char (cadr err
))
1995 (apply 'error
(cddr err
))))))
1997 (defun nxml-mark-token-after ()
1999 (push-mark (nxml-token-after) nil t
)
2000 (goto-char xmltok-start
)
2001 (message "Marked %s" xmltok-type
))
2005 (defun nxml-mark-paragraph ()
2006 "Put point at beginning of this paragraph, mark at end.
2007 The paragraph marked is the one that contains point or follows point."
2009 (nxml-forward-paragraph)
2011 (nxml-backward-paragraph))
2013 (defun nxml-forward-paragraph (&optional arg
)
2015 (or arg
(setq arg
1))
2017 (nxml-backward-paragraph (- arg
)))
2020 (while (and (nxml-forward-single-paragraph)
2021 (> (setq arg
(1- arg
)) 0))))))
2023 (defun nxml-backward-paragraph (&optional arg
)
2025 (or arg
(setq arg
1))
2027 (nxml-forward-paragraph (- arg
)))
2030 (let ((inhibit-field-text-motion t
))
2032 (while (and (nxml-backward-single-paragraph)
2033 (> (setq arg
(1- arg
)) 0))))))
2035 (defun nxml-forward-single-paragraph ()
2036 "Move forward over a single paragraph.
2037 Return nil at end of buffer, t otherwise."
2038 (let* ((token-end (nxml-token-after))
2039 (offset (- (point) xmltok-start
))
2041 (goto-char token-end
)
2042 (while (and (< (point) (point-max))
2044 (nxml-paragraph-end-pos had-data offset
))))
2045 (when (nxml-token-contains-data-p offset
)
2047 (nxml-tokenize-forward)
2049 (when pos
(goto-char pos
))))
2051 (defun nxml-backward-single-paragraph ()
2052 "Move backward over a single paragraph.
2053 Return nil at start of buffer, t otherwise."
2054 (let* ((token-end (nxml-token-before))
2055 (offset (- token-end
(point)))
2056 (last-tag-pos xmltok-start
)
2057 pos had-data last-data-pos
)
2058 (goto-char token-end
)
2059 (unless (setq pos
(nxml-paragraph-start-pos nil offset
))
2060 (setq had-data
(nxml-token-contains-data-p nil offset
))
2061 (goto-char xmltok-start
)
2062 (while (and (not pos
) (< (point-min) (point)))
2063 (cond ((search-backward "<" nxml-prolog-end t
)
2064 (nxml-move-outside-backwards)
2066 (while (< (point) last-tag-pos
)
2068 (when (and (not had-data
) (nxml-token-contains-data-p))
2070 (setq last-data-pos xmltok-start
))
2071 (let ((tem (nxml-paragraph-start-pos had-data
0)))
2072 (when tem
(setq pos tem
)))))
2073 (when (and (not had-data
) last-data-pos
(not pos
))
2076 (while (< (point) last-data-pos
)
2078 (let ((tem (nxml-paragraph-start-pos had-data
0)))
2079 (when tem
(setq pos tem
)))))
2080 (setq last-tag-pos
(point)))
2081 (t (goto-char (point-min))))))
2082 (when pos
(goto-char pos
))))
2084 (defun nxml-token-contains-data-p (&optional start end
)
2085 (setq start
(+ xmltok-start
(or start
0)))
2086 (setq end
(- (point) (or end
0)))
2087 (when (eq xmltok-type
'cdata-section
)
2088 (setq start
(max start
(+ xmltok-start
9)))
2089 (setq end
(min end
(- (point) 3))))
2090 (or (and (eq xmltok-type
'data
)
2091 (eq start xmltok-start
)
2093 (eq xmltok-type
'char-ref
)
2094 (and (memq xmltok-type
'(data cdata-section
))
2098 (re-search-forward "[^ \t\r\n]" end t
)))))
2100 (defun nxml-paragraph-end-pos (had-data offset
)
2101 "Return the position of the paragraph end if contained in the current token.
2102 Return nil if the current token does not contain the paragraph end.
2103 Only characters after OFFSET from the start of the token are eligible.
2104 HAD-DATA says whether there have been non-whitespace data characters yet."
2105 (cond ((not had-data
)
2106 (cond ((memq xmltok-type
'(data cdata-section
))
2108 (let ((end (point)))
2109 (goto-char (+ xmltok-start
2110 (max (if (eq xmltok-type
'cdata-section
)
2114 (and (re-search-forward "[^ \t\r\n]" end t
)
2115 (re-search-forward "^[ \t]*$" end t
)
2116 (match-beginning 0)))))
2117 ((and (eq xmltok-type
'comment
)
2118 (nxml-token-begins-line-p)
2119 (nxml-token-ends-line-p))
2121 (let ((end (point)))
2122 (goto-char (+ xmltok-start
(max 4 offset
)))
2123 (when (re-search-forward "[^ \t\r\n]" (- end
3) t
)
2124 (if (re-search-forward "^[ \t]*$" end t
)
2126 (goto-char (- end
3))
2127 (skip-chars-backward " \t")
2129 (beginning-of-line 2))
2131 ((memq xmltok-type
'(data space cdata-section
))
2133 (let ((end (point)))
2134 (goto-char (+ xmltok-start offset
))
2135 (and (re-search-forward "^[ \t]*$" end t
)
2136 (match-beginning 0)))))
2137 ((and (memq xmltok-type
'(start-tag
2141 processing-instruction
2143 (nxml-token-begins-line-p)
2144 (nxml-token-ends-line-p))
2146 (goto-char xmltok-start
)
2147 (skip-chars-backward " \t")
2149 ((and (eq xmltok-type
'end-tag
)
2150 (looking-at "[ \t]*$")
2151 (not (nxml-in-mixed-content-p t
)))
2153 (or (search-forward "\n" nil t
)
2156 (defun nxml-paragraph-start-pos (had-data offset
)
2157 "Return the position of the paragraph start if contained in the current token.
2158 Return nil if the current token does not contain the paragraph start.
2159 Only characters before OFFSET from the end of the token are eligible.
2160 HAD-DATA says whether there have been non-whitespace data characters yet."
2161 (cond ((not had-data
)
2162 (cond ((memq xmltok-type
'(data cdata-section
))
2164 (goto-char (- (point)
2165 (max (if (eq xmltok-type
'cdata-section
)
2169 (and (re-search-backward "[^ \t\r\n]" xmltok-start t
)
2170 (re-search-backward "^[ \t]*$" xmltok-start t
)
2171 (match-beginning 0))))
2172 ((and (eq xmltok-type
'comment
)
2173 (nxml-token-ends-line-p)
2174 (nxml-token-begins-line-p))
2176 (goto-char (- (point) (max 3 offset
)))
2177 (when (and (< (+ xmltok-start
4) (point))
2178 (re-search-backward "[^ \t\r\n]"
2181 (if (re-search-backward "^[ \t]*$" xmltok-start t
)
2183 (goto-char xmltok-start
)
2184 (if (looking-at "<!--[ \t]*\n")
2186 (skip-chars-backward " \t")
2188 ((memq xmltok-type
'(data space cdata-section
))
2190 (goto-char (- (point) offset
))
2191 (and (re-search-backward "^[ \t]*$" xmltok-start t
)
2192 (match-beginning 0))))
2193 ((and (memq xmltok-type
'(start-tag
2197 processing-instruction
2199 (nxml-token-ends-line-p)
2200 (nxml-token-begins-line-p))
2201 (or (search-forward "\n" nil t
)
2203 ((and (eq xmltok-type
'start-tag
)
2204 (nxml-token-begins-line-p)
2205 (not (save-excursion
2206 (goto-char xmltok-start
)
2207 (nxml-in-mixed-content-p nil
))))
2209 (goto-char xmltok-start
)
2210 (skip-chars-backward " \t")
2211 ;; include any blank line before
2212 (or (and (eq (char-before) ?
\n)
2214 (goto-char (1- (point)))
2215 (skip-chars-backward " \t")
2216 (and (bolp) (point))))
2219 (defun nxml-token-ends-line-p () (looking-at "[ \t]*$"))
2221 (defun nxml-token-begins-line-p ()
2223 (goto-char xmltok-start
)
2224 (skip-chars-backward " \t")
2227 (defun nxml-in-mixed-content-p (endp)
2228 "Return non-nil if point is in mixed content.
2229 Point must be after an end-tag or before a start-tag.
2230 ENDP is t in the former case, nil in the latter."
2231 (let (matching-tag-pos)
2232 (cond ((not (run-hook-with-args-until-failure
2233 'nxml-in-mixed-content-hook
))
2235 ;; See if the matching tag does not start or end a line.
2236 ((condition-case err
2238 (setq matching-tag-pos
2241 (and (nxml-scan-element-backward (point))
2243 (nxml-scan-element-forward (point)))))
2244 (and matching-tag-pos
2246 (goto-char matching-tag-pos
)
2249 (skip-chars-backward " \t")
2251 (looking-at "[ \t]*$"))))))
2252 (nxml-scan-error nil
))
2254 ;; See if there's data at the same level.
2257 (setq start matching-tag-pos
2260 end matching-tag-pos
))
2264 (nxml-preceding-sibling-data-p))
2267 (nxml-following-sibling-data-p)))))
2269 ;; Otherwise, treat as not mixed
2272 (defun nxml-preceding-sibling-data-p ()
2273 "Return non-nil if there is a previous sibling that is data."
2274 (let ((lim (max (- (point) nxml-mixed-scan-distance
)
2280 (while (and (< lim
(point))
2285 (search-backward "<" lim t
)))
2286 (nxml-move-outside-backwards)
2289 (let ((prev-level level
))
2290 (cond ((eq xmltok-type
'end-tag
)
2291 (setq level
(1+ level
)))
2292 ((eq xmltok-type
'start-tag
)
2293 (setq level
(1- level
))))
2294 (when (eq prev-level
0)
2295 (while (and (< (point) end
) (not found
))
2297 (when (memq xmltok-type
'(data cdata-section char-ref
))
2298 (setq found t
)))))))))
2301 (defun nxml-following-sibling-data-p ()
2302 (let ((lim (min (+ (point) nxml-mixed-scan-distance
)
2308 (while (and (< (point) lim
)
2310 (nxml-tokenize-forward)
2312 (cond ((eq xmltok-type
'start-tag
)
2313 (setq level
(1+ level
)))
2314 ((eq xmltok-type
'end-tag
)
2315 (setq level
(1- level
)))
2317 (memq xmltok-type
'(data cdata-section char-ref
)))
2323 (defun nxml-do-fill-paragraph (arg)
2324 (let (fill-paragraph-function
2328 (nxml-forward-paragraph)
2330 (nxml-backward-paragraph)
2331 (skip-chars-forward " \t\r\n")
2332 (setq start
(point))
2334 (setq fill-prefix
(buffer-substring-no-properties (point) start
))
2335 (when (and (not (nxml-get-inside (point)))
2336 (looking-at "[ \t]*<!--"))
2337 (setq fill-prefix
(concat fill-prefix
" ")))
2338 (fill-region-as-paragraph start end arg
))
2339 (skip-line-prefix fill-prefix
)
2342 (defun nxml-newline-and-indent (soft)
2343 (delete-horizontal-space)
2344 (if soft
(insert-and-inherit ?
\n) (newline 1))
2350 (defvar nxml-dynamic-markup-prev-pos nil
)
2351 (defvar nxml-dynamic-markup-prev-lengths nil
)
2352 (defvar nxml-dynamic-markup-prev-found-marker nil
)
2353 (defvar nxml-dynamic-markup-prev-start-tags
(make-hash-table :test
'equal
))
2355 (defun nxml-dynamic-markup-word ()
2356 "Dynamically markup the word before point.
2357 This attempts to find a tag to put around the word before point based
2358 on the contents of the current buffer. The end-tag will be inserted at
2359 point. The start-tag will be inserted at or before the beginning of
2360 the word before point; the contents of the current buffer is used to
2363 It works in a similar way to \\[dabbrev-expand]. It searches first
2364 backwards from point, then forwards from point for an element whose
2365 content is a string which matches the contents of the buffer before
2366 point and which includes at least the word before point. It then
2367 copies the start- and end-tags from that element and uses them to
2368 surround the matching string before point.
2370 Repeating \\[nxml-dynamic-markup-word] immediately after successful
2371 \\[nxml-dynamic-markup-word] removes the previously inserted markup
2372 and attempts to find another possible way to do the markup."
2374 (let (search-start-pos done
)
2375 (if (and (integerp nxml-dynamic-markup-prev-pos
)
2376 (= nxml-dynamic-markup-prev-pos
(point))
2377 (eq last-command this-command
)
2378 nxml-dynamic-markup-prev-lengths
)
2379 (let* ((end-tag-open-pos
2380 (- nxml-dynamic-markup-prev-pos
2381 (nth 2 nxml-dynamic-markup-prev-lengths
)))
2382 (start-tag-close-pos
2384 (nth 1 nxml-dynamic-markup-prev-lengths
)))
2386 (- start-tag-close-pos
2387 (nth 0 nxml-dynamic-markup-prev-lengths
))))
2388 (delete-region end-tag-open-pos nxml-dynamic-markup-prev-pos
)
2389 (delete-region start-tag-open-pos start-tag-close-pos
)
2390 (setq search-start-pos
2391 (marker-position nxml-dynamic-markup-prev-found-marker
)))
2392 (clrhash nxml-dynamic-markup-prev-start-tags
))
2393 (setq nxml-dynamic-markup-prev-pos nil
)
2394 (setq nxml-dynamic-markup-prev-lengths nil
)
2395 (setq nxml-dynamic-markup-prev-found-marker nil
)
2398 (let* ((pos (point))
2401 (unless (< (point) pos
)
2402 (error "No word to markup"))
2403 (buffer-substring-no-properties (point) pos
)))
2404 (search (concat word
"</"))
2406 (when search-start-pos
2407 (goto-char search-start-pos
))
2408 (while (and (not done
)
2409 (or (and (< (point) pos
)
2410 (or (search-backward search nil t
)
2411 (progn (goto-char pos
) nil
)))
2412 (search-forward search nil t
)))
2413 (goto-char (- (match-end 0) 2))
2414 (setq done
(nxml-try-copy-markup pos
)))
2416 (error (if (zerop (hash-table-count
2417 nxml-dynamic-markup-prev-start-tags
))
2418 "No possible markup found for `%s'"
2419 "No more markup possibilities found for `%s'")
2422 (defun nxml-try-copy-markup (word-end-pos)
2424 (let ((end-tag-pos (point)))
2425 (when (and (not (nxml-get-inside end-tag-pos
))
2426 (search-backward "<" nil t
)
2427 (not (nxml-get-inside (point))))
2429 (when (and (eq xmltok-type
'start-tag
)
2430 (< (point) end-tag-pos
))
2431 (let* ((start-tag-close-pos (point))
2433 (buffer-substring-no-properties xmltok-start
2434 start-tag-close-pos
))
2438 (buffer-substring-no-properties start-tag-close-pos
2441 (goto-char word-end-pos
)
2443 (re-search-backward (concat
2444 (regexp-quote (car words
))
2448 (setq words
(cdr words
))
2449 (skip-chars-backward " \t\r\n"))
2450 (when (and (not words
)
2452 (skip-chars-forward " \t\r\n")
2453 (not (gethash (cons (point) start-tag
)
2454 nxml-dynamic-markup-prev-start-tags
)))
2455 (or (< end-tag-pos
(point))
2456 (< word-end-pos xmltok-start
)))
2457 (setq nxml-dynamic-markup-prev-found-marker
2458 (copy-marker end-tag-pos t
))
2459 (puthash (cons (point) start-tag
)
2461 nxml-dynamic-markup-prev-start-tags
)
2462 (setq nxml-dynamic-markup-prev-lengths
2463 (list (- start-tag-close-pos xmltok-start
)
2464 (- word-end-pos
(point))
2465 (+ (- xmltok-name-end xmltok-start
) 2)))
2466 (let ((name (xmltok-start-tag-qname)))
2468 (goto-char (+ word-end-pos
2469 (- start-tag-close-pos xmltok-start
)))
2470 (insert "</" name
">")
2471 (setq nxml-dynamic-markup-prev-pos
(point))))))))))
2476 (defvar nxml-char-name-ignore-case t
)
2478 (defvar nxml-char-name-alist nil
2479 "Alist of character names.
2480 Each member of the list has the form (NAME CODE . NAMESET),
2481 where NAME is a string naming a character, NAMESET is a symbol
2482 identifying a set of names and CODE is an integer specifying the
2483 Unicode scalar value of the named character.
2484 The NAME will only be used for completion if NAMESET has
2485 a non-nil `nxml-char-name-set-enabled' property.
2486 If NAMESET does does not have `nxml-char-name-set-defined' property,
2487 then it must have a `nxml-char-name-set-file' property and `load'
2488 will be applied to the value of this property if the nameset
2491 (defvar nxml-char-name-table
(make-hash-table :test
'eq
)
2492 "Hash table for mapping char codes to names.
2493 Each key is a Unicode scalar value.
2494 Each value is a list of pairs of the form (NAMESET . NAME),
2495 where NAMESET is a symbol identifying a set of names,
2496 and NAME is a string naming a character.")
2498 (defvar nxml-autoload-char-name-set-list nil
2499 "List of char namesets that can be autoloaded.")
2501 (defun nxml-enable-char-name-set (nameset)
2502 (put nameset
'nxml-char-name-set-enabled t
))
2504 (defun nxml-disable-char-name-set (nameset)
2505 (put nameset
'nxml-char-name-set-enabled nil
))
2507 (defun nxml-char-name-set-enabled-p (nameset)
2508 (get nameset
'nxml-char-name-set-enabled
))
2510 (defun nxml-autoload-char-name-set (nameset file
)
2511 (unless (memq nameset nxml-autoload-char-name-set-list
)
2512 (setq nxml-autoload-char-name-set-list
2513 (cons nameset nxml-autoload-char-name-set-list
)))
2514 (put nameset
'nxml-char-name-set-file file
))
2516 (defun nxml-define-char-name-set (nameset alist
)
2517 "Define a set of character names.
2518 NAMESET is a symbol identifying the set.
2519 ALIST is a list where each member has the form (NAME CODE),
2520 where NAME is a string naming a character and code is an
2521 integer giving the Unicode scalar value of the character."
2522 (when (get nameset
'nxml-char-name-set-defined
)
2523 (error "Nameset `%s' already defined" nameset
))
2526 (let* ((name-code (car iter
))
2527 (name (car name-code
))
2528 (code (cadr name-code
)))
2530 (cons (cons nameset name
)
2531 (gethash code nxml-char-name-table
))
2532 nxml-char-name-table
))
2533 (setcdr (cdr (car iter
)) nameset
)
2534 (setq iter
(cdr iter
))))
2535 (setq nxml-char-name-alist
2536 (nconc alist nxml-char-name-alist
))
2537 (put nameset
'nxml-char-name-set-defined t
))
2539 (defun nxml-get-char-name (code)
2540 (mapc 'nxml-maybe-load-char-name-set nxml-autoload-char-name-set-list
)
2541 (let ((names (gethash code nxml-char-name-table
))
2543 (while (and names
(not name
))
2544 (if (nxml-char-name-set-enabled-p (caar names
))
2545 (setq name
(cdar names
))
2546 (setq names
(cdr names
))))
2549 (defvar nxml-named-char-history nil
)
2551 (defun nxml-insert-named-char (arg)
2552 "Insert a character using its name.
2553 The name is read from the minibuffer.
2554 Normally, inserts the character as a numeric character reference.
2555 With a prefix argument, inserts the character directly."
2557 (mapc 'nxml-maybe-load-char-name-set nxml-autoload-char-name-set-list
)
2559 (let ((completion-ignore-case nxml-char-name-ignore-case
))
2560 (completing-read "Character name: "
2561 nxml-char-name-alist
2563 (get (cddr member
) 'nxml-char-name-set-enabled
))
2566 'nxml-named-char-history
)))
2567 (alist nxml-char-name-alist
)
2569 (while (and alist
(not code
))
2570 (setq elt
(assoc name alist
))
2571 (if (get (cddr elt
) 'nxml-char-name-set-enabled
)
2572 (setq code
(cadr elt
))
2573 (setq alist
(cdr (member elt alist
)))))
2576 (or (decode-char 'ucs code
)
2577 (error "Character %x is not supported by Emacs"
2579 (format "&#x%X;" code
))))))
2581 (defun nxml-maybe-load-char-name-set (sym)
2582 (when (and (get sym
'nxml-char-name-set-enabled
)
2583 (not (get sym
'nxml-char-name-set-defined
))
2584 (stringp (get sym
'nxml-char-name-set-file
)))
2585 (load (get sym
'nxml-char-name-set-file
))))
2587 (defun nxml-toggle-char-ref-extra-display (arg)
2588 "Toggle the display of extra information for character references."
2590 (let ((new (if (null arg
)
2591 (not nxml-char-ref-extra-display
)
2592 (> (prefix-numeric-value arg
) 0))))
2593 (when (not (eq new nxml-char-ref-extra-display
))
2594 (setq nxml-char-ref-extra-display new
)
2595 (font-lock-fontify-buffer))))
2597 (put 'nxml-char-ref
'evaporate t
)
2599 (defun nxml-char-ref-display-extra (start end n
)
2600 (when nxml-char-ref-extra-display
2601 (let ((name (nxml-get-char-name n
))
2602 (glyph-string (and nxml-char-ref-display-glyph-flag
2603 (nxml-glyph-display-string n
'nxml-glyph
)))
2605 (when (or name glyph-string
)
2606 (setq ov
(make-overlay start end nil t
))
2607 (overlay-put ov
'category
'nxml-char-ref
)
2609 (overlay-put ov
'help-echo name
))
2613 (propertize glyph-string
'face
'nxml-glyph
)))))))
2615 (defun nxml-clear-char-ref-extra-display (start end
)
2616 (let ((ov (overlays-in start end
)))
2618 (when (eq (overlay-get (car ov
) 'category
) 'nxml-char-ref
)
2619 (delete-overlay (car ov
)))
2620 (setq ov
(cdr ov
)))))
2623 (defun nxml-start-delimiter-length (type)
2624 (or (get type
'nxml-start-delimiter-length
)
2627 (put 'cdata-section
'nxml-start-delimiter-length
9)
2628 (put 'comment
'nxml-start-delimiter-length
4)
2629 (put 'processing-instruction
'nxml-start-delimiter-length
2)
2630 (put 'start-tag
'nxml-start-delimiter-length
1)
2631 (put 'empty-element
'nxml-start-delimiter-length
1)
2632 (put 'partial-empty-element
'nxml-start-delimiter-length
1)
2633 (put 'entity-ref
'nxml-start-delimiter-length
1)
2634 (put 'char-ref
'nxml-start-delimiter-length
2)
2636 (defun nxml-end-delimiter-length (type)
2637 (or (get type
'nxml-end-delimiter-length
)
2640 (put 'cdata-section
'nxml-end-delimiter-length
3)
2641 (put 'comment
'nxml-end-delimiter-length
3)
2642 (put 'processing-instruction
'nxml-end-delimiter-length
2)
2643 (put 'start-tag
'nxml-end-delimiter-length
1)
2644 (put 'empty-element
'nxml-end-delimiter-length
2)
2645 (put 'partial-empty-element
'nxml-end-delimiter-length
1)
2646 (put 'entity-ref
'nxml-end-delimiter-length
1)
2647 (put 'char-ref
'nxml-end-delimiter-length
1)
2649 (defun nxml-token-type-friendly-name (type)
2650 (or (get type
'nxml-friendly-name
)
2651 (symbol-name type
)))
2653 (put 'cdata-section
'nxml-friendly-name
"CDATA section")
2654 (put 'processing-instruction
'nxml-friendly-name
"processing instruction")
2655 (put 'entity-ref
'nxml-friendly-name
"entity reference")
2656 (put 'char-ref
'nxml-friendly-name
"character reference")
2659 (defalias 'xml-mode
'nxml-mode
)
2661 (provide 'nxml-mode
)
2663 ;; arch-tag: 8603bc5f-1ef9-4021-b223-322fb2ca708e
2664 ;;; nxml-mode.el ends here