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