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