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