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