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