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