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