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