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