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