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