(auto-revert-check-vc-info): Minor doc fix.
[bpt/emacs.git] / lisp / textmodes / sgml-mode.el
CommitLineData
1caf38eb 1;;; sgml-mode.el --- SGML- and HTML-editing modes
72c0ae01 2
ed8031f2 3;; Copyright (C) 1992, 1995, 1996, 1998, 2001, 2002, 2003, 2004, 2005
e9146d5a 4;; Free Software Foundation, Inc.
6d74b528 5
64ae0c23 6;; Author: James Clark <jjc@jclark.com>
0fda8eff 7;; Maintainer: FSF
3e910376 8;; Adapted-By: ESR, Daniel Pfeiffer <occitan@esperanto.org>,
a391b179 9;; F.Potorti@cnuce.cnr.it
1caf38eb 10;; Keywords: wp, hypermedia, comm, languages
72c0ae01 11
72c0ae01
ER
12;; This file is part of GNU Emacs.
13
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
EN
25;; along with GNU Emacs; see the file COPYING. If not, write to the
26;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27;; Boston, MA 02111-1307, 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
RS
42(defgroup sgml nil
43 "SGML editing mode"
44 :group 'languages)
45
5f3d924d
SM
46(defcustom sgml-basic-offset 2
47 "*Specifies the basic indentation level for `sgml-indent-line'."
48 :type 'integer
49 :group 'sgml)
50
d10447ba 51(defcustom sgml-transformation 'identity
a391b179
RS
52 "*Default value for `skeleton-transformation' (which see) in SGML mode."
53 :type 'function
c60e7b0d 54 :group 'sgml)
a391b179
RS
55
56(put 'sgml-transformation 'variable-interactive
57 "aTransformation function: ")
58
d4c89075
DL
59(defcustom sgml-mode-hook nil
60 "Hook run by command `sgml-mode'.
61`text-mode-hook' is run first."
62 :group 'sgml
63 :type 'hook)
64
1caf38eb
RS
65;; As long as Emacs' syntax can't be complemented with predicates to context
66;; sensitively confirm the syntax of characters, we have to live with this
67;; kludgy kind of tradeoff.
21a6f23c 68(defvar sgml-specials '(?\")
f788776c 69 "List of characters that have a special meaning for SGML mode.
140d71ba 70This list is used when first loading the `sgml-mode' library.
1caf38eb
RS
71The supported characters and potential disadvantages are:
72
73 ?\\\" Makes \" in text start a string.
74 ?' Makes ' in text start a string.
75 ?- Makes -- in text start a comment.
76
4fa91cfe 77When only one of ?\\\" or ?' are included, \"'\" or '\"', as can be found in
1caf38eb 78DTDs, start a string. To partially avoid this problem this also makes these
21a6f23c
RS
79self insert as named entities depending on `sgml-quick-keys'.
80
81Including ?- has the problem of affecting dashes that have nothing to do
82with comments, so we normally turn it off.")
fcc3195e
RS
83
84(defvar sgml-quick-keys nil
2394187c 85 "Use <, >, &, /, SPC and `sgml-specials' keys \"electrically\" when non-nil.
140d71ba 86This takes effect when first loading the `sgml-mode' library.")
1caf38eb 87
1caf38eb 88(defvar sgml-mode-map
e1940c83 89 (let ((map (make-keymap)) ;`sparse' doesn't allow binding to charsets.
1caf38eb 90 (menu-map (make-sparse-keymap "SGML")))
1caf38eb
RS
91 (define-key map "\C-c\C-i" 'sgml-tags-invisible)
92 (define-key map "/" 'sgml-slash)
fcc3195e
RS
93 (define-key map "\C-c\C-n" 'sgml-name-char)
94 (define-key map "\C-c\C-t" 'sgml-tag)
1caf38eb
RS
95 (define-key map "\C-c\C-a" 'sgml-attributes)
96 (define-key map "\C-c\C-b" 'sgml-skip-tag-backward)
97 (define-key map [?\C-c left] 'sgml-skip-tag-backward)
98 (define-key map "\C-c\C-f" 'sgml-skip-tag-forward)
99 (define-key map [?\C-c right] 'sgml-skip-tag-forward)
100 (define-key map "\C-c\C-d" 'sgml-delete-tag)
101 (define-key map "\C-c\^?" 'sgml-delete-tag)
102 (define-key map "\C-c?" 'sgml-tag-help)
f6ab0573 103 (define-key map "\C-c/" 'sgml-close-tag)
1caf38eb
RS
104 (define-key map "\C-c8" 'sgml-name-8bit-mode)
105 (define-key map "\C-c\C-v" 'sgml-validate)
b4f05c38
SS
106 (when sgml-quick-keys
107 (define-key map "&" 'sgml-name-char)
108 (define-key map "<" 'sgml-tag)
109 (define-key map " " 'sgml-auto-attributes)
110 (define-key map ">" 'sgml-maybe-end-tag)
111 (when (memq ?\" sgml-specials)
112 (define-key map "\"" 'sgml-name-self))
113 (when (memq ?' sgml-specials)
114 (define-key map "'" 'sgml-name-self)))
f7ac3e28
SM
115 (define-key map (vector (make-char 'latin-iso8859-1))
116 'sgml-maybe-name-self)
2840d653
EZ
117 (let ((c 127)
118 (map (nth 1 map)))
119 (while (< (setq c (1+ c)) 256)
120 (aset map c 'sgml-maybe-name-self)))
1caf38eb
RS
121 (define-key map [menu-bar sgml] (cons "SGML" menu-map))
122 (define-key menu-map [sgml-validate] '("Validate" . sgml-validate))
123 (define-key menu-map [sgml-name-8bit-mode]
124 '("Toggle 8 Bit Insertion" . sgml-name-8bit-mode))
125 (define-key menu-map [sgml-tags-invisible]
126 '("Toggle Tag Visibility" . sgml-tags-invisible))
127 (define-key menu-map [sgml-tag-help]
128 '("Describe Tag" . sgml-tag-help))
129 (define-key menu-map [sgml-delete-tag]
130 '("Delete Tag" . sgml-delete-tag))
131 (define-key menu-map [sgml-skip-tag-forward]
132 '("Forward Tag" . sgml-skip-tag-forward))
133 (define-key menu-map [sgml-skip-tag-backward]
134 '("Backward Tag" . sgml-skip-tag-backward))
135 (define-key menu-map [sgml-attributes]
136 '("Insert Attributes" . sgml-attributes))
137 (define-key menu-map [sgml-tag] '("Insert Tag" . sgml-tag))
138 map)
139 "Keymap for SGML mode. See also `sgml-specials'.")
140
1c1d2eb6
SM
141(defun sgml-make-syntax-table (specials)
142 (let ((table (make-syntax-table text-mode-syntax-table)))
1caf38eb
RS
143 (modify-syntax-entry ?< "(>" table)
144 (modify-syntax-entry ?> ")<" table)
1c1d2eb6
SM
145 (modify-syntax-entry ?: "_" table)
146 (modify-syntax-entry ?_ "_" table)
147 (modify-syntax-entry ?. "_" table)
148 (if (memq ?- specials)
1caf38eb 149 (modify-syntax-entry ?- "_ 1234" table))
1c1d2eb6 150 (if (memq ?\" specials)
1caf38eb 151 (modify-syntax-entry ?\" "\"\"" table))
1c1d2eb6 152 (if (memq ?' specials)
1caf38eb 153 (modify-syntax-entry ?\' "\"'" table))
1c1d2eb6
SM
154 table))
155
156(defvar sgml-mode-syntax-table (sgml-make-syntax-table sgml-specials)
1caf38eb
RS
157 "Syntax table used in SGML mode. See also `sgml-specials'.")
158
1c1d2eb6
SM
159(defconst sgml-tag-syntax-table
160 (let ((table (sgml-make-syntax-table '(?- ?\" ?\'))))
161 (dolist (char '(?\( ?\) ?\{ ?\} ?\[ ?\] ?$ ?% ?& ?* ?+ ?/))
162 (modify-syntax-entry char "." table))
163 table)
164 "Syntax table used to parse SGML tags.")
165
64ae0c23 166(defcustom sgml-name-8bit-mode nil
2840d653 167 "*When non-nil, insert non-ASCII characters as named entities."
64ae0c23
RS
168 :type 'boolean
169 :group 'sgml)
72c0ae01 170
1caf38eb
RS
171(defvar sgml-char-names
172 [nil nil nil nil nil nil nil nil
173 nil nil nil nil nil nil nil nil
174 nil nil nil nil nil nil nil nil
175 nil nil nil nil nil nil nil nil
a391b179 176 "nbsp" "excl" "quot" "num" "dollar" "percnt" "amp" "apos"
1caf38eb
RS
177 "lpar" "rpar" "ast" "plus" "comma" "hyphen" "period" "sol"
178 nil nil nil nil nil nil nil nil
179 nil nil "colon" "semi" "lt" "eq" "gt" "quest"
180 "commat" nil nil nil nil nil nil nil
181 nil nil nil nil nil nil nil nil
182 nil nil nil nil nil nil nil nil
183 nil nil nil "lsqb" nil "rsqb" "uarr" "lowbar"
184 "lsquo" nil nil nil nil nil nil nil
185 nil nil nil nil nil nil nil nil
186 nil nil nil nil nil nil nil nil
187 nil nil nil "lcub" "verbar" "rcub" "tilde" nil
188 nil nil nil nil nil nil nil nil
189 nil nil nil nil nil nil nil nil
190 nil nil nil nil nil nil nil nil
191 nil nil nil nil nil nil nil nil
192 "nbsp" "iexcl" "cent" "pound" "curren" "yen" "brvbar" "sect"
193 "uml" "copy" "ordf" "laquo" "not" "shy" "reg" "macr"
194 "ring" "plusmn" "sup2" "sup3" "acute" "micro" "para" "middot"
e79ad8a1 195 "cedil" "sup1" "ordm" "raquo" "frac14" "frac12" "frac34" "iquest"
1caf38eb
RS
196 "Agrave" "Aacute" "Acirc" "Atilde" "Auml" "Aring" "AElig" "Ccedil"
197 "Egrave" "Eacute" "Ecirc" "Euml" "Igrave" "Iacute" "Icirc" "Iuml"
198 "ETH" "Ntilde" "Ograve" "Oacute" "Ocirc" "Otilde" "Ouml" nil
199 "Oslash" "Ugrave" "Uacute" "Ucirc" "Uuml" "Yacute" "THORN" "szlig"
200 "agrave" "aacute" "acirc" "atilde" "auml" "aring" "aelig" "ccedil"
201 "egrave" "eacute" "ecirc" "euml" "igrave" "iacute" "icirc" "iuml"
202 "eth" "ntilde" "ograve" "oacute" "ocirc" "otilde" "ouml" "divide"
203 "oslash" "ugrave" "uacute" "ucirc" "uuml" "yacute" "thorn" "yuml"]
204 "Vector of symbolic character names without `&' and `;'.")
205
2840d653
EZ
206(put 'sgml-table 'char-table-extra-slots 0)
207
208(defvar sgml-char-names-table
209 (let ((table (make-char-table 'sgml-table))
210 (i 32)
211 elt)
212 (while (< i 256)
213 (setq elt (aref sgml-char-names i))
214 (if elt (aset table (make-char 'latin-iso8859-1 i) elt))
215 (setq i (1+ i)))
216 table)
217 "A table for mapping non-ASCII characters into SGML entity names.
218Currently, only Latin-1 characters are supported.")
219
5121371d
DL
220;; nsgmls is a free SGML parser in the SP suite available from
221;; ftp.jclark.com and otherwise packaged for GNU systems.
1caf38eb
RS
222;; Its error messages can be parsed by next-error.
223;; The -s option suppresses output.
224
5121371d 225(defcustom sgml-validate-command "nsgmls -s" ; replaced old `sgmls'
72c0ae01
ER
226 "*The command to validate an SGML document.
227The file name of current buffer file name will be appended to this,
64ae0c23
RS
228separated by a space."
229 :type 'string
d4c89075 230 :version "21.1"
64ae0c23 231 :group 'sgml)
72c0ae01
ER
232
233(defvar sgml-saved-validate-command nil
234 "The command last used to validate in this buffer.")
235
e1940c83
SM
236;; I doubt that null end tags are used much for large elements,
237;; so use a small distance here.
64ae0c23 238(defcustom sgml-slash-distance 1000
f788776c 239 "*If non-nil, is the maximum distance to search for matching `/'."
64ae0c23
RS
240 :type '(choice (const nil) integer)
241 :group 'sgml)
72c0ae01 242
b0045305 243(defconst sgml-namespace-re "[_[:alpha:]][-_.[:alnum:]]*")
5f3d924d
SM
244(defconst sgml-name-re "[_:[:alpha:]][-_.:[:alnum:]]*")
245(defconst sgml-tag-name-re (concat "<\\([!/?]?" sgml-name-re "\\)"))
246(defconst sgml-attrs-re "\\(?:[^\"'/><]\\|\"[^\"]*\"\\|'[^']*'\\)*")
247(defconst sgml-start-tag-regex (concat "<" sgml-name-re sgml-attrs-re)
1caf38eb 248 "Regular expression that matches a non-empty start tag.
f788776c 249Any terminating `>' or `/' is not matched.")
1caf38eb 250
b0045305
SM
251(defface sgml-namespace-face
252 '((t (:inherit font-lock-builtin-face)))
253 "`sgml-mode' face used to highlight the namespace part of identifiers.")
254(defvar sgml-namespace-face 'sgml-namespace-face)
1caf38eb 255
c6a63534
RS
256;; internal
257(defconst sgml-font-lock-keywords-1
5f3d924d 258 `((,(concat "<\\([!?]" sgml-name-re "\\)") 1 font-lock-keyword-face)
b0045305
SM
259 ;; We could use the simpler "\\(" sgml-namespace-re ":\\)?" instead,
260 ;; but it would cause a bit more backtracking in the re-matcher.
261 (,(concat "</?\\(" sgml-namespace-re "\\)\\(?::\\(" sgml-name-re "\\)\\)?")
262 (1 (if (match-end 2) sgml-namespace-face font-lock-function-name-face))
263 (2 font-lock-function-name-face nil t))
1c1d2eb6 264 ;; FIXME: this doesn't cover the variables using a default value.
b0045305
SM
265 (,(concat "\\(" sgml-namespace-re "\\)\\(?::\\("
266 sgml-name-re "\\)\\)?=[\"']")
267 (1 (if (match-end 2) sgml-namespace-face font-lock-variable-name-face))
268 (2 font-lock-variable-name-face nil t))
5f3d924d 269 (,(concat "[&%]" sgml-name-re ";?") . font-lock-variable-name-face)))
64367655
SM
270
271(defconst sgml-font-lock-keywords-2
272 (append
273 sgml-font-lock-keywords-1
274 '((eval
275 . (cons (concat "<"
276 (regexp-opt (mapcar 'car sgml-tag-face-alist) t)
277 "\\([ \t][^>]*\\)?>\\([^<]+\\)</\\1>")
278 '(3 (cdr (assoc (downcase (match-string 1))
13b454db 279 sgml-tag-face-alist)) prepend))))))
c6a63534
RS
280
281;; for font-lock, but must be defvar'ed after
282;; sgml-font-lock-keywords-1 and sgml-font-lock-keywords-2 above
283(defvar sgml-font-lock-keywords sgml-font-lock-keywords-1
1caf38eb
RS
284 "*Rules for highlighting SGML code. See also `sgml-tag-face-alist'.")
285
64367655
SM
286(defvar sgml-font-lock-syntactic-keywords
287 ;; Use the `b' style of comments to avoid interference with the -- ... --
288 ;; comments recognized when `sgml-specials' includes ?-.
289 ;; FIXME: beware of <!--> blabla <!--> !!
290 '(("\\(<\\)!--" (1 "< b"))
291 ("--[ \t\n]*\\(>\\)" (1 "> b")))
292 "Syntactic keywords for `sgml-mode'.")
293
1caf38eb 294;; internal
1caf38eb
RS
295(defvar sgml-face-tag-alist ()
296 "Alist of face and tag name for facemenu.")
297
298(defvar sgml-tag-face-alist ()
299 "Tag names and face or list of faces to fontify with when invisible.
300When `font-lock-maximum-decoration' is 1 this is always used for fontifying.
301When more these are fontified together with `sgml-font-lock-keywords'.")
302
1caf38eb
RS
303(defvar sgml-display-text ()
304 "Tag names as lowercase symbols, and display string when invisible.")
305
306;; internal
307(defvar sgml-tags-invisible nil)
308
64ae0c23 309(defcustom sgml-tag-alist
fcc3195e
RS
310 '(("![" ("ignore" t) ("include" t))
311 ("!attlist")
1caf38eb
RS
312 ("!doctype")
313 ("!element")
314 ("!entity"))
315 "*Alist of tag names for completing read and insertion rules.
316This alist is made up as
317
318 ((\"tag\" . TAGRULE)
319 ...)
320
9d4ce428
MW
321TAGRULE is a list of optionally t (no endtag) or `\\n' (separate endtag by
322newlines) or a skeleton with nil, t or `\\n' in place of the interactor
1caf38eb
RS
323followed by an ATTRIBUTERULE (for an always present attribute) or an
324attribute alist.
325
326The attribute alist is made up as
327
328 ((\"attribute\" . ATTRIBUTERULE)
329 ...)
330
9d4ce428 331ATTRIBUTERULE is a list of optionally t (no value when no input) followed by
64ae0c23
RS
332an optional alist of possible values."
333 :type '(repeat (cons (string :tag "Tag Name")
334 (repeat :tag "Tag Rule" sexp)))
335 :group 'sgml)
1caf38eb 336
64ae0c23 337(defcustom sgml-tag-help
1caf38eb
RS
338 '(("!" . "Empty declaration for comment")
339 ("![" . "Embed declarations with parser directive")
340 ("!attlist" . "Tag attributes declaration")
341 ("!doctype" . "Document type (DTD) declaration")
342 ("!element" . "Tag declaration")
343 ("!entity" . "Entity (macro) declaration"))
64ae0c23
RS
344 "*Alist of tag name and short description."
345 :type '(repeat (cons (string :tag "Tag Name")
346 (string :tag "Description")))
347 :group 'sgml)
1caf38eb 348
a3ec4ba0 349(defcustom sgml-xml-mode nil
c77c3a73
SS
350 "*When non-nil, tag insertion functions will be XML-compliant.
351If this variable is customized, the custom value is used always.
352Otherwise, it is set to be buffer-local when the file has
af3abed1 353a DOCTYPE or an XML declaration."
c77c3a73 354 :type 'boolean
bf247b6e 355 :version "22.1"
c77c3a73
SS
356 :group 'sgml)
357
73d25e52
SM
358(defvar sgml-empty-tags nil
359 "List of tags whose !ELEMENT definition says EMPTY.")
360
5f3d924d
SM
361(defvar sgml-unclosed-tags nil
362 "List of tags whose !ELEMENT definition says the end-tag is optional.")
363
c77c3a73
SS
364(defun sgml-xml-guess ()
365 "Guess whether the current buffer is XML."
366 (save-excursion
367 (goto-char (point-min))
a3ec4ba0
SM
368 (when (or (string= "xml" (file-name-extension (or buffer-file-name "")))
369 (looking-at "\\s-*<\\?xml")
370 (when (re-search-forward
371 (eval-when-compile
59444a9c
SM
372 (mapconcat 'identity
373 '("<!DOCTYPE" "\\(\\w+\\)" "\\(\\w+\\)"
a3ec4ba0
SM
374 "\"\\([^\"]+\\)\"" "\"\\([^\"]+\\)\"")
375 "\\s-+"))
376 nil t)
377 (string-match "X\\(HT\\)?ML" (match-string 3))))
378 (set (make-local-variable 'sgml-xml-mode) t))))
c77c3a73 379
b0a377e6
DL
380(defvar v2) ; free for skeleton
381
60128096
SM
382(defun sgml-comment-indent-new-line (&optional soft)
383 (let ((comment-start "-- ")
384 (comment-start-skip "\\(<!\\)?--[ \t]*")
385 (comment-end " --")
386 (comment-style 'plain))
387 (comment-indent-new-line soft)))
388
a3ec4ba0
SM
389(defun sgml-mode-facemenu-add-face-function (face end)
390 (if (setq face (cdr (assq face sgml-face-tag-alist)))
391 (progn
392 (setq face (funcall skeleton-transformation face))
393 (setq facemenu-end-add-face (concat "</" face ">"))
394 (concat "<" face ">"))
395 (error "Face not configured for %s mode" mode-name)))
396
ed8031f2
SM
397(defun sgml-fill-nobreak ()
398 ;; Don't break between a tag name and its first argument.
399 (save-excursion
400 (skip-chars-backward " \t")
401 (and (not (zerop (skip-syntax-backward "w_")))
402 (skip-chars-backward "/?!")
403 (eq (char-before) ?<))))
404
a3ec4ba0
SM
405;;;###autoload
406(define-derived-mode sgml-mode text-mode "SGML"
407 "Major mode for editing SGML documents.
408Makes > match <.
2394187c 409Keys <, &, SPC within <>, \", / and ' can be electric depending on
a3ec4ba0
SM
410`sgml-quick-keys'.
411
412An argument of N to a tag-inserting command means to wrap it around
413the next N words. In Transient Mark mode, when the mark is active,
414N defaults to -1, which means to wrap it around the current region.
415
416If you like upcased tags, put (setq sgml-transformation 'upcase) in
417your `.emacs' file.
418
419Use \\[sgml-validate] to validate your document with an SGML parser.
420
421Do \\[describe-variable] sgml- SPC to see available variables.
422Do \\[describe-key] on the following bindings to discover what they do.
423\\{sgml-mode-map}"
72c0ae01 424 (make-local-variable 'sgml-saved-validate-command)
1caf38eb
RS
425 (make-local-variable 'facemenu-end-add-face)
426 ;;(make-local-variable 'facemenu-remove-face-function)
c77c3a73
SS
427 ;; A start or end tag by itself on a line separates a paragraph.
428 ;; This is desirable because SGML discards a newline that appears
429 ;; immediately after a start tag or immediately before an end tag.
5f3d924d
SM
430 (set (make-local-variable 'paragraph-start) (concat "[ \t]*$\\|\
431\[ \t]*</?\\(" sgml-name-re sgml-attrs-re "\\)?>"))
432 (set (make-local-variable 'paragraph-separate)
433 (concat paragraph-start "$"))
c77c3a73 434 (set (make-local-variable 'adaptive-fill-regexp) "[ \t]*")
ed8031f2 435 (add-hook 'fill-nobreak-predicate 'sgml-fill-nobreak nil t)
9c599518 436 (set (make-local-variable 'indent-line-function) 'sgml-indent-line)
c77c3a73
SS
437 (set (make-local-variable 'comment-start) "<!-- ")
438 (set (make-local-variable 'comment-end) " -->")
439 (set (make-local-variable 'comment-indent-function) 'sgml-comment-indent)
60128096
SM
440 (set (make-local-variable 'comment-line-break-function)
441 'sgml-comment-indent-new-line)
c77c3a73
SS
442 (set (make-local-variable 'skeleton-further-elements)
443 '((completion-ignore-case t)))
444 (set (make-local-variable 'skeleton-end-hook)
445 (lambda ()
446 (or (eolp)
447 (not (or (eq v2 '\n) (eq (car-safe v2) '\n)))
448 (newline-and-indent))))
449 (set (make-local-variable 'font-lock-defaults)
450 '((sgml-font-lock-keywords
451 sgml-font-lock-keywords-1
452 sgml-font-lock-keywords-2)
453 nil t nil nil
454 (font-lock-syntactic-keywords
455 . sgml-font-lock-syntactic-keywords)))
456 (set (make-local-variable 'facemenu-add-face-function)
457 'sgml-mode-facemenu-add-face-function)
a3ec4ba0
SM
458 (sgml-xml-guess)
459 (if sgml-xml-mode
460 (setq mode-name "XML")
461 (set (make-local-variable 'skeleton-transformation) sgml-transformation))
4afa094d
SM
462 ;; This will allow existing comments within declarations to be
463 ;; recognized.
464 (set (make-local-variable 'comment-start-skip) "\\(?:<!\\)?--[ \t]*")
a3ec4ba0 465 (set (make-local-variable 'comment-end-skip) "[ \t]*--\\([ \t\n]*>\\)?")
4456f964
DP
466 ;; This definition has an HTML leaning but probably fits well for other modes.
467 (setq imenu-generic-expression
468 `((nil
469 ,(concat "<!\\(element\\|entity\\)[ \t\n]+%?[ \t\n]*\\("
470 sgml-name-re "\\)")
471 2)
472 ("Id"
473 ,(concat "<[^>]+[ \t\n]+[Ii][Dd]=\\(['\"]"
474 (if sgml-xml-mode "" "?")
475 "\\)\\(" sgml-name-re "\\)\\1")
476 2)
477 ("Name"
478 ,(concat "<[^>]+[ \t\n]+[Nn][Aa][Mm][Ee]=\\(['\"]"
479 (if sgml-xml-mode "" "?")
480 "\\)\\(" sgml-name-re "\\)\\1")
481 2))))
1caf38eb 482
9d118494
CW
483;; Some programs (such as Glade 2) generate XML which has
484;; -*- mode: xml -*-.
7461dcb0 485;;;###autoload
9d118494
CW
486(defalias 'xml-mode 'sgml-mode)
487
72c0ae01 488(defun sgml-comment-indent ()
4afa094d 489 (if (looking-at "--") comment-column 0))
72c0ae01 490
72c0ae01 491(defun sgml-slash (arg)
2394187c
SM
492 "Insert ARG slash characters.
493Behaves electrically if `sgml-quick-keys' is non-nil."
494 (interactive "p")
495 (cond
496 ((not (and (eq (char-before) ?<) (= arg 1)))
497 (sgml-slash-matching arg))
498 ((eq sgml-quick-keys 'indent)
499 (insert-char ?/ 1)
500 (indent-according-to-mode))
501 ((eq sgml-quick-keys 'close)
502 (delete-backward-char 1)
f6ab0573 503 (sgml-close-tag))
2394187c
SM
504 (t
505 (sgml-slash-matching arg))))
506
507(defun sgml-slash-matching (arg)
f788776c
RS
508 "Insert `/' and display any previous matching `/'.
509Two `/'s are treated as matching if the first `/' ends a net-enabling
510start tag, and the second `/' is the corresponding null end tag."
72c0ae01
ER
511 (interactive "p")
512 (insert-char ?/ arg)
513 (if (> arg 0)
514 (let ((oldpos (point))
515 (blinkpos)
516 (level 0))
517 (save-excursion
518 (save-restriction
519 (if sgml-slash-distance
520 (narrow-to-region (max (point-min)
521 (- (point) sgml-slash-distance))
522 oldpos))
523 (if (and (re-search-backward sgml-start-tag-regex (point-min) t)
524 (eq (match-end 0) (1- oldpos)))
525 ()
526 (goto-char (1- oldpos))
527 (while (and (not blinkpos)
528 (search-backward "/" (point-min) t))
529 (let ((tagend (save-excursion
530 (if (re-search-backward sgml-start-tag-regex
531 (point-min) t)
532 (match-end 0)
533 nil))))
534 (if (eq tagend (point))
535 (if (eq level 0)
536 (setq blinkpos (point))
537 (setq level (1- level)))
538 (setq level (1+ level)))))))
5950e029
SS
539 (when blinkpos
540 (goto-char blinkpos)
541 (if (pos-visible-in-window-p)
542 (sit-for 1)
543 (message "Matches %s"
544 (buffer-substring (line-beginning-position)
545 (1+ blinkpos)))))))))
72c0ae01 546
0fda8eff
SM
547;; Why doesn't this use the iso-cvt table or, preferably, generate the
548;; inverse of the extensive table in the SGML Quail input method? -- fx
549;; I guess that's moot since it only works with Latin-1 anyhow.
1caf38eb
RS
550(defun sgml-name-char (&optional char)
551 "Insert a symbolic character name according to `sgml-char-names'.
2840d653
EZ
552Non-ASCII chars may be inserted either with the meta key, as in M-SPC for
553no-break space or M-- for a soft hyphen; or via an input method or
554encoded keyboard operation."
1caf38eb
RS
555 (interactive "*")
556 (insert ?&)
557 (or char
9b0ffdac 558 (setq char (read-quoted-char "Enter char or octal number")))
1caf38eb
RS
559 (delete-backward-char 1)
560 (insert char)
561 (undo-boundary)
4e7a42d2
SM
562 (sgml-namify-char))
563
564(defun sgml-namify-char ()
565 "Change the char before point into its `&name;' equivalent.
566Uses `sgml-char-names'."
567 (interactive)
568 (let* ((char (char-before))
569 (name
570 (cond
571 ((null char) (error "No char before point"))
572 ((< char 256) (or (aref sgml-char-names char) char))
573 ((aref sgml-char-names-table char))
574 ((encode-char char 'ucs)))))
575 (if (not name)
576 (error "Don't know the name of `%c'" char)
577 (delete-backward-char 1)
578 (insert (format (if (numberp name) "&#%d;" "&%s;") name)))))
1caf38eb
RS
579
580(defun sgml-name-self ()
581 "Insert a symbolic character name according to `sgml-char-names'."
582 (interactive "*")
583 (sgml-name-char last-command-char))
584
1caf38eb
RS
585(defun sgml-maybe-name-self ()
586 "Insert a symbolic character name according to `sgml-char-names'."
587 (interactive "*")
588 (if sgml-name-8bit-mode
2840d653
EZ
589 (let ((mc last-command-char))
590 (if (< mc 256)
591 (setq mc (unibyte-char-to-multibyte mc)))
592 (or mc (setq mc last-command-char))
593 (sgml-name-char mc))
1caf38eb
RS
594 (self-insert-command 1)))
595
1caf38eb 596(defun sgml-name-8bit-mode ()
0fda8eff
SM
597 "Toggle whether to insert named entities instead of non-ASCII characters.
598This only works for Latin-1 input."
1caf38eb 599 (interactive)
d10447ba 600 (setq sgml-name-8bit-mode (not sgml-name-8bit-mode))
2840d653 601 (message "sgml name entity mode is now %s"
d10447ba 602 (if sgml-name-8bit-mode "ON" "OFF")))
1caf38eb 603
f788776c
RS
604;; When an element of a skeleton is a string "str", it is passed
605;; through skeleton-transformation and inserted. If "str" is to be
606;; inserted literally, one should obtain it as the return value of a
607;; function, e.g. (identity "str").
1caf38eb 608
4e7a42d2
SM
609(defvar sgml-tag-last nil)
610(defvar sgml-tag-history nil)
1caf38eb 611(define-skeleton sgml-tag
f788776c
RS
612 "Prompt for a tag and insert it, optionally with attributes.
613Completion and configuration are done according to `sgml-tag-alist'.
d10447ba 614If you like tags and attributes in uppercase do \\[set-variable]
f788776c
RS
615skeleton-transformation RET upcase RET, or put this in your `.emacs':
616 (setq sgml-transformation 'upcase)"
51df53f8 617 (funcall (or skeleton-transformation 'identity)
4e7a42d2
SM
618 (setq sgml-tag-last
619 (completing-read
620 (if (> (length sgml-tag-last) 0)
621 (format "Tag (default %s): " sgml-tag-last)
622 "Tag: ")
623 sgml-tag-alist nil nil nil 'sgml-tag-history sgml-tag-last)))
4afa094d 624 ?< str |
d10447ba 625 (("") -1 '(undo-boundary) (identity "&lt;")) | ; see comment above
73d25e52
SM
626 `(("") '(setq v2 (sgml-attributes ,str t)) ?>
627 (cond
628 ((string= "![" ,str)
629 (backward-char)
630 '(("") " [ " _ " ]]"))
a3ec4ba0 631 ((and (eq v2 t) sgml-xml-mode (member ,str sgml-empty-tags))
af3abed1 632 '(("") -1 " />"))
a3ec4ba0 633 ((or (and (eq v2 t) (not sgml-xml-mode)) (string-match "^[/!?]" ,str))
73d25e52
SM
634 nil)
635 ((symbolp v2)
636 ;; Make sure we don't fall into an infinite loop.
637 ;; For xhtml's `tr' tag, we should maybe use \n instead.
638 (if (eq v2 t) (setq v2 nil))
639 ;; We use `identity' to prevent skeleton from passing
640 ;; `str' through skeleton-transformation a second time.
641 '(("") v2 _ v2 "</" (identity ',str) ?>))
642 ((eq (car v2) t)
643 (cons '("") (cdr v2)))
644 (t
645 (append '(("") (car v2))
646 (cdr v2)
647 '(resume: (car v2) _ "</" (identity ',str) ?>))))))
1caf38eb
RS
648
649(autoload 'skeleton-read "skeleton")
650
d10447ba 651(defun sgml-attributes (tag &optional quiet)
f788776c 652 "When at top level of a tag, interactively insert attributes.
d10447ba 653
f788776c
RS
654Completion and configuration of TAG are done according to `sgml-tag-alist'.
655If QUIET, do not print a message when there are no attributes for TAG."
1caf38eb 656 (interactive (list (save-excursion (sgml-beginning-of-tag t))))
d10447ba
RS
657 (or (stringp tag) (error "Wrong context for adding attribute"))
658 (if tag
1caf38eb 659 (let ((completion-ignore-case t)
d10447ba 660 (alist (cdr (assoc (downcase tag) sgml-tag-alist)))
1caf38eb 661 car attribute i)
1caf38eb
RS
662 (if (or (symbolp (car alist))
663 (symbolp (car (car alist))))
664 (setq car (car alist)
665 alist (cdr alist)))
666 (or quiet
667 (message "No attributes configured."))
668 (if (stringp (car alist))
669 (progn
d10447ba
RS
670 (insert (if (eq (preceding-char) ? ) "" ? )
671 (funcall skeleton-transformation (car alist)))
1caf38eb
RS
672 (sgml-value alist))
673 (setq i (length alist))
674 (while (> i 0)
675 (insert ? )
676 (insert (funcall skeleton-transformation
677 (setq attribute
678 (skeleton-read '(completing-read
d10447ba 679 "Attribute: "
1caf38eb
RS
680 alist)))))
681 (if (string= "" attribute)
682 (setq i 0)
aa7a8f0e 683 (sgml-value (assoc (downcase attribute) alist))
1caf38eb
RS
684 (setq i (1- i))))
685 (if (eq (preceding-char) ? )
686 (delete-backward-char 1)))
687 car)))
688
689(defun sgml-auto-attributes (arg)
f788776c
RS
690 "Self insert the character typed; at top level of tag, prompt for attributes.
691With prefix argument, only self insert."
1caf38eb
RS
692 (interactive "*P")
693 (let ((point (point))
694 tag)
695 (if (or arg
1caf38eb
RS
696 (not sgml-tag-alist) ; no message when nothing configured
697 (symbolp (setq tag (save-excursion (sgml-beginning-of-tag t))))
698 (eq (aref tag 0) ?/))
699 (self-insert-command (prefix-numeric-value arg))
700 (sgml-attributes tag)
701 (setq last-command-char ? )
702 (or (> (point) point)
703 (self-insert-command 1)))))
704
1caf38eb 705(defun sgml-tag-help (&optional tag)
f788776c 706 "Display description of tag TAG. If TAG is omitted, use the tag at point."
1caf38eb
RS
707 (interactive)
708 (or tag
709 (save-excursion
710 (if (eq (following-char) ?<)
711 (forward-char))
712 (setq tag (sgml-beginning-of-tag))))
713 (or (stringp tag)
714 (error "No tag selected"))
715 (setq tag (downcase tag))
f68f40e0 716 (message "%s"
aa7a8f0e 717 (or (cdr (assoc (downcase tag) sgml-tag-help))
1caf38eb 718 (and (eq (aref tag 0) ?/)
aa7a8f0e 719 (cdr (assoc (downcase (substring tag 1)) sgml-tag-help)))
1caf38eb
RS
720 "No description available")))
721
1c1d2eb6
SM
722(defun sgml-maybe-end-tag (&optional arg)
723 "Name self unless in position to end a tag or a prefix ARG is given."
724 (interactive "P")
725 (if (or arg (eq (car (sgml-lexical-context)) 'tag))
726 (self-insert-command (prefix-numeric-value arg))
727 (sgml-name-self)))
1caf38eb
RS
728
729(defun sgml-skip-tag-backward (arg)
730 "Skip to beginning of tag or matching opening tag if present.
f788776c 731With prefix argument ARG, repeat this ARG times."
1caf38eb 732 (interactive "p")
4e7a42d2 733 ;; FIXME: use sgml-get-context or something similar.
1caf38eb
RS
734 (while (>= arg 1)
735 (search-backward "<" nil t)
736 (if (looking-at "</\\([^ \n\t>]+\\)")
737 ;; end tag, skip any nested pairs
738 (let ((case-fold-search t)
65b34485
SM
739 (re (concat "</?" (regexp-quote (match-string 1))
740 ;; Ignore empty tags like <foo/>.
741 "\\([^>]*[^/>]\\)?>")))
1caf38eb
RS
742 (while (and (re-search-backward re nil t)
743 (eq (char-after (1+ (point))) ?/))
744 (forward-char 1)
745 (sgml-skip-tag-backward 1))))
746 (setq arg (1- arg))))
747
65b34485 748(defun sgml-skip-tag-forward (arg)
1caf38eb 749 "Skip to end of tag or matching closing tag if present.
f788776c 750With prefix argument ARG, repeat this ARG times.
1caf38eb
RS
751Return t iff after a closing tag."
752 (interactive "p")
4e7a42d2
SM
753 ;; FIXME: Use sgml-get-context or something similar.
754 ;; It currently might jump to an unrelated </P> if the <P>
755 ;; we're skipping has no matching </P>.
65b34485 756 (let ((return t))
4e7a42d2
SM
757 (with-syntax-table sgml-tag-syntax-table
758 (while (>= arg 1)
759 (skip-chars-forward "^<>")
760 (if (eq (following-char) ?>)
761 (up-list -1))
762 (if (looking-at "<\\([^/ \n\t>]+\\)\\([^>]*[^/>]\\)?>")
763 ;; start tag, skip any nested same pairs _and_ closing tag
764 (let ((case-fold-search t)
765 (re (concat "</?" (regexp-quote (match-string 1))
766 ;; Ignore empty tags like <foo/>.
767 "\\([^>]*[^/>]\\)?>"))
768 point close)
769 (forward-list 1)
770 (setq point (point))
771 ;; FIXME: This re-search-forward will mistakenly match
772 ;; tag-like text inside attributes.
773 (while (and (re-search-forward re nil t)
774 (not (setq close
775 (eq (char-after (1+ (match-beginning 0))) ?/)))
776 (goto-char (match-beginning 0))
777 (sgml-skip-tag-forward 1))
778 (setq close nil))
779 (unless close
780 (goto-char point)
781 (setq return nil)))
782 (forward-list 1))
783 (setq arg (1- arg)))
784 return)))
1caf38eb
RS
785
786(defun sgml-delete-tag (arg)
4e7a42d2 787 ;; FIXME: Should be called sgml-kill-tag or should not touch the kill-ring.
1caf38eb 788 "Delete tag on or after cursor, and matching closing or opening tag.
f788776c 789With prefix argument ARG, repeat this ARG times."
1caf38eb
RS
790 (interactive "p")
791 (while (>= arg 1)
792 (save-excursion
793 (let* (close open)
fcc3195e 794 (if (looking-at "[ \t\n]*<")
1caf38eb
RS
795 ;; just before tag
796 (if (eq (char-after (match-end 0)) ?/)
797 ;; closing tag
798 (progn
799 (setq close (point))
800 (goto-char (match-end 0))))
801 ;; on tag?
802 (or (save-excursion (setq close (sgml-beginning-of-tag)
803 close (and (stringp close)
804 (eq (aref close 0) ?/)
805 (point))))
806 ;; not on closing tag
807 (let ((point (point)))
808 (sgml-skip-tag-backward 1)
809 (if (or (not (eq (following-char) ?<))
810 (save-excursion
811 (forward-list 1)
812 (<= (point) point)))
813 (error "Not on or before tag")))))
814 (if close
815 (progn
816 (sgml-skip-tag-backward 1)
817 (setq open (point))
818 (goto-char close)
819 (kill-sexp 1))
820 (setq open (point))
af3abed1
JL
821 (when (and (sgml-skip-tag-forward 1)
822 (not (looking-back "/>")))
4e7a42d2
SM
823 (kill-sexp -1)))
824 ;; Delete any resulting empty line. If we didn't kill-sexp,
825 ;; this *should* do nothing, because we're right after the tag.
826 (if (progn (forward-line 0) (looking-at "\\(?:[ \t]*$\\)\n?"))
827 (delete-region (match-beginning 0) (match-end 0)))
1caf38eb 828 (goto-char open)
4e7a42d2
SM
829 (kill-sexp 1)
830 (if (progn (forward-line 0) (looking-at "\\(?:[ \t]*$\\)\n?"))
831 (delete-region (match-beginning 0) (match-end 0)))))
1caf38eb 832 (setq arg (1- arg))))
9d4ce428 833
a391b179
RS
834\f
835;; Put read-only last to enable setting this even when read-only enabled.
836(or (get 'sgml-tag 'invisible)
837 (setplist 'sgml-tag
838 (append '(invisible t
a391b179
RS
839 point-entered sgml-point-entered
840 rear-nonsticky t
841 read-only t)
842 (symbol-plist 'sgml-tag))))
1caf38eb
RS
843
844(defun sgml-tags-invisible (arg)
845 "Toggle visibility of existing tags."
846 (interactive "P")
847 (let ((modified (buffer-modified-p))
848 (inhibit-read-only t)
e1940c83
SM
849 (inhibit-modification-hooks t)
850 ;; Avoid spurious the `file-locked' checks.
851 (buffer-file-name nil)
a391b179
RS
852 ;; This is needed in case font lock gets called,
853 ;; since it moves point and might call sgml-point-entered.
64367655 854 ;; How could it get called? -stef
a391b179 855 (inhibit-point-motion-hooks t)
64367655 856 string)
e1940c83
SM
857 (unwind-protect
858 (save-excursion
859 (goto-char (point-min))
73d25e52
SM
860 (if (set (make-local-variable 'sgml-tags-invisible)
861 (if arg
862 (>= (prefix-numeric-value arg) 0)
863 (not sgml-tags-invisible)))
1c1d2eb6 864 (while (re-search-forward sgml-tag-name-re nil t)
64367655
SM
865 (setq string
866 (cdr (assq (intern-soft (downcase (match-string 1)))
867 sgml-display-text)))
e1940c83 868 (goto-char (match-beginning 0))
64367655 869 (and (stringp string)
e1940c83 870 (not (overlays-at (point)))
73d25e52
SM
871 (let ((ol (make-overlay (point) (match-beginning 1))))
872 (overlay-put ol 'before-string string)
873 (overlay-put ol 'sgml-tag t)))
e1940c83
SM
874 (put-text-property (point)
875 (progn (forward-list) (point))
876 'category 'sgml-tag))
64367655 877 (let ((pos (point-min)))
e1940c83 878 (while (< (setq pos (next-overlay-change pos)) (point-max))
73d25e52 879 (dolist (ol (overlays-at pos))
b2e8c203 880 (if (overlay-get ol 'sgml-tag)
73d25e52 881 (delete-overlay ol)))))
64367655 882 (remove-text-properties (point-min) (point-max) '(category nil))))
e1940c83 883 (restore-buffer-modified-p modified))
1caf38eb
RS
884 (run-hooks 'sgml-tags-invisible-hook)
885 (message "")))
886
887(defun sgml-point-entered (x y)
888 ;; Show preceding or following hidden tag, depending of cursor direction.
889 (let ((inhibit-point-motion-hooks t))
890 (save-excursion
891 (message "Invisible tag: %s"
e1940c83
SM
892 ;; Strip properties, otherwise, the text is invisible.
893 (buffer-substring-no-properties
1caf38eb
RS
894 (point)
895 (if (or (and (> x y)
896 (not (eq (following-char) ?<)))
897 (and (< x y)
898 (eq (preceding-char) ?>)))
899 (backward-list)
900 (forward-list)))))))
9d4ce428 901
a391b179 902\f
1caf38eb
RS
903(autoload 'compile-internal "compile")
904
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)
c7aa4667 921 (compile-internal command "No more errors"))
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
1386 (error "Unrecognised context %s" (car lcon)))
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")
b4f05c38 1721 ("em" . "Emphasised")
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
1807<b>bold</b>, <i>italic</i> or <u>underlined</u> using the normal M-g or
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)
da84bdc4
RS
1831 (make-local-variable 'sentence-end)
1832 (setq sentence-end
b8b14971
DL
1833 (if sentence-end-double-space
1834 "[.?!][]\"')}]*\\(<[^>]*>\\)*\\($\\| $\\|\t\\| \\)[ \t\n]*"
64367655 1835 "[.?!][]\"')}]*\\(<[^>]*>\\)*\\($\\|[ \t]\\)[ \t\n]*"))
a01588fc 1836 (setq sgml-tag-alist html-tag-alist
1caf38eb
RS
1837 sgml-face-tag-alist html-face-tag-alist
1838 sgml-tag-help html-tag-help
1839 outline-regexp "^.*<[Hh][1-6]\\>"
1840 outline-heading-end-regexp "</[Hh][1-6]>"
1841 outline-level (lambda ()
0fda8eff 1842 (char-before (match-end 0))))
3bf0b727 1843 (setq imenu-create-index-function 'html-imenu-index)
a3ec4ba0 1844 (when sgml-xml-mode (setq mode-name "XHTML"))
73d25e52 1845 (set (make-local-variable 'sgml-empty-tags)
5f3d924d
SM
1846 ;; From HTML-4.01's loose.dtd, parsed with `sgml-parse-dtd',
1847 ;; plus manual addition of "wbr".
1848 '("area" "base" "basefont" "br" "col" "frame" "hr" "img" "input"
1849 "isindex" "link" "meta" "param" "wbr"))
1850 (set (make-local-variable 'sgml-unclosed-tags)
1851 ;; From HTML-4.01's loose.dtd, parsed with `sgml-parse-dtd'.
1852 '("body" "colgroup" "dd" "dt" "head" "html" "li" "option"
1853 "p" "tbody" "td" "tfoot" "th" "thead" "tr"))
e1940c83
SM
1854 ;; It's for the user to decide if it defeats it or not -stef
1855 ;; (make-local-variable 'imenu-sort-function)
1856 ;; (setq imenu-sort-function nil) ; sorting the menu defeats the purpose
64367655 1857 )
9d4ce428 1858
3bf0b727
RS
1859(defvar html-imenu-regexp
1860 "\\s-*<h\\([1-9]\\)[^\n<>]*>\\(<[^\n<>]*>\\)*\\s-*\\([^\n<>]*\\)"
1861 "*A regular expression matching a head line to be added to the menu.
1862The first `match-string' should be a number from 1-9.
1863The second `match-string' matches extra tags and is ignored.
1864The third `match-string' will be the used in the menu.")
1865
1866(defun html-imenu-index ()
a9d4efa2 1867 "Return a table of contents for an HTML buffer for use with Imenu."
3bf0b727
RS
1868 (let (toc-index)
1869 (save-excursion
1870 (goto-char (point-min))
1871 (while (re-search-forward html-imenu-regexp nil t)
1872 (setq toc-index
1873 (cons (cons (concat (make-string
1874 (* 2 (1- (string-to-number (match-string 1))))
1875 ?\ )
1876 (match-string 3))
5950e029 1877 (line-beginning-position))
3bf0b727
RS
1878 toc-index))))
1879 (nreverse toc-index)))
1caf38eb 1880
4e7a42d2 1881(define-minor-mode html-autoview-mode
d4c89075 1882 "Toggle automatic viewing via `browse-url-of-buffer' upon saving buffer.
3bf0b727
RS
1883With positive prefix ARG always turns viewing on, with negative ARG always off.
1884Can be used as a value for `html-mode-hook'."
4e7a42d2 1885 nil nil nil
966cdb22 1886 :group 'sgml
4e7a42d2
SM
1887 (if html-autoview-mode
1888 (add-hook 'after-save-hook 'browse-url-of-buffer nil t)
1889 (remove-hook 'after-save-hook 'browse-url-of-buffer t)))
9d4ce428 1890
3bf0b727 1891\f
1caf38eb
RS
1892(define-skeleton html-href-anchor
1893 "HTML anchor tag with href attribute."
a391b179 1894 "URL: "
af3abed1 1895 ;; '(setq input "http:")
a391b179 1896 "<a href=\"" str "\">" _ "</a>")
1caf38eb
RS
1897
1898(define-skeleton html-name-anchor
1899 "HTML anchor tag with name attribute."
a391b179 1900 "Name: "
af3abed1
JL
1901 "<a name=\"" str "\""
1902 (if sgml-xml-mode (concat " id=\"" str "\""))
1903 ">" _ "</a>")
1caf38eb 1904
7e49eef2
RS
1905(define-skeleton html-headline-1
1906 "HTML level 1 headline tags."
1907 nil
1908 "<h1>" _ "</h1>")
1909
1910(define-skeleton html-headline-2
1911 "HTML level 2 headline tags."
1912 nil
1913 "<h2>" _ "</h2>")
1914
1915(define-skeleton html-headline-3
1916 "HTML level 3 headline tags."
1917 nil
1918 "<h3>" _ "</h3>")
1919
1920(define-skeleton html-headline-4
1921 "HTML level 4 headline tags."
1922 nil
1923 "<h4>" _ "</h4>")
1924
1925(define-skeleton html-headline-5
1926 "HTML level 5 headline tags."
1927 nil
1928 "<h5>" _ "</h5>")
1929
1930(define-skeleton html-headline-6
1931 "HTML level 6 headline tags."
1932 nil
1933 "<h6>" _ "</h6>")
1caf38eb
RS
1934
1935(define-skeleton html-horizontal-rule
1936 "HTML horizontal rule tag."
1937 nil
af3abed1 1938 (if sgml-xml-mode "<hr />" "<hr>") \n)
1caf38eb
RS
1939
1940(define-skeleton html-image
1941 "HTML image tag."
af3abed1
JL
1942 "Image URL: "
1943 "<img src=\"" str "\" alt=\"" _ "\""
1944 (if sgml-xml-mode " />" ">"))
1caf38eb
RS
1945
1946(define-skeleton html-line
1947 "HTML line break tag."
1948 nil
af3abed1 1949 (if sgml-xml-mode "<br />" "<br>") \n)
1caf38eb 1950
7e49eef2
RS
1951(define-skeleton html-ordered-list
1952 "HTML ordered list tags."
1953 nil
a391b179 1954 "<ol>" \n
a3ec4ba0 1955 "<li>" _ (if sgml-xml-mode "</li>") \n
7e49eef2
RS
1956 "</ol>")
1957
1958(define-skeleton html-unordered-list
1959 "HTML unordered list tags."
1960 nil
a391b179 1961 "<ul>" \n
a3ec4ba0 1962 "<li>" _ (if sgml-xml-mode "</li>") \n
7e49eef2 1963 "</ul>")
1caf38eb
RS
1964
1965(define-skeleton html-list-item
1966 "HTML list item tag."
1967 nil
1968 (if (bolp) nil '\n)
a3ec4ba0 1969 "<li>" _ (if sgml-xml-mode "</li>"))
1caf38eb
RS
1970
1971(define-skeleton html-paragraph
1972 "HTML paragraph tag."
1973 nil
1974 (if (bolp) nil ?\n)
af3abed1 1975 "<p>" _ (if sgml-xml-mode "</p>"))
1caf38eb 1976
fcc3195e
RS
1977(define-skeleton html-checkboxes
1978 "Group of connected checkbox inputs."
1979 nil
a391b179
RS
1980 '(setq v1 nil
1981 v2 nil)
1982 ("Value: "
d10447ba 1983 "<input type=\"" (identity "checkbox") ; see comment above about identity
a391b179 1984 "\" name=\"" (or v1 (setq v1 (skeleton-read "Name: ")))
fcc3195e 1985 "\" value=\"" str ?\"
b4f05c38 1986 (when (y-or-n-p "Set \"checked\" attribute? ")
af3abed1
JL
1987 (funcall skeleton-transformation
1988 (if sgml-xml-mode " checked=\"checked\"" " checked")))
1989 (if sgml-xml-mode " />" ">")
a391b179
RS
1990 (skeleton-read "Text: " (capitalize str))
1991 (or v2 (setq v2 (if (y-or-n-p "Newline after text? ")
b4f05c38 1992 (funcall skeleton-transformation
af3abed1 1993 (if sgml-xml-mode "<br />" "<br>"))
a391b179
RS
1994 "")))
1995 \n))
fcc3195e 1996
1caf38eb
RS
1997(define-skeleton html-radio-buttons
1998 "Group of connected radio button inputs."
1999 nil
a391b179
RS
2000 '(setq v1 nil
2001 v2 (cons nil nil))
2002 ("Value: "
d10447ba 2003 "<input type=\"" (identity "radio") ; see comment above about identity
a391b179 2004 "\" name=\"" (or (car v2) (setcar v2 (skeleton-read "Name: ")))
1caf38eb 2005 "\" value=\"" str ?\"
b4f05c38 2006 (when (and (not v1) (setq v1 (y-or-n-p "Set \"checked\" attribute? ")))
af3abed1
JL
2007 (funcall skeleton-transformation
2008 (if sgml-xml-mode " checked=\"checked\"" " checked")))
2009 (if sgml-xml-mode " />" ">")
a391b179
RS
2010 (skeleton-read "Text: " (capitalize str))
2011 (or (cdr v2) (setcdr v2 (if (y-or-n-p "Newline after text? ")
b4f05c38 2012 (funcall skeleton-transformation
af3abed1 2013 (if sgml-xml-mode "<br />" "<br>"))
a391b179
RS
2014 "")))
2015 \n))
1caf38eb 2016
e1940c83 2017(provide 'sgml-mode)
6a05d05f 2018
e9146d5a 2019;; arch-tag: 9675da94-b7f9-4bda-ad19-73ed7b4fb401
72c0ae01 2020;;; sgml-mode.el ends here