(grep-compute-defaults): Use null-device
[bpt/emacs.git] / lisp / textmodes / sgml-mode.el
CommitLineData
1caf38eb 1;;; sgml-mode.el --- SGML- and HTML-editing modes
72c0ae01 2
aa7a8f0e 3;; Copyright (C) 1992, 1995, 1996, 1998 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
82 (let ((map (list 'keymap (make-vector 256 nil)))
83 (menu-map (make-sparse-keymap "SGML")))
84 (define-key map "\t" 'indent-relative-maybe)
85 (define-key map "\C-c\C-i" 'sgml-tags-invisible)
86 (define-key map "/" 'sgml-slash)
fcc3195e
RS
87 (define-key map "\C-c\C-n" 'sgml-name-char)
88 (define-key map "\C-c\C-t" 'sgml-tag)
1caf38eb
RS
89 (define-key map "\C-c\C-a" 'sgml-attributes)
90 (define-key map "\C-c\C-b" 'sgml-skip-tag-backward)
91 (define-key map [?\C-c left] 'sgml-skip-tag-backward)
92 (define-key map "\C-c\C-f" 'sgml-skip-tag-forward)
93 (define-key map [?\C-c right] 'sgml-skip-tag-forward)
94 (define-key map "\C-c\C-d" 'sgml-delete-tag)
95 (define-key map "\C-c\^?" 'sgml-delete-tag)
96 (define-key map "\C-c?" 'sgml-tag-help)
1caf38eb
RS
97 (define-key map "\C-c8" 'sgml-name-8bit-mode)
98 (define-key map "\C-c\C-v" 'sgml-validate)
fcc3195e
RS
99 (if sgml-quick-keys
100 (progn
101 (define-key map "&" 'sgml-name-char)
102 (define-key map "<" 'sgml-tag)
103 (define-key map " " 'sgml-auto-attributes)
104 (define-key map ">" 'sgml-maybe-end-tag)
105 (if (memq ?\" sgml-specials)
106 (define-key map "\"" 'sgml-name-self))
107 (if (memq ?' sgml-specials)
108 (define-key map "'" 'sgml-name-self))))
1caf38eb
RS
109 (let ((c 127)
110 (map (nth 1 map)))
111 (while (< (setq c (1+ c)) 256)
112 (aset map c 'sgml-maybe-name-self)))
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
f788776c 149 "*When non-nil, insert 8 bit characters with their names."
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
188
5121371d
DL
189;; nsgmls is a free SGML parser in the SP suite available from
190;; ftp.jclark.com and otherwise packaged for GNU systems.
1caf38eb
RS
191;; Its error messages can be parsed by next-error.
192;; The -s option suppresses output.
193
5121371d 194(defcustom sgml-validate-command "nsgmls -s" ; replaced old `sgmls'
72c0ae01
ER
195 "*The command to validate an SGML document.
196The file name of current buffer file name will be appended to this,
64ae0c23
RS
197separated by a space."
198 :type 'string
d4c89075 199 :version "21.1"
64ae0c23 200 :group 'sgml)
72c0ae01
ER
201
202(defvar sgml-saved-validate-command nil
203 "The command last used to validate in this buffer.")
204
72c0ae01 205
1caf38eb
RS
206;;; I doubt that null end tags are used much for large elements,
207;;; so use a small distance here.
64ae0c23 208(defcustom sgml-slash-distance 1000
f788776c 209 "*If non-nil, is the maximum distance to search for matching `/'."
64ae0c23
RS
210 :type '(choice (const nil) integer)
211 :group 'sgml)
72c0ae01 212
1caf38eb
RS
213(defconst sgml-start-tag-regex
214 "<[A-Za-z]\\([-.A-Za-z0-9= \n\t]\\|\"[^\"]*\"\\|'[^']*'\\)*"
215 "Regular expression that matches a non-empty start tag.
f788776c 216Any terminating `>' or `/' is not matched.")
1caf38eb
RS
217
218
c6a63534
RS
219;; internal
220(defconst sgml-font-lock-keywords-1
b0a377e6
DL
221 '(("<\\([!?][a-z][-.a-z0-9]*\\)" 1 font-lock-keyword-face)
222 ("<\\(/?[a-z][-.a-z0-9]*\\)" 1 font-lock-function-name-face)
223 ("[&%][a-z][-.a-z0-9]*;?" . font-lock-variable-name-face)
4fa91cfe 224 ("<! *--.*-- *>" . font-lock-comment-face)))
c6a63534
RS
225
226(defconst sgml-font-lock-keywords-2 ())
227
228;; for font-lock, but must be defvar'ed after
229;; sgml-font-lock-keywords-1 and sgml-font-lock-keywords-2 above
230(defvar sgml-font-lock-keywords sgml-font-lock-keywords-1
1caf38eb
RS
231 "*Rules for highlighting SGML code. See also `sgml-tag-face-alist'.")
232
233;; internal
1caf38eb
RS
234(defvar sgml-face-tag-alist ()
235 "Alist of face and tag name for facemenu.")
236
237(defvar sgml-tag-face-alist ()
238 "Tag names and face or list of faces to fontify with when invisible.
239When `font-lock-maximum-decoration' is 1 this is always used for fontifying.
240When more these are fontified together with `sgml-font-lock-keywords'.")
241
242
243(defvar sgml-display-text ()
244 "Tag names as lowercase symbols, and display string when invisible.")
245
246;; internal
247(defvar sgml-tags-invisible nil)
248
249
64ae0c23 250(defcustom sgml-tag-alist
fcc3195e
RS
251 '(("![" ("ignore" t) ("include" t))
252 ("!attlist")
1caf38eb
RS
253 ("!doctype")
254 ("!element")
255 ("!entity"))
256 "*Alist of tag names for completing read and insertion rules.
257This alist is made up as
258
259 ((\"tag\" . TAGRULE)
260 ...)
261
262TAGRULE is a list of optionally `t' (no endtag) or `\\n' (separate endtag by
263newlines) or a skeleton with `nil', `t' or `\\n' in place of the interactor
264followed by an ATTRIBUTERULE (for an always present attribute) or an
265attribute alist.
266
267The attribute alist is made up as
268
269 ((\"attribute\" . ATTRIBUTERULE)
270 ...)
271
272ATTRIBUTERULE is a list of optionally `t' (no value when no input) followed by
64ae0c23
RS
273an optional alist of possible values."
274 :type '(repeat (cons (string :tag "Tag Name")
275 (repeat :tag "Tag Rule" sexp)))
276 :group 'sgml)
1caf38eb 277
64ae0c23 278(defcustom sgml-tag-help
1caf38eb
RS
279 '(("!" . "Empty declaration for comment")
280 ("![" . "Embed declarations with parser directive")
281 ("!attlist" . "Tag attributes declaration")
282 ("!doctype" . "Document type (DTD) declaration")
283 ("!element" . "Tag declaration")
284 ("!entity" . "Entity (macro) declaration"))
64ae0c23
RS
285 "*Alist of tag name and short description."
286 :type '(repeat (cons (string :tag "Tag Name")
287 (string :tag "Description")))
288 :group 'sgml)
1caf38eb 289
b0a377e6
DL
290(defvar v2) ; free for skeleton
291
1caf38eb
RS
292(defun sgml-mode-common (sgml-tag-face-alist sgml-display-text)
293 "Common code for setting up `sgml-mode' and derived modes.
c6a63534 294SGML-TAG-FACE-ALIST is used for calculating `sgml-font-lock-keywords-2'.
1caf38eb
RS
295SGML-DISPLAY-TEXT sets up alternate text for when tags are invisible (see
296varables of same name)."
72c0ae01 297 (setq local-abbrev-table text-mode-abbrev-table)
1caf38eb
RS
298 (set-syntax-table sgml-mode-syntax-table)
299 (make-local-variable 'indent-line-function)
72c0ae01 300 (make-local-variable 'paragraph-start)
72c0ae01 301 (make-local-variable 'paragraph-separate)
9e832d81 302 (make-local-variable 'adaptive-fill-regexp)
72c0ae01 303 (make-local-variable 'sgml-saved-validate-command)
72c0ae01 304 (make-local-variable 'comment-start)
72c0ae01 305 (make-local-variable 'comment-end)
e41b2db1 306 (make-local-variable 'comment-indent-function)
1caf38eb
RS
307 (make-local-variable 'comment-indent-function)
308 (make-local-variable 'sgml-tags-invisible)
309 (make-local-variable 'skeleton-transformation)
310 (make-local-variable 'skeleton-further-elements)
311 (make-local-variable 'skeleton-end-hook)
312 (make-local-variable 'font-lock-defaults)
313 (make-local-variable 'sgml-font-lock-keywords-1)
c6a63534 314 (make-local-variable 'sgml-font-lock-keywords-2)
1caf38eb
RS
315 (make-local-variable 'facemenu-add-face-function)
316 (make-local-variable 'facemenu-end-add-face)
317 ;;(make-local-variable 'facemenu-remove-face-function)
318 (and sgml-tag-face-alist
319 (not (assq 1 sgml-tag-face-alist))
320 (nconc sgml-tag-face-alist
321 `((1 (,(concat "<\\("
322 (mapconcat 'car sgml-tag-face-alist "\\|")
323 "\\)\\([ \t].+\\)?>\\(.+\\)</\\1>")
aa7a8f0e
DL
324 3 (cdr (assoc (downcase (match-string 1))
325 ',sgml-tag-face-alist)))))))
1caf38eb
RS
326 (setq indent-line-function 'indent-relative-maybe
327 ;; A start or end tag by itself on a line separates a paragraph.
328 ;; This is desirable because SGML discards a newline that appears
329 ;; immediately after a start tag or immediately before an end tag.
6c455d5c
RS
330 paragraph-separate "[ \t]*$\\|\
331\[ \t]*</?\\([A-Za-z]\\([-.A-Za-z0-9= \t\n]\\|\"[^\"]*\"\\|'[^']*'\\)*\\)?>$"
332 paragraph-start "[ \t]*$\\|\
fba5d1de 333\[ \t]*</?\\([A-Za-z]\\([-.A-Za-z0-9= \t\n]\\|\"[^\"]*\"\\|'[^']*'\\)*\\)?>"
6c455d5c 334 adaptive-fill-regexp "[ \t]*"
1caf38eb
RS
335 comment-start "<!-- "
336 comment-end " -->"
337 comment-indent-function 'sgml-comment-indent
a391b179 338 skeleton-transformation sgml-transformation
1caf38eb
RS
339 skeleton-further-elements '((completion-ignore-case t))
340 skeleton-end-hook (lambda ()
341 (or (eolp)
342 (not (or (eq v2 '\n)
343 (eq (car-safe v2) '\n)))
344 (newline-and-indent)))
c6a63534
RS
345 sgml-font-lock-keywords-2 (append
346 sgml-font-lock-keywords-1
347 (cdr (assq 1 sgml-tag-face-alist)))
1caf38eb 348 font-lock-defaults '((sgml-font-lock-keywords
c6a63534
RS
349 sgml-font-lock-keywords-1
350 sgml-font-lock-keywords-2)
1caf38eb
RS
351 nil
352 t)
6811a757 353 facemenu-add-face-function 'sgml-mode-facemenu-add-face-function)
4afa094d
SM
354 ;; This will allow existing comments within declarations to be
355 ;; recognized.
356 (set (make-local-variable 'comment-start-skip) "\\(?:<!\\)?--[ \t]*")
357 (set (make-local-variable 'comment-end-skip) "[ \t]*--\\([ \t\n]*>\\)?")
358 (dolist (pair sgml-display-text)
359 (put (car pair) 'before-string (cdr pair))))
72c0ae01 360
1caf38eb 361
6811a757
RS
362(defun sgml-mode-facemenu-add-face-function (face end)
363 (if (setq face (cdr (assq face sgml-face-tag-alist)))
364 (progn
365 (setq face (funcall skeleton-transformation face))
366 (setq facemenu-end-add-face (concat "</" face ">"))
367 (concat "<" face ">"))
368 (error "Face not configured for %s mode." mode-name)))
369
370
1caf38eb 371;;;###autoload
4afa094d 372(defun sgml-mode ()
1caf38eb
RS
373 "Major mode for editing SGML documents.
374Makes > match <. Makes / blink matching /.
fcc3195e
RS
375Keys <, &, SPC within <>, \" and ' can be electric depending on
376`sgml-quick-keys'.
1caf38eb 377
f788776c
RS
378An argument of N to a tag-inserting command means to wrap it around
379the next N words. In Transient Mark mode, when the mark is active,
380N defaults to -1, which means to wrap it around the current region.
a391b179 381
d10447ba 382If you like upcased tags, put (setq sgml-transformation 'upcase) in
f788776c 383your `.emacs' file.
1caf38eb
RS
384
385Use \\[sgml-validate] to validate your document with an SGML parser.
a391b179
RS
386
387Do \\[describe-variable] sgml- SPC to see available variables.
388Do \\[describe-key] on the following bindings to discover what they do.
1caf38eb
RS
389\\{sgml-mode-map}"
390 (interactive)
a01588fc
RS
391 (kill-all-local-variables)
392 (setq mode-name "SGML"
393 major-mode 'sgml-mode)
1caf38eb 394 (sgml-mode-common sgml-tag-face-alist sgml-display-text)
ec79b93a
KH
395 ;; Set imenu-generic-expression here, rather than in sgml-mode-common,
396 ;; because this definition probably is not useful in HTML mode.
397 (make-local-variable 'imenu-generic-expression)
398 (setq imenu-generic-expression
399 "<!\\(element\\|entity\\)[ \t\n]+%?[ \t\n]*\\([A-Za-z][-A-Za-z.0-9]*\\)")
1caf38eb 400 (use-local-map sgml-mode-map)
a01588fc 401 (run-hooks 'text-mode-hook 'sgml-mode-hook))
1caf38eb
RS
402
403
72c0ae01 404(defun sgml-comment-indent ()
4afa094d 405 (if (looking-at "--") comment-column 0))
72c0ae01 406
72c0ae01 407
72c0ae01
ER
408
409(defun sgml-slash (arg)
f788776c
RS
410 "Insert `/' and display any previous matching `/'.
411Two `/'s are treated as matching if the first `/' ends a net-enabling
412start tag, and the second `/' is the corresponding null end tag."
72c0ae01
ER
413 (interactive "p")
414 (insert-char ?/ arg)
415 (if (> arg 0)
416 (let ((oldpos (point))
417 (blinkpos)
418 (level 0))
419 (save-excursion
420 (save-restriction
421 (if sgml-slash-distance
422 (narrow-to-region (max (point-min)
423 (- (point) sgml-slash-distance))
424 oldpos))
425 (if (and (re-search-backward sgml-start-tag-regex (point-min) t)
426 (eq (match-end 0) (1- oldpos)))
427 ()
428 (goto-char (1- oldpos))
429 (while (and (not blinkpos)
430 (search-backward "/" (point-min) t))
431 (let ((tagend (save-excursion
432 (if (re-search-backward sgml-start-tag-regex
433 (point-min) t)
434 (match-end 0)
435 nil))))
436 (if (eq tagend (point))
437 (if (eq level 0)
438 (setq blinkpos (point))
439 (setq level (1- level)))
440 (setq level (1+ level)))))))
441 (if blinkpos
442 (progn
443 (goto-char blinkpos)
444 (if (pos-visible-in-window-p)
445 (sit-for 1)
446 (message "Matches %s"
447 (buffer-substring (progn
448 (beginning-of-line)
449 (point))
450 (1+ blinkpos))))))))))
451
1caf38eb
RS
452
453(defun sgml-name-char (&optional char)
454 "Insert a symbolic character name according to `sgml-char-names'.
4558 bit chars may be inserted with the meta key as in M-SPC for no break space,
456or M-- for a soft hyphen."
457 (interactive "*")
458 (insert ?&)
459 (or char
9b0ffdac 460 (setq char (read-quoted-char "Enter char or octal number")))
1caf38eb
RS
461 (delete-backward-char 1)
462 (insert char)
463 (undo-boundary)
464 (delete-backward-char 1)
465 (insert ?&
466 (or (aref sgml-char-names char)
467 (format "#%d" char))
468 ?\;))
469
470
471(defun sgml-name-self ()
472 "Insert a symbolic character name according to `sgml-char-names'."
473 (interactive "*")
474 (sgml-name-char last-command-char))
475
476
477(defun sgml-maybe-name-self ()
478 "Insert a symbolic character name according to `sgml-char-names'."
479 (interactive "*")
480 (if sgml-name-8bit-mode
481 (sgml-name-char last-command-char)
482 (self-insert-command 1)))
483
484
485(defun sgml-name-8bit-mode ()
486 "Toggle insertion of 8 bit characters."
487 (interactive)
d10447ba 488 (setq sgml-name-8bit-mode (not sgml-name-8bit-mode))
7be38f7d 489 (message "sgml name 8 bit mode is now %s"
d10447ba 490 (if sgml-name-8bit-mode "ON" "OFF")))
1caf38eb
RS
491
492
f788776c
RS
493;; When an element of a skeleton is a string "str", it is passed
494;; through skeleton-transformation and inserted. If "str" is to be
495;; inserted literally, one should obtain it as the return value of a
496;; function, e.g. (identity "str").
1caf38eb
RS
497
498(define-skeleton sgml-tag
f788776c
RS
499 "Prompt for a tag and insert it, optionally with attributes.
500Completion and configuration are done according to `sgml-tag-alist'.
d10447ba 501If you like tags and attributes in uppercase do \\[set-variable]
f788776c
RS
502skeleton-transformation RET upcase RET, or put this in your `.emacs':
503 (setq sgml-transformation 'upcase)"
4afa094d
SM
504 (completing-read "Tag: " sgml-tag-alist)
505 ?< str |
d10447ba 506 (("") -1 '(undo-boundary) (identity "&lt;")) | ; see comment above
4afa094d
SM
507 `(("") '(setq v2 (sgml-attributes ,str t)) ?>
508 (if (string= "![" ,str)
509 (prog1 '(("") " [ " _ " ]]")
510 (backward-char))
511 (if (or (eq v2 t)
512 (string-match "^[/!?]" ,str))
513 ()
514 (if (symbolp v2)
515 '(("") v2 _ v2 "</" ,str ?>)
516 (if (eq (car v2) t)
517 (cons '("") (cdr v2))
518 (append '(("") (car v2))
519 (cdr v2)
520 '(resume: (car v2) _ "</" ,str ?>))))))))
1caf38eb
RS
521
522(autoload 'skeleton-read "skeleton")
523
d10447ba 524(defun sgml-attributes (tag &optional quiet)
f788776c 525 "When at top level of a tag, interactively insert attributes.
d10447ba 526
f788776c
RS
527Completion and configuration of TAG are done according to `sgml-tag-alist'.
528If QUIET, do not print a message when there are no attributes for TAG."
1caf38eb 529 (interactive (list (save-excursion (sgml-beginning-of-tag t))))
d10447ba
RS
530 (or (stringp tag) (error "Wrong context for adding attribute"))
531 (if tag
1caf38eb 532 (let ((completion-ignore-case t)
d10447ba 533 (alist (cdr (assoc (downcase tag) sgml-tag-alist)))
1caf38eb 534 car attribute i)
1caf38eb
RS
535 (if (or (symbolp (car alist))
536 (symbolp (car (car alist))))
537 (setq car (car alist)
538 alist (cdr alist)))
539 (or quiet
540 (message "No attributes configured."))
541 (if (stringp (car alist))
542 (progn
d10447ba
RS
543 (insert (if (eq (preceding-char) ? ) "" ? )
544 (funcall skeleton-transformation (car alist)))
1caf38eb
RS
545 (sgml-value alist))
546 (setq i (length alist))
547 (while (> i 0)
548 (insert ? )
549 (insert (funcall skeleton-transformation
550 (setq attribute
551 (skeleton-read '(completing-read
d10447ba 552 "Attribute: "
1caf38eb
RS
553 alist)))))
554 (if (string= "" attribute)
555 (setq i 0)
aa7a8f0e 556 (sgml-value (assoc (downcase attribute) alist))
1caf38eb
RS
557 (setq i (1- i))))
558 (if (eq (preceding-char) ? )
559 (delete-backward-char 1)))
560 car)))
561
562(defun sgml-auto-attributes (arg)
f788776c
RS
563 "Self insert the character typed; at top level of tag, prompt for attributes.
564With prefix argument, only self insert."
1caf38eb
RS
565 (interactive "*P")
566 (let ((point (point))
567 tag)
568 (if (or arg
1caf38eb
RS
569 (not sgml-tag-alist) ; no message when nothing configured
570 (symbolp (setq tag (save-excursion (sgml-beginning-of-tag t))))
571 (eq (aref tag 0) ?/))
572 (self-insert-command (prefix-numeric-value arg))
573 (sgml-attributes tag)
574 (setq last-command-char ? )
575 (or (> (point) point)
576 (self-insert-command 1)))))
577
578
579(defun sgml-tag-help (&optional tag)
f788776c 580 "Display description of tag TAG. If TAG is omitted, use the tag at point."
1caf38eb
RS
581 (interactive)
582 (or tag
583 (save-excursion
584 (if (eq (following-char) ?<)
585 (forward-char))
586 (setq tag (sgml-beginning-of-tag))))
587 (or (stringp tag)
588 (error "No tag selected"))
589 (setq tag (downcase tag))
f68f40e0 590 (message "%s"
aa7a8f0e 591 (or (cdr (assoc (downcase tag) sgml-tag-help))
1caf38eb 592 (and (eq (aref tag 0) ?/)
aa7a8f0e 593 (cdr (assoc (downcase (substring tag 1)) sgml-tag-help)))
1caf38eb
RS
594 "No description available")))
595
596
597(defun sgml-maybe-end-tag ()
598 "Name self unless in position to end a tag."
599 (interactive)
600 (or (condition-case nil
601 (save-excursion (up-list -1))
602 (error
603 (sgml-name-self)
604 t))
605 (condition-case nil
606 (progn
607 (save-excursion (up-list 1))
608 (sgml-name-self))
609 (error (self-insert-command 1)))))
610
611
612(defun sgml-skip-tag-backward (arg)
613 "Skip to beginning of tag or matching opening tag if present.
f788776c 614With prefix argument ARG, repeat this ARG times."
1caf38eb
RS
615 (interactive "p")
616 (while (>= arg 1)
617 (search-backward "<" nil t)
618 (if (looking-at "</\\([^ \n\t>]+\\)")
619 ;; end tag, skip any nested pairs
620 (let ((case-fold-search t)
621 (re (concat "</?" (regexp-quote (match-string 1)))))
622 (while (and (re-search-backward re nil t)
623 (eq (char-after (1+ (point))) ?/))
624 (forward-char 1)
625 (sgml-skip-tag-backward 1))))
626 (setq arg (1- arg))))
627
628(defun sgml-skip-tag-forward (arg &optional return)
629 "Skip to end of tag or matching closing tag if present.
f788776c 630With prefix argument ARG, repeat this ARG times.
1caf38eb
RS
631Return t iff after a closing tag."
632 (interactive "p")
633 (setq return t)
634 (while (>= arg 1)
635 (skip-chars-forward "^<>")
636 (if (eq (following-char) ?>)
637 (up-list -1))
638 (if (looking-at "<\\([^/ \n\t>]+\\)")
639 ;; start tag, skip any nested same pairs _and_ closing tag
640 (let ((case-fold-search t)
641 (re (concat "</?" (regexp-quote (match-string 1))))
642 point close)
643 (forward-list 1)
644 (setq point (point))
645 (while (and (re-search-forward re nil t)
646 (not (setq close
647 (eq (char-after (1+ (match-beginning 0))) ?/)))
648 (not (up-list -1))
649 (sgml-skip-tag-forward 1))
650 (setq close nil))
651 (if close
652 (up-list 1)
653 (goto-char point)
654 (setq return)))
655 (forward-list 1))
656 (setq arg (1- arg)))
657 return)
658
659(defun sgml-delete-tag (arg)
660 "Delete tag on or after cursor, and matching closing or opening tag.
f788776c 661With prefix argument ARG, repeat this ARG times."
1caf38eb
RS
662 (interactive "p")
663 (while (>= arg 1)
664 (save-excursion
665 (let* (close open)
fcc3195e 666 (if (looking-at "[ \t\n]*<")
1caf38eb
RS
667 ;; just before tag
668 (if (eq (char-after (match-end 0)) ?/)
669 ;; closing tag
670 (progn
671 (setq close (point))
672 (goto-char (match-end 0))))
673 ;; on tag?
674 (or (save-excursion (setq close (sgml-beginning-of-tag)
675 close (and (stringp close)
676 (eq (aref close 0) ?/)
677 (point))))
678 ;; not on closing tag
679 (let ((point (point)))
680 (sgml-skip-tag-backward 1)
681 (if (or (not (eq (following-char) ?<))
682 (save-excursion
683 (forward-list 1)
684 (<= (point) point)))
685 (error "Not on or before tag")))))
686 (if close
687 (progn
688 (sgml-skip-tag-backward 1)
689 (setq open (point))
690 (goto-char close)
691 (kill-sexp 1))
692 (setq open (point))
693 (sgml-skip-tag-forward 1)
694 (backward-list)
695 (forward-char)
696 (if (eq (aref (sgml-beginning-of-tag) 0) ?/)
697 (kill-sexp 1)))
698 (goto-char open)
699 (kill-sexp 1)))
700 (setq arg (1- arg))))
a391b179
RS
701\f
702;; Put read-only last to enable setting this even when read-only enabled.
703(or (get 'sgml-tag 'invisible)
704 (setplist 'sgml-tag
705 (append '(invisible t
706 intangible t
707 point-entered sgml-point-entered
708 rear-nonsticky t
709 read-only t)
710 (symbol-plist 'sgml-tag))))
1caf38eb
RS
711
712(defun sgml-tags-invisible (arg)
713 "Toggle visibility of existing tags."
714 (interactive "P")
715 (let ((modified (buffer-modified-p))
716 (inhibit-read-only t)
a391b179
RS
717 ;; This is needed in case font lock gets called,
718 ;; since it moves point and might call sgml-point-entered.
719 (inhibit-point-motion-hooks t)
1caf38eb
RS
720 symbol)
721 (save-excursion
a391b179 722 (goto-char (point-min))
1caf38eb
RS
723 (if (setq sgml-tags-invisible
724 (if arg
725 (>= (prefix-numeric-value arg) 0)
726 (not sgml-tags-invisible)))
727 (while (re-search-forward "<\\([!/?A-Za-z][-A-Za-z0-9]*\\)"
728 nil t)
729 (setq symbol (intern-soft (downcase (match-string 1))))
730 (goto-char (match-beginning 0))
731 (and (get symbol 'before-string)
732 (not (overlays-at (point)))
733 (overlay-put (make-overlay (point)
734 (match-beginning 1))
735 'category symbol))
a391b179
RS
736 (put-text-property (point)
737 (progn (forward-list) (point))
1caf38eb 738 'category 'sgml-tag))
a391b179
RS
739 (let ((pos (point)))
740 (while (< (setq pos (next-overlay-change pos)) (point-max))
741 (delete-overlay (car (overlays-at pos)))))
1caf38eb
RS
742 (remove-text-properties (point-min) (point-max)
743 '(category sgml-tag intangible t))))
744 (set-buffer-modified-p modified)
745 (run-hooks 'sgml-tags-invisible-hook)
746 (message "")))
747
748(defun sgml-point-entered (x y)
749 ;; Show preceding or following hidden tag, depending of cursor direction.
750 (let ((inhibit-point-motion-hooks t))
751 (save-excursion
752 (message "Invisible tag: %s"
753 (buffer-substring
754 (point)
755 (if (or (and (> x y)
756 (not (eq (following-char) ?<)))
757 (and (< x y)
758 (eq (preceding-char) ?>)))
759 (backward-list)
760 (forward-list)))))))
a391b179 761\f
1caf38eb
RS
762(autoload 'compile-internal "compile")
763
72c0ae01
ER
764(defun sgml-validate (command)
765 "Validate an SGML document.
766Runs COMMAND, a shell command, in a separate process asynchronously
f788776c 767with output going to the buffer `*compilation*'.
72c0ae01
ER
768You can then use the command \\[next-error] to find the next error message
769and move to the line in the SGML document that caused it."
770 (interactive
771 (list (read-string "Validate command: "
772 (or sgml-saved-validate-command
773 (concat sgml-validate-command
774 " "
775 (let ((name (buffer-file-name)))
776 (and name
777 (file-name-nondirectory name))))))))
778 (setq sgml-saved-validate-command command)
b7cd1746 779 (save-some-buffers (not compilation-ask-about-save) nil)
c7aa4667 780 (compile-internal command "No more errors"))
72c0ae01 781
1caf38eb
RS
782
783(defun sgml-beginning-of-tag (&optional top-level)
784 "Skip to beginning of tag and return its name.
f788776c 785If this can't be done, return t."
1caf38eb
RS
786 (or (if top-level
787 (condition-case nil
788 (up-list -1)
789 (error t))
790 (>= (point)
791 (if (search-backward "<" nil t)
792 (save-excursion
793 (forward-list)
794 (point))
795 0)))
796 (if (looking-at "<[!?/]?[[A-Za-z][A-Za-z0-9]*")
797 (buffer-substring-no-properties
798 (1+ (point))
799 (match-end 0))
800 t)))
801
802(defun sgml-value (alist)
f788776c
RS
803 "Interactively insert value taken from attributerule ALIST.
804See `sgml-tag-alist' for info about attributerules.."
1caf38eb
RS
805 (setq alist (cdr alist))
806 (if (stringp (car alist))
807 (insert "=\"" (car alist) ?\")
808 (if (eq (car alist) t)
809 (if (cdr alist)
810 (progn
811 (insert "=\"")
812 (setq alist (skeleton-read '(completing-read
d10447ba 813 "Value: " (cdr alist))))
1caf38eb 814 (if (string< "" alist)
a391b179 815 (insert alist ?\")
1caf38eb
RS
816 (delete-backward-char 2))))
817 (insert "=\"")
818 (if alist
a391b179 819 (insert (skeleton-read '(completing-read "Value: " alist))))
1caf38eb
RS
820 (insert ?\"))))
821
822(provide 'sgml-mode)
823\f
d4c89075
DL
824(defcustom html-mode-hook nil
825 "Hook run by command `html-mode'.
826`text-mode-hook' and `sgml-mode-hook' are run first."
827 :group 'sgml
828 :type 'hook
829 :options '(html-autoview-mode))
830
fcc3195e 831(defvar html-quick-keys sgml-quick-keys
b1e7bb48 832 "Use C-c X combinations for quick insertion of frequent tags when non-nil.
fcc3195e 833This defaults to `sgml-quick-keys'.
1caf38eb
RS
834This takes effect when first loading the library.")
835
836(defvar html-mode-map
837 (let ((map (nconc (make-sparse-keymap) sgml-mode-map))
838 (menu-map (make-sparse-keymap "HTML")))
7e49eef2
RS
839 (define-key map "\C-c6" 'html-headline-6)
840 (define-key map "\C-c5" 'html-headline-5)
841 (define-key map "\C-c4" 'html-headline-4)
842 (define-key map "\C-c3" 'html-headline-3)
843 (define-key map "\C-c2" 'html-headline-2)
844 (define-key map "\C-c1" 'html-headline-1)
fcc3195e
RS
845 (define-key map "\C-c\r" 'html-paragraph)
846 (define-key map "\C-c\n" 'html-line)
847 (define-key map "\C-c\C-c-" 'html-horizontal-rule)
7e49eef2
RS
848 (define-key map "\C-c\C-co" 'html-ordered-list)
849 (define-key map "\C-c\C-cu" 'html-unordered-list)
fcc3195e
RS
850 (define-key map "\C-c\C-cr" 'html-radio-buttons)
851 (define-key map "\C-c\C-cc" 'html-checkboxes)
852 (define-key map "\C-c\C-cl" 'html-list-item)
853 (define-key map "\C-c\C-ch" 'html-href-anchor)
854 (define-key map "\C-c\C-cn" 'html-name-anchor)
855 (define-key map "\C-c\C-ci" 'html-image)
1caf38eb
RS
856 (if html-quick-keys
857 (progn
1caf38eb 858 (define-key map "\C-c-" 'html-horizontal-rule)
7e49eef2
RS
859 (define-key map "\C-co" 'html-ordered-list)
860 (define-key map "\C-cu" 'html-unordered-list)
1caf38eb 861 (define-key map "\C-cr" 'html-radio-buttons)
fcc3195e 862 (define-key map "\C-cc" 'html-checkboxes)
1caf38eb
RS
863 (define-key map "\C-cl" 'html-list-item)
864 (define-key map "\C-ch" 'html-href-anchor)
865 (define-key map "\C-cn" 'html-name-anchor)
866 (define-key map "\C-ci" 'html-image)))
867 (define-key map "\C-c\C-s" 'html-autoview-mode)
868 (define-key map "\C-c\C-v" 'browse-url-of-buffer)
869 (define-key map [menu-bar html] (cons "HTML" menu-map))
870 (define-key menu-map [html-autoview-mode]
871 '("Toggle Autoviewing" . html-autoview-mode))
872 (define-key menu-map [browse-url-of-buffer]
873 '("View Buffer Contents" . browse-url-of-buffer))
874 (define-key menu-map [nil] '("--"))
7e49eef2
RS
875 ;;(define-key menu-map "6" '("Heading 6" . html-headline-6))
876 ;;(define-key menu-map "5" '("Heading 5" . html-headline-5))
877 ;;(define-key menu-map "4" '("Heading 4" . html-headline-4))
878 (define-key menu-map "3" '("Heading 3" . html-headline-3))
879 (define-key menu-map "2" '("Heading 2" . html-headline-2))
880 (define-key menu-map "1" '("Heading 1" . html-headline-1))
1caf38eb 881 (define-key menu-map "l" '("Radio Buttons" . html-radio-buttons))
fcc3195e 882 (define-key menu-map "c" '("Checkboxes" . html-checkboxes))
1caf38eb 883 (define-key menu-map "l" '("List Item" . html-list-item))
7e49eef2
RS
884 (define-key menu-map "u" '("Unordered List" . html-unordered-list))
885 (define-key menu-map "o" '("Ordered List" . html-ordered-list))
fcc3195e 886 (define-key menu-map "-" '("Horizontal Rule" . html-horizontal-rule))
1caf38eb
RS
887 (define-key menu-map "\n" '("Line Break" . html-line))
888 (define-key menu-map "\r" '("Paragraph" . html-paragraph))
889 (define-key menu-map "i" '("Image" . html-image))
890 (define-key menu-map "h" '("Href Anchor" . html-href-anchor))
891 (define-key menu-map "n" '("Name Anchor" . html-name-anchor))
892 map)
893 "Keymap for commands for use in HTML mode.")
894
895
896(defvar html-face-tag-alist
897 '((bold . "b")
898 (italic . "i")
899 (underline . "u")
900 (modeline . "rev"))
901 "Value of `sgml-face-tag-alist' for HTML mode.")
902
903(defvar html-tag-face-alist
904 '(("b" . bold)
905 ("big" . bold)
906 ("blink" . highlight)
907 ("cite" . italic)
908 ("em" . italic)
909 ("h1" bold underline)
910 ("h2" bold-italic underline)
911 ("h3" italic underline)
912 ("h4" . underline)
913 ("h5" . underline)
914 ("h6" . underline)
915 ("i" . italic)
916 ("rev" . modeline)
917 ("s" . underline)
918 ("small" . default)
919 ("strong" . bold)
920 ("title" bold underline)
921 ("tt" . default)
922 ("u" . underline)
923 ("var" . italic))
924 "Value of `sgml-tag-face-alist' for HTML mode.")
925
926
927(defvar html-display-text
928 '((img . "[/]")
929 (hr . "----------")
930 (li . "o "))
931 "Value of `sgml-display-text' for HTML mode.")
3bf0b727
RS
932\f
933;; should code exactly HTML 3 here when that is finished
1caf38eb 934(defvar html-tag-alist
d10447ba
RS
935 (let* ((1-7 '(("1") ("2") ("3") ("4") ("5") ("6") ("7")))
936 (1-9 '(,@1-7 ("8") ("9")))
1caf38eb
RS
937 (align '(("align" ("left") ("center") ("right"))))
938 (valign '(("top") ("middle") ("bottom") ("baseline")))
939 (rel '(("next") ("previous") ("parent") ("subdocument") ("made")))
940 (href '("href" ("ftp:") ("file:") ("finger:") ("gopher:") ("http:")
941 ("mailto:") ("news:") ("rlogin:") ("telnet:") ("tn3270:")
fcc3195e 942 ("wais:") ("/cgi-bin/")))
1caf38eb
RS
943 (name '("name"))
944 (link `(,href
945 ("rel" ,@rel)
946 ("rev" ,@rel)
947 ("title")))
d10447ba
RS
948 (list '((nil \n ( "List item: "
949 "<li>" str \n))))
1caf38eb
RS
950 (cell `(t
951 ,align
952 ("valign" ,@valign)
953 ("colspan" ,@1-9)
954 ("rowspan" ,@1-9)
955 ("nowrap" t))))
956 ;; put ,-expressions first, else byte-compile chokes (as of V19.29)
957 ;; and like this it's more efficient anyway
958 `(("a" ,name ,@link)
959 ("base" t ,@href)
960 ("dir" ,@list)
d10447ba 961 ("font" nil "size" ("-1") ("+1") ("-2") ("+2") ,@1-7)
fcc3195e
RS
962 ("form" (\n _ \n "<input type=\"submit\" value=\"\">")
963 ("action" ,@(cdr href)) ("method" ("get") ("post")))
1caf38eb
RS
964 ("h1" ,@align)
965 ("h2" ,@align)
966 ("h3" ,@align)
967 ("h4" ,@align)
968 ("h5" ,@align)
969 ("h6" ,@align)
970 ("hr" t ("size" ,@1-9) ("width") ("noshade" t) ,@align)
971 ("img" t ("align" ,@valign ("texttop") ("absmiddle") ("absbottom"))
972 ("src") ("alt") ("width" "1") ("height" "1")
973 ("border" "1") ("vspace" "1") ("hspace" "1") ("ismap" t))
974 ("input" t ("size" ,@1-9) ("maxlength" ,@1-9) ("checked" t) ,name
fcc3195e
RS
975 ("type" ("text") ("password") ("checkbox") ("radio")
976 ("submit") ("reset"))
1caf38eb
RS
977 ("value"))
978 ("link" t ,@link)
979 ("menu" ,@list)
d10447ba 980 ("ol" ,@list ("type" ("A") ("a") ("I") ("i") ("1")))
1caf38eb
RS
981 ("p" t ,@align)
982 ("select" (nil \n
983 ("Text: "
984 "<option>" str \n))
985 ,name ("size" ,@1-9) ("multiple" t))
986 ("table" (nil \n
987 ((completing-read "Cell kind: " '(("td") ("th"))
988 nil t "t")
989 "<tr><" str ?> _ \n))
990 ("border" t ,@1-9) ("width" "10") ("cellpadding"))
991 ("td" ,@cell)
992 ("textarea" ,name ("rows" ,@1-9) ("cols" ,@1-9))
993 ("th" ,@cell)
d10447ba 994 ("ul" ,@list ("type" ("disc") ("circle") ("square")))
1caf38eb
RS
995
996 ,@sgml-tag-alist
997
998 ("abbrev")
999 ("acronym")
1000 ("address")
1001 ("array" (nil \n
1002 ("Item: " "<item>" str \n))
1003 "align")
1004 ("au")
1005 ("b")
1006 ("big")
1007 ("blink")
1008 ("blockquote" \n)
1009 ("body" \n ("background" ".gif") ("bgcolor" "#") ("text" "#")
1010 ("link" "#") ("alink" "#") ("vlink" "#"))
1011 ("box" (nil _ "<over>" _))
1012 ("br" t ("clear" ("left") ("right")))
1013 ("caption" ("valign" ("top") ("bottom")))
1014 ("center" \n)
1015 ("cite")
1016 ("code" \n)
1017 ("dd" t)
1018 ("del")
1019 ("dfn")
1020 ("dl" (nil \n
1021 ( "Term: "
1022 "<dt>" str "<dd>" _ \n)))
1023 ("dt" (t _ "<dd>"))
1024 ("em")
d10447ba 1025 ;("fn" "id" "fn") ; ???
1caf38eb
RS
1026 ("head" \n)
1027 ("html" (\n
1028 "<head>\n"
1029 "<title>" (setq str (read-input "Title: ")) "</title>\n"
5e532c5c 1030 "</head>\n"
1caf38eb
RS
1031 "<body>\n<h1>" str "</h1>\n" _
1032 "\n<address>\n<a href=\"mailto:"
be047262 1033 user-mail-address
5e532c5c
RS
1034 "\">" (user-full-name) "</a>\n</address>\n"
1035 "</body>"
1036 ))
1caf38eb
RS
1037 ("i")
1038 ("ins")
1039 ("isindex" t ("action") ("prompt"))
1040 ("kbd")
1041 ("lang")
1042 ("li" t)
1043 ("math" \n)
1044 ("nobr")
1045 ("option" t ("value") ("label") ("selected" t))
1046 ("over" t)
1047 ("person")
1048 ("pre" \n)
1049 ("q")
1050 ("rev")
1051 ("s")
1052 ("samp")
1053 ("small")
1054 ("strong")
1055 ("sub")
1056 ("sup")
1057 ("title")
1058 ("tr" t)
1059 ("tt")
1060 ("u")
1061 ("var")
1062 ("wbr" t)))
1063 "*Value of `sgml-tag-alist' for HTML mode.")
1064
1065(defvar html-tag-help
1066 `(,@sgml-tag-help
1067 ("a" . "Anchor of point or link elsewhere")
1068 ("abbrev" . "?")
1069 ("acronym" . "?")
1070 ("address" . "Formatted mail address")
1071 ("array" . "Math array")
1072 ("au" . "?")
1073 ("b" . "Bold face")
1074 ("base" . "Base address for URLs")
1075 ("big" . "Font size")
1076 ("blink" . "Blinking text")
1077 ("blockquote" . "Indented quotation")
1078 ("body" . "Document body")
1079 ("box" . "Math fraction")
1080 ("br" . "Line break")
1081 ("caption" . "Table caption")
1082 ("center" . "Centered text")
1083 ("changed" . "Change bars")
1084 ("cite" . "Citation of a document")
1085 ("code" . "Formatted source code")
1086 ("dd" . "Definition of term")
1087 ("del" . "?")
1088 ("dfn" . "?")
1089 ("dir" . "Directory list (obsolete)")
1090 ("dl" . "Definition list")
1091 ("dt" . "Term to be definined")
1092 ("em" . "Emphasised")
1093 ("embed" . "Embedded data in foreign format")
1094 ("fig" . "Figure")
1095 ("figa" . "Figure anchor")
1096 ("figd" . "Figure description")
1097 ("figt" . "Figure text")
d10447ba 1098 ;("fn" . "?") ; ???
1caf38eb
RS
1099 ("font" . "Font size")
1100 ("form" . "Form with input fields")
1101 ("group" . "Document grouping")
1102 ("h1" . "Most important section headline")
1103 ("h2" . "Important section headline")
1104 ("h3" . "Section headline")
1105 ("h4" . "Minor section headline")
1106 ("h5" . "Unimportant section headline")
1107 ("h6" . "Least important section headline")
1108 ("head" . "Document header")
1109 ("hr" . "Horizontal rule")
1110 ("html" . "HTML Document")
1111 ("i" . "Italic face")
1112 ("img" . "Graphic image")
1113 ("input" . "Form input field")
1114 ("ins" . "?")
1115 ("isindex" . "Input field for index search")
1116 ("kbd" . "Keybard example face")
1117 ("lang" . "Natural language")
1118 ("li" . "List item")
1119 ("link" . "Link relationship")
1120 ("math" . "Math formula")
1121 ("menu" . "Menu list (obsolete)")
1122 ("mh" . "Form mail header")
1123 ("nextid" . "Allocate new id")
1124 ("nobr" . "Text without line break")
1125 ("ol" . "Ordered list")
1126 ("option" . "Selection list item")
1127 ("over" . "Math fraction rule")
1128 ("p" . "Paragraph start")
1129 ("panel" . "Floating panel")
1130 ("person" . "?")
1131 ("pre" . "Preformatted fixed width text")
1132 ("q" . "?")
1133 ("rev" . "Reverse video")
1134 ("s" . "?")
1135 ("samp" . "Sample text")
1136 ("select" . "Selection list")
1137 ("small" . "Font size")
1138 ("sp" . "Nobreak space")
1139 ("strong" . "Standout text")
1140 ("sub" . "Subscript")
1141 ("sup" . "Superscript")
1142 ("table" . "Table with rows and columns")
1143 ("tb" . "Table vertical break")
1144 ("td" . "Table data cell")
1145 ("textarea" . "Form multiline edit area")
1146 ("th" . "Table header cell")
1147 ("title" . "Document title")
1148 ("tr" . "Table row separator")
1149 ("tt" . "Typewriter face")
1150 ("u" . "Underlined text")
1151 ("ul" . "Unordered list")
1152 ("var" . "Math variable face")
1153 ("wbr" . "Enable <br> within <nobr>"))
1154"*Value of `sgml-tag-help' for HTML mode.")
3bf0b727 1155\f
1caf38eb
RS
1156;;;###autoload
1157(defun html-mode ()
1158 "Major mode based on SGML mode for editing HTML documents.
7be38f7d 1159This allows inserting skeleton constructs used in hypertext documents with
fcc3195e
RS
1160completion. See below for an introduction to HTML. Use
1161\\[browse-url-of-buffer] to see how this comes out. See also `sgml-mode' on
1162which this is based.
1caf38eb 1163
fcc3195e 1164Do \\[describe-variable] html- SPC and \\[describe-variable] sgml- SPC to see available variables.
1caf38eb
RS
1165
1166To write fairly well formatted pages you only need to know few things. Most
1167browsers have a function to read the source code of the page being seen, so
1168you can imitate various tricks. Here's a very short HTML primer which you
1169can also view with a browser to see what happens:
1170
1171<title>A Title Describing Contents</title> should be on every page. Pages can
1172have <h1>Very Major Headlines</h1> through <h6>Very Minor Headlines</h6>
1173<hr> Parts can be separated with horizontal rules.
1174
1175<p>Paragraphs only need an opening tag. Line breaks and multiple spaces are
1176ignored unless the text is <pre>preformatted.</pre> Text can be marked as
1177<b>bold</b>, <i>italic</i> or <u>underlined</u> using the normal M-g or
1178Edit/Text Properties/Face commands.
1179
1180Pages can have <a name=\"SOMENAME\">named points</a> and can link other points
1181to them with <a href=\"#SOMENAME\">see also somename</a>. In the same way <a
1182href=\"URL\">see also URL</a> where URL is a filename relative to current
f788776c 1183directory, or absolute as in `http://www.cs.indiana.edu/elisp/w3/docs.html'.
1caf38eb
RS
1184
1185Images in many formats can be inlined with <img src=\"URL\">.
1186
f788776c
RS
1187If you mainly create your own documents, `sgml-specials' might be
1188interesting. But note that some HTML 2 browsers can't handle `&apos;'.
1189To work around that, do:
1190 (eval-after-load \"sgml-mode\" '(aset sgml-char-names ?' nil))
1caf38eb 1191
1caf38eb
RS
1192\\{html-mode-map}"
1193 (interactive)
a01588fc
RS
1194 (kill-all-local-variables)
1195 (setq mode-name "HTML"
1196 major-mode 'html-mode)
1caf38eb
RS
1197 (sgml-mode-common html-tag-face-alist html-display-text)
1198 (use-local-map html-mode-map)
1199 (make-local-variable 'sgml-tag-alist)
1200 (make-local-variable 'sgml-face-tag-alist)
1201 (make-local-variable 'sgml-tag-help)
1202 (make-local-variable 'outline-regexp)
1203 (make-local-variable 'outline-heading-end-regexp)
1204 (make-local-variable 'outline-level)
da84bdc4
RS
1205 (make-local-variable 'sentence-end)
1206 (setq sentence-end
b8b14971
DL
1207 (if sentence-end-double-space
1208 "[.?!][]\"')}]*\\(<[^>]*>\\)*\\($\\| $\\|\t\\| \\)[ \t\n]*"
1209
1210 "[.?!][]\"')}]*\\(<[^>]*>\\)*\\($\\| \\|\t\\)[ \t\n]*"))
a01588fc 1211 (setq sgml-tag-alist html-tag-alist
1caf38eb
RS
1212 sgml-face-tag-alist html-face-tag-alist
1213 sgml-tag-help html-tag-help
1214 outline-regexp "^.*<[Hh][1-6]\\>"
1215 outline-heading-end-regexp "</[Hh][1-6]>"
1216 outline-level (lambda ()
1217 (char-after (1- (match-end 0)))))
3bf0b727
RS
1218 (setq imenu-create-index-function 'html-imenu-index)
1219 (make-local-variable 'imenu-sort-function)
1220 (setq imenu-sort-function nil) ; sorting the menu defeats the purpose
a01588fc 1221 (run-hooks 'text-mode-hook 'sgml-mode-hook 'html-mode-hook))
3bf0b727
RS
1222\f
1223(defvar html-imenu-regexp
1224 "\\s-*<h\\([1-9]\\)[^\n<>]*>\\(<[^\n<>]*>\\)*\\s-*\\([^\n<>]*\\)"
1225 "*A regular expression matching a head line to be added to the menu.
1226The first `match-string' should be a number from 1-9.
1227The second `match-string' matches extra tags and is ignored.
1228The third `match-string' will be the used in the menu.")
1229
1230(defun html-imenu-index ()
1231 "Return an table of contents for an HTML buffer for use with Imenu."
1232 (let (toc-index)
1233 (save-excursion
1234 (goto-char (point-min))
1235 (while (re-search-forward html-imenu-regexp nil t)
1236 (setq toc-index
1237 (cons (cons (concat (make-string
1238 (* 2 (1- (string-to-number (match-string 1))))
1239 ?\ )
1240 (match-string 3))
1241 (save-excursion (beginning-of-line) (point)))
1242 toc-index))))
1243 (nreverse toc-index)))
1caf38eb 1244
3bf0b727 1245(defun html-autoview-mode (&optional arg)
d4c89075 1246 "Toggle automatic viewing via `browse-url-of-buffer' upon saving buffer.
3bf0b727
RS
1247With positive prefix ARG always turns viewing on, with negative ARG always off.
1248Can be used as a value for `html-mode-hook'."
1249 (interactive "P")
1250 (if (setq arg (if arg
1251 (< (prefix-numeric-value arg) 0)
1252 (and (boundp 'after-save-hook)
1253 (memq 'browse-url-of-buffer after-save-hook))))
1254 (setq after-save-hook (delq 'browse-url-of-buffer after-save-hook))
3bf0b727
RS
1255 (add-hook 'after-save-hook 'browse-url-of-buffer nil t))
1256 (message "Autoviewing turned %s."
1257 (if arg "off" "on")))
1258\f
1caf38eb
RS
1259(define-skeleton html-href-anchor
1260 "HTML anchor tag with href attribute."
a391b179
RS
1261 "URL: "
1262 '(setq input "http:")
1263 "<a href=\"" str "\">" _ "</a>")
1caf38eb
RS
1264
1265(define-skeleton html-name-anchor
1266 "HTML anchor tag with name attribute."
a391b179
RS
1267 "Name: "
1268 "<a name=\"" str "\">" _ "</a>")
1caf38eb 1269
7e49eef2
RS
1270(define-skeleton html-headline-1
1271 "HTML level 1 headline tags."
1272 nil
1273 "<h1>" _ "</h1>")
1274
1275(define-skeleton html-headline-2
1276 "HTML level 2 headline tags."
1277 nil
1278 "<h2>" _ "</h2>")
1279
1280(define-skeleton html-headline-3
1281 "HTML level 3 headline tags."
1282 nil
1283 "<h3>" _ "</h3>")
1284
1285(define-skeleton html-headline-4
1286 "HTML level 4 headline tags."
1287 nil
1288 "<h4>" _ "</h4>")
1289
1290(define-skeleton html-headline-5
1291 "HTML level 5 headline tags."
1292 nil
1293 "<h5>" _ "</h5>")
1294
1295(define-skeleton html-headline-6
1296 "HTML level 6 headline tags."
1297 nil
1298 "<h6>" _ "</h6>")
1caf38eb
RS
1299
1300(define-skeleton html-horizontal-rule
1301 "HTML horizontal rule tag."
1302 nil
1303 "<hr>" \n)
1304
1305(define-skeleton html-image
1306 "HTML image tag."
1307 nil
a391b179 1308 "<img src=\"" _ "\">")
1caf38eb
RS
1309
1310(define-skeleton html-line
1311 "HTML line break tag."
1312 nil
1313 "<br>" \n)
1314
7e49eef2
RS
1315(define-skeleton html-ordered-list
1316 "HTML ordered list tags."
1317 nil
a391b179 1318 "<ol>" \n
7e49eef2
RS
1319 "<li>" _ \n
1320 "</ol>")
1321
1322(define-skeleton html-unordered-list
1323 "HTML unordered list tags."
1324 nil
a391b179 1325 "<ul>" \n
1caf38eb 1326 "<li>" _ \n
7e49eef2 1327 "</ul>")
1caf38eb
RS
1328
1329(define-skeleton html-list-item
1330 "HTML list item tag."
1331 nil
1332 (if (bolp) nil '\n)
1333 "<li>")
1334
1335(define-skeleton html-paragraph
1336 "HTML paragraph tag."
1337 nil
1338 (if (bolp) nil ?\n)
1339 \n "<p>")
1340
fcc3195e
RS
1341(define-skeleton html-checkboxes
1342 "Group of connected checkbox inputs."
1343 nil
a391b179
RS
1344 '(setq v1 nil
1345 v2 nil)
1346 ("Value: "
d10447ba 1347 "<input type=\"" (identity "checkbox") ; see comment above about identity
a391b179 1348 "\" name=\"" (or v1 (setq v1 (skeleton-read "Name: ")))
fcc3195e 1349 "\" value=\"" str ?\"
a391b179
RS
1350 (if (y-or-n-p "Set \"checked\" attribute? ")
1351 (funcall skeleton-transformation " checked")) ">"
1352 (skeleton-read "Text: " (capitalize str))
1353 (or v2 (setq v2 (if (y-or-n-p "Newline after text? ")
1354 (funcall skeleton-transformation "<br>")
1355 "")))
1356 \n))
fcc3195e 1357
1caf38eb
RS
1358(define-skeleton html-radio-buttons
1359 "Group of connected radio button inputs."
1360 nil
a391b179
RS
1361 '(setq v1 nil
1362 v2 (cons nil nil))
1363 ("Value: "
d10447ba 1364 "<input type=\"" (identity "radio") ; see comment above about identity
a391b179 1365 "\" name=\"" (or (car v2) (setcar v2 (skeleton-read "Name: ")))
1caf38eb 1366 "\" value=\"" str ?\"
a391b179
RS
1367 (if (and (not v1) (setq v1 (y-or-n-p "Set \"checked\" attribute? ")))
1368 (funcall skeleton-transformation " checked") ">")
1369 (skeleton-read "Text: " (capitalize str))
1370 (or (cdr v2) (setcdr v2 (if (y-or-n-p "Newline after text? ")
1371 (funcall skeleton-transformation "<br>")
1372 "")))
1373 \n))
1caf38eb 1374
72c0ae01 1375;;; sgml-mode.el ends here