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