Commit | Line | Data |
---|---|---|
3d6ce9c2 | 1 | ;;; sgml-mode.el --- SGML- and HTML-editing modes -*- coding: iso-2022-7bit -*- |
72c0ae01 | 2 | |
3731a850 | 3 | ;; Copyright (C) 1992, 1995, 1996, 1998, 2001, 2002, 2003, 2004, |
d7a0267c | 4 | ;; 2005, 2006, 2007 Free Software Foundation, Inc. |
6d74b528 | 5 | |
64ae0c23 | 6 | ;; Author: James Clark <jjc@jclark.com> |
0fda8eff | 7 | ;; Maintainer: FSF |
3e910376 | 8 | ;; Adapted-By: ESR, Daniel Pfeiffer <occitan@esperanto.org>, |
a391b179 | 9 | ;; F.Potorti@cnuce.cnr.it |
1caf38eb | 10 | ;; Keywords: wp, hypermedia, comm, languages |
72c0ae01 | 11 | |
72c0ae01 ER |
12 | ;; This file is part of GNU Emacs. |
13 | ||
14 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
15 | ;; it under the terms of the GNU General Public License as published by | |
7c938215 | 16 | ;; the Free Software Foundation; either version 2, or (at your option) |
72c0ae01 ER |
17 | ;; any later version. |
18 | ||
19 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
20 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | ;; GNU General Public License for more details. | |
23 | ||
24 | ;; You should have received a copy of the GNU General Public License | |
b578f267 | 25 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
4fc5845f LK |
26 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
27 | ;; Boston, MA 02110-1301, USA. | |
72c0ae01 ER |
28 | |
29 | ;;; Commentary: | |
30 | ||
1caf38eb RS |
31 | ;; Configurable major mode for editing document in the SGML standard general |
32 | ;; markup language. As an example contains a mode for editing the derived | |
33 | ;; HTML hypertext markup language. | |
72c0ae01 ER |
34 | |
35 | ;;; Code: | |
36 | ||
d4c89075 DL |
37 | (eval-when-compile |
38 | (require 'skeleton) | |
a06283b1 MW |
39 | (require 'outline) |
40 | (require 'cl)) | |
b0a377e6 | 41 | |
64ae0c23 | 42 | (defgroup sgml nil |
fae1a906 | 43 | "SGML editing mode." |
8ec3bce0 | 44 | :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces) |
64ae0c23 RS |
45 | :group 'languages) |
46 | ||
5f3d924d SM |
47 | (defcustom sgml-basic-offset 2 |
48 | "*Specifies the basic indentation level for `sgml-indent-line'." | |
49 | :type 'integer | |
50 | :group 'sgml) | |
51 | ||
aa18ec07 RW |
52 | (defcustom sgml-transformation-function 'identity |
53 | "*Default value for `skeleton-transformation-function' in SGML mode." | |
a391b179 | 54 | :type 'function |
c60e7b0d | 55 | :group 'sgml) |
a391b179 | 56 | |
aa18ec07 | 57 | (put 'sgml-transformation-function 'variable-interactive |
a391b179 | 58 | "aTransformation function: ") |
aa18ec07 | 59 | (defvaralias 'sgml-transformation 'sgml-transformation-function) |
a391b179 | 60 | |
d4c89075 DL |
61 | (defcustom sgml-mode-hook nil |
62 | "Hook run by command `sgml-mode'. | |
63 | `text-mode-hook' is run first." | |
64 | :group 'sgml | |
65 | :type 'hook) | |
66 | ||
1caf38eb RS |
67 | ;; As long as Emacs' syntax can't be complemented with predicates to context |
68 | ;; sensitively confirm the syntax of characters, we have to live with this | |
69 | ;; kludgy kind of tradeoff. | |
21a6f23c | 70 | (defvar sgml-specials '(?\") |
f788776c | 71 | "List of characters that have a special meaning for SGML mode. |
140d71ba | 72 | This list is used when first loading the `sgml-mode' library. |
1caf38eb RS |
73 | The supported characters and potential disadvantages are: |
74 | ||
75 | ?\\\" Makes \" in text start a string. | |
76 | ?' Makes ' in text start a string. | |
77 | ?- Makes -- in text start a comment. | |
78 | ||
4fa91cfe | 79 | When only one of ?\\\" or ?' are included, \"'\" or '\"', as can be found in |
1caf38eb | 80 | DTDs, start a string. To partially avoid this problem this also makes these |
21a6f23c RS |
81 | self insert as named entities depending on `sgml-quick-keys'. |
82 | ||
83 | Including ?- has the problem of affecting dashes that have nothing to do | |
84 | with comments, so we normally turn it off.") | |
fcc3195e RS |
85 | |
86 | (defvar sgml-quick-keys nil | |
2394187c | 87 | "Use <, >, &, /, SPC and `sgml-specials' keys \"electrically\" when non-nil. |
140d71ba | 88 | This takes effect when first loading the `sgml-mode' library.") |
1caf38eb | 89 | |
1caf38eb | 90 | (defvar sgml-mode-map |
e1940c83 | 91 | (let ((map (make-keymap)) ;`sparse' doesn't allow binding to charsets. |
1caf38eb | 92 | (menu-map (make-sparse-keymap "SGML"))) |
1caf38eb RS |
93 | (define-key map "\C-c\C-i" 'sgml-tags-invisible) |
94 | (define-key map "/" 'sgml-slash) | |
fcc3195e RS |
95 | (define-key map "\C-c\C-n" 'sgml-name-char) |
96 | (define-key map "\C-c\C-t" 'sgml-tag) | |
1caf38eb RS |
97 | (define-key map "\C-c\C-a" 'sgml-attributes) |
98 | (define-key map "\C-c\C-b" 'sgml-skip-tag-backward) | |
99 | (define-key map [?\C-c left] 'sgml-skip-tag-backward) | |
100 | (define-key map "\C-c\C-f" 'sgml-skip-tag-forward) | |
101 | (define-key map [?\C-c right] 'sgml-skip-tag-forward) | |
102 | (define-key map "\C-c\C-d" 'sgml-delete-tag) | |
103 | (define-key map "\C-c\^?" 'sgml-delete-tag) | |
104 | (define-key map "\C-c?" 'sgml-tag-help) | |
f6ab0573 | 105 | (define-key map "\C-c/" 'sgml-close-tag) |
1caf38eb RS |
106 | (define-key map "\C-c8" 'sgml-name-8bit-mode) |
107 | (define-key map "\C-c\C-v" 'sgml-validate) | |
b4f05c38 SS |
108 | (when sgml-quick-keys |
109 | (define-key map "&" 'sgml-name-char) | |
110 | (define-key map "<" 'sgml-tag) | |
111 | (define-key map " " 'sgml-auto-attributes) | |
112 | (define-key map ">" 'sgml-maybe-end-tag) | |
113 | (when (memq ?\" sgml-specials) | |
114 | (define-key map "\"" 'sgml-name-self)) | |
115 | (when (memq ?' sgml-specials) | |
116 | (define-key map "'" 'sgml-name-self))) | |
f7ac3e28 SM |
117 | (define-key map (vector (make-char 'latin-iso8859-1)) |
118 | 'sgml-maybe-name-self) | |
2840d653 EZ |
119 | (let ((c 127) |
120 | (map (nth 1 map))) | |
121 | (while (< (setq c (1+ c)) 256) | |
122 | (aset map c 'sgml-maybe-name-self))) | |
1caf38eb RS |
123 | (define-key map [menu-bar sgml] (cons "SGML" menu-map)) |
124 | (define-key menu-map [sgml-validate] '("Validate" . sgml-validate)) | |
125 | (define-key menu-map [sgml-name-8bit-mode] | |
126 | '("Toggle 8 Bit Insertion" . sgml-name-8bit-mode)) | |
127 | (define-key menu-map [sgml-tags-invisible] | |
128 | '("Toggle Tag Visibility" . sgml-tags-invisible)) | |
129 | (define-key menu-map [sgml-tag-help] | |
130 | '("Describe Tag" . sgml-tag-help)) | |
131 | (define-key menu-map [sgml-delete-tag] | |
132 | '("Delete Tag" . sgml-delete-tag)) | |
133 | (define-key menu-map [sgml-skip-tag-forward] | |
134 | '("Forward Tag" . sgml-skip-tag-forward)) | |
135 | (define-key menu-map [sgml-skip-tag-backward] | |
136 | '("Backward Tag" . sgml-skip-tag-backward)) | |
137 | (define-key menu-map [sgml-attributes] | |
138 | '("Insert Attributes" . sgml-attributes)) | |
139 | (define-key menu-map [sgml-tag] '("Insert Tag" . sgml-tag)) | |
140 | map) | |
141 | "Keymap for SGML mode. See also `sgml-specials'.") | |
142 | ||
1c1d2eb6 SM |
143 | (defun sgml-make-syntax-table (specials) |
144 | (let ((table (make-syntax-table text-mode-syntax-table))) | |
1caf38eb RS |
145 | (modify-syntax-entry ?< "(>" table) |
146 | (modify-syntax-entry ?> ")<" table) | |
1c1d2eb6 SM |
147 | (modify-syntax-entry ?: "_" table) |
148 | (modify-syntax-entry ?_ "_" table) | |
149 | (modify-syntax-entry ?. "_" table) | |
150 | (if (memq ?- specials) | |
1caf38eb | 151 | (modify-syntax-entry ?- "_ 1234" table)) |
1c1d2eb6 | 152 | (if (memq ?\" specials) |
1caf38eb | 153 | (modify-syntax-entry ?\" "\"\"" table)) |
1c1d2eb6 | 154 | (if (memq ?' specials) |
1caf38eb | 155 | (modify-syntax-entry ?\' "\"'" table)) |
1c1d2eb6 SM |
156 | table)) |
157 | ||
158 | (defvar sgml-mode-syntax-table (sgml-make-syntax-table sgml-specials) | |
1caf38eb RS |
159 | "Syntax table used in SGML mode. See also `sgml-specials'.") |
160 | ||
1c1d2eb6 SM |
161 | (defconst sgml-tag-syntax-table |
162 | (let ((table (sgml-make-syntax-table '(?- ?\" ?\')))) | |
163 | (dolist (char '(?\( ?\) ?\{ ?\} ?\[ ?\] ?$ ?% ?& ?* ?+ ?/)) | |
164 | (modify-syntax-entry char "." table)) | |
165 | table) | |
166 | "Syntax table used to parse SGML tags.") | |
167 | ||
64ae0c23 | 168 | (defcustom sgml-name-8bit-mode nil |
2840d653 | 169 | "*When non-nil, insert non-ASCII characters as named entities." |
64ae0c23 RS |
170 | :type 'boolean |
171 | :group 'sgml) | |
72c0ae01 | 172 | |
1caf38eb RS |
173 | (defvar sgml-char-names |
174 | [nil nil nil nil nil nil nil nil | |
175 | nil nil nil nil nil nil nil nil | |
176 | nil nil nil nil nil nil nil nil | |
177 | nil nil nil nil nil nil nil nil | |
a391b179 | 178 | "nbsp" "excl" "quot" "num" "dollar" "percnt" "amp" "apos" |
1caf38eb RS |
179 | "lpar" "rpar" "ast" "plus" "comma" "hyphen" "period" "sol" |
180 | nil nil nil nil nil nil nil nil | |
181 | nil nil "colon" "semi" "lt" "eq" "gt" "quest" | |
182 | "commat" nil nil nil nil nil nil nil | |
183 | nil nil nil nil nil nil nil nil | |
184 | nil nil nil nil nil nil nil nil | |
185 | nil nil nil "lsqb" nil "rsqb" "uarr" "lowbar" | |
186 | "lsquo" nil nil nil nil nil nil nil | |
187 | nil nil nil nil nil nil nil nil | |
188 | nil nil nil nil nil nil nil nil | |
189 | nil nil nil "lcub" "verbar" "rcub" "tilde" nil | |
190 | nil nil nil nil nil nil nil nil | |
191 | nil nil nil nil nil nil nil nil | |
192 | nil nil nil nil nil nil nil nil | |
193 | nil nil nil nil nil nil nil nil | |
194 | "nbsp" "iexcl" "cent" "pound" "curren" "yen" "brvbar" "sect" | |
195 | "uml" "copy" "ordf" "laquo" "not" "shy" "reg" "macr" | |
196 | "ring" "plusmn" "sup2" "sup3" "acute" "micro" "para" "middot" | |
e79ad8a1 | 197 | "cedil" "sup1" "ordm" "raquo" "frac14" "frac12" "frac34" "iquest" |
1caf38eb RS |
198 | "Agrave" "Aacute" "Acirc" "Atilde" "Auml" "Aring" "AElig" "Ccedil" |
199 | "Egrave" "Eacute" "Ecirc" "Euml" "Igrave" "Iacute" "Icirc" "Iuml" | |
200 | "ETH" "Ntilde" "Ograve" "Oacute" "Ocirc" "Otilde" "Ouml" nil | |
201 | "Oslash" "Ugrave" "Uacute" "Ucirc" "Uuml" "Yacute" "THORN" "szlig" | |
202 | "agrave" "aacute" "acirc" "atilde" "auml" "aring" "aelig" "ccedil" | |
203 | "egrave" "eacute" "ecirc" "euml" "igrave" "iacute" "icirc" "iuml" | |
204 | "eth" "ntilde" "ograve" "oacute" "ocirc" "otilde" "ouml" "divide" | |
205 | "oslash" "ugrave" "uacute" "ucirc" "uuml" "yacute" "thorn" "yuml"] | |
206 | "Vector of symbolic character names without `&' and `;'.") | |
207 | ||
2840d653 EZ |
208 | (put 'sgml-table 'char-table-extra-slots 0) |
209 | ||
210 | (defvar sgml-char-names-table | |
211 | (let ((table (make-char-table 'sgml-table)) | |
212 | (i 32) | |
213 | elt) | |
214 | (while (< i 256) | |
215 | (setq elt (aref sgml-char-names i)) | |
216 | (if elt (aset table (make-char 'latin-iso8859-1 i) elt)) | |
217 | (setq i (1+ i))) | |
218 | table) | |
219 | "A table for mapping non-ASCII characters into SGML entity names. | |
220 | Currently, only Latin-1 characters are supported.") | |
221 | ||
5121371d DL |
222 | ;; nsgmls is a free SGML parser in the SP suite available from |
223 | ;; ftp.jclark.com and otherwise packaged for GNU systems. | |
1caf38eb RS |
224 | ;; Its error messages can be parsed by next-error. |
225 | ;; The -s option suppresses output. | |
226 | ||
5121371d | 227 | (defcustom sgml-validate-command "nsgmls -s" ; replaced old `sgmls' |
72c0ae01 ER |
228 | "*The command to validate an SGML document. |
229 | The file name of current buffer file name will be appended to this, | |
64ae0c23 RS |
230 | separated by a space." |
231 | :type 'string | |
d4c89075 | 232 | :version "21.1" |
64ae0c23 | 233 | :group 'sgml) |
72c0ae01 ER |
234 | |
235 | (defvar sgml-saved-validate-command nil | |
236 | "The command last used to validate in this buffer.") | |
237 | ||
e1940c83 SM |
238 | ;; I doubt that null end tags are used much for large elements, |
239 | ;; so use a small distance here. | |
64ae0c23 | 240 | (defcustom sgml-slash-distance 1000 |
f788776c | 241 | "*If non-nil, is the maximum distance to search for matching `/'." |
64ae0c23 RS |
242 | :type '(choice (const nil) integer) |
243 | :group 'sgml) | |
72c0ae01 | 244 | |
b0045305 | 245 | (defconst sgml-namespace-re "[_[:alpha:]][-_.[:alnum:]]*") |
5f3d924d SM |
246 | (defconst sgml-name-re "[_:[:alpha:]][-_.:[:alnum:]]*") |
247 | (defconst sgml-tag-name-re (concat "<\\([!/?]?" sgml-name-re "\\)")) | |
248 | (defconst sgml-attrs-re "\\(?:[^\"'/><]\\|\"[^\"]*\"\\|'[^']*'\\)*") | |
249 | (defconst sgml-start-tag-regex (concat "<" sgml-name-re sgml-attrs-re) | |
1caf38eb | 250 | "Regular expression that matches a non-empty start tag. |
f788776c | 251 | Any terminating `>' or `/' is not matched.") |
1caf38eb | 252 | |
b390eb09 | 253 | (defface sgml-namespace |
b0045305 | 254 | '((t (:inherit font-lock-builtin-face))) |
dc11bf76 LK |
255 | "`sgml-mode' face used to highlight the namespace part of identifiers." |
256 | :group 'sgml) | |
b390eb09 | 257 | (defvar sgml-namespace-face 'sgml-namespace) |
1caf38eb | 258 | |
c6a63534 RS |
259 | ;; internal |
260 | (defconst sgml-font-lock-keywords-1 | |
5f3d924d | 261 | `((,(concat "<\\([!?]" sgml-name-re "\\)") 1 font-lock-keyword-face) |
b0045305 SM |
262 | ;; We could use the simpler "\\(" sgml-namespace-re ":\\)?" instead, |
263 | ;; but it would cause a bit more backtracking in the re-matcher. | |
264 | (,(concat "</?\\(" sgml-namespace-re "\\)\\(?::\\(" sgml-name-re "\\)\\)?") | |
265 | (1 (if (match-end 2) sgml-namespace-face font-lock-function-name-face)) | |
266 | (2 font-lock-function-name-face nil t)) | |
1c1d2eb6 | 267 | ;; FIXME: this doesn't cover the variables using a default value. |
9bbceb74 SM |
268 | ;; The first shy-group is an important anchor: it prevents an O(n^2) |
269 | ;; pathological case where we otherwise keep retrying a failing match | |
270 | ;; against a very long word at every possible position within the word. | |
271 | (,(concat "\\(?:^\\|[ \t]\\)\\(" sgml-namespace-re "\\)\\(?::\\(" | |
b0045305 SM |
272 | sgml-name-re "\\)\\)?=[\"']") |
273 | (1 (if (match-end 2) sgml-namespace-face font-lock-variable-name-face)) | |
274 | (2 font-lock-variable-name-face nil t)) | |
5f3d924d | 275 | (,(concat "[&%]" sgml-name-re ";?") . font-lock-variable-name-face))) |
64367655 SM |
276 | |
277 | (defconst sgml-font-lock-keywords-2 | |
278 | (append | |
279 | sgml-font-lock-keywords-1 | |
280 | '((eval | |
281 | . (cons (concat "<" | |
282 | (regexp-opt (mapcar 'car sgml-tag-face-alist) t) | |
283 | "\\([ \t][^>]*\\)?>\\([^<]+\\)</\\1>") | |
284 | '(3 (cdr (assoc (downcase (match-string 1)) | |
13b454db | 285 | sgml-tag-face-alist)) prepend)))))) |
c6a63534 RS |
286 | |
287 | ;; for font-lock, but must be defvar'ed after | |
288 | ;; sgml-font-lock-keywords-1 and sgml-font-lock-keywords-2 above | |
289 | (defvar sgml-font-lock-keywords sgml-font-lock-keywords-1 | |
1caf38eb RS |
290 | "*Rules for highlighting SGML code. See also `sgml-tag-face-alist'.") |
291 | ||
64367655 SM |
292 | (defvar sgml-font-lock-syntactic-keywords |
293 | ;; Use the `b' style of comments to avoid interference with the -- ... -- | |
294 | ;; comments recognized when `sgml-specials' includes ?-. | |
295 | ;; FIXME: beware of <!--> blabla <!--> !! | |
296 | '(("\\(<\\)!--" (1 "< b")) | |
297 | ("--[ \t\n]*\\(>\\)" (1 "> b"))) | |
298 | "Syntactic keywords for `sgml-mode'.") | |
299 | ||
1caf38eb | 300 | ;; internal |
1caf38eb RS |
301 | (defvar sgml-face-tag-alist () |
302 | "Alist of face and tag name for facemenu.") | |
303 | ||
304 | (defvar sgml-tag-face-alist () | |
305 | "Tag names and face or list of faces to fontify with when invisible. | |
306 | When `font-lock-maximum-decoration' is 1 this is always used for fontifying. | |
307 | When more these are fontified together with `sgml-font-lock-keywords'.") | |
308 | ||
1caf38eb RS |
309 | (defvar sgml-display-text () |
310 | "Tag names as lowercase symbols, and display string when invisible.") | |
311 | ||
312 | ;; internal | |
313 | (defvar sgml-tags-invisible nil) | |
314 | ||
64ae0c23 | 315 | (defcustom sgml-tag-alist |
fcc3195e RS |
316 | '(("![" ("ignore" t) ("include" t)) |
317 | ("!attlist") | |
1caf38eb RS |
318 | ("!doctype") |
319 | ("!element") | |
320 | ("!entity")) | |
321 | "*Alist of tag names for completing read and insertion rules. | |
322 | This alist is made up as | |
323 | ||
324 | ((\"tag\" . TAGRULE) | |
325 | ...) | |
326 | ||
9d4ce428 MW |
327 | TAGRULE is a list of optionally t (no endtag) or `\\n' (separate endtag by |
328 | newlines) or a skeleton with nil, t or `\\n' in place of the interactor | |
1caf38eb RS |
329 | followed by an ATTRIBUTERULE (for an always present attribute) or an |
330 | attribute alist. | |
331 | ||
332 | The attribute alist is made up as | |
333 | ||
334 | ((\"attribute\" . ATTRIBUTERULE) | |
335 | ...) | |
336 | ||
9d4ce428 | 337 | ATTRIBUTERULE is a list of optionally t (no value when no input) followed by |
64ae0c23 RS |
338 | an optional alist of possible values." |
339 | :type '(repeat (cons (string :tag "Tag Name") | |
340 | (repeat :tag "Tag Rule" sexp))) | |
341 | :group 'sgml) | |
aa18ec07 | 342 | (put 'sgml-tag-alist 'risky-local-variable t) |
1caf38eb | 343 | |
64ae0c23 | 344 | (defcustom sgml-tag-help |
1caf38eb RS |
345 | '(("!" . "Empty declaration for comment") |
346 | ("![" . "Embed declarations with parser directive") | |
347 | ("!attlist" . "Tag attributes declaration") | |
348 | ("!doctype" . "Document type (DTD) declaration") | |
349 | ("!element" . "Tag declaration") | |
350 | ("!entity" . "Entity (macro) declaration")) | |
64ae0c23 RS |
351 | "*Alist of tag name and short description." |
352 | :type '(repeat (cons (string :tag "Tag Name") | |
353 | (string :tag "Description"))) | |
354 | :group 'sgml) | |
1caf38eb | 355 | |
a3ec4ba0 | 356 | (defcustom sgml-xml-mode nil |
c77c3a73 SS |
357 | "*When non-nil, tag insertion functions will be XML-compliant. |
358 | If this variable is customized, the custom value is used always. | |
359 | Otherwise, it is set to be buffer-local when the file has | |
af3abed1 | 360 | a DOCTYPE or an XML declaration." |
c77c3a73 | 361 | :type 'boolean |
bf247b6e | 362 | :version "22.1" |
c77c3a73 SS |
363 | :group 'sgml) |
364 | ||
73d25e52 SM |
365 | (defvar sgml-empty-tags nil |
366 | "List of tags whose !ELEMENT definition says EMPTY.") | |
367 | ||
5f3d924d SM |
368 | (defvar sgml-unclosed-tags nil |
369 | "List of tags whose !ELEMENT definition says the end-tag is optional.") | |
370 | ||
c77c3a73 SS |
371 | (defun sgml-xml-guess () |
372 | "Guess whether the current buffer is XML." | |
373 | (save-excursion | |
374 | (goto-char (point-min)) | |
a3ec4ba0 SM |
375 | (when (or (string= "xml" (file-name-extension (or buffer-file-name ""))) |
376 | (looking-at "\\s-*<\\?xml") | |
377 | (when (re-search-forward | |
378 | (eval-when-compile | |
59444a9c SM |
379 | (mapconcat 'identity |
380 | '("<!DOCTYPE" "\\(\\w+\\)" "\\(\\w+\\)" | |
a3ec4ba0 SM |
381 | "\"\\([^\"]+\\)\"" "\"\\([^\"]+\\)\"") |
382 | "\\s-+")) | |
383 | nil t) | |
384 | (string-match "X\\(HT\\)?ML" (match-string 3)))) | |
385 | (set (make-local-variable 'sgml-xml-mode) t)))) | |
c77c3a73 | 386 | |
b0a377e6 DL |
387 | (defvar v2) ; free for skeleton |
388 | ||
60128096 SM |
389 | (defun sgml-comment-indent-new-line (&optional soft) |
390 | (let ((comment-start "-- ") | |
391 | (comment-start-skip "\\(<!\\)?--[ \t]*") | |
392 | (comment-end " --") | |
393 | (comment-style 'plain)) | |
394 | (comment-indent-new-line soft))) | |
395 | ||
a3ec4ba0 SM |
396 | (defun sgml-mode-facemenu-add-face-function (face end) |
397 | (if (setq face (cdr (assq face sgml-face-tag-alist))) | |
398 | (progn | |
aa18ec07 | 399 | (setq face (funcall skeleton-transformation-function face)) |
a3ec4ba0 SM |
400 | (setq facemenu-end-add-face (concat "</" face ">")) |
401 | (concat "<" face ">")) | |
402 | (error "Face not configured for %s mode" mode-name))) | |
403 | ||
ed8031f2 SM |
404 | (defun sgml-fill-nobreak () |
405 | ;; Don't break between a tag name and its first argument. | |
406 | (save-excursion | |
407 | (skip-chars-backward " \t") | |
408 | (and (not (zerop (skip-syntax-backward "w_"))) | |
409 | (skip-chars-backward "/?!") | |
410 | (eq (char-before) ?<)))) | |
411 | ||
a3ec4ba0 SM |
412 | ;;;###autoload |
413 | (define-derived-mode sgml-mode text-mode "SGML" | |
414 | "Major mode for editing SGML documents. | |
415 | Makes > match <. | |
2394187c | 416 | Keys <, &, SPC within <>, \", / and ' can be electric depending on |
a3ec4ba0 SM |
417 | `sgml-quick-keys'. |
418 | ||
419 | An argument of N to a tag-inserting command means to wrap it around | |
420 | the next N words. In Transient Mark mode, when the mark is active, | |
421 | N defaults to -1, which means to wrap it around the current region. | |
422 | ||
aa18ec07 RW |
423 | If you like upcased tags, put (setq sgml-transformation-function 'upcase) |
424 | in your `.emacs' file. | |
a3ec4ba0 SM |
425 | |
426 | Use \\[sgml-validate] to validate your document with an SGML parser. | |
427 | ||
428 | Do \\[describe-variable] sgml- SPC to see available variables. | |
429 | Do \\[describe-key] on the following bindings to discover what they do. | |
430 | \\{sgml-mode-map}" | |
72c0ae01 | 431 | (make-local-variable 'sgml-saved-validate-command) |
1caf38eb RS |
432 | (make-local-variable 'facemenu-end-add-face) |
433 | ;;(make-local-variable 'facemenu-remove-face-function) | |
c77c3a73 SS |
434 | ;; A start or end tag by itself on a line separates a paragraph. |
435 | ;; This is desirable because SGML discards a newline that appears | |
436 | ;; immediately after a start tag or immediately before an end tag. | |
5f3d924d SM |
437 | (set (make-local-variable 'paragraph-start) (concat "[ \t]*$\\|\ |
438 | \[ \t]*</?\\(" sgml-name-re sgml-attrs-re "\\)?>")) | |
439 | (set (make-local-variable 'paragraph-separate) | |
440 | (concat paragraph-start "$")) | |
c77c3a73 | 441 | (set (make-local-variable 'adaptive-fill-regexp) "[ \t]*") |
ed8031f2 | 442 | (add-hook 'fill-nobreak-predicate 'sgml-fill-nobreak nil t) |
9c599518 | 443 | (set (make-local-variable 'indent-line-function) 'sgml-indent-line) |
c77c3a73 SS |
444 | (set (make-local-variable 'comment-start) "<!-- ") |
445 | (set (make-local-variable 'comment-end) " -->") | |
446 | (set (make-local-variable 'comment-indent-function) 'sgml-comment-indent) | |
60128096 SM |
447 | (set (make-local-variable 'comment-line-break-function) |
448 | 'sgml-comment-indent-new-line) | |
c77c3a73 SS |
449 | (set (make-local-variable 'skeleton-further-elements) |
450 | '((completion-ignore-case t))) | |
451 | (set (make-local-variable 'skeleton-end-hook) | |
452 | (lambda () | |
453 | (or (eolp) | |
454 | (not (or (eq v2 '\n) (eq (car-safe v2) '\n))) | |
455 | (newline-and-indent)))) | |
456 | (set (make-local-variable 'font-lock-defaults) | |
457 | '((sgml-font-lock-keywords | |
458 | sgml-font-lock-keywords-1 | |
459 | sgml-font-lock-keywords-2) | |
460 | nil t nil nil | |
461 | (font-lock-syntactic-keywords | |
462 | . sgml-font-lock-syntactic-keywords))) | |
463 | (set (make-local-variable 'facemenu-add-face-function) | |
464 | 'sgml-mode-facemenu-add-face-function) | |
a3ec4ba0 SM |
465 | (sgml-xml-guess) |
466 | (if sgml-xml-mode | |
467 | (setq mode-name "XML") | |
aa18ec07 RW |
468 | (set (make-local-variable 'skeleton-transformation-function) |
469 | sgml-transformation-function)) | |
4afa094d SM |
470 | ;; This will allow existing comments within declarations to be |
471 | ;; recognized. | |
472 | (set (make-local-variable 'comment-start-skip) "\\(?:<!\\)?--[ \t]*") | |
a3ec4ba0 | 473 | (set (make-local-variable 'comment-end-skip) "[ \t]*--\\([ \t\n]*>\\)?") |
4456f964 DP |
474 | ;; This definition has an HTML leaning but probably fits well for other modes. |
475 | (setq imenu-generic-expression | |
476 | `((nil | |
477 | ,(concat "<!\\(element\\|entity\\)[ \t\n]+%?[ \t\n]*\\(" | |
478 | sgml-name-re "\\)") | |
479 | 2) | |
480 | ("Id" | |
481 | ,(concat "<[^>]+[ \t\n]+[Ii][Dd]=\\(['\"]" | |
482 | (if sgml-xml-mode "" "?") | |
483 | "\\)\\(" sgml-name-re "\\)\\1") | |
484 | 2) | |
485 | ("Name" | |
486 | ,(concat "<[^>]+[ \t\n]+[Nn][Aa][Mm][Ee]=\\(['\"]" | |
487 | (if sgml-xml-mode "" "?") | |
488 | "\\)\\(" sgml-name-re "\\)\\1") | |
489 | 2)))) | |
1caf38eb | 490 | |
9d118494 CW |
491 | ;; Some programs (such as Glade 2) generate XML which has |
492 | ;; -*- mode: xml -*-. | |
7461dcb0 | 493 | ;;;###autoload |
9d118494 CW |
494 | (defalias 'xml-mode 'sgml-mode) |
495 | ||
72c0ae01 | 496 | (defun sgml-comment-indent () |
4afa094d | 497 | (if (looking-at "--") comment-column 0)) |
72c0ae01 | 498 | |
72c0ae01 | 499 | (defun sgml-slash (arg) |
2394187c SM |
500 | "Insert ARG slash characters. |
501 | Behaves electrically if `sgml-quick-keys' is non-nil." | |
502 | (interactive "p") | |
503 | (cond | |
504 | ((not (and (eq (char-before) ?<) (= arg 1))) | |
505 | (sgml-slash-matching arg)) | |
506 | ((eq sgml-quick-keys 'indent) | |
507 | (insert-char ?/ 1) | |
508 | (indent-according-to-mode)) | |
509 | ((eq sgml-quick-keys 'close) | |
510 | (delete-backward-char 1) | |
f6ab0573 | 511 | (sgml-close-tag)) |
2394187c SM |
512 | (t |
513 | (sgml-slash-matching arg)))) | |
514 | ||
515 | (defun sgml-slash-matching (arg) | |
f788776c RS |
516 | "Insert `/' and display any previous matching `/'. |
517 | Two `/'s are treated as matching if the first `/' ends a net-enabling | |
518 | start tag, and the second `/' is the corresponding null end tag." | |
72c0ae01 ER |
519 | (interactive "p") |
520 | (insert-char ?/ arg) | |
521 | (if (> arg 0) | |
522 | (let ((oldpos (point)) | |
523 | (blinkpos) | |
524 | (level 0)) | |
525 | (save-excursion | |
526 | (save-restriction | |
527 | (if sgml-slash-distance | |
528 | (narrow-to-region (max (point-min) | |
529 | (- (point) sgml-slash-distance)) | |
530 | oldpos)) | |
531 | (if (and (re-search-backward sgml-start-tag-regex (point-min) t) | |
532 | (eq (match-end 0) (1- oldpos))) | |
533 | () | |
534 | (goto-char (1- oldpos)) | |
535 | (while (and (not blinkpos) | |
536 | (search-backward "/" (point-min) t)) | |
537 | (let ((tagend (save-excursion | |
538 | (if (re-search-backward sgml-start-tag-regex | |
539 | (point-min) t) | |
540 | (match-end 0) | |
541 | nil)))) | |
542 | (if (eq tagend (point)) | |
543 | (if (eq level 0) | |
544 | (setq blinkpos (point)) | |
545 | (setq level (1- level))) | |
546 | (setq level (1+ level))))))) | |
5950e029 SS |
547 | (when blinkpos |
548 | (goto-char blinkpos) | |
549 | (if (pos-visible-in-window-p) | |
550 | (sit-for 1) | |
551 | (message "Matches %s" | |
552 | (buffer-substring (line-beginning-position) | |
553 | (1+ blinkpos))))))))) | |
72c0ae01 | 554 | |
0fda8eff SM |
555 | ;; Why doesn't this use the iso-cvt table or, preferably, generate the |
556 | ;; inverse of the extensive table in the SGML Quail input method? -- fx | |
557 | ;; I guess that's moot since it only works with Latin-1 anyhow. | |
1caf38eb RS |
558 | (defun sgml-name-char (&optional char) |
559 | "Insert a symbolic character name according to `sgml-char-names'. | |
2840d653 EZ |
560 | Non-ASCII chars may be inserted either with the meta key, as in M-SPC for |
561 | no-break space or M-- for a soft hyphen; or via an input method or | |
562 | encoded keyboard operation." | |
1caf38eb RS |
563 | (interactive "*") |
564 | (insert ?&) | |
565 | (or char | |
9b0ffdac | 566 | (setq char (read-quoted-char "Enter char or octal number"))) |
1caf38eb RS |
567 | (delete-backward-char 1) |
568 | (insert char) | |
569 | (undo-boundary) | |
4e7a42d2 SM |
570 | (sgml-namify-char)) |
571 | ||
572 | (defun sgml-namify-char () | |
573 | "Change the char before point into its `&name;' equivalent. | |
574 | Uses `sgml-char-names'." | |
575 | (interactive) | |
576 | (let* ((char (char-before)) | |
577 | (name | |
578 | (cond | |
579 | ((null char) (error "No char before point")) | |
580 | ((< char 256) (or (aref sgml-char-names char) char)) | |
581 | ((aref sgml-char-names-table char)) | |
582 | ((encode-char char 'ucs))))) | |
583 | (if (not name) | |
584 | (error "Don't know the name of `%c'" char) | |
585 | (delete-backward-char 1) | |
586 | (insert (format (if (numberp name) "&#%d;" "&%s;") name))))) | |
1caf38eb RS |
587 | |
588 | (defun sgml-name-self () | |
589 | "Insert a symbolic character name according to `sgml-char-names'." | |
590 | (interactive "*") | |
591 | (sgml-name-char last-command-char)) | |
592 | ||
1caf38eb RS |
593 | (defun sgml-maybe-name-self () |
594 | "Insert a symbolic character name according to `sgml-char-names'." | |
595 | (interactive "*") | |
596 | (if sgml-name-8bit-mode | |
2840d653 EZ |
597 | (let ((mc last-command-char)) |
598 | (if (< mc 256) | |
599 | (setq mc (unibyte-char-to-multibyte mc))) | |
600 | (or mc (setq mc last-command-char)) | |
601 | (sgml-name-char mc)) | |
1caf38eb RS |
602 | (self-insert-command 1))) |
603 | ||
1caf38eb | 604 | (defun sgml-name-8bit-mode () |
0fda8eff SM |
605 | "Toggle whether to insert named entities instead of non-ASCII characters. |
606 | This only works for Latin-1 input." | |
1caf38eb | 607 | (interactive) |
d10447ba | 608 | (setq sgml-name-8bit-mode (not sgml-name-8bit-mode)) |
2840d653 | 609 | (message "sgml name entity mode is now %s" |
d10447ba | 610 | (if sgml-name-8bit-mode "ON" "OFF"))) |
1caf38eb | 611 | |
f788776c | 612 | ;; When an element of a skeleton is a string "str", it is passed |
aa18ec07 RW |
613 | ;; through `skeleton-transformation-function' and inserted. |
614 | ;; If "str" is to be inserted literally, one should obtain it as | |
615 | ;; the return value of a function, e.g. (identity "str"). | |
1caf38eb | 616 | |
4e7a42d2 SM |
617 | (defvar sgml-tag-last nil) |
618 | (defvar sgml-tag-history nil) | |
1caf38eb | 619 | (define-skeleton sgml-tag |
f788776c RS |
620 | "Prompt for a tag and insert it, optionally with attributes. |
621 | Completion and configuration are done according to `sgml-tag-alist'. | |
d10447ba | 622 | If you like tags and attributes in uppercase do \\[set-variable] |
aa18ec07 RW |
623 | `skeleton-transformation-function' RET `upcase' RET, or put this |
624 | in your `.emacs': | |
625 | (setq sgml-transformation-function 'upcase)" | |
626 | (funcall (or skeleton-transformation-function 'identity) | |
4e7a42d2 SM |
627 | (setq sgml-tag-last |
628 | (completing-read | |
629 | (if (> (length sgml-tag-last) 0) | |
630 | (format "Tag (default %s): " sgml-tag-last) | |
631 | "Tag: ") | |
632 | sgml-tag-alist nil nil nil 'sgml-tag-history sgml-tag-last))) | |
4afa094d | 633 | ?< str | |
d10447ba | 634 | (("") -1 '(undo-boundary) (identity "<")) | ; see comment above |
73d25e52 SM |
635 | `(("") '(setq v2 (sgml-attributes ,str t)) ?> |
636 | (cond | |
637 | ((string= "![" ,str) | |
638 | (backward-char) | |
639 | '(("") " [ " _ " ]]")) | |
a3ec4ba0 | 640 | ((and (eq v2 t) sgml-xml-mode (member ,str sgml-empty-tags)) |
af3abed1 | 641 | '(("") -1 " />")) |
a3ec4ba0 | 642 | ((or (and (eq v2 t) (not sgml-xml-mode)) (string-match "^[/!?]" ,str)) |
73d25e52 SM |
643 | nil) |
644 | ((symbolp v2) | |
645 | ;; Make sure we don't fall into an infinite loop. | |
646 | ;; For xhtml's `tr' tag, we should maybe use \n instead. | |
647 | (if (eq v2 t) (setq v2 nil)) | |
648 | ;; We use `identity' to prevent skeleton from passing | |
aa18ec07 | 649 | ;; `str' through `skeleton-transformation-function' a second time. |
73d25e52 SM |
650 | '(("") v2 _ v2 "</" (identity ',str) ?>)) |
651 | ((eq (car v2) t) | |
652 | (cons '("") (cdr v2))) | |
653 | (t | |
654 | (append '(("") (car v2)) | |
655 | (cdr v2) | |
656 | '(resume: (car v2) _ "</" (identity ',str) ?>)))))) | |
1caf38eb RS |
657 | |
658 | (autoload 'skeleton-read "skeleton") | |
659 | ||
d10447ba | 660 | (defun sgml-attributes (tag &optional quiet) |
f788776c | 661 | "When at top level of a tag, interactively insert attributes. |
d10447ba | 662 | |
f788776c RS |
663 | Completion and configuration of TAG are done according to `sgml-tag-alist'. |
664 | If QUIET, do not print a message when there are no attributes for TAG." | |
1caf38eb | 665 | (interactive (list (save-excursion (sgml-beginning-of-tag t)))) |
d10447ba RS |
666 | (or (stringp tag) (error "Wrong context for adding attribute")) |
667 | (if tag | |
1caf38eb | 668 | (let ((completion-ignore-case t) |
d10447ba | 669 | (alist (cdr (assoc (downcase tag) sgml-tag-alist))) |
1caf38eb | 670 | car attribute i) |
1caf38eb RS |
671 | (if (or (symbolp (car alist)) |
672 | (symbolp (car (car alist)))) | |
673 | (setq car (car alist) | |
674 | alist (cdr alist))) | |
675 | (or quiet | |
676 | (message "No attributes configured.")) | |
677 | (if (stringp (car alist)) | |
678 | (progn | |
fae1a906 | 679 | (insert (if (eq (preceding-char) ?\s) "" ?\s) |
aa18ec07 | 680 | (funcall skeleton-transformation-function (car alist))) |
1caf38eb RS |
681 | (sgml-value alist)) |
682 | (setq i (length alist)) | |
683 | (while (> i 0) | |
fae1a906 | 684 | (insert ?\s) |
aa18ec07 | 685 | (insert (funcall skeleton-transformation-function |
1caf38eb RS |
686 | (setq attribute |
687 | (skeleton-read '(completing-read | |
d10447ba | 688 | "Attribute: " |
1caf38eb RS |
689 | alist))))) |
690 | (if (string= "" attribute) | |
691 | (setq i 0) | |
aa7a8f0e | 692 | (sgml-value (assoc (downcase attribute) alist)) |
1caf38eb | 693 | (setq i (1- i)))) |
fae1a906 | 694 | (if (eq (preceding-char) ?\s) |
1caf38eb RS |
695 | (delete-backward-char 1))) |
696 | car))) | |
697 | ||
698 | (defun sgml-auto-attributes (arg) | |
f788776c RS |
699 | "Self insert the character typed; at top level of tag, prompt for attributes. |
700 | With prefix argument, only self insert." | |
1caf38eb RS |
701 | (interactive "*P") |
702 | (let ((point (point)) | |
703 | tag) | |
704 | (if (or arg | |
1caf38eb RS |
705 | (not sgml-tag-alist) ; no message when nothing configured |
706 | (symbolp (setq tag (save-excursion (sgml-beginning-of-tag t)))) | |
707 | (eq (aref tag 0) ?/)) | |
708 | (self-insert-command (prefix-numeric-value arg)) | |
709 | (sgml-attributes tag) | |
fae1a906 | 710 | (setq last-command-char ?\s) |
1caf38eb RS |
711 | (or (> (point) point) |
712 | (self-insert-command 1))))) | |
713 | ||
1caf38eb | 714 | (defun sgml-tag-help (&optional tag) |
f788776c | 715 | "Display description of tag TAG. If TAG is omitted, use the tag at point." |
1caf38eb RS |
716 | (interactive) |
717 | (or tag | |
718 | (save-excursion | |
719 | (if (eq (following-char) ?<) | |
720 | (forward-char)) | |
721 | (setq tag (sgml-beginning-of-tag)))) | |
722 | (or (stringp tag) | |
723 | (error "No tag selected")) | |
724 | (setq tag (downcase tag)) | |
f68f40e0 | 725 | (message "%s" |
aa7a8f0e | 726 | (or (cdr (assoc (downcase tag) sgml-tag-help)) |
1caf38eb | 727 | (and (eq (aref tag 0) ?/) |
aa7a8f0e | 728 | (cdr (assoc (downcase (substring tag 1)) sgml-tag-help))) |
1caf38eb RS |
729 | "No description available"))) |
730 | ||
1c1d2eb6 SM |
731 | (defun sgml-maybe-end-tag (&optional arg) |
732 | "Name self unless in position to end a tag or a prefix ARG is given." | |
733 | (interactive "P") | |
734 | (if (or arg (eq (car (sgml-lexical-context)) 'tag)) | |
735 | (self-insert-command (prefix-numeric-value arg)) | |
736 | (sgml-name-self))) | |
1caf38eb RS |
737 | |
738 | (defun sgml-skip-tag-backward (arg) | |
739 | "Skip to beginning of tag or matching opening tag if present. | |
f788776c | 740 | With prefix argument ARG, repeat this ARG times." |
1caf38eb | 741 | (interactive "p") |
4e7a42d2 | 742 | ;; FIXME: use sgml-get-context or something similar. |
1caf38eb RS |
743 | (while (>= arg 1) |
744 | (search-backward "<" nil t) | |
745 | (if (looking-at "</\\([^ \n\t>]+\\)") | |
746 | ;; end tag, skip any nested pairs | |
747 | (let ((case-fold-search t) | |
65b34485 SM |
748 | (re (concat "</?" (regexp-quote (match-string 1)) |
749 | ;; Ignore empty tags like <foo/>. | |
750 | "\\([^>]*[^/>]\\)?>"))) | |
1caf38eb RS |
751 | (while (and (re-search-backward re nil t) |
752 | (eq (char-after (1+ (point))) ?/)) | |
753 | (forward-char 1) | |
754 | (sgml-skip-tag-backward 1)))) | |
755 | (setq arg (1- arg)))) | |
756 | ||
65b34485 | 757 | (defun sgml-skip-tag-forward (arg) |
1caf38eb | 758 | "Skip to end of tag or matching closing tag if present. |
f788776c | 759 | With prefix argument ARG, repeat this ARG times. |
1caf38eb RS |
760 | Return t iff after a closing tag." |
761 | (interactive "p") | |
4e7a42d2 SM |
762 | ;; FIXME: Use sgml-get-context or something similar. |
763 | ;; It currently might jump to an unrelated </P> if the <P> | |
764 | ;; we're skipping has no matching </P>. | |
65b34485 | 765 | (let ((return t)) |
4e7a42d2 SM |
766 | (with-syntax-table sgml-tag-syntax-table |
767 | (while (>= arg 1) | |
768 | (skip-chars-forward "^<>") | |
769 | (if (eq (following-char) ?>) | |
770 | (up-list -1)) | |
771 | (if (looking-at "<\\([^/ \n\t>]+\\)\\([^>]*[^/>]\\)?>") | |
772 | ;; start tag, skip any nested same pairs _and_ closing tag | |
773 | (let ((case-fold-search t) | |
774 | (re (concat "</?" (regexp-quote (match-string 1)) | |
775 | ;; Ignore empty tags like <foo/>. | |
776 | "\\([^>]*[^/>]\\)?>")) | |
777 | point close) | |
778 | (forward-list 1) | |
779 | (setq point (point)) | |
780 | ;; FIXME: This re-search-forward will mistakenly match | |
781 | ;; tag-like text inside attributes. | |
782 | (while (and (re-search-forward re nil t) | |
783 | (not (setq close | |
784 | (eq (char-after (1+ (match-beginning 0))) ?/))) | |
785 | (goto-char (match-beginning 0)) | |
786 | (sgml-skip-tag-forward 1)) | |
787 | (setq close nil)) | |
788 | (unless close | |
789 | (goto-char point) | |
790 | (setq return nil))) | |
791 | (forward-list 1)) | |
792 | (setq arg (1- arg))) | |
793 | return))) | |
1caf38eb RS |
794 | |
795 | (defun sgml-delete-tag (arg) | |
4e7a42d2 | 796 | ;; FIXME: Should be called sgml-kill-tag or should not touch the kill-ring. |
1caf38eb | 797 | "Delete tag on or after cursor, and matching closing or opening tag. |
f788776c | 798 | With prefix argument ARG, repeat this ARG times." |
1caf38eb RS |
799 | (interactive "p") |
800 | (while (>= arg 1) | |
801 | (save-excursion | |
802 | (let* (close open) | |
fcc3195e | 803 | (if (looking-at "[ \t\n]*<") |
1caf38eb RS |
804 | ;; just before tag |
805 | (if (eq (char-after (match-end 0)) ?/) | |
806 | ;; closing tag | |
807 | (progn | |
808 | (setq close (point)) | |
809 | (goto-char (match-end 0)))) | |
810 | ;; on tag? | |
811 | (or (save-excursion (setq close (sgml-beginning-of-tag) | |
812 | close (and (stringp close) | |
813 | (eq (aref close 0) ?/) | |
814 | (point)))) | |
815 | ;; not on closing tag | |
816 | (let ((point (point))) | |
817 | (sgml-skip-tag-backward 1) | |
818 | (if (or (not (eq (following-char) ?<)) | |
819 | (save-excursion | |
820 | (forward-list 1) | |
821 | (<= (point) point))) | |
822 | (error "Not on or before tag"))))) | |
823 | (if close | |
824 | (progn | |
825 | (sgml-skip-tag-backward 1) | |
826 | (setq open (point)) | |
827 | (goto-char close) | |
828 | (kill-sexp 1)) | |
829 | (setq open (point)) | |
af3abed1 JL |
830 | (when (and (sgml-skip-tag-forward 1) |
831 | (not (looking-back "/>"))) | |
4e7a42d2 SM |
832 | (kill-sexp -1))) |
833 | ;; Delete any resulting empty line. If we didn't kill-sexp, | |
834 | ;; this *should* do nothing, because we're right after the tag. | |
835 | (if (progn (forward-line 0) (looking-at "\\(?:[ \t]*$\\)\n?")) | |
836 | (delete-region (match-beginning 0) (match-end 0))) | |
1caf38eb | 837 | (goto-char open) |
4e7a42d2 SM |
838 | (kill-sexp 1) |
839 | (if (progn (forward-line 0) (looking-at "\\(?:[ \t]*$\\)\n?")) | |
840 | (delete-region (match-beginning 0) (match-end 0))))) | |
1caf38eb | 841 | (setq arg (1- arg)))) |
9d4ce428 | 842 | |
a391b179 RS |
843 | \f |
844 | ;; Put read-only last to enable setting this even when read-only enabled. | |
845 | (or (get 'sgml-tag 'invisible) | |
846 | (setplist 'sgml-tag | |
847 | (append '(invisible t | |
a391b179 RS |
848 | point-entered sgml-point-entered |
849 | rear-nonsticky t | |
850 | read-only t) | |
851 | (symbol-plist 'sgml-tag)))) | |
1caf38eb RS |
852 | |
853 | (defun sgml-tags-invisible (arg) | |
854 | "Toggle visibility of existing tags." | |
855 | (interactive "P") | |
856 | (let ((modified (buffer-modified-p)) | |
857 | (inhibit-read-only t) | |
e1940c83 SM |
858 | (inhibit-modification-hooks t) |
859 | ;; Avoid spurious the `file-locked' checks. | |
860 | (buffer-file-name nil) | |
a391b179 RS |
861 | ;; This is needed in case font lock gets called, |
862 | ;; since it moves point and might call sgml-point-entered. | |
64367655 | 863 | ;; How could it get called? -stef |
a391b179 | 864 | (inhibit-point-motion-hooks t) |
64367655 | 865 | string) |
e1940c83 SM |
866 | (unwind-protect |
867 | (save-excursion | |
868 | (goto-char (point-min)) | |
73d25e52 SM |
869 | (if (set (make-local-variable 'sgml-tags-invisible) |
870 | (if arg | |
871 | (>= (prefix-numeric-value arg) 0) | |
872 | (not sgml-tags-invisible))) | |
1c1d2eb6 | 873 | (while (re-search-forward sgml-tag-name-re nil t) |
64367655 SM |
874 | (setq string |
875 | (cdr (assq (intern-soft (downcase (match-string 1))) | |
876 | sgml-display-text))) | |
e1940c83 | 877 | (goto-char (match-beginning 0)) |
64367655 | 878 | (and (stringp string) |
e1940c83 | 879 | (not (overlays-at (point))) |
73d25e52 SM |
880 | (let ((ol (make-overlay (point) (match-beginning 1)))) |
881 | (overlay-put ol 'before-string string) | |
882 | (overlay-put ol 'sgml-tag t))) | |
e1940c83 SM |
883 | (put-text-property (point) |
884 | (progn (forward-list) (point)) | |
885 | 'category 'sgml-tag)) | |
64367655 | 886 | (let ((pos (point-min))) |
e1940c83 | 887 | (while (< (setq pos (next-overlay-change pos)) (point-max)) |
73d25e52 | 888 | (dolist (ol (overlays-at pos)) |
b2e8c203 | 889 | (if (overlay-get ol 'sgml-tag) |
73d25e52 | 890 | (delete-overlay ol))))) |
64367655 | 891 | (remove-text-properties (point-min) (point-max) '(category nil)))) |
e1940c83 | 892 | (restore-buffer-modified-p modified)) |
1caf38eb RS |
893 | (run-hooks 'sgml-tags-invisible-hook) |
894 | (message ""))) | |
895 | ||
896 | (defun sgml-point-entered (x y) | |
897 | ;; Show preceding or following hidden tag, depending of cursor direction. | |
898 | (let ((inhibit-point-motion-hooks t)) | |
899 | (save-excursion | |
900 | (message "Invisible tag: %s" | |
e1940c83 SM |
901 | ;; Strip properties, otherwise, the text is invisible. |
902 | (buffer-substring-no-properties | |
1caf38eb RS |
903 | (point) |
904 | (if (or (and (> x y) | |
905 | (not (eq (following-char) ?<))) | |
906 | (and (< x y) | |
907 | (eq (preceding-char) ?>))) | |
908 | (backward-list) | |
909 | (forward-list))))))) | |
9d4ce428 | 910 | |
a391b179 | 911 | \f |
72c0ae01 ER |
912 | (defun sgml-validate (command) |
913 | "Validate an SGML document. | |
914 | Runs COMMAND, a shell command, in a separate process asynchronously | |
f788776c | 915 | with output going to the buffer `*compilation*'. |
72c0ae01 ER |
916 | You can then use the command \\[next-error] to find the next error message |
917 | and move to the line in the SGML document that caused it." | |
918 | (interactive | |
919 | (list (read-string "Validate command: " | |
920 | (or sgml-saved-validate-command | |
921 | (concat sgml-validate-command | |
922 | " " | |
da8092ef EZ |
923 | (shell-quote-argument |
924 | (let ((name (buffer-file-name))) | |
925 | (and name | |
926 | (file-name-nondirectory name))))))))) | |
72c0ae01 | 927 | (setq sgml-saved-validate-command command) |
b7cd1746 | 928 | (save-some-buffers (not compilation-ask-about-save) nil) |
27024d2f | 929 | (compilation-start command)) |
72c0ae01 | 930 | |
662deeab MW |
931 | (defsubst sgml-at-indentation-p () |
932 | "Return true if point is at the first non-whitespace character on the line." | |
933 | (save-excursion | |
934 | (skip-chars-backward " \t") | |
935 | (bolp))) | |
936 | ||
1c1d2eb6 SM |
937 | (defun sgml-lexical-context (&optional limit) |
938 | "Return the lexical context at point as (TYPE . START). | |
939 | START is the location of the start of the lexical element. | |
2cfd19d4 | 940 | TYPE is one of `string', `comment', `tag', `cdata', or `text'. |
1c1d2eb6 | 941 | |
41bfcbee MW |
942 | Optional argument LIMIT is the position to start parsing from. |
943 | If nil, start from a preceding tag at indentation." | |
1c1d2eb6 SM |
944 | (save-excursion |
945 | (let ((pos (point)) | |
14614b6d | 946 | text-start state) |
41bfcbee MW |
947 | (if limit |
948 | (goto-char limit) | |
949 | ;; Skip tags backwards until we find one at indentation | |
950 | (while (and (ignore-errors (sgml-parse-tag-backward)) | |
951 | (not (sgml-at-indentation-p))))) | |
5f3d924d SM |
952 | (with-syntax-table sgml-tag-syntax-table |
953 | (while (< (point) pos) | |
954 | ;; When entering this loop we're inside text. | |
80fc318e | 955 | (setq text-start (point)) |
5f3d924d | 956 | (skip-chars-forward "^<" pos) |
14614b6d MW |
957 | (setq state |
958 | (cond | |
60128096 | 959 | ((= (point) pos) |
14614b6d MW |
960 | ;; We got to the end without seeing a tag. |
961 | nil) | |
962 | ((looking-at "<!\\[[A-Z]+\\[") | |
963 | ;; We've found a CDATA section or similar. | |
964 | (let ((cdata-start (point))) | |
965 | (unless (search-forward "]]>" pos 'move) | |
966 | (list 0 nil nil 'cdata nil nil nil nil cdata-start)))) | |
967 | (t | |
2871b07a | 968 | ;; We've reached a tag. Parse it. |
14614b6d MW |
969 | ;; FIXME: Handle net-enabling start-tags |
970 | (parse-partial-sexp (point) pos 0)))))) | |
971 | (cond | |
972 | ((eq (nth 3 state) 'cdata) (cons 'cdata (nth 8 state))) | |
973 | ((nth 3 state) (cons 'string (nth 8 state))) | |
974 | ((nth 4 state) (cons 'comment (nth 8 state))) | |
975 | ((and state (> (nth 0 state) 0)) (cons 'tag (nth 1 state))) | |
976 | (t (cons 'text text-start)))))) | |
1c1d2eb6 | 977 | |
1caf38eb RS |
978 | (defun sgml-beginning-of-tag (&optional top-level) |
979 | "Skip to beginning of tag and return its name. | |
1c1d2eb6 SM |
980 | If this can't be done, return nil." |
981 | (let ((context (sgml-lexical-context))) | |
982 | (if (eq (car context) 'tag) | |
983 | (progn | |
984 | (goto-char (cdr context)) | |
985 | (when (looking-at sgml-tag-name-re) | |
986 | (match-string-no-properties 1))) | |
987 | (if top-level nil | |
3fb819e5 | 988 | (when (not (eq (car context) 'text)) |
1c1d2eb6 SM |
989 | (goto-char (cdr context)) |
990 | (sgml-beginning-of-tag t)))))) | |
1caf38eb RS |
991 | |
992 | (defun sgml-value (alist) | |
347ea557 | 993 | "Interactively insert value taken from attribute-rule ALIST. |
5950e029 | 994 | See `sgml-tag-alist' for info about attribute rules." |
1caf38eb RS |
995 | (setq alist (cdr alist)) |
996 | (if (stringp (car alist)) | |
997 | (insert "=\"" (car alist) ?\") | |
a3ec4ba0 | 998 | (if (and (eq (car alist) t) (not sgml-xml-mode)) |
5950e029 | 999 | (when (cdr alist) |
73d25e52 SM |
1000 | (insert "=\"") |
1001 | (setq alist (skeleton-read '(completing-read "Value: " (cdr alist)))) | |
1002 | (if (string< "" alist) | |
1003 | (insert alist ?\") | |
1004 | (delete-backward-char 2))) | |
1caf38eb | 1005 | (insert "=\"") |
5950e029 SS |
1006 | (when alist |
1007 | (insert (skeleton-read '(completing-read "Value: " alist)))) | |
1caf38eb | 1008 | (insert ?\")))) |
64367655 SM |
1009 | |
1010 | (defun sgml-quote (start end &optional unquotep) | |
7492ed8e SM |
1011 | "Quote SGML text in region START ... END. |
1012 | Only &, < and > are quoted, the rest is left untouched. | |
1013 | With prefix argument UNQUOTEP, unquote the region." | |
1014 | (interactive "r\nP") | |
1015 | (save-restriction | |
1016 | (narrow-to-region start end) | |
1017 | (goto-char (point-min)) | |
1018 | (if unquotep | |
1019 | ;; FIXME: We should unquote other named character references as well. | |
1020 | (while (re-search-forward | |
1021 | "\\(&\\(amp\\|\\(l\\|\\(g\\)\\)t\\)\\)[][<>&;\n\t \"%!'(),/=?]" | |
1022 | nil t) | |
1023 | (replace-match (if (match-end 4) ">" (if (match-end 3) "<" "&")) t t | |
1024 | nil (if (eq (char-before (match-end 0)) ?\;) 0 1))) | |
1025 | (while (re-search-forward "[&<>]" nil t) | |
1026 | (replace-match (cdr (assq (char-before) '((?& . "&") | |
1027 | (?< . "<") | |
1028 | (?> . ">")))) | |
1029 | t t))))) | |
1030 | ||
1031 | (defun sgml-pretty-print (beg end) | |
1032 | "Simple-minded pretty printer for SGML. | |
1033 | Re-indents the code and inserts newlines between BEG and END. | |
1034 | You might want to turn on `auto-fill-mode' to get better results." | |
1035 | ;; TODO: | |
1036 | ;; - insert newline between some start-tag and text. | |
1037 | ;; - don't insert newline in front of some end-tags. | |
1038 | (interactive "r") | |
1039 | (save-excursion | |
1040 | (if (< beg end) | |
1041 | (goto-char beg) | |
1042 | (goto-char end) | |
1043 | (setq end beg) | |
1044 | (setq beg (point))) | |
1045 | ;; Don't use narrowing because it screws up auto-indent. | |
1046 | (setq end (copy-marker end t)) | |
1047 | (with-syntax-table sgml-tag-syntax-table | |
1048 | (while (re-search-forward "<" end t) | |
1049 | (goto-char (match-beginning 0)) | |
1050 | (unless (or ;;(looking-at "</") | |
1051 | (progn (skip-chars-backward " \t") (bolp))) | |
1052 | (reindent-then-newline-and-indent)) | |
1053 | (forward-sexp 1))) | |
1054 | ;; (indent-region beg end) | |
1055 | )) | |
e1940c83 | 1056 | |
2394187c SM |
1057 | \f |
1058 | ;; Parsing | |
1059 | ||
1060 | (defstruct (sgml-tag | |
1061 | (:constructor sgml-make-tag (type start end name))) | |
1062 | type start end name) | |
1063 | ||
1064 | (defsubst sgml-parse-tag-name () | |
1065 | "Skip past a tag-name, and return the name." | |
1066 | (buffer-substring-no-properties | |
1067 | (point) (progn (skip-syntax-forward "w_") (point)))) | |
1068 | ||
41bfcbee MW |
1069 | (defsubst sgml-looking-back-at (str) |
1070 | "Return t if the test before point matches STR." | |
1071 | (let ((start (- (point) (length str)))) | |
80fc318e | 1072 | (and (>= start (point-min)) |
41bfcbee | 1073 | (equal str (buffer-substring-no-properties start (point)))))) |
2394187c | 1074 | |
e9146d5a SM |
1075 | (defun sgml-tag-text-p (start end) |
1076 | "Return non-nil if text between START and END is a tag. | |
1077 | Checks among other things that the tag does not contain spurious | |
1078 | unquoted < or > chars inside, which would indicate that it | |
1079 | really isn't a tag after all." | |
1080 | (save-excursion | |
1081 | (with-syntax-table sgml-tag-syntax-table | |
1082 | (let ((pps (parse-partial-sexp start end 2))) | |
1083 | (and (= (nth 0 pps) 0)))))) | |
1084 | ||
4e7a42d2 | 1085 | (defun sgml-parse-tag-backward (&optional limit) |
2394187c SM |
1086 | "Parse an SGML tag backward, and return information about the tag. |
1087 | Assume that parsing starts from within a textual context. | |
1088 | Leave point at the beginning of the tag." | |
e9146d5a SM |
1089 | (catch 'found |
1090 | (let (tag-type tag-start tag-end name) | |
1091 | (or (re-search-backward "[<>]" limit 'move) | |
1092 | (error "No tag found")) | |
1093 | (when (eq (char-after) ?<) | |
1094 | ;; Oops!! Looks like we were not in a textual context after all!. | |
1095 | ;; Let's try to recover. | |
1096 | (with-syntax-table sgml-tag-syntax-table | |
1097 | (let ((pos (point))) | |
1098 | (condition-case nil | |
1099 | (forward-sexp) | |
1100 | (scan-error | |
1101 | ;; This < seems to be just a spurious one, let's ignore it. | |
1102 | (goto-char pos) | |
1103 | (throw 'found (sgml-parse-tag-backward limit)))) | |
1104 | ;; Check it is really a tag, without any extra < or > inside. | |
1105 | (unless (sgml-tag-text-p pos (point)) | |
1106 | (goto-char pos) | |
1107 | (throw 'found (sgml-parse-tag-backward limit))) | |
1108 | (forward-char -1)))) | |
1109 | (setq tag-end (1+ (point))) | |
1110 | (cond | |
1111 | ((sgml-looking-back-at "--") ; comment | |
1112 | (setq tag-type 'comment | |
1113 | tag-start (search-backward "<!--" nil t))) | |
1114 | ((sgml-looking-back-at "]]") ; cdata | |
1115 | (setq tag-type 'cdata | |
1116 | tag-start (re-search-backward "<!\\[[A-Z]+\\[" nil t))) | |
1117 | (t | |
1118 | (setq tag-start | |
1119 | (with-syntax-table sgml-tag-syntax-table | |
1120 | (goto-char tag-end) | |
1121 | (condition-case nil | |
1122 | (backward-sexp) | |
1123 | (scan-error | |
1124 | ;; This > isn't really the end of a tag. Skip it. | |
1125 | (goto-char (1- tag-end)) | |
1126 | (throw 'found (sgml-parse-tag-backward limit)))) | |
1127 | (point))) | |
1128 | (goto-char (1+ tag-start)) | |
1129 | (case (char-after) | |
1130 | (?! ; declaration | |
1131 | (setq tag-type 'decl)) | |
1132 | (?? ; processing-instruction | |
1133 | (setq tag-type 'pi)) | |
1134 | (?/ ; close-tag | |
1135 | (forward-char 1) | |
1136 | (setq tag-type 'close | |
1137 | name (sgml-parse-tag-name))) | |
1138 | (?% ; JSP tags | |
1139 | (setq tag-type 'jsp)) | |
1140 | (t ; open or empty tag | |
1141 | (setq tag-type 'open | |
1142 | name (sgml-parse-tag-name)) | |
1143 | (if (or (eq ?/ (char-before (- tag-end 1))) | |
1144 | (sgml-empty-tag-p name)) | |
1145 | (setq tag-type 'empty)))))) | |
1146 | (goto-char tag-start) | |
1147 | (sgml-make-tag tag-type tag-start tag-end name)))) | |
2394187c | 1148 | |
59444a9c | 1149 | (defun sgml-get-context (&optional until) |
2394187c | 1150 | "Determine the context of the current position. |
59444a9c SM |
1151 | By default, parse until we find a start-tag as the first thing on a line. |
1152 | If UNTIL is `empty', return even if the context is empty (i.e. | |
2394187c | 1153 | we just skipped over some element and got to a beginning of line). |
2394187c SM |
1154 | |
1155 | The context is a list of tag-info structures. The last one is the tag | |
59444a9c SM |
1156 | immediately enclosing the current position. |
1157 | ||
1158 | Point is assumed to be outside of any tag. If we discover that it's | |
1159 | not the case, the first tag returned is the one inside which we are." | |
2394187c | 1160 | (let ((here (point)) |
ed8031f2 | 1161 | (stack nil) |
2394187c SM |
1162 | (ignore nil) |
1163 | (context nil) | |
1164 | tag-info) | |
1165 | ;; CONTEXT keeps track of the tag-stack | |
ed8031f2 SM |
1166 | ;; STACK keeps track of the end tags we've seen (and thus the start-tags |
1167 | ;; we'll have to ignore) when skipping over matching open..close pairs. | |
1168 | ;; IGNORE is a list of tags that can be ignored because they have been | |
1169 | ;; closed implicitly. | |
2394187c SM |
1170 | (skip-chars-backward " \t\n") ; Make sure we're not at indentation. |
1171 | (while | |
59444a9c | 1172 | (and (not (eq until 'now)) |
ed8031f2 | 1173 | (or stack |
59444a9c | 1174 | (not (if until (eq until 'empty) context)) |
2394187c SM |
1175 | (not (sgml-at-indentation-p)) |
1176 | (and context | |
1177 | (/= (point) (sgml-tag-start (car context))) | |
59444a9c | 1178 | (sgml-unclosed-tag-p (sgml-tag-name (car context))))) |
2394187c | 1179 | (setq tag-info (ignore-errors (sgml-parse-tag-backward)))) |
a9d4efa2 | 1180 | |
2394187c SM |
1181 | ;; This tag may enclose things we thought were tags. If so, |
1182 | ;; discard them. | |
1183 | (while (and context | |
1184 | (> (sgml-tag-end tag-info) | |
1185 | (sgml-tag-end (car context)))) | |
1186 | (setq context (cdr context))) | |
a9d4efa2 | 1187 | |
2394187c | 1188 | (cond |
59444a9c SM |
1189 | ((> (sgml-tag-end tag-info) here) |
1190 | ;; Oops!! Looks like we were not outside of any tag, after all. | |
1191 | (push tag-info context) | |
1192 | (setq until 'now)) | |
2394187c | 1193 | |
2394187c SM |
1194 | ;; start-tag |
1195 | ((eq (sgml-tag-type tag-info) 'open) | |
1196 | (cond | |
ed8031f2 SM |
1197 | ((null stack) |
1198 | (if (member-ignore-case (sgml-tag-name tag-info) ignore) | |
2394187c SM |
1199 | ;; There was an implicit end-tag. |
1200 | nil | |
ed8031f2 SM |
1201 | (push tag-info context) |
1202 | ;; We're changing context so the tags implicitly closed inside | |
1203 | ;; the previous context aren't implicitly closed here any more. | |
1204 | ;; [ Well, actually it depends, but we don't have the info about | |
1205 | ;; when it doesn't and when it does. --Stef ] | |
1206 | (setq ignore nil))) | |
2394187c | 1207 | ((eq t (compare-strings (sgml-tag-name tag-info) nil nil |
ed8031f2 SM |
1208 | (car stack) nil nil t)) |
1209 | (setq stack (cdr stack))) | |
2394187c SM |
1210 | (t |
1211 | ;; The open and close tags don't match. | |
1212 | (if (not sgml-xml-mode) | |
2394187c | 1213 | (unless (sgml-unclosed-tag-p (sgml-tag-name tag-info)) |
7492ed8e | 1214 | (message "Unclosed tag <%s>" (sgml-tag-name tag-info)) |
ed8031f2 | 1215 | (let ((tmp stack)) |
7492ed8e SM |
1216 | ;; We could just assume that the tag is simply not closed |
1217 | ;; but it's a bad assumption when tags *are* closed but | |
1218 | ;; not properly nested. | |
1219 | (while (and (cdr tmp) | |
1220 | (not (eq t (compare-strings | |
1221 | (sgml-tag-name tag-info) nil nil | |
1222 | (cadr tmp) nil nil t)))) | |
1223 | (setq tmp (cdr tmp))) | |
1224 | (if (cdr tmp) (setcdr tmp (cddr tmp))))) | |
2394187c | 1225 | (message "Unmatched tags <%s> and </%s>" |
ed8031f2 | 1226 | (sgml-tag-name tag-info) (pop stack))))) |
bf247b6e | 1227 | |
ed8031f2 SM |
1228 | (if (and (null stack) (sgml-unclosed-tag-p (sgml-tag-name tag-info))) |
1229 | ;; This is a top-level open of an implicitly closed tag, so any | |
1230 | ;; occurrence of such an open tag at the same level can be ignored | |
1231 | ;; because it's been implicitly closed. | |
1232 | (push (sgml-tag-name tag-info) ignore))) | |
2394187c SM |
1233 | |
1234 | ;; end-tag | |
1235 | ((eq (sgml-tag-type tag-info) 'close) | |
1236 | (if (sgml-empty-tag-p (sgml-tag-name tag-info)) | |
1237 | (message "Spurious </%s>: empty tag" (sgml-tag-name tag-info)) | |
ed8031f2 | 1238 | (push (sgml-tag-name tag-info) stack))) |
2394187c SM |
1239 | )) |
1240 | ||
1241 | ;; return context | |
1242 | context)) | |
1243 | ||
1244 | (defun sgml-show-context (&optional full) | |
1245 | "Display the current context. | |
1246 | If FULL is non-nil, parse back to the beginning of the buffer." | |
1247 | (interactive "P") | |
1248 | (with-output-to-temp-buffer "*XML Context*" | |
7492ed8e SM |
1249 | (save-excursion |
1250 | (let ((context (sgml-get-context))) | |
1251 | (when full | |
1252 | (let ((more nil)) | |
1253 | (while (setq more (sgml-get-context)) | |
1254 | (setq context (nconc more context))))) | |
1255 | (pp context))))) | |
2394187c SM |
1256 | |
1257 | \f | |
1258 | ;; Editing shortcuts | |
1259 | ||
f6ab0573 | 1260 | (defun sgml-close-tag () |
4e7a42d2 SM |
1261 | "Close current element. |
1262 | Depending on context, inserts a matching close-tag, or closes | |
1263 | the current start-tag or the current comment or the current cdata, ..." | |
2394187c | 1264 | (interactive) |
f6ab0573 MW |
1265 | (case (car (sgml-lexical-context)) |
1266 | (comment (insert " -->")) | |
1267 | (cdata (insert "]]>")) | |
1268 | (pi (insert " ?>")) | |
1269 | (jsp (insert " %>")) | |
1270 | (tag (insert " />")) | |
1271 | (text | |
1272 | (let ((context (save-excursion (sgml-get-context)))) | |
1273 | (if context | |
2871b07a | 1274 | (progn |
f6ab0573 MW |
1275 | (insert "</" (sgml-tag-name (car (last context))) ">") |
1276 | (indent-according-to-mode))))) | |
1277 | (otherwise | |
1278 | (error "Nothing to close")))) | |
2394187c | 1279 | |
347ea557 MW |
1280 | (defun sgml-empty-tag-p (tag-name) |
1281 | "Return non-nil if TAG-NAME is an implicitly empty tag." | |
1282 | (and (not sgml-xml-mode) | |
1283 | (member-ignore-case tag-name sgml-empty-tags))) | |
1284 | ||
1285 | (defun sgml-unclosed-tag-p (tag-name) | |
1286 | "Return non-nil if TAG-NAME is a tag for which an end-tag is optional." | |
1287 | (and (not sgml-xml-mode) | |
1288 | (member-ignore-case tag-name sgml-unclosed-tags))) | |
1289 | ||
59444a9c SM |
1290 | (defun sgml-calculate-indent (&optional lcon) |
1291 | "Calculate the column to which this line should be indented. | |
1292 | LCON is the lexical context, if any." | |
1293 | (unless lcon (setq lcon (sgml-lexical-context))) | |
1294 | ||
1295 | ;; Indent comment-start markers inside <!-- just like comment-end markers. | |
1296 | (if (and (eq (car lcon) 'tag) | |
1297 | (looking-at "--") | |
1298 | (save-excursion (goto-char (cdr lcon)) (looking-at "<!--"))) | |
1299 | (setq lcon (cons 'comment (+ (cdr lcon) 2)))) | |
1300 | ||
1301 | (case (car lcon) | |
1302 | ||
1303 | (string | |
1304 | ;; Go back to previous non-empty line. | |
1305 | (while (and (> (point) (cdr lcon)) | |
1306 | (zerop (forward-line -1)) | |
1307 | (looking-at "[ \t]*$"))) | |
1308 | (if (> (point) (cdr lcon)) | |
1309 | ;; Previous line is inside the string. | |
1310 | (current-indentation) | |
1311 | (goto-char (cdr lcon)) | |
1312 | (1+ (current-column)))) | |
1313 | ||
1314 | (comment | |
1315 | (let ((mark (looking-at "--"))) | |
1c1d2eb6 SM |
1316 | ;; Go back to previous non-empty line. |
1317 | (while (and (> (point) (cdr lcon)) | |
1318 | (zerop (forward-line -1)) | |
59444a9c SM |
1319 | (or (looking-at "[ \t]*$") |
1320 | (if mark (not (looking-at "[ \t]*--")))))) | |
1c1d2eb6 | 1321 | (if (> (point) (cdr lcon)) |
59444a9c SM |
1322 | ;; Previous line is inside the comment. |
1323 | (skip-chars-forward " \t") | |
1c1d2eb6 | 1324 | (goto-char (cdr lcon)) |
59444a9c SM |
1325 | ;; Skip `<!' to get to the `--' with which we want to align. |
1326 | (search-forward "--") | |
1327 | (goto-char (match-beginning 0))) | |
1328 | (when (and (not mark) (looking-at "--")) | |
1329 | (forward-char 2) (skip-chars-forward " \t")) | |
1330 | (current-column))) | |
1331 | ||
1332 | ;; We don't know how to indent it. Let's be honest about it. | |
1333 | (cdata nil) | |
1334 | ||
1335 | (tag | |
1336 | (goto-char (1+ (cdr lcon))) | |
1337 | (skip-chars-forward "^ \t\n") ;Skip tag name. | |
1338 | (skip-chars-forward " \t") | |
1339 | (if (not (eolp)) | |
1340 | (current-column) | |
1341 | ;; This is the first attribute: indent. | |
1c1d2eb6 | 1342 | (goto-char (1+ (cdr lcon))) |
59444a9c SM |
1343 | (+ (current-column) sgml-basic-offset))) |
1344 | ||
1345 | (text | |
1346 | (while (looking-at "</") | |
1347 | (forward-sexp 1) | |
1348 | (skip-chars-forward " \t")) | |
1349 | (let* ((here (point)) | |
1350 | (unclosed (and ;; (not sgml-xml-mode) | |
1351 | (looking-at sgml-tag-name-re) | |
1352 | (member-ignore-case (match-string 1) | |
1353 | sgml-unclosed-tags) | |
1354 | (match-string 1))) | |
1355 | (context | |
1356 | ;; If possible, align on the previous non-empty text line. | |
1357 | ;; Otherwise, do a more serious parsing to find the | |
1358 | ;; tag(s) relative to which we should be indenting. | |
1359 | (if (and (not unclosed) (skip-chars-backward " \t") | |
1360 | (< (skip-chars-backward " \t\n") 0) | |
1361 | (back-to-indentation) | |
1362 | (> (point) (cdr lcon))) | |
1363 | nil | |
1364 | (goto-char here) | |
1365 | (nreverse (sgml-get-context (if unclosed nil 'empty))))) | |
1366 | (there (point))) | |
1367 | ;; Ignore previous unclosed start-tag in context. | |
1368 | (while (and context unclosed | |
1369 | (eq t (compare-strings | |
1370 | (sgml-tag-name (car context)) nil nil | |
1371 | unclosed nil nil t))) | |
1372 | (setq context (cdr context))) | |
1373 | ;; Indent to reflect nesting. | |
1374 | (cond | |
1375 | ;; If we were not in a text context after all, let's try again. | |
1376 | ((and context (> (sgml-tag-end (car context)) here)) | |
1377 | (goto-char here) | |
1378 | (sgml-calculate-indent | |
1379 | (cons (if (memq (sgml-tag-type (car context)) '(comment cdata)) | |
1380 | (sgml-tag-type (car context)) 'tag) | |
1381 | (sgml-tag-start (car context))))) | |
1382 | ;; Align on the first element after the nearest open-tag, if any. | |
1383 | ((and context | |
1384 | (goto-char (sgml-tag-end (car context))) | |
1385 | (skip-chars-forward " \t\n") | |
1386 | (< (point) here) (sgml-at-indentation-p)) | |
1387 | (current-column)) | |
1388 | (t | |
1389 | (goto-char there) | |
1390 | (+ (current-column) | |
1391 | (* sgml-basic-offset (length context))))))) | |
1392 | ||
1393 | (otherwise | |
98d90904 | 1394 | (error "Unrecognized context %s" (car lcon))) |
59444a9c SM |
1395 | |
1396 | )) | |
1c1d2eb6 SM |
1397 | |
1398 | (defun sgml-indent-line () | |
1399 | "Indent the current line as SGML." | |
1400 | (interactive) | |
1401 | (let* ((savep (point)) | |
1402 | (indent-col | |
1403 | (save-excursion | |
5f3d924d | 1404 | (back-to-indentation) |
1c1d2eb6 | 1405 | (if (>= (point) savep) (setq savep nil)) |
1c1d2eb6 | 1406 | (sgml-calculate-indent)))) |
59444a9c SM |
1407 | (if (null indent-col) |
1408 | 'noindent | |
1409 | (if savep | |
1410 | (save-excursion (indent-line-to indent-col)) | |
1411 | (indent-line-to indent-col))))) | |
1c1d2eb6 | 1412 | |
2871b07a MW |
1413 | (defun sgml-guess-indent () |
1414 | "Guess an appropriate value for `sgml-basic-offset'. | |
1415 | Base the guessed identation level on the first indented tag in the buffer. | |
1416 | Add this to `sgml-mode-hook' for convenience." | |
1417 | (interactive) | |
1418 | (save-excursion | |
1419 | (goto-char (point-min)) | |
232dbe4f | 1420 | (if (re-search-forward "^\\([ \t]+\\)<" 500 'noerror) |
2871b07a MW |
1421 | (progn |
1422 | (set (make-local-variable 'sgml-basic-offset) | |
1c8438ab | 1423 | (1- (current-column))) |
2871b07a MW |
1424 | (message "Guessed sgml-basic-offset = %d" |
1425 | sgml-basic-offset) | |
1426 | )))) | |
1427 | ||
5f3d924d SM |
1428 | (defun sgml-parse-dtd () |
1429 | "Simplistic parse of the current buffer as a DTD. | |
1430 | Currently just returns (EMPTY-TAGS UNCLOSED-TAGS)." | |
1431 | (goto-char (point-min)) | |
1432 | (let ((empty nil) | |
1433 | (unclosed nil)) | |
1434 | (while (re-search-forward "<!ELEMENT[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+[-O][ \t\n]+\\([-O]\\)[ \t\n]+\\([^ \t\n]+\\)" nil t) | |
1435 | (cond | |
1436 | ((string= (match-string 3) "EMPTY") | |
1437 | (push (match-string-no-properties 1) empty)) | |
1438 | ((string= (match-string 2) "O") | |
1439 | (push (match-string-no-properties 1) unclosed)))) | |
1440 | (setq empty (sort (mapcar 'downcase empty) 'string<)) | |
1441 | (setq unclosed (sort (mapcar 'downcase unclosed) 'string<)) | |
1442 | (list empty unclosed))) | |
1443 | ||
e1940c83 SM |
1444 | ;;; HTML mode |
1445 | ||
d4c89075 DL |
1446 | (defcustom html-mode-hook nil |
1447 | "Hook run by command `html-mode'. | |
1448 | `text-mode-hook' and `sgml-mode-hook' are run first." | |
1449 | :group 'sgml | |
1450 | :type 'hook | |
1451 | :options '(html-autoview-mode)) | |
1452 | ||
fcc3195e | 1453 | (defvar html-quick-keys sgml-quick-keys |
b1e7bb48 | 1454 | "Use C-c X combinations for quick insertion of frequent tags when non-nil. |
fcc3195e | 1455 | This defaults to `sgml-quick-keys'. |
1caf38eb RS |
1456 | This takes effect when first loading the library.") |
1457 | ||
1458 | (defvar html-mode-map | |
5f5c9e79 | 1459 | (let ((map (make-sparse-keymap)) |
1caf38eb | 1460 | (menu-map (make-sparse-keymap "HTML"))) |
5f5c9e79 | 1461 | (set-keymap-parent map sgml-mode-map) |
7e49eef2 RS |
1462 | (define-key map "\C-c6" 'html-headline-6) |
1463 | (define-key map "\C-c5" 'html-headline-5) | |
1464 | (define-key map "\C-c4" 'html-headline-4) | |
1465 | (define-key map "\C-c3" 'html-headline-3) | |
1466 | (define-key map "\C-c2" 'html-headline-2) | |
1467 | (define-key map "\C-c1" 'html-headline-1) | |
fcc3195e RS |
1468 | (define-key map "\C-c\r" 'html-paragraph) |
1469 | (define-key map "\C-c\n" 'html-line) | |
1470 | (define-key map "\C-c\C-c-" 'html-horizontal-rule) | |
7e49eef2 RS |
1471 | (define-key map "\C-c\C-co" 'html-ordered-list) |
1472 | (define-key map "\C-c\C-cu" 'html-unordered-list) | |
fcc3195e RS |
1473 | (define-key map "\C-c\C-cr" 'html-radio-buttons) |
1474 | (define-key map "\C-c\C-cc" 'html-checkboxes) | |
1475 | (define-key map "\C-c\C-cl" 'html-list-item) | |
1476 | (define-key map "\C-c\C-ch" 'html-href-anchor) | |
1477 | (define-key map "\C-c\C-cn" 'html-name-anchor) | |
1478 | (define-key map "\C-c\C-ci" 'html-image) | |
5950e029 SS |
1479 | (when html-quick-keys |
1480 | (define-key map "\C-c-" 'html-horizontal-rule) | |
1481 | (define-key map "\C-co" 'html-ordered-list) | |
1482 | (define-key map "\C-cu" 'html-unordered-list) | |
1483 | (define-key map "\C-cr" 'html-radio-buttons) | |
1484 | (define-key map "\C-cc" 'html-checkboxes) | |
1485 | (define-key map "\C-cl" 'html-list-item) | |
1486 | (define-key map "\C-ch" 'html-href-anchor) | |
1487 | (define-key map "\C-cn" 'html-name-anchor) | |
1488 | (define-key map "\C-ci" 'html-image)) | |
1caf38eb RS |
1489 | (define-key map "\C-c\C-s" 'html-autoview-mode) |
1490 | (define-key map "\C-c\C-v" 'browse-url-of-buffer) | |
1491 | (define-key map [menu-bar html] (cons "HTML" menu-map)) | |
1492 | (define-key menu-map [html-autoview-mode] | |
1493 | '("Toggle Autoviewing" . html-autoview-mode)) | |
1494 | (define-key menu-map [browse-url-of-buffer] | |
1495 | '("View Buffer Contents" . browse-url-of-buffer)) | |
1496 | (define-key menu-map [nil] '("--")) | |
7e49eef2 RS |
1497 | ;;(define-key menu-map "6" '("Heading 6" . html-headline-6)) |
1498 | ;;(define-key menu-map "5" '("Heading 5" . html-headline-5)) | |
1499 | ;;(define-key menu-map "4" '("Heading 4" . html-headline-4)) | |
1500 | (define-key menu-map "3" '("Heading 3" . html-headline-3)) | |
1501 | (define-key menu-map "2" '("Heading 2" . html-headline-2)) | |
1502 | (define-key menu-map "1" '("Heading 1" . html-headline-1)) | |
1caf38eb | 1503 | (define-key menu-map "l" '("Radio Buttons" . html-radio-buttons)) |
fcc3195e | 1504 | (define-key menu-map "c" '("Checkboxes" . html-checkboxes)) |
1caf38eb | 1505 | (define-key menu-map "l" '("List Item" . html-list-item)) |
7e49eef2 RS |
1506 | (define-key menu-map "u" '("Unordered List" . html-unordered-list)) |
1507 | (define-key menu-map "o" '("Ordered List" . html-ordered-list)) | |
fcc3195e | 1508 | (define-key menu-map "-" '("Horizontal Rule" . html-horizontal-rule)) |
1caf38eb RS |
1509 | (define-key menu-map "\n" '("Line Break" . html-line)) |
1510 | (define-key menu-map "\r" '("Paragraph" . html-paragraph)) | |
1511 | (define-key menu-map "i" '("Image" . html-image)) | |
1512 | (define-key menu-map "h" '("Href Anchor" . html-href-anchor)) | |
1513 | (define-key menu-map "n" '("Name Anchor" . html-name-anchor)) | |
1514 | map) | |
1515 | "Keymap for commands for use in HTML mode.") | |
1516 | ||
1caf38eb RS |
1517 | (defvar html-face-tag-alist |
1518 | '((bold . "b") | |
1519 | (italic . "i") | |
1520 | (underline . "u") | |
1521 | (modeline . "rev")) | |
1522 | "Value of `sgml-face-tag-alist' for HTML mode.") | |
1523 | ||
1524 | (defvar html-tag-face-alist | |
1525 | '(("b" . bold) | |
1526 | ("big" . bold) | |
1527 | ("blink" . highlight) | |
1528 | ("cite" . italic) | |
1529 | ("em" . italic) | |
1530 | ("h1" bold underline) | |
1531 | ("h2" bold-italic underline) | |
1532 | ("h3" italic underline) | |
1533 | ("h4" . underline) | |
1534 | ("h5" . underline) | |
1535 | ("h6" . underline) | |
1536 | ("i" . italic) | |
1537 | ("rev" . modeline) | |
1538 | ("s" . underline) | |
1539 | ("small" . default) | |
1540 | ("strong" . bold) | |
1541 | ("title" bold underline) | |
1542 | ("tt" . default) | |
1543 | ("u" . underline) | |
1544 | ("var" . italic)) | |
1545 | "Value of `sgml-tag-face-alist' for HTML mode.") | |
1546 | ||
1caf38eb RS |
1547 | (defvar html-display-text |
1548 | '((img . "[/]") | |
1549 | (hr . "----------") | |
1550 | (li . "o ")) | |
1551 | "Value of `sgml-display-text' for HTML mode.") | |
b4f05c38 | 1552 | |
9d4ce428 | 1553 | \f |
3bf0b727 | 1554 | ;; should code exactly HTML 3 here when that is finished |
1caf38eb | 1555 | (defvar html-tag-alist |
d10447ba | 1556 | (let* ((1-7 '(("1") ("2") ("3") ("4") ("5") ("6") ("7"))) |
e1940c83 | 1557 | (1-9 `(,@1-7 ("8") ("9"))) |
1caf38eb RS |
1558 | (align '(("align" ("left") ("center") ("right")))) |
1559 | (valign '(("top") ("middle") ("bottom") ("baseline"))) | |
1560 | (rel '(("next") ("previous") ("parent") ("subdocument") ("made"))) | |
1561 | (href '("href" ("ftp:") ("file:") ("finger:") ("gopher:") ("http:") | |
1562 | ("mailto:") ("news:") ("rlogin:") ("telnet:") ("tn3270:") | |
fcc3195e | 1563 | ("wais:") ("/cgi-bin/"))) |
1caf38eb RS |
1564 | (name '("name")) |
1565 | (link `(,href | |
1566 | ("rel" ,@rel) | |
1567 | ("rev" ,@rel) | |
1568 | ("title"))) | |
b4f05c38 | 1569 | (list '((nil \n ("List item: " "<li>" str |
a3ec4ba0 | 1570 | (if sgml-xml-mode "</li>") \n)))) |
1caf38eb | 1571 | (cell `(t |
e1940c83 | 1572 | ,@align |
1caf38eb RS |
1573 | ("valign" ,@valign) |
1574 | ("colspan" ,@1-9) | |
1575 | ("rowspan" ,@1-9) | |
1576 | ("nowrap" t)))) | |
1577 | ;; put ,-expressions first, else byte-compile chokes (as of V19.29) | |
1578 | ;; and like this it's more efficient anyway | |
1579 | `(("a" ,name ,@link) | |
1580 | ("base" t ,@href) | |
1581 | ("dir" ,@list) | |
d10447ba | 1582 | ("font" nil "size" ("-1") ("+1") ("-2") ("+2") ,@1-7) |
73d25e52 | 1583 | ("form" (\n _ \n "<input type=\"submit\" value=\"\"" |
af3abed1 | 1584 | (if sgml-xml-mode " />" ">")) |
fcc3195e | 1585 | ("action" ,@(cdr href)) ("method" ("get") ("post"))) |
1caf38eb RS |
1586 | ("h1" ,@align) |
1587 | ("h2" ,@align) | |
1588 | ("h3" ,@align) | |
1589 | ("h4" ,@align) | |
1590 | ("h5" ,@align) | |
1591 | ("h6" ,@align) | |
1592 | ("hr" t ("size" ,@1-9) ("width") ("noshade" t) ,@align) | |
1593 | ("img" t ("align" ,@valign ("texttop") ("absmiddle") ("absbottom")) | |
1594 | ("src") ("alt") ("width" "1") ("height" "1") | |
1595 | ("border" "1") ("vspace" "1") ("hspace" "1") ("ismap" t)) | |
1596 | ("input" t ("size" ,@1-9) ("maxlength" ,@1-9) ("checked" t) ,name | |
fcc3195e RS |
1597 | ("type" ("text") ("password") ("checkbox") ("radio") |
1598 | ("submit") ("reset")) | |
1caf38eb RS |
1599 | ("value")) |
1600 | ("link" t ,@link) | |
1601 | ("menu" ,@list) | |
d10447ba | 1602 | ("ol" ,@list ("type" ("A") ("a") ("I") ("i") ("1"))) |
1caf38eb RS |
1603 | ("p" t ,@align) |
1604 | ("select" (nil \n | |
1605 | ("Text: " | |
a3ec4ba0 | 1606 | "<option>" str (if sgml-xml-mode "</option>") \n)) |
1caf38eb RS |
1607 | ,name ("size" ,@1-9) ("multiple" t)) |
1608 | ("table" (nil \n | |
1609 | ((completing-read "Cell kind: " '(("td") ("th")) | |
1610 | nil t "t") | |
73d25e52 | 1611 | "<tr><" str ?> _ |
a3ec4ba0 | 1612 | (if sgml-xml-mode (concat "<" str "></tr>")) \n)) |
1caf38eb RS |
1613 | ("border" t ,@1-9) ("width" "10") ("cellpadding")) |
1614 | ("td" ,@cell) | |
1615 | ("textarea" ,name ("rows" ,@1-9) ("cols" ,@1-9)) | |
1616 | ("th" ,@cell) | |
d10447ba | 1617 | ("ul" ,@list ("type" ("disc") ("circle") ("square"))) |
1caf38eb RS |
1618 | |
1619 | ,@sgml-tag-alist | |
1620 | ||
1621 | ("abbrev") | |
1622 | ("acronym") | |
1623 | ("address") | |
1624 | ("array" (nil \n | |
a3ec4ba0 | 1625 | ("Item: " "<item>" str (if sgml-xml-mode "</item>") \n)) |
1caf38eb RS |
1626 | "align") |
1627 | ("au") | |
1628 | ("b") | |
1629 | ("big") | |
1630 | ("blink") | |
1631 | ("blockquote" \n) | |
1632 | ("body" \n ("background" ".gif") ("bgcolor" "#") ("text" "#") | |
1633 | ("link" "#") ("alink" "#") ("vlink" "#")) | |
a3ec4ba0 | 1634 | ("box" (nil _ "<over>" _ (if sgml-xml-mode "</over>"))) |
1caf38eb RS |
1635 | ("br" t ("clear" ("left") ("right"))) |
1636 | ("caption" ("valign" ("top") ("bottom"))) | |
1637 | ("center" \n) | |
1638 | ("cite") | |
1639 | ("code" \n) | |
a3ec4ba0 | 1640 | ("dd" ,(not sgml-xml-mode)) |
1caf38eb RS |
1641 | ("del") |
1642 | ("dfn") | |
e1940c83 | 1643 | ("div") |
1caf38eb RS |
1644 | ("dl" (nil \n |
1645 | ( "Term: " | |
a3ec4ba0 SM |
1646 | "<dt>" str (if sgml-xml-mode "</dt>") |
1647 | "<dd>" _ (if sgml-xml-mode "</dd>") \n))) | |
1648 | ("dt" (t _ (if sgml-xml-mode "</dt>") | |
1649 | "<dd>" (if sgml-xml-mode "</dd>") \n)) | |
1caf38eb | 1650 | ("em") |
d10447ba | 1651 | ;("fn" "id" "fn") ; ??? |
1caf38eb RS |
1652 | ("head" \n) |
1653 | ("html" (\n | |
1654 | "<head>\n" | |
1655 | "<title>" (setq str (read-input "Title: ")) "</title>\n" | |
5e532c5c | 1656 | "</head>\n" |
1caf38eb RS |
1657 | "<body>\n<h1>" str "</h1>\n" _ |
1658 | "\n<address>\n<a href=\"mailto:" | |
be047262 | 1659 | user-mail-address |
5e532c5c RS |
1660 | "\">" (user-full-name) "</a>\n</address>\n" |
1661 | "</body>" | |
1662 | )) | |
1caf38eb RS |
1663 | ("i") |
1664 | ("ins") | |
1665 | ("isindex" t ("action") ("prompt")) | |
1666 | ("kbd") | |
1667 | ("lang") | |
a3ec4ba0 | 1668 | ("li" ,(not sgml-xml-mode)) |
1caf38eb RS |
1669 | ("math" \n) |
1670 | ("nobr") | |
1671 | ("option" t ("value") ("label") ("selected" t)) | |
1672 | ("over" t) | |
1673 | ("person") | |
1674 | ("pre" \n) | |
1675 | ("q") | |
1676 | ("rev") | |
1677 | ("s") | |
1678 | ("samp") | |
1679 | ("small") | |
64367655 SM |
1680 | ("span" nil |
1681 | ("class" | |
1682 | ("builtin") | |
1683 | ("comment") | |
1684 | ("constant") | |
1685 | ("function-name") | |
1686 | ("keyword") | |
1687 | ("string") | |
1688 | ("type") | |
1689 | ("variable-name") | |
1690 | ("warning"))) | |
1caf38eb RS |
1691 | ("strong") |
1692 | ("sub") | |
1693 | ("sup") | |
1694 | ("title") | |
1695 | ("tr" t) | |
1696 | ("tt") | |
1697 | ("u") | |
1698 | ("var") | |
1699 | ("wbr" t))) | |
1700 | "*Value of `sgml-tag-alist' for HTML mode.") | |
1701 | ||
1702 | (defvar html-tag-help | |
1703 | `(,@sgml-tag-help | |
1704 | ("a" . "Anchor of point or link elsewhere") | |
1705 | ("abbrev" . "?") | |
1706 | ("acronym" . "?") | |
1707 | ("address" . "Formatted mail address") | |
1708 | ("array" . "Math array") | |
1709 | ("au" . "?") | |
1710 | ("b" . "Bold face") | |
1711 | ("base" . "Base address for URLs") | |
1712 | ("big" . "Font size") | |
1713 | ("blink" . "Blinking text") | |
1714 | ("blockquote" . "Indented quotation") | |
1715 | ("body" . "Document body") | |
1716 | ("box" . "Math fraction") | |
1717 | ("br" . "Line break") | |
1718 | ("caption" . "Table caption") | |
1719 | ("center" . "Centered text") | |
1720 | ("changed" . "Change bars") | |
1721 | ("cite" . "Citation of a document") | |
1722 | ("code" . "Formatted source code") | |
1723 | ("dd" . "Definition of term") | |
1724 | ("del" . "?") | |
1725 | ("dfn" . "?") | |
1726 | ("dir" . "Directory list (obsolete)") | |
1727 | ("dl" . "Definition list") | |
1728 | ("dt" . "Term to be definined") | |
98d90904 | 1729 | ("em" . "Emphasized") |
1caf38eb RS |
1730 | ("embed" . "Embedded data in foreign format") |
1731 | ("fig" . "Figure") | |
1732 | ("figa" . "Figure anchor") | |
1733 | ("figd" . "Figure description") | |
1734 | ("figt" . "Figure text") | |
d10447ba | 1735 | ;("fn" . "?") ; ??? |
1caf38eb RS |
1736 | ("font" . "Font size") |
1737 | ("form" . "Form with input fields") | |
1738 | ("group" . "Document grouping") | |
1739 | ("h1" . "Most important section headline") | |
1740 | ("h2" . "Important section headline") | |
1741 | ("h3" . "Section headline") | |
1742 | ("h4" . "Minor section headline") | |
1743 | ("h5" . "Unimportant section headline") | |
1744 | ("h6" . "Least important section headline") | |
1745 | ("head" . "Document header") | |
1746 | ("hr" . "Horizontal rule") | |
1747 | ("html" . "HTML Document") | |
1748 | ("i" . "Italic face") | |
1749 | ("img" . "Graphic image") | |
1750 | ("input" . "Form input field") | |
1751 | ("ins" . "?") | |
1752 | ("isindex" . "Input field for index search") | |
1753 | ("kbd" . "Keybard example face") | |
1754 | ("lang" . "Natural language") | |
1755 | ("li" . "List item") | |
1756 | ("link" . "Link relationship") | |
1757 | ("math" . "Math formula") | |
1758 | ("menu" . "Menu list (obsolete)") | |
1759 | ("mh" . "Form mail header") | |
1760 | ("nextid" . "Allocate new id") | |
1761 | ("nobr" . "Text without line break") | |
1762 | ("ol" . "Ordered list") | |
1763 | ("option" . "Selection list item") | |
1764 | ("over" . "Math fraction rule") | |
1765 | ("p" . "Paragraph start") | |
1766 | ("panel" . "Floating panel") | |
1767 | ("person" . "?") | |
1768 | ("pre" . "Preformatted fixed width text") | |
1769 | ("q" . "?") | |
1770 | ("rev" . "Reverse video") | |
1771 | ("s" . "?") | |
1772 | ("samp" . "Sample text") | |
1773 | ("select" . "Selection list") | |
1774 | ("small" . "Font size") | |
1775 | ("sp" . "Nobreak space") | |
1776 | ("strong" . "Standout text") | |
1777 | ("sub" . "Subscript") | |
1778 | ("sup" . "Superscript") | |
1779 | ("table" . "Table with rows and columns") | |
1780 | ("tb" . "Table vertical break") | |
1781 | ("td" . "Table data cell") | |
1782 | ("textarea" . "Form multiline edit area") | |
1783 | ("th" . "Table header cell") | |
1784 | ("title" . "Document title") | |
1785 | ("tr" . "Table row separator") | |
1786 | ("tt" . "Typewriter face") | |
1787 | ("u" . "Underlined text") | |
1788 | ("ul" . "Unordered list") | |
1789 | ("var" . "Math variable face") | |
1790 | ("wbr" . "Enable <br> within <nobr>")) | |
1791 | "*Value of `sgml-tag-help' for HTML mode.") | |
9d4ce428 | 1792 | |
3bf0b727 | 1793 | \f |
1caf38eb | 1794 | ;;;###autoload |
64367655 | 1795 | (define-derived-mode html-mode sgml-mode "HTML" |
1caf38eb | 1796 | "Major mode based on SGML mode for editing HTML documents. |
7be38f7d | 1797 | This allows inserting skeleton constructs used in hypertext documents with |
fcc3195e RS |
1798 | completion. See below for an introduction to HTML. Use |
1799 | \\[browse-url-of-buffer] to see how this comes out. See also `sgml-mode' on | |
1800 | which this is based. | |
1caf38eb | 1801 | |
fcc3195e | 1802 | Do \\[describe-variable] html- SPC and \\[describe-variable] sgml- SPC to see available variables. |
1caf38eb RS |
1803 | |
1804 | To write fairly well formatted pages you only need to know few things. Most | |
1805 | browsers have a function to read the source code of the page being seen, so | |
1806 | you can imitate various tricks. Here's a very short HTML primer which you | |
1807 | can also view with a browser to see what happens: | |
1808 | ||
1809 | <title>A Title Describing Contents</title> should be on every page. Pages can | |
1810 | have <h1>Very Major Headlines</h1> through <h6>Very Minor Headlines</h6> | |
1811 | <hr> Parts can be separated with horizontal rules. | |
1812 | ||
1813 | <p>Paragraphs only need an opening tag. Line breaks and multiple spaces are | |
1814 | ignored unless the text is <pre>preformatted.</pre> Text can be marked as | |
73cba75d | 1815 | <b>bold</b>, <i>italic</i> or <u>underlined</u> using the normal M-o or |
1caf38eb RS |
1816 | Edit/Text Properties/Face commands. |
1817 | ||
1818 | Pages can have <a name=\"SOMENAME\">named points</a> and can link other points | |
1819 | to them with <a href=\"#SOMENAME\">see also somename</a>. In the same way <a | |
1820 | href=\"URL\">see also URL</a> where URL is a filename relative to current | |
f788776c | 1821 | directory, or absolute as in `http://www.cs.indiana.edu/elisp/w3/docs.html'. |
1caf38eb RS |
1822 | |
1823 | Images in many formats can be inlined with <img src=\"URL\">. | |
1824 | ||
f788776c RS |
1825 | If you mainly create your own documents, `sgml-specials' might be |
1826 | interesting. But note that some HTML 2 browsers can't handle `''. | |
1827 | To work around that, do: | |
1828 | (eval-after-load \"sgml-mode\" '(aset sgml-char-names ?' nil)) | |
1caf38eb | 1829 | |
1caf38eb | 1830 | \\{html-mode-map}" |
64367655 SM |
1831 | (set (make-local-variable 'sgml-display-text) html-display-text) |
1832 | (set (make-local-variable 'sgml-tag-face-alist) html-tag-face-alist) | |
1caf38eb RS |
1833 | (make-local-variable 'sgml-tag-alist) |
1834 | (make-local-variable 'sgml-face-tag-alist) | |
1835 | (make-local-variable 'sgml-tag-help) | |
1836 | (make-local-variable 'outline-regexp) | |
1837 | (make-local-variable 'outline-heading-end-regexp) | |
1838 | (make-local-variable 'outline-level) | |
3d6ce9c2 JL |
1839 | (make-local-variable 'sentence-end-base) |
1840 | (setq sentence-end-base "[.?!][]\"'\e$B!I\e$,1r}\e(B)}]*\\(<[^>]*>\\)*" | |
1841 | sgml-tag-alist html-tag-alist | |
1caf38eb RS |
1842 | sgml-face-tag-alist html-face-tag-alist |
1843 | sgml-tag-help html-tag-help | |
1844 | outline-regexp "^.*<[Hh][1-6]\\>" | |
1845 | outline-heading-end-regexp "</[Hh][1-6]>" | |
1846 | outline-level (lambda () | |
0fda8eff | 1847 | (char-before (match-end 0)))) |
3bf0b727 | 1848 | (setq imenu-create-index-function 'html-imenu-index) |
a3ec4ba0 | 1849 | (when sgml-xml-mode (setq mode-name "XHTML")) |
73d25e52 | 1850 | (set (make-local-variable 'sgml-empty-tags) |
5f3d924d SM |
1851 | ;; From HTML-4.01's loose.dtd, parsed with `sgml-parse-dtd', |
1852 | ;; plus manual addition of "wbr". | |
1853 | '("area" "base" "basefont" "br" "col" "frame" "hr" "img" "input" | |
1854 | "isindex" "link" "meta" "param" "wbr")) | |
1855 | (set (make-local-variable 'sgml-unclosed-tags) | |
1856 | ;; From HTML-4.01's loose.dtd, parsed with `sgml-parse-dtd'. | |
1857 | '("body" "colgroup" "dd" "dt" "head" "html" "li" "option" | |
1858 | "p" "tbody" "td" "tfoot" "th" "thead" "tr")) | |
e1940c83 SM |
1859 | ;; It's for the user to decide if it defeats it or not -stef |
1860 | ;; (make-local-variable 'imenu-sort-function) | |
1861 | ;; (setq imenu-sort-function nil) ; sorting the menu defeats the purpose | |
64367655 | 1862 | ) |
9d4ce428 | 1863 | |
3bf0b727 RS |
1864 | (defvar html-imenu-regexp |
1865 | "\\s-*<h\\([1-9]\\)[^\n<>]*>\\(<[^\n<>]*>\\)*\\s-*\\([^\n<>]*\\)" | |
1866 | "*A regular expression matching a head line to be added to the menu. | |
1867 | The first `match-string' should be a number from 1-9. | |
1868 | The second `match-string' matches extra tags and is ignored. | |
1869 | The third `match-string' will be the used in the menu.") | |
1870 | ||
1871 | (defun html-imenu-index () | |
a9d4efa2 | 1872 | "Return a table of contents for an HTML buffer for use with Imenu." |
3bf0b727 RS |
1873 | (let (toc-index) |
1874 | (save-excursion | |
1875 | (goto-char (point-min)) | |
1876 | (while (re-search-forward html-imenu-regexp nil t) | |
1877 | (setq toc-index | |
1878 | (cons (cons (concat (make-string | |
1879 | (* 2 (1- (string-to-number (match-string 1)))) | |
fae1a906 | 1880 | ?\s) |
3bf0b727 | 1881 | (match-string 3)) |
5950e029 | 1882 | (line-beginning-position)) |
3bf0b727 RS |
1883 | toc-index)))) |
1884 | (nreverse toc-index))) | |
1caf38eb | 1885 | |
4e7a42d2 | 1886 | (define-minor-mode html-autoview-mode |
d4c89075 | 1887 | "Toggle automatic viewing via `browse-url-of-buffer' upon saving buffer. |
3bf0b727 RS |
1888 | With positive prefix ARG always turns viewing on, with negative ARG always off. |
1889 | Can be used as a value for `html-mode-hook'." | |
4e7a42d2 | 1890 | nil nil nil |
966cdb22 | 1891 | :group 'sgml |
4e7a42d2 SM |
1892 | (if html-autoview-mode |
1893 | (add-hook 'after-save-hook 'browse-url-of-buffer nil t) | |
1894 | (remove-hook 'after-save-hook 'browse-url-of-buffer t))) | |
9d4ce428 | 1895 | |
3bf0b727 | 1896 | \f |
1caf38eb RS |
1897 | (define-skeleton html-href-anchor |
1898 | "HTML anchor tag with href attribute." | |
a391b179 | 1899 | "URL: " |
af3abed1 | 1900 | ;; '(setq input "http:") |
a391b179 | 1901 | "<a href=\"" str "\">" _ "</a>") |
1caf38eb RS |
1902 | |
1903 | (define-skeleton html-name-anchor | |
1904 | "HTML anchor tag with name attribute." | |
a391b179 | 1905 | "Name: " |
af3abed1 JL |
1906 | "<a name=\"" str "\"" |
1907 | (if sgml-xml-mode (concat " id=\"" str "\"")) | |
1908 | ">" _ "</a>") | |
1caf38eb | 1909 | |
7e49eef2 RS |
1910 | (define-skeleton html-headline-1 |
1911 | "HTML level 1 headline tags." | |
1912 | nil | |
1913 | "<h1>" _ "</h1>") | |
1914 | ||
1915 | (define-skeleton html-headline-2 | |
1916 | "HTML level 2 headline tags." | |
1917 | nil | |
1918 | "<h2>" _ "</h2>") | |
1919 | ||
1920 | (define-skeleton html-headline-3 | |
1921 | "HTML level 3 headline tags." | |
1922 | nil | |
1923 | "<h3>" _ "</h3>") | |
1924 | ||
1925 | (define-skeleton html-headline-4 | |
1926 | "HTML level 4 headline tags." | |
1927 | nil | |
1928 | "<h4>" _ "</h4>") | |
1929 | ||
1930 | (define-skeleton html-headline-5 | |
1931 | "HTML level 5 headline tags." | |
1932 | nil | |
1933 | "<h5>" _ "</h5>") | |
1934 | ||
1935 | (define-skeleton html-headline-6 | |
1936 | "HTML level 6 headline tags." | |
1937 | nil | |
1938 | "<h6>" _ "</h6>") | |
1caf38eb RS |
1939 | |
1940 | (define-skeleton html-horizontal-rule | |
1941 | "HTML horizontal rule tag." | |
1942 | nil | |
af3abed1 | 1943 | (if sgml-xml-mode "<hr />" "<hr>") \n) |
1caf38eb RS |
1944 | |
1945 | (define-skeleton html-image | |
1946 | "HTML image tag." | |
af3abed1 JL |
1947 | "Image URL: " |
1948 | "<img src=\"" str "\" alt=\"" _ "\"" | |
1949 | (if sgml-xml-mode " />" ">")) | |
1caf38eb RS |
1950 | |
1951 | (define-skeleton html-line | |
1952 | "HTML line break tag." | |
1953 | nil | |
af3abed1 | 1954 | (if sgml-xml-mode "<br />" "<br>") \n) |
1caf38eb | 1955 | |
7e49eef2 RS |
1956 | (define-skeleton html-ordered-list |
1957 | "HTML ordered list tags." | |
1958 | nil | |
a391b179 | 1959 | "<ol>" \n |
a3ec4ba0 | 1960 | "<li>" _ (if sgml-xml-mode "</li>") \n |
7e49eef2 RS |
1961 | "</ol>") |
1962 | ||
1963 | (define-skeleton html-unordered-list | |
1964 | "HTML unordered list tags." | |
1965 | nil | |
a391b179 | 1966 | "<ul>" \n |
a3ec4ba0 | 1967 | "<li>" _ (if sgml-xml-mode "</li>") \n |
7e49eef2 | 1968 | "</ul>") |
1caf38eb RS |
1969 | |
1970 | (define-skeleton html-list-item | |
1971 | "HTML list item tag." | |
1972 | nil | |
1973 | (if (bolp) nil '\n) | |
a3ec4ba0 | 1974 | "<li>" _ (if sgml-xml-mode "</li>")) |
1caf38eb RS |
1975 | |
1976 | (define-skeleton html-paragraph | |
1977 | "HTML paragraph tag." | |
1978 | nil | |
1979 | (if (bolp) nil ?\n) | |
af3abed1 | 1980 | "<p>" _ (if sgml-xml-mode "</p>")) |
1caf38eb | 1981 | |
fcc3195e RS |
1982 | (define-skeleton html-checkboxes |
1983 | "Group of connected checkbox inputs." | |
1984 | nil | |
a391b179 RS |
1985 | '(setq v1 nil |
1986 | v2 nil) | |
1987 | ("Value: " | |
d10447ba | 1988 | "<input type=\"" (identity "checkbox") ; see comment above about identity |
a391b179 | 1989 | "\" name=\"" (or v1 (setq v1 (skeleton-read "Name: "))) |
fcc3195e | 1990 | "\" value=\"" str ?\" |
b4f05c38 | 1991 | (when (y-or-n-p "Set \"checked\" attribute? ") |
aa18ec07 | 1992 | (funcall skeleton-transformation-function |
af3abed1 JL |
1993 | (if sgml-xml-mode " checked=\"checked\"" " checked"))) |
1994 | (if sgml-xml-mode " />" ">") | |
a391b179 RS |
1995 | (skeleton-read "Text: " (capitalize str)) |
1996 | (or v2 (setq v2 (if (y-or-n-p "Newline after text? ") | |
aa18ec07 | 1997 | (funcall skeleton-transformation-function |
af3abed1 | 1998 | (if sgml-xml-mode "<br />" "<br>")) |
a391b179 RS |
1999 | ""))) |
2000 | \n)) | |
fcc3195e | 2001 | |
1caf38eb RS |
2002 | (define-skeleton html-radio-buttons |
2003 | "Group of connected radio button inputs." | |
2004 | nil | |
a391b179 RS |
2005 | '(setq v1 nil |
2006 | v2 (cons nil nil)) | |
2007 | ("Value: " | |
d10447ba | 2008 | "<input type=\"" (identity "radio") ; see comment above about identity |
a391b179 | 2009 | "\" name=\"" (or (car v2) (setcar v2 (skeleton-read "Name: "))) |
1caf38eb | 2010 | "\" value=\"" str ?\" |
b4f05c38 | 2011 | (when (and (not v1) (setq v1 (y-or-n-p "Set \"checked\" attribute? "))) |
aa18ec07 | 2012 | (funcall skeleton-transformation-function |
af3abed1 JL |
2013 | (if sgml-xml-mode " checked=\"checked\"" " checked"))) |
2014 | (if sgml-xml-mode " />" ">") | |
a391b179 RS |
2015 | (skeleton-read "Text: " (capitalize str)) |
2016 | (or (cdr v2) (setcdr v2 (if (y-or-n-p "Newline after text? ") | |
aa18ec07 | 2017 | (funcall skeleton-transformation-function |
af3abed1 | 2018 | (if sgml-xml-mode "<br />" "<br>")) |
a391b179 RS |
2019 | ""))) |
2020 | \n)) | |
1caf38eb | 2021 | |
e1940c83 | 2022 | (provide 'sgml-mode) |
6a05d05f | 2023 | |
e9146d5a | 2024 | ;; arch-tag: 9675da94-b7f9-4bda-ad19-73ed7b4fb401 |
72c0ae01 | 2025 | ;;; sgml-mode.el ends here |