1 ;;; sgml-mode.el --- SGML- and HTML-editing modes
3 ;; Copyright (C) 1992,95,96,98,2001,2002, 2003 Free Software Foundation, Inc.
5 ;; Author: James Clark <jjc@jclark.com>
7 ;; Adapted-By: ESR, Daniel Pfeiffer <occitan@esperanto.org>,
8 ;; F.Potorti@cnuce.cnr.it
9 ;; Keywords: wp, hypermedia, comm, languages
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
30 ;; Configurable major mode for editing document in the SGML standard general
31 ;; markup language. As an example contains a mode for editing the derived
32 ;; HTML hypertext markup language.
45 (defcustom sgml-basic-offset
2
46 "*Specifies the basic indentation level for `sgml-indent-line'."
50 (defcustom sgml-transformation
'identity
51 "*Default value for `skeleton-transformation' (which see) in SGML mode."
55 (put 'sgml-transformation
'variable-interactive
56 "aTransformation function: ")
58 (defcustom sgml-mode-hook nil
59 "Hook run by command `sgml-mode'.
60 `text-mode-hook' is run first."
64 ;; As long as Emacs' syntax can't be complemented with predicates to context
65 ;; sensitively confirm the syntax of characters, we have to live with this
66 ;; kludgy kind of tradeoff.
67 (defvar sgml-specials
'(?
\")
68 "List of characters that have a special meaning for SGML mode.
69 This list is used when first loading the `sgml-mode' library.
70 The supported characters and potential disadvantages are:
72 ?\\\" Makes \" in text start a string.
73 ?' Makes ' in text start a string.
74 ?- Makes -- in text start a comment.
76 When only one of ?\\\" or ?' are included, \"'\" or '\"', as can be found in
77 DTDs, start a string. To partially avoid this problem this also makes these
78 self insert as named entities depending on `sgml-quick-keys'.
80 Including ?- has the problem of affecting dashes that have nothing to do
81 with comments, so we normally turn it off.")
83 (defvar sgml-quick-keys nil
84 "Use <, >, &, /, SPC and `sgml-specials' keys \"electrically\" when non-nil.
85 This takes effect when first loading the `sgml-mode' library.")
88 (let ((map (make-keymap)) ;`sparse' doesn't allow binding to charsets.
89 (menu-map (make-sparse-keymap "SGML")))
90 (define-key map
"\C-c\C-i" 'sgml-tags-invisible
)
91 (define-key map
"/" 'sgml-slash
)
92 (define-key map
"\C-c\C-n" 'sgml-name-char
)
93 (define-key map
"\C-c\C-t" 'sgml-tag
)
94 (define-key map
"\C-c\C-a" 'sgml-attributes
)
95 (define-key map
"\C-c\C-b" 'sgml-skip-tag-backward
)
96 (define-key map
[?\C-c left
] 'sgml-skip-tag-backward
)
97 (define-key map
"\C-c\C-f" 'sgml-skip-tag-forward
)
98 (define-key map
[?\C-c right
] 'sgml-skip-tag-forward
)
99 (define-key map
"\C-c\C-d" 'sgml-delete-tag
)
100 (define-key map
"\C-c\^?" 'sgml-delete-tag
)
101 (define-key map
"\C-c?" 'sgml-tag-help
)
102 (define-key map
"\C-c/" 'sgml-close-tag
)
103 (define-key map
"\C-c8" 'sgml-name-8bit-mode
)
104 (define-key map
"\C-c\C-v" 'sgml-validate
)
105 (when sgml-quick-keys
106 (define-key map
"&" 'sgml-name-char
)
107 (define-key map
"<" 'sgml-tag
)
108 (define-key map
" " 'sgml-auto-attributes
)
109 (define-key map
">" 'sgml-maybe-end-tag
)
110 (when (memq ?
\" sgml-specials
)
111 (define-key map
"\"" 'sgml-name-self
))
112 (when (memq ?
' sgml-specials
)
113 (define-key map
"'" 'sgml-name-self
)))
114 (define-key map
(vector (make-char 'latin-iso8859-1
))
115 'sgml-maybe-name-self
)
118 (while (< (setq c
(1+ c
)) 256)
119 (aset map c
'sgml-maybe-name-self
)))
120 (define-key map
[menu-bar sgml
] (cons "SGML" menu-map
))
121 (define-key menu-map
[sgml-validate
] '("Validate" . sgml-validate
))
122 (define-key menu-map
[sgml-name-8bit-mode
]
123 '("Toggle 8 Bit Insertion" . sgml-name-8bit-mode
))
124 (define-key menu-map
[sgml-tags-invisible
]
125 '("Toggle Tag Visibility" . sgml-tags-invisible
))
126 (define-key menu-map
[sgml-tag-help
]
127 '("Describe Tag" . sgml-tag-help
))
128 (define-key menu-map
[sgml-delete-tag
]
129 '("Delete Tag" . sgml-delete-tag
))
130 (define-key menu-map
[sgml-skip-tag-forward
]
131 '("Forward Tag" . sgml-skip-tag-forward
))
132 (define-key menu-map
[sgml-skip-tag-backward
]
133 '("Backward Tag" . sgml-skip-tag-backward
))
134 (define-key menu-map
[sgml-attributes
]
135 '("Insert Attributes" . sgml-attributes
))
136 (define-key menu-map
[sgml-tag
] '("Insert Tag" . sgml-tag
))
138 "Keymap for SGML mode. See also `sgml-specials'.")
140 (defun sgml-make-syntax-table (specials)
141 (let ((table (make-syntax-table text-mode-syntax-table
)))
142 (modify-syntax-entry ?
< "(>" table
)
143 (modify-syntax-entry ?
> ")<" table
)
144 (modify-syntax-entry ?
: "_" table
)
145 (modify-syntax-entry ?_
"_" table
)
146 (modify-syntax-entry ?.
"_" table
)
147 (if (memq ?- specials
)
148 (modify-syntax-entry ?-
"_ 1234" table
))
149 (if (memq ?
\" specials
)
150 (modify-syntax-entry ?
\" "\"\"" table
))
151 (if (memq ?
' specials
)
152 (modify-syntax-entry ?
\' "\"'" table
))
155 (defvar sgml-mode-syntax-table
(sgml-make-syntax-table sgml-specials
)
156 "Syntax table used in SGML mode. See also `sgml-specials'.")
158 (defconst sgml-tag-syntax-table
159 (let ((table (sgml-make-syntax-table '(?- ?
\" ?
\'))))
160 (dolist (char '(?\
( ?\
) ?\
{ ?\
} ?\
[ ?\
] ?$ ?% ?
& ?
* ?
+ ?
/))
161 (modify-syntax-entry char
"." table
))
163 "Syntax table used to parse SGML tags.")
165 (defcustom sgml-name-8bit-mode nil
166 "*When non-nil, insert non-ASCII characters as named entities."
170 (defvar sgml-char-names
171 [nil nil nil nil nil nil nil nil
172 nil nil nil nil nil nil nil nil
173 nil nil nil nil nil nil nil nil
174 nil nil nil nil nil nil nil nil
175 "nbsp" "excl" "quot" "num" "dollar" "percnt" "amp" "apos"
176 "lpar" "rpar" "ast" "plus" "comma" "hyphen" "period" "sol"
177 nil nil nil nil nil nil nil nil
178 nil nil
"colon" "semi" "lt" "eq" "gt" "quest"
179 "commat" nil nil nil nil nil nil nil
180 nil nil nil nil nil nil nil nil
181 nil nil nil nil nil nil nil nil
182 nil nil nil
"lsqb" nil
"rsqb" "uarr" "lowbar"
183 "lsquo" nil nil nil nil nil nil nil
184 nil nil nil nil nil nil nil nil
185 nil nil nil nil nil nil nil nil
186 nil nil nil
"lcub" "verbar" "rcub" "tilde" nil
187 nil nil nil nil nil nil nil nil
188 nil nil nil nil nil nil nil nil
189 nil nil nil nil nil nil nil nil
190 nil nil nil nil nil nil nil nil
191 "nbsp" "iexcl" "cent" "pound" "curren" "yen" "brvbar" "sect"
192 "uml" "copy" "ordf" "laquo" "not" "shy" "reg" "macr"
193 "ring" "plusmn" "sup2" "sup3" "acute" "micro" "para" "middot"
194 "cedil" "sup1" "ordm" "raquo" "frac14" "frac12" "frac34" "iquest"
195 "Agrave" "Aacute" "Acirc" "Atilde" "Auml" "Aring" "AElig" "Ccedil"
196 "Egrave" "Eacute" "Ecirc" "Euml" "Igrave" "Iacute" "Icirc" "Iuml"
197 "ETH" "Ntilde" "Ograve" "Oacute" "Ocirc" "Otilde" "Ouml" nil
198 "Oslash" "Ugrave" "Uacute" "Ucirc" "Uuml" "Yacute" "THORN" "szlig"
199 "agrave" "aacute" "acirc" "atilde" "auml" "aring" "aelig" "ccedil"
200 "egrave" "eacute" "ecirc" "euml" "igrave" "iacute" "icirc" "iuml"
201 "eth" "ntilde" "ograve" "oacute" "ocirc" "otilde" "ouml" "divide"
202 "oslash" "ugrave" "uacute" "ucirc" "uuml" "yacute" "thorn" "yuml"]
203 "Vector of symbolic character names without `&' and `;'.")
205 (put 'sgml-table
'char-table-extra-slots
0)
207 (defvar sgml-char-names-table
208 (let ((table (make-char-table 'sgml-table
))
212 (setq elt
(aref sgml-char-names i
))
213 (if elt
(aset table
(make-char 'latin-iso8859-1 i
) elt
))
216 "A table for mapping non-ASCII characters into SGML entity names.
217 Currently, only Latin-1 characters are supported.")
219 ;; nsgmls is a free SGML parser in the SP suite available from
220 ;; ftp.jclark.com and otherwise packaged for GNU systems.
221 ;; Its error messages can be parsed by next-error.
222 ;; The -s option suppresses output.
224 (defcustom sgml-validate-command
"nsgmls -s" ; replaced old `sgmls'
225 "*The command to validate an SGML document.
226 The file name of current buffer file name will be appended to this,
227 separated by a space."
232 (defvar sgml-saved-validate-command nil
233 "The command last used to validate in this buffer.")
235 ;; I doubt that null end tags are used much for large elements,
236 ;; so use a small distance here.
237 (defcustom sgml-slash-distance
1000
238 "*If non-nil, is the maximum distance to search for matching `/'."
239 :type
'(choice (const nil
) integer
)
242 (defconst sgml-namespace-re
"[_[:alpha:]][-_.[:alnum:]]*")
243 (defconst sgml-name-re
"[_:[:alpha:]][-_.:[:alnum:]]*")
244 (defconst sgml-tag-name-re
(concat "<\\([!/?]?" sgml-name-re
"\\)"))
245 (defconst sgml-attrs-re
"\\(?:[^\"'/><]\\|\"[^\"]*\"\\|'[^']*'\\)*")
246 (defconst sgml-start-tag-regex
(concat "<" sgml-name-re sgml-attrs-re
)
247 "Regular expression that matches a non-empty start tag.
248 Any terminating `>' or `/' is not matched.")
250 (defface sgml-namespace-face
251 '((t (:inherit font-lock-builtin-face
)))
252 "`sgml-mode' face used to highlight the namespace part of identifiers.")
253 (defvar sgml-namespace-face
'sgml-namespace-face
)
256 (defconst sgml-font-lock-keywords-1
257 `((,(concat "<\\([!?]" sgml-name-re
"\\)") 1 font-lock-keyword-face
)
258 ;; We could use the simpler "\\(" sgml-namespace-re ":\\)?" instead,
259 ;; but it would cause a bit more backtracking in the re-matcher.
260 (,(concat "</?\\(" sgml-namespace-re
"\\)\\(?::\\(" sgml-name-re
"\\)\\)?")
261 (1 (if (match-end 2) sgml-namespace-face font-lock-function-name-face
))
262 (2 font-lock-function-name-face nil t
))
263 ;; FIXME: this doesn't cover the variables using a default value.
264 (,(concat "\\(" sgml-namespace-re
"\\)\\(?::\\("
265 sgml-name-re
"\\)\\)?=[\"']")
266 (1 (if (match-end 2) sgml-namespace-face font-lock-variable-name-face
))
267 (2 font-lock-variable-name-face nil t
))
268 (,(concat "[&%]" sgml-name-re
";?") . font-lock-variable-name-face
)))
270 (defconst sgml-font-lock-keywords-2
272 sgml-font-lock-keywords-1
275 (regexp-opt (mapcar 'car sgml-tag-face-alist
) t
)
276 "\\([ \t][^>]*\\)?>\\([^<]+\\)</\\1>")
277 '(3 (cdr (assoc (downcase (match-string 1))
278 sgml-tag-face-alist
)) prepend
))))))
280 ;; for font-lock, but must be defvar'ed after
281 ;; sgml-font-lock-keywords-1 and sgml-font-lock-keywords-2 above
282 (defvar sgml-font-lock-keywords sgml-font-lock-keywords-1
283 "*Rules for highlighting SGML code. See also `sgml-tag-face-alist'.")
285 (defvar sgml-font-lock-syntactic-keywords
286 ;; Use the `b' style of comments to avoid interference with the -- ... --
287 ;; comments recognized when `sgml-specials' includes ?-.
288 ;; FIXME: beware of <!--> blabla <!--> !!
289 '(("\\(<\\)!--" (1 "< b"))
290 ("--[ \t\n]*\\(>\\)" (1 "> b")))
291 "Syntactic keywords for `sgml-mode'.")
294 (defvar sgml-face-tag-alist
()
295 "Alist of face and tag name for facemenu.")
297 (defvar sgml-tag-face-alist
()
298 "Tag names and face or list of faces to fontify with when invisible.
299 When `font-lock-maximum-decoration' is 1 this is always used for fontifying.
300 When more these are fontified together with `sgml-font-lock-keywords'.")
302 (defvar sgml-display-text
()
303 "Tag names as lowercase symbols, and display string when invisible.")
306 (defvar sgml-tags-invisible nil
)
308 (defcustom sgml-tag-alist
309 '(("![" ("ignore" t
) ("include" t
))
314 "*Alist of tag names for completing read and insertion rules.
315 This alist is made up as
320 TAGRULE is a list of optionally t (no endtag) or `\\n' (separate endtag by
321 newlines) or a skeleton with nil, t or `\\n' in place of the interactor
322 followed by an ATTRIBUTERULE (for an always present attribute) or an
325 The attribute alist is made up as
327 ((\"attribute\" . ATTRIBUTERULE)
330 ATTRIBUTERULE is a list of optionally t (no value when no input) followed by
331 an optional alist of possible values."
332 :type
'(repeat (cons (string :tag
"Tag Name")
333 (repeat :tag
"Tag Rule" sexp
)))
336 (defcustom sgml-tag-help
337 '(("!" .
"Empty declaration for comment")
338 ("![" .
"Embed declarations with parser directive")
339 ("!attlist" .
"Tag attributes declaration")
340 ("!doctype" .
"Document type (DTD) declaration")
341 ("!element" .
"Tag declaration")
342 ("!entity" .
"Entity (macro) declaration"))
343 "*Alist of tag name and short description."
344 :type
'(repeat (cons (string :tag
"Tag Name")
345 (string :tag
"Description")))
348 (defcustom sgml-xml-mode nil
349 "*When non-nil, tag insertion functions will be XML-compliant.
350 If this variable is customized, the custom value is used always.
351 Otherwise, it is set to be buffer-local when the file has
352 a DOCTYPE or an XML declaration."
357 (defvar sgml-empty-tags nil
358 "List of tags whose !ELEMENT definition says EMPTY.")
360 (defvar sgml-unclosed-tags nil
361 "List of tags whose !ELEMENT definition says the end-tag is optional.")
363 (defun sgml-xml-guess ()
364 "Guess whether the current buffer is XML."
366 (goto-char (point-min))
367 (when (or (string= "xml" (file-name-extension (or buffer-file-name
"")))
368 (looking-at "\\s-*<\\?xml")
369 (when (re-search-forward
372 '("<!DOCTYPE" "\\(\\w+\\)" "\\(\\w+\\)"
373 "\"\\([^\"]+\\)\"" "\"\\([^\"]+\\)\"")
376 (string-match "X\\(HT\\)?ML" (match-string 3))))
377 (set (make-local-variable 'sgml-xml-mode
) t
))))
379 (defvar v2
) ; free for skeleton
381 (defun sgml-comment-indent-new-line (&optional soft
)
382 (let ((comment-start "-- ")
383 (comment-start-skip "\\(<!\\)?--[ \t]*")
385 (comment-style 'plain
))
386 (comment-indent-new-line soft
)))
388 (defun sgml-mode-facemenu-add-face-function (face end
)
389 (if (setq face
(cdr (assq face sgml-face-tag-alist
)))
391 (setq face
(funcall skeleton-transformation face
))
392 (setq facemenu-end-add-face
(concat "</" face
">"))
393 (concat "<" face
">"))
394 (error "Face not configured for %s mode" mode-name
)))
397 (define-derived-mode sgml-mode text-mode
"SGML"
398 "Major mode for editing SGML documents.
400 Keys <, &, SPC within <>, \", / and ' can be electric depending on
403 An argument of N to a tag-inserting command means to wrap it around
404 the next N words. In Transient Mark mode, when the mark is active,
405 N defaults to -1, which means to wrap it around the current region.
407 If you like upcased tags, put (setq sgml-transformation 'upcase) in
410 Use \\[sgml-validate] to validate your document with an SGML parser.
412 Do \\[describe-variable] sgml- SPC to see available variables.
413 Do \\[describe-key] on the following bindings to discover what they do.
415 (make-local-variable 'sgml-saved-validate-command
)
416 (make-local-variable 'facemenu-end-add-face
)
417 ;;(make-local-variable 'facemenu-remove-face-function)
418 ;; A start or end tag by itself on a line separates a paragraph.
419 ;; This is desirable because SGML discards a newline that appears
420 ;; immediately after a start tag or immediately before an end tag.
421 (set (make-local-variable 'paragraph-start
) (concat "[ \t]*$\\|\
422 \[ \t]*</?\\(" sgml-name-re sgml-attrs-re
"\\)?>"))
423 (set (make-local-variable 'paragraph-separate
)
424 (concat paragraph-start
"$"))
425 (set (make-local-variable 'adaptive-fill-regexp
) "[ \t]*")
426 (set (make-local-variable 'indent-line-function
) 'sgml-indent-line
)
427 (set (make-local-variable 'comment-start
) "<!-- ")
428 (set (make-local-variable 'comment-end
) " -->")
429 (set (make-local-variable 'comment-indent-function
) 'sgml-comment-indent
)
430 (set (make-local-variable 'comment-line-break-function
)
431 'sgml-comment-indent-new-line
)
432 (set (make-local-variable 'skeleton-further-elements
)
433 '((completion-ignore-case t
)))
434 (set (make-local-variable 'skeleton-end-hook
)
437 (not (or (eq v2
'\n) (eq (car-safe v2
) '\n)))
438 (newline-and-indent))))
439 (set (make-local-variable 'font-lock-defaults
)
440 '((sgml-font-lock-keywords
441 sgml-font-lock-keywords-1
442 sgml-font-lock-keywords-2
)
444 (font-lock-syntactic-keywords
445 . sgml-font-lock-syntactic-keywords
)))
446 (set (make-local-variable 'facemenu-add-face-function
)
447 'sgml-mode-facemenu-add-face-function
)
450 (setq mode-name
"XML")
451 (set (make-local-variable 'skeleton-transformation
) sgml-transformation
))
452 ;; This will allow existing comments within declarations to be
454 (set (make-local-variable 'comment-start-skip
) "\\(?:<!\\)?--[ \t]*")
455 (set (make-local-variable 'comment-end-skip
) "[ \t]*--\\([ \t\n]*>\\)?")
456 ;; This definition probably is not useful in derived modes.
457 (set (make-local-variable 'imenu-generic-expression
)
458 (concat "<!\\(element\\|entity\\)[ \t\n]+%?[ \t\n]*\\("
459 sgml-name-re
"\\)")))
461 ;; Some programs (such as Glade 2) generate XML which has
462 ;; -*- mode: xml -*-.
463 (defalias 'xml-mode
'sgml-mode
)
465 (defun sgml-comment-indent ()
466 (if (looking-at "--") comment-column
0))
468 (defun sgml-slash (arg)
469 "Insert ARG slash characters.
470 Behaves electrically if `sgml-quick-keys' is non-nil."
473 ((not (and (eq (char-before) ?
<) (= arg
1)))
474 (sgml-slash-matching arg
))
475 ((eq sgml-quick-keys
'indent
)
477 (indent-according-to-mode))
478 ((eq sgml-quick-keys
'close
)
479 (delete-backward-char 1)
482 (sgml-slash-matching arg
))))
484 (defun sgml-slash-matching (arg)
485 "Insert `/' and display any previous matching `/'.
486 Two `/'s are treated as matching if the first `/' ends a net-enabling
487 start tag, and the second `/' is the corresponding null end tag."
491 (let ((oldpos (point))
496 (if sgml-slash-distance
497 (narrow-to-region (max (point-min)
498 (- (point) sgml-slash-distance
))
500 (if (and (re-search-backward sgml-start-tag-regex
(point-min) t
)
501 (eq (match-end 0) (1- oldpos
)))
503 (goto-char (1- oldpos
))
504 (while (and (not blinkpos
)
505 (search-backward "/" (point-min) t
))
506 (let ((tagend (save-excursion
507 (if (re-search-backward sgml-start-tag-regex
511 (if (eq tagend
(point))
513 (setq blinkpos
(point))
514 (setq level
(1- level
)))
515 (setq level
(1+ level
)))))))
518 (if (pos-visible-in-window-p)
520 (message "Matches %s"
521 (buffer-substring (line-beginning-position)
522 (1+ blinkpos
)))))))))
524 ;; Why doesn't this use the iso-cvt table or, preferably, generate the
525 ;; inverse of the extensive table in the SGML Quail input method? -- fx
526 ;; I guess that's moot since it only works with Latin-1 anyhow.
527 (defun sgml-name-char (&optional char
)
528 "Insert a symbolic character name according to `sgml-char-names'.
529 Non-ASCII chars may be inserted either with the meta key, as in M-SPC for
530 no-break space or M-- for a soft hyphen; or via an input method or
531 encoded keyboard operation."
535 (setq char
(read-quoted-char "Enter char or octal number")))
536 (delete-backward-char 1)
541 (defun sgml-namify-char ()
542 "Change the char before point into its `&name;' equivalent.
543 Uses `sgml-char-names'."
545 (let* ((char (char-before))
548 ((null char
) (error "No char before point"))
549 ((< char
256) (or (aref sgml-char-names char
) char
))
550 ((aref sgml-char-names-table char
))
551 ((encode-char char
'ucs
)))))
553 (error "Don't know the name of `%c'" char
)
554 (delete-backward-char 1)
555 (insert (format (if (numberp name
) "&#%d;" "&%s;") name
)))))
557 (defun sgml-name-self ()
558 "Insert a symbolic character name according to `sgml-char-names'."
560 (sgml-name-char last-command-char
))
562 (defun sgml-maybe-name-self ()
563 "Insert a symbolic character name according to `sgml-char-names'."
565 (if sgml-name-8bit-mode
566 (let ((mc last-command-char
))
568 (setq mc
(unibyte-char-to-multibyte mc
)))
569 (or mc
(setq mc last-command-char
))
571 (self-insert-command 1)))
573 (defun sgml-name-8bit-mode ()
574 "Toggle whether to insert named entities instead of non-ASCII characters.
575 This only works for Latin-1 input."
577 (setq sgml-name-8bit-mode
(not sgml-name-8bit-mode
))
578 (message "sgml name entity mode is now %s"
579 (if sgml-name-8bit-mode
"ON" "OFF")))
581 ;; When an element of a skeleton is a string "str", it is passed
582 ;; through skeleton-transformation and inserted. If "str" is to be
583 ;; inserted literally, one should obtain it as the return value of a
584 ;; function, e.g. (identity "str").
586 (defvar sgml-tag-last nil
)
587 (defvar sgml-tag-history nil
)
588 (define-skeleton sgml-tag
589 "Prompt for a tag and insert it, optionally with attributes.
590 Completion and configuration are done according to `sgml-tag-alist'.
591 If you like tags and attributes in uppercase do \\[set-variable]
592 skeleton-transformation RET upcase RET, or put this in your `.emacs':
593 (setq sgml-transformation 'upcase)"
594 (funcall (or skeleton-transformation
'identity
)
597 (if (> (length sgml-tag-last
) 0)
598 (format "Tag (default %s): " sgml-tag-last
)
600 sgml-tag-alist nil nil nil
'sgml-tag-history sgml-tag-last
)))
602 (("") -
1 '(undo-boundary) (identity "<")) |
; see comment above
603 `(("") '(setq v2
(sgml-attributes ,str t
)) ?
>
607 '(("") " [ " _
" ]]"))
608 ((and (eq v2 t
) sgml-xml-mode
(member ,str sgml-empty-tags
))
610 ((or (and (eq v2 t
) (not sgml-xml-mode
)) (string-match "^[/!?]" ,str
))
613 ;; Make sure we don't fall into an infinite loop.
614 ;; For xhtml's `tr' tag, we should maybe use \n instead.
615 (if (eq v2 t
) (setq v2 nil
))
616 ;; We use `identity' to prevent skeleton from passing
617 ;; `str' through skeleton-transformation a second time.
618 '(("") v2 _ v2
"</" (identity ',str
) ?
>))
620 (cons '("") (cdr v2
)))
622 (append '(("") (car v2
))
624 '(resume: (car v2
) _
"</" (identity ',str
) ?
>))))))
626 (autoload 'skeleton-read
"skeleton")
628 (defun sgml-attributes (tag &optional quiet
)
629 "When at top level of a tag, interactively insert attributes.
631 Completion and configuration of TAG are done according to `sgml-tag-alist'.
632 If QUIET, do not print a message when there are no attributes for TAG."
633 (interactive (list (save-excursion (sgml-beginning-of-tag t
))))
634 (or (stringp tag
) (error "Wrong context for adding attribute"))
636 (let ((completion-ignore-case t
)
637 (alist (cdr (assoc (downcase tag
) sgml-tag-alist
)))
639 (if (or (symbolp (car alist
))
640 (symbolp (car (car alist
))))
641 (setq car
(car alist
)
644 (message "No attributes configured."))
645 (if (stringp (car alist
))
647 (insert (if (eq (preceding-char) ?
) "" ?
)
648 (funcall skeleton-transformation
(car alist
)))
650 (setq i
(length alist
))
653 (insert (funcall skeleton-transformation
655 (skeleton-read '(completing-read
658 (if (string= "" attribute
)
660 (sgml-value (assoc (downcase attribute
) alist
))
662 (if (eq (preceding-char) ?
)
663 (delete-backward-char 1)))
666 (defun sgml-auto-attributes (arg)
667 "Self insert the character typed; at top level of tag, prompt for attributes.
668 With prefix argument, only self insert."
670 (let ((point (point))
673 (not sgml-tag-alist
) ; no message when nothing configured
674 (symbolp (setq tag
(save-excursion (sgml-beginning-of-tag t
))))
675 (eq (aref tag
0) ?
/))
676 (self-insert-command (prefix-numeric-value arg
))
677 (sgml-attributes tag
)
678 (setq last-command-char ?
)
679 (or (> (point) point
)
680 (self-insert-command 1)))))
682 (defun sgml-tag-help (&optional tag
)
683 "Display description of tag TAG. If TAG is omitted, use the tag at point."
687 (if (eq (following-char) ?
<)
689 (setq tag
(sgml-beginning-of-tag))))
691 (error "No tag selected"))
692 (setq tag
(downcase tag
))
694 (or (cdr (assoc (downcase tag
) sgml-tag-help
))
695 (and (eq (aref tag
0) ?
/)
696 (cdr (assoc (downcase (substring tag
1)) sgml-tag-help
)))
697 "No description available")))
699 (defun sgml-maybe-end-tag (&optional arg
)
700 "Name self unless in position to end a tag or a prefix ARG is given."
702 (if (or arg
(eq (car (sgml-lexical-context)) 'tag
))
703 (self-insert-command (prefix-numeric-value arg
))
706 (defun sgml-skip-tag-backward (arg)
707 "Skip to beginning of tag or matching opening tag if present.
708 With prefix argument ARG, repeat this ARG times."
710 ;; FIXME: use sgml-get-context or something similar.
712 (search-backward "<" nil t
)
713 (if (looking-at "</\\([^ \n\t>]+\\)")
714 ;; end tag, skip any nested pairs
715 (let ((case-fold-search t
)
716 (re (concat "</?" (regexp-quote (match-string 1))
717 ;; Ignore empty tags like <foo/>.
718 "\\([^>]*[^/>]\\)?>")))
719 (while (and (re-search-backward re nil t
)
720 (eq (char-after (1+ (point))) ?
/))
722 (sgml-skip-tag-backward 1))))
723 (setq arg
(1- arg
))))
725 (defun sgml-skip-tag-forward (arg)
726 "Skip to end of tag or matching closing tag if present.
727 With prefix argument ARG, repeat this ARG times.
728 Return t iff after a closing tag."
730 ;; FIXME: Use sgml-get-context or something similar.
731 ;; It currently might jump to an unrelated </P> if the <P>
732 ;; we're skipping has no matching </P>.
734 (with-syntax-table sgml-tag-syntax-table
736 (skip-chars-forward "^<>")
737 (if (eq (following-char) ?
>)
739 (if (looking-at "<\\([^/ \n\t>]+\\)\\([^>]*[^/>]\\)?>")
740 ;; start tag, skip any nested same pairs _and_ closing tag
741 (let ((case-fold-search t
)
742 (re (concat "</?" (regexp-quote (match-string 1))
743 ;; Ignore empty tags like <foo/>.
744 "\\([^>]*[^/>]\\)?>"))
748 ;; FIXME: This re-search-forward will mistakenly match
749 ;; tag-like text inside attributes.
750 (while (and (re-search-forward re nil t
)
752 (eq (char-after (1+ (match-beginning 0))) ?
/)))
753 (goto-char (match-beginning 0))
754 (sgml-skip-tag-forward 1))
763 (defun sgml-delete-tag (arg)
764 ;; FIXME: Should be called sgml-kill-tag or should not touch the kill-ring.
765 "Delete tag on or after cursor, and matching closing or opening tag.
766 With prefix argument ARG, repeat this ARG times."
771 (if (looking-at "[ \t\n]*<")
773 (if (eq (char-after (match-end 0)) ?
/)
777 (goto-char (match-end 0))))
779 (or (save-excursion (setq close
(sgml-beginning-of-tag)
780 close
(and (stringp close
)
781 (eq (aref close
0) ?
/)
783 ;; not on closing tag
784 (let ((point (point)))
785 (sgml-skip-tag-backward 1)
786 (if (or (not (eq (following-char) ?
<))
790 (error "Not on or before tag")))))
793 (sgml-skip-tag-backward 1)
798 (when (sgml-skip-tag-forward 1)
800 ;; Delete any resulting empty line. If we didn't kill-sexp,
801 ;; this *should* do nothing, because we're right after the tag.
802 (if (progn (forward-line 0) (looking-at "\\(?:[ \t]*$\\)\n?"))
803 (delete-region (match-beginning 0) (match-end 0)))
806 (if (progn (forward-line 0) (looking-at "\\(?:[ \t]*$\\)\n?"))
807 (delete-region (match-beginning 0) (match-end 0)))))
808 (setq arg
(1- arg
))))
811 ;; Put read-only last to enable setting this even when read-only enabled.
812 (or (get 'sgml-tag
'invisible
)
814 (append '(invisible t
815 point-entered sgml-point-entered
818 (symbol-plist 'sgml-tag
))))
820 (defun sgml-tags-invisible (arg)
821 "Toggle visibility of existing tags."
823 (let ((modified (buffer-modified-p))
824 (inhibit-read-only t
)
825 (inhibit-modification-hooks t
)
826 ;; Avoid spurious the `file-locked' checks.
827 (buffer-file-name nil
)
828 ;; This is needed in case font lock gets called,
829 ;; since it moves point and might call sgml-point-entered.
830 ;; How could it get called? -stef
831 (inhibit-point-motion-hooks t
)
835 (goto-char (point-min))
836 (if (set (make-local-variable 'sgml-tags-invisible
)
838 (>= (prefix-numeric-value arg
) 0)
839 (not sgml-tags-invisible
)))
840 (while (re-search-forward sgml-tag-name-re nil t
)
842 (cdr (assq (intern-soft (downcase (match-string 1)))
844 (goto-char (match-beginning 0))
845 (and (stringp string
)
846 (not (overlays-at (point)))
847 (let ((ol (make-overlay (point) (match-beginning 1))))
848 (overlay-put ol
'before-string string
)
849 (overlay-put ol
'sgml-tag t
)))
850 (put-text-property (point)
851 (progn (forward-list) (point))
852 'category
'sgml-tag
))
853 (let ((pos (point-min)))
854 (while (< (setq pos
(next-overlay-change pos
)) (point-max))
855 (dolist (ol (overlays-at pos
))
856 (if (overlay-get ol
'sgml-tag
)
857 (delete-overlay ol
)))))
858 (remove-text-properties (point-min) (point-max) '(category nil
))))
859 (restore-buffer-modified-p modified
))
860 (run-hooks 'sgml-tags-invisible-hook
)
863 (defun sgml-point-entered (x y
)
864 ;; Show preceding or following hidden tag, depending of cursor direction.
865 (let ((inhibit-point-motion-hooks t
))
867 (message "Invisible tag: %s"
868 ;; Strip properties, otherwise, the text is invisible.
869 (buffer-substring-no-properties
872 (not (eq (following-char) ?
<)))
874 (eq (preceding-char) ?
>)))
879 (autoload 'compile-internal
"compile")
881 (defun sgml-validate (command)
882 "Validate an SGML document.
883 Runs COMMAND, a shell command, in a separate process asynchronously
884 with output going to the buffer `*compilation*'.
885 You can then use the command \\[next-error] to find the next error message
886 and move to the line in the SGML document that caused it."
888 (list (read-string "Validate command: "
889 (or sgml-saved-validate-command
890 (concat sgml-validate-command
892 (let ((name (buffer-file-name)))
894 (file-name-nondirectory name
))))))))
895 (setq sgml-saved-validate-command command
)
896 (save-some-buffers (not compilation-ask-about-save
) nil
)
897 (compile-internal command
"No more errors"))
899 (defsubst sgml-at-indentation-p
()
900 "Return true if point is at the first non-whitespace character on the line."
902 (skip-chars-backward " \t")
905 (defun sgml-lexical-context (&optional limit
)
906 "Return the lexical context at point as (TYPE . START).
907 START is the location of the start of the lexical element.
908 TYPE is one of `string', `comment', `tag', `cdata', or `text'.
910 Optional argument LIMIT is the position to start parsing from.
911 If nil, start from a preceding tag at indentation."
917 ;; Skip tags backwards until we find one at indentation
918 (while (and (ignore-errors (sgml-parse-tag-backward))
919 (not (sgml-at-indentation-p)))))
920 (with-syntax-table sgml-tag-syntax-table
921 (while (< (point) pos
)
922 ;; When entering this loop we're inside text.
923 (setq text-start
(point))
924 (skip-chars-forward "^<" pos
)
928 ;; We got to the end without seeing a tag.
930 ((looking-at "<!\\[[A-Z]+\\[")
931 ;; We've found a CDATA section or similar.
932 (let ((cdata-start (point)))
933 (unless (search-forward "]]>" pos
'move
)
934 (list 0 nil nil
'cdata nil nil nil nil cdata-start
))))
936 ;; We've reached a tag. Parse it.
937 ;; FIXME: Handle net-enabling start-tags
938 (parse-partial-sexp (point) pos
0))))))
940 ((eq (nth 3 state
) 'cdata
) (cons 'cdata
(nth 8 state
)))
941 ((nth 3 state
) (cons 'string
(nth 8 state
)))
942 ((nth 4 state
) (cons 'comment
(nth 8 state
)))
943 ((and state
(> (nth 0 state
) 0)) (cons 'tag
(nth 1 state
)))
944 (t (cons 'text text-start
))))))
946 (defun sgml-beginning-of-tag (&optional top-level
)
947 "Skip to beginning of tag and return its name.
948 If this can't be done, return nil."
949 (let ((context (sgml-lexical-context)))
950 (if (eq (car context
) 'tag
)
952 (goto-char (cdr context
))
953 (when (looking-at sgml-tag-name-re
)
954 (match-string-no-properties 1)))
956 (when (not (eq (car context
) 'text
))
957 (goto-char (cdr context
))
958 (sgml-beginning-of-tag t
))))))
960 (defun sgml-value (alist)
961 "Interactively insert value taken from attribute-rule ALIST.
962 See `sgml-tag-alist' for info about attribute rules."
963 (setq alist
(cdr alist
))
964 (if (stringp (car alist
))
965 (insert "=\"" (car alist
) ?
\")
966 (if (and (eq (car alist
) t
) (not sgml-xml-mode
))
969 (setq alist
(skeleton-read '(completing-read "Value: " (cdr alist
))))
970 (if (string< "" alist
)
972 (delete-backward-char 2)))
975 (insert (skeleton-read '(completing-read "Value: " alist
))))
978 (defun sgml-quote (start end
&optional unquotep
)
979 "Quote SGML text in region START ... END.
980 Only &, < and > are quoted, the rest is left untouched.
981 With prefix argument UNQUOTEP, unquote the region."
984 (narrow-to-region start end
)
985 (goto-char (point-min))
987 ;; FIXME: We should unquote other named character references as well.
988 (while (re-search-forward
989 "\\(&\\(amp\\|\\(l\\|\\(g\\)\\)t\\)\\)[][<>&;\n\t \"%!'(),/=?]"
991 (replace-match (if (match-end 4) ">" (if (match-end 3) "<" "&")) t t
992 nil
(if (eq (char-before (match-end 0)) ?\
;) 0 1)))
993 (while (re-search-forward "[&<>]" nil t
)
994 (replace-match (cdr (assq (char-before) '((?
& .
"&")
999 (defun sgml-pretty-print (beg end
)
1000 "Simple-minded pretty printer for SGML.
1001 Re-indents the code and inserts newlines between BEG and END.
1002 You might want to turn on `auto-fill-mode' to get better results."
1004 ;; - insert newline between some start-tag and text.
1005 ;; - don't insert newline in front of some end-tags.
1013 ;; Don't use narrowing because it screws up auto-indent.
1014 (setq end
(copy-marker end t
))
1015 (with-syntax-table sgml-tag-syntax-table
1016 (while (re-search-forward "<" end t
)
1017 (goto-char (match-beginning 0))
1018 (unless (or ;;(looking-at "</")
1019 (progn (skip-chars-backward " \t") (bolp)))
1020 (reindent-then-newline-and-indent))
1022 ;; (indent-region beg end)
1028 (defstruct (sgml-tag
1029 (:constructor sgml-make-tag
(type start end name
)))
1030 type start end name
)
1032 (defsubst sgml-parse-tag-name
()
1033 "Skip past a tag-name, and return the name."
1034 (buffer-substring-no-properties
1035 (point) (progn (skip-syntax-forward "w_") (point))))
1037 (defsubst sgml-looking-back-at
(str)
1038 "Return t if the test before point matches STR."
1039 (let ((start (- (point) (length str
))))
1040 (and (>= start
(point-min))
1041 (equal str
(buffer-substring-no-properties start
(point))))))
1043 (defun sgml-parse-tag-backward (&optional limit
)
1044 "Parse an SGML tag backward, and return information about the tag.
1045 Assume that parsing starts from within a textual context.
1046 Leave point at the beginning of the tag."
1047 (let (tag-type tag-start tag-end name
)
1048 (or (search-backward ">" limit
'move
)
1049 (error "No tag found"))
1050 (setq tag-end
(1+ (point)))
1052 ((sgml-looking-back-at "--") ; comment
1053 (setq tag-type
'comment
1054 tag-start
(search-backward "<!--" nil t
)))
1055 ((sgml-looking-back-at "]]") ; cdata
1056 (setq tag-type
'cdata
1057 tag-start
(re-search-backward "<!\\[[A-Z]+\\[" nil t
)))
1060 (with-syntax-table sgml-tag-syntax-table
1064 (goto-char (1+ tag-start
))
1067 (setq tag-type
'decl
))
1068 (??
; processing-instruction
1069 (setq tag-type
'pi
))
1072 (setq tag-type
'close
1073 name
(sgml-parse-tag-name)))
1075 (setq tag-type
'jsp
))
1076 (t ; open or empty tag
1077 (setq tag-type
'open
1078 name
(sgml-parse-tag-name))
1079 (if (or (eq ?
/ (char-before (- tag-end
1)))
1080 (sgml-empty-tag-p name
))
1081 (setq tag-type
'empty
))))))
1082 (goto-char tag-start
)
1083 (sgml-make-tag tag-type tag-start tag-end name
)))
1085 (defun sgml-get-context (&optional full
)
1086 "Determine the context of the current position.
1087 If FULL is `empty', return even if the context is empty (i.e.
1088 we just skipped over some element and got to a beginning of line).
1089 If FULL is non-nil, parse back to the beginning of the buffer, otherwise
1090 parse until we find a start-tag as the first thing on a line.
1092 The context is a list of tag-info structures. The last one is the tag
1093 immediately enclosing the current position."
1094 (let ((here (point))
1098 ;; CONTEXT keeps track of the tag-stack
1099 ;; IGNORE keeps track of the nesting level of point relative to the
1100 ;; first (outermost) tag on the context. This is the list of
1101 ;; enclosing start-tags we'll have to ignore.
1102 (skip-chars-backward " \t\n") ; Make sure we're not at indentation.
1105 (not (if full
(eq full
'empty
) context
))
1106 (not (sgml-at-indentation-p))
1108 (/= (point) (sgml-tag-start (car context
)))
1109 (sgml-unclosed-tag-p (sgml-tag-name (car context
)))))
1110 (setq tag-info
(ignore-errors (sgml-parse-tag-backward))))
1112 ;; This tag may enclose things we thought were tags. If so,
1115 (> (sgml-tag-end tag-info
)
1116 (sgml-tag-end (car context
))))
1117 (setq context
(cdr context
)))
1122 ((eq (sgml-tag-type tag-info
) 'open
)
1126 (sgml-unclosed-tag-p (sgml-tag-name tag-info
))
1127 (eq t
(compare-strings
1128 (sgml-tag-name tag-info
) nil nil
1129 (sgml-tag-name (car context
)) nil nil t
)))
1130 ;; There was an implicit end-tag.
1132 (push tag-info context
)))
1133 ((eq t
(compare-strings (sgml-tag-name tag-info
) nil nil
1134 (car ignore
) nil nil t
))
1135 (setq ignore
(cdr ignore
)))
1137 ;; The open and close tags don't match.
1138 (if (not sgml-xml-mode
)
1139 (unless (sgml-unclosed-tag-p (sgml-tag-name tag-info
))
1140 (message "Unclosed tag <%s>" (sgml-tag-name tag-info
))
1142 ;; We could just assume that the tag is simply not closed
1143 ;; but it's a bad assumption when tags *are* closed but
1144 ;; not properly nested.
1145 (while (and (cdr tmp
)
1146 (not (eq t
(compare-strings
1147 (sgml-tag-name tag-info
) nil nil
1148 (cadr tmp
) nil nil t
))))
1149 (setq tmp
(cdr tmp
)))
1150 (if (cdr tmp
) (setcdr tmp
(cddr tmp
)))))
1151 (message "Unmatched tags <%s> and </%s>"
1152 (sgml-tag-name tag-info
) (pop ignore
))))))
1155 ((eq (sgml-tag-type tag-info
) 'close
)
1156 (if (sgml-empty-tag-p (sgml-tag-name tag-info
))
1157 (message "Spurious </%s>: empty tag" (sgml-tag-name tag-info
))
1158 (push (sgml-tag-name tag-info
) ignore
)))
1164 (defun sgml-show-context (&optional full
)
1165 "Display the current context.
1166 If FULL is non-nil, parse back to the beginning of the buffer."
1168 (with-output-to-temp-buffer "*XML Context*"
1170 (let ((context (sgml-get-context)))
1173 (while (setq more
(sgml-get-context))
1174 (setq context
(nconc more context
)))))
1178 ;; Editing shortcuts
1180 (defun sgml-close-tag ()
1181 "Close current element.
1182 Depending on context, inserts a matching close-tag, or closes
1183 the current start-tag or the current comment or the current cdata, ..."
1185 (case (car (sgml-lexical-context))
1186 (comment (insert " -->"))
1187 (cdata (insert "]]>"))
1189 (jsp (insert " %>"))
1190 (tag (insert " />"))
1192 (let ((context (save-excursion (sgml-get-context))))
1195 (insert "</" (sgml-tag-name (car (last context
))) ">")
1196 (indent-according-to-mode)))))
1198 (error "Nothing to close"))))
1200 (defun sgml-empty-tag-p (tag-name)
1201 "Return non-nil if TAG-NAME is an implicitly empty tag."
1202 (and (not sgml-xml-mode
)
1203 (member-ignore-case tag-name sgml-empty-tags
)))
1205 (defun sgml-unclosed-tag-p (tag-name)
1206 "Return non-nil if TAG-NAME is a tag for which an end-tag is optional."
1207 (and (not sgml-xml-mode
)
1208 (member-ignore-case tag-name sgml-unclosed-tags
)))
1210 (defun sgml-calculate-indent ()
1211 "Calculate the column to which this line should be indented."
1212 (let ((lcon (sgml-lexical-context)))
1214 ;; Indent comment-start markers inside <!-- just like comment-end markers.
1215 (if (and (eq (car lcon
) 'tag
)
1217 (save-excursion (goto-char (cdr lcon
)) (looking-at "<!--")))
1218 (setq lcon
(cons 'comment
(+ (cdr lcon
) 2))))
1223 ;; Go back to previous non-empty line.
1224 (while (and (> (point) (cdr lcon
))
1225 (zerop (forward-line -
1))
1226 (looking-at "[ \t]*$")))
1227 (if (> (point) (cdr lcon
))
1228 ;; Previous line is inside the string.
1229 (current-indentation)
1230 (goto-char (cdr lcon
))
1231 (1+ (current-column))))
1234 (let ((mark (looking-at "--")))
1235 ;; Go back to previous non-empty line.
1236 (while (and (> (point) (cdr lcon
))
1237 (zerop (forward-line -
1))
1238 (or (looking-at "[ \t]*$")
1239 (if mark
(not (looking-at "[ \t]*--"))))))
1240 (if (> (point) (cdr lcon
))
1241 ;; Previous line is inside the comment.
1242 (skip-chars-forward " \t")
1243 (goto-char (cdr lcon
)))
1244 (when (and (not mark
) (looking-at "--"))
1245 (forward-char 2) (skip-chars-forward " \t"))
1252 (goto-char (1+ (cdr lcon
)))
1253 (skip-chars-forward "^ \t\n") ;Skip tag name.
1254 (skip-chars-forward " \t")
1257 ;; This is the first attribute: indent.
1258 (goto-char (1+ (cdr lcon
)))
1259 (+ (current-column) sgml-basic-offset
)))
1262 (while (looking-at "</")
1264 (skip-chars-forward " \t"))
1265 (let* ((here (point))
1266 (unclosed (and ;; (not sgml-xml-mode)
1267 (looking-at sgml-tag-name-re
)
1268 (member-ignore-case (match-string 1)
1272 ;; If possible, align on the previous non-empty text line.
1273 ;; Otherwise, do a more serious parsing to find the
1274 ;; tag(s) relative to which we should be indenting.
1275 (if (and (not unclosed
) (skip-chars-backward " \t")
1276 (< (skip-chars-backward " \t\n") 0)
1277 (back-to-indentation)
1278 (> (point) (cdr lcon
)))
1281 (nreverse (sgml-get-context (if unclosed nil
'empty
)))))
1283 ;; Ignore previous unclosed start-tag in context.
1284 (while (and context unclosed
1285 (eq t
(compare-strings
1286 (sgml-tag-name (car context
)) nil nil
1287 unclosed nil nil t
)))
1288 (setq context
(cdr context
)))
1289 ;; Indent to reflect nesting.
1291 (goto-char (sgml-tag-end (car context
)))
1292 (skip-chars-forward " \t\n")
1293 (< (point) here
) (sgml-at-indentation-p))
1297 (* sgml-basic-offset
(length context
))))))
1300 (error "Unrecognised context %s" (car lcon
)))
1304 (defun sgml-indent-line ()
1305 "Indent the current line as SGML."
1307 (let* ((savep (point))
1310 (back-to-indentation)
1311 (if (>= (point) savep
) (setq savep nil
))
1312 (sgml-calculate-indent))))
1314 (save-excursion (indent-line-to indent-col
))
1315 (indent-line-to indent-col
))))
1317 (defun sgml-guess-indent ()
1318 "Guess an appropriate value for `sgml-basic-offset'.
1319 Base the guessed identation level on the first indented tag in the buffer.
1320 Add this to `sgml-mode-hook' for convenience."
1323 (goto-char (point-min))
1324 (if (re-search-forward "^\\([ \t]+\\)<" 500 'noerror
)
1326 (set (make-local-variable 'sgml-basic-offset
)
1327 (1- (current-column)))
1328 (message "Guessed sgml-basic-offset = %d"
1332 (defun sgml-parse-dtd ()
1333 "Simplistic parse of the current buffer as a DTD.
1334 Currently just returns (EMPTY-TAGS UNCLOSED-TAGS)."
1335 (goto-char (point-min))
1338 (while (re-search-forward "<!ELEMENT[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+[-O][ \t\n]+\\([-O]\\)[ \t\n]+\\([^ \t\n]+\\)" nil t
)
1340 ((string= (match-string 3) "EMPTY")
1341 (push (match-string-no-properties 1) empty
))
1342 ((string= (match-string 2) "O")
1343 (push (match-string-no-properties 1) unclosed
))))
1344 (setq empty
(sort (mapcar 'downcase empty
) 'string
<))
1345 (setq unclosed
(sort (mapcar 'downcase unclosed
) 'string
<))
1346 (list empty unclosed
)))
1350 (defcustom html-mode-hook nil
1351 "Hook run by command `html-mode'.
1352 `text-mode-hook' and `sgml-mode-hook' are run first."
1355 :options
'(html-autoview-mode))
1357 (defvar html-quick-keys sgml-quick-keys
1358 "Use C-c X combinations for quick insertion of frequent tags when non-nil.
1359 This defaults to `sgml-quick-keys'.
1360 This takes effect when first loading the library.")
1362 (defvar html-mode-map
1363 (let ((map (make-sparse-keymap))
1364 (menu-map (make-sparse-keymap "HTML")))
1365 (set-keymap-parent map sgml-mode-map
)
1366 (define-key map
"\C-c6" 'html-headline-6
)
1367 (define-key map
"\C-c5" 'html-headline-5
)
1368 (define-key map
"\C-c4" 'html-headline-4
)
1369 (define-key map
"\C-c3" 'html-headline-3
)
1370 (define-key map
"\C-c2" 'html-headline-2
)
1371 (define-key map
"\C-c1" 'html-headline-1
)
1372 (define-key map
"\C-c\r" 'html-paragraph
)
1373 (define-key map
"\C-c\n" 'html-line
)
1374 (define-key map
"\C-c\C-c-" 'html-horizontal-rule
)
1375 (define-key map
"\C-c\C-co" 'html-ordered-list
)
1376 (define-key map
"\C-c\C-cu" 'html-unordered-list
)
1377 (define-key map
"\C-c\C-cr" 'html-radio-buttons
)
1378 (define-key map
"\C-c\C-cc" 'html-checkboxes
)
1379 (define-key map
"\C-c\C-cl" 'html-list-item
)
1380 (define-key map
"\C-c\C-ch" 'html-href-anchor
)
1381 (define-key map
"\C-c\C-cn" 'html-name-anchor
)
1382 (define-key map
"\C-c\C-ci" 'html-image
)
1383 (when html-quick-keys
1384 (define-key map
"\C-c-" 'html-horizontal-rule
)
1385 (define-key map
"\C-co" 'html-ordered-list
)
1386 (define-key map
"\C-cu" 'html-unordered-list
)
1387 (define-key map
"\C-cr" 'html-radio-buttons
)
1388 (define-key map
"\C-cc" 'html-checkboxes
)
1389 (define-key map
"\C-cl" 'html-list-item
)
1390 (define-key map
"\C-ch" 'html-href-anchor
)
1391 (define-key map
"\C-cn" 'html-name-anchor
)
1392 (define-key map
"\C-ci" 'html-image
))
1393 (define-key map
"\C-c\C-s" 'html-autoview-mode
)
1394 (define-key map
"\C-c\C-v" 'browse-url-of-buffer
)
1395 (define-key map
[menu-bar html
] (cons "HTML" menu-map
))
1396 (define-key menu-map
[html-autoview-mode
]
1397 '("Toggle Autoviewing" . html-autoview-mode
))
1398 (define-key menu-map
[browse-url-of-buffer
]
1399 '("View Buffer Contents" . browse-url-of-buffer
))
1400 (define-key menu-map
[nil] '("--"))
1401 ;;(define-key menu-map "6" '("Heading 6" . html-headline-6))
1402 ;;(define-key menu-map "5" '("Heading 5" . html-headline-5))
1403 ;;(define-key menu-map "4" '("Heading 4" . html-headline-4))
1404 (define-key menu-map "3" '("Heading 3" . html-headline-3))
1405 (define-key menu-map "2" '("Heading 2" . html-headline-2))
1406 (define-key menu-map "1" '("Heading 1" . html-headline-1))
1407 (define-key menu-map "l" '("Radio Buttons" . html-radio-buttons))
1408 (define-key menu-map "c" '("Checkboxes" . html-checkboxes))
1409 (define-key menu-map "l" '("List Item" . html-list-item))
1410 (define-key menu-map "u" '("Unordered List" . html-unordered-list))
1411 (define-key menu-map "o" '("Ordered List" . html-ordered-list))
1412 (define-key menu-map "-" '("Horizontal Rule" . html-horizontal-rule))
1413 (define-key menu-map "\n" '("Line Break" . html-line))
1414 (define-key menu-map "\r" '("Paragraph" . html-paragraph))
1415 (define-key menu-map "i" '("Image" . html-image))
1416 (define-key menu-map "h" '("Href Anchor" . html-href-anchor))
1417 (define-key menu-map "n" '("Name Anchor" . html-name-anchor))
1419 "Keymap for commands for use in HTML mode.")
1421 (defvar html-face-tag-alist
1426 "Value of `sgml-face-tag-alist' for HTML mode.")
1428 (defvar html-tag-face-alist
1431 ("blink" . highlight)
1434 ("h1" bold underline)
1435 ("h2" bold-italic underline)
1436 ("h3" italic underline)
1445 ("title" bold underline)
1449 "Value of `sgml-tag-face-alist' for HTML mode.")
1451 (defvar html-display-text
1455 "Value of `sgml-display-text' for HTML mode.")
1458 ;; should code exactly HTML 3 here when that is finished
1459 (defvar html-tag-alist
1460 (let* ((1-7 '(("1") ("2") ("3") ("4") ("5") ("6") ("7")))
1461 (1-9 `(,@1-7 ("8") ("9")))
1462 (align '(("align" ("left") ("center") ("right"))))
1463 (valign '(("top") ("middle") ("bottom") ("baseline")))
1464 (rel '(("next") ("previous") ("parent") ("subdocument") ("made")))
1465 (href '("href" ("ftp:") ("file:") ("finger:") ("gopher:") ("http:")
1466 ("mailto:") ("news:") ("rlogin:") ("telnet:") ("tn3270:")
1467 ("wais:") ("/cgi-bin/")))
1473 (list '((nil \n ("List item: " "<li>" str
1474 (if sgml-xml-mode "</li>") \n))))
1481 ;; put ,-expressions first, else byte-compile chokes (as of V19.29)
1482 ;; and like this it's more efficient anyway
1483 `(("a" ,name ,@link)
1486 ("font" nil "size" ("-1") ("+1") ("-2") ("+2") ,@1-7)
1487 ("form" (\n _ \n "<input type=\"submit\" value=\"\""
1488 (if sgml-xml-mode "/>" ">"))
1489 ("action" ,@(cdr href)) ("method" ("get") ("post")))
1496 ("hr" t ("size" ,@1-9) ("width") ("noshade" t) ,@align)
1497 ("img" t ("align" ,@valign ("texttop") ("absmiddle") ("absbottom"))
1498 ("src") ("alt") ("width" "1") ("height" "1")
1499 ("border" "1") ("vspace" "1") ("hspace" "1") ("ismap" t))
1500 ("input" t ("size" ,@1-9) ("maxlength" ,@1-9) ("checked" t) ,name
1501 ("type" ("text") ("password") ("checkbox") ("radio")
1502 ("submit") ("reset"))
1506 ("ol" ,@list ("type" ("A") ("a") ("I") ("i") ("1")))
1510 "<option>" str (if sgml-xml-mode "</option>") \n))
1511 ,name ("size" ,@1-9) ("multiple" t))
1513 ((completing-read "Cell kind: " '(("td") ("th"))
1516 (if sgml-xml-mode (concat "<" str "></tr>")) \n))
1517 ("border" t ,@1-9) ("width" "10") ("cellpadding"))
1519 ("textarea" ,name ("rows" ,@1-9) ("cols" ,@1-9))
1521 ("ul" ,@list ("type" ("disc") ("circle") ("square")))
1529 ("Item: " "<item>" str (if sgml-xml-mode "</item>") \n))
1536 ("body" \n ("background" ".gif") ("bgcolor" "#") ("text" "#")
1537 ("link" "#") ("alink" "#") ("vlink" "#"))
1538 ("box" (nil _ "<over>" _ (if sgml-xml-mode "</over>")))
1539 ("br" t ("clear" ("left") ("right")))
1540 ("caption" ("valign" ("top") ("bottom")))
1544 ("dd" ,(not sgml-xml-mode))
1550 "<dt>" str (if sgml-xml-mode "</dt>")
1551 "<dd>" _ (if sgml-xml-mode "</dd>") \n)))
1552 ("dt" (t _ (if sgml-xml-mode "</dt>")
1553 "<dd>" (if sgml-xml-mode "</dd>") \n))
1555 ;("fn" "id" "fn") ; ???
1559 "<title>" (setq str (read-input "Title: ")) "</title>\n"
1561 "<body>\n<h1>" str "</h1>\n" _
1562 "\n<address>\n<a href=\"mailto:"
1564 "\">" (user-full-name) "</a>\n</address>\n"
1569 ("isindex" t ("action") ("prompt"))
1572 ("li" ,(not sgml-xml-mode))
1575 ("option" t ("value") ("label") ("selected" t))
1604 "*Value of `sgml-tag-alist' for HTML mode.")
1606 (defvar html-tag-help
1608 ("a" . "Anchor of point or link elsewhere")
1611 ("address" . "Formatted mail address")
1612 ("array" . "Math array")
1615 ("base" . "Base address for URLs")
1616 ("big" . "Font size")
1617 ("blink" . "Blinking text")
1618 ("blockquote" . "Indented quotation")
1619 ("body" . "Document body")
1620 ("box" . "Math fraction")
1621 ("br" . "Line break")
1622 ("caption" . "Table caption")
1623 ("center" . "Centered text")
1624 ("changed" . "Change bars")
1625 ("cite" . "Citation of a document")
1626 ("code" . "Formatted source code")
1627 ("dd" . "Definition of term")
1630 ("dir" . "Directory list (obsolete)")
1631 ("dl" . "Definition list")
1632 ("dt" . "Term to be definined")
1633 ("em" . "Emphasised")
1634 ("embed" . "Embedded data in foreign format")
1636 ("figa" . "Figure anchor")
1637 ("figd" . "Figure description")
1638 ("figt" . "Figure text")
1640 ("font" . "Font size")
1641 ("form" . "Form with input fields")
1642 ("group" . "Document grouping")
1643 ("h1" . "Most important section headline")
1644 ("h2" . "Important section headline")
1645 ("h3" . "Section headline")
1646 ("h4" . "Minor section headline")
1647 ("h5" . "Unimportant section headline")
1648 ("h6" . "Least important section headline")
1649 ("head" . "Document header")
1650 ("hr" . "Horizontal rule")
1651 ("html" . "HTML Document")
1652 ("i" . "Italic face")
1653 ("img" . "Graphic image")
1654 ("input" . "Form input field")
1656 ("isindex" . "Input field for index search")
1657 ("kbd" . "Keybard example face")
1658 ("lang" . "Natural language")
1659 ("li" . "List item")
1660 ("link" . "Link relationship")
1661 ("math" . "Math formula")
1662 ("menu" . "Menu list (obsolete)")
1663 ("mh" . "Form mail header")
1664 ("nextid" . "Allocate new id")
1665 ("nobr" . "Text without line break")
1666 ("ol" . "Ordered list")
1667 ("option" . "Selection list item")
1668 ("over" . "Math fraction rule")
1669 ("p" . "Paragraph start")
1670 ("panel" . "Floating panel")
1672 ("pre" . "Preformatted fixed width text")
1674 ("rev" . "Reverse video")
1676 ("samp" . "Sample text")
1677 ("select" . "Selection list")
1678 ("small" . "Font size")
1679 ("sp" . "Nobreak space")
1680 ("strong" . "Standout text")
1681 ("sub" . "Subscript")
1682 ("sup" . "Superscript")
1683 ("table" . "Table with rows and columns")
1684 ("tb" . "Table vertical break")
1685 ("td" . "Table data cell")
1686 ("textarea" . "Form multiline edit area")
1687 ("th" . "Table header cell")
1688 ("title" . "Document title")
1689 ("tr" . "Table row separator")
1690 ("tt" . "Typewriter face")
1691 ("u" . "Underlined text")
1692 ("ul" . "Unordered list")
1693 ("var" . "Math variable face")
1694 ("wbr" . "Enable <br> within <nobr>"))
1695 "*Value of `sgml-tag-help' for HTML mode.")
1699 (define-derived-mode html-mode sgml-mode "HTML"
1700 "Major mode based on SGML mode for editing HTML documents.
1701 This allows inserting skeleton constructs used in hypertext documents with
1702 completion. See below for an introduction to HTML. Use
1703 \\[browse-url-of-buffer] to see how this comes out. See also `sgml-mode' on
1704 which this is based.
1706 Do \\[describe-variable] html- SPC and \\[describe-variable] sgml- SPC to see available variables.
1708 To write fairly well formatted pages you only need to know few things. Most
1709 browsers have a function to read the source code of the page being seen, so
1710 you can imitate various tricks. Here's a very short HTML primer which you
1711 can also view with a browser to see what happens:
1713 <title>A Title Describing Contents</title> should be on every page. Pages can
1714 have <h1>Very Major Headlines</h1> through <h6>Very Minor Headlines</h6>
1715 <hr> Parts can be separated with horizontal rules.
1717 <p>Paragraphs only need an opening tag. Line breaks and multiple spaces are
1718 ignored unless the text is <pre>preformatted.</pre> Text can be marked as
1719 <b>bold</b>, <i>italic</i> or <u>underlined</u> using the normal M-g or
1720 Edit/Text Properties/Face commands.
1722 Pages can have <a name=\"SOMENAME\">named points</a> and can link other points
1723 to them with <a href=\"#SOMENAME\">see also somename</a>. In the same way <a
1724 href=\"URL\">see also URL</a> where URL is a filename relative to current
1725 directory, or absolute as in `http://www.cs.indiana.edu/elisp/w3/docs.html'.
1727 Images in many formats can be inlined with <img src=\"URL\">.
1729 If you mainly create your own documents, `sgml-specials' might be
1730 interesting. But note that some HTML 2 browsers can't handle `''.
1731 To work around that, do:
1732 (eval-after-load \"sgml-mode\" '(aset sgml-char-names ?' nil))
1735 (set (make-local-variable 'sgml-display-text) html-display-text)
1736 (set (make-local-variable 'sgml-tag-face-alist) html-tag-face-alist)
1737 (make-local-variable 'sgml-tag-alist)
1738 (make-local-variable 'sgml-face-tag-alist)
1739 (make-local-variable 'sgml-tag-help)
1740 (make-local-variable 'outline-regexp)
1741 (make-local-variable 'outline-heading-end-regexp)
1742 (make-local-variable 'outline-level)
1743 (make-local-variable 'sentence-end)
1745 (if sentence-end-double-space
1746 "[.?!][]\"')}]*\\(<[^>]*>\\)*\\($\\| $\\|\t\\| \\)[ \t\n]*"
1747 "[.?!][]\"')}]*\\(<[^>]*>\\)*\\($\\|[ \t]\\)[ \t\n]*"))
1748 (setq sgml-tag-alist html-tag-alist
1749 sgml-face-tag-alist html-face-tag-alist
1750 sgml-tag-help html-tag-help
1751 outline-regexp "^.*<[Hh][1-6]\\>"
1752 outline-heading-end-regexp "</[Hh][1-6]>"
1753 outline-level (lambda ()
1754 (char-before (match-end 0))))
1755 (setq imenu-create-index-function 'html-imenu-index)
1756 (when sgml-xml-mode (setq mode-name "XHTML"))
1757 (set (make-local-variable 'sgml-empty-tags)
1758 ;; From HTML-4.01's loose.dtd, parsed with `sgml-parse-dtd',
1759 ;; plus manual addition of "wbr".
1760 '("area" "base" "basefont" "br" "col" "frame" "hr" "img" "input"
1761 "isindex" "link" "meta" "param" "wbr"))
1762 (set (make-local-variable 'sgml-unclosed-tags)
1763 ;; From HTML-4.01's loose.dtd, parsed with `sgml-parse-dtd'.
1764 '("body" "colgroup" "dd" "dt" "head" "html" "li" "option"
1765 "p" "tbody" "td" "tfoot" "th" "thead" "tr"))
1766 ;; It's for the user to decide if it defeats it or not -stef
1767 ;; (make-local-variable 'imenu-sort-function)
1768 ;; (setq imenu-sort-function nil) ; sorting the menu defeats the purpose
1771 (defvar html-imenu-regexp
1772 "\\s-*<h\\([1-9]\\)[^\n<>]*>\\(<[^\n<>]*>\\)*\\s-*\\([^\n<>]*\\)"
1773 "*A regular expression matching a head line to be added to the menu.
1774 The first `match-string' should be a number from 1-9.
1775 The second `match-string' matches extra tags and is ignored.
1776 The third `match-string' will be the used in the menu.")
1778 (defun html-imenu-index ()
1779 "Return a table of contents for an HTML buffer for use with Imenu."
1782 (goto-char (point-min))
1783 (while (re-search-forward html-imenu-regexp nil t)
1785 (cons (cons (concat (make-string
1786 (* 2 (1- (string-to-number (match-string 1))))
1789 (line-beginning-position))
1791 (nreverse toc-index)))
1793 (define-minor-mode html-autoview-mode
1794 "Toggle automatic viewing via `browse-url-of-buffer' upon saving buffer.
1795 With positive prefix ARG always turns viewing on, with negative ARG always off.
1796 Can be used as a value for `html-mode-hook'."
1799 (if html-autoview-mode
1800 (add-hook 'after-save-hook 'browse-url-of-buffer nil t)
1801 (remove-hook 'after-save-hook 'browse-url-of-buffer t)))
1804 (define-skeleton html-href-anchor
1805 "HTML anchor tag with href attribute."
1807 '(setq input "http:")
1808 "<a href=\"" str "\">" _ "</a>")
1810 (define-skeleton html-name-anchor
1811 "HTML anchor tag with name attribute."
1813 "<a name=\"" str "\">" _ "</a>")
1815 (define-skeleton html-headline-1
1816 "HTML level 1 headline tags."
1820 (define-skeleton html-headline-2
1821 "HTML level 2 headline tags."
1825 (define-skeleton html-headline-3
1826 "HTML level 3 headline tags."
1830 (define-skeleton html-headline-4
1831 "HTML level 4 headline tags."
1835 (define-skeleton html-headline-5
1836 "HTML level 5 headline tags."
1840 (define-skeleton html-headline-6
1841 "HTML level 6 headline tags."
1845 (define-skeleton html-horizontal-rule
1846 "HTML horizontal rule tag."
1848 (if sgml-xml-mode "<hr/>" "<hr>") \n)
1850 (define-skeleton html-image
1853 "<img src=\"" _ "\""
1854 (if sgml-xml-mode "/>" ">"))
1856 (define-skeleton html-line
1857 "HTML line break tag."
1859 (if sgml-xml-mode "<br/>" "<br>") \n)
1861 (define-skeleton html-ordered-list
1862 "HTML ordered list tags."
1865 "<li>" _ (if sgml-xml-mode "</li>") \n
1868 (define-skeleton html-unordered-list
1869 "HTML unordered list tags."
1872 "<li>" _ (if sgml-xml-mode "</li>") \n
1875 (define-skeleton html-list-item
1876 "HTML list item tag."
1879 "<li>" _ (if sgml-xml-mode "</li>"))
1881 (define-skeleton html-paragraph
1882 "HTML paragraph tag."
1885 \n "<p>" _ (if sgml-xml-mode "</p>"))
1887 (define-skeleton html-checkboxes
1888 "Group of connected checkbox inputs."
1893 "<input type=\"" (identity "checkbox") ; see comment above about identity
1894 "\" name=\"" (or v1 (setq v1 (skeleton-read "Name: ")))
1895 "\" value=\"" str ?\"
1896 (when (y-or-n-p "Set \"checked\" attribute? ")
1897 (funcall skeleton-transformation " checked"))
1898 (if sgml-xml-mode "/>" ">")
1899 (skeleton-read "Text: " (capitalize str))
1900 (or v2 (setq v2 (if (y-or-n-p "Newline after text? ")
1901 (funcall skeleton-transformation
1902 (if sgml-xml-mode "<br/>" "<br>"))
1906 (define-skeleton html-radio-buttons
1907 "Group of connected radio button inputs."
1912 "<input type=\"" (identity "radio") ; see comment above about identity
1913 "\" name=\"" (or (car v2) (setcar v2 (skeleton-read "Name: ")))
1914 "\" value=\"" str ?\"
1915 (when (and (not v1) (setq v1 (y-or-n-p "Set \"checked\" attribute? ")))
1916 (funcall skeleton-transformation " checked"))
1917 (if sgml-xml-mode "/>" ">")
1918 (skeleton-read "Text: " (capitalize str))
1919 (or (cdr v2) (setcdr v2 (if (y-or-n-p "Newline after text? ")
1920 (funcall skeleton-transformation
1921 (if sgml-xml-mode "<br/>" "<br>"))
1925 (provide 'sgml-mode)
1927 ;;; sgml-mode.el ends here