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