Commit | Line | Data |
---|---|---|
8cd39fb3 MH |
1 | ;;; nxml-mode.el --- a new XML mode |
2 | ||
114f9c96 | 3 | ;; Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010 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." |
eba5b4dd | 48 | :group 'languages) |
8cd39fb3 | 49 | |
92bbfd0d | 50 | (defgroup nxml-faces nil |
8cd39fb3 | 51 | "Faces for XML syntax highlighting." |
ff90f4b0 | 52 | :group 'nxml) |
8cd39fb3 | 53 | |
8cd39fb3 MH |
54 | (defcustom nxml-char-ref-display-glyph-flag t |
55 | "*Non-nil means display glyph following character reference. | |
92bbfd0d | 56 | The glyph is displayed in face `nxml-glyph'. The hook |
8cd39fb3 MH |
57 | `nxml-glyph-set-hook' can be used to customize for which characters |
58 | glyphs are displayed." | |
59 | :group 'nxml | |
60 | :type 'boolean) | |
61 | ||
62 | (defcustom nxml-mode-hook nil | |
63 | "Hook run by command `nxml-mode'." | |
64 | :group 'nxml | |
65 | :type 'hook) | |
66 | ||
67 | (defcustom nxml-sexp-element-flag nil | |
68 | "*Non-nil means sexp commands treat an element as a single expression." | |
69 | :group 'nxml | |
70 | :type 'boolean) | |
71 | ||
72 | (defcustom nxml-slash-auto-complete-flag nil | |
73 | "*Non-nil means typing a slash automatically completes the end-tag. | |
74 | This is used by `nxml-electric-slash'." | |
75 | :group 'nxml | |
76 | :type 'boolean) | |
77 | ||
78 | (defcustom nxml-child-indent 2 | |
79 | "*Indentation for the children of an element relative to the start-tag. | |
80 | This only applies when the line or lines containing the start-tag contains | |
81 | nothing else other than that start-tag." | |
82 | :group 'nxml | |
83 | :type 'integer) | |
84 | ||
85 | (defcustom nxml-attribute-indent 4 | |
86 | "*Indentation for the attributes of an element relative to the start-tag. | |
10545bd8 JB |
87 | This only applies when the first attribute of a tag starts a line. |
88 | In other cases, the first attribute on one line is indented the same | |
89 | as the first attribute on the previous line." | |
8cd39fb3 MH |
90 | :group 'nxml |
91 | :type 'integer) | |
92 | ||
8cd39fb3 MH |
93 | (defcustom nxml-bind-meta-tab-to-complete-flag (not window-system) |
94 | "*Non-nil means bind M-TAB in `nxml-mode-map' to `nxml-complete'. | |
95 | C-return will be bound to `nxml-complete' in any case. | |
96 | M-TAB gets swallowed by many window systems/managers, and | |
97 | `documentation' will show M-TAB rather than C-return as the | |
10545bd8 | 98 | binding for `nxml-complete' when both are bound. So it's better |
8cd39fb3 MH |
99 | to bind M-TAB only when it will work." |
100 | :group 'nxml | |
101 | :set (lambda (sym flag) | |
102 | (set-default sym flag) | |
103 | (when (and (boundp 'nxml-mode-map) nxml-mode-map) | |
104 | (define-key nxml-mode-map "\M-\t" (and flag 'nxml-complete)))) | |
105 | :type 'boolean) | |
106 | ||
107 | (defcustom nxml-prefer-utf-16-to-utf-8-flag nil | |
108 | "*Non-nil means prefer UTF-16 to UTF-8 when saving a buffer. | |
109 | This is used only when a buffer does not contain an encoding declaration | |
110 | and when its current `buffer-file-coding-system' specifies neither UTF-16 | |
111 | nor UTF-8." | |
112 | :group 'nxml | |
113 | :type 'boolean) | |
114 | ||
115 | (defcustom nxml-prefer-utf-16-little-to-big-endian-flag (eq system-type | |
116 | 'windows-nt) | |
117 | "*Non-nil means prefer little-endian to big-endian byte-order for UTF-16. | |
118 | This is used only for saving a buffer; when reading the byte-order is | |
119 | auto-detected. It may be relevant both when there is no encoding declaration | |
120 | and when the encoding declaration specifies `UTF-16'." | |
121 | :group 'nxml | |
122 | :type 'boolean) | |
123 | ||
124 | (defcustom nxml-default-buffer-file-coding-system nil | |
125 | "*Default value for `buffer-file-coding-system' for a buffer for a new file. | |
10545bd8 | 126 | A value of nil means use the default value of `buffer-file-coding-system' as normal. |
8cd39fb3 MH |
127 | A buffer's `buffer-file-coding-system' affects what \\[nxml-insert-xml-declaration] inserts." |
128 | :group 'nxml | |
129 | :type 'coding-system) | |
130 | ||
131 | (defcustom nxml-auto-insert-xml-declaration-flag nil | |
132 | "*Non-nil means automatically insert an XML declaration in a new file. | |
133 | The XML declaration is inserted using `nxml-insert-xml-declaration'." | |
134 | :group 'nxml | |
135 | :type 'boolean) | |
136 | ||
92bbfd0d JR |
137 | (defface nxml-delimited-data |
138 | '((t (:inherit font-lock-doc-face))) | |
8cd39fb3 | 139 | "Face used to highlight data enclosed between delimiters. |
92bbfd0d JR |
140 | This is not used directly, but only via inheritance by other faces." |
141 | :group 'nxml-faces) | |
8cd39fb3 | 142 | |
92bbfd0d JR |
143 | (defface nxml-name |
144 | '((t (:inherit font-lock-builtin-face))) | |
8cd39fb3 MH |
145 | "Face used to highlight various names. |
146 | This includes element and attribute names, processing | |
147 | instruction targets and the CDATA keyword in a CDATA section. | |
148 | This is not used directly, but only via inheritance by other faces." | |
92bbfd0d | 149 | :group 'nxml-faces) |
8cd39fb3 | 150 | |
92bbfd0d JR |
151 | (defface nxml-ref |
152 | '((t (:inherit font-lock-constant-face))) | |
8cd39fb3 MH |
153 | "Face used to highlight character and entity references. |
154 | This is not used directly, but only via inheritance by other faces." | |
92bbfd0d | 155 | :group 'nxml-faces) |
8cd39fb3 | 156 | |
92bbfd0d JR |
157 | (defface nxml-delimiter |
158 | nil | |
8cd39fb3 MH |
159 | "Face used to highlight delimiters. |
160 | This is not used directly, but only via inheritance by other faces." | |
92bbfd0d | 161 | :group 'nxml-faces) |
8cd39fb3 | 162 | |
92bbfd0d | 163 | (defface nxml-text |
8cd39fb3 MH |
164 | nil |
165 | "Face used to highlight text." | |
92bbfd0d | 166 | :group 'nxml-faces) |
8cd39fb3 | 167 | |
92bbfd0d JR |
168 | (defface nxml-comment-content |
169 | '((t (:inherit font-lock-comment-face))) | |
8cd39fb3 | 170 | "Face used to highlight the content of comments." |
92bbfd0d | 171 | :group 'nxml-faces) |
8cd39fb3 | 172 | |
92bbfd0d JR |
173 | (defface nxml-comment-delimiter |
174 | '((t (:inherit font-lock-comment-delimiter-face))) | |
8cd39fb3 | 175 | "Face used for the delimiters of comments, i.e <!-- and -->." |
92bbfd0d | 176 | :group 'nxml-faces) |
8cd39fb3 | 177 | |
92bbfd0d JR |
178 | (defface nxml-processing-instruction-delimiter |
179 | '((t (:inherit nxml-delimiter))) | |
8cd39fb3 | 180 | "Face used for the delimiters of processing instructions, i.e <? and ?>." |
92bbfd0d | 181 | :group 'nxml-faces) |
8cd39fb3 | 182 | |
92bbfd0d JR |
183 | (defface nxml-processing-instruction-target |
184 | '((t (:inherit font-lock-keyword-face))) | |
8cd39fb3 | 185 | "Face used for the target of processing instructions." |
92bbfd0d | 186 | :group 'nxml-faces) |
8cd39fb3 | 187 | |
92bbfd0d JR |
188 | (defface nxml-processing-instruction-content |
189 | '((t (:inherit nxml-delimited-data))) | |
8cd39fb3 | 190 | "Face used for the content of processing instructions." |
92bbfd0d | 191 | :group 'nxml-faces) |
8cd39fb3 | 192 | |
92bbfd0d JR |
193 | (defface nxml-cdata-section-delimiter |
194 | '((t (:inherit nxml-delimiter))) | |
8cd39fb3 | 195 | "Face used for the delimiters of CDATA sections, i.e <![, [, and ]]>." |
92bbfd0d | 196 | :group 'nxml-faces) |
8cd39fb3 | 197 | |
92bbfd0d JR |
198 | (defface nxml-cdata-section-CDATA |
199 | '((t (:inherit nxml-name))) | |
8cd39fb3 | 200 | "Face used for the CDATA keyword in CDATA sections." |
92bbfd0d | 201 | :group 'nxml-faces) |
8cd39fb3 | 202 | |
92bbfd0d JR |
203 | (defface nxml-cdata-section-content |
204 | '((t (:inherit nxml-text))) | |
8cd39fb3 | 205 | "Face used for the content of CDATA sections." |
92bbfd0d | 206 | :group 'nxml-faces) |
8cd39fb3 | 207 | |
92bbfd0d JR |
208 | (defface nxml-char-ref-number |
209 | '((t (:inherit nxml-ref))) | |
8cd39fb3 MH |
210 | "Face used for the number in character references. |
211 | This includes ths `x' in hex references." | |
92bbfd0d | 212 | :group 'nxml-faces) |
8cd39fb3 | 213 | |
92bbfd0d JR |
214 | (defface nxml-char-ref-delimiter |
215 | '((t (:inherit nxml-ref))) | |
8cd39fb3 | 216 | "Face used for the delimiters of character references, i.e &# and ;." |
92bbfd0d | 217 | :group 'nxml-faces) |
8cd39fb3 | 218 | |
92bbfd0d JR |
219 | (defface nxml-entity-ref-name |
220 | '((t (:inherit nxml-ref))) | |
8cd39fb3 | 221 | "Face used for the entity name in general entity references." |
92bbfd0d | 222 | :group 'nxml-faces) |
8cd39fb3 | 223 | |
92bbfd0d JR |
224 | (defface nxml-entity-ref-delimiter |
225 | '((t (:inherit nxml-ref))) | |
8cd39fb3 | 226 | "Face used for the delimiters of entity references, i.e & and ;." |
92bbfd0d | 227 | :group 'nxml-faces) |
8cd39fb3 | 228 | |
92bbfd0d JR |
229 | (defface nxml-tag-delimiter |
230 | '((t (:inherit nxml-delimiter))) | |
8cd39fb3 | 231 | "Face used for the angle brackets delimiting tags. |
92bbfd0d JR |
232 | `nxml-tag-slash' is used for slashes." |
233 | :group 'nxml-faces) | |
8cd39fb3 | 234 | |
92bbfd0d JR |
235 | (defface nxml-tag-slash |
236 | '((t (:inherit nxml-tag-delimiter))) | |
8cd39fb3 | 237 | "Face used for slashes in tags, both in end-tags and empty-elements." |
92bbfd0d | 238 | :group 'nxml-faces) |
8cd39fb3 | 239 | |
92bbfd0d JR |
240 | (defface nxml-element-prefix |
241 | '((t (:inherit nxml-name))) | |
8cd39fb3 | 242 | "Face used for the prefix of elements." |
92bbfd0d | 243 | :group 'nxml-faces) |
8cd39fb3 | 244 | |
92bbfd0d JR |
245 | (defface nxml-element-colon |
246 | nil | |
8cd39fb3 | 247 | "Face used for the colon in element names." |
92bbfd0d | 248 | :group 'nxml-faces) |
8cd39fb3 | 249 | |
92bbfd0d JR |
250 | (defface nxml-element-local-name |
251 | '((t (:inherit font-lock-function-name-face))) | |
8cd39fb3 | 252 | "Face used for the local name of elements." |
92bbfd0d | 253 | :group 'nxml-faces) |
8cd39fb3 | 254 | |
92bbfd0d JR |
255 | (defface nxml-attribute-prefix |
256 | '((t (:inherit nxml-name))) | |
8cd39fb3 | 257 | "Face used for the prefix of attributes." |
92bbfd0d | 258 | :group 'nxml-faces) |
8cd39fb3 | 259 | |
92bbfd0d JR |
260 | (defface nxml-attribute-colon |
261 | '((t (:inherit nxml-delimiter))) | |
8cd39fb3 | 262 | "Face used for the colon in attribute names." |
92bbfd0d | 263 | :group 'nxml-faces) |
10545bd8 | 264 | |
92bbfd0d JR |
265 | (defface nxml-attribute-local-name |
266 | '((t (:inherit font-lock-variable-name-face))) | |
8cd39fb3 | 267 | "Face used for the local name of attributes." |
92bbfd0d | 268 | :group 'nxml-faces) |
8cd39fb3 | 269 | |
92bbfd0d JR |
270 | (defface nxml-namespace-attribute-xmlns |
271 | '((t (:inherit nxml-attribute-prefix))) | |
8cd39fb3 | 272 | "Face used for `xmlns' in namespace attributes." |
92bbfd0d | 273 | :group 'nxml-faces) |
8cd39fb3 | 274 | |
92bbfd0d JR |
275 | (defface nxml-namespace-attribute-colon |
276 | '((t (:inherit nxml-attribute-colon))) | |
8cd39fb3 | 277 | "Face used for the colon in namespace attributes." |
92bbfd0d | 278 | :group 'nxml-faces) |
8cd39fb3 | 279 | |
92bbfd0d JR |
280 | (defface nxml-namespace-attribute-prefix |
281 | '((t (:inherit nxml-attribute-local-name))) | |
8cd39fb3 | 282 | "Face used for the prefix declared in namespace attributes." |
92bbfd0d | 283 | :group 'nxml-faces) |
8cd39fb3 | 284 | |
92bbfd0d JR |
285 | (defface nxml-attribute-value |
286 | '((t (:inherit font-lock-string-face))) | |
8cd39fb3 | 287 | "Face used for the value of attributes." |
92bbfd0d | 288 | :group 'nxml-faces) |
8cd39fb3 | 289 | |
92bbfd0d JR |
290 | (defface nxml-attribute-value-delimiter |
291 | '((t (:inherit nxml-attribute-value))) | |
8cd39fb3 | 292 | "Face used for the delimiters of attribute values." |
92bbfd0d | 293 | :group 'nxml-faces) |
8cd39fb3 | 294 | |
92bbfd0d JR |
295 | (defface nxml-namespace-attribute-value |
296 | '((t (:inherit nxml-attribute-value))) | |
8cd39fb3 | 297 | "Face used for the value of namespace attributes." |
92bbfd0d | 298 | :group 'nxml-faces) |
8cd39fb3 | 299 | |
92bbfd0d JR |
300 | (defface nxml-namespace-attribute-value-delimiter |
301 | '((t (:inherit nxml-attribute-value-delimiter))) | |
8cd39fb3 | 302 | "Face used for the delimiters of namespace attribute values." |
92bbfd0d | 303 | :group 'nxml-faces) |
8cd39fb3 | 304 | |
92bbfd0d JR |
305 | (defface nxml-prolog-literal-delimiter |
306 | '((t (:inherit nxml-delimited-data))) | |
8cd39fb3 | 307 | "Face used for the delimiters of literals in the prolog." |
92bbfd0d | 308 | :group 'nxml-faces) |
8cd39fb3 | 309 | |
92bbfd0d JR |
310 | (defface nxml-prolog-literal-content |
311 | '((t (:inherit nxml-delimited-data))) | |
8cd39fb3 | 312 | "Face used for the content of literals in the prolog." |
92bbfd0d | 313 | :group 'nxml-faces) |
8cd39fb3 | 314 | |
92bbfd0d JR |
315 | (defface nxml-prolog-keyword |
316 | '((t (:inherit font-lock-keyword-face))) | |
8cd39fb3 | 317 | "Face used for keywords in the prolog." |
92bbfd0d | 318 | :group 'nxml-faces) |
8cd39fb3 | 319 | |
92bbfd0d JR |
320 | (defface nxml-markup-declaration-delimiter |
321 | '((t (:inherit nxml-delimiter))) | |
8cd39fb3 MH |
322 | "Face used for the delimiters of markup declarations in the prolog. |
323 | The delimiters are <! and >." | |
92bbfd0d | 324 | :group 'nxml-faces) |
8cd39fb3 | 325 | |
92bbfd0d JR |
326 | (defface nxml-hash |
327 | '((t (:inherit nxml-name))) | |
8cd39fb3 | 328 | "Face used for # before a name in the prolog." |
92bbfd0d | 329 | :group 'nxml-faces) |
8cd39fb3 | 330 | |
92bbfd0d | 331 | (defface nxml-glyph |
8cd39fb3 MH |
332 | '((((type x)) |
333 | (:family | |
334 | "misc-fixed" | |
335 | :background | |
336 | "light grey" | |
337 | :foreground | |
338 | "black" | |
339 | :weight | |
10545bd8 | 340 | normal |
8cd39fb3 MH |
341 | :slant |
342 | normal)) | |
343 | (t | |
344 | (:background | |
345 | "light grey" | |
346 | :foreground | |
347 | "black" | |
348 | :weight | |
10545bd8 | 349 | normal |
8cd39fb3 MH |
350 | :slant |
351 | normal))) | |
352 | "Face used for glyph for char references." | |
92bbfd0d | 353 | :group 'nxml-faces) |
8cd39fb3 MH |
354 | |
355 | ;;; Global variables | |
356 | ||
8cd39fb3 MH |
357 | (defvar nxml-prolog-regions nil |
358 | "List of regions in the prolog to be fontified. | |
359 | See the function `xmltok-forward-prolog' for more information.") | |
360 | (make-variable-buffer-local 'nxml-prolog-regions) | |
361 | ||
362 | (defvar nxml-last-fontify-end nil | |
363 | "Position where fontification last ended. | |
10545bd8 | 364 | It is nil if the buffer changed since the last fontification.") |
8cd39fb3 MH |
365 | (make-variable-buffer-local 'nxml-last-fontify-end) |
366 | ||
367 | (defvar nxml-degraded nil | |
368 | "Non-nil if currently operating in degraded mode. | |
369 | Degraded mode is enabled when an internal error is encountered in the | |
370 | fontification or after-change functions.") | |
371 | (make-variable-buffer-local 'nxml-degraded) | |
372 | ||
373 | (defvar nxml-completion-hook nil | |
374 | "Hook run by `nxml-complete'. | |
375 | This hook is run until success.") | |
376 | ||
377 | (defvar nxml-in-mixed-content-hook nil | |
378 | "Hook to determine whether point is in mixed content. | |
379 | The hook is called without arguments. It should return nil if it is | |
380 | definitely not mixed; non-nil otherwise. The hook will be run until | |
381 | one of the functions returns nil.") | |
382 | ||
383 | (defvar nxml-mixed-scan-distance 4000 | |
384 | "Maximum distance from point to scan when checking for mixed content.") | |
385 | ||
386 | (defvar nxml-end-tag-indent-scan-distance 4000 | |
387 | "Maximum distance from point to scan backwards when indenting end-tag.") | |
388 | ||
389 | (defvar nxml-char-ref-extra-display t | |
390 | "Non-nil means display extra information for character references. | |
391 | The extra information consists of a tooltip with the character name | |
392 | and, if `nxml-char-ref-display-glyph-flag' is non-nil, a glyph | |
393 | corresponding to the referenced character following the character | |
394 | reference.") | |
395 | (make-variable-buffer-local 'nxml-char-ref-extra-display) | |
396 | ||
397 | (defvar nxml-mode-map | |
398 | (let ((map (make-sparse-keymap))) | |
399 | (define-key map "\M-\C-u" 'nxml-backward-up-element) | |
400 | (define-key map "\M-\C-d" 'nxml-down-element) | |
401 | (define-key map "\M-\C-n" 'nxml-forward-element) | |
402 | (define-key map "\M-\C-p" 'nxml-backward-element) | |
403 | (define-key map "\M-{" 'nxml-backward-paragraph) | |
404 | (define-key map "\M-}" 'nxml-forward-paragraph) | |
405 | (define-key map "\M-h" 'nxml-mark-paragraph) | |
406 | (define-key map "\C-c\C-f" 'nxml-finish-element) | |
8a79905d | 407 | (define-key map "\C-c]" 'nxml-finish-element) |
f8e63691 | 408 | (define-key map "\C-c/" 'nxml-finish-element) |
8cd39fb3 MH |
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. |
441 | C-c C-i closes a start-tag with `>' and then inserts a balancing end-tag | |
10545bd8 | 442 | leaving 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: |
444 | the start-tag, point, and end-tag are all left on separate lines. | |
445 | If `nxml-slash-auto-complete-flag' is non-nil, then inserting a `</' | |
446 | automatically 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 | |
451 | to choose a tag to put around the word preceding point. | |
452 | ||
453 | Sections of the document can be displayed in outline form. The | |
454 | variable `nxml-section-element-name-regexp' controls when an element | |
455 | is recognized as a section. The same key sequences that change | |
456 | visibility in outline mode are used except that they start with C-c C-o | |
457 | instead of C-c. | |
458 | ||
459 | Validation is provided by the related minor-mode `rng-validate-mode'. | |
460 | This also makes completion schema- and context- sensitive. Element | |
461 | names, attribute names, attribute values and namespace URIs can all be | |
10545bd8 JB |
462 | completed. By default, `rng-validate-mode' is automatically enabled. |
463 | You can toggle it using \\[rng-validate-mode] or change the default by | |
6bd1e223 | 464 | customizing `rng-nxml-auto-validate-flag'. |
8cd39fb3 MH |
465 | |
466 | \\[indent-for-tab-command] indents the current line appropriately. | |
467 | This can be customized using the variable `nxml-child-indent' | |
468 | and the variable `nxml-attribute-indent'. | |
469 | ||
470 | \\[nxml-insert-named-char] inserts a character reference using | |
10545bd8 JB |
471 | the character's name (by default, the Unicode name). |
472 | \\[universal-argument] \\[nxml-insert-named-char] inserts the character directly. | |
8cd39fb3 MH |
473 | |
474 | The Emacs commands that normally operate on balanced expressions will | |
475 | operate on XML markup items. Thus \\[forward-sexp] will move forward | |
476 | across one markup item; \\[backward-sexp] will move backward across | |
477 | one markup item; \\[kill-sexp] will kill the following markup item; | |
478 | \\[mark-sexp] will mark the following markup item. By default, each | |
479 | tag each treated as a single markup item; to make the complete element | |
480 | be 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 | ||
486 | Many 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. |
613 | Returns a cons cell containing a possibly-enlarged change region. | |
614 | You must call `nxml-extend-region' on this expanded region to obtain | |
615 | the full extent of the area needing refontification. | |
e8ec402f MO |
616 | |
617 | For bookkeeping, call this function even when fontification is | |
618 | disabled." | |
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. | |
655 | The 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 | 843 | Called 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) | |
116bd1ee CY |
885 | (let ((region (nxml-with-degradation-on-error |
886 | '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 | (if (consp region) region)))) | |
e8ec402f MO |
896 | |
897 | (defun nxml-extend-after-change-region1 (start end pre-change-length) | |
898 | (let* ((region (nxml-after-change1 start end pre-change-length)) | |
899 | (font-lock-beg (car region)) | |
900 | (font-lock-end (cdr region))) | |
901 | ||
902 | (nxml-extend-region) | |
903 | (cons font-lock-beg font-lock-end))) | |
904 | ||
905 | (defun nxml-fontify-matcher (bound) | |
906 | "Called as font-lock keyword matcher." | |
907 | ||
908 | (unless nxml-degraded | |
909 | (nxml-debug-change "nxml-fontify-matcher" (point) bound) | |
910 | ||
911 | (when (< (point) nxml-prolog-end) | |
912 | ;; prolog needs to be fontified in one go, and | |
913 | ;; nxml-extend-region makes sure we start at BOB. | |
914 | (assert (bobp)) | |
915 | (nxml-fontify-prolog) | |
916 | (goto-char nxml-prolog-end)) | |
917 | ||
918 | (let (xmltok-dependent-regions | |
919 | xmltok-errors) | |
920 | (while (and (nxml-tokenize-forward) | |
921 | (<= (point) bound)) ; intervals are open-ended | |
922 | (nxml-apply-fontify-rule))) | |
923 | ||
924 | (setq nxml-last-fontify-end (point))) | |
925 | ||
926 | ;; Since we did the fontification internally, tell font-lock to not | |
927 | ;; do anything itself. | |
928 | nil) | |
8cd39fb3 MH |
929 | |
930 | (defun nxml-fontify-prolog () | |
931 | "Fontify the prolog. | |
932 | The buffer is assumed to be prepared for fontification. | |
933 | This does not set the fontified property, but it does clear | |
934 | faces appropriately." | |
935 | (let ((regions nxml-prolog-regions)) | |
8cd39fb3 MH |
936 | (while regions |
937 | (let ((region (car regions))) | |
938 | (nxml-apply-fontify-rule (aref region 0) | |
939 | (aref region 1) | |
940 | (aref region 2))) | |
941 | (setq regions (cdr regions))))) | |
942 | ||
8cd39fb3 MH |
943 | ;; Vectors identify a substring of the token to be highlighted in some face. |
944 | ||
945 | ;; Token types returned by xmltok-forward. | |
946 | ||
947 | (put 'start-tag | |
948 | 'nxml-fontify-rule | |
92bbfd0d JR |
949 | '([nil 1 nxml-tag-delimiter] |
950 | [-1 nil nxml-tag-delimiter] | |
8cd39fb3 MH |
951 | (element-qname . 1) |
952 | attributes)) | |
953 | ||
954 | (put 'partial-start-tag | |
955 | 'nxml-fontify-rule | |
92bbfd0d | 956 | '([nil 1 nxml-tag-delimiter] |
8cd39fb3 MH |
957 | (element-qname . 1) |
958 | attributes)) | |
959 | ||
960 | (put 'end-tag | |
961 | 'nxml-fontify-rule | |
92bbfd0d JR |
962 | '([nil 1 nxml-tag-delimiter] |
963 | [1 2 nxml-tag-slash] | |
964 | [-1 nil nxml-tag-delimiter] | |
8cd39fb3 MH |
965 | (element-qname . 2))) |
966 | ||
967 | (put 'partial-end-tag | |
968 | 'nxml-fontify-rule | |
92bbfd0d JR |
969 | '([nil 1 nxml-tag-delimiter] |
970 | [1 2 nxml-tag-slash] | |
8cd39fb3 MH |
971 | (element-qname . 2))) |
972 | ||
973 | (put 'empty-element | |
974 | 'nxml-fontify-rule | |
92bbfd0d JR |
975 | '([nil 1 nxml-tag-delimiter] |
976 | [-2 -1 nxml-tag-slash] | |
977 | [-1 nil nxml-tag-delimiter] | |
8cd39fb3 MH |
978 | (element-qname . 1) |
979 | attributes)) | |
980 | ||
981 | (put 'partial-empty-element | |
982 | 'nxml-fontify-rule | |
92bbfd0d JR |
983 | '([nil 1 nxml-tag-delimiter] |
984 | [-1 nil nxml-tag-slash] | |
8cd39fb3 MH |
985 | (element-qname . 1) |
986 | attributes)) | |
987 | ||
988 | (put 'char-ref | |
989 | 'nxml-fontify-rule | |
92bbfd0d JR |
990 | '([nil 2 nxml-char-ref-delimiter] |
991 | [2 -1 nxml-char-ref-number] | |
992 | [-1 nil nxml-char-ref-delimiter] | |
8cd39fb3 MH |
993 | char-ref)) |
994 | ||
995 | (put 'entity-ref | |
996 | 'nxml-fontify-rule | |
92bbfd0d JR |
997 | '([nil 1 nxml-entity-ref-delimiter] |
998 | [1 -1 nxml-entity-ref-name] | |
999 | [-1 nil nxml-entity-ref-delimiter])) | |
8cd39fb3 MH |
1000 | |
1001 | (put 'comment | |
1002 | 'nxml-fontify-rule | |
92bbfd0d JR |
1003 | '([nil 4 nxml-comment-delimiter] |
1004 | [4 -3 nxml-comment-content] | |
1005 | [-3 nil nxml-comment-delimiter])) | |
8cd39fb3 MH |
1006 | |
1007 | (put 'processing-instruction | |
1008 | 'nxml-fontify-rule | |
92bbfd0d JR |
1009 | '([nil 2 nxml-processing-instruction-delimiter] |
1010 | [-2 nil nxml-processing-instruction-delimiter] | |
8cd39fb3 MH |
1011 | processing-instruction-content)) |
1012 | ||
1013 | (put 'cdata-section | |
1014 | 'nxml-fontify-rule | |
92bbfd0d JR |
1015 | '([nil 3 nxml-cdata-section-delimiter] ; <![ |
1016 | [3 8 nxml-cdata-section-CDATA] ; CDATA | |
1017 | [8 9 nxml-cdata-section-delimiter] ; [ | |
1018 | [9 -3 nxml-cdata-section-content] ; ]]> | |
1019 | [-3 nil nxml-cdata-section-delimiter])) | |
8cd39fb3 MH |
1020 | |
1021 | (put 'data | |
1022 | 'nxml-fontify-rule | |
92bbfd0d | 1023 | '([nil nil nxml-text])) |
8cd39fb3 MH |
1024 | |
1025 | ;; Prolog region types in list returned by xmltok-forward-prolog. | |
1026 | ||
1027 | (put 'xml-declaration | |
1028 | 'nxml-fontify-rule | |
92bbfd0d JR |
1029 | '([nil 2 nxml-processing-instruction-delimiter] |
1030 | [2 5 nxml-processing-instruction-target] | |
1031 | [-2 nil nxml-processing-instruction-delimiter])) | |
8cd39fb3 MH |
1032 | |
1033 | (put 'xml-declaration-attribute-name | |
1034 | 'nxml-fontify-rule | |
92bbfd0d | 1035 | '([nil nil nxml-attribute-local-name])) |
8cd39fb3 MH |
1036 | |
1037 | (put 'xml-declaration-attribute-value | |
1038 | 'nxml-fontify-rule | |
92bbfd0d JR |
1039 | '([nil 1 nxml-attribute-value-delimiter] |
1040 | [1 -1 nxml-attribute-value] | |
1041 | [-1 nil nxml-attribute-value-delimiter])) | |
8cd39fb3 MH |
1042 | |
1043 | (put 'processing-instruction-left | |
1044 | 'nxml-fontify-rule | |
92bbfd0d JR |
1045 | '([nil 2 nxml-processing-instruction-delimiter] |
1046 | [2 nil nxml-processing-instruction-target])) | |
8cd39fb3 MH |
1047 | |
1048 | (put 'processing-instruction-right | |
1049 | 'nxml-fontify-rule | |
92bbfd0d JR |
1050 | '([nil -2 nxml-processing-instruction-content] |
1051 | [-2 nil nxml-processing-instruction-delimiter])) | |
8cd39fb3 MH |
1052 | |
1053 | (put 'literal | |
1054 | 'nxml-fontify-rule | |
92bbfd0d JR |
1055 | '([nil 1 nxml-prolog-literal-delimiter] |
1056 | [1 -1 nxml-prolog-literal-content] | |
1057 | [-1 nil nxml-prolog-literal-delimiter])) | |
8cd39fb3 MH |
1058 | |
1059 | (put 'keyword | |
1060 | 'nxml-fontify-rule | |
92bbfd0d | 1061 | '([nil nil nxml-prolog-keyword])) |
8cd39fb3 MH |
1062 | |
1063 | (put 'markup-declaration-open | |
1064 | 'nxml-fontify-rule | |
92bbfd0d JR |
1065 | '([0 2 nxml-markup-declaration-delimiter] |
1066 | [2 nil nxml-prolog-keyword])) | |
8cd39fb3 MH |
1067 | |
1068 | (put 'markup-declaration-close | |
1069 | 'nxml-fontify-rule | |
92bbfd0d | 1070 | '([nil nil nxml-markup-declaration-delimiter])) |
8cd39fb3 MH |
1071 | |
1072 | (put 'internal-subset-open | |
1073 | 'nxml-fontify-rule | |
92bbfd0d | 1074 | '([nil nil nxml-markup-declaration-delimiter])) |
8cd39fb3 MH |
1075 | |
1076 | (put 'internal-subset-close | |
1077 | 'nxml-fontify-rule | |
92bbfd0d JR |
1078 | '([nil 1 nxml-markup-declaration-delimiter] |
1079 | [-1 nil nxml-markup-declaration-delimiter])) | |
8cd39fb3 MH |
1080 | |
1081 | (put 'hash-name | |
1082 | 'nxml-fontify-rule | |
92bbfd0d JR |
1083 | '([nil 1 nxml-hash] |
1084 | [1 nil nxml-prolog-keyword])) | |
8cd39fb3 MH |
1085 | |
1086 | (defun nxml-apply-fontify-rule (&optional type start end) | |
1087 | (let ((rule (get (or type xmltok-type) 'nxml-fontify-rule))) | |
1088 | (unless start (setq start xmltok-start)) | |
1089 | (unless end (setq end (point))) | |
1090 | (while rule | |
1091 | (let* ((action (car rule))) | |
1092 | (setq rule (cdr rule)) | |
1093 | (cond ((vectorp action) | |
1094 | (nxml-set-face (let ((offset (aref action 0))) | |
1095 | (cond ((not offset) start) | |
1096 | ((< offset 0) (+ end offset)) | |
1097 | (t (+ start offset)))) | |
1098 | (let ((offset (aref action 1))) | |
1099 | (cond ((not offset) end) | |
1100 | ((< offset 0) (+ end offset)) | |
1101 | (t (+ start offset)))) | |
1102 | (aref action 2))) | |
1103 | ((and (consp action) | |
1104 | (eq (car action) 'element-qname)) | |
1105 | (when xmltok-name-end ; maybe nil in partial-end-tag case | |
1106 | (nxml-fontify-qname (+ start (cdr action)) | |
1107 | xmltok-name-colon | |
1108 | xmltok-name-end | |
92bbfd0d JR |
1109 | 'nxml-element-prefix |
1110 | 'nxml-element-colon | |
1111 | 'nxml-element-local-name))) | |
8cd39fb3 MH |
1112 | ((eq action 'attributes) |
1113 | (nxml-fontify-attributes)) | |
1114 | ((eq action 'processing-instruction-content) | |
1115 | (nxml-set-face (+ start 2) | |
1116 | xmltok-name-end | |
92bbfd0d | 1117 | 'nxml-processing-instruction-target) |
8cd39fb3 MH |
1118 | (nxml-set-face (save-excursion |
1119 | (goto-char xmltok-name-end) | |
1120 | (skip-chars-forward " \t\r\n") | |
1121 | (point)) | |
1122 | (- end 2) | |
92bbfd0d | 1123 | 'nxml-processing-instruction-content)) |
8cd39fb3 MH |
1124 | ((eq action 'char-ref) |
1125 | (nxml-char-ref-display-extra start | |
1126 | end | |
1127 | (xmltok-char-number start end))) | |
1128 | (t (error "Invalid nxml-fontify-rule action %s" action))))))) | |
1129 | ||
1130 | (defun nxml-fontify-attributes () | |
1131 | (while xmltok-namespace-attributes | |
1132 | (nxml-fontify-attribute (car xmltok-namespace-attributes) | |
1133 | 'namespace) | |
1134 | (setq xmltok-namespace-attributes | |
1135 | (cdr xmltok-namespace-attributes))) | |
1136 | (while xmltok-attributes | |
1137 | (nxml-fontify-attribute (car xmltok-attributes)) | |
1138 | (setq xmltok-attributes | |
1139 | (cdr xmltok-attributes)))) | |
1140 | ||
1141 | (defun nxml-fontify-attribute (att &optional namespace-declaration) | |
1142 | (if namespace-declaration | |
1143 | (nxml-fontify-qname (xmltok-attribute-name-start att) | |
1144 | (xmltok-attribute-name-colon att) | |
1145 | (xmltok-attribute-name-end att) | |
92bbfd0d JR |
1146 | 'nxml-namespace-attribute-xmlns |
1147 | 'nxml-namespace-attribute-colon | |
1148 | 'nxml-namespace-attribute-prefix | |
1149 | 'nxml-namespace-attribute-xmlns) | |
8cd39fb3 MH |
1150 | (nxml-fontify-qname (xmltok-attribute-name-start att) |
1151 | (xmltok-attribute-name-colon att) | |
1152 | (xmltok-attribute-name-end att) | |
92bbfd0d JR |
1153 | 'nxml-attribute-prefix |
1154 | 'nxml-attribute-colon | |
1155 | 'nxml-attribute-local-name)) | |
8cd39fb3 MH |
1156 | (let ((start (xmltok-attribute-value-start att)) |
1157 | (end (xmltok-attribute-value-end att)) | |
1158 | (refs (xmltok-attribute-refs att)) | |
1159 | (delimiter-face (if namespace-declaration | |
92bbfd0d JR |
1160 | 'nxml-namespace-attribute-value-delimiter |
1161 | 'nxml-attribute-value-delimiter)) | |
8cd39fb3 | 1162 | (value-face (if namespace-declaration |
92bbfd0d JR |
1163 | 'nxml-namespace-attribute-value |
1164 | 'nxml-attribute-value))) | |
8cd39fb3 MH |
1165 | (when start |
1166 | (nxml-set-face (1- start) start delimiter-face) | |
1167 | (nxml-set-face end (1+ end) delimiter-face) | |
1168 | (while refs | |
1169 | (let* ((ref (car refs)) | |
1170 | (ref-type (aref ref 0)) | |
1171 | (ref-start (aref ref 1)) | |
1172 | (ref-end (aref ref 2))) | |
1173 | (nxml-set-face start ref-start value-face) | |
1174 | (nxml-apply-fontify-rule ref-type ref-start ref-end) | |
1175 | (setq start ref-end)) | |
1176 | (setq refs (cdr refs))) | |
1177 | (nxml-set-face start end value-face)))) | |
1178 | ||
1179 | (defun nxml-fontify-qname (start | |
1180 | colon | |
1181 | end | |
1182 | prefix-face | |
1183 | colon-face | |
1184 | local-name-face | |
1185 | &optional | |
1186 | unprefixed-face) | |
1187 | (cond (colon (nxml-set-face start colon prefix-face) | |
1188 | (nxml-set-face colon (1+ colon) colon-face) | |
1189 | (nxml-set-face (1+ colon) end local-name-face)) | |
1190 | (t (nxml-set-face start end (or unprefixed-face | |
1191 | local-name-face))))) | |
1192 | ||
1193 | ;;; Editing | |
1194 | ||
1195 | (defun nxml-electric-slash (arg) | |
1196 | "Insert a slash. | |
1197 | ||
1198 | With a prefix ARG, do nothing other than insert the slash. | |
1199 | ||
1200 | Otherwise, if `nxml-slash-auto-complete-flag' is non-nil, insert the | |
1201 | rest of the end-tag or empty-element if the slash is potentially part | |
1202 | of an end-tag or the close of an empty-element. | |
1203 | ||
1204 | If the slash is part of an end-tag that is the first non-whitespace | |
1205 | on the line, reindent the line." | |
1206 | (interactive "*P") | |
1207 | (nxml-ensure-scan-up-to-date) | |
1208 | (let* ((slash-pos (point)) | |
1209 | (end-tag-p (and (eq (char-before slash-pos) ?<) | |
1210 | (not (nxml-get-inside slash-pos)))) | |
1211 | (at-indentation (save-excursion | |
1212 | (back-to-indentation) | |
1213 | (eq (point) (1- slash-pos))))) | |
1214 | (self-insert-command (prefix-numeric-value arg)) | |
1215 | (unless arg | |
1216 | (if nxml-slash-auto-complete-flag | |
1217 | (if end-tag-p | |
1218 | (condition-case err | |
1219 | (let ((start-tag-end | |
1220 | (nxml-scan-element-backward (1- slash-pos) t))) | |
1221 | (when start-tag-end | |
1222 | (insert (xmltok-start-tag-qname) ">") | |
1223 | ;; copy the indentation of the start-tag | |
1224 | (when (and at-indentation | |
1225 | (save-excursion | |
1226 | (goto-char xmltok-start) | |
1227 | (back-to-indentation) | |
1228 | (eq (point) xmltok-start))) | |
1229 | (save-excursion | |
1230 | (indent-line-to (save-excursion | |
1231 | (goto-char xmltok-start) | |
1232 | (current-column))))))) | |
1233 | (nxml-scan-error nil)) | |
1234 | (when (and (eq (nxml-token-before) (point)) | |
1235 | (eq xmltok-type 'partial-empty-element)) | |
1236 | (insert ">"))) | |
1237 | (when (and end-tag-p at-indentation) | |
1238 | (nxml-indent-line)))))) | |
1239 | ||
1240 | (defun nxml-balanced-close-start-tag-block () | |
1241 | "Close the start-tag before point with `>' and insert a balancing end-tag. | |
1242 | Point is left between the start-tag and the end-tag. | |
1243 | If there is nothing but whitespace before the `<' that opens the | |
1244 | start-tag, then put point on a blank line, and put the end-tag on | |
1245 | another line aligned with the start-tag." | |
1246 | (interactive "*") | |
1247 | (nxml-balanced-close-start-tag 'block)) | |
1248 | ||
1249 | (defun nxml-balanced-close-start-tag-inline () | |
1250 | "Close the start-tag before point with `>' and insert a balancing end-tag. | |
1251 | Point is left between the start-tag and the end-tag. | |
1252 | No extra whitespace is inserted." | |
1253 | (interactive "*") | |
1254 | (nxml-balanced-close-start-tag 'inline)) | |
1255 | ||
1256 | (defun nxml-balanced-close-start-tag (block-or-inline) | |
1257 | (let ((token-end (nxml-token-before)) | |
ba07c9ad CY |
1258 | (pos (1+ (point))) |
1259 | (token-start xmltok-start)) | |
8cd39fb3 MH |
1260 | (unless (or (eq xmltok-type 'partial-start-tag) |
1261 | (and (memq xmltok-type '(start-tag | |
1262 | empty-element | |
1263 | partial-empty-element)) | |
1264 | (>= token-end pos))) | |
1265 | (error "Not in a start-tag")) | |
ba07c9ad | 1266 | ;; Note that this insertion changes xmltok-start. |
8cd39fb3 MH |
1267 | (insert "></" |
1268 | (buffer-substring-no-properties (+ xmltok-start 1) | |
1269 | (min xmltok-name-end (point))) | |
1270 | ">") | |
1271 | (if (eq block-or-inline 'inline) | |
1272 | (goto-char pos) | |
ba07c9ad | 1273 | (goto-char token-start) |
8cd39fb3 | 1274 | (back-to-indentation) |
ba07c9ad | 1275 | (if (= (point) token-start) |
8cd39fb3 | 1276 | (let ((indent (current-column))) |
ba07c9ad CY |
1277 | (goto-char pos) |
1278 | (insert "\n") | |
1279 | (indent-line-to indent) | |
1280 | (goto-char pos) | |
1281 | (insert "\n") | |
1282 | (indent-line-to (+ nxml-child-indent indent))) | |
8cd39fb3 | 1283 | (goto-char pos))))) |
10545bd8 | 1284 | |
8cd39fb3 MH |
1285 | (defun nxml-finish-element () |
1286 | "Finish the current element by inserting an end-tag." | |
1287 | (interactive "*") | |
1288 | (nxml-finish-element-1 nil)) | |
1289 | ||
1290 | (defvar nxml-last-split-position nil | |
1291 | "Position where `nxml-split-element' split the current element.") | |
1292 | ||
1293 | (defun nxml-split-element () | |
1294 | "Split the current element by inserting an end-tag and a start-tag. | |
1295 | Point is left after the newly inserted start-tag. When repeated, | |
1296 | split immediately before the previously inserted start-tag and leave | |
1297 | point unchanged." | |
1298 | (interactive "*") | |
1299 | (setq nxml-last-split-position | |
1300 | (if (and (eq last-command this-command) | |
1301 | nxml-last-split-position) | |
1302 | (save-excursion | |
1303 | (goto-char nxml-last-split-position) | |
1304 | (nxml-finish-element-1 t)) | |
1305 | (nxml-finish-element-1 t)))) | |
1306 | ||
1307 | (defun nxml-finish-element-1 (startp) | |
1308 | "Insert an end-tag for the current element and optionally a start-tag. | |
1309 | The start-tag is inserted if STARTP is non-nil. Return the position | |
1310 | of the inserted start-tag or nil if none was inserted." | |
1311 | (interactive "*") | |
1312 | (let* ((token-end (nxml-token-before)) | |
1313 | (start-tag-end | |
1314 | (save-excursion | |
1315 | (when (and (< (point) token-end) | |
1316 | (memq xmltok-type | |
1317 | '(cdata-section | |
1318 | processing-instruction | |
1319 | comment | |
1320 | start-tag | |
1321 | end-tag | |
1322 | empty-element))) | |
1323 | (error "Point is inside a %s" | |
1324 | (nxml-token-type-friendly-name xmltok-type))) | |
1325 | (nxml-scan-element-backward token-end t))) | |
1326 | (starts-line | |
1327 | (save-excursion | |
1328 | (unless (eq xmltok-type 'start-tag) | |
1329 | (error "No matching start-tag")) | |
1330 | (goto-char xmltok-start) | |
1331 | (back-to-indentation) | |
1332 | (eq (point) xmltok-start))) | |
1333 | (ends-line | |
1334 | (save-excursion | |
1335 | (goto-char start-tag-end) | |
1336 | (looking-at "[ \t\r\n]*$"))) | |
1337 | (start-tag-indent (save-excursion | |
1338 | (goto-char xmltok-start) | |
1339 | (current-column))) | |
1340 | (qname (xmltok-start-tag-qname)) | |
1341 | inserted-start-tag-pos) | |
1342 | (when (and starts-line ends-line) | |
1343 | ;; start-tag is on a line by itself | |
1344 | ;; => put the end-tag on a line by itself | |
1345 | (unless (<= (point) | |
1346 | (save-excursion | |
1347 | (back-to-indentation) | |
1348 | (point))) | |
1349 | (insert "\n")) | |
1350 | (indent-line-to start-tag-indent)) | |
1351 | (insert "</" qname ">") | |
1352 | (when startp | |
1353 | (when starts-line | |
1354 | (insert "\n") | |
1355 | (indent-line-to start-tag-indent)) | |
1356 | (setq inserted-start-tag-pos (point)) | |
1357 | (insert "<" qname ">") | |
1358 | (when (and starts-line ends-line) | |
1359 | (insert "\n") | |
1360 | (indent-line-to (save-excursion | |
1361 | (goto-char xmltok-start) | |
1362 | (forward-line 1) | |
1363 | (back-to-indentation) | |
1364 | (if (= (current-column) | |
1365 | (+ start-tag-indent nxml-child-indent)) | |
1366 | (+ start-tag-indent nxml-child-indent) | |
1367 | start-tag-indent))))) | |
1368 | inserted-start-tag-pos)) | |
1369 | ||
1370 | ;;; Indentation | |
1371 | ||
1372 | (defun nxml-indent-line () | |
1373 | "Indent current line as XML." | |
b263a4c4 SM |
1374 | (let* ((savep (point)) |
1375 | (indent (condition-case nil | |
1376 | (save-excursion | |
1377 | (forward-line 0) | |
1378 | (skip-chars-forward " \t") | |
1379 | (if (>= (point) savep) (setq savep nil)) | |
1380 | (or (nxml-compute-indent) 0)) | |
1381 | (error 0)))) | |
1382 | (if (not (numberp indent)) | |
1383 | ;; If something funny is used (e.g. `noindent'), return it. | |
1384 | indent | |
1385 | (if (< indent 0) (setq indent 0)) ;Just in case. | |
1386 | (if savep | |
1387 | (save-excursion (indent-line-to indent)) | |
1388 | (indent-line-to indent))))) | |
8cd39fb3 MH |
1389 | |
1390 | (defun nxml-compute-indent () | |
1391 | "Return the indent for the line containing point." | |
1392 | (or (nxml-compute-indent-from-matching-start-tag) | |
1393 | (nxml-compute-indent-from-previous-line))) | |
1394 | ||
1395 | (defun nxml-compute-indent-from-matching-start-tag () | |
1396 | "Compute the indent for a line with an end-tag using the matching start-tag. | |
1397 | When the line containing point ends with an end-tag and does not start | |
1398 | in the middle of a token, return the indent of the line containing the | |
1399 | matching start-tag, if there is one and it occurs at the beginning of | |
1400 | its line. Otherwise return nil." | |
1401 | (save-excursion | |
1402 | (back-to-indentation) | |
1403 | (let ((bol (point))) | |
1404 | (let ((inhibit-field-text-motion t)) | |
1405 | (end-of-line)) | |
1406 | (skip-chars-backward " \t") | |
1407 | (and (= (nxml-token-before) (point)) | |
1408 | (memq xmltok-type '(end-tag partial-end-tag)) | |
1409 | ;; start of line must not be inside a token | |
1410 | (or (= xmltok-start bol) | |
1411 | (save-excursion | |
1412 | (goto-char bol) | |
1413 | (nxml-token-after) | |
1414 | (= xmltok-start bol)) | |
1415 | (eq xmltok-type 'data)) | |
1416 | (condition-case err | |
1417 | (nxml-scan-element-backward | |
1418 | (point) | |
1419 | nil | |
1420 | (- (point) | |
1421 | nxml-end-tag-indent-scan-distance)) | |
1422 | (nxml-scan-error nil)) | |
1423 | (< xmltok-start bol) | |
1424 | (progn | |
1425 | (goto-char xmltok-start) | |
1426 | (skip-chars-backward " \t") | |
1427 | (bolp)) | |
1428 | (current-indentation))))) | |
1429 | ||
1430 | (defun nxml-compute-indent-from-previous-line () | |
1431 | "Compute the indent for a line using the indentation of a previous line." | |
1432 | (save-excursion | |
1433 | (end-of-line) | |
1434 | (let ((eol (point)) | |
1435 | bol prev-bol ref | |
1436 | before-context after-context) | |
1437 | (back-to-indentation) | |
1438 | (setq bol (point)) | |
1439 | (catch 'indent | |
1440 | ;; Move backwards until the start of a non-blank line that is | |
1441 | ;; not inside a token. | |
1442 | (while (progn | |
1443 | (when (= (forward-line -1) -1) | |
1444 | (throw 'indent 0)) | |
1445 | (back-to-indentation) | |
1446 | (if (looking-at "[ \t]*$") | |
1447 | t | |
1448 | (or prev-bol | |
1449 | (setq prev-bol (point))) | |
1450 | (nxml-token-after) | |
1451 | (not (or (= xmltok-start (point)) | |
1452 | (eq xmltok-type 'data)))))) | |
1453 | (setq ref (point)) | |
1454 | ;; Now scan over tokens until the end of the line to be indented. | |
1455 | ;; Determine the context before and after the beginning of the | |
1456 | ;; line. | |
1457 | (while (< (point) eol) | |
1458 | (nxml-tokenize-forward) | |
1459 | (cond ((<= bol xmltok-start) | |
1460 | (setq after-context | |
1461 | (nxml-merge-indent-context-type after-context))) | |
1462 | ((and (<= (point) bol) | |
1463 | (not (and (eq xmltok-type 'partial-start-tag) | |
1464 | (= (point) bol)))) | |
1465 | (setq before-context | |
1466 | (nxml-merge-indent-context-type before-context))) | |
1467 | ((eq xmltok-type 'data) | |
1468 | (setq before-context | |
1469 | (nxml-merge-indent-context-type before-context)) | |
1470 | (setq after-context | |
1471 | (nxml-merge-indent-context-type after-context))) | |
1472 | ;; If in the middle of a token that looks inline, | |
1473 | ;; then indent relative to the previous non-blank line | |
1474 | ((eq (nxml-merge-indent-context-type before-context) | |
1475 | 'mixed) | |
1476 | (goto-char prev-bol) | |
1477 | (throw 'indent (current-column))) | |
1478 | (t | |
1479 | (throw 'indent | |
1480 | (nxml-compute-indent-in-token bol)))) | |
1481 | (skip-chars-forward " \t\r\n")) | |
1482 | (goto-char ref) | |
1483 | (+ (current-column) | |
1484 | (* nxml-child-indent | |
1485 | (+ (if (eq before-context 'start-tag) 1 0) | |
1486 | (if (eq after-context 'end-tag) -1 0)))))))) | |
1487 | ||
1488 | (defun nxml-merge-indent-context-type (context) | |
1489 | "Merge the indent context type CONTEXT with the token in `xmltok-type'. | |
1490 | Return the merged indent context type. An indent context type is | |
10545bd8 JB |
1491 | either nil or one of the symbols `start-tag', `end-tag', `markup', |
1492 | `comment', `mixed'." | |
8cd39fb3 MH |
1493 | (cond ((memq xmltok-type '(start-tag partial-start-tag)) |
1494 | (if (memq context '(nil start-tag comment)) | |
1495 | 'start-tag | |
1496 | 'mixed)) | |
1497 | ((memq xmltok-type '(end-tag partial-end-tag)) | |
1498 | (if (memq context '(nil end-tag comment)) | |
1499 | 'end-tag | |
1500 | 'mixed)) | |
1501 | ((eq xmltok-type 'comment) | |
1502 | (cond ((memq context '(start-tag end-tag comment)) | |
1503 | context) | |
1504 | (context 'mixed) | |
1505 | (t 'comment))) | |
1506 | (context 'mixed) | |
1507 | (t 'markup))) | |
1508 | ||
1509 | (defun nxml-compute-indent-in-token (pos) | |
1510 | "Return the indent for a line that starts inside a token. | |
1511 | POS is the position of the first non-whitespace character of the line. | |
1512 | This expects the xmltok-* variables to be set up as by `xmltok-forward'." | |
1513 | (cond ((memq xmltok-type '(start-tag | |
1514 | partial-start-tag | |
1515 | empty-element | |
1516 | partial-empty-element)) | |
1517 | (nxml-compute-indent-in-start-tag pos)) | |
1518 | ((eq xmltok-type 'comment) | |
1519 | (nxml-compute-indent-in-delimited-token pos "<!--" "-->")) | |
1520 | ((eq xmltok-type 'cdata-section) | |
1521 | (nxml-compute-indent-in-delimited-token pos "<![CDATA[" "]]>")) | |
1522 | ((eq xmltok-type 'processing-instruction) | |
1523 | (nxml-compute-indent-in-delimited-token pos "<?" "?>")) | |
1524 | (t | |
1525 | (goto-char pos) | |
1526 | (if (and (= (forward-line -1) 0) | |
1527 | (< xmltok-start (point))) | |
1528 | (back-to-indentation) | |
1529 | (goto-char xmltok-start)) | |
1530 | (current-column)))) | |
1531 | ||
1532 | (defun nxml-compute-indent-in-start-tag (pos) | |
1533 | "Return the indent for a line that starts inside a start-tag. | |
1534 | Also for a line that starts inside an empty element. | |
1535 | POS is the position of the first non-whitespace character of the line. | |
1536 | This expects the xmltok-* variables to be set up as by `xmltok-forward'." | |
1537 | (let ((value-boundary (nxml-attribute-value-boundary pos)) | |
1538 | (off 0)) | |
1539 | (if value-boundary | |
1540 | ;; inside an attribute value | |
1541 | (let ((value-start (car value-boundary)) | |
1542 | (value-end (cdr value-boundary))) | |
1543 | (goto-char pos) | |
1544 | (forward-line -1) | |
1545 | (if (< (point) value-start) | |
1546 | (goto-char value-start) | |
1547 | (back-to-indentation))) | |
1548 | ;; outside an attribute value | |
1549 | (goto-char pos) | |
1550 | (while (and (= (forward-line -1) 0) | |
1551 | (nxml-attribute-value-boundary (point)))) | |
1552 | (cond ((<= (point) xmltok-start) | |
1553 | (goto-char xmltok-start) | |
1554 | (setq off nxml-attribute-indent) | |
1555 | (let ((atts (xmltok-merge-attributes))) | |
1556 | (when atts | |
1557 | (let* ((att (car atts)) | |
1558 | (start (xmltok-attribute-name-start att))) | |
1559 | (when (< start pos) | |
1560 | (goto-char start) | |
1561 | (setq off 0)))))) | |
1562 | (t | |
1563 | (back-to-indentation)))) | |
1564 | (+ (current-column) off))) | |
1565 | ||
1566 | (defun nxml-attribute-value-boundary (pos) | |
1567 | "Return a pair (START . END) if POS is inside an attribute value. | |
1568 | Otherwise return nil. START and END are the positions of the start | |
1569 | and end of the attribute value containing POS. This expects the | |
1570 | xmltok-* variables to be set up as by `xmltok-forward'." | |
1571 | (let ((atts (xmltok-merge-attributes)) | |
1572 | att value-start value-end value-boundary) | |
1573 | (while atts | |
1574 | (setq att (car atts)) | |
1575 | (setq value-start (xmltok-attribute-value-start att)) | |
1576 | (setq value-end (xmltok-attribute-value-end att)) | |
1577 | (cond ((and value-start (< pos value-start)) | |
1578 | (setq atts nil)) | |
1579 | ((and value-start value-end (<= pos value-end)) | |
1580 | (setq value-boundary (cons value-start value-end)) | |
1581 | (setq atts nil)) | |
1582 | (t (setq atts (cdr atts))))) | |
1583 | value-boundary)) | |
10545bd8 | 1584 | |
8cd39fb3 MH |
1585 | (defun nxml-compute-indent-in-delimited-token (pos open-delim close-delim) |
1586 | "Return the indent for a line that starts inside a token with delimiters. | |
1587 | OPEN-DELIM and CLOSE-DELIM are strings giving the opening and closing | |
1588 | delimiters. POS is the position of the first non-whitespace character | |
1589 | of the line. This expects the xmltok-* variables to be set up as by | |
1590 | `xmltok-forward'." | |
1591 | (cond ((let ((end (+ pos (length close-delim)))) | |
1592 | (and (<= end (point-max)) | |
1593 | (string= (buffer-substring-no-properties pos end) | |
1594 | close-delim))) | |
1595 | (goto-char xmltok-start)) | |
1596 | ((progn | |
1597 | (goto-char pos) | |
1598 | (forward-line -1) | |
1599 | (<= (point) xmltok-start)) | |
1600 | (goto-char (+ xmltok-start (length open-delim))) | |
1601 | (when (and (string= open-delim "<!--") | |
1602 | (looking-at " ")) | |
1603 | (goto-char (1+ (point))))) | |
1604 | (t (back-to-indentation))) | |
1605 | (current-column)) | |
1606 | ||
1607 | ;;; Completion | |
1608 | ||
1609 | (defun nxml-complete () | |
1610 | "Perform completion on the symbol preceding point. | |
1611 | ||
1612 | Inserts as many characters as can be completed. However, if not even | |
1613 | one character can be completed, then a buffer with the possibilities | |
1614 | is popped up and the symbol is read from the minibuffer with | |
10545bd8 | 1615 | completion. If the symbol is complete, then any characters that must |
8cd39fb3 MH |
1616 | follow the symbol are also inserted. |
1617 | ||
1618 | The name space used for completion and what is treated as a symbol | |
1619 | depends on the context. The contexts in which completion is performed | |
1620 | depend on `nxml-completion-hook'." | |
1621 | (interactive) | |
1622 | (unless (run-hook-with-args-until-success 'nxml-completion-hook) | |
1623 | ;; Eventually we will complete on entity names here. | |
1624 | (ding) | |
1625 | (message "Cannot complete in this context"))) | |
1626 | ||
1627 | ;;; Movement | |
1628 | ||
1629 | (defun nxml-forward-balanced-item (&optional arg) | |
1630 | "Move forward across one balanced item. | |
1631 | With ARG, do it that many times. Negative arg -N means | |
1632 | move backward across N balanced expressions. | |
1633 | This is the equivalent of `forward-sexp' for XML. | |
1634 | ||
1635 | An element contains as items strings with no markup, tags, processing | |
1636 | instructions, comments, CDATA sections, entity references and | |
10545bd8 | 1637 | characters references. However, if the variable |
8cd39fb3 MH |
1638 | `nxml-sexp-element-flag' is non-nil, then an element is treated as a |
1639 | single markup item. A start-tag contains an element name followed by | |
10545bd8 JB |
1640 | one or more attributes. An end-tag contains just an element name. |
1641 | An attribute value literals contains strings with no markup, entity | |
8cd39fb3 MH |
1642 | references and character references. A processing instruction |
1643 | consists of a target and a content string. A comment or a CDATA | |
1644 | section contains a single string. An entity reference contains a | |
1645 | single name. A character reference contains a character number." | |
1646 | (interactive "p") | |
1647 | (or arg (setq arg 1)) | |
1648 | (cond ((> arg 0) | |
1649 | (while (progn | |
1650 | (nxml-forward-single-balanced-item) | |
1651 | (> (setq arg (1- arg)) 0)))) | |
1652 | ((< arg 0) | |
1653 | (while (progn | |
1654 | (nxml-backward-single-balanced-item) | |
1655 | (< (setq arg (1+ arg)) 0)))))) | |
1656 | ||
1657 | (defun nxml-forward-single-balanced-item () | |
1658 | (condition-case err | |
1659 | (goto-char (let ((end (nxml-token-after))) | |
1660 | (save-excursion | |
1661 | (while (eq xmltok-type 'space) | |
1662 | (goto-char end) | |
1663 | (setq end (nxml-token-after))) | |
1664 | (cond ((/= (point) xmltok-start) | |
1665 | (nxml-scan-forward-within end)) | |
1666 | ((and nxml-sexp-element-flag | |
1667 | (eq xmltok-type 'start-tag)) | |
1668 | ;; can't ever return nil here | |
1669 | (nxml-scan-element-forward xmltok-start)) | |
1670 | ((and nxml-sexp-element-flag | |
1671 | (memq xmltok-type | |
1672 | '(end-tag partial-end-tag))) | |
1673 | (error "Already at end of element")) | |
1674 | (t end))))) | |
1675 | (nxml-scan-error | |
1676 | (goto-char (cadr err)) | |
1677 | (apply 'error (cddr err))))) | |
1678 | ||
1679 | (defun nxml-backward-single-balanced-item () | |
1680 | (condition-case err | |
1681 | (goto-char (let ((end (nxml-token-before))) | |
1682 | (save-excursion | |
1683 | (while (eq xmltok-type 'space) | |
1684 | (goto-char xmltok-start) | |
1685 | (setq end (nxml-token-before))) | |
1686 | (cond ((/= (point) end) | |
1687 | (nxml-scan-backward-within end)) | |
1688 | ((and nxml-sexp-element-flag | |
1689 | (eq xmltok-type 'end-tag)) | |
1690 | ;; can't ever return nil here | |
1691 | (nxml-scan-element-backward end) | |
1692 | xmltok-start) | |
1693 | ((and nxml-sexp-element-flag | |
1694 | (eq xmltok-type 'start-tag)) | |
1695 | (error "Already at start of element")) | |
1696 | (t xmltok-start))))) | |
1697 | (nxml-scan-error | |
1698 | (goto-char (cadr err)) | |
1699 | (apply 'error (cddr err))))) | |
1700 | ||
1701 | (defun nxml-scan-forward-within (end) | |
1702 | (setq end (- end (nxml-end-delimiter-length xmltok-type))) | |
1703 | (when (<= end (point)) | |
1704 | (error "Already at end of %s" | |
1705 | (nxml-token-type-friendly-name xmltok-type))) | |
1706 | (cond ((memq xmltok-type '(start-tag | |
1707 | empty-element | |
1708 | partial-start-tag | |
1709 | partial-empty-element)) | |
1710 | (if (< (point) xmltok-name-end) | |
1711 | xmltok-name-end | |
1712 | (let ((att (nxml-find-following-attribute))) | |
1713 | (cond ((not att) end) | |
1714 | ((and (xmltok-attribute-value-start att) | |
1715 | (<= (xmltok-attribute-value-start att) | |
1716 | (point))) | |
1717 | (nxml-scan-forward-in-attribute-value att)) | |
1718 | ((xmltok-attribute-value-end att) | |
1719 | (1+ (xmltok-attribute-value-end att))) | |
1720 | ((save-excursion | |
1721 | (goto-char (xmltok-attribute-name-end att)) | |
1722 | (looking-at "[ \t\r\n]*=")) | |
1723 | (match-end 0)) | |
1724 | (t (xmltok-attribute-name-end att)))))) | |
1725 | ((and (eq xmltok-type 'processing-instruction) | |
1726 | (< (point) xmltok-name-end)) | |
1727 | xmltok-name-end) | |
1728 | (t end))) | |
1729 | ||
1730 | (defun nxml-scan-backward-within (end) | |
1731 | (setq xmltok-start | |
1732 | (+ xmltok-start | |
1733 | (nxml-start-delimiter-length xmltok-type))) | |
1734 | (when (<= (point) xmltok-start) | |
1735 | (error "Already at start of %s" | |
1736 | (nxml-token-type-friendly-name xmltok-type))) | |
1737 | (cond ((memq xmltok-type '(start-tag | |
1738 | empty-element | |
1739 | partial-start-tag | |
1740 | partial-empty-element)) | |
1741 | (let ((att (nxml-find-preceding-attribute))) | |
1742 | (cond ((not att) xmltok-start) | |
1743 | ((and (xmltok-attribute-value-start att) | |
1744 | (<= (xmltok-attribute-value-start att) | |
1745 | (point)) | |
1746 | (<= (point) | |
1747 | (xmltok-attribute-value-end att))) | |
1748 | (nxml-scan-backward-in-attribute-value att)) | |
1749 | (t (xmltok-attribute-name-start att))))) | |
1750 | ((and (eq xmltok-type 'processing-instruction) | |
1751 | (let ((content-start (save-excursion | |
1752 | (goto-char xmltok-name-end) | |
1753 | (skip-chars-forward " \r\t\n") | |
1754 | (point)))) | |
1755 | (and (< content-start (point)) | |
1756 | content-start)))) | |
1757 | (t xmltok-start))) | |
1758 | ||
1759 | (defun nxml-scan-forward-in-attribute-value (att) | |
1760 | (when (= (point) (xmltok-attribute-value-end att)) | |
1761 | (error "Already at end of attribute value")) | |
1762 | (let ((refs (xmltok-attribute-refs att)) | |
1763 | ref) | |
1764 | (while refs | |
1765 | (setq ref (car refs)) | |
1766 | (if (< (point) (aref ref 2)) | |
1767 | (setq refs nil) | |
1768 | (setq ref nil) | |
1769 | (setq refs (cdr refs)))) | |
1770 | (cond ((not ref) | |
1771 | (xmltok-attribute-value-end att)) | |
1772 | ((< (point) (aref ref 1)) | |
1773 | (aref ref 1)) | |
1774 | ((= (point) (aref ref 1)) | |
1775 | (aref ref 2)) | |
1776 | (t | |
1777 | (let ((end (- (aref ref 2) | |
1778 | (nxml-end-delimiter-length (aref ref 0))))) | |
1779 | (if (< (point) end) | |
1780 | end | |
1781 | (error "Already at end of %s" | |
1782 | (nxml-token-type-friendly-name (aref ref 0))))))))) | |
1783 | ||
1784 | (defun nxml-scan-backward-in-attribute-value (att) | |
1785 | (when (= (point) (xmltok-attribute-value-start att)) | |
1786 | (error "Already at start of attribute value")) | |
1787 | (let ((refs (reverse (xmltok-attribute-refs att))) | |
1788 | ref) | |
1789 | (while refs | |
1790 | (setq ref (car refs)) | |
1791 | (if (< (aref ref 1) (point)) | |
1792 | (setq refs nil) | |
1793 | (setq ref nil) | |
1794 | (setq refs (cdr refs)))) | |
1795 | (cond ((not ref) | |
1796 | (xmltok-attribute-value-start att)) | |
1797 | ((< (aref ref 2) (point)) | |
1798 | (aref ref 2)) | |
1799 | ((= (point) (aref ref 2)) | |
1800 | (aref ref 1)) | |
1801 | (t | |
1802 | (let ((start (+ (aref ref 1) | |
1803 | (nxml-start-delimiter-length (aref ref 0))))) | |
1804 | (if (< start (point)) | |
1805 | start | |
1806 | (error "Already at start of %s" | |
1807 | (nxml-token-type-friendly-name (aref ref 0))))))))) | |
1808 | ||
1809 | (defun nxml-find-following-attribute () | |
1810 | (let ((ret nil) | |
1811 | (atts (or xmltok-attributes xmltok-namespace-attributes)) | |
1812 | (more-atts (and xmltok-attributes xmltok-namespace-attributes))) | |
1813 | (while atts | |
1814 | (let* ((att (car atts)) | |
1815 | (name-start (xmltok-attribute-name-start att))) | |
1816 | (cond ((and (<= name-start (point)) | |
1817 | (xmltok-attribute-value-end att) | |
1818 | ;; <= because end is before quote | |
1819 | (<= (point) (xmltok-attribute-value-end att))) | |
1820 | (setq atts nil) | |
1821 | (setq ret att)) | |
1822 | ((and (< (point) name-start) | |
1823 | (or (not ret) | |
1824 | (< name-start | |
1825 | (xmltok-attribute-name-start ret)))) | |
1826 | (setq ret att)))) | |
1827 | (setq atts (cdr atts)) | |
1828 | (unless atts | |
1829 | (setq atts more-atts) | |
1830 | (setq more-atts nil))) | |
1831 | ret)) | |
1832 | ||
1833 | (defun nxml-find-preceding-attribute () | |
1834 | (let ((ret nil) | |
1835 | (atts (or xmltok-attributes xmltok-namespace-attributes)) | |
1836 | (more-atts (and xmltok-attributes xmltok-namespace-attributes))) | |
1837 | (while atts | |
1838 | (let* ((att (car atts)) | |
1839 | (name-start (xmltok-attribute-name-start att))) | |
1840 | (cond ((and (< name-start (point)) | |
1841 | (xmltok-attribute-value-end att) | |
1842 | ;; <= because end is before quote | |
1843 | (<= (point) (xmltok-attribute-value-end att))) | |
1844 | (setq atts nil) | |
1845 | (setq ret att)) | |
1846 | ((and (< name-start (point)) | |
1847 | (or (not ret) | |
1848 | (< (xmltok-attribute-name-start ret) | |
1849 | name-start))) | |
1850 | (setq ret att)))) | |
1851 | (setq atts (cdr atts)) | |
1852 | (unless atts | |
1853 | (setq atts more-atts) | |
1854 | (setq more-atts nil))) | |
1855 | ret)) | |
1856 | ||
1857 | (defun nxml-up-element (&optional arg) | |
1858 | (interactive "p") | |
1859 | (or arg (setq arg 1)) | |
1860 | (if (< arg 0) | |
1861 | (nxml-backward-up-element (- arg)) | |
1862 | (condition-case err | |
1863 | (while (and (> arg 0) | |
1864 | (< (point) (point-max))) | |
1865 | (let ((token-end (nxml-token-after))) | |
1866 | (goto-char (cond ((or (memq xmltok-type '(end-tag | |
1867 | partial-end-tag)) | |
1868 | (and (memq xmltok-type | |
1869 | '(empty-element | |
1870 | partial-empty-element)) | |
1871 | (< xmltok-start (point)))) | |
1872 | token-end) | |
1873 | ((nxml-scan-element-forward | |
1874 | (if (and (eq xmltok-type 'start-tag) | |
1875 | (= (point) xmltok-start)) | |
1876 | xmltok-start | |
1877 | token-end) | |
1878 | t)) | |
1879 | (t (error "No parent element"))))) | |
1880 | (setq arg (1- arg))) | |
1881 | (nxml-scan-error | |
1882 | (goto-char (cadr err)) | |
1883 | (apply 'error (cddr err)))))) | |
1884 | ||
1885 | (defun nxml-backward-up-element (&optional arg) | |
1886 | (interactive "p") | |
1887 | (or arg (setq arg 1)) | |
1888 | (if (< arg 0) | |
1889 | (nxml-up-element (- arg)) | |
1890 | (condition-case err | |
1891 | (while (and (> arg 0) | |
1892 | (< (point-min) (point))) | |
1893 | (let ((token-end (nxml-token-before))) | |
1894 | (goto-char (cond ((or (memq xmltok-type '(start-tag | |
1895 | partial-start-tag)) | |
1896 | (and (memq xmltok-type | |
1897 | '(empty-element | |
1898 | partial-empty-element)) | |
1899 | (< (point) token-end))) | |
1900 | xmltok-start) | |
1901 | ((nxml-scan-element-backward | |
1902 | (if (and (eq xmltok-type 'end-tag) | |
1903 | (= (point) token-end)) | |
1904 | token-end | |
1905 | xmltok-start) | |
1906 | t) | |
1907 | xmltok-start) | |
1908 | (t (error "No parent element"))))) | |
1909 | (setq arg (1- arg))) | |
1910 | (nxml-scan-error | |
1911 | (goto-char (cadr err)) | |
1912 | (apply 'error (cddr err)))))) | |
1913 | ||
1914 | (defun nxml-down-element (&optional arg) | |
1915 | "Move forward down into the content of an element. | |
1916 | With ARG, do this that many times. | |
1917 | Negative ARG means move backward but still down." | |
1918 | (interactive "p") | |
1919 | (or arg (setq arg 1)) | |
1920 | (if (< arg 0) | |
1921 | (nxml-backward-down-element (- arg)) | |
1922 | (while (> arg 0) | |
1923 | (goto-char | |
1924 | (let ((token-end (nxml-token-after))) | |
1925 | (save-excursion | |
1926 | (goto-char token-end) | |
1927 | (while (progn | |
1928 | (when (memq xmltok-type '(nil end-tag partial-end-tag)) | |
1929 | (error "No following start-tags in this element")) | |
1930 | (not (memq xmltok-type '(start-tag partial-start-tag)))) | |
1931 | (nxml-tokenize-forward)) | |
1932 | (point)))) | |
1933 | (setq arg (1- arg))))) | |
1934 | ||
1935 | (defun nxml-backward-down-element (&optional arg) | |
1936 | (interactive "p") | |
1937 | (or arg (setq arg 1)) | |
1938 | (if (< arg 0) | |
1939 | (nxml-down-element (- arg)) | |
1940 | (while (> arg 0) | |
1941 | (goto-char | |
1942 | (save-excursion | |
1943 | (nxml-token-before) | |
1944 | (goto-char xmltok-start) | |
1945 | (while (progn | |
1946 | (when (memq xmltok-type '(start-tag | |
1947 | partial-start-tag | |
1948 | prolog | |
1949 | nil)) | |
1950 | (error "No preceding end-tags in this element")) | |
1951 | (not (memq xmltok-type '(end-tag partial-end-tag)))) | |
1952 | (if (or (<= (point) nxml-prolog-end) | |
1953 | (not (search-backward "<" nxml-prolog-end t))) | |
1954 | (setq xmltok-type nil) | |
1955 | (nxml-move-outside-backwards) | |
1956 | (xmltok-forward))) | |
1957 | xmltok-start)) | |
1958 | (setq arg (1- arg))))) | |
1959 | ||
1960 | (defun nxml-forward-element (&optional arg) | |
1961 | "Move forward over one element. | |
1962 | With ARG, do it that many times. | |
1963 | Negative ARG means move backward." | |
1964 | (interactive "p") | |
1965 | (or arg (setq arg 1)) | |
1966 | (if (< arg 0) | |
1967 | (nxml-backward-element (- arg)) | |
1968 | (condition-case err | |
1969 | (while (and (> arg 0) | |
1970 | (< (point) (point-max))) | |
1971 | (goto-char | |
1972 | (or (nxml-scan-element-forward (nxml-token-before)) | |
1973 | (error "No more elements"))) | |
1974 | (setq arg (1- arg))) | |
1975 | (nxml-scan-error | |
1976 | (goto-char (cadr err)) | |
1977 | (apply 'error (cddr err)))))) | |
1978 | ||
1979 | (defun nxml-backward-element (&optional arg) | |
1980 | "Move backward over one element. | |
1981 | With ARG, do it that many times. | |
1982 | Negative ARG means move forward." | |
1983 | (interactive "p") | |
1984 | (or arg (setq arg 1)) | |
1985 | (if (< arg 0) | |
1986 | (nxml-forward-element (- arg)) | |
1987 | (condition-case err | |
1988 | (while (and (> arg 0) | |
1989 | (< (point-min) (point))) | |
1990 | (goto-char | |
1991 | (or (and (nxml-scan-element-backward (progn | |
1992 | (nxml-token-after) | |
1993 | xmltok-start)) | |
1994 | xmltok-start) | |
1995 | (error "No preceding elements"))) | |
1996 | (setq arg (1- arg))) | |
1997 | (nxml-scan-error | |
1998 | (goto-char (cadr err)) | |
1999 | (apply 'error (cddr err)))))) | |
2000 | ||
2001 | (defun nxml-mark-token-after () | |
2002 | (interactive) | |
2003 | (push-mark (nxml-token-after) nil t) | |
2004 | (goto-char xmltok-start) | |
2005 | (message "Marked %s" xmltok-type)) | |
2006 | ||
2007 | ;;; Paragraphs | |
2008 | ||
2009 | (defun nxml-mark-paragraph () | |
2010 | "Put point at beginning of this paragraph, mark at end. | |
2011 | The paragraph marked is the one that contains point or follows point." | |
2012 | (interactive) | |
2013 | (nxml-forward-paragraph) | |
2014 | (push-mark nil t t) | |
2015 | (nxml-backward-paragraph)) | |
2016 | ||
2017 | (defun nxml-forward-paragraph (&optional arg) | |
2018 | (interactive "p") | |
2019 | (or arg (setq arg 1)) | |
2020 | (cond ((< arg 0) | |
2021 | (nxml-backward-paragraph (- arg))) | |
2022 | ((> arg 0) | |
2023 | (forward-line 0) | |
2024 | (while (and (nxml-forward-single-paragraph) | |
2025 | (> (setq arg (1- arg)) 0)))))) | |
2026 | ||
2027 | (defun nxml-backward-paragraph (&optional arg) | |
2028 | (interactive "p") | |
2029 | (or arg (setq arg 1)) | |
2030 | (cond ((< arg 0) | |
2031 | (nxml-forward-paragraph (- arg))) | |
2032 | ((> arg 0) | |
2033 | (unless (bolp) | |
2034 | (let ((inhibit-field-text-motion t)) | |
2035 | (end-of-line))) | |
2036 | (while (and (nxml-backward-single-paragraph) | |
2037 | (> (setq arg (1- arg)) 0)))))) | |
2038 | ||
2039 | (defun nxml-forward-single-paragraph () | |
2040 | "Move forward over a single paragraph. | |
2041 | Return nil at end of buffer, t otherwise." | |
2042 | (let* ((token-end (nxml-token-after)) | |
2043 | (offset (- (point) xmltok-start)) | |
2044 | pos had-data) | |
2045 | (goto-char token-end) | |
2046 | (while (and (< (point) (point-max)) | |
2047 | (not (setq pos | |
2048 | (nxml-paragraph-end-pos had-data offset)))) | |
2049 | (when (nxml-token-contains-data-p offset) | |
2050 | (setq had-data t)) | |
2051 | (nxml-tokenize-forward) | |
2052 | (setq offset 0)) | |
2053 | (when pos (goto-char pos)))) | |
2054 | ||
2055 | (defun nxml-backward-single-paragraph () | |
2056 | "Move backward over a single paragraph. | |
2057 | Return nil at start of buffer, t otherwise." | |
2058 | (let* ((token-end (nxml-token-before)) | |
2059 | (offset (- token-end (point))) | |
2060 | (last-tag-pos xmltok-start) | |
2061 | pos had-data last-data-pos) | |
2062 | (goto-char token-end) | |
2063 | (unless (setq pos (nxml-paragraph-start-pos nil offset)) | |
2064 | (setq had-data (nxml-token-contains-data-p nil offset)) | |
2065 | (goto-char xmltok-start) | |
2066 | (while (and (not pos) (< (point-min) (point))) | |
2067 | (cond ((search-backward "<" nxml-prolog-end t) | |
2068 | (nxml-move-outside-backwards) | |
2069 | (save-excursion | |
2070 | (while (< (point) last-tag-pos) | |
2071 | (xmltok-forward) | |
2072 | (when (and (not had-data) (nxml-token-contains-data-p)) | |
2073 | (setq pos nil) | |
2074 | (setq last-data-pos xmltok-start)) | |
2075 | (let ((tem (nxml-paragraph-start-pos had-data 0))) | |
2076 | (when tem (setq pos tem))))) | |
2077 | (when (and (not had-data) last-data-pos (not pos)) | |
2078 | (setq had-data t) | |
2079 | (save-excursion | |
2080 | (while (< (point) last-data-pos) | |
2081 | (xmltok-forward)) | |
2082 | (let ((tem (nxml-paragraph-start-pos had-data 0))) | |
2083 | (when tem (setq pos tem))))) | |
2084 | (setq last-tag-pos (point))) | |
2085 | (t (goto-char (point-min)))))) | |
2086 | (when pos (goto-char pos)))) | |
2087 | ||
2088 | (defun nxml-token-contains-data-p (&optional start end) | |
2089 | (setq start (+ xmltok-start (or start 0))) | |
2090 | (setq end (- (point) (or end 0))) | |
2091 | (when (eq xmltok-type 'cdata-section) | |
2092 | (setq start (max start (+ xmltok-start 9))) | |
2093 | (setq end (min end (- (point) 3)))) | |
2094 | (or (and (eq xmltok-type 'data) | |
2095 | (eq start xmltok-start) | |
2096 | (eq end (point))) | |
2097 | (eq xmltok-type 'char-ref) | |
2098 | (and (memq xmltok-type '(data cdata-section)) | |
2099 | (< start end) | |
2100 | (save-excursion | |
2101 | (goto-char start) | |
2102 | (re-search-forward "[^ \t\r\n]" end t))))) | |
2103 | ||
2104 | (defun nxml-paragraph-end-pos (had-data offset) | |
2105 | "Return the position of the paragraph end if contained in the current token. | |
2106 | Return nil if the current token does not contain the paragraph end. | |
2107 | Only characters after OFFSET from the start of the token are eligible. | |
2108 | HAD-DATA says whether there have been non-whitespace data characters yet." | |
2109 | (cond ((not had-data) | |
2110 | (cond ((memq xmltok-type '(data cdata-section)) | |
2111 | (save-excursion | |
2112 | (let ((end (point))) | |
2113 | (goto-char (+ xmltok-start | |
2114 | (max (if (eq xmltok-type 'cdata-section) | |
2115 | 9 | |
2116 | 0) | |
2117 | offset))) | |
2118 | (and (re-search-forward "[^ \t\r\n]" end t) | |
2119 | (re-search-forward "^[ \t]*$" end t) | |
2120 | (match-beginning 0))))) | |
2121 | ((and (eq xmltok-type 'comment) | |
2122 | (nxml-token-begins-line-p) | |
2123 | (nxml-token-ends-line-p)) | |
2124 | (save-excursion | |
2125 | (let ((end (point))) | |
2126 | (goto-char (+ xmltok-start (max 4 offset))) | |
2127 | (when (re-search-forward "[^ \t\r\n]" (- end 3) t) | |
2128 | (if (re-search-forward "^[ \t]*$" end t) | |
2129 | (match-beginning 0) | |
2130 | (goto-char (- end 3)) | |
2131 | (skip-chars-backward " \t") | |
2132 | (unless (bolp) | |
2133 | (beginning-of-line 2)) | |
2134 | (point)))))))) | |
2135 | ((memq xmltok-type '(data space cdata-section)) | |
2136 | (save-excursion | |
2137 | (let ((end (point))) | |
2138 | (goto-char (+ xmltok-start offset)) | |
2139 | (and (re-search-forward "^[ \t]*$" end t) | |
2140 | (match-beginning 0))))) | |
10545bd8 | 2141 | ((and (memq xmltok-type '(start-tag |
8cd39fb3 MH |
2142 | end-tag |
2143 | empty-element | |
2144 | comment | |
2145 | processing-instruction | |
2146 | entity-ref)) | |
2147 | (nxml-token-begins-line-p) | |
2148 | (nxml-token-ends-line-p)) | |
2149 | (save-excursion | |
2150 | (goto-char xmltok-start) | |
2151 | (skip-chars-backward " \t") | |
2152 | (point))) | |
2153 | ((and (eq xmltok-type 'end-tag) | |
2154 | (looking-at "[ \t]*$") | |
2155 | (not (nxml-in-mixed-content-p t))) | |
2156 | (save-excursion | |
10545bd8 | 2157 | (or (search-forward "\n" nil t) |
8cd39fb3 MH |
2158 | (point-max)))))) |
2159 | ||
2160 | (defun nxml-paragraph-start-pos (had-data offset) | |
2161 | "Return the position of the paragraph start if contained in the current token. | |
2162 | Return nil if the current token does not contain the paragraph start. | |
2163 | Only characters before OFFSET from the end of the token are eligible. | |
2164 | HAD-DATA says whether there have been non-whitespace data characters yet." | |
2165 | (cond ((not had-data) | |
2166 | (cond ((memq xmltok-type '(data cdata-section)) | |
2167 | (save-excursion | |
2168 | (goto-char (- (point) | |
2169 | (max (if (eq xmltok-type 'cdata-section) | |
2170 | 3 | |
2171 | 0) | |
2172 | offset))) | |
2173 | (and (re-search-backward "[^ \t\r\n]" xmltok-start t) | |
2174 | (re-search-backward "^[ \t]*$" xmltok-start t) | |
2175 | (match-beginning 0)))) | |
2176 | ((and (eq xmltok-type 'comment) | |
2177 | (nxml-token-ends-line-p) | |
2178 | (nxml-token-begins-line-p)) | |
2179 | (save-excursion | |
2180 | (goto-char (- (point) (max 3 offset))) | |
2181 | (when (and (< (+ xmltok-start 4) (point)) | |
2182 | (re-search-backward "[^ \t\r\n]" | |
2183 | (+ xmltok-start 4) | |
2184 | t)) | |
2185 | (if (re-search-backward "^[ \t]*$" xmltok-start t) | |
2186 | (match-beginning 0) | |
2187 | (goto-char xmltok-start) | |
2188 | (if (looking-at "<!--[ \t]*\n") | |
2189 | (match-end 0) | |
2190 | (skip-chars-backward " \t") | |
2191 | (point)))))))) | |
2192 | ((memq xmltok-type '(data space cdata-section)) | |
2193 | (save-excursion | |
2194 | (goto-char (- (point) offset)) | |
2195 | (and (re-search-backward "^[ \t]*$" xmltok-start t) | |
2196 | (match-beginning 0)))) | |
10545bd8 | 2197 | ((and (memq xmltok-type '(start-tag |
8cd39fb3 MH |
2198 | end-tag |
2199 | empty-element | |
2200 | comment | |
2201 | processing-instruction | |
2202 | entity-ref)) | |
2203 | (nxml-token-ends-line-p) | |
2204 | (nxml-token-begins-line-p)) | |
10545bd8 | 2205 | (or (search-forward "\n" nil t) |
8cd39fb3 MH |
2206 | (point-max))) |
2207 | ((and (eq xmltok-type 'start-tag) | |
2208 | (nxml-token-begins-line-p) | |
2209 | (not (save-excursion | |
2210 | (goto-char xmltok-start) | |
2211 | (nxml-in-mixed-content-p nil)))) | |
2212 | (save-excursion | |
2213 | (goto-char xmltok-start) | |
2214 | (skip-chars-backward " \t") | |
2215 | ;; include any blank line before | |
2216 | (or (and (eq (char-before) ?\n) | |
2217 | (save-excursion | |
2218 | (goto-char (1- (point))) | |
2219 | (skip-chars-backward " \t") | |
2220 | (and (bolp) (point)))) | |
2221 | (point)))))) | |
2222 | ||
2223 | (defun nxml-token-ends-line-p () (looking-at "[ \t]*$")) | |
2224 | ||
2225 | (defun nxml-token-begins-line-p () | |
2226 | (save-excursion | |
2227 | (goto-char xmltok-start) | |
2228 | (skip-chars-backward " \t") | |
2229 | (bolp))) | |
2230 | ||
2231 | (defun nxml-in-mixed-content-p (endp) | |
2232 | "Return non-nil if point is in mixed content. | |
2233 | Point must be after an end-tag or before a start-tag. | |
2234 | ENDP is t in the former case, nil in the latter." | |
2235 | (let (matching-tag-pos) | |
2236 | (cond ((not (run-hook-with-args-until-failure | |
2237 | 'nxml-in-mixed-content-hook)) | |
2238 | nil) | |
2239 | ;; See if the matching tag does not start or end a line. | |
2240 | ((condition-case err | |
2241 | (progn | |
2242 | (setq matching-tag-pos | |
2243 | (xmltok-save | |
2244 | (if endp | |
2245 | (and (nxml-scan-element-backward (point)) | |
2246 | xmltok-start) | |
2247 | (nxml-scan-element-forward (point))))) | |
2248 | (and matching-tag-pos | |
2249 | (save-excursion | |
2250 | (goto-char matching-tag-pos) | |
2251 | (not (if endp | |
2252 | (progn | |
2253 | (skip-chars-backward " \t") | |
2254 | (bolp)) | |
2255 | (looking-at "[ \t]*$")))))) | |
2256 | (nxml-scan-error nil)) | |
2257 | t) | |
2258 | ;; See if there's data at the same level. | |
2259 | ((let (start end) | |
2260 | (if endp | |
2261 | (setq start matching-tag-pos | |
2262 | end (point)) | |
2263 | (setq start (point) | |
2264 | end matching-tag-pos)) | |
2265 | (save-excursion | |
2266 | (or (when start | |
2267 | (goto-char start) | |
2268 | (nxml-preceding-sibling-data-p)) | |
2269 | (when end | |
2270 | (goto-char end) | |
2271 | (nxml-following-sibling-data-p))))) | |
2272 | t) | |
2273 | ;; Otherwise, treat as not mixed | |
2274 | (t nil)))) | |
2275 | ||
2276 | (defun nxml-preceding-sibling-data-p () | |
2277 | "Return non-nil if there is a previous sibling that is data." | |
2278 | (let ((lim (max (- (point) nxml-mixed-scan-distance) | |
2279 | nxml-prolog-end)) | |
2280 | (level 0) | |
2281 | found end) | |
2282 | (xmltok-save | |
2283 | (save-excursion | |
2284 | (while (and (< lim (point)) | |
2285 | (>= level 0) | |
2286 | (not found) | |
2287 | (progn | |
2288 | (setq end (point)) | |
2289 | (search-backward "<" lim t))) | |
2290 | (nxml-move-outside-backwards) | |
2291 | (save-excursion | |
2292 | (xmltok-forward) | |
2293 | (let ((prev-level level)) | |
2294 | (cond ((eq xmltok-type 'end-tag) | |
2295 | (setq level (1+ level))) | |
2296 | ((eq xmltok-type 'start-tag) | |
2297 | (setq level (1- level)))) | |
2298 | (when (eq prev-level 0) | |
2299 | (while (and (< (point) end) (not found)) | |
2300 | (xmltok-forward) | |
2301 | (when (memq xmltok-type '(data cdata-section char-ref)) | |
2302 | (setq found t))))))))) | |
2303 | found)) | |
2304 | ||
2305 | (defun nxml-following-sibling-data-p () | |
2306 | (let ((lim (min (+ (point) nxml-mixed-scan-distance) | |
2307 | (point-max))) | |
2308 | (level 0) | |
2309 | found) | |
2310 | (xmltok-save | |
2311 | (save-excursion | |
2312 | (while (and (< (point) lim) | |
2313 | (>= level 0) | |
2314 | (nxml-tokenize-forward) | |
2315 | (not found)) | |
2316 | (cond ((eq xmltok-type 'start-tag) | |
2317 | (setq level (1+ level))) | |
2318 | ((eq xmltok-type 'end-tag) | |
2319 | (setq level (1- level))) | |
2320 | ((and (eq level 0) | |
2321 | (memq xmltok-type '(data cdata-section char-ref))) | |
2322 | (setq found t)))))) | |
2323 | found)) | |
2324 | ||
2325 | ;;; Filling | |
2326 | ||
2327 | (defun nxml-do-fill-paragraph (arg) | |
2328 | (let (fill-paragraph-function | |
2329 | fill-prefix | |
2330 | start end) | |
2331 | (save-excursion | |
2332 | (nxml-forward-paragraph) | |
2333 | (setq end (point)) | |
2334 | (nxml-backward-paragraph) | |
2335 | (skip-chars-forward " \t\r\n") | |
2336 | (setq start (point)) | |
2337 | (beginning-of-line) | |
2338 | (setq fill-prefix (buffer-substring-no-properties (point) start)) | |
2339 | (when (and (not (nxml-get-inside (point))) | |
2340 | (looking-at "[ \t]*<!--")) | |
2341 | (setq fill-prefix (concat fill-prefix " "))) | |
2342 | (fill-region-as-paragraph start end arg)) | |
2343 | (skip-line-prefix fill-prefix) | |
2344 | fill-prefix)) | |
10545bd8 | 2345 | |
8cd39fb3 MH |
2346 | (defun nxml-newline-and-indent (soft) |
2347 | (delete-horizontal-space) | |
2348 | (if soft (insert-and-inherit ?\n) (newline 1)) | |
2349 | (nxml-indent-line)) | |
2350 | ||
2351 | ||
2352 | ;;; Dynamic markup | |
2353 | ||
2354 | (defvar nxml-dynamic-markup-prev-pos nil) | |
2355 | (defvar nxml-dynamic-markup-prev-lengths nil) | |
2356 | (defvar nxml-dynamic-markup-prev-found-marker nil) | |
2357 | (defvar nxml-dynamic-markup-prev-start-tags (make-hash-table :test 'equal)) | |
2358 | ||
2359 | (defun nxml-dynamic-markup-word () | |
2360 | "Dynamically markup the word before point. | |
2361 | This attempts to find a tag to put around the word before point based | |
2362 | on the contents of the current buffer. The end-tag will be inserted at | |
2363 | point. The start-tag will be inserted at or before the beginning of | |
2364 | the word before point; the contents of the current buffer is used to | |
2365 | decide where. | |
2366 | ||
10545bd8 | 2367 | It works in a similar way to \\[dabbrev-expand]. It searches first |
8cd39fb3 MH |
2368 | backwards from point, then forwards from point for an element whose |
2369 | content is a string which matches the contents of the buffer before | |
10545bd8 | 2370 | point and which includes at least the word before point. It then |
8cd39fb3 MH |
2371 | copies the start- and end-tags from that element and uses them to |
2372 | surround the matching string before point. | |
2373 | ||
2374 | Repeating \\[nxml-dynamic-markup-word] immediately after successful | |
2375 | \\[nxml-dynamic-markup-word] removes the previously inserted markup | |
2376 | and attempts to find another possible way to do the markup." | |
2377 | (interactive "*") | |
2378 | (let (search-start-pos done) | |
2379 | (if (and (integerp nxml-dynamic-markup-prev-pos) | |
2380 | (= nxml-dynamic-markup-prev-pos (point)) | |
2381 | (eq last-command this-command) | |
2382 | nxml-dynamic-markup-prev-lengths) | |
2383 | (let* ((end-tag-open-pos | |
2384 | (- nxml-dynamic-markup-prev-pos | |
2385 | (nth 2 nxml-dynamic-markup-prev-lengths))) | |
2386 | (start-tag-close-pos | |
2387 | (- end-tag-open-pos | |
2388 | (nth 1 nxml-dynamic-markup-prev-lengths))) | |
2389 | (start-tag-open-pos | |
2390 | (- start-tag-close-pos | |
2391 | (nth 0 nxml-dynamic-markup-prev-lengths)))) | |
2392 | (delete-region end-tag-open-pos nxml-dynamic-markup-prev-pos) | |
2393 | (delete-region start-tag-open-pos start-tag-close-pos) | |
2394 | (setq search-start-pos | |
2395 | (marker-position nxml-dynamic-markup-prev-found-marker))) | |
2396 | (clrhash nxml-dynamic-markup-prev-start-tags)) | |
2397 | (setq nxml-dynamic-markup-prev-pos nil) | |
2398 | (setq nxml-dynamic-markup-prev-lengths nil) | |
2399 | (setq nxml-dynamic-markup-prev-found-marker nil) | |
2400 | (goto-char | |
2401 | (save-excursion | |
2402 | (let* ((pos (point)) | |
2403 | (word (progn | |
2404 | (backward-word 1) | |
2405 | (unless (< (point) pos) | |
2406 | (error "No word to markup")) | |
2407 | (buffer-substring-no-properties (point) pos))) | |
2408 | (search (concat word "</")) | |
2409 | done) | |
2410 | (when search-start-pos | |
2411 | (goto-char search-start-pos)) | |
2412 | (while (and (not done) | |
2413 | (or (and (< (point) pos) | |
2414 | (or (search-backward search nil t) | |
2415 | (progn (goto-char pos) nil))) | |
2416 | (search-forward search nil t))) | |
2417 | (goto-char (- (match-end 0) 2)) | |
2418 | (setq done (nxml-try-copy-markup pos))) | |
2419 | (or done | |
2420 | (error (if (zerop (hash-table-count | |
2421 | nxml-dynamic-markup-prev-start-tags)) | |
2422 | "No possible markup found for `%s'" | |
2423 | "No more markup possibilities found for `%s'") | |
2424 | word))))))) | |
2425 | ||
2426 | (defun nxml-try-copy-markup (word-end-pos) | |
2427 | (save-excursion | |
2428 | (let ((end-tag-pos (point))) | |
2429 | (when (and (not (nxml-get-inside end-tag-pos)) | |
2430 | (search-backward "<" nil t) | |
2431 | (not (nxml-get-inside (point)))) | |
2432 | (xmltok-forward) | |
2433 | (when (and (eq xmltok-type 'start-tag) | |
2434 | (< (point) end-tag-pos)) | |
2435 | (let* ((start-tag-close-pos (point)) | |
2436 | (start-tag | |
2437 | (buffer-substring-no-properties xmltok-start | |
2438 | start-tag-close-pos)) | |
2439 | (words | |
2440 | (nreverse | |
2441 | (split-string | |
2442 | (buffer-substring-no-properties start-tag-close-pos | |
2443 | end-tag-pos) | |
2444 | "[ \t\r\n]+")))) | |
2445 | (goto-char word-end-pos) | |
2446 | (while (and words | |
2447 | (re-search-backward (concat | |
2448 | (regexp-quote (car words)) | |
2449 | "\\=") | |
2450 | nil | |
2451 | t)) | |
2452 | (setq words (cdr words)) | |
2453 | (skip-chars-backward " \t\r\n")) | |
2454 | (when (and (not words) | |
2455 | (progn | |
2456 | (skip-chars-forward " \t\r\n") | |
2457 | (not (gethash (cons (point) start-tag) | |
2458 | nxml-dynamic-markup-prev-start-tags))) | |
2459 | (or (< end-tag-pos (point)) | |
2460 | (< word-end-pos xmltok-start))) | |
2461 | (setq nxml-dynamic-markup-prev-found-marker | |
2462 | (copy-marker end-tag-pos t)) | |
2463 | (puthash (cons (point) start-tag) | |
2464 | t | |
2465 | nxml-dynamic-markup-prev-start-tags) | |
2466 | (setq nxml-dynamic-markup-prev-lengths | |
2467 | (list (- start-tag-close-pos xmltok-start) | |
2468 | (- word-end-pos (point)) | |
2469 | (+ (- xmltok-name-end xmltok-start) 2))) | |
2470 | (let ((name (xmltok-start-tag-qname))) | |
2471 | (insert start-tag) | |
2472 | (goto-char (+ word-end-pos | |
2473 | (- start-tag-close-pos xmltok-start))) | |
2474 | (insert "</" name ">") | |
2475 | (setq nxml-dynamic-markup-prev-pos (point)))))))))) | |
10545bd8 | 2476 | |
8cd39fb3 MH |
2477 | |
2478 | ;;; Character names | |
2479 | ||
b85b6604 | 2480 | (defvar nxml-char-name-ignore-case t) |
8cd39fb3 MH |
2481 | |
2482 | (defvar nxml-char-name-alist nil | |
2483 | "Alist of character names. | |
2484 | Each member of the list has the form (NAME CODE . NAMESET), | |
2485 | where NAME is a string naming a character, NAMESET is a symbol | |
2486 | identifying a set of names and CODE is an integer specifying the | |
2487 | Unicode scalar value of the named character. | |
2488 | The NAME will only be used for completion if NAMESET has | |
2489 | a non-nil `nxml-char-name-set-enabled' property. | |
2490 | If NAMESET does does not have `nxml-char-name-set-defined' property, | |
2491 | then it must have a `nxml-char-name-set-file' property and `load' | |
2492 | will be applied to the value of this property if the nameset | |
2493 | is enabled.") | |
2494 | ||
2495 | (defvar nxml-char-name-table (make-hash-table :test 'eq) | |
2496 | "Hash table for mapping char codes to names. | |
2497 | Each key is a Unicode scalar value. | |
2498 | Each value is a list of pairs of the form (NAMESET . NAME), | |
2499 | where NAMESET is a symbol identifying a set of names, | |
2500 | and NAME is a string naming a character.") | |
2501 | ||
2502 | (defvar nxml-autoload-char-name-set-list nil | |
2503 | "List of char namesets that can be autoloaded.") | |
2504 | ||
10545bd8 | 2505 | (defun nxml-enable-char-name-set (nameset) |
8cd39fb3 MH |
2506 | (put nameset 'nxml-char-name-set-enabled t)) |
2507 | ||
10545bd8 | 2508 | (defun nxml-disable-char-name-set (nameset) |
8cd39fb3 MH |
2509 | (put nameset 'nxml-char-name-set-enabled nil)) |
2510 | ||
2511 | (defun nxml-char-name-set-enabled-p (nameset) | |
2512 | (get nameset 'nxml-char-name-set-enabled)) | |
2513 | ||
2514 | (defun nxml-autoload-char-name-set (nameset file) | |
2515 | (unless (memq nameset nxml-autoload-char-name-set-list) | |
2516 | (setq nxml-autoload-char-name-set-list | |
2517 | (cons nameset nxml-autoload-char-name-set-list))) | |
2518 | (put nameset 'nxml-char-name-set-file file)) | |
2519 | ||
2520 | (defun nxml-define-char-name-set (nameset alist) | |
2521 | "Define a set of character names. | |
2522 | NAMESET is a symbol identifying the set. | |
10545bd8 JB |
2523 | ALIST is a list where each member has the form (NAME CODE), |
2524 | where NAME is a string naming a character and code is an | |
2525 | integer giving the Unicode scalar value of the character." | |
8cd39fb3 MH |
2526 | (when (get nameset 'nxml-char-name-set-defined) |
2527 | (error "Nameset `%s' already defined" nameset)) | |
2528 | (let ((iter alist)) | |
2529 | (while iter | |
2530 | (let* ((name-code (car iter)) | |
2531 | (name (car name-code)) | |
2532 | (code (cadr name-code))) | |
2533 | (puthash code | |
2534 | (cons (cons nameset name) | |
2535 | (gethash code nxml-char-name-table)) | |
2536 | nxml-char-name-table)) | |
2537 | (setcdr (cdr (car iter)) nameset) | |
2538 | (setq iter (cdr iter)))) | |
2539 | (setq nxml-char-name-alist | |
2540 | (nconc alist nxml-char-name-alist)) | |
2541 | (put nameset 'nxml-char-name-set-defined t)) | |
2542 | ||
2543 | (defun nxml-get-char-name (code) | |
e290ff07 | 2544 | (mapc 'nxml-maybe-load-char-name-set nxml-autoload-char-name-set-list) |
8cd39fb3 MH |
2545 | (let ((names (gethash code nxml-char-name-table)) |
2546 | name) | |
2547 | (while (and names (not name)) | |
2548 | (if (nxml-char-name-set-enabled-p (caar names)) | |
2549 | (setq name (cdar names)) | |
2550 | (setq names (cdr names)))) | |
2551 | name)) | |
2552 | ||
2553 | (defvar nxml-named-char-history nil) | |
2554 | ||
2555 | (defun nxml-insert-named-char (arg) | |
2556 | "Insert a character using its name. | |
2557 | The name is read from the minibuffer. | |
2558 | Normally, inserts the character as a numeric character reference. | |
2559 | With a prefix argument, inserts the character directly." | |
2560 | (interactive "*P") | |
e290ff07 | 2561 | (mapc 'nxml-maybe-load-char-name-set nxml-autoload-char-name-set-list) |
8cd39fb3 MH |
2562 | (let ((name |
2563 | (let ((completion-ignore-case nxml-char-name-ignore-case)) | |
2564 | (completing-read "Character name: " | |
2565 | nxml-char-name-alist | |
2566 | (lambda (member) | |
2567 | (get (cddr member) 'nxml-char-name-set-enabled)) | |
2568 | t | |
2569 | nil | |
2570 | 'nxml-named-char-history))) | |
2571 | (alist nxml-char-name-alist) | |
2572 | elt code) | |
2573 | (while (and alist (not code)) | |
2574 | (setq elt (assoc name alist)) | |
2575 | (if (get (cddr elt) 'nxml-char-name-set-enabled) | |
2576 | (setq code (cadr elt)) | |
2577 | (setq alist (cdr (member elt alist))))) | |
2578 | (when code | |
2579 | (insert (if arg | |
2580 | (or (decode-char 'ucs code) | |
2581 | (error "Character %x is not supported by Emacs" | |
2582 | code)) | |
2583 | (format "&#x%X;" code)))))) | |
10545bd8 | 2584 | |
8cd39fb3 MH |
2585 | (defun nxml-maybe-load-char-name-set (sym) |
2586 | (when (and (get sym 'nxml-char-name-set-enabled) | |
2587 | (not (get sym 'nxml-char-name-set-defined)) | |
2588 | (stringp (get sym 'nxml-char-name-set-file))) | |
2589 | (load (get sym 'nxml-char-name-set-file)))) | |
2590 | ||
2591 | (defun nxml-toggle-char-ref-extra-display (arg) | |
10545bd8 | 2592 | "Toggle the display of extra information for character references." |
8cd39fb3 MH |
2593 | (interactive "P") |
2594 | (let ((new (if (null arg) | |
2595 | (not nxml-char-ref-extra-display) | |
2596 | (> (prefix-numeric-value arg) 0)))) | |
2597 | (when (not (eq new nxml-char-ref-extra-display)) | |
2598 | (setq nxml-char-ref-extra-display new) | |
e8ec402f | 2599 | (font-lock-fontify-buffer)))) |
8cd39fb3 MH |
2600 | |
2601 | (put 'nxml-char-ref 'evaporate t) | |
2602 | ||
2603 | (defun nxml-char-ref-display-extra (start end n) | |
2604 | (when nxml-char-ref-extra-display | |
2605 | (let ((name (nxml-get-char-name n)) | |
2606 | (glyph-string (and nxml-char-ref-display-glyph-flag | |
92bbfd0d | 2607 | (nxml-glyph-display-string n 'nxml-glyph))) |
8cd39fb3 MH |
2608 | ov) |
2609 | (when (or name glyph-string) | |
2610 | (setq ov (make-overlay start end nil t)) | |
2611 | (overlay-put ov 'category 'nxml-char-ref) | |
2612 | (when name | |
2613 | (overlay-put ov 'help-echo name)) | |
2614 | (when glyph-string | |
2615 | (overlay-put ov | |
2616 | 'after-string | |
92bbfd0d | 2617 | (propertize glyph-string 'face 'nxml-glyph))))))) |
8cd39fb3 MH |
2618 | |
2619 | (defun nxml-clear-char-ref-extra-display (start end) | |
2620 | (let ((ov (overlays-in start end))) | |
2621 | (while ov | |
2622 | (when (eq (overlay-get (car ov) 'category) 'nxml-char-ref) | |
2623 | (delete-overlay (car ov))) | |
2624 | (setq ov (cdr ov))))) | |
2625 | ||
8cd39fb3 MH |
2626 | |
2627 | (defun nxml-start-delimiter-length (type) | |
2628 | (or (get type 'nxml-start-delimiter-length) | |
2629 | 0)) | |
10545bd8 | 2630 | |
8cd39fb3 MH |
2631 | (put 'cdata-section 'nxml-start-delimiter-length 9) |
2632 | (put 'comment 'nxml-start-delimiter-length 4) | |
2633 | (put 'processing-instruction 'nxml-start-delimiter-length 2) | |
2634 | (put 'start-tag 'nxml-start-delimiter-length 1) | |
2635 | (put 'empty-element 'nxml-start-delimiter-length 1) | |
2636 | (put 'partial-empty-element 'nxml-start-delimiter-length 1) | |
2637 | (put 'entity-ref 'nxml-start-delimiter-length 1) | |
2638 | (put 'char-ref 'nxml-start-delimiter-length 2) | |
2639 | ||
2640 | (defun nxml-end-delimiter-length (type) | |
2641 | (or (get type 'nxml-end-delimiter-length) | |
2642 | 0)) | |
10545bd8 | 2643 | |
8cd39fb3 MH |
2644 | (put 'cdata-section 'nxml-end-delimiter-length 3) |
2645 | (put 'comment 'nxml-end-delimiter-length 3) | |
2646 | (put 'processing-instruction 'nxml-end-delimiter-length 2) | |
2647 | (put 'start-tag 'nxml-end-delimiter-length 1) | |
2648 | (put 'empty-element 'nxml-end-delimiter-length 2) | |
2649 | (put 'partial-empty-element 'nxml-end-delimiter-length 1) | |
2650 | (put 'entity-ref 'nxml-end-delimiter-length 1) | |
2651 | (put 'char-ref 'nxml-end-delimiter-length 1) | |
2652 | ||
2653 | (defun nxml-token-type-friendly-name (type) | |
2654 | (or (get type 'nxml-friendly-name) | |
2655 | (symbol-name type))) | |
2656 | ||
2657 | (put 'cdata-section 'nxml-friendly-name "CDATA section") | |
2658 | (put 'processing-instruction 'nxml-friendly-name "processing instruction") | |
2659 | (put 'entity-ref 'nxml-friendly-name "entity reference") | |
2660 | (put 'char-ref 'nxml-friendly-name "character reference") | |
2661 | ||
e477ca84 CY |
2662 | ;;;###autoload |
2663 | (defalias 'xml-mode 'nxml-mode) | |
2664 | ||
8cd39fb3 MH |
2665 | (provide 'nxml-mode) |
2666 | ||
ab4c34c6 | 2667 | ;; arch-tag: 8603bc5f-1ef9-4021-b223-322fb2ca708e |
8cd39fb3 | 2668 | ;;; nxml-mode.el ends here |