(sgml-namify-char): New cmd.
[bpt/emacs.git] / lisp / textmodes / sgml-mode.el
1 ;;; sgml-mode.el --- SGML- and HTML-editing modes
2
3 ;; Copyright (C) 1992,95,96,98,2001,2002 Free Software Foundation, Inc.
4
5 ;; Author: James Clark <jjc@jclark.com>
6 ;; Maintainer: FSF
7 ;; Adapted-By: ESR, Daniel Pfeiffer <occitan@esperanto.org>,
8 ;; F.Potorti@cnuce.cnr.it
9 ;; Keywords: wp, hypermedia, comm, languages
10
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
15 ;; the Free Software Foundation; either version 2, or (at your option)
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
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.
27
28 ;;; Commentary:
29
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.
33
34 ;;; Code:
35
36 (eval-when-compile
37 (require 'skeleton)
38 (require 'outline)
39 (require 'cl))
40
41 (defgroup sgml nil
42 "SGML editing mode"
43 :group 'languages)
44
45 (defcustom sgml-basic-offset 2
46 "*Specifies the basic indentation level for `sgml-indent-line'."
47 :type 'integer
48 :group 'sgml)
49
50 (defcustom sgml-transformation 'identity
51 "*Default value for `skeleton-transformation' (which see) in SGML mode."
52 :type 'function
53 :group 'sgml)
54
55 (put 'sgml-transformation 'variable-interactive
56 "aTransformation function: ")
57
58 (defcustom sgml-mode-hook nil
59 "Hook run by command `sgml-mode'.
60 `text-mode-hook' is run first."
61 :group 'sgml
62 :type 'hook)
63
64 ;; As long as Emacs' syntax can't be complemented with predicates to context
65 ;; sensitively confirm the syntax of characters, we have to live with this
66 ;; kludgy kind of tradeoff.
67 (defvar sgml-specials '(?\")
68 "List of characters that have a special meaning for SGML mode.
69 This list is used when first loading the `sgml-mode' library.
70 The supported characters and potential disadvantages are:
71
72 ?\\\" Makes \" in text start a string.
73 ?' Makes ' in text start a string.
74 ?- Makes -- in text start a comment.
75
76 When only one of ?\\\" or ?' are included, \"'\" or '\"', as can be found in
77 DTDs, start a string. To partially avoid this problem this also makes these
78 self insert as named entities depending on `sgml-quick-keys'.
79
80 Including ?- has the problem of affecting dashes that have nothing to do
81 with comments, so we normally turn it off.")
82
83 (defvar sgml-quick-keys nil
84 "Use <, >, &, /, SPC and `sgml-specials' keys \"electrically\" when non-nil.
85 This takes effect when first loading the `sgml-mode' library.")
86
87 (defvar sgml-mode-map
88 (let ((map (make-keymap)) ;`sparse' doesn't allow binding to charsets.
89 (menu-map (make-sparse-keymap "SGML")))
90 (define-key map "\C-c\C-i" 'sgml-tags-invisible)
91 (define-key map "/" 'sgml-slash)
92 (define-key map "\C-c\C-n" 'sgml-name-char)
93 (define-key map "\C-c\C-t" 'sgml-tag)
94 (define-key map "\C-c\C-a" 'sgml-attributes)
95 (define-key map "\C-c\C-b" 'sgml-skip-tag-backward)
96 (define-key map [?\C-c left] 'sgml-skip-tag-backward)
97 (define-key map "\C-c\C-f" 'sgml-skip-tag-forward)
98 (define-key map [?\C-c right] 'sgml-skip-tag-forward)
99 (define-key map "\C-c\C-d" 'sgml-delete-tag)
100 (define-key map "\C-c\^?" 'sgml-delete-tag)
101 (define-key map "\C-c?" 'sgml-tag-help)
102 (define-key map "\C-c/" 'sgml-close-tag)
103 (define-key map "\C-c8" 'sgml-name-8bit-mode)
104 (define-key map "\C-c\C-v" 'sgml-validate)
105 (when sgml-quick-keys
106 (define-key map "&" 'sgml-name-char)
107 (define-key map "<" 'sgml-tag)
108 (define-key map " " 'sgml-auto-attributes)
109 (define-key map ">" 'sgml-maybe-end-tag)
110 (when (memq ?\" sgml-specials)
111 (define-key map "\"" 'sgml-name-self))
112 (when (memq ?' sgml-specials)
113 (define-key map "'" 'sgml-name-self)))
114 (define-key map (vector (make-char 'latin-iso8859-1))
115 'sgml-maybe-name-self)
116 (let ((c 127)
117 (map (nth 1 map)))
118 (while (< (setq c (1+ c)) 256)
119 (aset map c 'sgml-maybe-name-self)))
120 (define-key map [menu-bar sgml] (cons "SGML" menu-map))
121 (define-key menu-map [sgml-validate] '("Validate" . sgml-validate))
122 (define-key menu-map [sgml-name-8bit-mode]
123 '("Toggle 8 Bit Insertion" . sgml-name-8bit-mode))
124 (define-key menu-map [sgml-tags-invisible]
125 '("Toggle Tag Visibility" . sgml-tags-invisible))
126 (define-key menu-map [sgml-tag-help]
127 '("Describe Tag" . sgml-tag-help))
128 (define-key menu-map [sgml-delete-tag]
129 '("Delete Tag" . sgml-delete-tag))
130 (define-key menu-map [sgml-skip-tag-forward]
131 '("Forward Tag" . sgml-skip-tag-forward))
132 (define-key menu-map [sgml-skip-tag-backward]
133 '("Backward Tag" . sgml-skip-tag-backward))
134 (define-key menu-map [sgml-attributes]
135 '("Insert Attributes" . sgml-attributes))
136 (define-key menu-map [sgml-tag] '("Insert Tag" . sgml-tag))
137 map)
138 "Keymap for SGML mode. See also `sgml-specials'.")
139
140 (defun sgml-make-syntax-table (specials)
141 (let ((table (make-syntax-table text-mode-syntax-table)))
142 (modify-syntax-entry ?< "(>" table)
143 (modify-syntax-entry ?> ")<" table)
144 (modify-syntax-entry ?: "_" table)
145 (modify-syntax-entry ?_ "_" table)
146 (modify-syntax-entry ?. "_" table)
147 (if (memq ?- specials)
148 (modify-syntax-entry ?- "_ 1234" table))
149 (if (memq ?\" specials)
150 (modify-syntax-entry ?\" "\"\"" table))
151 (if (memq ?' specials)
152 (modify-syntax-entry ?\' "\"'" table))
153 table))
154
155 (defvar sgml-mode-syntax-table (sgml-make-syntax-table sgml-specials)
156 "Syntax table used in SGML mode. See also `sgml-specials'.")
157
158 (defconst sgml-tag-syntax-table
159 (let ((table (sgml-make-syntax-table '(?- ?\" ?\'))))
160 (dolist (char '(?\( ?\) ?\{ ?\} ?\[ ?\] ?$ ?% ?& ?* ?+ ?/))
161 (modify-syntax-entry char "." table))
162 table)
163 "Syntax table used to parse SGML tags.")
164
165 (defcustom sgml-name-8bit-mode nil
166 "*When non-nil, insert non-ASCII characters as named entities."
167 :type 'boolean
168 :group 'sgml)
169
170 (defvar sgml-char-names
171 [nil nil nil nil nil nil nil nil
172 nil nil nil nil nil nil nil nil
173 nil nil nil nil nil nil nil nil
174 nil nil nil nil nil nil nil nil
175 "nbsp" "excl" "quot" "num" "dollar" "percnt" "amp" "apos"
176 "lpar" "rpar" "ast" "plus" "comma" "hyphen" "period" "sol"
177 nil nil nil nil nil nil nil nil
178 nil nil "colon" "semi" "lt" "eq" "gt" "quest"
179 "commat" nil nil nil nil nil nil nil
180 nil nil nil nil nil nil nil nil
181 nil nil nil nil nil nil nil nil
182 nil nil nil "lsqb" nil "rsqb" "uarr" "lowbar"
183 "lsquo" nil nil nil nil nil nil nil
184 nil nil nil nil nil nil nil nil
185 nil nil nil nil nil nil nil nil
186 nil nil nil "lcub" "verbar" "rcub" "tilde" nil
187 nil nil nil nil nil nil nil nil
188 nil nil nil nil nil nil nil nil
189 nil nil nil nil nil nil nil nil
190 nil nil nil nil nil nil nil nil
191 "nbsp" "iexcl" "cent" "pound" "curren" "yen" "brvbar" "sect"
192 "uml" "copy" "ordf" "laquo" "not" "shy" "reg" "macr"
193 "ring" "plusmn" "sup2" "sup3" "acute" "micro" "para" "middot"
194 "cedil" "sup1" "ordm" "raquo" "frac14" "frac12" "frac34" "iquest"
195 "Agrave" "Aacute" "Acirc" "Atilde" "Auml" "Aring" "AElig" "Ccedil"
196 "Egrave" "Eacute" "Ecirc" "Euml" "Igrave" "Iacute" "Icirc" "Iuml"
197 "ETH" "Ntilde" "Ograve" "Oacute" "Ocirc" "Otilde" "Ouml" nil
198 "Oslash" "Ugrave" "Uacute" "Ucirc" "Uuml" "Yacute" "THORN" "szlig"
199 "agrave" "aacute" "acirc" "atilde" "auml" "aring" "aelig" "ccedil"
200 "egrave" "eacute" "ecirc" "euml" "igrave" "iacute" "icirc" "iuml"
201 "eth" "ntilde" "ograve" "oacute" "ocirc" "otilde" "ouml" "divide"
202 "oslash" "ugrave" "uacute" "ucirc" "uuml" "yacute" "thorn" "yuml"]
203 "Vector of symbolic character names without `&' and `;'.")
204
205 (put 'sgml-table 'char-table-extra-slots 0)
206
207 (defvar sgml-char-names-table
208 (let ((table (make-char-table 'sgml-table))
209 (i 32)
210 elt)
211 (while (< i 256)
212 (setq elt (aref sgml-char-names i))
213 (if elt (aset table (make-char 'latin-iso8859-1 i) elt))
214 (setq i (1+ i)))
215 table)
216 "A table for mapping non-ASCII characters into SGML entity names.
217 Currently, only Latin-1 characters are supported.")
218
219 ;; nsgmls is a free SGML parser in the SP suite available from
220 ;; ftp.jclark.com and otherwise packaged for GNU systems.
221 ;; Its error messages can be parsed by next-error.
222 ;; The -s option suppresses output.
223
224 (defcustom sgml-validate-command "nsgmls -s" ; replaced old `sgmls'
225 "*The command to validate an SGML document.
226 The file name of current buffer file name will be appended to this,
227 separated by a space."
228 :type 'string
229 :version "21.1"
230 :group 'sgml)
231
232 (defvar sgml-saved-validate-command nil
233 "The command last used to validate in this buffer.")
234
235 ;; I doubt that null end tags are used much for large elements,
236 ;; so use a small distance here.
237 (defcustom sgml-slash-distance 1000
238 "*If non-nil, is the maximum distance to search for matching `/'."
239 :type '(choice (const nil) integer)
240 :group 'sgml)
241
242 (defconst sgml-name-re "[_:[:alpha:]][-_.:[:alnum:]]*")
243 (defconst sgml-tag-name-re (concat "<\\([!/?]?" sgml-name-re "\\)"))
244 (defconst sgml-attrs-re "\\(?:[^\"'/><]\\|\"[^\"]*\"\\|'[^']*'\\)*")
245 (defconst sgml-start-tag-regex (concat "<" sgml-name-re sgml-attrs-re)
246 "Regular expression that matches a non-empty start tag.
247 Any terminating `>' or `/' is not matched.")
248
249
250 ;; internal
251 (defconst sgml-font-lock-keywords-1
252 `((,(concat "<\\([!?]" sgml-name-re "\\)") 1 font-lock-keyword-face)
253 (,(concat "<\\(/?" sgml-name-re"\\)") 1 font-lock-function-name-face)
254 ;; FIXME: this doesn't cover the variables using a default value.
255 (,(concat "\\(" sgml-name-re "\\)=[\"']") 1 font-lock-variable-name-face)
256 (,(concat "[&%]" sgml-name-re ";?") . font-lock-variable-name-face)))
257
258 (defconst sgml-font-lock-keywords-2
259 (append
260 sgml-font-lock-keywords-1
261 '((eval
262 . (cons (concat "<"
263 (regexp-opt (mapcar 'car sgml-tag-face-alist) t)
264 "\\([ \t][^>]*\\)?>\\([^<]+\\)</\\1>")
265 '(3 (cdr (assoc (downcase (match-string 1))
266 sgml-tag-face-alist))))))))
267
268 ;; for font-lock, but must be defvar'ed after
269 ;; sgml-font-lock-keywords-1 and sgml-font-lock-keywords-2 above
270 (defvar sgml-font-lock-keywords sgml-font-lock-keywords-1
271 "*Rules for highlighting SGML code. See also `sgml-tag-face-alist'.")
272
273 (defvar sgml-font-lock-syntactic-keywords
274 ;; Use the `b' style of comments to avoid interference with the -- ... --
275 ;; comments recognized when `sgml-specials' includes ?-.
276 ;; FIXME: beware of <!--> blabla <!--> !!
277 '(("\\(<\\)!--" (1 "< b"))
278 ("--[ \t\n]*\\(>\\)" (1 "> b")))
279 "Syntactic keywords for `sgml-mode'.")
280
281 ;; internal
282 (defvar sgml-face-tag-alist ()
283 "Alist of face and tag name for facemenu.")
284
285 (defvar sgml-tag-face-alist ()
286 "Tag names and face or list of faces to fontify with when invisible.
287 When `font-lock-maximum-decoration' is 1 this is always used for fontifying.
288 When more these are fontified together with `sgml-font-lock-keywords'.")
289
290 (defvar sgml-display-text ()
291 "Tag names as lowercase symbols, and display string when invisible.")
292
293 ;; internal
294 (defvar sgml-tags-invisible nil)
295
296 (defcustom sgml-tag-alist
297 '(("![" ("ignore" t) ("include" t))
298 ("!attlist")
299 ("!doctype")
300 ("!element")
301 ("!entity"))
302 "*Alist of tag names for completing read and insertion rules.
303 This alist is made up as
304
305 ((\"tag\" . TAGRULE)
306 ...)
307
308 TAGRULE is a list of optionally t (no endtag) or `\\n' (separate endtag by
309 newlines) or a skeleton with nil, t or `\\n' in place of the interactor
310 followed by an ATTRIBUTERULE (for an always present attribute) or an
311 attribute alist.
312
313 The attribute alist is made up as
314
315 ((\"attribute\" . ATTRIBUTERULE)
316 ...)
317
318 ATTRIBUTERULE is a list of optionally t (no value when no input) followed by
319 an optional alist of possible values."
320 :type '(repeat (cons (string :tag "Tag Name")
321 (repeat :tag "Tag Rule" sexp)))
322 :group 'sgml)
323
324 (defcustom sgml-tag-help
325 '(("!" . "Empty declaration for comment")
326 ("![" . "Embed declarations with parser directive")
327 ("!attlist" . "Tag attributes declaration")
328 ("!doctype" . "Document type (DTD) declaration")
329 ("!element" . "Tag declaration")
330 ("!entity" . "Entity (macro) declaration"))
331 "*Alist of tag name and short description."
332 :type '(repeat (cons (string :tag "Tag Name")
333 (string :tag "Description")))
334 :group 'sgml)
335
336 (defcustom sgml-xml-mode nil
337 "*When non-nil, tag insertion functions will be XML-compliant.
338 If this variable is customized, the custom value is used always.
339 Otherwise, it is set to be buffer-local when the file has
340 a DOCTYPE or an XML declaration."
341 :type 'boolean
342 :version "21.4"
343 :group 'sgml)
344
345 (defvar sgml-empty-tags nil
346 "List of tags whose !ELEMENT definition says EMPTY.")
347
348 (defvar sgml-unclosed-tags nil
349 "List of tags whose !ELEMENT definition says the end-tag is optional.")
350
351 (defun sgml-xml-guess ()
352 "Guess whether the current buffer is XML."
353 (save-excursion
354 (goto-char (point-min))
355 (when (or (string= "xml" (file-name-extension (or buffer-file-name "")))
356 (looking-at "\\s-*<\\?xml")
357 (when (re-search-forward
358 (eval-when-compile
359 (mapconcat 'identity
360 '("<!DOCTYPE" "\\(\\w+\\)" "\\(\\w+\\)"
361 "\"\\([^\"]+\\)\"" "\"\\([^\"]+\\)\"")
362 "\\s-+"))
363 nil t)
364 (string-match "X\\(HT\\)?ML" (match-string 3))))
365 (set (make-local-variable 'sgml-xml-mode) t))))
366
367 (defvar v2) ; free for skeleton
368
369 (defun sgml-comment-indent-new-line (&optional soft)
370 (let ((comment-start "-- ")
371 (comment-start-skip "\\(<!\\)?--[ \t]*")
372 (comment-end " --")
373 (comment-style 'plain))
374 (comment-indent-new-line soft)))
375
376 (defun sgml-mode-facemenu-add-face-function (face end)
377 (if (setq face (cdr (assq face sgml-face-tag-alist)))
378 (progn
379 (setq face (funcall skeleton-transformation face))
380 (setq facemenu-end-add-face (concat "</" face ">"))
381 (concat "<" face ">"))
382 (error "Face not configured for %s mode" mode-name)))
383
384 ;;;###autoload
385 (define-derived-mode sgml-mode text-mode "SGML"
386 "Major mode for editing SGML documents.
387 Makes > match <.
388 Keys <, &, SPC within <>, \", / and ' can be electric depending on
389 `sgml-quick-keys'.
390
391 An argument of N to a tag-inserting command means to wrap it around
392 the next N words. In Transient Mark mode, when the mark is active,
393 N defaults to -1, which means to wrap it around the current region.
394
395 If you like upcased tags, put (setq sgml-transformation 'upcase) in
396 your `.emacs' file.
397
398 Use \\[sgml-validate] to validate your document with an SGML parser.
399
400 Do \\[describe-variable] sgml- SPC to see available variables.
401 Do \\[describe-key] on the following bindings to discover what they do.
402 \\{sgml-mode-map}"
403 (make-local-variable 'sgml-saved-validate-command)
404 (make-local-variable 'facemenu-end-add-face)
405 ;;(make-local-variable 'facemenu-remove-face-function)
406 ;; A start or end tag by itself on a line separates a paragraph.
407 ;; This is desirable because SGML discards a newline that appears
408 ;; immediately after a start tag or immediately before an end tag.
409 (set (make-local-variable 'paragraph-start) (concat "[ \t]*$\\|\
410 \[ \t]*</?\\(" sgml-name-re sgml-attrs-re "\\)?>"))
411 (set (make-local-variable 'paragraph-separate)
412 (concat paragraph-start "$"))
413 (set (make-local-variable 'adaptive-fill-regexp) "[ \t]*")
414 (set (make-local-variable 'indent-line-function) 'sgml-indent-line)
415 (set (make-local-variable 'comment-start) "<!-- ")
416 (set (make-local-variable 'comment-end) " -->")
417 (set (make-local-variable 'comment-indent-function) 'sgml-comment-indent)
418 (set (make-local-variable 'comment-line-break-function)
419 'sgml-comment-indent-new-line)
420 (set (make-local-variable 'skeleton-further-elements)
421 '((completion-ignore-case t)))
422 (set (make-local-variable 'skeleton-end-hook)
423 (lambda ()
424 (or (eolp)
425 (not (or (eq v2 '\n) (eq (car-safe v2) '\n)))
426 (newline-and-indent))))
427 (set (make-local-variable 'font-lock-defaults)
428 '((sgml-font-lock-keywords
429 sgml-font-lock-keywords-1
430 sgml-font-lock-keywords-2)
431 nil t nil nil
432 (font-lock-syntactic-keywords
433 . sgml-font-lock-syntactic-keywords)))
434 (set (make-local-variable 'facemenu-add-face-function)
435 'sgml-mode-facemenu-add-face-function)
436 (sgml-xml-guess)
437 (if sgml-xml-mode
438 (setq mode-name "XML")
439 (set (make-local-variable 'skeleton-transformation) sgml-transformation))
440 ;; This will allow existing comments within declarations to be
441 ;; recognized.
442 (set (make-local-variable 'comment-start-skip) "\\(?:<!\\)?--[ \t]*")
443 (set (make-local-variable 'comment-end-skip) "[ \t]*--\\([ \t\n]*>\\)?")
444 ;; This definition probably is not useful in derived modes.
445 (set (make-local-variable 'imenu-generic-expression)
446 (concat "<!\\(element\\|entity\\)[ \t\n]+%?[ \t\n]*\\("
447 sgml-name-re "\\)")))
448
449 ;; Some programs (such as Glade 2) generate XML which has
450 ;; -*- mode: xml -*-.
451 (defalias 'xml-mode 'sgml-mode)
452
453 (defun sgml-comment-indent ()
454 (if (looking-at "--") comment-column 0))
455
456 (defun sgml-slash (arg)
457 "Insert ARG slash characters.
458 Behaves electrically if `sgml-quick-keys' is non-nil."
459 (interactive "p")
460 (cond
461 ((not (and (eq (char-before) ?<) (= arg 1)))
462 (sgml-slash-matching arg))
463 ((eq sgml-quick-keys 'indent)
464 (insert-char ?/ 1)
465 (indent-according-to-mode))
466 ((eq sgml-quick-keys 'close)
467 (delete-backward-char 1)
468 (sgml-close-tag))
469 (t
470 (sgml-slash-matching arg))))
471
472 (defun sgml-slash-matching (arg)
473 "Insert `/' and display any previous matching `/'.
474 Two `/'s are treated as matching if the first `/' ends a net-enabling
475 start tag, and the second `/' is the corresponding null end tag."
476 (interactive "p")
477 (insert-char ?/ arg)
478 (if (> arg 0)
479 (let ((oldpos (point))
480 (blinkpos)
481 (level 0))
482 (save-excursion
483 (save-restriction
484 (if sgml-slash-distance
485 (narrow-to-region (max (point-min)
486 (- (point) sgml-slash-distance))
487 oldpos))
488 (if (and (re-search-backward sgml-start-tag-regex (point-min) t)
489 (eq (match-end 0) (1- oldpos)))
490 ()
491 (goto-char (1- oldpos))
492 (while (and (not blinkpos)
493 (search-backward "/" (point-min) t))
494 (let ((tagend (save-excursion
495 (if (re-search-backward sgml-start-tag-regex
496 (point-min) t)
497 (match-end 0)
498 nil))))
499 (if (eq tagend (point))
500 (if (eq level 0)
501 (setq blinkpos (point))
502 (setq level (1- level)))
503 (setq level (1+ level)))))))
504 (when blinkpos
505 (goto-char blinkpos)
506 (if (pos-visible-in-window-p)
507 (sit-for 1)
508 (message "Matches %s"
509 (buffer-substring (line-beginning-position)
510 (1+ blinkpos)))))))))
511
512 ;; Why doesn't this use the iso-cvt table or, preferably, generate the
513 ;; inverse of the extensive table in the SGML Quail input method? -- fx
514 ;; I guess that's moot since it only works with Latin-1 anyhow.
515 (defun sgml-name-char (&optional char)
516 "Insert a symbolic character name according to `sgml-char-names'.
517 Non-ASCII chars may be inserted either with the meta key, as in M-SPC for
518 no-break space or M-- for a soft hyphen; or via an input method or
519 encoded keyboard operation."
520 (interactive "*")
521 (insert ?&)
522 (or char
523 (setq char (read-quoted-char "Enter char or octal number")))
524 (delete-backward-char 1)
525 (insert char)
526 (undo-boundary)
527 (sgml-namify-char))
528
529 (defun sgml-namify-char ()
530 "Change the char before point into its `&name;' equivalent.
531 Uses `sgml-char-names'."
532 (interactive)
533 (let* ((char (char-before))
534 (name
535 (cond
536 ((null char) (error "No char before point"))
537 ((< char 256) (or (aref sgml-char-names char) char))
538 ((aref sgml-char-names-table char))
539 ((encode-char char 'ucs)))))
540 (if (not name)
541 (error "Don't know the name of `%c'" char)
542 (delete-backward-char 1)
543 (insert (format (if (numberp name) "&#%d;" "&%s;") name)))))
544
545 (defun sgml-name-self ()
546 "Insert a symbolic character name according to `sgml-char-names'."
547 (interactive "*")
548 (sgml-name-char last-command-char))
549
550 (defun sgml-maybe-name-self ()
551 "Insert a symbolic character name according to `sgml-char-names'."
552 (interactive "*")
553 (if sgml-name-8bit-mode
554 (let ((mc last-command-char))
555 (if (< mc 256)
556 (setq mc (unibyte-char-to-multibyte mc)))
557 (or mc (setq mc last-command-char))
558 (sgml-name-char mc))
559 (self-insert-command 1)))
560
561 (defun sgml-name-8bit-mode ()
562 "Toggle whether to insert named entities instead of non-ASCII characters.
563 This only works for Latin-1 input."
564 (interactive)
565 (setq sgml-name-8bit-mode (not sgml-name-8bit-mode))
566 (message "sgml name entity mode is now %s"
567 (if sgml-name-8bit-mode "ON" "OFF")))
568
569 ;; When an element of a skeleton is a string "str", it is passed
570 ;; through skeleton-transformation and inserted. If "str" is to be
571 ;; inserted literally, one should obtain it as the return value of a
572 ;; function, e.g. (identity "str").
573
574 (defvar sgml-tag-last nil)
575 (defvar sgml-tag-history nil)
576 (define-skeleton sgml-tag
577 "Prompt for a tag and insert it, optionally with attributes.
578 Completion and configuration are done according to `sgml-tag-alist'.
579 If you like tags and attributes in uppercase do \\[set-variable]
580 skeleton-transformation RET upcase RET, or put this in your `.emacs':
581 (setq sgml-transformation 'upcase)"
582 (funcall (or skeleton-transformation 'identity)
583 (setq sgml-tag-last
584 (completing-read
585 (if (> (length sgml-tag-last) 0)
586 (format "Tag (default %s): " sgml-tag-last)
587 "Tag: ")
588 sgml-tag-alist nil nil nil 'sgml-tag-history sgml-tag-last)))
589 ?< str |
590 (("") -1 '(undo-boundary) (identity "&lt;")) | ; see comment above
591 `(("") '(setq v2 (sgml-attributes ,str t)) ?>
592 (cond
593 ((string= "![" ,str)
594 (backward-char)
595 '(("") " [ " _ " ]]"))
596 ((and (eq v2 t) sgml-xml-mode (member ,str sgml-empty-tags))
597 '(("") -1 "/>"))
598 ((or (and (eq v2 t) (not sgml-xml-mode)) (string-match "^[/!?]" ,str))
599 nil)
600 ((symbolp v2)
601 ;; Make sure we don't fall into an infinite loop.
602 ;; For xhtml's `tr' tag, we should maybe use \n instead.
603 (if (eq v2 t) (setq v2 nil))
604 ;; We use `identity' to prevent skeleton from passing
605 ;; `str' through skeleton-transformation a second time.
606 '(("") v2 _ v2 "</" (identity ',str) ?>))
607 ((eq (car v2) t)
608 (cons '("") (cdr v2)))
609 (t
610 (append '(("") (car v2))
611 (cdr v2)
612 '(resume: (car v2) _ "</" (identity ',str) ?>))))))
613
614 (autoload 'skeleton-read "skeleton")
615
616 (defun sgml-attributes (tag &optional quiet)
617 "When at top level of a tag, interactively insert attributes.
618
619 Completion and configuration of TAG are done according to `sgml-tag-alist'.
620 If QUIET, do not print a message when there are no attributes for TAG."
621 (interactive (list (save-excursion (sgml-beginning-of-tag t))))
622 (or (stringp tag) (error "Wrong context for adding attribute"))
623 (if tag
624 (let ((completion-ignore-case t)
625 (alist (cdr (assoc (downcase tag) sgml-tag-alist)))
626 car attribute i)
627 (if (or (symbolp (car alist))
628 (symbolp (car (car alist))))
629 (setq car (car alist)
630 alist (cdr alist)))
631 (or quiet
632 (message "No attributes configured."))
633 (if (stringp (car alist))
634 (progn
635 (insert (if (eq (preceding-char) ? ) "" ? )
636 (funcall skeleton-transformation (car alist)))
637 (sgml-value alist))
638 (setq i (length alist))
639 (while (> i 0)
640 (insert ? )
641 (insert (funcall skeleton-transformation
642 (setq attribute
643 (skeleton-read '(completing-read
644 "Attribute: "
645 alist)))))
646 (if (string= "" attribute)
647 (setq i 0)
648 (sgml-value (assoc (downcase attribute) alist))
649 (setq i (1- i))))
650 (if (eq (preceding-char) ? )
651 (delete-backward-char 1)))
652 car)))
653
654 (defun sgml-auto-attributes (arg)
655 "Self insert the character typed; at top level of tag, prompt for attributes.
656 With prefix argument, only self insert."
657 (interactive "*P")
658 (let ((point (point))
659 tag)
660 (if (or arg
661 (not sgml-tag-alist) ; no message when nothing configured
662 (symbolp (setq tag (save-excursion (sgml-beginning-of-tag t))))
663 (eq (aref tag 0) ?/))
664 (self-insert-command (prefix-numeric-value arg))
665 (sgml-attributes tag)
666 (setq last-command-char ? )
667 (or (> (point) point)
668 (self-insert-command 1)))))
669
670 (defun sgml-tag-help (&optional tag)
671 "Display description of tag TAG. If TAG is omitted, use the tag at point."
672 (interactive)
673 (or tag
674 (save-excursion
675 (if (eq (following-char) ?<)
676 (forward-char))
677 (setq tag (sgml-beginning-of-tag))))
678 (or (stringp tag)
679 (error "No tag selected"))
680 (setq tag (downcase tag))
681 (message "%s"
682 (or (cdr (assoc (downcase tag) sgml-tag-help))
683 (and (eq (aref tag 0) ?/)
684 (cdr (assoc (downcase (substring tag 1)) sgml-tag-help)))
685 "No description available")))
686
687 (defun sgml-maybe-end-tag (&optional arg)
688 "Name self unless in position to end a tag or a prefix ARG is given."
689 (interactive "P")
690 (if (or arg (eq (car (sgml-lexical-context)) 'tag))
691 (self-insert-command (prefix-numeric-value arg))
692 (sgml-name-self)))
693
694 (defun sgml-skip-tag-backward (arg)
695 "Skip to beginning of tag or matching opening tag if present.
696 With prefix argument ARG, repeat this ARG times."
697 (interactive "p")
698 ;; FIXME: use sgml-get-context or something similar.
699 (while (>= arg 1)
700 (search-backward "<" nil t)
701 (if (looking-at "</\\([^ \n\t>]+\\)")
702 ;; end tag, skip any nested pairs
703 (let ((case-fold-search t)
704 (re (concat "</?" (regexp-quote (match-string 1))
705 ;; Ignore empty tags like <foo/>.
706 "\\([^>]*[^/>]\\)?>")))
707 (while (and (re-search-backward re nil t)
708 (eq (char-after (1+ (point))) ?/))
709 (forward-char 1)
710 (sgml-skip-tag-backward 1))))
711 (setq arg (1- arg))))
712
713 (defun sgml-skip-tag-forward (arg)
714 "Skip to end of tag or matching closing tag if present.
715 With prefix argument ARG, repeat this ARG times.
716 Return t iff after a closing tag."
717 (interactive "p")
718 ;; FIXME: Use sgml-get-context or something similar.
719 ;; It currently might jump to an unrelated </P> if the <P>
720 ;; we're skipping has no matching </P>.
721 (let ((return t))
722 (with-syntax-table sgml-tag-syntax-table
723 (while (>= arg 1)
724 (skip-chars-forward "^<>")
725 (if (eq (following-char) ?>)
726 (up-list -1))
727 (if (looking-at "<\\([^/ \n\t>]+\\)\\([^>]*[^/>]\\)?>")
728 ;; start tag, skip any nested same pairs _and_ closing tag
729 (let ((case-fold-search t)
730 (re (concat "</?" (regexp-quote (match-string 1))
731 ;; Ignore empty tags like <foo/>.
732 "\\([^>]*[^/>]\\)?>"))
733 point close)
734 (forward-list 1)
735 (setq point (point))
736 ;; FIXME: This re-search-forward will mistakenly match
737 ;; tag-like text inside attributes.
738 (while (and (re-search-forward re nil t)
739 (not (setq close
740 (eq (char-after (1+ (match-beginning 0))) ?/)))
741 (goto-char (match-beginning 0))
742 (sgml-skip-tag-forward 1))
743 (setq close nil))
744 (unless close
745 (goto-char point)
746 (setq return nil)))
747 (forward-list 1))
748 (setq arg (1- arg)))
749 return)))
750
751 (defun sgml-delete-tag (arg)
752 ;; FIXME: Should be called sgml-kill-tag or should not touch the kill-ring.
753 "Delete tag on or after cursor, and matching closing or opening tag.
754 With prefix argument ARG, repeat this ARG times."
755 (interactive "p")
756 (while (>= arg 1)
757 (save-excursion
758 (let* (close open)
759 (if (looking-at "[ \t\n]*<")
760 ;; just before tag
761 (if (eq (char-after (match-end 0)) ?/)
762 ;; closing tag
763 (progn
764 (setq close (point))
765 (goto-char (match-end 0))))
766 ;; on tag?
767 (or (save-excursion (setq close (sgml-beginning-of-tag)
768 close (and (stringp close)
769 (eq (aref close 0) ?/)
770 (point))))
771 ;; not on closing tag
772 (let ((point (point)))
773 (sgml-skip-tag-backward 1)
774 (if (or (not (eq (following-char) ?<))
775 (save-excursion
776 (forward-list 1)
777 (<= (point) point)))
778 (error "Not on or before tag")))))
779 (if close
780 (progn
781 (sgml-skip-tag-backward 1)
782 (setq open (point))
783 (goto-char close)
784 (kill-sexp 1))
785 (setq open (point))
786 (when (sgml-skip-tag-forward 1)
787 (kill-sexp -1)))
788 ;; Delete any resulting empty line. If we didn't kill-sexp,
789 ;; this *should* do nothing, because we're right after the tag.
790 (if (progn (forward-line 0) (looking-at "\\(?:[ \t]*$\\)\n?"))
791 (delete-region (match-beginning 0) (match-end 0)))
792 (goto-char open)
793 (kill-sexp 1)
794 (if (progn (forward-line 0) (looking-at "\\(?:[ \t]*$\\)\n?"))
795 (delete-region (match-beginning 0) (match-end 0)))))
796 (setq arg (1- arg))))
797
798 \f
799 ;; Put read-only last to enable setting this even when read-only enabled.
800 (or (get 'sgml-tag 'invisible)
801 (setplist 'sgml-tag
802 (append '(invisible t
803 point-entered sgml-point-entered
804 rear-nonsticky t
805 read-only t)
806 (symbol-plist 'sgml-tag))))
807
808 (defun sgml-tags-invisible (arg)
809 "Toggle visibility of existing tags."
810 (interactive "P")
811 (let ((modified (buffer-modified-p))
812 (inhibit-read-only t)
813 (inhibit-modification-hooks t)
814 ;; Avoid spurious the `file-locked' checks.
815 (buffer-file-name nil)
816 ;; This is needed in case font lock gets called,
817 ;; since it moves point and might call sgml-point-entered.
818 ;; How could it get called? -stef
819 (inhibit-point-motion-hooks t)
820 string)
821 (unwind-protect
822 (save-excursion
823 (goto-char (point-min))
824 (if (set (make-local-variable 'sgml-tags-invisible)
825 (if arg
826 (>= (prefix-numeric-value arg) 0)
827 (not sgml-tags-invisible)))
828 (while (re-search-forward sgml-tag-name-re nil t)
829 (setq string
830 (cdr (assq (intern-soft (downcase (match-string 1)))
831 sgml-display-text)))
832 (goto-char (match-beginning 0))
833 (and (stringp string)
834 (not (overlays-at (point)))
835 (let ((ol (make-overlay (point) (match-beginning 1))))
836 (overlay-put ol 'before-string string)
837 (overlay-put ol 'sgml-tag t)))
838 (put-text-property (point)
839 (progn (forward-list) (point))
840 'category 'sgml-tag))
841 (let ((pos (point-min)))
842 (while (< (setq pos (next-overlay-change pos)) (point-max))
843 (dolist (ol (overlays-at pos))
844 (if (overlay-get ol 'sgml-tag)
845 (delete-overlay ol)))))
846 (remove-text-properties (point-min) (point-max) '(category nil))))
847 (restore-buffer-modified-p modified))
848 (run-hooks 'sgml-tags-invisible-hook)
849 (message "")))
850
851 (defun sgml-point-entered (x y)
852 ;; Show preceding or following hidden tag, depending of cursor direction.
853 (let ((inhibit-point-motion-hooks t))
854 (save-excursion
855 (message "Invisible tag: %s"
856 ;; Strip properties, otherwise, the text is invisible.
857 (buffer-substring-no-properties
858 (point)
859 (if (or (and (> x y)
860 (not (eq (following-char) ?<)))
861 (and (< x y)
862 (eq (preceding-char) ?>)))
863 (backward-list)
864 (forward-list)))))))
865
866 \f
867 (autoload 'compile-internal "compile")
868
869 (defun sgml-validate (command)
870 "Validate an SGML document.
871 Runs COMMAND, a shell command, in a separate process asynchronously
872 with output going to the buffer `*compilation*'.
873 You can then use the command \\[next-error] to find the next error message
874 and move to the line in the SGML document that caused it."
875 (interactive
876 (list (read-string "Validate command: "
877 (or sgml-saved-validate-command
878 (concat sgml-validate-command
879 " "
880 (let ((name (buffer-file-name)))
881 (and name
882 (file-name-nondirectory name))))))))
883 (setq sgml-saved-validate-command command)
884 (save-some-buffers (not compilation-ask-about-save) nil)
885 (compile-internal command "No more errors"))
886
887 (defsubst sgml-at-indentation-p ()
888 "Return true if point is at the first non-whitespace character on the line."
889 (save-excursion
890 (skip-chars-backward " \t")
891 (bolp)))
892
893 (defun sgml-lexical-context (&optional limit)
894 "Return the lexical context at point as (TYPE . START).
895 START is the location of the start of the lexical element.
896 TYPE is one of `string', `comment', `tag', `cdata', or `text'.
897
898 Optional argument LIMIT is the position to start parsing from.
899 If nil, start from a preceding tag at indentation."
900 (save-excursion
901 (let ((pos (point))
902 text-start state)
903 (if limit
904 (goto-char limit)
905 ;; Skip tags backwards until we find one at indentation
906 (while (and (ignore-errors (sgml-parse-tag-backward))
907 (not (sgml-at-indentation-p)))))
908 (with-syntax-table sgml-tag-syntax-table
909 (while (< (point) pos)
910 ;; When entering this loop we're inside text.
911 (setq text-start (point))
912 (skip-chars-forward "^<" pos)
913 (setq state
914 (cond
915 ((= (point) pos)
916 ;; We got to the end without seeing a tag.
917 nil)
918 ((looking-at "<!\\[[A-Z]+\\[")
919 ;; We've found a CDATA section or similar.
920 (let ((cdata-start (point)))
921 (unless (search-forward "]]>" pos 'move)
922 (list 0 nil nil 'cdata nil nil nil nil cdata-start))))
923 (t
924 ;; We've reached a tag. Parse it.
925 ;; FIXME: Handle net-enabling start-tags
926 (parse-partial-sexp (point) pos 0))))))
927 (cond
928 ((eq (nth 3 state) 'cdata) (cons 'cdata (nth 8 state)))
929 ((nth 3 state) (cons 'string (nth 8 state)))
930 ((nth 4 state) (cons 'comment (nth 8 state)))
931 ((and state (> (nth 0 state) 0)) (cons 'tag (nth 1 state)))
932 (t (cons 'text text-start))))))
933
934 (defun sgml-beginning-of-tag (&optional top-level)
935 "Skip to beginning of tag and return its name.
936 If this can't be done, return nil."
937 (let ((context (sgml-lexical-context)))
938 (if (eq (car context) 'tag)
939 (progn
940 (goto-char (cdr context))
941 (when (looking-at sgml-tag-name-re)
942 (match-string-no-properties 1)))
943 (if top-level nil
944 (when (not (eq (car context) 'text))
945 (goto-char (cdr context))
946 (sgml-beginning-of-tag t))))))
947
948 (defun sgml-value (alist)
949 "Interactively insert value taken from attribute-rule ALIST.
950 See `sgml-tag-alist' for info about attribute rules."
951 (setq alist (cdr alist))
952 (if (stringp (car alist))
953 (insert "=\"" (car alist) ?\")
954 (if (and (eq (car alist) t) (not sgml-xml-mode))
955 (when (cdr alist)
956 (insert "=\"")
957 (setq alist (skeleton-read '(completing-read "Value: " (cdr alist))))
958 (if (string< "" alist)
959 (insert alist ?\")
960 (delete-backward-char 2)))
961 (insert "=\"")
962 (when alist
963 (insert (skeleton-read '(completing-read "Value: " alist))))
964 (insert ?\"))))
965
966 (defun sgml-quote (start end &optional unquotep)
967 "Quote SGML text in region START ... END.
968 Only &, < and > are quoted, the rest is left untouched.
969 With prefix argument UNQUOTEP, unquote the region."
970 (interactive "r\nP")
971 (save-restriction
972 (narrow-to-region start end)
973 (goto-char (point-min))
974 (if unquotep
975 ;; FIXME: We should unquote other named character references as well.
976 (while (re-search-forward
977 "\\(&\\(amp\\|\\(l\\|\\(g\\)\\)t\\)\\)[][<>&;\n\t \"%!'(),/=?]"
978 nil t)
979 (replace-match (if (match-end 4) ">" (if (match-end 3) "<" "&")) t t
980 nil (if (eq (char-before (match-end 0)) ?\;) 0 1)))
981 (while (re-search-forward "[&<>]" nil t)
982 (replace-match (cdr (assq (char-before) '((?& . "&amp;")
983 (?< . "&lt;")
984 (?> . "&gt;"))))
985 t t)))))
986
987 (defun sgml-pretty-print (beg end)
988 "Simple-minded pretty printer for SGML.
989 Re-indents the code and inserts newlines between BEG and END.
990 You might want to turn on `auto-fill-mode' to get better results."
991 ;; TODO:
992 ;; - insert newline between some start-tag and text.
993 ;; - don't insert newline in front of some end-tags.
994 (interactive "r")
995 (save-excursion
996 (if (< beg end)
997 (goto-char beg)
998 (goto-char end)
999 (setq end beg)
1000 (setq beg (point)))
1001 ;; Don't use narrowing because it screws up auto-indent.
1002 (setq end (copy-marker end t))
1003 (with-syntax-table sgml-tag-syntax-table
1004 (while (re-search-forward "<" end t)
1005 (goto-char (match-beginning 0))
1006 (unless (or ;;(looking-at "</")
1007 (progn (skip-chars-backward " \t") (bolp)))
1008 (reindent-then-newline-and-indent))
1009 (forward-sexp 1)))
1010 ;; (indent-region beg end)
1011 ))
1012
1013 \f
1014 ;; Parsing
1015
1016 (defstruct (sgml-tag
1017 (:constructor sgml-make-tag (type start end name)))
1018 type start end name)
1019
1020 (defsubst sgml-parse-tag-name ()
1021 "Skip past a tag-name, and return the name."
1022 (buffer-substring-no-properties
1023 (point) (progn (skip-syntax-forward "w_") (point))))
1024
1025 (defsubst sgml-looking-back-at (str)
1026 "Return t if the test before point matches STR."
1027 (let ((start (- (point) (length str))))
1028 (and (>= start (point-min))
1029 (equal str (buffer-substring-no-properties start (point))))))
1030
1031 (defun sgml-parse-tag-backward (&optional limit)
1032 "Parse an SGML tag backward, and return information about the tag.
1033 Assume that parsing starts from within a textual context.
1034 Leave point at the beginning of the tag."
1035 (let (tag-type tag-start tag-end name)
1036 (or (search-backward ">" limit 'move)
1037 (error "No tag found"))
1038 (setq tag-end (1+ (point)))
1039 (cond
1040 ((sgml-looking-back-at "--") ; comment
1041 (setq tag-type 'comment
1042 tag-start (search-backward "<!--" nil t)))
1043 ((sgml-looking-back-at "]]") ; cdata
1044 (setq tag-type 'cdata
1045 tag-start (re-search-backward "<!\\[[A-Z]+\\[" nil t)))
1046 (t
1047 (setq tag-start
1048 (with-syntax-table sgml-tag-syntax-table
1049 (goto-char tag-end)
1050 (backward-sexp)
1051 (point)))
1052 (goto-char (1+ tag-start))
1053 (case (char-after)
1054 (?! ; declaration
1055 (setq tag-type 'decl))
1056 (?? ; processing-instruction
1057 (setq tag-type 'pi))
1058 (?/ ; close-tag
1059 (forward-char 1)
1060 (setq tag-type 'close
1061 name (sgml-parse-tag-name)))
1062 (?% ; JSP tags
1063 (setq tag-type 'jsp))
1064 (t ; open or empty tag
1065 (setq tag-type 'open
1066 name (sgml-parse-tag-name))
1067 (if (or (eq ?/ (char-before (- tag-end 1)))
1068 (sgml-empty-tag-p name))
1069 (setq tag-type 'empty))))))
1070 (goto-char tag-start)
1071 (sgml-make-tag tag-type tag-start tag-end name)))
1072
1073 (defun sgml-get-context (&optional full)
1074 "Determine the context of the current position.
1075 If FULL is `empty', return even if the context is empty (i.e.
1076 we just skipped over some element and got to a beginning of line).
1077 If FULL is non-nil, parse back to the beginning of the buffer, otherwise
1078 parse until we find a start-tag as the first thing on a line.
1079
1080 The context is a list of tag-info structures. The last one is the tag
1081 immediately enclosing the current position."
1082 (let ((here (point))
1083 (ignore nil)
1084 (context nil)
1085 tag-info)
1086 ;; CONTEXT keeps track of the tag-stack
1087 ;; IGNORE keeps track of the nesting level of point relative to the
1088 ;; first (outermost) tag on the context. This is the list of
1089 ;; enclosing start-tags we'll have to ignore.
1090 (skip-chars-backward " \t\n") ; Make sure we're not at indentation.
1091 (while
1092 (and (or ignore
1093 (not (if full (eq full 'empty) context))
1094 (not (sgml-at-indentation-p))
1095 (and context
1096 (/= (point) (sgml-tag-start (car context)))
1097 (sgml-unclosed-tag-p (sgml-tag-name (car context)))))
1098 (setq tag-info (ignore-errors (sgml-parse-tag-backward))))
1099
1100 ;; This tag may enclose things we thought were tags. If so,
1101 ;; discard them.
1102 (while (and context
1103 (> (sgml-tag-end tag-info)
1104 (sgml-tag-end (car context))))
1105 (setq context (cdr context)))
1106
1107 (cond
1108
1109 ;; start-tag
1110 ((eq (sgml-tag-type tag-info) 'open)
1111 (cond
1112 ((null ignore)
1113 (if (and context
1114 (sgml-unclosed-tag-p (sgml-tag-name tag-info))
1115 (eq t (compare-strings
1116 (sgml-tag-name tag-info) nil nil
1117 (sgml-tag-name (car context)) nil nil t)))
1118 ;; There was an implicit end-tag.
1119 nil
1120 (push tag-info context)))
1121 ((eq t (compare-strings (sgml-tag-name tag-info) nil nil
1122 (car ignore) nil nil t))
1123 (setq ignore (cdr ignore)))
1124 (t
1125 ;; The open and close tags don't match.
1126 (if (not sgml-xml-mode)
1127 (unless (sgml-unclosed-tag-p (sgml-tag-name tag-info))
1128 (message "Unclosed tag <%s>" (sgml-tag-name tag-info))
1129 (let ((tmp ignore))
1130 ;; We could just assume that the tag is simply not closed
1131 ;; but it's a bad assumption when tags *are* closed but
1132 ;; not properly nested.
1133 (while (and (cdr tmp)
1134 (not (eq t (compare-strings
1135 (sgml-tag-name tag-info) nil nil
1136 (cadr tmp) nil nil t))))
1137 (setq tmp (cdr tmp)))
1138 (if (cdr tmp) (setcdr tmp (cddr tmp)))))
1139 (message "Unmatched tags <%s> and </%s>"
1140 (sgml-tag-name tag-info) (pop ignore))))))
1141
1142 ;; end-tag
1143 ((eq (sgml-tag-type tag-info) 'close)
1144 (if (sgml-empty-tag-p (sgml-tag-name tag-info))
1145 (message "Spurious </%s>: empty tag" (sgml-tag-name tag-info))
1146 (push (sgml-tag-name tag-info) ignore)))
1147 ))
1148
1149 ;; return context
1150 context))
1151
1152 (defun sgml-show-context (&optional full)
1153 "Display the current context.
1154 If FULL is non-nil, parse back to the beginning of the buffer."
1155 (interactive "P")
1156 (with-output-to-temp-buffer "*XML Context*"
1157 (save-excursion
1158 (let ((context (sgml-get-context)))
1159 (when full
1160 (let ((more nil))
1161 (while (setq more (sgml-get-context))
1162 (setq context (nconc more context)))))
1163 (pp context)))))
1164
1165 \f
1166 ;; Editing shortcuts
1167
1168 (defun sgml-close-tag ()
1169 "Close current element.
1170 Depending on context, inserts a matching close-tag, or closes
1171 the current start-tag or the current comment or the current cdata, ..."
1172 (interactive)
1173 (case (car (sgml-lexical-context))
1174 (comment (insert " -->"))
1175 (cdata (insert "]]>"))
1176 (pi (insert " ?>"))
1177 (jsp (insert " %>"))
1178 (tag (insert " />"))
1179 (text
1180 (let ((context (save-excursion (sgml-get-context))))
1181 (if context
1182 (progn
1183 (insert "</" (sgml-tag-name (car (last context))) ">")
1184 (indent-according-to-mode)))))
1185 (otherwise
1186 (error "Nothing to close"))))
1187
1188 (defun sgml-empty-tag-p (tag-name)
1189 "Return non-nil if TAG-NAME is an implicitly empty tag."
1190 (and (not sgml-xml-mode)
1191 (member-ignore-case tag-name sgml-empty-tags)))
1192
1193 (defun sgml-unclosed-tag-p (tag-name)
1194 "Return non-nil if TAG-NAME is a tag for which an end-tag is optional."
1195 (and (not sgml-xml-mode)
1196 (member-ignore-case tag-name sgml-unclosed-tags)))
1197
1198 (defun sgml-calculate-indent ()
1199 "Calculate the column to which this line should be indented."
1200 (let ((lcon (sgml-lexical-context)))
1201
1202 ;; Indent comment-start markers inside <!-- just like comment-end markers.
1203 (if (and (eq (car lcon) 'tag)
1204 (looking-at "--")
1205 (save-excursion (goto-char (cdr lcon)) (looking-at "<!--")))
1206 (setq lcon (cons 'comment (+ (cdr lcon) 2))))
1207
1208 (case (car lcon)
1209
1210 (string
1211 ;; Go back to previous non-empty line.
1212 (while (and (> (point) (cdr lcon))
1213 (zerop (forward-line -1))
1214 (looking-at "[ \t]*$")))
1215 (if (> (point) (cdr lcon))
1216 ;; Previous line is inside the string.
1217 (current-indentation)
1218 (goto-char (cdr lcon))
1219 (1+ (current-column))))
1220
1221 (comment
1222 (let ((mark (looking-at "--")))
1223 ;; Go back to previous non-empty line.
1224 (while (and (> (point) (cdr lcon))
1225 (zerop (forward-line -1))
1226 (or (looking-at "[ \t]*$")
1227 (if mark (not (looking-at "[ \t]*--"))))))
1228 (if (> (point) (cdr lcon))
1229 ;; Previous line is inside the comment.
1230 (skip-chars-forward " \t")
1231 (goto-char (cdr lcon)))
1232 (when (and (not mark) (looking-at "--"))
1233 (forward-char 2) (skip-chars-forward " \t"))
1234 (current-column)))
1235
1236 (cdata
1237 (current-column))
1238
1239 (tag
1240 (goto-char (1+ (cdr lcon)))
1241 (skip-chars-forward "^ \t\n") ;Skip tag name.
1242 (skip-chars-forward " \t")
1243 (if (not (eolp))
1244 (current-column)
1245 ;; This is the first attribute: indent.
1246 (goto-char (1+ (cdr lcon)))
1247 (+ (current-column) sgml-basic-offset)))
1248
1249 (text
1250 (while (looking-at "</")
1251 (forward-sexp 1)
1252 (skip-chars-forward " \t"))
1253 (let* ((here (point))
1254 (unclosed (and ;; (not sgml-xml-mode)
1255 (looking-at sgml-tag-name-re)
1256 (member-ignore-case (match-string 1)
1257 sgml-unclosed-tags)
1258 (match-string 1)))
1259 (context
1260 ;; If possible, align on the previous non-empty text line.
1261 ;; Otherwise, do a more serious parsing to find the
1262 ;; tag(s) relative to which we should be indenting.
1263 (if (and (not unclosed) (skip-chars-backward " \t")
1264 (< (skip-chars-backward " \t\n") 0)
1265 (back-to-indentation)
1266 (> (point) (cdr lcon)))
1267 nil
1268 (goto-char here)
1269 (nreverse (sgml-get-context (if unclosed nil 'empty)))))
1270 (there (point)))
1271 ;; Ignore previous unclosed start-tag in context.
1272 (while (and context unclosed
1273 (eq t (compare-strings
1274 (sgml-tag-name (car context)) nil nil
1275 unclosed nil nil t)))
1276 (setq context (cdr context)))
1277 ;; Indent to reflect nesting.
1278 (if (and context
1279 (goto-char (sgml-tag-end (car context)))
1280 (skip-chars-forward " \t\n")
1281 (< (point) here) (sgml-at-indentation-p))
1282 (current-column)
1283 (goto-char there)
1284 (+ (current-column)
1285 (* sgml-basic-offset (length context))))))
1286
1287 (otherwise
1288 (error "Unrecognised context %s" (car lcon)))
1289
1290 )))
1291
1292 (defun sgml-indent-line ()
1293 "Indent the current line as SGML."
1294 (interactive)
1295 (let* ((savep (point))
1296 (indent-col
1297 (save-excursion
1298 (back-to-indentation)
1299 (if (>= (point) savep) (setq savep nil))
1300 (sgml-calculate-indent))))
1301 (if savep
1302 (save-excursion (indent-line-to indent-col))
1303 (indent-line-to indent-col))))
1304
1305 (defun sgml-guess-indent ()
1306 "Guess an appropriate value for `sgml-basic-offset'.
1307 Base the guessed identation level on the first indented tag in the buffer.
1308 Add this to `sgml-mode-hook' for convenience."
1309 (interactive)
1310 (save-excursion
1311 (goto-char (point-min))
1312 (if (re-search-forward "^\\([ \t]+\\)<" 500 'noerror)
1313 (progn
1314 (set (make-local-variable 'sgml-basic-offset)
1315 (1- (current-column)))
1316 (message "Guessed sgml-basic-offset = %d"
1317 sgml-basic-offset)
1318 ))))
1319
1320 (defun sgml-parse-dtd ()
1321 "Simplistic parse of the current buffer as a DTD.
1322 Currently just returns (EMPTY-TAGS UNCLOSED-TAGS)."
1323 (goto-char (point-min))
1324 (let ((empty nil)
1325 (unclosed nil))
1326 (while (re-search-forward "<!ELEMENT[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+[-O][ \t\n]+\\([-O]\\)[ \t\n]+\\([^ \t\n]+\\)" nil t)
1327 (cond
1328 ((string= (match-string 3) "EMPTY")
1329 (push (match-string-no-properties 1) empty))
1330 ((string= (match-string 2) "O")
1331 (push (match-string-no-properties 1) unclosed))))
1332 (setq empty (sort (mapcar 'downcase empty) 'string<))
1333 (setq unclosed (sort (mapcar 'downcase unclosed) 'string<))
1334 (list empty unclosed)))
1335
1336 ;;; HTML mode
1337
1338 (defcustom html-mode-hook nil
1339 "Hook run by command `html-mode'.
1340 `text-mode-hook' and `sgml-mode-hook' are run first."
1341 :group 'sgml
1342 :type 'hook
1343 :options '(html-autoview-mode))
1344
1345 (defvar html-quick-keys sgml-quick-keys
1346 "Use C-c X combinations for quick insertion of frequent tags when non-nil.
1347 This defaults to `sgml-quick-keys'.
1348 This takes effect when first loading the library.")
1349
1350 (defvar html-mode-map
1351 (let ((map (make-sparse-keymap))
1352 (menu-map (make-sparse-keymap "HTML")))
1353 (set-keymap-parent map sgml-mode-map)
1354 (define-key map "\C-c6" 'html-headline-6)
1355 (define-key map "\C-c5" 'html-headline-5)
1356 (define-key map "\C-c4" 'html-headline-4)
1357 (define-key map "\C-c3" 'html-headline-3)
1358 (define-key map "\C-c2" 'html-headline-2)
1359 (define-key map "\C-c1" 'html-headline-1)
1360 (define-key map "\C-c\r" 'html-paragraph)
1361 (define-key map "\C-c\n" 'html-line)
1362 (define-key map "\C-c\C-c-" 'html-horizontal-rule)
1363 (define-key map "\C-c\C-co" 'html-ordered-list)
1364 (define-key map "\C-c\C-cu" 'html-unordered-list)
1365 (define-key map "\C-c\C-cr" 'html-radio-buttons)
1366 (define-key map "\C-c\C-cc" 'html-checkboxes)
1367 (define-key map "\C-c\C-cl" 'html-list-item)
1368 (define-key map "\C-c\C-ch" 'html-href-anchor)
1369 (define-key map "\C-c\C-cn" 'html-name-anchor)
1370 (define-key map "\C-c\C-ci" 'html-image)
1371 (when html-quick-keys
1372 (define-key map "\C-c-" 'html-horizontal-rule)
1373 (define-key map "\C-co" 'html-ordered-list)
1374 (define-key map "\C-cu" 'html-unordered-list)
1375 (define-key map "\C-cr" 'html-radio-buttons)
1376 (define-key map "\C-cc" 'html-checkboxes)
1377 (define-key map "\C-cl" 'html-list-item)
1378 (define-key map "\C-ch" 'html-href-anchor)
1379 (define-key map "\C-cn" 'html-name-anchor)
1380 (define-key map "\C-ci" 'html-image))
1381 (define-key map "\C-c\C-s" 'html-autoview-mode)
1382 (define-key map "\C-c\C-v" 'browse-url-of-buffer)
1383 (define-key map [menu-bar html] (cons "HTML" menu-map))
1384 (define-key menu-map [html-autoview-mode]
1385 '("Toggle Autoviewing" . html-autoview-mode))
1386 (define-key menu-map [browse-url-of-buffer]
1387 '("View Buffer Contents" . browse-url-of-buffer))
1388 (define-key menu-map [nil] '("--"))
1389 ;;(define-key menu-map "6" '("Heading 6" . html-headline-6))
1390 ;;(define-key menu-map "5" '("Heading 5" . html-headline-5))
1391 ;;(define-key menu-map "4" '("Heading 4" . html-headline-4))
1392 (define-key menu-map "3" '("Heading 3" . html-headline-3))
1393 (define-key menu-map "2" '("Heading 2" . html-headline-2))
1394 (define-key menu-map "1" '("Heading 1" . html-headline-1))
1395 (define-key menu-map "l" '("Radio Buttons" . html-radio-buttons))
1396 (define-key menu-map "c" '("Checkboxes" . html-checkboxes))
1397 (define-key menu-map "l" '("List Item" . html-list-item))
1398 (define-key menu-map "u" '("Unordered List" . html-unordered-list))
1399 (define-key menu-map "o" '("Ordered List" . html-ordered-list))
1400 (define-key menu-map "-" '("Horizontal Rule" . html-horizontal-rule))
1401 (define-key menu-map "\n" '("Line Break" . html-line))
1402 (define-key menu-map "\r" '("Paragraph" . html-paragraph))
1403 (define-key menu-map "i" '("Image" . html-image))
1404 (define-key menu-map "h" '("Href Anchor" . html-href-anchor))
1405 (define-key menu-map "n" '("Name Anchor" . html-name-anchor))
1406 map)
1407 "Keymap for commands for use in HTML mode.")
1408
1409 (defvar html-face-tag-alist
1410 '((bold . "b")
1411 (italic . "i")
1412 (underline . "u")
1413 (modeline . "rev"))
1414 "Value of `sgml-face-tag-alist' for HTML mode.")
1415
1416 (defvar html-tag-face-alist
1417 '(("b" . bold)
1418 ("big" . bold)
1419 ("blink" . highlight)
1420 ("cite" . italic)
1421 ("em" . italic)
1422 ("h1" bold underline)
1423 ("h2" bold-italic underline)
1424 ("h3" italic underline)
1425 ("h4" . underline)
1426 ("h5" . underline)
1427 ("h6" . underline)
1428 ("i" . italic)
1429 ("rev" . modeline)
1430 ("s" . underline)
1431 ("small" . default)
1432 ("strong" . bold)
1433 ("title" bold underline)
1434 ("tt" . default)
1435 ("u" . underline)
1436 ("var" . italic))
1437 "Value of `sgml-tag-face-alist' for HTML mode.")
1438
1439 (defvar html-display-text
1440 '((img . "[/]")
1441 (hr . "----------")
1442 (li . "o "))
1443 "Value of `sgml-display-text' for HTML mode.")
1444
1445 \f
1446 ;; should code exactly HTML 3 here when that is finished
1447 (defvar html-tag-alist
1448 (let* ((1-7 '(("1") ("2") ("3") ("4") ("5") ("6") ("7")))
1449 (1-9 `(,@1-7 ("8") ("9")))
1450 (align '(("align" ("left") ("center") ("right"))))
1451 (valign '(("top") ("middle") ("bottom") ("baseline")))
1452 (rel '(("next") ("previous") ("parent") ("subdocument") ("made")))
1453 (href '("href" ("ftp:") ("file:") ("finger:") ("gopher:") ("http:")
1454 ("mailto:") ("news:") ("rlogin:") ("telnet:") ("tn3270:")
1455 ("wais:") ("/cgi-bin/")))
1456 (name '("name"))
1457 (link `(,href
1458 ("rel" ,@rel)
1459 ("rev" ,@rel)
1460 ("title")))
1461 (list '((nil \n ("List item: " "<li>" str
1462 (if sgml-xml-mode "</li>") \n))))
1463 (cell `(t
1464 ,@align
1465 ("valign" ,@valign)
1466 ("colspan" ,@1-9)
1467 ("rowspan" ,@1-9)
1468 ("nowrap" t))))
1469 ;; put ,-expressions first, else byte-compile chokes (as of V19.29)
1470 ;; and like this it's more efficient anyway
1471 `(("a" ,name ,@link)
1472 ("base" t ,@href)
1473 ("dir" ,@list)
1474 ("font" nil "size" ("-1") ("+1") ("-2") ("+2") ,@1-7)
1475 ("form" (\n _ \n "<input type=\"submit\" value=\"\""
1476 (if sgml-xml-mode "/>" ">"))
1477 ("action" ,@(cdr href)) ("method" ("get") ("post")))
1478 ("h1" ,@align)
1479 ("h2" ,@align)
1480 ("h3" ,@align)
1481 ("h4" ,@align)
1482 ("h5" ,@align)
1483 ("h6" ,@align)
1484 ("hr" t ("size" ,@1-9) ("width") ("noshade" t) ,@align)
1485 ("img" t ("align" ,@valign ("texttop") ("absmiddle") ("absbottom"))
1486 ("src") ("alt") ("width" "1") ("height" "1")
1487 ("border" "1") ("vspace" "1") ("hspace" "1") ("ismap" t))
1488 ("input" t ("size" ,@1-9) ("maxlength" ,@1-9) ("checked" t) ,name
1489 ("type" ("text") ("password") ("checkbox") ("radio")
1490 ("submit") ("reset"))
1491 ("value"))
1492 ("link" t ,@link)
1493 ("menu" ,@list)
1494 ("ol" ,@list ("type" ("A") ("a") ("I") ("i") ("1")))
1495 ("p" t ,@align)
1496 ("select" (nil \n
1497 ("Text: "
1498 "<option>" str (if sgml-xml-mode "</option>") \n))
1499 ,name ("size" ,@1-9) ("multiple" t))
1500 ("table" (nil \n
1501 ((completing-read "Cell kind: " '(("td") ("th"))
1502 nil t "t")
1503 "<tr><" str ?> _
1504 (if sgml-xml-mode (concat "<" str "></tr>")) \n))
1505 ("border" t ,@1-9) ("width" "10") ("cellpadding"))
1506 ("td" ,@cell)
1507 ("textarea" ,name ("rows" ,@1-9) ("cols" ,@1-9))
1508 ("th" ,@cell)
1509 ("ul" ,@list ("type" ("disc") ("circle") ("square")))
1510
1511 ,@sgml-tag-alist
1512
1513 ("abbrev")
1514 ("acronym")
1515 ("address")
1516 ("array" (nil \n
1517 ("Item: " "<item>" str (if sgml-xml-mode "</item>") \n))
1518 "align")
1519 ("au")
1520 ("b")
1521 ("big")
1522 ("blink")
1523 ("blockquote" \n)
1524 ("body" \n ("background" ".gif") ("bgcolor" "#") ("text" "#")
1525 ("link" "#") ("alink" "#") ("vlink" "#"))
1526 ("box" (nil _ "<over>" _ (if sgml-xml-mode "</over>")))
1527 ("br" t ("clear" ("left") ("right")))
1528 ("caption" ("valign" ("top") ("bottom")))
1529 ("center" \n)
1530 ("cite")
1531 ("code" \n)
1532 ("dd" ,(not sgml-xml-mode))
1533 ("del")
1534 ("dfn")
1535 ("div")
1536 ("dl" (nil \n
1537 ( "Term: "
1538 "<dt>" str (if sgml-xml-mode "</dt>")
1539 "<dd>" _ (if sgml-xml-mode "</dd>") \n)))
1540 ("dt" (t _ (if sgml-xml-mode "</dt>")
1541 "<dd>" (if sgml-xml-mode "</dd>") \n))
1542 ("em")
1543 ;("fn" "id" "fn") ; ???
1544 ("head" \n)
1545 ("html" (\n
1546 "<head>\n"
1547 "<title>" (setq str (read-input "Title: ")) "</title>\n"
1548 "</head>\n"
1549 "<body>\n<h1>" str "</h1>\n" _
1550 "\n<address>\n<a href=\"mailto:"
1551 user-mail-address
1552 "\">" (user-full-name) "</a>\n</address>\n"
1553 "</body>"
1554 ))
1555 ("i")
1556 ("ins")
1557 ("isindex" t ("action") ("prompt"))
1558 ("kbd")
1559 ("lang")
1560 ("li" ,(not sgml-xml-mode))
1561 ("math" \n)
1562 ("nobr")
1563 ("option" t ("value") ("label") ("selected" t))
1564 ("over" t)
1565 ("person")
1566 ("pre" \n)
1567 ("q")
1568 ("rev")
1569 ("s")
1570 ("samp")
1571 ("small")
1572 ("span" nil
1573 ("class"
1574 ("builtin")
1575 ("comment")
1576 ("constant")
1577 ("function-name")
1578 ("keyword")
1579 ("string")
1580 ("type")
1581 ("variable-name")
1582 ("warning")))
1583 ("strong")
1584 ("sub")
1585 ("sup")
1586 ("title")
1587 ("tr" t)
1588 ("tt")
1589 ("u")
1590 ("var")
1591 ("wbr" t)))
1592 "*Value of `sgml-tag-alist' for HTML mode.")
1593
1594 (defvar html-tag-help
1595 `(,@sgml-tag-help
1596 ("a" . "Anchor of point or link elsewhere")
1597 ("abbrev" . "?")
1598 ("acronym" . "?")
1599 ("address" . "Formatted mail address")
1600 ("array" . "Math array")
1601 ("au" . "?")
1602 ("b" . "Bold face")
1603 ("base" . "Base address for URLs")
1604 ("big" . "Font size")
1605 ("blink" . "Blinking text")
1606 ("blockquote" . "Indented quotation")
1607 ("body" . "Document body")
1608 ("box" . "Math fraction")
1609 ("br" . "Line break")
1610 ("caption" . "Table caption")
1611 ("center" . "Centered text")
1612 ("changed" . "Change bars")
1613 ("cite" . "Citation of a document")
1614 ("code" . "Formatted source code")
1615 ("dd" . "Definition of term")
1616 ("del" . "?")
1617 ("dfn" . "?")
1618 ("dir" . "Directory list (obsolete)")
1619 ("dl" . "Definition list")
1620 ("dt" . "Term to be definined")
1621 ("em" . "Emphasised")
1622 ("embed" . "Embedded data in foreign format")
1623 ("fig" . "Figure")
1624 ("figa" . "Figure anchor")
1625 ("figd" . "Figure description")
1626 ("figt" . "Figure text")
1627 ;("fn" . "?") ; ???
1628 ("font" . "Font size")
1629 ("form" . "Form with input fields")
1630 ("group" . "Document grouping")
1631 ("h1" . "Most important section headline")
1632 ("h2" . "Important section headline")
1633 ("h3" . "Section headline")
1634 ("h4" . "Minor section headline")
1635 ("h5" . "Unimportant section headline")
1636 ("h6" . "Least important section headline")
1637 ("head" . "Document header")
1638 ("hr" . "Horizontal rule")
1639 ("html" . "HTML Document")
1640 ("i" . "Italic face")
1641 ("img" . "Graphic image")
1642 ("input" . "Form input field")
1643 ("ins" . "?")
1644 ("isindex" . "Input field for index search")
1645 ("kbd" . "Keybard example face")
1646 ("lang" . "Natural language")
1647 ("li" . "List item")
1648 ("link" . "Link relationship")
1649 ("math" . "Math formula")
1650 ("menu" . "Menu list (obsolete)")
1651 ("mh" . "Form mail header")
1652 ("nextid" . "Allocate new id")
1653 ("nobr" . "Text without line break")
1654 ("ol" . "Ordered list")
1655 ("option" . "Selection list item")
1656 ("over" . "Math fraction rule")
1657 ("p" . "Paragraph start")
1658 ("panel" . "Floating panel")
1659 ("person" . "?")
1660 ("pre" . "Preformatted fixed width text")
1661 ("q" . "?")
1662 ("rev" . "Reverse video")
1663 ("s" . "?")
1664 ("samp" . "Sample text")
1665 ("select" . "Selection list")
1666 ("small" . "Font size")
1667 ("sp" . "Nobreak space")
1668 ("strong" . "Standout text")
1669 ("sub" . "Subscript")
1670 ("sup" . "Superscript")
1671 ("table" . "Table with rows and columns")
1672 ("tb" . "Table vertical break")
1673 ("td" . "Table data cell")
1674 ("textarea" . "Form multiline edit area")
1675 ("th" . "Table header cell")
1676 ("title" . "Document title")
1677 ("tr" . "Table row separator")
1678 ("tt" . "Typewriter face")
1679 ("u" . "Underlined text")
1680 ("ul" . "Unordered list")
1681 ("var" . "Math variable face")
1682 ("wbr" . "Enable <br> within <nobr>"))
1683 "*Value of `sgml-tag-help' for HTML mode.")
1684
1685 \f
1686 ;;;###autoload
1687 (define-derived-mode html-mode sgml-mode "HTML"
1688 "Major mode based on SGML mode for editing HTML documents.
1689 This allows inserting skeleton constructs used in hypertext documents with
1690 completion. See below for an introduction to HTML. Use
1691 \\[browse-url-of-buffer] to see how this comes out. See also `sgml-mode' on
1692 which this is based.
1693
1694 Do \\[describe-variable] html- SPC and \\[describe-variable] sgml- SPC to see available variables.
1695
1696 To write fairly well formatted pages you only need to know few things. Most
1697 browsers have a function to read the source code of the page being seen, so
1698 you can imitate various tricks. Here's a very short HTML primer which you
1699 can also view with a browser to see what happens:
1700
1701 <title>A Title Describing Contents</title> should be on every page. Pages can
1702 have <h1>Very Major Headlines</h1> through <h6>Very Minor Headlines</h6>
1703 <hr> Parts can be separated with horizontal rules.
1704
1705 <p>Paragraphs only need an opening tag. Line breaks and multiple spaces are
1706 ignored unless the text is <pre>preformatted.</pre> Text can be marked as
1707 <b>bold</b>, <i>italic</i> or <u>underlined</u> using the normal M-g or
1708 Edit/Text Properties/Face commands.
1709
1710 Pages can have <a name=\"SOMENAME\">named points</a> and can link other points
1711 to them with <a href=\"#SOMENAME\">see also somename</a>. In the same way <a
1712 href=\"URL\">see also URL</a> where URL is a filename relative to current
1713 directory, or absolute as in `http://www.cs.indiana.edu/elisp/w3/docs.html'.
1714
1715 Images in many formats can be inlined with <img src=\"URL\">.
1716
1717 If you mainly create your own documents, `sgml-specials' might be
1718 interesting. But note that some HTML 2 browsers can't handle `&apos;'.
1719 To work around that, do:
1720 (eval-after-load \"sgml-mode\" '(aset sgml-char-names ?' nil))
1721
1722 \\{html-mode-map}"
1723 (set (make-local-variable 'sgml-display-text) html-display-text)
1724 (set (make-local-variable 'sgml-tag-face-alist) html-tag-face-alist)
1725 (make-local-variable 'sgml-tag-alist)
1726 (make-local-variable 'sgml-face-tag-alist)
1727 (make-local-variable 'sgml-tag-help)
1728 (make-local-variable 'outline-regexp)
1729 (make-local-variable 'outline-heading-end-regexp)
1730 (make-local-variable 'outline-level)
1731 (make-local-variable 'sentence-end)
1732 (setq sentence-end
1733 (if sentence-end-double-space
1734 "[.?!][]\"')}]*\\(<[^>]*>\\)*\\($\\| $\\|\t\\| \\)[ \t\n]*"
1735 "[.?!][]\"')}]*\\(<[^>]*>\\)*\\($\\|[ \t]\\)[ \t\n]*"))
1736 (setq sgml-tag-alist html-tag-alist
1737 sgml-face-tag-alist html-face-tag-alist
1738 sgml-tag-help html-tag-help
1739 outline-regexp "^.*<[Hh][1-6]\\>"
1740 outline-heading-end-regexp "</[Hh][1-6]>"
1741 outline-level (lambda ()
1742 (char-before (match-end 0))))
1743 (setq imenu-create-index-function 'html-imenu-index)
1744 (when sgml-xml-mode (setq mode-name "XHTML"))
1745 (set (make-local-variable 'sgml-empty-tags)
1746 ;; From HTML-4.01's loose.dtd, parsed with `sgml-parse-dtd',
1747 ;; plus manual addition of "wbr".
1748 '("area" "base" "basefont" "br" "col" "frame" "hr" "img" "input"
1749 "isindex" "link" "meta" "param" "wbr"))
1750 (set (make-local-variable 'sgml-unclosed-tags)
1751 ;; From HTML-4.01's loose.dtd, parsed with `sgml-parse-dtd'.
1752 '("body" "colgroup" "dd" "dt" "head" "html" "li" "option"
1753 "p" "tbody" "td" "tfoot" "th" "thead" "tr"))
1754 ;; It's for the user to decide if it defeats it or not -stef
1755 ;; (make-local-variable 'imenu-sort-function)
1756 ;; (setq imenu-sort-function nil) ; sorting the menu defeats the purpose
1757 )
1758
1759 (defvar html-imenu-regexp
1760 "\\s-*<h\\([1-9]\\)[^\n<>]*>\\(<[^\n<>]*>\\)*\\s-*\\([^\n<>]*\\)"
1761 "*A regular expression matching a head line to be added to the menu.
1762 The first `match-string' should be a number from 1-9.
1763 The second `match-string' matches extra tags and is ignored.
1764 The third `match-string' will be the used in the menu.")
1765
1766 (defun html-imenu-index ()
1767 "Return a table of contents for an HTML buffer for use with Imenu."
1768 (let (toc-index)
1769 (save-excursion
1770 (goto-char (point-min))
1771 (while (re-search-forward html-imenu-regexp nil t)
1772 (setq toc-index
1773 (cons (cons (concat (make-string
1774 (* 2 (1- (string-to-number (match-string 1))))
1775 ?\ )
1776 (match-string 3))
1777 (line-beginning-position))
1778 toc-index))))
1779 (nreverse toc-index)))
1780
1781 (define-minor-mode html-autoview-mode
1782 "Toggle automatic viewing via `browse-url-of-buffer' upon saving buffer.
1783 With positive prefix ARG always turns viewing on, with negative ARG always off.
1784 Can be used as a value for `html-mode-hook'."
1785 nil nil nil
1786 (if html-autoview-mode
1787 (add-hook 'after-save-hook 'browse-url-of-buffer nil t)
1788 (remove-hook 'after-save-hook 'browse-url-of-buffer t)))
1789
1790 \f
1791 (define-skeleton html-href-anchor
1792 "HTML anchor tag with href attribute."
1793 "URL: "
1794 '(setq input "http:")
1795 "<a href=\"" str "\">" _ "</a>")
1796
1797 (define-skeleton html-name-anchor
1798 "HTML anchor tag with name attribute."
1799 "Name: "
1800 "<a name=\"" str "\">" _ "</a>")
1801
1802 (define-skeleton html-headline-1
1803 "HTML level 1 headline tags."
1804 nil
1805 "<h1>" _ "</h1>")
1806
1807 (define-skeleton html-headline-2
1808 "HTML level 2 headline tags."
1809 nil
1810 "<h2>" _ "</h2>")
1811
1812 (define-skeleton html-headline-3
1813 "HTML level 3 headline tags."
1814 nil
1815 "<h3>" _ "</h3>")
1816
1817 (define-skeleton html-headline-4
1818 "HTML level 4 headline tags."
1819 nil
1820 "<h4>" _ "</h4>")
1821
1822 (define-skeleton html-headline-5
1823 "HTML level 5 headline tags."
1824 nil
1825 "<h5>" _ "</h5>")
1826
1827 (define-skeleton html-headline-6
1828 "HTML level 6 headline tags."
1829 nil
1830 "<h6>" _ "</h6>")
1831
1832 (define-skeleton html-horizontal-rule
1833 "HTML horizontal rule tag."
1834 nil
1835 (if sgml-xml-mode "<hr/>" "<hr>") \n)
1836
1837 (define-skeleton html-image
1838 "HTML image tag."
1839 nil
1840 "<img src=\"" _ "\""
1841 (if sgml-xml-mode "/>" ">"))
1842
1843 (define-skeleton html-line
1844 "HTML line break tag."
1845 nil
1846 (if sgml-xml-mode "<br/>" "<br>") \n)
1847
1848 (define-skeleton html-ordered-list
1849 "HTML ordered list tags."
1850 nil
1851 "<ol>" \n
1852 "<li>" _ (if sgml-xml-mode "</li>") \n
1853 "</ol>")
1854
1855 (define-skeleton html-unordered-list
1856 "HTML unordered list tags."
1857 nil
1858 "<ul>" \n
1859 "<li>" _ (if sgml-xml-mode "</li>") \n
1860 "</ul>")
1861
1862 (define-skeleton html-list-item
1863 "HTML list item tag."
1864 nil
1865 (if (bolp) nil '\n)
1866 "<li>" _ (if sgml-xml-mode "</li>"))
1867
1868 (define-skeleton html-paragraph
1869 "HTML paragraph tag."
1870 nil
1871 (if (bolp) nil ?\n)
1872 \n "<p>" _ (if sgml-xml-mode "</p>"))
1873
1874 (define-skeleton html-checkboxes
1875 "Group of connected checkbox inputs."
1876 nil
1877 '(setq v1 nil
1878 v2 nil)
1879 ("Value: "
1880 "<input type=\"" (identity "checkbox") ; see comment above about identity
1881 "\" name=\"" (or v1 (setq v1 (skeleton-read "Name: ")))
1882 "\" value=\"" str ?\"
1883 (when (y-or-n-p "Set \"checked\" attribute? ")
1884 (funcall skeleton-transformation " checked"))
1885 (if sgml-xml-mode "/>" ">")
1886 (skeleton-read "Text: " (capitalize str))
1887 (or v2 (setq v2 (if (y-or-n-p "Newline after text? ")
1888 (funcall skeleton-transformation
1889 (if sgml-xml-mode "<br/>" "<br>"))
1890 "")))
1891 \n))
1892
1893 (define-skeleton html-radio-buttons
1894 "Group of connected radio button inputs."
1895 nil
1896 '(setq v1 nil
1897 v2 (cons nil nil))
1898 ("Value: "
1899 "<input type=\"" (identity "radio") ; see comment above about identity
1900 "\" name=\"" (or (car v2) (setcar v2 (skeleton-read "Name: ")))
1901 "\" value=\"" str ?\"
1902 (when (and (not v1) (setq v1 (y-or-n-p "Set \"checked\" attribute? ")))
1903 (funcall skeleton-transformation " checked"))
1904 (if sgml-xml-mode "/>" ">")
1905 (skeleton-read "Text: " (capitalize str))
1906 (or (cdr v2) (setcdr v2 (if (y-or-n-p "Newline after text? ")
1907 (funcall skeleton-transformation
1908 (if sgml-xml-mode "<br/>" "<br>"))
1909 "")))
1910 \n))
1911
1912 (provide 'sgml-mode)
1913
1914 ;;; sgml-mode.el ends here