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