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