(sgml-fill-nobreak): New fun.
[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
353 a DOCTYPE or an XML declaration."
354 :type 'boolean
89e7ad59 355 :version "21.4"
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))
73d25e52 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))
4e7a42d2
SM
821 (when (sgml-skip-tag-forward 1)
822 (kill-sexp -1)))
823 ;; Delete any resulting empty line. If we didn't kill-sexp,
824 ;; this *should* do nothing, because we're right after the tag.
825 (if (progn (forward-line 0) (looking-at "\\(?:[ \t]*$\\)\n?"))
826 (delete-region (match-beginning 0) (match-end 0)))
1caf38eb 827 (goto-char open)
4e7a42d2
SM
828 (kill-sexp 1)
829 (if (progn (forward-line 0) (looking-at "\\(?:[ \t]*$\\)\n?"))
830 (delete-region (match-beginning 0) (match-end 0)))))
1caf38eb 831 (setq arg (1- arg))))
9d4ce428 832
a391b179
RS
833\f
834;; Put read-only last to enable setting this even when read-only enabled.
835(or (get 'sgml-tag 'invisible)
836 (setplist 'sgml-tag
837 (append '(invisible t
a391b179
RS
838 point-entered sgml-point-entered
839 rear-nonsticky t
840 read-only t)
841 (symbol-plist 'sgml-tag))))
1caf38eb
RS
842
843(defun sgml-tags-invisible (arg)
844 "Toggle visibility of existing tags."
845 (interactive "P")
846 (let ((modified (buffer-modified-p))
847 (inhibit-read-only t)
e1940c83
SM
848 (inhibit-modification-hooks t)
849 ;; Avoid spurious the `file-locked' checks.
850 (buffer-file-name nil)
a391b179
RS
851 ;; This is needed in case font lock gets called,
852 ;; since it moves point and might call sgml-point-entered.
64367655 853 ;; How could it get called? -stef
a391b179 854 (inhibit-point-motion-hooks t)
64367655 855 string)
e1940c83
SM
856 (unwind-protect
857 (save-excursion
858 (goto-char (point-min))
73d25e52
SM
859 (if (set (make-local-variable 'sgml-tags-invisible)
860 (if arg
861 (>= (prefix-numeric-value arg) 0)
862 (not sgml-tags-invisible)))
1c1d2eb6 863 (while (re-search-forward sgml-tag-name-re nil t)
64367655
SM
864 (setq string
865 (cdr (assq (intern-soft (downcase (match-string 1)))
866 sgml-display-text)))
e1940c83 867 (goto-char (match-beginning 0))
64367655 868 (and (stringp string)
e1940c83 869 (not (overlays-at (point)))
73d25e52
SM
870 (let ((ol (make-overlay (point) (match-beginning 1))))
871 (overlay-put ol 'before-string string)
872 (overlay-put ol 'sgml-tag t)))
e1940c83
SM
873 (put-text-property (point)
874 (progn (forward-list) (point))
875 'category 'sgml-tag))
64367655 876 (let ((pos (point-min)))
e1940c83 877 (while (< (setq pos (next-overlay-change pos)) (point-max))
73d25e52 878 (dolist (ol (overlays-at pos))
b2e8c203 879 (if (overlay-get ol 'sgml-tag)
73d25e52 880 (delete-overlay ol)))))
64367655 881 (remove-text-properties (point-min) (point-max) '(category nil))))
e1940c83 882 (restore-buffer-modified-p modified))
1caf38eb
RS
883 (run-hooks 'sgml-tags-invisible-hook)
884 (message "")))
885
886(defun sgml-point-entered (x y)
887 ;; Show preceding or following hidden tag, depending of cursor direction.
888 (let ((inhibit-point-motion-hooks t))
889 (save-excursion
890 (message "Invisible tag: %s"
e1940c83
SM
891 ;; Strip properties, otherwise, the text is invisible.
892 (buffer-substring-no-properties
1caf38eb
RS
893 (point)
894 (if (or (and (> x y)
895 (not (eq (following-char) ?<)))
896 (and (< x y)
897 (eq (preceding-char) ?>)))
898 (backward-list)
899 (forward-list)))))))
9d4ce428 900
a391b179 901\f
1caf38eb
RS
902(autoload 'compile-internal "compile")
903
72c0ae01
ER
904(defun sgml-validate (command)
905 "Validate an SGML document.
906Runs COMMAND, a shell command, in a separate process asynchronously
f788776c 907with output going to the buffer `*compilation*'.
72c0ae01
ER
908You can then use the command \\[next-error] to find the next error message
909and move to the line in the SGML document that caused it."
910 (interactive
911 (list (read-string "Validate command: "
912 (or sgml-saved-validate-command
913 (concat sgml-validate-command
914 " "
915 (let ((name (buffer-file-name)))
916 (and name
917 (file-name-nondirectory name))))))))
918 (setq sgml-saved-validate-command command)
b7cd1746 919 (save-some-buffers (not compilation-ask-about-save) nil)
c7aa4667 920 (compile-internal command "No more errors"))
72c0ae01 921
662deeab
MW
922(defsubst sgml-at-indentation-p ()
923 "Return true if point is at the first non-whitespace character on the line."
924 (save-excursion
925 (skip-chars-backward " \t")
926 (bolp)))
927
1c1d2eb6
SM
928(defun sgml-lexical-context (&optional limit)
929 "Return the lexical context at point as (TYPE . START).
930START is the location of the start of the lexical element.
2cfd19d4 931TYPE is one of `string', `comment', `tag', `cdata', or `text'.
1c1d2eb6 932
41bfcbee
MW
933Optional argument LIMIT is the position to start parsing from.
934If nil, start from a preceding tag at indentation."
1c1d2eb6
SM
935 (save-excursion
936 (let ((pos (point))
14614b6d 937 text-start state)
41bfcbee
MW
938 (if limit
939 (goto-char limit)
940 ;; Skip tags backwards until we find one at indentation
941 (while (and (ignore-errors (sgml-parse-tag-backward))
942 (not (sgml-at-indentation-p)))))
5f3d924d
SM
943 (with-syntax-table sgml-tag-syntax-table
944 (while (< (point) pos)
945 ;; When entering this loop we're inside text.
80fc318e 946 (setq text-start (point))
5f3d924d 947 (skip-chars-forward "^<" pos)
14614b6d
MW
948 (setq state
949 (cond
60128096 950 ((= (point) pos)
14614b6d
MW
951 ;; We got to the end without seeing a tag.
952 nil)
953 ((looking-at "<!\\[[A-Z]+\\[")
954 ;; We've found a CDATA section or similar.
955 (let ((cdata-start (point)))
956 (unless (search-forward "]]>" pos 'move)
957 (list 0 nil nil 'cdata nil nil nil nil cdata-start))))
958 (t
2871b07a 959 ;; We've reached a tag. Parse it.
14614b6d
MW
960 ;; FIXME: Handle net-enabling start-tags
961 (parse-partial-sexp (point) pos 0))))))
962 (cond
963 ((eq (nth 3 state) 'cdata) (cons 'cdata (nth 8 state)))
964 ((nth 3 state) (cons 'string (nth 8 state)))
965 ((nth 4 state) (cons 'comment (nth 8 state)))
966 ((and state (> (nth 0 state) 0)) (cons 'tag (nth 1 state)))
967 (t (cons 'text text-start))))))
1c1d2eb6 968
1caf38eb
RS
969(defun sgml-beginning-of-tag (&optional top-level)
970 "Skip to beginning of tag and return its name.
1c1d2eb6
SM
971If this can't be done, return nil."
972 (let ((context (sgml-lexical-context)))
973 (if (eq (car context) 'tag)
974 (progn
975 (goto-char (cdr context))
976 (when (looking-at sgml-tag-name-re)
977 (match-string-no-properties 1)))
978 (if top-level nil
3fb819e5 979 (when (not (eq (car context) 'text))
1c1d2eb6
SM
980 (goto-char (cdr context))
981 (sgml-beginning-of-tag t))))))
1caf38eb
RS
982
983(defun sgml-value (alist)
347ea557 984 "Interactively insert value taken from attribute-rule ALIST.
5950e029 985See `sgml-tag-alist' for info about attribute rules."
1caf38eb
RS
986 (setq alist (cdr alist))
987 (if (stringp (car alist))
988 (insert "=\"" (car alist) ?\")
a3ec4ba0 989 (if (and (eq (car alist) t) (not sgml-xml-mode))
5950e029 990 (when (cdr alist)
73d25e52
SM
991 (insert "=\"")
992 (setq alist (skeleton-read '(completing-read "Value: " (cdr alist))))
993 (if (string< "" alist)
994 (insert alist ?\")
995 (delete-backward-char 2)))
1caf38eb 996 (insert "=\"")
5950e029
SS
997 (when alist
998 (insert (skeleton-read '(completing-read "Value: " alist))))
1caf38eb 999 (insert ?\"))))
64367655
SM
1000
1001(defun sgml-quote (start end &optional unquotep)
7492ed8e
SM
1002 "Quote SGML text in region START ... END.
1003Only &, < and > are quoted, the rest is left untouched.
1004With prefix argument UNQUOTEP, unquote the region."
1005 (interactive "r\nP")
1006 (save-restriction
1007 (narrow-to-region start end)
1008 (goto-char (point-min))
1009 (if unquotep
1010 ;; FIXME: We should unquote other named character references as well.
1011 (while (re-search-forward
1012 "\\(&\\(amp\\|\\(l\\|\\(g\\)\\)t\\)\\)[][<>&;\n\t \"%!'(),/=?]"
1013 nil t)
1014 (replace-match (if (match-end 4) ">" (if (match-end 3) "<" "&")) t t
1015 nil (if (eq (char-before (match-end 0)) ?\;) 0 1)))
1016 (while (re-search-forward "[&<>]" nil t)
1017 (replace-match (cdr (assq (char-before) '((?& . "&amp;")
1018 (?< . "&lt;")
1019 (?> . "&gt;"))))
1020 t t)))))
1021
1022(defun sgml-pretty-print (beg end)
1023 "Simple-minded pretty printer for SGML.
1024Re-indents the code and inserts newlines between BEG and END.
1025You might want to turn on `auto-fill-mode' to get better results."
1026 ;; TODO:
1027 ;; - insert newline between some start-tag and text.
1028 ;; - don't insert newline in front of some end-tags.
1029 (interactive "r")
1030 (save-excursion
1031 (if (< beg end)
1032 (goto-char beg)
1033 (goto-char end)
1034 (setq end beg)
1035 (setq beg (point)))
1036 ;; Don't use narrowing because it screws up auto-indent.
1037 (setq end (copy-marker end t))
1038 (with-syntax-table sgml-tag-syntax-table
1039 (while (re-search-forward "<" end t)
1040 (goto-char (match-beginning 0))
1041 (unless (or ;;(looking-at "</")
1042 (progn (skip-chars-backward " \t") (bolp)))
1043 (reindent-then-newline-and-indent))
1044 (forward-sexp 1)))
1045 ;; (indent-region beg end)
1046 ))
e1940c83 1047
2394187c
SM
1048\f
1049;; Parsing
1050
1051(defstruct (sgml-tag
1052 (:constructor sgml-make-tag (type start end name)))
1053 type start end name)
1054
1055(defsubst sgml-parse-tag-name ()
1056 "Skip past a tag-name, and return the name."
1057 (buffer-substring-no-properties
1058 (point) (progn (skip-syntax-forward "w_") (point))))
1059
41bfcbee
MW
1060(defsubst sgml-looking-back-at (str)
1061 "Return t if the test before point matches STR."
1062 (let ((start (- (point) (length str))))
80fc318e 1063 (and (>= start (point-min))
41bfcbee 1064 (equal str (buffer-substring-no-properties start (point))))))
2394187c 1065
e9146d5a
SM
1066(defun sgml-tag-text-p (start end)
1067 "Return non-nil if text between START and END is a tag.
1068Checks among other things that the tag does not contain spurious
1069unquoted < or > chars inside, which would indicate that it
1070really isn't a tag after all."
1071 (save-excursion
1072 (with-syntax-table sgml-tag-syntax-table
1073 (let ((pps (parse-partial-sexp start end 2)))
1074 (and (= (nth 0 pps) 0))))))
1075
4e7a42d2 1076(defun sgml-parse-tag-backward (&optional limit)
2394187c
SM
1077 "Parse an SGML tag backward, and return information about the tag.
1078Assume that parsing starts from within a textual context.
1079Leave point at the beginning of the tag."
e9146d5a
SM
1080 (catch 'found
1081 (let (tag-type tag-start tag-end name)
1082 (or (re-search-backward "[<>]" limit 'move)
1083 (error "No tag found"))
1084 (when (eq (char-after) ?<)
1085 ;; Oops!! Looks like we were not in a textual context after all!.
1086 ;; Let's try to recover.
1087 (with-syntax-table sgml-tag-syntax-table
1088 (let ((pos (point)))
1089 (condition-case nil
1090 (forward-sexp)
1091 (scan-error
1092 ;; This < seems to be just a spurious one, let's ignore it.
1093 (goto-char pos)
1094 (throw 'found (sgml-parse-tag-backward limit))))
1095 ;; Check it is really a tag, without any extra < or > inside.
1096 (unless (sgml-tag-text-p pos (point))
1097 (goto-char pos)
1098 (throw 'found (sgml-parse-tag-backward limit)))
1099 (forward-char -1))))
1100 (setq tag-end (1+ (point)))
1101 (cond
1102 ((sgml-looking-back-at "--") ; comment
1103 (setq tag-type 'comment
1104 tag-start (search-backward "<!--" nil t)))
1105 ((sgml-looking-back-at "]]") ; cdata
1106 (setq tag-type 'cdata
1107 tag-start (re-search-backward "<!\\[[A-Z]+\\[" nil t)))
1108 (t
1109 (setq tag-start
1110 (with-syntax-table sgml-tag-syntax-table
1111 (goto-char tag-end)
1112 (condition-case nil
1113 (backward-sexp)
1114 (scan-error
1115 ;; This > isn't really the end of a tag. Skip it.
1116 (goto-char (1- tag-end))
1117 (throw 'found (sgml-parse-tag-backward limit))))
1118 (point)))
1119 (goto-char (1+ tag-start))
1120 (case (char-after)
1121 (?! ; declaration
1122 (setq tag-type 'decl))
1123 (?? ; processing-instruction
1124 (setq tag-type 'pi))
1125 (?/ ; close-tag
1126 (forward-char 1)
1127 (setq tag-type 'close
1128 name (sgml-parse-tag-name)))
1129 (?% ; JSP tags
1130 (setq tag-type 'jsp))
1131 (t ; open or empty tag
1132 (setq tag-type 'open
1133 name (sgml-parse-tag-name))
1134 (if (or (eq ?/ (char-before (- tag-end 1)))
1135 (sgml-empty-tag-p name))
1136 (setq tag-type 'empty))))))
1137 (goto-char tag-start)
1138 (sgml-make-tag tag-type tag-start tag-end name))))
2394187c 1139
59444a9c 1140(defun sgml-get-context (&optional until)
2394187c 1141 "Determine the context of the current position.
59444a9c
SM
1142By default, parse until we find a start-tag as the first thing on a line.
1143If UNTIL is `empty', return even if the context is empty (i.e.
2394187c 1144we just skipped over some element and got to a beginning of line).
2394187c
SM
1145
1146The context is a list of tag-info structures. The last one is the tag
59444a9c
SM
1147immediately enclosing the current position.
1148
1149Point is assumed to be outside of any tag. If we discover that it's
1150not the case, the first tag returned is the one inside which we are."
2394187c 1151 (let ((here (point))
ed8031f2 1152 (stack nil)
2394187c
SM
1153 (ignore nil)
1154 (context nil)
1155 tag-info)
1156 ;; CONTEXT keeps track of the tag-stack
ed8031f2
SM
1157 ;; STACK keeps track of the end tags we've seen (and thus the start-tags
1158 ;; we'll have to ignore) when skipping over matching open..close pairs.
1159 ;; IGNORE is a list of tags that can be ignored because they have been
1160 ;; closed implicitly.
2394187c
SM
1161 (skip-chars-backward " \t\n") ; Make sure we're not at indentation.
1162 (while
59444a9c 1163 (and (not (eq until 'now))
ed8031f2 1164 (or stack
59444a9c 1165 (not (if until (eq until 'empty) context))
2394187c
SM
1166 (not (sgml-at-indentation-p))
1167 (and context
1168 (/= (point) (sgml-tag-start (car context)))
59444a9c 1169 (sgml-unclosed-tag-p (sgml-tag-name (car context)))))
2394187c 1170 (setq tag-info (ignore-errors (sgml-parse-tag-backward))))
a9d4efa2 1171
2394187c
SM
1172 ;; This tag may enclose things we thought were tags. If so,
1173 ;; discard them.
1174 (while (and context
1175 (> (sgml-tag-end tag-info)
1176 (sgml-tag-end (car context))))
1177 (setq context (cdr context)))
a9d4efa2 1178
2394187c 1179 (cond
59444a9c
SM
1180 ((> (sgml-tag-end tag-info) here)
1181 ;; Oops!! Looks like we were not outside of any tag, after all.
1182 (push tag-info context)
1183 (setq until 'now))
2394187c 1184
2394187c
SM
1185 ;; start-tag
1186 ((eq (sgml-tag-type tag-info) 'open)
1187 (cond
ed8031f2
SM
1188 ((null stack)
1189 (if (member-ignore-case (sgml-tag-name tag-info) ignore)
2394187c
SM
1190 ;; There was an implicit end-tag.
1191 nil
ed8031f2
SM
1192 (push tag-info context)
1193 ;; We're changing context so the tags implicitly closed inside
1194 ;; the previous context aren't implicitly closed here any more.
1195 ;; [ Well, actually it depends, but we don't have the info about
1196 ;; when it doesn't and when it does. --Stef ]
1197 (setq ignore nil)))
2394187c 1198 ((eq t (compare-strings (sgml-tag-name tag-info) nil nil
ed8031f2
SM
1199 (car stack) nil nil t))
1200 (setq stack (cdr stack)))
2394187c
SM
1201 (t
1202 ;; The open and close tags don't match.
1203 (if (not sgml-xml-mode)
2394187c 1204 (unless (sgml-unclosed-tag-p (sgml-tag-name tag-info))
7492ed8e 1205 (message "Unclosed tag <%s>" (sgml-tag-name tag-info))
ed8031f2 1206 (let ((tmp stack))
7492ed8e
SM
1207 ;; We could just assume that the tag is simply not closed
1208 ;; but it's a bad assumption when tags *are* closed but
1209 ;; not properly nested.
1210 (while (and (cdr tmp)
1211 (not (eq t (compare-strings
1212 (sgml-tag-name tag-info) nil nil
1213 (cadr tmp) nil nil t))))
1214 (setq tmp (cdr tmp)))
1215 (if (cdr tmp) (setcdr tmp (cddr tmp)))))
2394187c 1216 (message "Unmatched tags <%s> and </%s>"
ed8031f2
SM
1217 (sgml-tag-name tag-info) (pop stack)))))
1218
1219 (if (and (null stack) (sgml-unclosed-tag-p (sgml-tag-name tag-info)))
1220 ;; This is a top-level open of an implicitly closed tag, so any
1221 ;; occurrence of such an open tag at the same level can be ignored
1222 ;; because it's been implicitly closed.
1223 (push (sgml-tag-name tag-info) ignore)))
2394187c
SM
1224
1225 ;; end-tag
1226 ((eq (sgml-tag-type tag-info) 'close)
1227 (if (sgml-empty-tag-p (sgml-tag-name tag-info))
1228 (message "Spurious </%s>: empty tag" (sgml-tag-name tag-info))
ed8031f2 1229 (push (sgml-tag-name tag-info) stack)))
2394187c
SM
1230 ))
1231
1232 ;; return context
1233 context))
1234
1235(defun sgml-show-context (&optional full)
1236 "Display the current context.
1237If FULL is non-nil, parse back to the beginning of the buffer."
1238 (interactive "P")
1239 (with-output-to-temp-buffer "*XML Context*"
7492ed8e
SM
1240 (save-excursion
1241 (let ((context (sgml-get-context)))
1242 (when full
1243 (let ((more nil))
1244 (while (setq more (sgml-get-context))
1245 (setq context (nconc more context)))))
1246 (pp context)))))
2394187c
SM
1247
1248\f
1249;; Editing shortcuts
1250
f6ab0573 1251(defun sgml-close-tag ()
4e7a42d2
SM
1252 "Close current element.
1253Depending on context, inserts a matching close-tag, or closes
1254the current start-tag or the current comment or the current cdata, ..."
2394187c 1255 (interactive)
f6ab0573
MW
1256 (case (car (sgml-lexical-context))
1257 (comment (insert " -->"))
1258 (cdata (insert "]]>"))
1259 (pi (insert " ?>"))
1260 (jsp (insert " %>"))
1261 (tag (insert " />"))
1262 (text
1263 (let ((context (save-excursion (sgml-get-context))))
1264 (if context
2871b07a 1265 (progn
f6ab0573
MW
1266 (insert "</" (sgml-tag-name (car (last context))) ">")
1267 (indent-according-to-mode)))))
1268 (otherwise
1269 (error "Nothing to close"))))
2394187c 1270
347ea557
MW
1271(defun sgml-empty-tag-p (tag-name)
1272 "Return non-nil if TAG-NAME is an implicitly empty tag."
1273 (and (not sgml-xml-mode)
1274 (member-ignore-case tag-name sgml-empty-tags)))
1275
1276(defun sgml-unclosed-tag-p (tag-name)
1277 "Return non-nil if TAG-NAME is a tag for which an end-tag is optional."
1278 (and (not sgml-xml-mode)
1279 (member-ignore-case tag-name sgml-unclosed-tags)))
1280
59444a9c
SM
1281(defun sgml-calculate-indent (&optional lcon)
1282 "Calculate the column to which this line should be indented.
1283LCON is the lexical context, if any."
1284 (unless lcon (setq lcon (sgml-lexical-context)))
1285
1286 ;; Indent comment-start markers inside <!-- just like comment-end markers.
1287 (if (and (eq (car lcon) 'tag)
1288 (looking-at "--")
1289 (save-excursion (goto-char (cdr lcon)) (looking-at "<!--")))
1290 (setq lcon (cons 'comment (+ (cdr lcon) 2))))
1291
1292 (case (car lcon)
1293
1294 (string
1295 ;; Go back to previous non-empty line.
1296 (while (and (> (point) (cdr lcon))
1297 (zerop (forward-line -1))
1298 (looking-at "[ \t]*$")))
1299 (if (> (point) (cdr lcon))
1300 ;; Previous line is inside the string.
1301 (current-indentation)
1302 (goto-char (cdr lcon))
1303 (1+ (current-column))))
1304
1305 (comment
1306 (let ((mark (looking-at "--")))
1c1d2eb6
SM
1307 ;; Go back to previous non-empty line.
1308 (while (and (> (point) (cdr lcon))
1309 (zerop (forward-line -1))
59444a9c
SM
1310 (or (looking-at "[ \t]*$")
1311 (if mark (not (looking-at "[ \t]*--"))))))
1c1d2eb6 1312 (if (> (point) (cdr lcon))
59444a9c
SM
1313 ;; Previous line is inside the comment.
1314 (skip-chars-forward " \t")
1c1d2eb6 1315 (goto-char (cdr lcon))
59444a9c
SM
1316 ;; Skip `<!' to get to the `--' with which we want to align.
1317 (search-forward "--")
1318 (goto-char (match-beginning 0)))
1319 (when (and (not mark) (looking-at "--"))
1320 (forward-char 2) (skip-chars-forward " \t"))
1321 (current-column)))
1322
1323 ;; We don't know how to indent it. Let's be honest about it.
1324 (cdata nil)
1325
1326 (tag
1327 (goto-char (1+ (cdr lcon)))
1328 (skip-chars-forward "^ \t\n") ;Skip tag name.
1329 (skip-chars-forward " \t")
1330 (if (not (eolp))
1331 (current-column)
1332 ;; This is the first attribute: indent.
1c1d2eb6 1333 (goto-char (1+ (cdr lcon)))
59444a9c
SM
1334 (+ (current-column) sgml-basic-offset)))
1335
1336 (text
1337 (while (looking-at "</")
1338 (forward-sexp 1)
1339 (skip-chars-forward " \t"))
1340 (let* ((here (point))
1341 (unclosed (and ;; (not sgml-xml-mode)
1342 (looking-at sgml-tag-name-re)
1343 (member-ignore-case (match-string 1)
1344 sgml-unclosed-tags)
1345 (match-string 1)))
1346 (context
1347 ;; If possible, align on the previous non-empty text line.
1348 ;; Otherwise, do a more serious parsing to find the
1349 ;; tag(s) relative to which we should be indenting.
1350 (if (and (not unclosed) (skip-chars-backward " \t")
1351 (< (skip-chars-backward " \t\n") 0)
1352 (back-to-indentation)
1353 (> (point) (cdr lcon)))
1354 nil
1355 (goto-char here)
1356 (nreverse (sgml-get-context (if unclosed nil 'empty)))))
1357 (there (point)))
1358 ;; Ignore previous unclosed start-tag in context.
1359 (while (and context unclosed
1360 (eq t (compare-strings
1361 (sgml-tag-name (car context)) nil nil
1362 unclosed nil nil t)))
1363 (setq context (cdr context)))
1364 ;; Indent to reflect nesting.
1365 (cond
1366 ;; If we were not in a text context after all, let's try again.
1367 ((and context (> (sgml-tag-end (car context)) here))
1368 (goto-char here)
1369 (sgml-calculate-indent
1370 (cons (if (memq (sgml-tag-type (car context)) '(comment cdata))
1371 (sgml-tag-type (car context)) 'tag)
1372 (sgml-tag-start (car context)))))
1373 ;; Align on the first element after the nearest open-tag, if any.
1374 ((and context
1375 (goto-char (sgml-tag-end (car context)))
1376 (skip-chars-forward " \t\n")
1377 (< (point) here) (sgml-at-indentation-p))
1378 (current-column))
1379 (t
1380 (goto-char there)
1381 (+ (current-column)
1382 (* sgml-basic-offset (length context)))))))
1383
1384 (otherwise
1385 (error "Unrecognised context %s" (car lcon)))
1386
1387 ))
1c1d2eb6
SM
1388
1389(defun sgml-indent-line ()
1390 "Indent the current line as SGML."
1391 (interactive)
1392 (let* ((savep (point))
1393 (indent-col
1394 (save-excursion
5f3d924d 1395 (back-to-indentation)
1c1d2eb6 1396 (if (>= (point) savep) (setq savep nil))
1c1d2eb6 1397 (sgml-calculate-indent))))
59444a9c
SM
1398 (if (null indent-col)
1399 'noindent
1400 (if savep
1401 (save-excursion (indent-line-to indent-col))
1402 (indent-line-to indent-col)))))
1c1d2eb6 1403
2871b07a
MW
1404(defun sgml-guess-indent ()
1405 "Guess an appropriate value for `sgml-basic-offset'.
1406Base the guessed identation level on the first indented tag in the buffer.
1407Add this to `sgml-mode-hook' for convenience."
1408 (interactive)
1409 (save-excursion
1410 (goto-char (point-min))
232dbe4f 1411 (if (re-search-forward "^\\([ \t]+\\)<" 500 'noerror)
2871b07a
MW
1412 (progn
1413 (set (make-local-variable 'sgml-basic-offset)
1c8438ab 1414 (1- (current-column)))
2871b07a
MW
1415 (message "Guessed sgml-basic-offset = %d"
1416 sgml-basic-offset)
1417 ))))
1418
5f3d924d
SM
1419(defun sgml-parse-dtd ()
1420 "Simplistic parse of the current buffer as a DTD.
1421Currently just returns (EMPTY-TAGS UNCLOSED-TAGS)."
1422 (goto-char (point-min))
1423 (let ((empty nil)
1424 (unclosed nil))
1425 (while (re-search-forward "<!ELEMENT[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+[-O][ \t\n]+\\([-O]\\)[ \t\n]+\\([^ \t\n]+\\)" nil t)
1426 (cond
1427 ((string= (match-string 3) "EMPTY")
1428 (push (match-string-no-properties 1) empty))
1429 ((string= (match-string 2) "O")
1430 (push (match-string-no-properties 1) unclosed))))
1431 (setq empty (sort (mapcar 'downcase empty) 'string<))
1432 (setq unclosed (sort (mapcar 'downcase unclosed) 'string<))
1433 (list empty unclosed)))
1434
e1940c83
SM
1435;;; HTML mode
1436
d4c89075
DL
1437(defcustom html-mode-hook nil
1438 "Hook run by command `html-mode'.
1439`text-mode-hook' and `sgml-mode-hook' are run first."
1440 :group 'sgml
1441 :type 'hook
1442 :options '(html-autoview-mode))
1443
fcc3195e 1444(defvar html-quick-keys sgml-quick-keys
b1e7bb48 1445 "Use C-c X combinations for quick insertion of frequent tags when non-nil.
fcc3195e 1446This defaults to `sgml-quick-keys'.
1caf38eb
RS
1447This takes effect when first loading the library.")
1448
1449(defvar html-mode-map
5f5c9e79 1450 (let ((map (make-sparse-keymap))
1caf38eb 1451 (menu-map (make-sparse-keymap "HTML")))
5f5c9e79 1452 (set-keymap-parent map sgml-mode-map)
7e49eef2
RS
1453 (define-key map "\C-c6" 'html-headline-6)
1454 (define-key map "\C-c5" 'html-headline-5)
1455 (define-key map "\C-c4" 'html-headline-4)
1456 (define-key map "\C-c3" 'html-headline-3)
1457 (define-key map "\C-c2" 'html-headline-2)
1458 (define-key map "\C-c1" 'html-headline-1)
fcc3195e
RS
1459 (define-key map "\C-c\r" 'html-paragraph)
1460 (define-key map "\C-c\n" 'html-line)
1461 (define-key map "\C-c\C-c-" 'html-horizontal-rule)
7e49eef2
RS
1462 (define-key map "\C-c\C-co" 'html-ordered-list)
1463 (define-key map "\C-c\C-cu" 'html-unordered-list)
fcc3195e
RS
1464 (define-key map "\C-c\C-cr" 'html-radio-buttons)
1465 (define-key map "\C-c\C-cc" 'html-checkboxes)
1466 (define-key map "\C-c\C-cl" 'html-list-item)
1467 (define-key map "\C-c\C-ch" 'html-href-anchor)
1468 (define-key map "\C-c\C-cn" 'html-name-anchor)
1469 (define-key map "\C-c\C-ci" 'html-image)
5950e029
SS
1470 (when html-quick-keys
1471 (define-key map "\C-c-" 'html-horizontal-rule)
1472 (define-key map "\C-co" 'html-ordered-list)
1473 (define-key map "\C-cu" 'html-unordered-list)
1474 (define-key map "\C-cr" 'html-radio-buttons)
1475 (define-key map "\C-cc" 'html-checkboxes)
1476 (define-key map "\C-cl" 'html-list-item)
1477 (define-key map "\C-ch" 'html-href-anchor)
1478 (define-key map "\C-cn" 'html-name-anchor)
1479 (define-key map "\C-ci" 'html-image))
1caf38eb
RS
1480 (define-key map "\C-c\C-s" 'html-autoview-mode)
1481 (define-key map "\C-c\C-v" 'browse-url-of-buffer)
1482 (define-key map [menu-bar html] (cons "HTML" menu-map))
1483 (define-key menu-map [html-autoview-mode]
1484 '("Toggle Autoviewing" . html-autoview-mode))
1485 (define-key menu-map [browse-url-of-buffer]
1486 '("View Buffer Contents" . browse-url-of-buffer))
1487 (define-key menu-map [nil] '("--"))
7e49eef2
RS
1488 ;;(define-key menu-map "6" '("Heading 6" . html-headline-6))
1489 ;;(define-key menu-map "5" '("Heading 5" . html-headline-5))
1490 ;;(define-key menu-map "4" '("Heading 4" . html-headline-4))
1491 (define-key menu-map "3" '("Heading 3" . html-headline-3))
1492 (define-key menu-map "2" '("Heading 2" . html-headline-2))
1493 (define-key menu-map "1" '("Heading 1" . html-headline-1))
1caf38eb 1494 (define-key menu-map "l" '("Radio Buttons" . html-radio-buttons))
fcc3195e 1495 (define-key menu-map "c" '("Checkboxes" . html-checkboxes))
1caf38eb 1496 (define-key menu-map "l" '("List Item" . html-list-item))
7e49eef2
RS
1497 (define-key menu-map "u" '("Unordered List" . html-unordered-list))
1498 (define-key menu-map "o" '("Ordered List" . html-ordered-list))
fcc3195e 1499 (define-key menu-map "-" '("Horizontal Rule" . html-horizontal-rule))
1caf38eb
RS
1500 (define-key menu-map "\n" '("Line Break" . html-line))
1501 (define-key menu-map "\r" '("Paragraph" . html-paragraph))
1502 (define-key menu-map "i" '("Image" . html-image))
1503 (define-key menu-map "h" '("Href Anchor" . html-href-anchor))
1504 (define-key menu-map "n" '("Name Anchor" . html-name-anchor))
1505 map)
1506 "Keymap for commands for use in HTML mode.")
1507
1caf38eb
RS
1508(defvar html-face-tag-alist
1509 '((bold . "b")
1510 (italic . "i")
1511 (underline . "u")
1512 (modeline . "rev"))
1513 "Value of `sgml-face-tag-alist' for HTML mode.")
1514
1515(defvar html-tag-face-alist
1516 '(("b" . bold)
1517 ("big" . bold)
1518 ("blink" . highlight)
1519 ("cite" . italic)
1520 ("em" . italic)
1521 ("h1" bold underline)
1522 ("h2" bold-italic underline)
1523 ("h3" italic underline)
1524 ("h4" . underline)
1525 ("h5" . underline)
1526 ("h6" . underline)
1527 ("i" . italic)
1528 ("rev" . modeline)
1529 ("s" . underline)
1530 ("small" . default)
1531 ("strong" . bold)
1532 ("title" bold underline)
1533 ("tt" . default)
1534 ("u" . underline)
1535 ("var" . italic))
1536 "Value of `sgml-tag-face-alist' for HTML mode.")
1537
1caf38eb
RS
1538(defvar html-display-text
1539 '((img . "[/]")
1540 (hr . "----------")
1541 (li . "o "))
1542 "Value of `sgml-display-text' for HTML mode.")
b4f05c38 1543
9d4ce428 1544\f
3bf0b727 1545;; should code exactly HTML 3 here when that is finished
1caf38eb 1546(defvar html-tag-alist
d10447ba 1547 (let* ((1-7 '(("1") ("2") ("3") ("4") ("5") ("6") ("7")))
e1940c83 1548 (1-9 `(,@1-7 ("8") ("9")))
1caf38eb
RS
1549 (align '(("align" ("left") ("center") ("right"))))
1550 (valign '(("top") ("middle") ("bottom") ("baseline")))
1551 (rel '(("next") ("previous") ("parent") ("subdocument") ("made")))
1552 (href '("href" ("ftp:") ("file:") ("finger:") ("gopher:") ("http:")
1553 ("mailto:") ("news:") ("rlogin:") ("telnet:") ("tn3270:")
fcc3195e 1554 ("wais:") ("/cgi-bin/")))
1caf38eb
RS
1555 (name '("name"))
1556 (link `(,href
1557 ("rel" ,@rel)
1558 ("rev" ,@rel)
1559 ("title")))
b4f05c38 1560 (list '((nil \n ("List item: " "<li>" str
a3ec4ba0 1561 (if sgml-xml-mode "</li>") \n))))
1caf38eb 1562 (cell `(t
e1940c83 1563 ,@align
1caf38eb
RS
1564 ("valign" ,@valign)
1565 ("colspan" ,@1-9)
1566 ("rowspan" ,@1-9)
1567 ("nowrap" t))))
1568 ;; put ,-expressions first, else byte-compile chokes (as of V19.29)
1569 ;; and like this it's more efficient anyway
1570 `(("a" ,name ,@link)
1571 ("base" t ,@href)
1572 ("dir" ,@list)
d10447ba 1573 ("font" nil "size" ("-1") ("+1") ("-2") ("+2") ,@1-7)
73d25e52 1574 ("form" (\n _ \n "<input type=\"submit\" value=\"\""
a3ec4ba0 1575 (if sgml-xml-mode "/>" ">"))
fcc3195e 1576 ("action" ,@(cdr href)) ("method" ("get") ("post")))
1caf38eb
RS
1577 ("h1" ,@align)
1578 ("h2" ,@align)
1579 ("h3" ,@align)
1580 ("h4" ,@align)
1581 ("h5" ,@align)
1582 ("h6" ,@align)
1583 ("hr" t ("size" ,@1-9) ("width") ("noshade" t) ,@align)
1584 ("img" t ("align" ,@valign ("texttop") ("absmiddle") ("absbottom"))
1585 ("src") ("alt") ("width" "1") ("height" "1")
1586 ("border" "1") ("vspace" "1") ("hspace" "1") ("ismap" t))
1587 ("input" t ("size" ,@1-9) ("maxlength" ,@1-9) ("checked" t) ,name
fcc3195e
RS
1588 ("type" ("text") ("password") ("checkbox") ("radio")
1589 ("submit") ("reset"))
1caf38eb
RS
1590 ("value"))
1591 ("link" t ,@link)
1592 ("menu" ,@list)
d10447ba 1593 ("ol" ,@list ("type" ("A") ("a") ("I") ("i") ("1")))
1caf38eb
RS
1594 ("p" t ,@align)
1595 ("select" (nil \n
1596 ("Text: "
a3ec4ba0 1597 "<option>" str (if sgml-xml-mode "</option>") \n))
1caf38eb
RS
1598 ,name ("size" ,@1-9) ("multiple" t))
1599 ("table" (nil \n
1600 ((completing-read "Cell kind: " '(("td") ("th"))
1601 nil t "t")
73d25e52 1602 "<tr><" str ?> _
a3ec4ba0 1603 (if sgml-xml-mode (concat "<" str "></tr>")) \n))
1caf38eb
RS
1604 ("border" t ,@1-9) ("width" "10") ("cellpadding"))
1605 ("td" ,@cell)
1606 ("textarea" ,name ("rows" ,@1-9) ("cols" ,@1-9))
1607 ("th" ,@cell)
d10447ba 1608 ("ul" ,@list ("type" ("disc") ("circle") ("square")))
1caf38eb
RS
1609
1610 ,@sgml-tag-alist
1611
1612 ("abbrev")
1613 ("acronym")
1614 ("address")
1615 ("array" (nil \n
a3ec4ba0 1616 ("Item: " "<item>" str (if sgml-xml-mode "</item>") \n))
1caf38eb
RS
1617 "align")
1618 ("au")
1619 ("b")
1620 ("big")
1621 ("blink")
1622 ("blockquote" \n)
1623 ("body" \n ("background" ".gif") ("bgcolor" "#") ("text" "#")
1624 ("link" "#") ("alink" "#") ("vlink" "#"))
a3ec4ba0 1625 ("box" (nil _ "<over>" _ (if sgml-xml-mode "</over>")))
1caf38eb
RS
1626 ("br" t ("clear" ("left") ("right")))
1627 ("caption" ("valign" ("top") ("bottom")))
1628 ("center" \n)
1629 ("cite")
1630 ("code" \n)
a3ec4ba0 1631 ("dd" ,(not sgml-xml-mode))
1caf38eb
RS
1632 ("del")
1633 ("dfn")
e1940c83 1634 ("div")
1caf38eb
RS
1635 ("dl" (nil \n
1636 ( "Term: "
a3ec4ba0
SM
1637 "<dt>" str (if sgml-xml-mode "</dt>")
1638 "<dd>" _ (if sgml-xml-mode "</dd>") \n)))
1639 ("dt" (t _ (if sgml-xml-mode "</dt>")
1640 "<dd>" (if sgml-xml-mode "</dd>") \n))
1caf38eb 1641 ("em")
d10447ba 1642 ;("fn" "id" "fn") ; ???
1caf38eb
RS
1643 ("head" \n)
1644 ("html" (\n
1645 "<head>\n"
1646 "<title>" (setq str (read-input "Title: ")) "</title>\n"
5e532c5c 1647 "</head>\n"
1caf38eb
RS
1648 "<body>\n<h1>" str "</h1>\n" _
1649 "\n<address>\n<a href=\"mailto:"
be047262 1650 user-mail-address
5e532c5c
RS
1651 "\">" (user-full-name) "</a>\n</address>\n"
1652 "</body>"
1653 ))
1caf38eb
RS
1654 ("i")
1655 ("ins")
1656 ("isindex" t ("action") ("prompt"))
1657 ("kbd")
1658 ("lang")
a3ec4ba0 1659 ("li" ,(not sgml-xml-mode))
1caf38eb
RS
1660 ("math" \n)
1661 ("nobr")
1662 ("option" t ("value") ("label") ("selected" t))
1663 ("over" t)
1664 ("person")
1665 ("pre" \n)
1666 ("q")
1667 ("rev")
1668 ("s")
1669 ("samp")
1670 ("small")
64367655
SM
1671 ("span" nil
1672 ("class"
1673 ("builtin")
1674 ("comment")
1675 ("constant")
1676 ("function-name")
1677 ("keyword")
1678 ("string")
1679 ("type")
1680 ("variable-name")
1681 ("warning")))
1caf38eb
RS
1682 ("strong")
1683 ("sub")
1684 ("sup")
1685 ("title")
1686 ("tr" t)
1687 ("tt")
1688 ("u")
1689 ("var")
1690 ("wbr" t)))
1691 "*Value of `sgml-tag-alist' for HTML mode.")
1692
1693(defvar html-tag-help
1694 `(,@sgml-tag-help
1695 ("a" . "Anchor of point or link elsewhere")
1696 ("abbrev" . "?")
1697 ("acronym" . "?")
1698 ("address" . "Formatted mail address")
1699 ("array" . "Math array")
1700 ("au" . "?")
1701 ("b" . "Bold face")
1702 ("base" . "Base address for URLs")
1703 ("big" . "Font size")
1704 ("blink" . "Blinking text")
1705 ("blockquote" . "Indented quotation")
1706 ("body" . "Document body")
1707 ("box" . "Math fraction")
1708 ("br" . "Line break")
1709 ("caption" . "Table caption")
1710 ("center" . "Centered text")
1711 ("changed" . "Change bars")
1712 ("cite" . "Citation of a document")
1713 ("code" . "Formatted source code")
1714 ("dd" . "Definition of term")
1715 ("del" . "?")
1716 ("dfn" . "?")
1717 ("dir" . "Directory list (obsolete)")
1718 ("dl" . "Definition list")
1719 ("dt" . "Term to be definined")
b4f05c38 1720 ("em" . "Emphasised")
1caf38eb
RS
1721 ("embed" . "Embedded data in foreign format")
1722 ("fig" . "Figure")
1723 ("figa" . "Figure anchor")
1724 ("figd" . "Figure description")
1725 ("figt" . "Figure text")
d10447ba 1726 ;("fn" . "?") ; ???
1caf38eb
RS
1727 ("font" . "Font size")
1728 ("form" . "Form with input fields")
1729 ("group" . "Document grouping")
1730 ("h1" . "Most important section headline")
1731 ("h2" . "Important section headline")
1732 ("h3" . "Section headline")
1733 ("h4" . "Minor section headline")
1734 ("h5" . "Unimportant section headline")
1735 ("h6" . "Least important section headline")
1736 ("head" . "Document header")
1737 ("hr" . "Horizontal rule")
1738 ("html" . "HTML Document")
1739 ("i" . "Italic face")
1740 ("img" . "Graphic image")
1741 ("input" . "Form input field")
1742 ("ins" . "?")
1743 ("isindex" . "Input field for index search")
1744 ("kbd" . "Keybard example face")
1745 ("lang" . "Natural language")
1746 ("li" . "List item")
1747 ("link" . "Link relationship")
1748 ("math" . "Math formula")
1749 ("menu" . "Menu list (obsolete)")
1750 ("mh" . "Form mail header")
1751 ("nextid" . "Allocate new id")
1752 ("nobr" . "Text without line break")
1753 ("ol" . "Ordered list")
1754 ("option" . "Selection list item")
1755 ("over" . "Math fraction rule")
1756 ("p" . "Paragraph start")
1757 ("panel" . "Floating panel")
1758 ("person" . "?")
1759 ("pre" . "Preformatted fixed width text")
1760 ("q" . "?")
1761 ("rev" . "Reverse video")
1762 ("s" . "?")
1763 ("samp" . "Sample text")
1764 ("select" . "Selection list")
1765 ("small" . "Font size")
1766 ("sp" . "Nobreak space")
1767 ("strong" . "Standout text")
1768 ("sub" . "Subscript")
1769 ("sup" . "Superscript")
1770 ("table" . "Table with rows and columns")
1771 ("tb" . "Table vertical break")
1772 ("td" . "Table data cell")
1773 ("textarea" . "Form multiline edit area")
1774 ("th" . "Table header cell")
1775 ("title" . "Document title")
1776 ("tr" . "Table row separator")
1777 ("tt" . "Typewriter face")
1778 ("u" . "Underlined text")
1779 ("ul" . "Unordered list")
1780 ("var" . "Math variable face")
1781 ("wbr" . "Enable <br> within <nobr>"))
1782"*Value of `sgml-tag-help' for HTML mode.")
9d4ce428 1783
3bf0b727 1784\f
1caf38eb 1785;;;###autoload
64367655 1786(define-derived-mode html-mode sgml-mode "HTML"
1caf38eb 1787 "Major mode based on SGML mode for editing HTML documents.
7be38f7d 1788This allows inserting skeleton constructs used in hypertext documents with
fcc3195e
RS
1789completion. See below for an introduction to HTML. Use
1790\\[browse-url-of-buffer] to see how this comes out. See also `sgml-mode' on
1791which this is based.
1caf38eb 1792
fcc3195e 1793Do \\[describe-variable] html- SPC and \\[describe-variable] sgml- SPC to see available variables.
1caf38eb
RS
1794
1795To write fairly well formatted pages you only need to know few things. Most
1796browsers have a function to read the source code of the page being seen, so
1797you can imitate various tricks. Here's a very short HTML primer which you
1798can also view with a browser to see what happens:
1799
1800<title>A Title Describing Contents</title> should be on every page. Pages can
1801have <h1>Very Major Headlines</h1> through <h6>Very Minor Headlines</h6>
1802<hr> Parts can be separated with horizontal rules.
1803
1804<p>Paragraphs only need an opening tag. Line breaks and multiple spaces are
1805ignored unless the text is <pre>preformatted.</pre> Text can be marked as
1806<b>bold</b>, <i>italic</i> or <u>underlined</u> using the normal M-g or
1807Edit/Text Properties/Face commands.
1808
1809Pages can have <a name=\"SOMENAME\">named points</a> and can link other points
1810to them with <a href=\"#SOMENAME\">see also somename</a>. In the same way <a
1811href=\"URL\">see also URL</a> where URL is a filename relative to current
f788776c 1812directory, or absolute as in `http://www.cs.indiana.edu/elisp/w3/docs.html'.
1caf38eb
RS
1813
1814Images in many formats can be inlined with <img src=\"URL\">.
1815
f788776c
RS
1816If you mainly create your own documents, `sgml-specials' might be
1817interesting. But note that some HTML 2 browsers can't handle `&apos;'.
1818To work around that, do:
1819 (eval-after-load \"sgml-mode\" '(aset sgml-char-names ?' nil))
1caf38eb 1820
1caf38eb 1821\\{html-mode-map}"
64367655
SM
1822 (set (make-local-variable 'sgml-display-text) html-display-text)
1823 (set (make-local-variable 'sgml-tag-face-alist) html-tag-face-alist)
1caf38eb
RS
1824 (make-local-variable 'sgml-tag-alist)
1825 (make-local-variable 'sgml-face-tag-alist)
1826 (make-local-variable 'sgml-tag-help)
1827 (make-local-variable 'outline-regexp)
1828 (make-local-variable 'outline-heading-end-regexp)
1829 (make-local-variable 'outline-level)
da84bdc4
RS
1830 (make-local-variable 'sentence-end)
1831 (setq sentence-end
b8b14971
DL
1832 (if sentence-end-double-space
1833 "[.?!][]\"')}]*\\(<[^>]*>\\)*\\($\\| $\\|\t\\| \\)[ \t\n]*"
64367655 1834 "[.?!][]\"')}]*\\(<[^>]*>\\)*\\($\\|[ \t]\\)[ \t\n]*"))
a01588fc 1835 (setq sgml-tag-alist html-tag-alist
1caf38eb
RS
1836 sgml-face-tag-alist html-face-tag-alist
1837 sgml-tag-help html-tag-help
1838 outline-regexp "^.*<[Hh][1-6]\\>"
1839 outline-heading-end-regexp "</[Hh][1-6]>"
1840 outline-level (lambda ()
0fda8eff 1841 (char-before (match-end 0))))
3bf0b727 1842 (setq imenu-create-index-function 'html-imenu-index)
a3ec4ba0 1843 (when sgml-xml-mode (setq mode-name "XHTML"))
73d25e52 1844 (set (make-local-variable 'sgml-empty-tags)
5f3d924d
SM
1845 ;; From HTML-4.01's loose.dtd, parsed with `sgml-parse-dtd',
1846 ;; plus manual addition of "wbr".
1847 '("area" "base" "basefont" "br" "col" "frame" "hr" "img" "input"
1848 "isindex" "link" "meta" "param" "wbr"))
1849 (set (make-local-variable 'sgml-unclosed-tags)
1850 ;; From HTML-4.01's loose.dtd, parsed with `sgml-parse-dtd'.
1851 '("body" "colgroup" "dd" "dt" "head" "html" "li" "option"
1852 "p" "tbody" "td" "tfoot" "th" "thead" "tr"))
e1940c83
SM
1853 ;; It's for the user to decide if it defeats it or not -stef
1854 ;; (make-local-variable 'imenu-sort-function)
1855 ;; (setq imenu-sort-function nil) ; sorting the menu defeats the purpose
64367655 1856 )
9d4ce428 1857
3bf0b727
RS
1858(defvar html-imenu-regexp
1859 "\\s-*<h\\([1-9]\\)[^\n<>]*>\\(<[^\n<>]*>\\)*\\s-*\\([^\n<>]*\\)"
1860 "*A regular expression matching a head line to be added to the menu.
1861The first `match-string' should be a number from 1-9.
1862The second `match-string' matches extra tags and is ignored.
1863The third `match-string' will be the used in the menu.")
1864
1865(defun html-imenu-index ()
a9d4efa2 1866 "Return a table of contents for an HTML buffer for use with Imenu."
3bf0b727
RS
1867 (let (toc-index)
1868 (save-excursion
1869 (goto-char (point-min))
1870 (while (re-search-forward html-imenu-regexp nil t)
1871 (setq toc-index
1872 (cons (cons (concat (make-string
1873 (* 2 (1- (string-to-number (match-string 1))))
1874 ?\ )
1875 (match-string 3))
5950e029 1876 (line-beginning-position))
3bf0b727
RS
1877 toc-index))))
1878 (nreverse toc-index)))
1caf38eb 1879
4e7a42d2 1880(define-minor-mode html-autoview-mode
d4c89075 1881 "Toggle automatic viewing via `browse-url-of-buffer' upon saving buffer.
3bf0b727
RS
1882With positive prefix ARG always turns viewing on, with negative ARG always off.
1883Can be used as a value for `html-mode-hook'."
4e7a42d2 1884 nil nil nil
966cdb22 1885 :group 'sgml
4e7a42d2
SM
1886 (if html-autoview-mode
1887 (add-hook 'after-save-hook 'browse-url-of-buffer nil t)
1888 (remove-hook 'after-save-hook 'browse-url-of-buffer t)))
9d4ce428 1889
3bf0b727 1890\f
1caf38eb
RS
1891(define-skeleton html-href-anchor
1892 "HTML anchor tag with href attribute."
a391b179
RS
1893 "URL: "
1894 '(setq input "http:")
1895 "<a href=\"" str "\">" _ "</a>")
1caf38eb
RS
1896
1897(define-skeleton html-name-anchor
1898 "HTML anchor tag with name attribute."
a391b179
RS
1899 "Name: "
1900 "<a name=\"" str "\">" _ "</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
a3ec4ba0 1935 (if sgml-xml-mode "<hr/>" "<hr>") \n)
1caf38eb
RS
1936
1937(define-skeleton html-image
1938 "HTML image tag."
1939 nil
b4f05c38 1940 "<img src=\"" _ "\""
a3ec4ba0 1941 (if sgml-xml-mode "/>" ">"))
1caf38eb
RS
1942
1943(define-skeleton html-line
1944 "HTML line break tag."
1945 nil
a3ec4ba0 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)
a3ec4ba0 1972 \n "<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
SS
1983 (when (y-or-n-p "Set \"checked\" attribute? ")
1984 (funcall skeleton-transformation " checked"))
a3ec4ba0 1985 (if sgml-xml-mode "/>" ">")
a391b179
RS
1986 (skeleton-read "Text: " (capitalize str))
1987 (or v2 (setq v2 (if (y-or-n-p "Newline after text? ")
b4f05c38 1988 (funcall skeleton-transformation
a3ec4ba0 1989 (if sgml-xml-mode "<br/>" "<br>"))
a391b179
RS
1990 "")))
1991 \n))
fcc3195e 1992
1caf38eb
RS
1993(define-skeleton html-radio-buttons
1994 "Group of connected radio button inputs."
1995 nil
a391b179
RS
1996 '(setq v1 nil
1997 v2 (cons nil nil))
1998 ("Value: "
d10447ba 1999 "<input type=\"" (identity "radio") ; see comment above about identity
a391b179 2000 "\" name=\"" (or (car v2) (setcar v2 (skeleton-read "Name: ")))
1caf38eb 2001 "\" value=\"" str ?\"
b4f05c38
SS
2002 (when (and (not v1) (setq v1 (y-or-n-p "Set \"checked\" attribute? ")))
2003 (funcall skeleton-transformation " checked"))
a3ec4ba0 2004 (if sgml-xml-mode "/>" ">")
a391b179
RS
2005 (skeleton-read "Text: " (capitalize str))
2006 (or (cdr v2) (setcdr v2 (if (y-or-n-p "Newline after text? ")
b4f05c38 2007 (funcall skeleton-transformation
a3ec4ba0 2008 (if sgml-xml-mode "<br/>" "<br>"))
a391b179
RS
2009 "")))
2010 \n))
1caf38eb 2011
e1940c83 2012(provide 'sgml-mode)
6a05d05f 2013
e9146d5a 2014;; arch-tag: 9675da94-b7f9-4bda-ad19-73ed7b4fb401
72c0ae01 2015;;; sgml-mode.el ends here