1 ;;; nxml-mode.el --- a new XML mode
3 ;; Copyright (C) 2003, 2004, 2007, 2008 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 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.
27 ;; The font locking here is independent of font-lock.el. We want to
28 ;; do more sophisticated handling of changes and we want to use the
29 ;; same xmltok rather than regexps for parsing so that we parse
30 ;; consistently and correctly.
34 (when (featurep 'mucs
)
35 (error "nxml-mode is not compatible with Mule-UCS"))
44 (declare-function rng-nxml-mode-init
"rng-nxml")
45 (declare-function nxml-enable-unicode-char-name-sets
"nxml-uchnm")
50 "New XML editing mode"
54 (defgroup nxml-faces nil
55 "Faces for XML syntax highlighting."
57 :group
'font-lock-faces
)
59 (defcustom nxml-syntax-highlight-flag t
60 "*Non-nil means nxml-mode should perform syntax highlighting."
64 (defcustom nxml-char-ref-display-glyph-flag t
65 "*Non-nil means display glyph following character reference.
66 The glyph is displayed in face `nxml-glyph'. The hook
67 `nxml-glyph-set-hook' can be used to customize for which characters
68 glyphs are displayed."
72 (defcustom nxml-mode-hook nil
73 "Hook run by command `nxml-mode'."
77 (defcustom nxml-sexp-element-flag nil
78 "*Non-nil means sexp commands treat an element as a single expression."
82 (defcustom nxml-slash-auto-complete-flag nil
83 "*Non-nil means typing a slash automatically completes the end-tag.
84 This is used by `nxml-electric-slash'."
88 (defcustom nxml-child-indent
2
89 "*Indentation for the children of an element relative to the start-tag.
90 This only applies when the line or lines containing the start-tag contains
91 nothing else other than that start-tag."
95 (defcustom nxml-attribute-indent
4
96 "*Indentation for the attributes of an element relative to the start-tag.
97 This only applies when the first attribute of a tag starts a line. In other
98 cases, the first attribute on one line is indented the same as the first
99 attribute on the previous line."
103 (defvar nxml-fontify-chunk-size
500)
105 (defcustom nxml-bind-meta-tab-to-complete-flag
(not window-system
)
106 "*Non-nil means bind M-TAB in `nxml-mode-map' to `nxml-complete'.
107 C-return will be bound to `nxml-complete' in any case.
108 M-TAB gets swallowed by many window systems/managers, and
109 `documentation' will show M-TAB rather than C-return as the
110 binding `rng-complete' when both are bound. So it's better
111 to bind M-TAB only when it will work."
113 :set
(lambda (sym flag
)
114 (set-default sym flag
)
115 (when (and (boundp 'nxml-mode-map
) nxml-mode-map
)
116 (define-key nxml-mode-map
"\M-\t" (and flag
'nxml-complete
))))
119 (defcustom nxml-prefer-utf-16-to-utf-8-flag nil
120 "*Non-nil means prefer UTF-16 to UTF-8 when saving a buffer.
121 This is used only when a buffer does not contain an encoding declaration
122 and when its current `buffer-file-coding-system' specifies neither UTF-16
127 (defcustom nxml-prefer-utf-16-little-to-big-endian-flag
(eq system-type
129 "*Non-nil means prefer little-endian to big-endian byte-order for UTF-16.
130 This is used only for saving a buffer; when reading the byte-order is
131 auto-detected. It may be relevant both when there is no encoding declaration
132 and when the encoding declaration specifies `UTF-16'."
136 (defcustom nxml-default-buffer-file-coding-system nil
137 "*Default value for `buffer-file-coding-system' for a buffer for a new file.
138 Nil means use the default value of `buffer-file-coding-system' as normal.
139 A buffer's `buffer-file-coding-system' affects what \\[nxml-insert-xml-declaration] inserts."
141 :type
'coding-system
)
143 (defcustom nxml-auto-insert-xml-declaration-flag nil
144 "*Non-nil means automatically insert an XML declaration in a new file.
145 The XML declaration is inserted using `nxml-insert-xml-declaration'."
149 (defface nxml-delimited-data
150 '((t (:inherit font-lock-doc-face
)))
151 "Face used to highlight data enclosed between delimiters.
152 This is not used directly, but only via inheritance by other faces."
156 '((t (:inherit font-lock-builtin-face
)))
157 "Face used to highlight various names.
158 This includes element and attribute names, processing
159 instruction targets and the CDATA keyword in a CDATA section.
160 This is not used directly, but only via inheritance by other faces."
164 '((t (:inherit font-lock-constant-face
)))
165 "Face used to highlight character and entity references.
166 This is not used directly, but only via inheritance by other faces."
169 (defface nxml-delimiter
171 "Face used to highlight delimiters.
172 This is not used directly, but only via inheritance by other faces."
177 "Face used to highlight text."
180 (defface nxml-comment-content
181 '((t (:inherit font-lock-comment-face
)))
182 "Face used to highlight the content of comments."
185 (defface nxml-comment-delimiter
186 '((t (:inherit font-lock-comment-delimiter-face
)))
187 "Face used for the delimiters of comments, i.e <!-- and -->."
190 (defface nxml-processing-instruction-delimiter
191 '((t (:inherit nxml-delimiter
)))
192 "Face used for the delimiters of processing instructions, i.e <? and ?>."
195 (defface nxml-processing-instruction-target
196 '((t (:inherit font-lock-keyword-face
)))
197 "Face used for the target of processing instructions."
200 (defface nxml-processing-instruction-content
201 '((t (:inherit nxml-delimited-data
)))
202 "Face used for the content of processing instructions."
205 (defface nxml-cdata-section-delimiter
206 '((t (:inherit nxml-delimiter
)))
207 "Face used for the delimiters of CDATA sections, i.e <![, [, and ]]>."
210 (defface nxml-cdata-section-CDATA
211 '((t (:inherit nxml-name
)))
212 "Face used for the CDATA keyword in CDATA sections."
215 (defface nxml-cdata-section-content
216 '((t (:inherit nxml-text
)))
217 "Face used for the content of CDATA sections."
220 (defface nxml-char-ref-number
221 '((t (:inherit nxml-ref
)))
222 "Face used for the number in character references.
223 This includes ths `x' in hex references."
226 (defface nxml-char-ref-delimiter
227 '((t (:inherit nxml-ref
)))
228 "Face used for the delimiters of character references, i.e &# and ;."
231 (defface nxml-entity-ref-name
232 '((t (:inherit nxml-ref
)))
233 "Face used for the entity name in general entity references."
236 (defface nxml-entity-ref-delimiter
237 '((t (:inherit nxml-ref
)))
238 "Face used for the delimiters of entity references, i.e & and ;."
241 (defface nxml-tag-delimiter
242 '((t (:inherit nxml-delimiter
)))
243 "Face used for the angle brackets delimiting tags.
244 `nxml-tag-slash' is used for slashes."
247 (defface nxml-tag-slash
248 '((t (:inherit nxml-tag-delimiter
)))
249 "Face used for slashes in tags, both in end-tags and empty-elements."
252 (defface nxml-element-prefix
253 '((t (:inherit nxml-name
)))
254 "Face used for the prefix of elements."
257 (defface nxml-element-colon
259 "Face used for the colon in element names."
262 (defface nxml-element-local-name
263 '((t (:inherit font-lock-function-name-face
)))
264 "Face used for the local name of elements."
267 (defface nxml-attribute-prefix
268 '((t (:inherit nxml-name
)))
269 "Face used for the prefix of attributes."
272 (defface nxml-attribute-colon
273 '((t (:inherit nxml-delimiter
)))
274 "Face used for the colon in attribute names."
277 (defface nxml-attribute-local-name
278 '((t (:inherit font-lock-variable-name-face
)))
279 "Face used for the local name of attributes."
282 (defface nxml-namespace-attribute-xmlns
283 '((t (:inherit nxml-attribute-prefix
)))
284 "Face used for `xmlns' in namespace attributes."
287 (defface nxml-namespace-attribute-colon
288 '((t (:inherit nxml-attribute-colon
)))
289 "Face used for the colon in namespace attributes."
292 (defface nxml-namespace-attribute-prefix
293 '((t (:inherit nxml-attribute-local-name
)))
294 "Face used for the prefix declared in namespace attributes."
297 (defface nxml-attribute-value
298 '((t (:inherit font-lock-string-face
)))
299 "Face used for the value of attributes."
302 (defface nxml-attribute-value-delimiter
303 '((t (:inherit nxml-attribute-value
)))
304 "Face used for the delimiters of attribute values."
307 (defface nxml-namespace-attribute-value
308 '((t (:inherit nxml-attribute-value
)))
309 "Face used for the value of namespace attributes."
312 (defface nxml-namespace-attribute-value-delimiter
313 '((t (:inherit nxml-attribute-value-delimiter
)))
314 "Face used for the delimiters of namespace attribute values."
317 (defface nxml-prolog-literal-delimiter
318 '((t (:inherit nxml-delimited-data
)))
319 "Face used for the delimiters of literals in the prolog."
322 (defface nxml-prolog-literal-content
323 '((t (:inherit nxml-delimited-data
)))
324 "Face used for the content of literals in the prolog."
327 (defface nxml-prolog-keyword
328 '((t (:inherit font-lock-keyword-face
)))
329 "Face used for keywords in the prolog."
332 (defface nxml-markup-declaration-delimiter
333 '((t (:inherit nxml-delimiter
)))
334 "Face used for the delimiters of markup declarations in the prolog.
335 The delimiters are <! and >."
339 '((t (:inherit nxml-name
)))
340 "Face used for # before a name in the prolog."
364 "Face used for glyph for char references."
369 (defvar nxml-prolog-regions nil
370 "List of regions in the prolog to be fontified.
371 See the function `xmltok-forward-prolog' for more information.")
372 (make-variable-buffer-local 'nxml-prolog-regions
)
374 (defvar nxml-last-fontify-end nil
375 "Position where fontification last ended.
376 Nil if the buffer changed since the last fontification.")
377 (make-variable-buffer-local 'nxml-last-fontify-end
)
379 (defvar nxml-degraded nil
380 "Non-nil if currently operating in degraded mode.
381 Degraded mode is enabled when an internal error is encountered in the
382 fontification or after-change functions.")
383 (make-variable-buffer-local 'nxml-degraded
)
385 (defvar nxml-completion-hook nil
386 "Hook run by `nxml-complete'.
387 This hook is run until success.")
389 (defvar nxml-in-mixed-content-hook nil
390 "Hook to determine whether point is in mixed content.
391 The hook is called without arguments. It should return nil if it is
392 definitely not mixed; non-nil otherwise. The hook will be run until
393 one of the functions returns nil.")
395 (defvar nxml-mixed-scan-distance
4000
396 "Maximum distance from point to scan when checking for mixed content.")
398 (defvar nxml-end-tag-indent-scan-distance
4000
399 "Maximum distance from point to scan backwards when indenting end-tag.")
401 (defvar nxml-char-ref-extra-display t
402 "Non-nil means display extra information for character references.
403 The extra information consists of a tooltip with the character name
404 and, if `nxml-char-ref-display-glyph-flag' is non-nil, a glyph
405 corresponding to the referenced character following the character
407 (make-variable-buffer-local 'nxml-char-ref-extra-display
)
409 (defvar nxml-mode-map
410 (let ((map (make-sparse-keymap)))
411 (define-key map
"\M-\C-u" 'nxml-backward-up-element
)
412 (define-key map
"\M-\C-d" 'nxml-down-element
)
413 (define-key map
"\M-\C-n" 'nxml-forward-element
)
414 (define-key map
"\M-\C-p" 'nxml-backward-element
)
415 (define-key map
"\M-{" 'nxml-backward-paragraph
)
416 (define-key map
"\M-}" 'nxml-forward-paragraph
)
417 (define-key map
"\M-h" 'nxml-mark-paragraph
)
418 (define-key map
"\C-c\C-f" 'nxml-finish-element
)
419 (define-key map
"\C-c\C-m" 'nxml-split-element
)
420 (define-key map
"\C-c\C-b" 'nxml-balanced-close-start-tag-block
)
421 (define-key map
"\C-c\C-i" 'nxml-balanced-close-start-tag-inline
)
422 (define-key map
"\C-c\C-x" 'nxml-insert-xml-declaration
)
423 (define-key map
"\C-c\C-d" 'nxml-dynamic-markup-word
)
425 (define-key map
"\C-c\C-u" 'nxml-insert-named-char
)
426 (define-key map
"\C-c\C-o" nxml-outline-prefix-map
)
427 (define-key map
[S-mouse-2
] 'nxml-mouse-hide-direct-text-content
)
428 (define-key map
"/" 'nxml-electric-slash
)
429 (define-key map
[C-return
] 'nxml-complete
)
430 (when nxml-bind-meta-tab-to-complete-flag
431 (define-key map
"\M-\t" 'nxml-complete
))
433 "Keymap for nxml-mode.")
435 (defsubst nxml-set-face
(start end face
)
436 (when (and face
(< start end
))
437 (put-text-property start end
'face face
)))
439 (defun nxml-clear-face (start end
)
440 (remove-text-properties start end
'(face nil
))
441 (nxml-clear-char-ref-extra-display start end
))
443 (defsubst nxml-set-fontified
(start end
)
444 (put-text-property start end
'fontified t
))
446 (defsubst nxml-clear-fontified
(start end
)
447 (remove-text-properties start end
'(fontified nil
)))
451 ;; We use C-c C-i instead of \\[nxml-balanced-close-start-tag-inline]
452 ;; because Emacs turns C-c C-i into C-c TAB which is hard to type and
454 "Major mode for editing XML.
456 Syntax highlighting is performed unless the variable
457 `nxml-syntax-highlight-flag' is nil.
459 \\[nxml-finish-element] finishes the current element by inserting an end-tag.
460 C-c C-i closes a start-tag with `>' and then inserts a balancing end-tag
461 leaving point between the start-tag and end-tag.
462 \\[nxml-balanced-close-start-tag-block] is similar but for block rather than inline elements:
463 the start-tag, point, and end-tag are all left on separate lines.
464 If `nxml-slash-auto-complete-flag' is non-nil, then inserting a `</'
465 automatically inserts the rest of the end-tag.
467 \\[nxml-complete] performs completion on the symbol preceding point.
469 \\[nxml-dynamic-markup-word] uses the contents of the current buffer
470 to choose a tag to put around the word preceding point.
472 Sections of the document can be displayed in outline form. The
473 variable `nxml-section-element-name-regexp' controls when an element
474 is recognized as a section. The same key sequences that change
475 visibility in outline mode are used except that they start with C-c C-o
478 Validation is provided by the related minor-mode `rng-validate-mode'.
479 This also makes completion schema- and context- sensitive. Element
480 names, attribute names, attribute values and namespace URIs can all be
481 completed. By default, `rng-validate-mode' is automatically enabled. You
482 can toggle it using \\[rng-validate-mode] or change the default by
483 customizing `rng-nxml-auto-validate-flag'.
485 \\[indent-for-tab-command] indents the current line appropriately.
486 This can be customized using the variable `nxml-child-indent'
487 and the variable `nxml-attribute-indent'.
489 \\[nxml-insert-named-char] inserts a character reference using
490 the character's name (by default, the Unicode name). \\[universal-argument] \\[nxml-insert-named-char]
491 inserts the character directly.
493 The Emacs commands that normally operate on balanced expressions will
494 operate on XML markup items. Thus \\[forward-sexp] will move forward
495 across one markup item; \\[backward-sexp] will move backward across
496 one markup item; \\[kill-sexp] will kill the following markup item;
497 \\[mark-sexp] will mark the following markup item. By default, each
498 tag each treated as a single markup item; to make the complete element
499 be treated as a single markup item, set the variable
500 `nxml-sexp-element-flag' to t. For more details, see the function
501 `nxml-forward-balanced-item'.
503 \\[nxml-backward-up-element] and \\[nxml-down-element] move up and down the element structure.
505 Many aspects this mode can be customized using
506 \\[customize-group] nxml RET."
508 (kill-all-local-variables)
509 (setq major-mode
'nxml-mode
)
510 (setq mode-name
"nXML")
511 (set (make-local-variable 'mode-line-process
) '((nxml-degraded "/degraded")))
512 ;; We'll determine the fill prefix ourselves
513 (make-local-variable 'adaptive-fill-mode
)
514 (setq adaptive-fill-mode nil
)
515 (make-local-variable 'forward-sexp-function
)
516 (setq forward-sexp-function
'nxml-forward-balanced-item
)
517 (make-local-variable 'indent-line-function
)
518 (setq indent-line-function
'nxml-indent-line
)
519 (make-local-variable 'fill-paragraph-function
)
520 (setq fill-paragraph-function
'nxml-do-fill-paragraph
)
522 ;; This doesn't seem to work too well;
523 ;; I think we should probably roll our own nxml-comment-dwim function.
524 (make-local-variable 'comment-indent-function
)
525 (setq comment-indent-function
'nxml-indent-line
)
526 (make-local-variable 'comment-start
)
527 (setq comment-start
"<!--")
528 (make-local-variable 'comment-start-skip
)
529 (setq comment-start-skip
"<!--[ \t\r\n]*")
530 (make-local-variable 'comment-end
)
531 (setq comment-end
"-->")
532 (make-local-variable 'comment-end-skip
)
533 (setq comment-end-skip
"[ \t\r\n]*-->")
534 (make-local-variable 'comment-line-break-function
)
535 (setq comment-line-break-function
'nxml-newline-and-indent
)
536 (use-local-map nxml-mode-map
)
540 (nxml-clear-dependent-regions (point-min) (point-max))
541 (setq nxml-scan-end
(copy-marker (point-min) nil
))
542 (nxml-with-unmodifying-text-property-changes
543 (when nxml-syntax-highlight-flag
544 (nxml-clear-fontified (point-min) (point-max)))
545 (nxml-clear-inside (point-min) (point-max))
546 (nxml-with-invisible-motion
547 (nxml-scan-prolog)))))
548 (when nxml-syntax-highlight-flag
549 (add-hook 'fontification-functions
'nxml-fontify nil t
))
550 (add-hook 'after-change-functions
'nxml-after-change nil t
)
551 (add-hook 'change-major-mode-hook
'nxml-cleanup nil t
)
553 ;; Emacs 23 handles the encoding attribute on the xml declaration
554 ;; transparently to nxml-mode, so there is no longer a need for the below
555 ;; hook. The hook also had the drawback of overriding explicit user
556 ;; instruction to save as some encoding other than utf-8.
557 ;;; (add-hook 'write-contents-hooks 'nxml-prepare-to-save)
558 (when (not (and (buffer-file-name) (file-exists-p (buffer-file-name))))
559 (when (and nxml-default-buffer-file-coding-system
560 (not (local-variable-p 'buffer-file-coding-system
)))
561 (setq buffer-file-coding-system nxml-default-buffer-file-coding-system
))
562 (when nxml-auto-insert-xml-declaration-flag
563 (nxml-insert-xml-declaration)))
565 (nxml-enable-unicode-char-name-sets)
566 (run-hooks 'nxml-mode-hook
))
568 (defun nxml-cleanup ()
569 "Clean up after nxml-mode."
570 ;; Disable associated minor modes.
571 (rng-validate-mode -
1)
572 ;; Clean up fontification.
575 (let ((inhibit-read-only t
)
577 (modified (buffer-modified-p)))
578 (nxml-with-invisible-motion
579 (remove-text-properties (point-min) (point-max) '(face)))
580 (set-buffer-modified-p modified
)))
581 (remove-hook 'change-major-mode-hook
'nxml-cleanup t
))
583 (defun nxml-degrade (context err
)
584 (message "Internal nXML mode error in %s (%s), degrading"
586 (error-message-string err
))
588 (setq nxml-degraded t
)
589 (setq nxml-prolog-end
1)
593 (nxml-with-unmodifying-text-property-changes
594 (nxml-clear-face (point-min) (point-max))
595 (nxml-set-fontified (point-min) (point-max))
596 (nxml-clear-inside (point-min) (point-max))))))
598 ;;; Change management
600 (defun nxml-after-change (start end pre-change-length
)
601 ;; Work around bug in insert-file-contents.
602 (when (> end
(1+ (buffer-size)))
604 (setq end
(1+ (buffer-size))))
605 (unless nxml-degraded
611 (nxml-with-invisible-motion
612 (nxml-with-unmodifying-text-property-changes
613 (nxml-after-change1 start end pre-change-length
))))))
615 (nxml-degrade 'nxml-after-change err
)))))
617 (defun nxml-after-change1 (start end pre-change-length
)
618 (setq nxml-last-fontify-end nil
)
619 (let ((pre-change-end (+ start pre-change-length
)))
621 (nxml-adjust-start-for-dependent-regions start
625 ;; Add 2 so as to include the < and following char
626 ;; that start the instance, since changing these
627 ;; 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
630 (when (< pre-change-end nxml-prolog-end
)
632 ;; don't let end get out of range even if pre-change-length
635 (+ end
(- nxml-prolog-end pre-change-end
)))))
637 (cond ((<= end nxml-prolog-end
)
638 (setq end nxml-prolog-end
)
640 ;; This is so that Emacs redisplay works
641 (setq start
(line-beginning-position)))
642 ((and (<= start nxml-scan-end
)
643 (> start
(point-min))
644 (nxml-get-inside (1- start
)))
645 ;; The closing delimiter might have been removed.
646 ;; So we may need to redisplay from the beginning
648 (goto-char (1- start
))
649 (nxml-move-outside-backwards)
650 ;; This is so that Emacs redisplay works
651 (setq start
(line-beginning-position))
652 (setq end
(max (nxml-scan-after-change (point) end
)
656 ;; This is both for redisplay and to move back
657 ;; past any incomplete opening delimiters
658 (setq start
(line-beginning-position))
659 (setq end
(max (nxml-scan-after-change start end
)
661 (when nxml-syntax-highlight-flag
663 ;; Must clear at least one char so as to trigger redisplay.
664 (cond ((< start
(point-max))
665 (setq end
(1+ start
)))
667 (setq end
(point-max))
669 (setq start
(line-beginning-position)))))
670 (nxml-clear-fontified start end
)))
674 (defun nxml-insert-xml-declaration ()
675 "Insert an XML declaration at the beginning of buffer.
676 The XML declaration will declare an encoding depending on the buffer's
677 `buffer-file-coding-system'."
680 (if (and buffer-file-coding-system
681 (coding-system-p buffer-file-coding-system
)
682 (coding-system-get buffer-file-coding-system
684 buffer-file-coding-system
685 (nxml-choose-utf-coding-system))))
686 (goto-char (point-min))
687 (insert (format "<?xml version=\"1.0\" encoding=\"%s\"?>\n"
688 (nxml-coding-system-name coding-system
)))))
690 (defun nxml-prepare-to-save ()
691 (unless (and (not enable-multibyte-characters
)
692 (local-variable-p 'buffer-file-coding-system
)
693 buffer-file-coding-system
694 (or (eq (coding-system-type buffer-file-coding-system
) 5)
695 (eq buffer-file-coding-system
'no-conversion
)))
697 (setq buffer-file-coding-system
(nxml-select-coding-system))))
698 ;; nil from a function in `write-contents-hooks' means
699 ;; to continue and write the file as normal
702 (defun nxml-select-coding-system ()
703 (let* ((suitable-coding-systems
704 (find-coding-systems-region (point-min) (point-max)))
706 (goto-char (point-min))
707 (xmltok-get-declared-encoding-position)))
710 (buffer-substring-no-properties (car enc-pos
)
714 (if (string= (downcase enc-name
) "utf-16")
715 (nxml-choose-utf-16-coding-system)
716 (nxml-mime-charset-coding-system enc-name
)))
717 (enc-pos (nxml-choose-utf-coding-system)))))
718 ;; Make sure we have a coding-system
719 (unless coding-system
721 (and (not buffer-read-only
)
722 (nxml-choose-suitable-coding-system
723 suitable-coding-systems
)))
726 (format "Unknown encoding %s" enc-name
)
727 "XML declaration is not well-formed")))
728 (cond ((not coding-system
)
729 (error "%s" message
))
735 "Modify and save with encoding %s")
736 (nxml-coding-system-name coding-system
))
738 (nxml-fix-encoding-declaration enc-pos coding-system
))
739 (t (signal 'quit nil
)))))
740 ;; Make sure it can encode all the characters in the buffer
741 (unless (or (memq (coding-system-base coding-system
)
742 suitable-coding-systems
)
743 (equal suitable-coding-systems
'(undecided)))
745 (nxml-unsuitable-coding-system-message coding-system
748 (and (not buffer-read-only
)
749 (nxml-choose-suitable-coding-system
750 suitable-coding-systems
)))
751 (cond ((not coding-system
) (error "%s" message
))
752 ((y-or-n-p (concat message
753 (format ". Save with %s "
754 (nxml-coding-system-name
756 (nxml-fix-encoding-declaration enc-pos coding-system
))
757 (t (signal 'quit nil
)))))
758 ;; Merge the newline type of our existing encoding
759 (let ((current-eol-type
760 (coding-system-eol-type buffer-file-coding-system
)))
761 (when (and current-eol-type
(integerp current-eol-type
))
763 (coding-system-change-eol-conversion coding-system
767 (defun nxml-unsuitable-coding-system-message (coding-system &optional enc-name
)
768 (if (nxml-coding-system-unicode-p coding-system
)
769 "Cannot translate some characters to Unicode"
770 (format "Cannot encode some characters with %s"
772 (nxml-coding-system-name coding-system
)))))
774 (defconst nxml-utf-16-coding-systems
(and (coding-system-p 'utf-16-be
)
775 (coding-system-p 'utf-16-le
)
776 '(utf-16-be utf-16-le
)))
778 (defconst nxml-utf-coding-systems
(cons 'utf-8 nxml-utf-16-coding-systems
))
780 (defun nxml-coding-system-unicode-p (coding-system)
781 (nxml-coding-system-member (coding-system-base coding-system
)
782 nxml-utf-coding-systems
))
784 (defun nxml-coding-system-name (coding-system)
785 (setq coding-system
(coding-system-base coding-system
))
787 (if (nxml-coding-system-member coding-system nxml-utf-16-coding-systems
)
789 (or (coding-system-get coding-system
'mime-charset
)
792 (defun nxml-fix-encoding-declaration (enc-pos coding-system
)
793 (let ((charset (nxml-coding-system-name coding-system
)))
794 (cond ((consp enc-pos
)
795 (delete-region (car enc-pos
) (cdr enc-pos
))
796 (goto-char (car enc-pos
))
800 (insert " encoding=\"" charset ?
\"))
802 (goto-char (point-min))
803 (insert "<?xml version=\"1.0\" encoding=\""
806 (when (and (not enc-pos
)
807 (let ((case-fold-search t
))
808 (looking-at xmltok-bad-xml-decl-regexp
)))
809 (delete-region (point) (match-end 0)))))))
811 (defun nxml-choose-suitable-coding-system (suitable-coding-systems)
812 (let (ret coding-system
)
813 (if (and buffer-file-coding-system
814 (memq (coding-system-base buffer-file-coding-system
)
815 suitable-coding-systems
))
816 buffer-file-coding-system
817 (while (and suitable-coding-systems
(not ret
))
818 (setq coding-system
(car suitable-coding-systems
))
819 (if (coding-system-get coding-system
'mime-charset
)
820 (setq ret coding-system
)
821 (setq suitable-coding-systems
(cdr suitable-coding-systems
))))
824 (defun nxml-choose-utf-coding-system ()
825 (let ((cur (and (local-variable-p 'buffer-file-coding-system
)
826 buffer-file-coding-system
827 (coding-system-base buffer-file-coding-system
))))
828 (cond ((car (nxml-coding-system-member cur nxml-utf-coding-systems
)))
829 ((and nxml-prefer-utf-16-to-utf-8-flag
830 (coding-system-p 'utf-16-le
)
831 (coding-system-p 'utf-16-be
))
832 (if nxml-prefer-utf-16-little-to-big-endian-flag
837 (defun nxml-choose-utf-16-coding-system ()
838 (let ((cur (and (local-variable-p 'buffer-file-coding-system
)
839 buffer-file-coding-system
840 (coding-system-base buffer-file-coding-system
))))
841 (cond ((car (nxml-coding-system-member cur nxml-utf-16-coding-systems
)))
842 (nxml-prefer-utf-16-little-to-big-endian-flag
843 (and (coding-system-p 'utf-16-le
) 'utf-16-le
))
844 (t (and (coding-system-p 'utf-16-be
) 'utf-16-be
)))))
846 (defun nxml-coding-system-member (coding-system coding-systems
)
848 (while (and coding-systems
(not ret
))
849 (if (coding-system-equal coding-system
850 (car coding-systems
))
851 (setq ret coding-systems
)
852 (setq coding-systems
(cdr coding-systems
))))
857 (defun nxml-fontify (start)
863 (nxml-with-invisible-motion
864 (nxml-with-unmodifying-text-property-changes
865 (if (or nxml-degraded
866 ;; just in case we get called in the wrong buffer
867 (not nxml-prolog-end
))
868 (nxml-set-fontified start
(point-max))
869 (nxml-fontify1 start
)))))))
871 (nxml-degrade 'nxml-fontify err
))))
873 (defun nxml-fontify1 (start)
874 (cond ((< start nxml-prolog-end
)
875 (nxml-fontify-prolog)
876 (nxml-set-fontified (point-min)
880 (when (not (eq nxml-last-fontify-end start
))
881 (when (not (equal (char-after) ?\
<))
882 (search-backward "<" nxml-prolog-end t
))
883 (nxml-ensure-scan-up-to-date)
884 (nxml-move-outside-backwards))
885 (let ((start (point)))
886 (nxml-do-fontify (min (point-max)
887 (+ start nxml-fontify-chunk-size
)))
888 (setq nxml-last-fontify-end
(point))
889 (nxml-set-fontified start nxml-last-fontify-end
)))))
891 (defun nxml-fontify-buffer ()
896 (nxml-with-invisible-motion
897 (goto-char (point-min))
898 (nxml-with-unmodifying-text-property-changes
899 (nxml-fontify-prolog)
900 (goto-char nxml-prolog-end
)
901 (nxml-do-fontify))))))
903 (defun nxml-fontify-prolog ()
905 The buffer is assumed to be prepared for fontification.
906 This does not set the fontified property, but it does clear
907 faces appropriately."
908 (let ((regions nxml-prolog-regions
))
909 (nxml-clear-face (point-min) nxml-prolog-end
)
911 (let ((region (car regions
)))
912 (nxml-apply-fontify-rule (aref region
0)
915 (setq regions
(cdr regions
)))))
917 (defun nxml-do-fontify (&optional bound
)
918 "Fontify at least as far as bound.
919 Leave point after last fontified position."
920 (unless bound
(setq bound
(point-max)))
921 (let (xmltok-dependent-regions
923 (while (and (< (point) bound
)
924 (nxml-tokenize-forward))
925 (nxml-clear-face xmltok-start
(point))
926 (nxml-apply-fontify-rule))))
928 ;; Vectors identify a substring of the token to be highlighted in some face.
930 ;; Token types returned by xmltok-forward.
934 '([nil
1 nxml-tag-delimiter
]
935 [-
1 nil nxml-tag-delimiter
]
939 (put 'partial-start-tag
941 '([nil
1 nxml-tag-delimiter
]
947 '([nil
1 nxml-tag-delimiter
]
949 [-
1 nil nxml-tag-delimiter
]
950 (element-qname .
2)))
952 (put 'partial-end-tag
954 '([nil
1 nxml-tag-delimiter
]
956 (element-qname .
2)))
960 '([nil
1 nxml-tag-delimiter
]
961 [-
2 -
1 nxml-tag-slash
]
962 [-
1 nil nxml-tag-delimiter
]
966 (put 'partial-empty-element
968 '([nil
1 nxml-tag-delimiter
]
969 [-
1 nil nxml-tag-slash
]
975 '([nil
2 nxml-char-ref-delimiter
]
976 [2 -
1 nxml-char-ref-number
]
977 [-
1 nil nxml-char-ref-delimiter
]
982 '([nil
1 nxml-entity-ref-delimiter
]
983 [1 -
1 nxml-entity-ref-name
]
984 [-
1 nil nxml-entity-ref-delimiter
]))
988 '([nil
4 nxml-comment-delimiter
]
989 [4 -
3 nxml-comment-content
]
990 [-
3 nil nxml-comment-delimiter
]))
992 (put 'processing-instruction
994 '([nil
2 nxml-processing-instruction-delimiter
]
995 [-
2 nil nxml-processing-instruction-delimiter
]
996 processing-instruction-content
))
1000 '([nil
3 nxml-cdata-section-delimiter
] ; <![
1001 [3 8 nxml-cdata-section-CDATA
] ; CDATA
1002 [8 9 nxml-cdata-section-delimiter
] ; [
1003 [9 -
3 nxml-cdata-section-content
] ; ]]>
1004 [-
3 nil nxml-cdata-section-delimiter
]))
1008 '([nil nil nxml-text
]))
1010 ;; Prolog region types in list returned by xmltok-forward-prolog.
1012 (put 'xml-declaration
1014 '([nil
2 nxml-processing-instruction-delimiter
]
1015 [2 5 nxml-processing-instruction-target
]
1016 [-
2 nil nxml-processing-instruction-delimiter
]))
1018 (put 'xml-declaration-attribute-name
1020 '([nil nil nxml-attribute-local-name
]))
1022 (put 'xml-declaration-attribute-value
1024 '([nil
1 nxml-attribute-value-delimiter
]
1025 [1 -
1 nxml-attribute-value
]
1026 [-
1 nil nxml-attribute-value-delimiter
]))
1028 (put 'processing-instruction-left
1030 '([nil
2 nxml-processing-instruction-delimiter
]
1031 [2 nil nxml-processing-instruction-target
]))
1033 (put 'processing-instruction-right
1035 '([nil -
2 nxml-processing-instruction-content
]
1036 [-
2 nil nxml-processing-instruction-delimiter
]))
1040 '([nil
1 nxml-prolog-literal-delimiter
]
1041 [1 -
1 nxml-prolog-literal-content
]
1042 [-
1 nil nxml-prolog-literal-delimiter
]))
1046 '([nil nil nxml-prolog-keyword
]))
1048 (put 'markup-declaration-open
1050 '([0 2 nxml-markup-declaration-delimiter
]
1051 [2 nil nxml-prolog-keyword
]))
1053 (put 'markup-declaration-close
1055 '([nil nil nxml-markup-declaration-delimiter
]))
1057 (put 'internal-subset-open
1059 '([nil nil nxml-markup-declaration-delimiter
]))
1061 (put 'internal-subset-close
1063 '([nil
1 nxml-markup-declaration-delimiter
]
1064 [-
1 nil nxml-markup-declaration-delimiter
]))
1069 [1 nil nxml-prolog-keyword
]))
1071 (defun nxml-apply-fontify-rule (&optional type start end
)
1072 (let ((rule (get (or type xmltok-type
) 'nxml-fontify-rule
)))
1073 (unless start
(setq start xmltok-start
))
1074 (unless end
(setq end
(point)))
1076 (let* ((action (car rule
)))
1077 (setq rule
(cdr rule
))
1078 (cond ((vectorp action
)
1079 (nxml-set-face (let ((offset (aref action
0)))
1080 (cond ((not offset
) start
)
1081 ((< offset
0) (+ end offset
))
1082 (t (+ start offset
))))
1083 (let ((offset (aref action
1)))
1084 (cond ((not offset
) end
)
1085 ((< offset
0) (+ end offset
))
1086 (t (+ start offset
))))
1088 ((and (consp action
)
1089 (eq (car action
) 'element-qname
))
1090 (when xmltok-name-end
; maybe nil in partial-end-tag case
1091 (nxml-fontify-qname (+ start
(cdr action
))
1094 'nxml-element-prefix
1096 'nxml-element-local-name
)))
1097 ((eq action
'attributes
)
1098 (nxml-fontify-attributes))
1099 ((eq action
'processing-instruction-content
)
1100 (nxml-set-face (+ start
2)
1102 'nxml-processing-instruction-target
)
1103 (nxml-set-face (save-excursion
1104 (goto-char xmltok-name-end
)
1105 (skip-chars-forward " \t\r\n")
1108 'nxml-processing-instruction-content
))
1109 ((eq action
'char-ref
)
1110 (nxml-char-ref-display-extra start
1112 (xmltok-char-number start end
)))
1113 (t (error "Invalid nxml-fontify-rule action %s" action
)))))))
1115 (defun nxml-fontify-attributes ()
1116 (while xmltok-namespace-attributes
1117 (nxml-fontify-attribute (car xmltok-namespace-attributes
)
1119 (setq xmltok-namespace-attributes
1120 (cdr xmltok-namespace-attributes
)))
1121 (while xmltok-attributes
1122 (nxml-fontify-attribute (car xmltok-attributes
))
1123 (setq xmltok-attributes
1124 (cdr xmltok-attributes
))))
1126 (defun nxml-fontify-attribute (att &optional namespace-declaration
)
1127 (if namespace-declaration
1128 (nxml-fontify-qname (xmltok-attribute-name-start att
)
1129 (xmltok-attribute-name-colon att
)
1130 (xmltok-attribute-name-end att
)
1131 'nxml-namespace-attribute-xmlns
1132 'nxml-namespace-attribute-colon
1133 'nxml-namespace-attribute-prefix
1134 'nxml-namespace-attribute-xmlns
)
1135 (nxml-fontify-qname (xmltok-attribute-name-start att
)
1136 (xmltok-attribute-name-colon att
)
1137 (xmltok-attribute-name-end att
)
1138 'nxml-attribute-prefix
1139 'nxml-attribute-colon
1140 'nxml-attribute-local-name
))
1141 (let ((start (xmltok-attribute-value-start att
))
1142 (end (xmltok-attribute-value-end att
))
1143 (refs (xmltok-attribute-refs att
))
1144 (delimiter-face (if namespace-declaration
1145 'nxml-namespace-attribute-value-delimiter
1146 'nxml-attribute-value-delimiter
))
1147 (value-face (if namespace-declaration
1148 'nxml-namespace-attribute-value
1149 'nxml-attribute-value
)))
1151 (nxml-set-face (1- start
) start delimiter-face
)
1152 (nxml-set-face end
(1+ end
) delimiter-face
)
1154 (let* ((ref (car refs
))
1155 (ref-type (aref ref
0))
1156 (ref-start (aref ref
1))
1157 (ref-end (aref ref
2)))
1158 (nxml-set-face start ref-start value-face
)
1159 (nxml-apply-fontify-rule ref-type ref-start ref-end
)
1160 (setq start ref-end
))
1161 (setq refs
(cdr refs
)))
1162 (nxml-set-face start end value-face
))))
1164 (defun nxml-fontify-qname (start
1172 (cond (colon (nxml-set-face start colon prefix-face
)
1173 (nxml-set-face colon
(1+ colon
) colon-face
)
1174 (nxml-set-face (1+ colon
) end local-name-face
))
1175 (t (nxml-set-face start end
(or unprefixed-face
1176 local-name-face
)))))
1180 (defun nxml-electric-slash (arg)
1183 With a prefix ARG, do nothing other than insert the slash.
1185 Otherwise, if `nxml-slash-auto-complete-flag' is non-nil, insert the
1186 rest of the end-tag or empty-element if the slash is potentially part
1187 of an end-tag or the close of an empty-element.
1189 If the slash is part of an end-tag that is the first non-whitespace
1190 on the line, reindent the line."
1192 (nxml-ensure-scan-up-to-date)
1193 (let* ((slash-pos (point))
1194 (end-tag-p (and (eq (char-before slash-pos
) ?
<)
1195 (not (nxml-get-inside slash-pos
))))
1196 (at-indentation (save-excursion
1197 (back-to-indentation)
1198 (eq (point) (1- slash-pos
)))))
1199 (self-insert-command (prefix-numeric-value arg
))
1201 (if nxml-slash-auto-complete-flag
1204 (let ((start-tag-end
1205 (nxml-scan-element-backward (1- slash-pos
) t
)))
1207 (insert (xmltok-start-tag-qname) ">")
1208 ;; copy the indentation of the start-tag
1209 (when (and at-indentation
1211 (goto-char xmltok-start
)
1212 (back-to-indentation)
1213 (eq (point) xmltok-start
)))
1215 (indent-line-to (save-excursion
1216 (goto-char xmltok-start
)
1217 (current-column)))))))
1218 (nxml-scan-error nil
))
1219 (when (and (eq (nxml-token-before) (point))
1220 (eq xmltok-type
'partial-empty-element
))
1222 (when (and end-tag-p at-indentation
)
1223 (nxml-indent-line))))))
1225 (defun nxml-balanced-close-start-tag-block ()
1226 "Close the start-tag before point with `>' and insert a balancing end-tag.
1227 Point is left between the start-tag and the end-tag.
1228 If there is nothing but whitespace before the `<' that opens the
1229 start-tag, then put point on a blank line, and put the end-tag on
1230 another line aligned with the start-tag."
1232 (nxml-balanced-close-start-tag 'block
))
1234 (defun nxml-balanced-close-start-tag-inline ()
1235 "Close the start-tag before point with `>' and insert a balancing end-tag.
1236 Point is left between the start-tag and the end-tag.
1237 No extra whitespace is inserted."
1239 (nxml-balanced-close-start-tag 'inline
))
1241 (defun nxml-balanced-close-start-tag (block-or-inline)
1242 (let ((token-end (nxml-token-before))
1244 (unless (or (eq xmltok-type
'partial-start-tag
)
1245 (and (memq xmltok-type
'(start-tag
1247 partial-empty-element
))
1248 (>= token-end pos
)))
1249 (error "Not in a start-tag"))
1251 (buffer-substring-no-properties (+ xmltok-start
1)
1252 (min xmltok-name-end
(point)))
1254 (if (eq block-or-inline
'inline
)
1256 (goto-char xmltok-start
)
1257 (back-to-indentation)
1258 (if (= (point) xmltok-start
)
1259 (let ((indent (current-column)))
1262 (indent-line-to indent
)
1265 (indent-line-to (+ nxml-child-indent indent
)))
1268 (defun nxml-finish-element ()
1269 "Finish the current element by inserting an end-tag."
1271 (nxml-finish-element-1 nil
))
1273 (defvar nxml-last-split-position nil
1274 "Position where `nxml-split-element' split the current element.")
1276 (defun nxml-split-element ()
1277 "Split the current element by inserting an end-tag and a start-tag.
1278 Point is left after the newly inserted start-tag. When repeated,
1279 split immediately before the previously inserted start-tag and leave
1282 (setq nxml-last-split-position
1283 (if (and (eq last-command this-command
)
1284 nxml-last-split-position
)
1286 (goto-char nxml-last-split-position
)
1287 (nxml-finish-element-1 t
))
1288 (nxml-finish-element-1 t
))))
1290 (defun nxml-finish-element-1 (startp)
1291 "Insert an end-tag for the current element and optionally a start-tag.
1292 The start-tag is inserted if STARTP is non-nil. Return the position
1293 of the inserted start-tag or nil if none was inserted."
1295 (let* ((token-end (nxml-token-before))
1298 (when (and (< (point) token-end
)
1301 processing-instruction
1306 (error "Point is inside a %s"
1307 (nxml-token-type-friendly-name xmltok-type
)))
1308 (nxml-scan-element-backward token-end t
)))
1311 (unless (eq xmltok-type
'start-tag
)
1312 (error "No matching start-tag"))
1313 (goto-char xmltok-start
)
1314 (back-to-indentation)
1315 (eq (point) xmltok-start
)))
1318 (goto-char start-tag-end
)
1319 (looking-at "[ \t\r\n]*$")))
1320 (start-tag-indent (save-excursion
1321 (goto-char xmltok-start
)
1323 (qname (xmltok-start-tag-qname))
1324 inserted-start-tag-pos
)
1325 (when (and starts-line ends-line
)
1326 ;; start-tag is on a line by itself
1327 ;; => put the end-tag on a line by itself
1330 (back-to-indentation)
1333 (indent-line-to start-tag-indent
))
1334 (insert "</" qname
">")
1338 (indent-line-to start-tag-indent
))
1339 (setq inserted-start-tag-pos
(point))
1340 (insert "<" qname
">")
1341 (when (and starts-line ends-line
)
1343 (indent-line-to (save-excursion
1344 (goto-char xmltok-start
)
1346 (back-to-indentation)
1347 (if (= (current-column)
1348 (+ start-tag-indent nxml-child-indent
))
1349 (+ start-tag-indent nxml-child-indent
)
1350 start-tag-indent
)))))
1351 inserted-start-tag-pos
))
1355 (defun nxml-indent-line ()
1356 "Indent current line as XML."
1357 (let ((indent (nxml-compute-indent))
1358 (from-end (- (point-max) (point))))
1361 (let ((bol (point)))
1362 (skip-chars-forward " \t")
1363 (delete-region bol
(point)))
1365 (when (> (- (point-max) from-end
) (point))
1366 (goto-char (- (point-max) from-end
))))))
1368 (defun nxml-compute-indent ()
1369 "Return the indent for the line containing point."
1370 (or (nxml-compute-indent-from-matching-start-tag)
1371 (nxml-compute-indent-from-previous-line)))
1373 (defun nxml-compute-indent-from-matching-start-tag ()
1374 "Compute the indent for a line with an end-tag using the matching start-tag.
1375 When the line containing point ends with an end-tag and does not start
1376 in the middle of a token, return the indent of the line containing the
1377 matching start-tag, if there is one and it occurs at the beginning of
1378 its line. Otherwise return nil."
1380 (back-to-indentation)
1381 (let ((bol (point)))
1382 (let ((inhibit-field-text-motion t
))
1384 (skip-chars-backward " \t")
1385 (and (= (nxml-token-before) (point))
1386 (memq xmltok-type
'(end-tag partial-end-tag
))
1387 ;; start of line must not be inside a token
1388 (or (= xmltok-start bol
)
1392 (= xmltok-start bol
))
1393 (eq xmltok-type
'data
))
1395 (nxml-scan-element-backward
1399 nxml-end-tag-indent-scan-distance
))
1400 (nxml-scan-error nil
))
1401 (< xmltok-start bol
)
1403 (goto-char xmltok-start
)
1404 (skip-chars-backward " \t")
1406 (current-indentation)))))
1408 (defun nxml-compute-indent-from-previous-line ()
1409 "Compute the indent for a line using the indentation of a previous line."
1414 before-context after-context
)
1415 (back-to-indentation)
1418 ;; Move backwards until the start of a non-blank line that is
1419 ;; not inside a token.
1421 (when (= (forward-line -
1) -
1)
1423 (back-to-indentation)
1424 (if (looking-at "[ \t]*$")
1427 (setq prev-bol
(point)))
1429 (not (or (= xmltok-start
(point))
1430 (eq xmltok-type
'data
))))))
1432 ;; Now scan over tokens until the end of the line to be indented.
1433 ;; Determine the context before and after the beginning of the
1435 (while (< (point) eol
)
1436 (nxml-tokenize-forward)
1437 (cond ((<= bol xmltok-start
)
1439 (nxml-merge-indent-context-type after-context
)))
1440 ((and (<= (point) bol
)
1441 (not (and (eq xmltok-type
'partial-start-tag
)
1443 (setq before-context
1444 (nxml-merge-indent-context-type before-context
)))
1445 ((eq xmltok-type
'data
)
1446 (setq before-context
1447 (nxml-merge-indent-context-type before-context
))
1449 (nxml-merge-indent-context-type after-context
)))
1450 ;; If in the middle of a token that looks inline,
1451 ;; then indent relative to the previous non-blank line
1452 ((eq (nxml-merge-indent-context-type before-context
)
1454 (goto-char prev-bol
)
1455 (throw 'indent
(current-column)))
1458 (nxml-compute-indent-in-token bol
))))
1459 (skip-chars-forward " \t\r\n"))
1462 (* nxml-child-indent
1463 (+ (if (eq before-context
'start-tag
) 1 0)
1464 (if (eq after-context
'end-tag
) -
1 0))))))))
1466 (defun nxml-merge-indent-context-type (context)
1467 "Merge the indent context type CONTEXT with the token in `xmltok-type'.
1468 Return the merged indent context type. An indent context type is
1469 either nil or one of the symbols start-tag, end-tag, markup, comment,
1471 (cond ((memq xmltok-type
'(start-tag partial-start-tag
))
1472 (if (memq context
'(nil start-tag comment
))
1475 ((memq xmltok-type
'(end-tag partial-end-tag
))
1476 (if (memq context
'(nil end-tag comment
))
1479 ((eq xmltok-type
'comment
)
1480 (cond ((memq context
'(start-tag end-tag comment
))
1487 (defun nxml-compute-indent-in-token (pos)
1488 "Return the indent for a line that starts inside a token.
1489 POS is the position of the first non-whitespace character of the line.
1490 This expects the xmltok-* variables to be set up as by `xmltok-forward'."
1491 (cond ((memq xmltok-type
'(start-tag
1494 partial-empty-element
))
1495 (nxml-compute-indent-in-start-tag pos
))
1496 ((eq xmltok-type
'comment
)
1497 (nxml-compute-indent-in-delimited-token pos
"<!--" "-->"))
1498 ((eq xmltok-type
'cdata-section
)
1499 (nxml-compute-indent-in-delimited-token pos
"<![CDATA[" "]]>"))
1500 ((eq xmltok-type
'processing-instruction
)
1501 (nxml-compute-indent-in-delimited-token pos
"<?" "?>"))
1504 (if (and (= (forward-line -
1) 0)
1505 (< xmltok-start
(point)))
1506 (back-to-indentation)
1507 (goto-char xmltok-start
))
1510 (defun nxml-compute-indent-in-start-tag (pos)
1511 "Return the indent for a line that starts inside a start-tag.
1512 Also for a line that starts inside an empty element.
1513 POS is the position of the first non-whitespace character of the line.
1514 This expects the xmltok-* variables to be set up as by `xmltok-forward'."
1515 (let ((value-boundary (nxml-attribute-value-boundary pos
))
1518 ;; inside an attribute value
1519 (let ((value-start (car value-boundary
))
1520 (value-end (cdr value-boundary
)))
1523 (if (< (point) value-start
)
1524 (goto-char value-start
)
1525 (back-to-indentation)))
1526 ;; outside an attribute value
1528 (while (and (= (forward-line -
1) 0)
1529 (nxml-attribute-value-boundary (point))))
1530 (cond ((<= (point) xmltok-start
)
1531 (goto-char xmltok-start
)
1532 (setq off nxml-attribute-indent
)
1533 (let ((atts (xmltok-merge-attributes)))
1535 (let* ((att (car atts
))
1536 (start (xmltok-attribute-name-start att
)))
1541 (back-to-indentation))))
1542 (+ (current-column) off
)))
1544 (defun nxml-attribute-value-boundary (pos)
1545 "Return a pair (START . END) if POS is inside an attribute value.
1546 Otherwise return nil. START and END are the positions of the start
1547 and end of the attribute value containing POS. This expects the
1548 xmltok-* variables to be set up as by `xmltok-forward'."
1549 (let ((atts (xmltok-merge-attributes))
1550 att value-start value-end value-boundary
)
1552 (setq att
(car atts
))
1553 (setq value-start
(xmltok-attribute-value-start att
))
1554 (setq value-end
(xmltok-attribute-value-end att
))
1555 (cond ((and value-start
(< pos value-start
))
1557 ((and value-start value-end
(<= pos value-end
))
1558 (setq value-boundary
(cons value-start value-end
))
1560 (t (setq atts
(cdr atts
)))))
1563 (defun nxml-compute-indent-in-delimited-token (pos open-delim close-delim
)
1564 "Return the indent for a line that starts inside a token with delimiters.
1565 OPEN-DELIM and CLOSE-DELIM are strings giving the opening and closing
1566 delimiters. POS is the position of the first non-whitespace character
1567 of the line. This expects the xmltok-* variables to be set up as by
1569 (cond ((let ((end (+ pos
(length close-delim
))))
1570 (and (<= end
(point-max))
1571 (string= (buffer-substring-no-properties pos end
)
1573 (goto-char xmltok-start
))
1577 (<= (point) xmltok-start
))
1578 (goto-char (+ xmltok-start
(length open-delim
)))
1579 (when (and (string= open-delim
"<!--")
1581 (goto-char (1+ (point)))))
1582 (t (back-to-indentation)))
1587 (defun nxml-complete ()
1588 "Perform completion on the symbol preceding point.
1590 Inserts as many characters as can be completed. However, if not even
1591 one character can be completed, then a buffer with the possibilities
1592 is popped up and the symbol is read from the minibuffer with
1593 completion. If the symbol is complete, then any characters that must
1594 follow the symbol are also inserted.
1596 The name space used for completion and what is treated as a symbol
1597 depends on the context. The contexts in which completion is performed
1598 depend on `nxml-completion-hook'."
1600 (unless (run-hook-with-args-until-success 'nxml-completion-hook
)
1601 ;; Eventually we will complete on entity names here.
1603 (message "Cannot complete in this context")))
1607 (defun nxml-forward-balanced-item (&optional arg
)
1608 "Move forward across one balanced item.
1609 With ARG, do it that many times. Negative arg -N means
1610 move backward across N balanced expressions.
1611 This is the equivalent of `forward-sexp' for XML.
1613 An element contains as items strings with no markup, tags, processing
1614 instructions, comments, CDATA sections, entity references and
1615 characters references. However, if the variable
1616 `nxml-sexp-element-flag' is non-nil, then an element is treated as a
1617 single markup item. A start-tag contains an element name followed by
1618 one or more attributes. An end-tag contains just an element name. An
1619 attribute value literals contains strings with no markup, entity
1620 references and character references. A processing instruction
1621 consists of a target and a content string. A comment or a CDATA
1622 section contains a single string. An entity reference contains a
1623 single name. A character reference contains a character number."
1625 (or arg
(setq arg
1))
1628 (nxml-forward-single-balanced-item)
1629 (> (setq arg
(1- arg
)) 0))))
1632 (nxml-backward-single-balanced-item)
1633 (< (setq arg
(1+ arg
)) 0))))))
1635 (defun nxml-forward-single-balanced-item ()
1637 (goto-char (let ((end (nxml-token-after)))
1639 (while (eq xmltok-type
'space
)
1641 (setq end
(nxml-token-after)))
1642 (cond ((/= (point) xmltok-start
)
1643 (nxml-scan-forward-within end
))
1644 ((and nxml-sexp-element-flag
1645 (eq xmltok-type
'start-tag
))
1646 ;; can't ever return nil here
1647 (nxml-scan-element-forward xmltok-start
))
1648 ((and nxml-sexp-element-flag
1650 '(end-tag partial-end-tag
)))
1651 (error "Already at end of element"))
1654 (goto-char (cadr err
))
1655 (apply 'error
(cddr err
)))))
1657 (defun nxml-backward-single-balanced-item ()
1659 (goto-char (let ((end (nxml-token-before)))
1661 (while (eq xmltok-type
'space
)
1662 (goto-char xmltok-start
)
1663 (setq end
(nxml-token-before)))
1664 (cond ((/= (point) end
)
1665 (nxml-scan-backward-within end
))
1666 ((and nxml-sexp-element-flag
1667 (eq xmltok-type
'end-tag
))
1668 ;; can't ever return nil here
1669 (nxml-scan-element-backward end
)
1671 ((and nxml-sexp-element-flag
1672 (eq xmltok-type
'start-tag
))
1673 (error "Already at start of element"))
1674 (t xmltok-start
)))))
1676 (goto-char (cadr err
))
1677 (apply 'error
(cddr err
)))))
1679 (defun nxml-scan-forward-within (end)
1680 (setq end
(- end
(nxml-end-delimiter-length xmltok-type
)))
1681 (when (<= end
(point))
1682 (error "Already at end of %s"
1683 (nxml-token-type-friendly-name xmltok-type
)))
1684 (cond ((memq xmltok-type
'(start-tag
1687 partial-empty-element
))
1688 (if (< (point) xmltok-name-end
)
1690 (let ((att (nxml-find-following-attribute)))
1691 (cond ((not att
) end
)
1692 ((and (xmltok-attribute-value-start att
)
1693 (<= (xmltok-attribute-value-start att
)
1695 (nxml-scan-forward-in-attribute-value att
))
1696 ((xmltok-attribute-value-end att
)
1697 (1+ (xmltok-attribute-value-end att
)))
1699 (goto-char (xmltok-attribute-name-end att
))
1700 (looking-at "[ \t\r\n]*="))
1702 (t (xmltok-attribute-name-end att
))))))
1703 ((and (eq xmltok-type
'processing-instruction
)
1704 (< (point) xmltok-name-end
))
1708 (defun nxml-scan-backward-within (end)
1711 (nxml-start-delimiter-length xmltok-type
)))
1712 (when (<= (point) xmltok-start
)
1713 (error "Already at start of %s"
1714 (nxml-token-type-friendly-name xmltok-type
)))
1715 (cond ((memq xmltok-type
'(start-tag
1718 partial-empty-element
))
1719 (let ((att (nxml-find-preceding-attribute)))
1720 (cond ((not att
) xmltok-start
)
1721 ((and (xmltok-attribute-value-start att
)
1722 (<= (xmltok-attribute-value-start att
)
1725 (xmltok-attribute-value-end att
)))
1726 (nxml-scan-backward-in-attribute-value att
))
1727 (t (xmltok-attribute-name-start att
)))))
1728 ((and (eq xmltok-type
'processing-instruction
)
1729 (let ((content-start (save-excursion
1730 (goto-char xmltok-name-end
)
1731 (skip-chars-forward " \r\t\n")
1733 (and (< content-start
(point))
1737 (defun nxml-scan-forward-in-attribute-value (att)
1738 (when (= (point) (xmltok-attribute-value-end att
))
1739 (error "Already at end of attribute value"))
1740 (let ((refs (xmltok-attribute-refs att
))
1743 (setq ref
(car refs
))
1744 (if (< (point) (aref ref
2))
1747 (setq refs
(cdr refs
))))
1749 (xmltok-attribute-value-end att
))
1750 ((< (point) (aref ref
1))
1752 ((= (point) (aref ref
1))
1755 (let ((end (- (aref ref
2)
1756 (nxml-end-delimiter-length (aref ref
0)))))
1759 (error "Already at end of %s"
1760 (nxml-token-type-friendly-name (aref ref
0)))))))))
1762 (defun nxml-scan-backward-in-attribute-value (att)
1763 (when (= (point) (xmltok-attribute-value-start att
))
1764 (error "Already at start of attribute value"))
1765 (let ((refs (reverse (xmltok-attribute-refs att
)))
1768 (setq ref
(car refs
))
1769 (if (< (aref ref
1) (point))
1772 (setq refs
(cdr refs
))))
1774 (xmltok-attribute-value-start att
))
1775 ((< (aref ref
2) (point))
1777 ((= (point) (aref ref
2))
1780 (let ((start (+ (aref ref
1)
1781 (nxml-start-delimiter-length (aref ref
0)))))
1782 (if (< start
(point))
1784 (error "Already at start of %s"
1785 (nxml-token-type-friendly-name (aref ref
0)))))))))
1787 (defun nxml-find-following-attribute ()
1789 (atts (or xmltok-attributes xmltok-namespace-attributes
))
1790 (more-atts (and xmltok-attributes xmltok-namespace-attributes
)))
1792 (let* ((att (car atts
))
1793 (name-start (xmltok-attribute-name-start att
)))
1794 (cond ((and (<= name-start
(point))
1795 (xmltok-attribute-value-end att
)
1796 ;; <= because end is before quote
1797 (<= (point) (xmltok-attribute-value-end att
)))
1800 ((and (< (point) name-start
)
1803 (xmltok-attribute-name-start ret
))))
1805 (setq atts
(cdr atts
))
1807 (setq atts more-atts
)
1808 (setq more-atts nil
)))
1811 (defun nxml-find-preceding-attribute ()
1813 (atts (or xmltok-attributes xmltok-namespace-attributes
))
1814 (more-atts (and xmltok-attributes xmltok-namespace-attributes
)))
1816 (let* ((att (car atts
))
1817 (name-start (xmltok-attribute-name-start att
)))
1818 (cond ((and (< name-start
(point))
1819 (xmltok-attribute-value-end att
)
1820 ;; <= because end is before quote
1821 (<= (point) (xmltok-attribute-value-end att
)))
1824 ((and (< name-start
(point))
1826 (< (xmltok-attribute-name-start ret
)
1829 (setq atts
(cdr atts
))
1831 (setq atts more-atts
)
1832 (setq more-atts nil
)))
1835 (defun nxml-up-element (&optional arg
)
1837 (or arg
(setq arg
1))
1839 (nxml-backward-up-element (- arg
))
1841 (while (and (> arg
0)
1842 (< (point) (point-max)))
1843 (let ((token-end (nxml-token-after)))
1844 (goto-char (cond ((or (memq xmltok-type
'(end-tag
1846 (and (memq xmltok-type
1848 partial-empty-element
))
1849 (< xmltok-start
(point))))
1851 ((nxml-scan-element-forward
1852 (if (and (eq xmltok-type
'start-tag
)
1853 (= (point) xmltok-start
))
1857 (t (error "No parent element")))))
1858 (setq arg
(1- arg
)))
1860 (goto-char (cadr err
))
1861 (apply 'error
(cddr err
))))))
1863 (defun nxml-backward-up-element (&optional arg
)
1865 (or arg
(setq arg
1))
1867 (nxml-up-element (- arg
))
1869 (while (and (> arg
0)
1870 (< (point-min) (point)))
1871 (let ((token-end (nxml-token-before)))
1872 (goto-char (cond ((or (memq xmltok-type
'(start-tag
1874 (and (memq xmltok-type
1876 partial-empty-element
))
1877 (< (point) token-end
)))
1879 ((nxml-scan-element-backward
1880 (if (and (eq xmltok-type
'end-tag
)
1881 (= (point) token-end
))
1886 (t (error "No parent element")))))
1887 (setq arg
(1- arg
)))
1889 (goto-char (cadr err
))
1890 (apply 'error
(cddr err
))))))
1892 (defun nxml-down-element (&optional arg
)
1893 "Move forward down into the content of an element.
1894 With ARG, do this that many times.
1895 Negative ARG means move backward but still down."
1897 (or arg
(setq arg
1))
1899 (nxml-backward-down-element (- arg
))
1902 (let ((token-end (nxml-token-after)))
1904 (goto-char token-end
)
1906 (when (memq xmltok-type
'(nil end-tag partial-end-tag
))
1907 (error "No following start-tags in this element"))
1908 (not (memq xmltok-type
'(start-tag partial-start-tag
))))
1909 (nxml-tokenize-forward))
1911 (setq arg
(1- arg
)))))
1913 (defun nxml-backward-down-element (&optional arg
)
1915 (or arg
(setq arg
1))
1917 (nxml-down-element (- arg
))
1922 (goto-char xmltok-start
)
1924 (when (memq xmltok-type
'(start-tag
1928 (error "No preceding end-tags in this element"))
1929 (not (memq xmltok-type
'(end-tag partial-end-tag
))))
1930 (if (or (<= (point) nxml-prolog-end
)
1931 (not (search-backward "<" nxml-prolog-end t
)))
1932 (setq xmltok-type nil
)
1933 (nxml-move-outside-backwards)
1936 (setq arg
(1- arg
)))))
1938 (defun nxml-forward-element (&optional arg
)
1939 "Move forward over one element.
1940 With ARG, do it that many times.
1941 Negative ARG means move backward."
1943 (or arg
(setq arg
1))
1945 (nxml-backward-element (- arg
))
1947 (while (and (> arg
0)
1948 (< (point) (point-max)))
1950 (or (nxml-scan-element-forward (nxml-token-before))
1951 (error "No more elements")))
1952 (setq arg
(1- arg
)))
1954 (goto-char (cadr err
))
1955 (apply 'error
(cddr err
))))))
1957 (defun nxml-backward-element (&optional arg
)
1958 "Move backward over one element.
1959 With ARG, do it that many times.
1960 Negative ARG means move forward."
1962 (or arg
(setq arg
1))
1964 (nxml-forward-element (- arg
))
1966 (while (and (> arg
0)
1967 (< (point-min) (point)))
1969 (or (and (nxml-scan-element-backward (progn
1973 (error "No preceding elements")))
1974 (setq arg
(1- arg
)))
1976 (goto-char (cadr err
))
1977 (apply 'error
(cddr err
))))))
1979 (defun nxml-mark-token-after ()
1981 (push-mark (nxml-token-after) nil t
)
1982 (goto-char xmltok-start
)
1983 (message "Marked %s" xmltok-type
))
1987 (defun nxml-mark-paragraph ()
1988 "Put point at beginning of this paragraph, mark at end.
1989 The paragraph marked is the one that contains point or follows point."
1991 (nxml-forward-paragraph)
1993 (nxml-backward-paragraph))
1995 (defun nxml-forward-paragraph (&optional arg
)
1997 (or arg
(setq arg
1))
1999 (nxml-backward-paragraph (- arg
)))
2002 (while (and (nxml-forward-single-paragraph)
2003 (> (setq arg
(1- arg
)) 0))))))
2005 (defun nxml-backward-paragraph (&optional arg
)
2007 (or arg
(setq arg
1))
2009 (nxml-forward-paragraph (- arg
)))
2012 (let ((inhibit-field-text-motion t
))
2014 (while (and (nxml-backward-single-paragraph)
2015 (> (setq arg
(1- arg
)) 0))))))
2017 (defun nxml-forward-single-paragraph ()
2018 "Move forward over a single paragraph.
2019 Return nil at end of buffer, t otherwise."
2020 (let* ((token-end (nxml-token-after))
2021 (offset (- (point) xmltok-start
))
2023 (goto-char token-end
)
2024 (while (and (< (point) (point-max))
2026 (nxml-paragraph-end-pos had-data offset
))))
2027 (when (nxml-token-contains-data-p offset
)
2029 (nxml-tokenize-forward)
2031 (when pos
(goto-char pos
))))
2033 (defun nxml-backward-single-paragraph ()
2034 "Move backward over a single paragraph.
2035 Return nil at start of buffer, t otherwise."
2036 (let* ((token-end (nxml-token-before))
2037 (offset (- token-end
(point)))
2038 (last-tag-pos xmltok-start
)
2039 pos had-data last-data-pos
)
2040 (goto-char token-end
)
2041 (unless (setq pos
(nxml-paragraph-start-pos nil offset
))
2042 (setq had-data
(nxml-token-contains-data-p nil offset
))
2043 (goto-char xmltok-start
)
2044 (while (and (not pos
) (< (point-min) (point)))
2045 (cond ((search-backward "<" nxml-prolog-end t
)
2046 (nxml-move-outside-backwards)
2048 (while (< (point) last-tag-pos
)
2050 (when (and (not had-data
) (nxml-token-contains-data-p))
2052 (setq last-data-pos xmltok-start
))
2053 (let ((tem (nxml-paragraph-start-pos had-data
0)))
2054 (when tem
(setq pos tem
)))))
2055 (when (and (not had-data
) last-data-pos
(not pos
))
2058 (while (< (point) last-data-pos
)
2060 (let ((tem (nxml-paragraph-start-pos had-data
0)))
2061 (when tem
(setq pos tem
)))))
2062 (setq last-tag-pos
(point)))
2063 (t (goto-char (point-min))))))
2064 (when pos
(goto-char pos
))))
2066 (defun nxml-token-contains-data-p (&optional start end
)
2067 (setq start
(+ xmltok-start
(or start
0)))
2068 (setq end
(- (point) (or end
0)))
2069 (when (eq xmltok-type
'cdata-section
)
2070 (setq start
(max start
(+ xmltok-start
9)))
2071 (setq end
(min end
(- (point) 3))))
2072 (or (and (eq xmltok-type
'data
)
2073 (eq start xmltok-start
)
2075 (eq xmltok-type
'char-ref
)
2076 (and (memq xmltok-type
'(data cdata-section
))
2080 (re-search-forward "[^ \t\r\n]" end t
)))))
2082 (defun nxml-paragraph-end-pos (had-data offset
)
2083 "Return the position of the paragraph end if contained in the current token.
2084 Return nil if the current token does not contain the paragraph end.
2085 Only characters after OFFSET from the start of the token are eligible.
2086 HAD-DATA says whether there have been non-whitespace data characters yet."
2087 (cond ((not had-data
)
2088 (cond ((memq xmltok-type
'(data cdata-section
))
2090 (let ((end (point)))
2091 (goto-char (+ xmltok-start
2092 (max (if (eq xmltok-type
'cdata-section
)
2096 (and (re-search-forward "[^ \t\r\n]" end t
)
2097 (re-search-forward "^[ \t]*$" end t
)
2098 (match-beginning 0)))))
2099 ((and (eq xmltok-type
'comment
)
2100 (nxml-token-begins-line-p)
2101 (nxml-token-ends-line-p))
2103 (let ((end (point)))
2104 (goto-char (+ xmltok-start
(max 4 offset
)))
2105 (when (re-search-forward "[^ \t\r\n]" (- end
3) t
)
2106 (if (re-search-forward "^[ \t]*$" end t
)
2108 (goto-char (- end
3))
2109 (skip-chars-backward " \t")
2111 (beginning-of-line 2))
2113 ((memq xmltok-type
'(data space cdata-section
))
2115 (let ((end (point)))
2116 (goto-char (+ xmltok-start offset
))
2117 (and (re-search-forward "^[ \t]*$" end t
)
2118 (match-beginning 0)))))
2119 ((and (memq xmltok-type
'(start-tag
2123 processing-instruction
2125 (nxml-token-begins-line-p)
2126 (nxml-token-ends-line-p))
2128 (goto-char xmltok-start
)
2129 (skip-chars-backward " \t")
2131 ((and (eq xmltok-type
'end-tag
)
2132 (looking-at "[ \t]*$")
2133 (not (nxml-in-mixed-content-p t
)))
2135 (or (search-forward "\n" nil t
)
2138 (defun nxml-paragraph-start-pos (had-data offset
)
2139 "Return the position of the paragraph start if contained in the current token.
2140 Return nil if the current token does not contain the paragraph start.
2141 Only characters before OFFSET from the end of the token are eligible.
2142 HAD-DATA says whether there have been non-whitespace data characters yet."
2143 (cond ((not had-data
)
2144 (cond ((memq xmltok-type
'(data cdata-section
))
2146 (goto-char (- (point)
2147 (max (if (eq xmltok-type
'cdata-section
)
2151 (and (re-search-backward "[^ \t\r\n]" xmltok-start t
)
2152 (re-search-backward "^[ \t]*$" xmltok-start t
)
2153 (match-beginning 0))))
2154 ((and (eq xmltok-type
'comment
)
2155 (nxml-token-ends-line-p)
2156 (nxml-token-begins-line-p))
2158 (goto-char (- (point) (max 3 offset
)))
2159 (when (and (< (+ xmltok-start
4) (point))
2160 (re-search-backward "[^ \t\r\n]"
2163 (if (re-search-backward "^[ \t]*$" xmltok-start t
)
2165 (goto-char xmltok-start
)
2166 (if (looking-at "<!--[ \t]*\n")
2168 (skip-chars-backward " \t")
2170 ((memq xmltok-type
'(data space cdata-section
))
2172 (goto-char (- (point) offset
))
2173 (and (re-search-backward "^[ \t]*$" xmltok-start t
)
2174 (match-beginning 0))))
2175 ((and (memq xmltok-type
'(start-tag
2179 processing-instruction
2181 (nxml-token-ends-line-p)
2182 (nxml-token-begins-line-p))
2183 (or (search-forward "\n" nil t
)
2185 ((and (eq xmltok-type
'start-tag
)
2186 (nxml-token-begins-line-p)
2187 (not (save-excursion
2188 (goto-char xmltok-start
)
2189 (nxml-in-mixed-content-p nil
))))
2191 (goto-char xmltok-start
)
2192 (skip-chars-backward " \t")
2193 ;; include any blank line before
2194 (or (and (eq (char-before) ?
\n)
2196 (goto-char (1- (point)))
2197 (skip-chars-backward " \t")
2198 (and (bolp) (point))))
2201 (defun nxml-token-ends-line-p () (looking-at "[ \t]*$"))
2203 (defun nxml-token-begins-line-p ()
2205 (goto-char xmltok-start
)
2206 (skip-chars-backward " \t")
2209 (defun nxml-in-mixed-content-p (endp)
2210 "Return non-nil if point is in mixed content.
2211 Point must be after an end-tag or before a start-tag.
2212 ENDP is t in the former case, nil in the latter."
2213 (let (matching-tag-pos)
2214 (cond ((not (run-hook-with-args-until-failure
2215 'nxml-in-mixed-content-hook
))
2217 ;; See if the matching tag does not start or end a line.
2218 ((condition-case err
2220 (setq matching-tag-pos
2223 (and (nxml-scan-element-backward (point))
2225 (nxml-scan-element-forward (point)))))
2226 (and matching-tag-pos
2228 (goto-char matching-tag-pos
)
2231 (skip-chars-backward " \t")
2233 (looking-at "[ \t]*$"))))))
2234 (nxml-scan-error nil
))
2236 ;; See if there's data at the same level.
2239 (setq start matching-tag-pos
2242 end matching-tag-pos
))
2246 (nxml-preceding-sibling-data-p))
2249 (nxml-following-sibling-data-p)))))
2251 ;; Otherwise, treat as not mixed
2254 (defun nxml-preceding-sibling-data-p ()
2255 "Return non-nil if there is a previous sibling that is data."
2256 (let ((lim (max (- (point) nxml-mixed-scan-distance
)
2262 (while (and (< lim
(point))
2267 (search-backward "<" lim t
)))
2268 (nxml-move-outside-backwards)
2271 (let ((prev-level level
))
2272 (cond ((eq xmltok-type
'end-tag
)
2273 (setq level
(1+ level
)))
2274 ((eq xmltok-type
'start-tag
)
2275 (setq level
(1- level
))))
2276 (when (eq prev-level
0)
2277 (while (and (< (point) end
) (not found
))
2279 (when (memq xmltok-type
'(data cdata-section char-ref
))
2280 (setq found t
)))))))))
2283 (defun nxml-following-sibling-data-p ()
2284 (let ((lim (min (+ (point) nxml-mixed-scan-distance
)
2290 (while (and (< (point) lim
)
2292 (nxml-tokenize-forward)
2294 (cond ((eq xmltok-type
'start-tag
)
2295 (setq level
(1+ level
)))
2296 ((eq xmltok-type
'end-tag
)
2297 (setq level
(1- level
)))
2299 (memq xmltok-type
'(data cdata-section char-ref
)))
2305 (defun nxml-do-fill-paragraph (arg)
2306 (let (fill-paragraph-function
2310 (nxml-forward-paragraph)
2312 (nxml-backward-paragraph)
2313 (skip-chars-forward " \t\r\n")
2314 (setq start
(point))
2316 (setq fill-prefix
(buffer-substring-no-properties (point) start
))
2317 (when (and (not (nxml-get-inside (point)))
2318 (looking-at "[ \t]*<!--"))
2319 (setq fill-prefix
(concat fill-prefix
" ")))
2320 (fill-region-as-paragraph start end arg
))
2321 (skip-line-prefix fill-prefix
)
2324 (defun nxml-newline-and-indent (soft)
2325 (delete-horizontal-space)
2326 (if soft
(insert-and-inherit ?
\n) (newline 1))
2332 (defvar nxml-dynamic-markup-prev-pos nil
)
2333 (defvar nxml-dynamic-markup-prev-lengths nil
)
2334 (defvar nxml-dynamic-markup-prev-found-marker nil
)
2335 (defvar nxml-dynamic-markup-prev-start-tags
(make-hash-table :test
'equal
))
2337 (defun nxml-dynamic-markup-word ()
2338 "Dynamically markup the word before point.
2339 This attempts to find a tag to put around the word before point based
2340 on the contents of the current buffer. The end-tag will be inserted at
2341 point. The start-tag will be inserted at or before the beginning of
2342 the word before point; the contents of the current buffer is used to
2345 It works in a similar way to \\[dabbrev-expand]. It searches first
2346 backwards from point, then forwards from point for an element whose
2347 content is a string which matches the contents of the buffer before
2348 point and which includes at least the word before point. It then
2349 copies the start- and end-tags from that element and uses them to
2350 surround the matching string before point.
2352 Repeating \\[nxml-dynamic-markup-word] immediately after successful
2353 \\[nxml-dynamic-markup-word] removes the previously inserted markup
2354 and attempts to find another possible way to do the markup."
2356 (let (search-start-pos done
)
2357 (if (and (integerp nxml-dynamic-markup-prev-pos
)
2358 (= nxml-dynamic-markup-prev-pos
(point))
2359 (eq last-command this-command
)
2360 nxml-dynamic-markup-prev-lengths
)
2361 (let* ((end-tag-open-pos
2362 (- nxml-dynamic-markup-prev-pos
2363 (nth 2 nxml-dynamic-markup-prev-lengths
)))
2364 (start-tag-close-pos
2366 (nth 1 nxml-dynamic-markup-prev-lengths
)))
2368 (- start-tag-close-pos
2369 (nth 0 nxml-dynamic-markup-prev-lengths
))))
2370 (delete-region end-tag-open-pos nxml-dynamic-markup-prev-pos
)
2371 (delete-region start-tag-open-pos start-tag-close-pos
)
2372 (setq search-start-pos
2373 (marker-position nxml-dynamic-markup-prev-found-marker
)))
2374 (clrhash nxml-dynamic-markup-prev-start-tags
))
2375 (setq nxml-dynamic-markup-prev-pos nil
)
2376 (setq nxml-dynamic-markup-prev-lengths nil
)
2377 (setq nxml-dynamic-markup-prev-found-marker nil
)
2380 (let* ((pos (point))
2383 (unless (< (point) pos
)
2384 (error "No word to markup"))
2385 (buffer-substring-no-properties (point) pos
)))
2386 (search (concat word
"</"))
2388 (when search-start-pos
2389 (goto-char search-start-pos
))
2390 (while (and (not done
)
2391 (or (and (< (point) pos
)
2392 (or (search-backward search nil t
)
2393 (progn (goto-char pos
) nil
)))
2394 (search-forward search nil t
)))
2395 (goto-char (- (match-end 0) 2))
2396 (setq done
(nxml-try-copy-markup pos
)))
2398 (error (if (zerop (hash-table-count
2399 nxml-dynamic-markup-prev-start-tags
))
2400 "No possible markup found for `%s'"
2401 "No more markup possibilities found for `%s'")
2404 (defun nxml-try-copy-markup (word-end-pos)
2406 (let ((end-tag-pos (point)))
2407 (when (and (not (nxml-get-inside end-tag-pos
))
2408 (search-backward "<" nil t
)
2409 (not (nxml-get-inside (point))))
2411 (when (and (eq xmltok-type
'start-tag
)
2412 (< (point) end-tag-pos
))
2413 (let* ((start-tag-close-pos (point))
2415 (buffer-substring-no-properties xmltok-start
2416 start-tag-close-pos
))
2420 (buffer-substring-no-properties start-tag-close-pos
2423 (goto-char word-end-pos
)
2425 (re-search-backward (concat
2426 (regexp-quote (car words
))
2430 (setq words
(cdr words
))
2431 (skip-chars-backward " \t\r\n"))
2432 (when (and (not words
)
2434 (skip-chars-forward " \t\r\n")
2435 (not (gethash (cons (point) start-tag
)
2436 nxml-dynamic-markup-prev-start-tags
)))
2437 (or (< end-tag-pos
(point))
2438 (< word-end-pos xmltok-start
)))
2439 (setq nxml-dynamic-markup-prev-found-marker
2440 (copy-marker end-tag-pos t
))
2441 (puthash (cons (point) start-tag
)
2443 nxml-dynamic-markup-prev-start-tags
)
2444 (setq nxml-dynamic-markup-prev-lengths
2445 (list (- start-tag-close-pos xmltok-start
)
2446 (- word-end-pos
(point))
2447 (+ (- xmltok-name-end xmltok-start
) 2)))
2448 (let ((name (xmltok-start-tag-qname)))
2450 (goto-char (+ word-end-pos
2451 (- start-tag-close-pos xmltok-start
)))
2452 (insert "</" name
">")
2453 (setq nxml-dynamic-markup-prev-pos
(point))))))))))
2458 (defvar nxml-char-name-ignore-case t
)
2460 (defvar nxml-char-name-alist nil
2461 "Alist of character names.
2462 Each member of the list has the form (NAME CODE . NAMESET),
2463 where NAME is a string naming a character, NAMESET is a symbol
2464 identifying a set of names and CODE is an integer specifying the
2465 Unicode scalar value of the named character.
2466 The NAME will only be used for completion if NAMESET has
2467 a non-nil `nxml-char-name-set-enabled' property.
2468 If NAMESET does does not have `nxml-char-name-set-defined' property,
2469 then it must have a `nxml-char-name-set-file' property and `load'
2470 will be applied to the value of this property if the nameset
2473 (defvar nxml-char-name-table
(make-hash-table :test
'eq
)
2474 "Hash table for mapping char codes to names.
2475 Each key is a Unicode scalar value.
2476 Each value is a list of pairs of the form (NAMESET . NAME),
2477 where NAMESET is a symbol identifying a set of names,
2478 and NAME is a string naming a character.")
2480 (defvar nxml-autoload-char-name-set-list nil
2481 "List of char namesets that can be autoloaded.")
2483 (defun nxml-enable-char-name-set (nameset)
2484 (put nameset
'nxml-char-name-set-enabled t
))
2486 (defun nxml-disable-char-name-set (nameset)
2487 (put nameset
'nxml-char-name-set-enabled nil
))
2489 (defun nxml-char-name-set-enabled-p (nameset)
2490 (get nameset
'nxml-char-name-set-enabled
))
2492 (defun nxml-autoload-char-name-set (nameset file
)
2493 (unless (memq nameset nxml-autoload-char-name-set-list
)
2494 (setq nxml-autoload-char-name-set-list
2495 (cons nameset nxml-autoload-char-name-set-list
)))
2496 (put nameset
'nxml-char-name-set-file file
))
2498 (defun nxml-define-char-name-set (nameset alist
)
2499 "Define a set of character names.
2500 NAMESET is a symbol identifying the set.
2501 Alist is a list where each member has the form (NAME CODE),
2502 where NAME is a string naming a character and code
2503 is an integer giving the Unicode scalar value of the character."
2504 (when (get nameset
'nxml-char-name-set-defined
)
2505 (error "Nameset `%s' already defined" nameset
))
2508 (let* ((name-code (car iter
))
2509 (name (car name-code
))
2510 (code (cadr name-code
)))
2512 (cons (cons nameset name
)
2513 (gethash code nxml-char-name-table
))
2514 nxml-char-name-table
))
2515 (setcdr (cdr (car iter
)) nameset
)
2516 (setq iter
(cdr iter
))))
2517 (setq nxml-char-name-alist
2518 (nconc alist nxml-char-name-alist
))
2519 (put nameset
'nxml-char-name-set-defined t
))
2521 (defun nxml-get-char-name (code)
2522 (mapc 'nxml-maybe-load-char-name-set nxml-autoload-char-name-set-list
)
2523 (let ((names (gethash code nxml-char-name-table
))
2525 (while (and names
(not name
))
2526 (if (nxml-char-name-set-enabled-p (caar names
))
2527 (setq name
(cdar names
))
2528 (setq names
(cdr names
))))
2531 (defvar nxml-named-char-history nil
)
2533 (defun nxml-insert-named-char (arg)
2534 "Insert a character using its name.
2535 The name is read from the minibuffer.
2536 Normally, inserts the character as a numeric character reference.
2537 With a prefix argument, inserts the character directly."
2539 (mapc 'nxml-maybe-load-char-name-set nxml-autoload-char-name-set-list
)
2541 (let ((completion-ignore-case nxml-char-name-ignore-case
))
2542 (completing-read "Character name: "
2543 nxml-char-name-alist
2545 (get (cddr member
) 'nxml-char-name-set-enabled
))
2548 'nxml-named-char-history
)))
2549 (alist nxml-char-name-alist
)
2551 (while (and alist
(not code
))
2552 (setq elt
(assoc name alist
))
2553 (if (get (cddr elt
) 'nxml-char-name-set-enabled
)
2554 (setq code
(cadr elt
))
2555 (setq alist
(cdr (member elt alist
)))))
2558 (or (decode-char 'ucs code
)
2559 (error "Character %x is not supported by Emacs"
2561 (format "&#x%X;" code
))))))
2563 (defun nxml-maybe-load-char-name-set (sym)
2564 (when (and (get sym
'nxml-char-name-set-enabled
)
2565 (not (get sym
'nxml-char-name-set-defined
))
2566 (stringp (get sym
'nxml-char-name-set-file
)))
2567 (load (get sym
'nxml-char-name-set-file
))))
2569 (defun nxml-toggle-char-ref-extra-display (arg)
2570 "*Toggle the display of extra information for character references."
2572 (let ((new (if (null arg
)
2573 (not nxml-char-ref-extra-display
)
2574 (> (prefix-numeric-value arg
) 0))))
2575 (when (not (eq new nxml-char-ref-extra-display
))
2576 (setq nxml-char-ref-extra-display new
)
2580 (if nxml-char-ref-extra-display
2581 (nxml-with-unmodifying-text-property-changes
2582 (nxml-clear-fontified (point-min) (point-max)))
2583 (nxml-clear-char-ref-extra-display (point-min) (point-max))))))))
2585 (put 'nxml-char-ref
'evaporate t
)
2587 (defun nxml-char-ref-display-extra (start end n
)
2588 (when nxml-char-ref-extra-display
2589 (let ((name (nxml-get-char-name n
))
2590 (glyph-string (and nxml-char-ref-display-glyph-flag
2591 (nxml-glyph-display-string n
'nxml-glyph
)))
2593 (when (or name glyph-string
)
2594 (setq ov
(make-overlay start end nil t
))
2595 (overlay-put ov
'category
'nxml-char-ref
)
2597 (overlay-put ov
'help-echo name
))
2601 (propertize glyph-string
'face
'nxml-glyph
)))))))
2603 (defun nxml-clear-char-ref-extra-display (start end
)
2604 (let ((ov (overlays-in start end
)))
2606 (when (eq (overlay-get (car ov
) 'category
) 'nxml-char-ref
)
2607 (delete-overlay (car ov
)))
2608 (setq ov
(cdr ov
)))))
2611 (defun nxml-start-delimiter-length (type)
2612 (or (get type
'nxml-start-delimiter-length
)
2615 (put 'cdata-section
'nxml-start-delimiter-length
9)
2616 (put 'comment
'nxml-start-delimiter-length
4)
2617 (put 'processing-instruction
'nxml-start-delimiter-length
2)
2618 (put 'start-tag
'nxml-start-delimiter-length
1)
2619 (put 'empty-element
'nxml-start-delimiter-length
1)
2620 (put 'partial-empty-element
'nxml-start-delimiter-length
1)
2621 (put 'entity-ref
'nxml-start-delimiter-length
1)
2622 (put 'char-ref
'nxml-start-delimiter-length
2)
2624 (defun nxml-end-delimiter-length (type)
2625 (or (get type
'nxml-end-delimiter-length
)
2628 (put 'cdata-section
'nxml-end-delimiter-length
3)
2629 (put 'comment
'nxml-end-delimiter-length
3)
2630 (put 'processing-instruction
'nxml-end-delimiter-length
2)
2631 (put 'start-tag
'nxml-end-delimiter-length
1)
2632 (put 'empty-element
'nxml-end-delimiter-length
2)
2633 (put 'partial-empty-element
'nxml-end-delimiter-length
1)
2634 (put 'entity-ref
'nxml-end-delimiter-length
1)
2635 (put 'char-ref
'nxml-end-delimiter-length
1)
2637 (defun nxml-token-type-friendly-name (type)
2638 (or (get type
'nxml-friendly-name
)
2639 (symbol-name type
)))
2641 (put 'cdata-section
'nxml-friendly-name
"CDATA section")
2642 (put 'processing-instruction
'nxml-friendly-name
"processing instruction")
2643 (put 'entity-ref
'nxml-friendly-name
"entity reference")
2644 (put 'char-ref
'nxml-friendly-name
"character reference")
2646 (provide 'nxml-mode
)
2648 ;; arch-tag: 8603bc5f-1ef9-4021-b223-322fb2ca708e
2649 ;;; nxml-mode.el ends here