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