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