1 ;;; nxml-mode.el --- a new XML mode
3 ;; Copyright (C) 2003-2004, 2007-2011 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; See nxml-rap.el for description of parsing strategy.
29 (when (featurep 'mucs
)
30 (error "nxml-mode is not compatible with Mule-UCS"))
32 (eval-when-compile (require 'cl
)) ; for assert
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 hook
58 `nxml-glyph-set-hook' can be used to customize for which characters
59 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
(not window-system
)
90 "Non-nil means bind M-TAB in `nxml-mode-map' to `nxml-complete'.
91 C-return will be bound to `nxml-complete' in any case.
92 M-TAB gets swallowed by many window systems/managers, and
93 `documentation' will show M-TAB rather than C-return as the
94 binding for `nxml-complete' when both are bound. So it's better
95 to bind M-TAB only when it will work."
97 :set
(lambda (sym flag
)
98 (set-default sym flag
)
99 (when (and (boundp 'nxml-mode-map
) nxml-mode-map
)
100 (define-key nxml-mode-map
"\M-\t" (and flag
'nxml-complete
))))
103 (defcustom nxml-prefer-utf-16-to-utf-8-flag nil
104 "Non-nil means prefer UTF-16 to UTF-8 when saving a buffer.
105 This is used only when a buffer does not contain an encoding declaration
106 and when its current `buffer-file-coding-system' specifies neither UTF-16
111 (defcustom nxml-prefer-utf-16-little-to-big-endian-flag
(eq system-type
113 "Non-nil means prefer little-endian to big-endian byte-order for UTF-16.
114 This is used only for saving a buffer; when reading the byte-order is
115 auto-detected. It may be relevant both when there is no encoding declaration
116 and when the encoding declaration specifies `UTF-16'."
120 (defcustom nxml-default-buffer-file-coding-system nil
121 "Default value for `buffer-file-coding-system' for a buffer for a new file.
122 A value of nil means use the default value of `buffer-file-coding-system' as normal.
123 A buffer's `buffer-file-coding-system' affects what \\[nxml-insert-xml-declaration] inserts."
125 :type
'coding-system
)
127 (defcustom nxml-auto-insert-xml-declaration-flag nil
128 "Non-nil means automatically insert an XML declaration in a new file.
129 The XML declaration is inserted using `nxml-insert-xml-declaration'."
133 (defface nxml-delimited-data
134 '((t (:inherit font-lock-doc-face
)))
135 "Face used to highlight data enclosed between delimiters.
136 This is not used directly, but only via inheritance by other faces."
140 '((t (:inherit font-lock-builtin-face
)))
141 "Face used to highlight various names.
142 This includes element and attribute names, processing
143 instruction targets and the CDATA keyword in a CDATA section.
144 This is not used directly, but only via inheritance by other faces."
148 '((t (:inherit font-lock-constant-face
)))
149 "Face used to highlight character and entity references.
150 This is not used directly, but only via inheritance by other faces."
153 (defface nxml-delimiter
155 "Face used to highlight delimiters.
156 This is not used directly, but only via inheritance by other faces."
161 "Face used to highlight text."
164 (defface nxml-comment-content
165 '((t (:inherit font-lock-comment-face
)))
166 "Face used to highlight the content of comments."
169 (defface nxml-comment-delimiter
170 '((t (:inherit font-lock-comment-delimiter-face
)))
171 "Face used for the delimiters of comments, i.e <!-- and -->."
174 (defface nxml-processing-instruction-delimiter
175 '((t (:inherit nxml-delimiter
)))
176 "Face used for the delimiters of processing instructions, i.e <? and ?>."
179 (defface nxml-processing-instruction-target
180 '((t (:inherit font-lock-keyword-face
)))
181 "Face used for the target of processing instructions."
184 (defface nxml-processing-instruction-content
185 '((t (:inherit nxml-delimited-data
)))
186 "Face used for the content of processing instructions."
189 (defface nxml-cdata-section-delimiter
190 '((t (:inherit nxml-delimiter
)))
191 "Face used for the delimiters of CDATA sections, i.e <![, [, and ]]>."
194 (defface nxml-cdata-section-CDATA
195 '((t (:inherit nxml-name
)))
196 "Face used for the CDATA keyword in CDATA sections."
199 (defface nxml-cdata-section-content
200 '((t (:inherit nxml-text
)))
201 "Face used for the content of CDATA sections."
204 (defface nxml-char-ref-number
205 '((t (:inherit nxml-ref
)))
206 "Face used for the number in character references.
207 This includes ths `x' in hex references."
210 (defface nxml-char-ref-delimiter
211 '((t (:inherit nxml-ref
)))
212 "Face used for the delimiters of character references, i.e &# and ;."
215 (defface nxml-entity-ref-name
216 '((t (:inherit nxml-ref
)))
217 "Face used for the entity name in general entity references."
220 (defface nxml-entity-ref-delimiter
221 '((t (:inherit nxml-ref
)))
222 "Face used for the delimiters of entity references, i.e & and ;."
225 (defface nxml-tag-delimiter
226 '((t (:inherit nxml-delimiter
)))
227 "Face used for the angle brackets delimiting tags.
228 `nxml-tag-slash' is used for slashes."
231 (defface nxml-tag-slash
232 '((t (:inherit nxml-tag-delimiter
)))
233 "Face used for slashes in tags, both in end-tags and empty-elements."
236 (defface nxml-element-prefix
237 '((t (:inherit nxml-name
)))
238 "Face used for the prefix of elements."
241 (defface nxml-element-colon
243 "Face used for the colon in element names."
246 (defface nxml-element-local-name
247 '((t (:inherit font-lock-function-name-face
)))
248 "Face used for the local name of elements."
251 (defface nxml-attribute-prefix
252 '((t (:inherit nxml-name
)))
253 "Face used for the prefix of attributes."
256 (defface nxml-attribute-colon
257 '((t (:inherit nxml-delimiter
)))
258 "Face used for the colon in attribute names."
261 (defface nxml-attribute-local-name
262 '((t (:inherit font-lock-variable-name-face
)))
263 "Face used for the local name of attributes."
266 (defface nxml-namespace-attribute-xmlns
267 '((t (:inherit nxml-attribute-prefix
)))
268 "Face used for `xmlns' in namespace attributes."
271 (defface nxml-namespace-attribute-colon
272 '((t (:inherit nxml-attribute-colon
)))
273 "Face used for the colon in namespace attributes."
276 (defface nxml-namespace-attribute-prefix
277 '((t (:inherit nxml-attribute-local-name
)))
278 "Face used for the prefix declared in namespace attributes."
281 (defface nxml-attribute-value
282 '((t (:inherit font-lock-string-face
)))
283 "Face used for the value of attributes."
286 (defface nxml-attribute-value-delimiter
287 '((t (:inherit nxml-attribute-value
)))
288 "Face used for the delimiters of attribute values."
291 (defface nxml-namespace-attribute-value
292 '((t (:inherit nxml-attribute-value
)))
293 "Face used for the value of namespace attributes."
296 (defface nxml-namespace-attribute-value-delimiter
297 '((t (:inherit nxml-attribute-value-delimiter
)))
298 "Face used for the delimiters of namespace attribute values."
301 (defface nxml-prolog-literal-delimiter
302 '((t (:inherit nxml-delimited-data
)))
303 "Face used for the delimiters of literals in the prolog."
306 (defface nxml-prolog-literal-content
307 '((t (:inherit nxml-delimited-data
)))
308 "Face used for the content of literals in the prolog."
311 (defface nxml-prolog-keyword
312 '((t (:inherit font-lock-keyword-face
)))
313 "Face used for keywords in the prolog."
316 (defface nxml-markup-declaration-delimiter
317 '((t (:inherit nxml-delimiter
)))
318 "Face used for the delimiters of markup declarations in the prolog.
319 The delimiters are <! and >."
323 '((t (:inherit nxml-name
)))
324 "Face used for # before a name in the prolog."
348 "Face used for glyph for char references."
353 (defvar nxml-parent-document nil
354 "The parent document for a part of a modular document.
355 Use `nxml-parent-document-set' to set it.")
356 (make-variable-buffer-local 'nxml-parent-document
)
357 (put 'nxml-parent-document
'safe-local-variable
'stringp
)
359 (defvar nxml-prolog-regions nil
360 "List of regions in the prolog to be fontified.
361 See the function `xmltok-forward-prolog' for more information.")
362 (make-variable-buffer-local 'nxml-prolog-regions
)
364 (defvar nxml-last-fontify-end nil
365 "Position where fontification last ended.
366 It is nil if the buffer changed since the last fontification.")
367 (make-variable-buffer-local 'nxml-last-fontify-end
)
369 (defvar nxml-degraded nil
370 "Non-nil if currently operating in degraded mode.
371 Degraded mode is enabled when an internal error is encountered in the
372 fontification or after-change functions.")
373 (make-variable-buffer-local 'nxml-degraded
)
375 (defvar nxml-completion-hook nil
376 "Hook run by `nxml-complete'.
377 This hook is run until success.")
379 (defvar nxml-in-mixed-content-hook nil
380 "Hook to determine whether point is in mixed content.
381 The hook is called without arguments. It should return nil if it is
382 definitely not mixed; non-nil otherwise. The hook will be run until
383 one of the functions returns nil.")
385 (defvar nxml-mixed-scan-distance
4000
386 "Maximum distance from point to scan when checking for mixed content.")
388 (defvar nxml-end-tag-indent-scan-distance
4000
389 "Maximum distance from point to scan backwards when indenting end-tag.")
391 (defvar nxml-char-ref-extra-display t
392 "Non-nil means display extra information for character references.
393 The extra information consists of a tooltip with the character name
394 and, if `nxml-char-ref-display-glyph-flag' is non-nil, a glyph
395 corresponding to the referenced character following the character
397 (make-variable-buffer-local 'nxml-char-ref-extra-display
)
399 (defvar nxml-mode-map
400 (let ((map (make-sparse-keymap)))
401 (define-key map
"\M-\C-u" 'nxml-backward-up-element
)
402 (define-key map
"\M-\C-d" 'nxml-down-element
)
403 (define-key map
"\M-\C-n" 'nxml-forward-element
)
404 (define-key map
"\M-\C-p" 'nxml-backward-element
)
405 (define-key map
"\M-{" 'nxml-backward-paragraph
)
406 (define-key map
"\M-}" 'nxml-forward-paragraph
)
407 (define-key map
"\M-h" 'nxml-mark-paragraph
)
408 (define-key map
"\C-c\C-f" 'nxml-finish-element
)
409 (define-key map
"\C-c]" 'nxml-finish-element
)
410 (define-key map
"\C-c/" 'nxml-finish-element
)
411 (define-key map
"\C-c\C-m" 'nxml-split-element
)
412 (define-key map
"\C-c\C-b" 'nxml-balanced-close-start-tag-block
)
413 (define-key map
"\C-c\C-i" 'nxml-balanced-close-start-tag-inline
)
414 (define-key map
"\C-c\C-x" 'nxml-insert-xml-declaration
)
415 (define-key map
"\C-c\C-d" 'nxml-dynamic-markup-word
)
417 (define-key map
"\C-c\C-u" 'nxml-insert-named-char
)
418 (define-key map
"\C-c\C-o" nxml-outline-prefix-map
)
419 (define-key map
[S-mouse-2
] 'nxml-mouse-hide-direct-text-content
)
420 (define-key map
"/" 'nxml-electric-slash
)
421 (define-key map
[C-return
] 'nxml-complete
)
422 (when nxml-bind-meta-tab-to-complete-flag
423 (define-key map
"\M-\t" 'nxml-complete
))
425 "Keymap for nxml-mode.")
427 (defvar nxml-font-lock-keywords
428 '(nxml-fontify-matcher)
429 "Default font lock keywords for nxml-mode.")
431 (defsubst nxml-set-face
(start end face
)
432 (when (and face
(< start end
))
433 (font-lock-append-text-property start end
'face face
)))
435 (defun nxml-parent-document-set (parent-document)
436 "Set `nxml-parent-document' and inherit the DTD &c."
437 ;; FIXME: this does not work.
438 ;; the idea is that by inheriting some variables from the parent,
439 ;; `rng-validate-mode' will validate entities declared in the parent.
440 ;; alas, the most interesting variables (`rng-compile-table' et al)
441 ;; are circular and cannot be printed even with `print-circle'.
442 (interactive "fParent document")
443 (let (dtd current-schema current-schema-file-name compile-table
444 ipattern-table last-ipattern-index
)
445 (when (string= (file-truename parent-document
)
446 (file-truename buffer-file-name
))
447 (error "Parent document cannot be the same as the document"))
448 (with-current-buffer (find-file-noselect parent-document
)
450 current-schema rng-current-schema
451 current-schema-file-name rng-current-schema-file-name
452 compile-table rng-compile-table
453 ipattern-table rng-ipattern-table
454 last-ipattern-index rng-last-ipattern-index
455 parent-document buffer-file-name
))
457 rng-current-schema current-schema
458 rng-current-schema-file-name current-schema-file-name
459 rng-compile-table compile-table
460 rng-ipattern-table ipattern-table
461 rng-last-ipattern-index last-ipattern-index
462 nxml-parent-document parent-document
)
463 (message "Set parent document to %s" parent-document
)
464 (when rng-validate-mode
465 (rng-validate-while-idle (current-buffer)))))
468 (define-derived-mode nxml-mode text-mode
"nXML"
469 ;; We use C-c C-i instead of \\[nxml-balanced-close-start-tag-inline]
470 ;; because Emacs turns C-c C-i into C-c TAB which is hard to type and
472 "Major mode for editing XML.
474 \\[nxml-finish-element] finishes the current element by inserting an end-tag.
475 C-c C-i closes a start-tag with `>' and then inserts a balancing end-tag
476 leaving point between the start-tag and end-tag.
477 \\[nxml-balanced-close-start-tag-block] is similar but for block rather than inline elements:
478 the start-tag, point, and end-tag are all left on separate lines.
479 If `nxml-slash-auto-complete-flag' is non-nil, then inserting a `</'
480 automatically inserts the rest of the end-tag.
482 \\[nxml-complete] performs completion on the symbol preceding point.
484 \\[nxml-dynamic-markup-word] uses the contents of the current buffer
485 to choose a tag to put around the word preceding point.
487 Sections of the document can be displayed in outline form. The
488 variable `nxml-section-element-name-regexp' controls when an element
489 is recognized as a section. The same key sequences that change
490 visibility in outline mode are used except that they start with C-c C-o
493 Validation is provided by the related minor-mode `rng-validate-mode'.
494 This also makes completion schema- and context- sensitive. Element
495 names, attribute names, attribute values and namespace URIs can all be
496 completed. By default, `rng-validate-mode' is automatically enabled.
497 You can toggle it using \\[rng-validate-mode] or change the default by
498 customizing `rng-nxml-auto-validate-flag'.
500 \\[indent-for-tab-command] indents the current line appropriately.
501 This can be customized using the variable `nxml-child-indent'
502 and the variable `nxml-attribute-indent'.
504 \\[nxml-insert-named-char] inserts a character reference using
505 the character's name (by default, the Unicode name).
506 \\[universal-argument] \\[nxml-insert-named-char] inserts the character directly.
508 The Emacs commands that normally operate on balanced expressions will
509 operate on XML markup items. Thus \\[forward-sexp] will move forward
510 across one markup item; \\[backward-sexp] will move backward across
511 one markup item; \\[kill-sexp] will kill the following markup item;
512 \\[mark-sexp] will mark the following markup item. By default, each
513 tag each treated as a single markup item; to make the complete element
514 be treated as a single markup item, set the variable
515 `nxml-sexp-element-flag' to t. For more details, see the function
516 `nxml-forward-balanced-item'.
518 \\[nxml-backward-up-element] and \\[nxml-down-element] move up and down the element structure.
520 Many aspects this mode can be customized using
521 \\[customize-group] nxml RET."
522 ;; (kill-all-local-variables)
523 (set (make-local-variable 'mode-line-process
) '((nxml-degraded "/degraded")))
524 ;; We'll determine the fill prefix ourselves
525 (make-local-variable 'adaptive-fill-mode
)
526 (setq adaptive-fill-mode nil
)
527 (make-local-variable 'forward-sexp-function
)
528 (setq forward-sexp-function
'nxml-forward-balanced-item
)
529 (make-local-variable 'indent-line-function
)
530 (setq indent-line-function
'nxml-indent-line
)
531 (make-local-variable 'fill-paragraph-function
)
532 (setq fill-paragraph-function
'nxml-do-fill-paragraph
)
534 ;; This doesn't seem to work too well;
535 ;; I think we should probably roll our own nxml-comment-dwim function.
536 (make-local-variable 'comment-indent-function
)
537 (setq comment-indent-function
'nxml-indent-line
)
538 (make-local-variable 'comment-start
)
539 (setq comment-start
"<!--")
540 (make-local-variable 'comment-start-skip
)
541 (setq comment-start-skip
"<!--[ \t\r\n]*")
542 (make-local-variable 'comment-end
)
543 (setq comment-end
"-->")
544 (make-local-variable 'comment-end-skip
)
545 (setq comment-end-skip
"[ \t\r\n]*-->")
546 (make-local-variable 'comment-line-break-function
)
547 (setq comment-line-break-function
'nxml-newline-and-indent
)
548 (use-local-map nxml-mode-map
)
552 (nxml-clear-dependent-regions (point-min) (point-max))
553 (setq nxml-scan-end
(copy-marker (point-min) nil
))
554 (nxml-with-unmodifying-text-property-changes
555 (nxml-clear-inside (point-min) (point-max))
556 (nxml-with-invisible-motion
557 (nxml-scan-prolog)))))
558 (add-hook 'after-change-functions
'nxml-after-change nil t
)
559 (add-hook 'change-major-mode-hook
'nxml-cleanup nil t
)
561 ;; Emacs 23 handles the encoding attribute on the xml declaration
562 ;; transparently to nxml-mode, so there is no longer a need for the below
563 ;; hook. The hook also had the drawback of overriding explicit user
564 ;; instruction to save as some encoding other than utf-8.
565 ;;; (add-hook 'write-contents-hooks 'nxml-prepare-to-save)
566 (when (not (and (buffer-file-name) (file-exists-p (buffer-file-name))))
567 (when (and nxml-default-buffer-file-coding-system
568 (not (local-variable-p 'buffer-file-coding-system
)))
569 (setq buffer-file-coding-system nxml-default-buffer-file-coding-system
))
570 (when nxml-auto-insert-xml-declaration-flag
571 (nxml-insert-xml-declaration)))
573 (setq font-lock-defaults
574 '(nxml-font-lock-keywords
575 t
; keywords-only; we highlight comments and strings here
576 nil
; font-lock-keywords-case-fold-search. XML is case sensitive
577 nil
; no special syntax table
578 nil
; no automatic syntactic fontification
579 (font-lock-extend-after-change-region-function
580 . nxml-extend-after-change-region
)
581 (font-lock-extend-region-functions .
(nxml-extend-region))
582 (jit-lock-contextually . t
)
583 (font-lock-unfontify-region-function . nxml-unfontify-region
)))
586 (nxml-enable-unicode-char-name-sets))
588 (defun nxml-cleanup ()
589 "Clean up after nxml-mode."
590 ;; Disable associated minor modes.
591 (rng-validate-mode -
1)
592 ;; Clean up fontification.
595 (let ((inhibit-read-only t
)
597 (modified (buffer-modified-p)))
598 (nxml-with-invisible-motion
599 (remove-text-properties (point-min) (point-max) '(face)))
600 (set-buffer-modified-p modified
)))
601 (remove-hook 'change-major-mode-hook
'nxml-cleanup t
))
603 (defun nxml-degrade (context err
)
604 (message "Internal nXML mode error in %s (%s), degrading"
606 (error-message-string err
))
608 (setq nxml-degraded t
)
609 (setq nxml-prolog-end
1)
613 (nxml-with-unmodifying-text-property-changes
614 (nxml-clear-inside (point-min) (point-max))))))
616 ;;; Change management
618 (defun nxml-debug-region (start end
)
620 (let ((font-lock-beg start
)
623 (goto-char font-lock-beg
)
624 (set-mark font-lock-end
)))
626 (defun nxml-after-change (start end pre-change-length
)
627 ; In font-lock mode, nxml-after-change1 is called via
628 ; nxml-extend-after-change-region instead so that the updated
629 ; book-keeping information is available for fontification.
630 (unless (or font-lock-mode nxml-degraded
)
631 (nxml-with-degradation-on-error 'nxml-after-change
636 (nxml-with-invisible-motion
637 (nxml-with-unmodifying-text-property-changes
639 start end pre-change-length
)))))))))
641 (defun nxml-after-change1 (start end pre-change-length
)
642 "After-change bookkeeping.
643 Returns a cons cell containing a possibly-enlarged change region.
644 You must call `nxml-extend-region' on this expanded region to obtain
645 the full extent of the area needing refontification.
647 For bookkeeping, call this function even when fontification is
649 (let ((pre-change-end (+ start pre-change-length
)))
651 (nxml-adjust-start-for-dependent-regions start
654 ;; If the prolog might have changed, rescan the prolog
656 ;; Add 2 so as to include the < and following char that
657 ;; start the instance (document element), since changing
658 ;; these can change where the prolog ends.
659 (+ nxml-prolog-end
2))
660 ;; end must be extended to at least the end of the old prolog in
661 ;; case the new prolog is shorter
662 (when (< pre-change-end nxml-prolog-end
)
664 ;; don't let end get out of range even if pre-change-length
667 (+ end
(- nxml-prolog-end pre-change-end
)))))
669 (setq start
(point-min))))
671 (when (> end nxml-prolog-end
)
673 (nxml-move-tag-backwards (point-min))
675 (setq end
(max (nxml-scan-after-change start end
)
678 (nxml-debug-change "nxml-after-change1" start end
)
683 (defun nxml-insert-xml-declaration ()
684 "Insert an XML declaration at the beginning of buffer.
685 The XML declaration will declare an encoding depending on the buffer's
686 `buffer-file-coding-system'."
689 (if (and buffer-file-coding-system
690 (coding-system-p buffer-file-coding-system
)
691 (coding-system-get buffer-file-coding-system
693 buffer-file-coding-system
694 (nxml-choose-utf-coding-system))))
695 (goto-char (point-min))
696 (insert (format "<?xml version=\"1.0\" encoding=\"%s\"?>\n"
697 (nxml-coding-system-name coding-system
)))))
699 (defun nxml-prepare-to-save ()
700 (unless (and (not enable-multibyte-characters
)
701 (local-variable-p 'buffer-file-coding-system
)
702 buffer-file-coding-system
703 (or (eq (coding-system-type buffer-file-coding-system
) 5)
704 (eq buffer-file-coding-system
'no-conversion
)))
706 (setq buffer-file-coding-system
(nxml-select-coding-system))))
707 ;; nil from a function in `write-contents-hooks' means
708 ;; to continue and write the file as normal
711 (defun nxml-select-coding-system ()
712 (let* ((suitable-coding-systems
713 (find-coding-systems-region (point-min) (point-max)))
715 (goto-char (point-min))
716 (xmltok-get-declared-encoding-position)))
719 (buffer-substring-no-properties (car enc-pos
)
723 (if (string= (downcase enc-name
) "utf-16")
724 (nxml-choose-utf-16-coding-system)
725 (nxml-mime-charset-coding-system enc-name
)))
726 (enc-pos (nxml-choose-utf-coding-system)))))
727 ;; Make sure we have a coding-system
728 (unless coding-system
730 (and (not buffer-read-only
)
731 (nxml-choose-suitable-coding-system
732 suitable-coding-systems
)))
735 (format "Unknown encoding %s" enc-name
)
736 "XML declaration is not well-formed")))
737 (cond ((not coding-system
)
738 (error "%s" message
))
744 "Modify and save with encoding %s")
745 (nxml-coding-system-name coding-system
))
747 (nxml-fix-encoding-declaration enc-pos coding-system
))
748 (t (signal 'quit nil
)))))
749 ;; Make sure it can encode all the characters in the buffer
750 (unless (or (memq (coding-system-base coding-system
)
751 suitable-coding-systems
)
752 (equal suitable-coding-systems
'(undecided)))
754 (nxml-unsuitable-coding-system-message coding-system
757 (and (not buffer-read-only
)
758 (nxml-choose-suitable-coding-system
759 suitable-coding-systems
)))
760 (cond ((not coding-system
) (error "%s" message
))
761 ((y-or-n-p (concat message
762 (format ". Save with %s "
763 (nxml-coding-system-name
765 (nxml-fix-encoding-declaration enc-pos coding-system
))
766 (t (signal 'quit nil
)))))
767 ;; Merge the newline type of our existing encoding
768 (let ((current-eol-type
769 (coding-system-eol-type buffer-file-coding-system
)))
770 (when (and current-eol-type
(integerp current-eol-type
))
772 (coding-system-change-eol-conversion coding-system
776 (defun nxml-unsuitable-coding-system-message (coding-system &optional enc-name
)
777 (if (nxml-coding-system-unicode-p coding-system
)
778 "Cannot translate some characters to Unicode"
779 (format "Cannot encode some characters with %s"
781 (nxml-coding-system-name coding-system
)))))
783 (defconst nxml-utf-16-coding-systems
(and (coding-system-p 'utf-16-be
)
784 (coding-system-p 'utf-16-le
)
785 '(utf-16-be utf-16-le
)))
787 (defconst nxml-utf-coding-systems
(cons 'utf-8 nxml-utf-16-coding-systems
))
789 (defun nxml-coding-system-unicode-p (coding-system)
790 (nxml-coding-system-member (coding-system-base coding-system
)
791 nxml-utf-coding-systems
))
793 (defun nxml-coding-system-name (coding-system)
794 (setq coding-system
(coding-system-base coding-system
))
796 (if (nxml-coding-system-member coding-system nxml-utf-16-coding-systems
)
798 (or (coding-system-get coding-system
'mime-charset
)
801 (defun nxml-fix-encoding-declaration (enc-pos coding-system
)
802 (let ((charset (nxml-coding-system-name coding-system
)))
803 (cond ((consp enc-pos
)
804 (delete-region (car enc-pos
) (cdr enc-pos
))
805 (goto-char (car enc-pos
))
809 (insert " encoding=\"" charset ?
\"))
811 (goto-char (point-min))
812 (insert "<?xml version=\"1.0\" encoding=\""
815 (when (and (not enc-pos
)
816 (let ((case-fold-search t
))
817 (looking-at xmltok-bad-xml-decl-regexp
)))
818 (delete-region (point) (match-end 0)))))))
820 (defun nxml-choose-suitable-coding-system (suitable-coding-systems)
821 (let (ret coding-system
)
822 (if (and buffer-file-coding-system
823 (memq (coding-system-base buffer-file-coding-system
)
824 suitable-coding-systems
))
825 buffer-file-coding-system
826 (while (and suitable-coding-systems
(not ret
))
827 (setq coding-system
(car suitable-coding-systems
))
828 (if (coding-system-get coding-system
'mime-charset
)
829 (setq ret coding-system
)
830 (setq suitable-coding-systems
(cdr suitable-coding-systems
))))
833 (defun nxml-choose-utf-coding-system ()
834 (let ((cur (and (local-variable-p 'buffer-file-coding-system
)
835 buffer-file-coding-system
836 (coding-system-base buffer-file-coding-system
))))
837 (cond ((car (nxml-coding-system-member cur nxml-utf-coding-systems
)))
838 ((and nxml-prefer-utf-16-to-utf-8-flag
839 (coding-system-p 'utf-16-le
)
840 (coding-system-p 'utf-16-be
))
841 (if nxml-prefer-utf-16-little-to-big-endian-flag
846 (defun nxml-choose-utf-16-coding-system ()
847 (let ((cur (and (local-variable-p 'buffer-file-coding-system
)
848 buffer-file-coding-system
849 (coding-system-base buffer-file-coding-system
))))
850 (cond ((car (nxml-coding-system-member cur nxml-utf-16-coding-systems
)))
851 (nxml-prefer-utf-16-little-to-big-endian-flag
852 (and (coding-system-p 'utf-16-le
) 'utf-16-le
))
853 (t (and (coding-system-p 'utf-16-be
) 'utf-16-be
)))))
855 (defun nxml-coding-system-member (coding-system coding-systems
)
857 (while (and coding-systems
(not ret
))
858 (if (coding-system-equal coding-system
859 (car coding-systems
))
860 (setq ret coding-systems
)
861 (setq coding-systems
(cdr coding-systems
))))
866 (defun nxml-unfontify-region (start end
)
867 (font-lock-default-unfontify-region start end
)
868 (nxml-clear-char-ref-extra-display start end
))
870 (defvar font-lock-beg
) (defvar font-lock-end
)
871 (defun nxml-extend-region ()
872 "Extend the region to hold the minimum area we can fontify with nXML.
873 Called with `font-lock-beg' and `font-lock-end' dynamically bound."
874 (let ((start font-lock-beg
)
877 (nxml-debug-change "nxml-extend-region(input)" start end
)
879 (when (< start nxml-prolog-end
)
880 (setq start
(point-min)))
882 (cond ((<= end nxml-prolog-end
)
883 (setq end nxml-prolog-end
))
887 ;; some font-lock backends (like Emacs 22 jit-lock) snap
888 ;; the region to the beginning of the line no matter what
889 ;; we say here. To mitigate the resulting excess
890 ;; fontification, ignore leading whitespace.
891 (skip-syntax-forward " ")
893 ;; find the beginning of the previous tag
894 (when (not (equal (char-after) ?\
<))
895 (search-backward "<" nxml-prolog-end t
))
896 (nxml-ensure-scan-up-to-date)
897 (nxml-move-outside-backwards)
900 (while (< (point) end
)
901 (nxml-tokenize-forward))
905 (when (or (< start font-lock-beg
)
906 (> end font-lock-end
))
907 (setq font-lock-beg start
909 (nxml-debug-change "nxml-extend-region" start end
)
912 (defun nxml-extend-after-change-region (start end pre-change-length
)
913 (unless nxml-degraded
914 (setq nxml-last-fontify-end nil
)
915 (let ((region (nxml-with-degradation-on-error
916 'nxml-extend-after-change-region
921 (nxml-with-invisible-motion
922 (nxml-with-unmodifying-text-property-changes
923 (nxml-extend-after-change-region1
924 start end pre-change-length
)))))))))
925 (if (consp region
) region
))))
927 (defun nxml-extend-after-change-region1 (start end pre-change-length
)
928 (let* ((region (nxml-after-change1 start end pre-change-length
))
929 (font-lock-beg (car region
))
930 (font-lock-end (cdr region
)))
933 (cons font-lock-beg font-lock-end
)))
935 (defun nxml-fontify-matcher (bound)
936 "Called as font-lock keyword matcher."
938 (unless nxml-degraded
939 (nxml-debug-change "nxml-fontify-matcher" (point) bound
)
941 (when (< (point) nxml-prolog-end
)
942 ;; prolog needs to be fontified in one go, and
943 ;; nxml-extend-region makes sure we start at BOB.
945 (nxml-fontify-prolog)
946 (goto-char nxml-prolog-end
))
948 (let (xmltok-dependent-regions
950 (while (and (nxml-tokenize-forward)
951 (<= (point) bound
)) ; intervals are open-ended
952 (nxml-apply-fontify-rule)))
954 (setq nxml-last-fontify-end
(point)))
956 ;; Since we did the fontification internally, tell font-lock to not
957 ;; do anything itself.
960 (defun nxml-fontify-prolog ()
962 The buffer is assumed to be prepared for fontification.
963 This does not set the fontified property, but it does clear
964 faces appropriately."
965 (let ((regions nxml-prolog-regions
))
967 (let ((region (car regions
)))
968 (nxml-apply-fontify-rule (aref region
0)
971 (setq regions
(cdr regions
)))))
973 ;; Vectors identify a substring of the token to be highlighted in some face.
975 ;; Token types returned by xmltok-forward.
979 '([nil
1 nxml-tag-delimiter
]
980 [-
1 nil nxml-tag-delimiter
]
984 (put 'partial-start-tag
986 '([nil
1 nxml-tag-delimiter
]
992 '([nil
1 nxml-tag-delimiter
]
994 [-
1 nil nxml-tag-delimiter
]
995 (element-qname .
2)))
997 (put 'partial-end-tag
999 '([nil
1 nxml-tag-delimiter
]
1000 [1 2 nxml-tag-slash
]
1001 (element-qname .
2)))
1005 '([nil
1 nxml-tag-delimiter
]
1006 [-
2 -
1 nxml-tag-slash
]
1007 [-
1 nil nxml-tag-delimiter
]
1011 (put 'partial-empty-element
1013 '([nil
1 nxml-tag-delimiter
]
1014 [-
1 nil nxml-tag-slash
]
1020 '([nil
2 nxml-char-ref-delimiter
]
1021 [2 -
1 nxml-char-ref-number
]
1022 [-
1 nil nxml-char-ref-delimiter
]
1027 '([nil
1 nxml-entity-ref-delimiter
]
1028 [1 -
1 nxml-entity-ref-name
]
1029 [-
1 nil nxml-entity-ref-delimiter
]))
1033 '([nil
4 nxml-comment-delimiter
]
1034 [4 -
3 nxml-comment-content
]
1035 [-
3 nil nxml-comment-delimiter
]))
1037 (put 'processing-instruction
1039 '([nil
2 nxml-processing-instruction-delimiter
]
1040 [-
2 nil nxml-processing-instruction-delimiter
]
1041 processing-instruction-content
))
1045 '([nil
3 nxml-cdata-section-delimiter
] ; <![
1046 [3 8 nxml-cdata-section-CDATA
] ; CDATA
1047 [8 9 nxml-cdata-section-delimiter
] ; [
1048 [9 -
3 nxml-cdata-section-content
] ; ]]>
1049 [-
3 nil nxml-cdata-section-delimiter
]))
1053 '([nil nil nxml-text
]))
1055 ;; Prolog region types in list returned by xmltok-forward-prolog.
1057 (put 'xml-declaration
1059 '([nil
2 nxml-processing-instruction-delimiter
]
1060 [2 5 nxml-processing-instruction-target
]
1061 [-
2 nil nxml-processing-instruction-delimiter
]))
1063 (put 'xml-declaration-attribute-name
1065 '([nil nil nxml-attribute-local-name
]))
1067 (put 'xml-declaration-attribute-value
1069 '([nil
1 nxml-attribute-value-delimiter
]
1070 [1 -
1 nxml-attribute-value
]
1071 [-
1 nil nxml-attribute-value-delimiter
]))
1073 (put 'processing-instruction-left
1075 '([nil
2 nxml-processing-instruction-delimiter
]
1076 [2 nil nxml-processing-instruction-target
]))
1078 (put 'processing-instruction-right
1080 '([nil -
2 nxml-processing-instruction-content
]
1081 [-
2 nil nxml-processing-instruction-delimiter
]))
1085 '([nil
1 nxml-prolog-literal-delimiter
]
1086 [1 -
1 nxml-prolog-literal-content
]
1087 [-
1 nil nxml-prolog-literal-delimiter
]))
1091 '([nil nil nxml-prolog-keyword
]))
1093 (put 'markup-declaration-open
1095 '([0 2 nxml-markup-declaration-delimiter
]
1096 [2 nil nxml-prolog-keyword
]))
1098 (put 'markup-declaration-close
1100 '([nil nil nxml-markup-declaration-delimiter
]))
1102 (put 'internal-subset-open
1104 '([nil nil nxml-markup-declaration-delimiter
]))
1106 (put 'internal-subset-close
1108 '([nil
1 nxml-markup-declaration-delimiter
]
1109 [-
1 nil nxml-markup-declaration-delimiter
]))
1114 [1 nil nxml-prolog-keyword
]))
1116 (defun nxml-apply-fontify-rule (&optional type start end
)
1117 (let ((rule (get (or type xmltok-type
) 'nxml-fontify-rule
)))
1118 (unless start
(setq start xmltok-start
))
1119 (unless end
(setq end
(point)))
1121 (let* ((action (car rule
)))
1122 (setq rule
(cdr rule
))
1123 (cond ((vectorp action
)
1124 (nxml-set-face (let ((offset (aref action
0)))
1125 (cond ((not offset
) start
)
1126 ((< offset
0) (+ end offset
))
1127 (t (+ start offset
))))
1128 (let ((offset (aref action
1)))
1129 (cond ((not offset
) end
)
1130 ((< offset
0) (+ end offset
))
1131 (t (+ start offset
))))
1133 ((and (consp action
)
1134 (eq (car action
) 'element-qname
))
1135 (when xmltok-name-end
; maybe nil in partial-end-tag case
1136 (nxml-fontify-qname (+ start
(cdr action
))
1139 'nxml-element-prefix
1141 'nxml-element-local-name
)))
1142 ((eq action
'attributes
)
1143 (nxml-fontify-attributes))
1144 ((eq action
'processing-instruction-content
)
1145 (nxml-set-face (+ start
2)
1147 'nxml-processing-instruction-target
)
1148 (nxml-set-face (save-excursion
1149 (goto-char xmltok-name-end
)
1150 (skip-chars-forward " \t\r\n")
1153 'nxml-processing-instruction-content
))
1154 ((eq action
'char-ref
)
1155 (nxml-char-ref-display-extra start
1157 (xmltok-char-number start end
)))
1158 (t (error "Invalid nxml-fontify-rule action %s" action
)))))))
1160 (defun nxml-fontify-attributes ()
1161 (while xmltok-namespace-attributes
1162 (nxml-fontify-attribute (car xmltok-namespace-attributes
)
1164 (setq xmltok-namespace-attributes
1165 (cdr xmltok-namespace-attributes
)))
1166 (while xmltok-attributes
1167 (nxml-fontify-attribute (car xmltok-attributes
))
1168 (setq xmltok-attributes
1169 (cdr xmltok-attributes
))))
1171 (defun nxml-fontify-attribute (att &optional namespace-declaration
)
1172 (if namespace-declaration
1173 (nxml-fontify-qname (xmltok-attribute-name-start att
)
1174 (xmltok-attribute-name-colon att
)
1175 (xmltok-attribute-name-end att
)
1176 'nxml-namespace-attribute-xmlns
1177 'nxml-namespace-attribute-colon
1178 'nxml-namespace-attribute-prefix
1179 'nxml-namespace-attribute-xmlns
)
1180 (nxml-fontify-qname (xmltok-attribute-name-start att
)
1181 (xmltok-attribute-name-colon att
)
1182 (xmltok-attribute-name-end att
)
1183 'nxml-attribute-prefix
1184 'nxml-attribute-colon
1185 'nxml-attribute-local-name
))
1186 (let ((start (xmltok-attribute-value-start att
))
1187 (end (xmltok-attribute-value-end att
))
1188 (refs (xmltok-attribute-refs att
))
1189 (delimiter-face (if namespace-declaration
1190 'nxml-namespace-attribute-value-delimiter
1191 'nxml-attribute-value-delimiter
))
1192 (value-face (if namespace-declaration
1193 'nxml-namespace-attribute-value
1194 'nxml-attribute-value
)))
1196 (nxml-set-face (1- start
) start delimiter-face
)
1197 (nxml-set-face end
(1+ end
) delimiter-face
)
1199 (let* ((ref (car refs
))
1200 (ref-type (aref ref
0))
1201 (ref-start (aref ref
1))
1202 (ref-end (aref ref
2)))
1203 (nxml-set-face start ref-start value-face
)
1204 (nxml-apply-fontify-rule ref-type ref-start ref-end
)
1205 (setq start ref-end
))
1206 (setq refs
(cdr refs
)))
1207 (nxml-set-face start end value-face
))))
1209 (defun nxml-fontify-qname (start
1217 (cond (colon (nxml-set-face start colon prefix-face
)
1218 (nxml-set-face colon
(1+ colon
) colon-face
)
1219 (nxml-set-face (1+ colon
) end local-name-face
))
1220 (t (nxml-set-face start end
(or unprefixed-face
1221 local-name-face
)))))
1225 (defun nxml-electric-slash (arg)
1228 With a prefix ARG, do nothing other than insert the slash.
1230 Otherwise, if `nxml-slash-auto-complete-flag' is non-nil, insert the
1231 rest of the end-tag or empty-element if the slash is potentially part
1232 of an end-tag or the close of an empty-element.
1234 If the slash is part of an end-tag that is the first non-whitespace
1235 on the line, reindent the line."
1237 (nxml-ensure-scan-up-to-date)
1238 (let* ((slash-pos (point))
1239 (end-tag-p (and (eq (char-before slash-pos
) ?
<)
1240 (not (nxml-get-inside slash-pos
))))
1241 (at-indentation (save-excursion
1242 (back-to-indentation)
1243 (eq (point) (1- slash-pos
)))))
1244 (self-insert-command (prefix-numeric-value arg
))
1246 (if nxml-slash-auto-complete-flag
1249 (let ((start-tag-end
1250 (nxml-scan-element-backward (1- slash-pos
) t
)))
1252 (insert (xmltok-start-tag-qname) ">")
1253 ;; copy the indentation of the start-tag
1254 (when (and at-indentation
1256 (goto-char xmltok-start
)
1257 (back-to-indentation)
1258 (eq (point) xmltok-start
)))
1260 (indent-line-to (save-excursion
1261 (goto-char xmltok-start
)
1262 (current-column)))))))
1263 (nxml-scan-error nil
))
1264 (when (and (eq (nxml-token-before) (point))
1265 (eq xmltok-type
'partial-empty-element
))
1267 (when (and end-tag-p at-indentation
)
1268 (nxml-indent-line))))))
1270 (defun nxml-balanced-close-start-tag-block ()
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 If there is nothing but whitespace before the `<' that opens the
1274 start-tag, then put point on a blank line, and put the end-tag on
1275 another line aligned with the start-tag."
1277 (nxml-balanced-close-start-tag 'block
))
1279 (defun nxml-balanced-close-start-tag-inline ()
1280 "Close the start-tag before point with `>' and insert a balancing end-tag.
1281 Point is left between the start-tag and the end-tag.
1282 No extra whitespace is inserted."
1284 (nxml-balanced-close-start-tag 'inline
))
1286 (defun nxml-balanced-close-start-tag (block-or-inline)
1287 (let ((token-end (nxml-token-before))
1289 (token-start xmltok-start
))
1290 (unless (or (eq xmltok-type
'partial-start-tag
)
1291 (and (memq xmltok-type
'(start-tag
1293 partial-empty-element
))
1294 (>= token-end pos
)))
1295 (error "Not in a start-tag"))
1296 ;; Note that this insertion changes xmltok-start.
1298 (buffer-substring-no-properties (+ xmltok-start
1)
1299 (min xmltok-name-end
(point)))
1301 (if (eq block-or-inline
'inline
)
1303 (goto-char token-start
)
1304 (back-to-indentation)
1305 (if (= (point) token-start
)
1306 (let ((indent (current-column)))
1309 (indent-line-to indent
)
1312 (indent-line-to (+ nxml-child-indent indent
)))
1315 (defun nxml-finish-element ()
1316 "Finish the current element by inserting an end-tag."
1318 (nxml-finish-element-1 nil
))
1320 (defvar nxml-last-split-position nil
1321 "Position where `nxml-split-element' split the current element.")
1323 (defun nxml-split-element ()
1324 "Split the current element by inserting an end-tag and a start-tag.
1325 Point is left after the newly inserted start-tag. When repeated,
1326 split immediately before the previously inserted start-tag and leave
1329 (setq nxml-last-split-position
1330 (if (and (eq last-command this-command
)
1331 nxml-last-split-position
)
1333 (goto-char nxml-last-split-position
)
1334 (nxml-finish-element-1 t
))
1335 (nxml-finish-element-1 t
))))
1337 (defun nxml-finish-element-1 (startp)
1338 "Insert an end-tag for the current element and optionally a start-tag.
1339 The start-tag is inserted if STARTP is non-nil. Return the position
1340 of the inserted start-tag or nil if none was inserted."
1342 (let* ((token-end (nxml-token-before))
1345 (when (and (< (point) token-end
)
1348 processing-instruction
1353 (error "Point is inside a %s"
1354 (nxml-token-type-friendly-name xmltok-type
)))
1355 (nxml-scan-element-backward token-end t
)))
1358 (unless (eq xmltok-type
'start-tag
)
1359 (error "No matching start-tag"))
1360 (goto-char xmltok-start
)
1361 (back-to-indentation)
1362 (eq (point) xmltok-start
)))
1365 (goto-char start-tag-end
)
1366 (looking-at "[ \t\r\n]*$")))
1367 (start-tag-indent (save-excursion
1368 (goto-char xmltok-start
)
1370 (qname (xmltok-start-tag-qname))
1371 inserted-start-tag-pos
)
1372 (when (and starts-line ends-line
)
1373 ;; start-tag is on a line by itself
1374 ;; => put the end-tag on a line by itself
1377 (back-to-indentation)
1380 (indent-line-to start-tag-indent
))
1381 (insert "</" qname
">")
1385 (indent-line-to start-tag-indent
))
1386 (setq inserted-start-tag-pos
(point))
1387 (insert "<" qname
">")
1388 (when (and starts-line ends-line
)
1390 (indent-line-to (save-excursion
1391 (goto-char xmltok-start
)
1393 (back-to-indentation)
1394 (if (= (current-column)
1395 (+ start-tag-indent nxml-child-indent
))
1396 (+ start-tag-indent nxml-child-indent
)
1397 start-tag-indent
)))))
1398 inserted-start-tag-pos
))
1402 (defun nxml-indent-line ()
1403 "Indent current line as XML."
1404 (let* ((savep (point))
1405 (indent (condition-case nil
1408 (skip-chars-forward " \t")
1409 (if (>= (point) savep
) (setq savep nil
))
1410 (or (nxml-compute-indent) 0))
1412 (if (not (numberp indent
))
1413 ;; If something funny is used (e.g. `noindent'), return it.
1415 (if (< indent
0) (setq indent
0)) ;Just in case.
1417 (save-excursion (indent-line-to indent
))
1418 (indent-line-to indent
)))))
1420 (defun nxml-compute-indent ()
1421 "Return the indent for the line containing point."
1422 (or (nxml-compute-indent-from-matching-start-tag)
1423 (nxml-compute-indent-from-previous-line)))
1425 (defun nxml-compute-indent-from-matching-start-tag ()
1426 "Compute the indent for a line with an end-tag using the matching start-tag.
1427 When the line containing point ends with an end-tag and does not start
1428 in the middle of a token, return the indent of the line containing the
1429 matching start-tag, if there is one and it occurs at the beginning of
1430 its line. Otherwise return nil."
1432 (back-to-indentation)
1433 (let ((bol (point)))
1434 (let ((inhibit-field-text-motion t
))
1436 (skip-chars-backward " \t")
1437 (and (= (nxml-token-before) (point))
1438 (memq xmltok-type
'(end-tag partial-end-tag
))
1439 ;; start of line must not be inside a token
1440 (or (= xmltok-start bol
)
1444 (= xmltok-start bol
))
1445 (eq xmltok-type
'data
))
1447 (nxml-scan-element-backward
1451 nxml-end-tag-indent-scan-distance
))
1452 (nxml-scan-error nil
))
1453 (< xmltok-start bol
)
1455 (goto-char xmltok-start
)
1456 (skip-chars-backward " \t")
1458 (current-indentation)))))
1460 (defun nxml-compute-indent-from-previous-line ()
1461 "Compute the indent for a line using the indentation of a previous line."
1466 before-context after-context
)
1467 (back-to-indentation)
1470 ;; Move backwards until the start of a non-blank line that is
1471 ;; not inside a token.
1473 (when (= (forward-line -
1) -
1)
1475 (back-to-indentation)
1476 (if (looking-at "[ \t]*$")
1479 (setq prev-bol
(point)))
1481 (not (or (= xmltok-start
(point))
1482 (eq xmltok-type
'data
))))))
1484 ;; Now scan over tokens until the end of the line to be indented.
1485 ;; Determine the context before and after the beginning of the
1487 (while (< (point) eol
)
1488 (nxml-tokenize-forward)
1489 (cond ((<= bol xmltok-start
)
1491 (nxml-merge-indent-context-type after-context
)))
1492 ((and (<= (point) bol
)
1493 (not (and (eq xmltok-type
'partial-start-tag
)
1495 (setq before-context
1496 (nxml-merge-indent-context-type before-context
)))
1497 ((eq xmltok-type
'data
)
1498 (setq before-context
1499 (nxml-merge-indent-context-type before-context
))
1501 (nxml-merge-indent-context-type after-context
)))
1502 ;; If in the middle of a token that looks inline,
1503 ;; then indent relative to the previous non-blank line
1504 ((eq (nxml-merge-indent-context-type before-context
)
1506 (goto-char prev-bol
)
1507 (throw 'indent
(current-column)))
1510 (nxml-compute-indent-in-token bol
))))
1511 (skip-chars-forward " \t\r\n"))
1514 (* nxml-child-indent
1515 (+ (if (eq before-context
'start-tag
) 1 0)
1516 (if (eq after-context
'end-tag
) -
1 0))))))))
1518 (defun nxml-merge-indent-context-type (context)
1519 "Merge the indent context type CONTEXT with the token in `xmltok-type'.
1520 Return the merged indent context type. An indent context type is
1521 either nil or one of the symbols `start-tag', `end-tag', `markup',
1522 `comment', `mixed'."
1523 (cond ((memq xmltok-type
'(start-tag partial-start-tag
))
1524 (if (memq context
'(nil start-tag comment
))
1527 ((memq xmltok-type
'(end-tag partial-end-tag
))
1528 (if (memq context
'(nil end-tag comment
))
1531 ((eq xmltok-type
'comment
)
1532 (cond ((memq context
'(start-tag end-tag comment
))
1539 (defun nxml-compute-indent-in-token (pos)
1540 "Return the indent for a line that starts inside a token.
1541 POS is the position of the first non-whitespace character of the line.
1542 This expects the xmltok-* variables to be set up as by `xmltok-forward'."
1543 (cond ((memq xmltok-type
'(start-tag
1546 partial-empty-element
))
1547 (nxml-compute-indent-in-start-tag pos
))
1548 ((eq xmltok-type
'comment
)
1549 (nxml-compute-indent-in-delimited-token pos
"<!--" "-->"))
1550 ((eq xmltok-type
'cdata-section
)
1551 (nxml-compute-indent-in-delimited-token pos
"<![CDATA[" "]]>"))
1552 ((eq xmltok-type
'processing-instruction
)
1553 (nxml-compute-indent-in-delimited-token pos
"<?" "?>"))
1556 (if (and (= (forward-line -
1) 0)
1557 (< xmltok-start
(point)))
1558 (back-to-indentation)
1559 (goto-char xmltok-start
))
1562 (defun nxml-compute-indent-in-start-tag (pos)
1563 "Return the indent for a line that starts inside a start-tag.
1564 Also for a line that starts inside an empty element.
1565 POS is the position of the first non-whitespace character of the line.
1566 This expects the xmltok-* variables to be set up as by `xmltok-forward'."
1567 (let ((value-boundary (nxml-attribute-value-boundary pos
))
1570 ;; inside an attribute value
1571 (let ((value-start (car value-boundary
))
1572 (value-end (cdr value-boundary
)))
1575 (if (< (point) value-start
)
1576 (goto-char value-start
)
1577 (back-to-indentation)))
1578 ;; outside an attribute value
1580 (while (and (= (forward-line -
1) 0)
1581 (nxml-attribute-value-boundary (point))))
1582 (cond ((<= (point) xmltok-start
)
1583 (goto-char xmltok-start
)
1584 (setq off nxml-attribute-indent
)
1585 (let ((atts (xmltok-merge-attributes)))
1587 (let* ((att (car atts
))
1588 (start (xmltok-attribute-name-start att
)))
1593 (back-to-indentation))))
1594 (+ (current-column) off
)))
1596 (defun nxml-attribute-value-boundary (pos)
1597 "Return a pair (START . END) if POS is inside an attribute value.
1598 Otherwise return nil. START and END are the positions of the start
1599 and end of the attribute value containing POS. This expects the
1600 xmltok-* variables to be set up as by `xmltok-forward'."
1601 (let ((atts (xmltok-merge-attributes))
1602 att value-start value-end value-boundary
)
1604 (setq att
(car atts
))
1605 (setq value-start
(xmltok-attribute-value-start att
))
1606 (setq value-end
(xmltok-attribute-value-end att
))
1607 (cond ((and value-start
(< pos value-start
))
1609 ((and value-start value-end
(<= pos value-end
))
1610 (setq value-boundary
(cons value-start value-end
))
1612 (t (setq atts
(cdr atts
)))))
1615 (defun nxml-compute-indent-in-delimited-token (pos open-delim close-delim
)
1616 "Return the indent for a line that starts inside a token with delimiters.
1617 OPEN-DELIM and CLOSE-DELIM are strings giving the opening and closing
1618 delimiters. POS is the position of the first non-whitespace character
1619 of the line. This expects the xmltok-* variables to be set up as by
1621 (cond ((let ((end (+ pos
(length close-delim
))))
1622 (and (<= end
(point-max))
1623 (string= (buffer-substring-no-properties pos end
)
1625 (goto-char xmltok-start
))
1629 (<= (point) xmltok-start
))
1630 (goto-char (+ xmltok-start
(length open-delim
)))
1631 (when (and (string= open-delim
"<!--")
1633 (goto-char (1+ (point)))))
1634 (t (back-to-indentation)))
1639 (defun nxml-complete ()
1640 "Perform completion on the symbol preceding point.
1642 Inserts as many characters as can be completed. However, if not even
1643 one character can be completed, then a buffer with the possibilities
1644 is popped up and the symbol is read from the minibuffer with
1645 completion. If the symbol is complete, then any characters that must
1646 follow the symbol are also inserted.
1648 The name space used for completion and what is treated as a symbol
1649 depends on the context. The contexts in which completion is performed
1650 depend on `nxml-completion-hook'."
1652 (unless (run-hook-with-args-until-success 'nxml-completion-hook
)
1653 ;; Eventually we will complete on entity names here.
1655 (message "Cannot complete in this context")))
1659 (defun nxml-forward-balanced-item (&optional arg
)
1660 "Move forward across one balanced item.
1661 With ARG, do it that many times. Negative arg -N means
1662 move backward across N balanced expressions.
1663 This is the equivalent of `forward-sexp' for XML.
1665 An element contains as items strings with no markup, tags, processing
1666 instructions, comments, CDATA sections, entity references and
1667 characters references. However, if the variable
1668 `nxml-sexp-element-flag' is non-nil, then an element is treated as a
1669 single markup item. A start-tag contains an element name followed by
1670 one or more attributes. An end-tag contains just an element name.
1671 An attribute value literals contains strings with no markup, entity
1672 references and character references. A processing instruction
1673 consists of a target and a content string. A comment or a CDATA
1674 section contains a single string. An entity reference contains a
1675 single name. A character reference contains a character number."
1677 (or arg
(setq arg
1))
1680 (nxml-forward-single-balanced-item)
1681 (> (setq arg
(1- arg
)) 0))))
1684 (nxml-backward-single-balanced-item)
1685 (< (setq arg
(1+ arg
)) 0))))))
1687 (defun nxml-forward-single-balanced-item ()
1689 (goto-char (let ((end (nxml-token-after)))
1691 (while (eq xmltok-type
'space
)
1693 (setq end
(nxml-token-after)))
1694 (cond ((/= (point) xmltok-start
)
1695 (nxml-scan-forward-within end
))
1696 ((and nxml-sexp-element-flag
1697 (eq xmltok-type
'start-tag
))
1698 ;; can't ever return nil here
1699 (nxml-scan-element-forward xmltok-start
))
1700 ((and nxml-sexp-element-flag
1702 '(end-tag partial-end-tag
)))
1703 (error "Already at end of element"))
1706 (goto-char (cadr err
))
1707 (apply 'error
(cddr err
)))))
1709 (defun nxml-backward-single-balanced-item ()
1711 (goto-char (let ((end (nxml-token-before)))
1713 (while (eq xmltok-type
'space
)
1714 (goto-char xmltok-start
)
1715 (setq end
(nxml-token-before)))
1716 (cond ((/= (point) end
)
1717 (nxml-scan-backward-within end
))
1718 ((and nxml-sexp-element-flag
1719 (eq xmltok-type
'end-tag
))
1720 ;; can't ever return nil here
1721 (nxml-scan-element-backward end
)
1723 ((and nxml-sexp-element-flag
1724 (eq xmltok-type
'start-tag
))
1725 (error "Already at start of element"))
1726 (t xmltok-start
)))))
1728 (goto-char (cadr err
))
1729 (apply 'error
(cddr err
)))))
1731 (defun nxml-scan-forward-within (end)
1732 (setq end
(- end
(nxml-end-delimiter-length xmltok-type
)))
1733 (when (<= end
(point))
1734 (error "Already at end of %s"
1735 (nxml-token-type-friendly-name xmltok-type
)))
1736 (cond ((memq xmltok-type
'(start-tag
1739 partial-empty-element
))
1740 (if (< (point) xmltok-name-end
)
1742 (let ((att (nxml-find-following-attribute)))
1743 (cond ((not att
) end
)
1744 ((and (xmltok-attribute-value-start att
)
1745 (<= (xmltok-attribute-value-start att
)
1747 (nxml-scan-forward-in-attribute-value att
))
1748 ((xmltok-attribute-value-end att
)
1749 (1+ (xmltok-attribute-value-end att
)))
1751 (goto-char (xmltok-attribute-name-end att
))
1752 (looking-at "[ \t\r\n]*="))
1754 (t (xmltok-attribute-name-end att
))))))
1755 ((and (eq xmltok-type
'processing-instruction
)
1756 (< (point) xmltok-name-end
))
1760 (defun nxml-scan-backward-within (end)
1763 (nxml-start-delimiter-length xmltok-type
)))
1764 (when (<= (point) xmltok-start
)
1765 (error "Already at start of %s"
1766 (nxml-token-type-friendly-name xmltok-type
)))
1767 (cond ((memq xmltok-type
'(start-tag
1770 partial-empty-element
))
1771 (let ((att (nxml-find-preceding-attribute)))
1772 (cond ((not att
) xmltok-start
)
1773 ((and (xmltok-attribute-value-start att
)
1774 (<= (xmltok-attribute-value-start att
)
1777 (xmltok-attribute-value-end att
)))
1778 (nxml-scan-backward-in-attribute-value att
))
1779 (t (xmltok-attribute-name-start att
)))))
1780 ((and (eq xmltok-type
'processing-instruction
)
1781 (let ((content-start (save-excursion
1782 (goto-char xmltok-name-end
)
1783 (skip-chars-forward " \r\t\n")
1785 (and (< content-start
(point))
1789 (defun nxml-scan-forward-in-attribute-value (att)
1790 (when (= (point) (xmltok-attribute-value-end att
))
1791 (error "Already at end of attribute value"))
1792 (let ((refs (xmltok-attribute-refs att
))
1795 (setq ref
(car refs
))
1796 (if (< (point) (aref ref
2))
1799 (setq refs
(cdr refs
))))
1801 (xmltok-attribute-value-end att
))
1802 ((< (point) (aref ref
1))
1804 ((= (point) (aref ref
1))
1807 (let ((end (- (aref ref
2)
1808 (nxml-end-delimiter-length (aref ref
0)))))
1811 (error "Already at end of %s"
1812 (nxml-token-type-friendly-name (aref ref
0)))))))))
1814 (defun nxml-scan-backward-in-attribute-value (att)
1815 (when (= (point) (xmltok-attribute-value-start att
))
1816 (error "Already at start of attribute value"))
1817 (let ((refs (reverse (xmltok-attribute-refs att
)))
1820 (setq ref
(car refs
))
1821 (if (< (aref ref
1) (point))
1824 (setq refs
(cdr refs
))))
1826 (xmltok-attribute-value-start att
))
1827 ((< (aref ref
2) (point))
1829 ((= (point) (aref ref
2))
1832 (let ((start (+ (aref ref
1)
1833 (nxml-start-delimiter-length (aref ref
0)))))
1834 (if (< start
(point))
1836 (error "Already at start of %s"
1837 (nxml-token-type-friendly-name (aref ref
0)))))))))
1839 (defun nxml-find-following-attribute ()
1841 (atts (or xmltok-attributes xmltok-namespace-attributes
))
1842 (more-atts (and xmltok-attributes xmltok-namespace-attributes
)))
1844 (let* ((att (car atts
))
1845 (name-start (xmltok-attribute-name-start att
)))
1846 (cond ((and (<= name-start
(point))
1847 (xmltok-attribute-value-end att
)
1848 ;; <= because end is before quote
1849 (<= (point) (xmltok-attribute-value-end att
)))
1852 ((and (< (point) name-start
)
1855 (xmltok-attribute-name-start ret
))))
1857 (setq atts
(cdr atts
))
1859 (setq atts more-atts
)
1860 (setq more-atts nil
)))
1863 (defun nxml-find-preceding-attribute ()
1865 (atts (or xmltok-attributes xmltok-namespace-attributes
))
1866 (more-atts (and xmltok-attributes xmltok-namespace-attributes
)))
1868 (let* ((att (car atts
))
1869 (name-start (xmltok-attribute-name-start att
)))
1870 (cond ((and (< name-start
(point))
1871 (xmltok-attribute-value-end att
)
1872 ;; <= because end is before quote
1873 (<= (point) (xmltok-attribute-value-end att
)))
1876 ((and (< name-start
(point))
1878 (< (xmltok-attribute-name-start ret
)
1881 (setq atts
(cdr atts
))
1883 (setq atts more-atts
)
1884 (setq more-atts nil
)))
1887 (defun nxml-up-element (&optional arg
)
1889 (or arg
(setq arg
1))
1891 (nxml-backward-up-element (- arg
))
1893 (while (and (> arg
0)
1894 (< (point) (point-max)))
1895 (let ((token-end (nxml-token-after)))
1896 (goto-char (cond ((or (memq xmltok-type
'(end-tag
1898 (and (memq xmltok-type
1900 partial-empty-element
))
1901 (< xmltok-start
(point))))
1903 ((nxml-scan-element-forward
1904 (if (and (eq xmltok-type
'start-tag
)
1905 (= (point) xmltok-start
))
1909 (t (error "No parent element")))))
1910 (setq arg
(1- arg
)))
1912 (goto-char (cadr err
))
1913 (apply 'error
(cddr err
))))))
1915 (defun nxml-backward-up-element (&optional arg
)
1917 (or arg
(setq arg
1))
1919 (nxml-up-element (- arg
))
1921 (while (and (> arg
0)
1922 (< (point-min) (point)))
1923 (let ((token-end (nxml-token-before)))
1924 (goto-char (cond ((or (memq xmltok-type
'(start-tag
1926 (and (memq xmltok-type
1928 partial-empty-element
))
1929 (< (point) token-end
)))
1931 ((nxml-scan-element-backward
1932 (if (and (eq xmltok-type
'end-tag
)
1933 (= (point) token-end
))
1938 (t (error "No parent element")))))
1939 (setq arg
(1- arg
)))
1941 (goto-char (cadr err
))
1942 (apply 'error
(cddr err
))))))
1944 (defun nxml-down-element (&optional arg
)
1945 "Move forward down into the content of an element.
1946 With ARG, do this that many times.
1947 Negative ARG means move backward but still down."
1949 (or arg
(setq arg
1))
1951 (nxml-backward-down-element (- arg
))
1954 (let ((token-end (nxml-token-after)))
1956 (goto-char token-end
)
1958 (when (memq xmltok-type
'(nil end-tag partial-end-tag
))
1959 (error "No following start-tags in this element"))
1960 (not (memq xmltok-type
'(start-tag partial-start-tag
))))
1961 (nxml-tokenize-forward))
1963 (setq arg
(1- arg
)))))
1965 (defun nxml-backward-down-element (&optional arg
)
1967 (or arg
(setq arg
1))
1969 (nxml-down-element (- arg
))
1974 (goto-char xmltok-start
)
1976 (when (memq xmltok-type
'(start-tag
1980 (error "No preceding end-tags in this element"))
1981 (not (memq xmltok-type
'(end-tag partial-end-tag
))))
1982 (if (or (<= (point) nxml-prolog-end
)
1983 (not (search-backward "<" nxml-prolog-end t
)))
1984 (setq xmltok-type nil
)
1985 (nxml-move-outside-backwards)
1988 (setq arg
(1- arg
)))))
1990 (defun nxml-forward-element (&optional arg
)
1991 "Move forward over one element.
1992 With ARG, do it that many times.
1993 Negative ARG means move backward."
1995 (or arg
(setq arg
1))
1997 (nxml-backward-element (- arg
))
1999 (while (and (> arg
0)
2000 (< (point) (point-max)))
2002 (or (nxml-scan-element-forward (nxml-token-before))
2003 (error "No more elements")))
2004 (setq arg
(1- arg
)))
2006 (goto-char (cadr err
))
2007 (apply 'error
(cddr err
))))))
2009 (defun nxml-backward-element (&optional arg
)
2010 "Move backward over one element.
2011 With ARG, do it that many times.
2012 Negative ARG means move forward."
2014 (or arg
(setq arg
1))
2016 (nxml-forward-element (- arg
))
2018 (while (and (> arg
0)
2019 (< (point-min) (point)))
2021 (or (and (nxml-scan-element-backward (progn
2025 (error "No preceding elements")))
2026 (setq arg
(1- arg
)))
2028 (goto-char (cadr err
))
2029 (apply 'error
(cddr err
))))))
2031 (defun nxml-mark-token-after ()
2033 (push-mark (nxml-token-after) nil t
)
2034 (goto-char xmltok-start
)
2035 (message "Marked %s" xmltok-type
))
2039 (defun nxml-mark-paragraph ()
2040 "Put point at beginning of this paragraph, mark at end.
2041 The paragraph marked is the one that contains point or follows point."
2043 (nxml-forward-paragraph)
2045 (nxml-backward-paragraph))
2047 (defun nxml-forward-paragraph (&optional arg
)
2049 (or arg
(setq arg
1))
2051 (nxml-backward-paragraph (- arg
)))
2054 (while (and (nxml-forward-single-paragraph)
2055 (> (setq arg
(1- arg
)) 0))))))
2057 (defun nxml-backward-paragraph (&optional arg
)
2059 (or arg
(setq arg
1))
2061 (nxml-forward-paragraph (- arg
)))
2064 (let ((inhibit-field-text-motion t
))
2066 (while (and (nxml-backward-single-paragraph)
2067 (> (setq arg
(1- arg
)) 0))))))
2069 (defun nxml-forward-single-paragraph ()
2070 "Move forward over a single paragraph.
2071 Return nil at end of buffer, t otherwise."
2072 (let* ((token-end (nxml-token-after))
2073 (offset (- (point) xmltok-start
))
2075 (goto-char token-end
)
2076 (while (and (< (point) (point-max))
2078 (nxml-paragraph-end-pos had-data offset
))))
2079 (when (nxml-token-contains-data-p offset
)
2081 (nxml-tokenize-forward)
2083 (when pos
(goto-char pos
))))
2085 (defun nxml-backward-single-paragraph ()
2086 "Move backward over a single paragraph.
2087 Return nil at start of buffer, t otherwise."
2088 (let* ((token-end (nxml-token-before))
2089 (offset (- token-end
(point)))
2090 (last-tag-pos xmltok-start
)
2091 pos had-data last-data-pos
)
2092 (goto-char token-end
)
2093 (unless (setq pos
(nxml-paragraph-start-pos nil offset
))
2094 (setq had-data
(nxml-token-contains-data-p nil offset
))
2095 (goto-char xmltok-start
)
2096 (while (and (not pos
) (< (point-min) (point)))
2097 (cond ((search-backward "<" nxml-prolog-end t
)
2098 (nxml-move-outside-backwards)
2100 (while (< (point) last-tag-pos
)
2102 (when (and (not had-data
) (nxml-token-contains-data-p))
2104 (setq last-data-pos xmltok-start
))
2105 (let ((tem (nxml-paragraph-start-pos had-data
0)))
2106 (when tem
(setq pos tem
)))))
2107 (when (and (not had-data
) last-data-pos
(not pos
))
2110 (while (< (point) last-data-pos
)
2112 (let ((tem (nxml-paragraph-start-pos had-data
0)))
2113 (when tem
(setq pos tem
)))))
2114 (setq last-tag-pos
(point)))
2115 (t (goto-char (point-min))))))
2116 (when pos
(goto-char pos
))))
2118 (defun nxml-token-contains-data-p (&optional start end
)
2119 (setq start
(+ xmltok-start
(or start
0)))
2120 (setq end
(- (point) (or end
0)))
2121 (when (eq xmltok-type
'cdata-section
)
2122 (setq start
(max start
(+ xmltok-start
9)))
2123 (setq end
(min end
(- (point) 3))))
2124 (or (and (eq xmltok-type
'data
)
2125 (eq start xmltok-start
)
2127 (eq xmltok-type
'char-ref
)
2128 (and (memq xmltok-type
'(data cdata-section
))
2132 (re-search-forward "[^ \t\r\n]" end t
)))))
2134 (defun nxml-paragraph-end-pos (had-data offset
)
2135 "Return the position of the paragraph end if contained in the current token.
2136 Return nil if the current token does not contain the paragraph end.
2137 Only characters after OFFSET from the start of the token are eligible.
2138 HAD-DATA says whether there have been non-whitespace data characters yet."
2139 (cond ((not had-data
)
2140 (cond ((memq xmltok-type
'(data cdata-section
))
2142 (let ((end (point)))
2143 (goto-char (+ xmltok-start
2144 (max (if (eq xmltok-type
'cdata-section
)
2148 (and (re-search-forward "[^ \t\r\n]" end t
)
2149 (re-search-forward "^[ \t]*$" end t
)
2150 (match-beginning 0)))))
2151 ((and (eq xmltok-type
'comment
)
2152 (nxml-token-begins-line-p)
2153 (nxml-token-ends-line-p))
2155 (let ((end (point)))
2156 (goto-char (+ xmltok-start
(max 4 offset
)))
2157 (when (re-search-forward "[^ \t\r\n]" (- end
3) t
)
2158 (if (re-search-forward "^[ \t]*$" end t
)
2160 (goto-char (- end
3))
2161 (skip-chars-backward " \t")
2163 (beginning-of-line 2))
2165 ((memq xmltok-type
'(data space cdata-section
))
2167 (let ((end (point)))
2168 (goto-char (+ xmltok-start offset
))
2169 (and (re-search-forward "^[ \t]*$" end t
)
2170 (match-beginning 0)))))
2171 ((and (memq xmltok-type
'(start-tag
2175 processing-instruction
2177 (nxml-token-begins-line-p)
2178 (nxml-token-ends-line-p))
2180 (goto-char xmltok-start
)
2181 (skip-chars-backward " \t")
2183 ((and (eq xmltok-type
'end-tag
)
2184 (looking-at "[ \t]*$")
2185 (not (nxml-in-mixed-content-p t
)))
2187 (or (search-forward "\n" nil t
)
2190 (defun nxml-paragraph-start-pos (had-data offset
)
2191 "Return the position of the paragraph start if contained in the current token.
2192 Return nil if the current token does not contain the paragraph start.
2193 Only characters before OFFSET from the end of the token are eligible.
2194 HAD-DATA says whether there have been non-whitespace data characters yet."
2195 (cond ((not had-data
)
2196 (cond ((memq xmltok-type
'(data cdata-section
))
2198 (goto-char (- (point)
2199 (max (if (eq xmltok-type
'cdata-section
)
2203 (and (re-search-backward "[^ \t\r\n]" xmltok-start t
)
2204 (re-search-backward "^[ \t]*$" xmltok-start t
)
2205 (match-beginning 0))))
2206 ((and (eq xmltok-type
'comment
)
2207 (nxml-token-ends-line-p)
2208 (nxml-token-begins-line-p))
2210 (goto-char (- (point) (max 3 offset
)))
2211 (when (and (< (+ xmltok-start
4) (point))
2212 (re-search-backward "[^ \t\r\n]"
2215 (if (re-search-backward "^[ \t]*$" xmltok-start t
)
2217 (goto-char xmltok-start
)
2218 (if (looking-at "<!--[ \t]*\n")
2220 (skip-chars-backward " \t")
2222 ((memq xmltok-type
'(data space cdata-section
))
2224 (goto-char (- (point) offset
))
2225 (and (re-search-backward "^[ \t]*$" xmltok-start t
)
2226 (match-beginning 0))))
2227 ((and (memq xmltok-type
'(start-tag
2231 processing-instruction
2233 (nxml-token-ends-line-p)
2234 (nxml-token-begins-line-p))
2235 (or (search-forward "\n" nil t
)
2237 ((and (eq xmltok-type
'start-tag
)
2238 (nxml-token-begins-line-p)
2239 (not (save-excursion
2240 (goto-char xmltok-start
)
2241 (nxml-in-mixed-content-p nil
))))
2243 (goto-char xmltok-start
)
2244 (skip-chars-backward " \t")
2245 ;; include any blank line before
2246 (or (and (eq (char-before) ?
\n)
2248 (goto-char (1- (point)))
2249 (skip-chars-backward " \t")
2250 (and (bolp) (point))))
2253 (defun nxml-token-ends-line-p () (looking-at "[ \t]*$"))
2255 (defun nxml-token-begins-line-p ()
2257 (goto-char xmltok-start
)
2258 (skip-chars-backward " \t")
2261 (defun nxml-in-mixed-content-p (endp)
2262 "Return non-nil if point is in mixed content.
2263 Point must be after an end-tag or before a start-tag.
2264 ENDP is t in the former case, nil in the latter."
2265 (let (matching-tag-pos)
2266 (cond ((not (run-hook-with-args-until-failure
2267 'nxml-in-mixed-content-hook
))
2269 ;; See if the matching tag does not start or end a line.
2270 ((condition-case err
2272 (setq matching-tag-pos
2275 (and (nxml-scan-element-backward (point))
2277 (nxml-scan-element-forward (point)))))
2278 (and matching-tag-pos
2280 (goto-char matching-tag-pos
)
2283 (skip-chars-backward " \t")
2285 (looking-at "[ \t]*$"))))))
2286 (nxml-scan-error nil
))
2288 ;; See if there's data at the same level.
2291 (setq start matching-tag-pos
2294 end matching-tag-pos
))
2298 (nxml-preceding-sibling-data-p))
2301 (nxml-following-sibling-data-p)))))
2303 ;; Otherwise, treat as not mixed
2306 (defun nxml-preceding-sibling-data-p ()
2307 "Return non-nil if there is a previous sibling that is data."
2308 (let ((lim (max (- (point) nxml-mixed-scan-distance
)
2314 (while (and (< lim
(point))
2319 (search-backward "<" lim t
)))
2320 (nxml-move-outside-backwards)
2323 (let ((prev-level level
))
2324 (cond ((eq xmltok-type
'end-tag
)
2325 (setq level
(1+ level
)))
2326 ((eq xmltok-type
'start-tag
)
2327 (setq level
(1- level
))))
2328 (when (eq prev-level
0)
2329 (while (and (< (point) end
) (not found
))
2331 (when (memq xmltok-type
'(data cdata-section char-ref
))
2332 (setq found t
)))))))))
2335 (defun nxml-following-sibling-data-p ()
2336 (let ((lim (min (+ (point) nxml-mixed-scan-distance
)
2342 (while (and (< (point) lim
)
2344 (nxml-tokenize-forward)
2346 (cond ((eq xmltok-type
'start-tag
)
2347 (setq level
(1+ level
)))
2348 ((eq xmltok-type
'end-tag
)
2349 (setq level
(1- level
)))
2351 (memq xmltok-type
'(data cdata-section char-ref
)))
2357 (defun nxml-do-fill-paragraph (arg)
2358 (let (fill-paragraph-function
2362 (nxml-forward-paragraph)
2364 (nxml-backward-paragraph)
2365 (skip-chars-forward " \t\r\n")
2366 (setq start
(point))
2368 (setq fill-prefix
(buffer-substring-no-properties (point) start
))
2369 (when (and (not (nxml-get-inside (point)))
2370 (looking-at "[ \t]*<!--"))
2371 (setq fill-prefix
(concat fill-prefix
" ")))
2372 (fill-region-as-paragraph start end arg
))
2373 (skip-line-prefix fill-prefix
)
2376 (defun nxml-newline-and-indent (soft)
2377 (delete-horizontal-space)
2378 (if soft
(insert-and-inherit ?
\n) (newline 1))
2384 (defvar nxml-dynamic-markup-prev-pos nil
)
2385 (defvar nxml-dynamic-markup-prev-lengths nil
)
2386 (defvar nxml-dynamic-markup-prev-found-marker nil
)
2387 (defvar nxml-dynamic-markup-prev-start-tags
(make-hash-table :test
'equal
))
2389 (defun nxml-dynamic-markup-word ()
2390 "Dynamically markup the word before point.
2391 This attempts to find a tag to put around the word before point based
2392 on the contents of the current buffer. The end-tag will be inserted at
2393 point. The start-tag will be inserted at or before the beginning of
2394 the word before point; the contents of the current buffer is used to
2397 It works in a similar way to \\[dabbrev-expand]. It searches first
2398 backwards from point, then forwards from point for an element whose
2399 content is a string which matches the contents of the buffer before
2400 point and which includes at least the word before point. It then
2401 copies the start- and end-tags from that element and uses them to
2402 surround the matching string before point.
2404 Repeating \\[nxml-dynamic-markup-word] immediately after successful
2405 \\[nxml-dynamic-markup-word] removes the previously inserted markup
2406 and attempts to find another possible way to do the markup."
2408 (let (search-start-pos done
)
2409 (if (and (integerp nxml-dynamic-markup-prev-pos
)
2410 (= nxml-dynamic-markup-prev-pos
(point))
2411 (eq last-command this-command
)
2412 nxml-dynamic-markup-prev-lengths
)
2413 (let* ((end-tag-open-pos
2414 (- nxml-dynamic-markup-prev-pos
2415 (nth 2 nxml-dynamic-markup-prev-lengths
)))
2416 (start-tag-close-pos
2418 (nth 1 nxml-dynamic-markup-prev-lengths
)))
2420 (- start-tag-close-pos
2421 (nth 0 nxml-dynamic-markup-prev-lengths
))))
2422 (delete-region end-tag-open-pos nxml-dynamic-markup-prev-pos
)
2423 (delete-region start-tag-open-pos start-tag-close-pos
)
2424 (setq search-start-pos
2425 (marker-position nxml-dynamic-markup-prev-found-marker
)))
2426 (clrhash nxml-dynamic-markup-prev-start-tags
))
2427 (setq nxml-dynamic-markup-prev-pos nil
)
2428 (setq nxml-dynamic-markup-prev-lengths nil
)
2429 (setq nxml-dynamic-markup-prev-found-marker nil
)
2432 (let* ((pos (point))
2435 (unless (< (point) pos
)
2436 (error "No word to markup"))
2437 (buffer-substring-no-properties (point) pos
)))
2438 (search (concat word
"</"))
2440 (when search-start-pos
2441 (goto-char search-start-pos
))
2442 (while (and (not done
)
2443 (or (and (< (point) pos
)
2444 (or (search-backward search nil t
)
2445 (progn (goto-char pos
) nil
)))
2446 (search-forward search nil t
)))
2447 (goto-char (- (match-end 0) 2))
2448 (setq done
(nxml-try-copy-markup pos
)))
2450 (error (if (zerop (hash-table-count
2451 nxml-dynamic-markup-prev-start-tags
))
2452 "No possible markup found for `%s'"
2453 "No more markup possibilities found for `%s'")
2456 (defun nxml-try-copy-markup (word-end-pos)
2458 (let ((end-tag-pos (point)))
2459 (when (and (not (nxml-get-inside end-tag-pos
))
2460 (search-backward "<" nil t
)
2461 (not (nxml-get-inside (point))))
2463 (when (and (eq xmltok-type
'start-tag
)
2464 (< (point) end-tag-pos
))
2465 (let* ((start-tag-close-pos (point))
2467 (buffer-substring-no-properties xmltok-start
2468 start-tag-close-pos
))
2472 (buffer-substring-no-properties start-tag-close-pos
2475 (goto-char word-end-pos
)
2477 (re-search-backward (concat
2478 (regexp-quote (car words
))
2482 (setq words
(cdr words
))
2483 (skip-chars-backward " \t\r\n"))
2484 (when (and (not words
)
2486 (skip-chars-forward " \t\r\n")
2487 (not (gethash (cons (point) start-tag
)
2488 nxml-dynamic-markup-prev-start-tags
)))
2489 (or (< end-tag-pos
(point))
2490 (< word-end-pos xmltok-start
)))
2491 (setq nxml-dynamic-markup-prev-found-marker
2492 (copy-marker end-tag-pos t
))
2493 (puthash (cons (point) start-tag
)
2495 nxml-dynamic-markup-prev-start-tags
)
2496 (setq nxml-dynamic-markup-prev-lengths
2497 (list (- start-tag-close-pos xmltok-start
)
2498 (- word-end-pos
(point))
2499 (+ (- xmltok-name-end xmltok-start
) 2)))
2500 (let ((name (xmltok-start-tag-qname)))
2502 (goto-char (+ word-end-pos
2503 (- start-tag-close-pos xmltok-start
)))
2504 (insert "</" name
">")
2505 (setq nxml-dynamic-markup-prev-pos
(point))))))))))
2510 (defvar nxml-char-name-ignore-case t
)
2512 (defvar nxml-char-name-alist nil
2513 "Alist of character names.
2514 Each member of the list has the form (NAME CODE . NAMESET),
2515 where NAME is a string naming a character, NAMESET is a symbol
2516 identifying a set of names and CODE is an integer specifying the
2517 Unicode scalar value of the named character.
2518 The NAME will only be used for completion if NAMESET has
2519 a non-nil `nxml-char-name-set-enabled' property.
2520 If NAMESET does does not have `nxml-char-name-set-defined' property,
2521 then it must have a `nxml-char-name-set-file' property and `load'
2522 will be applied to the value of this property if the nameset
2525 (defvar nxml-char-name-table
(make-hash-table :test
'eq
)
2526 "Hash table for mapping char codes to names.
2527 Each key is a Unicode scalar value.
2528 Each value is a list of pairs of the form (NAMESET . NAME),
2529 where NAMESET is a symbol identifying a set of names,
2530 and NAME is a string naming a character.")
2532 (defvar nxml-autoload-char-name-set-list nil
2533 "List of char namesets that can be autoloaded.")
2535 (defun nxml-enable-char-name-set (nameset)
2536 (put nameset
'nxml-char-name-set-enabled t
))
2538 (defun nxml-disable-char-name-set (nameset)
2539 (put nameset
'nxml-char-name-set-enabled nil
))
2541 (defun nxml-char-name-set-enabled-p (nameset)
2542 (get nameset
'nxml-char-name-set-enabled
))
2544 (defun nxml-autoload-char-name-set (nameset file
)
2545 (unless (memq nameset nxml-autoload-char-name-set-list
)
2546 (setq nxml-autoload-char-name-set-list
2547 (cons nameset nxml-autoload-char-name-set-list
)))
2548 (put nameset
'nxml-char-name-set-file file
))
2550 (defun nxml-define-char-name-set (nameset alist
)
2551 "Define a set of character names.
2552 NAMESET is a symbol identifying the set.
2553 ALIST is a list where each member has the form (NAME CODE),
2554 where NAME is a string naming a character and code is an
2555 integer giving the Unicode scalar value of the character."
2556 (when (get nameset
'nxml-char-name-set-defined
)
2557 (error "Nameset `%s' already defined" nameset
))
2560 (let* ((name-code (car iter
))
2561 (name (car name-code
))
2562 (code (cadr name-code
)))
2564 (cons (cons nameset name
)
2565 (gethash code nxml-char-name-table
))
2566 nxml-char-name-table
))
2567 (setcdr (cdr (car iter
)) nameset
)
2568 (setq iter
(cdr iter
))))
2569 (setq nxml-char-name-alist
2570 (nconc alist nxml-char-name-alist
))
2571 (put nameset
'nxml-char-name-set-defined t
))
2573 (defun nxml-get-char-name (code)
2574 (mapc 'nxml-maybe-load-char-name-set nxml-autoload-char-name-set-list
)
2575 (let ((names (gethash code nxml-char-name-table
))
2577 (while (and names
(not name
))
2578 (if (nxml-char-name-set-enabled-p (caar names
))
2579 (setq name
(cdar names
))
2580 (setq names
(cdr names
))))
2583 (defvar nxml-named-char-history nil
)
2585 (defun nxml-insert-named-char (arg)
2586 "Insert a character using its name.
2587 The name is read from the minibuffer.
2588 Normally, inserts the character as a numeric character reference.
2589 With a prefix argument, inserts the character directly."
2591 (mapc 'nxml-maybe-load-char-name-set nxml-autoload-char-name-set-list
)
2593 (let ((completion-ignore-case nxml-char-name-ignore-case
))
2594 (completing-read "Character name: "
2595 nxml-char-name-alist
2597 (get (cddr member
) 'nxml-char-name-set-enabled
))
2600 'nxml-named-char-history
)))
2601 (alist nxml-char-name-alist
)
2603 (while (and alist
(not code
))
2604 (setq elt
(assoc name alist
))
2605 (if (get (cddr elt
) 'nxml-char-name-set-enabled
)
2606 (setq code
(cadr elt
))
2607 (setq alist
(cdr (member elt alist
)))))
2610 (or (decode-char 'ucs code
)
2611 (error "Character %x is not supported by Emacs"
2613 (format "&#x%X;" code
))))))
2615 (defun nxml-maybe-load-char-name-set (sym)
2616 (when (and (get sym
'nxml-char-name-set-enabled
)
2617 (not (get sym
'nxml-char-name-set-defined
))
2618 (stringp (get sym
'nxml-char-name-set-file
)))
2619 (load (get sym
'nxml-char-name-set-file
))))
2621 (defun nxml-toggle-char-ref-extra-display (arg)
2622 "Toggle the display of extra information for character references."
2624 (let ((new (if (null arg
)
2625 (not nxml-char-ref-extra-display
)
2626 (> (prefix-numeric-value arg
) 0))))
2627 (when (not (eq new nxml-char-ref-extra-display
))
2628 (setq nxml-char-ref-extra-display new
)
2629 (font-lock-fontify-buffer))))
2631 (put 'nxml-char-ref
'evaporate t
)
2633 (defun nxml-char-ref-display-extra (start end n
)
2634 (when nxml-char-ref-extra-display
2635 (let ((name (nxml-get-char-name n
))
2636 (glyph-string (and nxml-char-ref-display-glyph-flag
2637 (nxml-glyph-display-string n
'nxml-glyph
)))
2639 (when (or name glyph-string
)
2640 (setq ov
(make-overlay start end nil t
))
2641 (overlay-put ov
'category
'nxml-char-ref
)
2643 (overlay-put ov
'help-echo name
))
2647 (propertize glyph-string
'face
'nxml-glyph
)))))))
2649 (defun nxml-clear-char-ref-extra-display (start end
)
2650 (let ((ov (overlays-in start end
)))
2652 (when (eq (overlay-get (car ov
) 'category
) 'nxml-char-ref
)
2653 (delete-overlay (car ov
)))
2654 (setq ov
(cdr ov
)))))
2657 (defun nxml-start-delimiter-length (type)
2658 (or (get type
'nxml-start-delimiter-length
)
2661 (put 'cdata-section
'nxml-start-delimiter-length
9)
2662 (put 'comment
'nxml-start-delimiter-length
4)
2663 (put 'processing-instruction
'nxml-start-delimiter-length
2)
2664 (put 'start-tag
'nxml-start-delimiter-length
1)
2665 (put 'empty-element
'nxml-start-delimiter-length
1)
2666 (put 'partial-empty-element
'nxml-start-delimiter-length
1)
2667 (put 'entity-ref
'nxml-start-delimiter-length
1)
2668 (put 'char-ref
'nxml-start-delimiter-length
2)
2670 (defun nxml-end-delimiter-length (type)
2671 (or (get type
'nxml-end-delimiter-length
)
2674 (put 'cdata-section
'nxml-end-delimiter-length
3)
2675 (put 'comment
'nxml-end-delimiter-length
3)
2676 (put 'processing-instruction
'nxml-end-delimiter-length
2)
2677 (put 'start-tag
'nxml-end-delimiter-length
1)
2678 (put 'empty-element
'nxml-end-delimiter-length
2)
2679 (put 'partial-empty-element
'nxml-end-delimiter-length
1)
2680 (put 'entity-ref
'nxml-end-delimiter-length
1)
2681 (put 'char-ref
'nxml-end-delimiter-length
1)
2683 (defun nxml-token-type-friendly-name (type)
2684 (or (get type
'nxml-friendly-name
)
2685 (symbol-name type
)))
2687 (put 'cdata-section
'nxml-friendly-name
"CDATA section")
2688 (put 'processing-instruction
'nxml-friendly-name
"processing instruction")
2689 (put 'entity-ref
'nxml-friendly-name
"entity reference")
2690 (put 'char-ref
'nxml-friendly-name
"character reference")
2693 (defalias 'xml-mode
'nxml-mode
)
2695 (provide 'nxml-mode
)
2697 ;;; nxml-mode.el ends here