1 ;;; sgml-mode.el --- SGML- and HTML-editing modes
3 ;; Copyright (C) 1992,95,96,98,2001,2002 Free Software Foundation, Inc.
5 ;; Author: James Clark <jjc@jclark.com>
7 ;; Adapted-By: ESR, Daniel Pfeiffer <occitan@esperanto.org>,
8 ;; F.Potorti@cnuce.cnr.it
9 ;; Keywords: wp, hypermedia, comm, languages
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
30 ;; Configurable major mode for editing document in the SGML standard general
31 ;; markup language. As an example contains a mode for editing the derived
32 ;; HTML hypertext markup language.
45 (defcustom sgml-basic-offset 2
46 "*Specifies the basic indentation level for `sgml-indent-line'."
50 (defcustom sgml-transformation 'identity
51 "*Default value for `skeleton-transformation' (which see) in SGML mode."
55 (put 'sgml-transformation 'variable-interactive
56 "aTransformation function: ")
58 (defcustom sgml-mode-hook nil
59 "Hook run by command `sgml-mode'.
60 `text-mode-hook' is run first."
64 ;; As long as Emacs' syntax can't be complemented with predicates to context
65 ;; sensitively confirm the syntax of characters, we have to live with this
66 ;; kludgy kind of tradeoff.
67 (defvar sgml-specials '(?\")
68 "List of characters that have a special meaning for SGML mode.
69 This list is used when first loading the `sgml-mode' library.
70 The supported characters and potential disadvantages are:
72 ?\\\" Makes \" in text start a string.
73 ?' Makes ' in text start a string.
74 ?- Makes -- in text start a comment.
76 When only one of ?\\\" or ?' are included, \"'\" or '\"', as can be found in
77 DTDs, start a string. To partially avoid this problem this also makes these
78 self insert as named entities depending on `sgml-quick-keys'.
80 Including ?- has the problem of affecting dashes that have nothing to do
81 with comments, so we normally turn it off.")
83 (defvar sgml-quick-keys nil
84 "Use <, >, &, /, SPC and `sgml-specials' keys \"electrically\" when non-nil.
85 This takes effect when first loading the `sgml-mode' library.")
89 (let ((map (make-keymap)) ;`sparse' doesn't allow binding to charsets.
90 (menu-map (make-sparse-keymap "SGML")))
91 (define-key map "\C-c\C-i" 'sgml-tags-invisible)
92 (define-key map "/" 'sgml-slash)
93 (define-key map "\C-c\C-n" 'sgml-name-char)
94 (define-key map "\C-c\C-t" 'sgml-tag)
95 (define-key map "\C-c\C-a" 'sgml-attributes)
96 (define-key map "\C-c\C-b" 'sgml-skip-tag-backward)
97 (define-key map [?\C-c left] 'sgml-skip-tag-backward)
98 (define-key map "\C-c\C-f" 'sgml-skip-tag-forward)
99 (define-key map [?\C-c right] 'sgml-skip-tag-forward)
100 (define-key map "\C-c\C-d" 'sgml-delete-tag)
101 (define-key map "\C-c\^?" 'sgml-delete-tag)
102 (define-key map "\C-c?" 'sgml-tag-help)
103 (define-key map "\C-c/" 'sgml-close-tag)
104 (define-key map "\C-c8" 'sgml-name-8bit-mode)
105 (define-key map "\C-c\C-v" 'sgml-validate)
106 (when sgml-quick-keys
107 (define-key map "&" 'sgml-name-char)
108 (define-key map "<" 'sgml-tag)
109 (define-key map " " 'sgml-auto-attributes)
110 (define-key map ">" 'sgml-maybe-end-tag)
111 (when (memq ?\" sgml-specials)
112 (define-key map "\"" 'sgml-name-self))
113 (when (memq ?' sgml-specials)
114 (define-key map "'" 'sgml-name-self)))
115 (define-key map (vector (make-char 'latin-iso8859-1))
116 'sgml-maybe-name-self)
119 (while (< (setq c (1+ c)) 256)
120 (aset map c 'sgml-maybe-name-self)))
121 (define-key map [menu-bar sgml] (cons "SGML" menu-map))
122 (define-key menu-map [sgml-validate] '("Validate" . sgml-validate))
123 (define-key menu-map [sgml-name-8bit-mode]
124 '("Toggle 8 Bit Insertion" . sgml-name-8bit-mode))
125 (define-key menu-map [sgml-tags-invisible]
126 '("Toggle Tag Visibility" . sgml-tags-invisible))
127 (define-key menu-map [sgml-tag-help]
128 '("Describe Tag" . sgml-tag-help))
129 (define-key menu-map [sgml-delete-tag]
130 '("Delete Tag" . sgml-delete-tag))
131 (define-key menu-map [sgml-skip-tag-forward]
132 '("Forward Tag" . sgml-skip-tag-forward))
133 (define-key menu-map [sgml-skip-tag-backward]
134 '("Backward Tag" . sgml-skip-tag-backward))
135 (define-key menu-map [sgml-attributes]
136 '("Insert Attributes" . sgml-attributes))
137 (define-key menu-map [sgml-tag] '("Insert Tag" . sgml-tag))
139 "Keymap for SGML mode. See also `sgml-specials'.")
142 (defun sgml-make-syntax-table (specials)
143 (let ((table (make-syntax-table text-mode-syntax-table)))
144 (modify-syntax-entry ?< "(>" table)
145 (modify-syntax-entry ?> ")<" table)
146 (modify-syntax-entry ?: "_" table)
147 (modify-syntax-entry ?_ "_" table)
148 (modify-syntax-entry ?. "_" table)
149 (if (memq ?- specials)
150 (modify-syntax-entry ?- "_ 1234" table))
151 (if (memq ?\" specials)
152 (modify-syntax-entry ?\" "\"\"" table))
153 (if (memq ?' specials)
154 (modify-syntax-entry ?\' "\"'" table))
157 (defvar sgml-mode-syntax-table (sgml-make-syntax-table sgml-specials)
158 "Syntax table used in SGML mode. See also `sgml-specials'.")
160 (defconst sgml-tag-syntax-table
161 (let ((table (sgml-make-syntax-table '(?- ?\" ?\'))))
162 (dolist (char '(?\( ?\) ?\{ ?\} ?\[ ?\] ?$ ?% ?& ?* ?+ ?/))
163 (modify-syntax-entry char "." table))
165 "Syntax table used to parse SGML tags.")
168 (defcustom sgml-name-8bit-mode nil
169 "*When non-nil, insert non-ASCII characters as named entities."
173 (defvar sgml-char-names
174 [nil nil nil nil nil nil nil nil
175 nil nil nil nil nil nil nil nil
176 nil nil nil nil nil nil nil nil
177 nil nil nil nil nil nil nil nil
178 "nbsp" "excl" "quot" "num" "dollar" "percnt" "amp" "apos"
179 "lpar" "rpar" "ast" "plus" "comma" "hyphen" "period" "sol"
180 nil nil nil nil nil nil nil nil
181 nil nil "colon" "semi" "lt" "eq" "gt" "quest"
182 "commat" nil nil nil nil nil nil nil
183 nil nil nil nil nil nil nil nil
184 nil nil nil nil nil nil nil nil
185 nil nil nil "lsqb" nil "rsqb" "uarr" "lowbar"
186 "lsquo" nil nil nil nil nil nil nil
187 nil nil nil nil nil nil nil nil
188 nil nil nil nil nil nil nil nil
189 nil nil nil "lcub" "verbar" "rcub" "tilde" nil
190 nil nil nil nil nil nil nil nil
191 nil nil nil nil nil nil nil nil
192 nil nil nil nil nil nil nil nil
193 nil nil nil nil nil nil nil nil
194 "nbsp" "iexcl" "cent" "pound" "curren" "yen" "brvbar" "sect"
195 "uml" "copy" "ordf" "laquo" "not" "shy" "reg" "macr"
196 "ring" "plusmn" "sup2" "sup3" "acute" "micro" "para" "middot"
197 "cedil" "sup1" "ordm" "raquo" "frac14" "frac12" "frac34" "iquest"
198 "Agrave" "Aacute" "Acirc" "Atilde" "Auml" "Aring" "AElig" "Ccedil"
199 "Egrave" "Eacute" "Ecirc" "Euml" "Igrave" "Iacute" "Icirc" "Iuml"
200 "ETH" "Ntilde" "Ograve" "Oacute" "Ocirc" "Otilde" "Ouml" nil
201 "Oslash" "Ugrave" "Uacute" "Ucirc" "Uuml" "Yacute" "THORN" "szlig"
202 "agrave" "aacute" "acirc" "atilde" "auml" "aring" "aelig" "ccedil"
203 "egrave" "eacute" "ecirc" "euml" "igrave" "iacute" "icirc" "iuml"
204 "eth" "ntilde" "ograve" "oacute" "ocirc" "otilde" "ouml" "divide"
205 "oslash" "ugrave" "uacute" "ucirc" "uuml" "yacute" "thorn" "yuml"]
206 "Vector of symbolic character names without `&' and `;'.")
208 (put 'sgml-table 'char-table-extra-slots 0)
210 (defvar sgml-char-names-table
211 (let ((table (make-char-table 'sgml-table))
215 (setq elt (aref sgml-char-names i))
216 (if elt (aset table (make-char 'latin-iso8859-1 i) elt))
219 "A table for mapping non-ASCII characters into SGML entity names.
220 Currently, only Latin-1 characters are supported.")
223 ;; nsgmls is a free SGML parser in the SP suite available from
224 ;; ftp.jclark.com and otherwise packaged for GNU systems.
225 ;; Its error messages can be parsed by next-error.
226 ;; The -s option suppresses output.
228 (defcustom sgml-validate-command "nsgmls -s" ; replaced old `sgmls'
229 "*The command to validate an SGML document.
230 The file name of current buffer file name will be appended to this,
231 separated by a space."
236 (defvar sgml-saved-validate-command nil
237 "The command last used to validate in this buffer.")
240 ;; I doubt that null end tags are used much for large elements,
241 ;; so use a small distance here.
242 (defcustom sgml-slash-distance 1000
243 "*If non-nil, is the maximum distance to search for matching `/'."
244 :type '(choice (const nil) integer)
247 (defconst sgml-name-re "[_:[:alpha:]][-_.:[:alnum:]]*")
248 (defconst sgml-tag-name-re (concat "<\\([!/?]?" sgml-name-re "\\)"))
249 (defconst sgml-attrs-re "\\(?:[^\"'/><]\\|\"[^\"]*\"\\|'[^']*'\\)*")
250 (defconst sgml-start-tag-regex (concat "<" sgml-name-re sgml-attrs-re)
251 "Regular expression that matches a non-empty start tag.
252 Any terminating `>' or `/' is not matched.")
256 (defconst sgml-font-lock-keywords-1
257 `((,(concat "<\\([!?]" sgml-name-re "\\)") 1 font-lock-keyword-face)
258 (,(concat "<\\(/?" sgml-name-re"\\)") 1 font-lock-function-name-face)
259 ;; FIXME: this doesn't cover the variables using a default value.
260 (,(concat "\\(" sgml-name-re "\\)=[\"']") 1 font-lock-variable-name-face)
261 (,(concat "[&%]" sgml-name-re ";?") . font-lock-variable-name-face)))
263 (defconst sgml-font-lock-keywords-2
265 sgml-font-lock-keywords-1
268 (regexp-opt (mapcar 'car sgml-tag-face-alist) t)
269 "\\([ \t][^>]*\\)?>\\([^<]+\\)</\\1>")
270 '(3 (cdr (assoc (downcase (match-string 1))
271 sgml-tag-face-alist))))))))
273 ;; for font-lock, but must be defvar'ed after
274 ;; sgml-font-lock-keywords-1 and sgml-font-lock-keywords-2 above
275 (defvar sgml-font-lock-keywords sgml-font-lock-keywords-1
276 "*Rules for highlighting SGML code. See also `sgml-tag-face-alist'.")
278 (defvar sgml-font-lock-syntactic-keywords
279 ;; Use the `b' style of comments to avoid interference with the -- ... --
280 ;; comments recognized when `sgml-specials' includes ?-.
281 ;; FIXME: beware of <!--> blabla <!--> !!
282 '(("\\(<\\)!--" (1 "< b"))
283 ("--[ \t\n]*\\(>\\)" (1 "> b")))
284 "Syntactic keywords for `sgml-mode'.")
287 (defvar sgml-face-tag-alist ()
288 "Alist of face and tag name for facemenu.")
290 (defvar sgml-tag-face-alist ()
291 "Tag names and face or list of faces to fontify with when invisible.
292 When `font-lock-maximum-decoration' is 1 this is always used for fontifying.
293 When more these are fontified together with `sgml-font-lock-keywords'.")
296 (defvar sgml-display-text ()
297 "Tag names as lowercase symbols, and display string when invisible.")
300 (defvar sgml-tags-invisible nil)
303 (defcustom sgml-tag-alist
304 '(("![" ("ignore" t) ("include" t))
309 "*Alist of tag names for completing read and insertion rules.
310 This alist is made up as
315 TAGRULE is a list of optionally `t' (no endtag) or `\\n' (separate endtag by
316 newlines) or a skeleton with `nil', `t' or `\\n' in place of the interactor
317 followed by an ATTRIBUTERULE (for an always present attribute) or an
320 The attribute alist is made up as
322 ((\"attribute\" . ATTRIBUTERULE)
325 ATTRIBUTERULE is a list of optionally `t' (no value when no input) followed by
326 an optional alist of possible values."
327 :type '(repeat (cons (string :tag "Tag Name")
328 (repeat :tag "Tag Rule" sexp)))
331 (defcustom sgml-tag-help
332 '(("!" . "Empty declaration for comment")
333 ("![" . "Embed declarations with parser directive")
334 ("!attlist" . "Tag attributes declaration")
335 ("!doctype" . "Document type (DTD) declaration")
336 ("!element" . "Tag declaration")
337 ("!entity" . "Entity (macro) declaration"))
338 "*Alist of tag name and short description."
339 :type '(repeat (cons (string :tag "Tag Name")
340 (string :tag "Description")))
343 (defcustom sgml-xml-mode nil
344 "*When non-nil, tag insertion functions will be XML-compliant.
345 If this variable is customized, the custom value is used always.
346 Otherwise, it is set to be buffer-local when the file has
347 a DOCTYPE or an XML declaration."
352 (defvar sgml-empty-tags nil
353 "List of tags whose !ELEMENT definition says EMPTY.")
355 (defvar sgml-unclosed-tags nil
356 "List of tags whose !ELEMENT definition says the end-tag is optional.")
358 (defun sgml-xml-guess ()
359 "Guess whether the current buffer is XML."
361 (goto-char (point-min))
362 (when (or (string= "xml" (file-name-extension (or buffer-file-name "")))
363 (looking-at "\\s-*<\\?xml")
364 (when (re-search-forward
367 '("<!DOCTYPE" "\\(\\w+\\)" "\\(\\w+\\)"
368 "\"\\([^\"]+\\)\"" "\"\\([^\"]+\\)\"")
371 (string-match "X\\(HT\\)?ML" (match-string 3))))
372 (set (make-local-variable 'sgml-xml-mode) t))))
374 (defvar v2) ; free for skeleton
376 (defun sgml-mode-facemenu-add-face-function (face end)
377 (if (setq face (cdr (assq face sgml-face-tag-alist)))
379 (setq face (funcall skeleton-transformation face))
380 (setq facemenu-end-add-face (concat "</" face ">"))
381 (concat "<" face ">"))
382 (error "Face not configured for %s mode" mode-name)))
386 (define-derived-mode sgml-mode text-mode "SGML"
387 "Major mode for editing SGML documents.
389 Keys <, &, SPC within <>, \", / and ' can be electric depending on
392 An argument of N to a tag-inserting command means to wrap it around
393 the next N words. In Transient Mark mode, when the mark is active,
394 N defaults to -1, which means to wrap it around the current region.
396 If you like upcased tags, put (setq sgml-transformation 'upcase) in
399 Use \\[sgml-validate] to validate your document with an SGML parser.
401 Do \\[describe-variable] sgml- SPC to see available variables.
402 Do \\[describe-key] on the following bindings to discover what they do.
404 (make-local-variable 'sgml-saved-validate-command)
405 (make-local-variable 'facemenu-end-add-face)
406 ;;(make-local-variable 'facemenu-remove-face-function)
407 ;; A start or end tag by itself on a line separates a paragraph.
408 ;; This is desirable because SGML discards a newline that appears
409 ;; immediately after a start tag or immediately before an end tag.
410 (set (make-local-variable 'paragraph-start) (concat "[ \t]*$\\|\
411 \[ \t]*</?\\(" sgml-name-re sgml-attrs-re "\\)?>"))
412 (set (make-local-variable 'paragraph-separate)
413 (concat paragraph-start "$"))
414 (set (make-local-variable 'adaptive-fill-regexp) "[ \t]*")
415 (set (make-local-variable 'indent-line-function) 'sgml-indent-line)
416 (set (make-local-variable 'comment-start) "<!-- ")
417 (set (make-local-variable 'comment-end) " -->")
418 (set (make-local-variable 'comment-indent-function) 'sgml-comment-indent)
419 (set (make-local-variable 'skeleton-further-elements)
420 '((completion-ignore-case t)))
421 (set (make-local-variable 'skeleton-end-hook)
424 (not (or (eq v2 '\n) (eq (car-safe v2) '\n)))
425 (newline-and-indent))))
426 (set (make-local-variable 'font-lock-defaults)
427 '((sgml-font-lock-keywords
428 sgml-font-lock-keywords-1
429 sgml-font-lock-keywords-2)
431 (font-lock-syntactic-keywords
432 . sgml-font-lock-syntactic-keywords)))
433 (set (make-local-variable 'facemenu-add-face-function)
434 'sgml-mode-facemenu-add-face-function)
437 (setq mode-name "XML")
438 (set (make-local-variable 'skeleton-transformation) sgml-transformation))
439 ;; This will allow existing comments within declarations to be
441 (set (make-local-variable 'comment-start-skip) "\\(?:<!\\)?--[ \t]*")
442 (set (make-local-variable 'comment-end-skip) "[ \t]*--\\([ \t\n]*>\\)?")
443 ;; This definition probably is not useful in derived modes.
444 (set (make-local-variable 'imenu-generic-expression)
445 (concat "<!\\(element\\|entity\\)[ \t\n]+%?[ \t\n]*\\("
446 sgml-name-re "\\)")))
449 (defun sgml-comment-indent ()
450 (if (looking-at "--") comment-column 0))
454 (defun sgml-slash (arg)
455 "Insert ARG slash characters.
456 Behaves electrically if `sgml-quick-keys' is non-nil."
459 ((not (and (eq (char-before) ?<) (= arg 1)))
460 (sgml-slash-matching arg))
461 ((eq sgml-quick-keys 'indent)
463 (indent-according-to-mode))
464 ((eq sgml-quick-keys 'close)
465 (delete-backward-char 1)
468 (sgml-slash-matching arg))))
470 (defun sgml-slash-matching (arg)
471 "Insert `/' and display any previous matching `/'.
472 Two `/'s are treated as matching if the first `/' ends a net-enabling
473 start tag, and the second `/' is the corresponding null end tag."
477 (let ((oldpos (point))
482 (if sgml-slash-distance
483 (narrow-to-region (max (point-min)
484 (- (point) sgml-slash-distance))
486 (if (and (re-search-backward sgml-start-tag-regex (point-min) t)
487 (eq (match-end 0) (1- oldpos)))
489 (goto-char (1- oldpos))
490 (while (and (not blinkpos)
491 (search-backward "/" (point-min) t))
492 (let ((tagend (save-excursion
493 (if (re-search-backward sgml-start-tag-regex
497 (if (eq tagend (point))
499 (setq blinkpos (point))
500 (setq level (1- level)))
501 (setq level (1+ level)))))))
504 (if (pos-visible-in-window-p)
506 (message "Matches %s"
507 (buffer-substring (line-beginning-position)
508 (1+ blinkpos)))))))))
511 ;; Why doesn't this use the iso-cvt table or, preferably, generate the
512 ;; inverse of the extensive table in the SGML Quail input method? -- fx
513 ;; I guess that's moot since it only works with Latin-1 anyhow.
514 (defun sgml-name-char (&optional char)
515 "Insert a symbolic character name according to `sgml-char-names'.
516 Non-ASCII chars may be inserted either with the meta key, as in M-SPC for
517 no-break space or M-- for a soft hyphen; or via an input method or
518 encoded keyboard operation."
522 (setq char (read-quoted-char "Enter char or octal number")))
523 (delete-backward-char 1)
526 (delete-backward-char 1)
530 (or (aref sgml-char-names char)
533 ((aref sgml-char-names-table char)
534 (insert ?& (aref sgml-char-names-table char) ?\;))
535 ((let ((c (encode-char char 'ucs)))
537 (insert (format "&#%d;" c))
539 (t ; should be an error? -- fx
542 (defun sgml-name-self ()
543 "Insert a symbolic character name according to `sgml-char-names'."
545 (sgml-name-char last-command-char))
547 (defun sgml-maybe-name-self ()
548 "Insert a symbolic character name according to `sgml-char-names'."
550 (if sgml-name-8bit-mode
551 (let ((mc last-command-char))
553 (setq mc (unibyte-char-to-multibyte mc)))
554 (or mc (setq mc last-command-char))
556 (self-insert-command 1)))
558 (defun sgml-name-8bit-mode ()
559 "Toggle whether to insert named entities instead of non-ASCII characters.
560 This only works for Latin-1 input."
562 (setq sgml-name-8bit-mode (not sgml-name-8bit-mode))
563 (message "sgml name entity mode is now %s"
564 (if sgml-name-8bit-mode "ON" "OFF")))
566 ;; When an element of a skeleton is a string "str", it is passed
567 ;; through skeleton-transformation and inserted. If "str" is to be
568 ;; inserted literally, one should obtain it as the return value of a
569 ;; function, e.g. (identity "str").
571 (define-skeleton sgml-tag
572 "Prompt for a tag and insert it, optionally with attributes.
573 Completion and configuration are done according to `sgml-tag-alist'.
574 If you like tags and attributes in uppercase do \\[set-variable]
575 skeleton-transformation RET upcase RET, or put this in your `.emacs':
576 (setq sgml-transformation 'upcase)"
577 (funcall skeleton-transformation
578 (completing-read "Tag: " sgml-tag-alist))
580 (("") -1 '(undo-boundary) (identity "<")) | ; see comment above
581 `(("") '(setq v2 (sgml-attributes ,str t)) ?>
585 '(("") " [ " _ " ]]"))
586 ((and (eq v2 t) sgml-xml-mode (member ,str sgml-empty-tags))
588 ((or (and (eq v2 t) (not sgml-xml-mode)) (string-match "^[/!?]" ,str))
591 ;; Make sure we don't fall into an infinite loop.
592 ;; For xhtml's `tr' tag, we should maybe use \n instead.
593 (if (eq v2 t) (setq v2 nil))
594 ;; We use `identity' to prevent skeleton from passing
595 ;; `str' through skeleton-transformation a second time.
596 '(("") v2 _ v2 "</" (identity ',str) ?>))
598 (cons '("") (cdr v2)))
600 (append '(("") (car v2))
602 '(resume: (car v2) _ "</" (identity ',str) ?>))))))
604 (autoload 'skeleton-read "skeleton")
606 (defun sgml-attributes (tag &optional quiet)
607 "When at top level of a tag, interactively insert attributes.
609 Completion and configuration of TAG are done according to `sgml-tag-alist'.
610 If QUIET, do not print a message when there are no attributes for TAG."
611 (interactive (list (save-excursion (sgml-beginning-of-tag t))))
612 (or (stringp tag) (error "Wrong context for adding attribute"))
614 (let ((completion-ignore-case t)
615 (alist (cdr (assoc (downcase tag) sgml-tag-alist)))
617 (if (or (symbolp (car alist))
618 (symbolp (car (car alist))))
619 (setq car (car alist)
622 (message "No attributes configured."))
623 (if (stringp (car alist))
625 (insert (if (eq (preceding-char) ? ) "" ? )
626 (funcall skeleton-transformation (car alist)))
628 (setq i (length alist))
631 (insert (funcall skeleton-transformation
633 (skeleton-read '(completing-read
636 (if (string= "" attribute)
638 (sgml-value (assoc (downcase attribute) alist))
640 (if (eq (preceding-char) ? )
641 (delete-backward-char 1)))
644 (defun sgml-auto-attributes (arg)
645 "Self insert the character typed; at top level of tag, prompt for attributes.
646 With prefix argument, only self insert."
648 (let ((point (point))
651 (not sgml-tag-alist) ; no message when nothing configured
652 (symbolp (setq tag (save-excursion (sgml-beginning-of-tag t))))
653 (eq (aref tag 0) ?/))
654 (self-insert-command (prefix-numeric-value arg))
655 (sgml-attributes tag)
656 (setq last-command-char ? )
657 (or (> (point) point)
658 (self-insert-command 1)))))
661 (defun sgml-tag-help (&optional tag)
662 "Display description of tag TAG. If TAG is omitted, use the tag at point."
666 (if (eq (following-char) ?<)
668 (setq tag (sgml-beginning-of-tag))))
670 (error "No tag selected"))
671 (setq tag (downcase tag))
673 (or (cdr (assoc (downcase tag) sgml-tag-help))
674 (and (eq (aref tag 0) ?/)
675 (cdr (assoc (downcase (substring tag 1)) sgml-tag-help)))
676 "No description available")))
679 (defun sgml-maybe-end-tag (&optional arg)
680 "Name self unless in position to end a tag or a prefix ARG is given."
682 (if (or arg (eq (car (sgml-lexical-context)) 'tag))
683 (self-insert-command (prefix-numeric-value arg))
686 (defun sgml-skip-tag-backward (arg)
687 "Skip to beginning of tag or matching opening tag if present.
688 With prefix argument ARG, repeat this ARG times."
691 (search-backward "<" nil t)
692 (if (looking-at "</\\([^ \n\t>]+\\)")
693 ;; end tag, skip any nested pairs
694 (let ((case-fold-search t)
695 (re (concat "</?" (regexp-quote (match-string 1)))))
696 (while (and (re-search-backward re nil t)
697 (eq (char-after (1+ (point))) ?/))
699 (sgml-skip-tag-backward 1))))
700 (setq arg (1- arg))))
702 (defun sgml-skip-tag-forward (arg &optional return)
703 "Skip to end of tag or matching closing tag if present.
704 With prefix argument ARG, repeat this ARG times.
705 Return t iff after a closing tag."
709 (skip-chars-forward "^<>")
710 (if (eq (following-char) ?>)
712 (if (looking-at "<\\([^/ \n\t>]+\\)")
713 ;; start tag, skip any nested same pairs _and_ closing tag
714 (let ((case-fold-search t)
715 (re (concat "</?" (regexp-quote (match-string 1))))
719 (while (and (re-search-forward re nil t)
721 (eq (char-after (1+ (match-beginning 0))) ?/)))
723 (sgml-skip-tag-forward 1))
733 (defun sgml-delete-tag (arg)
734 "Delete tag on or after cursor, and matching closing or opening tag.
735 With prefix argument ARG, repeat this ARG times."
740 (if (looking-at "[ \t\n]*<")
742 (if (eq (char-after (match-end 0)) ?/)
746 (goto-char (match-end 0))))
748 (or (save-excursion (setq close (sgml-beginning-of-tag)
749 close (and (stringp close)
750 (eq (aref close 0) ?/)
752 ;; not on closing tag
753 (let ((point (point)))
754 (sgml-skip-tag-backward 1)
755 (if (or (not (eq (following-char) ?<))
759 (error "Not on or before tag")))))
762 (sgml-skip-tag-backward 1)
767 (sgml-skip-tag-forward 1)
770 (if (eq (aref (sgml-beginning-of-tag) 0) ?/)
774 (setq arg (1- arg))))
776 ;; Put read-only last to enable setting this even when read-only enabled.
777 (or (get 'sgml-tag 'invisible)
779 (append '(invisible t
781 point-entered sgml-point-entered
784 (symbol-plist 'sgml-tag))))
786 (defun sgml-tags-invisible (arg)
787 "Toggle visibility of existing tags."
789 (let ((modified (buffer-modified-p))
790 (inhibit-read-only t)
791 (inhibit-modification-hooks t)
792 ;; Avoid spurious the `file-locked' checks.
793 (buffer-file-name nil)
794 ;; This is needed in case font lock gets called,
795 ;; since it moves point and might call sgml-point-entered.
796 ;; How could it get called? -stef
797 (inhibit-point-motion-hooks t)
801 (goto-char (point-min))
802 (if (set (make-local-variable 'sgml-tags-invisible)
804 (>= (prefix-numeric-value arg) 0)
805 (not sgml-tags-invisible)))
806 (while (re-search-forward sgml-tag-name-re nil t)
808 (cdr (assq (intern-soft (downcase (match-string 1)))
810 (goto-char (match-beginning 0))
811 (and (stringp string)
812 (not (overlays-at (point)))
813 (let ((ol (make-overlay (point) (match-beginning 1))))
814 (overlay-put ol 'before-string string)
815 (overlay-put ol 'sgml-tag t)))
816 (put-text-property (point)
817 (progn (forward-list) (point))
818 'category 'sgml-tag))
819 (let ((pos (point-min)))
820 (while (< (setq pos (next-overlay-change pos)) (point-max))
821 (dolist (ol (overlays-at pos))
822 (if (overlay-get ol 'sgml-tag)
823 (delete-overlay ol)))))
824 (remove-text-properties (point-min) (point-max) '(category nil))))
825 (restore-buffer-modified-p modified))
826 (run-hooks 'sgml-tags-invisible-hook)
829 (defun sgml-point-entered (x y)
830 ;; Show preceding or following hidden tag, depending of cursor direction.
831 (let ((inhibit-point-motion-hooks t))
833 (message "Invisible tag: %s"
834 ;; Strip properties, otherwise, the text is invisible.
835 (buffer-substring-no-properties
838 (not (eq (following-char) ?<)))
840 (eq (preceding-char) ?>)))
844 (autoload 'compile-internal "compile")
846 (defun sgml-validate (command)
847 "Validate an SGML document.
848 Runs COMMAND, a shell command, in a separate process asynchronously
849 with output going to the buffer `*compilation*'.
850 You can then use the command \\[next-error] to find the next error message
851 and move to the line in the SGML document that caused it."
853 (list (read-string "Validate command: "
854 (or sgml-saved-validate-command
855 (concat sgml-validate-command
857 (let ((name (buffer-file-name)))
859 (file-name-nondirectory name))))))))
860 (setq sgml-saved-validate-command command)
861 (save-some-buffers (not compilation-ask-about-save) nil)
862 (compile-internal command "No more errors"))
865 (defun sgml-lexical-context (&optional limit)
866 "Return the lexical context at point as (TYPE . START).
867 START is the location of the start of the lexical element.
868 TYPE is one of `string', `comment', `tag', `cdata', or `text'.
870 If non-nil LIMIT is a nearby position before point outside of any tag."
871 ;; As usual, it's difficult to get a reliable answer without parsing the
872 ;; whole buffer. We'll assume that a tag at indentation is outside of
873 ;; any string or tag or comment or ...
877 (if limit (goto-char limit)
878 ;; Hopefully this regexp will match something that's not inside
879 ;; a tag and also hopefully the match is nearby.
880 (re-search-backward "^[ \t]*<[_:[:alpha:]/%!?#]" nil 'move))
881 (with-syntax-table sgml-tag-syntax-table
882 (while (< (point) pos)
883 ;; When entering this loop we're inside text.
884 (setq text-start (point))
885 (skip-chars-forward "^<" pos)
889 ;; We got to the end without seeing a tag.
891 ((looking-at "<!\\[[A-Z]+\\[")
892 ;; We've found a CDATA section or similar.
893 (let ((cdata-start (point)))
894 (unless (search-forward "]]>" pos 'move)
895 (list 0 nil nil 'cdata nil nil nil nil cdata-start))))
897 ;; We've reached a tag. Parse it.
898 ;; FIXME: Handle net-enabling start-tags
899 (parse-partial-sexp (point) pos 0))))))
901 ((eq (nth 3 state) 'cdata) (cons 'cdata (nth 8 state)))
902 ((nth 3 state) (cons 'string (nth 8 state)))
903 ((nth 4 state) (cons 'comment (nth 8 state)))
904 ((and state (> (nth 0 state) 0)) (cons 'tag (nth 1 state)))
905 (t (cons 'text text-start))))))
907 (defun sgml-beginning-of-tag (&optional top-level)
908 "Skip to beginning of tag and return its name.
909 If this can't be done, return nil."
910 (let ((context (sgml-lexical-context)))
911 (if (eq (car context) 'tag)
913 (goto-char (cdr context))
914 (when (looking-at sgml-tag-name-re)
915 (match-string-no-properties 1)))
917 (when (not (eq (car context) 'text))
918 (goto-char (cdr context))
919 (sgml-beginning-of-tag t))))))
921 (defun sgml-value (alist)
922 "Interactively insert value taken from attribute-rule ALIST.
923 See `sgml-tag-alist' for info about attribute rules."
924 (setq alist (cdr alist))
925 (if (stringp (car alist))
926 (insert "=\"" (car alist) ?\")
927 (if (and (eq (car alist) t) (not sgml-xml-mode))
930 (setq alist (skeleton-read '(completing-read "Value: " (cdr alist))))
931 (if (string< "" alist)
933 (delete-backward-char 2)))
936 (insert (skeleton-read '(completing-read "Value: " alist))))
939 (defun sgml-quote (start end &optional unquotep)
940 "Quote SGML text in region.
941 With prefix argument, unquote the region."
948 (while (re-search-forward "&\\(amp\\|\\(l\\|\\(g\\)\\)t\\)[;\n]" end t)
949 (replace-match (if (match-end 3) ">" (if (match-end 2) "<" "&"))))
950 (while (re-search-forward "[&<>]" end t)
951 (replace-match (cdr (assq (char-before) '((?& . "&")
956 (defsubst sgml-at-indentation-p ()
957 "Return true if point is at the first non-whitespace character on the line."
959 (skip-chars-backward " \t")
966 (:constructor sgml-make-tag (type start end name)))
969 (defsubst sgml-parse-tag-name ()
970 "Skip past a tag-name, and return the name."
971 (buffer-substring-no-properties
972 (point) (progn (skip-syntax-forward "w_") (point))))
974 (defsubst sgml-looking-back-at (s)
975 (let ((start (- (point) (length s))))
976 (and (>= start (point-min))
977 (equal s (buffer-substring-no-properties start (point))))))
979 (defun sgml-parse-tag-backward ()
980 "Parse an SGML tag backward, and return information about the tag.
981 Assume that parsing starts from within a textual context.
982 Leave point at the beginning of the tag."
983 (let (tag-type tag-start tag-end name)
984 (search-backward ">")
985 (setq tag-end (1+ (point)))
987 ((sgml-looking-back-at "--") ; comment
988 (setq tag-type 'comment
989 tag-start (search-backward "<!--" nil t)))
990 ((sgml-looking-back-at "]]") ; cdata
991 (setq tag-type 'cdata
992 tag-start (re-search-backward "<!\\[[A-Z]+\\[" nil t)))
995 (with-syntax-table sgml-tag-syntax-table
999 (goto-char (1+ tag-start))
1002 (setq tag-type 'decl))
1003 (?? ; processing-instruction
1004 (setq tag-type 'pi))
1007 (setq tag-type 'close
1008 name (sgml-parse-tag-name)))
1010 (setq tag-type 'jsp))
1011 (t ; open or empty tag
1012 (setq tag-type 'open
1013 name (sgml-parse-tag-name))
1014 (if (or (eq ?/ (char-before (- tag-end 1)))
1015 (sgml-empty-tag-p name))
1016 (setq tag-type 'empty))))))
1017 (goto-char tag-start)
1018 (sgml-make-tag tag-type tag-start tag-end name)))
1020 (defun sgml-get-context (&optional full)
1021 "Determine the context of the current position.
1022 If FULL is `empty', return even if the context is empty (i.e.
1023 we just skipped over some element and got to a beginning of line).
1024 If FULL is non-nil, parse back to the beginning of the buffer, otherwise
1025 parse until we find a start-tag as the first thing on a line.
1027 The context is a list of tag-info structures. The last one is the tag
1028 immediately enclosing the current position."
1029 (let ((here (point))
1033 ;; CONTEXT keeps track of the tag-stack
1034 ;; IGNORE keeps track of the nesting level of point relative to the
1035 ;; first (outermost) tag on the context. This is the list of
1036 ;; enclosing start-tags we'll have to ignore.
1037 (skip-chars-backward " \t\n") ; Make sure we're not at indentation.
1040 (not (if full (eq full 'empty) context))
1041 (not (sgml-at-indentation-p))
1043 (/= (point) (sgml-tag-start (car context)))
1044 (sgml-unclosed-tag-p (sgml-tag-name (car context)))))
1045 (setq tag-info (ignore-errors (sgml-parse-tag-backward))))
1047 ;; This tag may enclose things we thought were tags. If so,
1050 (> (sgml-tag-end tag-info)
1051 (sgml-tag-end (car context))))
1052 (setq context (cdr context)))
1057 ((eq (sgml-tag-type tag-info) 'open)
1061 (sgml-unclosed-tag-p (sgml-tag-name tag-info))
1062 (eq t (compare-strings
1063 (sgml-tag-name tag-info) nil nil
1064 (sgml-tag-name (car context)) nil nil t)))
1065 ;; There was an implicit end-tag.
1067 (push tag-info context)))
1068 ((eq t (compare-strings (sgml-tag-name tag-info) nil nil
1069 (car ignore) nil nil t))
1070 (setq ignore (cdr ignore)))
1072 ;; The open and close tags don't match.
1073 (if (not sgml-xml-mode)
1074 ;; Assume the open tag is simply not closed.
1075 (unless (sgml-unclosed-tag-p (sgml-tag-name tag-info))
1076 (message "Unclosed tag <%s>" (sgml-tag-name tag-info)))
1077 (message "Unmatched tags <%s> and </%s>"
1078 (sgml-tag-name tag-info) (pop ignore))))))
1081 ((eq (sgml-tag-type tag-info) 'close)
1082 (if (sgml-empty-tag-p (sgml-tag-name tag-info))
1083 (message "Spurious </%s>: empty tag" (sgml-tag-name tag-info))
1084 (push (sgml-tag-name tag-info) ignore)))
1090 (defun sgml-show-context (&optional full)
1091 "Display the current context.
1092 If FULL is non-nil, parse back to the beginning of the buffer."
1094 (with-output-to-temp-buffer "*XML Context*"
1095 (pp (save-excursion (sgml-get-context full)))))
1098 ;; Editing shortcuts
1100 (defun sgml-close-tag ()
1101 "Insert an close-tag for the current element."
1103 (case (car (sgml-lexical-context))
1104 (comment (insert " -->"))
1105 (cdata (insert "]]>"))
1107 (jsp (insert " %>"))
1108 (tag (insert " />"))
1110 (let ((context (save-excursion (sgml-get-context))))
1113 (insert "</" (sgml-tag-name (car (last context))) ">")
1114 (indent-according-to-mode)))))
1116 (error "Nothing to close"))))
1118 (defun sgml-empty-tag-p (tag-name)
1119 "Return non-nil if TAG-NAME is an implicitly empty tag."
1120 (and (not sgml-xml-mode)
1121 (member-ignore-case tag-name sgml-empty-tags)))
1123 (defun sgml-unclosed-tag-p (tag-name)
1124 "Return non-nil if TAG-NAME is a tag for which an end-tag is optional."
1125 (and (not sgml-xml-mode)
1126 (member-ignore-case tag-name sgml-unclosed-tags)))
1128 (defun sgml-calculate-indent ()
1129 "Calculate the column to which this line should be indented."
1130 (let ((lcon (sgml-lexical-context)))
1132 ;; Indent comment-start markers inside <!-- just like comment-end markers.
1133 (if (and (eq (car lcon) 'tag)
1135 (save-excursion (goto-char (cdr lcon)) (looking-at "<!--")))
1136 (setq lcon (cons 'comment (+ (cdr lcon) 2))))
1141 ;; Go back to previous non-empty line.
1142 (while (and (> (point) (cdr lcon))
1143 (zerop (forward-line -1))
1144 (looking-at "[ \t]*$")))
1145 (if (> (point) (cdr lcon))
1146 ;; Previous line is inside the string.
1147 (current-indentation)
1148 (goto-char (cdr lcon))
1149 (1+ (current-column))))
1152 (let ((mark (looking-at "--")))
1153 ;; Go back to previous non-empty line.
1154 (while (and (> (point) (cdr lcon))
1155 (zerop (forward-line -1))
1156 (or (looking-at "[ \t]*$")
1157 (if mark (not (looking-at "[ \t]*--"))))))
1158 (if (> (point) (cdr lcon))
1159 ;; Previous line is inside the comment.
1160 (skip-chars-forward " \t")
1161 (goto-char (cdr lcon)))
1162 (when (and (not mark) (looking-at "--"))
1163 (forward-char 2) (skip-chars-forward " \t"))
1170 (goto-char (1+ (cdr lcon)))
1171 (skip-chars-forward "^ \t\n") ;Skip tag name.
1172 (skip-chars-forward " \t")
1175 ;; This is the first attribute: indent.
1176 (goto-char (1+ (cdr lcon)))
1177 (+ (current-column) sgml-basic-offset)))
1180 (while (looking-at "</")
1182 (skip-chars-forward " \t"))
1183 (let* ((here (point))
1184 (unclosed (and ;; (not sgml-xml-mode)
1185 (looking-at sgml-tag-name-re)
1186 (member-ignore-case (match-string 1)
1190 ;; If possible, align on the previous non-empty text line.
1191 ;; Otherwise, do a more serious parsing to find the
1192 ;; tag(s) relative to which we should be indenting.
1193 (if (and (not unclosed) (skip-chars-backward " \t")
1194 (< (skip-chars-backward " \t\n") 0)
1195 (back-to-indentation)
1196 (> (point) (cdr lcon)))
1199 (nreverse (sgml-get-context (if unclosed nil 'empty)))))
1201 ;; Ignore previous unclosed start-tag in context.
1202 (while (and context unclosed
1203 (eq t (compare-strings
1204 (sgml-tag-name (car context)) nil nil
1205 unclosed nil nil t)))
1206 (setq context (cdr context)))
1207 ;; Indent to reflect nesting.
1209 (goto-char (sgml-tag-end (car context)))
1210 (skip-chars-forward " \t\n")
1211 (< (point) here) (sgml-at-indentation-p))
1215 (* sgml-basic-offset (length context))))))
1218 (error "Unrecognised context %s" (car lcon)))
1222 (defun sgml-indent-line ()
1223 "Indent the current line as SGML."
1225 (let* ((savep (point))
1228 (back-to-indentation)
1229 (if (>= (point) savep) (setq savep nil))
1230 (sgml-calculate-indent))))
1232 (save-excursion (indent-line-to indent-col))
1233 (indent-line-to indent-col))))
1235 (defun sgml-parse-dtd ()
1236 "Simplistic parse of the current buffer as a DTD.
1237 Currently just returns (EMPTY-TAGS UNCLOSED-TAGS)."
1238 (goto-char (point-min))
1241 (while (re-search-forward "<!ELEMENT[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+[-O][ \t\n]+\\([-O]\\)[ \t\n]+\\([^ \t\n]+\\)" nil t)
1243 ((string= (match-string 3) "EMPTY")
1244 (push (match-string-no-properties 1) empty))
1245 ((string= (match-string 2) "O")
1246 (push (match-string-no-properties 1) unclosed))))
1247 (setq empty (sort (mapcar 'downcase empty) 'string<))
1248 (setq unclosed (sort (mapcar 'downcase unclosed) 'string<))
1249 (list empty unclosed)))
1253 (defcustom html-mode-hook nil
1254 "Hook run by command `html-mode'.
1255 `text-mode-hook' and `sgml-mode-hook' are run first."
1258 :options '(html-autoview-mode))
1260 (defvar html-quick-keys sgml-quick-keys
1261 "Use C-c X combinations for quick insertion of frequent tags when non-nil.
1262 This defaults to `sgml-quick-keys'.
1263 This takes effect when first loading the library.")
1265 (defvar html-mode-map
1266 (let ((map (make-sparse-keymap))
1267 (menu-map (make-sparse-keymap "HTML")))
1268 (set-keymap-parent map sgml-mode-map)
1269 (define-key map "\C-c6" 'html-headline-6)
1270 (define-key map "\C-c5" 'html-headline-5)
1271 (define-key map "\C-c4" 'html-headline-4)
1272 (define-key map "\C-c3" 'html-headline-3)
1273 (define-key map "\C-c2" 'html-headline-2)
1274 (define-key map "\C-c1" 'html-headline-1)
1275 (define-key map "\C-c\r" 'html-paragraph)
1276 (define-key map "\C-c\n" 'html-line)
1277 (define-key map "\C-c\C-c-" 'html-horizontal-rule)
1278 (define-key map "\C-c\C-co" 'html-ordered-list)
1279 (define-key map "\C-c\C-cu" 'html-unordered-list)
1280 (define-key map "\C-c\C-cr" 'html-radio-buttons)
1281 (define-key map "\C-c\C-cc" 'html-checkboxes)
1282 (define-key map "\C-c\C-cl" 'html-list-item)
1283 (define-key map "\C-c\C-ch" 'html-href-anchor)
1284 (define-key map "\C-c\C-cn" 'html-name-anchor)
1285 (define-key map "\C-c\C-ci" 'html-image)
1286 (when html-quick-keys
1287 (define-key map "\C-c-" 'html-horizontal-rule)
1288 (define-key map "\C-co" 'html-ordered-list)
1289 (define-key map "\C-cu" 'html-unordered-list)
1290 (define-key map "\C-cr" 'html-radio-buttons)
1291 (define-key map "\C-cc" 'html-checkboxes)
1292 (define-key map "\C-cl" 'html-list-item)
1293 (define-key map "\C-ch" 'html-href-anchor)
1294 (define-key map "\C-cn" 'html-name-anchor)
1295 (define-key map "\C-ci" 'html-image))
1296 (define-key map "\C-c\C-s" 'html-autoview-mode)
1297 (define-key map "\C-c\C-v" 'browse-url-of-buffer)
1298 (define-key map [menu-bar html] (cons "HTML" menu-map))
1299 (define-key menu-map [html-autoview-mode]
1300 '("Toggle Autoviewing" . html-autoview-mode))
1301 (define-key menu-map [browse-url-of-buffer]
1302 '("View Buffer Contents" . browse-url-of-buffer))
1303 (define-key menu-map [nil] '("--"))
1304 ;;(define-key menu-map "6" '("Heading 6" . html-headline-6))
1305 ;;(define-key menu-map "5" '("Heading 5" . html-headline-5))
1306 ;;(define-key menu-map "4" '("Heading 4" . html-headline-4))
1307 (define-key menu-map "3" '("Heading 3" . html-headline-3))
1308 (define-key menu-map "2" '("Heading 2" . html-headline-2))
1309 (define-key menu-map "1" '("Heading 1" . html-headline-1))
1310 (define-key menu-map "l" '("Radio Buttons" . html-radio-buttons))
1311 (define-key menu-map "c" '("Checkboxes" . html-checkboxes))
1312 (define-key menu-map "l" '("List Item" . html-list-item))
1313 (define-key menu-map "u" '("Unordered List" . html-unordered-list))
1314 (define-key menu-map "o" '("Ordered List" . html-ordered-list))
1315 (define-key menu-map "-" '("Horizontal Rule" . html-horizontal-rule))
1316 (define-key menu-map "\n" '("Line Break" . html-line))
1317 (define-key menu-map "\r" '("Paragraph" . html-paragraph))
1318 (define-key menu-map "i" '("Image" . html-image))
1319 (define-key menu-map "h" '("Href Anchor" . html-href-anchor))
1320 (define-key menu-map "n" '("Name Anchor" . html-name-anchor))
1322 "Keymap for commands for use in HTML mode.")
1325 (defvar html-face-tag-alist
1330 "Value of `sgml-face-tag-alist' for HTML mode.")
1332 (defvar html-tag-face-alist
1335 ("blink" . highlight)
1338 ("h1" bold underline)
1339 ("h2" bold-italic underline)
1340 ("h3" italic underline)
1349 ("title" bold underline)
1353 "Value of `sgml-tag-face-alist' for HTML mode.")
1356 (defvar html-display-text
1360 "Value of `sgml-display-text' for HTML mode.")
1363 ;; should code exactly HTML 3 here when that is finished
1364 (defvar html-tag-alist
1365 (let* ((1-7 '(("1") ("2") ("3") ("4") ("5") ("6") ("7")))
1366 (1-9 `(,@1-7 ("8") ("9")))
1367 (align '(("align" ("left") ("center") ("right"))))
1368 (valign '(("top") ("middle") ("bottom") ("baseline")))
1369 (rel '(("next") ("previous") ("parent") ("subdocument") ("made")))
1370 (href '("href" ("ftp:") ("file:") ("finger:") ("gopher:") ("http:")
1371 ("mailto:") ("news:") ("rlogin:") ("telnet:") ("tn3270:")
1372 ("wais:") ("/cgi-bin/")))
1378 (list '((nil \n ("List item: " "<li>" str
1379 (if sgml-xml-mode "</li>") \n))))
1386 ;; put ,-expressions first, else byte-compile chokes (as of V19.29)
1387 ;; and like this it's more efficient anyway
1388 `(("a" ,name ,@link)
1391 ("font" nil "size" ("-1") ("+1") ("-2") ("+2") ,@1-7)
1392 ("form" (\n _ \n "<input type=\"submit\" value=\"\""
1393 (if sgml-xml-mode "/>" ">"))
1394 ("action" ,@(cdr href)) ("method" ("get") ("post")))
1401 ("hr" t ("size" ,@1-9) ("width") ("noshade" t) ,@align)
1402 ("img" t ("align" ,@valign ("texttop") ("absmiddle") ("absbottom"))
1403 ("src") ("alt") ("width" "1") ("height" "1")
1404 ("border" "1") ("vspace" "1") ("hspace" "1") ("ismap" t))
1405 ("input" t ("size" ,@1-9) ("maxlength" ,@1-9) ("checked" t) ,name
1406 ("type" ("text") ("password") ("checkbox") ("radio")
1407 ("submit") ("reset"))
1411 ("ol" ,@list ("type" ("A") ("a") ("I") ("i") ("1")))
1415 "<option>" str (if sgml-xml-mode "</option>") \n))
1416 ,name ("size" ,@1-9) ("multiple" t))
1418 ((completing-read "Cell kind: " '(("td") ("th"))
1421 (if sgml-xml-mode (concat "<" str "></tr>")) \n))
1422 ("border" t ,@1-9) ("width" "10") ("cellpadding"))
1424 ("textarea" ,name ("rows" ,@1-9) ("cols" ,@1-9))
1426 ("ul" ,@list ("type" ("disc") ("circle") ("square")))
1434 ("Item: " "<item>" str (if sgml-xml-mode "</item>") \n))
1441 ("body" \n ("background" ".gif") ("bgcolor" "#") ("text" "#")
1442 ("link" "#") ("alink" "#") ("vlink" "#"))
1443 ("box" (nil _ "<over>" _ (if sgml-xml-mode "</over>")))
1444 ("br" t ("clear" ("left") ("right")))
1445 ("caption" ("valign" ("top") ("bottom")))
1449 ("dd" ,(not sgml-xml-mode))
1455 "<dt>" str (if sgml-xml-mode "</dt>")
1456 "<dd>" _ (if sgml-xml-mode "</dd>") \n)))
1457 ("dt" (t _ (if sgml-xml-mode "</dt>")
1458 "<dd>" (if sgml-xml-mode "</dd>") \n))
1460 ;("fn" "id" "fn") ; ???
1464 "<title>" (setq str (read-input "Title: ")) "</title>\n"
1466 "<body>\n<h1>" str "</h1>\n" _
1467 "\n<address>\n<a href=\"mailto:"
1469 "\">" (user-full-name) "</a>\n</address>\n"
1474 ("isindex" t ("action") ("prompt"))
1477 ("li" ,(not sgml-xml-mode))
1480 ("option" t ("value") ("label") ("selected" t))
1509 "*Value of `sgml-tag-alist' for HTML mode.")
1511 (defvar html-tag-help
1513 ("a" . "Anchor of point or link elsewhere")
1516 ("address" . "Formatted mail address")
1517 ("array" . "Math array")
1520 ("base" . "Base address for URLs")
1521 ("big" . "Font size")
1522 ("blink" . "Blinking text")
1523 ("blockquote" . "Indented quotation")
1524 ("body" . "Document body")
1525 ("box" . "Math fraction")
1526 ("br" . "Line break")
1527 ("caption" . "Table caption")
1528 ("center" . "Centered text")
1529 ("changed" . "Change bars")
1530 ("cite" . "Citation of a document")
1531 ("code" . "Formatted source code")
1532 ("dd" . "Definition of term")
1535 ("dir" . "Directory list (obsolete)")
1536 ("dl" . "Definition list")
1537 ("dt" . "Term to be definined")
1538 ("em" . "Emphasised")
1539 ("embed" . "Embedded data in foreign format")
1541 ("figa" . "Figure anchor")
1542 ("figd" . "Figure description")
1543 ("figt" . "Figure text")
1545 ("font" . "Font size")
1546 ("form" . "Form with input fields")
1547 ("group" . "Document grouping")
1548 ("h1" . "Most important section headline")
1549 ("h2" . "Important section headline")
1550 ("h3" . "Section headline")
1551 ("h4" . "Minor section headline")
1552 ("h5" . "Unimportant section headline")
1553 ("h6" . "Least important section headline")
1554 ("head" . "Document header")
1555 ("hr" . "Horizontal rule")
1556 ("html" . "HTML Document")
1557 ("i" . "Italic face")
1558 ("img" . "Graphic image")
1559 ("input" . "Form input field")
1561 ("isindex" . "Input field for index search")
1562 ("kbd" . "Keybard example face")
1563 ("lang" . "Natural language")
1564 ("li" . "List item")
1565 ("link" . "Link relationship")
1566 ("math" . "Math formula")
1567 ("menu" . "Menu list (obsolete)")
1568 ("mh" . "Form mail header")
1569 ("nextid" . "Allocate new id")
1570 ("nobr" . "Text without line break")
1571 ("ol" . "Ordered list")
1572 ("option" . "Selection list item")
1573 ("over" . "Math fraction rule")
1574 ("p" . "Paragraph start")
1575 ("panel" . "Floating panel")
1577 ("pre" . "Preformatted fixed width text")
1579 ("rev" . "Reverse video")
1581 ("samp" . "Sample text")
1582 ("select" . "Selection list")
1583 ("small" . "Font size")
1584 ("sp" . "Nobreak space")
1585 ("strong" . "Standout text")
1586 ("sub" . "Subscript")
1587 ("sup" . "Superscript")
1588 ("table" . "Table with rows and columns")
1589 ("tb" . "Table vertical break")
1590 ("td" . "Table data cell")
1591 ("textarea" . "Form multiline edit area")
1592 ("th" . "Table header cell")
1593 ("title" . "Document title")
1594 ("tr" . "Table row separator")
1595 ("tt" . "Typewriter face")
1596 ("u" . "Underlined text")
1597 ("ul" . "Unordered list")
1598 ("var" . "Math variable face")
1599 ("wbr" . "Enable <br> within <nobr>"))
1600 "*Value of `sgml-tag-help' for HTML mode.")
1603 (define-derived-mode html-mode sgml-mode "HTML"
1604 "Major mode based on SGML mode for editing HTML documents.
1605 This allows inserting skeleton constructs used in hypertext documents with
1606 completion. See below for an introduction to HTML. Use
1607 \\[browse-url-of-buffer] to see how this comes out. See also `sgml-mode' on
1608 which this is based.
1610 Do \\[describe-variable] html- SPC and \\[describe-variable] sgml- SPC to see available variables.
1612 To write fairly well formatted pages you only need to know few things. Most
1613 browsers have a function to read the source code of the page being seen, so
1614 you can imitate various tricks. Here's a very short HTML primer which you
1615 can also view with a browser to see what happens:
1617 <title>A Title Describing Contents</title> should be on every page. Pages can
1618 have <h1>Very Major Headlines</h1> through <h6>Very Minor Headlines</h6>
1619 <hr> Parts can be separated with horizontal rules.
1621 <p>Paragraphs only need an opening tag. Line breaks and multiple spaces are
1622 ignored unless the text is <pre>preformatted.</pre> Text can be marked as
1623 <b>bold</b>, <i>italic</i> or <u>underlined</u> using the normal M-g or
1624 Edit/Text Properties/Face commands.
1626 Pages can have <a name=\"SOMENAME\">named points</a> and can link other points
1627 to them with <a href=\"#SOMENAME\">see also somename</a>. In the same way <a
1628 href=\"URL\">see also URL</a> where URL is a filename relative to current
1629 directory, or absolute as in `http://www.cs.indiana.edu/elisp/w3/docs.html'.
1631 Images in many formats can be inlined with <img src=\"URL\">.
1633 If you mainly create your own documents, `sgml-specials' might be
1634 interesting. But note that some HTML 2 browsers can't handle `''.
1635 To work around that, do:
1636 (eval-after-load \"sgml-mode\" '(aset sgml-char-names ?' nil))
1639 (set (make-local-variable 'sgml-display-text) html-display-text)
1640 (set (make-local-variable 'sgml-tag-face-alist) html-tag-face-alist)
1641 (make-local-variable 'sgml-tag-alist)
1642 (make-local-variable 'sgml-face-tag-alist)
1643 (make-local-variable 'sgml-tag-help)
1644 (make-local-variable 'outline-regexp)
1645 (make-local-variable 'outline-heading-end-regexp)
1646 (make-local-variable 'outline-level)
1647 (make-local-variable 'sentence-end)
1649 (if sentence-end-double-space
1650 "[.?!][]\"')}]*\\(<[^>]*>\\)*\\($\\| $\\|\t\\| \\)[ \t\n]*"
1651 "[.?!][]\"')}]*\\(<[^>]*>\\)*\\($\\|[ \t]\\)[ \t\n]*"))
1652 (setq sgml-tag-alist html-tag-alist
1653 sgml-face-tag-alist html-face-tag-alist
1654 sgml-tag-help html-tag-help
1655 outline-regexp "^.*<[Hh][1-6]\\>"
1656 outline-heading-end-regexp "</[Hh][1-6]>"
1657 outline-level (lambda ()
1658 (char-before (match-end 0))))
1659 (setq imenu-create-index-function 'html-imenu-index)
1660 (when sgml-xml-mode (setq mode-name "XHTML"))
1661 (set (make-local-variable 'sgml-empty-tags)
1662 ;; From HTML-4.01's loose.dtd, parsed with `sgml-parse-dtd',
1663 ;; plus manual addition of "wbr".
1664 '("area" "base" "basefont" "br" "col" "frame" "hr" "img" "input"
1665 "isindex" "link" "meta" "param" "wbr"))
1666 (set (make-local-variable 'sgml-unclosed-tags)
1667 ;; From HTML-4.01's loose.dtd, parsed with `sgml-parse-dtd'.
1668 '("body" "colgroup" "dd" "dt" "head" "html" "li" "option"
1669 "p" "tbody" "td" "tfoot" "th" "thead" "tr"))
1670 ;; It's for the user to decide if it defeats it or not -stef
1671 ;; (make-local-variable 'imenu-sort-function)
1672 ;; (setq imenu-sort-function nil) ; sorting the menu defeats the purpose
1675 (defvar html-imenu-regexp
1676 "\\s-*<h\\([1-9]\\)[^\n<>]*>\\(<[^\n<>]*>\\)*\\s-*\\([^\n<>]*\\)"
1677 "*A regular expression matching a head line to be added to the menu.
1678 The first `match-string' should be a number from 1-9.
1679 The second `match-string' matches extra tags and is ignored.
1680 The third `match-string' will be the used in the menu.")
1682 (defun html-imenu-index ()
1683 "Return an table of contents for an HTML buffer for use with Imenu."
1686 (goto-char (point-min))
1687 (while (re-search-forward html-imenu-regexp nil t)
1689 (cons (cons (concat (make-string
1690 (* 2 (1- (string-to-number (match-string 1))))
1693 (line-beginning-position))
1695 (nreverse toc-index)))
1697 (defun html-autoview-mode (&optional arg)
1698 "Toggle automatic viewing via `browse-url-of-buffer' upon saving buffer.
1699 With positive prefix ARG always turns viewing on, with negative ARG always off.
1700 Can be used as a value for `html-mode-hook'."
1702 (if (setq arg (if arg
1703 (< (prefix-numeric-value arg) 0)
1704 (and (boundp 'after-save-hook)
1705 (memq 'browse-url-of-buffer after-save-hook))))
1706 (setq after-save-hook (delq 'browse-url-of-buffer after-save-hook))
1707 (add-hook 'after-save-hook 'browse-url-of-buffer nil t))
1708 (message "Autoviewing turned %s."
1709 (if arg "off" "on")))
1711 (define-skeleton html-href-anchor
1712 "HTML anchor tag with href attribute."
1714 '(setq input "http:")
1715 "<a href=\"" str "\">" _ "</a>")
1717 (define-skeleton html-name-anchor
1718 "HTML anchor tag with name attribute."
1720 "<a name=\"" str "\">" _ "</a>")
1722 (define-skeleton html-headline-1
1723 "HTML level 1 headline tags."
1727 (define-skeleton html-headline-2
1728 "HTML level 2 headline tags."
1732 (define-skeleton html-headline-3
1733 "HTML level 3 headline tags."
1737 (define-skeleton html-headline-4
1738 "HTML level 4 headline tags."
1742 (define-skeleton html-headline-5
1743 "HTML level 5 headline tags."
1747 (define-skeleton html-headline-6
1748 "HTML level 6 headline tags."
1752 (define-skeleton html-horizontal-rule
1753 "HTML horizontal rule tag."
1755 (if sgml-xml-mode "<hr/>" "<hr>") \n)
1757 (define-skeleton html-image
1760 "<img src=\"" _ "\""
1761 (if sgml-xml-mode "/>" ">"))
1763 (define-skeleton html-line
1764 "HTML line break tag."
1766 (if sgml-xml-mode "<br/>" "<br>") \n)
1768 (define-skeleton html-ordered-list
1769 "HTML ordered list tags."
1772 "<li>" _ (if sgml-xml-mode "</li>") \n
1775 (define-skeleton html-unordered-list
1776 "HTML unordered list tags."
1779 "<li>" _ (if sgml-xml-mode "</li>") \n
1782 (define-skeleton html-list-item
1783 "HTML list item tag."
1786 "<li>" _ (if sgml-xml-mode "</li>"))
1788 (define-skeleton html-paragraph
1789 "HTML paragraph tag."
1792 \n "<p>" _ (if sgml-xml-mode "</p>"))
1794 (define-skeleton html-checkboxes
1795 "Group of connected checkbox inputs."
1800 "<input type=\"" (identity "checkbox") ; see comment above about identity
1801 "\" name=\"" (or v1 (setq v1 (skeleton-read "Name: ")))
1802 "\" value=\"" str ?\"
1803 (when (y-or-n-p "Set \"checked\" attribute? ")
1804 (funcall skeleton-transformation " checked"))
1805 (if sgml-xml-mode "/>" ">")
1806 (skeleton-read "Text: " (capitalize str))
1807 (or v2 (setq v2 (if (y-or-n-p "Newline after text? ")
1808 (funcall skeleton-transformation
1809 (if sgml-xml-mode "<br/>" "<br>"))
1813 (define-skeleton html-radio-buttons
1814 "Group of connected radio button inputs."
1819 "<input type=\"" (identity "radio") ; see comment above about identity
1820 "\" name=\"" (or (car v2) (setcar v2 (skeleton-read "Name: ")))
1821 "\" value=\"" str ?\"
1822 (when (and (not v1) (setq v1 (y-or-n-p "Set \"checked\" attribute? ")))
1823 (funcall skeleton-transformation " checked"))
1824 (if sgml-xml-mode "/>" ">")
1825 (skeleton-read "Text: " (capitalize str))
1826 (or (cdr v2) (setcdr v2 (if (y-or-n-p "Newline after text? ")
1827 (funcall skeleton-transformation
1828 (if sgml-xml-mode "<br/>" "<br>"))
1832 (provide 'sgml-mode)
1834 ;;; sgml-mode.el ends here