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.")
88 (let ((map (make-keymap)) ;`sparse' doesn't allow binding to charsets.
89 (menu-map (make-sparse-keymap "SGML")))
90 (define-key map
"\C-c\C-i" 'sgml-tags-invisible
)
91 (define-key map
"/" 'sgml-slash
)
92 (define-key map
"\C-c\C-n" 'sgml-name-char
)
93 (define-key map
"\C-c\C-t" 'sgml-tag
)
94 (define-key map
"\C-c\C-a" 'sgml-attributes
)
95 (define-key map
"\C-c\C-b" 'sgml-skip-tag-backward
)
96 (define-key map
[?\C-c left
] 'sgml-skip-tag-backward
)
97 (define-key map
"\C-c\C-f" 'sgml-skip-tag-forward
)
98 (define-key map
[?\C-c right
] 'sgml-skip-tag-forward
)
99 (define-key map
"\C-c\C-d" 'sgml-delete-tag
)
100 (define-key map
"\C-c\^?" 'sgml-delete-tag
)
101 (define-key map
"\C-c?" 'sgml-tag-help
)
102 (define-key map
"\C-c/" 'sgml-close-tag
)
103 (define-key map
"\C-c8" 'sgml-name-8bit-mode
)
104 (define-key map
"\C-c\C-v" 'sgml-validate
)
105 (when sgml-quick-keys
106 (define-key map
"&" 'sgml-name-char
)
107 (define-key map
"<" 'sgml-tag
)
108 (define-key map
" " 'sgml-auto-attributes
)
109 (define-key map
">" 'sgml-maybe-end-tag
)
110 (when (memq ?
\" sgml-specials
)
111 (define-key map
"\"" 'sgml-name-self
))
112 (when (memq ?
' sgml-specials
)
113 (define-key map
"'" 'sgml-name-self
)))
114 (define-key map
(vector (make-char 'latin-iso8859-1
))
115 'sgml-maybe-name-self
)
118 (while (< (setq c
(1+ c
)) 256)
119 (aset map c
'sgml-maybe-name-self
)))
120 (define-key map
[menu-bar sgml
] (cons "SGML" menu-map
))
121 (define-key menu-map
[sgml-validate
] '("Validate" . sgml-validate
))
122 (define-key menu-map
[sgml-name-8bit-mode
]
123 '("Toggle 8 Bit Insertion" . sgml-name-8bit-mode
))
124 (define-key menu-map
[sgml-tags-invisible
]
125 '("Toggle Tag Visibility" . sgml-tags-invisible
))
126 (define-key menu-map
[sgml-tag-help
]
127 '("Describe Tag" . sgml-tag-help
))
128 (define-key menu-map
[sgml-delete-tag
]
129 '("Delete Tag" . sgml-delete-tag
))
130 (define-key menu-map
[sgml-skip-tag-forward
]
131 '("Forward Tag" . sgml-skip-tag-forward
))
132 (define-key menu-map
[sgml-skip-tag-backward
]
133 '("Backward Tag" . sgml-skip-tag-backward
))
134 (define-key menu-map
[sgml-attributes
]
135 '("Insert Attributes" . sgml-attributes
))
136 (define-key menu-map
[sgml-tag
] '("Insert Tag" . sgml-tag
))
138 "Keymap for SGML mode. See also `sgml-specials'.")
140 (defun sgml-make-syntax-table (specials)
141 (let ((table (make-syntax-table text-mode-syntax-table
)))
142 (modify-syntax-entry ?
< "(>" table
)
143 (modify-syntax-entry ?
> ")<" table
)
144 (modify-syntax-entry ?
: "_" table
)
145 (modify-syntax-entry ?_
"_" table
)
146 (modify-syntax-entry ?.
"_" table
)
147 (if (memq ?- specials
)
148 (modify-syntax-entry ?-
"_ 1234" table
))
149 (if (memq ?
\" specials
)
150 (modify-syntax-entry ?
\" "\"\"" table
))
151 (if (memq ?
' specials
)
152 (modify-syntax-entry ?
\' "\"'" table
))
155 (defvar sgml-mode-syntax-table
(sgml-make-syntax-table sgml-specials
)
156 "Syntax table used in SGML mode. See also `sgml-specials'.")
158 (defconst sgml-tag-syntax-table
159 (let ((table (sgml-make-syntax-table '(?- ?
\" ?
\'))))
160 (dolist (char '(?\
( ?\
) ?\
{ ?\
} ?\
[ ?\
] ?$ ?% ?
& ?
* ?
+ ?
/))
161 (modify-syntax-entry char
"." table
))
163 "Syntax table used to parse SGML tags.")
165 (defcustom sgml-name-8bit-mode nil
166 "*When non-nil, insert non-ASCII characters as named entities."
170 (defvar sgml-char-names
171 [nil nil nil nil nil nil nil nil
172 nil nil nil nil nil nil nil nil
173 nil nil nil nil nil nil nil nil
174 nil nil nil nil nil nil nil nil
175 "nbsp" "excl" "quot" "num" "dollar" "percnt" "amp" "apos"
176 "lpar" "rpar" "ast" "plus" "comma" "hyphen" "period" "sol"
177 nil nil nil nil nil nil nil nil
178 nil nil
"colon" "semi" "lt" "eq" "gt" "quest"
179 "commat" nil nil nil nil nil nil nil
180 nil nil nil nil nil nil nil nil
181 nil nil nil nil nil nil nil nil
182 nil nil nil
"lsqb" nil
"rsqb" "uarr" "lowbar"
183 "lsquo" nil nil nil nil nil nil nil
184 nil nil nil nil nil nil nil nil
185 nil nil nil nil nil nil nil nil
186 nil nil nil
"lcub" "verbar" "rcub" "tilde" nil
187 nil nil nil nil nil nil nil nil
188 nil nil nil nil nil nil nil nil
189 nil nil nil nil nil nil nil nil
190 nil nil nil nil nil nil nil nil
191 "nbsp" "iexcl" "cent" "pound" "curren" "yen" "brvbar" "sect"
192 "uml" "copy" "ordf" "laquo" "not" "shy" "reg" "macr"
193 "ring" "plusmn" "sup2" "sup3" "acute" "micro" "para" "middot"
194 "cedil" "sup1" "ordm" "raquo" "frac14" "frac12" "frac34" "iquest"
195 "Agrave" "Aacute" "Acirc" "Atilde" "Auml" "Aring" "AElig" "Ccedil"
196 "Egrave" "Eacute" "Ecirc" "Euml" "Igrave" "Iacute" "Icirc" "Iuml"
197 "ETH" "Ntilde" "Ograve" "Oacute" "Ocirc" "Otilde" "Ouml" nil
198 "Oslash" "Ugrave" "Uacute" "Ucirc" "Uuml" "Yacute" "THORN" "szlig"
199 "agrave" "aacute" "acirc" "atilde" "auml" "aring" "aelig" "ccedil"
200 "egrave" "eacute" "ecirc" "euml" "igrave" "iacute" "icirc" "iuml"
201 "eth" "ntilde" "ograve" "oacute" "ocirc" "otilde" "ouml" "divide"
202 "oslash" "ugrave" "uacute" "ucirc" "uuml" "yacute" "thorn" "yuml"]
203 "Vector of symbolic character names without `&' and `;'.")
205 (put 'sgml-table
'char-table-extra-slots
0)
207 (defvar sgml-char-names-table
208 (let ((table (make-char-table 'sgml-table
))
212 (setq elt
(aref sgml-char-names i
))
213 (if elt
(aset table
(make-char 'latin-iso8859-1 i
) elt
))
216 "A table for mapping non-ASCII characters into SGML entity names.
217 Currently, only Latin-1 characters are supported.")
219 ;; nsgmls is a free SGML parser in the SP suite available from
220 ;; ftp.jclark.com and otherwise packaged for GNU systems.
221 ;; Its error messages can be parsed by next-error.
222 ;; The -s option suppresses output.
224 (defcustom sgml-validate-command
"nsgmls -s" ; replaced old `sgmls'
225 "*The command to validate an SGML document.
226 The file name of current buffer file name will be appended to this,
227 separated by a space."
232 (defvar sgml-saved-validate-command nil
233 "The command last used to validate in this buffer.")
235 ;; I doubt that null end tags are used much for large elements,
236 ;; so use a small distance here.
237 (defcustom sgml-slash-distance
1000
238 "*If non-nil, is the maximum distance to search for matching `/'."
239 :type
'(choice (const nil
) integer
)
242 (defconst sgml-name-re
"[_:[:alpha:]][-_.:[:alnum:]]*")
243 (defconst sgml-tag-name-re
(concat "<\\([!/?]?" sgml-name-re
"\\)"))
244 (defconst sgml-attrs-re
"\\(?:[^\"'/><]\\|\"[^\"]*\"\\|'[^']*'\\)*")
245 (defconst sgml-start-tag-regex
(concat "<" sgml-name-re sgml-attrs-re
)
246 "Regular expression that matches a non-empty start tag.
247 Any terminating `>' or `/' is not matched.")
251 (defconst sgml-font-lock-keywords-1
252 `((,(concat "<\\([!?]" sgml-name-re
"\\)") 1 font-lock-keyword-face
)
253 (,(concat "<\\(/?" sgml-name-re
"\\)") 1 font-lock-function-name-face
)
254 ;; FIXME: this doesn't cover the variables using a default value.
255 (,(concat "\\(" sgml-name-re
"\\)=[\"']") 1 font-lock-variable-name-face
)
256 (,(concat "[&%]" sgml-name-re
";?") . font-lock-variable-name-face
)))
258 (defconst sgml-font-lock-keywords-2
260 sgml-font-lock-keywords-1
263 (regexp-opt (mapcar 'car sgml-tag-face-alist
) t
)
264 "\\([ \t][^>]*\\)?>\\([^<]+\\)</\\1>")
265 '(3 (cdr (assoc (downcase (match-string 1))
266 sgml-tag-face-alist
))))))))
268 ;; for font-lock, but must be defvar'ed after
269 ;; sgml-font-lock-keywords-1 and sgml-font-lock-keywords-2 above
270 (defvar sgml-font-lock-keywords sgml-font-lock-keywords-1
271 "*Rules for highlighting SGML code. See also `sgml-tag-face-alist'.")
273 (defvar sgml-font-lock-syntactic-keywords
274 ;; Use the `b' style of comments to avoid interference with the -- ... --
275 ;; comments recognized when `sgml-specials' includes ?-.
276 ;; FIXME: beware of <!--> blabla <!--> !!
277 '(("\\(<\\)!--" (1 "< b"))
278 ("--[ \t\n]*\\(>\\)" (1 "> b")))
279 "Syntactic keywords for `sgml-mode'.")
282 (defvar sgml-face-tag-alist
()
283 "Alist of face and tag name for facemenu.")
285 (defvar sgml-tag-face-alist
()
286 "Tag names and face or list of faces to fontify with when invisible.
287 When `font-lock-maximum-decoration' is 1 this is always used for fontifying.
288 When more these are fontified together with `sgml-font-lock-keywords'.")
290 (defvar sgml-display-text
()
291 "Tag names as lowercase symbols, and display string when invisible.")
294 (defvar sgml-tags-invisible nil
)
296 (defcustom sgml-tag-alist
297 '(("![" ("ignore" t
) ("include" t
))
302 "*Alist of tag names for completing read and insertion rules.
303 This alist is made up as
308 TAGRULE is a list of optionally t (no endtag) or `\\n' (separate endtag by
309 newlines) or a skeleton with nil, t or `\\n' in place of the interactor
310 followed by an ATTRIBUTERULE (for an always present attribute) or an
313 The attribute alist is made up as
315 ((\"attribute\" . ATTRIBUTERULE)
318 ATTRIBUTERULE is a list of optionally t (no value when no input) followed by
319 an optional alist of possible values."
320 :type
'(repeat (cons (string :tag
"Tag Name")
321 (repeat :tag
"Tag Rule" sexp
)))
324 (defcustom sgml-tag-help
325 '(("!" .
"Empty declaration for comment")
326 ("![" .
"Embed declarations with parser directive")
327 ("!attlist" .
"Tag attributes declaration")
328 ("!doctype" .
"Document type (DTD) declaration")
329 ("!element" .
"Tag declaration")
330 ("!entity" .
"Entity (macro) declaration"))
331 "*Alist of tag name and short description."
332 :type
'(repeat (cons (string :tag
"Tag Name")
333 (string :tag
"Description")))
336 (defcustom sgml-xml-mode nil
337 "*When non-nil, tag insertion functions will be XML-compliant.
338 If this variable is customized, the custom value is used always.
339 Otherwise, it is set to be buffer-local when the file has
340 a DOCTYPE or an XML declaration."
345 (defvar sgml-empty-tags nil
346 "List of tags whose !ELEMENT definition says EMPTY.")
348 (defvar sgml-unclosed-tags nil
349 "List of tags whose !ELEMENT definition says the end-tag is optional.")
351 (defun sgml-xml-guess ()
352 "Guess whether the current buffer is XML."
354 (goto-char (point-min))
355 (when (or (string= "xml" (file-name-extension (or buffer-file-name
"")))
356 (looking-at "\\s-*<\\?xml")
357 (when (re-search-forward
360 '("<!DOCTYPE" "\\(\\w+\\)" "\\(\\w+\\)"
361 "\"\\([^\"]+\\)\"" "\"\\([^\"]+\\)\"")
364 (string-match "X\\(HT\\)?ML" (match-string 3))))
365 (set (make-local-variable 'sgml-xml-mode
) t
))))
367 (defvar v2
) ; free for skeleton
369 (defun sgml-comment-indent-new-line (&optional soft
)
370 (let ((comment-start "-- ")
371 (comment-start-skip "\\(<!\\)?--[ \t]*")
373 (comment-style 'plain
))
374 (comment-indent-new-line soft
)))
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
)))
385 (define-derived-mode sgml-mode text-mode
"SGML"
386 "Major mode for editing SGML documents.
388 Keys <, &, SPC within <>, \", / and ' can be electric depending on
391 An argument of N to a tag-inserting command means to wrap it around
392 the next N words. In Transient Mark mode, when the mark is active,
393 N defaults to -1, which means to wrap it around the current region.
395 If you like upcased tags, put (setq sgml-transformation 'upcase) in
398 Use \\[sgml-validate] to validate your document with an SGML parser.
400 Do \\[describe-variable] sgml- SPC to see available variables.
401 Do \\[describe-key] on the following bindings to discover what they do.
403 (make-local-variable 'sgml-saved-validate-command
)
404 (make-local-variable 'facemenu-end-add-face
)
405 ;;(make-local-variable 'facemenu-remove-face-function)
406 ;; A start or end tag by itself on a line separates a paragraph.
407 ;; This is desirable because SGML discards a newline that appears
408 ;; immediately after a start tag or immediately before an end tag.
409 (set (make-local-variable 'paragraph-start
) (concat "[ \t]*$\\|\
410 \[ \t]*</?\\(" sgml-name-re sgml-attrs-re
"\\)?>"))
411 (set (make-local-variable 'paragraph-separate
)
412 (concat paragraph-start
"$"))
413 (set (make-local-variable 'adaptive-fill-regexp
) "[ \t]*")
414 (set (make-local-variable 'indent-line-function
) 'sgml-indent-line
)
415 (set (make-local-variable 'comment-start
) "<!-- ")
416 (set (make-local-variable 'comment-end
) " -->")
417 (set (make-local-variable 'comment-indent-function
) 'sgml-comment-indent
)
418 (set (make-local-variable 'comment-line-break-function
)
419 'sgml-comment-indent-new-line
)
420 (set (make-local-variable 'skeleton-further-elements
)
421 '((completion-ignore-case t
)))
422 (set (make-local-variable 'skeleton-end-hook
)
425 (not (or (eq v2
'\n) (eq (car-safe v2
) '\n)))
426 (newline-and-indent))))
427 (set (make-local-variable 'font-lock-defaults
)
428 '((sgml-font-lock-keywords
429 sgml-font-lock-keywords-1
430 sgml-font-lock-keywords-2
)
432 (font-lock-syntactic-keywords
433 . sgml-font-lock-syntactic-keywords
)))
434 (set (make-local-variable 'facemenu-add-face-function
)
435 'sgml-mode-facemenu-add-face-function
)
438 (setq mode-name
"XML")
439 (set (make-local-variable 'skeleton-transformation
) sgml-transformation
))
440 ;; This will allow existing comments within declarations to be
442 (set (make-local-variable 'comment-start-skip
) "\\(?:<!\\)?--[ \t]*")
443 (set (make-local-variable 'comment-end-skip
) "[ \t]*--\\([ \t\n]*>\\)?")
444 ;; This definition probably is not useful in derived modes.
445 (set (make-local-variable 'imenu-generic-expression
)
446 (concat "<!\\(element\\|entity\\)[ \t\n]+%?[ \t\n]*\\("
447 sgml-name-re
"\\)")))
449 ;; Some programs (such as Glade 2) generate XML which has
450 ;; -*- mode: xml -*-.
451 (defalias 'xml-mode
'sgml-mode
)
453 (defun sgml-comment-indent ()
454 (if (looking-at "--") comment-column
0))
456 (defun sgml-slash (arg)
457 "Insert ARG slash characters.
458 Behaves electrically if `sgml-quick-keys' is non-nil."
461 ((not (and (eq (char-before) ?
<) (= arg
1)))
462 (sgml-slash-matching arg
))
463 ((eq sgml-quick-keys
'indent
)
465 (indent-according-to-mode))
466 ((eq sgml-quick-keys
'close
)
467 (delete-backward-char 1)
470 (sgml-slash-matching arg
))))
472 (defun sgml-slash-matching (arg)
473 "Insert `/' and display any previous matching `/'.
474 Two `/'s are treated as matching if the first `/' ends a net-enabling
475 start tag, and the second `/' is the corresponding null end tag."
479 (let ((oldpos (point))
484 (if sgml-slash-distance
485 (narrow-to-region (max (point-min)
486 (- (point) sgml-slash-distance
))
488 (if (and (re-search-backward sgml-start-tag-regex
(point-min) t
)
489 (eq (match-end 0) (1- oldpos
)))
491 (goto-char (1- oldpos
))
492 (while (and (not blinkpos
)
493 (search-backward "/" (point-min) t
))
494 (let ((tagend (save-excursion
495 (if (re-search-backward sgml-start-tag-regex
499 (if (eq tagend
(point))
501 (setq blinkpos
(point))
502 (setq level
(1- level
)))
503 (setq level
(1+ level
)))))))
506 (if (pos-visible-in-window-p)
508 (message "Matches %s"
509 (buffer-substring (line-beginning-position)
510 (1+ blinkpos
)))))))))
512 ;; Why doesn't this use the iso-cvt table or, preferably, generate the
513 ;; inverse of the extensive table in the SGML Quail input method? -- fx
514 ;; I guess that's moot since it only works with Latin-1 anyhow.
515 (defun sgml-name-char (&optional char
)
516 "Insert a symbolic character name according to `sgml-char-names'.
517 Non-ASCII chars may be inserted either with the meta key, as in M-SPC for
518 no-break space or M-- for a soft hyphen; or via an input method or
519 encoded keyboard operation."
523 (setq char
(read-quoted-char "Enter char or octal number")))
524 (delete-backward-char 1)
527 (delete-backward-char 1)
531 (or (aref sgml-char-names char
)
534 ((aref sgml-char-names-table char
)
535 (insert ?
& (aref sgml-char-names-table char
) ?\
;))
536 ((let ((c (encode-char char
'ucs
)))
538 (insert (format "&#%d;" c
))
540 (t ; should be an error? -- fx
543 (defun sgml-name-self ()
544 "Insert a symbolic character name according to `sgml-char-names'."
546 (sgml-name-char last-command-char
))
548 (defun sgml-maybe-name-self ()
549 "Insert a symbolic character name according to `sgml-char-names'."
551 (if sgml-name-8bit-mode
552 (let ((mc last-command-char
))
554 (setq mc
(unibyte-char-to-multibyte mc
)))
555 (or mc
(setq mc last-command-char
))
557 (self-insert-command 1)))
559 (defun sgml-name-8bit-mode ()
560 "Toggle whether to insert named entities instead of non-ASCII characters.
561 This only works for Latin-1 input."
563 (setq sgml-name-8bit-mode
(not sgml-name-8bit-mode
))
564 (message "sgml name entity mode is now %s"
565 (if sgml-name-8bit-mode
"ON" "OFF")))
567 ;; When an element of a skeleton is a string "str", it is passed
568 ;; through skeleton-transformation and inserted. If "str" is to be
569 ;; inserted literally, one should obtain it as the return value of a
570 ;; function, e.g. (identity "str").
572 (define-skeleton sgml-tag
573 "Prompt for a tag and insert it, optionally with attributes.
574 Completion and configuration are done according to `sgml-tag-alist'.
575 If you like tags and attributes in uppercase do \\[set-variable]
576 skeleton-transformation RET upcase RET, or put this in your `.emacs':
577 (setq sgml-transformation 'upcase)"
578 (funcall (or skeleton-transformation
'identity
)
579 (completing-read "Tag: " sgml-tag-alist
))
581 (("") -
1 '(undo-boundary) (identity "<")) |
; see comment above
582 `(("") '(setq v2
(sgml-attributes ,str t
)) ?
>
586 '(("") " [ " _
" ]]"))
587 ((and (eq v2 t
) sgml-xml-mode
(member ,str sgml-empty-tags
))
589 ((or (and (eq v2 t
) (not sgml-xml-mode
)) (string-match "^[/!?]" ,str
))
592 ;; Make sure we don't fall into an infinite loop.
593 ;; For xhtml's `tr' tag, we should maybe use \n instead.
594 (if (eq v2 t
) (setq v2 nil
))
595 ;; We use `identity' to prevent skeleton from passing
596 ;; `str' through skeleton-transformation a second time.
597 '(("") v2 _ v2
"</" (identity ',str
) ?
>))
599 (cons '("") (cdr v2
)))
601 (append '(("") (car v2
))
603 '(resume: (car v2
) _
"</" (identity ',str
) ?
>))))))
605 (autoload 'skeleton-read
"skeleton")
607 (defun sgml-attributes (tag &optional quiet
)
608 "When at top level of a tag, interactively insert attributes.
610 Completion and configuration of TAG are done according to `sgml-tag-alist'.
611 If QUIET, do not print a message when there are no attributes for TAG."
612 (interactive (list (save-excursion (sgml-beginning-of-tag t
))))
613 (or (stringp tag
) (error "Wrong context for adding attribute"))
615 (let ((completion-ignore-case t
)
616 (alist (cdr (assoc (downcase tag
) sgml-tag-alist
)))
618 (if (or (symbolp (car alist
))
619 (symbolp (car (car alist
))))
620 (setq car
(car alist
)
623 (message "No attributes configured."))
624 (if (stringp (car alist
))
626 (insert (if (eq (preceding-char) ?
) "" ?
)
627 (funcall skeleton-transformation
(car alist
)))
629 (setq i
(length alist
))
632 (insert (funcall skeleton-transformation
634 (skeleton-read '(completing-read
637 (if (string= "" attribute
)
639 (sgml-value (assoc (downcase attribute
) alist
))
641 (if (eq (preceding-char) ?
)
642 (delete-backward-char 1)))
645 (defun sgml-auto-attributes (arg)
646 "Self insert the character typed; at top level of tag, prompt for attributes.
647 With prefix argument, only self insert."
649 (let ((point (point))
652 (not sgml-tag-alist
) ; no message when nothing configured
653 (symbolp (setq tag
(save-excursion (sgml-beginning-of-tag t
))))
654 (eq (aref tag
0) ?
/))
655 (self-insert-command (prefix-numeric-value arg
))
656 (sgml-attributes tag
)
657 (setq last-command-char ?
)
658 (or (> (point) point
)
659 (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")))
678 (defun sgml-maybe-end-tag (&optional arg
)
679 "Name self unless in position to end a tag or a prefix ARG is given."
681 (if (or arg
(eq (car (sgml-lexical-context)) 'tag
))
682 (self-insert-command (prefix-numeric-value arg
))
685 (defun sgml-skip-tag-backward (arg)
686 "Skip to beginning of tag or matching opening tag if present.
687 With prefix argument ARG, repeat this ARG times."
690 (search-backward "<" nil t
)
691 (if (looking-at "</\\([^ \n\t>]+\\)")
692 ;; end tag, skip any nested pairs
693 (let ((case-fold-search t
)
694 (re (concat "</?" (regexp-quote (match-string 1)))))
695 (while (and (re-search-backward re nil t
)
696 (eq (char-after (1+ (point))) ?
/))
698 (sgml-skip-tag-backward 1))))
699 (setq arg
(1- arg
))))
701 (defun sgml-skip-tag-forward (arg &optional return
)
702 "Skip to end of tag or matching closing tag if present.
703 With prefix argument ARG, repeat this ARG times.
704 Return t iff after a closing tag."
708 (skip-chars-forward "^<>")
709 (if (eq (following-char) ?
>)
711 (if (looking-at "<\\([^/ \n\t>]+\\)")
712 ;; start tag, skip any nested same pairs _and_ closing tag
713 (let ((case-fold-search t
)
714 (re (concat "</?" (regexp-quote (match-string 1))))
718 (while (and (re-search-forward re nil t
)
720 (eq (char-after (1+ (match-beginning 0))) ?
/)))
722 (sgml-skip-tag-forward 1))
732 (defun sgml-delete-tag (arg)
733 "Delete tag on or after cursor, and matching closing or opening tag.
734 With prefix argument ARG, repeat this ARG times."
739 (if (looking-at "[ \t\n]*<")
741 (if (eq (char-after (match-end 0)) ?
/)
745 (goto-char (match-end 0))))
747 (or (save-excursion (setq close
(sgml-beginning-of-tag)
748 close
(and (stringp close
)
749 (eq (aref close
0) ?
/)
751 ;; not on closing tag
752 (let ((point (point)))
753 (sgml-skip-tag-backward 1)
754 (if (or (not (eq (following-char) ?
<))
758 (error "Not on or before tag")))))
761 (sgml-skip-tag-backward 1)
766 (sgml-skip-tag-forward 1)
769 (if (eq (aref (sgml-beginning-of-tag) 0) ?
/)
773 (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) ?
>)))
845 (autoload 'compile-internal
"compile")
847 (defun sgml-validate (command)
848 "Validate an SGML document.
849 Runs COMMAND, a shell command, in a separate process asynchronously
850 with output going to the buffer `*compilation*'.
851 You can then use the command \\[next-error] to find the next error message
852 and move to the line in the SGML document that caused it."
854 (list (read-string "Validate command: "
855 (or sgml-saved-validate-command
856 (concat sgml-validate-command
858 (let ((name (buffer-file-name)))
860 (file-name-nondirectory name
))))))))
861 (setq sgml-saved-validate-command command
)
862 (save-some-buffers (not compilation-ask-about-save
) nil
)
863 (compile-internal command
"No more errors"))
865 (defsubst sgml-at-indentation-p
()
866 "Return true if point is at the first non-whitespace character on the line."
868 (skip-chars-backward " \t")
871 (defun sgml-lexical-context (&optional limit
)
872 "Return the lexical context at point as (TYPE . START).
873 START is the location of the start of the lexical element.
874 TYPE is one of `string', `comment', `tag', `cdata', or `text'.
876 Optional argument LIMIT is the position to start parsing from.
877 If nil, start from a preceding tag at indentation."
883 ;; Skip tags backwards until we find one at indentation
884 (while (and (ignore-errors (sgml-parse-tag-backward))
885 (not (sgml-at-indentation-p)))))
886 (with-syntax-table sgml-tag-syntax-table
887 (while (< (point) pos
)
888 ;; When entering this loop we're inside text.
889 (setq text-start
(point))
890 (skip-chars-forward "^<" pos
)
894 ;; We got to the end without seeing a tag.
896 ((looking-at "<!\\[[A-Z]+\\[")
897 ;; We've found a CDATA section or similar.
898 (let ((cdata-start (point)))
899 (unless (search-forward "]]>" pos
'move
)
900 (list 0 nil nil
'cdata nil nil nil nil cdata-start
))))
902 ;; We've reached a tag. Parse it.
903 ;; FIXME: Handle net-enabling start-tags
904 (parse-partial-sexp (point) pos
0))))))
906 ((eq (nth 3 state
) 'cdata
) (cons 'cdata
(nth 8 state
)))
907 ((nth 3 state
) (cons 'string
(nth 8 state
)))
908 ((nth 4 state
) (cons 'comment
(nth 8 state
)))
909 ((and state
(> (nth 0 state
) 0)) (cons 'tag
(nth 1 state
)))
910 (t (cons 'text text-start
))))))
912 (defun sgml-beginning-of-tag (&optional top-level
)
913 "Skip to beginning of tag and return its name.
914 If this can't be done, return nil."
915 (let ((context (sgml-lexical-context)))
916 (if (eq (car context
) 'tag
)
918 (goto-char (cdr context
))
919 (when (looking-at sgml-tag-name-re
)
920 (match-string-no-properties 1)))
922 (when (not (eq (car context
) 'text
))
923 (goto-char (cdr context
))
924 (sgml-beginning-of-tag t
))))))
926 (defun sgml-value (alist)
927 "Interactively insert value taken from attribute-rule ALIST.
928 See `sgml-tag-alist' for info about attribute rules."
929 (setq alist
(cdr alist
))
930 (if (stringp (car alist
))
931 (insert "=\"" (car alist
) ?
\")
932 (if (and (eq (car alist
) t
) (not sgml-xml-mode
))
935 (setq alist
(skeleton-read '(completing-read "Value: " (cdr alist
))))
936 (if (string< "" alist
)
938 (delete-backward-char 2)))
941 (insert (skeleton-read '(completing-read "Value: " alist
))))
944 (defun sgml-quote (start end
&optional unquotep
)
945 "Quote SGML text in region START ... END.
946 Only &, < and > are quoted, the rest is left untouched.
947 With prefix argument UNQUOTEP, unquote the region."
950 (narrow-to-region start end
)
951 (goto-char (point-min))
953 ;; FIXME: We should unquote other named character references as well.
954 (while (re-search-forward
955 "\\(&\\(amp\\|\\(l\\|\\(g\\)\\)t\\)\\)[][<>&;\n\t \"%!'(),/=?]"
957 (replace-match (if (match-end 4) ">" (if (match-end 3) "<" "&")) t t
958 nil
(if (eq (char-before (match-end 0)) ?\
;) 0 1)))
959 (while (re-search-forward "[&<>]" nil t
)
960 (replace-match (cdr (assq (char-before) '((?
& .
"&")
965 (defun sgml-pretty-print (beg end
)
966 "Simple-minded pretty printer for SGML.
967 Re-indents the code and inserts newlines between BEG and END.
968 You might want to turn on `auto-fill-mode' to get better results."
970 ;; - insert newline between some start-tag and text.
971 ;; - don't insert newline in front of some end-tags.
979 ;; Don't use narrowing because it screws up auto-indent.
980 (setq end
(copy-marker end t
))
981 (with-syntax-table sgml-tag-syntax-table
982 (while (re-search-forward "<" end t
)
983 (goto-char (match-beginning 0))
984 (unless (or ;;(looking-at "</")
985 (progn (skip-chars-backward " \t") (bolp)))
986 (reindent-then-newline-and-indent))
988 ;; (indent-region beg end)
995 (:constructor sgml-make-tag
(type start end name
)))
998 (defsubst sgml-parse-tag-name
()
999 "Skip past a tag-name, and return the name."
1000 (buffer-substring-no-properties
1001 (point) (progn (skip-syntax-forward "w_") (point))))
1003 (defsubst sgml-looking-back-at
(str)
1004 "Return t if the test before point matches STR."
1005 (let ((start (- (point) (length str
))))
1006 (and (>= start
(point-min))
1007 (equal str
(buffer-substring-no-properties start
(point))))))
1009 (defun sgml-parse-tag-backward ()
1010 "Parse an SGML tag backward, and return information about the tag.
1011 Assume that parsing starts from within a textual context.
1012 Leave point at the beginning of the tag."
1013 (let (tag-type tag-start tag-end name
)
1014 (or (search-backward ">" nil
'move
)
1015 (error "No tag found"))
1016 (setq tag-end
(1+ (point)))
1018 ((sgml-looking-back-at "--") ; comment
1019 (setq tag-type
'comment
1020 tag-start
(search-backward "<!--" nil t
)))
1021 ((sgml-looking-back-at "]]") ; cdata
1022 (setq tag-type
'cdata
1023 tag-start
(re-search-backward "<!\\[[A-Z]+\\[" nil t
)))
1026 (with-syntax-table sgml-tag-syntax-table
1030 (goto-char (1+ tag-start
))
1033 (setq tag-type
'decl
))
1034 (??
; processing-instruction
1035 (setq tag-type
'pi
))
1038 (setq tag-type
'close
1039 name
(sgml-parse-tag-name)))
1041 (setq tag-type
'jsp
))
1042 (t ; open or empty tag
1043 (setq tag-type
'open
1044 name
(sgml-parse-tag-name))
1045 (if (or (eq ?
/ (char-before (- tag-end
1)))
1046 (sgml-empty-tag-p name
))
1047 (setq tag-type
'empty
))))))
1048 (goto-char tag-start
)
1049 (sgml-make-tag tag-type tag-start tag-end name
)))
1051 (defun sgml-get-context (&optional full
)
1052 "Determine the context of the current position.
1053 If FULL is `empty', return even if the context is empty (i.e.
1054 we just skipped over some element and got to a beginning of line).
1055 If FULL is non-nil, parse back to the beginning of the buffer, otherwise
1056 parse until we find a start-tag as the first thing on a line.
1058 The context is a list of tag-info structures. The last one is the tag
1059 immediately enclosing the current position."
1060 (let ((here (point))
1064 ;; CONTEXT keeps track of the tag-stack
1065 ;; IGNORE keeps track of the nesting level of point relative to the
1066 ;; first (outermost) tag on the context. This is the list of
1067 ;; enclosing start-tags we'll have to ignore.
1068 (skip-chars-backward " \t\n") ; Make sure we're not at indentation.
1071 (not (if full
(eq full
'empty
) context
))
1072 (not (sgml-at-indentation-p))
1074 (/= (point) (sgml-tag-start (car context
)))
1075 (sgml-unclosed-tag-p (sgml-tag-name (car context
)))))
1076 (setq tag-info
(ignore-errors (sgml-parse-tag-backward))))
1078 ;; This tag may enclose things we thought were tags. If so,
1081 (> (sgml-tag-end tag-info
)
1082 (sgml-tag-end (car context
))))
1083 (setq context
(cdr context
)))
1088 ((eq (sgml-tag-type tag-info
) 'open
)
1092 (sgml-unclosed-tag-p (sgml-tag-name tag-info
))
1093 (eq t
(compare-strings
1094 (sgml-tag-name tag-info
) nil nil
1095 (sgml-tag-name (car context
)) nil nil t
)))
1096 ;; There was an implicit end-tag.
1098 (push tag-info context
)))
1099 ((eq t
(compare-strings (sgml-tag-name tag-info
) nil nil
1100 (car ignore
) nil nil t
))
1101 (setq ignore
(cdr ignore
)))
1103 ;; The open and close tags don't match.
1104 (if (not sgml-xml-mode
)
1105 (unless (sgml-unclosed-tag-p (sgml-tag-name tag-info
))
1106 (message "Unclosed tag <%s>" (sgml-tag-name tag-info
))
1108 ;; We could just assume that the tag is simply not closed
1109 ;; but it's a bad assumption when tags *are* closed but
1110 ;; not properly nested.
1111 (while (and (cdr tmp
)
1112 (not (eq t
(compare-strings
1113 (sgml-tag-name tag-info
) nil nil
1114 (cadr tmp
) nil nil t
))))
1115 (setq tmp
(cdr tmp
)))
1116 (if (cdr tmp
) (setcdr tmp
(cddr tmp
)))))
1117 (message "Unmatched tags <%s> and </%s>"
1118 (sgml-tag-name tag-info
) (pop ignore
))))))
1121 ((eq (sgml-tag-type tag-info
) 'close
)
1122 (if (sgml-empty-tag-p (sgml-tag-name tag-info
))
1123 (message "Spurious </%s>: empty tag" (sgml-tag-name tag-info
))
1124 (push (sgml-tag-name tag-info
) ignore
)))
1130 (defun sgml-show-context (&optional full
)
1131 "Display the current context.
1132 If FULL is non-nil, parse back to the beginning of the buffer."
1134 (with-output-to-temp-buffer "*XML Context*"
1136 (let ((context (sgml-get-context)))
1139 (while (setq more
(sgml-get-context))
1140 (setq context
(nconc more context
)))))
1144 ;; Editing shortcuts
1146 (defun sgml-close-tag ()
1147 "Insert a close-tag for the current element."
1149 (case (car (sgml-lexical-context))
1150 (comment (insert " -->"))
1151 (cdata (insert "]]>"))
1153 (jsp (insert " %>"))
1154 (tag (insert " />"))
1156 (let ((context (save-excursion (sgml-get-context))))
1159 (insert "</" (sgml-tag-name (car (last context
))) ">")
1160 (indent-according-to-mode)))))
1162 (error "Nothing to close"))))
1164 (defun sgml-empty-tag-p (tag-name)
1165 "Return non-nil if TAG-NAME is an implicitly empty tag."
1166 (and (not sgml-xml-mode
)
1167 (member-ignore-case tag-name sgml-empty-tags
)))
1169 (defun sgml-unclosed-tag-p (tag-name)
1170 "Return non-nil if TAG-NAME is a tag for which an end-tag is optional."
1171 (and (not sgml-xml-mode
)
1172 (member-ignore-case tag-name sgml-unclosed-tags
)))
1174 (defun sgml-calculate-indent ()
1175 "Calculate the column to which this line should be indented."
1176 (let ((lcon (sgml-lexical-context)))
1178 ;; Indent comment-start markers inside <!-- just like comment-end markers.
1179 (if (and (eq (car lcon
) 'tag
)
1181 (save-excursion (goto-char (cdr lcon
)) (looking-at "<!--")))
1182 (setq lcon
(cons 'comment
(+ (cdr lcon
) 2))))
1187 ;; Go back to previous non-empty line.
1188 (while (and (> (point) (cdr lcon
))
1189 (zerop (forward-line -
1))
1190 (looking-at "[ \t]*$")))
1191 (if (> (point) (cdr lcon
))
1192 ;; Previous line is inside the string.
1193 (current-indentation)
1194 (goto-char (cdr lcon
))
1195 (1+ (current-column))))
1198 (let ((mark (looking-at "--")))
1199 ;; Go back to previous non-empty line.
1200 (while (and (> (point) (cdr lcon
))
1201 (zerop (forward-line -
1))
1202 (or (looking-at "[ \t]*$")
1203 (if mark
(not (looking-at "[ \t]*--"))))))
1204 (if (> (point) (cdr lcon
))
1205 ;; Previous line is inside the comment.
1206 (skip-chars-forward " \t")
1207 (goto-char (cdr lcon
)))
1208 (when (and (not mark
) (looking-at "--"))
1209 (forward-char 2) (skip-chars-forward " \t"))
1216 (goto-char (1+ (cdr lcon
)))
1217 (skip-chars-forward "^ \t\n") ;Skip tag name.
1218 (skip-chars-forward " \t")
1221 ;; This is the first attribute: indent.
1222 (goto-char (1+ (cdr lcon
)))
1223 (+ (current-column) sgml-basic-offset
)))
1226 (while (looking-at "</")
1228 (skip-chars-forward " \t"))
1229 (let* ((here (point))
1230 (unclosed (and ;; (not sgml-xml-mode)
1231 (looking-at sgml-tag-name-re
)
1232 (member-ignore-case (match-string 1)
1236 ;; If possible, align on the previous non-empty text line.
1237 ;; Otherwise, do a more serious parsing to find the
1238 ;; tag(s) relative to which we should be indenting.
1239 (if (and (not unclosed
) (skip-chars-backward " \t")
1240 (< (skip-chars-backward " \t\n") 0)
1241 (back-to-indentation)
1242 (> (point) (cdr lcon
)))
1245 (nreverse (sgml-get-context (if unclosed nil
'empty
)))))
1247 ;; Ignore previous unclosed start-tag in context.
1248 (while (and context unclosed
1249 (eq t
(compare-strings
1250 (sgml-tag-name (car context
)) nil nil
1251 unclosed nil nil t
)))
1252 (setq context
(cdr context
)))
1253 ;; Indent to reflect nesting.
1255 (goto-char (sgml-tag-end (car context
)))
1256 (skip-chars-forward " \t\n")
1257 (< (point) here
) (sgml-at-indentation-p))
1261 (* sgml-basic-offset
(length context
))))))
1264 (error "Unrecognised context %s" (car lcon
)))
1268 (defun sgml-indent-line ()
1269 "Indent the current line as SGML."
1271 (let* ((savep (point))
1274 (back-to-indentation)
1275 (if (>= (point) savep
) (setq savep nil
))
1276 (sgml-calculate-indent))))
1278 (save-excursion (indent-line-to indent-col
))
1279 (indent-line-to indent-col
))))
1281 (defun sgml-guess-indent ()
1282 "Guess an appropriate value for `sgml-basic-offset'.
1283 Base the guessed identation level on the first indented tag in the buffer.
1284 Add this to `sgml-mode-hook' for convenience."
1287 (goto-char (point-min))
1288 (if (re-search-forward "^\\([ \t]+\\)<" 500 'noerror
)
1290 (set (make-local-variable 'sgml-basic-offset
)
1291 (1- (current-column)))
1292 (message "Guessed sgml-basic-offset = %d"
1296 (defun sgml-parse-dtd ()
1297 "Simplistic parse of the current buffer as a DTD.
1298 Currently just returns (EMPTY-TAGS UNCLOSED-TAGS)."
1299 (goto-char (point-min))
1302 (while (re-search-forward "<!ELEMENT[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+[-O][ \t\n]+\\([-O]\\)[ \t\n]+\\([^ \t\n]+\\)" nil t
)
1304 ((string= (match-string 3) "EMPTY")
1305 (push (match-string-no-properties 1) empty
))
1306 ((string= (match-string 2) "O")
1307 (push (match-string-no-properties 1) unclosed
))))
1308 (setq empty
(sort (mapcar 'downcase empty
) 'string
<))
1309 (setq unclosed
(sort (mapcar 'downcase unclosed
) 'string
<))
1310 (list empty unclosed
)))
1314 (defcustom html-mode-hook nil
1315 "Hook run by command `html-mode'.
1316 `text-mode-hook' and `sgml-mode-hook' are run first."
1319 :options
'(html-autoview-mode))
1321 (defvar html-quick-keys sgml-quick-keys
1322 "Use C-c X combinations for quick insertion of frequent tags when non-nil.
1323 This defaults to `sgml-quick-keys'.
1324 This takes effect when first loading the library.")
1326 (defvar html-mode-map
1327 (let ((map (make-sparse-keymap))
1328 (menu-map (make-sparse-keymap "HTML")))
1329 (set-keymap-parent map sgml-mode-map
)
1330 (define-key map
"\C-c6" 'html-headline-6
)
1331 (define-key map
"\C-c5" 'html-headline-5
)
1332 (define-key map
"\C-c4" 'html-headline-4
)
1333 (define-key map
"\C-c3" 'html-headline-3
)
1334 (define-key map
"\C-c2" 'html-headline-2
)
1335 (define-key map
"\C-c1" 'html-headline-1
)
1336 (define-key map
"\C-c\r" 'html-paragraph
)
1337 (define-key map
"\C-c\n" 'html-line
)
1338 (define-key map
"\C-c\C-c-" 'html-horizontal-rule
)
1339 (define-key map
"\C-c\C-co" 'html-ordered-list
)
1340 (define-key map
"\C-c\C-cu" 'html-unordered-list
)
1341 (define-key map
"\C-c\C-cr" 'html-radio-buttons
)
1342 (define-key map
"\C-c\C-cc" 'html-checkboxes
)
1343 (define-key map
"\C-c\C-cl" 'html-list-item
)
1344 (define-key map
"\C-c\C-ch" 'html-href-anchor
)
1345 (define-key map
"\C-c\C-cn" 'html-name-anchor
)
1346 (define-key map
"\C-c\C-ci" 'html-image
)
1347 (when html-quick-keys
1348 (define-key map
"\C-c-" 'html-horizontal-rule
)
1349 (define-key map
"\C-co" 'html-ordered-list
)
1350 (define-key map
"\C-cu" 'html-unordered-list
)
1351 (define-key map
"\C-cr" 'html-radio-buttons
)
1352 (define-key map
"\C-cc" 'html-checkboxes
)
1353 (define-key map
"\C-cl" 'html-list-item
)
1354 (define-key map
"\C-ch" 'html-href-anchor
)
1355 (define-key map
"\C-cn" 'html-name-anchor
)
1356 (define-key map
"\C-ci" 'html-image
))
1357 (define-key map
"\C-c\C-s" 'html-autoview-mode
)
1358 (define-key map
"\C-c\C-v" 'browse-url-of-buffer
)
1359 (define-key map
[menu-bar html
] (cons "HTML" menu-map
))
1360 (define-key menu-map
[html-autoview-mode
]
1361 '("Toggle Autoviewing" . html-autoview-mode
))
1362 (define-key menu-map
[browse-url-of-buffer
]
1363 '("View Buffer Contents" . browse-url-of-buffer
))
1364 (define-key menu-map
[nil] '("--"))
1365 ;;(define-key menu-map "6" '("Heading 6" . html-headline-6))
1366 ;;(define-key menu-map "5" '("Heading 5" . html-headline-5))
1367 ;;(define-key menu-map "4" '("Heading 4" . html-headline-4))
1368 (define-key menu-map "3" '("Heading 3" . html-headline-3))
1369 (define-key menu-map "2" '("Heading 2" . html-headline-2))
1370 (define-key menu-map "1" '("Heading 1" . html-headline-1))
1371 (define-key menu-map "l" '("Radio Buttons" . html-radio-buttons))
1372 (define-key menu-map "c" '("Checkboxes" . html-checkboxes))
1373 (define-key menu-map "l" '("List Item" . html-list-item))
1374 (define-key menu-map "u" '("Unordered List" . html-unordered-list))
1375 (define-key menu-map "o" '("Ordered List" . html-ordered-list))
1376 (define-key menu-map "-" '("Horizontal Rule" . html-horizontal-rule))
1377 (define-key menu-map "\n" '("Line Break" . html-line))
1378 (define-key menu-map "\r" '("Paragraph" . html-paragraph))
1379 (define-key menu-map "i" '("Image" . html-image))
1380 (define-key menu-map "h" '("Href Anchor" . html-href-anchor))
1381 (define-key menu-map "n" '("Name Anchor" . html-name-anchor))
1383 "Keymap for commands for use in HTML mode.")
1385 (defvar html-face-tag-alist
1390 "Value of `sgml-face-tag-alist' for HTML mode.")
1392 (defvar html-tag-face-alist
1395 ("blink" . highlight)
1398 ("h1" bold underline)
1399 ("h2" bold-italic underline)
1400 ("h3" italic underline)
1409 ("title" bold underline)
1413 "Value of `sgml-tag-face-alist' for HTML mode.")
1415 (defvar html-display-text
1419 "Value of `sgml-display-text' for HTML mode.")
1422 ;; should code exactly HTML 3 here when that is finished
1423 (defvar html-tag-alist
1424 (let* ((1-7 '(("1") ("2") ("3") ("4") ("5") ("6") ("7")))
1425 (1-9 `(,@1-7 ("8") ("9")))
1426 (align '(("align" ("left") ("center") ("right"))))
1427 (valign '(("top") ("middle") ("bottom") ("baseline")))
1428 (rel '(("next") ("previous") ("parent") ("subdocument") ("made")))
1429 (href '("href" ("ftp:") ("file:") ("finger:") ("gopher:") ("http:")
1430 ("mailto:") ("news:") ("rlogin:") ("telnet:") ("tn3270:")
1431 ("wais:") ("/cgi-bin/")))
1437 (list '((nil \n ("List item: " "<li>" str
1438 (if sgml-xml-mode "</li>") \n))))
1445 ;; put ,-expressions first, else byte-compile chokes (as of V19.29)
1446 ;; and like this it's more efficient anyway
1447 `(("a" ,name ,@link)
1450 ("font" nil "size" ("-1") ("+1") ("-2") ("+2") ,@1-7)
1451 ("form" (\n _ \n "<input type=\"submit\" value=\"\""
1452 (if sgml-xml-mode "/>" ">"))
1453 ("action" ,@(cdr href)) ("method" ("get") ("post")))
1460 ("hr" t ("size" ,@1-9) ("width") ("noshade" t) ,@align)
1461 ("img" t ("align" ,@valign ("texttop") ("absmiddle") ("absbottom"))
1462 ("src") ("alt") ("width" "1") ("height" "1")
1463 ("border" "1") ("vspace" "1") ("hspace" "1") ("ismap" t))
1464 ("input" t ("size" ,@1-9) ("maxlength" ,@1-9) ("checked" t) ,name
1465 ("type" ("text") ("password") ("checkbox") ("radio")
1466 ("submit") ("reset"))
1470 ("ol" ,@list ("type" ("A") ("a") ("I") ("i") ("1")))
1474 "<option>" str (if sgml-xml-mode "</option>") \n))
1475 ,name ("size" ,@1-9) ("multiple" t))
1477 ((completing-read "Cell kind: " '(("td") ("th"))
1480 (if sgml-xml-mode (concat "<" str "></tr>")) \n))
1481 ("border" t ,@1-9) ("width" "10") ("cellpadding"))
1483 ("textarea" ,name ("rows" ,@1-9) ("cols" ,@1-9))
1485 ("ul" ,@list ("type" ("disc") ("circle") ("square")))
1493 ("Item: " "<item>" str (if sgml-xml-mode "</item>") \n))
1500 ("body" \n ("background" ".gif") ("bgcolor" "#") ("text" "#")
1501 ("link" "#") ("alink" "#") ("vlink" "#"))
1502 ("box" (nil _ "<over>" _ (if sgml-xml-mode "</over>")))
1503 ("br" t ("clear" ("left") ("right")))
1504 ("caption" ("valign" ("top") ("bottom")))
1508 ("dd" ,(not sgml-xml-mode))
1514 "<dt>" str (if sgml-xml-mode "</dt>")
1515 "<dd>" _ (if sgml-xml-mode "</dd>") \n)))
1516 ("dt" (t _ (if sgml-xml-mode "</dt>")
1517 "<dd>" (if sgml-xml-mode "</dd>") \n))
1519 ;("fn" "id" "fn") ; ???
1523 "<title>" (setq str (read-input "Title: ")) "</title>\n"
1525 "<body>\n<h1>" str "</h1>\n" _
1526 "\n<address>\n<a href=\"mailto:"
1528 "\">" (user-full-name) "</a>\n</address>\n"
1533 ("isindex" t ("action") ("prompt"))
1536 ("li" ,(not sgml-xml-mode))
1539 ("option" t ("value") ("label") ("selected" t))
1568 "*Value of `sgml-tag-alist' for HTML mode.")
1570 (defvar html-tag-help
1572 ("a" . "Anchor of point or link elsewhere")
1575 ("address" . "Formatted mail address")
1576 ("array" . "Math array")
1579 ("base" . "Base address for URLs")
1580 ("big" . "Font size")
1581 ("blink" . "Blinking text")
1582 ("blockquote" . "Indented quotation")
1583 ("body" . "Document body")
1584 ("box" . "Math fraction")
1585 ("br" . "Line break")
1586 ("caption" . "Table caption")
1587 ("center" . "Centered text")
1588 ("changed" . "Change bars")
1589 ("cite" . "Citation of a document")
1590 ("code" . "Formatted source code")
1591 ("dd" . "Definition of term")
1594 ("dir" . "Directory list (obsolete)")
1595 ("dl" . "Definition list")
1596 ("dt" . "Term to be definined")
1597 ("em" . "Emphasised")
1598 ("embed" . "Embedded data in foreign format")
1600 ("figa" . "Figure anchor")
1601 ("figd" . "Figure description")
1602 ("figt" . "Figure text")
1604 ("font" . "Font size")
1605 ("form" . "Form with input fields")
1606 ("group" . "Document grouping")
1607 ("h1" . "Most important section headline")
1608 ("h2" . "Important section headline")
1609 ("h3" . "Section headline")
1610 ("h4" . "Minor section headline")
1611 ("h5" . "Unimportant section headline")
1612 ("h6" . "Least important section headline")
1613 ("head" . "Document header")
1614 ("hr" . "Horizontal rule")
1615 ("html" . "HTML Document")
1616 ("i" . "Italic face")
1617 ("img" . "Graphic image")
1618 ("input" . "Form input field")
1620 ("isindex" . "Input field for index search")
1621 ("kbd" . "Keybard example face")
1622 ("lang" . "Natural language")
1623 ("li" . "List item")
1624 ("link" . "Link relationship")
1625 ("math" . "Math formula")
1626 ("menu" . "Menu list (obsolete)")
1627 ("mh" . "Form mail header")
1628 ("nextid" . "Allocate new id")
1629 ("nobr" . "Text without line break")
1630 ("ol" . "Ordered list")
1631 ("option" . "Selection list item")
1632 ("over" . "Math fraction rule")
1633 ("p" . "Paragraph start")
1634 ("panel" . "Floating panel")
1636 ("pre" . "Preformatted fixed width text")
1638 ("rev" . "Reverse video")
1640 ("samp" . "Sample text")
1641 ("select" . "Selection list")
1642 ("small" . "Font size")
1643 ("sp" . "Nobreak space")
1644 ("strong" . "Standout text")
1645 ("sub" . "Subscript")
1646 ("sup" . "Superscript")
1647 ("table" . "Table with rows and columns")
1648 ("tb" . "Table vertical break")
1649 ("td" . "Table data cell")
1650 ("textarea" . "Form multiline edit area")
1651 ("th" . "Table header cell")
1652 ("title" . "Document title")
1653 ("tr" . "Table row separator")
1654 ("tt" . "Typewriter face")
1655 ("u" . "Underlined text")
1656 ("ul" . "Unordered list")
1657 ("var" . "Math variable face")
1658 ("wbr" . "Enable <br> within <nobr>"))
1659 "*Value of `sgml-tag-help' for HTML mode.")
1663 (define-derived-mode html-mode sgml-mode "HTML"
1664 "Major mode based on SGML mode for editing HTML documents.
1665 This allows inserting skeleton constructs used in hypertext documents with
1666 completion. See below for an introduction to HTML. Use
1667 \\[browse-url-of-buffer] to see how this comes out. See also `sgml-mode' on
1668 which this is based.
1670 Do \\[describe-variable] html- SPC and \\[describe-variable] sgml- SPC to see available variables.
1672 To write fairly well formatted pages you only need to know few things. Most
1673 browsers have a function to read the source code of the page being seen, so
1674 you can imitate various tricks. Here's a very short HTML primer which you
1675 can also view with a browser to see what happens:
1677 <title>A Title Describing Contents</title> should be on every page. Pages can
1678 have <h1>Very Major Headlines</h1> through <h6>Very Minor Headlines</h6>
1679 <hr> Parts can be separated with horizontal rules.
1681 <p>Paragraphs only need an opening tag. Line breaks and multiple spaces are
1682 ignored unless the text is <pre>preformatted.</pre> Text can be marked as
1683 <b>bold</b>, <i>italic</i> or <u>underlined</u> using the normal M-g or
1684 Edit/Text Properties/Face commands.
1686 Pages can have <a name=\"SOMENAME\">named points</a> and can link other points
1687 to them with <a href=\"#SOMENAME\">see also somename</a>. In the same way <a
1688 href=\"URL\">see also URL</a> where URL is a filename relative to current
1689 directory, or absolute as in `http://www.cs.indiana.edu/elisp/w3/docs.html'.
1691 Images in many formats can be inlined with <img src=\"URL\">.
1693 If you mainly create your own documents, `sgml-specials' might be
1694 interesting. But note that some HTML 2 browsers can't handle `''.
1695 To work around that, do:
1696 (eval-after-load \"sgml-mode\" '(aset sgml-char-names ?' nil))
1699 (set (make-local-variable 'sgml-display-text) html-display-text)
1700 (set (make-local-variable 'sgml-tag-face-alist) html-tag-face-alist)
1701 (make-local-variable 'sgml-tag-alist)
1702 (make-local-variable 'sgml-face-tag-alist)
1703 (make-local-variable 'sgml-tag-help)
1704 (make-local-variable 'outline-regexp)
1705 (make-local-variable 'outline-heading-end-regexp)
1706 (make-local-variable 'outline-level)
1707 (make-local-variable 'sentence-end)
1709 (if sentence-end-double-space
1710 "[.?!][]\"')}]*\\(<[^>]*>\\)*\\($\\| $\\|\t\\| \\)[ \t\n]*"
1711 "[.?!][]\"')}]*\\(<[^>]*>\\)*\\($\\|[ \t]\\)[ \t\n]*"))
1712 (setq sgml-tag-alist html-tag-alist
1713 sgml-face-tag-alist html-face-tag-alist
1714 sgml-tag-help html-tag-help
1715 outline-regexp "^.*<[Hh][1-6]\\>"
1716 outline-heading-end-regexp "</[Hh][1-6]>"
1717 outline-level (lambda ()
1718 (char-before (match-end 0))))
1719 (setq imenu-create-index-function 'html-imenu-index)
1720 (when sgml-xml-mode (setq mode-name "XHTML"))
1721 (set (make-local-variable 'sgml-empty-tags)
1722 ;; From HTML-4.01's loose.dtd, parsed with `sgml-parse-dtd',
1723 ;; plus manual addition of "wbr".
1724 '("area" "base" "basefont" "br" "col" "frame" "hr" "img" "input"
1725 "isindex" "link" "meta" "param" "wbr"))
1726 (set (make-local-variable 'sgml-unclosed-tags)
1727 ;; From HTML-4.01's loose.dtd, parsed with `sgml-parse-dtd'.
1728 '("body" "colgroup" "dd" "dt" "head" "html" "li" "option"
1729 "p" "tbody" "td" "tfoot" "th" "thead" "tr"))
1730 ;; It's for the user to decide if it defeats it or not -stef
1731 ;; (make-local-variable 'imenu-sort-function)
1732 ;; (setq imenu-sort-function nil) ; sorting the menu defeats the purpose
1735 (defvar html-imenu-regexp
1736 "\\s-*<h\\([1-9]\\)[^\n<>]*>\\(<[^\n<>]*>\\)*\\s-*\\([^\n<>]*\\)"
1737 "*A regular expression matching a head line to be added to the menu.
1738 The first `match-string' should be a number from 1-9.
1739 The second `match-string' matches extra tags and is ignored.
1740 The third `match-string' will be the used in the menu.")
1742 (defun html-imenu-index ()
1743 "Return a table of contents for an HTML buffer for use with Imenu."
1746 (goto-char (point-min))
1747 (while (re-search-forward html-imenu-regexp nil t)
1749 (cons (cons (concat (make-string
1750 (* 2 (1- (string-to-number (match-string 1))))
1753 (line-beginning-position))
1755 (nreverse toc-index)))
1757 (defun html-autoview-mode (&optional arg)
1758 "Toggle automatic viewing via `browse-url-of-buffer' upon saving buffer.
1759 With positive prefix ARG always turns viewing on, with negative ARG always off.
1760 Can be used as a value for `html-mode-hook'."
1762 (if (setq arg (if arg
1763 (< (prefix-numeric-value arg) 0)
1764 (and (boundp 'after-save-hook)
1765 (memq 'browse-url-of-buffer after-save-hook))))
1766 (setq after-save-hook (delq 'browse-url-of-buffer after-save-hook))
1767 (add-hook 'after-save-hook 'browse-url-of-buffer nil t))
1768 (message "Autoviewing turned %s."
1769 (if arg "off" "on")))
1772 (define-skeleton html-href-anchor
1773 "HTML anchor tag with href attribute."
1775 '(setq input "http:")
1776 "<a href=\"" str "\">" _ "</a>")
1778 (define-skeleton html-name-anchor
1779 "HTML anchor tag with name attribute."
1781 "<a name=\"" str "\">" _ "</a>")
1783 (define-skeleton html-headline-1
1784 "HTML level 1 headline tags."
1788 (define-skeleton html-headline-2
1789 "HTML level 2 headline tags."
1793 (define-skeleton html-headline-3
1794 "HTML level 3 headline tags."
1798 (define-skeleton html-headline-4
1799 "HTML level 4 headline tags."
1803 (define-skeleton html-headline-5
1804 "HTML level 5 headline tags."
1808 (define-skeleton html-headline-6
1809 "HTML level 6 headline tags."
1813 (define-skeleton html-horizontal-rule
1814 "HTML horizontal rule tag."
1816 (if sgml-xml-mode "<hr/>" "<hr>") \n)
1818 (define-skeleton html-image
1821 "<img src=\"" _ "\""
1822 (if sgml-xml-mode "/>" ">"))
1824 (define-skeleton html-line
1825 "HTML line break tag."
1827 (if sgml-xml-mode "<br/>" "<br>") \n)
1829 (define-skeleton html-ordered-list
1830 "HTML ordered list tags."
1833 "<li>" _ (if sgml-xml-mode "</li>") \n
1836 (define-skeleton html-unordered-list
1837 "HTML unordered list tags."
1840 "<li>" _ (if sgml-xml-mode "</li>") \n
1843 (define-skeleton html-list-item
1844 "HTML list item tag."
1847 "<li>" _ (if sgml-xml-mode "</li>"))
1849 (define-skeleton html-paragraph
1850 "HTML paragraph tag."
1853 \n "<p>" _ (if sgml-xml-mode "</p>"))
1855 (define-skeleton html-checkboxes
1856 "Group of connected checkbox inputs."
1861 "<input type=\"" (identity "checkbox") ; see comment above about identity
1862 "\" name=\"" (or v1 (setq v1 (skeleton-read "Name: ")))
1863 "\" value=\"" str ?\"
1864 (when (y-or-n-p "Set \"checked\" attribute? ")
1865 (funcall skeleton-transformation " checked"))
1866 (if sgml-xml-mode "/>" ">")
1867 (skeleton-read "Text: " (capitalize str))
1868 (or v2 (setq v2 (if (y-or-n-p "Newline after text? ")
1869 (funcall skeleton-transformation
1870 (if sgml-xml-mode "<br/>" "<br>"))
1874 (define-skeleton html-radio-buttons
1875 "Group of connected radio button inputs."
1880 "<input type=\"" (identity "radio") ; see comment above about identity
1881 "\" name=\"" (or (car v2) (setcar v2 (skeleton-read "Name: ")))
1882 "\" value=\"" str ?\"
1883 (when (and (not v1) (setq v1 (y-or-n-p "Set \"checked\" attribute? ")))
1884 (funcall skeleton-transformation " checked"))
1885 (if sgml-xml-mode "/>" ">")
1886 (skeleton-read "Text: " (capitalize str))
1887 (or (cdr v2) (setcdr v2 (if (y-or-n-p "Newline after text? ")
1888 (funcall skeleton-transformation
1889 (if sgml-xml-mode "<br/>" "<br>"))
1893 (provide 'sgml-mode)
1895 ;;; sgml-mode.el ends here