Avoid segfaults when some display vector is an empty string
[emacs.git] / lisp / nxml / nxml-mode.el
blob7e33e743de0a09a170b674b95293b5dd62785c6b
1 ;;; nxml-mode.el --- a new XML mode -*- lexical-binding:t -*-
3 ;; Copyright (C) 2003-2004, 2007-2017 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 <http://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 nil
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, each
475 tag each treated as a single markup item; to make the complete element
476 be treated as a single markup item, set the variable
477 `nxml-sexp-element-flag' to t. For more details, see the function
478 `nxml-forward-balanced-item'.
480 \\[nxml-backward-up-element] and \\[nxml-down-element] move up and down the element structure.
482 Many aspects this mode can be customized using
483 \\[customize-group] nxml RET."
484 ;; (kill-all-local-variables)
485 ;; If encoding does not allow non-break space character, use reference.
486 ;; FIXME: This duplicates code from sgml-mode, perhaps derive from it?
487 ;; FIXME: Perhaps use &nbsp; if possible (e.g. XHTML)?
488 (setq-local tildify-space-string
489 (if (equal (decode-coding-string
490 (encode-coding-string " " buffer-file-coding-system)
491 buffer-file-coding-system) " ")
492 " " "&#160;"))
493 ;; FIXME: Use the fact that we're parsing the document already
494 ;; rather than using regex-based filtering.
495 (setq-local tildify-foreach-region-function
496 (apply-partially #'tildify-foreach-ignore-environments
497 '(("<! *--" . "-- *>") ("<" . ">"))))
498 (setq-local mode-line-process '((nxml-degraded "/degraded")))
499 ;; We'll determine the fill prefix ourselves
500 (setq-local adaptive-fill-mode nil)
501 (setq-local forward-sexp-function #'nxml-forward-balanced-item)
502 (setq-local indent-line-function #'nxml-indent-line)
503 (setq-local fill-paragraph-function #'nxml-do-fill-paragraph)
504 ;; Comment support
505 ;; This doesn't seem to work too well;
506 ;; I think we should probably roll our own nxml-comment-dwim function.
507 (setq-local comment-indent-function #'nxml-indent-line)
508 (setq-local comment-start "<!--")
509 (setq-local comment-start-skip "<!--[ \t\r\n]*")
510 (setq-local comment-end "-->")
511 (setq-local comment-end-skip "[ \t\r\n]*-->")
512 (setq-local comment-line-break-function #'nxml-newline-and-indent)
513 (setq-local comment-quote-nested-function #'nxml-comment-quote-nested)
514 (save-excursion
515 (save-restriction
516 (widen)
517 (with-silent-modifications
518 (nxml-with-invisible-motion
519 (nxml-scan-prolog)))))
520 (setq-local syntax-ppss-table sgml-tag-syntax-table)
521 (setq-local syntax-propertize-function #'sgml-syntax-propertize)
522 (add-hook 'change-major-mode-hook #'nxml-cleanup nil t)
524 ;; Emacs 23 handles the encoding attribute on the xml declaration
525 ;; transparently to nxml-mode, so there is no longer a need for the below
526 ;; hook. The hook also had the drawback of overriding explicit user
527 ;; instruction to save as some encoding other than utf-8.
528 ;;(add-hook 'write-contents-hooks #'nxml-prepare-to-save)
529 (when (not (and (buffer-file-name) (file-exists-p (buffer-file-name))))
530 (when (and nxml-default-buffer-file-coding-system
531 (not (local-variable-p 'buffer-file-coding-system)))
532 (setq buffer-file-coding-system nxml-default-buffer-file-coding-system))
533 (when nxml-auto-insert-xml-declaration-flag
534 (nxml-insert-xml-declaration)))
536 (setq font-lock-defaults
537 '(nxml-font-lock-keywords
538 nil ; highlight comments and strings based on syntax-tables
539 nil ; font-lock-keywords-case-fold-search. XML is case sensitive
540 nil ; no special syntax table
541 (font-lock-extend-region-functions . (nxml-extend-region))
542 (jit-lock-contextually . t)
543 (font-lock-unfontify-region-function . nxml-unfontify-region)))
545 (with-demoted-errors (rng-nxml-mode-init)))
547 (defun nxml-cleanup ()
548 "Clean up after nxml-mode."
549 ;; Disable associated minor modes.
550 (rng-validate-mode -1)
551 ;; Clean up fontification.
552 (save-excursion
553 (widen)
554 (with-silent-modifications
555 (nxml-with-invisible-motion
556 (remove-text-properties (point-min) (point-max) '(face)))))
557 (remove-hook 'change-major-mode-hook #'nxml-cleanup t))
559 (defun nxml-degrade (context err)
560 (message "Internal nXML mode error in %s (%s), degrading"
561 context
562 (error-message-string err))
563 (ding)
564 (setq nxml-degraded t)
565 (setq nxml-prolog-end 1))
567 ;;; Change management
569 (defvar font-lock-beg) (defvar font-lock-end)
570 (defun nxml-debug-region (start end)
571 (interactive "r")
572 (let ((font-lock-beg start)
573 (font-lock-end end))
574 (nxml-extend-region)
575 (goto-char font-lock-beg)
576 (set-mark font-lock-end)))
578 ;;; Encodings
580 (defun nxml-insert-xml-declaration ()
581 "Insert an XML declaration at the beginning of buffer.
582 The XML declaration will declare an encoding depending on the buffer's
583 `buffer-file-coding-system'."
584 (interactive "*")
585 (let ((coding-system
586 (if (and buffer-file-coding-system
587 (coding-system-p buffer-file-coding-system)
588 (coding-system-get buffer-file-coding-system
589 'mime-charset))
590 buffer-file-coding-system
591 (nxml-choose-utf-coding-system))))
592 (goto-char (point-min))
593 (insert (format "<?xml version=\"1.0\" encoding=\"%s\"?>\n"
594 (nxml-coding-system-name coding-system)))))
596 (defun nxml-prepare-to-save ()
597 (unless (and (not enable-multibyte-characters)
598 (local-variable-p 'buffer-file-coding-system)
599 buffer-file-coding-system
600 (or (eq (coding-system-type buffer-file-coding-system) 5)
601 (eq buffer-file-coding-system 'no-conversion)))
602 (save-excursion
603 (setq buffer-file-coding-system (nxml-select-coding-system))))
604 ;; nil from a function in `write-contents-hooks' means
605 ;; to continue and write the file as normal
606 nil)
608 (defun nxml-select-coding-system ()
609 (let* ((suitable-coding-systems
610 (find-coding-systems-region (point-min) (point-max)))
611 (enc-pos (progn
612 (goto-char (point-min))
613 (xmltok-get-declared-encoding-position)))
614 (enc-name
615 (and (consp enc-pos)
616 (buffer-substring-no-properties (car enc-pos)
617 (cdr enc-pos))))
618 (coding-system
619 (cond (enc-name
620 (if (string= (downcase enc-name) "utf-16")
621 (nxml-choose-utf-16-coding-system)
622 (nxml-mime-charset-coding-system enc-name)))
623 (enc-pos (nxml-choose-utf-coding-system)))))
624 ;; Make sure we have a coding-system
625 (unless coding-system
626 (setq coding-system
627 (and (not buffer-read-only)
628 (nxml-choose-suitable-coding-system
629 suitable-coding-systems)))
630 (let ((message
631 (if enc-name
632 (format "Unknown encoding %s" enc-name)
633 "XML declaration is not well-formed")))
634 (cond ((not coding-system)
635 (error "%s" message))
636 ((y-or-n-p
637 (concat message
638 ". "
639 (format (if enc-name
640 "Save with %s"
641 "Modify and save with encoding %s")
642 (nxml-coding-system-name coding-system))
643 " "))
644 (nxml-fix-encoding-declaration enc-pos coding-system))
645 (t (signal 'quit nil)))))
646 ;; Make sure it can encode all the characters in the buffer
647 (unless (or (memq (coding-system-base coding-system)
648 suitable-coding-systems)
649 (equal suitable-coding-systems '(undecided)))
650 (let ((message
651 (nxml-unsuitable-coding-system-message coding-system
652 enc-name)))
653 (setq coding-system
654 (and (not buffer-read-only)
655 (nxml-choose-suitable-coding-system
656 suitable-coding-systems)))
657 (cond ((not coding-system) (error "%s" message))
658 ((y-or-n-p (concat message
659 (format ". Save with %s "
660 (nxml-coding-system-name
661 coding-system))))
662 (nxml-fix-encoding-declaration enc-pos coding-system))
663 (t (signal 'quit nil)))))
664 ;; Merge the newline type of our existing encoding
665 (let ((current-eol-type
666 (coding-system-eol-type buffer-file-coding-system)))
667 (when (and current-eol-type (integerp current-eol-type))
668 (setq coding-system
669 (coding-system-change-eol-conversion coding-system
670 current-eol-type))))
671 coding-system))
673 (defun nxml-unsuitable-coding-system-message (coding-system &optional enc-name)
674 (if (nxml-coding-system-unicode-p coding-system)
675 "Cannot translate some characters to Unicode"
676 (format "Cannot encode some characters with %s"
677 (or enc-name
678 (nxml-coding-system-name coding-system)))))
680 (defconst nxml-utf-16-coding-systems (and (coding-system-p 'utf-16-be)
681 (coding-system-p 'utf-16-le)
682 '(utf-16-be utf-16-le)))
684 (defconst nxml-utf-coding-systems (cons 'utf-8 nxml-utf-16-coding-systems))
686 (defun nxml-coding-system-unicode-p (coding-system)
687 (nxml-coding-system-member (coding-system-base coding-system)
688 nxml-utf-coding-systems))
690 (defun nxml-coding-system-name (coding-system)
691 (setq coding-system (coding-system-base coding-system))
692 (symbol-name
693 (if (nxml-coding-system-member coding-system nxml-utf-16-coding-systems)
694 'utf-16
695 (or (coding-system-get coding-system 'mime-charset)
696 coding-system))))
698 (defun nxml-fix-encoding-declaration (enc-pos coding-system)
699 (let ((charset (nxml-coding-system-name coding-system)))
700 (cond ((consp enc-pos)
701 (delete-region (car enc-pos) (cdr enc-pos))
702 (goto-char (car enc-pos))
703 (insert charset))
704 ((integerp enc-pos)
705 (goto-char enc-pos)
706 (insert " encoding=\"" charset ?\"))
708 (goto-char (point-min))
709 (insert "<?xml version=\"1.0\" encoding=\""
710 charset
711 "\"?>\n")
712 (when (and (not enc-pos)
713 (let ((case-fold-search t))
714 (looking-at xmltok-bad-xml-decl-regexp)))
715 (delete-region (point) (match-end 0)))))))
717 (defun nxml-choose-suitable-coding-system (suitable-coding-systems)
718 (let (ret coding-system)
719 (if (and buffer-file-coding-system
720 (memq (coding-system-base buffer-file-coding-system)
721 suitable-coding-systems))
722 buffer-file-coding-system
723 (while (and suitable-coding-systems (not ret))
724 (setq coding-system (car suitable-coding-systems))
725 (if (coding-system-get coding-system 'mime-charset)
726 (setq ret coding-system)
727 (setq suitable-coding-systems (cdr suitable-coding-systems))))
728 ret)))
730 (defun nxml-choose-utf-coding-system ()
731 (let ((cur (and (local-variable-p 'buffer-file-coding-system)
732 buffer-file-coding-system
733 (coding-system-base buffer-file-coding-system))))
734 (cond ((car (nxml-coding-system-member cur nxml-utf-coding-systems)))
735 ((and nxml-prefer-utf-16-to-utf-8-flag
736 (coding-system-p 'utf-16-le)
737 (coding-system-p 'utf-16-be))
738 (if nxml-prefer-utf-16-little-to-big-endian-flag
739 'utf-16-le
740 'utf-16-be))
741 (t 'utf-8))))
743 (defun nxml-choose-utf-16-coding-system ()
744 (let ((cur (and (local-variable-p 'buffer-file-coding-system)
745 buffer-file-coding-system
746 (coding-system-base buffer-file-coding-system))))
747 (cond ((car (nxml-coding-system-member cur nxml-utf-16-coding-systems)))
748 (nxml-prefer-utf-16-little-to-big-endian-flag
749 (and (coding-system-p 'utf-16-le) 'utf-16-le))
750 (t (and (coding-system-p 'utf-16-be) 'utf-16-be)))))
752 (defun nxml-coding-system-member (coding-system coding-systems)
753 (let (ret)
754 (while (and coding-systems (not ret))
755 (if (coding-system-equal coding-system
756 (car coding-systems))
757 (setq ret coding-systems)
758 (setq coding-systems (cdr coding-systems))))
759 ret))
761 ;;; Fontification
763 (defun nxml-unfontify-region (start end)
764 (font-lock-default-unfontify-region start end)
765 (nxml-clear-char-ref-extra-display start end))
767 (defun nxml-extend-region ()
768 "Extend the region to hold the minimum area we can fontify with nXML.
769 Called with `font-lock-beg' and `font-lock-end' dynamically bound."
770 (let ((start font-lock-beg)
771 (end font-lock-end))
773 (nxml-debug-change "nxml-extend-region(input)" start end)
775 (when (< start nxml-prolog-end)
776 (setq start (point-min)))
778 (cond ((<= end nxml-prolog-end)
779 (setq end nxml-prolog-end))
782 (goto-char start)
783 ;; some font-lock backends (like Emacs 22 jit-lock) snap
784 ;; the region to the beginning of the line no matter what
785 ;; we say here. To mitigate the resulting excess
786 ;; fontification, ignore leading whitespace.
787 (skip-syntax-forward " ")
789 ;; find the beginning of the previous tag
790 (when (not (equal (char-after) ?\<))
791 (search-backward "<" nxml-prolog-end t))
792 (nxml-ensure-scan-up-to-date)
793 (nxml-move-outside-backwards)
794 (setq start (point))
796 (while (< (point) end)
797 (nxml-tokenize-forward))
799 (setq end (point))))
801 (when (or (< start font-lock-beg)
802 (> end font-lock-end))
803 (setq font-lock-beg start
804 font-lock-end end)
805 (nxml-debug-change "nxml-extend-region" start end)
806 t)))
808 (defun nxml-fontify-matcher (bound)
809 "Called as font-lock keyword matcher."
810 (syntax-propertize bound)
811 (unless nxml-degraded
812 (nxml-debug-change "nxml-fontify-matcher" (point) bound)
814 (when (< (point) nxml-prolog-end)
815 ;; Prolog needs to be fontified in one go, and
816 ;; nxml-extend-region makes sure we start at BOB.
817 (cl-assert (bobp))
818 (nxml-fontify-prolog)
819 (goto-char nxml-prolog-end))
821 (let (xmltok-errors)
822 (while (and (nxml-tokenize-forward)
823 (<= (point) bound)) ; Intervals are open-ended.
824 (nxml-apply-fontify-rule)))
828 ;; Since we did the fontification internally, tell font-lock to not
829 ;; do anything itself.
830 nil)
832 (defun nxml-fontify-prolog ()
833 "Fontify the prolog.
834 The buffer is assumed to be prepared for fontification.
835 This does not set the fontified property, but it does clear
836 faces appropriately."
837 (let ((regions nxml-prolog-regions))
838 (while regions
839 (let ((region (car regions)))
840 (nxml-apply-fontify-rule (aref region 0)
841 (aref region 1)
842 (aref region 2)))
843 (setq regions (cdr regions)))))
845 ;; Vectors identify a substring of the token to be highlighted in some face.
847 ;; Token types returned by xmltok-forward.
849 (put 'start-tag
850 'nxml-fontify-rule
851 '([nil 1 nxml-tag-delimiter]
852 [-1 nil nxml-tag-delimiter]
853 (element-qname . 1)
854 attributes))
856 (put 'partial-start-tag
857 'nxml-fontify-rule
858 '([nil 1 nxml-tag-delimiter]
859 (element-qname . 1)
860 attributes))
862 (put 'end-tag
863 'nxml-fontify-rule
864 '([nil 1 nxml-tag-delimiter]
865 [1 2 nxml-tag-slash]
866 [-1 nil nxml-tag-delimiter]
867 (element-qname . 2)))
869 (put 'partial-end-tag
870 'nxml-fontify-rule
871 '([nil 1 nxml-tag-delimiter]
872 [1 2 nxml-tag-slash]
873 (element-qname . 2)))
875 (put 'empty-element
876 'nxml-fontify-rule
877 '([nil 1 nxml-tag-delimiter]
878 [-2 -1 nxml-tag-slash]
879 [-1 nil nxml-tag-delimiter]
880 (element-qname . 1)
881 attributes))
883 (put 'partial-empty-element
884 'nxml-fontify-rule
885 '([nil 1 nxml-tag-delimiter]
886 [-1 nil nxml-tag-slash]
887 (element-qname . 1)
888 attributes))
890 (put 'char-ref
891 'nxml-fontify-rule
892 '([nil 2 nxml-char-ref-delimiter]
893 [2 -1 nxml-char-ref-number]
894 [-1 nil nxml-char-ref-delimiter]
895 char-ref))
897 (put 'entity-ref
898 'nxml-fontify-rule
899 '([nil 1 nxml-entity-ref-delimiter]
900 [1 -1 nxml-entity-ref-name]
901 [-1 nil nxml-entity-ref-delimiter]))
903 ;; (put 'comment
904 ;; 'nxml-fontify-rule
905 ;; '([nil 4 nxml-comment-delimiter]
906 ;; [4 -3 nxml-comment-content]
907 ;; [-3 nil nxml-comment-delimiter]))
909 (put 'processing-instruction
910 'nxml-fontify-rule
911 '([nil 2 nxml-processing-instruction-delimiter]
912 [-2 nil nxml-processing-instruction-delimiter]
913 processing-instruction-content))
915 (put 'cdata-section
916 'nxml-fontify-rule
917 '([nil 3 nxml-cdata-section-delimiter] ; <![
918 [3 8 nxml-cdata-section-CDATA] ; CDATA
919 [8 9 nxml-cdata-section-delimiter] ; [
920 [9 -3 nxml-cdata-section-content] ; ]]>
921 [-3 nil nxml-cdata-section-delimiter]))
923 (put 'data
924 'nxml-fontify-rule
925 '([nil nil nxml-text]))
927 ;; Prolog region types in list returned by xmltok-forward-prolog.
929 (put 'xml-declaration
930 'nxml-fontify-rule
931 '([nil 2 nxml-processing-instruction-delimiter]
932 [2 5 nxml-processing-instruction-target]
933 [-2 nil nxml-processing-instruction-delimiter]))
935 (put 'xml-declaration-attribute-name
936 'nxml-fontify-rule
937 '([nil nil nxml-attribute-local-name]))
939 (put 'xml-declaration-attribute-value ;FIXME: What is this for?
940 'nxml-fontify-rule
941 '([nil 1 nxml-attribute-value-delimiter]
942 [1 -1 nxml-attribute-value]
943 [-1 nil nxml-attribute-value-delimiter]))
945 (put 'processing-instruction-left
946 'nxml-fontify-rule
947 '([nil 2 nxml-processing-instruction-delimiter]
948 [2 nil nxml-processing-instruction-target]))
950 (put 'processing-instruction-right
951 'nxml-fontify-rule
952 '([nil -2 nxml-processing-instruction-content]
953 [-2 nil nxml-processing-instruction-delimiter]))
955 (put 'literal
956 'nxml-fontify-rule
957 '([nil 1 nxml-prolog-literal-delimiter]
958 [1 -1 nxml-prolog-literal-content]
959 [-1 nil nxml-prolog-literal-delimiter]))
961 (put 'keyword
962 'nxml-fontify-rule
963 '([nil nil nxml-prolog-keyword]))
965 (put 'markup-declaration-open
966 'nxml-fontify-rule
967 '([0 2 nxml-markup-declaration-delimiter]
968 [2 nil nxml-prolog-keyword]))
970 (put 'markup-declaration-close
971 'nxml-fontify-rule
972 '([nil nil nxml-markup-declaration-delimiter]))
974 (put 'internal-subset-open
975 'nxml-fontify-rule
976 '([nil nil nxml-markup-declaration-delimiter]))
978 (put 'internal-subset-close
979 'nxml-fontify-rule
980 '([nil 1 nxml-markup-declaration-delimiter]
981 [-1 nil nxml-markup-declaration-delimiter]))
983 (put 'hash-name
984 'nxml-fontify-rule
985 '([nil 1 nxml-hash]
986 [1 nil nxml-prolog-keyword]))
988 (defun nxml-apply-fontify-rule (&optional type start end)
989 (let ((rule (get (or type xmltok-type) 'nxml-fontify-rule)))
990 (unless start (setq start xmltok-start))
991 (unless end (setq end (point)))
992 (while rule
993 (let* ((action (car rule)))
994 (setq rule (cdr rule))
995 (cond ((vectorp action)
996 (nxml-set-face (let ((offset (aref action 0)))
997 (cond ((not offset) start)
998 ((< offset 0) (+ end offset))
999 (t (+ start offset))))
1000 (let ((offset (aref action 1)))
1001 (cond ((not offset) end)
1002 ((< offset 0) (+ end offset))
1003 (t (+ start offset))))
1004 (aref action 2)))
1005 ((and (consp action)
1006 (eq (car action) 'element-qname))
1007 (when xmltok-name-end ; maybe nil in partial-end-tag case
1008 (nxml-fontify-qname (+ start (cdr action))
1009 xmltok-name-colon
1010 xmltok-name-end
1011 'nxml-element-prefix
1012 'nxml-element-colon
1013 'nxml-element-local-name)))
1014 ((eq action 'attributes)
1015 (nxml-fontify-attributes))
1016 ((eq action 'processing-instruction-content)
1017 (nxml-set-face (+ start 2)
1018 xmltok-name-end
1019 'nxml-processing-instruction-target)
1020 (nxml-set-face (save-excursion
1021 (goto-char xmltok-name-end)
1022 (skip-chars-forward " \t\r\n")
1023 (point))
1024 (- end 2)
1025 'nxml-processing-instruction-content))
1026 ((eq action 'char-ref)
1027 (nxml-char-ref-display-extra start
1029 (xmltok-char-number start end)))
1030 (t (error "Invalid nxml-fontify-rule action %s" action)))))))
1032 (defun nxml-fontify-attributes ()
1033 (while xmltok-namespace-attributes
1034 (nxml-fontify-attribute (car xmltok-namespace-attributes)
1035 'namespace)
1036 (setq xmltok-namespace-attributes
1037 (cdr xmltok-namespace-attributes)))
1038 (while xmltok-attributes
1039 (nxml-fontify-attribute (car xmltok-attributes))
1040 (setq xmltok-attributes
1041 (cdr xmltok-attributes))))
1043 (defun nxml-fontify-attribute (att &optional namespace-declaration)
1044 (if namespace-declaration
1045 (nxml-fontify-qname (xmltok-attribute-name-start att)
1046 (xmltok-attribute-name-colon att)
1047 (xmltok-attribute-name-end att)
1048 'nxml-namespace-attribute-xmlns
1049 'nxml-namespace-attribute-colon
1050 'nxml-namespace-attribute-prefix
1051 'nxml-namespace-attribute-xmlns)
1052 (nxml-fontify-qname (xmltok-attribute-name-start att)
1053 (xmltok-attribute-name-colon att)
1054 (xmltok-attribute-name-end att)
1055 'nxml-attribute-prefix
1056 'nxml-attribute-colon
1057 'nxml-attribute-local-name))
1058 (dolist (ref (xmltok-attribute-refs att))
1059 (let* ((ref-type (aref ref 0))
1060 (ref-start (aref ref 1))
1061 (ref-end (aref ref 2)))
1062 (nxml-apply-fontify-rule ref-type ref-start ref-end))))
1064 (defun nxml-fontify-qname (start
1065 colon
1067 prefix-face
1068 colon-face
1069 local-name-face
1070 &optional
1071 unprefixed-face)
1072 (cond (colon (nxml-set-face start colon prefix-face)
1073 (nxml-set-face colon (1+ colon) colon-face)
1074 (nxml-set-face (1+ colon) end local-name-face))
1075 (t (nxml-set-face start end (or unprefixed-face
1076 local-name-face)))))
1078 ;;; Editing
1080 (defun nxml-electric-slash (arg)
1081 "Insert a slash.
1083 With a prefix ARG, do nothing other than insert the slash.
1085 Otherwise, if `nxml-slash-auto-complete-flag' is non-nil, insert the
1086 rest of the end-tag or empty-element if the slash is potentially part
1087 of an end-tag or the close of an empty-element.
1089 If the slash is part of an end-tag that is the first non-whitespace
1090 on the line, reindent the line."
1091 (interactive "*P")
1092 (nxml-ensure-scan-up-to-date)
1093 (let* ((slash-pos (point))
1094 (end-tag-p (and (eq (char-before slash-pos) ?<)
1095 (not (nxml-get-inside slash-pos))))
1096 (at-indentation (save-excursion
1097 (back-to-indentation)
1098 (eq (point) (1- slash-pos)))))
1099 (self-insert-command (prefix-numeric-value arg))
1100 (unless arg
1101 (if nxml-slash-auto-complete-flag
1102 (if end-tag-p
1103 (condition-case nil
1104 (let ((start-tag-end
1105 (nxml-scan-element-backward (1- slash-pos) t)))
1106 (when start-tag-end
1107 (insert (xmltok-start-tag-qname) ">")
1108 ;; copy the indentation of the start-tag
1109 (when (and at-indentation
1110 (save-excursion
1111 (goto-char xmltok-start)
1112 (back-to-indentation)
1113 (eq (point) xmltok-start)))
1114 (save-excursion
1115 (indent-line-to (save-excursion
1116 (goto-char xmltok-start)
1117 (current-column)))))))
1118 (nxml-scan-error nil))
1119 (when (and (eq (nxml-token-before) (point))
1120 (eq xmltok-type 'partial-empty-element))
1121 (insert ">"))))
1122 (when (and end-tag-p at-indentation)
1123 (nxml-indent-line)))))
1125 (defun nxml-balanced-close-start-tag-block ()
1126 "Close the start-tag before point with `>' and insert a balancing end-tag.
1127 Point is left between the start-tag and the end-tag.
1128 If there is nothing but whitespace before the `<' that opens the
1129 start-tag, then put point on a blank line, and put the end-tag on
1130 another line aligned with the start-tag."
1131 (interactive "*")
1132 (nxml-balanced-close-start-tag 'block))
1134 (defun nxml-balanced-close-start-tag-inline ()
1135 "Close the start-tag before point with `>' and insert a balancing end-tag.
1136 Point is left between the start-tag and the end-tag.
1137 No extra whitespace is inserted."
1138 (interactive "*")
1139 (nxml-balanced-close-start-tag 'inline))
1141 (defun nxml-balanced-close-start-tag (block-or-inline)
1142 (let ((token-end (nxml-token-before))
1143 (pos (1+ (point)))
1144 (token-start xmltok-start))
1145 (unless (or (eq xmltok-type 'partial-start-tag)
1146 (and (memq xmltok-type '(start-tag
1147 empty-element
1148 partial-empty-element))
1149 (>= token-end pos)))
1150 (error "Not in a start-tag"))
1151 ;; Note that this insertion changes xmltok-start.
1152 (insert "></"
1153 (buffer-substring-no-properties (+ xmltok-start 1)
1154 (min xmltok-name-end (point)))
1155 ">")
1156 (if (eq block-or-inline 'inline)
1157 (goto-char pos)
1158 (goto-char token-start)
1159 (back-to-indentation)
1160 (if (= (point) token-start)
1161 (let ((indent (current-column)))
1162 (goto-char pos)
1163 (insert "\n")
1164 (indent-line-to indent)
1165 (goto-char pos)
1166 (insert "\n")
1167 (indent-line-to (+ nxml-child-indent indent)))
1168 (goto-char pos)))))
1170 (defun nxml-finish-element ()
1171 "Finish the current element by inserting an end-tag."
1172 (interactive "*")
1173 (nxml-finish-element-1 nil))
1175 (defvar nxml-last-split-position nil
1176 "Position where `nxml-split-element' split the current element.")
1178 (defun nxml-split-element ()
1179 "Split the current element by inserting an end-tag and a start-tag.
1180 Point is left after the newly inserted start-tag. When repeated,
1181 split immediately before the previously inserted start-tag and leave
1182 point unchanged."
1183 (interactive "*")
1184 (setq nxml-last-split-position
1185 (if (and (eq last-command this-command)
1186 nxml-last-split-position)
1187 (save-excursion
1188 (goto-char nxml-last-split-position)
1189 (nxml-finish-element-1 t))
1190 (nxml-finish-element-1 t))))
1192 (defun nxml-finish-element-1 (startp)
1193 "Insert an end-tag for the current element and optionally a start-tag.
1194 The start-tag is inserted if STARTP is non-nil. Return the position
1195 of the inserted start-tag or nil if none was inserted."
1196 (interactive "*")
1197 (let* ((token-end (nxml-token-before))
1198 (start-tag-end
1199 (save-excursion
1200 (when (and (< (point) token-end)
1201 (memq xmltok-type
1202 '(cdata-section
1203 processing-instruction
1204 comment
1205 start-tag
1206 end-tag
1207 empty-element)))
1208 (error "Point is inside a %s"
1209 (nxml-token-type-friendly-name xmltok-type)))
1210 (nxml-scan-element-backward token-end t)))
1211 (starts-line
1212 (save-excursion
1213 (unless (eq xmltok-type 'start-tag)
1214 (error "No matching start-tag"))
1215 (goto-char xmltok-start)
1216 (back-to-indentation)
1217 (eq (point) xmltok-start)))
1218 (ends-line
1219 (save-excursion
1220 (goto-char start-tag-end)
1221 (looking-at "[ \t\r\n]*$")))
1222 (start-tag-indent (save-excursion
1223 (goto-char xmltok-start)
1224 (current-column)))
1225 (qname (xmltok-start-tag-qname))
1226 inserted-start-tag-pos)
1227 (when (and starts-line ends-line)
1228 ;; start-tag is on a line by itself
1229 ;; => put the end-tag on a line by itself
1230 (unless (<= (point)
1231 (save-excursion
1232 (back-to-indentation)
1233 (point)))
1234 (insert "\n"))
1235 (indent-line-to start-tag-indent))
1236 (insert "</" qname ">")
1237 (when startp
1238 (when starts-line
1239 (insert "\n")
1240 (indent-line-to start-tag-indent))
1241 (setq inserted-start-tag-pos (point))
1242 (insert "<" qname ">")
1243 (when (and starts-line ends-line)
1244 (insert "\n")
1245 (indent-line-to (save-excursion
1246 (goto-char xmltok-start)
1247 (forward-line 1)
1248 (back-to-indentation)
1249 (if (= (current-column)
1250 (+ start-tag-indent nxml-child-indent))
1251 (+ start-tag-indent nxml-child-indent)
1252 start-tag-indent)))))
1253 inserted-start-tag-pos))
1255 (defun nxml-comment-quote-nested (_cs _ce unp)
1256 "Quote nested comments in buffer.
1257 See `comment-quote-nested-function' for more information."
1258 (goto-char (point-min))
1259 (save-match-data
1260 (while (re-search-forward "-[\\]*-" nil t)
1261 (goto-char (match-beginning 0))
1262 (forward-char 1)
1263 (if unp
1264 (delete-char 1)
1265 (insert "\\")))))
1267 ;;; Indentation
1269 (defun nxml-indent-line ()
1270 "Indent current line as XML."
1271 (let* ((savep (point))
1272 (indent (condition-case nil
1273 (save-excursion
1274 (forward-line 0)
1275 (skip-chars-forward " \t")
1276 (if (>= (point) savep) (setq savep nil))
1277 (or (nxml-compute-indent) 0))
1278 (error 0))))
1279 (if (not (numberp indent))
1280 ;; If something funny is used (e.g. `noindent'), return it.
1281 indent
1282 (if (< indent 0) (setq indent 0)) ;Just in case.
1283 (if savep
1284 (save-excursion (indent-line-to indent))
1285 (indent-line-to indent)))))
1287 (defun nxml-compute-indent ()
1288 "Return the indent for the line containing point."
1289 (or (nxml-compute-indent-from-matching-start-tag)
1290 (nxml-compute-indent-from-previous-line)))
1292 (defun nxml-compute-indent-from-matching-start-tag ()
1293 "Compute the indent for a line with an end-tag using the matching start-tag.
1294 When the line containing point ends with an end-tag and does not start
1295 in the middle of a token, return the indent of the line containing the
1296 matching start-tag, if there is one and it occurs at the beginning of
1297 its line. Otherwise return nil."
1298 (save-excursion
1299 (back-to-indentation)
1300 (let ((bol (point)))
1301 (let ((inhibit-field-text-motion t))
1302 (end-of-line))
1303 (skip-chars-backward " \t")
1304 (and (= (nxml-token-before) (point))
1305 (memq xmltok-type '(end-tag partial-end-tag))
1306 ;; start of line must not be inside a token
1307 (or (= xmltok-start bol)
1308 (save-excursion
1309 (goto-char bol)
1310 (nxml-token-after)
1311 (= xmltok-start bol))
1312 (eq xmltok-type 'data))
1313 (condition-case nil
1314 (nxml-scan-element-backward
1315 (point)
1317 (- (point)
1318 nxml-end-tag-indent-scan-distance))
1319 (nxml-scan-error nil))
1320 (< xmltok-start bol)
1321 (progn
1322 (goto-char xmltok-start)
1323 (skip-chars-backward " \t")
1324 (bolp))
1325 (current-indentation)))))
1327 (defun nxml-compute-indent-from-previous-line ()
1328 "Compute the indent for a line using the indentation of a previous line."
1329 (save-excursion
1330 (end-of-line)
1331 (let ((eol (point))
1332 bol prev-bol ref
1333 before-context after-context)
1334 (back-to-indentation)
1335 (setq bol (point))
1336 (catch 'indent
1337 ;; Move backwards until the start of a non-blank line that is
1338 ;; not inside a token.
1339 (while (progn
1340 (when (= (forward-line -1) -1)
1341 (throw 'indent 0))
1342 (back-to-indentation)
1343 (if (looking-at "[ \t]*$")
1345 (or prev-bol
1346 (setq prev-bol (point)))
1347 (nxml-token-after)
1348 (not (or (= xmltok-start (point))
1349 (eq xmltok-type 'data))))))
1350 (setq ref (point))
1351 ;; Now scan over tokens until the end of the line to be indented.
1352 ;; Determine the context before and after the beginning of the
1353 ;; line.
1354 (while (< (point) eol)
1355 (nxml-tokenize-forward)
1356 (cond ((<= bol xmltok-start)
1357 (setq after-context
1358 (nxml-merge-indent-context-type after-context)))
1359 ((and (<= (point) bol)
1360 (not (and (eq xmltok-type 'partial-start-tag)
1361 (= (point) bol))))
1362 (setq before-context
1363 (nxml-merge-indent-context-type before-context)))
1364 ((eq xmltok-type 'data)
1365 (setq before-context
1366 (nxml-merge-indent-context-type before-context))
1367 (setq after-context
1368 (nxml-merge-indent-context-type after-context)))
1369 ;; If in the middle of a token that looks inline,
1370 ;; then indent relative to the previous non-blank line
1371 ((eq (nxml-merge-indent-context-type before-context)
1372 'mixed)
1373 (goto-char prev-bol)
1374 (throw 'indent (current-column)))
1376 (throw 'indent
1377 (nxml-compute-indent-in-token bol))))
1378 (skip-chars-forward " \t\r\n"))
1379 (goto-char ref)
1380 (+ (current-column)
1381 (* nxml-child-indent
1382 (+ (if (eq before-context 'start-tag) 1 0)
1383 (if (eq after-context 'end-tag) -1 0))))))))
1385 (defun nxml-merge-indent-context-type (context)
1386 "Merge the indent context type CONTEXT with the token in `xmltok-type'.
1387 Return the merged indent context type. An indent context type is
1388 either nil or one of the symbols `start-tag', `end-tag', `markup',
1389 `comment', `mixed'."
1390 (cond ((memq xmltok-type '(start-tag partial-start-tag))
1391 (if (memq context '(nil start-tag comment))
1392 'start-tag
1393 'mixed))
1394 ((memq xmltok-type '(end-tag partial-end-tag))
1395 (if (memq context '(nil end-tag comment))
1396 'end-tag
1397 'mixed))
1398 ((eq xmltok-type 'comment)
1399 (cond ((memq context '(start-tag end-tag comment))
1400 context)
1401 (context 'mixed)
1402 (t 'comment)))
1403 (context 'mixed)
1404 (t 'markup)))
1406 (defun nxml-compute-indent-in-token (pos)
1407 "Return the indent for a line that starts inside a token.
1408 POS is the position of the first non-whitespace character of the line.
1409 This expects the xmltok-* variables to be set up as by `xmltok-forward'."
1410 (cond ((memq xmltok-type '(start-tag
1411 partial-start-tag
1412 empty-element
1413 partial-empty-element))
1414 (nxml-compute-indent-in-start-tag pos))
1415 ((eq xmltok-type 'comment)
1416 (nxml-compute-indent-in-delimited-token pos "<!--" "-->"))
1417 ((eq xmltok-type 'cdata-section)
1418 (nxml-compute-indent-in-delimited-token pos "<![CDATA[" "]]>"))
1419 ((eq xmltok-type 'processing-instruction)
1420 (nxml-compute-indent-in-delimited-token pos "<?" "?>"))
1422 (goto-char pos)
1423 (if (and (= (forward-line -1) 0)
1424 (< xmltok-start (point)))
1425 (back-to-indentation)
1426 (goto-char xmltok-start))
1427 (current-column))))
1429 (defun nxml-compute-indent-in-start-tag (pos)
1430 "Return the indent for a line that starts inside a start-tag.
1431 Also for a line that starts inside an empty element.
1432 POS is the position of the first non-whitespace character of the line.
1433 This expects the xmltok-* variables to be set up as by `xmltok-forward'."
1434 (let ((value-boundary (nxml-attribute-value-boundary pos))
1435 (off 0))
1436 (if value-boundary
1437 ;; inside an attribute value
1438 (let ((value-start (car value-boundary)))
1439 (goto-char pos)
1440 (forward-line -1)
1441 (if (< (point) value-start)
1442 (goto-char value-start)
1443 (back-to-indentation)))
1444 ;; outside an attribute value
1445 (goto-char pos)
1446 (while (and (= (forward-line -1) 0)
1447 (nxml-attribute-value-boundary (point))))
1448 (cond ((<= (point) xmltok-start)
1449 (goto-char xmltok-start)
1450 (setq off nxml-attribute-indent)
1451 (let ((atts (xmltok-merge-attributes)))
1452 (when atts
1453 (let* ((att (car atts))
1454 (start (xmltok-attribute-name-start att)))
1455 (when (< start pos)
1456 (goto-char start)
1457 (setq off 0))))))
1459 (back-to-indentation))))
1460 (+ (current-column) off)))
1462 (defun nxml-attribute-value-boundary (pos)
1463 "Return a pair (START . END) if POS is inside an attribute value.
1464 Otherwise return nil. START and END are the positions of the start
1465 and end of the attribute value containing POS. This expects the
1466 xmltok-* variables to be set up as by `xmltok-forward'."
1467 (let ((atts (xmltok-merge-attributes))
1468 att value-start value-end value-boundary)
1469 (while atts
1470 (setq att (car atts))
1471 (setq value-start (xmltok-attribute-value-start att))
1472 (setq value-end (xmltok-attribute-value-end att))
1473 (cond ((and value-start (< pos value-start))
1474 (setq atts nil))
1475 ((and value-start value-end (<= pos value-end))
1476 (setq value-boundary (cons value-start value-end))
1477 (setq atts nil))
1478 (t (setq atts (cdr atts)))))
1479 value-boundary))
1481 (defun nxml-compute-indent-in-delimited-token (pos open-delim close-delim)
1482 "Return the indent for a line that starts inside a token with delimiters.
1483 OPEN-DELIM and CLOSE-DELIM are strings giving the opening and closing
1484 delimiters. POS is the position of the first non-whitespace character
1485 of the line. This expects the xmltok-* variables to be set up as by
1486 `xmltok-forward'."
1487 (cond ((let ((end (+ pos (length close-delim))))
1488 (and (<= end (point-max))
1489 (string= (buffer-substring-no-properties pos end)
1490 close-delim)))
1491 (goto-char xmltok-start))
1492 ((progn
1493 (goto-char pos)
1494 (forward-line -1)
1495 (<= (point) xmltok-start))
1496 (goto-char (+ xmltok-start (length open-delim)))
1497 (when (and (string= open-delim "<!--")
1498 (looking-at " "))
1499 (goto-char (1+ (point)))))
1500 (t (back-to-indentation)))
1501 (current-column))
1503 (define-obsolete-function-alias 'nxml-complete #'completion-at-point "26.1")
1505 ;;; Movement
1507 (defun nxml-forward-balanced-item (&optional arg)
1508 "Move forward across one balanced item.
1509 With ARG, do it that many times. Negative arg -N means
1510 move backward across N balanced expressions.
1511 This is the equivalent of `forward-sexp' for XML.
1513 An element contains as items strings with no markup, tags, processing
1514 instructions, comments, CDATA sections, entity references and
1515 characters references. However, if the variable
1516 `nxml-sexp-element-flag' is non-nil, then an element is treated as a
1517 single markup item. A start-tag contains an element name followed by
1518 one or more attributes. An end-tag contains just an element name.
1519 An attribute value literals contains strings with no markup, entity
1520 references and character references. A processing instruction
1521 consists of a target and a content string. A comment or a CDATA
1522 section contains a single string. An entity reference contains a
1523 single name. A character 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