1 ;;; sgml-mode.el --- SGML- and HTML-editing modes
3 ;; Copyright (C) 1992, 1995, 1996, 1998 Free Software Foundation, Inc.
5 ;; Author: James Clark <jjc@jclark.com>
6 ;; Adapted-By: ESR; Daniel.Pfeiffer@Informatik.START.dbp.de
7 ;; F.Potorti@cnuce.cnr.it
8 ;; Keywords: wp, hypermedia, comm, languages
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
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.
39 (defcustom sgml-transformation
'identity
40 "*Default value for `skeleton-transformation' (which see) in SGML mode."
44 (put 'sgml-transformation
'variable-interactive
45 "aTransformation function: ")
47 ;; As long as Emacs' syntax can't be complemented with predicates to context
48 ;; sensitively confirm the syntax of characters, we have to live with this
49 ;; kludgy kind of tradeoff.
50 (defvar sgml-specials
'(?
\")
51 "List of characters that have a special meaning for SGML mode.
52 This list is used when first loading the sgml-mode library.
53 The supported characters and potential disadvantages are:
55 ?\\\" Makes \" in text start a string.
56 ?' Makes ' in text start a string.
57 ?- Makes -- in text start a comment.
59 When only one of ?\\\" or ?' are included, \"'\" or '\"', as can be found in
60 DTDs, start a string. To partially avoid this problem this also makes these
61 self insert as named entities depending on `sgml-quick-keys'.
63 Including ?- has the problem of affecting dashes that have nothing to do
64 with comments, so we normally turn it off.")
66 (defvar sgml-quick-keys nil
67 "Use <, >, &, SPC and `sgml-specials' keys \"electrically\" when non-nil.
68 This takes effect when first loading the sgml-mode library.")
72 (let ((map (list 'keymap
(make-vector 256 nil
)))
73 (menu-map (make-sparse-keymap "SGML")))
74 (define-key map
"\t" 'indent-relative-maybe
)
75 (define-key map
"\C-c\C-i" 'sgml-tags-invisible
)
76 (define-key map
"/" 'sgml-slash
)
77 (define-key map
"\C-c\C-n" 'sgml-name-char
)
78 (define-key map
"\C-c\C-t" 'sgml-tag
)
79 (define-key map
"\C-c\C-a" 'sgml-attributes
)
80 (define-key map
"\C-c\C-b" 'sgml-skip-tag-backward
)
81 (define-key map
[?\C-c left
] 'sgml-skip-tag-backward
)
82 (define-key map
"\C-c\C-f" 'sgml-skip-tag-forward
)
83 (define-key map
[?\C-c right
] 'sgml-skip-tag-forward
)
84 (define-key map
"\C-c\C-d" 'sgml-delete-tag
)
85 (define-key map
"\C-c\^?" 'sgml-delete-tag
)
86 (define-key map
"\C-c?" 'sgml-tag-help
)
87 (define-key map
"\C-c8" 'sgml-name-8bit-mode
)
88 (define-key map
"\C-c\C-v" 'sgml-validate
)
91 (define-key map
"&" 'sgml-name-char
)
92 (define-key map
"<" 'sgml-tag
)
93 (define-key map
" " 'sgml-auto-attributes
)
94 (define-key map
">" 'sgml-maybe-end-tag
)
95 (if (memq ?
\" sgml-specials
)
96 (define-key map
"\"" 'sgml-name-self
))
97 (if (memq ?
' sgml-specials
)
98 (define-key map
"'" 'sgml-name-self
))))
101 (while (< (setq c
(1+ c
)) 256)
102 (aset map c
'sgml-maybe-name-self
)))
103 (define-key map
[menu-bar sgml
] (cons "SGML" menu-map
))
104 (define-key menu-map
[sgml-validate
] '("Validate" . sgml-validate
))
105 (define-key menu-map
[sgml-name-8bit-mode
]
106 '("Toggle 8 Bit Insertion" . sgml-name-8bit-mode
))
107 (define-key menu-map
[sgml-tags-invisible
]
108 '("Toggle Tag Visibility" . sgml-tags-invisible
))
109 (define-key menu-map
[sgml-tag-help
]
110 '("Describe Tag" . sgml-tag-help
))
111 (define-key menu-map
[sgml-delete-tag
]
112 '("Delete Tag" . sgml-delete-tag
))
113 (define-key menu-map
[sgml-skip-tag-forward
]
114 '("Forward Tag" . sgml-skip-tag-forward
))
115 (define-key menu-map
[sgml-skip-tag-backward
]
116 '("Backward Tag" . sgml-skip-tag-backward
))
117 (define-key menu-map
[sgml-attributes
]
118 '("Insert Attributes" . sgml-attributes
))
119 (define-key menu-map
[sgml-tag
] '("Insert Tag" . sgml-tag
))
121 "Keymap for SGML mode. See also `sgml-specials'.")
124 (defvar sgml-mode-syntax-table
125 (let ((table (copy-syntax-table text-mode-syntax-table
)))
126 (modify-syntax-entry ?
< "(>" table
)
127 (modify-syntax-entry ?
> ")<" table
)
128 (if (memq ?- sgml-specials
)
129 (modify-syntax-entry ?-
"_ 1234" table
))
130 (if (memq ?
\" sgml-specials
)
131 (modify-syntax-entry ?
\" "\"\"" table
))
132 (if (memq ?
' sgml-specials
)
133 (modify-syntax-entry ?
\' "\"'" table
))
135 "Syntax table used in SGML mode. See also `sgml-specials'.")
138 (defcustom sgml-name-8bit-mode nil
139 "*When non-nil, insert 8 bit characters with their names."
143 (defvar sgml-char-names
144 [nil nil nil nil nil nil nil nil
145 nil nil nil nil nil nil nil nil
146 nil nil nil nil nil nil nil nil
147 nil nil nil nil nil nil nil nil
148 "nbsp" "excl" "quot" "num" "dollar" "percnt" "amp" "apos"
149 "lpar" "rpar" "ast" "plus" "comma" "hyphen" "period" "sol"
150 nil nil nil nil nil nil nil nil
151 nil nil
"colon" "semi" "lt" "eq" "gt" "quest"
152 "commat" nil nil nil nil nil nil nil
153 nil nil nil nil nil nil nil nil
154 nil nil nil nil nil nil nil nil
155 nil nil nil
"lsqb" nil
"rsqb" "uarr" "lowbar"
156 "lsquo" nil nil nil nil nil nil nil
157 nil nil nil nil nil nil nil nil
158 nil nil nil nil nil nil nil nil
159 nil nil nil
"lcub" "verbar" "rcub" "tilde" nil
160 nil nil nil nil nil nil nil nil
161 nil nil nil nil nil nil nil nil
162 nil nil nil nil nil nil nil nil
163 nil nil nil nil nil nil nil nil
164 "nbsp" "iexcl" "cent" "pound" "curren" "yen" "brvbar" "sect"
165 "uml" "copy" "ordf" "laquo" "not" "shy" "reg" "macr"
166 "ring" "plusmn" "sup2" "sup3" "acute" "micro" "para" "middot"
167 "cedil" "sup1" "ordm" "raquo" "frac14" "half" "frac34" "iquest"
168 "Agrave" "Aacute" "Acirc" "Atilde" "Auml" "Aring" "AElig" "Ccedil"
169 "Egrave" "Eacute" "Ecirc" "Euml" "Igrave" "Iacute" "Icirc" "Iuml"
170 "ETH" "Ntilde" "Ograve" "Oacute" "Ocirc" "Otilde" "Ouml" nil
171 "Oslash" "Ugrave" "Uacute" "Ucirc" "Uuml" "Yacute" "THORN" "szlig"
172 "agrave" "aacute" "acirc" "atilde" "auml" "aring" "aelig" "ccedil"
173 "egrave" "eacute" "ecirc" "euml" "igrave" "iacute" "icirc" "iuml"
174 "eth" "ntilde" "ograve" "oacute" "ocirc" "otilde" "ouml" "divide"
175 "oslash" "ugrave" "uacute" "ucirc" "uuml" "yacute" "thorn" "yuml"]
176 "Vector of symbolic character names without `&' and `;'.")
179 ;; sgmls is a free SGML parser available from
180 ;; ftp.uu.net:pub/text-processing/sgml
181 ;; Its error messages can be parsed by next-error.
182 ;; The -s option suppresses output.
184 (defcustom sgml-validate-command
"sgmls -s"
185 "*The command to validate an SGML document.
186 The file name of current buffer file name will be appended to this,
187 separated by a space."
191 (defvar sgml-saved-validate-command nil
192 "The command last used to validate in this buffer.")
195 ;;; I doubt that null end tags are used much for large elements,
196 ;;; so use a small distance here.
197 (defcustom sgml-slash-distance
1000
198 "*If non-nil, is the maximum distance to search for matching `/'."
199 :type
'(choice (const nil
) integer
)
202 (defconst sgml-start-tag-regex
203 "<[A-Za-z]\\([-.A-Za-z0-9= \n\t]\\|\"[^\"]*\"\\|'[^']*'\\)*"
204 "Regular expression that matches a non-empty start tag.
205 Any terminating `>' or `/' is not matched.")
209 (defconst sgml-font-lock-keywords-1
210 '(("<\\([!?][a-z][-.a-z0-9]+\\)" 1 font-lock-keyword-face
)
211 ("<\\(/?[a-z][-.a-z0-9]+\\)" 1 font-lock-function-name-face
)
212 ("[&%][a-z][-.a-z0-9]+;?" . font-lock-variable-name-face
)
213 ("<! *--.*-- *>" . font-lock-comment-face
)))
215 (defconst sgml-font-lock-keywords-2
())
217 ;; for font-lock, but must be defvar'ed after
218 ;; sgml-font-lock-keywords-1 and sgml-font-lock-keywords-2 above
219 (defvar sgml-font-lock-keywords sgml-font-lock-keywords-1
220 "*Rules for highlighting SGML code. See also `sgml-tag-face-alist'.")
223 (defvar sgml-face-tag-alist
()
224 "Alist of face and tag name for facemenu.")
226 (defvar sgml-tag-face-alist
()
227 "Tag names and face or list of faces to fontify with when invisible.
228 When `font-lock-maximum-decoration' is 1 this is always used for fontifying.
229 When more these are fontified together with `sgml-font-lock-keywords'.")
232 (defvar sgml-display-text
()
233 "Tag names as lowercase symbols, and display string when invisible.")
236 (defvar sgml-tags-invisible nil
)
239 (defcustom sgml-tag-alist
240 '(("![" ("ignore" t
) ("include" t
))
245 "*Alist of tag names for completing read and insertion rules.
246 This alist is made up as
251 TAGRULE is a list of optionally `t' (no endtag) or `\\n' (separate endtag by
252 newlines) or a skeleton with `nil', `t' or `\\n' in place of the interactor
253 followed by an ATTRIBUTERULE (for an always present attribute) or an
256 The attribute alist is made up as
258 ((\"attribute\" . ATTRIBUTERULE)
261 ATTRIBUTERULE is a list of optionally `t' (no value when no input) followed by
262 an optional alist of possible values."
263 :type
'(repeat (cons (string :tag
"Tag Name")
264 (repeat :tag
"Tag Rule" sexp
)))
267 (defcustom sgml-tag-help
268 '(("!" .
"Empty declaration for comment")
269 ("![" .
"Embed declarations with parser directive")
270 ("!attlist" .
"Tag attributes declaration")
271 ("!doctype" .
"Document type (DTD) declaration")
272 ("!element" .
"Tag declaration")
273 ("!entity" .
"Entity (macro) declaration"))
274 "*Alist of tag name and short description."
275 :type
'(repeat (cons (string :tag
"Tag Name")
276 (string :tag
"Description")))
279 (defun sgml-mode-common (sgml-tag-face-alist sgml-display-text
)
280 "Common code for setting up `sgml-mode' and derived modes.
281 SGML-TAG-FACE-ALIST is used for calculating `sgml-font-lock-keywords-2'.
282 SGML-DISPLAY-TEXT sets up alternate text for when tags are invisible (see
283 varables of same name)."
284 (kill-all-local-variables)
285 (setq local-abbrev-table text-mode-abbrev-table
)
286 (set-syntax-table sgml-mode-syntax-table
)
287 (make-local-variable 'indent-line-function
)
288 (make-local-variable 'paragraph-start
)
289 (make-local-variable 'paragraph-separate
)
290 (make-local-variable 'adaptive-fill-regexp
)
291 (make-local-variable 'sgml-saved-validate-command
)
292 (make-local-variable 'comment-start
)
293 (make-local-variable 'comment-end
)
294 (make-local-variable 'comment-indent-function
)
295 (make-local-variable 'comment-start-skip
)
296 (make-local-variable 'comment-indent-function
)
297 (make-local-variable 'sgml-tags-invisible
)
298 (make-local-variable 'skeleton-transformation
)
299 (make-local-variable 'skeleton-further-elements
)
300 (make-local-variable 'skeleton-end-hook
)
301 (make-local-variable 'font-lock-defaults
)
302 (make-local-variable 'sgml-font-lock-keywords-1
)
303 (make-local-variable 'sgml-font-lock-keywords-2
)
304 (make-local-variable 'facemenu-add-face-function
)
305 (make-local-variable 'facemenu-end-add-face
)
306 ;;(make-local-variable 'facemenu-remove-face-function)
307 (and sgml-tag-face-alist
308 (not (assq 1 sgml-tag-face-alist
))
309 (nconc sgml-tag-face-alist
310 `((1 (,(concat "<\\("
311 (mapconcat 'car sgml-tag-face-alist
"\\|")
312 "\\)\\([ \t].+\\)?>\\(.+\\)</\\1>")
313 3 (cdr (assoc (downcase (match-string 1))
314 ',sgml-tag-face-alist
)))))))
315 (setq indent-line-function
'indent-relative-maybe
316 ;; A start or end tag by itself on a line separates a paragraph.
317 ;; This is desirable because SGML discards a newline that appears
318 ;; immediately after a start tag or immediately before an end tag.
319 paragraph-separate
"[ \t]*$\\|\
320 \[ \t]*</?\\([A-Za-z]\\([-.A-Za-z0-9= \t\n]\\|\"[^\"]*\"\\|'[^']*'\\)*\\)?>$"
321 paragraph-start
"[ \t]*$\\|\
322 \[ \t]*</?\\([A-Za-z]\\([-.A-Za-z0-9= \t\n]\\|\"[^\"]*\"\\|'[^']*'\\)*\\)?>$"
323 adaptive-fill-regexp
"[ \t]*"
324 comment-start
"<!-- "
326 comment-indent-function
'sgml-comment-indent
327 ;; This will allow existing comments within declarations to be
329 comment-start-skip
"--[ \t]*"
330 skeleton-transformation sgml-transformation
331 skeleton-further-elements
'((completion-ignore-case t
))
332 skeleton-end-hook
(lambda ()
335 (eq (car-safe v2
) '\n)))
336 (newline-and-indent)))
337 sgml-font-lock-keywords-2
(append
338 sgml-font-lock-keywords-1
339 (cdr (assq 1 sgml-tag-face-alist
)))
340 font-lock-defaults
'((sgml-font-lock-keywords
341 sgml-font-lock-keywords-1
342 sgml-font-lock-keywords-2
)
345 facemenu-add-face-function
'sgml-mode-facemenu-add-face-function
)
346 (while sgml-display-text
347 (put (car (car sgml-display-text
)) 'before-string
348 (cdr (car sgml-display-text
)))
349 (setq sgml-display-text
(cdr sgml-display-text
)))
350 (run-hooks 'text-mode-hook
'sgml-mode-hook
))
353 (defun sgml-mode-facemenu-add-face-function (face end
)
354 (if (setq face
(cdr (assq face sgml-face-tag-alist
)))
356 (setq face
(funcall skeleton-transformation face
))
357 (setq facemenu-end-add-face
(concat "</" face
">"))
358 (concat "<" face
">"))
359 (error "Face not configured for %s mode." mode-name
)))
363 (defun sgml-mode (&optional function
)
364 "Major mode for editing SGML documents.
365 Makes > match <. Makes / blink matching /.
366 Keys <, &, SPC within <>, \" and ' can be electric depending on
369 An argument of N to a tag-inserting command means to wrap it around
370 the next N words. In Transient Mark mode, when the mark is active,
371 N defaults to -1, which means to wrap it around the current region.
373 If you like upcased tags, put (setq sgml-transformation 'upcase) in
376 Use \\[sgml-validate] to validate your document with an SGML parser.
378 Do \\[describe-variable] sgml- SPC to see available variables.
379 Do \\[describe-key] on the following bindings to discover what they do.
382 (sgml-mode-common sgml-tag-face-alist sgml-display-text
)
383 ;; Set imenu-generic-expression here, rather than in sgml-mode-common,
384 ;; because this definition probably is not useful in HTML mode.
385 (make-local-variable 'imenu-generic-expression
)
386 (setq imenu-generic-expression
387 "<!\\(element\\|entity\\)[ \t\n]+%?[ \t\n]*\\([A-Za-z][-A-Za-z.0-9]*\\)")
388 (use-local-map sgml-mode-map
)
389 (setq mode-name
"SGML"
390 major-mode
'sgml-mode
))
394 (defun sgml-comment-indent ()
395 (if (and (looking-at "--")
396 (not (and (eq (preceding-char) ?
!)
397 (eq (char-after (- (point) 2)) ?
<))))
399 (skip-chars-backward " \t")
400 (max comment-column
(1+ (current-column))))
405 (defun sgml-slash (arg)
406 "Insert `/' and display any previous matching `/'.
407 Two `/'s are treated as matching if the first `/' ends a net-enabling
408 start tag, and the second `/' is the corresponding null end tag."
412 (let ((oldpos (point))
417 (if sgml-slash-distance
418 (narrow-to-region (max (point-min)
419 (- (point) sgml-slash-distance
))
421 (if (and (re-search-backward sgml-start-tag-regex
(point-min) t
)
422 (eq (match-end 0) (1- oldpos
)))
424 (goto-char (1- oldpos
))
425 (while (and (not blinkpos
)
426 (search-backward "/" (point-min) t
))
427 (let ((tagend (save-excursion
428 (if (re-search-backward sgml-start-tag-regex
432 (if (eq tagend
(point))
434 (setq blinkpos
(point))
435 (setq level
(1- level
)))
436 (setq level
(1+ level
)))))))
440 (if (pos-visible-in-window-p)
442 (message "Matches %s"
443 (buffer-substring (progn
446 (1+ blinkpos
))))))))))
449 (defun sgml-name-char (&optional char
)
450 "Insert a symbolic character name according to `sgml-char-names'.
451 8 bit chars may be inserted with the meta key as in M-SPC for no break space,
452 or M-- for a soft hyphen."
456 (setq char
(read-quoted-char "Enter char or octal number")))
457 (delete-backward-char 1)
460 (delete-backward-char 1)
462 (or (aref sgml-char-names char
)
467 (defun sgml-name-self ()
468 "Insert a symbolic character name according to `sgml-char-names'."
470 (sgml-name-char last-command-char
))
473 (defun sgml-maybe-name-self ()
474 "Insert a symbolic character name according to `sgml-char-names'."
476 (if sgml-name-8bit-mode
477 (sgml-name-char last-command-char
)
478 (self-insert-command 1)))
481 (defun sgml-name-8bit-mode ()
482 "Toggle insertion of 8 bit characters."
484 (setq sgml-name-8bit-mode
(not sgml-name-8bit-mode
))
485 (message "sgml name 8 bit mode is now %"
486 (if sgml-name-8bit-mode
"ON" "OFF")))
489 ;; When an element of a skeleton is a string "str", it is passed
490 ;; through skeleton-transformation and inserted. If "str" is to be
491 ;; inserted literally, one should obtain it as the return value of a
492 ;; function, e.g. (identity "str").
494 (define-skeleton sgml-tag
495 "Prompt for a tag and insert it, optionally with attributes.
496 Completion and configuration are done according to `sgml-tag-alist'.
497 If you like tags and attributes in uppercase do \\[set-variable]
498 skeleton-transformation RET upcase RET, or put this in your `.emacs':
499 (setq sgml-transformation 'upcase)"
500 (funcall skeleton-transformation
501 (completing-read "Tag: " sgml-tag-alist
))
502 ?
< (setq v1
(eval str
)) |
503 (("") -
1 '(undo-boundary) (identity "<")) |
; see comment above
504 (("") '(setq v2
(sgml-attributes v1 t
)) ?
>
505 (if (string= "![" v1
)
506 (prog1 '(("") " [ " _
" ]]")
509 (string-match "^[/!?]" v1
))
512 '(("") v2 _ v2
"</" v1 ?
>)
514 (cons '("") (cdr v2
))
515 (append '(("") (car v2
))
517 '(resume: (car v2
) _
"</" v1 ?
>))))))))
519 (autoload 'skeleton-read
"skeleton")
521 (defun sgml-attributes (tag &optional quiet
)
522 "When at top level of a tag, interactively insert attributes.
524 Completion and configuration of TAG are done according to `sgml-tag-alist'.
525 If QUIET, do not print a message when there are no attributes for TAG."
526 (interactive (list (save-excursion (sgml-beginning-of-tag t
))))
527 (or (stringp tag
) (error "Wrong context for adding attribute"))
529 (let ((completion-ignore-case t
)
530 (alist (cdr (assoc (downcase tag
) sgml-tag-alist
)))
532 (if (or (symbolp (car alist
))
533 (symbolp (car (car alist
))))
534 (setq car
(car alist
)
537 (message "No attributes configured."))
538 (if (stringp (car alist
))
540 (insert (if (eq (preceding-char) ?
) "" ?
)
541 (funcall skeleton-transformation
(car alist
)))
543 (setq i
(length alist
))
546 (insert (funcall skeleton-transformation
548 (skeleton-read '(completing-read
551 (if (string= "" attribute
)
553 (sgml-value (assoc (downcase attribute
) alist
))
555 (if (eq (preceding-char) ?
)
556 (delete-backward-char 1)))
559 (defun sgml-auto-attributes (arg)
560 "Self insert the character typed; at top level of tag, prompt for attributes.
561 With prefix argument, only self insert."
563 (let ((point (point))
566 (not sgml-tag-alist
) ; no message when nothing configured
567 (symbolp (setq tag
(save-excursion (sgml-beginning-of-tag t
))))
568 (eq (aref tag
0) ?
/))
569 (self-insert-command (prefix-numeric-value arg
))
570 (sgml-attributes tag
)
571 (setq last-command-char ?
)
572 (or (> (point) point
)
573 (self-insert-command 1)))))
576 (defun sgml-tag-help (&optional tag
)
577 "Display description of tag TAG. If TAG is omitted, use the tag at point."
581 (if (eq (following-char) ?
<)
583 (setq tag
(sgml-beginning-of-tag))))
585 (error "No tag selected"))
586 (setq tag
(downcase tag
))
588 (or (cdr (assoc (downcase tag
) sgml-tag-help
))
589 (and (eq (aref tag
0) ?
/)
590 (cdr (assoc (downcase (substring tag
1)) sgml-tag-help
)))
591 "No description available")))
594 (defun sgml-maybe-end-tag ()
595 "Name self unless in position to end a tag."
597 (or (condition-case nil
598 (save-excursion (up-list -
1))
604 (save-excursion (up-list 1))
606 (error (self-insert-command 1)))))
609 (defun sgml-skip-tag-backward (arg)
610 "Skip to beginning of tag or matching opening tag if present.
611 With prefix argument ARG, repeat this ARG times."
614 (search-backward "<" nil t
)
615 (if (looking-at "</\\([^ \n\t>]+\\)")
616 ;; end tag, skip any nested pairs
617 (let ((case-fold-search t
)
618 (re (concat "</?" (regexp-quote (match-string 1)))))
619 (while (and (re-search-backward re nil t
)
620 (eq (char-after (1+ (point))) ?
/))
622 (sgml-skip-tag-backward 1))))
623 (setq arg
(1- arg
))))
625 (defun sgml-skip-tag-forward (arg &optional return
)
626 "Skip to end of tag or matching closing tag if present.
627 With prefix argument ARG, repeat this ARG times.
628 Return t iff after a closing tag."
632 (skip-chars-forward "^<>")
633 (if (eq (following-char) ?
>)
635 (if (looking-at "<\\([^/ \n\t>]+\\)")
636 ;; start tag, skip any nested same pairs _and_ closing tag
637 (let ((case-fold-search t
)
638 (re (concat "</?" (regexp-quote (match-string 1))))
642 (while (and (re-search-forward re nil t
)
644 (eq (char-after (1+ (match-beginning 0))) ?
/)))
646 (sgml-skip-tag-forward 1))
656 (defun sgml-delete-tag (arg)
657 "Delete tag on or after cursor, and matching closing or opening tag.
658 With prefix argument ARG, repeat this ARG times."
663 (if (looking-at "[ \t\n]*<")
665 (if (eq (char-after (match-end 0)) ?
/)
669 (goto-char (match-end 0))))
671 (or (save-excursion (setq close
(sgml-beginning-of-tag)
672 close
(and (stringp close
)
673 (eq (aref close
0) ?
/)
675 ;; not on closing tag
676 (let ((point (point)))
677 (sgml-skip-tag-backward 1)
678 (if (or (not (eq (following-char) ?
<))
682 (error "Not on or before tag")))))
685 (sgml-skip-tag-backward 1)
690 (sgml-skip-tag-forward 1)
693 (if (eq (aref (sgml-beginning-of-tag) 0) ?
/)
697 (setq arg
(1- arg
))))
699 ;; Put read-only last to enable setting this even when read-only enabled.
700 (or (get 'sgml-tag
'invisible
)
702 (append '(invisible t
704 point-entered sgml-point-entered
707 (symbol-plist 'sgml-tag
))))
709 (defun sgml-tags-invisible (arg)
710 "Toggle visibility of existing tags."
712 (let ((modified (buffer-modified-p))
713 (inhibit-read-only t
)
714 ;; This is needed in case font lock gets called,
715 ;; since it moves point and might call sgml-point-entered.
716 (inhibit-point-motion-hooks t
)
719 (goto-char (point-min))
720 (if (setq sgml-tags-invisible
722 (>= (prefix-numeric-value arg
) 0)
723 (not sgml-tags-invisible
)))
724 (while (re-search-forward "<\\([!/?A-Za-z][-A-Za-z0-9]*\\)"
726 (setq symbol
(intern-soft (downcase (match-string 1))))
727 (goto-char (match-beginning 0))
728 (and (get symbol
'before-string
)
729 (not (overlays-at (point)))
730 (overlay-put (make-overlay (point)
733 (put-text-property (point)
734 (progn (forward-list) (point))
735 'category
'sgml-tag
))
737 (while (< (setq pos
(next-overlay-change pos
)) (point-max))
738 (delete-overlay (car (overlays-at pos
)))))
739 (remove-text-properties (point-min) (point-max)
740 '(category sgml-tag intangible t
))))
741 (set-buffer-modified-p modified
)
742 (run-hooks 'sgml-tags-invisible-hook
)
745 (defun sgml-point-entered (x y
)
746 ;; Show preceding or following hidden tag, depending of cursor direction.
747 (let ((inhibit-point-motion-hooks t
))
749 (message "Invisible tag: %s"
753 (not (eq (following-char) ?
<)))
755 (eq (preceding-char) ?
>)))
759 (autoload 'compile-internal
"compile")
761 (defun sgml-validate (command)
762 "Validate an SGML document.
763 Runs COMMAND, a shell command, in a separate process asynchronously
764 with output going to the buffer `*compilation*'.
765 You can then use the command \\[next-error] to find the next error message
766 and move to the line in the SGML document that caused it."
768 (list (read-string "Validate command: "
769 (or sgml-saved-validate-command
770 (concat sgml-validate-command
772 (let ((name (buffer-file-name)))
774 (file-name-nondirectory name
))))))))
775 (setq sgml-saved-validate-command command
)
776 (if (or (not compilation-ask-about-save
)
777 (y-or-n-p (message "Save buffer %s? " (buffer-name))))
779 (compile-internal command
"No more errors"))
782 (defun sgml-beginning-of-tag (&optional top-level
)
783 "Skip to beginning of tag and return its name.
784 If this can't be done, return t."
790 (if (search-backward "<" nil t
)
795 (if (looking-at "<[!?/]?[[A-Za-z][A-Za-z0-9]*")
796 (buffer-substring-no-properties
801 (defun sgml-value (alist)
802 "Interactively insert value taken from attributerule ALIST.
803 See `sgml-tag-alist' for info about attributerules.."
804 (setq alist
(cdr alist
))
805 (if (stringp (car alist
))
806 (insert "=\"" (car alist
) ?
\")
807 (if (eq (car alist
) t
)
811 (setq alist
(skeleton-read '(completing-read
812 "Value: " (cdr alist
))))
813 (if (string< "" alist
)
815 (delete-backward-char 2))))
818 (insert (skeleton-read '(completing-read "Value: " alist
))))
823 (defvar html-quick-keys sgml-quick-keys
824 "Use C-c X combinations for quick insertion of frequent tags when non-nil.
825 This defaults to `sgml-quick-keys'.
826 This takes effect when first loading the library.")
828 (defvar html-mode-map
829 (let ((map (nconc (make-sparse-keymap) sgml-mode-map
))
830 (menu-map (make-sparse-keymap "HTML")))
831 (define-key map
"\C-c6" 'html-headline-6
)
832 (define-key map
"\C-c5" 'html-headline-5
)
833 (define-key map
"\C-c4" 'html-headline-4
)
834 (define-key map
"\C-c3" 'html-headline-3
)
835 (define-key map
"\C-c2" 'html-headline-2
)
836 (define-key map
"\C-c1" 'html-headline-1
)
837 (define-key map
"\C-c\r" 'html-paragraph
)
838 (define-key map
"\C-c\n" 'html-line
)
839 (define-key map
"\C-c\C-c-" 'html-horizontal-rule
)
840 (define-key map
"\C-c\C-co" 'html-ordered-list
)
841 (define-key map
"\C-c\C-cu" 'html-unordered-list
)
842 (define-key map
"\C-c\C-cr" 'html-radio-buttons
)
843 (define-key map
"\C-c\C-cc" 'html-checkboxes
)
844 (define-key map
"\C-c\C-cl" 'html-list-item
)
845 (define-key map
"\C-c\C-ch" 'html-href-anchor
)
846 (define-key map
"\C-c\C-cn" 'html-name-anchor
)
847 (define-key map
"\C-c\C-ci" 'html-image
)
850 (define-key map
"\C-c-" 'html-horizontal-rule
)
851 (define-key map
"\C-co" 'html-ordered-list
)
852 (define-key map
"\C-cu" 'html-unordered-list
)
853 (define-key map
"\C-cr" 'html-radio-buttons
)
854 (define-key map
"\C-cc" 'html-checkboxes
)
855 (define-key map
"\C-cl" 'html-list-item
)
856 (define-key map
"\C-ch" 'html-href-anchor
)
857 (define-key map
"\C-cn" 'html-name-anchor
)
858 (define-key map
"\C-ci" 'html-image
)))
859 (define-key map
"\C-c\C-s" 'html-autoview-mode
)
860 (define-key map
"\C-c\C-v" 'browse-url-of-buffer
)
861 (define-key map
[menu-bar html
] (cons "HTML" menu-map
))
862 (define-key menu-map
[html-autoview-mode
]
863 '("Toggle Autoviewing" . html-autoview-mode
))
864 (define-key menu-map
[browse-url-of-buffer
]
865 '("View Buffer Contents" . browse-url-of-buffer
))
866 (define-key menu-map
[nil] '("--"))
867 ;;(define-key menu-map "6" '("Heading 6" . html-headline-6))
868 ;;(define-key menu-map "5" '("Heading 5" . html-headline-5))
869 ;;(define-key menu-map "4" '("Heading 4" . html-headline-4))
870 (define-key menu-map "3" '("Heading 3" . html-headline-3))
871 (define-key menu-map "2" '("Heading 2" . html-headline-2))
872 (define-key menu-map "1" '("Heading 1" . html-headline-1))
873 (define-key menu-map "l" '("Radio Buttons" . html-radio-buttons))
874 (define-key menu-map "c" '("Checkboxes" . html-checkboxes))
875 (define-key menu-map "l" '("List Item" . html-list-item))
876 (define-key menu-map "u" '("Unordered List" . html-unordered-list))
877 (define-key menu-map "o" '("Ordered List" . html-ordered-list))
878 (define-key menu-map "-" '("Horizontal Rule" . html-horizontal-rule))
879 (define-key menu-map "\n" '("Line Break" . html-line))
880 (define-key menu-map "\r" '("Paragraph" . html-paragraph))
881 (define-key menu-map "i" '("Image" . html-image))
882 (define-key menu-map "h" '("Href Anchor" . html-href-anchor))
883 (define-key menu-map "n" '("Name Anchor" . html-name-anchor))
885 "Keymap for commands for use in HTML mode.")
888 (defvar html-face-tag-alist
893 "Value of `sgml-face-tag-alist' for HTML mode.")
895 (defvar html-tag-face-alist
898 ("blink" . highlight)
901 ("h1" bold underline)
902 ("h2" bold-italic underline)
903 ("h3" italic underline)
912 ("title" bold underline)
916 "Value of `sgml-tag-face-alist' for HTML mode.")
919 (defvar html-display-text
923 "Value of `sgml-display-text' for HTML mode.")
926 ; should code exactly HTML 3 here when that is finished
927 (defvar html-tag-alist
928 (let* ((1-7 '(("1") ("2") ("3") ("4") ("5") ("6") ("7")))
929 (1-9 '(,@1-7 ("8") ("9")))
930 (align '(("align" ("left") ("center") ("right"))))
931 (valign '(("top") ("middle") ("bottom") ("baseline")))
932 (rel '(("next") ("previous") ("parent") ("subdocument") ("made")))
933 (href '("href" ("ftp:") ("file:") ("finger:") ("gopher:") ("http:")
934 ("mailto:") ("news:") ("rlogin:") ("telnet:") ("tn3270:")
935 ("wais:") ("/cgi-bin/")))
941 (list '((nil \n ( "List item: "
949 ;; put ,-expressions first, else byte-compile chokes (as of V19.29)
950 ;; and like this it's more efficient anyway
954 ("font" nil "size" ("-1") ("+1") ("-2") ("+2") ,@1-7)
955 ("form" (\n _ \n "<input type=\"submit\" value=\"\">")
956 ("action" ,@(cdr href)) ("method" ("get") ("post")))
963 ("hr" t ("size" ,@1-9) ("width") ("noshade" t) ,@align)
964 ("img" t ("align" ,@valign ("texttop") ("absmiddle") ("absbottom"))
965 ("src") ("alt") ("width" "1") ("height" "1")
966 ("border" "1") ("vspace" "1") ("hspace" "1") ("ismap" t))
967 ("input" t ("size" ,@1-9) ("maxlength" ,@1-9) ("checked" t) ,name
968 ("type" ("text") ("password") ("checkbox") ("radio")
969 ("submit") ("reset"))
973 ("ol" ,@list ("type" ("A") ("a") ("I") ("i") ("1")))
978 ,name ("size" ,@1-9) ("multiple" t))
980 ((completing-read "Cell kind: " '(("td") ("th"))
982 "<tr><" str ?> _ \n))
983 ("border" t ,@1-9) ("width" "10") ("cellpadding"))
985 ("textarea" ,name ("rows" ,@1-9) ("cols" ,@1-9))
987 ("ul" ,@list ("type" ("disc") ("circle") ("square")))
995 ("Item: " "<item>" str \n))
1002 ("body" \n ("background" ".gif") ("bgcolor" "#") ("text" "#")
1003 ("link" "#") ("alink" "#") ("vlink" "#"))
1004 ("box" (nil _ "<over>" _))
1005 ("br" t ("clear" ("left") ("right")))
1006 ("caption" ("valign" ("top") ("bottom")))
1015 "<dt>" str "<dd>" _ \n)))
1018 ;("fn" "id" "fn") ; ???
1022 "<title>" (setq str (read-input "Title: ")) "</title>\n"
1023 "<body>\n<h1>" str "</h1>\n" _
1024 "\n<address>\n<a href=\"mailto:"
1026 "\">" (user-full-name) "</a>\n</address>"))
1029 ("isindex" t ("action") ("prompt"))
1035 ("option" t ("value") ("label") ("selected" t))
1053 "*Value of `sgml-tag-alist' for HTML mode.")
1055 (defvar html-tag-help
1057 ("a" . "Anchor of point or link elsewhere")
1060 ("address" . "Formatted mail address")
1061 ("array" . "Math array")
1064 ("base" . "Base address for URLs")
1065 ("big" . "Font size")
1066 ("blink" . "Blinking text")
1067 ("blockquote" . "Indented quotation")
1068 ("body" . "Document body")
1069 ("box" . "Math fraction")
1070 ("br" . "Line break")
1071 ("caption" . "Table caption")
1072 ("center" . "Centered text")
1073 ("changed" . "Change bars")
1074 ("cite" . "Citation of a document")
1075 ("code" . "Formatted source code")
1076 ("dd" . "Definition of term")
1079 ("dir" . "Directory list (obsolete)")
1080 ("dl" . "Definition list")
1081 ("dt" . "Term to be definined")
1082 ("em" . "Emphasised")
1083 ("embed" . "Embedded data in foreign format")
1085 ("figa" . "Figure anchor")
1086 ("figd" . "Figure description")
1087 ("figt" . "Figure text")
1089 ("font" . "Font size")
1090 ("form" . "Form with input fields")
1091 ("group" . "Document grouping")
1092 ("h1" . "Most important section headline")
1093 ("h2" . "Important section headline")
1094 ("h3" . "Section headline")
1095 ("h4" . "Minor section headline")
1096 ("h5" . "Unimportant section headline")
1097 ("h6" . "Least important section headline")
1098 ("head" . "Document header")
1099 ("hr" . "Horizontal rule")
1100 ("html" . "HTML Document")
1101 ("i" . "Italic face")
1102 ("img" . "Graphic image")
1103 ("input" . "Form input field")
1105 ("isindex" . "Input field for index search")
1106 ("kbd" . "Keybard example face")
1107 ("lang" . "Natural language")
1108 ("li" . "List item")
1109 ("link" . "Link relationship")
1110 ("math" . "Math formula")
1111 ("menu" . "Menu list (obsolete)")
1112 ("mh" . "Form mail header")
1113 ("nextid" . "Allocate new id")
1114 ("nobr" . "Text without line break")
1115 ("ol" . "Ordered list")
1116 ("option" . "Selection list item")
1117 ("over" . "Math fraction rule")
1118 ("p" . "Paragraph start")
1119 ("panel" . "Floating panel")
1121 ("pre" . "Preformatted fixed width text")
1123 ("rev" . "Reverse video")
1125 ("samp" . "Sample text")
1126 ("select" . "Selection list")
1127 ("small" . "Font size")
1128 ("sp" . "Nobreak space")
1129 ("strong" . "Standout text")
1130 ("sub" . "Subscript")
1131 ("sup" . "Superscript")
1132 ("table" . "Table with rows and columns")
1133 ("tb" . "Table vertical break")
1134 ("td" . "Table data cell")
1135 ("textarea" . "Form multiline edit area")
1136 ("th" . "Table header cell")
1137 ("title" . "Document title")
1138 ("tr" . "Table row separator")
1139 ("tt" . "Typewriter face")
1140 ("u" . "Underlined text")
1141 ("ul" . "Unordered list")
1142 ("var" . "Math variable face")
1143 ("wbr" . "Enable <br> within <nobr>"))
1144 "*Value of `sgml-tag-help' for HTML mode.")
1150 "Major mode based on SGML mode for editing HTML documents.
1151 This allows inserting skeleton costructs used in hypertext documents with
1152 completion. See below for an introduction to HTML. Use
1153 \\[browse-url-of-buffer] to see how this comes out. See also `sgml-mode' on
1154 which this is based.
1156 Do \\[describe-variable] html- SPC and \\[describe-variable] sgml- SPC to see available variables.
1158 To write fairly well formatted pages you only need to know few things. Most
1159 browsers have a function to read the source code of the page being seen, so
1160 you can imitate various tricks. Here's a very short HTML primer which you
1161 can also view with a browser to see what happens:
1163 <title>A Title Describing Contents</title> should be on every page. Pages can
1164 have <h1>Very Major Headlines</h1> through <h6>Very Minor Headlines</h6>
1165 <hr> Parts can be separated with horizontal rules.
1167 <p>Paragraphs only need an opening tag. Line breaks and multiple spaces are
1168 ignored unless the text is <pre>preformatted.</pre> Text can be marked as
1169 <b>bold</b>, <i>italic</i> or <u>underlined</u> using the normal M-g or
1170 Edit/Text Properties/Face commands.
1172 Pages can have <a name=\"SOMENAME\">named points</a> and can link other points
1173 to them with <a href=\"#SOMENAME\">see also somename</a>. In the same way <a
1174 href=\"URL\">see also URL</a> where URL is a filename relative to current
1175 directory, or absolute as in `http://www.cs.indiana.edu/elisp/w3/docs.html'.
1177 Images in many formats can be inlined with <img src=\"URL\">.
1179 If you mainly create your own documents, `sgml-specials' might be
1180 interesting. But note that some HTML 2 browsers can't handle `''.
1181 To work around that, do:
1182 (eval-after-load \"sgml-mode\" '(aset sgml-char-names ?' nil))
1186 (sgml-mode-common html-tag-face-alist html-display-text)
1187 (use-local-map html-mode-map)
1188 (make-local-variable 'sgml-tag-alist)
1189 (make-local-variable 'sgml-face-tag-alist)
1190 (make-local-variable 'sgml-tag-help)
1191 (make-local-variable 'outline-regexp)
1192 (make-local-variable 'outline-heading-end-regexp)
1193 (make-local-variable 'outline-level)
1194 (make-local-variable 'sentence-end)
1196 "[.?!][]\"')}]*\\(<[^>]*>\\)*\\($\\| $\\|\t\\| \\)[ \t\n]*")
1197 (setq mode-name "HTML"
1198 major-mode 'html-mode
1199 sgml-tag-alist html-tag-alist
1200 sgml-face-tag-alist html-face-tag-alist
1201 sgml-tag-help html-tag-help
1202 outline-regexp "^.*<[Hh][1-6]\\>"
1203 outline-heading-end-regexp "</[Hh][1-6]>"
1204 outline-level (lambda ()
1205 (char-after (1- (match-end 0)))))
1206 (run-hooks 'html-mode-hook))
1209 (define-skeleton html-href-anchor
1210 "HTML anchor tag with href attribute."
1212 '(setq input "http:")
1213 "<a href=\"" str "\">" _ "</a>")
1215 (define-skeleton html-name-anchor
1216 "HTML anchor tag with name attribute."
1218 "<a name=\"" str "\">" _ "</a>")
1220 (define-skeleton html-headline-1
1221 "HTML level 1 headline tags."
1225 (define-skeleton html-headline-2
1226 "HTML level 2 headline tags."
1230 (define-skeleton html-headline-3
1231 "HTML level 3 headline tags."
1235 (define-skeleton html-headline-4
1236 "HTML level 4 headline tags."
1240 (define-skeleton html-headline-5
1241 "HTML level 5 headline tags."
1245 (define-skeleton html-headline-6
1246 "HTML level 6 headline tags."
1250 (define-skeleton html-horizontal-rule
1251 "HTML horizontal rule tag."
1255 (define-skeleton html-image
1258 "<img src=\"" _ "\">")
1260 (define-skeleton html-line
1261 "HTML line break tag."
1265 (define-skeleton html-ordered-list
1266 "HTML ordered list tags."
1272 (define-skeleton html-unordered-list
1273 "HTML unordered list tags."
1279 (define-skeleton html-list-item
1280 "HTML list item tag."
1285 (define-skeleton html-paragraph
1286 "HTML paragraph tag."
1291 (define-skeleton html-checkboxes
1292 "Group of connected checkbox inputs."
1297 "<input type=\"" (identity "checkbox") ; see comment above about identity
1298 "\" name=\"" (or v1 (setq v1 (skeleton-read "Name: ")))
1299 "\" value=\"" str ?\"
1300 (if (y-or-n-p "Set \"checked\" attribute? ")
1301 (funcall skeleton-transformation " checked")) ">"
1302 (skeleton-read "Text: " (capitalize str))
1303 (or v2 (setq v2 (if (y-or-n-p "Newline after text? ")
1304 (funcall skeleton-transformation "<br>")
1308 (define-skeleton html-radio-buttons
1309 "Group of connected radio button inputs."
1314 "<input type=\"" (identity "radio") ; see comment above about identity
1315 "\" name=\"" (or (car v2) (setcar v2 (skeleton-read "Name: ")))
1316 "\" value=\"" str ?\"
1317 (if (and (not v1) (setq v1 (y-or-n-p "Set \"checked\" attribute? ")))
1318 (funcall skeleton-transformation " checked") ">")
1319 (skeleton-read "Text: " (capitalize str))
1320 (or (cdr v2) (setcdr v2 (if (y-or-n-p "Newline after text? ")
1321 (funcall skeleton-transformation "<br>")
1326 (defun html-autoview-mode (&optional arg)
1327 "Toggle automatic viewing via `html-viewer' upon saving buffer.
1328 With positive prefix ARG always turns viewing on, with negative ARG always off.
1329 Can be used as a value for `html-mode-hook'."
1331 (if (setq arg (if arg
1332 (< (prefix-numeric-value arg) 0)
1333 (and (boundp 'after-save-hook)
1334 (memq 'browse-url-of-buffer after-save-hook))))
1335 (setq after-save-hook (delq 'browse-url-of-buffer after-save-hook))
1336 (make-local-hook 'after-save-hook)
1337 (add-hook 'after-save-hook 'browse-url-of-buffer nil t))
1338 (message "Autoviewing turned %s."
1339 (if arg "off" "on")))
1341 ;;; sgml-mode.el ends here