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