Followup to last change
[emacs.git] / lisp / nxml / nxml-mode.el
bloba9298be4fb409d96f8804d1e9df4db5cee70a785
1 ;;; nxml-mode.el --- a new XML mode -*- lexical-binding:t -*-
3 ;; Copyright (C) 2003-2004, 2007-2018 Free Software Foundation, Inc.
5 ;; Author: James Clark
6 ;; Keywords: wp, hypermedia, languages, XML
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 <https://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; See nxml-rap.el for description of parsing strategy.
27 ;;; Code:
29 (eval-when-compile (require 'cl-lib))
31 (require 'xmltok)
32 (require 'nxml-enc)
33 (require 'nxml-util)
34 (require 'nxml-rap)
35 (require 'nxml-outln)
36 ;; nxml-mode calls rng-nxml-mode-init, which is autoloaded from rng-nxml.
37 ;; So we might as well just require it and silence the compiler.
38 (provide 'nxml-mode) ; avoid recursive require
39 (require 'rng-nxml)
40 (require 'sgml-mode)
42 ;;; Customization
44 (defgroup nxml nil
45 "New XML editing mode."
46 :link '(custom-manual "(nxml-mode) Top")
47 :group 'languages)
49 (defgroup nxml-faces nil
50 "Faces for XML syntax highlighting."
51 :group 'nxml)
53 (defcustom nxml-char-ref-display-glyph-flag t
54 "Non-nil means display glyph following character reference.
55 The glyph is displayed in face `nxml-glyph'."
56 :group 'nxml
57 :type 'boolean)
59 (defcustom nxml-sexp-element-flag t
60 "Non-nil means sexp commands treat an element as a single expression."
61 :group 'nxml
62 :type 'boolean)
64 (defcustom nxml-slash-auto-complete-flag nil
65 "Non-nil means typing a slash automatically completes the end-tag.
66 This is used by `nxml-electric-slash'."
67 :group 'nxml
68 :type 'boolean)
70 (defcustom nxml-child-indent 2
71 "Indentation for the children of an element relative to the start-tag.
72 This only applies when the line or lines containing the start-tag contains
73 nothing else other than that start-tag."
74 :group 'nxml
75 :type 'integer)
77 (defcustom nxml-attribute-indent 4
78 "Indentation for the attributes of an element relative to the start-tag.
79 This only applies when the first attribute of a tag starts a line.
80 In other cases, the first attribute on one line is indented the same
81 as the first attribute on the previous line."
82 :group 'nxml
83 :type 'integer)
85 (defcustom nxml-bind-meta-tab-to-complete-flag t
86 "Non-nil means to use nXML completion in \\[completion-at-point]."
87 :group 'nxml
88 :type 'boolean)
90 (defcustom nxml-prefer-utf-16-to-utf-8-flag nil
91 "Non-nil means prefer UTF-16 to UTF-8 when saving a buffer.
92 This is used only when a buffer does not contain an encoding declaration
93 and when its current `buffer-file-coding-system' specifies neither UTF-16
94 nor UTF-8."
95 :group 'nxml
96 :type 'boolean)
98 (defcustom nxml-prefer-utf-16-little-to-big-endian-flag (eq system-type
99 'windows-nt)
100 "Non-nil means prefer little-endian to big-endian byte-order for UTF-16.
101 This is used only for saving a buffer; when reading the byte-order is
102 auto-detected. It may be relevant both when there is no encoding declaration
103 and when the encoding declaration specifies `UTF-16'."
104 :group 'nxml
105 :type 'boolean)
107 (defcustom nxml-default-buffer-file-coding-system nil
108 "Default value for `buffer-file-coding-system' for a buffer for a new file.
109 A value of nil means use the default value of `buffer-file-coding-system' as normal.
110 A buffer's `buffer-file-coding-system' affects what \\[nxml-insert-xml-declaration] inserts."
111 :group 'nxml
112 :type 'coding-system)
114 (defcustom nxml-auto-insert-xml-declaration-flag nil
115 "Non-nil means automatically insert an XML declaration in a new file.
116 The XML declaration is inserted using `nxml-insert-xml-declaration'."
117 :group 'nxml
118 :type 'boolean)
120 (defface nxml-delimited-data
121 '((t (:inherit font-lock-doc-face)))
122 "Face used to highlight data enclosed between delimiters.
123 This is not used directly, but only via inheritance by other faces."
124 :group 'nxml-faces)
126 (defface nxml-name
127 '((t (:inherit font-lock-builtin-face)))
128 "Face used to highlight various names.
129 This includes element and attribute names, processing
130 instruction targets and the CDATA keyword in a CDATA section.
131 This is not used directly, but only via inheritance by other faces."
132 :group 'nxml-faces)
134 (defface nxml-ref
135 '((t (:inherit font-lock-constant-face)))
136 "Face used to highlight character and entity references.
137 This is not used directly, but only via inheritance by other faces."
138 :group 'nxml-faces)
140 (defface nxml-delimiter
142 "Face used to highlight delimiters.
143 This is not used directly, but only via inheritance by other faces."
144 :group 'nxml-faces)
146 (defface nxml-text
148 "Face used to highlight text."
149 :group 'nxml-faces)
151 (defface nxml-processing-instruction-delimiter
152 '((t (:inherit nxml-delimiter)))
153 "Face used for the delimiters of processing instructions, i.e., <? and ?>."
154 :group 'nxml-faces)
156 (defface nxml-processing-instruction-target
157 '((t (:inherit font-lock-keyword-face)))
158 "Face used for the target of processing instructions."
159 :group 'nxml-faces)
161 (defface nxml-processing-instruction-content
162 '((t (:inherit nxml-delimited-data)))
163 "Face used for the content of processing instructions."
164 :group 'nxml-faces)
166 (defface nxml-cdata-section-delimiter
167 '((t (:inherit nxml-delimiter)))
168 "Face used for the delimiters of CDATA sections, i.e., <![, [, and ]]>."
169 :group 'nxml-faces)
171 (defface nxml-cdata-section-CDATA
172 '((t (:inherit nxml-name)))
173 "Face used for the CDATA keyword in CDATA sections."
174 :group 'nxml-faces)
176 (defface nxml-cdata-section-content
177 '((t (:inherit nxml-text)))
178 "Face used for the content of CDATA sections."
179 :group 'nxml-faces)
181 (defface nxml-char-ref-number
182 '((t (:inherit nxml-ref)))
183 "Face used for the number in character references.
184 This includes ths `x' in hex references."
185 :group 'nxml-faces)
187 (defface nxml-char-ref-delimiter
188 '((t (:inherit nxml-ref)))
189 "Face used for the delimiters of character references, i.e., &# and ;."
190 :group 'nxml-faces)
192 (defface nxml-entity-ref-name
193 '((t (:inherit nxml-ref)))
194 "Face used for the entity name in general entity references."
195 :group 'nxml-faces)
197 (defface nxml-entity-ref-delimiter
198 '((t (:inherit nxml-ref)))
199 "Face used for the delimiters of entity references, i.e., & and ;."
200 :group 'nxml-faces)
202 (defface nxml-tag-delimiter
203 '((t (:inherit nxml-delimiter)))
204 "Face used for the angle brackets delimiting tags.
205 `nxml-tag-slash' is used for slashes."
206 :group 'nxml-faces)
208 (defface nxml-tag-slash
209 '((t (:inherit nxml-tag-delimiter)))
210 "Face used for slashes in tags, both in end-tags and empty-elements."
211 :group 'nxml-faces)
213 (defface nxml-element-prefix
214 '((t (:inherit nxml-name)))
215 "Face used for the prefix of elements."
216 :group 'nxml-faces)
218 (defface nxml-element-colon
220 "Face used for the colon in element names."
221 :group 'nxml-faces)
223 (defface nxml-element-local-name
224 '((t (:inherit font-lock-function-name-face)))
225 "Face used for the local name of elements."
226 :group 'nxml-faces)
228 (defface nxml-attribute-prefix
229 '((t (:inherit nxml-name)))
230 "Face used for the prefix of attributes."
231 :group 'nxml-faces)
233 (defface nxml-attribute-colon
234 '((t (:inherit nxml-delimiter)))
235 "Face used for the colon in attribute names."
236 :group 'nxml-faces)
238 (defface nxml-attribute-local-name
239 '((t (:inherit font-lock-variable-name-face)))
240 "Face used for the local name of attributes."
241 :group 'nxml-faces)
243 (defface nxml-namespace-attribute-xmlns
244 '((t (:inherit nxml-attribute-prefix)))
245 "Face used for `xmlns' in namespace attributes."
246 :group 'nxml-faces)
248 (defface nxml-namespace-attribute-colon
249 '((t (:inherit nxml-attribute-colon)))
250 "Face used for the colon in namespace attributes."
251 :group 'nxml-faces)
253 (defface nxml-namespace-attribute-prefix
254 '((t (:inherit nxml-attribute-local-name)))
255 "Face used for the prefix declared in namespace attributes."
256 :group 'nxml-faces)
258 (defface nxml-attribute-value
259 '((t (:inherit font-lock-string-face)))
260 "Face used for the value of attributes."
261 :group 'nxml-faces)
263 (defface nxml-attribute-value-delimiter
264 '((t (:inherit nxml-attribute-value)))
265 "Face used for the delimiters of attribute values."
266 :group 'nxml-faces)
269 (defface nxml-prolog-literal-delimiter
270 '((t (:inherit nxml-delimited-data)))
271 "Face used for the delimiters of literals in the prolog."
272 :group 'nxml-faces)
274 (defface nxml-prolog-literal-content
275 '((t (:inherit nxml-delimited-data)))
276 "Face used for the content of literals in the prolog."
277 :group 'nxml-faces)
279 (defface nxml-prolog-keyword
280 '((t (:inherit font-lock-keyword-face)))
281 "Face used for keywords in the prolog."
282 :group 'nxml-faces)
284 (defface nxml-markup-declaration-delimiter
285 '((t (:inherit nxml-delimiter)))
286 "Face used for the delimiters of markup declarations in the prolog.
287 The delimiters are <! and >."
288 :group 'nxml-faces)
290 (defface nxml-hash
291 '((t (:inherit nxml-name)))
292 "Face used for # before a name in the prolog."
293 :group 'nxml-faces)
295 (defface nxml-glyph
296 '((((type x))
297 (:family
298 "misc-fixed"
299 :background
300 "light grey"
301 :foreground
302 "black"
303 :weight
304 normal
305 :slant
306 normal))
308 (:background
309 "light grey"
310 :foreground
311 "black"
312 :weight
313 normal
314 :slant
315 normal)))
316 "Face used for glyph for char references."
317 :group 'nxml-faces)
319 ;;; Global variables
321 (defvar-local nxml-parent-document nil
322 "The parent document for a part of a modular document.
323 Use `nxml-parent-document-set' to set it.")
324 (put 'nxml-parent-document 'safe-local-variable 'stringp)
326 (defvar-local nxml-prolog-regions nil
327 "List of regions in the prolog to be fontified.
328 See the function `xmltok-forward-prolog' for more information.")
330 (defvar-local nxml-degraded nil
331 "Non-nil if currently operating in degraded mode.
332 Degraded mode is enabled when an internal error is encountered in the
333 fontification or after-change functions.")
335 (defvar nxml-completion-hook nil
336 "Hook run by `nxml-complete'.
337 This hook is run until success.")
339 (defvar nxml-in-mixed-content-hook nil
340 "Hook to determine whether point is in mixed content.
341 The hook is called without arguments. It should return nil if it is
342 definitely not mixed; non-nil otherwise. The hook will be run until
343 one of the functions returns nil.")
345 (defvar nxml-mixed-scan-distance 4000
346 "Maximum distance from point to scan when checking for mixed content.")
348 (defvar nxml-end-tag-indent-scan-distance 4000
349 "Maximum distance from point to scan backwards when indenting end-tag.")
351 (defvar-local nxml-char-ref-extra-display t
352 "Non-nil means display extra information for character references.
353 The extra information consists of a tooltip with the character name
354 and, if `nxml-char-ref-display-glyph-flag' is non-nil, a glyph
355 corresponding to the referenced character following the character
356 reference.")
358 (defvar nxml-mode-map
359 (let ((map (make-sparse-keymap)))
360 (define-key map "\M-\C-u" 'nxml-backward-up-element)
361 (define-key map "\M-\C-d" 'nxml-down-element)
362 (define-key map "\M-\C-n" 'nxml-forward-element)
363 (define-key map "\M-\C-p" 'nxml-backward-element)
364 (define-key map "\M-{" 'nxml-backward-paragraph)
365 (define-key map "\M-}" 'nxml-forward-paragraph)
366 (define-key map "\M-h" 'nxml-mark-paragraph)
367 (define-key map "\C-c\C-f" 'nxml-finish-element)
368 (define-key map "\C-c]" 'nxml-finish-element)
369 (define-key map "\C-c/" 'nxml-finish-element)
370 (define-key map "\C-c\C-m" 'nxml-split-element)
371 (define-key map "\C-c\C-b" 'nxml-balanced-close-start-tag-block)
372 (define-key map "\C-c\C-i" 'nxml-balanced-close-start-tag-inline)
373 (define-key map "\C-c\C-x" 'nxml-insert-xml-declaration)
374 (define-key map "\C-c\C-d" 'nxml-dynamic-markup-word)
375 ;; u is for Unicode
376 (define-key map "\C-c\C-u" 'nxml-insert-named-char)
377 (define-key map "\C-c\C-o" nxml-outline-prefix-map)
378 (define-key map [S-mouse-2] 'nxml-mouse-hide-direct-text-content)
379 (define-key map "/" 'nxml-electric-slash)
380 (define-key map "\M-\t" 'completion-at-point)
381 map)
382 "Keymap for nxml-mode.")
384 (defvar nxml-font-lock-keywords
385 '(nxml-fontify-matcher)
386 "Default font lock keywords for nxml-mode.")
388 (defsubst nxml-set-face (start end face)
389 (when (and face (< start end))
390 ;; Prepend, so the character reference highlighting takes precedence over
391 ;; the string highlighting applied syntactically.
392 (font-lock-prepend-text-property start end 'face face)))
394 (defun nxml-parent-document-set (parent-document)
395 "Set `nxml-parent-document' and inherit the DTD &c."
396 ;; FIXME: this does not work.
397 ;; the idea is that by inheriting some variables from the parent,
398 ;; `rng-validate-mode' will validate entities declared in the parent.
399 ;; alas, the most interesting variables (`rng-compile-table' et al)
400 ;; are circular and cannot be printed even with `print-circle'.
401 (interactive "fParent document")
402 (let (dtd current-schema current-schema-file-name compile-table
403 ipattern-table last-ipattern-index)
404 (when (string= (file-truename parent-document)
405 (file-truename buffer-file-name))
406 (error "Parent document cannot be the same as the document"))
407 (with-current-buffer (find-file-noselect parent-document)
408 (setq dtd rng-dtd
409 current-schema rng-current-schema
410 current-schema-file-name rng-current-schema-file-name
411 compile-table rng-compile-table
412 ipattern-table rng-ipattern-table
413 last-ipattern-index rng-last-ipattern-index
414 parent-document buffer-file-name))
415 (setq rng-dtd dtd
416 rng-current-schema current-schema
417 rng-current-schema-file-name current-schema-file-name
418 rng-compile-table compile-table
419 rng-ipattern-table ipattern-table
420 rng-last-ipattern-index last-ipattern-index
421 nxml-parent-document parent-document)
422 (message "Set parent document to %s" parent-document)
423 (when rng-validate-mode
424 (rng-validate-while-idle (current-buffer)))))
426 (defvar tildify-space-string)
427 (defvar tildify-foreach-region-function)
429 ;;;###autoload
430 (define-derived-mode nxml-mode text-mode "nXML"
431 ;; We use C-c C-i instead of \\[nxml-balanced-close-start-tag-inline]
432 ;; because Emacs turns C-c C-i into C-c TAB which is hard to type and
433 ;; not mnemonic.
434 "Major mode for editing XML.
436 \\[nxml-finish-element] finishes the current element by inserting an end-tag.
437 C-c C-i closes a start-tag with `>' and then inserts a balancing end-tag
438 leaving point between the start-tag and end-tag.
439 \\[nxml-balanced-close-start-tag-block] is similar but for block rather than inline elements:
440 the start-tag, point, and end-tag are all left on separate lines.
441 If `nxml-slash-auto-complete-flag' is non-nil, then inserting a `</'
442 automatically inserts the rest of the end-tag.
444 \\[completion-at-point] performs completion on the symbol preceding point.
446 \\[nxml-dynamic-markup-word] uses the contents of the current buffer
447 to choose a tag to put around the word preceding point.
449 Sections of the document can be displayed in outline form. The
450 variable `nxml-section-element-name-regexp' controls when an element
451 is recognized as a section. The same key sequences that change
452 visibility in outline mode are used except that they start with C-c C-o
453 instead of C-c.
455 Validation is provided by the related minor-mode `rng-validate-mode'.
456 This also makes completion schema- and context- sensitive. Element
457 names, attribute names, attribute values and namespace URIs can all be
458 completed. By default, `rng-validate-mode' is automatically enabled.
459 You can toggle it using \\[rng-validate-mode] or change the default by
460 customizing `rng-nxml-auto-validate-flag'.
462 \\[indent-for-tab-command] indents the current line appropriately.
463 This can be customized using the variable `nxml-child-indent'
464 and the variable `nxml-attribute-indent'.
466 \\[nxml-insert-named-char] inserts a character reference using
467 the character's name (by default, the Unicode name).
468 \\[universal-argument] \\[nxml-insert-named-char] inserts the character directly.
470 The Emacs commands that normally operate on balanced expressions will
471 operate on XML markup items. Thus \\[forward-sexp] will move forward
472 across one markup item; \\[backward-sexp] will move backward across
473 one markup item; \\[kill-sexp] will kill the following markup item;
474 \\[mark-sexp] will mark the following markup item. By default, the
475 complete element is treated as a single markup item; to make each tag be
476 treated as a separate markup item, set the variable `nxml-sexp-element-flag'
477 to nil. For more details, see the function `nxml-forward-balanced-item'.
479 \\[nxml-backward-up-element] and \\[nxml-down-element] move up and down the element structure.
481 Many aspects this mode can be customized using
482 \\[customize-group] nxml RET."
483 ;; (kill-all-local-variables)
484 ;; If encoding does not allow non-break space character, use reference.
485 ;; FIXME: This duplicates code from sgml-mode, perhaps derive from it?
486 ;; FIXME: Perhaps use &nbsp; if possible (e.g. XHTML)?
487 (setq-local tildify-space-string
488 (if (equal (decode-coding-string
489 (encode-coding-string " " buffer-file-coding-system)
490 buffer-file-coding-system) " ")
491 " " "&#160;"))
492 ;; FIXME: Use the fact that we're parsing the document already
493 ;; rather than using regex-based filtering.
494 (setq-local tildify-foreach-region-function
495 (apply-partially 'tildify-foreach-ignore-environments
496 '(("<! *--" . "-- *>") ("<" . ">"))))
497 (setq-local mode-line-process '((nxml-degraded "/degraded")))
498 ;; We'll determine the fill prefix ourselves
499 (setq-local adaptive-fill-mode nil)
500 (setq-local forward-sexp-function #'nxml-forward-balanced-item)
501 (setq-local indent-line-function #'nxml-indent-line)
502 (setq-local fill-paragraph-function #'nxml-do-fill-paragraph)
503 ;; Comment support
504 ;; This doesn't seem to work too well;
505 ;; I think we should probably roll our own nxml-comment-dwim function.
506 (setq-local comment-indent-function #'nxml-indent-line)
507 (setq-local comment-start "<!--")
508 (setq-local comment-start-skip "<!--[ \t\r\n]*")
509 (setq-local comment-end "-->")
510 (setq-local comment-end-skip "[ \t\r\n]*-->")
511 (setq-local comment-line-break-function #'nxml-newline-and-indent)
512 (setq-local comment-quote-nested-function #'nxml-comment-quote-nested)
513 (save-excursion
514 (save-restriction
515 (widen)
516 (with-silent-modifications
517 (nxml-with-invisible-motion
518 (nxml-scan-prolog)))))
519 (setq-local syntax-ppss-table sgml-tag-syntax-table)
520 (setq-local syntax-propertize-function #'sgml-syntax-propertize)
521 (add-hook 'change-major-mode-hook #'nxml-cleanup nil t)
523 ;; Emacs 23 handles the encoding attribute on the xml declaration
524 ;; transparently to nxml-mode, so there is no longer a need for the below
525 ;; hook. The hook also had the drawback of overriding explicit user
526 ;; instruction to save as some encoding other than utf-8.
527 ;;(add-hook 'write-contents-hooks #'nxml-prepare-to-save)
528 (when (not (and (buffer-file-name) (file-exists-p (buffer-file-name))))
529 (when (and nxml-default-buffer-file-coding-system
530 (not (local-variable-p 'buffer-file-coding-system)))
531 (setq buffer-file-coding-system nxml-default-buffer-file-coding-system))
532 (when nxml-auto-insert-xml-declaration-flag
533 (nxml-insert-xml-declaration)))
535 (setq font-lock-defaults
536 '(nxml-font-lock-keywords
537 nil ; highlight comments and strings based on syntax-tables
538 nil ; font-lock-keywords-case-fold-search. XML is case sensitive
539 nil ; no special syntax table
540 (font-lock-extend-region-functions . (nxml-extend-region))
541 (jit-lock-contextually . t)
542 (font-lock-unfontify-region-function . nxml-unfontify-region)))
544 (with-demoted-errors (rng-nxml-mode-init)))
546 (defun nxml-cleanup ()
547 "Clean up after nxml-mode."
548 ;; Disable associated minor modes.
549 (rng-validate-mode -1)
550 ;; Clean up fontification.
551 (save-excursion
552 (widen)
553 (with-silent-modifications
554 (nxml-with-invisible-motion
555 (remove-text-properties (point-min) (point-max) '(face)))))
556 (remove-hook 'change-major-mode-hook #'nxml-cleanup t))
558 (defun nxml-degrade (context err)
559 (message "Internal nXML mode error in %s (%s), degrading"
560 context
561 (error-message-string err))
562 (ding)
563 (setq nxml-degraded t)
564 (setq nxml-prolog-end 1))
566 ;;; Change management
568 (defvar font-lock-beg) (defvar font-lock-end)
569 (defun nxml-debug-region (start end)
570 (interactive "r")
571 (let ((font-lock-beg start)
572 (font-lock-end end))
573 (nxml-extend-region)
574 (goto-char font-lock-beg)
575 (set-mark font-lock-end)))
577 ;;; Encodings
579 (defun nxml-insert-xml-declaration ()
580 "Insert an XML declaration at the beginning of buffer.
581 The XML declaration will declare an encoding depending on the buffer's
582 `buffer-file-coding-system'."
583 (interactive "*")
584 (let ((coding-system
585 (if (and buffer-file-coding-system
586 (coding-system-p buffer-file-coding-system)
587 (coding-system-get buffer-file-coding-system
588 'mime-charset))
589 buffer-file-coding-system
590 (nxml-choose-utf-coding-system))))
591 (goto-char (point-min))
592 (insert (format "<?xml version=\"1.0\" encoding=\"%s\"?>\n"
593 (nxml-coding-system-name coding-system)))))
595 (defun nxml-prepare-to-save ()
596 (unless (and (not enable-multibyte-characters)
597 (local-variable-p 'buffer-file-coding-system)
598 buffer-file-coding-system
599 (or (eq (coding-system-type buffer-file-coding-system) 5)
600 (eq buffer-file-coding-system 'no-conversion)))
601 (save-excursion
602 (setq buffer-file-coding-system (nxml-select-coding-system))))
603 ;; nil from a function in `write-contents-hooks' means
604 ;; to continue and write the file as normal
605 nil)
607 (defun nxml-select-coding-system ()
608 (let* ((suitable-coding-systems
609 (find-coding-systems-region (point-min) (point-max)))
610 (enc-pos (progn
611 (goto-char (point-min))
612 (xmltok-get-declared-encoding-position)))
613 (enc-name
614 (and (consp enc-pos)
615 (buffer-substring-no-properties (car enc-pos)
616 (cdr enc-pos))))
617 (coding-system
618 (cond (enc-name
619 (if (string= (downcase enc-name) "utf-16")
620 (nxml-choose-utf-16-coding-system)
621 (nxml-mime-charset-coding-system enc-name)))
622 (enc-pos (nxml-choose-utf-coding-system)))))
623 ;; Make sure we have a coding-system
624 (unless coding-system
625 (setq coding-system
626 (and (not buffer-read-only)
627 (nxml-choose-suitable-coding-system
628 suitable-coding-systems)))
629 (let ((message
630 (if enc-name
631 (format "Unknown encoding %s" enc-name)
632 "XML declaration is not well-formed")))
633 (cond ((not coding-system)
634 (error "%s" message))
635 ((y-or-n-p
636 (concat message
637 ". "
638 (format (if enc-name
639 "Save with %s"
640 "Modify and save with encoding %s")
641 (nxml-coding-system-name coding-system))
642 " "))
643 (nxml-fix-encoding-declaration enc-pos coding-system))
644 (t (signal 'quit nil)))))
645 ;; Make sure it can encode all the characters in the buffer
646 (unless (or (memq (coding-system-base coding-system)
647 suitable-coding-systems)
648 (equal suitable-coding-systems '(undecided)))
649 (let ((message
650 (nxml-unsuitable-coding-system-message coding-system
651 enc-name)))
652 (setq coding-system
653 (and (not buffer-read-only)
654 (nxml-choose-suitable-coding-system
655 suitable-coding-systems)))
656 (cond ((not coding-system) (error "%s" message))
657 ((y-or-n-p (concat message
658 (format ". Save with %s "
659 (nxml-coding-system-name
660 coding-system))))
661 (nxml-fix-encoding-declaration enc-pos coding-system))
662 (t (signal 'quit nil)))))
663 ;; Merge the newline type of our existing encoding
664 (let ((current-eol-type
665 (coding-system-eol-type buffer-file-coding-system)))
666 (when (and current-eol-type (integerp current-eol-type))
667 (setq coding-system
668 (coding-system-change-eol-conversion coding-system
669 current-eol-type))))
670 coding-system))
672 (defun nxml-unsuitable-coding-system-message (coding-system &optional enc-name)
673 (if (nxml-coding-system-unicode-p coding-system)
674 "Cannot translate some characters to Unicode"
675 (format "Cannot encode some characters with %s"
676 (or enc-name
677 (nxml-coding-system-name coding-system)))))
679 (defconst nxml-utf-16-coding-systems (and (coding-system-p 'utf-16-be)
680 (coding-system-p 'utf-16-le)
681 '(utf-16-be utf-16-le)))
683 (defconst nxml-utf-coding-systems (cons 'utf-8 nxml-utf-16-coding-systems))
685 (defun nxml-coding-system-unicode-p (coding-system)
686 (nxml-coding-system-member (coding-system-base coding-system)
687 nxml-utf-coding-systems))
689 (defun nxml-coding-system-name (coding-system)
690 (setq coding-system (coding-system-base coding-system))
691 (symbol-name
692 (if (nxml-coding-system-member coding-system nxml-utf-16-coding-systems)
693 'utf-16
694 (or (coding-system-get coding-system 'mime-charset)
695 coding-system))))
697 (defun nxml-fix-encoding-declaration (enc-pos coding-system)
698 (let ((charset (nxml-coding-system-name coding-system)))
699 (cond ((consp enc-pos)
700 (delete-region (car enc-pos) (cdr enc-pos))
701 (goto-char (car enc-pos))
702 (insert charset))
703 ((integerp enc-pos)
704 (goto-char enc-pos)
705 (insert " encoding=\"" charset ?\"))
707 (goto-char (point-min))
708 (insert "<?xml version=\"1.0\" encoding=\""
709 charset
710 "\"?>\n")
711 (when (and (not enc-pos)
712 (let ((case-fold-search t))
713 (looking-at xmltok-bad-xml-decl-regexp)))
714 (delete-region (point) (match-end 0)))))))
716 (defun nxml-choose-suitable-coding-system (suitable-coding-systems)
717 (let (ret coding-system)
718 (if (and buffer-file-coding-system
719 (memq (coding-system-base buffer-file-coding-system)
720 suitable-coding-systems))
721 buffer-file-coding-system
722 (while (and suitable-coding-systems (not ret))
723 (setq coding-system (car suitable-coding-systems))
724 (if (coding-system-get coding-system 'mime-charset)
725 (setq ret coding-system)
726 (setq suitable-coding-systems (cdr suitable-coding-systems))))
727 ret)))
729 (defun nxml-choose-utf-coding-system ()
730 (let ((cur (and (local-variable-p 'buffer-file-coding-system)
731 buffer-file-coding-system
732 (coding-system-base buffer-file-coding-system))))
733 (cond ((car (nxml-coding-system-member cur nxml-utf-coding-systems)))
734 ((and nxml-prefer-utf-16-to-utf-8-flag
735 (coding-system-p 'utf-16-le)
736 (coding-system-p 'utf-16-be))
737 (if nxml-prefer-utf-16-little-to-big-endian-flag
738 'utf-16-le
739 'utf-16-be))
740 (t 'utf-8))))
742 (defun nxml-choose-utf-16-coding-system ()
743 (let ((cur (and (local-variable-p 'buffer-file-coding-system)
744 buffer-file-coding-system
745 (coding-system-base buffer-file-coding-system))))
746 (cond ((car (nxml-coding-system-member cur nxml-utf-16-coding-systems)))
747 (nxml-prefer-utf-16-little-to-big-endian-flag
748 (and (coding-system-p 'utf-16-le) 'utf-16-le))
749 (t (and (coding-system-p 'utf-16-be) 'utf-16-be)))))
751 (defun nxml-coding-system-member (coding-system coding-systems)
752 (let (ret)
753 (while (and coding-systems (not ret))
754 (if (coding-system-equal coding-system
755 (car coding-systems))
756 (setq ret coding-systems)
757 (setq coding-systems (cdr coding-systems))))
758 ret))
760 ;;; Fontification
762 (defun nxml-unfontify-region (start end)
763 (font-lock-default-unfontify-region start end)
764 (nxml-clear-char-ref-extra-display start end))
766 (defun nxml-extend-region ()
767 "Extend the region to hold the minimum area we can fontify with nXML.
768 Called with `font-lock-beg' and `font-lock-end' dynamically bound."
769 (let ((start font-lock-beg)
770 (end font-lock-end))
772 (nxml-debug-change "nxml-extend-region(input)" start end)
774 (when (< start nxml-prolog-end)
775 (setq start (point-min)))
777 (cond ((<= end nxml-prolog-end)
778 (setq end nxml-prolog-end))
781 (goto-char start)
782 ;; some font-lock backends (like Emacs 22 jit-lock) snap
783 ;; the region to the beginning of the line no matter what
784 ;; we say here. To mitigate the resulting excess
785 ;; fontification, ignore leading whitespace.
786 (skip-syntax-forward " ")
788 ;; find the beginning of the previous tag
789 (when (not (equal (char-after) ?\<))
790 (search-backward "<" nxml-prolog-end t))
791 (nxml-ensure-scan-up-to-date)
792 (nxml-move-outside-backwards)
793 (setq start (point))
795 (while (< (point) end)
796 (nxml-tokenize-forward))
798 (setq end (point))))
800 (when (or (< start font-lock-beg)
801 (> end font-lock-end))
802 (setq font-lock-beg start
803 font-lock-end end)
804 (nxml-debug-change "nxml-extend-region" start end)
805 t)))
807 (defun nxml-fontify-matcher (bound)
808 "Called as font-lock keyword matcher."
809 (syntax-propertize bound)
810 (unless nxml-degraded
811 (nxml-debug-change "nxml-fontify-matcher" (point) bound)
813 (when (< (point) nxml-prolog-end)
814 ;; Prolog needs to be fontified in one go, and
815 ;; nxml-extend-region makes sure we start at BOB.
816 (cl-assert (bobp))
817 (nxml-fontify-prolog)
818 (goto-char nxml-prolog-end))
820 (let (xmltok-errors)
821 (while (and (nxml-tokenize-forward)
822 (<= (point) bound)) ; Intervals are open-ended.
823 (nxml-apply-fontify-rule)))
827 ;; Since we did the fontification internally, tell font-lock to not
828 ;; do anything itself.
829 nil)
831 (defun nxml-fontify-prolog ()
832 "Fontify the prolog.
833 The buffer is assumed to be prepared for fontification.
834 This does not set the fontified property, but it does clear
835 faces appropriately."
836 (let ((regions nxml-prolog-regions))
837 (while regions
838 (let ((region (car regions)))
839 (nxml-apply-fontify-rule (aref region 0)
840 (aref region 1)
841 (aref region 2)))
842 (setq regions (cdr regions)))))
844 ;; Vectors identify a substring of the token to be highlighted in some face.
846 ;; Token types returned by xmltok-forward.
848 (put 'start-tag
849 'nxml-fontify-rule
850 '([nil 1 nxml-tag-delimiter]
851 [-1 nil nxml-tag-delimiter]
852 (element-qname . 1)
853 attributes))
855 (put 'partial-start-tag
856 'nxml-fontify-rule
857 '([nil 1 nxml-tag-delimiter]
858 (element-qname . 1)
859 attributes))
861 (put 'end-tag
862 'nxml-fontify-rule
863 '([nil 1 nxml-tag-delimiter]
864 [1 2 nxml-tag-slash]
865 [-1 nil nxml-tag-delimiter]
866 (element-qname . 2)))
868 (put 'partial-end-tag
869 'nxml-fontify-rule
870 '([nil 1 nxml-tag-delimiter]
871 [1 2 nxml-tag-slash]
872 (element-qname . 2)))
874 (put 'empty-element
875 'nxml-fontify-rule
876 '([nil 1 nxml-tag-delimiter]
877 [-2 -1 nxml-tag-slash]
878 [-1 nil nxml-tag-delimiter]
879 (element-qname . 1)
880 attributes))
882 (put 'partial-empty-element
883 'nxml-fontify-rule
884 '([nil 1 nxml-tag-delimiter]
885 [-1 nil nxml-tag-slash]
886 (element-qname . 1)
887 attributes))
889 (put 'char-ref
890 'nxml-fontify-rule
891 '([nil 2 nxml-char-ref-delimiter]
892 [2 -1 nxml-char-ref-number]
893 [-1 nil nxml-char-ref-delimiter]
894 char-ref))
896 (put 'entity-ref
897 'nxml-fontify-rule
898 '([nil 1 nxml-entity-ref-delimiter]
899 [1 -1 nxml-entity-ref-name]
900 [-1 nil nxml-entity-ref-delimiter]))
902 ;; (put 'comment
903 ;; 'nxml-fontify-rule
904 ;; '([nil 4 nxml-comment-delimiter]
905 ;; [4 -3 nxml-comment-content]
906 ;; [-3 nil nxml-comment-delimiter]))
908 (put 'processing-instruction
909 'nxml-fontify-rule
910 '([nil 2 nxml-processing-instruction-delimiter]
911 [-2 nil nxml-processing-instruction-delimiter]
912 processing-instruction-content))
914 (put 'cdata-section
915 'nxml-fontify-rule
916 '([nil 3 nxml-cdata-section-delimiter] ; <![
917 [3 8 nxml-cdata-section-CDATA] ; CDATA
918 [8 9 nxml-cdata-section-delimiter] ; [
919 [9 -3 nxml-cdata-section-content] ; ]]>
920 [-3 nil nxml-cdata-section-delimiter]))
922 (put 'data
923 'nxml-fontify-rule
924 '([nil nil nxml-text]))
926 ;; Prolog region types in list returned by xmltok-forward-prolog.
928 (put 'xml-declaration
929 'nxml-fontify-rule
930 '([nil 2 nxml-processing-instruction-delimiter]
931 [2 5 nxml-processing-instruction-target]
932 [-2 nil nxml-processing-instruction-delimiter]))
934 (put 'xml-declaration-attribute-name
935 'nxml-fontify-rule
936 '([nil nil nxml-attribute-local-name]))
938 (put 'xml-declaration-attribute-value ;FIXME: What is this for?
939 'nxml-fontify-rule
940 '([nil 1 nxml-attribute-value-delimiter]
941 [1 -1 nxml-attribute-value]
942 [-1 nil nxml-attribute-value-delimiter]))
944 (put 'processing-instruction-left
945 'nxml-fontify-rule
946 '([nil 2 nxml-processing-instruction-delimiter]
947 [2 nil nxml-processing-instruction-target]))
949 (put 'processing-instruction-right
950 'nxml-fontify-rule
951 '([nil -2 nxml-processing-instruction-content]
952 [-2 nil nxml-processing-instruction-delimiter]))
954 (put 'literal
955 'nxml-fontify-rule
956 '([nil 1 nxml-prolog-literal-delimiter]
957 [1 -1 nxml-prolog-literal-content]
958 [-1 nil nxml-prolog-literal-delimiter]))
960 (put 'keyword
961 'nxml-fontify-rule
962 '([nil nil nxml-prolog-keyword]))
964 (put 'markup-declaration-open
965 'nxml-fontify-rule
966 '([0 2 nxml-markup-declaration-delimiter]
967 [2 nil nxml-prolog-keyword]))
969 (put 'markup-declaration-close
970 'nxml-fontify-rule
971 '([nil nil nxml-markup-declaration-delimiter]))
973 (put 'internal-subset-open
974 'nxml-fontify-rule
975 '([nil nil nxml-markup-declaration-delimiter]))
977 (put 'internal-subset-close
978 'nxml-fontify-rule
979 '([nil 1 nxml-markup-declaration-delimiter]
980 [-1 nil nxml-markup-declaration-delimiter]))
982 (put 'hash-name
983 'nxml-fontify-rule
984 '([nil 1 nxml-hash]
985 [1 nil nxml-prolog-keyword]))
987 (defun nxml-apply-fontify-rule (&optional type start end)
988 (let ((rule (get (or type xmltok-type) 'nxml-fontify-rule)))
989 (unless start (setq start xmltok-start))
990 (unless end (setq end (point)))
991 (while rule
992 (let* ((action (car rule)))
993 (setq rule (cdr rule))
994 (cond ((vectorp action)
995 (nxml-set-face (let ((offset (aref action 0)))
996 (cond ((not offset) start)
997 ((< offset 0) (+ end offset))
998 (t (+ start offset))))
999 (let ((offset (aref action 1)))
1000 (cond ((not offset) end)
1001 ((< offset 0) (+ end offset))
1002 (t (+ start offset))))
1003 (aref action 2)))
1004 ((and (consp action)
1005 (eq (car action) 'element-qname))
1006 (when xmltok-name-end ; maybe nil in partial-end-tag case
1007 (nxml-fontify-qname (+ start (cdr action))
1008 xmltok-name-colon
1009 xmltok-name-end
1010 'nxml-element-prefix
1011 'nxml-element-colon
1012 'nxml-element-local-name)))
1013 ((eq action 'attributes)
1014 (nxml-fontify-attributes))
1015 ((eq action 'processing-instruction-content)
1016 (nxml-set-face (+ start 2)
1017 xmltok-name-end
1018 'nxml-processing-instruction-target)
1019 (nxml-set-face (save-excursion
1020 (goto-char xmltok-name-end)
1021 (skip-chars-forward " \t\r\n")
1022 (point))
1023 (- end 2)
1024 'nxml-processing-instruction-content))
1025 ((eq action 'char-ref)
1026 (nxml-char-ref-display-extra start
1028 (xmltok-char-number start end)))
1029 (t (error "Invalid nxml-fontify-rule action %s" action)))))))
1031 (defun nxml-fontify-attributes ()
1032 (while xmltok-namespace-attributes
1033 (nxml-fontify-attribute (car xmltok-namespace-attributes)
1034 'namespace)
1035 (setq xmltok-namespace-attributes
1036 (cdr xmltok-namespace-attributes)))
1037 (while xmltok-attributes
1038 (nxml-fontify-attribute (car xmltok-attributes))
1039 (setq xmltok-attributes
1040 (cdr xmltok-attributes))))
1042 (defun nxml-fontify-attribute (att &optional namespace-declaration)
1043 (if namespace-declaration
1044 (nxml-fontify-qname (xmltok-attribute-name-start att)
1045 (xmltok-attribute-name-colon att)
1046 (xmltok-attribute-name-end att)
1047 'nxml-namespace-attribute-xmlns
1048 'nxml-namespace-attribute-colon
1049 'nxml-namespace-attribute-prefix
1050 'nxml-namespace-attribute-xmlns)
1051 (nxml-fontify-qname (xmltok-attribute-name-start att)
1052 (xmltok-attribute-name-colon att)
1053 (xmltok-attribute-name-end att)
1054 'nxml-attribute-prefix
1055 'nxml-attribute-colon
1056 'nxml-attribute-local-name))
1057 (dolist (ref (xmltok-attribute-refs att))
1058 (let* ((ref-type (aref ref 0))
1059 (ref-start (aref ref 1))
1060 (ref-end (aref ref 2)))
1061 (nxml-apply-fontify-rule ref-type ref-start ref-end))))
1063 (defun nxml-fontify-qname (start
1064 colon
1066 prefix-face
1067 colon-face
1068 local-name-face
1069 &optional
1070 unprefixed-face)
1071 (cond (colon (nxml-set-face start colon prefix-face)
1072 (nxml-set-face colon (1+ colon) colon-face)
1073 (nxml-set-face (1+ colon) end local-name-face))
1074 (t (nxml-set-face start end (or unprefixed-face
1075 local-name-face)))))
1077 ;;; Editing
1079 (defun nxml-electric-slash (arg)
1080 "Insert a slash.
1082 With a prefix ARG, do nothing other than insert the slash.
1084 Otherwise, if `nxml-slash-auto-complete-flag' is non-nil, insert the
1085 rest of the end-tag or empty-element if the slash is potentially part
1086 of an end-tag or the close of an empty-element.
1088 If the slash is part of an end-tag that is the first non-whitespace
1089 on the line, reindent the line."
1090 (interactive "*P")
1091 (nxml-ensure-scan-up-to-date)
1092 (let* ((slash-pos (point))
1093 (end-tag-p (and (eq (char-before slash-pos) ?<)
1094 (not (nxml-get-inside slash-pos))))
1095 (at-indentation (save-excursion
1096 (back-to-indentation)
1097 (eq (point) (1- slash-pos)))))
1098 (self-insert-command (prefix-numeric-value arg))
1099 (unless arg
1100 (if nxml-slash-auto-complete-flag
1101 (if end-tag-p
1102 (condition-case nil
1103 (let ((start-tag-end
1104 (nxml-scan-element-backward (1- slash-pos) t)))
1105 (when start-tag-end
1106 (insert (xmltok-start-tag-qname) ">")
1107 ;; copy the indentation of the start-tag
1108 (when (and at-indentation
1109 (save-excursion
1110 (goto-char xmltok-start)
1111 (back-to-indentation)
1112 (eq (point) xmltok-start)))
1113 (save-excursion
1114 (indent-line-to (save-excursion
1115 (goto-char xmltok-start)
1116 (current-column)))))))
1117 (nxml-scan-error nil))
1118 (when (and (eq (nxml-token-before) (point))
1119 (eq xmltok-type 'partial-empty-element))
1120 (insert ">"))))
1121 (when (and end-tag-p at-indentation)
1122 (nxml-indent-line)))))
1124 (defun nxml-balanced-close-start-tag-block ()
1125 "Close the start-tag before point with `>' and insert a balancing end-tag.
1126 Point is left between the start-tag and the end-tag.
1127 If there is nothing but whitespace before the `<' that opens the
1128 start-tag, then put point on a blank line, and put the end-tag on
1129 another line aligned with the start-tag."
1130 (interactive "*")
1131 (nxml-balanced-close-start-tag 'block))
1133 (defun nxml-balanced-close-start-tag-inline ()
1134 "Close the start-tag before point with `>' and insert a balancing end-tag.
1135 Point is left between the start-tag and the end-tag.
1136 No extra whitespace is inserted."
1137 (interactive "*")
1138 (nxml-balanced-close-start-tag 'inline))
1140 (defun nxml-balanced-close-start-tag (block-or-inline)
1141 (let ((token-end (nxml-token-before))
1142 (pos (1+ (point)))
1143 (token-start xmltok-start))
1144 (unless (or (eq xmltok-type 'partial-start-tag)
1145 (and (memq xmltok-type '(start-tag
1146 empty-element
1147 partial-empty-element))
1148 (>= token-end pos)))
1149 (error "Not in a start-tag"))
1150 ;; Note that this insertion changes xmltok-start.
1151 (insert "></"
1152 (buffer-substring-no-properties (+ xmltok-start 1)
1153 (min xmltok-name-end (point)))
1154 ">")
1155 (if (eq block-or-inline 'inline)
1156 (goto-char pos)
1157 (goto-char token-start)
1158 (back-to-indentation)
1159 (if (= (point) token-start)
1160 (let ((indent (current-column)))
1161 (goto-char pos)
1162 (insert "\n")
1163 (indent-line-to indent)
1164 (goto-char pos)
1165 (insert "\n")
1166 (indent-line-to (+ nxml-child-indent indent)))
1167 (goto-char pos)))))
1169 (defun nxml-finish-element ()
1170 "Finish the current element by inserting an end-tag."
1171 (interactive "*")
1172 (nxml-finish-element-1 nil))
1174 (defvar nxml-last-split-position nil
1175 "Position where `nxml-split-element' split the current element.")
1177 (defun nxml-split-element ()
1178 "Split the current element by inserting an end-tag and a start-tag.
1179 Point is left after the newly inserted start-tag. When repeated,
1180 split immediately before the previously inserted start-tag and leave
1181 point unchanged."
1182 (interactive "*")
1183 (setq nxml-last-split-position
1184 (if (and (eq last-command this-command)
1185 nxml-last-split-position)
1186 (save-excursion
1187 (goto-char nxml-last-split-position)
1188 (nxml-finish-element-1 t))
1189 (nxml-finish-element-1 t))))
1191 (defun nxml-finish-element-1 (startp)
1192 "Insert an end-tag for the current element and optionally a start-tag.
1193 The start-tag is inserted if STARTP is non-nil. Return the position
1194 of the inserted start-tag or nil if none was inserted."
1195 (interactive "*")
1196 (let* ((token-end (nxml-token-before))
1197 (start-tag-end
1198 (save-excursion
1199 (when (and (< (point) token-end)
1200 (memq xmltok-type
1201 '(cdata-section
1202 processing-instruction
1203 comment
1204 start-tag
1205 end-tag
1206 empty-element)))
1207 (error "Point is inside a %s"
1208 (nxml-token-type-friendly-name xmltok-type)))
1209 (nxml-scan-element-backward token-end t)))
1210 (starts-line
1211 (save-excursion
1212 (unless (eq xmltok-type 'start-tag)
1213 (error "No matching start-tag"))
1214 (goto-char xmltok-start)
1215 (back-to-indentation)
1216 (eq (point) xmltok-start)))
1217 (ends-line
1218 (save-excursion
1219 (goto-char start-tag-end)
1220 (looking-at "[ \t\r\n]*$")))
1221 (start-tag-indent (save-excursion
1222 (goto-char xmltok-start)
1223 (current-column)))
1224 (qname (xmltok-start-tag-qname))
1225 inserted-start-tag-pos)
1226 (when (and starts-line ends-line)
1227 ;; start-tag is on a line by itself
1228 ;; => put the end-tag on a line by itself
1229 (unless (<= (point)
1230 (save-excursion
1231 (back-to-indentation)
1232 (point)))
1233 (insert "\n"))
1234 (indent-line-to start-tag-indent))
1235 (insert "</" qname ">")
1236 (when startp
1237 (when starts-line
1238 (insert "\n")
1239 (indent-line-to start-tag-indent))
1240 (setq inserted-start-tag-pos (point))
1241 (insert "<" qname ">")
1242 (when (and starts-line ends-line)
1243 (insert "\n")
1244 (indent-line-to (save-excursion
1245 (goto-char xmltok-start)
1246 (forward-line 1)
1247 (back-to-indentation)
1248 (if (= (current-column)
1249 (+ start-tag-indent nxml-child-indent))
1250 (+ start-tag-indent nxml-child-indent)
1251 start-tag-indent)))))
1252 inserted-start-tag-pos))
1254 (defun nxml-comment-quote-nested (_cs _ce unp)
1255 "Quote nested comments in buffer.
1256 See `comment-quote-nested-function' for more information."
1257 (goto-char (point-min))
1258 (save-match-data
1259 (while (re-search-forward "-[\\]*-" nil t)
1260 (goto-char (match-beginning 0))
1261 (forward-char 1)
1262 (if unp
1263 (delete-char 1)
1264 (insert "\\")))))
1266 ;;; Indentation
1268 (defun nxml-indent-line ()
1269 "Indent current line as XML."
1270 (let* ((savep (point))
1271 (indent (condition-case nil
1272 (save-excursion
1273 (forward-line 0)
1274 (skip-chars-forward " \t")
1275 (if (>= (point) savep) (setq savep nil))
1276 (or (nxml-compute-indent) 0))
1277 (error 0))))
1278 (if (not (numberp indent))
1279 ;; If something funny is used (e.g. `noindent'), return it.
1280 indent
1281 (if (< indent 0) (setq indent 0)) ;Just in case.
1282 (if savep
1283 (save-excursion (indent-line-to indent))
1284 (indent-line-to indent)))))
1286 (defun nxml-compute-indent ()
1287 "Return the indent for the line containing point."
1288 (or (nxml-compute-indent-from-matching-start-tag)
1289 (nxml-compute-indent-from-previous-line)))
1291 (defun nxml-compute-indent-from-matching-start-tag ()
1292 "Compute the indent for a line with an end-tag using the matching start-tag.
1293 When the line containing point ends with an end-tag and does not start
1294 in the middle of a token, return the indent of the line containing the
1295 matching start-tag, if there is one and it occurs at the beginning of
1296 its line. Otherwise return nil."
1297 (save-excursion
1298 (back-to-indentation)
1299 (let ((bol (point)))
1300 (let ((inhibit-field-text-motion t))
1301 (end-of-line))
1302 (skip-chars-backward " \t")
1303 (and (= (nxml-token-before) (point))
1304 (memq xmltok-type '(end-tag partial-end-tag))
1305 ;; start of line must not be inside a token
1306 (or (= xmltok-start bol)
1307 (save-excursion
1308 (goto-char bol)
1309 (nxml-token-after)
1310 (= xmltok-start bol))
1311 (eq xmltok-type 'data))
1312 (condition-case nil
1313 (nxml-scan-element-backward
1314 (point)
1316 (- (point)
1317 nxml-end-tag-indent-scan-distance))
1318 (nxml-scan-error nil))
1319 (< xmltok-start bol)
1320 (progn
1321 (goto-char xmltok-start)
1322 (skip-chars-backward " \t")
1323 (bolp))
1324 (current-indentation)))))
1326 (defun nxml-compute-indent-from-previous-line ()
1327 "Compute the indent for a line using the indentation of a previous line."
1328 (save-excursion
1329 (end-of-line)
1330 (let ((eol (point))
1331 bol prev-bol ref
1332 before-context after-context)
1333 (back-to-indentation)
1334 (setq bol (point))
1335 (catch 'indent
1336 ;; Move backwards until the start of a non-blank line that is
1337 ;; not inside a token.
1338 (while (progn
1339 (when (= (forward-line -1) -1)
1340 (throw 'indent 0))
1341 (back-to-indentation)
1342 (if (looking-at "[ \t]*$")
1344 (or prev-bol
1345 (setq prev-bol (point)))
1346 (nxml-token-after)
1347 (not (or (= xmltok-start (point))
1348 (eq xmltok-type 'data))))))
1349 (setq ref (point))
1350 ;; Now scan over tokens until the end of the line to be indented.
1351 ;; Determine the context before and after the beginning of the
1352 ;; line.
1353 (while (< (point) eol)
1354 (nxml-tokenize-forward)
1355 (cond ((<= bol xmltok-start)
1356 (setq after-context
1357 (nxml-merge-indent-context-type after-context)))
1358 ((and (<= (point) bol)
1359 (not (and (eq xmltok-type 'partial-start-tag)
1360 (= (point) bol))))
1361 (setq before-context
1362 (nxml-merge-indent-context-type before-context)))
1363 ((eq xmltok-type 'data)
1364 (setq before-context
1365 (nxml-merge-indent-context-type before-context))
1366 (setq after-context
1367 (nxml-merge-indent-context-type after-context)))
1368 ;; If in the middle of a token that looks inline,
1369 ;; then indent relative to the previous non-blank line
1370 ((eq (nxml-merge-indent-context-type before-context)
1371 'mixed)
1372 (goto-char prev-bol)
1373 (throw 'indent (current-column)))
1375 (throw 'indent
1376 (nxml-compute-indent-in-token bol))))
1377 (skip-chars-forward " \t\r\n"))
1378 (goto-char ref)
1379 (+ (current-column)
1380 (* nxml-child-indent
1381 (+ (if (eq before-context 'start-tag) 1 0)
1382 (if (eq after-context 'end-tag) -1 0))))))))
1384 (defun nxml-merge-indent-context-type (context)
1385 "Merge the indent context type CONTEXT with the token in `xmltok-type'.
1386 Return the merged indent context type. An indent context type is
1387 either nil or one of the symbols `start-tag', `end-tag', `markup',
1388 `comment', `mixed'."
1389 (cond ((memq xmltok-type '(start-tag partial-start-tag))
1390 (if (memq context '(nil start-tag comment))
1391 'start-tag
1392 'mixed))
1393 ((memq xmltok-type '(end-tag partial-end-tag))
1394 (if (memq context '(nil end-tag comment))
1395 'end-tag
1396 'mixed))
1397 ((eq xmltok-type 'comment)
1398 (cond ((memq context '(start-tag end-tag comment))
1399 context)
1400 (context 'mixed)
1401 (t 'comment)))
1402 (context 'mixed)
1403 (t 'markup)))
1405 (defun nxml-compute-indent-in-token (pos)
1406 "Return the indent for a line that starts inside a token.
1407 POS is the position of the first non-whitespace character of the line.
1408 This expects the xmltok-* variables to be set up as by `xmltok-forward'."
1409 (cond ((memq xmltok-type '(start-tag
1410 partial-start-tag
1411 empty-element
1412 partial-empty-element))
1413 (nxml-compute-indent-in-start-tag pos))
1414 ((eq xmltok-type 'comment)
1415 (nxml-compute-indent-in-delimited-token pos "<!--" "-->"))
1416 ((eq xmltok-type 'cdata-section)
1417 (nxml-compute-indent-in-delimited-token pos "<![CDATA[" "]]>"))
1418 ((eq xmltok-type 'processing-instruction)
1419 (nxml-compute-indent-in-delimited-token pos "<?" "?>"))
1421 (goto-char pos)
1422 (if (and (= (forward-line -1) 0)
1423 (< xmltok-start (point)))
1424 (back-to-indentation)
1425 (goto-char xmltok-start))
1426 (current-column))))
1428 (defun nxml-compute-indent-in-start-tag (pos)
1429 "Return the indent for a line that starts inside a start-tag.
1430 Also for a line that starts inside an empty element.
1431 POS is the position of the first non-whitespace character of the line.
1432 This expects the xmltok-* variables to be set up as by `xmltok-forward'."
1433 (let ((value-boundary (nxml-attribute-value-boundary pos))
1434 (off 0))
1435 (if value-boundary
1436 ;; inside an attribute value
1437 (let ((value-start (car value-boundary)))
1438 (goto-char pos)
1439 (forward-line -1)
1440 (if (< (point) value-start)
1441 (goto-char value-start)
1442 (back-to-indentation)))
1443 ;; outside an attribute value
1444 (goto-char pos)
1445 (while (and (= (forward-line -1) 0)
1446 (nxml-attribute-value-boundary (point))))
1447 (cond ((<= (point) xmltok-start)
1448 (goto-char xmltok-start)
1449 (setq off nxml-attribute-indent)
1450 (let ((atts (xmltok-merge-attributes)))
1451 (when atts
1452 (let* ((att (car atts))
1453 (start (xmltok-attribute-name-start att)))
1454 (when (< start pos)
1455 (goto-char start)
1456 (setq off 0))))))
1458 (back-to-indentation))))
1459 (+ (current-column) off)))
1461 (defun nxml-attribute-value-boundary (pos)
1462 "Return a pair (START . END) if POS is inside an attribute value.
1463 Otherwise return nil. START and END are the positions of the start
1464 and end of the attribute value containing POS. This expects the
1465 xmltok-* variables to be set up as by `xmltok-forward'."
1466 (let ((atts (xmltok-merge-attributes))
1467 att value-start value-end value-boundary)
1468 (while atts
1469 (setq att (car atts))
1470 (setq value-start (xmltok-attribute-value-start att))
1471 (setq value-end (xmltok-attribute-value-end att))
1472 (cond ((and value-start (< pos value-start))
1473 (setq atts nil))
1474 ((and value-start value-end (<= pos value-end))
1475 (setq value-boundary (cons value-start value-end))
1476 (setq atts nil))
1477 (t (setq atts (cdr atts)))))
1478 value-boundary))
1480 (defun nxml-compute-indent-in-delimited-token (pos open-delim close-delim)
1481 "Return the indent for a line that starts inside a token with delimiters.
1482 OPEN-DELIM and CLOSE-DELIM are strings giving the opening and closing
1483 delimiters. POS is the position of the first non-whitespace character
1484 of the line. This expects the xmltok-* variables to be set up as by
1485 `xmltok-forward'."
1486 (cond ((let ((end (+ pos (length close-delim))))
1487 (and (<= end (point-max))
1488 (string= (buffer-substring-no-properties pos end)
1489 close-delim)))
1490 (goto-char xmltok-start))
1491 ((progn
1492 (goto-char pos)
1493 (forward-line -1)
1494 (<= (point) xmltok-start))
1495 (goto-char (+ xmltok-start (length open-delim)))
1496 (when (and (string= open-delim "<!--")
1497 (looking-at " "))
1498 (goto-char (1+ (point)))))
1499 (t (back-to-indentation)))
1500 (current-column))
1502 (define-obsolete-function-alias 'nxml-complete #'completion-at-point "26.1")
1504 ;;; Movement
1506 (defun nxml-forward-balanced-item (&optional arg)
1507 "Move forward across one balanced item.
1508 With ARG, do it that many times. Negative arg -N means
1509 move backward across N balanced expressions.
1510 This is the equivalent of `forward-sexp' for XML.
1512 An element is by default treated as a single markup item.
1513 However, if the variable `nxml-sexp-element-flag' is nil, then an
1514 element contains as items strings with no markup, tags,
1515 processing instructions, comments, CDATA sections, entity
1516 references and character references. A start-tag contains an
1517 element name followed by one or more attributes. An end-tag
1518 contains just an element name. An attribute value literals
1519 contains strings with no markup, entity references and character
1520 references. A processing instruction consists of a target and a
1521 content string. A comment or a CDATA section contains a single
1522 string. An entity reference contains a single name. A character
1523 reference contains a character number."
1524 (interactive "^p")
1525 (or arg (setq arg 1))
1526 (cond ((> arg 0)
1527 (while (progn
1528 (nxml-forward-single-balanced-item)
1529 (> (setq arg (1- arg)) 0))))
1530 ((< arg 0)
1531 (while (progn
1532 (nxml-backward-single-balanced-item)
1533 (< (setq arg (1+ arg)) 0))))))
1535 (defun nxml-forward-single-balanced-item ()
1536 (condition-case err
1537 (goto-char (let ((end (nxml-token-after)))
1538 (save-excursion
1539 (while (eq xmltok-type 'space)
1540 (goto-char end)
1541 (setq end (nxml-token-after)))
1542 (cond ((/= (point) xmltok-start)
1543 (nxml-scan-forward-within end))
1544 ((and nxml-sexp-element-flag
1545 (eq xmltok-type 'start-tag))
1546 ;; can't ever return nil here
1547 (nxml-scan-element-forward xmltok-start))
1548 ((and nxml-sexp-element-flag
1549 (memq xmltok-type
1550 '(end-tag partial-end-tag)))
1551 (error "Already at end of element"))
1552 (t end)))))
1553 (nxml-scan-error
1554 (goto-char (cadr err))
1555 (apply #'error (cddr err)))))
1557 (defun nxml-backward-single-balanced-item ()
1558 (condition-case err
1559 (goto-char (let ((end (nxml-token-before)))
1560 (save-excursion
1561 (while (eq xmltok-type 'space)
1562 (goto-char xmltok-start)
1563 (setq end (nxml-token-before)))
1564 (cond ((/= (point) end)
1565 (nxml-scan-backward-within end))
1566 ((and nxml-sexp-element-flag
1567 (eq xmltok-type 'end-tag))
1568 ;; can't ever return nil here
1569 (nxml-scan-element-backward end)
1570 xmltok-start)
1571 ((and nxml-sexp-element-flag
1572 (eq xmltok-type 'start-tag))
1573 (error "Already at start of element"))
1574 (t xmltok-start)))))
1575 (nxml-scan-error
1576 (goto-char (cadr err))
1577 (apply #'error (cddr err)))))
1579 (defun nxml-scan-forward-within (end)
1580 (setq end (- end (nxml-end-delimiter-length xmltok-type)))
1581 (when (<= end (point))
1582 (error "Already at end of %s"
1583 (nxml-token-type-friendly-name xmltok-type)))
1584 (cond ((memq xmltok-type '(start-tag
1585 empty-element
1586 partial-start-tag
1587 partial-empty-element))
1588 (if (< (point) xmltok-name-end)
1589 xmltok-name-end
1590 (let ((att (nxml-find-following-attribute)))
1591 (cond ((not att) end)
1592 ((and (xmltok-attribute-value-start att)
1593 (<= (xmltok-attribute-value-start att)
1594 (point)))
1595 (nxml-scan-forward-in-attribute-value att))
1596 ((xmltok-attribute-value-end att)
1597 (1+ (xmltok-attribute-value-end att)))
1598 ((save-excursion
1599 (goto-char (xmltok-attribute-name-end att))
1600 (looking-at "[ \t\r\n]*="))
1601 (match-end 0))
1602 (t (xmltok-attribute-name-end att))))))
1603 ((and (eq xmltok-type 'processing-instruction)
1604 (< (point) xmltok-name-end))
1605 xmltok-name-end)
1606 (t end)))
1608 (defun nxml-scan-backward-within (_end)
1609 (setq xmltok-start
1610 (+ xmltok-start
1611 (nxml-start-delimiter-length xmltok-type)))
1612 (when (<= (point) xmltok-start)
1613 (error "Already at start of %s"
1614 (nxml-token-type-friendly-name xmltok-type)))
1615 (cond ((memq xmltok-type '(start-tag
1616 empty-element
1617 partial-start-tag
1618 partial-empty-element))
1619 (let ((att (nxml-find-preceding-attribute)))
1620 (cond ((not att) xmltok-start)
1621 ((and (xmltok-attribute-value-start att)
1622 (<= (xmltok-attribute-value-start att)
1623 (point))
1624 (<= (point)
1625 (xmltok-attribute-value-end att)))
1626 (nxml-scan-backward-in-attribute-value att))
1627 (t (xmltok-attribute-name-start att)))))
1628 ((and (eq xmltok-type 'processing-instruction)
1629 (let ((content-start (save-excursion
1630 (goto-char xmltok-name-end)
1631 (skip-chars-forward " \r\t\n")
1632 (point))))
1633 (and (< content-start (point))
1634 content-start))))
1635 (t xmltok-start)))
1637 (defun nxml-scan-forward-in-attribute-value (att)
1638 (when (= (point) (xmltok-attribute-value-end att))
1639 (error "Already at end of attribute value"))
1640 (let ((refs (xmltok-attribute-refs att))
1641 ref)
1642 (while refs
1643 (setq ref (car refs))
1644 (if (< (point) (aref ref 2))
1645 (setq refs nil)
1646 (setq ref nil)
1647 (setq refs (cdr refs))))
1648 (cond ((not ref)
1649 (xmltok-attribute-value-end att))
1650 ((< (point) (aref ref 1))
1651 (aref ref 1))
1652 ((= (point) (aref ref 1))
1653 (aref ref 2))
1655 (let ((end (- (aref ref 2)
1656 (nxml-end-delimiter-length (aref ref 0)))))
1657 (if (< (point) end)
1659 (error "Already at end of %s"
1660 (nxml-token-type-friendly-name (aref ref 0)))))))))
1662 (defun nxml-scan-backward-in-attribute-value (att)
1663 (when (= (point) (xmltok-attribute-value-start att))
1664 (error "Already at start of attribute value"))
1665 (let ((refs (reverse (xmltok-attribute-refs att)))
1666 ref)
1667 (while refs
1668 (setq ref (car refs))
1669 (if (< (aref ref 1) (point))
1670 (setq refs nil)
1671 (setq ref nil)
1672 (setq refs (cdr refs))))
1673 (cond ((not ref)
1674 (xmltok-attribute-value-start att))
1675 ((< (aref ref 2) (point))
1676 (aref ref 2))
1677 ((= (point) (aref ref 2))
1678 (aref ref 1))
1680 (let ((start (+ (aref ref 1)
1681 (nxml-start-delimiter-length (aref ref 0)))))
1682 (if (< start (point))
1683 start
1684 (error "Already at start of %s"
1685 (nxml-token-type-friendly-name (aref ref 0)))))))))
1687 (defun nxml-find-following-attribute ()
1688 (let ((ret nil)
1689 (atts (or xmltok-attributes xmltok-namespace-attributes))
1690 (more-atts (and xmltok-attributes xmltok-namespace-attributes)))
1691 (while atts
1692 (let* ((att (car atts))
1693 (name-start (xmltok-attribute-name-start att)))
1694 (cond ((and (<= name-start (point))
1695 (xmltok-attribute-value-end att)
1696 ;; <= because end is before quote
1697 (<= (point) (xmltok-attribute-value-end att)))
1698 (setq atts nil)
1699 (setq ret att))
1700 ((and (< (point) name-start)
1701 (or (not ret)
1702 (< name-start
1703 (xmltok-attribute-name-start ret))))
1704 (setq ret att))))
1705 (setq atts (cdr atts))
1706 (unless atts
1707 (setq atts more-atts)
1708 (setq more-atts nil)))
1709 ret))
1711 (defun nxml-find-preceding-attribute ()
1712 (let ((ret nil)
1713 (atts (or xmltok-attributes xmltok-namespace-attributes))
1714 (more-atts (and xmltok-attributes xmltok-namespace-attributes)))
1715 (while atts
1716 (let* ((att (car atts))
1717 (name-start (xmltok-attribute-name-start att)))
1718 (cond ((and (< name-start (point))
1719 (xmltok-attribute-value-end att)
1720 ;; <= because end is before quote
1721 (<= (point) (xmltok-attribute-value-end att)))
1722 (setq atts nil)
1723 (setq ret att))
1724 ((and (< name-start (point))
1725 (or (not ret)
1726 (< (xmltok-attribute-name-start ret)
1727 name-start)))
1728 (setq ret att))))
1729 (setq atts (cdr atts))
1730 (unless atts
1731 (setq atts more-atts)
1732 (setq more-atts nil)))
1733 ret))
1735 (defun nxml-up-element (&optional arg)
1736 (interactive "^p")
1737 (or arg (setq arg 1))
1738 (if (< arg 0)
1739 (nxml-backward-up-element (- arg))
1740 (condition-case err
1741 (while (and (> arg 0)
1742 (< (point) (point-max)))
1743 (let ((token-end (nxml-token-after)))
1744 (goto-char (cond ((or (memq xmltok-type '(end-tag
1745 partial-end-tag))
1746 (and (memq xmltok-type
1747 '(empty-element
1748 partial-empty-element))
1749 (< xmltok-start (point))))
1750 token-end)
1751 ((nxml-scan-element-forward
1752 (if (and (eq xmltok-type 'start-tag)
1753 (= (point) xmltok-start))
1754 xmltok-start
1755 token-end)
1757 (t (error "No parent element")))))
1758 (setq arg (1- arg)))
1759 (nxml-scan-error
1760 (goto-char (cadr err))
1761 (apply #'error (cddr err))))))
1763 (defun nxml-backward-up-element (&optional arg)
1764 (interactive "^p")
1765 (or arg (setq arg 1))
1766 (if (< arg 0)
1767 (nxml-up-element (- arg))
1768 (condition-case err
1769 (while (and (> arg 0)
1770 (< (point-min) (point)))
1771 (let ((token-end (nxml-token-before)))
1772 (goto-char (cond ((or (memq xmltok-type '(start-tag
1773 partial-start-tag))
1774 (and (memq xmltok-type
1775 '(empty-element
1776 partial-empty-element))
1777 (< (point) token-end)))
1778 xmltok-start)
1779 ((nxml-scan-element-backward
1780 (if (and (eq xmltok-type 'end-tag)
1781 (= (point) token-end))
1782 token-end
1783 xmltok-start)
1785 xmltok-start)
1786 (t (error "No parent element")))))
1787 (setq arg (1- arg)))
1788 (nxml-scan-error
1789 (goto-char (cadr err))
1790 (apply #'error (cddr err))))))
1792 (defun nxml-down-element (&optional arg)
1793 "Move forward down into the content of an element.
1794 With ARG, do this that many times.
1795 Negative ARG means move backward but still down."
1796 (interactive "^p")
1797 (or arg (setq arg 1))
1798 (if (< arg 0)
1799 (nxml-backward-down-element (- arg))
1800 (while (> arg 0)
1801 (goto-char
1802 (let ((token-end (nxml-token-after)))
1803 (save-excursion
1804 (goto-char token-end)
1805 (while (progn
1806 (when (memq xmltok-type '(nil end-tag partial-end-tag))
1807 (error "No following start-tags in this element"))
1808 (not (memq xmltok-type '(start-tag partial-start-tag))))
1809 (nxml-tokenize-forward))
1810 (point))))
1811 (setq arg (1- arg)))))
1813 (defun nxml-backward-down-element (&optional arg)
1814 (interactive "^p")
1815 (or arg (setq arg 1))
1816 (if (< arg 0)
1817 (nxml-down-element (- arg))
1818 (while (> arg 0)
1819 (goto-char
1820 (save-excursion
1821 (nxml-token-before)
1822 (goto-char xmltok-start)
1823 (while (progn
1824 (when (memq xmltok-type '(start-tag
1825 partial-start-tag
1826 prolog
1827 nil))
1828 (error "No preceding end-tags in this element"))
1829 (not (memq xmltok-type '(end-tag partial-end-tag))))
1830 (if (or (<= (point) nxml-prolog-end)
1831 (not (search-backward "<" nxml-prolog-end t)))
1832 (setq xmltok-type nil)
1833 (nxml-move-outside-backwards)
1834 (xmltok-forward)))
1835 xmltok-start))
1836 (setq arg (1- arg)))))
1838 (defun nxml-forward-element (&optional arg)
1839 "Move forward over one element.
1840 With ARG, do it that many times.
1841 Negative ARG means move backward."
1842 (interactive "^p")
1843 (or arg (setq arg 1))
1844 (if (< arg 0)
1845 (nxml-backward-element (- arg))
1846 (condition-case err
1847 (while (and (> arg 0)
1848 (< (point) (point-max)))
1849 (goto-char
1850 (or (nxml-scan-element-forward (nxml-token-before))
1851 (error "No more elements")))
1852 (setq arg (1- arg)))
1853 (nxml-scan-error
1854 (goto-char (cadr err))
1855 (apply #'error (cddr err))))))
1857 (defun nxml-backward-element (&optional arg)
1858 "Move backward over one element.
1859 With ARG, do it that many times.
1860 Negative ARG means move forward."
1861 (interactive "^p")
1862 (or arg (setq arg 1))
1863 (if (< arg 0)
1864 (nxml-forward-element (- arg))
1865 (condition-case err
1866 (while (and (> arg 0)
1867 (< (point-min) (point)))
1868 (goto-char
1869 (or (and (nxml-scan-element-backward (progn
1870 (nxml-token-after)
1871 xmltok-start))
1872 xmltok-start)
1873 (error "No preceding elements")))
1874 (setq arg (1- arg)))
1875 (nxml-scan-error
1876 (goto-char (cadr err))
1877 (apply #'error (cddr err))))))
1879 (defun nxml-mark-token-after ()
1880 (interactive)
1881 (push-mark (nxml-token-after) nil t)
1882 (goto-char xmltok-start)
1883 (message "Marked %s" xmltok-type))
1885 ;;; Paragraphs
1887 (defun nxml-mark-paragraph ()
1888 "Put point at beginning of this paragraph, mark at end.
1889 The paragraph marked is the one that contains point or follows point."
1890 (interactive)
1891 (nxml-forward-paragraph)
1892 (push-mark nil t t)
1893 (nxml-backward-paragraph))
1895 (defun nxml-forward-paragraph (&optional arg)
1896 (interactive "^p")
1897 (or arg (setq arg 1))
1898 (cond ((< arg 0)
1899 (nxml-backward-paragraph (- arg)))
1900 ((> arg 0)
1901 (forward-line 0)
1902 (while (and (nxml-forward-single-paragraph)
1903 (> (setq arg (1- arg)) 0))))))
1905 (defun nxml-backward-paragraph (&optional arg)
1906 (interactive "^p")
1907 (or arg (setq arg 1))
1908 (cond ((< arg 0)
1909 (nxml-forward-paragraph (- arg)))
1910 ((> arg 0)
1911 (unless (bolp)
1912 (let ((inhibit-field-text-motion t))
1913 (end-of-line)))
1914 (while (and (nxml-backward-single-paragraph)
1915 (> (setq arg (1- arg)) 0))))))
1917 (defun nxml-forward-single-paragraph ()
1918 "Move forward over a single paragraph.
1919 Return nil at end of buffer, t otherwise."
1920 (let* ((token-end (nxml-token-after))
1921 (offset (- (point) xmltok-start))
1922 pos had-data)
1923 (goto-char token-end)
1924 (while (and (< (point) (point-max))
1925 (not (setq pos
1926 (nxml-paragraph-end-pos had-data offset))))
1927 (when (nxml-token-contains-data-p offset)
1928 (setq had-data t))
1929 (nxml-tokenize-forward)
1930 (setq offset 0))
1931 (when pos (goto-char pos))))
1933 (defun nxml-backward-single-paragraph ()
1934 "Move backward over a single paragraph.
1935 Return nil at start of buffer, t otherwise."
1936 (let* ((token-end (nxml-token-before))
1937 (offset (- token-end (point)))
1938 (last-tag-pos xmltok-start)
1939 pos had-data last-data-pos)
1940 (goto-char token-end)
1941 (unless (setq pos (nxml-paragraph-start-pos nil offset))
1942 (setq had-data (nxml-token-contains-data-p nil offset))
1943 (goto-char xmltok-start)
1944 (while (and (not pos) (< (point-min) (point)))
1945 (cond ((search-backward "<" nxml-prolog-end t)
1946 (nxml-move-outside-backwards)
1947 (save-excursion
1948 (while (< (point) last-tag-pos)
1949 (xmltok-forward)
1950 (when (and (not had-data) (nxml-token-contains-data-p))
1951 (setq pos nil)
1952 (setq last-data-pos xmltok-start))
1953 (let ((tem (nxml-paragraph-start-pos had-data 0)))
1954 (when tem (setq pos tem)))))
1955 (when (and (not had-data) last-data-pos (not pos))
1956 (setq had-data t)
1957 (save-excursion
1958 (while (< (point) last-data-pos)
1959 (xmltok-forward))
1960 (let ((tem (nxml-paragraph-start-pos had-data 0)))
1961 (when tem (setq pos tem)))))
1962 (setq last-tag-pos (point)))
1963 (t (goto-char (point-min))))))
1964 (when pos (goto-char pos))))
1966 (defun nxml-token-contains-data-p (&optional start end)
1967 (setq start (+ xmltok-start (or start 0)))
1968 (setq end (- (point) (or end 0)))
1969 (when (eq xmltok-type 'cdata-section)
1970 (setq start (max start (+ xmltok-start 9)))
1971 (setq end (min end (- (point) 3))))
1972 (or (and (eq xmltok-type 'data)
1973 (eq start xmltok-start)
1974 (eq end (point)))
1975 (eq xmltok-type 'char-ref)
1976 (and (memq xmltok-type '(data cdata-section))
1977 (< start end)
1978 (save-excursion
1979 (goto-char start)
1980 (re-search-forward "[^ \t\r\n]" end t)))))
1982 (defun nxml-paragraph-end-pos (had-data offset)
1983 "Return the position of the paragraph end if contained in the current token.
1984 Return nil if the current token does not contain the paragraph end.
1985 Only characters after OFFSET from the start of the token are eligible.
1986 HAD-DATA says whether there have been non-whitespace data characters yet."
1987 (cond ((not had-data)
1988 (cond ((memq xmltok-type '(data cdata-section))
1989 (save-excursion
1990 (let ((end (point)))
1991 (goto-char (+ xmltok-start
1992 (max (if (eq xmltok-type 'cdata-section)
1995 offset)))
1996 (and (re-search-forward "[^ \t\r\n]" end t)
1997 (re-search-forward "^[ \t]*$" end t)
1998 (match-beginning 0)))))
1999 ((and (eq xmltok-type 'comment)
2000 (nxml-token-begins-line-p)
2001 (nxml-token-ends-line-p))
2002 (save-excursion
2003 (let ((end (point)))
2004 (goto-char (+ xmltok-start (max 4 offset)))
2005 (when (re-search-forward "[^ \t\r\n]" (- end 3) t)
2006 (if (re-search-forward "^[ \t]*$" end t)
2007 (match-beginning 0)
2008 (goto-char (- end 3))
2009 (skip-chars-backward " \t")
2010 (unless (bolp)
2011 (beginning-of-line 2))
2012 (point))))))))
2013 ((memq xmltok-type '(data space cdata-section))
2014 (save-excursion
2015 (let ((end (point)))
2016 (goto-char (+ xmltok-start offset))
2017 (and (re-search-forward "^[ \t]*$" end t)
2018 (match-beginning 0)))))
2019 ((and (memq xmltok-type '(start-tag
2020 end-tag
2021 empty-element
2022 comment
2023 processing-instruction
2024 entity-ref))
2025 (nxml-token-begins-line-p)
2026 (nxml-token-ends-line-p))
2027 (save-excursion
2028 (goto-char xmltok-start)
2029 (skip-chars-backward " \t")
2030 (point)))
2031 ((and (eq xmltok-type 'end-tag)
2032 (looking-at "[ \t]*$")
2033 (not (nxml-in-mixed-content-p t)))
2034 (save-excursion
2035 (or (search-forward "\n" nil t)
2036 (point-max))))))
2038 (defun nxml-paragraph-start-pos (had-data offset)
2039 "Return the position of the paragraph start if contained in the current token.
2040 Return nil if the current token does not contain the paragraph start.
2041 Only characters before OFFSET from the end of the token are eligible.
2042 HAD-DATA says whether there have been non-whitespace data characters yet."
2043 (cond ((not had-data)
2044 (cond ((memq xmltok-type '(data cdata-section))
2045 (save-excursion
2046 (goto-char (- (point)
2047 (max (if (eq xmltok-type 'cdata-section)
2050 offset)))
2051 (and (re-search-backward "[^ \t\r\n]" xmltok-start t)
2052 (re-search-backward "^[ \t]*$" xmltok-start t)
2053 (match-beginning 0))))
2054 ((and (eq xmltok-type 'comment)
2055 (nxml-token-ends-line-p)
2056 (nxml-token-begins-line-p))
2057 (save-excursion
2058 (goto-char (- (point) (max 3 offset)))
2059 (when (and (< (+ xmltok-start 4) (point))
2060 (re-search-backward "[^ \t\r\n]"
2061 (+ xmltok-start 4)
2063 (if (re-search-backward "^[ \t]*$" xmltok-start t)
2064 (match-beginning 0)
2065 (goto-char xmltok-start)
2066 (if (looking-at "<!--[ \t]*\n")
2067 (match-end 0)
2068 (skip-chars-backward " \t")
2069 (point))))))))
2070 ((memq xmltok-type '(data space cdata-section))
2071 (save-excursion
2072 (goto-char (- (point) offset))
2073 (and (re-search-backward "^[ \t]*$" xmltok-start t)
2074 (match-beginning 0))))
2075 ((and (memq xmltok-type '(start-tag
2076 end-tag
2077 empty-element
2078 comment
2079 processing-instruction
2080 entity-ref))
2081 (nxml-token-ends-line-p)
2082 (nxml-token-begins-line-p))
2083 (or (search-forward "\n" nil t)
2084 (point-max)))
2085 ((and (eq xmltok-type 'start-tag)
2086 (nxml-token-begins-line-p)
2087 (not (save-excursion
2088 (goto-char xmltok-start)
2089 (nxml-in-mixed-content-p nil))))
2090 (save-excursion
2091 (goto-char xmltok-start)
2092 (skip-chars-backward " \t")
2093 ;; include any blank line before
2094 (or (and (eq (char-before) ?\n)
2095 (save-excursion
2096 (goto-char (1- (point)))
2097 (skip-chars-backward " \t")
2098 (and (bolp) (point))))
2099 (point))))))
2101 (defun nxml-token-ends-line-p () (looking-at "[ \t]*$"))
2103 (defun nxml-token-begins-line-p ()
2104 (save-excursion
2105 (goto-char xmltok-start)
2106 (skip-chars-backward " \t")
2107 (bolp)))
2109 (defun nxml-in-mixed-content-p (endp)
2110 "Return non-nil if point is in mixed content.
2111 Point must be after an end-tag or before a start-tag.
2112 ENDP is t in the former case, nil in the latter."
2113 (let (matching-tag-pos)
2114 (cond ((not (run-hook-with-args-until-failure
2115 'nxml-in-mixed-content-hook))
2116 nil)
2117 ;; See if the matching tag does not start or end a line.
2118 ((condition-case nil
2119 (progn
2120 (setq matching-tag-pos
2121 (xmltok-save
2122 (if endp
2123 (and (nxml-scan-element-backward (point))
2124 xmltok-start)
2125 (nxml-scan-element-forward (point)))))
2126 (and matching-tag-pos
2127 (save-excursion
2128 (goto-char matching-tag-pos)
2129 (not (if endp
2130 (progn
2131 (skip-chars-backward " \t")
2132 (bolp))
2133 (looking-at "[ \t]*$"))))))
2134 (nxml-scan-error nil))
2136 ;; See if there's data at the same level.
2137 ((let (start end)
2138 (if endp
2139 (setq start matching-tag-pos
2140 end (point))
2141 (setq start (point)
2142 end matching-tag-pos))
2143 (save-excursion
2144 (or (when start
2145 (goto-char start)
2146 (nxml-preceding-sibling-data-p))
2147 (when end
2148 (goto-char end)
2149 (nxml-following-sibling-data-p)))))
2151 ;; Otherwise, treat as not mixed
2152 (t nil))))
2154 (defun nxml-preceding-sibling-data-p ()
2155 "Return non-nil if there is a previous sibling that is data."
2156 (let ((lim (max (- (point) nxml-mixed-scan-distance)
2157 nxml-prolog-end))
2158 (level 0)
2159 found end)
2160 (xmltok-save
2161 (save-excursion
2162 (while (and (< lim (point))
2163 (>= level 0)
2164 (not found)
2165 (progn
2166 (setq end (point))
2167 (search-backward "<" lim t)))
2168 (nxml-move-outside-backwards)
2169 (save-excursion
2170 (xmltok-forward)
2171 (let ((prev-level level))
2172 (cond ((eq xmltok-type 'end-tag)
2173 (setq level (1+ level)))
2174 ((eq xmltok-type 'start-tag)
2175 (setq level (1- level))))
2176 (when (eq prev-level 0)
2177 (while (and (< (point) end) (not found))
2178 (xmltok-forward)
2179 (when (memq xmltok-type '(data cdata-section char-ref))
2180 (setq found t)))))))))
2181 found))
2183 (defun nxml-following-sibling-data-p ()
2184 (let ((lim (min (+ (point) nxml-mixed-scan-distance)
2185 (point-max)))
2186 (level 0)
2187 found)
2188 (xmltok-save
2189 (save-excursion
2190 (while (and (< (point) lim)
2191 (>= level 0)
2192 (nxml-tokenize-forward)
2193 (not found))
2194 (cond ((eq xmltok-type 'start-tag)
2195 (setq level (1+ level)))
2196 ((eq xmltok-type 'end-tag)
2197 (setq level (1- level)))
2198 ((and (eq level 0)
2199 (memq xmltok-type '(data cdata-section char-ref)))
2200 (setq found t))))))
2201 found))
2203 ;;; Filling
2205 (defun nxml-do-fill-paragraph (arg)
2206 (let (fill-paragraph-function
2207 fill-prefix
2208 start end)
2209 (save-excursion
2210 (nxml-forward-paragraph)
2211 (setq end (point))
2212 (nxml-backward-paragraph)
2213 (skip-chars-forward " \t\r\n")
2214 (setq start (point))
2215 (beginning-of-line)
2216 (setq fill-prefix (buffer-substring-no-properties (point) start))
2217 (when (and (not (nxml-get-inside (point)))
2218 (looking-at "[ \t]*<!--"))
2219 (setq fill-prefix (concat fill-prefix " ")))
2220 (fill-region-as-paragraph start end arg))
2221 (skip-line-prefix fill-prefix)
2222 fill-prefix))
2224 (defun nxml-newline-and-indent (soft)
2225 (delete-horizontal-space)
2226 (if soft (insert-and-inherit ?\n) (newline 1))
2227 (nxml-indent-line))
2230 ;;; Dynamic markup
2232 (defvar nxml-dynamic-markup-prev-pos nil)
2233 (defvar nxml-dynamic-markup-prev-lengths nil)
2234 (defvar nxml-dynamic-markup-prev-found-marker nil)
2235 (defvar nxml-dynamic-markup-prev-start-tags (make-hash-table :test 'equal))
2237 (defun nxml-dynamic-markup-word ()
2238 "Dynamically markup the word before point.
2239 This attempts to find a tag to put around the word before point based
2240 on the contents of the current buffer. The end-tag will be inserted at
2241 point. The start-tag will be inserted at or before the beginning of
2242 the word before point; the contents of the current buffer is used to
2243 decide where.
2245 It works in a similar way to \\[dabbrev-expand]. It searches first
2246 backwards from point, then forwards from point for an element whose
2247 content is a string which matches the contents of the buffer before
2248 point and which includes at least the word before point. It then
2249 copies the start- and end-tags from that element and uses them to
2250 surround the matching string before point.
2252 Repeating \\[nxml-dynamic-markup-word] immediately after successful
2253 \\[nxml-dynamic-markup-word] removes the previously inserted markup
2254 and attempts to find another possible way to do the markup."
2255 (interactive "*")
2256 (let (search-start-pos)
2257 (if (and (integerp nxml-dynamic-markup-prev-pos)
2258 (= nxml-dynamic-markup-prev-pos (point))
2259 (eq last-command this-command)
2260 nxml-dynamic-markup-prev-lengths)
2261 (let* ((end-tag-open-pos
2262 (- nxml-dynamic-markup-prev-pos
2263 (nth 2 nxml-dynamic-markup-prev-lengths)))
2264 (start-tag-close-pos
2265 (- end-tag-open-pos
2266 (nth 1 nxml-dynamic-markup-prev-lengths)))
2267 (start-tag-open-pos
2268 (- start-tag-close-pos
2269 (nth 0 nxml-dynamic-markup-prev-lengths))))
2270 (delete-region end-tag-open-pos nxml-dynamic-markup-prev-pos)
2271 (delete-region start-tag-open-pos start-tag-close-pos)
2272 (setq search-start-pos
2273 (marker-position nxml-dynamic-markup-prev-found-marker)))
2274 (clrhash nxml-dynamic-markup-prev-start-tags))
2275 (setq nxml-dynamic-markup-prev-pos nil)
2276 (setq nxml-dynamic-markup-prev-lengths nil)
2277 (setq nxml-dynamic-markup-prev-found-marker nil)
2278 (goto-char
2279 (save-excursion
2280 (let* ((pos (point))
2281 (word (progn
2282 (backward-word 1)
2283 (unless (< (point) pos)
2284 (error "No word to markup"))
2285 (buffer-substring-no-properties (point) pos)))
2286 (search (concat word "</"))
2287 done)
2288 (when search-start-pos
2289 (goto-char search-start-pos))
2290 (while (and (not done)
2291 (or (and (< (point) pos)
2292 (or (search-backward search nil t)
2293 (progn (goto-char pos) nil)))
2294 (search-forward search nil t)))
2295 (goto-char (- (match-end 0) 2))
2296 (setq done (nxml-try-copy-markup pos)))
2297 (or done
2298 (error (if (zerop (hash-table-count
2299 nxml-dynamic-markup-prev-start-tags))
2300 "No possible markup found for `%s'"
2301 "No more markup possibilities found for `%s'")
2302 word)))))))
2304 (defun nxml-try-copy-markup (word-end-pos)
2305 (save-excursion
2306 (let ((end-tag-pos (point)))
2307 (when (and (not (nxml-get-inside end-tag-pos))
2308 (search-backward "<" nil t)
2309 (not (nxml-get-inside (point))))
2310 (xmltok-forward)
2311 (when (and (eq xmltok-type 'start-tag)
2312 (< (point) end-tag-pos))
2313 (let* ((start-tag-close-pos (point))
2314 (start-tag
2315 (buffer-substring-no-properties xmltok-start
2316 start-tag-close-pos))
2317 (words
2318 (nreverse
2319 (split-string
2320 (buffer-substring-no-properties start-tag-close-pos
2321 end-tag-pos)
2322 "[ \t\r\n]+"))))
2323 (goto-char word-end-pos)
2324 (while (and words
2325 (re-search-backward (concat
2326 (regexp-quote (car words))
2327 "\\=")
2330 (setq words (cdr words))
2331 (skip-chars-backward " \t\r\n"))
2332 (when (and (not words)
2333 (progn
2334 (skip-chars-forward " \t\r\n")
2335 (not (gethash (cons (point) start-tag)
2336 nxml-dynamic-markup-prev-start-tags)))
2337 (or (< end-tag-pos (point))
2338 (< word-end-pos xmltok-start)))
2339 (setq nxml-dynamic-markup-prev-found-marker
2340 (copy-marker end-tag-pos t))
2341 (puthash (cons (point) start-tag)
2343 nxml-dynamic-markup-prev-start-tags)
2344 (setq nxml-dynamic-markup-prev-lengths
2345 (list (- start-tag-close-pos xmltok-start)
2346 (- word-end-pos (point))
2347 (+ (- xmltok-name-end xmltok-start) 2)))
2348 (let ((name (xmltok-start-tag-qname)))
2349 (insert start-tag)
2350 (goto-char (+ word-end-pos
2351 (- start-tag-close-pos xmltok-start)))
2352 (insert "</" name ">")
2353 (setq nxml-dynamic-markup-prev-pos (point))))))))))
2356 ;;; Character names
2358 (defun nxml-insert-named-char (arg)
2359 "Insert a character using its name.
2360 The name is read from the minibuffer.
2361 Normally, inserts the character as a numeric character reference.
2362 With a prefix argument, inserts the character directly."
2363 (interactive "*P")
2364 (let ((code (read-char-by-name "Character name: ")))
2365 (when code
2366 (insert (if arg code (format "&#x%X;" code))))))
2368 (defun nxml-toggle-char-ref-extra-display (arg)
2369 "Toggle the display of extra information for character references."
2370 (interactive "P")
2371 (let ((new (if (null arg)
2372 (not nxml-char-ref-extra-display)
2373 (> (prefix-numeric-value arg) 0))))
2374 (when (not (eq new nxml-char-ref-extra-display))
2375 (setq nxml-char-ref-extra-display new)
2376 (font-lock-flush))))
2378 (put 'nxml-char-ref 'evaporate t)
2380 (defun nxml-char-ref-display-extra (start end n)
2381 (when nxml-char-ref-extra-display
2382 (let ((name (or (get-char-code-property n 'name)
2383 (get-char-code-property n 'old-name)))
2384 (glyph-string (and nxml-char-ref-display-glyph-flag
2385 (char-displayable-p n)
2386 (string n)))
2388 (when (or name glyph-string)
2389 (setq ov (make-overlay start end nil t))
2390 (overlay-put ov 'category 'nxml-char-ref)
2391 (when name
2392 (overlay-put ov 'help-echo name))
2393 (when glyph-string
2394 (overlay-put ov
2395 'after-string
2396 (propertize glyph-string 'face 'nxml-glyph)))))))
2398 (defun nxml-clear-char-ref-extra-display (start end)
2399 (let ((ov (overlays-in start end)))
2400 (while ov
2401 (when (eq (overlay-get (car ov) 'category) 'nxml-char-ref)
2402 (delete-overlay (car ov)))
2403 (setq ov (cdr ov)))))
2406 (defun nxml-start-delimiter-length (type)
2407 (or (get type 'nxml-start-delimiter-length)
2410 (put 'cdata-section 'nxml-start-delimiter-length 9)
2411 (put 'comment 'nxml-start-delimiter-length 4)
2412 (put 'processing-instruction 'nxml-start-delimiter-length 2)
2413 (put 'start-tag 'nxml-start-delimiter-length 1)
2414 (put 'empty-element 'nxml-start-delimiter-length 1)
2415 (put 'partial-empty-element 'nxml-start-delimiter-length 1)
2416 (put 'entity-ref 'nxml-start-delimiter-length 1)
2417 (put 'char-ref 'nxml-start-delimiter-length 2)
2419 (defun nxml-end-delimiter-length (type)
2420 (or (get type 'nxml-end-delimiter-length)
2423 (put 'cdata-section 'nxml-end-delimiter-length 3)
2424 (put 'comment 'nxml-end-delimiter-length 3)
2425 (put 'processing-instruction 'nxml-end-delimiter-length 2)
2426 (put 'start-tag 'nxml-end-delimiter-length 1)
2427 (put 'empty-element 'nxml-end-delimiter-length 2)
2428 (put 'partial-empty-element 'nxml-end-delimiter-length 1)
2429 (put 'entity-ref 'nxml-end-delimiter-length 1)
2430 (put 'char-ref 'nxml-end-delimiter-length 1)
2432 (defun nxml-token-type-friendly-name (type)
2433 (or (get type 'nxml-friendly-name)
2434 (symbol-name type)))
2436 (put 'cdata-section 'nxml-friendly-name "CDATA section")
2437 (put 'processing-instruction 'nxml-friendly-name "processing instruction")
2438 (put 'entity-ref 'nxml-friendly-name "entity reference")
2439 (put 'char-ref 'nxml-friendly-name "character reference")
2441 ;; Only do this in loaddefs, so that if someone defines a different
2442 ;; alias in .emacs, loading this file afterwards does not clobber it.
2443 ;;;###autoload(defalias 'xml-mode 'nxml-mode)
2445 (provide 'nxml-mode)
2447 ;;; nxml-mode.el ends here