(sgml-mode-common): Correct value of
[bpt/emacs.git] / lisp / textmodes / sgml-mode.el
1 ;;; sgml-mode.el --- SGML- and HTML-editing modes
2
3 ;; Copyright (C) 1992, 1995, 1996, 1998 Free Software Foundation, Inc.
4
5 ;; Author: James Clark <jjc@jclark.com>
6 ;; Adapted-By: ESR, Daniel Pfeiffer <occitan@esperanto.org>,
7 ;; F.Potorti@cnuce.cnr.it
8 ;; Keywords: wp, hypermedia, comm, languages
9
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
14 ;; the Free Software Foundation; either version 2, or (at your option)
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
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.
26
27 ;;; Commentary:
28
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.
32
33 ;;; Code:
34
35 (eval-when-compile
36 (require 'skeleton)
37 (require 'outline))
38
39 (defgroup sgml nil
40 "SGML editing mode"
41 :group 'languages)
42
43 (defcustom sgml-transformation 'identity
44 "*Default value for `skeleton-transformation' (which see) in SGML mode."
45 :type 'function
46 :group 'sgml)
47
48 (put 'sgml-transformation 'variable-interactive
49 "aTransformation function: ")
50
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
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.
60 (defvar sgml-specials '(?\")
61 "List of characters that have a special meaning for SGML mode.
62 This list is used when first loading the sgml-mode library.
63 The 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
69 When only one of ?\\\" or ?' are included, \"'\" or '\"', as can be found in
70 DTDs, start a string. To partially avoid this problem this also makes these
71 self insert as named entities depending on `sgml-quick-keys'.
72
73 Including ?- has the problem of affecting dashes that have nothing to do
74 with comments, so we normally turn it off.")
75
76 (defvar sgml-quick-keys nil
77 "Use <, >, &, SPC and `sgml-specials' keys \"electrically\" when non-nil.
78 This takes effect when first loading the sgml-mode library.")
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)
87 (define-key map "\C-c\C-n" 'sgml-name-char)
88 (define-key map "\C-c\C-t" 'sgml-tag)
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)
97 (define-key map "\C-c8" 'sgml-name-8bit-mode)
98 (define-key map "\C-c\C-v" 'sgml-validate)
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))))
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
147
148 (defcustom sgml-name-8bit-mode nil
149 "*When non-nil, insert 8 bit characters with their names."
150 :type 'boolean
151 :group 'sgml)
152
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
158 "nbsp" "excl" "quot" "num" "dollar" "percnt" "amp" "apos"
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"
177 "cedil" "sup1" "ordm" "raquo" "frac14" "frac12" "frac34" "iquest"
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
189 ;; nsgmls is a free SGML parser in the SP suite available from
190 ;; ftp.jclark.com and otherwise packaged for GNU systems.
191 ;; Its error messages can be parsed by next-error.
192 ;; The -s option suppresses output.
193
194 (defcustom sgml-validate-command "nsgmls -s" ; replaced old `sgmls'
195 "*The command to validate an SGML document.
196 The file name of current buffer file name will be appended to this,
197 separated by a space."
198 :type 'string
199 :version "21.1"
200 :group 'sgml)
201
202 (defvar sgml-saved-validate-command nil
203 "The command last used to validate in this buffer.")
204
205
206 ;;; I doubt that null end tags are used much for large elements,
207 ;;; so use a small distance here.
208 (defcustom sgml-slash-distance 1000
209 "*If non-nil, is the maximum distance to search for matching `/'."
210 :type '(choice (const nil) integer)
211 :group 'sgml)
212
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.
216 Any terminating `>' or `/' is not matched.")
217
218
219 ;; internal
220 (defconst sgml-font-lock-keywords-1
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)
224 ("<! *--.*-- *>" . font-lock-comment-face)))
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
231 "*Rules for highlighting SGML code. See also `sgml-tag-face-alist'.")
232
233 ;; internal
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.
239 When `font-lock-maximum-decoration' is 1 this is always used for fontifying.
240 When 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
250 (defcustom sgml-tag-alist
251 '(("![" ("ignore" t) ("include" t))
252 ("!attlist")
253 ("!doctype")
254 ("!element")
255 ("!entity"))
256 "*Alist of tag names for completing read and insertion rules.
257 This alist is made up as
258
259 ((\"tag\" . TAGRULE)
260 ...)
261
262 TAGRULE is a list of optionally `t' (no endtag) or `\\n' (separate endtag by
263 newlines) or a skeleton with `nil', `t' or `\\n' in place of the interactor
264 followed by an ATTRIBUTERULE (for an always present attribute) or an
265 attribute alist.
266
267 The attribute alist is made up as
268
269 ((\"attribute\" . ATTRIBUTERULE)
270 ...)
271
272 ATTRIBUTERULE is a list of optionally `t' (no value when no input) followed by
273 an optional alist of possible values."
274 :type '(repeat (cons (string :tag "Tag Name")
275 (repeat :tag "Tag Rule" sexp)))
276 :group 'sgml)
277
278 (defcustom sgml-tag-help
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"))
285 "*Alist of tag name and short description."
286 :type '(repeat (cons (string :tag "Tag Name")
287 (string :tag "Description")))
288 :group 'sgml)
289
290 (defvar v2) ; free for skeleton
291
292 (defun sgml-mode-common (sgml-tag-face-alist sgml-display-text)
293 "Common code for setting up `sgml-mode' and derived modes.
294 SGML-TAG-FACE-ALIST is used for calculating `sgml-font-lock-keywords-2'.
295 SGML-DISPLAY-TEXT sets up alternate text for when tags are invisible (see
296 varables of same name)."
297 (setq local-abbrev-table text-mode-abbrev-table)
298 (set-syntax-table sgml-mode-syntax-table)
299 (make-local-variable 'indent-line-function)
300 (make-local-variable 'paragraph-start)
301 (make-local-variable 'paragraph-separate)
302 (make-local-variable 'adaptive-fill-regexp)
303 (make-local-variable 'sgml-saved-validate-command)
304 (make-local-variable 'comment-start)
305 (make-local-variable 'comment-end)
306 (make-local-variable 'comment-indent-function)
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)
314 (make-local-variable 'sgml-font-lock-keywords-2)
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>")
324 3 (cdr (assoc (downcase (match-string 1))
325 ',sgml-tag-face-alist)))))))
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.
330 paragraph-separate "[ \t]*$\\|\
331 \[ \t]*</?\\([A-Za-z]\\([-.A-Za-z0-9= \t\n]\\|\"[^\"]*\"\\|'[^']*'\\)*\\)?>$"
332 paragraph-start "[ \t]*$\\|\
333 \[ \t]*</?\\([A-Za-z]\\([-.A-Za-z0-9= \t\n]\\|\"[^\"]*\"\\|'[^']*'\\)*\\)?>"
334 adaptive-fill-regexp "[ \t]*"
335 comment-start "<!-- "
336 comment-end " -->"
337 comment-indent-function 'sgml-comment-indent
338 skeleton-transformation sgml-transformation
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)))
345 sgml-font-lock-keywords-2 (append
346 sgml-font-lock-keywords-1
347 (cdr (assq 1 sgml-tag-face-alist)))
348 font-lock-defaults '((sgml-font-lock-keywords
349 sgml-font-lock-keywords-1
350 sgml-font-lock-keywords-2)
351 nil
352 t)
353 facemenu-add-face-function 'sgml-mode-facemenu-add-face-function)
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))))
360
361
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
371 ;;;###autoload
372 (defun sgml-mode ()
373 "Major mode for editing SGML documents.
374 Makes > match <. Makes / blink matching /.
375 Keys <, &, SPC within <>, \" and ' can be electric depending on
376 `sgml-quick-keys'.
377
378 An argument of N to a tag-inserting command means to wrap it around
379 the next N words. In Transient Mark mode, when the mark is active,
380 N defaults to -1, which means to wrap it around the current region.
381
382 If you like upcased tags, put (setq sgml-transformation 'upcase) in
383 your `.emacs' file.
384
385 Use \\[sgml-validate] to validate your document with an SGML parser.
386
387 Do \\[describe-variable] sgml- SPC to see available variables.
388 Do \\[describe-key] on the following bindings to discover what they do.
389 \\{sgml-mode-map}"
390 (interactive)
391 (kill-all-local-variables)
392 (setq mode-name "SGML"
393 major-mode 'sgml-mode)
394 (sgml-mode-common sgml-tag-face-alist sgml-display-text)
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]*\\)")
400 (use-local-map sgml-mode-map)
401 (run-hooks 'text-mode-hook 'sgml-mode-hook))
402
403
404 (defun sgml-comment-indent ()
405 (if (looking-at "--") comment-column 0))
406
407
408
409 (defun sgml-slash (arg)
410 "Insert `/' and display any previous matching `/'.
411 Two `/'s are treated as matching if the first `/' ends a net-enabling
412 start tag, and the second `/' is the corresponding null end tag."
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
452
453 (defun sgml-name-char (&optional char)
454 "Insert a symbolic character name according to `sgml-char-names'.
455 8 bit chars may be inserted with the meta key as in M-SPC for no break space,
456 or M-- for a soft hyphen."
457 (interactive "*")
458 (insert ?&)
459 (or char
460 (setq char (read-quoted-char "Enter char or octal number")))
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)
488 (setq sgml-name-8bit-mode (not sgml-name-8bit-mode))
489 (message "sgml name 8 bit mode is now %s"
490 (if sgml-name-8bit-mode "ON" "OFF")))
491
492
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").
497
498 (define-skeleton sgml-tag
499 "Prompt for a tag and insert it, optionally with attributes.
500 Completion and configuration are done according to `sgml-tag-alist'.
501 If you like tags and attributes in uppercase do \\[set-variable]
502 skeleton-transformation RET upcase RET, or put this in your `.emacs':
503 (setq sgml-transformation 'upcase)"
504 (completing-read "Tag: " sgml-tag-alist)
505 ?< str |
506 (("") -1 '(undo-boundary) (identity "&lt;")) | ; see comment above
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 ?>))))))))
521
522 (autoload 'skeleton-read "skeleton")
523
524 (defun sgml-attributes (tag &optional quiet)
525 "When at top level of a tag, interactively insert attributes.
526
527 Completion and configuration of TAG are done according to `sgml-tag-alist'.
528 If QUIET, do not print a message when there are no attributes for TAG."
529 (interactive (list (save-excursion (sgml-beginning-of-tag t))))
530 (or (stringp tag) (error "Wrong context for adding attribute"))
531 (if tag
532 (let ((completion-ignore-case t)
533 (alist (cdr (assoc (downcase tag) sgml-tag-alist)))
534 car attribute i)
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
543 (insert (if (eq (preceding-char) ? ) "" ? )
544 (funcall skeleton-transformation (car alist)))
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
552 "Attribute: "
553 alist)))))
554 (if (string= "" attribute)
555 (setq i 0)
556 (sgml-value (assoc (downcase attribute) alist))
557 (setq i (1- i))))
558 (if (eq (preceding-char) ? )
559 (delete-backward-char 1)))
560 car)))
561
562 (defun sgml-auto-attributes (arg)
563 "Self insert the character typed; at top level of tag, prompt for attributes.
564 With prefix argument, only self insert."
565 (interactive "*P")
566 (let ((point (point))
567 tag)
568 (if (or arg
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)
580 "Display description of tag TAG. If TAG is omitted, use the tag at point."
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))
590 (message "%s"
591 (or (cdr (assoc (downcase tag) sgml-tag-help))
592 (and (eq (aref tag 0) ?/)
593 (cdr (assoc (downcase (substring tag 1)) sgml-tag-help)))
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.
614 With prefix argument ARG, repeat this ARG times."
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.
630 With prefix argument ARG, repeat this ARG times.
631 Return 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.
661 With prefix argument ARG, repeat this ARG times."
662 (interactive "p")
663 (while (>= arg 1)
664 (save-excursion
665 (let* (close open)
666 (if (looking-at "[ \t\n]*<")
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))))
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))))
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)
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)
720 symbol)
721 (save-excursion
722 (goto-char (point-min))
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))
736 (put-text-property (point)
737 (progn (forward-list) (point))
738 'category 'sgml-tag))
739 (let ((pos (point)))
740 (while (< (setq pos (next-overlay-change pos)) (point-max))
741 (delete-overlay (car (overlays-at pos)))))
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)))))))
761 \f
762 (autoload 'compile-internal "compile")
763
764 (defun sgml-validate (command)
765 "Validate an SGML document.
766 Runs COMMAND, a shell command, in a separate process asynchronously
767 with output going to the buffer `*compilation*'.
768 You can then use the command \\[next-error] to find the next error message
769 and 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)
779 (save-some-buffers (not compilation-ask-about-save) nil)
780 (compile-internal command "No more errors"))
781
782
783 (defun sgml-beginning-of-tag (&optional top-level)
784 "Skip to beginning of tag and return its name.
785 If this can't be done, return t."
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)
803 "Interactively insert value taken from attributerule ALIST.
804 See `sgml-tag-alist' for info about attributerules.."
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
813 "Value: " (cdr alist))))
814 (if (string< "" alist)
815 (insert alist ?\")
816 (delete-backward-char 2))))
817 (insert "=\"")
818 (if alist
819 (insert (skeleton-read '(completing-read "Value: " alist))))
820 (insert ?\"))))
821
822 (provide 'sgml-mode)
823 \f
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
831 (defvar html-quick-keys sgml-quick-keys
832 "Use C-c X combinations for quick insertion of frequent tags when non-nil.
833 This defaults to `sgml-quick-keys'.
834 This 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")))
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)
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)
848 (define-key map "\C-c\C-co" 'html-ordered-list)
849 (define-key map "\C-c\C-cu" 'html-unordered-list)
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)
856 (if html-quick-keys
857 (progn
858 (define-key map "\C-c-" 'html-horizontal-rule)
859 (define-key map "\C-co" 'html-ordered-list)
860 (define-key map "\C-cu" 'html-unordered-list)
861 (define-key map "\C-cr" 'html-radio-buttons)
862 (define-key map "\C-cc" 'html-checkboxes)
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] '("--"))
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))
881 (define-key menu-map "l" '("Radio Buttons" . html-radio-buttons))
882 (define-key menu-map "c" '("Checkboxes" . html-checkboxes))
883 (define-key menu-map "l" '("List Item" . html-list-item))
884 (define-key menu-map "u" '("Unordered List" . html-unordered-list))
885 (define-key menu-map "o" '("Ordered List" . html-ordered-list))
886 (define-key menu-map "-" '("Horizontal Rule" . html-horizontal-rule))
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.")
932 \f
933 ;; should code exactly HTML 3 here when that is finished
934 (defvar html-tag-alist
935 (let* ((1-7 '(("1") ("2") ("3") ("4") ("5") ("6") ("7")))
936 (1-9 '(,@1-7 ("8") ("9")))
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:")
942 ("wais:") ("/cgi-bin/")))
943 (name '("name"))
944 (link `(,href
945 ("rel" ,@rel)
946 ("rev" ,@rel)
947 ("title")))
948 (list '((nil \n ( "List item: "
949 "<li>" str \n))))
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)
961 ("font" nil "size" ("-1") ("+1") ("-2") ("+2") ,@1-7)
962 ("form" (\n _ \n "<input type=\"submit\" value=\"\">")
963 ("action" ,@(cdr href)) ("method" ("get") ("post")))
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
975 ("type" ("text") ("password") ("checkbox") ("radio")
976 ("submit") ("reset"))
977 ("value"))
978 ("link" t ,@link)
979 ("menu" ,@list)
980 ("ol" ,@list ("type" ("A") ("a") ("I") ("i") ("1")))
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)
994 ("ul" ,@list ("type" ("disc") ("circle") ("square")))
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")
1025 ;("fn" "id" "fn") ; ???
1026 ("head" \n)
1027 ("html" (\n
1028 "<head>\n"
1029 "<title>" (setq str (read-input "Title: ")) "</title>\n"
1030 "</head>\n"
1031 "<body>\n<h1>" str "</h1>\n" _
1032 "\n<address>\n<a href=\"mailto:"
1033 user-mail-address
1034 "\">" (user-full-name) "</a>\n</address>\n"
1035 "</body>"
1036 ))
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")
1098 ;("fn" . "?") ; ???
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.")
1155 \f
1156 ;;;###autoload
1157 (defun html-mode ()
1158 "Major mode based on SGML mode for editing HTML documents.
1159 This allows inserting skeleton constructs used in hypertext documents with
1160 completion. See below for an introduction to HTML. Use
1161 \\[browse-url-of-buffer] to see how this comes out. See also `sgml-mode' on
1162 which this is based.
1163
1164 Do \\[describe-variable] html- SPC and \\[describe-variable] sgml- SPC to see available variables.
1165
1166 To write fairly well formatted pages you only need to know few things. Most
1167 browsers have a function to read the source code of the page being seen, so
1168 you can imitate various tricks. Here's a very short HTML primer which you
1169 can also view with a browser to see what happens:
1170
1171 <title>A Title Describing Contents</title> should be on every page. Pages can
1172 have <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
1176 ignored 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
1178 Edit/Text Properties/Face commands.
1179
1180 Pages can have <a name=\"SOMENAME\">named points</a> and can link other points
1181 to them with <a href=\"#SOMENAME\">see also somename</a>. In the same way <a
1182 href=\"URL\">see also URL</a> where URL is a filename relative to current
1183 directory, or absolute as in `http://www.cs.indiana.edu/elisp/w3/docs.html'.
1184
1185 Images in many formats can be inlined with <img src=\"URL\">.
1186
1187 If you mainly create your own documents, `sgml-specials' might be
1188 interesting. But note that some HTML 2 browsers can't handle `&apos;'.
1189 To work around that, do:
1190 (eval-after-load \"sgml-mode\" '(aset sgml-char-names ?' nil))
1191
1192 \\{html-mode-map}"
1193 (interactive)
1194 (kill-all-local-variables)
1195 (setq mode-name "HTML"
1196 major-mode 'html-mode)
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)
1205 (make-local-variable 'sentence-end)
1206 (setq sentence-end
1207 (if sentence-end-double-space
1208 "[.?!][]\"')}]*\\(<[^>]*>\\)*\\($\\| $\\|\t\\| \\)[ \t\n]*"
1209
1210 "[.?!][]\"')}]*\\(<[^>]*>\\)*\\($\\| \\|\t\\)[ \t\n]*"))
1211 (setq sgml-tag-alist html-tag-alist
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)))))
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
1221 (run-hooks 'text-mode-hook 'sgml-mode-hook 'html-mode-hook))
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.
1226 The first `match-string' should be a number from 1-9.
1227 The second `match-string' matches extra tags and is ignored.
1228 The 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)))
1244
1245 (defun html-autoview-mode (&optional arg)
1246 "Toggle automatic viewing via `browse-url-of-buffer' upon saving buffer.
1247 With positive prefix ARG always turns viewing on, with negative ARG always off.
1248 Can 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))
1255 (add-hook 'after-save-hook 'browse-url-of-buffer nil t))
1256 (message "Autoviewing turned %s."
1257 (if arg "off" "on")))
1258 \f
1259 (define-skeleton html-href-anchor
1260 "HTML anchor tag with href attribute."
1261 "URL: "
1262 '(setq input "http:")
1263 "<a href=\"" str "\">" _ "</a>")
1264
1265 (define-skeleton html-name-anchor
1266 "HTML anchor tag with name attribute."
1267 "Name: "
1268 "<a name=\"" str "\">" _ "</a>")
1269
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>")
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
1308 "<img src=\"" _ "\">")
1309
1310 (define-skeleton html-line
1311 "HTML line break tag."
1312 nil
1313 "<br>" \n)
1314
1315 (define-skeleton html-ordered-list
1316 "HTML ordered list tags."
1317 nil
1318 "<ol>" \n
1319 "<li>" _ \n
1320 "</ol>")
1321
1322 (define-skeleton html-unordered-list
1323 "HTML unordered list tags."
1324 nil
1325 "<ul>" \n
1326 "<li>" _ \n
1327 "</ul>")
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
1341 (define-skeleton html-checkboxes
1342 "Group of connected checkbox inputs."
1343 nil
1344 '(setq v1 nil
1345 v2 nil)
1346 ("Value: "
1347 "<input type=\"" (identity "checkbox") ; see comment above about identity
1348 "\" name=\"" (or v1 (setq v1 (skeleton-read "Name: ")))
1349 "\" value=\"" str ?\"
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))
1357
1358 (define-skeleton html-radio-buttons
1359 "Group of connected radio button inputs."
1360 nil
1361 '(setq v1 nil
1362 v2 (cons nil nil))
1363 ("Value: "
1364 "<input type=\"" (identity "radio") ; see comment above about identity
1365 "\" name=\"" (or (car v2) (setcar v2 (skeleton-read "Name: ")))
1366 "\" value=\"" str ?\"
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))
1374
1375 ;;; sgml-mode.el ends here