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