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