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