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