(sgml-specials, sgml-quick-keys): Doc fixes.
[bpt/emacs.git] / lisp / textmodes / sgml-mode.el
1 ;;; sgml-mode.el --- SGML- and HTML-editing modes
2
3 ;; Copyright (C) 1992,95,96,98,2001 Free Software Foundation, Inc.
4
5 ;; Author: James Clark <jjc@jclark.com>
6 ;; Adapted-By: ESR, Daniel Pfeiffer <occitan@esperanto.org>,
7 ;; F.Potorti@cnuce.cnr.it
8 ;; Keywords: wp, hypermedia, comm, languages
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;; Configurable major mode for editing document in the SGML standard general
30 ;; markup language. As an example contains a mode for editing the derived
31 ;; HTML hypertext markup language.
32
33 ;;; Code:
34
35 (eval-when-compile
36 (require 'skeleton)
37 (require 'outline))
38
39 (defgroup sgml nil
40 "SGML editing mode"
41 :group 'languages)
42
43 (defcustom sgml-transformation 'identity
44 "*Default value for `skeleton-transformation' (which see) in SGML mode."
45 :type 'function
46 :group 'sgml)
47
48 (put 'sgml-transformation 'variable-interactive
49 "aTransformation function: ")
50
51 (defcustom sgml-mode-hook nil
52 "Hook run by command `sgml-mode'.
53 `text-mode-hook' is run first."
54 :group 'sgml
55 :type 'hook)
56
57 ;; As long as Emacs' syntax can't be complemented with predicates to context
58 ;; sensitively confirm the syntax of characters, we have to live with this
59 ;; kludgy kind of tradeoff.
60 (defvar sgml-specials '(?\")
61 "List of characters that have a special meaning for SGML mode.
62 This list is used when first loading the `sgml-mode' library.
63 The supported characters and potential disadvantages are:
64
65 ?\\\" Makes \" in text start a string.
66 ?' Makes ' in text start a string.
67 ?- Makes -- in text start a comment.
68
69 When only one of ?\\\" or ?' are included, \"'\" or '\"', as can be found in
70 DTDs, start a string. To partially avoid this problem this also makes these
71 self insert as named entities depending on `sgml-quick-keys'.
72
73 Including ?- has the problem of affecting dashes that have nothing to do
74 with comments, so we normally turn it off.")
75
76 (defvar sgml-quick-keys nil
77 "Use <, >, &, SPC and `sgml-specials' keys \"electrically\" when non-nil.
78 This takes effect when first loading the `sgml-mode' library.")
79
80
81 (defvar sgml-mode-map
82 (let ((map (make-keymap)) ;`sparse' doesn't allow binding to charsets.
83 (menu-map (make-sparse-keymap "SGML")))
84 (define-key map "\C-c\C-i" 'sgml-tags-invisible)
85 (define-key map "/" 'sgml-slash)
86 (define-key map "\C-c\C-n" 'sgml-name-char)
87 (define-key map "\C-c\C-t" 'sgml-tag)
88 (define-key map "\C-c\C-a" 'sgml-attributes)
89 (define-key map "\C-c\C-b" 'sgml-skip-tag-backward)
90 (define-key map [?\C-c left] 'sgml-skip-tag-backward)
91 (define-key map "\C-c\C-f" 'sgml-skip-tag-forward)
92 (define-key map [?\C-c right] 'sgml-skip-tag-forward)
93 (define-key map "\C-c\C-d" 'sgml-delete-tag)
94 (define-key map "\C-c\^?" 'sgml-delete-tag)
95 (define-key map "\C-c?" 'sgml-tag-help)
96 (define-key map "\C-c8" 'sgml-name-8bit-mode)
97 (define-key map "\C-c\C-v" 'sgml-validate)
98 (when sgml-quick-keys
99 (define-key map "&" 'sgml-name-char)
100 (define-key map "<" 'sgml-tag)
101 (define-key map " " 'sgml-auto-attributes)
102 (define-key map ">" 'sgml-maybe-end-tag)
103 (when (memq ?\" sgml-specials)
104 (define-key map "\"" 'sgml-name-self))
105 (when (memq ?' sgml-specials)
106 (define-key map "'" 'sgml-name-self)))
107 (define-key map (vector (make-char 'latin-iso8859-1))
108 'sgml-maybe-name-self)
109 (let ((c 127)
110 (map (nth 1 map)))
111 (while (< (setq c (1+ c)) 256)
112 (aset map c 'sgml-maybe-name-self)))
113 (define-key map [menu-bar sgml] (cons "SGML" menu-map))
114 (define-key menu-map [sgml-validate] '("Validate" . sgml-validate))
115 (define-key menu-map [sgml-name-8bit-mode]
116 '("Toggle 8 Bit Insertion" . sgml-name-8bit-mode))
117 (define-key menu-map [sgml-tags-invisible]
118 '("Toggle Tag Visibility" . sgml-tags-invisible))
119 (define-key menu-map [sgml-tag-help]
120 '("Describe Tag" . sgml-tag-help))
121 (define-key menu-map [sgml-delete-tag]
122 '("Delete Tag" . sgml-delete-tag))
123 (define-key menu-map [sgml-skip-tag-forward]
124 '("Forward Tag" . sgml-skip-tag-forward))
125 (define-key menu-map [sgml-skip-tag-backward]
126 '("Backward Tag" . sgml-skip-tag-backward))
127 (define-key menu-map [sgml-attributes]
128 '("Insert Attributes" . sgml-attributes))
129 (define-key menu-map [sgml-tag] '("Insert Tag" . sgml-tag))
130 map)
131 "Keymap for SGML mode. See also `sgml-specials'.")
132
133
134 (defvar sgml-mode-syntax-table
135 (let ((table (copy-syntax-table text-mode-syntax-table)))
136 (modify-syntax-entry ?< "(>" table)
137 (modify-syntax-entry ?> ")<" table)
138 (if (memq ?- sgml-specials)
139 (modify-syntax-entry ?- "_ 1234" table))
140 (if (memq ?\" sgml-specials)
141 (modify-syntax-entry ?\" "\"\"" table))
142 (if (memq ?' sgml-specials)
143 (modify-syntax-entry ?\' "\"'" table))
144 table)
145 "Syntax table used in SGML mode. See also `sgml-specials'.")
146
147
148 (defcustom sgml-name-8bit-mode nil
149 "*When non-nil, insert non-ASCII characters as named entities."
150 :type 'boolean
151 :group 'sgml)
152
153 (defvar sgml-char-names
154 [nil nil nil nil nil nil nil nil
155 nil nil nil nil nil nil nil nil
156 nil nil nil nil nil nil nil nil
157 nil nil nil nil nil nil nil nil
158 "nbsp" "excl" "quot" "num" "dollar" "percnt" "amp" "apos"
159 "lpar" "rpar" "ast" "plus" "comma" "hyphen" "period" "sol"
160 nil nil nil nil nil nil nil nil
161 nil nil "colon" "semi" "lt" "eq" "gt" "quest"
162 "commat" nil nil nil nil nil nil nil
163 nil nil nil nil nil nil nil nil
164 nil nil nil nil nil nil nil nil
165 nil nil nil "lsqb" nil "rsqb" "uarr" "lowbar"
166 "lsquo" nil nil nil nil nil nil nil
167 nil nil nil nil nil nil nil nil
168 nil nil nil nil nil nil nil nil
169 nil nil nil "lcub" "verbar" "rcub" "tilde" nil
170 nil nil nil nil nil nil nil nil
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 "nbsp" "iexcl" "cent" "pound" "curren" "yen" "brvbar" "sect"
175 "uml" "copy" "ordf" "laquo" "not" "shy" "reg" "macr"
176 "ring" "plusmn" "sup2" "sup3" "acute" "micro" "para" "middot"
177 "cedil" "sup1" "ordm" "raquo" "frac14" "frac12" "frac34" "iquest"
178 "Agrave" "Aacute" "Acirc" "Atilde" "Auml" "Aring" "AElig" "Ccedil"
179 "Egrave" "Eacute" "Ecirc" "Euml" "Igrave" "Iacute" "Icirc" "Iuml"
180 "ETH" "Ntilde" "Ograve" "Oacute" "Ocirc" "Otilde" "Ouml" nil
181 "Oslash" "Ugrave" "Uacute" "Ucirc" "Uuml" "Yacute" "THORN" "szlig"
182 "agrave" "aacute" "acirc" "atilde" "auml" "aring" "aelig" "ccedil"
183 "egrave" "eacute" "ecirc" "euml" "igrave" "iacute" "icirc" "iuml"
184 "eth" "ntilde" "ograve" "oacute" "ocirc" "otilde" "ouml" "divide"
185 "oslash" "ugrave" "uacute" "ucirc" "uuml" "yacute" "thorn" "yuml"]
186 "Vector of symbolic character names without `&' and `;'.")
187
188 (put 'sgml-table 'char-table-extra-slots 0)
189
190 (defvar sgml-char-names-table
191 (let ((table (make-char-table 'sgml-table))
192 (i 32)
193 elt)
194 (while (< i 256)
195 (setq elt (aref sgml-char-names i))
196 (if elt (aset table (make-char 'latin-iso8859-1 i) elt))
197 (setq i (1+ i)))
198 table)
199 "A table for mapping non-ASCII characters into SGML entity names.
200 Currently, only Latin-1 characters are supported.")
201
202
203 ;; nsgmls is a free SGML parser in the SP suite available from
204 ;; ftp.jclark.com and otherwise packaged for GNU systems.
205 ;; Its error messages can be parsed by next-error.
206 ;; The -s option suppresses output.
207
208 (defcustom sgml-validate-command "nsgmls -s" ; replaced old `sgmls'
209 "*The command to validate an SGML document.
210 The file name of current buffer file name will be appended to this,
211 separated by a space."
212 :type 'string
213 :version "21.1"
214 :group 'sgml)
215
216 (defvar sgml-saved-validate-command nil
217 "The command last used to validate in this buffer.")
218
219
220 ;; I doubt that null end tags are used much for large elements,
221 ;; so use a small distance here.
222 (defcustom sgml-slash-distance 1000
223 "*If non-nil, is the maximum distance to search for matching `/'."
224 :type '(choice (const nil) integer)
225 :group 'sgml)
226
227 (defconst sgml-start-tag-regex
228 "<[A-Za-z]\\([-.A-Za-z0-9= \n\t]\\|\"[^\"]*\"\\|'[^']*'\\)*"
229 "Regular expression that matches a non-empty start tag.
230 Any terminating `>' or `/' is not matched.")
231
232
233 ;; internal
234 (defconst sgml-font-lock-keywords-1
235 '(("<\\([!?][a-z][-.a-z0-9]*\\)" 1 font-lock-keyword-face)
236 ("<\\(/?[a-z][-.a-z0-9]*\\)" 1 font-lock-function-name-face)
237 ("[&%][a-z][-.a-z0-9]*;?" . font-lock-variable-name-face)))
238
239 (defconst sgml-font-lock-keywords-2
240 (append
241 sgml-font-lock-keywords-1
242 '((eval
243 . (cons (concat "<"
244 (regexp-opt (mapcar 'car sgml-tag-face-alist) t)
245 "\\([ \t][^>]*\\)?>\\([^<]+\\)</\\1>")
246 '(3 (cdr (assoc (downcase (match-string 1))
247 sgml-tag-face-alist))))))))
248
249 ;; for font-lock, but must be defvar'ed after
250 ;; sgml-font-lock-keywords-1 and sgml-font-lock-keywords-2 above
251 (defvar sgml-font-lock-keywords sgml-font-lock-keywords-1
252 "*Rules for highlighting SGML code. See also `sgml-tag-face-alist'.")
253
254 (defvar sgml-font-lock-syntactic-keywords
255 ;; Use the `b' style of comments to avoid interference with the -- ... --
256 ;; comments recognized when `sgml-specials' includes ?-.
257 ;; FIXME: beware of <!--> blabla <!--> !!
258 '(("\\(<\\)!--" (1 "< b"))
259 ("--[ \t\n]*\\(>\\)" (1 "> b")))
260 "Syntactic keywords for `sgml-mode'.")
261
262 ;; internal
263 (defvar sgml-face-tag-alist ()
264 "Alist of face and tag name for facemenu.")
265
266 (defvar sgml-tag-face-alist ()
267 "Tag names and face or list of faces to fontify with when invisible.
268 When `font-lock-maximum-decoration' is 1 this is always used for fontifying.
269 When more these are fontified together with `sgml-font-lock-keywords'.")
270
271
272 (defvar sgml-display-text ()
273 "Tag names as lowercase symbols, and display string when invisible.")
274
275 ;; internal
276 (defvar sgml-tags-invisible nil)
277
278
279 (defcustom sgml-tag-alist
280 '(("![" ("ignore" t) ("include" t))
281 ("!attlist")
282 ("!doctype")
283 ("!element")
284 ("!entity"))
285 "*Alist of tag names for completing read and insertion rules.
286 This alist is made up as
287
288 ((\"tag\" . TAGRULE)
289 ...)
290
291 TAGRULE is a list of optionally `t' (no endtag) or `\\n' (separate endtag by
292 newlines) or a skeleton with `nil', `t' or `\\n' in place of the interactor
293 followed by an ATTRIBUTERULE (for an always present attribute) or an
294 attribute alist.
295
296 The attribute alist is made up as
297
298 ((\"attribute\" . ATTRIBUTERULE)
299 ...)
300
301 ATTRIBUTERULE is a list of optionally `t' (no value when no input) followed by
302 an optional alist of possible values."
303 :type '(repeat (cons (string :tag "Tag Name")
304 (repeat :tag "Tag Rule" sexp)))
305 :group 'sgml)
306
307 (defcustom sgml-tag-help
308 '(("!" . "Empty declaration for comment")
309 ("![" . "Embed declarations with parser directive")
310 ("!attlist" . "Tag attributes declaration")
311 ("!doctype" . "Document type (DTD) declaration")
312 ("!element" . "Tag declaration")
313 ("!entity" . "Entity (macro) declaration"))
314 "*Alist of tag name and short description."
315 :type '(repeat (cons (string :tag "Tag Name")
316 (string :tag "Description")))
317 :group 'sgml)
318
319 (defcustom sgml-xml nil
320 "*When non-nil, tag insertion functions will be XML-compliant.
321 If this variable is customized, the custom value is used always.
322 Otherwise, it is set to be buffer-local when the file has
323 a DOCTYPE or an XML declaration."
324 :type 'boolean
325 :version "21.2"
326 :group 'sgml)
327
328 (defvar sgml-empty-tags nil
329 "List of tags whose !ELEMENT definition says EMPTY.")
330
331 (defun sgml-xml-guess ()
332 "Guess whether the current buffer is XML."
333 (save-excursion
334 (goto-char (point-min))
335 (cond ((or (string= "xml" (file-name-extension (or buffer-file-name "")))
336 (looking-at "\\s-*<\\?xml"))
337 (set (make-local-variable 'sgml-xml) t))
338 ((re-search-forward
339 (eval-when-compile
340 (mapconcat 'identity
341 '("<!DOCTYPE" "\\(\\w+\\)" "\\(\\w+\\)"
342 "\"\\([^\"]+\\)\"" "\"\\([^\"]+\\)\"")
343 "\\s-+"))
344 nil t)
345 (let ((name (match-string 1))
346 (pub (match-string 2))
347 (id (match-string 3))
348 (url (match-string 4)))
349 (cond ((string= name "html")
350 (set (make-local-variable 'sgml-xml)
351 (not (null (string-match "XHTML" id)))))
352 ((string-match "XML" id)
353 (set (make-local-variable 'sgml-xml) t))))))))
354
355 (defvar v2) ; free for skeleton
356
357 (defun sgml-mode-common ()
358 "Common code for setting up `sgml-mode' and derived modes."
359 (make-local-variable 'sgml-saved-validate-command)
360 (make-local-variable 'facemenu-end-add-face)
361 ;;(make-local-variable 'facemenu-remove-face-function)
362 (set (make-local-variable 'indent-line-function) 'indent-relative-maybe)
363 ;; A start or end tag by itself on a line separates a paragraph.
364 ;; This is desirable because SGML discards a newline that appears
365 ;; immediately after a start tag or immediately before an end tag.
366 (set (make-local-variable 'paragraph-separate) "[ \t]*$\\|\
367 \[ \t]*</?\\([A-Za-z]\\([-.A-Za-z0-9= \t\n]\\|\"[^\"]*\"\\|'[^']*'\\)*\\)?>$")
368 (set (make-local-variable 'paragraph-start) "[ \t]*$\\|\
369 \[ \t]*</?\\([A-Za-z]\\([-.A-Za-z0-9= \t\n]\\|\"[^\"]*\"\\|'[^']*'\\)*\\)?>")
370 (set (make-local-variable 'adaptive-fill-regexp) "[ \t]*")
371 (set (make-local-variable 'comment-start) "<!-- ")
372 (set (make-local-variable 'comment-end) " -->")
373 (set (make-local-variable 'comment-indent-function) 'sgml-comment-indent)
374 (set (make-local-variable 'skeleton-transformation) sgml-transformation)
375 (set (make-local-variable 'skeleton-further-elements)
376 '((completion-ignore-case t)))
377 (set (make-local-variable 'skeleton-end-hook)
378 (lambda ()
379 (or (eolp)
380 (not (or (eq v2 '\n) (eq (car-safe v2) '\n)))
381 (newline-and-indent))))
382 (set (make-local-variable 'font-lock-defaults)
383 '((sgml-font-lock-keywords
384 sgml-font-lock-keywords-1
385 sgml-font-lock-keywords-2)
386 nil t nil nil
387 (font-lock-syntactic-keywords
388 . sgml-font-lock-syntactic-keywords)))
389 (set (make-local-variable 'facemenu-add-face-function)
390 'sgml-mode-facemenu-add-face-function)
391 ;; `sgml-xml' not customized -- guess
392 (unless (get 'sgml-xml 'saved-value) (sgml-xml-guess))
393 ;; This will allow existing comments within declarations to be
394 ;; recognized.
395 (set (make-local-variable 'comment-start-skip) "\\(?:<!\\)?--[ \t]*")
396 (set (make-local-variable 'comment-end-skip) "[ \t]*--\\([ \t\n]*>\\)?"))
397
398
399 (defun sgml-mode-facemenu-add-face-function (face end)
400 (if (setq face (cdr (assq face sgml-face-tag-alist)))
401 (progn
402 (setq face (funcall skeleton-transformation face))
403 (setq facemenu-end-add-face (concat "</" face ">"))
404 (concat "<" face ">"))
405 (error "Face not configured for %s mode" mode-name)))
406
407
408 ;;;###autoload
409 (define-derived-mode sgml-mode text-mode "SGML"
410 "Major mode for editing SGML documents.
411 Makes > match <. Makes / blink matching /.
412 Keys <, &, SPC within <>, \" and ' can be electric depending on
413 `sgml-quick-keys'.
414
415 An argument of N to a tag-inserting command means to wrap it around
416 the next N words. In Transient Mark mode, when the mark is active,
417 N defaults to -1, which means to wrap it around the current region.
418
419 If you like upcased tags, put (setq sgml-transformation 'upcase) in
420 your `.emacs' file.
421
422 Use \\[sgml-validate] to validate your document with an SGML parser.
423
424 Do \\[describe-variable] sgml- SPC to see available variables.
425 Do \\[describe-key] on the following bindings to discover what they do.
426 \\{sgml-mode-map}"
427 (sgml-mode-common)
428 (when sgml-xml (setq mode-name "XML"))
429 ;; Set `imenu-generic-expression' here, rather than in `sgml-mode-common',
430 ;; because this definition probably is not useful in HTML mode.
431 (set (make-local-variable 'imenu-generic-expression)
432 "<!\\(element\\|entity\\)[ \t\n]+%?[ \t\n]*\\([A-Za-z][-A-Za-z.0-9]*\\)"))
433
434
435 (defun sgml-comment-indent ()
436 (if (looking-at "--") comment-column 0))
437
438
439
440 (defun sgml-slash (arg)
441 "Insert `/' and display any previous matching `/'.
442 Two `/'s are treated as matching if the first `/' ends a net-enabling
443 start tag, and the second `/' is the corresponding null end tag."
444 (interactive "p")
445 (insert-char ?/ arg)
446 (if (> arg 0)
447 (let ((oldpos (point))
448 (blinkpos)
449 (level 0))
450 (save-excursion
451 (save-restriction
452 (if sgml-slash-distance
453 (narrow-to-region (max (point-min)
454 (- (point) sgml-slash-distance))
455 oldpos))
456 (if (and (re-search-backward sgml-start-tag-regex (point-min) t)
457 (eq (match-end 0) (1- oldpos)))
458 ()
459 (goto-char (1- oldpos))
460 (while (and (not blinkpos)
461 (search-backward "/" (point-min) t))
462 (let ((tagend (save-excursion
463 (if (re-search-backward sgml-start-tag-regex
464 (point-min) t)
465 (match-end 0)
466 nil))))
467 (if (eq tagend (point))
468 (if (eq level 0)
469 (setq blinkpos (point))
470 (setq level (1- level)))
471 (setq level (1+ level)))))))
472 (when blinkpos
473 (goto-char blinkpos)
474 (if (pos-visible-in-window-p)
475 (sit-for 1)
476 (message "Matches %s"
477 (buffer-substring (line-beginning-position)
478 (1+ blinkpos)))))))))
479
480
481 (defun sgml-name-char (&optional char)
482 "Insert a symbolic character name according to `sgml-char-names'.
483 Non-ASCII chars may be inserted either with the meta key, as in M-SPC for
484 no-break space or M-- for a soft hyphen; or via an input method or
485 encoded keyboard operation."
486 (interactive "*")
487 (insert ?&)
488 (or char
489 (setq char (read-quoted-char "Enter char or octal number")))
490 (delete-backward-char 1)
491 (insert char)
492 (undo-boundary)
493 (delete-backward-char 1)
494 (cond
495 ((< char 256)
496 (insert ?&
497 (or (aref sgml-char-names char)
498 (format "#%d" char))
499 ?\;))
500 ((aref sgml-char-names-table char)
501 (insert ?& (aref sgml-char-names-table char) ?\;))
502 ((memq (char-charset char) '(mule-unicode-0100-24ff
503 mule-unicode-2500-33ff
504 mule-unicode-e000-ffff))
505 (insert (format "&#%d;" (encode-char char 'ucs))))
506 (t
507 (insert char))))
508
509 (defun sgml-name-self ()
510 "Insert a symbolic character name according to `sgml-char-names'."
511 (interactive "*")
512 (sgml-name-char last-command-char))
513
514 (defun sgml-maybe-name-self ()
515 "Insert a symbolic character name according to `sgml-char-names'."
516 (interactive "*")
517 (if sgml-name-8bit-mode
518 (let ((mc last-command-char))
519 (if (< mc 256)
520 (setq mc (unibyte-char-to-multibyte mc)))
521 (or mc (setq mc last-command-char))
522 (sgml-name-char mc))
523 (self-insert-command 1)))
524
525 (defun sgml-name-8bit-mode ()
526 "Toggle whether to insert named entities instead of non-ASCII characters."
527 (interactive)
528 (setq sgml-name-8bit-mode (not sgml-name-8bit-mode))
529 (message "sgml name entity mode is now %s"
530 (if sgml-name-8bit-mode "ON" "OFF")))
531
532 ;; When an element of a skeleton is a string "str", it is passed
533 ;; through skeleton-transformation and inserted. If "str" is to be
534 ;; inserted literally, one should obtain it as the return value of a
535 ;; function, e.g. (identity "str").
536
537 (define-skeleton sgml-tag
538 "Prompt for a tag and insert it, optionally with attributes.
539 Completion and configuration are done according to `sgml-tag-alist'.
540 If you like tags and attributes in uppercase do \\[set-variable]
541 skeleton-transformation RET upcase RET, or put this in your `.emacs':
542 (setq sgml-transformation 'upcase)"
543 (funcall skeleton-transformation
544 (completing-read "Tag: " sgml-tag-alist))
545 ?< str |
546 (("") -1 '(undo-boundary) (identity "&lt;")) | ; see comment above
547 `(("") '(setq v2 (sgml-attributes ,str t)) ?>
548 (cond
549 ((string= "![" ,str)
550 (backward-char)
551 '(("") " [ " _ " ]]"))
552 ((and (eq v2 t) sgml-xml (member ,str sgml-empty-tags))
553 '(("") -1 "/>"))
554 ((or (and (eq v2 t) (not sgml-xml)) (string-match "^[/!?]" ,str))
555 nil)
556 ((symbolp v2)
557 ;; Make sure we don't fall into an infinite loop.
558 ;; For xhtml's `tr' tag, we should maybe use \n instead.
559 (if (eq v2 t) (setq v2 nil))
560 ;; We use `identity' to prevent skeleton from passing
561 ;; `str' through skeleton-transformation a second time.
562 '(("") v2 _ v2 "</" (identity ',str) ?>))
563 ((eq (car v2) t)
564 (cons '("") (cdr v2)))
565 (t
566 (append '(("") (car v2))
567 (cdr v2)
568 '(resume: (car v2) _ "</" (identity ',str) ?>))))))
569
570 (autoload 'skeleton-read "skeleton")
571
572 (defun sgml-attributes (tag &optional quiet)
573 "When at top level of a tag, interactively insert attributes.
574
575 Completion and configuration of TAG are done according to `sgml-tag-alist'.
576 If QUIET, do not print a message when there are no attributes for TAG."
577 (interactive (list (save-excursion (sgml-beginning-of-tag t))))
578 (or (stringp tag) (error "Wrong context for adding attribute"))
579 (if tag
580 (let ((completion-ignore-case t)
581 (alist (cdr (assoc (downcase tag) sgml-tag-alist)))
582 car attribute i)
583 (if (or (symbolp (car alist))
584 (symbolp (car (car alist))))
585 (setq car (car alist)
586 alist (cdr alist)))
587 (or quiet
588 (message "No attributes configured."))
589 (if (stringp (car alist))
590 (progn
591 (insert (if (eq (preceding-char) ? ) "" ? )
592 (funcall skeleton-transformation (car alist)))
593 (sgml-value alist))
594 (setq i (length alist))
595 (while (> i 0)
596 (insert ? )
597 (insert (funcall skeleton-transformation
598 (setq attribute
599 (skeleton-read '(completing-read
600 "Attribute: "
601 alist)))))
602 (if (string= "" attribute)
603 (setq i 0)
604 (sgml-value (assoc (downcase attribute) alist))
605 (setq i (1- i))))
606 (if (eq (preceding-char) ? )
607 (delete-backward-char 1)))
608 car)))
609
610 (defun sgml-auto-attributes (arg)
611 "Self insert the character typed; at top level of tag, prompt for attributes.
612 With prefix argument, only self insert."
613 (interactive "*P")
614 (let ((point (point))
615 tag)
616 (if (or arg
617 (not sgml-tag-alist) ; no message when nothing configured
618 (symbolp (setq tag (save-excursion (sgml-beginning-of-tag t))))
619 (eq (aref tag 0) ?/))
620 (self-insert-command (prefix-numeric-value arg))
621 (sgml-attributes tag)
622 (setq last-command-char ? )
623 (or (> (point) point)
624 (self-insert-command 1)))))
625
626
627 (defun sgml-tag-help (&optional tag)
628 "Display description of tag TAG. If TAG is omitted, use the tag at point."
629 (interactive)
630 (or tag
631 (save-excursion
632 (if (eq (following-char) ?<)
633 (forward-char))
634 (setq tag (sgml-beginning-of-tag))))
635 (or (stringp tag)
636 (error "No tag selected"))
637 (setq tag (downcase tag))
638 (message "%s"
639 (or (cdr (assoc (downcase tag) sgml-tag-help))
640 (and (eq (aref tag 0) ?/)
641 (cdr (assoc (downcase (substring tag 1)) sgml-tag-help)))
642 "No description available")))
643
644
645 (defun sgml-maybe-end-tag ()
646 "Name self unless in position to end a tag."
647 (interactive)
648 (or (condition-case nil
649 (save-excursion (up-list -1))
650 (error
651 (sgml-name-self)
652 t))
653 (condition-case nil
654 (progn
655 (save-excursion (up-list 1))
656 (sgml-name-self))
657 (error (self-insert-command 1)))))
658
659
660 (defun sgml-skip-tag-backward (arg)
661 "Skip to beginning of tag or matching opening tag if present.
662 With prefix argument ARG, repeat this ARG times."
663 (interactive "p")
664 (while (>= arg 1)
665 (search-backward "<" nil t)
666 (if (looking-at "</\\([^ \n\t>]+\\)")
667 ;; end tag, skip any nested pairs
668 (let ((case-fold-search t)
669 (re (concat "</?" (regexp-quote (match-string 1)))))
670 (while (and (re-search-backward re nil t)
671 (eq (char-after (1+ (point))) ?/))
672 (forward-char 1)
673 (sgml-skip-tag-backward 1))))
674 (setq arg (1- arg))))
675
676 (defun sgml-skip-tag-forward (arg &optional return)
677 "Skip to end of tag or matching closing tag if present.
678 With prefix argument ARG, repeat this ARG times.
679 Return t iff after a closing tag."
680 (interactive "p")
681 (setq return t)
682 (while (>= arg 1)
683 (skip-chars-forward "^<>")
684 (if (eq (following-char) ?>)
685 (up-list -1))
686 (if (looking-at "<\\([^/ \n\t>]+\\)")
687 ;; start tag, skip any nested same pairs _and_ closing tag
688 (let ((case-fold-search t)
689 (re (concat "</?" (regexp-quote (match-string 1))))
690 point close)
691 (forward-list 1)
692 (setq point (point))
693 (while (and (re-search-forward re nil t)
694 (not (setq close
695 (eq (char-after (1+ (match-beginning 0))) ?/)))
696 (not (up-list -1))
697 (sgml-skip-tag-forward 1))
698 (setq close nil))
699 (if close
700 (up-list 1)
701 (goto-char point)
702 (setq return)))
703 (forward-list 1))
704 (setq arg (1- arg)))
705 return)
706
707 (defun sgml-delete-tag (arg)
708 "Delete tag on or after cursor, and matching closing or opening tag.
709 With prefix argument ARG, repeat this ARG times."
710 (interactive "p")
711 (while (>= arg 1)
712 (save-excursion
713 (let* (close open)
714 (if (looking-at "[ \t\n]*<")
715 ;; just before tag
716 (if (eq (char-after (match-end 0)) ?/)
717 ;; closing tag
718 (progn
719 (setq close (point))
720 (goto-char (match-end 0))))
721 ;; on tag?
722 (or (save-excursion (setq close (sgml-beginning-of-tag)
723 close (and (stringp close)
724 (eq (aref close 0) ?/)
725 (point))))
726 ;; not on closing tag
727 (let ((point (point)))
728 (sgml-skip-tag-backward 1)
729 (if (or (not (eq (following-char) ?<))
730 (save-excursion
731 (forward-list 1)
732 (<= (point) point)))
733 (error "Not on or before tag")))))
734 (if close
735 (progn
736 (sgml-skip-tag-backward 1)
737 (setq open (point))
738 (goto-char close)
739 (kill-sexp 1))
740 (setq open (point))
741 (sgml-skip-tag-forward 1)
742 (backward-list)
743 (forward-char)
744 (if (eq (aref (sgml-beginning-of-tag) 0) ?/)
745 (kill-sexp 1)))
746 (goto-char open)
747 (kill-sexp 1)))
748 (setq arg (1- arg))))
749 \f
750 ;; Put read-only last to enable setting this even when read-only enabled.
751 (or (get 'sgml-tag 'invisible)
752 (setplist 'sgml-tag
753 (append '(invisible t
754 intangible t
755 point-entered sgml-point-entered
756 rear-nonsticky t
757 read-only t)
758 (symbol-plist 'sgml-tag))))
759
760 (defun sgml-tags-invisible (arg)
761 "Toggle visibility of existing tags."
762 (interactive "P")
763 (let ((modified (buffer-modified-p))
764 (inhibit-read-only t)
765 (inhibit-modification-hooks t)
766 ;; Avoid spurious the `file-locked' checks.
767 (buffer-file-name nil)
768 ;; This is needed in case font lock gets called,
769 ;; since it moves point and might call sgml-point-entered.
770 ;; How could it get called? -stef
771 (inhibit-point-motion-hooks t)
772 string)
773 (unwind-protect
774 (save-excursion
775 (goto-char (point-min))
776 (if (set (make-local-variable 'sgml-tags-invisible)
777 (if arg
778 (>= (prefix-numeric-value arg) 0)
779 (not sgml-tags-invisible)))
780 (while (re-search-forward "<\\([!/?A-Za-z][-A-Za-z0-9]*\\)"
781 nil t)
782 (setq string
783 (cdr (assq (intern-soft (downcase (match-string 1)))
784 sgml-display-text)))
785 (goto-char (match-beginning 0))
786 (and (stringp string)
787 (not (overlays-at (point)))
788 (let ((ol (make-overlay (point) (match-beginning 1))))
789 (overlay-put ol 'before-string string)
790 (overlay-put ol 'sgml-tag t)))
791 (put-text-property (point)
792 (progn (forward-list) (point))
793 'category 'sgml-tag))
794 (let ((pos (point-min)))
795 (while (< (setq pos (next-overlay-change pos)) (point-max))
796 (dolist (ol (overlays-at pos))
797 (if (overlay-get ol 'sgml-tag)
798 (delete-overlay ol)))))
799 (remove-text-properties (point-min) (point-max) '(category nil))))
800 (restore-buffer-modified-p modified))
801 (run-hooks 'sgml-tags-invisible-hook)
802 (message "")))
803
804 (defun sgml-point-entered (x y)
805 ;; Show preceding or following hidden tag, depending of cursor direction.
806 (let ((inhibit-point-motion-hooks t))
807 (save-excursion
808 (message "Invisible tag: %s"
809 ;; Strip properties, otherwise, the text is invisible.
810 (buffer-substring-no-properties
811 (point)
812 (if (or (and (> x y)
813 (not (eq (following-char) ?<)))
814 (and (< x y)
815 (eq (preceding-char) ?>)))
816 (backward-list)
817 (forward-list)))))))
818 \f
819 (autoload 'compile-internal "compile")
820
821 (defun sgml-validate (command)
822 "Validate an SGML document.
823 Runs COMMAND, a shell command, in a separate process asynchronously
824 with output going to the buffer `*compilation*'.
825 You can then use the command \\[next-error] to find the next error message
826 and move to the line in the SGML document that caused it."
827 (interactive
828 (list (read-string "Validate command: "
829 (or sgml-saved-validate-command
830 (concat sgml-validate-command
831 " "
832 (let ((name (buffer-file-name)))
833 (and name
834 (file-name-nondirectory name))))))))
835 (setq sgml-saved-validate-command command)
836 (save-some-buffers (not compilation-ask-about-save) nil)
837 (compile-internal command "No more errors"))
838
839
840 (defun sgml-beginning-of-tag (&optional top-level)
841 "Skip to beginning of tag and return its name.
842 If this can't be done, return t."
843 (or (if top-level
844 (condition-case nil
845 (up-list -1)
846 (error t))
847 (>= (point)
848 (if (search-backward "<" nil t)
849 (save-excursion
850 (forward-list)
851 (point))
852 0)))
853 (if (looking-at "<[!?/]?[[A-Za-z][A-Za-z0-9]*")
854 (buffer-substring-no-properties
855 (1+ (point))
856 (match-end 0))
857 t)))
858
859 (defun sgml-value (alist)
860 "Interactively insert value taken from attributerule ALIST.
861 See `sgml-tag-alist' for info about attribute rules."
862 (setq alist (cdr alist))
863 (if (stringp (car alist))
864 (insert "=\"" (car alist) ?\")
865 (if (and (eq (car alist) t) (not sgml-xml))
866 (when (cdr alist)
867 (insert "=\"")
868 (setq alist (skeleton-read '(completing-read "Value: " (cdr alist))))
869 (if (string< "" alist)
870 (insert alist ?\")
871 (delete-backward-char 2)))
872 (insert "=\"")
873 (when alist
874 (insert (skeleton-read '(completing-read "Value: " alist))))
875 (insert ?\"))))
876
877 (defun sgml-quote (start end &optional unquotep)
878 "Quote SGML text in region.
879 With prefix argument, unquote the region."
880 (interactive "r\np")
881 (if (< start end)
882 (goto-char start)
883 (goto-char end)
884 (setq end start))
885 (if unquotep
886 (while (re-search-forward "&\\(amp\\|\\(l\\|\\(g\\)\\)t\\);" end t)
887 (replace-match (if (match-end 3) ">" (if (match-end 2) "<" "&"))))
888 (while (re-search-forward "[&<>]" end t)
889 (replace-match (cdr (assq (char-before) '((?& . "&amp;")
890 (?< . "&lt;")
891 (?> . "&gt;"))))))))
892 \f
893
894 ;;; HTML mode
895
896 (defcustom html-mode-hook nil
897 "Hook run by command `html-mode'.
898 `text-mode-hook' and `sgml-mode-hook' are run first."
899 :group 'sgml
900 :type 'hook
901 :options '(html-autoview-mode))
902
903 (defvar html-quick-keys sgml-quick-keys
904 "Use C-c X combinations for quick insertion of frequent tags when non-nil.
905 This defaults to `sgml-quick-keys'.
906 This takes effect when first loading the library.")
907
908 (defvar html-mode-map
909 (let ((map (make-sparse-keymap))
910 (menu-map (make-sparse-keymap "HTML")))
911 (set-keymap-parent map sgml-mode-map)
912 (define-key map "\C-c6" 'html-headline-6)
913 (define-key map "\C-c5" 'html-headline-5)
914 (define-key map "\C-c4" 'html-headline-4)
915 (define-key map "\C-c3" 'html-headline-3)
916 (define-key map "\C-c2" 'html-headline-2)
917 (define-key map "\C-c1" 'html-headline-1)
918 (define-key map "\C-c\r" 'html-paragraph)
919 (define-key map "\C-c\n" 'html-line)
920 (define-key map "\C-c\C-c-" 'html-horizontal-rule)
921 (define-key map "\C-c\C-co" 'html-ordered-list)
922 (define-key map "\C-c\C-cu" 'html-unordered-list)
923 (define-key map "\C-c\C-cr" 'html-radio-buttons)
924 (define-key map "\C-c\C-cc" 'html-checkboxes)
925 (define-key map "\C-c\C-cl" 'html-list-item)
926 (define-key map "\C-c\C-ch" 'html-href-anchor)
927 (define-key map "\C-c\C-cn" 'html-name-anchor)
928 (define-key map "\C-c\C-ci" 'html-image)
929 (when html-quick-keys
930 (define-key map "\C-c-" 'html-horizontal-rule)
931 (define-key map "\C-co" 'html-ordered-list)
932 (define-key map "\C-cu" 'html-unordered-list)
933 (define-key map "\C-cr" 'html-radio-buttons)
934 (define-key map "\C-cc" 'html-checkboxes)
935 (define-key map "\C-cl" 'html-list-item)
936 (define-key map "\C-ch" 'html-href-anchor)
937 (define-key map "\C-cn" 'html-name-anchor)
938 (define-key map "\C-ci" 'html-image))
939 (define-key map "\C-c\C-s" 'html-autoview-mode)
940 (define-key map "\C-c\C-v" 'browse-url-of-buffer)
941 (define-key map [menu-bar html] (cons "HTML" menu-map))
942 (define-key menu-map [html-autoview-mode]
943 '("Toggle Autoviewing" . html-autoview-mode))
944 (define-key menu-map [browse-url-of-buffer]
945 '("View Buffer Contents" . browse-url-of-buffer))
946 (define-key menu-map [nil] '("--"))
947 ;;(define-key menu-map "6" '("Heading 6" . html-headline-6))
948 ;;(define-key menu-map "5" '("Heading 5" . html-headline-5))
949 ;;(define-key menu-map "4" '("Heading 4" . html-headline-4))
950 (define-key menu-map "3" '("Heading 3" . html-headline-3))
951 (define-key menu-map "2" '("Heading 2" . html-headline-2))
952 (define-key menu-map "1" '("Heading 1" . html-headline-1))
953 (define-key menu-map "l" '("Radio Buttons" . html-radio-buttons))
954 (define-key menu-map "c" '("Checkboxes" . html-checkboxes))
955 (define-key menu-map "l" '("List Item" . html-list-item))
956 (define-key menu-map "u" '("Unordered List" . html-unordered-list))
957 (define-key menu-map "o" '("Ordered List" . html-ordered-list))
958 (define-key menu-map "-" '("Horizontal Rule" . html-horizontal-rule))
959 (define-key menu-map "\n" '("Line Break" . html-line))
960 (define-key menu-map "\r" '("Paragraph" . html-paragraph))
961 (define-key menu-map "i" '("Image" . html-image))
962 (define-key menu-map "h" '("Href Anchor" . html-href-anchor))
963 (define-key menu-map "n" '("Name Anchor" . html-name-anchor))
964 map)
965 "Keymap for commands for use in HTML mode.")
966
967
968 (defvar html-face-tag-alist
969 '((bold . "b")
970 (italic . "i")
971 (underline . "u")
972 (modeline . "rev"))
973 "Value of `sgml-face-tag-alist' for HTML mode.")
974
975 (defvar html-tag-face-alist
976 '(("b" . bold)
977 ("big" . bold)
978 ("blink" . highlight)
979 ("cite" . italic)
980 ("em" . italic)
981 ("h1" bold underline)
982 ("h2" bold-italic underline)
983 ("h3" italic underline)
984 ("h4" . underline)
985 ("h5" . underline)
986 ("h6" . underline)
987 ("i" . italic)
988 ("rev" . modeline)
989 ("s" . underline)
990 ("small" . default)
991 ("strong" . bold)
992 ("title" bold underline)
993 ("tt" . default)
994 ("u" . underline)
995 ("var" . italic))
996 "Value of `sgml-tag-face-alist' for HTML mode.")
997
998
999 (defvar html-display-text
1000 '((img . "[/]")
1001 (hr . "----------")
1002 (li . "o "))
1003 "Value of `sgml-display-text' for HTML mode.")
1004 \f
1005
1006 ;; should code exactly HTML 3 here when that is finished
1007 (defvar html-tag-alist
1008 (let* ((1-7 '(("1") ("2") ("3") ("4") ("5") ("6") ("7")))
1009 (1-9 `(,@1-7 ("8") ("9")))
1010 (align '(("align" ("left") ("center") ("right"))))
1011 (valign '(("top") ("middle") ("bottom") ("baseline")))
1012 (rel '(("next") ("previous") ("parent") ("subdocument") ("made")))
1013 (href '("href" ("ftp:") ("file:") ("finger:") ("gopher:") ("http:")
1014 ("mailto:") ("news:") ("rlogin:") ("telnet:") ("tn3270:")
1015 ("wais:") ("/cgi-bin/")))
1016 (name '("name"))
1017 (link `(,href
1018 ("rel" ,@rel)
1019 ("rev" ,@rel)
1020 ("title")))
1021 (list '((nil \n ("List item: " "<li>" str
1022 (if sgml-xml "</li>") \n))))
1023 (cell `(t
1024 ,@align
1025 ("valign" ,@valign)
1026 ("colspan" ,@1-9)
1027 ("rowspan" ,@1-9)
1028 ("nowrap" t))))
1029 ;; put ,-expressions first, else byte-compile chokes (as of V19.29)
1030 ;; and like this it's more efficient anyway
1031 `(("a" ,name ,@link)
1032 ("base" t ,@href)
1033 ("dir" ,@list)
1034 ("font" nil "size" ("-1") ("+1") ("-2") ("+2") ,@1-7)
1035 ("form" (\n _ \n "<input type=\"submit\" value=\"\""
1036 (if sgml-xml "/>" ">"))
1037 ("action" ,@(cdr href)) ("method" ("get") ("post")))
1038 ("h1" ,@align)
1039 ("h2" ,@align)
1040 ("h3" ,@align)
1041 ("h4" ,@align)
1042 ("h5" ,@align)
1043 ("h6" ,@align)
1044 ("hr" t ("size" ,@1-9) ("width") ("noshade" t) ,@align)
1045 ("img" t ("align" ,@valign ("texttop") ("absmiddle") ("absbottom"))
1046 ("src") ("alt") ("width" "1") ("height" "1")
1047 ("border" "1") ("vspace" "1") ("hspace" "1") ("ismap" t))
1048 ("input" t ("size" ,@1-9) ("maxlength" ,@1-9) ("checked" t) ,name
1049 ("type" ("text") ("password") ("checkbox") ("radio")
1050 ("submit") ("reset"))
1051 ("value"))
1052 ("link" t ,@link)
1053 ("menu" ,@list)
1054 ("ol" ,@list ("type" ("A") ("a") ("I") ("i") ("1")))
1055 ("p" t ,@align)
1056 ("select" (nil \n
1057 ("Text: "
1058 "<option>" str (if sgml-xml "</option>") \n))
1059 ,name ("size" ,@1-9) ("multiple" t))
1060 ("table" (nil \n
1061 ((completing-read "Cell kind: " '(("td") ("th"))
1062 nil t "t")
1063 "<tr><" str ?> _
1064 (if sgml-xml (concat "<" str "></tr>")) \n))
1065 ("border" t ,@1-9) ("width" "10") ("cellpadding"))
1066 ("td" ,@cell)
1067 ("textarea" ,name ("rows" ,@1-9) ("cols" ,@1-9))
1068 ("th" ,@cell)
1069 ("ul" ,@list ("type" ("disc") ("circle") ("square")))
1070
1071 ,@sgml-tag-alist
1072
1073 ("abbrev")
1074 ("acronym")
1075 ("address")
1076 ("array" (nil \n
1077 ("Item: " "<item>" str (if sgml-xml "</item>") \n))
1078 "align")
1079 ("au")
1080 ("b")
1081 ("big")
1082 ("blink")
1083 ("blockquote" \n)
1084 ("body" \n ("background" ".gif") ("bgcolor" "#") ("text" "#")
1085 ("link" "#") ("alink" "#") ("vlink" "#"))
1086 ("box" (nil _ "<over>" _ (if sgml-xml "</over>")))
1087 ("br" t ("clear" ("left") ("right")))
1088 ("caption" ("valign" ("top") ("bottom")))
1089 ("center" \n)
1090 ("cite")
1091 ("code" \n)
1092 ("dd" ,(not sgml-xml))
1093 ("del")
1094 ("dfn")
1095 ("div")
1096 ("dl" (nil \n
1097 ( "Term: "
1098 "<dt>" str (if sgml-xml "</dt>")
1099 "<dd>" _ (if sgml-xml "</dd>") \n)))
1100 ("dt" (t _ (if sgml-xml "</dt>")
1101 "<dd>" (if sgml-xml "</dd>") \n))
1102 ("em")
1103 ;("fn" "id" "fn") ; ???
1104 ("head" \n)
1105 ("html" (\n
1106 "<head>\n"
1107 "<title>" (setq str (read-input "Title: ")) "</title>\n"
1108 "</head>\n"
1109 "<body>\n<h1>" str "</h1>\n" _
1110 "\n<address>\n<a href=\"mailto:"
1111 user-mail-address
1112 "\">" (user-full-name) "</a>\n</address>\n"
1113 "</body>"
1114 ))
1115 ("i")
1116 ("ins")
1117 ("isindex" t ("action") ("prompt"))
1118 ("kbd")
1119 ("lang")
1120 ("li" ,(not sgml-xml))
1121 ("math" \n)
1122 ("nobr")
1123 ("option" t ("value") ("label") ("selected" t))
1124 ("over" t)
1125 ("person")
1126 ("pre" \n)
1127 ("q")
1128 ("rev")
1129 ("s")
1130 ("samp")
1131 ("small")
1132 ("span" nil
1133 ("class"
1134 ("builtin")
1135 ("comment")
1136 ("constant")
1137 ("function-name")
1138 ("keyword")
1139 ("string")
1140 ("type")
1141 ("variable-name")
1142 ("warning")))
1143 ("strong")
1144 ("sub")
1145 ("sup")
1146 ("title")
1147 ("tr" t)
1148 ("tt")
1149 ("u")
1150 ("var")
1151 ("wbr" t)))
1152 "*Value of `sgml-tag-alist' for HTML mode.")
1153
1154 (defvar html-tag-help
1155 `(,@sgml-tag-help
1156 ("a" . "Anchor of point or link elsewhere")
1157 ("abbrev" . "?")
1158 ("acronym" . "?")
1159 ("address" . "Formatted mail address")
1160 ("array" . "Math array")
1161 ("au" . "?")
1162 ("b" . "Bold face")
1163 ("base" . "Base address for URLs")
1164 ("big" . "Font size")
1165 ("blink" . "Blinking text")
1166 ("blockquote" . "Indented quotation")
1167 ("body" . "Document body")
1168 ("box" . "Math fraction")
1169 ("br" . "Line break")
1170 ("caption" . "Table caption")
1171 ("center" . "Centered text")
1172 ("changed" . "Change bars")
1173 ("cite" . "Citation of a document")
1174 ("code" . "Formatted source code")
1175 ("dd" . "Definition of term")
1176 ("del" . "?")
1177 ("dfn" . "?")
1178 ("dir" . "Directory list (obsolete)")
1179 ("dl" . "Definition list")
1180 ("dt" . "Term to be definined")
1181 ("em" . "Emphasised")
1182 ("embed" . "Embedded data in foreign format")
1183 ("fig" . "Figure")
1184 ("figa" . "Figure anchor")
1185 ("figd" . "Figure description")
1186 ("figt" . "Figure text")
1187 ;("fn" . "?") ; ???
1188 ("font" . "Font size")
1189 ("form" . "Form with input fields")
1190 ("group" . "Document grouping")
1191 ("h1" . "Most important section headline")
1192 ("h2" . "Important section headline")
1193 ("h3" . "Section headline")
1194 ("h4" . "Minor section headline")
1195 ("h5" . "Unimportant section headline")
1196 ("h6" . "Least important section headline")
1197 ("head" . "Document header")
1198 ("hr" . "Horizontal rule")
1199 ("html" . "HTML Document")
1200 ("i" . "Italic face")
1201 ("img" . "Graphic image")
1202 ("input" . "Form input field")
1203 ("ins" . "?")
1204 ("isindex" . "Input field for index search")
1205 ("kbd" . "Keybard example face")
1206 ("lang" . "Natural language")
1207 ("li" . "List item")
1208 ("link" . "Link relationship")
1209 ("math" . "Math formula")
1210 ("menu" . "Menu list (obsolete)")
1211 ("mh" . "Form mail header")
1212 ("nextid" . "Allocate new id")
1213 ("nobr" . "Text without line break")
1214 ("ol" . "Ordered list")
1215 ("option" . "Selection list item")
1216 ("over" . "Math fraction rule")
1217 ("p" . "Paragraph start")
1218 ("panel" . "Floating panel")
1219 ("person" . "?")
1220 ("pre" . "Preformatted fixed width text")
1221 ("q" . "?")
1222 ("rev" . "Reverse video")
1223 ("s" . "?")
1224 ("samp" . "Sample text")
1225 ("select" . "Selection list")
1226 ("small" . "Font size")
1227 ("sp" . "Nobreak space")
1228 ("strong" . "Standout text")
1229 ("sub" . "Subscript")
1230 ("sup" . "Superscript")
1231 ("table" . "Table with rows and columns")
1232 ("tb" . "Table vertical break")
1233 ("td" . "Table data cell")
1234 ("textarea" . "Form multiline edit area")
1235 ("th" . "Table header cell")
1236 ("title" . "Document title")
1237 ("tr" . "Table row separator")
1238 ("tt" . "Typewriter face")
1239 ("u" . "Underlined text")
1240 ("ul" . "Unordered list")
1241 ("var" . "Math variable face")
1242 ("wbr" . "Enable <br> within <nobr>"))
1243 "*Value of `sgml-tag-help' for HTML mode.")
1244 \f
1245 ;;;###autoload
1246 (define-derived-mode html-mode sgml-mode "HTML"
1247 "Major mode based on SGML mode for editing HTML documents.
1248 This allows inserting skeleton constructs used in hypertext documents with
1249 completion. See below for an introduction to HTML. Use
1250 \\[browse-url-of-buffer] to see how this comes out. See also `sgml-mode' on
1251 which this is based.
1252
1253 Do \\[describe-variable] html- SPC and \\[describe-variable] sgml- SPC to see available variables.
1254
1255 To write fairly well formatted pages you only need to know few things. Most
1256 browsers have a function to read the source code of the page being seen, so
1257 you can imitate various tricks. Here's a very short HTML primer which you
1258 can also view with a browser to see what happens:
1259
1260 <title>A Title Describing Contents</title> should be on every page. Pages can
1261 have <h1>Very Major Headlines</h1> through <h6>Very Minor Headlines</h6>
1262 <hr> Parts can be separated with horizontal rules.
1263
1264 <p>Paragraphs only need an opening tag. Line breaks and multiple spaces are
1265 ignored unless the text is <pre>preformatted.</pre> Text can be marked as
1266 <b>bold</b>, <i>italic</i> or <u>underlined</u> using the normal M-g or
1267 Edit/Text Properties/Face commands.
1268
1269 Pages can have <a name=\"SOMENAME\">named points</a> and can link other points
1270 to them with <a href=\"#SOMENAME\">see also somename</a>. In the same way <a
1271 href=\"URL\">see also URL</a> where URL is a filename relative to current
1272 directory, or absolute as in `http://www.cs.indiana.edu/elisp/w3/docs.html'.
1273
1274 Images in many formats can be inlined with <img src=\"URL\">.
1275
1276 If you mainly create your own documents, `sgml-specials' might be
1277 interesting. But note that some HTML 2 browsers can't handle `&apos;'.
1278 To work around that, do:
1279 (eval-after-load \"sgml-mode\" '(aset sgml-char-names ?' nil))
1280
1281 \\{html-mode-map}"
1282 (set (make-local-variable 'sgml-display-text) html-display-text)
1283 (set (make-local-variable 'sgml-tag-face-alist) html-tag-face-alist)
1284 (make-local-variable 'sgml-tag-alist)
1285 (make-local-variable 'sgml-face-tag-alist)
1286 (make-local-variable 'sgml-tag-help)
1287 (make-local-variable 'outline-regexp)
1288 (make-local-variable 'outline-heading-end-regexp)
1289 (make-local-variable 'outline-level)
1290 (make-local-variable 'sentence-end)
1291 (setq sentence-end
1292 (if sentence-end-double-space
1293 "[.?!][]\"')}]*\\(<[^>]*>\\)*\\($\\| $\\|\t\\| \\)[ \t\n]*"
1294 "[.?!][]\"')}]*\\(<[^>]*>\\)*\\($\\|[ \t]\\)[ \t\n]*"))
1295 (setq sgml-tag-alist html-tag-alist
1296 sgml-face-tag-alist html-face-tag-alist
1297 sgml-tag-help html-tag-help
1298 outline-regexp "^.*<[Hh][1-6]\\>"
1299 outline-heading-end-regexp "</[Hh][1-6]>"
1300 outline-level (lambda ()
1301 (char-after (1- (match-end 0)))))
1302 (setq imenu-create-index-function 'html-imenu-index)
1303 (when sgml-xml (setq mode-name "XHTML"))
1304 (set (make-local-variable 'sgml-empty-tags)
1305 '("br" "hr" "img" "input" "area" "link" "param" "col"
1306 "base" "meta" "basefont" "frame" "isindex" "wbr"))
1307 ;; It's for the user to decide if it defeats it or not -stef
1308 ;; (make-local-variable 'imenu-sort-function)
1309 ;; (setq imenu-sort-function nil) ; sorting the menu defeats the purpose
1310 )
1311 \f
1312 (defvar html-imenu-regexp
1313 "\\s-*<h\\([1-9]\\)[^\n<>]*>\\(<[^\n<>]*>\\)*\\s-*\\([^\n<>]*\\)"
1314 "*A regular expression matching a head line to be added to the menu.
1315 The first `match-string' should be a number from 1-9.
1316 The second `match-string' matches extra tags and is ignored.
1317 The third `match-string' will be the used in the menu.")
1318
1319 (defun html-imenu-index ()
1320 "Return an table of contents for an HTML buffer for use with Imenu."
1321 (let (toc-index)
1322 (save-excursion
1323 (goto-char (point-min))
1324 (while (re-search-forward html-imenu-regexp nil t)
1325 (setq toc-index
1326 (cons (cons (concat (make-string
1327 (* 2 (1- (string-to-number (match-string 1))))
1328 ?\ )
1329 (match-string 3))
1330 (line-beginning-position))
1331 toc-index))))
1332 (nreverse toc-index)))
1333
1334 (defun html-autoview-mode (&optional arg)
1335 "Toggle automatic viewing via `browse-url-of-buffer' upon saving buffer.
1336 With positive prefix ARG always turns viewing on, with negative ARG always off.
1337 Can be used as a value for `html-mode-hook'."
1338 (interactive "P")
1339 (if (setq arg (if arg
1340 (< (prefix-numeric-value arg) 0)
1341 (and (boundp 'after-save-hook)
1342 (memq 'browse-url-of-buffer after-save-hook))))
1343 (setq after-save-hook (delq 'browse-url-of-buffer after-save-hook))
1344 (add-hook 'after-save-hook 'browse-url-of-buffer nil t))
1345 (message "Autoviewing turned %s."
1346 (if arg "off" "on")))
1347 \f
1348 (define-skeleton html-href-anchor
1349 "HTML anchor tag with href attribute."
1350 "URL: "
1351 '(setq input "http:")
1352 "<a href=\"" str "\">" _ "</a>")
1353
1354 (define-skeleton html-name-anchor
1355 "HTML anchor tag with name attribute."
1356 "Name: "
1357 "<a name=\"" str "\">" _ "</a>")
1358
1359 (define-skeleton html-headline-1
1360 "HTML level 1 headline tags."
1361 nil
1362 "<h1>" _ "</h1>")
1363
1364 (define-skeleton html-headline-2
1365 "HTML level 2 headline tags."
1366 nil
1367 "<h2>" _ "</h2>")
1368
1369 (define-skeleton html-headline-3
1370 "HTML level 3 headline tags."
1371 nil
1372 "<h3>" _ "</h3>")
1373
1374 (define-skeleton html-headline-4
1375 "HTML level 4 headline tags."
1376 nil
1377 "<h4>" _ "</h4>")
1378
1379 (define-skeleton html-headline-5
1380 "HTML level 5 headline tags."
1381 nil
1382 "<h5>" _ "</h5>")
1383
1384 (define-skeleton html-headline-6
1385 "HTML level 6 headline tags."
1386 nil
1387 "<h6>" _ "</h6>")
1388
1389 (define-skeleton html-horizontal-rule
1390 "HTML horizontal rule tag."
1391 nil
1392 (if sgml-xml "<hr/>" "<hr>") \n)
1393
1394 (define-skeleton html-image
1395 "HTML image tag."
1396 nil
1397 "<img src=\"" _ "\""
1398 (if sgml-xml "/>" ">"))
1399
1400 (define-skeleton html-line
1401 "HTML line break tag."
1402 nil
1403 (if sgml-xml "<br/>" "<br>") \n)
1404
1405 (define-skeleton html-ordered-list
1406 "HTML ordered list tags."
1407 nil
1408 "<ol>" \n
1409 "<li>" _ (if sgml-xml "</li>") \n
1410 "</ol>")
1411
1412 (define-skeleton html-unordered-list
1413 "HTML unordered list tags."
1414 nil
1415 "<ul>" \n
1416 "<li>" _ (if sgml-xml "</li>") \n
1417 "</ul>")
1418
1419 (define-skeleton html-list-item
1420 "HTML list item tag."
1421 nil
1422 (if (bolp) nil '\n)
1423 "<li>" _ (if sgml-xml "</li>"))
1424
1425 (define-skeleton html-paragraph
1426 "HTML paragraph tag."
1427 nil
1428 (if (bolp) nil ?\n)
1429 \n "<p>" _ (if sgml-xml "</p>"))
1430
1431 (define-skeleton html-checkboxes
1432 "Group of connected checkbox inputs."
1433 nil
1434 '(setq v1 nil
1435 v2 nil)
1436 ("Value: "
1437 "<input type=\"" (identity "checkbox") ; see comment above about identity
1438 "\" name=\"" (or v1 (setq v1 (skeleton-read "Name: ")))
1439 "\" value=\"" str ?\"
1440 (when (y-or-n-p "Set \"checked\" attribute? ")
1441 (funcall skeleton-transformation " checked"))
1442 (if sgml-xml "/>" ">")
1443 (skeleton-read "Text: " (capitalize str))
1444 (or v2 (setq v2 (if (y-or-n-p "Newline after text? ")
1445 (funcall skeleton-transformation
1446 (if sgml-xml "<br/>" "<br>"))
1447 "")))
1448 \n))
1449
1450 (define-skeleton html-radio-buttons
1451 "Group of connected radio button inputs."
1452 nil
1453 '(setq v1 nil
1454 v2 (cons nil nil))
1455 ("Value: "
1456 "<input type=\"" (identity "radio") ; see comment above about identity
1457 "\" name=\"" (or (car v2) (setcar v2 (skeleton-read "Name: ")))
1458 "\" value=\"" str ?\"
1459 (when (and (not v1) (setq v1 (y-or-n-p "Set \"checked\" attribute? ")))
1460 (funcall skeleton-transformation " checked"))
1461 (if sgml-xml "/>" ">")
1462 (skeleton-read "Text: " (capitalize str))
1463 (or (cdr v2) (setcdr v2 (if (y-or-n-p "Newline after text? ")
1464 (funcall skeleton-transformation
1465 (if sgml-xml "<br/>" "<br>"))
1466 "")))
1467 \n))
1468
1469 (provide 'sgml-mode)
1470
1471 ;;; sgml-mode.el ends here