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