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