1 ;;; sgml-mode.el --- SGML- and HTML-editing modes -*- coding: utf-8 -*-
3 ;; Copyright (C) 1992, 1995-1996, 1998, 2001-2015 Free Software
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
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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.
41 :link
'(custom-group-link :tag
"Font Lock Faces group" font-lock-faces
)
44 (defcustom sgml-basic-offset
2
45 "Specifies the basic indentation level for `sgml-indent-line'."
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."
57 (defcustom sgml-transformation-function
'identity
58 "Default value for `skeleton-transformation-function' in SGML mode."
60 :initialize
'custom-initialize-default
61 :set
(lambda (sym val
)
64 (with-current-buffer buff
65 (and (derived-mode-p 'sgml-mode
)
67 (setq skeleton-transformation-function val
))))
71 (put 'sgml-transformation-function
'variable-interactive
72 "aTransformation function: ")
73 (defvaralias 'sgml-transformation
'sgml-transformation-function
)
75 (defcustom sgml-mode-hook nil
76 "Hook run by command `sgml-mode'.
77 `text-mode-hook' is run first."
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:
89 ?\\\" Makes \" in text start a string.
90 ?' Makes ' in text start a string.
91 ?- Makes -- in text start a comment.
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'.
97 Including ?- has the problem of affecting dashes that have nothing to do
98 with comments, so we normally turn it off.")
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.")
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
)
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
)
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
)))
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
))
159 "Keymap for SGML mode. See also `sgml-specials'.")
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
))
176 (defvar sgml-mode-syntax-table
(sgml-make-syntax-table sgml-specials
)
177 "Syntax table used in SGML mode. See also `sgml-specials'.")
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
))
187 "Syntax table used to parse SGML tags.")
189 (defcustom sgml-name-8bit-mode nil
190 "When non-nil, insert non-ASCII characters as named entities."
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 `;'.")
229 (put 'sgml-table
'char-table-extra-slots
0)
231 (defvar sgml-char-names-table
232 (let ((table (make-char-table 'sgml-table
))
236 (setq elt
(aref sgml-char-names i
))
237 (if elt
(aset table
(make-char 'latin-iso8859-1 i
) elt
))
240 "A table for mapping non-ASCII characters into SGML entity names.
241 Currently, only Latin-1 characters are supported.")
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'.
253 ((executable-find "onsgmls")
254 ;; onsgmls is the community version of `nsgmls'
255 ;; hosted on http://openjade.sourceforge.net/
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."
265 (defvar sgml-saved-validate-command nil
266 "The command last used to validate in this buffer.")
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
)
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.")
283 (defface sgml-namespace
284 '((t (:inherit font-lock-builtin-face
)))
285 "`sgml-mode' face used to highlight the namespace part of identifiers."
287 (defvar sgml-namespace-face
'sgml-namespace
)
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
)))
307 (defconst sgml-font-lock-keywords-2
309 sgml-font-lock-keywords-1
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
))
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'.")
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'.")
338 (defvar sgml-face-tag-alist
()
339 "Alist of face and tag name for facemenu.")
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'.")
346 (defvar sgml-display-text
()
347 "Tag names as lowercase symbols, and display string when invisible.")
350 (defvar sgml-tags-invisible nil
)
352 (defcustom sgml-tag-alist
353 '(("![" ("ignore" t
) ("include" t
))
358 "Alist of tag names for completing read and insertion rules.
359 This alist is made up as
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
369 The attribute alist is made up as
371 ((\"attribute\" . ATTRIBUTERULE)
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
)))
379 (put 'sgml-tag-alist
'risky-local-variable t
)
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")))
393 (defvar sgml-empty-tags nil
394 "List of tags whose !ELEMENT definition says EMPTY.")
396 (defvar sgml-unclosed-tags nil
397 "List of tags whose !ELEMENT definition says the end-tag is optional.")
399 (defun sgml-xml-guess ()
400 "Guess whether the current buffer is XML. Return non-nil if so."
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
411 '("<!DOCTYPE" "\\(\\w+\\)" "\\(\\w+\\)"
412 "\"\\([^\"]+\\)\"" "\"\\([^\"]+\\)\"")
415 (string-match "X\\(HT\\)?ML" (match-string 3))))))
417 (defvar v2
) ; free for skeleton
419 (defun sgml-comment-indent-new-line (&optional soft
)
420 (let ((comment-start "-- ")
421 (comment-start-skip "\\(<!\\)?--[ \t]*")
423 (comment-style 'plain
))
424 (comment-indent-new-line soft
)))
426 (defun sgml-mode-facemenu-add-face-function (face end
)
427 (let ((tag-face (cdr (assq face sgml-face-tag-alist
))))
429 (setq tag-face
(funcall skeleton-transformation-function tag-face
))
430 (setq facemenu-end-add-face
(concat "</" tag-face
">"))
431 (concat "<" tag-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
)
443 (error "Face not configured for %s mode"
444 (format-mode-line mode-name
))))))
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'.
450 <a href=\"some://where\" type=\"text/plain\">
452 | no break here | but still allowed here"
454 (skip-chars-backward " \t")
455 (and (not (zerop (skip-syntax-backward "w_")))
456 (skip-chars-backward "/?!")
457 (eq (char-before) ?
<))))
459 (defvar tildify-space-string
)
460 (defvar tildify-foreach-region-function
)
463 (define-derived-mode sgml-mode text-mode
'(sgml-xml-mode "XML" "SGML")
464 "Major mode for editing SGML documents.
466 Keys <, &, SPC within <>, \", / and ' can be electric depending on
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.
473 If you like upcased tags, put (setq sgml-transformation-function 'upcase)
476 Use \\[sgml-validate] to validate your document with an SGML parser.
478 Do \\[describe-variable] sgml- SPC to see available variables.
479 Do \\[describe-key] on the following bindings to discover what they do.
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 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
) " ")
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
494 'tildify-foreach-ignore-environments
495 `((,(eval-when-compile
498 (regexp-opt '("pre" "dfn" "code" "samp" "kbd" "var"
499 "PRE" "DFN" "CODE" "SAMP" "KBD" "VAR"))
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
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
)
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
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
544 ,(concat "<!\\(element\\|entity\\)[ \t\n]+%?[ \t\n]*\\("
548 ,(concat "<[^>]+[ \t\n]+[Ii][Dd]=\\(['\"]"
549 (if sgml-xml-mode
"" "?")
550 "\\)\\(" sgml-name-re
"\\)\\1")
553 ,(concat "<[^>]+[ \t\n]+[Nn][Aa][Mm][Ee]=\\(['\"]"
554 (if sgml-xml-mode
"" "?")
555 "\\)\\(" sgml-name-re
"\\)\\1")
558 (defun sgml-comment-indent ()
559 (if (looking-at "--") comment-column
0))
561 (defun sgml-slash (arg)
562 "Insert ARG slash characters.
563 Behaves electrically if `sgml-quick-keys' is non-nil."
566 ((not (and (eq (char-before) ?
<) (= arg
1)))
567 (sgml-slash-matching arg
))
568 ((eq sgml-quick-keys
'indent
)
570 (indent-according-to-mode))
571 ((eq sgml-quick-keys
'close
)
575 (sgml-slash-matching arg
))))
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."
584 (let ((oldpos (point))
589 (if sgml-slash-distance
590 (narrow-to-region (max (point-min)
591 (- (point) sgml-slash-distance
))
593 (if (and (re-search-backward sgml-start-tag-regex
(point-min) t
)
594 (eq (match-end 0) (1- oldpos
)))
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
604 (if (eq tagend
(point))
606 (setq blinkpos
(point))
607 (setq level
(1- level
)))
608 (setq level
(1+ level
)))))))
611 (if (pos-visible-in-window-p)
613 (message "Matches %s"
614 (buffer-substring (line-beginning-position)
615 (1+ blinkpos
)))))))))
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."
628 (setq char
(read-quoted-char "Enter char or octal number")))
634 (defun sgml-namify-char ()
635 "Change the char before point into its `&name;' equivalent.
636 Uses `sgml-char-names'."
638 (let* ((char (char-before))
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
)))))
646 (error "Don't know the name of `%c'" char
)
648 (insert (format (if (numberp name
) "&#%d;" "&%s;") name
)))))
650 (defun sgml-name-self ()
651 "Insert a symbolic character name according to `sgml-char-names'."
653 (sgml-name-char last-command-event
))
655 (defun sgml-maybe-name-self ()
656 "Insert a symbolic character name according to `sgml-char-names'."
658 (if sgml-name-8bit-mode
659 (sgml-name-char last-command-event
)
660 (self-insert-command 1)))
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."
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")))
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").
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
)
685 (if (> (length sgml-tag-last
) 0)
686 (format "Tag (default %s): " sgml-tag-last
)
688 sgml-tag-alist nil nil nil
'sgml-tag-history sgml-tag-last
)))
690 (("") -
1 '(undo-boundary) (identity "<")) |
; see comment above
691 `(("") '(setq v2
(sgml-attributes ,str t
)) ?
>
695 '(("") " [ " _
" ]]"))
696 ((and (eq v2 t
) sgml-xml-mode
(member ,str sgml-empty-tags
))
698 ((or (and (eq v2 t
) (not sgml-xml-mode
)) (string-match "^[/!?]" ,str
))
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
) ?
> >))
708 (cons '("") (cdr v2
)))
710 (append '(("") (car v2
))
712 '(resume: (car v2
) _
"</" (identity ',str
) ?
> >))))))
714 (autoload 'skeleton-read
"skeleton")
716 (defun sgml-attributes (tag &optional quiet
)
717 "When at top level of a tag, interactively insert attributes.
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"))
724 (let ((completion-ignore-case t
)
725 (alist (cdr (assoc (downcase tag
) sgml-tag-alist
)))
727 (if (or (symbolp (car alist
))
728 (symbolp (car (car alist
))))
729 (setq car
(car alist
)
732 (message "No attributes configured."))
733 (if (stringp (car alist
))
735 (insert (if (eq (preceding-char) ?\s
) "" ?\s
)
736 (funcall skeleton-transformation-function
(car alist
)))
738 (setq i
(length alist
))
741 (insert (funcall skeleton-transformation-function
743 (skeleton-read '(completing-read
746 (if (string= "" attribute
)
748 (sgml-value (assoc (downcase attribute
) alist
))
750 (if (eq (preceding-char) ?\s
)
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."
758 (let ((point (point))
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)))))
770 (defun sgml-tag-help (&optional tag
)
771 "Display description of tag TAG. If TAG is omitted, use the tag at point."
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
)
779 sgml-tag-alist nil nil nil
780 'sgml-tag-history def
))))
781 (or (and tag
(> (length tag
) 0))
783 (if (eq (following-char) ?
<)
785 (setq tag
(sgml-beginning-of-tag))))
787 (error "No tag selected"))
788 (setq tag
(downcase tag
))
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")))
795 (defun sgml-maybe-end-tag (&optional arg
)
796 "Name self unless in position to end a tag or a prefix ARG is given."
798 (if (or arg
(eq (car (sgml-lexical-context)) 'tag
))
799 (self-insert-command (prefix-numeric-value arg
))
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."
807 ;; FIXME: use sgml-get-context or something similar.
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))) ?
/))
820 (sgml-skip-tag-backward 1)))
825 (defvar sgml-electric-tag-pair-overlays nil
)
826 (defvar sgml-electric-tag-pair-timer nil
)
828 (defun sgml-electric-tag-pair-before-change-function (beg 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) ?
/))
840 (cl-end (progn (skip-chars-forward "[:alnum:]-_.:") (point)))
843 (when (sgml-skip-tag-backward 1) (forward-char 1) t
)
844 (with-syntax-table sgml-tag-syntax-table
846 (when (sgml-skip-tag-forward 1)
850 (clones (get-char-property (point) 'text-clones
)))
853 (equal (buffer-substring cl-start cl-end
)
854 (buffer-substring (point)
856 (skip-chars-forward "[:alnum:]-_.:")
858 (or (not endp
) (eq (char-after cl-end
) ?
>)))
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
))))))
868 (error (message "Error in sgml-electric-pair-mode: %s" err
))))
870 (defun sgml-electric-tag-pair-flush-overlays ()
871 (while sgml-electric-tag-pair-overlays
872 (delete-overlay (pop sgml-electric-tag-pair-overlays
))))
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.
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."
884 (if sgml-electric-tag-pair-mode
886 (add-hook 'before-change-functions
887 'sgml-electric-tag-pair-before-change-function
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
895 ;; We leave the timer running for other buffers.
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."
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>.
908 (with-syntax-table sgml-tag-syntax-table
910 (skip-chars-forward "^<>")
911 (if (eq (following-char) ?
>)
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 "\\([^>]*[^/>]\\)?>"))
922 ;; FIXME: This re-search-forward will mistakenly match
923 ;; tag-like text inside attributes.
924 (while (and (re-search-forward re nil t
)
926 (eq (char-after (1+ (match-beginning 0))) ?
/)))
927 (goto-char (match-beginning 0))
928 (sgml-skip-tag-forward 1))
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))))))
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."
951 (if (looking-at "[ \t\n]*<")
953 (if (eq (char-after (match-end 0)) ?
/)
957 (goto-char (match-end 0))))
959 (or (save-excursion (setq close
(sgml-beginning-of-tag)
960 close
(and (stringp close
)
961 (eq (aref close
0) ?
/)
963 ;; not on closing tag
964 (let ((point (point)))
965 (sgml-skip-tag-backward 1)
966 (if (or (not (eq (following-char) ?
<))
970 (error "Not on or before tag")))))
973 (sgml-skip-tag-backward 1)
978 (when (and (sgml-skip-tag-forward 1)
979 (not (sgml-looking-back-at "/>")))
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)))
987 (if (progn (forward-line 0) (looking-at "\\(?:[ \t]*$\\)\n?"))
988 (delete-region (match-beginning 0) (match-end 0)))))
989 (setq arg
(1- arg
))))
992 ;; Put read-only last to enable setting this even when read-only enabled.
993 (or (get 'sgml-tag
'invisible
)
995 (append '(invisible t
996 point-entered sgml-point-entered
999 (symbol-plist 'sgml-tag
))))
1001 (defun sgml-tags-invisible (arg)
1002 "Toggle visibility of existing tags."
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
)
1016 (goto-char (point-min))
1017 (if (setq-local sgml-tags-invisible
1019 (>= (prefix-numeric-value arg
) 0)
1020 (not sgml-tags-invisible
)))
1021 (while (re-search-forward sgml-tag-name-re nil t
)
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
)
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
))
1049 (message "Invisible tag: %s"
1050 ;; Strip properties, otherwise, the text is invisible.
1051 (buffer-substring-no-properties
1053 (if (or (and (> x y
)
1054 (not (eq (following-char) ?
<)))
1056 (eq (preceding-char) ?
>)))
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."
1070 (list (read-string "Validate command: "
1071 (or sgml-saved-validate-command
1072 (concat sgml-validate-command
1074 (shell-quote-argument
1075 (let ((name (buffer-file-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
))
1082 (defsubst sgml-at-indentation-p
()
1083 "Return true if point is at the first non-whitespace character on the line."
1085 (skip-chars-backward " \t")
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'.
1093 Optional argument LIMIT is the position to start parsing from.
1094 If nil, start from a preceding tag at indentation."
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
)
1111 ;; We got to the end without seeing a tag.
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
))))
1132 ;; We've reached a tag. Parse it.
1133 ;; FIXME: Handle net-enabling start-tags
1134 (parse-partial-sexp (point) pos
0))))))
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
))))))
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
)
1148 (goto-char (cdr context
))
1149 (when (looking-at sgml-tag-name-re
)
1150 (match-string-no-properties 1)))
1152 (when (not (eq (car context
) 'text
))
1153 (goto-char (cdr context
))
1154 (sgml-beginning-of-tag t
))))))
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
))
1165 (setq alist
(skeleton-read '(completing-read "Value: " (cdr alist
))))
1166 (if (string< "" alist
)
1171 (insert (skeleton-read '(completing-read "Value: " alist
)))
1173 (insert (skeleton-read '(read-string "Value: ")))))
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")
1182 (narrow-to-region start end
)
1183 (goto-char (point-min))
1185 ;; FIXME: We should unquote other named character references as well.
1186 (while (re-search-forward
1187 "\\(&\\(amp\\|\\(l\\|\\(g\\)\\)t\\)\\)[][<>&;\n\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) '((?
& .
"&")
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."
1202 ;; - insert newline between some start-tag and text.
1203 ;; - don't insert newline in front of some end-tags.
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))
1220 ;; (indent-region beg end)
1226 (cl-defstruct (sgml-tag
1227 (:constructor sgml-make-tag
(type start end name
)))
1228 type start end name
)
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))))
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."
1241 (with-syntax-table sgml-tag-syntax-table
1242 (let ((pps (parse-partial-sexp start end
2)))
1243 (and (= (nth 0 pps
) 0))))))
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."
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
1260 (setq tag-start
(point))
1261 (with-syntax-table sgml-tag-syntax-table
1262 (let ((pos (point)))
1264 ;; FIXME: This does not correctly skip over PI an CDATA tags.
1267 ;; This < seems to be just a spurious one, let's ignore it.
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))
1273 (throw 'found
(sgml-parse-tag-backward limit
)))
1274 (forward-char -
1))))
1275 (setq tag-end
(1+ (point)))
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
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
1292 tag-start
(or tag-start
(search-backward "<?" nil t
))))
1296 (with-syntax-table sgml-tag-syntax-table
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
))))
1305 (goto-char (1+ tag-start
))
1307 (?
! (setq tag-type
'decl
)) ; declaration
1308 (??
(setq tag-type
'pi
)) ; processing-instruction
1309 (?%
(setq tag-type
'jsp
)) ; JSP tags
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
))))
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).
1329 The context is a list of tag-info structures. The last one is the tag
1330 immediately enclosing the current position.
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))
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.
1346 (and (not (eq until
'now
))
1348 (not (if until
(eq until
'empty
) context
))
1349 (not (sgml-at-indentation-p))
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))))
1355 ;; This tag may enclose things we thought were tags. If so,
1358 (> (sgml-tag-end tag-info
)
1359 (sgml-tag-end (car context
))))
1360 (setq context
(cdr context
)))
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
)
1369 ((eq (sgml-tag-type tag-info
) 'open
)
1372 (if (assoc-string (sgml-tag-name tag-info
) ignore t
)
1373 ;; There was an implicit end-tag.
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 ]
1381 ((eq t
(compare-strings (sgml-tag-name tag-info
) nil nil
1382 (car stack
) nil nil t
))
1383 (setq stack
(cdr stack
)))
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
))
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
)))))
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
)))
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
)))
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."
1422 (with-output-to-temp-buffer "*XML Context*"
1424 (let ((context (sgml-get-context)))
1427 (while (setq more
(sgml-get-context))
1428 (setq context
(nconc more context
)))))
1432 ;; Editing shortcuts
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, ..."
1439 (pcase (car (sgml-lexical-context))
1440 (`comment
(insert " -->"))
1441 (`cdata
(insert "]]>"))
1442 (`pi
(insert " ?>"))
1443 (`jsp
(insert " %>"))
1444 (`tag
(insert " />"))
1446 (let ((context (save-excursion (sgml-get-context))))
1449 (insert "</" (sgml-tag-name (car (last context
))) ">")
1450 (indent-according-to-mode)))))
1452 (error "Nothing to close"))))
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
)))
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
)))
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)))
1470 ;; Indent comment-start markers inside <!-- just like comment-end markers.
1471 (if (and (eq (car lcon
) 'tag
)
1473 (save-excursion (goto-char (cdr lcon
)) (looking-at "<!--")))
1474 (setq lcon
(cons 'comment
(+ (cdr lcon
) 2))))
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))))
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"))
1507 ;; We don't know how to indent it. Let's be honest about it.
1509 ;; We don't know how to indent it. Let's be honest about it.
1513 (goto-char (1+ (cdr lcon
)))
1514 (skip-chars-forward "^ \t\n") ;Skip tag name.
1515 (skip-chars-forward " \t")
1518 ;; This is the first attribute: indent.
1519 (goto-char (1+ (cdr lcon
)))
1520 (+ (current-column) sgml-basic-offset
)))
1523 (while (looking-at "</")
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
)
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
)))
1542 (nreverse (sgml-get-context (if unclosed nil
'empty
)))))
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.
1552 ;; If we were not in a text context after all, let's try again.
1553 ((and context
(> (sgml-tag-end (car context
)) 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.
1561 (goto-char (sgml-tag-end (car context
)))
1562 (skip-chars-forward " \t\n")
1563 (< (point) here
) (sgml-at-indentation-p))
1568 (* sgml-basic-offset
(length context
)))))))
1571 (error "Unrecognized context %s" (car lcon
)))
1575 (defun sgml-indent-line ()
1576 "Indent the current line as SGML."
1578 (let* ((savep (point))
1581 (back-to-indentation)
1582 (if (>= (point) savep
) (setq savep nil
))
1583 (sgml-calculate-indent))))
1584 (if (null indent-col
)
1587 (save-excursion (indent-line-to indent-col
))
1588 (indent-line-to indent-col
)))))
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."
1596 (goto-char (point-min))
1597 (if (re-search-forward "^\\([ \t]+\\)<" 500 'noerror
)
1599 (setq-local sgml-basic-offset
(1- (current-column)))
1600 (message "Guessed sgml-basic-offset = %d"
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))
1610 (while (re-search-forward "<!ELEMENT[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+[-O][ \t\n]+\\([-O]\\)[ \t\n]+\\([^ \t\n]+\\)" nil t
)
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
)))
1622 (defcustom html-mode-hook nil
1623 "Hook run by command `html-mode'.
1624 `text-mode-hook' and `sgml-mode-hook' are run first."
1627 :options
'(html-autoview-mode))
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.")
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))
1691 "Keymap for commands for use in HTML mode.")
1693 (defvar html-face-tag-alist
1697 (mode-line . "rev"))
1698 "Value of `sgml-face-tag-alist' for HTML mode.")
1700 (defvar html-tag-face-alist
1703 ("blink" . highlight)
1706 ("h1" bold underline)
1707 ("h2" bold-italic underline)
1708 ("h3" italic underline)
1717 ("title" bold underline)
1721 "Value of `sgml-tag-face-alist' for HTML mode.")
1723 (defvar html-display-text
1727 "Value of `sgml-display-text' for HTML mode.")
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/")))
1745 (list '((nil \n ("List item: " "<li>" str
1746 (if sgml-xml-mode "</li>") \n))))
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)
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")))
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"))
1778 ("ol" ,@list ("type" ("A") ("a") ("I") ("i") ("1")))
1782 "<option>" str (if sgml-xml-mode "</option>") \n))
1783 ,name ("size" ,@1-9) ("multiple" t))
1785 ((completing-read "Cell kind: " '(("td") ("th"))
1788 (if sgml-xml-mode (concat "<" str "></tr>")) \n))
1789 ("border" t ,@1-9) ("width" "10") ("cellpadding"))
1791 ("textarea" ,name ("rows" ,@1-9) ("cols" ,@1-9))
1793 ("ul" ,@list ("type" ("disc") ("circle") ("square")))
1801 ("Item: " "<item>" str (if sgml-xml-mode "</item>") \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")))
1816 ("dd" ,(not sgml-xml-mode))
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))
1827 ("fn" "id" "fn") ;; Footnotes were deprecated in HTML 3.2
1831 "<title>" (setq str (read-input "Title: ")) "</title>\n"
1833 "<body>\n<h1>" str "</h1>\n" _
1834 "\n<address>\n<a href=\"mailto:"
1836 "\">" (user-full-name) "</a>\n</address>\n"
1841 ("isindex" t ("action") ("prompt"))
1844 ("li" ,(not sgml-xml-mode))
1847 ("option" t ("value") ("label") ("selected" t))
1849 ("person") ;; Tag for person's name tag deprecated in HTML 3.2
1876 "Value of `sgml-tag-alist' for HTML mode.")
1878 (defvar html-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")
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")
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")
1947 ("rev" . "Reverse video")
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.")
1971 (defvar outline-regexp)
1972 (defvar outline-heading-end-regexp)
1973 (defvar outline-level)
1975 (defun html-current-defun-name ()
1976 "Return the name of the last HTML title or heading, or nil."
1978 (if (re-search-backward
1981 "\\(?:[hH][0-6]\\|title\\|TITLE\\|Title\\)"
1984 "\\([^<\r\n]*[^ <\t\r\n]+\\)")
1986 (match-string-no-properties 1))))
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.
1997 Do \\[describe-variable] html- SPC and \\[describe-variable] sgml- SPC to see available variables.
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:
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.
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.
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'.
2018 Images in many formats can be inlined with <img src=\"URL\">.
2020 If you mainly create your own documents, `sgml-specials' might be
2021 interesting. But note that some HTML 2 browsers can't handle `''.
2022 To work around that, do:
2023 (eval-after-load \"sgml-mode\" '(aset sgml-char-names ?' nil))
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 "[.?!][]\"'”)}]*\\(<[^>]*>\\)*")
2038 (setq imenu-create-index-function 'html-imenu-index)
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
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.")
2061 (defun html-imenu-index ()
2062 "Return a table of contents for an HTML buffer for use with Imenu."
2065 (goto-char (point-min))
2066 (while (re-search-forward html-imenu-regexp nil t)
2068 (cons (cons (concat (make-string
2069 (* 2 (1- (string-to-number (match-string 1))))
2072 (line-beginning-position))
2074 (nreverse toc-index)))
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.
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."
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)))
2092 (define-skeleton html-href-anchor
2093 "HTML anchor tag with href attribute."
2095 ;; '(setq input "http:")
2096 "<a href=\"" str "\">" _ "</a>")
2098 (define-skeleton html-name-anchor
2099 "HTML anchor tag with name attribute."
2101 "<a name=\"" str "\""
2102 (if sgml-xml-mode (concat " id=\"" str "\""))
2105 (define-skeleton html-headline-1
2106 "HTML level 1 headline tags."
2110 (define-skeleton html-headline-2
2111 "HTML level 2 headline tags."
2115 (define-skeleton html-headline-3
2116 "HTML level 3 headline tags."
2120 (define-skeleton html-headline-4
2121 "HTML level 4 headline tags."
2125 (define-skeleton html-headline-5
2126 "HTML level 5 headline tags."
2130 (define-skeleton html-headline-6
2131 "HTML level 6 headline tags."
2135 (define-skeleton html-horizontal-rule
2136 "HTML horizontal rule tag."
2138 (if sgml-xml-mode "<hr />" "<hr>") \n)
2140 (define-skeleton html-image
2143 "<img src=\"" str "\" alt=\"" _ "\""
2144 (if sgml-xml-mode " />" ">"))
2146 (define-skeleton html-line
2147 "HTML line break tag."
2149 (if sgml-xml-mode "<br />" "<br>") \n)
2151 (define-skeleton html-ordered-list
2152 "HTML ordered list tags."
2155 "<li>" _ (if sgml-xml-mode "</li>") \n
2158 (define-skeleton html-unordered-list
2159 "HTML unordered list tags."
2162 "<li>" _ (if sgml-xml-mode "</li>") \n
2165 (define-skeleton html-list-item
2166 "HTML list item tag."
2169 "<li>" _ (if sgml-xml-mode "</li>"))
2171 (define-skeleton html-paragraph
2172 "HTML paragraph tag."
2175 "<p>" _ (if sgml-xml-mode "</p>"))
2177 (define-skeleton html-checkboxes
2178 "Group of connected checkbox inputs."
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>"))
2197 (define-skeleton html-radio-buttons
2198 "Group of connected radio button inputs."
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>"))
2217 (provide 'sgml-mode)
2219 ;;; sgml-mode.el ends here