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