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