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