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