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