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