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