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