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.
29 (when (featurep 'mucs
)
30 (error "nxml-mode is not compatible with Mule-UCS"))
39 (declare-function rng-nxml-mode-init
"rng-nxml")
40 (declare-function nxml-enable-unicode-char-name-sets
"nxml-uchnm")
45 "New XML editing mode"
49 (defgroup nxml-faces nil
50 "Faces for XML syntax highlighting."
52 :group
'font-lock-faces
)
54 (defcustom nxml-char-ref-display-glyph-flag t
55 "*Non-nil means display glyph following character reference.
56 The glyph is displayed in face `nxml-glyph'. The hook
57 `nxml-glyph-set-hook' can be used to customize for which characters
58 glyphs are displayed."
62 (defcustom nxml-mode-hook nil
63 "Hook run by command `nxml-mode'."
67 (defcustom nxml-sexp-element-flag nil
68 "*Non-nil means sexp commands treat an element as a single expression."
72 (defcustom nxml-slash-auto-complete-flag nil
73 "*Non-nil means typing a slash automatically completes the end-tag.
74 This is used by `nxml-electric-slash'."
78 (defcustom nxml-child-indent
2
79 "*Indentation for the children of an element relative to the start-tag.
80 This only applies when the line or lines containing the start-tag contains
81 nothing else other than that start-tag."
85 (defcustom nxml-attribute-indent
4
86 "*Indentation for the attributes of an element relative to the start-tag.
87 This only applies when the first attribute of a tag starts a line. In other
88 cases, the first attribute on one line is indented the same as the first
89 attribute on the previous line."
93 (defcustom nxml-bind-meta-tab-to-complete-flag
(not window-system
)
94 "*Non-nil means bind M-TAB in `nxml-mode-map' to `nxml-complete'.
95 C-return will be bound to `nxml-complete' in any case.
96 M-TAB gets swallowed by many window systems/managers, and
97 `documentation' will show M-TAB rather than C-return as the
98 binding `rng-complete' when both are bound. So it's better
99 to bind M-TAB only when it will work."
101 :set
(lambda (sym flag
)
102 (set-default sym flag
)
103 (when (and (boundp 'nxml-mode-map
) nxml-mode-map
)
104 (define-key nxml-mode-map
"\M-\t" (and flag
'nxml-complete
))))
107 (defcustom nxml-prefer-utf-16-to-utf-8-flag nil
108 "*Non-nil means prefer UTF-16 to UTF-8 when saving a buffer.
109 This is used only when a buffer does not contain an encoding declaration
110 and when its current `buffer-file-coding-system' specifies neither UTF-16
115 (defcustom nxml-prefer-utf-16-little-to-big-endian-flag
(eq system-type
117 "*Non-nil means prefer little-endian to big-endian byte-order for UTF-16.
118 This is used only for saving a buffer; when reading the byte-order is
119 auto-detected. It may be relevant both when there is no encoding declaration
120 and when the encoding declaration specifies `UTF-16'."
124 (defcustom nxml-default-buffer-file-coding-system nil
125 "*Default value for `buffer-file-coding-system' for a buffer for a new file.
126 Nil means use the default value of `buffer-file-coding-system' as normal.
127 A buffer's `buffer-file-coding-system' affects what \\[nxml-insert-xml-declaration] inserts."
129 :type
'coding-system
)
131 (defcustom nxml-auto-insert-xml-declaration-flag nil
132 "*Non-nil means automatically insert an XML declaration in a new file.
133 The XML declaration is inserted using `nxml-insert-xml-declaration'."
137 (defface nxml-delimited-data
138 '((t (:inherit font-lock-doc-face
)))
139 "Face used to highlight data enclosed between delimiters.
140 This is not used directly, but only via inheritance by other faces."
144 '((t (:inherit font-lock-builtin-face
)))
145 "Face used to highlight various names.
146 This includes element and attribute names, processing
147 instruction targets and the CDATA keyword in a CDATA section.
148 This is not used directly, but only via inheritance by other faces."
152 '((t (:inherit font-lock-constant-face
)))
153 "Face used to highlight character and entity references.
154 This is not used directly, but only via inheritance by other faces."
157 (defface nxml-delimiter
159 "Face used to highlight delimiters.
160 This is not used directly, but only via inheritance by other faces."
165 "Face used to highlight text."
168 (defface nxml-comment-content
169 '((t (:inherit font-lock-comment-face
)))
170 "Face used to highlight the content of comments."
173 (defface nxml-comment-delimiter
174 '((t (:inherit font-lock-comment-delimiter-face
)))
175 "Face used for the delimiters of comments, i.e <!-- and -->."
178 (defface nxml-processing-instruction-delimiter
179 '((t (:inherit nxml-delimiter
)))
180 "Face used for the delimiters of processing instructions, i.e <? and ?>."
183 (defface nxml-processing-instruction-target
184 '((t (:inherit font-lock-keyword-face
)))
185 "Face used for the target of processing instructions."
188 (defface nxml-processing-instruction-content
189 '((t (:inherit nxml-delimited-data
)))
190 "Face used for the content of processing instructions."
193 (defface nxml-cdata-section-delimiter
194 '((t (:inherit nxml-delimiter
)))
195 "Face used for the delimiters of CDATA sections, i.e <![, [, and ]]>."
198 (defface nxml-cdata-section-CDATA
199 '((t (:inherit nxml-name
)))
200 "Face used for the CDATA keyword in CDATA sections."
203 (defface nxml-cdata-section-content
204 '((t (:inherit nxml-text
)))
205 "Face used for the content of CDATA sections."
208 (defface nxml-char-ref-number
209 '((t (:inherit nxml-ref
)))
210 "Face used for the number in character references.
211 This includes ths `x' in hex references."
214 (defface nxml-char-ref-delimiter
215 '((t (:inherit nxml-ref
)))
216 "Face used for the delimiters of character references, i.e &# and ;."
219 (defface nxml-entity-ref-name
220 '((t (:inherit nxml-ref
)))
221 "Face used for the entity name in general entity references."
224 (defface nxml-entity-ref-delimiter
225 '((t (:inherit nxml-ref
)))
226 "Face used for the delimiters of entity references, i.e & and ;."
229 (defface nxml-tag-delimiter
230 '((t (:inherit nxml-delimiter
)))
231 "Face used for the angle brackets delimiting tags.
232 `nxml-tag-slash' is used for slashes."
235 (defface nxml-tag-slash
236 '((t (:inherit nxml-tag-delimiter
)))
237 "Face used for slashes in tags, both in end-tags and empty-elements."
240 (defface nxml-element-prefix
241 '((t (:inherit nxml-name
)))
242 "Face used for the prefix of elements."
245 (defface nxml-element-colon
247 "Face used for the colon in element names."
250 (defface nxml-element-local-name
251 '((t (:inherit font-lock-function-name-face
)))
252 "Face used for the local name of elements."
255 (defface nxml-attribute-prefix
256 '((t (:inherit nxml-name
)))
257 "Face used for the prefix of attributes."
260 (defface nxml-attribute-colon
261 '((t (:inherit nxml-delimiter
)))
262 "Face used for the colon in attribute names."
265 (defface nxml-attribute-local-name
266 '((t (:inherit font-lock-variable-name-face
)))
267 "Face used for the local name of attributes."
270 (defface nxml-namespace-attribute-xmlns
271 '((t (:inherit nxml-attribute-prefix
)))
272 "Face used for `xmlns' in namespace attributes."
275 (defface nxml-namespace-attribute-colon
276 '((t (:inherit nxml-attribute-colon
)))
277 "Face used for the colon in namespace attributes."
280 (defface nxml-namespace-attribute-prefix
281 '((t (:inherit nxml-attribute-local-name
)))
282 "Face used for the prefix declared in namespace attributes."
285 (defface nxml-attribute-value
286 '((t (:inherit font-lock-string-face
)))
287 "Face used for the value of attributes."
290 (defface nxml-attribute-value-delimiter
291 '((t (:inherit nxml-attribute-value
)))
292 "Face used for the delimiters of attribute values."
295 (defface nxml-namespace-attribute-value
296 '((t (:inherit nxml-attribute-value
)))
297 "Face used for the value of namespace attributes."
300 (defface nxml-namespace-attribute-value-delimiter
301 '((t (:inherit nxml-attribute-value-delimiter
)))
302 "Face used for the delimiters of namespace attribute values."
305 (defface nxml-prolog-literal-delimiter
306 '((t (:inherit nxml-delimited-data
)))
307 "Face used for the delimiters of literals in the prolog."
310 (defface nxml-prolog-literal-content
311 '((t (:inherit nxml-delimited-data
)))
312 "Face used for the content of literals in the prolog."
315 (defface nxml-prolog-keyword
316 '((t (:inherit font-lock-keyword-face
)))
317 "Face used for keywords in the prolog."
320 (defface nxml-markup-declaration-delimiter
321 '((t (:inherit nxml-delimiter
)))
322 "Face used for the delimiters of markup declarations in the prolog.
323 The delimiters are <! and >."
327 '((t (:inherit nxml-name
)))
328 "Face used for # before a name in the prolog."
352 "Face used for glyph for char references."
357 (defvar nxml-prolog-regions nil
358 "List of regions in the prolog to be fontified.
359 See the function `xmltok-forward-prolog' for more information.")
360 (make-variable-buffer-local 'nxml-prolog-regions
)
362 (defvar nxml-last-fontify-end nil
363 "Position where fontification last ended.
364 Nil if the buffer changed since the last fontification.")
365 (make-variable-buffer-local 'nxml-last-fontify-end
)
367 (defvar nxml-degraded nil
368 "Non-nil if currently operating in degraded mode.
369 Degraded mode is enabled when an internal error is encountered in the
370 fontification or after-change functions.")
371 (make-variable-buffer-local 'nxml-degraded
)
373 (defvar nxml-completion-hook nil
374 "Hook run by `nxml-complete'.
375 This hook is run until success.")
377 (defvar nxml-in-mixed-content-hook nil
378 "Hook to determine whether point is in mixed content.
379 The hook is called without arguments. It should return nil if it is
380 definitely not mixed; non-nil otherwise. The hook will be run until
381 one of the functions returns nil.")
383 (defvar nxml-mixed-scan-distance
4000
384 "Maximum distance from point to scan when checking for mixed content.")
386 (defvar nxml-end-tag-indent-scan-distance
4000
387 "Maximum distance from point to scan backwards when indenting end-tag.")
389 (defvar nxml-char-ref-extra-display t
390 "Non-nil means display extra information for character references.
391 The extra information consists of a tooltip with the character name
392 and, if `nxml-char-ref-display-glyph-flag' is non-nil, a glyph
393 corresponding to the referenced character following the character
395 (make-variable-buffer-local 'nxml-char-ref-extra-display
)
397 (defvar nxml-mode-map
398 (let ((map (make-sparse-keymap)))
399 (define-key map
"\M-\C-u" 'nxml-backward-up-element
)
400 (define-key map
"\M-\C-d" 'nxml-down-element
)
401 (define-key map
"\M-\C-n" 'nxml-forward-element
)
402 (define-key map
"\M-\C-p" 'nxml-backward-element
)
403 (define-key map
"\M-{" 'nxml-backward-paragraph
)
404 (define-key map
"\M-}" 'nxml-forward-paragraph
)
405 (define-key map
"\M-h" 'nxml-mark-paragraph
)
406 (define-key map
"\C-c\C-f" 'nxml-finish-element
)
407 (define-key map
"\C-c\C-m" 'nxml-split-element
)
408 (define-key map
"\C-c\C-b" 'nxml-balanced-close-start-tag-block
)
409 (define-key map
"\C-c\C-i" 'nxml-balanced-close-start-tag-inline
)
410 (define-key map
"\C-c\C-x" 'nxml-insert-xml-declaration
)
411 (define-key map
"\C-c\C-d" 'nxml-dynamic-markup-word
)
413 (define-key map
"\C-c\C-u" 'nxml-insert-named-char
)
414 (define-key map
"\C-c\C-o" nxml-outline-prefix-map
)
415 (define-key map
[S-mouse-2
] 'nxml-mouse-hide-direct-text-content
)
416 (define-key map
"/" 'nxml-electric-slash
)
417 (define-key map
[C-return
] 'nxml-complete
)
418 (when nxml-bind-meta-tab-to-complete-flag
419 (define-key map
"\M-\t" 'nxml-complete
))
421 "Keymap for nxml-mode.")
423 (defvar nxml-font-lock-keywords
424 '(nxml-fontify-matcher)
425 "Default font lock keywords for nxml-mode.")
427 (defsubst nxml-set-face
(start end face
)
428 (when (and face
(< start end
))
429 (font-lock-append-text-property start end
'face face
)))
433 ;; We use C-c C-i instead of \\[nxml-balanced-close-start-tag-inline]
434 ;; because Emacs turns C-c C-i into C-c TAB which is hard to type and
436 "Major mode for editing XML.
438 \\[nxml-finish-element] finishes the current element by inserting an end-tag.
439 C-c C-i closes a start-tag with `>' and then inserts a balancing end-tag
440 leaving point between the start-tag and end-tag.
441 \\[nxml-balanced-close-start-tag-block] is similar but for block rather than inline elements:
442 the start-tag, point, and end-tag are all left on separate lines.
443 If `nxml-slash-auto-complete-flag' is non-nil, then inserting a `</'
444 automatically inserts the rest of the end-tag.
446 \\[nxml-complete] performs completion on the symbol preceding point.
448 \\[nxml-dynamic-markup-word] uses the contents of the current buffer
449 to choose a tag to put around the word preceding point.
451 Sections of the document can be displayed in outline form. The
452 variable `nxml-section-element-name-regexp' controls when an element
453 is recognized as a section. The same key sequences that change
454 visibility in outline mode are used except that they start with C-c C-o
457 Validation is provided by the related minor-mode `rng-validate-mode'.
458 This also makes completion schema- and context- sensitive. Element
459 names, attribute names, attribute values and namespace URIs can all be
460 completed. By default, `rng-validate-mode' is automatically enabled. You
461 can toggle it using \\[rng-validate-mode] or change the default by
462 customizing `rng-nxml-auto-validate-flag'.
464 \\[indent-for-tab-command] indents the current line appropriately.
465 This can be customized using the variable `nxml-child-indent'
466 and the variable `nxml-attribute-indent'.
468 \\[nxml-insert-named-char] inserts a character reference using
469 the character's name (by default, the Unicode name). \\[universal-argument] \\[nxml-insert-named-char]
470 inserts the character directly.
472 The Emacs commands that normally operate on balanced expressions will
473 operate on XML markup items. Thus \\[forward-sexp] will move forward
474 across one markup item; \\[backward-sexp] will move backward across
475 one markup item; \\[kill-sexp] will kill the following markup item;
476 \\[mark-sexp] will mark the following markup item. By default, each
477 tag each treated as a single markup item; to make the complete element
478 be treated as a single markup item, set the variable
479 `nxml-sexp-element-flag' to t. For more details, see the function
480 `nxml-forward-balanced-item'.
482 \\[nxml-backward-up-element] and \\[nxml-down-element] move up and down the element structure.
484 Many aspects this mode can be customized using
485 \\[customize-group] nxml RET."
487 (kill-all-local-variables)
488 (setq major-mode
'nxml-mode
)
489 (setq mode-name
"nXML")
490 (set (make-local-variable 'mode-line-process
) '((nxml-degraded "/degraded")))
491 ;; We'll determine the fill prefix ourselves
492 (make-local-variable 'adaptive-fill-mode
)
493 (setq adaptive-fill-mode nil
)
494 (make-local-variable 'forward-sexp-function
)
495 (setq forward-sexp-function
'nxml-forward-balanced-item
)
496 (make-local-variable 'indent-line-function
)
497 (setq indent-line-function
'nxml-indent-line
)
498 (make-local-variable 'fill-paragraph-function
)
499 (setq fill-paragraph-function
'nxml-do-fill-paragraph
)
501 ;; This doesn't seem to work too well;
502 ;; I think we should probably roll our own nxml-comment-dwim function.
503 (make-local-variable 'comment-indent-function
)
504 (setq comment-indent-function
'nxml-indent-line
)
505 (make-local-variable 'comment-start
)
506 (setq comment-start
"<!--")
507 (make-local-variable 'comment-start-skip
)
508 (setq comment-start-skip
"<!--[ \t\r\n]*")
509 (make-local-variable 'comment-end
)
510 (setq comment-end
"-->")
511 (make-local-variable 'comment-end-skip
)
512 (setq comment-end-skip
"[ \t\r\n]*-->")
513 (make-local-variable 'comment-line-break-function
)
514 (setq comment-line-break-function
'nxml-newline-and-indent
)
515 (use-local-map nxml-mode-map
)
519 (nxml-clear-dependent-regions (point-min) (point-max))
520 (setq nxml-scan-end
(copy-marker (point-min) nil
))
521 (nxml-with-unmodifying-text-property-changes
522 (nxml-clear-inside (point-min) (point-max))
523 (nxml-with-invisible-motion
524 (nxml-scan-prolog)))))
525 (add-hook 'after-change-functions
'nxml-after-change nil t
)
526 (add-hook 'change-major-mode-hook
'nxml-cleanup nil t
)
528 ;; Emacs 23 handles the encoding attribute on the xml declaration
529 ;; transparently to nxml-mode, so there is no longer a need for the below
530 ;; hook. The hook also had the drawback of overriding explicit user
531 ;; instruction to save as some encoding other than utf-8.
532 ;;; (add-hook 'write-contents-hooks 'nxml-prepare-to-save)
533 (when (not (and (buffer-file-name) (file-exists-p (buffer-file-name))))
534 (when (and nxml-default-buffer-file-coding-system
535 (not (local-variable-p 'buffer-file-coding-system
)))
536 (setq buffer-file-coding-system nxml-default-buffer-file-coding-system
))
537 (when nxml-auto-insert-xml-declaration-flag
538 (nxml-insert-xml-declaration)))
540 (setq font-lock-defaults
541 '(nxml-font-lock-keywords
542 t
; keywords-only; we highlight comments and strings here
543 nil
; font-lock-keywords-case-fold-search. XML is case sensitive
544 nil
; no special syntax table
545 nil
; no automatic syntactic fontification
546 (font-lock-extend-after-change-region-function
547 . nxml-extend-after-change-region
)
548 (font-lock-extend-region-functions .
(nxml-extend-region))
549 (jit-lock-contextually . t
)
550 (font-lock-unfontify-region-function . nxml-unfontify-region
)))
553 (nxml-enable-unicode-char-name-sets)
554 (run-mode-hooks 'nxml-mode-hook
))
556 (defun nxml-cleanup ()
557 "Clean up after nxml-mode."
558 ;; Disable associated minor modes.
559 (rng-validate-mode -
1)
560 ;; Clean up fontification.
563 (let ((inhibit-read-only t
)
565 (modified (buffer-modified-p)))
566 (nxml-with-invisible-motion
567 (remove-text-properties (point-min) (point-max) '(face)))
568 (set-buffer-modified-p modified
)))
569 (remove-hook 'change-major-mode-hook
'nxml-cleanup t
))
571 (defun nxml-degrade (context err
)
572 (message "Internal nXML mode error in %s (%s), degrading"
574 (error-message-string err
))
576 (setq nxml-degraded t
)
577 (setq nxml-prolog-end
1)
581 (nxml-with-unmodifying-text-property-changes
582 (nxml-clear-inside (point-min) (point-max))))))
584 ;;; Change management
586 (defun nxml-debug-region (start end
)
588 (let ((font-lock-beg start
)
591 (goto-char font-lock-beg
)
592 (set-mark font-lock-end
)))
594 (defun nxml-after-change (start end pre-change-length
)
595 ; In font-lock mode, nxml-after-change1 is called via
596 ; nxml-extend-after-change-region instead so that the updated
597 ; book-keeping information is available for fontification.
598 (unless (or font-lock-mode nxml-degraded
)
599 (nxml-with-degradation-on-error 'nxml-after-change
604 (nxml-with-invisible-motion
605 (nxml-with-unmodifying-text-property-changes
607 start end pre-change-length
)))))))))
609 (defun nxml-after-change1 (start end pre-change-length
)
610 "After-change bookkeeping. Returns a cons cell containing a
611 possibly-enlarged change region. You must call
612 nxml-extend-region on this expanded region to obtain the full
613 extent of the area needing refontification.
615 For bookkeeping, call this function even when fontification is
617 (let ((pre-change-end (+ start pre-change-length
)))
619 (nxml-adjust-start-for-dependent-regions start
622 ;; If the prolog might have changed, rescan the prolog
624 ;; Add 2 so as to include the < and following char that
625 ;; start the instance (document element), since changing
626 ;; these can change where the prolog ends.
627 (+ nxml-prolog-end
2))
628 ;; end must be extended to at least the end of the old prolog in
629 ;; case the new prolog is shorter
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 (setq start
(point-min))))
639 (when (> end nxml-prolog-end
)
641 (nxml-move-tag-backwards (point-min))
643 (setq end
(max (nxml-scan-after-change start end
)
646 (nxml-debug-change "nxml-after-change1" start end
)
651 (defun nxml-insert-xml-declaration ()
652 "Insert an XML declaration at the beginning of buffer.
653 The XML declaration will declare an encoding depending on the buffer's
654 `buffer-file-coding-system'."
657 (if (and buffer-file-coding-system
658 (coding-system-p buffer-file-coding-system
)
659 (coding-system-get buffer-file-coding-system
661 buffer-file-coding-system
662 (nxml-choose-utf-coding-system))))
663 (goto-char (point-min))
664 (insert (format "<?xml version=\"1.0\" encoding=\"%s\"?>\n"
665 (nxml-coding-system-name coding-system
)))))
667 (defun nxml-prepare-to-save ()
668 (unless (and (not enable-multibyte-characters
)
669 (local-variable-p 'buffer-file-coding-system
)
670 buffer-file-coding-system
671 (or (eq (coding-system-type buffer-file-coding-system
) 5)
672 (eq buffer-file-coding-system
'no-conversion
)))
674 (setq buffer-file-coding-system
(nxml-select-coding-system))))
675 ;; nil from a function in `write-contents-hooks' means
676 ;; to continue and write the file as normal
679 (defun nxml-select-coding-system ()
680 (let* ((suitable-coding-systems
681 (find-coding-systems-region (point-min) (point-max)))
683 (goto-char (point-min))
684 (xmltok-get-declared-encoding-position)))
687 (buffer-substring-no-properties (car enc-pos
)
691 (if (string= (downcase enc-name
) "utf-16")
692 (nxml-choose-utf-16-coding-system)
693 (nxml-mime-charset-coding-system enc-name
)))
694 (enc-pos (nxml-choose-utf-coding-system)))))
695 ;; Make sure we have a coding-system
696 (unless coding-system
698 (and (not buffer-read-only
)
699 (nxml-choose-suitable-coding-system
700 suitable-coding-systems
)))
703 (format "Unknown encoding %s" enc-name
)
704 "XML declaration is not well-formed")))
705 (cond ((not coding-system
)
706 (error "%s" message
))
712 "Modify and save with encoding %s")
713 (nxml-coding-system-name coding-system
))
715 (nxml-fix-encoding-declaration enc-pos coding-system
))
716 (t (signal 'quit nil
)))))
717 ;; Make sure it can encode all the characters in the buffer
718 (unless (or (memq (coding-system-base coding-system
)
719 suitable-coding-systems
)
720 (equal suitable-coding-systems
'(undecided)))
722 (nxml-unsuitable-coding-system-message coding-system
725 (and (not buffer-read-only
)
726 (nxml-choose-suitable-coding-system
727 suitable-coding-systems
)))
728 (cond ((not coding-system
) (error "%s" message
))
729 ((y-or-n-p (concat message
730 (format ". Save with %s "
731 (nxml-coding-system-name
733 (nxml-fix-encoding-declaration enc-pos coding-system
))
734 (t (signal 'quit nil
)))))
735 ;; Merge the newline type of our existing encoding
736 (let ((current-eol-type
737 (coding-system-eol-type buffer-file-coding-system
)))
738 (when (and current-eol-type
(integerp current-eol-type
))
740 (coding-system-change-eol-conversion coding-system
744 (defun nxml-unsuitable-coding-system-message (coding-system &optional enc-name
)
745 (if (nxml-coding-system-unicode-p coding-system
)
746 "Cannot translate some characters to Unicode"
747 (format "Cannot encode some characters with %s"
749 (nxml-coding-system-name coding-system
)))))
751 (defconst nxml-utf-16-coding-systems
(and (coding-system-p 'utf-16-be
)
752 (coding-system-p 'utf-16-le
)
753 '(utf-16-be utf-16-le
)))
755 (defconst nxml-utf-coding-systems
(cons 'utf-8 nxml-utf-16-coding-systems
))
757 (defun nxml-coding-system-unicode-p (coding-system)
758 (nxml-coding-system-member (coding-system-base coding-system
)
759 nxml-utf-coding-systems
))
761 (defun nxml-coding-system-name (coding-system)
762 (setq coding-system
(coding-system-base coding-system
))
764 (if (nxml-coding-system-member coding-system nxml-utf-16-coding-systems
)
766 (or (coding-system-get coding-system
'mime-charset
)
769 (defun nxml-fix-encoding-declaration (enc-pos coding-system
)
770 (let ((charset (nxml-coding-system-name coding-system
)))
771 (cond ((consp enc-pos
)
772 (delete-region (car enc-pos
) (cdr enc-pos
))
773 (goto-char (car enc-pos
))
777 (insert " encoding=\"" charset ?
\"))
779 (goto-char (point-min))
780 (insert "<?xml version=\"1.0\" encoding=\""
783 (when (and (not enc-pos
)
784 (let ((case-fold-search t
))
785 (looking-at xmltok-bad-xml-decl-regexp
)))
786 (delete-region (point) (match-end 0)))))))
788 (defun nxml-choose-suitable-coding-system (suitable-coding-systems)
789 (let (ret coding-system
)
790 (if (and buffer-file-coding-system
791 (memq (coding-system-base buffer-file-coding-system
)
792 suitable-coding-systems
))
793 buffer-file-coding-system
794 (while (and suitable-coding-systems
(not ret
))
795 (setq coding-system
(car suitable-coding-systems
))
796 (if (coding-system-get coding-system
'mime-charset
)
797 (setq ret coding-system
)
798 (setq suitable-coding-systems
(cdr suitable-coding-systems
))))
801 (defun nxml-choose-utf-coding-system ()
802 (let ((cur (and (local-variable-p 'buffer-file-coding-system
)
803 buffer-file-coding-system
804 (coding-system-base buffer-file-coding-system
))))
805 (cond ((car (nxml-coding-system-member cur nxml-utf-coding-systems
)))
806 ((and nxml-prefer-utf-16-to-utf-8-flag
807 (coding-system-p 'utf-16-le
)
808 (coding-system-p 'utf-16-be
))
809 (if nxml-prefer-utf-16-little-to-big-endian-flag
814 (defun nxml-choose-utf-16-coding-system ()
815 (let ((cur (and (local-variable-p 'buffer-file-coding-system
)
816 buffer-file-coding-system
817 (coding-system-base buffer-file-coding-system
))))
818 (cond ((car (nxml-coding-system-member cur nxml-utf-16-coding-systems
)))
819 (nxml-prefer-utf-16-little-to-big-endian-flag
820 (and (coding-system-p 'utf-16-le
) 'utf-16-le
))
821 (t (and (coding-system-p 'utf-16-be
) 'utf-16-be
)))))
823 (defun nxml-coding-system-member (coding-system coding-systems
)
825 (while (and coding-systems
(not ret
))
826 (if (coding-system-equal coding-system
827 (car coding-systems
))
828 (setq ret coding-systems
)
829 (setq coding-systems
(cdr coding-systems
))))
834 (defun nxml-unfontify-region (start end
)
835 (font-lock-default-unfontify-region start end
)
836 (nxml-clear-char-ref-extra-display start end
))
838 (defvar font-lock-beg
) (defvar font-lock-end
)
839 (defun nxml-extend-region ()
840 "Extend the region to hold the minimum area we can fontify with nXML.
841 Called with font-lock-beg and font-lock-end dynamically bound."
842 (let ((start font-lock-beg
)
845 (nxml-debug-change "nxml-extend-region(input)" start end
)
847 (when (< start nxml-prolog-end
)
848 (setq start
(point-min)))
850 (cond ((<= end nxml-prolog-end
)
851 (setq end nxml-prolog-end
))
855 ;; some font-lock backends (like Emacs 22 jit-lock) snap
856 ;; the region to the beginning of the line no matter what
857 ;; we say here. To mitigate the resulting excess
858 ;; fontification, ignore leading whitespace.
859 (skip-syntax-forward " ")
861 ;; find the beginning of the previous tag
862 (when (not (equal (char-after) ?\
<))
863 (search-backward "<" nxml-prolog-end t
))
864 (nxml-ensure-scan-up-to-date)
865 (nxml-move-outside-backwards)
868 (while (< (point) end
)
869 (nxml-tokenize-forward))
873 (when (or (< start font-lock-beg
)
874 (> end font-lock-end
))
875 (setq font-lock-beg start
877 (nxml-debug-change "nxml-extend-region" start end
)
880 (defun nxml-extend-after-change-region (start end pre-change-length
)
881 (unless nxml-degraded
882 (setq nxml-last-fontify-end nil
)
884 (nxml-with-degradation-on-error 'nxml-extend-after-change-region
889 (nxml-with-invisible-motion
890 (nxml-with-unmodifying-text-property-changes
891 (nxml-extend-after-change-region1
892 start end pre-change-length
)))))))))
894 (defun nxml-extend-after-change-region1 (start end pre-change-length
)
895 (let* ((region (nxml-after-change1 start end pre-change-length
))
896 (font-lock-beg (car region
))
897 (font-lock-end (cdr region
)))
900 (cons font-lock-beg font-lock-end
)))
902 (defun nxml-fontify-matcher (bound)
903 "Called as font-lock keyword matcher."
905 (unless nxml-degraded
906 (nxml-debug-change "nxml-fontify-matcher" (point) bound
)
908 (when (< (point) nxml-prolog-end
)
909 ;; prolog needs to be fontified in one go, and
910 ;; nxml-extend-region makes sure we start at BOB.
912 (nxml-fontify-prolog)
913 (goto-char nxml-prolog-end
))
915 (let (xmltok-dependent-regions
917 (while (and (nxml-tokenize-forward)
918 (<= (point) bound
)) ; intervals are open-ended
919 (nxml-apply-fontify-rule)))
921 (setq nxml-last-fontify-end
(point)))
923 ;; Since we did the fontification internally, tell font-lock to not
924 ;; do anything itself.
927 (defun nxml-fontify-prolog ()
929 The buffer is assumed to be prepared for fontification.
930 This does not set the fontified property, but it does clear
931 faces appropriately."
932 (let ((regions nxml-prolog-regions
))
934 (let ((region (car regions
)))
935 (nxml-apply-fontify-rule (aref region
0)
938 (setq regions
(cdr regions
)))))
940 ;; Vectors identify a substring of the token to be highlighted in some face.
942 ;; Token types returned by xmltok-forward.
946 '([nil
1 nxml-tag-delimiter
]
947 [-
1 nil nxml-tag-delimiter
]
951 (put 'partial-start-tag
953 '([nil
1 nxml-tag-delimiter
]
959 '([nil
1 nxml-tag-delimiter
]
961 [-
1 nil nxml-tag-delimiter
]
962 (element-qname .
2)))
964 (put 'partial-end-tag
966 '([nil
1 nxml-tag-delimiter
]
968 (element-qname .
2)))
972 '([nil
1 nxml-tag-delimiter
]
973 [-
2 -
1 nxml-tag-slash
]
974 [-
1 nil nxml-tag-delimiter
]
978 (put 'partial-empty-element
980 '([nil
1 nxml-tag-delimiter
]
981 [-
1 nil nxml-tag-slash
]
987 '([nil
2 nxml-char-ref-delimiter
]
988 [2 -
1 nxml-char-ref-number
]
989 [-
1 nil nxml-char-ref-delimiter
]
994 '([nil
1 nxml-entity-ref-delimiter
]
995 [1 -
1 nxml-entity-ref-name
]
996 [-
1 nil nxml-entity-ref-delimiter
]))
1000 '([nil
4 nxml-comment-delimiter
]
1001 [4 -
3 nxml-comment-content
]
1002 [-
3 nil nxml-comment-delimiter
]))
1004 (put 'processing-instruction
1006 '([nil
2 nxml-processing-instruction-delimiter
]
1007 [-
2 nil nxml-processing-instruction-delimiter
]
1008 processing-instruction-content
))
1012 '([nil
3 nxml-cdata-section-delimiter
] ; <![
1013 [3 8 nxml-cdata-section-CDATA
] ; CDATA
1014 [8 9 nxml-cdata-section-delimiter
] ; [
1015 [9 -
3 nxml-cdata-section-content
] ; ]]>
1016 [-
3 nil nxml-cdata-section-delimiter
]))
1020 '([nil nil nxml-text
]))
1022 ;; Prolog region types in list returned by xmltok-forward-prolog.
1024 (put 'xml-declaration
1026 '([nil
2 nxml-processing-instruction-delimiter
]
1027 [2 5 nxml-processing-instruction-target
]
1028 [-
2 nil nxml-processing-instruction-delimiter
]))
1030 (put 'xml-declaration-attribute-name
1032 '([nil nil nxml-attribute-local-name
]))
1034 (put 'xml-declaration-attribute-value
1036 '([nil
1 nxml-attribute-value-delimiter
]
1037 [1 -
1 nxml-attribute-value
]
1038 [-
1 nil nxml-attribute-value-delimiter
]))
1040 (put 'processing-instruction-left
1042 '([nil
2 nxml-processing-instruction-delimiter
]
1043 [2 nil nxml-processing-instruction-target
]))
1045 (put 'processing-instruction-right
1047 '([nil -
2 nxml-processing-instruction-content
]
1048 [-
2 nil nxml-processing-instruction-delimiter
]))
1052 '([nil
1 nxml-prolog-literal-delimiter
]
1053 [1 -
1 nxml-prolog-literal-content
]
1054 [-
1 nil nxml-prolog-literal-delimiter
]))
1058 '([nil nil nxml-prolog-keyword
]))
1060 (put 'markup-declaration-open
1062 '([0 2 nxml-markup-declaration-delimiter
]
1063 [2 nil nxml-prolog-keyword
]))
1065 (put 'markup-declaration-close
1067 '([nil nil nxml-markup-declaration-delimiter
]))
1069 (put 'internal-subset-open
1071 '([nil nil nxml-markup-declaration-delimiter
]))
1073 (put 'internal-subset-close
1075 '([nil
1 nxml-markup-declaration-delimiter
]
1076 [-
1 nil nxml-markup-declaration-delimiter
]))
1081 [1 nil nxml-prolog-keyword
]))
1083 (defun nxml-apply-fontify-rule (&optional type start end
)
1084 (let ((rule (get (or type xmltok-type
) 'nxml-fontify-rule
)))
1085 (unless start
(setq start xmltok-start
))
1086 (unless end
(setq end
(point)))
1088 (let* ((action (car rule
)))
1089 (setq rule
(cdr rule
))
1090 (cond ((vectorp action
)
1091 (nxml-set-face (let ((offset (aref action
0)))
1092 (cond ((not offset
) start
)
1093 ((< offset
0) (+ end offset
))
1094 (t (+ start offset
))))
1095 (let ((offset (aref action
1)))
1096 (cond ((not offset
) end
)
1097 ((< offset
0) (+ end offset
))
1098 (t (+ start offset
))))
1100 ((and (consp action
)
1101 (eq (car action
) 'element-qname
))
1102 (when xmltok-name-end
; maybe nil in partial-end-tag case
1103 (nxml-fontify-qname (+ start
(cdr action
))
1106 'nxml-element-prefix
1108 'nxml-element-local-name
)))
1109 ((eq action
'attributes
)
1110 (nxml-fontify-attributes))
1111 ((eq action
'processing-instruction-content
)
1112 (nxml-set-face (+ start
2)
1114 'nxml-processing-instruction-target
)
1115 (nxml-set-face (save-excursion
1116 (goto-char xmltok-name-end
)
1117 (skip-chars-forward " \t\r\n")
1120 'nxml-processing-instruction-content
))
1121 ((eq action
'char-ref
)
1122 (nxml-char-ref-display-extra start
1124 (xmltok-char-number start end
)))
1125 (t (error "Invalid nxml-fontify-rule action %s" action
)))))))
1127 (defun nxml-fontify-attributes ()
1128 (while xmltok-namespace-attributes
1129 (nxml-fontify-attribute (car xmltok-namespace-attributes
)
1131 (setq xmltok-namespace-attributes
1132 (cdr xmltok-namespace-attributes
)))
1133 (while xmltok-attributes
1134 (nxml-fontify-attribute (car xmltok-attributes
))
1135 (setq xmltok-attributes
1136 (cdr xmltok-attributes
))))
1138 (defun nxml-fontify-attribute (att &optional namespace-declaration
)
1139 (if namespace-declaration
1140 (nxml-fontify-qname (xmltok-attribute-name-start att
)
1141 (xmltok-attribute-name-colon att
)
1142 (xmltok-attribute-name-end att
)
1143 'nxml-namespace-attribute-xmlns
1144 'nxml-namespace-attribute-colon
1145 'nxml-namespace-attribute-prefix
1146 'nxml-namespace-attribute-xmlns
)
1147 (nxml-fontify-qname (xmltok-attribute-name-start att
)
1148 (xmltok-attribute-name-colon att
)
1149 (xmltok-attribute-name-end att
)
1150 'nxml-attribute-prefix
1151 'nxml-attribute-colon
1152 'nxml-attribute-local-name
))
1153 (let ((start (xmltok-attribute-value-start att
))
1154 (end (xmltok-attribute-value-end att
))
1155 (refs (xmltok-attribute-refs att
))
1156 (delimiter-face (if namespace-declaration
1157 'nxml-namespace-attribute-value-delimiter
1158 'nxml-attribute-value-delimiter
))
1159 (value-face (if namespace-declaration
1160 'nxml-namespace-attribute-value
1161 'nxml-attribute-value
)))
1163 (nxml-set-face (1- start
) start delimiter-face
)
1164 (nxml-set-face end
(1+ end
) delimiter-face
)
1166 (let* ((ref (car refs
))
1167 (ref-type (aref ref
0))
1168 (ref-start (aref ref
1))
1169 (ref-end (aref ref
2)))
1170 (nxml-set-face start ref-start value-face
)
1171 (nxml-apply-fontify-rule ref-type ref-start ref-end
)
1172 (setq start ref-end
))
1173 (setq refs
(cdr refs
)))
1174 (nxml-set-face start end value-face
))))
1176 (defun nxml-fontify-qname (start
1184 (cond (colon (nxml-set-face start colon prefix-face
)
1185 (nxml-set-face colon
(1+ colon
) colon-face
)
1186 (nxml-set-face (1+ colon
) end local-name-face
))
1187 (t (nxml-set-face start end
(or unprefixed-face
1188 local-name-face
)))))
1192 (defun nxml-electric-slash (arg)
1195 With a prefix ARG, do nothing other than insert the slash.
1197 Otherwise, if `nxml-slash-auto-complete-flag' is non-nil, insert the
1198 rest of the end-tag or empty-element if the slash is potentially part
1199 of an end-tag or the close of an empty-element.
1201 If the slash is part of an end-tag that is the first non-whitespace
1202 on the line, reindent the line."
1204 (nxml-ensure-scan-up-to-date)
1205 (let* ((slash-pos (point))
1206 (end-tag-p (and (eq (char-before slash-pos
) ?
<)
1207 (not (nxml-get-inside slash-pos
))))
1208 (at-indentation (save-excursion
1209 (back-to-indentation)
1210 (eq (point) (1- slash-pos
)))))
1211 (self-insert-command (prefix-numeric-value arg
))
1213 (if nxml-slash-auto-complete-flag
1216 (let ((start-tag-end
1217 (nxml-scan-element-backward (1- slash-pos
) t
)))
1219 (insert (xmltok-start-tag-qname) ">")
1220 ;; copy the indentation of the start-tag
1221 (when (and at-indentation
1223 (goto-char xmltok-start
)
1224 (back-to-indentation)
1225 (eq (point) xmltok-start
)))
1227 (indent-line-to (save-excursion
1228 (goto-char xmltok-start
)
1229 (current-column)))))))
1230 (nxml-scan-error nil
))
1231 (when (and (eq (nxml-token-before) (point))
1232 (eq xmltok-type
'partial-empty-element
))
1234 (when (and end-tag-p at-indentation
)
1235 (nxml-indent-line))))))
1237 (defun nxml-balanced-close-start-tag-block ()
1238 "Close the start-tag before point with `>' and insert a balancing end-tag.
1239 Point is left between the start-tag and the end-tag.
1240 If there is nothing but whitespace before the `<' that opens the
1241 start-tag, then put point on a blank line, and put the end-tag on
1242 another line aligned with the start-tag."
1244 (nxml-balanced-close-start-tag 'block
))
1246 (defun nxml-balanced-close-start-tag-inline ()
1247 "Close the start-tag before point with `>' and insert a balancing end-tag.
1248 Point is left between the start-tag and the end-tag.
1249 No extra whitespace is inserted."
1251 (nxml-balanced-close-start-tag 'inline
))
1253 (defun nxml-balanced-close-start-tag (block-or-inline)
1254 (let ((token-end (nxml-token-before))
1256 (unless (or (eq xmltok-type
'partial-start-tag
)
1257 (and (memq xmltok-type
'(start-tag
1259 partial-empty-element
))
1260 (>= token-end pos
)))
1261 (error "Not in a start-tag"))
1263 (buffer-substring-no-properties (+ xmltok-start
1)
1264 (min xmltok-name-end
(point)))
1266 (if (eq block-or-inline
'inline
)
1268 (goto-char xmltok-start
)
1269 (back-to-indentation)
1270 (if (= (point) xmltok-start
)
1271 (let ((indent (current-column)))
1274 (indent-line-to indent
)
1277 (indent-line-to (+ nxml-child-indent indent
)))
1280 (defun nxml-finish-element ()
1281 "Finish the current element by inserting an end-tag."
1283 (nxml-finish-element-1 nil
))
1285 (defvar nxml-last-split-position nil
1286 "Position where `nxml-split-element' split the current element.")
1288 (defun nxml-split-element ()
1289 "Split the current element by inserting an end-tag and a start-tag.
1290 Point is left after the newly inserted start-tag. When repeated,
1291 split immediately before the previously inserted start-tag and leave
1294 (setq nxml-last-split-position
1295 (if (and (eq last-command this-command
)
1296 nxml-last-split-position
)
1298 (goto-char nxml-last-split-position
)
1299 (nxml-finish-element-1 t
))
1300 (nxml-finish-element-1 t
))))
1302 (defun nxml-finish-element-1 (startp)
1303 "Insert an end-tag for the current element and optionally a start-tag.
1304 The start-tag is inserted if STARTP is non-nil. Return the position
1305 of the inserted start-tag or nil if none was inserted."
1307 (let* ((token-end (nxml-token-before))
1310 (when (and (< (point) token-end
)
1313 processing-instruction
1318 (error "Point is inside a %s"
1319 (nxml-token-type-friendly-name xmltok-type
)))
1320 (nxml-scan-element-backward token-end t
)))
1323 (unless (eq xmltok-type
'start-tag
)
1324 (error "No matching start-tag"))
1325 (goto-char xmltok-start
)
1326 (back-to-indentation)
1327 (eq (point) xmltok-start
)))
1330 (goto-char start-tag-end
)
1331 (looking-at "[ \t\r\n]*$")))
1332 (start-tag-indent (save-excursion
1333 (goto-char xmltok-start
)
1335 (qname (xmltok-start-tag-qname))
1336 inserted-start-tag-pos
)
1337 (when (and starts-line ends-line
)
1338 ;; start-tag is on a line by itself
1339 ;; => put the end-tag on a line by itself
1342 (back-to-indentation)
1345 (indent-line-to start-tag-indent
))
1346 (insert "</" qname
">")
1350 (indent-line-to start-tag-indent
))
1351 (setq inserted-start-tag-pos
(point))
1352 (insert "<" qname
">")
1353 (when (and starts-line ends-line
)
1355 (indent-line-to (save-excursion
1356 (goto-char xmltok-start
)
1358 (back-to-indentation)
1359 (if (= (current-column)
1360 (+ start-tag-indent nxml-child-indent
))
1361 (+ start-tag-indent nxml-child-indent
)
1362 start-tag-indent
)))))
1363 inserted-start-tag-pos
))
1367 (defun nxml-indent-line ()
1368 "Indent current line as XML."
1369 (let ((indent (nxml-compute-indent))
1370 (from-end (- (point-max) (point))))
1373 (let ((bol (point)))
1374 (skip-chars-forward " \t")
1375 (delete-region bol
(point)))
1377 (when (> (- (point-max) from-end
) (point))
1378 (goto-char (- (point-max) from-end
))))))
1380 (defun nxml-compute-indent ()
1381 "Return the indent for the line containing point."
1382 (or (nxml-compute-indent-from-matching-start-tag)
1383 (nxml-compute-indent-from-previous-line)))
1385 (defun nxml-compute-indent-from-matching-start-tag ()
1386 "Compute the indent for a line with an end-tag using the matching start-tag.
1387 When the line containing point ends with an end-tag and does not start
1388 in the middle of a token, return the indent of the line containing the
1389 matching start-tag, if there is one and it occurs at the beginning of
1390 its line. Otherwise return nil."
1392 (back-to-indentation)
1393 (let ((bol (point)))
1394 (let ((inhibit-field-text-motion t
))
1396 (skip-chars-backward " \t")
1397 (and (= (nxml-token-before) (point))
1398 (memq xmltok-type
'(end-tag partial-end-tag
))
1399 ;; start of line must not be inside a token
1400 (or (= xmltok-start bol
)
1404 (= xmltok-start bol
))
1405 (eq xmltok-type
'data
))
1407 (nxml-scan-element-backward
1411 nxml-end-tag-indent-scan-distance
))
1412 (nxml-scan-error nil
))
1413 (< xmltok-start bol
)
1415 (goto-char xmltok-start
)
1416 (skip-chars-backward " \t")
1418 (current-indentation)))))
1420 (defun nxml-compute-indent-from-previous-line ()
1421 "Compute the indent for a line using the indentation of a previous line."
1426 before-context after-context
)
1427 (back-to-indentation)
1430 ;; Move backwards until the start of a non-blank line that is
1431 ;; not inside a token.
1433 (when (= (forward-line -
1) -
1)
1435 (back-to-indentation)
1436 (if (looking-at "[ \t]*$")
1439 (setq prev-bol
(point)))
1441 (not (or (= xmltok-start
(point))
1442 (eq xmltok-type
'data
))))))
1444 ;; Now scan over tokens until the end of the line to be indented.
1445 ;; Determine the context before and after the beginning of the
1447 (while (< (point) eol
)
1448 (nxml-tokenize-forward)
1449 (cond ((<= bol xmltok-start
)
1451 (nxml-merge-indent-context-type after-context
)))
1452 ((and (<= (point) bol
)
1453 (not (and (eq xmltok-type
'partial-start-tag
)
1455 (setq before-context
1456 (nxml-merge-indent-context-type before-context
)))
1457 ((eq xmltok-type
'data
)
1458 (setq before-context
1459 (nxml-merge-indent-context-type before-context
))
1461 (nxml-merge-indent-context-type after-context
)))
1462 ;; If in the middle of a token that looks inline,
1463 ;; then indent relative to the previous non-blank line
1464 ((eq (nxml-merge-indent-context-type before-context
)
1466 (goto-char prev-bol
)
1467 (throw 'indent
(current-column)))
1470 (nxml-compute-indent-in-token bol
))))
1471 (skip-chars-forward " \t\r\n"))
1474 (* nxml-child-indent
1475 (+ (if (eq before-context
'start-tag
) 1 0)
1476 (if (eq after-context
'end-tag
) -
1 0))))))))
1478 (defun nxml-merge-indent-context-type (context)
1479 "Merge the indent context type CONTEXT with the token in `xmltok-type'.
1480 Return the merged indent context type. An indent context type is
1481 either nil or one of the symbols start-tag, end-tag, markup, comment,
1483 (cond ((memq xmltok-type
'(start-tag partial-start-tag
))
1484 (if (memq context
'(nil start-tag comment
))
1487 ((memq xmltok-type
'(end-tag partial-end-tag
))
1488 (if (memq context
'(nil end-tag comment
))
1491 ((eq xmltok-type
'comment
)
1492 (cond ((memq context
'(start-tag end-tag comment
))
1499 (defun nxml-compute-indent-in-token (pos)
1500 "Return the indent for a line that starts inside a token.
1501 POS is the position of the first non-whitespace character of the line.
1502 This expects the xmltok-* variables to be set up as by `xmltok-forward'."
1503 (cond ((memq xmltok-type
'(start-tag
1506 partial-empty-element
))
1507 (nxml-compute-indent-in-start-tag pos
))
1508 ((eq xmltok-type
'comment
)
1509 (nxml-compute-indent-in-delimited-token pos
"<!--" "-->"))
1510 ((eq xmltok-type
'cdata-section
)
1511 (nxml-compute-indent-in-delimited-token pos
"<![CDATA[" "]]>"))
1512 ((eq xmltok-type
'processing-instruction
)
1513 (nxml-compute-indent-in-delimited-token pos
"<?" "?>"))
1516 (if (and (= (forward-line -
1) 0)
1517 (< xmltok-start
(point)))
1518 (back-to-indentation)
1519 (goto-char xmltok-start
))
1522 (defun nxml-compute-indent-in-start-tag (pos)
1523 "Return the indent for a line that starts inside a start-tag.
1524 Also for a line that starts inside an empty element.
1525 POS is the position of the first non-whitespace character of the line.
1526 This expects the xmltok-* variables to be set up as by `xmltok-forward'."
1527 (let ((value-boundary (nxml-attribute-value-boundary pos
))
1530 ;; inside an attribute value
1531 (let ((value-start (car value-boundary
))
1532 (value-end (cdr value-boundary
)))
1535 (if (< (point) value-start
)
1536 (goto-char value-start
)
1537 (back-to-indentation)))
1538 ;; outside an attribute value
1540 (while (and (= (forward-line -
1) 0)
1541 (nxml-attribute-value-boundary (point))))
1542 (cond ((<= (point) xmltok-start
)
1543 (goto-char xmltok-start
)
1544 (setq off nxml-attribute-indent
)
1545 (let ((atts (xmltok-merge-attributes)))
1547 (let* ((att (car atts
))
1548 (start (xmltok-attribute-name-start att
)))
1553 (back-to-indentation))))
1554 (+ (current-column) off
)))
1556 (defun nxml-attribute-value-boundary (pos)
1557 "Return a pair (START . END) if POS is inside an attribute value.
1558 Otherwise return nil. START and END are the positions of the start
1559 and end of the attribute value containing POS. This expects the
1560 xmltok-* variables to be set up as by `xmltok-forward'."
1561 (let ((atts (xmltok-merge-attributes))
1562 att value-start value-end value-boundary
)
1564 (setq att
(car atts
))
1565 (setq value-start
(xmltok-attribute-value-start att
))
1566 (setq value-end
(xmltok-attribute-value-end att
))
1567 (cond ((and value-start
(< pos value-start
))
1569 ((and value-start value-end
(<= pos value-end
))
1570 (setq value-boundary
(cons value-start value-end
))
1572 (t (setq atts
(cdr atts
)))))
1575 (defun nxml-compute-indent-in-delimited-token (pos open-delim close-delim
)
1576 "Return the indent for a line that starts inside a token with delimiters.
1577 OPEN-DELIM and CLOSE-DELIM are strings giving the opening and closing
1578 delimiters. POS is the position of the first non-whitespace character
1579 of the line. This expects the xmltok-* variables to be set up as by
1581 (cond ((let ((end (+ pos
(length close-delim
))))
1582 (and (<= end
(point-max))
1583 (string= (buffer-substring-no-properties pos end
)
1585 (goto-char xmltok-start
))
1589 (<= (point) xmltok-start
))
1590 (goto-char (+ xmltok-start
(length open-delim
)))
1591 (when (and (string= open-delim
"<!--")
1593 (goto-char (1+ (point)))))
1594 (t (back-to-indentation)))
1599 (defun nxml-complete ()
1600 "Perform completion on the symbol preceding point.
1602 Inserts as many characters as can be completed. However, if not even
1603 one character can be completed, then a buffer with the possibilities
1604 is popped up and the symbol is read from the minibuffer with
1605 completion. If the symbol is complete, then any characters that must
1606 follow the symbol are also inserted.
1608 The name space used for completion and what is treated as a symbol
1609 depends on the context. The contexts in which completion is performed
1610 depend on `nxml-completion-hook'."
1612 (unless (run-hook-with-args-until-success 'nxml-completion-hook
)
1613 ;; Eventually we will complete on entity names here.
1615 (message "Cannot complete in this context")))
1619 (defun nxml-forward-balanced-item (&optional arg
)
1620 "Move forward across one balanced item.
1621 With ARG, do it that many times. Negative arg -N means
1622 move backward across N balanced expressions.
1623 This is the equivalent of `forward-sexp' for XML.
1625 An element contains as items strings with no markup, tags, processing
1626 instructions, comments, CDATA sections, entity references and
1627 characters references. However, if the variable
1628 `nxml-sexp-element-flag' is non-nil, then an element is treated as a
1629 single markup item. A start-tag contains an element name followed by
1630 one or more attributes. An end-tag contains just an element name. An
1631 attribute value literals contains strings with no markup, entity
1632 references and character references. A processing instruction
1633 consists of a target and a content string. A comment or a CDATA
1634 section contains a single string. An entity reference contains a
1635 single name. A character reference contains a character number."
1637 (or arg
(setq arg
1))
1640 (nxml-forward-single-balanced-item)
1641 (> (setq arg
(1- arg
)) 0))))
1644 (nxml-backward-single-balanced-item)
1645 (< (setq arg
(1+ arg
)) 0))))))
1647 (defun nxml-forward-single-balanced-item ()
1649 (goto-char (let ((end (nxml-token-after)))
1651 (while (eq xmltok-type
'space
)
1653 (setq end
(nxml-token-after)))
1654 (cond ((/= (point) xmltok-start
)
1655 (nxml-scan-forward-within end
))
1656 ((and nxml-sexp-element-flag
1657 (eq xmltok-type
'start-tag
))
1658 ;; can't ever return nil here
1659 (nxml-scan-element-forward xmltok-start
))
1660 ((and nxml-sexp-element-flag
1662 '(end-tag partial-end-tag
)))
1663 (error "Already at end of element"))
1666 (goto-char (cadr err
))
1667 (apply 'error
(cddr err
)))))
1669 (defun nxml-backward-single-balanced-item ()
1671 (goto-char (let ((end (nxml-token-before)))
1673 (while (eq xmltok-type
'space
)
1674 (goto-char xmltok-start
)
1675 (setq end
(nxml-token-before)))
1676 (cond ((/= (point) end
)
1677 (nxml-scan-backward-within end
))
1678 ((and nxml-sexp-element-flag
1679 (eq xmltok-type
'end-tag
))
1680 ;; can't ever return nil here
1681 (nxml-scan-element-backward end
)
1683 ((and nxml-sexp-element-flag
1684 (eq xmltok-type
'start-tag
))
1685 (error "Already at start of element"))
1686 (t xmltok-start
)))))
1688 (goto-char (cadr err
))
1689 (apply 'error
(cddr err
)))))
1691 (defun nxml-scan-forward-within (end)
1692 (setq end
(- end
(nxml-end-delimiter-length xmltok-type
)))
1693 (when (<= end
(point))
1694 (error "Already at end of %s"
1695 (nxml-token-type-friendly-name xmltok-type
)))
1696 (cond ((memq xmltok-type
'(start-tag
1699 partial-empty-element
))
1700 (if (< (point) xmltok-name-end
)
1702 (let ((att (nxml-find-following-attribute)))
1703 (cond ((not att
) end
)
1704 ((and (xmltok-attribute-value-start att
)
1705 (<= (xmltok-attribute-value-start att
)
1707 (nxml-scan-forward-in-attribute-value att
))
1708 ((xmltok-attribute-value-end att
)
1709 (1+ (xmltok-attribute-value-end att
)))
1711 (goto-char (xmltok-attribute-name-end att
))
1712 (looking-at "[ \t\r\n]*="))
1714 (t (xmltok-attribute-name-end att
))))))
1715 ((and (eq xmltok-type
'processing-instruction
)
1716 (< (point) xmltok-name-end
))
1720 (defun nxml-scan-backward-within (end)
1723 (nxml-start-delimiter-length xmltok-type
)))
1724 (when (<= (point) xmltok-start
)
1725 (error "Already at start of %s"
1726 (nxml-token-type-friendly-name xmltok-type
)))
1727 (cond ((memq xmltok-type
'(start-tag
1730 partial-empty-element
))
1731 (let ((att (nxml-find-preceding-attribute)))
1732 (cond ((not att
) xmltok-start
)
1733 ((and (xmltok-attribute-value-start att
)
1734 (<= (xmltok-attribute-value-start att
)
1737 (xmltok-attribute-value-end att
)))
1738 (nxml-scan-backward-in-attribute-value att
))
1739 (t (xmltok-attribute-name-start att
)))))
1740 ((and (eq xmltok-type
'processing-instruction
)
1741 (let ((content-start (save-excursion
1742 (goto-char xmltok-name-end
)
1743 (skip-chars-forward " \r\t\n")
1745 (and (< content-start
(point))
1749 (defun nxml-scan-forward-in-attribute-value (att)
1750 (when (= (point) (xmltok-attribute-value-end att
))
1751 (error "Already at end of attribute value"))
1752 (let ((refs (xmltok-attribute-refs att
))
1755 (setq ref
(car refs
))
1756 (if (< (point) (aref ref
2))
1759 (setq refs
(cdr refs
))))
1761 (xmltok-attribute-value-end att
))
1762 ((< (point) (aref ref
1))
1764 ((= (point) (aref ref
1))
1767 (let ((end (- (aref ref
2)
1768 (nxml-end-delimiter-length (aref ref
0)))))
1771 (error "Already at end of %s"
1772 (nxml-token-type-friendly-name (aref ref
0)))))))))
1774 (defun nxml-scan-backward-in-attribute-value (att)
1775 (when (= (point) (xmltok-attribute-value-start att
))
1776 (error "Already at start of attribute value"))
1777 (let ((refs (reverse (xmltok-attribute-refs att
)))
1780 (setq ref
(car refs
))
1781 (if (< (aref ref
1) (point))
1784 (setq refs
(cdr refs
))))
1786 (xmltok-attribute-value-start att
))
1787 ((< (aref ref
2) (point))
1789 ((= (point) (aref ref
2))
1792 (let ((start (+ (aref ref
1)
1793 (nxml-start-delimiter-length (aref ref
0)))))
1794 (if (< start
(point))
1796 (error "Already at start of %s"
1797 (nxml-token-type-friendly-name (aref ref
0)))))))))
1799 (defun nxml-find-following-attribute ()
1801 (atts (or xmltok-attributes xmltok-namespace-attributes
))
1802 (more-atts (and xmltok-attributes xmltok-namespace-attributes
)))
1804 (let* ((att (car atts
))
1805 (name-start (xmltok-attribute-name-start att
)))
1806 (cond ((and (<= name-start
(point))
1807 (xmltok-attribute-value-end att
)
1808 ;; <= because end is before quote
1809 (<= (point) (xmltok-attribute-value-end att
)))
1812 ((and (< (point) name-start
)
1815 (xmltok-attribute-name-start ret
))))
1817 (setq atts
(cdr atts
))
1819 (setq atts more-atts
)
1820 (setq more-atts nil
)))
1823 (defun nxml-find-preceding-attribute ()
1825 (atts (or xmltok-attributes xmltok-namespace-attributes
))
1826 (more-atts (and xmltok-attributes xmltok-namespace-attributes
)))
1828 (let* ((att (car atts
))
1829 (name-start (xmltok-attribute-name-start att
)))
1830 (cond ((and (< name-start
(point))
1831 (xmltok-attribute-value-end att
)
1832 ;; <= because end is before quote
1833 (<= (point) (xmltok-attribute-value-end att
)))
1836 ((and (< name-start
(point))
1838 (< (xmltok-attribute-name-start ret
)
1841 (setq atts
(cdr atts
))
1843 (setq atts more-atts
)
1844 (setq more-atts nil
)))
1847 (defun nxml-up-element (&optional arg
)
1849 (or arg
(setq arg
1))
1851 (nxml-backward-up-element (- arg
))
1853 (while (and (> arg
0)
1854 (< (point) (point-max)))
1855 (let ((token-end (nxml-token-after)))
1856 (goto-char (cond ((or (memq xmltok-type
'(end-tag
1858 (and (memq xmltok-type
1860 partial-empty-element
))
1861 (< xmltok-start
(point))))
1863 ((nxml-scan-element-forward
1864 (if (and (eq xmltok-type
'start-tag
)
1865 (= (point) xmltok-start
))
1869 (t (error "No parent element")))))
1870 (setq arg
(1- arg
)))
1872 (goto-char (cadr err
))
1873 (apply 'error
(cddr err
))))))
1875 (defun nxml-backward-up-element (&optional arg
)
1877 (or arg
(setq arg
1))
1879 (nxml-up-element (- arg
))
1881 (while (and (> arg
0)
1882 (< (point-min) (point)))
1883 (let ((token-end (nxml-token-before)))
1884 (goto-char (cond ((or (memq xmltok-type
'(start-tag
1886 (and (memq xmltok-type
1888 partial-empty-element
))
1889 (< (point) token-end
)))
1891 ((nxml-scan-element-backward
1892 (if (and (eq xmltok-type
'end-tag
)
1893 (= (point) token-end
))
1898 (t (error "No parent element")))))
1899 (setq arg
(1- arg
)))
1901 (goto-char (cadr err
))
1902 (apply 'error
(cddr err
))))))
1904 (defun nxml-down-element (&optional arg
)
1905 "Move forward down into the content of an element.
1906 With ARG, do this that many times.
1907 Negative ARG means move backward but still down."
1909 (or arg
(setq arg
1))
1911 (nxml-backward-down-element (- arg
))
1914 (let ((token-end (nxml-token-after)))
1916 (goto-char token-end
)
1918 (when (memq xmltok-type
'(nil end-tag partial-end-tag
))
1919 (error "No following start-tags in this element"))
1920 (not (memq xmltok-type
'(start-tag partial-start-tag
))))
1921 (nxml-tokenize-forward))
1923 (setq arg
(1- arg
)))))
1925 (defun nxml-backward-down-element (&optional arg
)
1927 (or arg
(setq arg
1))
1929 (nxml-down-element (- arg
))
1934 (goto-char xmltok-start
)
1936 (when (memq xmltok-type
'(start-tag
1940 (error "No preceding end-tags in this element"))
1941 (not (memq xmltok-type
'(end-tag partial-end-tag
))))
1942 (if (or (<= (point) nxml-prolog-end
)
1943 (not (search-backward "<" nxml-prolog-end t
)))
1944 (setq xmltok-type nil
)
1945 (nxml-move-outside-backwards)
1948 (setq arg
(1- arg
)))))
1950 (defun nxml-forward-element (&optional arg
)
1951 "Move forward over one element.
1952 With ARG, do it that many times.
1953 Negative ARG means move backward."
1955 (or arg
(setq arg
1))
1957 (nxml-backward-element (- arg
))
1959 (while (and (> arg
0)
1960 (< (point) (point-max)))
1962 (or (nxml-scan-element-forward (nxml-token-before))
1963 (error "No more elements")))
1964 (setq arg
(1- arg
)))
1966 (goto-char (cadr err
))
1967 (apply 'error
(cddr err
))))))
1969 (defun nxml-backward-element (&optional arg
)
1970 "Move backward over one element.
1971 With ARG, do it that many times.
1972 Negative ARG means move forward."
1974 (or arg
(setq arg
1))
1976 (nxml-forward-element (- arg
))
1978 (while (and (> arg
0)
1979 (< (point-min) (point)))
1981 (or (and (nxml-scan-element-backward (progn
1985 (error "No preceding elements")))
1986 (setq arg
(1- arg
)))
1988 (goto-char (cadr err
))
1989 (apply 'error
(cddr err
))))))
1991 (defun nxml-mark-token-after ()
1993 (push-mark (nxml-token-after) nil t
)
1994 (goto-char xmltok-start
)
1995 (message "Marked %s" xmltok-type
))
1999 (defun nxml-mark-paragraph ()
2000 "Put point at beginning of this paragraph, mark at end.
2001 The paragraph marked is the one that contains point or follows point."
2003 (nxml-forward-paragraph)
2005 (nxml-backward-paragraph))
2007 (defun nxml-forward-paragraph (&optional arg
)
2009 (or arg
(setq arg
1))
2011 (nxml-backward-paragraph (- arg
)))
2014 (while (and (nxml-forward-single-paragraph)
2015 (> (setq arg
(1- arg
)) 0))))))
2017 (defun nxml-backward-paragraph (&optional arg
)
2019 (or arg
(setq arg
1))
2021 (nxml-forward-paragraph (- arg
)))
2024 (let ((inhibit-field-text-motion t
))
2026 (while (and (nxml-backward-single-paragraph)
2027 (> (setq arg
(1- arg
)) 0))))))
2029 (defun nxml-forward-single-paragraph ()
2030 "Move forward over a single paragraph.
2031 Return nil at end of buffer, t otherwise."
2032 (let* ((token-end (nxml-token-after))
2033 (offset (- (point) xmltok-start
))
2035 (goto-char token-end
)
2036 (while (and (< (point) (point-max))
2038 (nxml-paragraph-end-pos had-data offset
))))
2039 (when (nxml-token-contains-data-p offset
)
2041 (nxml-tokenize-forward)
2043 (when pos
(goto-char pos
))))
2045 (defun nxml-backward-single-paragraph ()
2046 "Move backward over a single paragraph.
2047 Return nil at start of buffer, t otherwise."
2048 (let* ((token-end (nxml-token-before))
2049 (offset (- token-end
(point)))
2050 (last-tag-pos xmltok-start
)
2051 pos had-data last-data-pos
)
2052 (goto-char token-end
)
2053 (unless (setq pos
(nxml-paragraph-start-pos nil offset
))
2054 (setq had-data
(nxml-token-contains-data-p nil offset
))
2055 (goto-char xmltok-start
)
2056 (while (and (not pos
) (< (point-min) (point)))
2057 (cond ((search-backward "<" nxml-prolog-end t
)
2058 (nxml-move-outside-backwards)
2060 (while (< (point) last-tag-pos
)
2062 (when (and (not had-data
) (nxml-token-contains-data-p))
2064 (setq last-data-pos xmltok-start
))
2065 (let ((tem (nxml-paragraph-start-pos had-data
0)))
2066 (when tem
(setq pos tem
)))))
2067 (when (and (not had-data
) last-data-pos
(not pos
))
2070 (while (< (point) last-data-pos
)
2072 (let ((tem (nxml-paragraph-start-pos had-data
0)))
2073 (when tem
(setq pos tem
)))))
2074 (setq last-tag-pos
(point)))
2075 (t (goto-char (point-min))))))
2076 (when pos
(goto-char pos
))))
2078 (defun nxml-token-contains-data-p (&optional start end
)
2079 (setq start
(+ xmltok-start
(or start
0)))
2080 (setq end
(- (point) (or end
0)))
2081 (when (eq xmltok-type
'cdata-section
)
2082 (setq start
(max start
(+ xmltok-start
9)))
2083 (setq end
(min end
(- (point) 3))))
2084 (or (and (eq xmltok-type
'data
)
2085 (eq start xmltok-start
)
2087 (eq xmltok-type
'char-ref
)
2088 (and (memq xmltok-type
'(data cdata-section
))
2092 (re-search-forward "[^ \t\r\n]" end t
)))))
2094 (defun nxml-paragraph-end-pos (had-data offset
)
2095 "Return the position of the paragraph end if contained in the current token.
2096 Return nil if the current token does not contain the paragraph end.
2097 Only characters after OFFSET from the start of the token are eligible.
2098 HAD-DATA says whether there have been non-whitespace data characters yet."
2099 (cond ((not had-data
)
2100 (cond ((memq xmltok-type
'(data cdata-section
))
2102 (let ((end (point)))
2103 (goto-char (+ xmltok-start
2104 (max (if (eq xmltok-type
'cdata-section
)
2108 (and (re-search-forward "[^ \t\r\n]" end t
)
2109 (re-search-forward "^[ \t]*$" end t
)
2110 (match-beginning 0)))))
2111 ((and (eq xmltok-type
'comment
)
2112 (nxml-token-begins-line-p)
2113 (nxml-token-ends-line-p))
2115 (let ((end (point)))
2116 (goto-char (+ xmltok-start
(max 4 offset
)))
2117 (when (re-search-forward "[^ \t\r\n]" (- end
3) t
)
2118 (if (re-search-forward "^[ \t]*$" end t
)
2120 (goto-char (- end
3))
2121 (skip-chars-backward " \t")
2123 (beginning-of-line 2))
2125 ((memq xmltok-type
'(data space cdata-section
))
2127 (let ((end (point)))
2128 (goto-char (+ xmltok-start offset
))
2129 (and (re-search-forward "^[ \t]*$" end t
)
2130 (match-beginning 0)))))
2131 ((and (memq xmltok-type
'(start-tag
2135 processing-instruction
2137 (nxml-token-begins-line-p)
2138 (nxml-token-ends-line-p))
2140 (goto-char xmltok-start
)
2141 (skip-chars-backward " \t")
2143 ((and (eq xmltok-type
'end-tag
)
2144 (looking-at "[ \t]*$")
2145 (not (nxml-in-mixed-content-p t
)))
2147 (or (search-forward "\n" nil t
)
2150 (defun nxml-paragraph-start-pos (had-data offset
)
2151 "Return the position of the paragraph start if contained in the current token.
2152 Return nil if the current token does not contain the paragraph start.
2153 Only characters before OFFSET from the end of the token are eligible.
2154 HAD-DATA says whether there have been non-whitespace data characters yet."
2155 (cond ((not had-data
)
2156 (cond ((memq xmltok-type
'(data cdata-section
))
2158 (goto-char (- (point)
2159 (max (if (eq xmltok-type
'cdata-section
)
2163 (and (re-search-backward "[^ \t\r\n]" xmltok-start t
)
2164 (re-search-backward "^[ \t]*$" xmltok-start t
)
2165 (match-beginning 0))))
2166 ((and (eq xmltok-type
'comment
)
2167 (nxml-token-ends-line-p)
2168 (nxml-token-begins-line-p))
2170 (goto-char (- (point) (max 3 offset
)))
2171 (when (and (< (+ xmltok-start
4) (point))
2172 (re-search-backward "[^ \t\r\n]"
2175 (if (re-search-backward "^[ \t]*$" xmltok-start t
)
2177 (goto-char xmltok-start
)
2178 (if (looking-at "<!--[ \t]*\n")
2180 (skip-chars-backward " \t")
2182 ((memq xmltok-type
'(data space cdata-section
))
2184 (goto-char (- (point) offset
))
2185 (and (re-search-backward "^[ \t]*$" xmltok-start t
)
2186 (match-beginning 0))))
2187 ((and (memq xmltok-type
'(start-tag
2191 processing-instruction
2193 (nxml-token-ends-line-p)
2194 (nxml-token-begins-line-p))
2195 (or (search-forward "\n" nil t
)
2197 ((and (eq xmltok-type
'start-tag
)
2198 (nxml-token-begins-line-p)
2199 (not (save-excursion
2200 (goto-char xmltok-start
)
2201 (nxml-in-mixed-content-p nil
))))
2203 (goto-char xmltok-start
)
2204 (skip-chars-backward " \t")
2205 ;; include any blank line before
2206 (or (and (eq (char-before) ?
\n)
2208 (goto-char (1- (point)))
2209 (skip-chars-backward " \t")
2210 (and (bolp) (point))))
2213 (defun nxml-token-ends-line-p () (looking-at "[ \t]*$"))
2215 (defun nxml-token-begins-line-p ()
2217 (goto-char xmltok-start
)
2218 (skip-chars-backward " \t")
2221 (defun nxml-in-mixed-content-p (endp)
2222 "Return non-nil if point is in mixed content.
2223 Point must be after an end-tag or before a start-tag.
2224 ENDP is t in the former case, nil in the latter."
2225 (let (matching-tag-pos)
2226 (cond ((not (run-hook-with-args-until-failure
2227 'nxml-in-mixed-content-hook
))
2229 ;; See if the matching tag does not start or end a line.
2230 ((condition-case err
2232 (setq matching-tag-pos
2235 (and (nxml-scan-element-backward (point))
2237 (nxml-scan-element-forward (point)))))
2238 (and matching-tag-pos
2240 (goto-char matching-tag-pos
)
2243 (skip-chars-backward " \t")
2245 (looking-at "[ \t]*$"))))))
2246 (nxml-scan-error nil
))
2248 ;; See if there's data at the same level.
2251 (setq start matching-tag-pos
2254 end matching-tag-pos
))
2258 (nxml-preceding-sibling-data-p))
2261 (nxml-following-sibling-data-p)))))
2263 ;; Otherwise, treat as not mixed
2266 (defun nxml-preceding-sibling-data-p ()
2267 "Return non-nil if there is a previous sibling that is data."
2268 (let ((lim (max (- (point) nxml-mixed-scan-distance
)
2274 (while (and (< lim
(point))
2279 (search-backward "<" lim t
)))
2280 (nxml-move-outside-backwards)
2283 (let ((prev-level level
))
2284 (cond ((eq xmltok-type
'end-tag
)
2285 (setq level
(1+ level
)))
2286 ((eq xmltok-type
'start-tag
)
2287 (setq level
(1- level
))))
2288 (when (eq prev-level
0)
2289 (while (and (< (point) end
) (not found
))
2291 (when (memq xmltok-type
'(data cdata-section char-ref
))
2292 (setq found t
)))))))))
2295 (defun nxml-following-sibling-data-p ()
2296 (let ((lim (min (+ (point) nxml-mixed-scan-distance
)
2302 (while (and (< (point) lim
)
2304 (nxml-tokenize-forward)
2306 (cond ((eq xmltok-type
'start-tag
)
2307 (setq level
(1+ level
)))
2308 ((eq xmltok-type
'end-tag
)
2309 (setq level
(1- level
)))
2311 (memq xmltok-type
'(data cdata-section char-ref
)))
2317 (defun nxml-do-fill-paragraph (arg)
2318 (let (fill-paragraph-function
2322 (nxml-forward-paragraph)
2324 (nxml-backward-paragraph)
2325 (skip-chars-forward " \t\r\n")
2326 (setq start
(point))
2328 (setq fill-prefix
(buffer-substring-no-properties (point) start
))
2329 (when (and (not (nxml-get-inside (point)))
2330 (looking-at "[ \t]*<!--"))
2331 (setq fill-prefix
(concat fill-prefix
" ")))
2332 (fill-region-as-paragraph start end arg
))
2333 (skip-line-prefix fill-prefix
)
2336 (defun nxml-newline-and-indent (soft)
2337 (delete-horizontal-space)
2338 (if soft
(insert-and-inherit ?
\n) (newline 1))
2344 (defvar nxml-dynamic-markup-prev-pos nil
)
2345 (defvar nxml-dynamic-markup-prev-lengths nil
)
2346 (defvar nxml-dynamic-markup-prev-found-marker nil
)
2347 (defvar nxml-dynamic-markup-prev-start-tags
(make-hash-table :test
'equal
))
2349 (defun nxml-dynamic-markup-word ()
2350 "Dynamically markup the word before point.
2351 This attempts to find a tag to put around the word before point based
2352 on the contents of the current buffer. The end-tag will be inserted at
2353 point. The start-tag will be inserted at or before the beginning of
2354 the word before point; the contents of the current buffer is used to
2357 It works in a similar way to \\[dabbrev-expand]. It searches first
2358 backwards from point, then forwards from point for an element whose
2359 content is a string which matches the contents of the buffer before
2360 point and which includes at least the word before point. It then
2361 copies the start- and end-tags from that element and uses them to
2362 surround the matching string before point.
2364 Repeating \\[nxml-dynamic-markup-word] immediately after successful
2365 \\[nxml-dynamic-markup-word] removes the previously inserted markup
2366 and attempts to find another possible way to do the markup."
2368 (let (search-start-pos done
)
2369 (if (and (integerp nxml-dynamic-markup-prev-pos
)
2370 (= nxml-dynamic-markup-prev-pos
(point))
2371 (eq last-command this-command
)
2372 nxml-dynamic-markup-prev-lengths
)
2373 (let* ((end-tag-open-pos
2374 (- nxml-dynamic-markup-prev-pos
2375 (nth 2 nxml-dynamic-markup-prev-lengths
)))
2376 (start-tag-close-pos
2378 (nth 1 nxml-dynamic-markup-prev-lengths
)))
2380 (- start-tag-close-pos
2381 (nth 0 nxml-dynamic-markup-prev-lengths
))))
2382 (delete-region end-tag-open-pos nxml-dynamic-markup-prev-pos
)
2383 (delete-region start-tag-open-pos start-tag-close-pos
)
2384 (setq search-start-pos
2385 (marker-position nxml-dynamic-markup-prev-found-marker
)))
2386 (clrhash nxml-dynamic-markup-prev-start-tags
))
2387 (setq nxml-dynamic-markup-prev-pos nil
)
2388 (setq nxml-dynamic-markup-prev-lengths nil
)
2389 (setq nxml-dynamic-markup-prev-found-marker nil
)
2392 (let* ((pos (point))
2395 (unless (< (point) pos
)
2396 (error "No word to markup"))
2397 (buffer-substring-no-properties (point) pos
)))
2398 (search (concat word
"</"))
2400 (when search-start-pos
2401 (goto-char search-start-pos
))
2402 (while (and (not done
)
2403 (or (and (< (point) pos
)
2404 (or (search-backward search nil t
)
2405 (progn (goto-char pos
) nil
)))
2406 (search-forward search nil t
)))
2407 (goto-char (- (match-end 0) 2))
2408 (setq done
(nxml-try-copy-markup pos
)))
2410 (error (if (zerop (hash-table-count
2411 nxml-dynamic-markup-prev-start-tags
))
2412 "No possible markup found for `%s'"
2413 "No more markup possibilities found for `%s'")
2416 (defun nxml-try-copy-markup (word-end-pos)
2418 (let ((end-tag-pos (point)))
2419 (when (and (not (nxml-get-inside end-tag-pos
))
2420 (search-backward "<" nil t
)
2421 (not (nxml-get-inside (point))))
2423 (when (and (eq xmltok-type
'start-tag
)
2424 (< (point) end-tag-pos
))
2425 (let* ((start-tag-close-pos (point))
2427 (buffer-substring-no-properties xmltok-start
2428 start-tag-close-pos
))
2432 (buffer-substring-no-properties start-tag-close-pos
2435 (goto-char word-end-pos
)
2437 (re-search-backward (concat
2438 (regexp-quote (car words
))
2442 (setq words
(cdr words
))
2443 (skip-chars-backward " \t\r\n"))
2444 (when (and (not words
)
2446 (skip-chars-forward " \t\r\n")
2447 (not (gethash (cons (point) start-tag
)
2448 nxml-dynamic-markup-prev-start-tags
)))
2449 (or (< end-tag-pos
(point))
2450 (< word-end-pos xmltok-start
)))
2451 (setq nxml-dynamic-markup-prev-found-marker
2452 (copy-marker end-tag-pos t
))
2453 (puthash (cons (point) start-tag
)
2455 nxml-dynamic-markup-prev-start-tags
)
2456 (setq nxml-dynamic-markup-prev-lengths
2457 (list (- start-tag-close-pos xmltok-start
)
2458 (- word-end-pos
(point))
2459 (+ (- xmltok-name-end xmltok-start
) 2)))
2460 (let ((name (xmltok-start-tag-qname)))
2462 (goto-char (+ word-end-pos
2463 (- start-tag-close-pos xmltok-start
)))
2464 (insert "</" name
">")
2465 (setq nxml-dynamic-markup-prev-pos
(point))))))))))
2470 (defvar nxml-char-name-ignore-case t
)
2472 (defvar nxml-char-name-alist nil
2473 "Alist of character names.
2474 Each member of the list has the form (NAME CODE . NAMESET),
2475 where NAME is a string naming a character, NAMESET is a symbol
2476 identifying a set of names and CODE is an integer specifying the
2477 Unicode scalar value of the named character.
2478 The NAME will only be used for completion if NAMESET has
2479 a non-nil `nxml-char-name-set-enabled' property.
2480 If NAMESET does does not have `nxml-char-name-set-defined' property,
2481 then it must have a `nxml-char-name-set-file' property and `load'
2482 will be applied to the value of this property if the nameset
2485 (defvar nxml-char-name-table
(make-hash-table :test
'eq
)
2486 "Hash table for mapping char codes to names.
2487 Each key is a Unicode scalar value.
2488 Each value is a list of pairs of the form (NAMESET . NAME),
2489 where NAMESET is a symbol identifying a set of names,
2490 and NAME is a string naming a character.")
2492 (defvar nxml-autoload-char-name-set-list nil
2493 "List of char namesets that can be autoloaded.")
2495 (defun nxml-enable-char-name-set (nameset)
2496 (put nameset
'nxml-char-name-set-enabled t
))
2498 (defun nxml-disable-char-name-set (nameset)
2499 (put nameset
'nxml-char-name-set-enabled nil
))
2501 (defun nxml-char-name-set-enabled-p (nameset)
2502 (get nameset
'nxml-char-name-set-enabled
))
2504 (defun nxml-autoload-char-name-set (nameset file
)
2505 (unless (memq nameset nxml-autoload-char-name-set-list
)
2506 (setq nxml-autoload-char-name-set-list
2507 (cons nameset nxml-autoload-char-name-set-list
)))
2508 (put nameset
'nxml-char-name-set-file file
))
2510 (defun nxml-define-char-name-set (nameset alist
)
2511 "Define a set of character names.
2512 NAMESET is a symbol identifying the set.
2513 Alist is a list where each member has the form (NAME CODE),
2514 where NAME is a string naming a character and code
2515 is an integer giving the Unicode scalar value of the character."
2516 (when (get nameset
'nxml-char-name-set-defined
)
2517 (error "Nameset `%s' already defined" nameset
))
2520 (let* ((name-code (car iter
))
2521 (name (car name-code
))
2522 (code (cadr name-code
)))
2524 (cons (cons nameset name
)
2525 (gethash code nxml-char-name-table
))
2526 nxml-char-name-table
))
2527 (setcdr (cdr (car iter
)) nameset
)
2528 (setq iter
(cdr iter
))))
2529 (setq nxml-char-name-alist
2530 (nconc alist nxml-char-name-alist
))
2531 (put nameset
'nxml-char-name-set-defined t
))
2533 (defun nxml-get-char-name (code)
2534 (mapc 'nxml-maybe-load-char-name-set nxml-autoload-char-name-set-list
)
2535 (let ((names (gethash code nxml-char-name-table
))
2537 (while (and names
(not name
))
2538 (if (nxml-char-name-set-enabled-p (caar names
))
2539 (setq name
(cdar names
))
2540 (setq names
(cdr names
))))
2543 (defvar nxml-named-char-history nil
)
2545 (defun nxml-insert-named-char (arg)
2546 "Insert a character using its name.
2547 The name is read from the minibuffer.
2548 Normally, inserts the character as a numeric character reference.
2549 With a prefix argument, inserts the character directly."
2551 (mapc 'nxml-maybe-load-char-name-set nxml-autoload-char-name-set-list
)
2553 (let ((completion-ignore-case nxml-char-name-ignore-case
))
2554 (completing-read "Character name: "
2555 nxml-char-name-alist
2557 (get (cddr member
) 'nxml-char-name-set-enabled
))
2560 'nxml-named-char-history
)))
2561 (alist nxml-char-name-alist
)
2563 (while (and alist
(not code
))
2564 (setq elt
(assoc name alist
))
2565 (if (get (cddr elt
) 'nxml-char-name-set-enabled
)
2566 (setq code
(cadr elt
))
2567 (setq alist
(cdr (member elt alist
)))))
2570 (or (decode-char 'ucs code
)
2571 (error "Character %x is not supported by Emacs"
2573 (format "&#x%X;" code
))))))
2575 (defun nxml-maybe-load-char-name-set (sym)
2576 (when (and (get sym
'nxml-char-name-set-enabled
)
2577 (not (get sym
'nxml-char-name-set-defined
))
2578 (stringp (get sym
'nxml-char-name-set-file
)))
2579 (load (get sym
'nxml-char-name-set-file
))))
2581 (defun nxml-toggle-char-ref-extra-display (arg)
2582 "*Toggle the display of extra information for character references."
2584 (let ((new (if (null arg
)
2585 (not nxml-char-ref-extra-display
)
2586 (> (prefix-numeric-value arg
) 0))))
2587 (when (not (eq new nxml-char-ref-extra-display
))
2588 (setq nxml-char-ref-extra-display new
)
2589 (font-lock-fontify-buffer))))
2591 (put 'nxml-char-ref
'evaporate t
)
2593 (defun nxml-char-ref-display-extra (start end n
)
2594 (when nxml-char-ref-extra-display
2595 (let ((name (nxml-get-char-name n
))
2596 (glyph-string (and nxml-char-ref-display-glyph-flag
2597 (nxml-glyph-display-string n
'nxml-glyph
)))
2599 (when (or name glyph-string
)
2600 (setq ov
(make-overlay start end nil t
))
2601 (overlay-put ov
'category
'nxml-char-ref
)
2603 (overlay-put ov
'help-echo name
))
2607 (propertize glyph-string
'face
'nxml-glyph
)))))))
2609 (defun nxml-clear-char-ref-extra-display (start end
)
2610 (let ((ov (overlays-in start end
)))
2612 (when (eq (overlay-get (car ov
) 'category
) 'nxml-char-ref
)
2613 (delete-overlay (car ov
)))
2614 (setq ov
(cdr ov
)))))
2617 (defun nxml-start-delimiter-length (type)
2618 (or (get type
'nxml-start-delimiter-length
)
2621 (put 'cdata-section
'nxml-start-delimiter-length
9)
2622 (put 'comment
'nxml-start-delimiter-length
4)
2623 (put 'processing-instruction
'nxml-start-delimiter-length
2)
2624 (put 'start-tag
'nxml-start-delimiter-length
1)
2625 (put 'empty-element
'nxml-start-delimiter-length
1)
2626 (put 'partial-empty-element
'nxml-start-delimiter-length
1)
2627 (put 'entity-ref
'nxml-start-delimiter-length
1)
2628 (put 'char-ref
'nxml-start-delimiter-length
2)
2630 (defun nxml-end-delimiter-length (type)
2631 (or (get type
'nxml-end-delimiter-length
)
2634 (put 'cdata-section
'nxml-end-delimiter-length
3)
2635 (put 'comment
'nxml-end-delimiter-length
3)
2636 (put 'processing-instruction
'nxml-end-delimiter-length
2)
2637 (put 'start-tag
'nxml-end-delimiter-length
1)
2638 (put 'empty-element
'nxml-end-delimiter-length
2)
2639 (put 'partial-empty-element
'nxml-end-delimiter-length
1)
2640 (put 'entity-ref
'nxml-end-delimiter-length
1)
2641 (put 'char-ref
'nxml-end-delimiter-length
1)
2643 (defun nxml-token-type-friendly-name (type)
2644 (or (get type
'nxml-friendly-name
)
2645 (symbol-name type
)))
2647 (put 'cdata-section
'nxml-friendly-name
"CDATA section")
2648 (put 'processing-instruction
'nxml-friendly-name
"processing instruction")
2649 (put 'entity-ref
'nxml-friendly-name
"entity reference")
2650 (put 'char-ref
'nxml-friendly-name
"character reference")
2652 (provide 'nxml-mode
)
2654 ;; arch-tag: 8603bc5f-1ef9-4021-b223-322fb2ca708e
2655 ;;; nxml-mode.el ends here