1 ;;; nxml-mode.el --- a new XML mode
3 ;; Copyright (C) 2003-2004, 2007-2013 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; See nxml-rap.el for description of parsing strategy.
29 (when (featurep 'mucs
)
30 (error "nxml-mode is not compatible with Mule-UCS"))
32 (eval-when-compile (require 'cl-lib
))
40 ;; nxml-mode calls rng-nxml-mode-init, which is autoloaded from rng-nxml.
41 ;; So we might as well just require it and silence the compiler.
42 (provide 'nxml-mode
) ; avoid recursive require
48 "New XML editing mode."
51 (defgroup nxml-faces nil
52 "Faces for XML syntax highlighting."
55 (defcustom nxml-char-ref-display-glyph-flag t
56 "Non-nil means display glyph following character reference.
57 The glyph is displayed in face `nxml-glyph'. The abnormal hook
58 `nxml-glyph-set-functions' can be used to change the characters
59 for which glyphs are displayed."
63 (defcustom nxml-sexp-element-flag nil
64 "Non-nil means sexp commands treat an element as a single expression."
68 (defcustom nxml-slash-auto-complete-flag nil
69 "Non-nil means typing a slash automatically completes the end-tag.
70 This is used by `nxml-electric-slash'."
74 (defcustom nxml-child-indent
2
75 "Indentation for the children of an element relative to the start-tag.
76 This only applies when the line or lines containing the start-tag contains
77 nothing else other than that start-tag."
81 (defcustom nxml-attribute-indent
4
82 "Indentation for the attributes of an element relative to the start-tag.
83 This only applies when the first attribute of a tag starts a line.
84 In other cases, the first attribute on one line is indented the same
85 as the first attribute on the previous line."
89 (defcustom nxml-bind-meta-tab-to-complete-flag t
90 "Non-nil means to use nXML completion in \\[completion-at-point]."
94 (defcustom nxml-prefer-utf-16-to-utf-8-flag nil
95 "Non-nil means prefer UTF-16 to UTF-8 when saving a buffer.
96 This is used only when a buffer does not contain an encoding declaration
97 and when its current `buffer-file-coding-system' specifies neither UTF-16
102 (defcustom nxml-prefer-utf-16-little-to-big-endian-flag
(eq system-type
104 "Non-nil means prefer little-endian to big-endian byte-order for UTF-16.
105 This is used only for saving a buffer; when reading the byte-order is
106 auto-detected. It may be relevant both when there is no encoding declaration
107 and when the encoding declaration specifies `UTF-16'."
111 (defcustom nxml-default-buffer-file-coding-system nil
112 "Default value for `buffer-file-coding-system' for a buffer for a new file.
113 A value of nil means use the default value of `buffer-file-coding-system' as normal.
114 A buffer's `buffer-file-coding-system' affects what \\[nxml-insert-xml-declaration] inserts."
116 :type
'coding-system
)
118 (defcustom nxml-auto-insert-xml-declaration-flag nil
119 "Non-nil means automatically insert an XML declaration in a new file.
120 The XML declaration is inserted using `nxml-insert-xml-declaration'."
124 (defface nxml-delimited-data
125 '((t (:inherit font-lock-doc-face
)))
126 "Face used to highlight data enclosed between delimiters.
127 This is not used directly, but only via inheritance by other faces."
131 '((t (:inherit font-lock-builtin-face
)))
132 "Face used to highlight various names.
133 This includes element and attribute names, processing
134 instruction targets and the CDATA keyword in a CDATA section.
135 This is not used directly, but only via inheritance by other faces."
139 '((t (:inherit font-lock-constant-face
)))
140 "Face used to highlight character and entity references.
141 This is not used directly, but only via inheritance by other faces."
144 (defface nxml-delimiter
146 "Face used to highlight delimiters.
147 This is not used directly, but only via inheritance by other faces."
152 "Face used to highlight text."
155 (defface nxml-comment-content
156 '((t (:inherit font-lock-comment-face
)))
157 "Face used to highlight the content of comments."
160 (defface nxml-comment-delimiter
161 '((t (:inherit font-lock-comment-delimiter-face
)))
162 "Face used for the delimiters of comments, i.e <!-- and -->."
165 (defface nxml-processing-instruction-delimiter
166 '((t (:inherit nxml-delimiter
)))
167 "Face used for the delimiters of processing instructions, i.e <? and ?>."
170 (defface nxml-processing-instruction-target
171 '((t (:inherit font-lock-keyword-face
)))
172 "Face used for the target of processing instructions."
175 (defface nxml-processing-instruction-content
176 '((t (:inherit nxml-delimited-data
)))
177 "Face used for the content of processing instructions."
180 (defface nxml-cdata-section-delimiter
181 '((t (:inherit nxml-delimiter
)))
182 "Face used for the delimiters of CDATA sections, i.e <![, [, and ]]>."
185 (defface nxml-cdata-section-CDATA
186 '((t (:inherit nxml-name
)))
187 "Face used for the CDATA keyword in CDATA sections."
190 (defface nxml-cdata-section-content
191 '((t (:inherit nxml-text
)))
192 "Face used for the content of CDATA sections."
195 (defface nxml-char-ref-number
196 '((t (:inherit nxml-ref
)))
197 "Face used for the number in character references.
198 This includes ths `x' in hex references."
201 (defface nxml-char-ref-delimiter
202 '((t (:inherit nxml-ref
)))
203 "Face used for the delimiters of character references, i.e &# and ;."
206 (defface nxml-entity-ref-name
207 '((t (:inherit nxml-ref
)))
208 "Face used for the entity name in general entity references."
211 (defface nxml-entity-ref-delimiter
212 '((t (:inherit nxml-ref
)))
213 "Face used for the delimiters of entity references, i.e & and ;."
216 (defface nxml-tag-delimiter
217 '((t (:inherit nxml-delimiter
)))
218 "Face used for the angle brackets delimiting tags.
219 `nxml-tag-slash' is used for slashes."
222 (defface nxml-tag-slash
223 '((t (:inherit nxml-tag-delimiter
)))
224 "Face used for slashes in tags, both in end-tags and empty-elements."
227 (defface nxml-element-prefix
228 '((t (:inherit nxml-name
)))
229 "Face used for the prefix of elements."
232 (defface nxml-element-colon
234 "Face used for the colon in element names."
237 (defface nxml-element-local-name
238 '((t (:inherit font-lock-function-name-face
)))
239 "Face used for the local name of elements."
242 (defface nxml-attribute-prefix
243 '((t (:inherit nxml-name
)))
244 "Face used for the prefix of attributes."
247 (defface nxml-attribute-colon
248 '((t (:inherit nxml-delimiter
)))
249 "Face used for the colon in attribute names."
252 (defface nxml-attribute-local-name
253 '((t (:inherit font-lock-variable-name-face
)))
254 "Face used for the local name of attributes."
257 (defface nxml-namespace-attribute-xmlns
258 '((t (:inherit nxml-attribute-prefix
)))
259 "Face used for `xmlns' in namespace attributes."
262 (defface nxml-namespace-attribute-colon
263 '((t (:inherit nxml-attribute-colon
)))
264 "Face used for the colon in namespace attributes."
267 (defface nxml-namespace-attribute-prefix
268 '((t (:inherit nxml-attribute-local-name
)))
269 "Face used for the prefix declared in namespace attributes."
272 (defface nxml-attribute-value
273 '((t (:inherit font-lock-string-face
)))
274 "Face used for the value of attributes."
277 (defface nxml-attribute-value-delimiter
278 '((t (:inherit nxml-attribute-value
)))
279 "Face used for the delimiters of attribute values."
282 (defface nxml-namespace-attribute-value
283 '((t (:inherit nxml-attribute-value
)))
284 "Face used for the value of namespace attributes."
287 (defface nxml-namespace-attribute-value-delimiter
288 '((t (:inherit nxml-attribute-value-delimiter
)))
289 "Face used for the delimiters of namespace attribute values."
292 (defface nxml-prolog-literal-delimiter
293 '((t (:inherit nxml-delimited-data
)))
294 "Face used for the delimiters of literals in the prolog."
297 (defface nxml-prolog-literal-content
298 '((t (:inherit nxml-delimited-data
)))
299 "Face used for the content of literals in the prolog."
302 (defface nxml-prolog-keyword
303 '((t (:inherit font-lock-keyword-face
)))
304 "Face used for keywords in the prolog."
307 (defface nxml-markup-declaration-delimiter
308 '((t (:inherit nxml-delimiter
)))
309 "Face used for the delimiters of markup declarations in the prolog.
310 The delimiters are <! and >."
314 '((t (:inherit nxml-name
)))
315 "Face used for # before a name in the prolog."
339 "Face used for glyph for char references."
344 (defvar nxml-parent-document nil
345 "The parent document for a part of a modular document.
346 Use `nxml-parent-document-set' to set it.")
347 (make-variable-buffer-local 'nxml-parent-document
)
348 (put 'nxml-parent-document
'safe-local-variable
'stringp
)
350 (defvar nxml-prolog-regions nil
351 "List of regions in the prolog to be fontified.
352 See the function `xmltok-forward-prolog' for more information.")
353 (make-variable-buffer-local 'nxml-prolog-regions
)
355 (defvar nxml-last-fontify-end nil
356 "Position where fontification last ended.
357 It is nil if the buffer changed since the last fontification.")
358 (make-variable-buffer-local 'nxml-last-fontify-end
)
360 (defvar nxml-degraded nil
361 "Non-nil if currently operating in degraded mode.
362 Degraded mode is enabled when an internal error is encountered in the
363 fontification or after-change functions.")
364 (make-variable-buffer-local 'nxml-degraded
)
366 (defvar nxml-completion-hook nil
367 "Hook run by `nxml-complete'.
368 This hook is run until success.")
370 (defvar nxml-in-mixed-content-hook nil
371 "Hook to determine whether point is in mixed content.
372 The hook is called without arguments. It should return nil if it is
373 definitely not mixed; non-nil otherwise. The hook will be run until
374 one of the functions returns nil.")
376 (defvar nxml-mixed-scan-distance
4000
377 "Maximum distance from point to scan when checking for mixed content.")
379 (defvar nxml-end-tag-indent-scan-distance
4000
380 "Maximum distance from point to scan backwards when indenting end-tag.")
382 (defvar nxml-char-ref-extra-display t
383 "Non-nil means display extra information for character references.
384 The extra information consists of a tooltip with the character name
385 and, if `nxml-char-ref-display-glyph-flag' is non-nil, a glyph
386 corresponding to the referenced character following the character
388 (make-variable-buffer-local 'nxml-char-ref-extra-display
)
390 (defvar nxml-mode-map
391 (let ((map (make-sparse-keymap)))
392 (define-key map
"\M-\C-u" 'nxml-backward-up-element
)
393 (define-key map
"\M-\C-d" 'nxml-down-element
)
394 (define-key map
"\M-\C-n" 'nxml-forward-element
)
395 (define-key map
"\M-\C-p" 'nxml-backward-element
)
396 (define-key map
"\M-{" 'nxml-backward-paragraph
)
397 (define-key map
"\M-}" 'nxml-forward-paragraph
)
398 (define-key map
"\M-h" 'nxml-mark-paragraph
)
399 (define-key map
"\C-c\C-f" 'nxml-finish-element
)
400 (define-key map
"\C-c]" 'nxml-finish-element
)
401 (define-key map
"\C-c/" 'nxml-finish-element
)
402 (define-key map
"\C-c\C-m" 'nxml-split-element
)
403 (define-key map
"\C-c\C-b" 'nxml-balanced-close-start-tag-block
)
404 (define-key map
"\C-c\C-i" 'nxml-balanced-close-start-tag-inline
)
405 (define-key map
"\C-c\C-x" 'nxml-insert-xml-declaration
)
406 (define-key map
"\C-c\C-d" 'nxml-dynamic-markup-word
)
408 (define-key map
"\C-c\C-u" 'nxml-insert-named-char
)
409 (define-key map
"\C-c\C-o" nxml-outline-prefix-map
)
410 (define-key map
[S-mouse-2
] 'nxml-mouse-hide-direct-text-content
)
411 (define-key map
"/" 'nxml-electric-slash
)
412 (define-key map
"\M-\t" 'completion-at-point
)
414 "Keymap for nxml-mode.")
416 (defvar nxml-font-lock-keywords
417 '(nxml-fontify-matcher)
418 "Default font lock keywords for nxml-mode.")
420 (defsubst nxml-set-face
(start end face
)
421 (when (and face
(< start end
))
422 (font-lock-append-text-property start end
'face face
)))
424 (defun nxml-parent-document-set (parent-document)
425 "Set `nxml-parent-document' and inherit the DTD &c."
426 ;; FIXME: this does not work.
427 ;; the idea is that by inheriting some variables from the parent,
428 ;; `rng-validate-mode' will validate entities declared in the parent.
429 ;; alas, the most interesting variables (`rng-compile-table' et al)
430 ;; are circular and cannot be printed even with `print-circle'.
431 (interactive "fParent document")
432 (let (dtd current-schema current-schema-file-name compile-table
433 ipattern-table last-ipattern-index
)
434 (when (string= (file-truename parent-document
)
435 (file-truename buffer-file-name
))
436 (error "Parent document cannot be the same as the document"))
437 (with-current-buffer (find-file-noselect parent-document
)
439 current-schema rng-current-schema
440 current-schema-file-name rng-current-schema-file-name
441 compile-table rng-compile-table
442 ipattern-table rng-ipattern-table
443 last-ipattern-index rng-last-ipattern-index
444 parent-document buffer-file-name
))
446 rng-current-schema current-schema
447 rng-current-schema-file-name current-schema-file-name
448 rng-compile-table compile-table
449 rng-ipattern-table ipattern-table
450 rng-last-ipattern-index last-ipattern-index
451 nxml-parent-document parent-document
)
452 (message "Set parent document to %s" parent-document
)
453 (when rng-validate-mode
454 (rng-validate-while-idle (current-buffer)))))
457 (define-derived-mode nxml-mode text-mode
"nXML"
458 ;; We use C-c C-i instead of \\[nxml-balanced-close-start-tag-inline]
459 ;; because Emacs turns C-c C-i into C-c TAB which is hard to type and
461 "Major mode for editing XML.
463 \\[nxml-finish-element] finishes the current element by inserting an end-tag.
464 C-c C-i closes a start-tag with `>' and then inserts a balancing end-tag
465 leaving point between the start-tag and end-tag.
466 \\[nxml-balanced-close-start-tag-block] is similar but for block rather than inline elements:
467 the start-tag, point, and end-tag are all left on separate lines.
468 If `nxml-slash-auto-complete-flag' is non-nil, then inserting a `</'
469 automatically inserts the rest of the end-tag.
471 \\[completion-at-point] performs completion on the symbol preceding point.
473 \\[nxml-dynamic-markup-word] uses the contents of the current buffer
474 to choose a tag to put around the word preceding point.
476 Sections of the document can be displayed in outline form. The
477 variable `nxml-section-element-name-regexp' controls when an element
478 is recognized as a section. The same key sequences that change
479 visibility in outline mode are used except that they start with C-c C-o
482 Validation is provided by the related minor-mode `rng-validate-mode'.
483 This also makes completion schema- and context- sensitive. Element
484 names, attribute names, attribute values and namespace URIs can all be
485 completed. By default, `rng-validate-mode' is automatically enabled.
486 You can toggle it using \\[rng-validate-mode] or change the default by
487 customizing `rng-nxml-auto-validate-flag'.
489 \\[indent-for-tab-command] indents the current line appropriately.
490 This can be customized using the variable `nxml-child-indent'
491 and the variable `nxml-attribute-indent'.
493 \\[nxml-insert-named-char] inserts a character reference using
494 the character's name (by default, the Unicode name).
495 \\[universal-argument] \\[nxml-insert-named-char] inserts the character directly.
497 The Emacs commands that normally operate on balanced expressions will
498 operate on XML markup items. Thus \\[forward-sexp] will move forward
499 across one markup item; \\[backward-sexp] will move backward across
500 one markup item; \\[kill-sexp] will kill the following markup item;
501 \\[mark-sexp] will mark the following markup item. By default, each
502 tag each treated as a single markup item; to make the complete element
503 be treated as a single markup item, set the variable
504 `nxml-sexp-element-flag' to t. For more details, see the function
505 `nxml-forward-balanced-item'.
507 \\[nxml-backward-up-element] and \\[nxml-down-element] move up and down the element structure.
509 Many aspects this mode can be customized using
510 \\[customize-group] nxml RET."
511 ;; (kill-all-local-variables)
512 (set (make-local-variable 'mode-line-process
) '((nxml-degraded "/degraded")))
513 ;; We'll determine the fill prefix ourselves
514 (make-local-variable 'adaptive-fill-mode
)
515 (setq adaptive-fill-mode nil
)
516 (make-local-variable 'forward-sexp-function
)
517 (setq forward-sexp-function
'nxml-forward-balanced-item
)
518 (make-local-variable 'indent-line-function
)
519 (setq indent-line-function
'nxml-indent-line
)
520 (make-local-variable 'fill-paragraph-function
)
521 (setq fill-paragraph-function
'nxml-do-fill-paragraph
)
523 ;; This doesn't seem to work too well;
524 ;; I think we should probably roll our own nxml-comment-dwim function.
525 (make-local-variable 'comment-indent-function
)
526 (setq comment-indent-function
'nxml-indent-line
)
527 (make-local-variable 'comment-start
)
528 (setq comment-start
"<!--")
529 (make-local-variable 'comment-start-skip
)
530 (setq comment-start-skip
"<!--[ \t\r\n]*")
531 (make-local-variable 'comment-end
)
532 (setq comment-end
"-->")
533 (make-local-variable 'comment-end-skip
)
534 (setq comment-end-skip
"[ \t\r\n]*-->")
535 (make-local-variable 'comment-line-break-function
)
536 (setq comment-line-break-function
'nxml-newline-and-indent
)
537 (use-local-map nxml-mode-map
)
541 (nxml-clear-dependent-regions (point-min) (point-max))
542 (setq nxml-scan-end
(copy-marker (point-min) nil
))
543 (nxml-with-unmodifying-text-property-changes
544 (nxml-clear-inside (point-min) (point-max))
545 (nxml-with-invisible-motion
546 (nxml-scan-prolog)))))
547 (add-hook 'completion-at-point-functions
548 #'nxml-completion-at-point-function nil t
)
549 (add-hook 'after-change-functions
'nxml-after-change nil t
)
550 (add-hook 'change-major-mode-hook
'nxml-cleanup nil t
)
552 ;; Emacs 23 handles the encoding attribute on the xml declaration
553 ;; transparently to nxml-mode, so there is no longer a need for the below
554 ;; hook. The hook also had the drawback of overriding explicit user
555 ;; instruction to save as some encoding other than utf-8.
556 ;;; (add-hook 'write-contents-hooks 'nxml-prepare-to-save)
557 (when (not (and (buffer-file-name) (file-exists-p (buffer-file-name))))
558 (when (and nxml-default-buffer-file-coding-system
559 (not (local-variable-p 'buffer-file-coding-system
)))
560 (setq buffer-file-coding-system nxml-default-buffer-file-coding-system
))
561 (when nxml-auto-insert-xml-declaration-flag
562 (nxml-insert-xml-declaration)))
564 (setq font-lock-defaults
565 '(nxml-font-lock-keywords
566 t
; keywords-only; we highlight comments and strings here
567 nil
; font-lock-keywords-case-fold-search. XML is case sensitive
568 nil
; no special syntax table
569 nil
; no automatic syntactic fontification
570 (font-lock-extend-after-change-region-function
571 . nxml-extend-after-change-region
)
572 (font-lock-extend-region-functions .
(nxml-extend-region))
573 (jit-lock-contextually . t
)
574 (font-lock-unfontify-region-function . nxml-unfontify-region
)))
577 (nxml-enable-unicode-char-name-sets))
579 (defun nxml-cleanup ()
580 "Clean up after nxml-mode."
581 ;; Disable associated minor modes.
582 (rng-validate-mode -
1)
583 ;; Clean up fontification.
586 (let ((inhibit-read-only t
)
588 (modified (buffer-modified-p)))
589 (nxml-with-invisible-motion
590 (remove-text-properties (point-min) (point-max) '(face)))
591 (set-buffer-modified-p modified
)))
592 (remove-hook 'change-major-mode-hook
'nxml-cleanup t
))
594 (defun nxml-degrade (context err
)
595 (message "Internal nXML mode error in %s (%s), degrading"
597 (error-message-string err
))
599 (setq nxml-degraded t
)
600 (setq nxml-prolog-end
1)
604 (nxml-with-unmodifying-text-property-changes
605 (nxml-clear-inside (point-min) (point-max))))))
607 ;;; Change management
609 (defun nxml-debug-region (start end
)
611 (let ((font-lock-beg start
)
614 (goto-char font-lock-beg
)
615 (set-mark font-lock-end
)))
617 (defun nxml-after-change (start end pre-change-length
)
618 ; In font-lock mode, nxml-after-change1 is called via
619 ; nxml-extend-after-change-region instead so that the updated
620 ; book-keeping information is available for fontification.
621 (unless (or font-lock-mode nxml-degraded
)
622 (nxml-with-degradation-on-error 'nxml-after-change
627 (nxml-with-invisible-motion
628 (nxml-with-unmodifying-text-property-changes
630 start end pre-change-length
)))))))))
632 (defun nxml-after-change1 (start end pre-change-length
)
633 "After-change bookkeeping.
634 Returns a cons cell containing a possibly-enlarged change region.
635 You must call `nxml-extend-region' on this expanded region to obtain
636 the full extent of the area needing refontification.
638 For bookkeeping, call this function even when fontification is
640 (let ((pre-change-end (+ start pre-change-length
)))
642 (nxml-adjust-start-for-dependent-regions start
645 ;; If the prolog might have changed, rescan the prolog
647 ;; Add 2 so as to include the < and following char that
648 ;; start the instance (document element), since changing
649 ;; these can change where the prolog ends.
650 (+ nxml-prolog-end
2))
651 ;; end must be extended to at least the end of the old prolog in
652 ;; case the new prolog is shorter
653 (when (< pre-change-end nxml-prolog-end
)
655 ;; don't let end get out of range even if pre-change-length
658 (+ end
(- nxml-prolog-end pre-change-end
)))))
660 (setq start
(point-min))))
662 (when (> end nxml-prolog-end
)
664 (nxml-move-tag-backwards (point-min))
666 (setq end
(max (nxml-scan-after-change start end
)
669 (nxml-debug-change "nxml-after-change1" 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-unfontify-region (start end
)
858 (font-lock-default-unfontify-region start end
)
859 (nxml-clear-char-ref-extra-display start end
))
861 (defvar font-lock-beg
) (defvar font-lock-end
)
862 (defun nxml-extend-region ()
863 "Extend the region to hold the minimum area we can fontify with nXML.
864 Called with `font-lock-beg' and `font-lock-end' dynamically bound."
865 (let ((start font-lock-beg
)
868 (nxml-debug-change "nxml-extend-region(input)" start end
)
870 (when (< start nxml-prolog-end
)
871 (setq start
(point-min)))
873 (cond ((<= end nxml-prolog-end
)
874 (setq end nxml-prolog-end
))
878 ;; some font-lock backends (like Emacs 22 jit-lock) snap
879 ;; the region to the beginning of the line no matter what
880 ;; we say here. To mitigate the resulting excess
881 ;; fontification, ignore leading whitespace.
882 (skip-syntax-forward " ")
884 ;; find the beginning of the previous tag
885 (when (not (equal (char-after) ?\
<))
886 (search-backward "<" nxml-prolog-end t
))
887 (nxml-ensure-scan-up-to-date)
888 (nxml-move-outside-backwards)
891 (while (< (point) end
)
892 (nxml-tokenize-forward))
896 (when (or (< start font-lock-beg
)
897 (> end font-lock-end
))
898 (setq font-lock-beg start
900 (nxml-debug-change "nxml-extend-region" start end
)
903 (defun nxml-extend-after-change-region (start end pre-change-length
)
904 (unless nxml-degraded
905 (setq nxml-last-fontify-end nil
)
906 (let ((region (nxml-with-degradation-on-error
907 'nxml-extend-after-change-region
912 (nxml-with-invisible-motion
913 (nxml-with-unmodifying-text-property-changes
914 (nxml-extend-after-change-region1
915 start end pre-change-length
)))))))))
916 (if (consp region
) region
))))
918 (defun nxml-extend-after-change-region1 (start end pre-change-length
)
919 (let* ((region (nxml-after-change1 start end pre-change-length
))
920 (font-lock-beg (car region
))
921 (font-lock-end (cdr region
)))
924 (cons font-lock-beg font-lock-end
)))
926 (defun nxml-fontify-matcher (bound)
927 "Called as font-lock keyword matcher."
929 (unless nxml-degraded
930 (nxml-debug-change "nxml-fontify-matcher" (point) bound
)
932 (when (< (point) nxml-prolog-end
)
933 ;; Prolog needs to be fontified in one go, and
934 ;; nxml-extend-region makes sure we start at BOB.
936 (nxml-fontify-prolog)
937 (goto-char nxml-prolog-end
))
939 (let (xmltok-dependent-regions
941 (while (and (nxml-tokenize-forward)
942 (<= (point) bound
)) ; Intervals are open-ended.
943 (nxml-apply-fontify-rule)))
945 (setq nxml-last-fontify-end
(point)))
947 ;; Since we did the fontification internally, tell font-lock to not
948 ;; do anything itself.
951 (defun nxml-fontify-prolog ()
953 The buffer is assumed to be prepared for fontification.
954 This does not set the fontified property, but it does clear
955 faces appropriately."
956 (let ((regions nxml-prolog-regions
))
958 (let ((region (car regions
)))
959 (nxml-apply-fontify-rule (aref region
0)
962 (setq regions
(cdr regions
)))))
964 ;; Vectors identify a substring of the token to be highlighted in some face.
966 ;; Token types returned by xmltok-forward.
970 '([nil
1 nxml-tag-delimiter
]
971 [-
1 nil nxml-tag-delimiter
]
975 (put 'partial-start-tag
977 '([nil
1 nxml-tag-delimiter
]
983 '([nil
1 nxml-tag-delimiter
]
985 [-
1 nil nxml-tag-delimiter
]
986 (element-qname .
2)))
988 (put 'partial-end-tag
990 '([nil
1 nxml-tag-delimiter
]
992 (element-qname .
2)))
996 '([nil
1 nxml-tag-delimiter
]
997 [-
2 -
1 nxml-tag-slash
]
998 [-
1 nil nxml-tag-delimiter
]
1002 (put 'partial-empty-element
1004 '([nil
1 nxml-tag-delimiter
]
1005 [-
1 nil nxml-tag-slash
]
1011 '([nil
2 nxml-char-ref-delimiter
]
1012 [2 -
1 nxml-char-ref-number
]
1013 [-
1 nil nxml-char-ref-delimiter
]
1018 '([nil
1 nxml-entity-ref-delimiter
]
1019 [1 -
1 nxml-entity-ref-name
]
1020 [-
1 nil nxml-entity-ref-delimiter
]))
1024 '([nil
4 nxml-comment-delimiter
]
1025 [4 -
3 nxml-comment-content
]
1026 [-
3 nil nxml-comment-delimiter
]))
1028 (put 'processing-instruction
1030 '([nil
2 nxml-processing-instruction-delimiter
]
1031 [-
2 nil nxml-processing-instruction-delimiter
]
1032 processing-instruction-content
))
1036 '([nil
3 nxml-cdata-section-delimiter
] ; <![
1037 [3 8 nxml-cdata-section-CDATA
] ; CDATA
1038 [8 9 nxml-cdata-section-delimiter
] ; [
1039 [9 -
3 nxml-cdata-section-content
] ; ]]>
1040 [-
3 nil nxml-cdata-section-delimiter
]))
1044 '([nil nil nxml-text
]))
1046 ;; Prolog region types in list returned by xmltok-forward-prolog.
1048 (put 'xml-declaration
1050 '([nil
2 nxml-processing-instruction-delimiter
]
1051 [2 5 nxml-processing-instruction-target
]
1052 [-
2 nil nxml-processing-instruction-delimiter
]))
1054 (put 'xml-declaration-attribute-name
1056 '([nil nil nxml-attribute-local-name
]))
1058 (put 'xml-declaration-attribute-value
1060 '([nil
1 nxml-attribute-value-delimiter
]
1061 [1 -
1 nxml-attribute-value
]
1062 [-
1 nil nxml-attribute-value-delimiter
]))
1064 (put 'processing-instruction-left
1066 '([nil
2 nxml-processing-instruction-delimiter
]
1067 [2 nil nxml-processing-instruction-target
]))
1069 (put 'processing-instruction-right
1071 '([nil -
2 nxml-processing-instruction-content
]
1072 [-
2 nil nxml-processing-instruction-delimiter
]))
1076 '([nil
1 nxml-prolog-literal-delimiter
]
1077 [1 -
1 nxml-prolog-literal-content
]
1078 [-
1 nil nxml-prolog-literal-delimiter
]))
1082 '([nil nil nxml-prolog-keyword
]))
1084 (put 'markup-declaration-open
1086 '([0 2 nxml-markup-declaration-delimiter
]
1087 [2 nil nxml-prolog-keyword
]))
1089 (put 'markup-declaration-close
1091 '([nil nil nxml-markup-declaration-delimiter
]))
1093 (put 'internal-subset-open
1095 '([nil nil nxml-markup-declaration-delimiter
]))
1097 (put 'internal-subset-close
1099 '([nil
1 nxml-markup-declaration-delimiter
]
1100 [-
1 nil nxml-markup-declaration-delimiter
]))
1105 [1 nil nxml-prolog-keyword
]))
1107 (defun nxml-apply-fontify-rule (&optional type start end
)
1108 (let ((rule (get (or type xmltok-type
) 'nxml-fontify-rule
)))
1109 (unless start
(setq start xmltok-start
))
1110 (unless end
(setq end
(point)))
1112 (let* ((action (car rule
)))
1113 (setq rule
(cdr rule
))
1114 (cond ((vectorp action
)
1115 (nxml-set-face (let ((offset (aref action
0)))
1116 (cond ((not offset
) start
)
1117 ((< offset
0) (+ end offset
))
1118 (t (+ start offset
))))
1119 (let ((offset (aref action
1)))
1120 (cond ((not offset
) end
)
1121 ((< offset
0) (+ end offset
))
1122 (t (+ start offset
))))
1124 ((and (consp action
)
1125 (eq (car action
) 'element-qname
))
1126 (when xmltok-name-end
; maybe nil in partial-end-tag case
1127 (nxml-fontify-qname (+ start
(cdr action
))
1130 'nxml-element-prefix
1132 'nxml-element-local-name
)))
1133 ((eq action
'attributes
)
1134 (nxml-fontify-attributes))
1135 ((eq action
'processing-instruction-content
)
1136 (nxml-set-face (+ start
2)
1138 'nxml-processing-instruction-target
)
1139 (nxml-set-face (save-excursion
1140 (goto-char xmltok-name-end
)
1141 (skip-chars-forward " \t\r\n")
1144 'nxml-processing-instruction-content
))
1145 ((eq action
'char-ref
)
1146 (nxml-char-ref-display-extra start
1148 (xmltok-char-number start end
)))
1149 (t (error "Invalid nxml-fontify-rule action %s" action
)))))))
1151 (defun nxml-fontify-attributes ()
1152 (while xmltok-namespace-attributes
1153 (nxml-fontify-attribute (car xmltok-namespace-attributes
)
1155 (setq xmltok-namespace-attributes
1156 (cdr xmltok-namespace-attributes
)))
1157 (while xmltok-attributes
1158 (nxml-fontify-attribute (car xmltok-attributes
))
1159 (setq xmltok-attributes
1160 (cdr xmltok-attributes
))))
1162 (defun nxml-fontify-attribute (att &optional namespace-declaration
)
1163 (if namespace-declaration
1164 (nxml-fontify-qname (xmltok-attribute-name-start att
)
1165 (xmltok-attribute-name-colon att
)
1166 (xmltok-attribute-name-end att
)
1167 'nxml-namespace-attribute-xmlns
1168 'nxml-namespace-attribute-colon
1169 'nxml-namespace-attribute-prefix
1170 'nxml-namespace-attribute-xmlns
)
1171 (nxml-fontify-qname (xmltok-attribute-name-start att
)
1172 (xmltok-attribute-name-colon att
)
1173 (xmltok-attribute-name-end att
)
1174 'nxml-attribute-prefix
1175 'nxml-attribute-colon
1176 'nxml-attribute-local-name
))
1177 (let ((start (xmltok-attribute-value-start att
))
1178 (end (xmltok-attribute-value-end att
))
1179 (refs (xmltok-attribute-refs att
))
1180 (delimiter-face (if namespace-declaration
1181 'nxml-namespace-attribute-value-delimiter
1182 'nxml-attribute-value-delimiter
))
1183 (value-face (if namespace-declaration
1184 'nxml-namespace-attribute-value
1185 'nxml-attribute-value
)))
1187 (nxml-set-face (1- start
) start delimiter-face
)
1188 (nxml-set-face end
(1+ end
) delimiter-face
)
1190 (let* ((ref (car refs
))
1191 (ref-type (aref ref
0))
1192 (ref-start (aref ref
1))
1193 (ref-end (aref ref
2)))
1194 (nxml-set-face start ref-start value-face
)
1195 (nxml-apply-fontify-rule ref-type ref-start ref-end
)
1196 (setq start ref-end
))
1197 (setq refs
(cdr refs
)))
1198 (nxml-set-face start end value-face
))))
1200 (defun nxml-fontify-qname (start
1208 (cond (colon (nxml-set-face start colon prefix-face
)
1209 (nxml-set-face colon
(1+ colon
) colon-face
)
1210 (nxml-set-face (1+ colon
) end local-name-face
))
1211 (t (nxml-set-face start end
(or unprefixed-face
1212 local-name-face
)))))
1216 (defun nxml-electric-slash (arg)
1219 With a prefix ARG, do nothing other than insert the slash.
1221 Otherwise, if `nxml-slash-auto-complete-flag' is non-nil, insert the
1222 rest of the end-tag or empty-element if the slash is potentially part
1223 of an end-tag or the close of an empty-element.
1225 If the slash is part of an end-tag that is the first non-whitespace
1226 on the line, reindent the line."
1228 (nxml-ensure-scan-up-to-date)
1229 (let* ((slash-pos (point))
1230 (end-tag-p (and (eq (char-before slash-pos
) ?
<)
1231 (not (nxml-get-inside slash-pos
))))
1232 (at-indentation (save-excursion
1233 (back-to-indentation)
1234 (eq (point) (1- slash-pos
)))))
1235 (self-insert-command (prefix-numeric-value arg
))
1237 (if nxml-slash-auto-complete-flag
1240 (let ((start-tag-end
1241 (nxml-scan-element-backward (1- slash-pos
) t
)))
1243 (insert (xmltok-start-tag-qname) ">")
1244 ;; copy the indentation of the start-tag
1245 (when (and at-indentation
1247 (goto-char xmltok-start
)
1248 (back-to-indentation)
1249 (eq (point) xmltok-start
)))
1251 (indent-line-to (save-excursion
1252 (goto-char xmltok-start
)
1253 (current-column)))))))
1254 (nxml-scan-error nil
))
1255 (when (and (eq (nxml-token-before) (point))
1256 (eq xmltok-type
'partial-empty-element
))
1258 (when (and end-tag-p at-indentation
)
1259 (nxml-indent-line)))))
1261 (defun nxml-balanced-close-start-tag-block ()
1262 "Close the start-tag before point with `>' and insert a balancing end-tag.
1263 Point is left between the start-tag and the end-tag.
1264 If there is nothing but whitespace before the `<' that opens the
1265 start-tag, then put point on a blank line, and put the end-tag on
1266 another line aligned with the start-tag."
1268 (nxml-balanced-close-start-tag 'block
))
1270 (defun nxml-balanced-close-start-tag-inline ()
1271 "Close the start-tag before point with `>' and insert a balancing end-tag.
1272 Point is left between the start-tag and the end-tag.
1273 No extra whitespace is inserted."
1275 (nxml-balanced-close-start-tag 'inline
))
1277 (defun nxml-balanced-close-start-tag (block-or-inline)
1278 (let ((token-end (nxml-token-before))
1280 (token-start xmltok-start
))
1281 (unless (or (eq xmltok-type
'partial-start-tag
)
1282 (and (memq xmltok-type
'(start-tag
1284 partial-empty-element
))
1285 (>= token-end pos
)))
1286 (error "Not in a start-tag"))
1287 ;; Note that this insertion changes xmltok-start.
1289 (buffer-substring-no-properties (+ xmltok-start
1)
1290 (min xmltok-name-end
(point)))
1292 (if (eq block-or-inline
'inline
)
1294 (goto-char token-start
)
1295 (back-to-indentation)
1296 (if (= (point) token-start
)
1297 (let ((indent (current-column)))
1300 (indent-line-to indent
)
1303 (indent-line-to (+ nxml-child-indent indent
)))
1306 (defun nxml-finish-element ()
1307 "Finish the current element by inserting an end-tag."
1309 (nxml-finish-element-1 nil
))
1311 (defvar nxml-last-split-position nil
1312 "Position where `nxml-split-element' split the current element.")
1314 (defun nxml-split-element ()
1315 "Split the current element by inserting an end-tag and a start-tag.
1316 Point is left after the newly inserted start-tag. When repeated,
1317 split immediately before the previously inserted start-tag and leave
1320 (setq nxml-last-split-position
1321 (if (and (eq last-command this-command
)
1322 nxml-last-split-position
)
1324 (goto-char nxml-last-split-position
)
1325 (nxml-finish-element-1 t
))
1326 (nxml-finish-element-1 t
))))
1328 (defun nxml-finish-element-1 (startp)
1329 "Insert an end-tag for the current element and optionally a start-tag.
1330 The start-tag is inserted if STARTP is non-nil. Return the position
1331 of the inserted start-tag or nil if none was inserted."
1333 (let* ((token-end (nxml-token-before))
1336 (when (and (< (point) token-end
)
1339 processing-instruction
1344 (error "Point is inside a %s"
1345 (nxml-token-type-friendly-name xmltok-type
)))
1346 (nxml-scan-element-backward token-end t
)))
1349 (unless (eq xmltok-type
'start-tag
)
1350 (error "No matching start-tag"))
1351 (goto-char xmltok-start
)
1352 (back-to-indentation)
1353 (eq (point) xmltok-start
)))
1356 (goto-char start-tag-end
)
1357 (looking-at "[ \t\r\n]*$")))
1358 (start-tag-indent (save-excursion
1359 (goto-char xmltok-start
)
1361 (qname (xmltok-start-tag-qname))
1362 inserted-start-tag-pos
)
1363 (when (and starts-line ends-line
)
1364 ;; start-tag is on a line by itself
1365 ;; => put the end-tag on a line by itself
1368 (back-to-indentation)
1371 (indent-line-to start-tag-indent
))
1372 (insert "</" qname
">")
1376 (indent-line-to start-tag-indent
))
1377 (setq inserted-start-tag-pos
(point))
1378 (insert "<" qname
">")
1379 (when (and starts-line ends-line
)
1381 (indent-line-to (save-excursion
1382 (goto-char xmltok-start
)
1384 (back-to-indentation)
1385 (if (= (current-column)
1386 (+ start-tag-indent nxml-child-indent
))
1387 (+ start-tag-indent nxml-child-indent
)
1388 start-tag-indent
)))))
1389 inserted-start-tag-pos
))
1393 (defun nxml-indent-line ()
1394 "Indent current line as XML."
1395 (let* ((savep (point))
1396 (indent (condition-case nil
1399 (skip-chars-forward " \t")
1400 (if (>= (point) savep
) (setq savep nil
))
1401 (or (nxml-compute-indent) 0))
1403 (if (not (numberp indent
))
1404 ;; If something funny is used (e.g. `noindent'), return it.
1406 (if (< indent
0) (setq indent
0)) ;Just in case.
1408 (save-excursion (indent-line-to indent
))
1409 (indent-line-to indent
)))))
1411 (defun nxml-compute-indent ()
1412 "Return the indent for the line containing point."
1413 (or (nxml-compute-indent-from-matching-start-tag)
1414 (nxml-compute-indent-from-previous-line)))
1416 (defun nxml-compute-indent-from-matching-start-tag ()
1417 "Compute the indent for a line with an end-tag using the matching start-tag.
1418 When the line containing point ends with an end-tag and does not start
1419 in the middle of a token, return the indent of the line containing the
1420 matching start-tag, if there is one and it occurs at the beginning of
1421 its line. Otherwise return nil."
1423 (back-to-indentation)
1424 (let ((bol (point)))
1425 (let ((inhibit-field-text-motion t
))
1427 (skip-chars-backward " \t")
1428 (and (= (nxml-token-before) (point))
1429 (memq xmltok-type
'(end-tag partial-end-tag
))
1430 ;; start of line must not be inside a token
1431 (or (= xmltok-start bol
)
1435 (= xmltok-start bol
))
1436 (eq xmltok-type
'data
))
1438 (nxml-scan-element-backward
1442 nxml-end-tag-indent-scan-distance
))
1443 (nxml-scan-error nil
))
1444 (< xmltok-start bol
)
1446 (goto-char xmltok-start
)
1447 (skip-chars-backward " \t")
1449 (current-indentation)))))
1451 (defun nxml-compute-indent-from-previous-line ()
1452 "Compute the indent for a line using the indentation of a previous line."
1457 before-context after-context
)
1458 (back-to-indentation)
1461 ;; Move backwards until the start of a non-blank line that is
1462 ;; not inside a token.
1464 (when (= (forward-line -
1) -
1)
1466 (back-to-indentation)
1467 (if (looking-at "[ \t]*$")
1470 (setq prev-bol
(point)))
1472 (not (or (= xmltok-start
(point))
1473 (eq xmltok-type
'data
))))))
1475 ;; Now scan over tokens until the end of the line to be indented.
1476 ;; Determine the context before and after the beginning of the
1478 (while (< (point) eol
)
1479 (nxml-tokenize-forward)
1480 (cond ((<= bol xmltok-start
)
1482 (nxml-merge-indent-context-type after-context
)))
1483 ((and (<= (point) bol
)
1484 (not (and (eq xmltok-type
'partial-start-tag
)
1486 (setq before-context
1487 (nxml-merge-indent-context-type before-context
)))
1488 ((eq xmltok-type
'data
)
1489 (setq before-context
1490 (nxml-merge-indent-context-type before-context
))
1492 (nxml-merge-indent-context-type after-context
)))
1493 ;; If in the middle of a token that looks inline,
1494 ;; then indent relative to the previous non-blank line
1495 ((eq (nxml-merge-indent-context-type before-context
)
1497 (goto-char prev-bol
)
1498 (throw 'indent
(current-column)))
1501 (nxml-compute-indent-in-token bol
))))
1502 (skip-chars-forward " \t\r\n"))
1505 (* nxml-child-indent
1506 (+ (if (eq before-context
'start-tag
) 1 0)
1507 (if (eq after-context
'end-tag
) -
1 0))))))))
1509 (defun nxml-merge-indent-context-type (context)
1510 "Merge the indent context type CONTEXT with the token in `xmltok-type'.
1511 Return the merged indent context type. An indent context type is
1512 either nil or one of the symbols `start-tag', `end-tag', `markup',
1513 `comment', `mixed'."
1514 (cond ((memq xmltok-type
'(start-tag partial-start-tag
))
1515 (if (memq context
'(nil start-tag comment
))
1518 ((memq xmltok-type
'(end-tag partial-end-tag
))
1519 (if (memq context
'(nil end-tag comment
))
1522 ((eq xmltok-type
'comment
)
1523 (cond ((memq context
'(start-tag end-tag comment
))
1530 (defun nxml-compute-indent-in-token (pos)
1531 "Return the indent for a line that starts inside a token.
1532 POS is the position of the first non-whitespace character of the line.
1533 This expects the xmltok-* variables to be set up as by `xmltok-forward'."
1534 (cond ((memq xmltok-type
'(start-tag
1537 partial-empty-element
))
1538 (nxml-compute-indent-in-start-tag pos
))
1539 ((eq xmltok-type
'comment
)
1540 (nxml-compute-indent-in-delimited-token pos
"<!--" "-->"))
1541 ((eq xmltok-type
'cdata-section
)
1542 (nxml-compute-indent-in-delimited-token pos
"<![CDATA[" "]]>"))
1543 ((eq xmltok-type
'processing-instruction
)
1544 (nxml-compute-indent-in-delimited-token pos
"<?" "?>"))
1547 (if (and (= (forward-line -
1) 0)
1548 (< xmltok-start
(point)))
1549 (back-to-indentation)
1550 (goto-char xmltok-start
))
1553 (defun nxml-compute-indent-in-start-tag (pos)
1554 "Return the indent for a line that starts inside a start-tag.
1555 Also for a line that starts inside an empty element.
1556 POS is the position of the first non-whitespace character of the line.
1557 This expects the xmltok-* variables to be set up as by `xmltok-forward'."
1558 (let ((value-boundary (nxml-attribute-value-boundary pos
))
1561 ;; inside an attribute value
1562 (let ((value-start (car value-boundary
)))
1565 (if (< (point) value-start
)
1566 (goto-char value-start
)
1567 (back-to-indentation)))
1568 ;; outside an attribute value
1570 (while (and (= (forward-line -
1) 0)
1571 (nxml-attribute-value-boundary (point))))
1572 (cond ((<= (point) xmltok-start
)
1573 (goto-char xmltok-start
)
1574 (setq off nxml-attribute-indent
)
1575 (let ((atts (xmltok-merge-attributes)))
1577 (let* ((att (car atts
))
1578 (start (xmltok-attribute-name-start att
)))
1583 (back-to-indentation))))
1584 (+ (current-column) off
)))
1586 (defun nxml-attribute-value-boundary (pos)
1587 "Return a pair (START . END) if POS is inside an attribute value.
1588 Otherwise return nil. START and END are the positions of the start
1589 and end of the attribute value containing POS. This expects the
1590 xmltok-* variables to be set up as by `xmltok-forward'."
1591 (let ((atts (xmltok-merge-attributes))
1592 att value-start value-end value-boundary
)
1594 (setq att
(car atts
))
1595 (setq value-start
(xmltok-attribute-value-start att
))
1596 (setq value-end
(xmltok-attribute-value-end att
))
1597 (cond ((and value-start
(< pos value-start
))
1599 ((and value-start value-end
(<= pos value-end
))
1600 (setq value-boundary
(cons value-start value-end
))
1602 (t (setq atts
(cdr atts
)))))
1605 (defun nxml-compute-indent-in-delimited-token (pos open-delim close-delim
)
1606 "Return the indent for a line that starts inside a token with delimiters.
1607 OPEN-DELIM and CLOSE-DELIM are strings giving the opening and closing
1608 delimiters. POS is the position of the first non-whitespace character
1609 of the line. This expects the xmltok-* variables to be set up as by
1611 (cond ((let ((end (+ pos
(length close-delim
))))
1612 (and (<= end
(point-max))
1613 (string= (buffer-substring-no-properties pos end
)
1615 (goto-char xmltok-start
))
1619 (<= (point) xmltok-start
))
1620 (goto-char (+ xmltok-start
(length open-delim
)))
1621 (when (and (string= open-delim
"<!--")
1623 (goto-char (1+ (point)))))
1624 (t (back-to-indentation)))
1629 (defun nxml-complete ()
1630 "Perform completion on the symbol preceding point.
1632 Inserts as many characters as can be completed. However, if not even
1633 one character can be completed, then a buffer with the possibilities
1634 is popped up and the symbol is read from the minibuffer with
1635 completion. If the symbol is complete, then any characters that must
1636 follow the symbol are also inserted.
1638 The name space used for completion and what is treated as a symbol
1639 depends on the context. The contexts in which completion is performed
1640 depend on `nxml-completion-hook'."
1642 (unless (run-hook-with-args-until-success 'nxml-completion-hook
)
1643 ;; Eventually we will complete on entity names here.
1645 (message "Cannot complete in this context")))
1647 (defun nxml-completion-at-point-function ()
1648 "Call `nxml-complete' to perform completion at point."
1649 (when nxml-bind-meta-tab-to-complete-flag
1654 (defun nxml-forward-balanced-item (&optional arg
)
1655 "Move forward across one balanced item.
1656 With ARG, do it that many times. Negative arg -N means
1657 move backward across N balanced expressions.
1658 This is the equivalent of `forward-sexp' for XML.
1660 An element contains as items strings with no markup, tags, processing
1661 instructions, comments, CDATA sections, entity references and
1662 characters references. However, if the variable
1663 `nxml-sexp-element-flag' is non-nil, then an element is treated as a
1664 single markup item. A start-tag contains an element name followed by
1665 one or more attributes. An end-tag contains just an element name.
1666 An attribute value literals contains strings with no markup, entity
1667 references and character references. A processing instruction
1668 consists of a target and a content string. A comment or a CDATA
1669 section contains a single string. An entity reference contains a
1670 single name. A character reference contains a character number."
1672 (or arg
(setq arg
1))
1675 (nxml-forward-single-balanced-item)
1676 (> (setq arg
(1- arg
)) 0))))
1679 (nxml-backward-single-balanced-item)
1680 (< (setq arg
(1+ arg
)) 0))))))
1682 (defun nxml-forward-single-balanced-item ()
1684 (goto-char (let ((end (nxml-token-after)))
1686 (while (eq xmltok-type
'space
)
1688 (setq end
(nxml-token-after)))
1689 (cond ((/= (point) xmltok-start
)
1690 (nxml-scan-forward-within end
))
1691 ((and nxml-sexp-element-flag
1692 (eq xmltok-type
'start-tag
))
1693 ;; can't ever return nil here
1694 (nxml-scan-element-forward xmltok-start
))
1695 ((and nxml-sexp-element-flag
1697 '(end-tag partial-end-tag
)))
1698 (error "Already at end of element"))
1701 (goto-char (cadr err
))
1702 (apply 'error
(cddr err
)))))
1704 (defun nxml-backward-single-balanced-item ()
1706 (goto-char (let ((end (nxml-token-before)))
1708 (while (eq xmltok-type
'space
)
1709 (goto-char xmltok-start
)
1710 (setq end
(nxml-token-before)))
1711 (cond ((/= (point) end
)
1712 (nxml-scan-backward-within end
))
1713 ((and nxml-sexp-element-flag
1714 (eq xmltok-type
'end-tag
))
1715 ;; can't ever return nil here
1716 (nxml-scan-element-backward end
)
1718 ((and nxml-sexp-element-flag
1719 (eq xmltok-type
'start-tag
))
1720 (error "Already at start of element"))
1721 (t xmltok-start
)))))
1723 (goto-char (cadr err
))
1724 (apply 'error
(cddr err
)))))
1726 (defun nxml-scan-forward-within (end)
1727 (setq end
(- end
(nxml-end-delimiter-length xmltok-type
)))
1728 (when (<= end
(point))
1729 (error "Already at end of %s"
1730 (nxml-token-type-friendly-name xmltok-type
)))
1731 (cond ((memq xmltok-type
'(start-tag
1734 partial-empty-element
))
1735 (if (< (point) xmltok-name-end
)
1737 (let ((att (nxml-find-following-attribute)))
1738 (cond ((not att
) end
)
1739 ((and (xmltok-attribute-value-start att
)
1740 (<= (xmltok-attribute-value-start att
)
1742 (nxml-scan-forward-in-attribute-value att
))
1743 ((xmltok-attribute-value-end att
)
1744 (1+ (xmltok-attribute-value-end att
)))
1746 (goto-char (xmltok-attribute-name-end att
))
1747 (looking-at "[ \t\r\n]*="))
1749 (t (xmltok-attribute-name-end att
))))))
1750 ((and (eq xmltok-type
'processing-instruction
)
1751 (< (point) xmltok-name-end
))
1755 (defun nxml-scan-backward-within (_end)
1758 (nxml-start-delimiter-length xmltok-type
)))
1759 (when (<= (point) xmltok-start
)
1760 (error "Already at start of %s"
1761 (nxml-token-type-friendly-name xmltok-type
)))
1762 (cond ((memq xmltok-type
'(start-tag
1765 partial-empty-element
))
1766 (let ((att (nxml-find-preceding-attribute)))
1767 (cond ((not att
) xmltok-start
)
1768 ((and (xmltok-attribute-value-start att
)
1769 (<= (xmltok-attribute-value-start att
)
1772 (xmltok-attribute-value-end att
)))
1773 (nxml-scan-backward-in-attribute-value att
))
1774 (t (xmltok-attribute-name-start att
)))))
1775 ((and (eq xmltok-type
'processing-instruction
)
1776 (let ((content-start (save-excursion
1777 (goto-char xmltok-name-end
)
1778 (skip-chars-forward " \r\t\n")
1780 (and (< content-start
(point))
1784 (defun nxml-scan-forward-in-attribute-value (att)
1785 (when (= (point) (xmltok-attribute-value-end att
))
1786 (error "Already at end of attribute value"))
1787 (let ((refs (xmltok-attribute-refs att
))
1790 (setq ref
(car refs
))
1791 (if (< (point) (aref ref
2))
1794 (setq refs
(cdr refs
))))
1796 (xmltok-attribute-value-end att
))
1797 ((< (point) (aref ref
1))
1799 ((= (point) (aref ref
1))
1802 (let ((end (- (aref ref
2)
1803 (nxml-end-delimiter-length (aref ref
0)))))
1806 (error "Already at end of %s"
1807 (nxml-token-type-friendly-name (aref ref
0)))))))))
1809 (defun nxml-scan-backward-in-attribute-value (att)
1810 (when (= (point) (xmltok-attribute-value-start att
))
1811 (error "Already at start of attribute value"))
1812 (let ((refs (reverse (xmltok-attribute-refs att
)))
1815 (setq ref
(car refs
))
1816 (if (< (aref ref
1) (point))
1819 (setq refs
(cdr refs
))))
1821 (xmltok-attribute-value-start att
))
1822 ((< (aref ref
2) (point))
1824 ((= (point) (aref ref
2))
1827 (let ((start (+ (aref ref
1)
1828 (nxml-start-delimiter-length (aref ref
0)))))
1829 (if (< start
(point))
1831 (error "Already at start of %s"
1832 (nxml-token-type-friendly-name (aref ref
0)))))))))
1834 (defun nxml-find-following-attribute ()
1836 (atts (or xmltok-attributes xmltok-namespace-attributes
))
1837 (more-atts (and xmltok-attributes xmltok-namespace-attributes
)))
1839 (let* ((att (car atts
))
1840 (name-start (xmltok-attribute-name-start att
)))
1841 (cond ((and (<= name-start
(point))
1842 (xmltok-attribute-value-end att
)
1843 ;; <= because end is before quote
1844 (<= (point) (xmltok-attribute-value-end att
)))
1847 ((and (< (point) name-start
)
1850 (xmltok-attribute-name-start ret
))))
1852 (setq atts
(cdr atts
))
1854 (setq atts more-atts
)
1855 (setq more-atts nil
)))
1858 (defun nxml-find-preceding-attribute ()
1860 (atts (or xmltok-attributes xmltok-namespace-attributes
))
1861 (more-atts (and xmltok-attributes xmltok-namespace-attributes
)))
1863 (let* ((att (car atts
))
1864 (name-start (xmltok-attribute-name-start att
)))
1865 (cond ((and (< name-start
(point))
1866 (xmltok-attribute-value-end att
)
1867 ;; <= because end is before quote
1868 (<= (point) (xmltok-attribute-value-end att
)))
1871 ((and (< name-start
(point))
1873 (< (xmltok-attribute-name-start ret
)
1876 (setq atts
(cdr atts
))
1878 (setq atts more-atts
)
1879 (setq more-atts nil
)))
1882 (defun nxml-up-element (&optional arg
)
1884 (or arg
(setq arg
1))
1886 (nxml-backward-up-element (- arg
))
1888 (while (and (> arg
0)
1889 (< (point) (point-max)))
1890 (let ((token-end (nxml-token-after)))
1891 (goto-char (cond ((or (memq xmltok-type
'(end-tag
1893 (and (memq xmltok-type
1895 partial-empty-element
))
1896 (< xmltok-start
(point))))
1898 ((nxml-scan-element-forward
1899 (if (and (eq xmltok-type
'start-tag
)
1900 (= (point) xmltok-start
))
1904 (t (error "No parent element")))))
1905 (setq arg
(1- arg
)))
1907 (goto-char (cadr err
))
1908 (apply 'error
(cddr err
))))))
1910 (defun nxml-backward-up-element (&optional arg
)
1912 (or arg
(setq arg
1))
1914 (nxml-up-element (- arg
))
1916 (while (and (> arg
0)
1917 (< (point-min) (point)))
1918 (let ((token-end (nxml-token-before)))
1919 (goto-char (cond ((or (memq xmltok-type
'(start-tag
1921 (and (memq xmltok-type
1923 partial-empty-element
))
1924 (< (point) token-end
)))
1926 ((nxml-scan-element-backward
1927 (if (and (eq xmltok-type
'end-tag
)
1928 (= (point) token-end
))
1933 (t (error "No parent element")))))
1934 (setq arg
(1- arg
)))
1936 (goto-char (cadr err
))
1937 (apply 'error
(cddr err
))))))
1939 (defun nxml-down-element (&optional arg
)
1940 "Move forward down into the content of an element.
1941 With ARG, do this that many times.
1942 Negative ARG means move backward but still down."
1944 (or arg
(setq arg
1))
1946 (nxml-backward-down-element (- arg
))
1949 (let ((token-end (nxml-token-after)))
1951 (goto-char token-end
)
1953 (when (memq xmltok-type
'(nil end-tag partial-end-tag
))
1954 (error "No following start-tags in this element"))
1955 (not (memq xmltok-type
'(start-tag partial-start-tag
))))
1956 (nxml-tokenize-forward))
1958 (setq arg
(1- arg
)))))
1960 (defun nxml-backward-down-element (&optional arg
)
1962 (or arg
(setq arg
1))
1964 (nxml-down-element (- arg
))
1969 (goto-char xmltok-start
)
1971 (when (memq xmltok-type
'(start-tag
1975 (error "No preceding end-tags in this element"))
1976 (not (memq xmltok-type
'(end-tag partial-end-tag
))))
1977 (if (or (<= (point) nxml-prolog-end
)
1978 (not (search-backward "<" nxml-prolog-end t
)))
1979 (setq xmltok-type nil
)
1980 (nxml-move-outside-backwards)
1983 (setq arg
(1- arg
)))))
1985 (defun nxml-forward-element (&optional arg
)
1986 "Move forward over one element.
1987 With ARG, do it that many times.
1988 Negative ARG means move backward."
1990 (or arg
(setq arg
1))
1992 (nxml-backward-element (- arg
))
1994 (while (and (> arg
0)
1995 (< (point) (point-max)))
1997 (or (nxml-scan-element-forward (nxml-token-before))
1998 (error "No more elements")))
1999 (setq arg
(1- arg
)))
2001 (goto-char (cadr err
))
2002 (apply 'error
(cddr err
))))))
2004 (defun nxml-backward-element (&optional arg
)
2005 "Move backward over one element.
2006 With ARG, do it that many times.
2007 Negative ARG means move forward."
2009 (or arg
(setq arg
1))
2011 (nxml-forward-element (- arg
))
2013 (while (and (> arg
0)
2014 (< (point-min) (point)))
2016 (or (and (nxml-scan-element-backward (progn
2020 (error "No preceding elements")))
2021 (setq arg
(1- arg
)))
2023 (goto-char (cadr err
))
2024 (apply 'error
(cddr err
))))))
2026 (defun nxml-mark-token-after ()
2028 (push-mark (nxml-token-after) nil t
)
2029 (goto-char xmltok-start
)
2030 (message "Marked %s" xmltok-type
))
2034 (defun nxml-mark-paragraph ()
2035 "Put point at beginning of this paragraph, mark at end.
2036 The paragraph marked is the one that contains point or follows point."
2038 (nxml-forward-paragraph)
2040 (nxml-backward-paragraph))
2042 (defun nxml-forward-paragraph (&optional arg
)
2044 (or arg
(setq arg
1))
2046 (nxml-backward-paragraph (- arg
)))
2049 (while (and (nxml-forward-single-paragraph)
2050 (> (setq arg
(1- arg
)) 0))))))
2052 (defun nxml-backward-paragraph (&optional arg
)
2054 (or arg
(setq arg
1))
2056 (nxml-forward-paragraph (- arg
)))
2059 (let ((inhibit-field-text-motion t
))
2061 (while (and (nxml-backward-single-paragraph)
2062 (> (setq arg
(1- arg
)) 0))))))
2064 (defun nxml-forward-single-paragraph ()
2065 "Move forward over a single paragraph.
2066 Return nil at end of buffer, t otherwise."
2067 (let* ((token-end (nxml-token-after))
2068 (offset (- (point) xmltok-start
))
2070 (goto-char token-end
)
2071 (while (and (< (point) (point-max))
2073 (nxml-paragraph-end-pos had-data offset
))))
2074 (when (nxml-token-contains-data-p offset
)
2076 (nxml-tokenize-forward)
2078 (when pos
(goto-char pos
))))
2080 (defun nxml-backward-single-paragraph ()
2081 "Move backward over a single paragraph.
2082 Return nil at start of buffer, t otherwise."
2083 (let* ((token-end (nxml-token-before))
2084 (offset (- token-end
(point)))
2085 (last-tag-pos xmltok-start
)
2086 pos had-data last-data-pos
)
2087 (goto-char token-end
)
2088 (unless (setq pos
(nxml-paragraph-start-pos nil offset
))
2089 (setq had-data
(nxml-token-contains-data-p nil offset
))
2090 (goto-char xmltok-start
)
2091 (while (and (not pos
) (< (point-min) (point)))
2092 (cond ((search-backward "<" nxml-prolog-end t
)
2093 (nxml-move-outside-backwards)
2095 (while (< (point) last-tag-pos
)
2097 (when (and (not had-data
) (nxml-token-contains-data-p))
2099 (setq last-data-pos xmltok-start
))
2100 (let ((tem (nxml-paragraph-start-pos had-data
0)))
2101 (when tem
(setq pos tem
)))))
2102 (when (and (not had-data
) last-data-pos
(not pos
))
2105 (while (< (point) last-data-pos
)
2107 (let ((tem (nxml-paragraph-start-pos had-data
0)))
2108 (when tem
(setq pos tem
)))))
2109 (setq last-tag-pos
(point)))
2110 (t (goto-char (point-min))))))
2111 (when pos
(goto-char pos
))))
2113 (defun nxml-token-contains-data-p (&optional start end
)
2114 (setq start
(+ xmltok-start
(or start
0)))
2115 (setq end
(- (point) (or end
0)))
2116 (when (eq xmltok-type
'cdata-section
)
2117 (setq start
(max start
(+ xmltok-start
9)))
2118 (setq end
(min end
(- (point) 3))))
2119 (or (and (eq xmltok-type
'data
)
2120 (eq start xmltok-start
)
2122 (eq xmltok-type
'char-ref
)
2123 (and (memq xmltok-type
'(data cdata-section
))
2127 (re-search-forward "[^ \t\r\n]" end t
)))))
2129 (defun nxml-paragraph-end-pos (had-data offset
)
2130 "Return the position of the paragraph end if contained in the current token.
2131 Return nil if the current token does not contain the paragraph end.
2132 Only characters after OFFSET from the start of the token are eligible.
2133 HAD-DATA says whether there have been non-whitespace data characters yet."
2134 (cond ((not had-data
)
2135 (cond ((memq xmltok-type
'(data cdata-section
))
2137 (let ((end (point)))
2138 (goto-char (+ xmltok-start
2139 (max (if (eq xmltok-type
'cdata-section
)
2143 (and (re-search-forward "[^ \t\r\n]" end t
)
2144 (re-search-forward "^[ \t]*$" end t
)
2145 (match-beginning 0)))))
2146 ((and (eq xmltok-type
'comment
)
2147 (nxml-token-begins-line-p)
2148 (nxml-token-ends-line-p))
2150 (let ((end (point)))
2151 (goto-char (+ xmltok-start
(max 4 offset
)))
2152 (when (re-search-forward "[^ \t\r\n]" (- end
3) t
)
2153 (if (re-search-forward "^[ \t]*$" end t
)
2155 (goto-char (- end
3))
2156 (skip-chars-backward " \t")
2158 (beginning-of-line 2))
2160 ((memq xmltok-type
'(data space cdata-section
))
2162 (let ((end (point)))
2163 (goto-char (+ xmltok-start offset
))
2164 (and (re-search-forward "^[ \t]*$" end t
)
2165 (match-beginning 0)))))
2166 ((and (memq xmltok-type
'(start-tag
2170 processing-instruction
2172 (nxml-token-begins-line-p)
2173 (nxml-token-ends-line-p))
2175 (goto-char xmltok-start
)
2176 (skip-chars-backward " \t")
2178 ((and (eq xmltok-type
'end-tag
)
2179 (looking-at "[ \t]*$")
2180 (not (nxml-in-mixed-content-p t
)))
2182 (or (search-forward "\n" nil t
)
2185 (defun nxml-paragraph-start-pos (had-data offset
)
2186 "Return the position of the paragraph start if contained in the current token.
2187 Return nil if the current token does not contain the paragraph start.
2188 Only characters before OFFSET from the end of the token are eligible.
2189 HAD-DATA says whether there have been non-whitespace data characters yet."
2190 (cond ((not had-data
)
2191 (cond ((memq xmltok-type
'(data cdata-section
))
2193 (goto-char (- (point)
2194 (max (if (eq xmltok-type
'cdata-section
)
2198 (and (re-search-backward "[^ \t\r\n]" xmltok-start t
)
2199 (re-search-backward "^[ \t]*$" xmltok-start t
)
2200 (match-beginning 0))))
2201 ((and (eq xmltok-type
'comment
)
2202 (nxml-token-ends-line-p)
2203 (nxml-token-begins-line-p))
2205 (goto-char (- (point) (max 3 offset
)))
2206 (when (and (< (+ xmltok-start
4) (point))
2207 (re-search-backward "[^ \t\r\n]"
2210 (if (re-search-backward "^[ \t]*$" xmltok-start t
)
2212 (goto-char xmltok-start
)
2213 (if (looking-at "<!--[ \t]*\n")
2215 (skip-chars-backward " \t")
2217 ((memq xmltok-type
'(data space cdata-section
))
2219 (goto-char (- (point) offset
))
2220 (and (re-search-backward "^[ \t]*$" xmltok-start t
)
2221 (match-beginning 0))))
2222 ((and (memq xmltok-type
'(start-tag
2226 processing-instruction
2228 (nxml-token-ends-line-p)
2229 (nxml-token-begins-line-p))
2230 (or (search-forward "\n" nil t
)
2232 ((and (eq xmltok-type
'start-tag
)
2233 (nxml-token-begins-line-p)
2234 (not (save-excursion
2235 (goto-char xmltok-start
)
2236 (nxml-in-mixed-content-p nil
))))
2238 (goto-char xmltok-start
)
2239 (skip-chars-backward " \t")
2240 ;; include any blank line before
2241 (or (and (eq (char-before) ?
\n)
2243 (goto-char (1- (point)))
2244 (skip-chars-backward " \t")
2245 (and (bolp) (point))))
2248 (defun nxml-token-ends-line-p () (looking-at "[ \t]*$"))
2250 (defun nxml-token-begins-line-p ()
2252 (goto-char xmltok-start
)
2253 (skip-chars-backward " \t")
2256 (defun nxml-in-mixed-content-p (endp)
2257 "Return non-nil if point is in mixed content.
2258 Point must be after an end-tag or before a start-tag.
2259 ENDP is t in the former case, nil in the latter."
2260 (let (matching-tag-pos)
2261 (cond ((not (run-hook-with-args-until-failure
2262 'nxml-in-mixed-content-hook
))
2264 ;; See if the matching tag does not start or end a line.
2265 ((condition-case nil
2267 (setq matching-tag-pos
2270 (and (nxml-scan-element-backward (point))
2272 (nxml-scan-element-forward (point)))))
2273 (and matching-tag-pos
2275 (goto-char matching-tag-pos
)
2278 (skip-chars-backward " \t")
2280 (looking-at "[ \t]*$"))))))
2281 (nxml-scan-error nil
))
2283 ;; See if there's data at the same level.
2286 (setq start matching-tag-pos
2289 end matching-tag-pos
))
2293 (nxml-preceding-sibling-data-p))
2296 (nxml-following-sibling-data-p)))))
2298 ;; Otherwise, treat as not mixed
2301 (defun nxml-preceding-sibling-data-p ()
2302 "Return non-nil if there is a previous sibling that is data."
2303 (let ((lim (max (- (point) nxml-mixed-scan-distance
)
2309 (while (and (< lim
(point))
2314 (search-backward "<" lim t
)))
2315 (nxml-move-outside-backwards)
2318 (let ((prev-level level
))
2319 (cond ((eq xmltok-type
'end-tag
)
2320 (setq level
(1+ level
)))
2321 ((eq xmltok-type
'start-tag
)
2322 (setq level
(1- level
))))
2323 (when (eq prev-level
0)
2324 (while (and (< (point) end
) (not found
))
2326 (when (memq xmltok-type
'(data cdata-section char-ref
))
2327 (setq found t
)))))))))
2330 (defun nxml-following-sibling-data-p ()
2331 (let ((lim (min (+ (point) nxml-mixed-scan-distance
)
2337 (while (and (< (point) lim
)
2339 (nxml-tokenize-forward)
2341 (cond ((eq xmltok-type
'start-tag
)
2342 (setq level
(1+ level
)))
2343 ((eq xmltok-type
'end-tag
)
2344 (setq level
(1- level
)))
2346 (memq xmltok-type
'(data cdata-section char-ref
)))
2352 (defun nxml-do-fill-paragraph (arg)
2353 (let (fill-paragraph-function
2357 (nxml-forward-paragraph)
2359 (nxml-backward-paragraph)
2360 (skip-chars-forward " \t\r\n")
2361 (setq start
(point))
2363 (setq fill-prefix
(buffer-substring-no-properties (point) start
))
2364 (when (and (not (nxml-get-inside (point)))
2365 (looking-at "[ \t]*<!--"))
2366 (setq fill-prefix
(concat fill-prefix
" ")))
2367 (fill-region-as-paragraph start end arg
))
2368 (skip-line-prefix fill-prefix
)
2371 (defun nxml-newline-and-indent (soft)
2372 (delete-horizontal-space)
2373 (if soft
(insert-and-inherit ?
\n) (newline 1))
2379 (defvar nxml-dynamic-markup-prev-pos nil
)
2380 (defvar nxml-dynamic-markup-prev-lengths nil
)
2381 (defvar nxml-dynamic-markup-prev-found-marker nil
)
2382 (defvar nxml-dynamic-markup-prev-start-tags
(make-hash-table :test
'equal
))
2384 (defun nxml-dynamic-markup-word ()
2385 "Dynamically markup the word before point.
2386 This attempts to find a tag to put around the word before point based
2387 on the contents of the current buffer. The end-tag will be inserted at
2388 point. The start-tag will be inserted at or before the beginning of
2389 the word before point; the contents of the current buffer is used to
2392 It works in a similar way to \\[dabbrev-expand]. It searches first
2393 backwards from point, then forwards from point for an element whose
2394 content is a string which matches the contents of the buffer before
2395 point and which includes at least the word before point. It then
2396 copies the start- and end-tags from that element and uses them to
2397 surround the matching string before point.
2399 Repeating \\[nxml-dynamic-markup-word] immediately after successful
2400 \\[nxml-dynamic-markup-word] removes the previously inserted markup
2401 and attempts to find another possible way to do the markup."
2403 (let (search-start-pos)
2404 (if (and (integerp nxml-dynamic-markup-prev-pos
)
2405 (= nxml-dynamic-markup-prev-pos
(point))
2406 (eq last-command this-command
)
2407 nxml-dynamic-markup-prev-lengths
)
2408 (let* ((end-tag-open-pos
2409 (- nxml-dynamic-markup-prev-pos
2410 (nth 2 nxml-dynamic-markup-prev-lengths
)))
2411 (start-tag-close-pos
2413 (nth 1 nxml-dynamic-markup-prev-lengths
)))
2415 (- start-tag-close-pos
2416 (nth 0 nxml-dynamic-markup-prev-lengths
))))
2417 (delete-region end-tag-open-pos nxml-dynamic-markup-prev-pos
)
2418 (delete-region start-tag-open-pos start-tag-close-pos
)
2419 (setq search-start-pos
2420 (marker-position nxml-dynamic-markup-prev-found-marker
)))
2421 (clrhash nxml-dynamic-markup-prev-start-tags
))
2422 (setq nxml-dynamic-markup-prev-pos nil
)
2423 (setq nxml-dynamic-markup-prev-lengths nil
)
2424 (setq nxml-dynamic-markup-prev-found-marker nil
)
2427 (let* ((pos (point))
2430 (unless (< (point) pos
)
2431 (error "No word to markup"))
2432 (buffer-substring-no-properties (point) pos
)))
2433 (search (concat word
"</"))
2435 (when search-start-pos
2436 (goto-char search-start-pos
))
2437 (while (and (not done
)
2438 (or (and (< (point) pos
)
2439 (or (search-backward search nil t
)
2440 (progn (goto-char pos
) nil
)))
2441 (search-forward search nil t
)))
2442 (goto-char (- (match-end 0) 2))
2443 (setq done
(nxml-try-copy-markup pos
)))
2445 (error (if (zerop (hash-table-count
2446 nxml-dynamic-markup-prev-start-tags
))
2447 "No possible markup found for `%s'"
2448 "No more markup possibilities found for `%s'")
2451 (defun nxml-try-copy-markup (word-end-pos)
2453 (let ((end-tag-pos (point)))
2454 (when (and (not (nxml-get-inside end-tag-pos
))
2455 (search-backward "<" nil t
)
2456 (not (nxml-get-inside (point))))
2458 (when (and (eq xmltok-type
'start-tag
)
2459 (< (point) end-tag-pos
))
2460 (let* ((start-tag-close-pos (point))
2462 (buffer-substring-no-properties xmltok-start
2463 start-tag-close-pos
))
2467 (buffer-substring-no-properties start-tag-close-pos
2470 (goto-char word-end-pos
)
2472 (re-search-backward (concat
2473 (regexp-quote (car words
))
2477 (setq words
(cdr words
))
2478 (skip-chars-backward " \t\r\n"))
2479 (when (and (not words
)
2481 (skip-chars-forward " \t\r\n")
2482 (not (gethash (cons (point) start-tag
)
2483 nxml-dynamic-markup-prev-start-tags
)))
2484 (or (< end-tag-pos
(point))
2485 (< word-end-pos xmltok-start
)))
2486 (setq nxml-dynamic-markup-prev-found-marker
2487 (copy-marker end-tag-pos t
))
2488 (puthash (cons (point) start-tag
)
2490 nxml-dynamic-markup-prev-start-tags
)
2491 (setq nxml-dynamic-markup-prev-lengths
2492 (list (- start-tag-close-pos xmltok-start
)
2493 (- word-end-pos
(point))
2494 (+ (- xmltok-name-end xmltok-start
) 2)))
2495 (let ((name (xmltok-start-tag-qname)))
2497 (goto-char (+ word-end-pos
2498 (- start-tag-close-pos xmltok-start
)))
2499 (insert "</" name
">")
2500 (setq nxml-dynamic-markup-prev-pos
(point))))))))))
2505 (defvar nxml-char-name-ignore-case t
)
2507 (defvar nxml-char-name-alist nil
2508 "Alist of character names.
2509 Each member of the list has the form (NAME CODE . NAMESET),
2510 where NAME is a string naming a character, NAMESET is a symbol
2511 identifying a set of names and CODE is an integer specifying the
2512 Unicode scalar value of the named character.
2513 The NAME will only be used for completion if NAMESET has
2514 a non-nil `nxml-char-name-set-enabled' property.
2515 If NAMESET does does not have `nxml-char-name-set-defined' property,
2516 then it must have a `nxml-char-name-set-file' property and `load'
2517 will be applied to the value of this property if the nameset
2520 (defvar nxml-char-name-table
(make-hash-table :test
'eq
)
2521 "Hash table for mapping char codes to names.
2522 Each key is a Unicode scalar value.
2523 Each value is a list of pairs of the form (NAMESET . NAME),
2524 where NAMESET is a symbol identifying a set of names,
2525 and NAME is a string naming a character.")
2527 (defvar nxml-autoload-char-name-set-list nil
2528 "List of char namesets that can be autoloaded.")
2530 (defun nxml-enable-char-name-set (nameset)
2531 (put nameset
'nxml-char-name-set-enabled t
))
2533 (defun nxml-disable-char-name-set (nameset)
2534 (put nameset
'nxml-char-name-set-enabled nil
))
2536 (defun nxml-char-name-set-enabled-p (nameset)
2537 (get nameset
'nxml-char-name-set-enabled
))
2539 (defun nxml-autoload-char-name-set (nameset file
)
2540 (unless (memq nameset nxml-autoload-char-name-set-list
)
2541 (setq nxml-autoload-char-name-set-list
2542 (cons nameset nxml-autoload-char-name-set-list
)))
2543 (put nameset
'nxml-char-name-set-file file
))
2545 (defun nxml-define-char-name-set (nameset alist
)
2546 "Define a set of character names.
2547 NAMESET is a symbol identifying the set.
2548 ALIST is a list where each member has the form (NAME CODE),
2549 where NAME is a string naming a character and code is an
2550 integer giving the Unicode scalar value of the character."
2551 (when (get nameset
'nxml-char-name-set-defined
)
2552 (error "Nameset `%s' already defined" nameset
))
2555 (let* ((name-code (car iter
))
2556 (name (car name-code
))
2557 (code (cadr name-code
)))
2559 (cons (cons nameset name
)
2560 (gethash code nxml-char-name-table
))
2561 nxml-char-name-table
))
2562 (setcdr (cdr (car iter
)) nameset
)
2563 (setq iter
(cdr iter
))))
2564 (setq nxml-char-name-alist
2565 (nconc alist nxml-char-name-alist
))
2566 (put nameset
'nxml-char-name-set-defined t
))
2568 (defun nxml-get-char-name (code)
2569 (mapc 'nxml-maybe-load-char-name-set nxml-autoload-char-name-set-list
)
2570 (let ((names (gethash code nxml-char-name-table
))
2572 (while (and names
(not name
))
2573 (if (nxml-char-name-set-enabled-p (caar names
))
2574 (setq name
(cdar names
))
2575 (setq names
(cdr names
))))
2578 (defvar nxml-named-char-history nil
)
2580 (defun nxml-insert-named-char (arg)
2581 "Insert a character using its name.
2582 The name is read from the minibuffer.
2583 Normally, inserts the character as a numeric character reference.
2584 With a prefix argument, inserts the character directly."
2586 (mapc 'nxml-maybe-load-char-name-set nxml-autoload-char-name-set-list
)
2588 (let ((completion-ignore-case nxml-char-name-ignore-case
))
2589 (completing-read "Character name: "
2590 nxml-char-name-alist
2592 (get (cddr member
) 'nxml-char-name-set-enabled
))
2595 'nxml-named-char-history
)))
2596 (alist nxml-char-name-alist
)
2598 (while (and alist
(not code
))
2599 (setq elt
(assoc name alist
))
2600 (if (get (cddr elt
) 'nxml-char-name-set-enabled
)
2601 (setq code
(cadr elt
))
2602 (setq alist
(cdr (member elt alist
)))))
2605 (or (decode-char 'ucs code
)
2606 (error "Character %x is not supported by Emacs"
2608 (format "&#x%X;" code
))))))
2610 (defun nxml-maybe-load-char-name-set (sym)
2611 (when (and (get sym
'nxml-char-name-set-enabled
)
2612 (not (get sym
'nxml-char-name-set-defined
))
2613 (stringp (get sym
'nxml-char-name-set-file
)))
2614 (load (get sym
'nxml-char-name-set-file
))))
2616 (defun nxml-toggle-char-ref-extra-display (arg)
2617 "Toggle the display of extra information for character references."
2619 (let ((new (if (null arg
)
2620 (not nxml-char-ref-extra-display
)
2621 (> (prefix-numeric-value arg
) 0))))
2622 (when (not (eq new nxml-char-ref-extra-display
))
2623 (setq nxml-char-ref-extra-display new
)
2624 (font-lock-fontify-buffer))))
2626 (put 'nxml-char-ref
'evaporate t
)
2628 (defun nxml-char-ref-display-extra (start end n
)
2629 (when nxml-char-ref-extra-display
2630 (let ((name (nxml-get-char-name n
))
2631 (glyph-string (and nxml-char-ref-display-glyph-flag
2632 (nxml-glyph-display-string n
'nxml-glyph
)))
2634 (when (or name glyph-string
)
2635 (setq ov
(make-overlay start end nil t
))
2636 (overlay-put ov
'category
'nxml-char-ref
)
2638 (overlay-put ov
'help-echo name
))
2642 (propertize glyph-string
'face
'nxml-glyph
)))))))
2644 (defun nxml-clear-char-ref-extra-display (start end
)
2645 (let ((ov (overlays-in start end
)))
2647 (when (eq (overlay-get (car ov
) 'category
) 'nxml-char-ref
)
2648 (delete-overlay (car ov
)))
2649 (setq ov
(cdr ov
)))))
2652 (defun nxml-start-delimiter-length (type)
2653 (or (get type
'nxml-start-delimiter-length
)
2656 (put 'cdata-section
'nxml-start-delimiter-length
9)
2657 (put 'comment
'nxml-start-delimiter-length
4)
2658 (put 'processing-instruction
'nxml-start-delimiter-length
2)
2659 (put 'start-tag
'nxml-start-delimiter-length
1)
2660 (put 'empty-element
'nxml-start-delimiter-length
1)
2661 (put 'partial-empty-element
'nxml-start-delimiter-length
1)
2662 (put 'entity-ref
'nxml-start-delimiter-length
1)
2663 (put 'char-ref
'nxml-start-delimiter-length
2)
2665 (defun nxml-end-delimiter-length (type)
2666 (or (get type
'nxml-end-delimiter-length
)
2669 (put 'cdata-section
'nxml-end-delimiter-length
3)
2670 (put 'comment
'nxml-end-delimiter-length
3)
2671 (put 'processing-instruction
'nxml-end-delimiter-length
2)
2672 (put 'start-tag
'nxml-end-delimiter-length
1)
2673 (put 'empty-element
'nxml-end-delimiter-length
2)
2674 (put 'partial-empty-element
'nxml-end-delimiter-length
1)
2675 (put 'entity-ref
'nxml-end-delimiter-length
1)
2676 (put 'char-ref
'nxml-end-delimiter-length
1)
2678 (defun nxml-token-type-friendly-name (type)
2679 (or (get type
'nxml-friendly-name
)
2680 (symbol-name type
)))
2682 (put 'cdata-section
'nxml-friendly-name
"CDATA section")
2683 (put 'processing-instruction
'nxml-friendly-name
"processing instruction")
2684 (put 'entity-ref
'nxml-friendly-name
"entity reference")
2685 (put 'char-ref
'nxml-friendly-name
"character reference")
2688 (defalias 'xml-mode
'nxml-mode
)
2690 (provide 'nxml-mode
)
2692 ;;; nxml-mode.el ends here