1 ;;; sgml-mode.el --- SGML- and HTML-editing modes
3 ;; Copyright (C) 1992, 1995, 1996 Free Software Foundation, Inc.
5 ;; Author: James Clark <jjc@clark.com>
6 ;; Adapted-By: ESR; Daniel.Pfeiffer@Informatik.START.dbp.de
7 ;; Keywords: wp, hypermedia, comm, languages
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; Configurable major mode for editing document in the SGML standard general
29 ;; markup language. As an example contains a mode for editing the derived
30 ;; HTML hypertext markup language.
34 ;; As long as Emacs' syntax can't be complemented with predicates to context
35 ;; sensitively confirm the syntax of characters, we have to live with this
36 ;; kludgy kind of tradeoff.
37 (defvar sgml-specials '(?\")
38 "List of characters that have a special meaning for sgml-mode.
39 This list is used when first loading the sgml-mode library.
40 The supported characters and potential disadvantages are:
42 ?\\\" Makes \" in text start a string.
43 ?' Makes ' in text start a string.
44 ?- Makes -- in text start a comment.
46 When only one of ?\\\" or ?' are included, \"'\" or '\"' as it can be found in
47 DTDs, start a string. To partially avoid this problem this also makes these
48 self insert as named entities depending on `sgml-quick-keys'.
50 Including ?- has the problem of affecting dashes that have nothing to do
51 with comments, so we normally turn it off.")
53 (defvar sgml-quick-keys nil
54 "Use <, >, &, SPC and `sgml-specials' keys ``electrically'' when non-nil.
55 This takes effect when first loading the library.")
59 (let (;;(map (list 'keymap (make-vector 256 nil)))
61 (menu-map (make-sparse-keymap "SGML")))
62 (define-key map "\t" 'indent-relative-maybe)
63 (define-key map "\C-c\C-i" 'sgml-tags-invisible)
64 (define-key map "/" 'sgml-slash)
65 (define-key map "\C-c\C-n" 'sgml-name-char)
66 (define-key map "\C-c\C-t" 'sgml-tag)
67 (define-key map "\C-c\C-a" 'sgml-attributes)
68 (define-key map "\C-c\C-b" 'sgml-skip-tag-backward)
69 (define-key map [?\C-c left] 'sgml-skip-tag-backward)
70 (define-key map "\C-c\C-f" 'sgml-skip-tag-forward)
71 (define-key map [?\C-c right] 'sgml-skip-tag-forward)
72 (define-key map "\C-c\C-d" 'sgml-delete-tag)
73 (define-key map "\C-c\^?" 'sgml-delete-tag)
74 (define-key map "\C-c?" 'sgml-tag-help)
75 (define-key map "\C-c8" 'sgml-name-8bit-mode)
76 (define-key map "\C-c\C-v" 'sgml-validate)
79 (define-key map "&" 'sgml-name-char)
80 (define-key map "<" 'sgml-tag)
81 (define-key map " " 'sgml-auto-attributes)
82 (define-key map ">" 'sgml-maybe-end-tag)
83 (if (memq ?\" sgml-specials)
84 (define-key map "\"" 'sgml-name-self))
85 (if (memq ?' sgml-specials)
86 (define-key map "'" 'sgml-name-self))))
90 (while (< (setq c (1+ c)) 256)
91 ;; (aset map c 'sgml-maybe-name-self)))
92 (define-key map (int-char c) 'sgml-maybe-name-self)))
93 (define-key map [menu-bar sgml] (cons "SGML" menu-map))
94 (define-key menu-map [sgml-validate] '("Validate" . sgml-validate))
95 (define-key menu-map [sgml-name-8bit-mode]
96 '("Toggle 8 Bit Insertion" . sgml-name-8bit-mode))
97 (define-key menu-map [sgml-tags-invisible]
98 '("Toggle Tag Visibility" . sgml-tags-invisible))
99 (define-key menu-map [sgml-tag-help]
100 '("Describe Tag" . sgml-tag-help))
101 (define-key menu-map [sgml-delete-tag]
102 '("Delete Tag" . sgml-delete-tag))
103 (define-key menu-map [sgml-skip-tag-forward]
104 '("Forward Tag" . sgml-skip-tag-forward))
105 (define-key menu-map [sgml-skip-tag-backward]
106 '("Backward Tag" . sgml-skip-tag-backward))
107 (define-key menu-map [sgml-attributes]
108 '("Insert Attributes" . sgml-attributes))
109 (define-key menu-map [sgml-tag] '("Insert Tag" . sgml-tag))
111 "Keymap for SGML mode. See also `sgml-specials'.")
114 (defvar sgml-mode-syntax-table
115 (let ((table (copy-syntax-table text-mode-syntax-table)))
116 (modify-syntax-entry ?< "(>" table)
117 (modify-syntax-entry ?> ")<" table)
118 (if (memq ?- sgml-specials)
119 (modify-syntax-entry ?- "_ 1234" table))
120 (if (memq ?\" sgml-specials)
121 (modify-syntax-entry ?\" "\"\"" table))
122 (if (memq ?' sgml-specials)
123 (modify-syntax-entry ?\' "\"'" table))
125 "Syntax table used in SGML mode. See also `sgml-specials'.")
128 (defvar sgml-name-8bit-mode nil
129 "*When non-`nil' insert 8 bit characters with their names.")
131 (defvar sgml-char-names
132 [nil nil nil nil nil nil nil nil
133 nil nil nil nil nil nil nil nil
134 nil nil nil nil nil nil nil nil
135 nil nil nil nil nil nil nil nil
136 "ensp" "excl" "quot" "num" "dollar" "percnt" "amp" "apos"
137 "lpar" "rpar" "ast" "plus" "comma" "hyphen" "period" "sol"
138 nil nil nil nil nil nil nil nil
139 nil nil "colon" "semi" "lt" "eq" "gt" "quest"
140 "commat" nil nil nil nil nil nil nil
141 nil nil nil nil nil nil nil nil
142 nil nil nil nil nil nil nil nil
143 nil nil nil "lsqb" nil "rsqb" "uarr" "lowbar"
144 "lsquo" nil nil nil nil nil nil nil
145 nil nil nil nil nil nil nil nil
146 nil nil nil nil nil nil nil nil
147 nil nil nil "lcub" "verbar" "rcub" "tilde" nil
148 nil nil nil nil nil nil nil nil
149 nil nil nil nil nil nil nil nil
150 nil nil nil nil nil nil nil nil
151 nil nil nil nil nil nil nil nil
152 "nbsp" "iexcl" "cent" "pound" "curren" "yen" "brvbar" "sect"
153 "uml" "copy" "ordf" "laquo" "not" "shy" "reg" "macr"
154 "ring" "plusmn" "sup2" "sup3" "acute" "micro" "para" "middot"
155 "cedil" "sup1" "ordm" "raquo" "frac14" "half" "frac34" "iquest"
156 "Agrave" "Aacute" "Acirc" "Atilde" "Auml" "Aring" "AElig" "Ccedil"
157 "Egrave" "Eacute" "Ecirc" "Euml" "Igrave" "Iacute" "Icirc" "Iuml"
158 "ETH" "Ntilde" "Ograve" "Oacute" "Ocirc" "Otilde" "Ouml" nil
159 "Oslash" "Ugrave" "Uacute" "Ucirc" "Uuml" "Yacute" "THORN" "szlig"
160 "agrave" "aacute" "acirc" "atilde" "auml" "aring" "aelig" "ccedil"
161 "egrave" "eacute" "ecirc" "euml" "igrave" "iacute" "icirc" "iuml"
162 "eth" "ntilde" "ograve" "oacute" "ocirc" "otilde" "ouml" "divide"
163 "oslash" "ugrave" "uacute" "ucirc" "uuml" "yacute" "thorn" "yuml"]
164 "Vector of symbolic character names without `&' and `;'.")
167 ;; sgmls is a free SGML parser available from
168 ;; ftp.uu.net:pub/text-processing/sgml
169 ;; Its error messages can be parsed by next-error.
170 ;; The -s option suppresses output.
172 (defvar sgml-validate-command "sgmls -s"
173 "*The command to validate an SGML document.
174 The file name of current buffer file name will be appended to this,
175 separated by a space.")
177 (defvar sgml-saved-validate-command nil
178 "The command last used to validate in this buffer.")
181 ;;; I doubt that null end tags are used much for large elements,
182 ;;; so use a small distance here.
183 (defconst sgml-slash-distance 1000
184 "*If non-nil, is the maximum distance to search for matching /.")
186 (defconst sgml-start-tag-regex
187 "<[A-Za-z]\\([-.A-Za-z0-9= \n\t]\\|\"[^\"]*\"\\|'[^']*'\\)*"
188 "Regular expression that matches a non-empty start tag.
189 Any terminating > or / is not matched.")
192 (defvar sgml-font-lock-keywords
193 '(("<\\([!?][a-z0-9]+\\)" 1 font-lock-keyword-face)
194 ("<\\(/?[a-z0-9]+\\)" 1 font-lock-function-name-face)
195 ("[&%][-.A-Za-z0-9]+;?" . font-lock-variable-name-face)
196 ("<!--[^<>]*-->" . font-lock-comment-face))
197 "*Rules for highlighting SGML code. See also `sgml-tag-face-alist'.")
200 (defvar sgml-font-lock-keywords-1 ())
202 (defvar sgml-face-tag-alist ()
203 "Alist of face and tag name for facemenu.")
205 (defvar sgml-tag-face-alist ()
206 "Tag names and face or list of faces to fontify with when invisible.
207 When `font-lock-maximum-decoration' is 1 this is always used for fontifying.
208 When more these are fontified together with `sgml-font-lock-keywords'.")
211 (defvar sgml-display-text ()
212 "Tag names as lowercase symbols, and display string when invisible.")
215 (defvar sgml-tags-invisible nil)
218 (defvar sgml-tag-alist
219 '(("![" ("ignore" t) ("include" t))
224 "*Alist of tag names for completing read and insertion rules.
225 This alist is made up as
230 TAGRULE is a list of optionally `t' (no endtag) or `\\n' (separate endtag by
231 newlines) or a skeleton with `nil', `t' or `\\n' in place of the interactor
232 followed by an ATTRIBUTERULE (for an always present attribute) or an
235 The attribute alist is made up as
237 ((\"attribute\" . ATTRIBUTERULE)
240 ATTRIBUTERULE is a list of optionally `t' (no value when no input) followed by
241 an optional alist of possible values.")
243 (defvar sgml-tag-help
244 '(("!" . "Empty declaration for comment")
245 ("![" . "Embed declarations with parser directive")
246 ("!attlist" . "Tag attributes declaration")
247 ("!doctype" . "Document type (DTD) declaration")
248 ("!element" . "Tag declaration")
249 ("!entity" . "Entity (macro) declaration"))
250 "*Alist of tag name and short description.")
253 ;; put read-only last to enable setting this even when read-only enabled
254 (or (get 'sgml-tag 'invisible)
256 (append '(invisible t
258 point-entered sgml-point-entered
260 (symbol-plist 'sgml-tag))))
264 (defun sgml-mode-common (sgml-tag-face-alist sgml-display-text)
265 "Common code for setting up `sgml-mode' and derived modes.
266 SGML-TAG-FACE-ALIST is used for calculating `sgml-font-lock-keywords-1'.
267 SGML-DISPLAY-TEXT sets up alternate text for when tags are invisible (see
268 variables of same name)."
269 (kill-all-local-variables)
270 (setq local-abbrev-table text-mode-abbrev-table)
271 (set-syntax-table sgml-mode-syntax-table)
272 (make-local-variable 'indent-line-function)
273 (make-local-variable 'paragraph-start)
274 (make-local-variable 'paragraph-separate)
275 (make-local-variable 'sgml-saved-validate-command)
276 (make-local-variable 'comment-start)
277 (make-local-variable 'comment-end)
278 (make-local-variable 'comment-indent-function)
279 (make-local-variable 'comment-start-skip)
280 (make-local-variable 'comment-indent-function)
281 (make-local-variable 'sgml-tags-invisible)
282 (make-local-variable 'skeleton-transformation)
283 (make-local-variable 'skeleton-further-elements)
284 (make-local-variable 'skeleton-end-hook)
285 (make-local-variable 'font-lock-defaults)
286 (make-local-variable 'sgml-font-lock-keywords-1)
287 (make-local-variable 'facemenu-add-face-function)
288 (make-local-variable 'facemenu-end-add-face)
289 ;;(make-local-variable 'facemenu-remove-face-function)
290 (and sgml-tag-face-alist
291 (not (assq 1 sgml-tag-face-alist))
292 (nconc sgml-tag-face-alist
293 `((1 (,(concat "<\\("
294 (mapconcat 'car sgml-tag-face-alist "\\|")
295 "\\)\\([ \t].+\\)?>\\(.+\\)</\\1>")
296 3 (cdr (assoc (match-string 1) ',sgml-tag-face-alist)))))))
297 (setq indent-line-function 'indent-relative-maybe
298 ;; A start or end tag by itself on a line separates a paragraph.
299 ;; This is desirable because SGML discards a newline that appears
300 ;; immediately after a start tag or immediately before an end tag.
301 paragraph-start "^[ \t\n]\\|\
302 \\(</?\\([A-Za-z]\\([-.A-Za-z0-9= \t\n]\\|\"[^\"]*\"\\|'[^']*'\\)*\\)?>$\\)"
303 paragraph-separate "^[ \t\n]*$\\|\
304 ^</?\\([A-Za-z]\\([-.A-Za-z0-9= \t\n]\\|\"[^\"]*\"\\|'[^']*'\\)*\\)?>$"
305 comment-start "<!-- "
307 comment-indent-function 'sgml-comment-indent
308 ;; This will allow existing comments within declarations to be
310 comment-start-skip "--[ \t]*"
311 skeleton-transformation 'identity
312 skeleton-further-elements '((completion-ignore-case t))
313 skeleton-end-hook (lambda ()
316 (eq (car-safe v2) '\n)))
317 (newline-and-indent)))
318 sgml-font-lock-keywords-1 (cdr (assq 1 sgml-tag-face-alist))
319 font-lock-defaults '((sgml-font-lock-keywords
320 sgml-font-lock-keywords-1)
323 facemenu-add-face-function
325 (if (setq face (cdr (assq face sgml-face-tag-alist)))
327 (setq facemenu-end-add-face (concat "</" face ">"))
328 (concat "<" face ">"))
329 (error "Face not configured for %s mode." mode-name))))
330 (while sgml-display-text
331 (put (car (car sgml-display-text)) 'before-string
332 (cdr (car sgml-display-text)))
333 (setq sgml-display-text (cdr sgml-display-text)))
334 (run-hooks 'text-mode-hook 'sgml-mode-hook))
336 ;; Conflicts with psgml, don't autoload
338 (defun sgml-mode (&optional function)
339 "Major mode for editing SGML documents.
340 Makes > match <. Makes / blink matching /.
341 Keys <, &, SPC within <>, \" and ' can be electric depending on
344 Do \\[describe-variable] sgml- SPC to see available variables.
346 Use \\[sgml-validate] to validate your document with an SGML parser.
349 (sgml-mode-common sgml-tag-face-alist sgml-display-text)
350 (use-local-map sgml-mode-map)
351 (setq mode-name "SGML"
352 major-mode 'sgml-mode))
356 (defun sgml-comment-indent ()
357 (if (and (looking-at "--")
358 (not (and (eq (preceding-char) ?!)
359 (eq (char-after (- (point) 2)) ?<))))
361 (skip-chars-backward " \t")
362 (max comment-column (1+ (current-column))))
367 (defun sgml-slash (arg)
368 "Insert / and display any previous matching /.
369 Two /s are treated as matching if the first / ends a net-enabling
370 start tag, and the second / is the corresponding null end tag."
374 (let ((oldpos (point))
379 (if sgml-slash-distance
380 (narrow-to-region (max (point-min)
381 (- (point) sgml-slash-distance))
383 (if (and (re-search-backward sgml-start-tag-regex (point-min) t)
384 (eq (match-end 0) (1- oldpos)))
386 (goto-char (1- oldpos))
387 (while (and (not blinkpos)
388 (search-backward "/" (point-min) t))
389 (let ((tagend (save-excursion
390 (if (re-search-backward sgml-start-tag-regex
394 (if (eq tagend (point))
396 (setq blinkpos (point))
397 (setq level (1- level)))
398 (setq level (1+ level)))))))
402 (if (pos-visible-in-window-p)
404 (message "Matches %s"
405 (buffer-substring (progn
408 (1+ blinkpos))))))))))
411 (defun sgml-name-char (&optional char)
412 "Insert a symbolic character name according to `sgml-char-names'.
413 8 bit chars may be inserted with the meta key as in M-SPC for no break space,
414 or M-- for a soft hyphen."
418 (setq char (read-quoted-char)))
419 (delete-backward-char 1)
422 (delete-backward-char 1)
424 (or (aref sgml-char-names char)
429 (defun sgml-name-self ()
430 "Insert a symbolic character name according to `sgml-char-names'."
432 (sgml-name-char last-command-char))
435 (defun sgml-maybe-name-self ()
436 "Insert a symbolic character name according to `sgml-char-names'."
438 (if sgml-name-8bit-mode
439 (sgml-name-char last-command-char)
440 (self-insert-command 1)))
443 (defun sgml-name-8bit-mode ()
444 "Toggle insertion of 8 bit characters."
446 (setq sgml-name-8bit-mode (not sgml-name-8bit-mode)))
450 (define-skeleton sgml-tag
451 "Insert a tag you are prompted for, optionally with attributes.
452 Completion and configuration is according to `sgml-tag-alist'.
453 If you like tags and attributes in uppercase set `skeleton-transformation'
455 (funcall skeleton-transformation
456 (completing-read "Tag: " sgml-tag-alist))
457 ?< (setq v1 (eval str)) |
458 (("") -1 '(undo-boundary) "<") |
459 (("") '(setq v2 (sgml-attributes v1 t)) ?>
460 (if (string= "![" v1)
461 (prog1 '(("") " [ " _ " ]]")
464 (string-match "^[/!?]" v1))
467 '(("") v2 _ v2 "</" v1 ?>)
469 (cons '("") (cdr v2))
470 (append '(("") (car v2))
472 '(resume: (car v2) _ "</" v1 ?>))))))))
474 (autoload 'skeleton-read "skeleton")
476 (defun sgml-attributes (alist &optional quiet)
477 "When at toplevel of a tag, interactively insert attributes."
478 (interactive (list (save-excursion (sgml-beginning-of-tag t))))
479 (or (stringp alist) (error "Wrong context for adding attribute"))
481 (let ((completion-ignore-case t)
483 (setq alist (cdr (assoc (downcase alist) sgml-tag-alist)))
484 (if (or (symbolp (car alist))
485 (symbolp (car (car alist))))
486 (setq car (car alist)
489 (message "No attributes configured."))
490 (if (stringp (car alist))
492 (insert (if (eq (preceding-char) ? ) "" ? ) (car alist))
494 (setq i (length alist))
497 (insert (funcall skeleton-transformation
499 (skeleton-read '(completing-read
502 (if (string= "" attribute)
504 (sgml-value (assoc attribute alist))
506 (if (eq (preceding-char) ? )
507 (delete-backward-char 1)))
510 (defun sgml-auto-attributes (arg)
511 "Self insert, except, when at top level of tag, prompt for attributes.
512 With prefix ARG only self insert."
514 (let ((point (point))
517 (not sgml-tag-alist) ; no message when nothing configured
518 (symbolp (setq tag (save-excursion (sgml-beginning-of-tag t))))
519 (eq (aref tag 0) ?/))
520 (self-insert-command (prefix-numeric-value arg))
521 (sgml-attributes tag)
522 (setq last-command-char ? )
523 (or (> (point) point)
524 (self-insert-command 1)))))
527 (defun sgml-tag-help (&optional tag)
528 "Display description of optional TAG or tag at point."
532 (if (eq (following-char) ?<)
534 (setq tag (sgml-beginning-of-tag))))
536 (error "No tag selected"))
537 (setq tag (downcase tag))
539 (or (cdr (assoc tag sgml-tag-help))
540 (and (eq (aref tag 0) ?/)
541 (cdr (assoc (substring tag 1) sgml-tag-help)))
542 "No description available")))
545 (defun sgml-maybe-end-tag ()
546 "Name self unless in position to end a tag."
548 (or (condition-case nil
549 (save-excursion (up-list -1))
555 (save-excursion (up-list 1))
557 (error (self-insert-command 1)))))
560 (defun sgml-skip-tag-backward (arg)
561 "Skip to beginning of tag or matching opening tag if present.
562 With prefix ARG, repeat that many times."
565 (search-backward "<" nil t)
566 (if (looking-at "</\\([^ \n\t>]+\\)")
567 ;; end tag, skip any nested pairs
568 (let ((case-fold-search t)
569 (re (concat "</?" (regexp-quote (match-string 1)))))
570 (while (and (re-search-backward re nil t)
571 (eq (char-after (1+ (point))) ?/))
573 (sgml-skip-tag-backward 1))))
574 (setq arg (1- arg))))
576 (defun sgml-skip-tag-forward (arg &optional return)
577 "Skip to end of tag or matching closing tag if present.
578 With prefix ARG, repeat that many times.
579 Return t iff after a closing tag."
583 (skip-chars-forward "^<>")
584 (if (eq (following-char) ?>)
586 (if (looking-at "<\\([^/ \n\t>]+\\)")
587 ;; start tag, skip any nested same pairs _and_ closing tag
588 (let ((case-fold-search t)
589 (re (concat "</?" (regexp-quote (match-string 1))))
593 (while (and (re-search-forward re nil t)
595 (eq (char-after (1+ (match-beginning 0))) ?/)))
597 (sgml-skip-tag-forward 1))
607 (defun sgml-delete-tag (arg)
608 "Delete tag on or after cursor, and matching closing or opening tag.
609 With prefix ARG, repeat that many times."
614 (if (looking-at "[ \t\n]*<")
616 (if (eq (char-after (match-end 0)) ?/)
620 (goto-char (match-end 0))))
622 (or (save-excursion (setq close (sgml-beginning-of-tag)
623 close (and (stringp close)
624 (eq (aref close 0) ?/)
626 ;; not on closing tag
627 (let ((point (point)))
628 (sgml-skip-tag-backward 1)
629 (if (or (not (eq (following-char) ?<))
633 (error "Not on or before tag")))))
636 (sgml-skip-tag-backward 1)
641 (sgml-skip-tag-forward 1)
644 (if (eq (aref (sgml-beginning-of-tag) 0) ?/)
648 (setq arg (1- arg))))
652 (defun sgml-tags-invisible (arg)
653 "Toggle visibility of existing tags."
655 (let ((modified (buffer-modified-p))
656 (inhibit-read-only t)
661 (if (setq sgml-tags-invisible
663 (>= (prefix-numeric-value arg) 0)
664 (not sgml-tags-invisible)))
665 (while (re-search-forward "<\\([!/?A-Za-z][-A-Za-z0-9]*\\)"
667 (setq symbol (intern-soft (downcase (match-string 1))))
668 (goto-char (match-beginning 0))
669 (and (get symbol 'before-string)
670 (not (overlays-at (point)))
671 (overlay-put (make-overlay (point)
674 (put-text-property (setq point (point)) (forward-list)
676 (put-text-property point (point)
677 'category 'sgml-tag))
678 (while (< (setq point (next-overlay-change point)) (point-max))
679 (delete-overlay (car (overlays-at point))))
680 (remove-text-properties (point-min) (point-max)
681 '(category sgml-tag intangible t))))
682 (set-buffer-modified-p modified)
683 (run-hooks 'sgml-tags-invisible-hook)
686 (defun sgml-point-entered (x y)
687 ;; Show preceding or following hidden tag, depending of cursor direction.
688 (let ((inhibit-point-motion-hooks t))
690 (message "Invisible tag: %s"
694 (not (eq (following-char) ?<)))
696 (eq (preceding-char) ?>)))
701 (autoload 'compile-internal "compile")
703 (defun sgml-validate (command)
704 "Validate an SGML document.
705 Runs COMMAND, a shell command, in a separate process asynchronously
706 with output going to the buffer *compilation*.
707 You can then use the command \\[next-error] to find the next error message
708 and move to the line in the SGML document that caused it."
710 (list (read-string "Validate command: "
711 (or sgml-saved-validate-command
712 (concat sgml-validate-command
714 (let ((name (buffer-file-name)))
716 (file-name-nondirectory name))))))))
717 (setq sgml-saved-validate-command command)
718 (if (or (not compilation-ask-about-save)
719 (y-or-n-p (message "Save buffer %s? " (buffer-name))))
721 (compile-internal command "No more errors"))
724 (defun sgml-beginning-of-tag (&optional top-level)
725 "Skip to beginning of tag and return its name.
732 (if (search-backward "<" nil t)
737 (if (looking-at "<[!?/]?[[A-Za-z][A-Za-z0-9]*")
738 (buffer-substring-no-properties
743 (defun sgml-value (alist)
744 (setq alist (cdr alist))
745 (if (stringp (car alist))
746 (insert "=\"" (car alist) ?\")
747 (if (eq (car alist) t)
751 (setq alist (skeleton-read '(completing-read
752 "[Value]: " (cdr alist))))
753 (if (string< "" alist)
754 (insert (funcall skeleton-transformation alist) ?\")
755 (delete-backward-char 2))))
758 (insert (funcall skeleton-transformation
759 (skeleton-read '(completing-read "Value: " alist)))))
764 (defvar html-quick-keys sgml-quick-keys
765 "Use C-c X combinations for quick insertion of frequent tags when non-nil.
766 This defaults to `sgml-quick-keys'.
767 This takes effect when first loading the library.")
769 (defvar html-mode-map
770 (let (; (map (nconc (make-sparse-keymap) sgml-mode-map))
771 (map (copy-keymap sgml-mode-map))
772 (menu-map (make-sparse-keymap "HTML")))
773 (define-key map "\C-c6" 'html-headline-6)
774 (define-key map "\C-c5" 'html-headline-5)
775 (define-key map "\C-c4" 'html-headline-4)
776 (define-key map "\C-c3" 'html-headline-3)
777 (define-key map "\C-c2" 'html-headline-2)
778 (define-key map "\C-c1" 'html-headline-1)
779 (define-key map "\C-c\r" 'html-paragraph)
780 (define-key map "\C-c\n" 'html-line)
781 (define-key map "\C-c\C-c-" 'html-horizontal-rule)
782 (define-key map "\C-c\C-co" 'html-ordered-list)
783 (define-key map "\C-c\C-cu" 'html-unordered-list)
784 (define-key map "\C-c\C-cr" 'html-radio-buttons)
785 (define-key map "\C-c\C-cc" 'html-checkboxes)
786 (define-key map "\C-c\C-cl" 'html-list-item)
787 (define-key map "\C-c\C-ch" 'html-href-anchor)
788 (define-key map "\C-c\C-cn" 'html-name-anchor)
789 (define-key map "\C-c\C-ci" 'html-image)
792 (define-key map "\C-c-" 'html-horizontal-rule)
793 (define-key map "\C-co" 'html-ordered-list)
794 (define-key map "\C-cu" 'html-unordered-list)
795 (define-key map "\C-cr" 'html-radio-buttons)
796 (define-key map "\C-cc" 'html-checkboxes)
797 (define-key map "\C-cl" 'html-list-item)
798 (define-key map "\C-ch" 'html-href-anchor)
799 (define-key map "\C-cn" 'html-name-anchor)
800 (define-key map "\C-ci" 'html-image)))
801 (define-key map "\C-c\C-s" 'html-autoview-mode)
802 (define-key map "\C-c\C-v" 'browse-url-of-buffer)
803 (define-key map [menu-bar html] (cons "HTML" menu-map))
804 (define-key menu-map [html-autoview-mode]
805 '("Toggle Autoviewing" . html-autoview-mode))
806 (define-key menu-map [browse-url-of-buffer]
807 '("View Buffer Contents" . browse-url-of-buffer))
808 (define-key menu-map [nil] '("--"))
809 ;;(define-key menu-map "6" '("Heading 6" . html-headline-6))
810 ;;(define-key menu-map "5" '("Heading 5" . html-headline-5))
811 ;;(define-key menu-map "4" '("Heading 4" . html-headline-4))
812 (define-key menu-map "3" '("Heading 3" . html-headline-3))
813 (define-key menu-map "2" '("Heading 2" . html-headline-2))
814 (define-key menu-map "1" '("Heading 1" . html-headline-1))
815 (define-key menu-map "l" '("Radio Buttons" . html-radio-buttons))
816 (define-key menu-map "c" '("Checkboxes" . html-checkboxes))
817 (define-key menu-map "l" '("List Item" . html-list-item))
818 (define-key menu-map "u" '("Unordered List" . html-unordered-list))
819 (define-key menu-map "o" '("Ordered List" . html-ordered-list))
820 (define-key menu-map "-" '("Horizontal Rule" . html-horizontal-rule))
821 (define-key menu-map "\n" '("Line Break" . html-line))
822 (define-key menu-map "\r" '("Paragraph" . html-paragraph))
823 (define-key menu-map "i" '("Image" . html-image))
824 (define-key menu-map "h" '("Href Anchor" . html-href-anchor))
825 (define-key menu-map "n" '("Name Anchor" . html-name-anchor))
827 "Keymap for commands for use in HTML mode.")
830 (defvar html-face-tag-alist
835 "Value of `sgml-face-tag-alist' for HTML mode.")
837 (defvar html-tag-face-alist
840 ("blink" . highlight)
843 ("h1" bold underline)
844 ("h2" bold-italic underline)
845 ("h3" italic underline)
854 ("title" bold underline)
858 "Value of `sgml-tag-face-alist' for HTML mode.")
861 (defvar html-display-text
865 "Value of `sgml-display-text' for HTML mode.")
868 ; should code exactly HTML 3 here when that is finished
869 (defvar html-tag-alist
870 (let* ((1-9 '(("8") ("9")
871 ("1") ("2") ("3") ("4") ("5") ("6") ("7")))
872 (align '(("align" ("left") ("center") ("right"))))
873 (valign '(("top") ("middle") ("bottom") ("baseline")))
874 (rel '(("next") ("previous") ("parent") ("subdocument") ("made")))
875 (href '("href" ("ftp:") ("file:") ("finger:") ("gopher:") ("http:")
876 ("mailto:") ("news:") ("rlogin:") ("telnet:") ("tn3270:")
877 ("wais:") ("/cgi-bin/")))
886 ("type" ("A") ("a") ("I") ("i") ("1"))))
893 ;; put ,-expressions first, else byte-compile chokes (as of V19.29)
894 ;; and like this it's more efficient anyway
898 ("font" ("size" ("-1") ("+1") ("-2") ("+2") ,@(cdr (cdr 1-9))))
899 ("form" (\n _ \n "<input type=\"submit\" value=\"\">")
900 ("action" ,@(cdr href)) ("method" ("get") ("post")))
907 ("hr" t ("size" ,@1-9) ("width") ("noshade" t) ,@align)
908 ("img" t ("align" ,@valign ("texttop") ("absmiddle") ("absbottom"))
909 ("src") ("alt") ("width" "1") ("height" "1")
910 ("border" "1") ("vspace" "1") ("hspace" "1") ("ismap" t))
911 ("input" t ("size" ,@1-9) ("maxlength" ,@1-9) ("checked" t) ,name
912 ("type" ("text") ("password") ("checkbox") ("radio")
913 ("submit") ("reset"))
922 ,name ("size" ,@1-9) ("multiple" t))
924 ((completing-read "Cell kind: " '(("td") ("th"))
926 "<tr><" str ?> _ \n))
927 ("border" t ,@1-9) ("width" "10") ("cellpadding"))
929 ("textarea" ,name ("rows" ,@1-9) ("cols" ,@1-9))
939 ("Item: " "<item>" str \n))
946 ("body" \n ("background" ".gif") ("bgcolor" "#") ("text" "#")
947 ("link" "#") ("alink" "#") ("vlink" "#"))
948 ("box" (nil _ "<over>" _))
949 ("br" t ("clear" ("left") ("right")))
950 ("caption" ("valign" ("top") ("bottom")))
959 "<dt>" str "<dd>" _ \n)))
966 "<title>" (setq str (read-input "Title: ")) "</title>\n"
967 "<body>\n<h1>" str "</h1>\n" _
968 "\n<address>\n<a href=\"mailto:"
970 "\">" (user-full-name) "</a>\n</address>"))
973 ("isindex" t ("action") ("prompt"))
979 ("option" t ("value") ("label") ("selected" t))
997 "*Value of `sgml-tag-alist' for HTML mode.")
999 (defvar html-tag-help
1001 ("a" . "Anchor of point or link elsewhere")
1004 ("address" . "Formatted mail address")
1005 ("array" . "Math array")
1008 ("base" . "Base address for URLs")
1009 ("big" . "Font size")
1010 ("blink" . "Blinking text")
1011 ("blockquote" . "Indented quotation")
1012 ("body" . "Document body")
1013 ("box" . "Math fraction")
1014 ("br" . "Line break")
1015 ("caption" . "Table caption")
1016 ("center" . "Centered text")
1017 ("changed" . "Change bars")
1018 ("cite" . "Citation of a document")
1019 ("code" . "Formatted source code")
1020 ("dd" . "Definition of term")
1023 ("dir" . "Directory list (obsolete)")
1024 ("dl" . "Definition list")
1025 ("dt" . "Term to be definined")
1026 ("em" . "Emphasised")
1027 ("embed" . "Embedded data in foreign format")
1029 ("figa" . "Figure anchor")
1030 ("figd" . "Figure description")
1031 ("figt" . "Figure text")
1033 ("font" . "Font size")
1034 ("form" . "Form with input fields")
1035 ("group" . "Document grouping")
1036 ("h1" . "Most important section headline")
1037 ("h2" . "Important section headline")
1038 ("h3" . "Section headline")
1039 ("h4" . "Minor section headline")
1040 ("h5" . "Unimportant section headline")
1041 ("h6" . "Least important section headline")
1042 ("head" . "Document header")
1043 ("hr" . "Horizontal rule")
1044 ("html" . "HTML Document")
1045 ("i" . "Italic face")
1046 ("img" . "Graphic image")
1047 ("input" . "Form input field")
1049 ("isindex" . "Input field for index search")
1050 ("kbd" . "Keybard example face")
1051 ("lang" . "Natural language")
1052 ("li" . "List item")
1053 ("link" . "Link relationship")
1054 ("math" . "Math formula")
1055 ("menu" . "Menu list (obsolete)")
1056 ("mh" . "Form mail header")
1057 ("nextid" . "Allocate new id")
1058 ("nobr" . "Text without line break")
1059 ("ol" . "Ordered list")
1060 ("option" . "Selection list item")
1061 ("over" . "Math fraction rule")
1062 ("p" . "Paragraph start")
1063 ("panel" . "Floating panel")
1065 ("pre" . "Preformatted fixed width text")
1067 ("rev" . "Reverse video")
1069 ("samp" . "Sample text")
1070 ("select" . "Selection list")
1071 ("small" . "Font size")
1072 ("sp" . "Nobreak space")
1073 ("strong" . "Standout text")
1074 ("sub" . "Subscript")
1075 ("sup" . "Superscript")
1076 ("table" . "Table with rows and columns")
1077 ("tb" . "Table vertical break")
1078 ("td" . "Table data cell")
1079 ("textarea" . "Form multiline edit area")
1080 ("th" . "Table header cell")
1081 ("title" . "Document title")
1082 ("tr" . "Table row separator")
1083 ("tt" . "Typewriter face")
1084 ("u" . "Underlined text")
1085 ("ul" . "Unordered list")
1086 ("var" . "Math variable face")
1087 ("wbr" . "Enable <br> within <nobr>"))
1088 "*Value of `sgml-tag-help' for HTML mode.")
1091 ;; Conflicts with psgml, don't autoload
1094 "Major mode based on SGML mode for editing HTML documents.
1095 This allows inserting skeleton constructs used in hypertext documents with
1096 completion. See below for an introduction to HTML. Use
1097 \\[browse-url-of-buffer] to see how this comes out. See also `sgml-mode' on
1098 which this is based.
1100 Do \\[describe-variable] html- SPC and \\[describe-variable] sgml- SPC to see available variables.
1102 To write fairly well formatted pages you only need to know few things. Most
1103 browsers have a function to read the source code of the page being seen, so
1104 you can imitate various tricks. Here's a very short HTML primer which you
1105 can also view with a browser to see what happens:
1107 <title>A Title Describing Contents</title> should be on every page. Pages can
1108 have <h1>Very Major Headlines</h1> through <h6>Very Minor Headlines</h6>
1109 <hr> Parts can be separated with horizontal rules.
1111 <p>Paragraphs only need an opening tag. Line breaks and multiple spaces are
1112 ignored unless the text is <pre>preformatted.</pre> Text can be marked as
1113 <b>bold</b>, <i>italic</i> or <u>underlined</u> using the normal M-g or
1114 Edit/Text Properties/Face commands.
1116 Pages can have <a name=\"SOMENAME\">named points</a> and can link other points
1117 to them with <a href=\"#SOMENAME\">see also somename</a>. In the same way <a
1118 href=\"URL\">see also URL</a> where URL is a filename relative to current
1119 directory or something like http://www.cs.indiana.edu/elisp/w3/docs.html.
1121 Images in many formats can be inlined with <img src=\"URL\">.
1123 If you mainly create your own documents, `sgml-specials' might be interesting.
1124 But note that some HTML 2 browsers can't handle '. To work around that
1127 \(eval-after-load \"sgml-mode\" '(aset sgml-char-names ?' nil))
1130 (sgml-mode-common html-tag-face-alist html-display-text)
1131 (use-local-map html-mode-map)
1132 (make-local-variable 'sgml-tag-alist)
1133 (make-local-variable 'sgml-face-tag-alist)
1134 (make-local-variable 'sgml-tag-help)
1135 (make-local-variable 'outline-regexp)
1136 (make-local-variable 'outline-heading-end-regexp)
1137 (make-local-variable 'outline-level)
1138 (make-local-variable 'sentence-end)
1140 "[.?!][]\"')}]*\\(<[^>]*>\\)*\\($\\| $\\|\t\\| \\)[ \t\n]*")
1141 (setq mode-name "HTML"
1142 major-mode 'html-mode
1143 sgml-tag-alist html-tag-alist
1144 sgml-face-tag-alist html-face-tag-alist
1145 sgml-tag-help html-tag-help
1146 outline-regexp "^.*<[Hh][1-6]\\>"
1147 outline-heading-end-regexp "</[Hh][1-6]>"
1148 outline-level (lambda ()
1149 (char-after (1- (match-end 0)))))
1150 (run-hooks 'html-mode-hook))
1153 (define-skeleton html-href-anchor
1154 "HTML anchor tag with href attribute."
1156 "<a href=\"http:" _ "\"></a>")
1158 (define-skeleton html-name-anchor
1159 "HTML anchor tag with name attribute."
1161 "<a name=\"" _ "\"></a>")
1163 (define-skeleton html-headline-1
1164 "HTML level 1 headline tags."
1168 (define-skeleton html-headline-2
1169 "HTML level 2 headline tags."
1173 (define-skeleton html-headline-3
1174 "HTML level 3 headline tags."
1178 (define-skeleton html-headline-4
1179 "HTML level 4 headline tags."
1183 (define-skeleton html-headline-5
1184 "HTML level 5 headline tags."
1188 (define-skeleton html-headline-6
1189 "HTML level 6 headline tags."
1193 (define-skeleton html-horizontal-rule
1194 "HTML horizontal rule tag."
1198 (define-skeleton html-image
1201 "<img src=\"http:" _ "\">")
1203 (define-skeleton html-line
1204 "HTML line break tag."
1208 (define-skeleton html-ordered-list
1209 "HTML ordered list tags."
1215 (define-skeleton html-unordered-list
1216 "HTML unordered list tags."
1222 (define-skeleton html-list-item
1223 "HTML list item tag."
1228 (define-skeleton html-paragraph
1229 "HTML paragraph tag."
1234 (define-skeleton html-checkboxes
1235 "Group of connected checkbox inputs."
1237 '(setq v1 (eval str)) ; allow passing name as argument
1239 "<input type=\"checkbox\" name=\""
1240 (or v1 (setq v1 (skeleton-read "Name: ")))
1241 "\" value=\"" str ?\"
1242 (if v2 "" " checked") ?> str
1243 (or v2 (setq v2 (if (y-or-n-p "Newline? ") "<br>" ""))) \n))
1245 (define-skeleton html-radio-buttons
1246 "Group of connected radio button inputs."
1248 '(setq v1 (eval str)) ; allow passing name as argument
1250 "<input type=\"radio\" name=\""
1251 (or v1 (setq v1 (skeleton-read "Name: ")))
1252 "\" value=\"" str ?\"
1253 (if v2 "" " checked") ?> str
1254 (or v2 (setq v2 (if (y-or-n-p "Newline? ") "<br>" ""))) \n))
1257 (defun html-autoview-mode (&optional arg)
1258 "Toggle automatic viewing via `html-viewer' upon saving buffer.
1259 With positive prefix ARG always turns viewing on, with negative ARG always off.
1260 Can be used as a value for `html-mode-hook'."
1262 (if (setq arg (if arg
1263 (< (prefix-numeric-value arg) 0)
1264 (and (boundp 'after-save-hook)
1265 (memq 'browse-url-of-buffer after-save-hook))))
1266 (setq after-save-hook (delq 'browse-url-of-buffer after-save-hook))
1267 (make-local-hook 'after-save-hook)
1268 (add-hook 'after-save-hook 'browse-url-of-buffer nil t))
1269 (message "Autoviewing turned %s."
1270 (if arg "off" "on")))
1272 ;;; sgml-mode.el ends here