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, or (at your option)
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; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 ;; See nxml-rap.el for description of parsing strategy.
29 ;; The font locking here is independent of font-lock.el. We want to
30 ;; do more sophisticated handling of changes and we want to use the
31 ;; same xmltok rather than regexps for parsing so that we parse
32 ;; consistently and correctly.
36 (when (featurep 'mucs
)
37 (error "nxml-mode is not compatible with Mule-UCS"))
46 (declare-function rng-nxml-mode-init
"rng-nxml")
47 (declare-function nxml-enable-unicode-char-name-sets
"nxml-uchnm")
52 "New XML editing mode"
56 (defgroup nxml-faces nil
57 "Faces for XML syntax highlighting."
59 :group
'font-lock-faces
)
61 (defcustom nxml-syntax-highlight-flag t
62 "*Non-nil means nxml-mode should perform syntax highlighting."
66 (defcustom nxml-char-ref-display-glyph-flag t
67 "*Non-nil means display glyph following character reference.
68 The glyph is displayed in face `nxml-glyph'. The hook
69 `nxml-glyph-set-hook' can be used to customize for which characters
70 glyphs are displayed."
74 (defcustom nxml-mode-hook nil
75 "Hook run by command `nxml-mode'."
79 (defcustom nxml-sexp-element-flag nil
80 "*Non-nil means sexp commands treat an element as a single expression."
84 (defcustom nxml-slash-auto-complete-flag nil
85 "*Non-nil means typing a slash automatically completes the end-tag.
86 This is used by `nxml-electric-slash'."
90 (defcustom nxml-child-indent
2
91 "*Indentation for the children of an element relative to the start-tag.
92 This only applies when the line or lines containing the start-tag contains
93 nothing else other than that start-tag."
97 (defcustom nxml-attribute-indent
4
98 "*Indentation for the attributes of an element relative to the start-tag.
99 This only applies when the first attribute of a tag starts a line. In other
100 cases, the first attribute on one line is indented the same as the first
101 attribute on the previous line."
105 (defvar nxml-fontify-chunk-size
500)
107 (defcustom nxml-bind-meta-tab-to-complete-flag
(not window-system
)
108 "*Non-nil means bind M-TAB in `nxml-mode-map' to `nxml-complete'.
109 C-return will be bound to `nxml-complete' in any case.
110 M-TAB gets swallowed by many window systems/managers, and
111 `documentation' will show M-TAB rather than C-return as the
112 binding `rng-complete' when both are bound. So it's better
113 to bind M-TAB only when it will work."
115 :set
(lambda (sym flag
)
116 (set-default sym flag
)
117 (when (and (boundp 'nxml-mode-map
) nxml-mode-map
)
118 (define-key nxml-mode-map
"\M-\t" (and flag
'nxml-complete
))))
121 (defcustom nxml-prefer-utf-16-to-utf-8-flag nil
122 "*Non-nil means prefer UTF-16 to UTF-8 when saving a buffer.
123 This is used only when a buffer does not contain an encoding declaration
124 and when its current `buffer-file-coding-system' specifies neither UTF-16
129 (defcustom nxml-prefer-utf-16-little-to-big-endian-flag
(eq system-type
131 "*Non-nil means prefer little-endian to big-endian byte-order for UTF-16.
132 This is used only for saving a buffer; when reading the byte-order is
133 auto-detected. It may be relevant both when there is no encoding declaration
134 and when the encoding declaration specifies `UTF-16'."
138 (defcustom nxml-default-buffer-file-coding-system nil
139 "*Default value for `buffer-file-coding-system' for a buffer for a new file.
140 Nil means use the default value of `buffer-file-coding-system' as normal.
141 A buffer's `buffer-file-coding-system' affects what \\[nxml-insert-xml-declaration] inserts."
143 :type
'coding-system
)
145 (defcustom nxml-auto-insert-xml-declaration-flag nil
146 "*Non-nil means automatically insert an XML declaration in a new file.
147 The XML declaration is inserted using `nxml-insert-xml-declaration'."
151 (defface nxml-delimited-data
152 '((t (:inherit font-lock-doc-face
)))
153 "Face used to highlight data enclosed between delimiters.
154 This is not used directly, but only via inheritance by other faces."
158 '((t (:inherit font-lock-builtin-face
)))
159 "Face used to highlight various names.
160 This includes element and attribute names, processing
161 instruction targets and the CDATA keyword in a CDATA section.
162 This is not used directly, but only via inheritance by other faces."
166 '((t (:inherit font-lock-constant-face
)))
167 "Face used to highlight character and entity references.
168 This is not used directly, but only via inheritance by other faces."
171 (defface nxml-delimiter
173 "Face used to highlight delimiters.
174 This is not used directly, but only via inheritance by other faces."
179 "Face used to highlight text."
182 (defface nxml-comment-content
183 '((t (:inherit font-lock-comment-face
)))
184 "Face used to highlight the content of comments."
187 (defface nxml-comment-delimiter
188 '((t (:inherit font-lock-comment-delimiter-face
)))
189 "Face used for the delimiters of comments, i.e <!-- and -->."
192 (defface nxml-processing-instruction-delimiter
193 '((t (:inherit nxml-delimiter
)))
194 "Face used for the delimiters of processing instructions, i.e <? and ?>."
197 (defface nxml-processing-instruction-target
198 '((t (:inherit font-lock-keyword-face
)))
199 "Face used for the target of processing instructions."
202 (defface nxml-processing-instruction-content
203 '((t (:inherit nxml-delimited-data
)))
204 "Face used for the content of processing instructions."
207 (defface nxml-cdata-section-delimiter
208 '((t (:inherit nxml-delimiter
)))
209 "Face used for the delimiters of CDATA sections, i.e <![, [, and ]]>."
212 (defface nxml-cdata-section-CDATA
213 '((t (:inherit nxml-name
)))
214 "Face used for the CDATA keyword in CDATA sections."
217 (defface nxml-cdata-section-content
218 '((t (:inherit nxml-text
)))
219 "Face used for the content of CDATA sections."
222 (defface nxml-char-ref-number
223 '((t (:inherit nxml-ref
)))
224 "Face used for the number in character references.
225 This includes ths `x' in hex references."
228 (defface nxml-char-ref-delimiter
229 '((t (:inherit nxml-ref
)))
230 "Face used for the delimiters of character references, i.e &# and ;."
233 (defface nxml-entity-ref-name
234 '((t (:inherit nxml-ref
)))
235 "Face used for the entity name in general entity references."
238 (defface nxml-entity-ref-delimiter
239 '((t (:inherit nxml-ref
)))
240 "Face used for the delimiters of entity references, i.e & and ;."
243 (defface nxml-tag-delimiter
244 '((t (:inherit nxml-delimiter
)))
245 "Face used for the angle brackets delimiting tags.
246 `nxml-tag-slash' is used for slashes."
249 (defface nxml-tag-slash
250 '((t (:inherit nxml-tag-delimiter
)))
251 "Face used for slashes in tags, both in end-tags and empty-elements."
254 (defface nxml-element-prefix
255 '((t (:inherit nxml-name
)))
256 "Face used for the prefix of elements."
259 (defface nxml-element-colon
261 "Face used for the colon in element names."
264 (defface nxml-element-local-name
265 '((t (:inherit font-lock-function-name-face
)))
266 "Face used for the local name of elements."
269 (defface nxml-attribute-prefix
270 '((t (:inherit nxml-name
)))
271 "Face used for the prefix of attributes."
274 (defface nxml-attribute-colon
275 '((t (:inherit nxml-delimiter
)))
276 "Face used for the colon in attribute names."
279 (defface nxml-attribute-local-name
280 '((t (:inherit font-lock-variable-name-face
)))
281 "Face used for the local name of attributes."
284 (defface nxml-namespace-attribute-xmlns
285 '((t (:inherit nxml-attribute-prefix
)))
286 "Face used for `xmlns' in namespace attributes."
289 (defface nxml-namespace-attribute-colon
290 '((t (:inherit nxml-attribute-colon
)))
291 "Face used for the colon in namespace attributes."
294 (defface nxml-namespace-attribute-prefix
295 '((t (:inherit nxml-attribute-local-name
)))
296 "Face used for the prefix declared in namespace attributes."
299 (defface nxml-attribute-value
300 '((t (:inherit font-lock-string-face
)))
301 "Face used for the value of attributes."
304 (defface nxml-attribute-value-delimiter
305 '((t (:inherit nxml-attribute-value
)))
306 "Face used for the delimiters of attribute values."
309 (defface nxml-namespace-attribute-value
310 '((t (:inherit nxml-attribute-value
)))
311 "Face used for the value of namespace attributes."
314 (defface nxml-namespace-attribute-value-delimiter
315 '((t (:inherit nxml-attribute-value-delimiter
)))
316 "Face used for the delimiters of namespace attribute values."
319 (defface nxml-prolog-literal-delimiter
320 '((t (:inherit nxml-delimited-data
)))
321 "Face used for the delimiters of literals in the prolog."
324 (defface nxml-prolog-literal-content
325 '((t (:inherit nxml-delimited-data
)))
326 "Face used for the content of literals in the prolog."
329 (defface nxml-prolog-keyword
330 '((t (:inherit font-lock-keyword-face
)))
331 "Face used for keywords in the prolog."
334 (defface nxml-markup-declaration-delimiter
335 '((t (:inherit nxml-delimiter
)))
336 "Face used for the delimiters of markup declarations in the prolog.
337 The delimiters are <! and >."
341 '((t (:inherit nxml-name
)))
342 "Face used for # before a name in the prolog."
366 "Face used for glyph for char references."
371 (defvar nxml-prolog-regions nil
372 "List of regions in the prolog to be fontified.
373 See the function `xmltok-forward-prolog' for more information.")
374 (make-variable-buffer-local 'nxml-prolog-regions
)
376 (defvar nxml-last-fontify-end nil
377 "Position where fontification last ended.
378 Nil if the buffer changed since the last fontification.")
379 (make-variable-buffer-local 'nxml-last-fontify-end
)
381 (defvar nxml-degraded nil
382 "Non-nil if currently operating in degraded mode.
383 Degraded mode is enabled when an internal error is encountered in the
384 fontification or after-change functions.")
385 (make-variable-buffer-local 'nxml-degraded
)
387 (defvar nxml-completion-hook nil
388 "Hook run by `nxml-complete'.
389 This hook is run until success.")
391 (defvar nxml-in-mixed-content-hook nil
392 "Hook to determine whether point is in mixed content.
393 The hook is called without arguments. It should return nil if it is
394 definitely not mixed; non-nil otherwise. The hook will be run until
395 one of the functions returns nil.")
397 (defvar nxml-mixed-scan-distance
4000
398 "Maximum distance from point to scan when checking for mixed content.")
400 (defvar nxml-end-tag-indent-scan-distance
4000
401 "Maximum distance from point to scan backwards when indenting end-tag.")
403 (defvar nxml-char-ref-extra-display t
404 "Non-nil means display extra information for character references.
405 The extra information consists of a tooltip with the character name
406 and, if `nxml-char-ref-display-glyph-flag' is non-nil, a glyph
407 corresponding to the referenced character following the character
409 (make-variable-buffer-local 'nxml-char-ref-extra-display
)
411 (defvar nxml-mode-map
412 (let ((map (make-sparse-keymap)))
413 (define-key map
"\M-\C-u" 'nxml-backward-up-element
)
414 (define-key map
"\M-\C-d" 'nxml-down-element
)
415 (define-key map
"\M-\C-n" 'nxml-forward-element
)
416 (define-key map
"\M-\C-p" 'nxml-backward-element
)
417 (define-key map
"\M-{" 'nxml-backward-paragraph
)
418 (define-key map
"\M-}" 'nxml-forward-paragraph
)
419 (define-key map
"\M-h" 'nxml-mark-paragraph
)
420 (define-key map
"\C-c\C-f" 'nxml-finish-element
)
421 (define-key map
"\C-c\C-m" 'nxml-split-element
)
422 (define-key map
"\C-c\C-b" 'nxml-balanced-close-start-tag-block
)
423 (define-key map
"\C-c\C-i" 'nxml-balanced-close-start-tag-inline
)
424 (define-key map
"\C-c\C-x" 'nxml-insert-xml-declaration
)
425 (define-key map
"\C-c\C-d" 'nxml-dynamic-markup-word
)
427 (define-key map
"\C-c\C-u" 'nxml-insert-named-char
)
428 (define-key map
"\C-c\C-o" nxml-outline-prefix-map
)
429 (define-key map
[S-mouse-2
] 'nxml-mouse-hide-direct-text-content
)
430 (define-key map
"/" 'nxml-electric-slash
)
431 (define-key map
[C-return
] 'nxml-complete
)
432 (when nxml-bind-meta-tab-to-complete-flag
433 (define-key map
"\M-\t" 'nxml-complete
))
435 "Keymap for nxml-mode.")
437 (defsubst nxml-set-face
(start end face
)
438 (when (and face
(< start end
))
439 (put-text-property start end
'face face
)))
441 (defun nxml-clear-face (start end
)
442 (remove-text-properties start end
'(face nil
))
443 (nxml-clear-char-ref-extra-display start end
))
445 (defsubst nxml-set-fontified
(start end
)
446 (put-text-property start end
'fontified t
))
448 (defsubst nxml-clear-fontified
(start end
)
449 (remove-text-properties start end
'(fontified nil
)))
453 ;; We use C-c C-i instead of \\[nxml-balanced-close-start-tag-inline]
454 ;; because Emacs turns C-c C-i into C-c TAB which is hard to type and
456 "Major mode for editing XML.
458 Syntax highlighting is performed unless the variable
459 `nxml-syntax-highlight-flag' is nil.
461 \\[nxml-finish-element] finishes the current element by inserting an end-tag.
462 C-c C-i closes a start-tag with `>' and then inserts a balancing end-tag
463 leaving point between the start-tag and end-tag.
464 \\[nxml-balanced-close-start-tag-block] is similar but for block rather than inline elements:
465 the start-tag, point, and end-tag are all left on separate lines.
466 If `nxml-slash-auto-complete-flag' is non-nil, then inserting a `</'
467 automatically inserts the rest of the end-tag.
469 \\[nxml-complete] performs completion on the symbol preceding point.
471 \\[nxml-dynamic-markup-word] uses the contents of the current buffer
472 to choose a tag to put around the word preceding point.
474 Sections of the document can be displayed in outline form. The
475 variable `nxml-section-element-name-regexp' controls when an element
476 is recognized as a section. The same key sequences that change
477 visibility in outline mode are used except that they start with C-c C-o
480 Validation is provided by the related minor-mode `rng-validate-mode'.
481 This also makes completion schema- and context- sensitive. Element
482 names, attribute names, attribute values and namespace URIs can all be
483 completed. By default, `rng-validate-mode' is automatically enabled. You
484 can toggle it using \\[rng-validate-mode] or change the default by
485 customizing `rng-nxml-auto-validate-flag'.
487 \\[indent-for-tab-command] indents the current line appropriately.
488 This can be customized using the variable `nxml-child-indent'
489 and the variable `nxml-attribute-indent'.
491 \\[nxml-insert-named-char] inserts a character reference using
492 the character's name (by default, the Unicode name). \\[universal-argument] \\[nxml-insert-named-char]
493 inserts the character directly.
495 The Emacs commands that normally operate on balanced expressions will
496 operate on XML markup items. Thus \\[forward-sexp] will move forward
497 across one markup item; \\[backward-sexp] will move backward across
498 one markup item; \\[kill-sexp] will kill the following markup item;
499 \\[mark-sexp] will mark the following markup item. By default, each
500 tag each treated as a single markup item; to make the complete element
501 be treated as a single markup item, set the variable
502 `nxml-sexp-element-flag' to t. For more details, see the function
503 `nxml-forward-balanced-item'.
505 \\[nxml-backward-up-element] and \\[nxml-down-element] move up and down the element structure.
507 Many aspects this mode can be customized using
508 \\[customize-group] nxml RET."
510 (kill-all-local-variables)
511 (setq major-mode
'nxml-mode
)
512 (setq mode-name
"nXML")
513 (set (make-local-variable 'mode-line-process
) '((nxml-degraded "/degraded")))
514 ;; We'll determine the fill prefix ourselves
515 (make-local-variable 'adaptive-fill-mode
)
516 (setq adaptive-fill-mode nil
)
517 (make-local-variable 'forward-sexp-function
)
518 (setq forward-sexp-function
'nxml-forward-balanced-item
)
519 (make-local-variable 'indent-line-function
)
520 (setq indent-line-function
'nxml-indent-line
)
521 (make-local-variable 'fill-paragraph-function
)
522 (setq fill-paragraph-function
'nxml-do-fill-paragraph
)
524 ;; This doesn't seem to work too well;
525 ;; I think we should probably roll our own nxml-comment-dwim function.
526 (make-local-variable 'comment-indent-function
)
527 (setq comment-indent-function
'nxml-indent-line
)
528 (make-local-variable 'comment-start
)
529 (setq comment-start
"<!--")
530 (make-local-variable 'comment-start-skip
)
531 (setq comment-start-skip
"<!--[ \t\r\n]*")
532 (make-local-variable 'comment-end
)
533 (setq comment-end
"-->")
534 (make-local-variable 'comment-end-skip
)
535 (setq comment-end-skip
"[ \t\r\n]*-->")
536 (make-local-variable 'comment-line-break-function
)
537 (setq comment-line-break-function
'nxml-newline-and-indent
)
538 (use-local-map nxml-mode-map
)
542 (nxml-clear-dependent-regions (point-min) (point-max))
543 (setq nxml-scan-end
(copy-marker (point-min) nil
))
544 (nxml-with-unmodifying-text-property-changes
545 (when nxml-syntax-highlight-flag
546 (nxml-clear-fontified (point-min) (point-max)))
547 (nxml-clear-inside (point-min) (point-max))
548 (nxml-with-invisible-motion
549 (nxml-scan-prolog)))))
550 (when nxml-syntax-highlight-flag
551 (add-hook 'fontification-functions
'nxml-fontify nil t
))
552 (add-hook 'after-change-functions
'nxml-after-change nil t
)
553 ;; Emacs 23 handles the encoding attribute on the xml declaration
554 ;; transparently to nxml-mode, so there is no longer a need for the below
555 ;; hook. The hook also had the drawback of overriding explicit user
556 ;; instruction to save as some encoding other than utf-8.
557 ;;; (add-hook 'write-contents-hooks 'nxml-prepare-to-save)
558 (when (not (and (buffer-file-name) (file-exists-p (buffer-file-name))))
559 (when (and nxml-default-buffer-file-coding-system
560 (not (local-variable-p 'buffer-file-coding-system
)))
561 (setq buffer-file-coding-system nxml-default-buffer-file-coding-system
))
562 (when nxml-auto-insert-xml-declaration-flag
563 (nxml-insert-xml-declaration)))
565 (nxml-enable-unicode-char-name-sets)
566 (run-hooks 'nxml-mode-hook
))
568 (defun nxml-degrade (context err
)
569 (message "Internal nXML mode error in %s (%s), degrading"
571 (error-message-string err
))
573 (setq nxml-degraded t
)
574 (setq nxml-prolog-end
1)
578 (nxml-with-unmodifying-text-property-changes
579 (nxml-clear-face (point-min) (point-max))
580 (nxml-set-fontified (point-min) (point-max))
581 (nxml-clear-inside (point-min) (point-max))))))
583 ;;; Change management
585 (defun nxml-after-change (start end pre-change-length
)
586 ;; Work around bug in insert-file-contents.
587 (when (> end
(1+ (buffer-size)))
589 (setq end
(1+ (buffer-size))))
590 (unless nxml-degraded
596 (nxml-with-invisible-motion
597 (nxml-with-unmodifying-text-property-changes
598 (nxml-after-change1 start end pre-change-length
))))))
600 (nxml-degrade 'nxml-after-change err
)))))
602 (defun nxml-after-change1 (start end pre-change-length
)
603 (setq nxml-last-fontify-end nil
)
604 (let ((pre-change-end (+ start pre-change-length
)))
606 (nxml-adjust-start-for-dependent-regions start
610 ;; Add 2 so as to include the < and following char
611 ;; that start the instance, since changing these
612 ;; can change where the prolog ends.
613 (+ nxml-prolog-end
2))
614 ;; end must be extended to at least the end of the old prolog
615 (when (< pre-change-end nxml-prolog-end
)
617 ;; don't let end get out of range even if pre-change-length
620 (+ end
(- nxml-prolog-end pre-change-end
)))))
622 (cond ((<= end nxml-prolog-end
)
623 (setq end nxml-prolog-end
)
625 ;; This is so that Emacs redisplay works
626 (setq start
(line-beginning-position)))
627 ((and (<= start nxml-scan-end
)
628 (> start
(point-min))
629 (nxml-get-inside (1- start
)))
630 ;; The closing delimiter might have been removed.
631 ;; So we may need to redisplay from the beginning
633 (goto-char (1- start
))
634 (nxml-move-outside-backwards)
635 ;; This is so that Emacs redisplay works
636 (setq start
(line-beginning-position))
637 (setq end
(max (nxml-scan-after-change (point) end
)
641 ;; This is both for redisplay and to move back
642 ;; past any incomplete opening delimiters
643 (setq start
(line-beginning-position))
644 (setq end
(max (nxml-scan-after-change start end
)
646 (when nxml-syntax-highlight-flag
648 ;; Must clear at least one char so as to trigger redisplay.
649 (cond ((< start
(point-max))
650 (setq end
(1+ start
)))
652 (setq end
(point-max))
654 (setq start
(line-beginning-position)))))
655 (nxml-clear-fontified start end
)))
659 (defun nxml-insert-xml-declaration ()
660 "Insert an XML declaration at the beginning of buffer.
661 The XML declaration will declare an encoding depending on the buffer's
662 `buffer-file-coding-system'."
665 (if (and buffer-file-coding-system
666 (coding-system-p buffer-file-coding-system
)
667 (coding-system-get buffer-file-coding-system
669 buffer-file-coding-system
670 (nxml-choose-utf-coding-system))))
671 (goto-char (point-min))
672 (insert (format "<?xml version=\"1.0\" encoding=\"%s\"?>\n"
673 (nxml-coding-system-name coding-system
)))))
675 (defun nxml-prepare-to-save ()
676 (unless (and (not enable-multibyte-characters
)
677 (local-variable-p 'buffer-file-coding-system
)
678 buffer-file-coding-system
679 (or (eq (coding-system-type buffer-file-coding-system
) 5)
680 (eq buffer-file-coding-system
'no-conversion
)))
682 (setq buffer-file-coding-system
(nxml-select-coding-system))))
683 ;; nil from a function in `write-contents-hooks' means
684 ;; to continue and write the file as normal
687 (defun nxml-select-coding-system ()
688 (let* ((suitable-coding-systems
689 (find-coding-systems-region (point-min) (point-max)))
691 (goto-char (point-min))
692 (xmltok-get-declared-encoding-position)))
695 (buffer-substring-no-properties (car enc-pos
)
699 (if (string= (downcase enc-name
) "utf-16")
700 (nxml-choose-utf-16-coding-system)
701 (nxml-mime-charset-coding-system enc-name
)))
702 (enc-pos (nxml-choose-utf-coding-system)))))
703 ;; Make sure we have a coding-system
704 (unless coding-system
706 (and (not buffer-read-only
)
707 (nxml-choose-suitable-coding-system
708 suitable-coding-systems
)))
711 (format "Unknown encoding %s" enc-name
)
712 "XML declaration is not well-formed")))
713 (cond ((not coding-system
)
714 (error "%s" message
))
720 "Modify and save with encoding %s")
721 (nxml-coding-system-name coding-system
))
723 (nxml-fix-encoding-declaration enc-pos coding-system
))
724 (t (signal 'quit nil
)))))
725 ;; Make sure it can encode all the characters in the buffer
726 (unless (or (memq (coding-system-base coding-system
)
727 suitable-coding-systems
)
728 (equal suitable-coding-systems
'(undecided)))
730 (nxml-unsuitable-coding-system-message coding-system
733 (and (not buffer-read-only
)
734 (nxml-choose-suitable-coding-system
735 suitable-coding-systems
)))
736 (cond ((not coding-system
) (error "%s" message
))
737 ((y-or-n-p (concat message
738 (format ". Save with %s "
739 (nxml-coding-system-name
741 (nxml-fix-encoding-declaration enc-pos coding-system
))
742 (t (signal 'quit nil
)))))
743 ;; Merge the newline type of our existing encoding
744 (let ((current-eol-type
745 (coding-system-eol-type buffer-file-coding-system
)))
746 (when (and current-eol-type
(integerp current-eol-type
))
748 (coding-system-change-eol-conversion coding-system
752 (defun nxml-unsuitable-coding-system-message (coding-system &optional enc-name
)
753 (if (nxml-coding-system-unicode-p coding-system
)
754 "Cannot translate some characters to Unicode"
755 (format "Cannot encode some characters with %s"
757 (nxml-coding-system-name coding-system
)))))
759 (defconst nxml-utf-16-coding-systems
(and (coding-system-p 'utf-16-be
)
760 (coding-system-p 'utf-16-le
)
761 '(utf-16-be utf-16-le
)))
763 (defconst nxml-utf-coding-systems
(cons 'utf-8 nxml-utf-16-coding-systems
))
765 (defun nxml-coding-system-unicode-p (coding-system)
766 (nxml-coding-system-member (coding-system-base coding-system
)
767 nxml-utf-coding-systems
))
769 (defun nxml-coding-system-name (coding-system)
770 (setq coding-system
(coding-system-base coding-system
))
772 (if (nxml-coding-system-member coding-system nxml-utf-16-coding-systems
)
774 (or (coding-system-get coding-system
'mime-charset
)
777 (defun nxml-fix-encoding-declaration (enc-pos coding-system
)
778 (let ((charset (nxml-coding-system-name coding-system
)))
779 (cond ((consp enc-pos
)
780 (delete-region (car enc-pos
) (cdr enc-pos
))
781 (goto-char (car enc-pos
))
785 (insert " encoding=\"" charset ?
\"))
787 (goto-char (point-min))
788 (insert "<?xml version=\"1.0\" encoding=\""
791 (when (and (not enc-pos
)
792 (let ((case-fold-search t
))
793 (looking-at xmltok-bad-xml-decl-regexp
)))
794 (delete-region (point) (match-end 0)))))))
796 (defun nxml-choose-suitable-coding-system (suitable-coding-systems)
797 (let (ret coding-system
)
798 (if (and buffer-file-coding-system
799 (memq (coding-system-base buffer-file-coding-system
)
800 suitable-coding-systems
))
801 buffer-file-coding-system
802 (while (and suitable-coding-systems
(not ret
))
803 (setq coding-system
(car suitable-coding-systems
))
804 (if (coding-system-get coding-system
'mime-charset
)
805 (setq ret coding-system
)
806 (setq suitable-coding-systems
(cdr suitable-coding-systems
))))
809 (defun nxml-choose-utf-coding-system ()
810 (let ((cur (and (local-variable-p 'buffer-file-coding-system
)
811 buffer-file-coding-system
812 (coding-system-base buffer-file-coding-system
))))
813 (cond ((car (nxml-coding-system-member cur nxml-utf-coding-systems
)))
814 ((and nxml-prefer-utf-16-to-utf-8-flag
815 (coding-system-p 'utf-16-le
)
816 (coding-system-p 'utf-16-be
))
817 (if nxml-prefer-utf-16-little-to-big-endian-flag
822 (defun nxml-choose-utf-16-coding-system ()
823 (let ((cur (and (local-variable-p 'buffer-file-coding-system
)
824 buffer-file-coding-system
825 (coding-system-base buffer-file-coding-system
))))
826 (cond ((car (nxml-coding-system-member cur nxml-utf-16-coding-systems
)))
827 (nxml-prefer-utf-16-little-to-big-endian-flag
828 (and (coding-system-p 'utf-16-le
) 'utf-16-le
))
829 (t (and (coding-system-p 'utf-16-be
) 'utf-16-be
)))))
831 (defun nxml-coding-system-member (coding-system coding-systems
)
833 (while (and coding-systems
(not ret
))
834 (if (coding-system-equal coding-system
835 (car coding-systems
))
836 (setq ret coding-systems
)
837 (setq coding-systems
(cdr coding-systems
))))
842 (defun nxml-fontify (start)
848 (nxml-with-invisible-motion
849 (nxml-with-unmodifying-text-property-changes
850 (if (or nxml-degraded
851 ;; just in case we get called in the wrong buffer
852 (not nxml-prolog-end
))
853 (nxml-set-fontified start
(point-max))
854 (nxml-fontify1 start
)))))))
856 (nxml-degrade 'nxml-fontify err
))))
858 (defun nxml-fontify1 (start)
859 (cond ((< start nxml-prolog-end
)
860 (nxml-fontify-prolog)
861 (nxml-set-fontified (point-min)
865 (when (not (eq nxml-last-fontify-end start
))
866 (when (not (equal (char-after) ?\
<))
867 (search-backward "<" nxml-prolog-end t
))
868 (nxml-ensure-scan-up-to-date)
869 (nxml-move-outside-backwards))
870 (let ((start (point)))
871 (nxml-do-fontify (min (point-max)
872 (+ start nxml-fontify-chunk-size
)))
873 (setq nxml-last-fontify-end
(point))
874 (nxml-set-fontified start nxml-last-fontify-end
)))))
876 (defun nxml-fontify-buffer ()
881 (nxml-with-invisible-motion
882 (goto-char (point-min))
883 (nxml-with-unmodifying-text-property-changes
884 (nxml-fontify-prolog)
885 (goto-char nxml-prolog-end
)
886 (nxml-do-fontify))))))
888 (defun nxml-fontify-prolog ()
890 The buffer is assumed to be prepared for fontification.
891 This does not set the fontified property, but it does clear
892 faces appropriately."
893 (let ((regions nxml-prolog-regions
))
894 (nxml-clear-face (point-min) nxml-prolog-end
)
896 (let ((region (car regions
)))
897 (nxml-apply-fontify-rule (aref region
0)
900 (setq regions
(cdr regions
)))))
902 (defun nxml-do-fontify (&optional bound
)
903 "Fontify at least as far as bound.
904 Leave point after last fontified position."
905 (unless bound
(setq bound
(point-max)))
906 (let (xmltok-dependent-regions
908 (while (and (< (point) bound
)
909 (nxml-tokenize-forward))
910 (nxml-clear-face xmltok-start
(point))
911 (nxml-apply-fontify-rule))))
913 ;; Vectors identify a substring of the token to be highlighted in some face.
915 ;; Token types returned by xmltok-forward.
919 '([nil
1 nxml-tag-delimiter
]
920 [-
1 nil nxml-tag-delimiter
]
924 (put 'partial-start-tag
926 '([nil
1 nxml-tag-delimiter
]
932 '([nil
1 nxml-tag-delimiter
]
934 [-
1 nil nxml-tag-delimiter
]
935 (element-qname .
2)))
937 (put 'partial-end-tag
939 '([nil
1 nxml-tag-delimiter
]
941 (element-qname .
2)))
945 '([nil
1 nxml-tag-delimiter
]
946 [-
2 -
1 nxml-tag-slash
]
947 [-
1 nil nxml-tag-delimiter
]
951 (put 'partial-empty-element
953 '([nil
1 nxml-tag-delimiter
]
954 [-
1 nil nxml-tag-slash
]
960 '([nil
2 nxml-char-ref-delimiter
]
961 [2 -
1 nxml-char-ref-number
]
962 [-
1 nil nxml-char-ref-delimiter
]
967 '([nil
1 nxml-entity-ref-delimiter
]
968 [1 -
1 nxml-entity-ref-name
]
969 [-
1 nil nxml-entity-ref-delimiter
]))
973 '([nil
4 nxml-comment-delimiter
]
974 [4 -
3 nxml-comment-content
]
975 [-
3 nil nxml-comment-delimiter
]))
977 (put 'processing-instruction
979 '([nil
2 nxml-processing-instruction-delimiter
]
980 [-
2 nil nxml-processing-instruction-delimiter
]
981 processing-instruction-content
))
985 '([nil
3 nxml-cdata-section-delimiter
] ; <![
986 [3 8 nxml-cdata-section-CDATA
] ; CDATA
987 [8 9 nxml-cdata-section-delimiter
] ; [
988 [9 -
3 nxml-cdata-section-content
] ; ]]>
989 [-
3 nil nxml-cdata-section-delimiter
]))
993 '([nil nil nxml-text
]))
995 ;; Prolog region types in list returned by xmltok-forward-prolog.
997 (put 'xml-declaration
999 '([nil
2 nxml-processing-instruction-delimiter
]
1000 [2 5 nxml-processing-instruction-target
]
1001 [-
2 nil nxml-processing-instruction-delimiter
]))
1003 (put 'xml-declaration-attribute-name
1005 '([nil nil nxml-attribute-local-name
]))
1007 (put 'xml-declaration-attribute-value
1009 '([nil
1 nxml-attribute-value-delimiter
]
1010 [1 -
1 nxml-attribute-value
]
1011 [-
1 nil nxml-attribute-value-delimiter
]))
1013 (put 'processing-instruction-left
1015 '([nil
2 nxml-processing-instruction-delimiter
]
1016 [2 nil nxml-processing-instruction-target
]))
1018 (put 'processing-instruction-right
1020 '([nil -
2 nxml-processing-instruction-content
]
1021 [-
2 nil nxml-processing-instruction-delimiter
]))
1025 '([nil
1 nxml-prolog-literal-delimiter
]
1026 [1 -
1 nxml-prolog-literal-content
]
1027 [-
1 nil nxml-prolog-literal-delimiter
]))
1031 '([nil nil nxml-prolog-keyword
]))
1033 (put 'markup-declaration-open
1035 '([0 2 nxml-markup-declaration-delimiter
]
1036 [2 nil nxml-prolog-keyword
]))
1038 (put 'markup-declaration-close
1040 '([nil nil nxml-markup-declaration-delimiter
]))
1042 (put 'internal-subset-open
1044 '([nil nil nxml-markup-declaration-delimiter
]))
1046 (put 'internal-subset-close
1048 '([nil
1 nxml-markup-declaration-delimiter
]
1049 [-
1 nil nxml-markup-declaration-delimiter
]))
1054 [1 nil nxml-prolog-keyword
]))
1056 (defun nxml-apply-fontify-rule (&optional type start end
)
1057 (let ((rule (get (or type xmltok-type
) 'nxml-fontify-rule
)))
1058 (unless start
(setq start xmltok-start
))
1059 (unless end
(setq end
(point)))
1061 (let* ((action (car rule
)))
1062 (setq rule
(cdr rule
))
1063 (cond ((vectorp action
)
1064 (nxml-set-face (let ((offset (aref action
0)))
1065 (cond ((not offset
) start
)
1066 ((< offset
0) (+ end offset
))
1067 (t (+ start offset
))))
1068 (let ((offset (aref action
1)))
1069 (cond ((not offset
) end
)
1070 ((< offset
0) (+ end offset
))
1071 (t (+ start offset
))))
1073 ((and (consp action
)
1074 (eq (car action
) 'element-qname
))
1075 (when xmltok-name-end
; maybe nil in partial-end-tag case
1076 (nxml-fontify-qname (+ start
(cdr action
))
1079 'nxml-element-prefix
1081 'nxml-element-local-name
)))
1082 ((eq action
'attributes
)
1083 (nxml-fontify-attributes))
1084 ((eq action
'processing-instruction-content
)
1085 (nxml-set-face (+ start
2)
1087 'nxml-processing-instruction-target
)
1088 (nxml-set-face (save-excursion
1089 (goto-char xmltok-name-end
)
1090 (skip-chars-forward " \t\r\n")
1093 'nxml-processing-instruction-content
))
1094 ((eq action
'char-ref
)
1095 (nxml-char-ref-display-extra start
1097 (xmltok-char-number start end
)))
1098 (t (error "Invalid nxml-fontify-rule action %s" action
)))))))
1100 (defun nxml-fontify-attributes ()
1101 (while xmltok-namespace-attributes
1102 (nxml-fontify-attribute (car xmltok-namespace-attributes
)
1104 (setq xmltok-namespace-attributes
1105 (cdr xmltok-namespace-attributes
)))
1106 (while xmltok-attributes
1107 (nxml-fontify-attribute (car xmltok-attributes
))
1108 (setq xmltok-attributes
1109 (cdr xmltok-attributes
))))
1111 (defun nxml-fontify-attribute (att &optional namespace-declaration
)
1112 (if namespace-declaration
1113 (nxml-fontify-qname (xmltok-attribute-name-start att
)
1114 (xmltok-attribute-name-colon att
)
1115 (xmltok-attribute-name-end att
)
1116 'nxml-namespace-attribute-xmlns
1117 'nxml-namespace-attribute-colon
1118 'nxml-namespace-attribute-prefix
1119 'nxml-namespace-attribute-xmlns
)
1120 (nxml-fontify-qname (xmltok-attribute-name-start att
)
1121 (xmltok-attribute-name-colon att
)
1122 (xmltok-attribute-name-end att
)
1123 'nxml-attribute-prefix
1124 'nxml-attribute-colon
1125 'nxml-attribute-local-name
))
1126 (let ((start (xmltok-attribute-value-start att
))
1127 (end (xmltok-attribute-value-end att
))
1128 (refs (xmltok-attribute-refs att
))
1129 (delimiter-face (if namespace-declaration
1130 'nxml-namespace-attribute-value-delimiter
1131 'nxml-attribute-value-delimiter
))
1132 (value-face (if namespace-declaration
1133 'nxml-namespace-attribute-value
1134 'nxml-attribute-value
)))
1136 (nxml-set-face (1- start
) start delimiter-face
)
1137 (nxml-set-face end
(1+ end
) delimiter-face
)
1139 (let* ((ref (car refs
))
1140 (ref-type (aref ref
0))
1141 (ref-start (aref ref
1))
1142 (ref-end (aref ref
2)))
1143 (nxml-set-face start ref-start value-face
)
1144 (nxml-apply-fontify-rule ref-type ref-start ref-end
)
1145 (setq start ref-end
))
1146 (setq refs
(cdr refs
)))
1147 (nxml-set-face start end value-face
))))
1149 (defun nxml-fontify-qname (start
1157 (cond (colon (nxml-set-face start colon prefix-face
)
1158 (nxml-set-face colon
(1+ colon
) colon-face
)
1159 (nxml-set-face (1+ colon
) end local-name-face
))
1160 (t (nxml-set-face start end
(or unprefixed-face
1161 local-name-face
)))))
1165 (defun nxml-electric-slash (arg)
1168 With a prefix ARG, do nothing other than insert the slash.
1170 Otherwise, if `nxml-slash-auto-complete-flag' is non-nil, insert the
1171 rest of the end-tag or empty-element if the slash is potentially part
1172 of an end-tag or the close of an empty-element.
1174 If the slash is part of an end-tag that is the first non-whitespace
1175 on the line, reindent the line."
1177 (nxml-ensure-scan-up-to-date)
1178 (let* ((slash-pos (point))
1179 (end-tag-p (and (eq (char-before slash-pos
) ?
<)
1180 (not (nxml-get-inside slash-pos
))))
1181 (at-indentation (save-excursion
1182 (back-to-indentation)
1183 (eq (point) (1- slash-pos
)))))
1184 (self-insert-command (prefix-numeric-value arg
))
1186 (if nxml-slash-auto-complete-flag
1189 (let ((start-tag-end
1190 (nxml-scan-element-backward (1- slash-pos
) t
)))
1192 (insert (xmltok-start-tag-qname) ">")
1193 ;; copy the indentation of the start-tag
1194 (when (and at-indentation
1196 (goto-char xmltok-start
)
1197 (back-to-indentation)
1198 (eq (point) xmltok-start
)))
1200 (indent-line-to (save-excursion
1201 (goto-char xmltok-start
)
1202 (current-column)))))))
1203 (nxml-scan-error nil
))
1204 (when (and (eq (nxml-token-before) (point))
1205 (eq xmltok-type
'partial-empty-element
))
1207 (when (and end-tag-p at-indentation
)
1208 (nxml-indent-line))))))
1210 (defun nxml-balanced-close-start-tag-block ()
1211 "Close the start-tag before point with `>' and insert a balancing end-tag.
1212 Point is left between the start-tag and the end-tag.
1213 If there is nothing but whitespace before the `<' that opens the
1214 start-tag, then put point on a blank line, and put the end-tag on
1215 another line aligned with the start-tag."
1217 (nxml-balanced-close-start-tag 'block
))
1219 (defun nxml-balanced-close-start-tag-inline ()
1220 "Close the start-tag before point with `>' and insert a balancing end-tag.
1221 Point is left between the start-tag and the end-tag.
1222 No extra whitespace is inserted."
1224 (nxml-balanced-close-start-tag 'inline
))
1226 (defun nxml-balanced-close-start-tag (block-or-inline)
1227 (let ((token-end (nxml-token-before))
1229 (unless (or (eq xmltok-type
'partial-start-tag
)
1230 (and (memq xmltok-type
'(start-tag
1232 partial-empty-element
))
1233 (>= token-end pos
)))
1234 (error "Not in a start-tag"))
1236 (buffer-substring-no-properties (+ xmltok-start
1)
1237 (min xmltok-name-end
(point)))
1239 (if (eq block-or-inline
'inline
)
1241 (goto-char xmltok-start
)
1242 (back-to-indentation)
1243 (if (= (point) xmltok-start
)
1244 (let ((indent (current-column)))
1247 (indent-line-to indent
)
1250 (indent-line-to (+ nxml-child-indent indent
)))
1253 (defun nxml-finish-element ()
1254 "Finish the current element by inserting an end-tag."
1256 (nxml-finish-element-1 nil
))
1258 (defvar nxml-last-split-position nil
1259 "Position where `nxml-split-element' split the current element.")
1261 (defun nxml-split-element ()
1262 "Split the current element by inserting an end-tag and a start-tag.
1263 Point is left after the newly inserted start-tag. When repeated,
1264 split immediately before the previously inserted start-tag and leave
1267 (setq nxml-last-split-position
1268 (if (and (eq last-command this-command
)
1269 nxml-last-split-position
)
1271 (goto-char nxml-last-split-position
)
1272 (nxml-finish-element-1 t
))
1273 (nxml-finish-element-1 t
))))
1275 (defun nxml-finish-element-1 (startp)
1276 "Insert an end-tag for the current element and optionally a start-tag.
1277 The start-tag is inserted if STARTP is non-nil. Return the position
1278 of the inserted start-tag or nil if none was inserted."
1280 (let* ((token-end (nxml-token-before))
1283 (when (and (< (point) token-end
)
1286 processing-instruction
1291 (error "Point is inside a %s"
1292 (nxml-token-type-friendly-name xmltok-type
)))
1293 (nxml-scan-element-backward token-end t
)))
1296 (unless (eq xmltok-type
'start-tag
)
1297 (error "No matching start-tag"))
1298 (goto-char xmltok-start
)
1299 (back-to-indentation)
1300 (eq (point) xmltok-start
)))
1303 (goto-char start-tag-end
)
1304 (looking-at "[ \t\r\n]*$")))
1305 (start-tag-indent (save-excursion
1306 (goto-char xmltok-start
)
1308 (qname (xmltok-start-tag-qname))
1309 inserted-start-tag-pos
)
1310 (when (and starts-line ends-line
)
1311 ;; start-tag is on a line by itself
1312 ;; => put the end-tag on a line by itself
1315 (back-to-indentation)
1318 (indent-line-to start-tag-indent
))
1319 (insert "</" qname
">")
1323 (indent-line-to start-tag-indent
))
1324 (setq inserted-start-tag-pos
(point))
1325 (insert "<" qname
">")
1326 (when (and starts-line ends-line
)
1328 (indent-line-to (save-excursion
1329 (goto-char xmltok-start
)
1331 (back-to-indentation)
1332 (if (= (current-column)
1333 (+ start-tag-indent nxml-child-indent
))
1334 (+ start-tag-indent nxml-child-indent
)
1335 start-tag-indent
)))))
1336 inserted-start-tag-pos
))
1340 (defun nxml-indent-line ()
1341 "Indent current line as XML."
1342 (let ((indent (nxml-compute-indent))
1343 (from-end (- (point-max) (point))))
1346 (let ((bol (point)))
1347 (skip-chars-forward " \t")
1348 (delete-region bol
(point)))
1350 (when (> (- (point-max) from-end
) (point))
1351 (goto-char (- (point-max) from-end
))))))
1353 (defun nxml-compute-indent ()
1354 "Return the indent for the line containing point."
1355 (or (nxml-compute-indent-from-matching-start-tag)
1356 (nxml-compute-indent-from-previous-line)))
1358 (defun nxml-compute-indent-from-matching-start-tag ()
1359 "Compute the indent for a line with an end-tag using the matching start-tag.
1360 When the line containing point ends with an end-tag and does not start
1361 in the middle of a token, return the indent of the line containing the
1362 matching start-tag, if there is one and it occurs at the beginning of
1363 its line. Otherwise return nil."
1365 (back-to-indentation)
1366 (let ((bol (point)))
1367 (let ((inhibit-field-text-motion t
))
1369 (skip-chars-backward " \t")
1370 (and (= (nxml-token-before) (point))
1371 (memq xmltok-type
'(end-tag partial-end-tag
))
1372 ;; start of line must not be inside a token
1373 (or (= xmltok-start bol
)
1377 (= xmltok-start bol
))
1378 (eq xmltok-type
'data
))
1380 (nxml-scan-element-backward
1384 nxml-end-tag-indent-scan-distance
))
1385 (nxml-scan-error nil
))
1386 (< xmltok-start bol
)
1388 (goto-char xmltok-start
)
1389 (skip-chars-backward " \t")
1391 (current-indentation)))))
1393 (defun nxml-compute-indent-from-previous-line ()
1394 "Compute the indent for a line using the indentation of a previous line."
1399 before-context after-context
)
1400 (back-to-indentation)
1403 ;; Move backwards until the start of a non-blank line that is
1404 ;; not inside a token.
1406 (when (= (forward-line -
1) -
1)
1408 (back-to-indentation)
1409 (if (looking-at "[ \t]*$")
1412 (setq prev-bol
(point)))
1414 (not (or (= xmltok-start
(point))
1415 (eq xmltok-type
'data
))))))
1417 ;; Now scan over tokens until the end of the line to be indented.
1418 ;; Determine the context before and after the beginning of the
1420 (while (< (point) eol
)
1421 (nxml-tokenize-forward)
1422 (cond ((<= bol xmltok-start
)
1424 (nxml-merge-indent-context-type after-context
)))
1425 ((and (<= (point) bol
)
1426 (not (and (eq xmltok-type
'partial-start-tag
)
1428 (setq before-context
1429 (nxml-merge-indent-context-type before-context
)))
1430 ((eq xmltok-type
'data
)
1431 (setq before-context
1432 (nxml-merge-indent-context-type before-context
))
1434 (nxml-merge-indent-context-type after-context
)))
1435 ;; If in the middle of a token that looks inline,
1436 ;; then indent relative to the previous non-blank line
1437 ((eq (nxml-merge-indent-context-type before-context
)
1439 (goto-char prev-bol
)
1440 (throw 'indent
(current-column)))
1443 (nxml-compute-indent-in-token bol
))))
1444 (skip-chars-forward " \t\r\n"))
1447 (* nxml-child-indent
1448 (+ (if (eq before-context
'start-tag
) 1 0)
1449 (if (eq after-context
'end-tag
) -
1 0))))))))
1451 (defun nxml-merge-indent-context-type (context)
1452 "Merge the indent context type CONTEXT with the token in `xmltok-type'.
1453 Return the merged indent context type. An indent context type is
1454 either nil or one of the symbols start-tag, end-tag, markup, comment,
1456 (cond ((memq xmltok-type
'(start-tag partial-start-tag
))
1457 (if (memq context
'(nil start-tag comment
))
1460 ((memq xmltok-type
'(end-tag partial-end-tag
))
1461 (if (memq context
'(nil end-tag comment
))
1464 ((eq xmltok-type
'comment
)
1465 (cond ((memq context
'(start-tag end-tag comment
))
1472 (defun nxml-compute-indent-in-token (pos)
1473 "Return the indent for a line that starts inside a token.
1474 POS is the position of the first non-whitespace character of the line.
1475 This expects the xmltok-* variables to be set up as by `xmltok-forward'."
1476 (cond ((memq xmltok-type
'(start-tag
1479 partial-empty-element
))
1480 (nxml-compute-indent-in-start-tag pos
))
1481 ((eq xmltok-type
'comment
)
1482 (nxml-compute-indent-in-delimited-token pos
"<!--" "-->"))
1483 ((eq xmltok-type
'cdata-section
)
1484 (nxml-compute-indent-in-delimited-token pos
"<![CDATA[" "]]>"))
1485 ((eq xmltok-type
'processing-instruction
)
1486 (nxml-compute-indent-in-delimited-token pos
"<?" "?>"))
1489 (if (and (= (forward-line -
1) 0)
1490 (< xmltok-start
(point)))
1491 (back-to-indentation)
1492 (goto-char xmltok-start
))
1495 (defun nxml-compute-indent-in-start-tag (pos)
1496 "Return the indent for a line that starts inside a start-tag.
1497 Also for a line that starts inside an empty element.
1498 POS is the position of the first non-whitespace character of the line.
1499 This expects the xmltok-* variables to be set up as by `xmltok-forward'."
1500 (let ((value-boundary (nxml-attribute-value-boundary pos
))
1503 ;; inside an attribute value
1504 (let ((value-start (car value-boundary
))
1505 (value-end (cdr value-boundary
)))
1508 (if (< (point) value-start
)
1509 (goto-char value-start
)
1510 (back-to-indentation)))
1511 ;; outside an attribute value
1513 (while (and (= (forward-line -
1) 0)
1514 (nxml-attribute-value-boundary (point))))
1515 (cond ((<= (point) xmltok-start
)
1516 (goto-char xmltok-start
)
1517 (setq off nxml-attribute-indent
)
1518 (let ((atts (xmltok-merge-attributes)))
1520 (let* ((att (car atts
))
1521 (start (xmltok-attribute-name-start att
)))
1526 (back-to-indentation))))
1527 (+ (current-column) off
)))
1529 (defun nxml-attribute-value-boundary (pos)
1530 "Return a pair (START . END) if POS is inside an attribute value.
1531 Otherwise return nil. START and END are the positions of the start
1532 and end of the attribute value containing POS. This expects the
1533 xmltok-* variables to be set up as by `xmltok-forward'."
1534 (let ((atts (xmltok-merge-attributes))
1535 att value-start value-end value-boundary
)
1537 (setq att
(car atts
))
1538 (setq value-start
(xmltok-attribute-value-start att
))
1539 (setq value-end
(xmltok-attribute-value-end att
))
1540 (cond ((and value-start
(< pos value-start
))
1542 ((and value-start value-end
(<= pos value-end
))
1543 (setq value-boundary
(cons value-start value-end
))
1545 (t (setq atts
(cdr atts
)))))
1548 (defun nxml-compute-indent-in-delimited-token (pos open-delim close-delim
)
1549 "Return the indent for a line that starts inside a token with delimiters.
1550 OPEN-DELIM and CLOSE-DELIM are strings giving the opening and closing
1551 delimiters. POS is the position of the first non-whitespace character
1552 of the line. This expects the xmltok-* variables to be set up as by
1554 (cond ((let ((end (+ pos
(length close-delim
))))
1555 (and (<= end
(point-max))
1556 (string= (buffer-substring-no-properties pos end
)
1558 (goto-char xmltok-start
))
1562 (<= (point) xmltok-start
))
1563 (goto-char (+ xmltok-start
(length open-delim
)))
1564 (when (and (string= open-delim
"<!--")
1566 (goto-char (1+ (point)))))
1567 (t (back-to-indentation)))
1572 (defun nxml-complete ()
1573 "Perform completion on the symbol preceding point.
1575 Inserts as many characters as can be completed. However, if not even
1576 one character can be completed, then a buffer with the possibilities
1577 is popped up and the symbol is read from the minibuffer with
1578 completion. If the symbol is complete, then any characters that must
1579 follow the symbol are also inserted.
1581 The name space used for completion and what is treated as a symbol
1582 depends on the context. The contexts in which completion is performed
1583 depend on `nxml-completion-hook'."
1585 (unless (run-hook-with-args-until-success 'nxml-completion-hook
)
1586 ;; Eventually we will complete on entity names here.
1588 (message "Cannot complete in this context")))
1592 (defun nxml-forward-balanced-item (&optional arg
)
1593 "Move forward across one balanced item.
1594 With ARG, do it that many times. Negative arg -N means
1595 move backward across N balanced expressions.
1596 This is the equivalent of `forward-sexp' for XML.
1598 An element contains as items strings with no markup, tags, processing
1599 instructions, comments, CDATA sections, entity references and
1600 characters references. However, if the variable
1601 `nxml-sexp-element-flag' is non-nil, then an element is treated as a
1602 single markup item. A start-tag contains an element name followed by
1603 one or more attributes. An end-tag contains just an element name. An
1604 attribute value literals contains strings with no markup, entity
1605 references and character references. A processing instruction
1606 consists of a target and a content string. A comment or a CDATA
1607 section contains a single string. An entity reference contains a
1608 single name. A character reference contains a character number."
1610 (or arg
(setq arg
1))
1613 (nxml-forward-single-balanced-item)
1614 (> (setq arg
(1- arg
)) 0))))
1617 (nxml-backward-single-balanced-item)
1618 (< (setq arg
(1+ arg
)) 0))))))
1620 (defun nxml-forward-single-balanced-item ()
1622 (goto-char (let ((end (nxml-token-after)))
1624 (while (eq xmltok-type
'space
)
1626 (setq end
(nxml-token-after)))
1627 (cond ((/= (point) xmltok-start
)
1628 (nxml-scan-forward-within end
))
1629 ((and nxml-sexp-element-flag
1630 (eq xmltok-type
'start-tag
))
1631 ;; can't ever return nil here
1632 (nxml-scan-element-forward xmltok-start
))
1633 ((and nxml-sexp-element-flag
1635 '(end-tag partial-end-tag
)))
1636 (error "Already at end of element"))
1639 (goto-char (cadr err
))
1640 (apply 'error
(cddr err
)))))
1642 (defun nxml-backward-single-balanced-item ()
1644 (goto-char (let ((end (nxml-token-before)))
1646 (while (eq xmltok-type
'space
)
1647 (goto-char xmltok-start
)
1648 (setq end
(nxml-token-before)))
1649 (cond ((/= (point) end
)
1650 (nxml-scan-backward-within end
))
1651 ((and nxml-sexp-element-flag
1652 (eq xmltok-type
'end-tag
))
1653 ;; can't ever return nil here
1654 (nxml-scan-element-backward end
)
1656 ((and nxml-sexp-element-flag
1657 (eq xmltok-type
'start-tag
))
1658 (error "Already at start of element"))
1659 (t xmltok-start
)))))
1661 (goto-char (cadr err
))
1662 (apply 'error
(cddr err
)))))
1664 (defun nxml-scan-forward-within (end)
1665 (setq end
(- end
(nxml-end-delimiter-length xmltok-type
)))
1666 (when (<= end
(point))
1667 (error "Already at end of %s"
1668 (nxml-token-type-friendly-name xmltok-type
)))
1669 (cond ((memq xmltok-type
'(start-tag
1672 partial-empty-element
))
1673 (if (< (point) xmltok-name-end
)
1675 (let ((att (nxml-find-following-attribute)))
1676 (cond ((not att
) end
)
1677 ((and (xmltok-attribute-value-start att
)
1678 (<= (xmltok-attribute-value-start att
)
1680 (nxml-scan-forward-in-attribute-value att
))
1681 ((xmltok-attribute-value-end att
)
1682 (1+ (xmltok-attribute-value-end att
)))
1684 (goto-char (xmltok-attribute-name-end att
))
1685 (looking-at "[ \t\r\n]*="))
1687 (t (xmltok-attribute-name-end att
))))))
1688 ((and (eq xmltok-type
'processing-instruction
)
1689 (< (point) xmltok-name-end
))
1693 (defun nxml-scan-backward-within (end)
1696 (nxml-start-delimiter-length xmltok-type
)))
1697 (when (<= (point) xmltok-start
)
1698 (error "Already at start of %s"
1699 (nxml-token-type-friendly-name xmltok-type
)))
1700 (cond ((memq xmltok-type
'(start-tag
1703 partial-empty-element
))
1704 (let ((att (nxml-find-preceding-attribute)))
1705 (cond ((not att
) xmltok-start
)
1706 ((and (xmltok-attribute-value-start att
)
1707 (<= (xmltok-attribute-value-start att
)
1710 (xmltok-attribute-value-end att
)))
1711 (nxml-scan-backward-in-attribute-value att
))
1712 (t (xmltok-attribute-name-start att
)))))
1713 ((and (eq xmltok-type
'processing-instruction
)
1714 (let ((content-start (save-excursion
1715 (goto-char xmltok-name-end
)
1716 (skip-chars-forward " \r\t\n")
1718 (and (< content-start
(point))
1722 (defun nxml-scan-forward-in-attribute-value (att)
1723 (when (= (point) (xmltok-attribute-value-end att
))
1724 (error "Already at end of attribute value"))
1725 (let ((refs (xmltok-attribute-refs att
))
1728 (setq ref
(car refs
))
1729 (if (< (point) (aref ref
2))
1732 (setq refs
(cdr refs
))))
1734 (xmltok-attribute-value-end att
))
1735 ((< (point) (aref ref
1))
1737 ((= (point) (aref ref
1))
1740 (let ((end (- (aref ref
2)
1741 (nxml-end-delimiter-length (aref ref
0)))))
1744 (error "Already at end of %s"
1745 (nxml-token-type-friendly-name (aref ref
0)))))))))
1747 (defun nxml-scan-backward-in-attribute-value (att)
1748 (when (= (point) (xmltok-attribute-value-start att
))
1749 (error "Already at start of attribute value"))
1750 (let ((refs (reverse (xmltok-attribute-refs att
)))
1753 (setq ref
(car refs
))
1754 (if (< (aref ref
1) (point))
1757 (setq refs
(cdr refs
))))
1759 (xmltok-attribute-value-start att
))
1760 ((< (aref ref
2) (point))
1762 ((= (point) (aref ref
2))
1765 (let ((start (+ (aref ref
1)
1766 (nxml-start-delimiter-length (aref ref
0)))))
1767 (if (< start
(point))
1769 (error "Already at start of %s"
1770 (nxml-token-type-friendly-name (aref ref
0)))))))))
1772 (defun nxml-find-following-attribute ()
1774 (atts (or xmltok-attributes xmltok-namespace-attributes
))
1775 (more-atts (and xmltok-attributes xmltok-namespace-attributes
)))
1777 (let* ((att (car atts
))
1778 (name-start (xmltok-attribute-name-start att
)))
1779 (cond ((and (<= name-start
(point))
1780 (xmltok-attribute-value-end att
)
1781 ;; <= because end is before quote
1782 (<= (point) (xmltok-attribute-value-end att
)))
1785 ((and (< (point) name-start
)
1788 (xmltok-attribute-name-start ret
))))
1790 (setq atts
(cdr atts
))
1792 (setq atts more-atts
)
1793 (setq more-atts nil
)))
1796 (defun nxml-find-preceding-attribute ()
1798 (atts (or xmltok-attributes xmltok-namespace-attributes
))
1799 (more-atts (and xmltok-attributes xmltok-namespace-attributes
)))
1801 (let* ((att (car atts
))
1802 (name-start (xmltok-attribute-name-start att
)))
1803 (cond ((and (< name-start
(point))
1804 (xmltok-attribute-value-end att
)
1805 ;; <= because end is before quote
1806 (<= (point) (xmltok-attribute-value-end att
)))
1809 ((and (< name-start
(point))
1811 (< (xmltok-attribute-name-start ret
)
1814 (setq atts
(cdr atts
))
1816 (setq atts more-atts
)
1817 (setq more-atts nil
)))
1820 (defun nxml-up-element (&optional arg
)
1822 (or arg
(setq arg
1))
1824 (nxml-backward-up-element (- arg
))
1826 (while (and (> arg
0)
1827 (< (point) (point-max)))
1828 (let ((token-end (nxml-token-after)))
1829 (goto-char (cond ((or (memq xmltok-type
'(end-tag
1831 (and (memq xmltok-type
1833 partial-empty-element
))
1834 (< xmltok-start
(point))))
1836 ((nxml-scan-element-forward
1837 (if (and (eq xmltok-type
'start-tag
)
1838 (= (point) xmltok-start
))
1842 (t (error "No parent element")))))
1843 (setq arg
(1- arg
)))
1845 (goto-char (cadr err
))
1846 (apply 'error
(cddr err
))))))
1848 (defun nxml-backward-up-element (&optional arg
)
1850 (or arg
(setq arg
1))
1852 (nxml-up-element (- arg
))
1854 (while (and (> arg
0)
1855 (< (point-min) (point)))
1856 (let ((token-end (nxml-token-before)))
1857 (goto-char (cond ((or (memq xmltok-type
'(start-tag
1859 (and (memq xmltok-type
1861 partial-empty-element
))
1862 (< (point) token-end
)))
1864 ((nxml-scan-element-backward
1865 (if (and (eq xmltok-type
'end-tag
)
1866 (= (point) token-end
))
1871 (t (error "No parent element")))))
1872 (setq arg
(1- arg
)))
1874 (goto-char (cadr err
))
1875 (apply 'error
(cddr err
))))))
1877 (defun nxml-down-element (&optional arg
)
1878 "Move forward down into the content of an element.
1879 With ARG, do this that many times.
1880 Negative ARG means move backward but still down."
1882 (or arg
(setq arg
1))
1884 (nxml-backward-down-element (- arg
))
1887 (let ((token-end (nxml-token-after)))
1889 (goto-char token-end
)
1891 (when (memq xmltok-type
'(nil end-tag partial-end-tag
))
1892 (error "No following start-tags in this element"))
1893 (not (memq xmltok-type
'(start-tag partial-start-tag
))))
1894 (nxml-tokenize-forward))
1896 (setq arg
(1- arg
)))))
1898 (defun nxml-backward-down-element (&optional arg
)
1900 (or arg
(setq arg
1))
1902 (nxml-down-element (- arg
))
1907 (goto-char xmltok-start
)
1909 (when (memq xmltok-type
'(start-tag
1913 (error "No preceding end-tags in this element"))
1914 (not (memq xmltok-type
'(end-tag partial-end-tag
))))
1915 (if (or (<= (point) nxml-prolog-end
)
1916 (not (search-backward "<" nxml-prolog-end t
)))
1917 (setq xmltok-type nil
)
1918 (nxml-move-outside-backwards)
1921 (setq arg
(1- arg
)))))
1923 (defun nxml-forward-element (&optional arg
)
1924 "Move forward over one element.
1925 With ARG, do it that many times.
1926 Negative ARG means move backward."
1928 (or arg
(setq arg
1))
1930 (nxml-backward-element (- arg
))
1932 (while (and (> arg
0)
1933 (< (point) (point-max)))
1935 (or (nxml-scan-element-forward (nxml-token-before))
1936 (error "No more elements")))
1937 (setq arg
(1- arg
)))
1939 (goto-char (cadr err
))
1940 (apply 'error
(cddr err
))))))
1942 (defun nxml-backward-element (&optional arg
)
1943 "Move backward over one element.
1944 With ARG, do it that many times.
1945 Negative ARG means move forward."
1947 (or arg
(setq arg
1))
1949 (nxml-forward-element (- arg
))
1951 (while (and (> arg
0)
1952 (< (point-min) (point)))
1954 (or (and (nxml-scan-element-backward (progn
1958 (error "No preceding elements")))
1959 (setq arg
(1- arg
)))
1961 (goto-char (cadr err
))
1962 (apply 'error
(cddr err
))))))
1964 (defun nxml-mark-token-after ()
1966 (push-mark (nxml-token-after) nil t
)
1967 (goto-char xmltok-start
)
1968 (message "Marked %s" xmltok-type
))
1972 (defun nxml-mark-paragraph ()
1973 "Put point at beginning of this paragraph, mark at end.
1974 The paragraph marked is the one that contains point or follows point."
1976 (nxml-forward-paragraph)
1978 (nxml-backward-paragraph))
1980 (defun nxml-forward-paragraph (&optional arg
)
1982 (or arg
(setq arg
1))
1984 (nxml-backward-paragraph (- arg
)))
1987 (while (and (nxml-forward-single-paragraph)
1988 (> (setq arg
(1- arg
)) 0))))))
1990 (defun nxml-backward-paragraph (&optional arg
)
1992 (or arg
(setq arg
1))
1994 (nxml-forward-paragraph (- arg
)))
1997 (let ((inhibit-field-text-motion t
))
1999 (while (and (nxml-backward-single-paragraph)
2000 (> (setq arg
(1- arg
)) 0))))))
2002 (defun nxml-forward-single-paragraph ()
2003 "Move forward over a single paragraph.
2004 Return nil at end of buffer, t otherwise."
2005 (let* ((token-end (nxml-token-after))
2006 (offset (- (point) xmltok-start
))
2008 (goto-char token-end
)
2009 (while (and (< (point) (point-max))
2011 (nxml-paragraph-end-pos had-data offset
))))
2012 (when (nxml-token-contains-data-p offset
)
2014 (nxml-tokenize-forward)
2016 (when pos
(goto-char pos
))))
2018 (defun nxml-backward-single-paragraph ()
2019 "Move backward over a single paragraph.
2020 Return nil at start of buffer, t otherwise."
2021 (let* ((token-end (nxml-token-before))
2022 (offset (- token-end
(point)))
2023 (last-tag-pos xmltok-start
)
2024 pos had-data last-data-pos
)
2025 (goto-char token-end
)
2026 (unless (setq pos
(nxml-paragraph-start-pos nil offset
))
2027 (setq had-data
(nxml-token-contains-data-p nil offset
))
2028 (goto-char xmltok-start
)
2029 (while (and (not pos
) (< (point-min) (point)))
2030 (cond ((search-backward "<" nxml-prolog-end t
)
2031 (nxml-move-outside-backwards)
2033 (while (< (point) last-tag-pos
)
2035 (when (and (not had-data
) (nxml-token-contains-data-p))
2037 (setq last-data-pos xmltok-start
))
2038 (let ((tem (nxml-paragraph-start-pos had-data
0)))
2039 (when tem
(setq pos tem
)))))
2040 (when (and (not had-data
) last-data-pos
(not pos
))
2043 (while (< (point) last-data-pos
)
2045 (let ((tem (nxml-paragraph-start-pos had-data
0)))
2046 (when tem
(setq pos tem
)))))
2047 (setq last-tag-pos
(point)))
2048 (t (goto-char (point-min))))))
2049 (when pos
(goto-char pos
))))
2051 (defun nxml-token-contains-data-p (&optional start end
)
2052 (setq start
(+ xmltok-start
(or start
0)))
2053 (setq end
(- (point) (or end
0)))
2054 (when (eq xmltok-type
'cdata-section
)
2055 (setq start
(max start
(+ xmltok-start
9)))
2056 (setq end
(min end
(- (point) 3))))
2057 (or (and (eq xmltok-type
'data
)
2058 (eq start xmltok-start
)
2060 (eq xmltok-type
'char-ref
)
2061 (and (memq xmltok-type
'(data cdata-section
))
2065 (re-search-forward "[^ \t\r\n]" end t
)))))
2067 (defun nxml-paragraph-end-pos (had-data offset
)
2068 "Return the position of the paragraph end if contained in the current token.
2069 Return nil if the current token does not contain the paragraph end.
2070 Only characters after OFFSET from the start of the token are eligible.
2071 HAD-DATA says whether there have been non-whitespace data characters yet."
2072 (cond ((not had-data
)
2073 (cond ((memq xmltok-type
'(data cdata-section
))
2075 (let ((end (point)))
2076 (goto-char (+ xmltok-start
2077 (max (if (eq xmltok-type
'cdata-section
)
2081 (and (re-search-forward "[^ \t\r\n]" end t
)
2082 (re-search-forward "^[ \t]*$" end t
)
2083 (match-beginning 0)))))
2084 ((and (eq xmltok-type
'comment
)
2085 (nxml-token-begins-line-p)
2086 (nxml-token-ends-line-p))
2088 (let ((end (point)))
2089 (goto-char (+ xmltok-start
(max 4 offset
)))
2090 (when (re-search-forward "[^ \t\r\n]" (- end
3) t
)
2091 (if (re-search-forward "^[ \t]*$" end t
)
2093 (goto-char (- end
3))
2094 (skip-chars-backward " \t")
2096 (beginning-of-line 2))
2098 ((memq xmltok-type
'(data space cdata-section
))
2100 (let ((end (point)))
2101 (goto-char (+ xmltok-start offset
))
2102 (and (re-search-forward "^[ \t]*$" end t
)
2103 (match-beginning 0)))))
2104 ((and (memq xmltok-type
'(start-tag
2108 processing-instruction
2110 (nxml-token-begins-line-p)
2111 (nxml-token-ends-line-p))
2113 (goto-char xmltok-start
)
2114 (skip-chars-backward " \t")
2116 ((and (eq xmltok-type
'end-tag
)
2117 (looking-at "[ \t]*$")
2118 (not (nxml-in-mixed-content-p t
)))
2120 (or (search-forward "\n" nil t
)
2123 (defun nxml-paragraph-start-pos (had-data offset
)
2124 "Return the position of the paragraph start if contained in the current token.
2125 Return nil if the current token does not contain the paragraph start.
2126 Only characters before OFFSET from the end of the token are eligible.
2127 HAD-DATA says whether there have been non-whitespace data characters yet."
2128 (cond ((not had-data
)
2129 (cond ((memq xmltok-type
'(data cdata-section
))
2131 (goto-char (- (point)
2132 (max (if (eq xmltok-type
'cdata-section
)
2136 (and (re-search-backward "[^ \t\r\n]" xmltok-start t
)
2137 (re-search-backward "^[ \t]*$" xmltok-start t
)
2138 (match-beginning 0))))
2139 ((and (eq xmltok-type
'comment
)
2140 (nxml-token-ends-line-p)
2141 (nxml-token-begins-line-p))
2143 (goto-char (- (point) (max 3 offset
)))
2144 (when (and (< (+ xmltok-start
4) (point))
2145 (re-search-backward "[^ \t\r\n]"
2148 (if (re-search-backward "^[ \t]*$" xmltok-start t
)
2150 (goto-char xmltok-start
)
2151 (if (looking-at "<!--[ \t]*\n")
2153 (skip-chars-backward " \t")
2155 ((memq xmltok-type
'(data space cdata-section
))
2157 (goto-char (- (point) offset
))
2158 (and (re-search-backward "^[ \t]*$" xmltok-start t
)
2159 (match-beginning 0))))
2160 ((and (memq xmltok-type
'(start-tag
2164 processing-instruction
2166 (nxml-token-ends-line-p)
2167 (nxml-token-begins-line-p))
2168 (or (search-forward "\n" nil t
)
2170 ((and (eq xmltok-type
'start-tag
)
2171 (nxml-token-begins-line-p)
2172 (not (save-excursion
2173 (goto-char xmltok-start
)
2174 (nxml-in-mixed-content-p nil
))))
2176 (goto-char xmltok-start
)
2177 (skip-chars-backward " \t")
2178 ;; include any blank line before
2179 (or (and (eq (char-before) ?
\n)
2181 (goto-char (1- (point)))
2182 (skip-chars-backward " \t")
2183 (and (bolp) (point))))
2186 (defun nxml-token-ends-line-p () (looking-at "[ \t]*$"))
2188 (defun nxml-token-begins-line-p ()
2190 (goto-char xmltok-start
)
2191 (skip-chars-backward " \t")
2194 (defun nxml-in-mixed-content-p (endp)
2195 "Return non-nil if point is in mixed content.
2196 Point must be after an end-tag or before a start-tag.
2197 ENDP is t in the former case, nil in the latter."
2198 (let (matching-tag-pos)
2199 (cond ((not (run-hook-with-args-until-failure
2200 'nxml-in-mixed-content-hook
))
2202 ;; See if the matching tag does not start or end a line.
2203 ((condition-case err
2205 (setq matching-tag-pos
2208 (and (nxml-scan-element-backward (point))
2210 (nxml-scan-element-forward (point)))))
2211 (and matching-tag-pos
2213 (goto-char matching-tag-pos
)
2216 (skip-chars-backward " \t")
2218 (looking-at "[ \t]*$"))))))
2219 (nxml-scan-error nil
))
2221 ;; See if there's data at the same level.
2224 (setq start matching-tag-pos
2227 end matching-tag-pos
))
2231 (nxml-preceding-sibling-data-p))
2234 (nxml-following-sibling-data-p)))))
2236 ;; Otherwise, treat as not mixed
2239 (defun nxml-preceding-sibling-data-p ()
2240 "Return non-nil if there is a previous sibling that is data."
2241 (let ((lim (max (- (point) nxml-mixed-scan-distance
)
2247 (while (and (< lim
(point))
2252 (search-backward "<" lim t
)))
2253 (nxml-move-outside-backwards)
2256 (let ((prev-level level
))
2257 (cond ((eq xmltok-type
'end-tag
)
2258 (setq level
(1+ level
)))
2259 ((eq xmltok-type
'start-tag
)
2260 (setq level
(1- level
))))
2261 (when (eq prev-level
0)
2262 (while (and (< (point) end
) (not found
))
2264 (when (memq xmltok-type
'(data cdata-section char-ref
))
2265 (setq found t
)))))))))
2268 (defun nxml-following-sibling-data-p ()
2269 (let ((lim (min (+ (point) nxml-mixed-scan-distance
)
2275 (while (and (< (point) lim
)
2277 (nxml-tokenize-forward)
2279 (cond ((eq xmltok-type
'start-tag
)
2280 (setq level
(1+ level
)))
2281 ((eq xmltok-type
'end-tag
)
2282 (setq level
(1- level
)))
2284 (memq xmltok-type
'(data cdata-section char-ref
)))
2290 (defun nxml-do-fill-paragraph (arg)
2291 (let (fill-paragraph-function
2295 (nxml-forward-paragraph)
2297 (nxml-backward-paragraph)
2298 (skip-chars-forward " \t\r\n")
2299 (setq start
(point))
2301 (setq fill-prefix
(buffer-substring-no-properties (point) start
))
2302 (when (and (not (nxml-get-inside (point)))
2303 (looking-at "[ \t]*<!--"))
2304 (setq fill-prefix
(concat fill-prefix
" ")))
2305 (fill-region-as-paragraph start end arg
))
2306 (skip-line-prefix fill-prefix
)
2309 (defun nxml-newline-and-indent (soft)
2310 (delete-horizontal-space)
2311 (if soft
(insert-and-inherit ?
\n) (newline 1))
2317 (defvar nxml-dynamic-markup-prev-pos nil
)
2318 (defvar nxml-dynamic-markup-prev-lengths nil
)
2319 (defvar nxml-dynamic-markup-prev-found-marker nil
)
2320 (defvar nxml-dynamic-markup-prev-start-tags
(make-hash-table :test
'equal
))
2322 (defun nxml-dynamic-markup-word ()
2323 "Dynamically markup the word before point.
2324 This attempts to find a tag to put around the word before point based
2325 on the contents of the current buffer. The end-tag will be inserted at
2326 point. The start-tag will be inserted at or before the beginning of
2327 the word before point; the contents of the current buffer is used to
2330 It works in a similar way to \\[dabbrev-expand]. It searches first
2331 backwards from point, then forwards from point for an element whose
2332 content is a string which matches the contents of the buffer before
2333 point and which includes at least the word before point. It then
2334 copies the start- and end-tags from that element and uses them to
2335 surround the matching string before point.
2337 Repeating \\[nxml-dynamic-markup-word] immediately after successful
2338 \\[nxml-dynamic-markup-word] removes the previously inserted markup
2339 and attempts to find another possible way to do the markup."
2341 (let (search-start-pos done
)
2342 (if (and (integerp nxml-dynamic-markup-prev-pos
)
2343 (= nxml-dynamic-markup-prev-pos
(point))
2344 (eq last-command this-command
)
2345 nxml-dynamic-markup-prev-lengths
)
2346 (let* ((end-tag-open-pos
2347 (- nxml-dynamic-markup-prev-pos
2348 (nth 2 nxml-dynamic-markup-prev-lengths
)))
2349 (start-tag-close-pos
2351 (nth 1 nxml-dynamic-markup-prev-lengths
)))
2353 (- start-tag-close-pos
2354 (nth 0 nxml-dynamic-markup-prev-lengths
))))
2355 (delete-region end-tag-open-pos nxml-dynamic-markup-prev-pos
)
2356 (delete-region start-tag-open-pos start-tag-close-pos
)
2357 (setq search-start-pos
2358 (marker-position nxml-dynamic-markup-prev-found-marker
)))
2359 (clrhash nxml-dynamic-markup-prev-start-tags
))
2360 (setq nxml-dynamic-markup-prev-pos nil
)
2361 (setq nxml-dynamic-markup-prev-lengths nil
)
2362 (setq nxml-dynamic-markup-prev-found-marker nil
)
2365 (let* ((pos (point))
2368 (unless (< (point) pos
)
2369 (error "No word to markup"))
2370 (buffer-substring-no-properties (point) pos
)))
2371 (search (concat word
"</"))
2373 (when search-start-pos
2374 (goto-char search-start-pos
))
2375 (while (and (not done
)
2376 (or (and (< (point) pos
)
2377 (or (search-backward search nil t
)
2378 (progn (goto-char pos
) nil
)))
2379 (search-forward search nil t
)))
2380 (goto-char (- (match-end 0) 2))
2381 (setq done
(nxml-try-copy-markup pos
)))
2383 (error (if (zerop (hash-table-count
2384 nxml-dynamic-markup-prev-start-tags
))
2385 "No possible markup found for `%s'"
2386 "No more markup possibilities found for `%s'")
2389 (defun nxml-try-copy-markup (word-end-pos)
2391 (let ((end-tag-pos (point)))
2392 (when (and (not (nxml-get-inside end-tag-pos
))
2393 (search-backward "<" nil t
)
2394 (not (nxml-get-inside (point))))
2396 (when (and (eq xmltok-type
'start-tag
)
2397 (< (point) end-tag-pos
))
2398 (let* ((start-tag-close-pos (point))
2400 (buffer-substring-no-properties xmltok-start
2401 start-tag-close-pos
))
2405 (buffer-substring-no-properties start-tag-close-pos
2408 (goto-char word-end-pos
)
2410 (re-search-backward (concat
2411 (regexp-quote (car words
))
2415 (setq words
(cdr words
))
2416 (skip-chars-backward " \t\r\n"))
2417 (when (and (not words
)
2419 (skip-chars-forward " \t\r\n")
2420 (not (gethash (cons (point) start-tag
)
2421 nxml-dynamic-markup-prev-start-tags
)))
2422 (or (< end-tag-pos
(point))
2423 (< word-end-pos xmltok-start
)))
2424 (setq nxml-dynamic-markup-prev-found-marker
2425 (copy-marker end-tag-pos t
))
2426 (puthash (cons (point) start-tag
)
2428 nxml-dynamic-markup-prev-start-tags
)
2429 (setq nxml-dynamic-markup-prev-lengths
2430 (list (- start-tag-close-pos xmltok-start
)
2431 (- word-end-pos
(point))
2432 (+ (- xmltok-name-end xmltok-start
) 2)))
2433 (let ((name (xmltok-start-tag-qname)))
2435 (goto-char (+ word-end-pos
2436 (- start-tag-close-pos xmltok-start
)))
2437 (insert "</" name
">")
2438 (setq nxml-dynamic-markup-prev-pos
(point))))))))))
2443 (defvar nxml-char-name-ignore-case t
)
2445 (defvar nxml-char-name-alist nil
2446 "Alist of character names.
2447 Each member of the list has the form (NAME CODE . NAMESET),
2448 where NAME is a string naming a character, NAMESET is a symbol
2449 identifying a set of names and CODE is an integer specifying the
2450 Unicode scalar value of the named character.
2451 The NAME will only be used for completion if NAMESET has
2452 a non-nil `nxml-char-name-set-enabled' property.
2453 If NAMESET does does not have `nxml-char-name-set-defined' property,
2454 then it must have a `nxml-char-name-set-file' property and `load'
2455 will be applied to the value of this property if the nameset
2458 (defvar nxml-char-name-table
(make-hash-table :test
'eq
)
2459 "Hash table for mapping char codes to names.
2460 Each key is a Unicode scalar value.
2461 Each value is a list of pairs of the form (NAMESET . NAME),
2462 where NAMESET is a symbol identifying a set of names,
2463 and NAME is a string naming a character.")
2465 (defvar nxml-autoload-char-name-set-list nil
2466 "List of char namesets that can be autoloaded.")
2468 (defun nxml-enable-char-name-set (nameset)
2469 (put nameset
'nxml-char-name-set-enabled t
))
2471 (defun nxml-disable-char-name-set (nameset)
2472 (put nameset
'nxml-char-name-set-enabled nil
))
2474 (defun nxml-char-name-set-enabled-p (nameset)
2475 (get nameset
'nxml-char-name-set-enabled
))
2477 (defun nxml-autoload-char-name-set (nameset file
)
2478 (unless (memq nameset nxml-autoload-char-name-set-list
)
2479 (setq nxml-autoload-char-name-set-list
2480 (cons nameset nxml-autoload-char-name-set-list
)))
2481 (put nameset
'nxml-char-name-set-file file
))
2483 (defun nxml-define-char-name-set (nameset alist
)
2484 "Define a set of character names.
2485 NAMESET is a symbol identifying the set.
2486 Alist is a list where each member has the form (NAME CODE),
2487 where NAME is a string naming a character and code
2488 is an integer giving the Unicode scalar value of the character."
2489 (when (get nameset
'nxml-char-name-set-defined
)
2490 (error "Nameset `%s' already defined" nameset
))
2493 (let* ((name-code (car iter
))
2494 (name (car name-code
))
2495 (code (cadr name-code
)))
2497 (cons (cons nameset name
)
2498 (gethash code nxml-char-name-table
))
2499 nxml-char-name-table
))
2500 (setcdr (cdr (car iter
)) nameset
)
2501 (setq iter
(cdr iter
))))
2502 (setq nxml-char-name-alist
2503 (nconc alist nxml-char-name-alist
))
2504 (put nameset
'nxml-char-name-set-defined t
))
2506 (defun nxml-get-char-name (code)
2507 (mapc 'nxml-maybe-load-char-name-set nxml-autoload-char-name-set-list
)
2508 (let ((names (gethash code nxml-char-name-table
))
2510 (while (and names
(not name
))
2511 (if (nxml-char-name-set-enabled-p (caar names
))
2512 (setq name
(cdar names
))
2513 (setq names
(cdr names
))))
2516 (defvar nxml-named-char-history nil
)
2518 (defun nxml-insert-named-char (arg)
2519 "Insert a character using its name.
2520 The name is read from the minibuffer.
2521 Normally, inserts the character as a numeric character reference.
2522 With a prefix argument, inserts the character directly."
2524 (mapc 'nxml-maybe-load-char-name-set nxml-autoload-char-name-set-list
)
2526 (let ((completion-ignore-case nxml-char-name-ignore-case
))
2527 (completing-read "Character name: "
2528 nxml-char-name-alist
2530 (get (cddr member
) 'nxml-char-name-set-enabled
))
2533 'nxml-named-char-history
)))
2534 (alist nxml-char-name-alist
)
2536 (while (and alist
(not code
))
2537 (setq elt
(assoc name alist
))
2538 (if (get (cddr elt
) 'nxml-char-name-set-enabled
)
2539 (setq code
(cadr elt
))
2540 (setq alist
(cdr (member elt alist
)))))
2543 (or (decode-char 'ucs code
)
2544 (error "Character %x is not supported by Emacs"
2546 (format "&#x%X;" code
))))))
2548 (defun nxml-maybe-load-char-name-set (sym)
2549 (when (and (get sym
'nxml-char-name-set-enabled
)
2550 (not (get sym
'nxml-char-name-set-defined
))
2551 (stringp (get sym
'nxml-char-name-set-file
)))
2552 (load (get sym
'nxml-char-name-set-file
))))
2554 (defun nxml-toggle-char-ref-extra-display (arg)
2555 "*Toggle the display of extra information for character references."
2557 (let ((new (if (null arg
)
2558 (not nxml-char-ref-extra-display
)
2559 (> (prefix-numeric-value arg
) 0))))
2560 (when (not (eq new nxml-char-ref-extra-display
))
2561 (setq nxml-char-ref-extra-display new
)
2565 (if nxml-char-ref-extra-display
2566 (nxml-with-unmodifying-text-property-changes
2567 (nxml-clear-fontified (point-min) (point-max)))
2568 (nxml-clear-char-ref-extra-display (point-min) (point-max))))))))
2570 (put 'nxml-char-ref
'evaporate t
)
2572 (defun nxml-char-ref-display-extra (start end n
)
2573 (when nxml-char-ref-extra-display
2574 (let ((name (nxml-get-char-name n
))
2575 (glyph-string (and nxml-char-ref-display-glyph-flag
2576 (nxml-glyph-display-string n
'nxml-glyph
)))
2578 (when (or name glyph-string
)
2579 (setq ov
(make-overlay start end nil t
))
2580 (overlay-put ov
'category
'nxml-char-ref
)
2582 (overlay-put ov
'help-echo name
))
2586 (propertize glyph-string
'face
'nxml-glyph
)))))))
2588 (defun nxml-clear-char-ref-extra-display (start end
)
2589 (let ((ov (overlays-in start end
)))
2591 (when (eq (overlay-get (car ov
) 'category
) 'nxml-char-ref
)
2592 (delete-overlay (car ov
)))
2593 (setq ov
(cdr ov
)))))
2596 (defun nxml-start-delimiter-length (type)
2597 (or (get type
'nxml-start-delimiter-length
)
2600 (put 'cdata-section
'nxml-start-delimiter-length
9)
2601 (put 'comment
'nxml-start-delimiter-length
4)
2602 (put 'processing-instruction
'nxml-start-delimiter-length
2)
2603 (put 'start-tag
'nxml-start-delimiter-length
1)
2604 (put 'empty-element
'nxml-start-delimiter-length
1)
2605 (put 'partial-empty-element
'nxml-start-delimiter-length
1)
2606 (put 'entity-ref
'nxml-start-delimiter-length
1)
2607 (put 'char-ref
'nxml-start-delimiter-length
2)
2609 (defun nxml-end-delimiter-length (type)
2610 (or (get type
'nxml-end-delimiter-length
)
2613 (put 'cdata-section
'nxml-end-delimiter-length
3)
2614 (put 'comment
'nxml-end-delimiter-length
3)
2615 (put 'processing-instruction
'nxml-end-delimiter-length
2)
2616 (put 'start-tag
'nxml-end-delimiter-length
1)
2617 (put 'empty-element
'nxml-end-delimiter-length
2)
2618 (put 'partial-empty-element
'nxml-end-delimiter-length
1)
2619 (put 'entity-ref
'nxml-end-delimiter-length
1)
2620 (put 'char-ref
'nxml-end-delimiter-length
1)
2622 (defun nxml-token-type-friendly-name (type)
2623 (or (get type
'nxml-friendly-name
)
2624 (symbol-name type
)))
2626 (put 'cdata-section
'nxml-friendly-name
"CDATA section")
2627 (put 'processing-instruction
'nxml-friendly-name
"processing instruction")
2628 (put 'entity-ref
'nxml-friendly-name
"entity reference")
2629 (put 'char-ref
'nxml-friendly-name
"character reference")
2631 (provide 'nxml-mode
)
2633 ;; arch-tag: 8603bc5f-1ef9-4021-b223-322fb2ca708e
2634 ;;; nxml-mode.el ends here