(sgml-lexical-context): Fix up CDATA detection for boundary cases.
[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.
2cfd19d4 867TYPE is one of `string', `comment', `tag', `cdata', 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))
14614b6d 875 text-start state)
5f3d924d
SM
876 (if limit (goto-char limit)
877 ;; Hopefully this regexp will match something that's not inside
878 ;; a tag and also hopefully the match is nearby.
879 (re-search-backward "^[ \t]*<[_:[:alpha:]/%!?#]" nil 'move))
880 (with-syntax-table sgml-tag-syntax-table
881 (while (< (point) pos)
882 ;; When entering this loop we're inside text.
80fc318e 883 (setq text-start (point))
5f3d924d 884 (skip-chars-forward "^<" pos)
14614b6d
MW
885 (setq state
886 (cond
887 ((= (point) pos)
888 ;; We got to the end without seeing a tag.
889 nil)
890 ((looking-at "<!\\[[A-Z]+\\[")
891 ;; We've found a CDATA section or similar.
892 (let ((cdata-start (point)))
893 (unless (search-forward "]]>" pos 'move)
894 (list 0 nil nil 'cdata nil nil nil nil cdata-start))))
895 (t
896 ;; We've reached a tag. Parse it.
897 ;; FIXME: Handle net-enabling start-tags
898 (parse-partial-sexp (point) pos 0))))))
899 (cond
900 ((eq (nth 3 state) 'cdata) (cons 'cdata (nth 8 state)))
901 ((nth 3 state) (cons 'string (nth 8 state)))
902 ((nth 4 state) (cons 'comment (nth 8 state)))
903 ((and state (> (nth 0 state) 0)) (cons 'tag (nth 1 state)))
904 (t (cons 'text text-start))))))
1c1d2eb6 905
1caf38eb
RS
906(defun sgml-beginning-of-tag (&optional top-level)
907 "Skip to beginning of tag and return its name.
1c1d2eb6
SM
908If this can't be done, return nil."
909 (let ((context (sgml-lexical-context)))
910 (if (eq (car context) 'tag)
911 (progn
912 (goto-char (cdr context))
913 (when (looking-at sgml-tag-name-re)
914 (match-string-no-properties 1)))
915 (if top-level nil
3fb819e5 916 (when (not (eq (car context) 'text))
1c1d2eb6
SM
917 (goto-char (cdr context))
918 (sgml-beginning-of-tag t))))))
1caf38eb
RS
919
920(defun sgml-value (alist)
347ea557 921 "Interactively insert value taken from attribute-rule ALIST.
5950e029 922See `sgml-tag-alist' for info about attribute rules."
1caf38eb
RS
923 (setq alist (cdr alist))
924 (if (stringp (car alist))
925 (insert "=\"" (car alist) ?\")
a3ec4ba0 926 (if (and (eq (car alist) t) (not sgml-xml-mode))
5950e029 927 (when (cdr alist)
73d25e52
SM
928 (insert "=\"")
929 (setq alist (skeleton-read '(completing-read "Value: " (cdr alist))))
930 (if (string< "" alist)
931 (insert alist ?\")
932 (delete-backward-char 2)))
1caf38eb 933 (insert "=\"")
5950e029
SS
934 (when alist
935 (insert (skeleton-read '(completing-read "Value: " alist))))
1caf38eb 936 (insert ?\"))))
64367655
SM
937
938(defun sgml-quote (start end &optional unquotep)
939 "Quote SGML text in region.
940With prefix argument, unquote the region."
941 (interactive "r\np")
942 (if (< start end)
943 (goto-char start)
944 (goto-char end)
945 (setq end start))
946 (if unquotep
1c1d2eb6 947 (while (re-search-forward "&\\(amp\\|\\(l\\|\\(g\\)\\)t\\)[;\n]" end t)
64367655
SM
948 (replace-match (if (match-end 3) ">" (if (match-end 2) "<" "&"))))
949 (while (re-search-forward "[&<>]" end t)
950 (replace-match (cdr (assq (char-before) '((?& . "&amp;")
951 (?< . "&lt;")
952 (?> . "&gt;"))))))))
1caf38eb 953\f
e1940c83 954
2394187c
SM
955(defsubst sgml-at-indentation-p ()
956 "Return true if point is at the first non-whitespace character on the line."
957 (save-excursion
958 (skip-chars-backward " \t")
959 (bolp)))
960
961\f
962;; Parsing
963
964(defstruct (sgml-tag
965 (:constructor sgml-make-tag (type start end name)))
966 type start end name)
967
968(defsubst sgml-parse-tag-name ()
969 "Skip past a tag-name, and return the name."
970 (buffer-substring-no-properties
971 (point) (progn (skip-syntax-forward "w_") (point))))
972
973(defsubst sgml-looking-back-at (s)
80fc318e
MW
974 (let ((start (- (point) (length s))))
975 (and (>= start (point-min))
976 (equal s (buffer-substring-no-properties start (point))))))
2394187c
SM
977
978(defun sgml-parse-tag-backward ()
979 "Parse an SGML tag backward, and return information about the tag.
980Assume that parsing starts from within a textual context.
981Leave point at the beginning of the tag."
982 (let (tag-type tag-start tag-end name)
983 (search-backward ">")
984 (setq tag-end (1+ (point)))
985 (cond
986 ((sgml-looking-back-at "--") ; comment
987 (setq tag-type 'comment
988 tag-start (search-backward "<!--" nil t)))
989 ((sgml-looking-back-at "]]") ; cdata
2cfd19d4
MW
990 (setq tag-type 'cdata
991 tag-start (re-search-backward "<!\\[[A-Z]+\\[" nil t)))
2394187c
SM
992 (t
993 (setq tag-start
994 (with-syntax-table sgml-tag-syntax-table
995 (goto-char tag-end)
996 (backward-sexp)
997 (point)))
998 (goto-char (1+ tag-start))
999 (case (char-after)
1000 (?! ; declaration
1001 (setq tag-type 'decl))
1002 (?? ; processing-instruction
1003 (setq tag-type 'pi))
1004 (?/ ; close-tag
1005 (forward-char 1)
1006 (setq tag-type 'close
1007 name (sgml-parse-tag-name)))
f6ab0573
MW
1008 (?% ; JSP tags
1009 (setq tag-type 'jsp))
2394187c
SM
1010 (t ; open or empty tag
1011 (setq tag-type 'open
1012 name (sgml-parse-tag-name))
1013 (if (or (eq ?/ (char-before (- tag-end 1)))
1014 (sgml-empty-tag-p name))
1015 (setq tag-type 'empty))))))
1016 (goto-char tag-start)
1017 (sgml-make-tag tag-type tag-start tag-end name)))
1018
2394187c
SM
1019(defun sgml-get-context (&optional full)
1020 "Determine the context of the current position.
1021If FULL is `empty', return even if the context is empty (i.e.
1022we just skipped over some element and got to a beginning of line).
1023If FULL is non-nil, parse back to the beginning of the buffer, otherwise
1024parse until we find a start-tag as the first thing on a line.
1025
1026The context is a list of tag-info structures. The last one is the tag
1027immediately enclosing the current position."
1028 (let ((here (point))
1029 (ignore nil)
1030 (context nil)
1031 tag-info)
1032 ;; CONTEXT keeps track of the tag-stack
1033 ;; IGNORE keeps track of the nesting level of point relative to the
1034 ;; first (outermost) tag on the context. This is the list of
1035 ;; enclosing start-tags we'll have to ignore.
1036 (skip-chars-backward " \t\n") ; Make sure we're not at indentation.
1037 (while
1038 (and (or ignore
1039 (not (if full (eq full 'empty) context))
1040 (not (sgml-at-indentation-p))
1041 (and context
1042 (/= (point) (sgml-tag-start (car context)))
1043 (sgml-unclosed-tag-p (sgml-tag-name (car context)))))
1044 (setq tag-info (ignore-errors (sgml-parse-tag-backward))))
1045
1046 ;; This tag may enclose things we thought were tags. If so,
1047 ;; discard them.
1048 (while (and context
1049 (> (sgml-tag-end tag-info)
1050 (sgml-tag-end (car context))))
1051 (setq context (cdr context)))
1052
1053 (cond
1054
2394187c
SM
1055 ;; start-tag
1056 ((eq (sgml-tag-type tag-info) 'open)
1057 (cond
1058 ((null ignore)
1059 (if (and context
1060 (sgml-unclosed-tag-p (sgml-tag-name tag-info))
1061 (eq t (compare-strings
1062 (sgml-tag-name tag-info) nil nil
1063 (sgml-tag-name (car context)) nil nil t)))
1064 ;; There was an implicit end-tag.
1065 nil
1066 (push tag-info context)))
1067 ((eq t (compare-strings (sgml-tag-name tag-info) nil nil
1068 (car ignore) nil nil t))
1069 (setq ignore (cdr ignore)))
1070 (t
1071 ;; The open and close tags don't match.
1072 (if (not sgml-xml-mode)
1073 ;; Assume the open tag is simply not closed.
1074 (unless (sgml-unclosed-tag-p (sgml-tag-name tag-info))
1075 (message "Unclosed tag <%s>" (sgml-tag-name tag-info)))
1076 (message "Unmatched tags <%s> and </%s>"
1077 (sgml-tag-name tag-info) (pop ignore))))))
1078
1079 ;; end-tag
1080 ((eq (sgml-tag-type tag-info) 'close)
1081 (if (sgml-empty-tag-p (sgml-tag-name tag-info))
1082 (message "Spurious </%s>: empty tag" (sgml-tag-name tag-info))
1083 (push (sgml-tag-name tag-info) ignore)))
1084 ))
1085
1086 ;; return context
1087 context))
1088
1089(defun sgml-show-context (&optional full)
1090 "Display the current context.
1091If FULL is non-nil, parse back to the beginning of the buffer."
1092 (interactive "P")
1093 (with-output-to-temp-buffer "*XML Context*"
1094 (pp (save-excursion (sgml-get-context full)))))
1095
1096\f
1097;; Editing shortcuts
1098
f6ab0573
MW
1099(defun sgml-close-tag ()
1100 "Insert an close-tag for the current element."
2394187c 1101 (interactive)
f6ab0573
MW
1102 (case (car (sgml-lexical-context))
1103 (comment (insert " -->"))
1104 (cdata (insert "]]>"))
1105 (pi (insert " ?>"))
1106 (jsp (insert " %>"))
1107 (tag (insert " />"))
1108 (text
1109 (let ((context (save-excursion (sgml-get-context))))
1110 (if context
1111 (progn
1112 (insert "</" (sgml-tag-name (car (last context))) ">")
1113 (indent-according-to-mode)))))
1114 (otherwise
1115 (error "Nothing to close"))))
2394187c 1116
347ea557
MW
1117(defun sgml-empty-tag-p (tag-name)
1118 "Return non-nil if TAG-NAME is an implicitly empty tag."
1119 (and (not sgml-xml-mode)
1120 (member-ignore-case tag-name sgml-empty-tags)))
1121
1122(defun sgml-unclosed-tag-p (tag-name)
1123 "Return non-nil if TAG-NAME is a tag for which an end-tag is optional."
1124 (and (not sgml-xml-mode)
1125 (member-ignore-case tag-name sgml-unclosed-tags)))
1126
1c1d2eb6
SM
1127(defun sgml-calculate-indent ()
1128 "Calculate the column to which this line should be indented."
1129 (let ((lcon (sgml-lexical-context)))
347ea557 1130
1c1d2eb6
SM
1131 ;; Indent comment-start markers inside <!-- just like comment-end markers.
1132 (if (and (eq (car lcon) 'tag)
1133 (looking-at "--")
1134 (save-excursion (goto-char (cdr lcon)) (looking-at "<!--")))
1135 (setq lcon (cons 'comment (+ (cdr lcon) 2))))
1136
1137 (case (car lcon)
347ea557 1138
1c1d2eb6
SM
1139 (string
1140 ;; Go back to previous non-empty line.
1141 (while (and (> (point) (cdr lcon))
1142 (zerop (forward-line -1))
1143 (looking-at "[ \t]*$")))
1144 (if (> (point) (cdr lcon))
1145 ;; Previous line is inside the string.
1146 (current-indentation)
1147 (goto-char (cdr lcon))
1148 (1+ (current-column))))
1149
1150 (comment
1151 (let ((mark (looking-at "--")))
1152 ;; Go back to previous non-empty line.
1153 (while (and (> (point) (cdr lcon))
1154 (zerop (forward-line -1))
1155 (or (looking-at "[ \t]*$")
1156 (if mark (not (looking-at "[ \t]*--"))))))
1157 (if (> (point) (cdr lcon))
1158 ;; Previous line is inside the comment.
1159 (skip-chars-forward " \t")
1160 (goto-char (cdr lcon)))
1161 (when (and (not mark) (looking-at "--"))
1162 (forward-char 2) (skip-chars-forward " \t"))
1163 (current-column)))
1164
80fc318e
MW
1165 (cdata
1166 (current-column))
1167
1c1d2eb6
SM
1168 (tag
1169 (goto-char (1+ (cdr lcon)))
1170 (skip-chars-forward "^ \t\n") ;Skip tag name.
1171 (skip-chars-forward " \t")
1172 (if (not (eolp))
1173 (current-column)
1174 ;; This is the first attribute: indent.
1175 (goto-char (1+ (cdr lcon)))
1176 (+ (current-column) sgml-basic-offset)))
1177
347ea557 1178 (text
1c1d2eb6
SM
1179 (while (looking-at "</")
1180 (forward-sexp 1)
1181 (skip-chars-forward " \t"))
3fb819e5
SM
1182 (let* ((here (point))
1183 (unclosed (and ;; (not sgml-xml-mode)
1184 (looking-at sgml-tag-name-re)
1185 (member-ignore-case (match-string 1)
1186 sgml-unclosed-tags)
1187 (match-string 1)))
1188 (context
1189 ;; If possible, align on the previous non-empty text line.
1190 ;; Otherwise, do a more serious parsing to find the
1191 ;; tag(s) relative to which we should be indenting.
1192 (if (and (not unclosed) (skip-chars-backward " \t")
1193 (< (skip-chars-backward " \t\n") 0)
1194 (back-to-indentation)
1195 (> (point) (cdr lcon)))
1196 nil
1197 (goto-char here)
2394187c 1198 (nreverse (sgml-get-context (if unclosed nil 'empty)))))
3fb819e5
SM
1199 (there (point)))
1200 ;; Ignore previous unclosed start-tag in context.
1201 (while (and context unclosed
1202 (eq t (compare-strings
2394187c 1203 (sgml-tag-name (car context)) nil nil
3fb819e5
SM
1204 unclosed nil nil t)))
1205 (setq context (cdr context)))
1206 ;; Indent to reflect nesting.
1207 (if (and context
2394187c 1208 (goto-char (sgml-tag-end (car context)))
3fb819e5 1209 (skip-chars-forward " \t\n")
2394187c 1210 (< (point) here) (sgml-at-indentation-p))
3fb819e5
SM
1211 (current-column)
1212 (goto-char there)
1213 (+ (current-column)
347ea557
MW
1214 (* sgml-basic-offset (length context))))))
1215
1216 (otherwise
1217 (error "Unrecognised context %s" (car lcon)))
1218
1219 )))
1c1d2eb6
SM
1220
1221(defun sgml-indent-line ()
1222 "Indent the current line as SGML."
1223 (interactive)
1224 (let* ((savep (point))
1225 (indent-col
1226 (save-excursion
5f3d924d 1227 (back-to-indentation)
1c1d2eb6 1228 (if (>= (point) savep) (setq savep nil))
1c1d2eb6
SM
1229 (sgml-calculate-indent))))
1230 (if savep
1231 (save-excursion (indent-line-to indent-col))
1232 (indent-line-to indent-col))))
1233
5f3d924d
SM
1234(defun sgml-parse-dtd ()
1235 "Simplistic parse of the current buffer as a DTD.
1236Currently just returns (EMPTY-TAGS UNCLOSED-TAGS)."
1237 (goto-char (point-min))
1238 (let ((empty nil)
1239 (unclosed nil))
1240 (while (re-search-forward "<!ELEMENT[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+[-O][ \t\n]+\\([-O]\\)[ \t\n]+\\([^ \t\n]+\\)" nil t)
1241 (cond
1242 ((string= (match-string 3) "EMPTY")
1243 (push (match-string-no-properties 1) empty))
1244 ((string= (match-string 2) "O")
1245 (push (match-string-no-properties 1) unclosed))))
1246 (setq empty (sort (mapcar 'downcase empty) 'string<))
1247 (setq unclosed (sort (mapcar 'downcase unclosed) 'string<))
1248 (list empty unclosed)))
1249
e1940c83
SM
1250;;; HTML mode
1251
d4c89075
DL
1252(defcustom html-mode-hook nil
1253 "Hook run by command `html-mode'.
1254`text-mode-hook' and `sgml-mode-hook' are run first."
1255 :group 'sgml
1256 :type 'hook
1257 :options '(html-autoview-mode))
1258
fcc3195e 1259(defvar html-quick-keys sgml-quick-keys
b1e7bb48 1260 "Use C-c X combinations for quick insertion of frequent tags when non-nil.
fcc3195e 1261This defaults to `sgml-quick-keys'.
1caf38eb
RS
1262This takes effect when first loading the library.")
1263
1264(defvar html-mode-map
5f5c9e79 1265 (let ((map (make-sparse-keymap))
1caf38eb 1266 (menu-map (make-sparse-keymap "HTML")))
5f5c9e79 1267 (set-keymap-parent map sgml-mode-map)
7e49eef2
RS
1268 (define-key map "\C-c6" 'html-headline-6)
1269 (define-key map "\C-c5" 'html-headline-5)
1270 (define-key map "\C-c4" 'html-headline-4)
1271 (define-key map "\C-c3" 'html-headline-3)
1272 (define-key map "\C-c2" 'html-headline-2)
1273 (define-key map "\C-c1" 'html-headline-1)
fcc3195e
RS
1274 (define-key map "\C-c\r" 'html-paragraph)
1275 (define-key map "\C-c\n" 'html-line)
1276 (define-key map "\C-c\C-c-" 'html-horizontal-rule)
7e49eef2
RS
1277 (define-key map "\C-c\C-co" 'html-ordered-list)
1278 (define-key map "\C-c\C-cu" 'html-unordered-list)
fcc3195e
RS
1279 (define-key map "\C-c\C-cr" 'html-radio-buttons)
1280 (define-key map "\C-c\C-cc" 'html-checkboxes)
1281 (define-key map "\C-c\C-cl" 'html-list-item)
1282 (define-key map "\C-c\C-ch" 'html-href-anchor)
1283 (define-key map "\C-c\C-cn" 'html-name-anchor)
1284 (define-key map "\C-c\C-ci" 'html-image)
5950e029
SS
1285 (when html-quick-keys
1286 (define-key map "\C-c-" 'html-horizontal-rule)
1287 (define-key map "\C-co" 'html-ordered-list)
1288 (define-key map "\C-cu" 'html-unordered-list)
1289 (define-key map "\C-cr" 'html-radio-buttons)
1290 (define-key map "\C-cc" 'html-checkboxes)
1291 (define-key map "\C-cl" 'html-list-item)
1292 (define-key map "\C-ch" 'html-href-anchor)
1293 (define-key map "\C-cn" 'html-name-anchor)
1294 (define-key map "\C-ci" 'html-image))
1caf38eb
RS
1295 (define-key map "\C-c\C-s" 'html-autoview-mode)
1296 (define-key map "\C-c\C-v" 'browse-url-of-buffer)
1297 (define-key map [menu-bar html] (cons "HTML" menu-map))
1298 (define-key menu-map [html-autoview-mode]
1299 '("Toggle Autoviewing" . html-autoview-mode))
1300 (define-key menu-map [browse-url-of-buffer]
1301 '("View Buffer Contents" . browse-url-of-buffer))
1302 (define-key menu-map [nil] '("--"))
7e49eef2
RS
1303 ;;(define-key menu-map "6" '("Heading 6" . html-headline-6))
1304 ;;(define-key menu-map "5" '("Heading 5" . html-headline-5))
1305 ;;(define-key menu-map "4" '("Heading 4" . html-headline-4))
1306 (define-key menu-map "3" '("Heading 3" . html-headline-3))
1307 (define-key menu-map "2" '("Heading 2" . html-headline-2))
1308 (define-key menu-map "1" '("Heading 1" . html-headline-1))
1caf38eb 1309 (define-key menu-map "l" '("Radio Buttons" . html-radio-buttons))
fcc3195e 1310 (define-key menu-map "c" '("Checkboxes" . html-checkboxes))
1caf38eb 1311 (define-key menu-map "l" '("List Item" . html-list-item))
7e49eef2
RS
1312 (define-key menu-map "u" '("Unordered List" . html-unordered-list))
1313 (define-key menu-map "o" '("Ordered List" . html-ordered-list))
fcc3195e 1314 (define-key menu-map "-" '("Horizontal Rule" . html-horizontal-rule))
1caf38eb
RS
1315 (define-key menu-map "\n" '("Line Break" . html-line))
1316 (define-key menu-map "\r" '("Paragraph" . html-paragraph))
1317 (define-key menu-map "i" '("Image" . html-image))
1318 (define-key menu-map "h" '("Href Anchor" . html-href-anchor))
1319 (define-key menu-map "n" '("Name Anchor" . html-name-anchor))
1320 map)
1321 "Keymap for commands for use in HTML mode.")
1322
1323
1324(defvar html-face-tag-alist
1325 '((bold . "b")
1326 (italic . "i")
1327 (underline . "u")
1328 (modeline . "rev"))
1329 "Value of `sgml-face-tag-alist' for HTML mode.")
1330
1331(defvar html-tag-face-alist
1332 '(("b" . bold)
1333 ("big" . bold)
1334 ("blink" . highlight)
1335 ("cite" . italic)
1336 ("em" . italic)
1337 ("h1" bold underline)
1338 ("h2" bold-italic underline)
1339 ("h3" italic underline)
1340 ("h4" . underline)
1341 ("h5" . underline)
1342 ("h6" . underline)
1343 ("i" . italic)
1344 ("rev" . modeline)
1345 ("s" . underline)
1346 ("small" . default)
1347 ("strong" . bold)
1348 ("title" bold underline)
1349 ("tt" . default)
1350 ("u" . underline)
1351 ("var" . italic))
1352 "Value of `sgml-tag-face-alist' for HTML mode.")
1353
1354
1355(defvar html-display-text
1356 '((img . "[/]")
1357 (hr . "----------")
1358 (li . "o "))
1359 "Value of `sgml-display-text' for HTML mode.")
3bf0b727 1360\f
b4f05c38 1361
3bf0b727 1362;; should code exactly HTML 3 here when that is finished
1caf38eb 1363(defvar html-tag-alist
d10447ba 1364 (let* ((1-7 '(("1") ("2") ("3") ("4") ("5") ("6") ("7")))
e1940c83 1365 (1-9 `(,@1-7 ("8") ("9")))
1caf38eb
RS
1366 (align '(("align" ("left") ("center") ("right"))))
1367 (valign '(("top") ("middle") ("bottom") ("baseline")))
1368 (rel '(("next") ("previous") ("parent") ("subdocument") ("made")))
1369 (href '("href" ("ftp:") ("file:") ("finger:") ("gopher:") ("http:")
1370 ("mailto:") ("news:") ("rlogin:") ("telnet:") ("tn3270:")
fcc3195e 1371 ("wais:") ("/cgi-bin/")))
1caf38eb
RS
1372 (name '("name"))
1373 (link `(,href
1374 ("rel" ,@rel)
1375 ("rev" ,@rel)
1376 ("title")))
b4f05c38 1377 (list '((nil \n ("List item: " "<li>" str
a3ec4ba0 1378 (if sgml-xml-mode "</li>") \n))))
1caf38eb 1379 (cell `(t
e1940c83 1380 ,@align
1caf38eb
RS
1381 ("valign" ,@valign)
1382 ("colspan" ,@1-9)
1383 ("rowspan" ,@1-9)
1384 ("nowrap" t))))
1385 ;; put ,-expressions first, else byte-compile chokes (as of V19.29)
1386 ;; and like this it's more efficient anyway
1387 `(("a" ,name ,@link)
1388 ("base" t ,@href)
1389 ("dir" ,@list)
d10447ba 1390 ("font" nil "size" ("-1") ("+1") ("-2") ("+2") ,@1-7)
73d25e52 1391 ("form" (\n _ \n "<input type=\"submit\" value=\"\""
a3ec4ba0 1392 (if sgml-xml-mode "/>" ">"))
fcc3195e 1393 ("action" ,@(cdr href)) ("method" ("get") ("post")))
1caf38eb
RS
1394 ("h1" ,@align)
1395 ("h2" ,@align)
1396 ("h3" ,@align)
1397 ("h4" ,@align)
1398 ("h5" ,@align)
1399 ("h6" ,@align)
1400 ("hr" t ("size" ,@1-9) ("width") ("noshade" t) ,@align)
1401 ("img" t ("align" ,@valign ("texttop") ("absmiddle") ("absbottom"))
1402 ("src") ("alt") ("width" "1") ("height" "1")
1403 ("border" "1") ("vspace" "1") ("hspace" "1") ("ismap" t))
1404 ("input" t ("size" ,@1-9) ("maxlength" ,@1-9) ("checked" t) ,name
fcc3195e
RS
1405 ("type" ("text") ("password") ("checkbox") ("radio")
1406 ("submit") ("reset"))
1caf38eb
RS
1407 ("value"))
1408 ("link" t ,@link)
1409 ("menu" ,@list)
d10447ba 1410 ("ol" ,@list ("type" ("A") ("a") ("I") ("i") ("1")))
1caf38eb
RS
1411 ("p" t ,@align)
1412 ("select" (nil \n
1413 ("Text: "
a3ec4ba0 1414 "<option>" str (if sgml-xml-mode "</option>") \n))
1caf38eb
RS
1415 ,name ("size" ,@1-9) ("multiple" t))
1416 ("table" (nil \n
1417 ((completing-read "Cell kind: " '(("td") ("th"))
1418 nil t "t")
73d25e52 1419 "<tr><" str ?> _
a3ec4ba0 1420 (if sgml-xml-mode (concat "<" str "></tr>")) \n))
1caf38eb
RS
1421 ("border" t ,@1-9) ("width" "10") ("cellpadding"))
1422 ("td" ,@cell)
1423 ("textarea" ,name ("rows" ,@1-9) ("cols" ,@1-9))
1424 ("th" ,@cell)
d10447ba 1425 ("ul" ,@list ("type" ("disc") ("circle") ("square")))
1caf38eb
RS
1426
1427 ,@sgml-tag-alist
1428
1429 ("abbrev")
1430 ("acronym")
1431 ("address")
1432 ("array" (nil \n
a3ec4ba0 1433 ("Item: " "<item>" str (if sgml-xml-mode "</item>") \n))
1caf38eb
RS
1434 "align")
1435 ("au")
1436 ("b")
1437 ("big")
1438 ("blink")
1439 ("blockquote" \n)
1440 ("body" \n ("background" ".gif") ("bgcolor" "#") ("text" "#")
1441 ("link" "#") ("alink" "#") ("vlink" "#"))
a3ec4ba0 1442 ("box" (nil _ "<over>" _ (if sgml-xml-mode "</over>")))
1caf38eb
RS
1443 ("br" t ("clear" ("left") ("right")))
1444 ("caption" ("valign" ("top") ("bottom")))
1445 ("center" \n)
1446 ("cite")
1447 ("code" \n)
a3ec4ba0 1448 ("dd" ,(not sgml-xml-mode))
1caf38eb
RS
1449 ("del")
1450 ("dfn")
e1940c83 1451 ("div")
1caf38eb
RS
1452 ("dl" (nil \n
1453 ( "Term: "
a3ec4ba0
SM
1454 "<dt>" str (if sgml-xml-mode "</dt>")
1455 "<dd>" _ (if sgml-xml-mode "</dd>") \n)))
1456 ("dt" (t _ (if sgml-xml-mode "</dt>")
1457 "<dd>" (if sgml-xml-mode "</dd>") \n))
1caf38eb 1458 ("em")
d10447ba 1459 ;("fn" "id" "fn") ; ???
1caf38eb
RS
1460 ("head" \n)
1461 ("html" (\n
1462 "<head>\n"
1463 "<title>" (setq str (read-input "Title: ")) "</title>\n"
5e532c5c 1464 "</head>\n"
1caf38eb
RS
1465 "<body>\n<h1>" str "</h1>\n" _
1466 "\n<address>\n<a href=\"mailto:"
be047262 1467 user-mail-address
5e532c5c
RS
1468 "\">" (user-full-name) "</a>\n</address>\n"
1469 "</body>"
1470 ))
1caf38eb
RS
1471 ("i")
1472 ("ins")
1473 ("isindex" t ("action") ("prompt"))
1474 ("kbd")
1475 ("lang")
a3ec4ba0 1476 ("li" ,(not sgml-xml-mode))
1caf38eb
RS
1477 ("math" \n)
1478 ("nobr")
1479 ("option" t ("value") ("label") ("selected" t))
1480 ("over" t)
1481 ("person")
1482 ("pre" \n)
1483 ("q")
1484 ("rev")
1485 ("s")
1486 ("samp")
1487 ("small")
64367655
SM
1488 ("span" nil
1489 ("class"
1490 ("builtin")
1491 ("comment")
1492 ("constant")
1493 ("function-name")
1494 ("keyword")
1495 ("string")
1496 ("type")
1497 ("variable-name")
1498 ("warning")))
1caf38eb
RS
1499 ("strong")
1500 ("sub")
1501 ("sup")
1502 ("title")
1503 ("tr" t)
1504 ("tt")
1505 ("u")
1506 ("var")
1507 ("wbr" t)))
1508 "*Value of `sgml-tag-alist' for HTML mode.")
1509
1510(defvar html-tag-help
1511 `(,@sgml-tag-help
1512 ("a" . "Anchor of point or link elsewhere")
1513 ("abbrev" . "?")
1514 ("acronym" . "?")
1515 ("address" . "Formatted mail address")
1516 ("array" . "Math array")
1517 ("au" . "?")
1518 ("b" . "Bold face")
1519 ("base" . "Base address for URLs")
1520 ("big" . "Font size")
1521 ("blink" . "Blinking text")
1522 ("blockquote" . "Indented quotation")
1523 ("body" . "Document body")
1524 ("box" . "Math fraction")
1525 ("br" . "Line break")
1526 ("caption" . "Table caption")
1527 ("center" . "Centered text")
1528 ("changed" . "Change bars")
1529 ("cite" . "Citation of a document")
1530 ("code" . "Formatted source code")
1531 ("dd" . "Definition of term")
1532 ("del" . "?")
1533 ("dfn" . "?")
1534 ("dir" . "Directory list (obsolete)")
1535 ("dl" . "Definition list")
1536 ("dt" . "Term to be definined")
b4f05c38 1537 ("em" . "Emphasised")
1caf38eb
RS
1538 ("embed" . "Embedded data in foreign format")
1539 ("fig" . "Figure")
1540 ("figa" . "Figure anchor")
1541 ("figd" . "Figure description")
1542 ("figt" . "Figure text")
d10447ba 1543 ;("fn" . "?") ; ???
1caf38eb
RS
1544 ("font" . "Font size")
1545 ("form" . "Form with input fields")
1546 ("group" . "Document grouping")
1547 ("h1" . "Most important section headline")
1548 ("h2" . "Important section headline")
1549 ("h3" . "Section headline")
1550 ("h4" . "Minor section headline")
1551 ("h5" . "Unimportant section headline")
1552 ("h6" . "Least important section headline")
1553 ("head" . "Document header")
1554 ("hr" . "Horizontal rule")
1555 ("html" . "HTML Document")
1556 ("i" . "Italic face")
1557 ("img" . "Graphic image")
1558 ("input" . "Form input field")
1559 ("ins" . "?")
1560 ("isindex" . "Input field for index search")
1561 ("kbd" . "Keybard example face")
1562 ("lang" . "Natural language")
1563 ("li" . "List item")
1564 ("link" . "Link relationship")
1565 ("math" . "Math formula")
1566 ("menu" . "Menu list (obsolete)")
1567 ("mh" . "Form mail header")
1568 ("nextid" . "Allocate new id")
1569 ("nobr" . "Text without line break")
1570 ("ol" . "Ordered list")
1571 ("option" . "Selection list item")
1572 ("over" . "Math fraction rule")
1573 ("p" . "Paragraph start")
1574 ("panel" . "Floating panel")
1575 ("person" . "?")
1576 ("pre" . "Preformatted fixed width text")
1577 ("q" . "?")
1578 ("rev" . "Reverse video")
1579 ("s" . "?")
1580 ("samp" . "Sample text")
1581 ("select" . "Selection list")
1582 ("small" . "Font size")
1583 ("sp" . "Nobreak space")
1584 ("strong" . "Standout text")
1585 ("sub" . "Subscript")
1586 ("sup" . "Superscript")
1587 ("table" . "Table with rows and columns")
1588 ("tb" . "Table vertical break")
1589 ("td" . "Table data cell")
1590 ("textarea" . "Form multiline edit area")
1591 ("th" . "Table header cell")
1592 ("title" . "Document title")
1593 ("tr" . "Table row separator")
1594 ("tt" . "Typewriter face")
1595 ("u" . "Underlined text")
1596 ("ul" . "Unordered list")
1597 ("var" . "Math variable face")
1598 ("wbr" . "Enable <br> within <nobr>"))
1599"*Value of `sgml-tag-help' for HTML mode.")
3bf0b727 1600\f
1caf38eb 1601;;;###autoload
64367655 1602(define-derived-mode html-mode sgml-mode "HTML"
1caf38eb 1603 "Major mode based on SGML mode for editing HTML documents.
7be38f7d 1604This allows inserting skeleton constructs used in hypertext documents with
fcc3195e
RS
1605completion. See below for an introduction to HTML. Use
1606\\[browse-url-of-buffer] to see how this comes out. See also `sgml-mode' on
1607which this is based.
1caf38eb 1608
fcc3195e 1609Do \\[describe-variable] html- SPC and \\[describe-variable] sgml- SPC to see available variables.
1caf38eb
RS
1610
1611To write fairly well formatted pages you only need to know few things. Most
1612browsers have a function to read the source code of the page being seen, so
1613you can imitate various tricks. Here's a very short HTML primer which you
1614can also view with a browser to see what happens:
1615
1616<title>A Title Describing Contents</title> should be on every page. Pages can
1617have <h1>Very Major Headlines</h1> through <h6>Very Minor Headlines</h6>
1618<hr> Parts can be separated with horizontal rules.
1619
1620<p>Paragraphs only need an opening tag. Line breaks and multiple spaces are
1621ignored unless the text is <pre>preformatted.</pre> Text can be marked as
1622<b>bold</b>, <i>italic</i> or <u>underlined</u> using the normal M-g or
1623Edit/Text Properties/Face commands.
1624
1625Pages can have <a name=\"SOMENAME\">named points</a> and can link other points
1626to them with <a href=\"#SOMENAME\">see also somename</a>. In the same way <a
1627href=\"URL\">see also URL</a> where URL is a filename relative to current
f788776c 1628directory, or absolute as in `http://www.cs.indiana.edu/elisp/w3/docs.html'.
1caf38eb
RS
1629
1630Images in many formats can be inlined with <img src=\"URL\">.
1631
f788776c
RS
1632If you mainly create your own documents, `sgml-specials' might be
1633interesting. But note that some HTML 2 browsers can't handle `&apos;'.
1634To work around that, do:
1635 (eval-after-load \"sgml-mode\" '(aset sgml-char-names ?' nil))
1caf38eb 1636
1caf38eb 1637\\{html-mode-map}"
64367655
SM
1638 (set (make-local-variable 'sgml-display-text) html-display-text)
1639 (set (make-local-variable 'sgml-tag-face-alist) html-tag-face-alist)
1caf38eb
RS
1640 (make-local-variable 'sgml-tag-alist)
1641 (make-local-variable 'sgml-face-tag-alist)
1642 (make-local-variable 'sgml-tag-help)
1643 (make-local-variable 'outline-regexp)
1644 (make-local-variable 'outline-heading-end-regexp)
1645 (make-local-variable 'outline-level)
da84bdc4
RS
1646 (make-local-variable 'sentence-end)
1647 (setq sentence-end
b8b14971
DL
1648 (if sentence-end-double-space
1649 "[.?!][]\"')}]*\\(<[^>]*>\\)*\\($\\| $\\|\t\\| \\)[ \t\n]*"
64367655 1650 "[.?!][]\"')}]*\\(<[^>]*>\\)*\\($\\|[ \t]\\)[ \t\n]*"))
a01588fc 1651 (setq sgml-tag-alist html-tag-alist
1caf38eb
RS
1652 sgml-face-tag-alist html-face-tag-alist
1653 sgml-tag-help html-tag-help
1654 outline-regexp "^.*<[Hh][1-6]\\>"
1655 outline-heading-end-regexp "</[Hh][1-6]>"
1656 outline-level (lambda ()
0fda8eff 1657 (char-before (match-end 0))))
3bf0b727 1658 (setq imenu-create-index-function 'html-imenu-index)
a3ec4ba0 1659 (when sgml-xml-mode (setq mode-name "XHTML"))
73d25e52 1660 (set (make-local-variable 'sgml-empty-tags)
5f3d924d
SM
1661 ;; From HTML-4.01's loose.dtd, parsed with `sgml-parse-dtd',
1662 ;; plus manual addition of "wbr".
1663 '("area" "base" "basefont" "br" "col" "frame" "hr" "img" "input"
1664 "isindex" "link" "meta" "param" "wbr"))
1665 (set (make-local-variable 'sgml-unclosed-tags)
1666 ;; From HTML-4.01's loose.dtd, parsed with `sgml-parse-dtd'.
1667 '("body" "colgroup" "dd" "dt" "head" "html" "li" "option"
1668 "p" "tbody" "td" "tfoot" "th" "thead" "tr"))
e1940c83
SM
1669 ;; It's for the user to decide if it defeats it or not -stef
1670 ;; (make-local-variable 'imenu-sort-function)
1671 ;; (setq imenu-sort-function nil) ; sorting the menu defeats the purpose
64367655 1672 )
3bf0b727
RS
1673\f
1674(defvar html-imenu-regexp
1675 "\\s-*<h\\([1-9]\\)[^\n<>]*>\\(<[^\n<>]*>\\)*\\s-*\\([^\n<>]*\\)"
1676 "*A regular expression matching a head line to be added to the menu.
1677The first `match-string' should be a number from 1-9.
1678The second `match-string' matches extra tags and is ignored.
1679The third `match-string' will be the used in the menu.")
1680
1681(defun html-imenu-index ()
1682 "Return an table of contents for an HTML buffer for use with Imenu."
1683 (let (toc-index)
1684 (save-excursion
1685 (goto-char (point-min))
1686 (while (re-search-forward html-imenu-regexp nil t)
1687 (setq toc-index
1688 (cons (cons (concat (make-string
1689 (* 2 (1- (string-to-number (match-string 1))))
1690 ?\ )
1691 (match-string 3))
5950e029 1692 (line-beginning-position))
3bf0b727
RS
1693 toc-index))))
1694 (nreverse toc-index)))
1caf38eb 1695
3bf0b727 1696(defun html-autoview-mode (&optional arg)
d4c89075 1697 "Toggle automatic viewing via `browse-url-of-buffer' upon saving buffer.
3bf0b727
RS
1698With positive prefix ARG always turns viewing on, with negative ARG always off.
1699Can be used as a value for `html-mode-hook'."
1700 (interactive "P")
1701 (if (setq arg (if arg
1702 (< (prefix-numeric-value arg) 0)
1703 (and (boundp 'after-save-hook)
1704 (memq 'browse-url-of-buffer after-save-hook))))
1705 (setq after-save-hook (delq 'browse-url-of-buffer after-save-hook))
3bf0b727
RS
1706 (add-hook 'after-save-hook 'browse-url-of-buffer nil t))
1707 (message "Autoviewing turned %s."
1708 (if arg "off" "on")))
1709\f
1caf38eb
RS
1710(define-skeleton html-href-anchor
1711 "HTML anchor tag with href attribute."
a391b179
RS
1712 "URL: "
1713 '(setq input "http:")
1714 "<a href=\"" str "\">" _ "</a>")
1caf38eb
RS
1715
1716(define-skeleton html-name-anchor
1717 "HTML anchor tag with name attribute."
a391b179
RS
1718 "Name: "
1719 "<a name=\"" str "\">" _ "</a>")
1caf38eb 1720
7e49eef2
RS
1721(define-skeleton html-headline-1
1722 "HTML level 1 headline tags."
1723 nil
1724 "<h1>" _ "</h1>")
1725
1726(define-skeleton html-headline-2
1727 "HTML level 2 headline tags."
1728 nil
1729 "<h2>" _ "</h2>")
1730
1731(define-skeleton html-headline-3
1732 "HTML level 3 headline tags."
1733 nil
1734 "<h3>" _ "</h3>")
1735
1736(define-skeleton html-headline-4
1737 "HTML level 4 headline tags."
1738 nil
1739 "<h4>" _ "</h4>")
1740
1741(define-skeleton html-headline-5
1742 "HTML level 5 headline tags."
1743 nil
1744 "<h5>" _ "</h5>")
1745
1746(define-skeleton html-headline-6
1747 "HTML level 6 headline tags."
1748 nil
1749 "<h6>" _ "</h6>")
1caf38eb
RS
1750
1751(define-skeleton html-horizontal-rule
1752 "HTML horizontal rule tag."
1753 nil
a3ec4ba0 1754 (if sgml-xml-mode "<hr/>" "<hr>") \n)
1caf38eb
RS
1755
1756(define-skeleton html-image
1757 "HTML image tag."
1758 nil
b4f05c38 1759 "<img src=\"" _ "\""
a3ec4ba0 1760 (if sgml-xml-mode "/>" ">"))
1caf38eb
RS
1761
1762(define-skeleton html-line
1763 "HTML line break tag."
1764 nil
a3ec4ba0 1765 (if sgml-xml-mode "<br/>" "<br>") \n)
1caf38eb 1766
7e49eef2
RS
1767(define-skeleton html-ordered-list
1768 "HTML ordered list tags."
1769 nil
a391b179 1770 "<ol>" \n
a3ec4ba0 1771 "<li>" _ (if sgml-xml-mode "</li>") \n
7e49eef2
RS
1772 "</ol>")
1773
1774(define-skeleton html-unordered-list
1775 "HTML unordered list tags."
1776 nil
a391b179 1777 "<ul>" \n
a3ec4ba0 1778 "<li>" _ (if sgml-xml-mode "</li>") \n
7e49eef2 1779 "</ul>")
1caf38eb
RS
1780
1781(define-skeleton html-list-item
1782 "HTML list item tag."
1783 nil
1784 (if (bolp) nil '\n)
a3ec4ba0 1785 "<li>" _ (if sgml-xml-mode "</li>"))
1caf38eb
RS
1786
1787(define-skeleton html-paragraph
1788 "HTML paragraph tag."
1789 nil
1790 (if (bolp) nil ?\n)
a3ec4ba0 1791 \n "<p>" _ (if sgml-xml-mode "</p>"))
1caf38eb 1792
fcc3195e
RS
1793(define-skeleton html-checkboxes
1794 "Group of connected checkbox inputs."
1795 nil
a391b179
RS
1796 '(setq v1 nil
1797 v2 nil)
1798 ("Value: "
d10447ba 1799 "<input type=\"" (identity "checkbox") ; see comment above about identity
a391b179 1800 "\" name=\"" (or v1 (setq v1 (skeleton-read "Name: ")))
fcc3195e 1801 "\" value=\"" str ?\"
b4f05c38
SS
1802 (when (y-or-n-p "Set \"checked\" attribute? ")
1803 (funcall skeleton-transformation " checked"))
a3ec4ba0 1804 (if sgml-xml-mode "/>" ">")
a391b179
RS
1805 (skeleton-read "Text: " (capitalize str))
1806 (or v2 (setq v2 (if (y-or-n-p "Newline after text? ")
b4f05c38 1807 (funcall skeleton-transformation
a3ec4ba0 1808 (if sgml-xml-mode "<br/>" "<br>"))
a391b179
RS
1809 "")))
1810 \n))
fcc3195e 1811
1caf38eb
RS
1812(define-skeleton html-radio-buttons
1813 "Group of connected radio button inputs."
1814 nil
a391b179
RS
1815 '(setq v1 nil
1816 v2 (cons nil nil))
1817 ("Value: "
d10447ba 1818 "<input type=\"" (identity "radio") ; see comment above about identity
a391b179 1819 "\" name=\"" (or (car v2) (setcar v2 (skeleton-read "Name: ")))
1caf38eb 1820 "\" value=\"" str ?\"
b4f05c38
SS
1821 (when (and (not v1) (setq v1 (y-or-n-p "Set \"checked\" attribute? ")))
1822 (funcall skeleton-transformation " checked"))
a3ec4ba0 1823 (if sgml-xml-mode "/>" ">")
a391b179
RS
1824 (skeleton-read "Text: " (capitalize str))
1825 (or (cdr v2) (setcdr v2 (if (y-or-n-p "Newline after text? ")
b4f05c38 1826 (funcall skeleton-transformation
a3ec4ba0 1827 (if sgml-xml-mode "<br/>" "<br>"))
a391b179
RS
1828 "")))
1829 \n))
1caf38eb 1830
e1940c83 1831(provide 'sgml-mode)
6a05d05f 1832
72c0ae01 1833;;; sgml-mode.el ends here