1 ;;; sgml-mode.el --- SGML- and HTML-editing modes
3 ;; Copyright (C) 1992, 1995, 1996 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 it 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.")
208 (defvar sgml-font-lock-keywords
209 '(("<\\([!?][a-z0-9]+\\)" 1 font-lock-keyword-face
)
210 ("<\\(/?[a-z0-9]+\\)" 1 font-lock-function-name-face
)
211 ("[&%][-.A-Za-z0-9]+;?" . font-lock-variable-name-face
)
212 ("<!--[^<>]*-->" . font-lock-comment-face
))
213 "*Rules for highlighting SGML code. See also `sgml-tag-face-alist'.")
216 (defvar sgml-font-lock-keywords-1
())
218 (defvar sgml-face-tag-alist
()
219 "Alist of face and tag name for facemenu.")
221 (defvar sgml-tag-face-alist
()
222 "Tag names and face or list of faces to fontify with when invisible.
223 When `font-lock-maximum-decoration' is 1 this is always used for fontifying.
224 When more these are fontified together with `sgml-font-lock-keywords'.")
227 (defvar sgml-display-text
()
228 "Tag names as lowercase symbols, and display string when invisible.")
231 (defvar sgml-tags-invisible nil
)
234 (defcustom sgml-tag-alist
235 '(("![" ("ignore" t
) ("include" t
))
240 "*Alist of tag names for completing read and insertion rules.
241 This alist is made up as
246 TAGRULE is a list of optionally `t' (no endtag) or `\\n' (separate endtag by
247 newlines) or a skeleton with `nil', `t' or `\\n' in place of the interactor
248 followed by an ATTRIBUTERULE (for an always present attribute) or an
251 The attribute alist is made up as
253 ((\"attribute\" . ATTRIBUTERULE)
256 ATTRIBUTERULE is a list of optionally `t' (no value when no input) followed by
257 an optional alist of possible values."
258 :type
'(repeat (cons (string :tag
"Tag Name")
259 (repeat :tag
"Tag Rule" sexp
)))
262 (defcustom sgml-tag-help
263 '(("!" .
"Empty declaration for comment")
264 ("![" .
"Embed declarations with parser directive")
265 ("!attlist" .
"Tag attributes declaration")
266 ("!doctype" .
"Document type (DTD) declaration")
267 ("!element" .
"Tag declaration")
268 ("!entity" .
"Entity (macro) declaration"))
269 "*Alist of tag name and short description."
270 :type
'(repeat (cons (string :tag
"Tag Name")
271 (string :tag
"Description")))
274 (defun sgml-mode-common (sgml-tag-face-alist sgml-display-text
)
275 "Common code for setting up `sgml-mode' and derived modes.
276 SGML-TAG-FACE-ALIST is used for calculating `sgml-font-lock-keywords-1'.
277 SGML-DISPLAY-TEXT sets up alternate text for when tags are invisible (see
278 varables of same name)."
279 (kill-all-local-variables)
280 (setq local-abbrev-table text-mode-abbrev-table
)
281 (set-syntax-table sgml-mode-syntax-table
)
282 (make-local-variable 'indent-line-function
)
283 (make-local-variable 'paragraph-start
)
284 (make-local-variable 'paragraph-separate
)
285 (make-local-variable 'sgml-saved-validate-command
)
286 (make-local-variable 'comment-start
)
287 (make-local-variable 'comment-end
)
288 (make-local-variable 'comment-indent-function
)
289 (make-local-variable 'comment-start-skip
)
290 (make-local-variable 'comment-indent-function
)
291 (make-local-variable 'sgml-tags-invisible
)
292 (make-local-variable 'skeleton-transformation
)
293 (make-local-variable 'skeleton-further-elements
)
294 (make-local-variable 'skeleton-end-hook
)
295 (make-local-variable 'font-lock-defaults
)
296 (make-local-variable 'sgml-font-lock-keywords-1
)
297 (make-local-variable 'facemenu-add-face-function
)
298 (make-local-variable 'facemenu-end-add-face
)
299 ;;(make-local-variable 'facemenu-remove-face-function)
300 (and sgml-tag-face-alist
301 (not (assq 1 sgml-tag-face-alist
))
302 (nconc sgml-tag-face-alist
303 `((1 (,(concat "<\\("
304 (mapconcat 'car sgml-tag-face-alist
"\\|")
305 "\\)\\([ \t].+\\)?>\\(.+\\)</\\1>")
306 3 (cdr (assoc (match-string 1) ',sgml-tag-face-alist
)))))))
307 (setq indent-line-function
'indent-relative-maybe
308 ;; A start or end tag by itself on a line separates a paragraph.
309 ;; This is desirable because SGML discards a newline that appears
310 ;; immediately after a start tag or immediately before an end tag.
311 paragraph-separate
"[ \t]*$\\|\
312 \[ \t]*</?\\([A-Za-z]\\([-.A-Za-z0-9= \t\n]\\|\"[^\"]*\"\\|'[^']*'\\)*\\)?>$"
313 paragraph-start
"[ \t]*$\\|\
314 \[ \t]*</?\\([A-Za-z]\\([-.A-Za-z0-9= \t\n]\\|\"[^\"]*\"\\|'[^']*'\\)*\\)?>$"
315 adaptive-fill-regexp
"[ \t]*"
316 comment-start
"<!-- "
318 comment-indent-function
'sgml-comment-indent
319 ;; This will allow existing comments within declarations to be
321 comment-start-skip
"--[ \t]*"
322 skeleton-transformation sgml-transformation
323 skeleton-further-elements
'((completion-ignore-case t
))
324 skeleton-end-hook
(lambda ()
327 (eq (car-safe v2
) '\n)))
328 (newline-and-indent)))
329 sgml-font-lock-keywords-1
(cdr (assq 1 sgml-tag-face-alist
))
330 font-lock-defaults
'((sgml-font-lock-keywords
331 sgml-font-lock-keywords-1
)
334 facemenu-add-face-function
'sgml-mode-facemenu-add-face-function
)
335 (while sgml-display-text
336 (put (car (car sgml-display-text
)) 'before-string
337 (cdr (car sgml-display-text
)))
338 (setq sgml-display-text
(cdr sgml-display-text
)))
339 (run-hooks 'text-mode-hook
'sgml-mode-hook
))
342 (defun sgml-mode-facemenu-add-face-function (face end
)
343 (if (setq face
(cdr (assq face sgml-face-tag-alist
)))
345 (setq face
(funcall skeleton-transformation face
))
346 (setq facemenu-end-add-face
(concat "</" face
">"))
347 (concat "<" face
">"))
348 (error "Face not configured for %s mode." mode-name
)))
352 (defun sgml-mode (&optional function
)
353 "Major mode for editing SGML documents.
354 Makes > match <. Makes / blink matching /.
355 Keys <, &, SPC within <>, \" and ' can be electric depending on
358 An argument of N to a tag-inserting command means to wrap it around
359 the next N words. In Transient Mark mode, when the mark is active,
360 N defaults to -1, which means to wrap it around the current region.
362 If you like upcased tags, put (setq sgml-transformation 'upcase) in
365 Use \\[sgml-validate] to validate your document with an SGML parser.
367 Do \\[describe-variable] sgml- SPC to see available variables.
368 Do \\[describe-key] on the following bindings to discover what they do.
371 (sgml-mode-common sgml-tag-face-alist sgml-display-text
)
372 (use-local-map sgml-mode-map
)
373 (setq mode-name
"SGML"
374 major-mode
'sgml-mode
))
378 (defun sgml-comment-indent ()
379 (if (and (looking-at "--")
380 (not (and (eq (preceding-char) ?
!)
381 (eq (char-after (- (point) 2)) ?
<))))
383 (skip-chars-backward " \t")
384 (max comment-column
(1+ (current-column))))
389 (defun sgml-slash (arg)
390 "Insert `/' and display any previous matching `/'.
391 Two `/'s are treated as matching if the first `/' ends a net-enabling
392 start tag, and the second `/' is the corresponding null end tag."
396 (let ((oldpos (point))
401 (if sgml-slash-distance
402 (narrow-to-region (max (point-min)
403 (- (point) sgml-slash-distance
))
405 (if (and (re-search-backward sgml-start-tag-regex
(point-min) t
)
406 (eq (match-end 0) (1- oldpos
)))
408 (goto-char (1- oldpos
))
409 (while (and (not blinkpos
)
410 (search-backward "/" (point-min) t
))
411 (let ((tagend (save-excursion
412 (if (re-search-backward sgml-start-tag-regex
416 (if (eq tagend
(point))
418 (setq blinkpos
(point))
419 (setq level
(1- level
)))
420 (setq level
(1+ level
)))))))
424 (if (pos-visible-in-window-p)
426 (message "Matches %s"
427 (buffer-substring (progn
430 (1+ blinkpos
))))))))))
433 (defun sgml-name-char (&optional char
)
434 "Insert a symbolic character name according to `sgml-char-names'.
435 8 bit chars may be inserted with the meta key as in M-SPC for no break space,
436 or M-- for a soft hyphen."
440 (setq char
(read-quoted-char "Enter char or octal number")))
441 (delete-backward-char 1)
444 (delete-backward-char 1)
446 (or (aref sgml-char-names char
)
451 (defun sgml-name-self ()
452 "Insert a symbolic character name according to `sgml-char-names'."
454 (sgml-name-char last-command-char
))
457 (defun sgml-maybe-name-self ()
458 "Insert a symbolic character name according to `sgml-char-names'."
460 (if sgml-name-8bit-mode
461 (sgml-name-char last-command-char
)
462 (self-insert-command 1)))
465 (defun sgml-name-8bit-mode ()
466 "Toggle insertion of 8 bit characters."
468 (setq sgml-name-8bit-mode
(not sgml-name-8bit-mode
))
469 (message "sgml name 8 bit mode is now %"
470 (if sgml-name-8bit-mode
"ON" "OFF")))
473 ;; When an element of a skeleton is a string "str", it is passed
474 ;; through skeleton-transformation and inserted. If "str" is to be
475 ;; inserted literally, one should obtain it as the return value of a
476 ;; function, e.g. (identity "str").
478 (define-skeleton sgml-tag
479 "Prompt for a tag and insert it, optionally with attributes.
480 Completion and configuration are done according to `sgml-tag-alist'.
481 If you like tags and attributes in uppercase do \\[set-variable]
482 skeleton-transformation RET upcase RET, or put this in your `.emacs':
483 (setq sgml-transformation 'upcase)"
484 (funcall skeleton-transformation
485 (completing-read "Tag: " sgml-tag-alist
))
486 ?
< (setq v1
(eval str
)) |
487 (("") -
1 '(undo-boundary) (identity "<")) |
; see comment above
488 (("") '(setq v2
(sgml-attributes v1 t
)) ?
>
489 (if (string= "![" v1
)
490 (prog1 '(("") " [ " _
" ]]")
493 (string-match "^[/!?]" v1
))
496 '(("") v2 _ v2
"</" v1 ?
>)
498 (cons '("") (cdr v2
))
499 (append '(("") (car v2
))
501 '(resume: (car v2
) _
"</" v1 ?
>))))))))
503 (autoload 'skeleton-read
"skeleton")
505 (defun sgml-attributes (tag &optional quiet
)
506 "When at top level of a tag, interactively insert attributes.
508 Completion and configuration of TAG are done according to `sgml-tag-alist'.
509 If QUIET, do not print a message when there are no attributes for TAG."
510 (interactive (list (save-excursion (sgml-beginning-of-tag t
))))
511 (or (stringp tag
) (error "Wrong context for adding attribute"))
513 (let ((completion-ignore-case t
)
514 (alist (cdr (assoc (downcase tag
) sgml-tag-alist
)))
516 (if (or (symbolp (car alist
))
517 (symbolp (car (car alist
))))
518 (setq car
(car alist
)
521 (message "No attributes configured."))
522 (if (stringp (car alist
))
524 (insert (if (eq (preceding-char) ?
) "" ?
)
525 (funcall skeleton-transformation
(car alist
)))
527 (setq i
(length alist
))
530 (insert (funcall skeleton-transformation
532 (skeleton-read '(completing-read
535 (if (string= "" attribute
)
537 (sgml-value (assoc attribute alist
))
539 (if (eq (preceding-char) ?
)
540 (delete-backward-char 1)))
543 (defun sgml-auto-attributes (arg)
544 "Self insert the character typed; at top level of tag, prompt for attributes.
545 With prefix argument, only self insert."
547 (let ((point (point))
550 (not sgml-tag-alist
) ; no message when nothing configured
551 (symbolp (setq tag
(save-excursion (sgml-beginning-of-tag t
))))
552 (eq (aref tag
0) ?
/))
553 (self-insert-command (prefix-numeric-value arg
))
554 (sgml-attributes tag
)
555 (setq last-command-char ?
)
556 (or (> (point) point
)
557 (self-insert-command 1)))))
560 (defun sgml-tag-help (&optional tag
)
561 "Display description of tag TAG. If TAG is omitted, use the tag at point."
565 (if (eq (following-char) ?
<)
567 (setq tag
(sgml-beginning-of-tag))))
569 (error "No tag selected"))
570 (setq tag
(downcase tag
))
572 (or (cdr (assoc tag sgml-tag-help
))
573 (and (eq (aref tag
0) ?
/)
574 (cdr (assoc (substring tag
1) sgml-tag-help
)))
575 "No description available")))
578 (defun sgml-maybe-end-tag ()
579 "Name self unless in position to end a tag."
581 (or (condition-case nil
582 (save-excursion (up-list -
1))
588 (save-excursion (up-list 1))
590 (error (self-insert-command 1)))))
593 (defun sgml-skip-tag-backward (arg)
594 "Skip to beginning of tag or matching opening tag if present.
595 With prefix argument ARG, repeat this ARG times."
598 (search-backward "<" nil t
)
599 (if (looking-at "</\\([^ \n\t>]+\\)")
600 ;; end tag, skip any nested pairs
601 (let ((case-fold-search t
)
602 (re (concat "</?" (regexp-quote (match-string 1)))))
603 (while (and (re-search-backward re nil t
)
604 (eq (char-after (1+ (point))) ?
/))
606 (sgml-skip-tag-backward 1))))
607 (setq arg
(1- arg
))))
609 (defun sgml-skip-tag-forward (arg &optional return
)
610 "Skip to end of tag or matching closing tag if present.
611 With prefix argument ARG, repeat this ARG times.
612 Return t iff after a closing tag."
616 (skip-chars-forward "^<>")
617 (if (eq (following-char) ?
>)
619 (if (looking-at "<\\([^/ \n\t>]+\\)")
620 ;; start tag, skip any nested same pairs _and_ closing tag
621 (let ((case-fold-search t
)
622 (re (concat "</?" (regexp-quote (match-string 1))))
626 (while (and (re-search-forward re nil t
)
628 (eq (char-after (1+ (match-beginning 0))) ?
/)))
630 (sgml-skip-tag-forward 1))
640 (defun sgml-delete-tag (arg)
641 "Delete tag on or after cursor, and matching closing or opening tag.
642 With prefix argument ARG, repeat this ARG times."
647 (if (looking-at "[ \t\n]*<")
649 (if (eq (char-after (match-end 0)) ?
/)
653 (goto-char (match-end 0))))
655 (or (save-excursion (setq close
(sgml-beginning-of-tag)
656 close
(and (stringp close
)
657 (eq (aref close
0) ?
/)
659 ;; not on closing tag
660 (let ((point (point)))
661 (sgml-skip-tag-backward 1)
662 (if (or (not (eq (following-char) ?
<))
666 (error "Not on or before tag")))))
669 (sgml-skip-tag-backward 1)
674 (sgml-skip-tag-forward 1)
677 (if (eq (aref (sgml-beginning-of-tag) 0) ?
/)
681 (setq arg
(1- arg
))))
683 ;; Put read-only last to enable setting this even when read-only enabled.
684 (or (get 'sgml-tag
'invisible
)
686 (append '(invisible t
688 point-entered sgml-point-entered
691 (symbol-plist 'sgml-tag
))))
693 (defun sgml-tags-invisible (arg)
694 "Toggle visibility of existing tags."
696 (let ((modified (buffer-modified-p))
697 (inhibit-read-only t
)
698 ;; This is needed in case font lock gets called,
699 ;; since it moves point and might call sgml-point-entered.
700 (inhibit-point-motion-hooks t
)
703 (goto-char (point-min))
704 (if (setq sgml-tags-invisible
706 (>= (prefix-numeric-value arg
) 0)
707 (not sgml-tags-invisible
)))
708 (while (re-search-forward "<\\([!/?A-Za-z][-A-Za-z0-9]*\\)"
710 (setq symbol
(intern-soft (downcase (match-string 1))))
711 (goto-char (match-beginning 0))
712 (and (get symbol
'before-string
)
713 (not (overlays-at (point)))
714 (overlay-put (make-overlay (point)
717 (put-text-property (point)
718 (progn (forward-list) (point))
719 'category
'sgml-tag
))
721 (while (< (setq pos
(next-overlay-change pos
)) (point-max))
722 (delete-overlay (car (overlays-at pos
)))))
723 (remove-text-properties (point-min) (point-max)
724 '(category sgml-tag intangible t
))))
725 (set-buffer-modified-p modified
)
726 (run-hooks 'sgml-tags-invisible-hook
)
729 (defun sgml-point-entered (x y
)
730 ;; Show preceding or following hidden tag, depending of cursor direction.
731 (let ((inhibit-point-motion-hooks t
))
733 (message "Invisible tag: %s"
737 (not (eq (following-char) ?
<)))
739 (eq (preceding-char) ?
>)))
743 (autoload 'compile-internal
"compile")
745 (defun sgml-validate (command)
746 "Validate an SGML document.
747 Runs COMMAND, a shell command, in a separate process asynchronously
748 with output going to the buffer `*compilation*'.
749 You can then use the command \\[next-error] to find the next error message
750 and move to the line in the SGML document that caused it."
752 (list (read-string "Validate command: "
753 (or sgml-saved-validate-command
754 (concat sgml-validate-command
756 (let ((name (buffer-file-name)))
758 (file-name-nondirectory name
))))))))
759 (setq sgml-saved-validate-command command
)
760 (if (or (not compilation-ask-about-save
)
761 (y-or-n-p (message "Save buffer %s? " (buffer-name))))
763 (compile-internal command
"No more errors"))
766 (defun sgml-beginning-of-tag (&optional top-level
)
767 "Skip to beginning of tag and return its name.
768 If this can't be done, return t."
774 (if (search-backward "<" nil t
)
779 (if (looking-at "<[!?/]?[[A-Za-z][A-Za-z0-9]*")
780 (buffer-substring-no-properties
785 (defun sgml-value (alist)
786 "Interactively insert value taken from attributerule ALIST.
787 See `sgml-tag-alist' for info about attributerules.."
788 (setq alist
(cdr alist
))
789 (if (stringp (car alist
))
790 (insert "=\"" (car alist
) ?
\")
791 (if (eq (car alist
) t
)
795 (setq alist
(skeleton-read '(completing-read
796 "Value: " (cdr alist
))))
797 (if (string< "" alist
)
799 (delete-backward-char 2))))
802 (insert (skeleton-read '(completing-read "Value: " alist
))))
807 (defvar html-quick-keys sgml-quick-keys
808 "Use C-c X combinations for quick insertion of frequent tags when non-nil.
809 This defaults to `sgml-quick-keys'.
810 This takes effect when first loading the library.")
812 (defvar html-mode-map
813 (let ((map (nconc (make-sparse-keymap) sgml-mode-map
))
814 (menu-map (make-sparse-keymap "HTML")))
815 (define-key map
"\C-c6" 'html-headline-6
)
816 (define-key map
"\C-c5" 'html-headline-5
)
817 (define-key map
"\C-c4" 'html-headline-4
)
818 (define-key map
"\C-c3" 'html-headline-3
)
819 (define-key map
"\C-c2" 'html-headline-2
)
820 (define-key map
"\C-c1" 'html-headline-1
)
821 (define-key map
"\C-c\r" 'html-paragraph
)
822 (define-key map
"\C-c\n" 'html-line
)
823 (define-key map
"\C-c\C-c-" 'html-horizontal-rule
)
824 (define-key map
"\C-c\C-co" 'html-ordered-list
)
825 (define-key map
"\C-c\C-cu" 'html-unordered-list
)
826 (define-key map
"\C-c\C-cr" 'html-radio-buttons
)
827 (define-key map
"\C-c\C-cc" 'html-checkboxes
)
828 (define-key map
"\C-c\C-cl" 'html-list-item
)
829 (define-key map
"\C-c\C-ch" 'html-href-anchor
)
830 (define-key map
"\C-c\C-cn" 'html-name-anchor
)
831 (define-key map
"\C-c\C-ci" 'html-image
)
834 (define-key map
"\C-c-" 'html-horizontal-rule
)
835 (define-key map
"\C-co" 'html-ordered-list
)
836 (define-key map
"\C-cu" 'html-unordered-list
)
837 (define-key map
"\C-cr" 'html-radio-buttons
)
838 (define-key map
"\C-cc" 'html-checkboxes
)
839 (define-key map
"\C-cl" 'html-list-item
)
840 (define-key map
"\C-ch" 'html-href-anchor
)
841 (define-key map
"\C-cn" 'html-name-anchor
)
842 (define-key map
"\C-ci" 'html-image
)))
843 (define-key map
"\C-c\C-s" 'html-autoview-mode
)
844 (define-key map
"\C-c\C-v" 'browse-url-of-buffer
)
845 (define-key map
[menu-bar html
] (cons "HTML" menu-map
))
846 (define-key menu-map
[html-autoview-mode
]
847 '("Toggle Autoviewing" . html-autoview-mode
))
848 (define-key menu-map
[browse-url-of-buffer
]
849 '("View Buffer Contents" . browse-url-of-buffer
))
850 (define-key menu-map
[nil] '("--"))
851 ;;(define-key menu-map "6" '("Heading 6" . html-headline-6))
852 ;;(define-key menu-map "5" '("Heading 5" . html-headline-5))
853 ;;(define-key menu-map "4" '("Heading 4" . html-headline-4))
854 (define-key menu-map "3" '("Heading 3" . html-headline-3))
855 (define-key menu-map "2" '("Heading 2" . html-headline-2))
856 (define-key menu-map "1" '("Heading 1" . html-headline-1))
857 (define-key menu-map "l" '("Radio Buttons" . html-radio-buttons))
858 (define-key menu-map "c" '("Checkboxes" . html-checkboxes))
859 (define-key menu-map "l" '("List Item" . html-list-item))
860 (define-key menu-map "u" '("Unordered List" . html-unordered-list))
861 (define-key menu-map "o" '("Ordered List" . html-ordered-list))
862 (define-key menu-map "-" '("Horizontal Rule" . html-horizontal-rule))
863 (define-key menu-map "\n" '("Line Break" . html-line))
864 (define-key menu-map "\r" '("Paragraph" . html-paragraph))
865 (define-key menu-map "i" '("Image" . html-image))
866 (define-key menu-map "h" '("Href Anchor" . html-href-anchor))
867 (define-key menu-map "n" '("Name Anchor" . html-name-anchor))
869 "Keymap for commands for use in HTML mode.")
872 (defvar html-face-tag-alist
877 "Value of `sgml-face-tag-alist' for HTML mode.")
879 (defvar html-tag-face-alist
882 ("blink" . highlight)
885 ("h1" bold underline)
886 ("h2" bold-italic underline)
887 ("h3" italic underline)
896 ("title" bold underline)
900 "Value of `sgml-tag-face-alist' for HTML mode.")
903 (defvar html-display-text
907 "Value of `sgml-display-text' for HTML mode.")
910 ; should code exactly HTML 3 here when that is finished
911 (defvar html-tag-alist
912 (let* ((1-7 '(("1") ("2") ("3") ("4") ("5") ("6") ("7")))
913 (1-9 '(,@1-7 ("8") ("9")))
914 (align '(("align" ("left") ("center") ("right"))))
915 (valign '(("top") ("middle") ("bottom") ("baseline")))
916 (rel '(("next") ("previous") ("parent") ("subdocument") ("made")))
917 (href '("href" ("ftp:") ("file:") ("finger:") ("gopher:") ("http:")
918 ("mailto:") ("news:") ("rlogin:") ("telnet:") ("tn3270:")
919 ("wais:") ("/cgi-bin/")))
925 (list '((nil \n ( "List item: "
933 ;; put ,-expressions first, else byte-compile chokes (as of V19.29)
934 ;; and like this it's more efficient anyway
938 ("font" nil "size" ("-1") ("+1") ("-2") ("+2") ,@1-7)
939 ("form" (\n _ \n "<input type=\"submit\" value=\"\">")
940 ("action" ,@(cdr href)) ("method" ("get") ("post")))
947 ("hr" t ("size" ,@1-9) ("width") ("noshade" t) ,@align)
948 ("img" t ("align" ,@valign ("texttop") ("absmiddle") ("absbottom"))
949 ("src") ("alt") ("width" "1") ("height" "1")
950 ("border" "1") ("vspace" "1") ("hspace" "1") ("ismap" t))
951 ("input" t ("size" ,@1-9) ("maxlength" ,@1-9) ("checked" t) ,name
952 ("type" ("text") ("password") ("checkbox") ("radio")
953 ("submit") ("reset"))
957 ("ol" ,@list ("type" ("A") ("a") ("I") ("i") ("1")))
962 ,name ("size" ,@1-9) ("multiple" t))
964 ((completing-read "Cell kind: " '(("td") ("th"))
966 "<tr><" str ?> _ \n))
967 ("border" t ,@1-9) ("width" "10") ("cellpadding"))
969 ("textarea" ,name ("rows" ,@1-9) ("cols" ,@1-9))
971 ("ul" ,@list ("type" ("disc") ("circle") ("square")))
979 ("Item: " "<item>" str \n))
986 ("body" \n ("background" ".gif") ("bgcolor" "#") ("text" "#")
987 ("link" "#") ("alink" "#") ("vlink" "#"))
988 ("box" (nil _ "<over>" _))
989 ("br" t ("clear" ("left") ("right")))
990 ("caption" ("valign" ("top") ("bottom")))
999 "<dt>" str "<dd>" _ \n)))
1002 ;("fn" "id" "fn") ; ???
1006 "<title>" (setq str (read-input "Title: ")) "</title>\n"
1007 "<body>\n<h1>" str "</h1>\n" _
1008 "\n<address>\n<a href=\"mailto:"
1010 "\">" (user-full-name) "</a>\n</address>"))
1013 ("isindex" t ("action") ("prompt"))
1019 ("option" t ("value") ("label") ("selected" t))
1037 "*Value of `sgml-tag-alist' for HTML mode.")
1039 (defvar html-tag-help
1041 ("a" . "Anchor of point or link elsewhere")
1044 ("address" . "Formatted mail address")
1045 ("array" . "Math array")
1048 ("base" . "Base address for URLs")
1049 ("big" . "Font size")
1050 ("blink" . "Blinking text")
1051 ("blockquote" . "Indented quotation")
1052 ("body" . "Document body")
1053 ("box" . "Math fraction")
1054 ("br" . "Line break")
1055 ("caption" . "Table caption")
1056 ("center" . "Centered text")
1057 ("changed" . "Change bars")
1058 ("cite" . "Citation of a document")
1059 ("code" . "Formatted source code")
1060 ("dd" . "Definition of term")
1063 ("dir" . "Directory list (obsolete)")
1064 ("dl" . "Definition list")
1065 ("dt" . "Term to be definined")
1066 ("em" . "Emphasised")
1067 ("embed" . "Embedded data in foreign format")
1069 ("figa" . "Figure anchor")
1070 ("figd" . "Figure description")
1071 ("figt" . "Figure text")
1073 ("font" . "Font size")
1074 ("form" . "Form with input fields")
1075 ("group" . "Document grouping")
1076 ("h1" . "Most important section headline")
1077 ("h2" . "Important section headline")
1078 ("h3" . "Section headline")
1079 ("h4" . "Minor section headline")
1080 ("h5" . "Unimportant section headline")
1081 ("h6" . "Least important section headline")
1082 ("head" . "Document header")
1083 ("hr" . "Horizontal rule")
1084 ("html" . "HTML Document")
1085 ("i" . "Italic face")
1086 ("img" . "Graphic image")
1087 ("input" . "Form input field")
1089 ("isindex" . "Input field for index search")
1090 ("kbd" . "Keybard example face")
1091 ("lang" . "Natural language")
1092 ("li" . "List item")
1093 ("link" . "Link relationship")
1094 ("math" . "Math formula")
1095 ("menu" . "Menu list (obsolete)")
1096 ("mh" . "Form mail header")
1097 ("nextid" . "Allocate new id")
1098 ("nobr" . "Text without line break")
1099 ("ol" . "Ordered list")
1100 ("option" . "Selection list item")
1101 ("over" . "Math fraction rule")
1102 ("p" . "Paragraph start")
1103 ("panel" . "Floating panel")
1105 ("pre" . "Preformatted fixed width text")
1107 ("rev" . "Reverse video")
1109 ("samp" . "Sample text")
1110 ("select" . "Selection list")
1111 ("small" . "Font size")
1112 ("sp" . "Nobreak space")
1113 ("strong" . "Standout text")
1114 ("sub" . "Subscript")
1115 ("sup" . "Superscript")
1116 ("table" . "Table with rows and columns")
1117 ("tb" . "Table vertical break")
1118 ("td" . "Table data cell")
1119 ("textarea" . "Form multiline edit area")
1120 ("th" . "Table header cell")
1121 ("title" . "Document title")
1122 ("tr" . "Table row separator")
1123 ("tt" . "Typewriter face")
1124 ("u" . "Underlined text")
1125 ("ul" . "Unordered list")
1126 ("var" . "Math variable face")
1127 ("wbr" . "Enable <br> within <nobr>"))
1128 "*Value of `sgml-tag-help' for HTML mode.")
1134 "Major mode based on SGML mode for editing HTML documents.
1135 This allows inserting skeleton costructs used in hypertext documents with
1136 completion. See below for an introduction to HTML. Use
1137 \\[browse-url-of-buffer] to see how this comes out. See also `sgml-mode' on
1138 which this is based.
1140 Do \\[describe-variable] html- SPC and \\[describe-variable] sgml- SPC to see available variables.
1142 To write fairly well formatted pages you only need to know few things. Most
1143 browsers have a function to read the source code of the page being seen, so
1144 you can imitate various tricks. Here's a very short HTML primer which you
1145 can also view with a browser to see what happens:
1147 <title>A Title Describing Contents</title> should be on every page. Pages can
1148 have <h1>Very Major Headlines</h1> through <h6>Very Minor Headlines</h6>
1149 <hr> Parts can be separated with horizontal rules.
1151 <p>Paragraphs only need an opening tag. Line breaks and multiple spaces are
1152 ignored unless the text is <pre>preformatted.</pre> Text can be marked as
1153 <b>bold</b>, <i>italic</i> or <u>underlined</u> using the normal M-g or
1154 Edit/Text Properties/Face commands.
1156 Pages can have <a name=\"SOMENAME\">named points</a> and can link other points
1157 to them with <a href=\"#SOMENAME\">see also somename</a>. In the same way <a
1158 href=\"URL\">see also URL</a> where URL is a filename relative to current
1159 directory, or absolute as in `http://www.cs.indiana.edu/elisp/w3/docs.html'.
1161 Images in many formats can be inlined with <img src=\"URL\">.
1163 If you mainly create your own documents, `sgml-specials' might be
1164 interesting. But note that some HTML 2 browsers can't handle `''.
1165 To work around that, do:
1166 (eval-after-load \"sgml-mode\" '(aset sgml-char-names ?' nil))
1170 (sgml-mode-common html-tag-face-alist html-display-text)
1171 (use-local-map html-mode-map)
1172 (make-local-variable 'sgml-tag-alist)
1173 (make-local-variable 'sgml-face-tag-alist)
1174 (make-local-variable 'sgml-tag-help)
1175 (make-local-variable 'outline-regexp)
1176 (make-local-variable 'outline-heading-end-regexp)
1177 (make-local-variable 'outline-level)
1178 (make-local-variable 'sentence-end)
1180 "[.?!][]\"')}]*\\(<[^>]*>\\)*\\($\\| $\\|\t\\| \\)[ \t\n]*")
1181 (setq mode-name "HTML"
1182 major-mode 'html-mode
1183 sgml-tag-alist html-tag-alist
1184 sgml-face-tag-alist html-face-tag-alist
1185 sgml-tag-help html-tag-help
1186 outline-regexp "^.*<[Hh][1-6]\\>"
1187 outline-heading-end-regexp "</[Hh][1-6]>"
1188 outline-level (lambda ()
1189 (char-after (1- (match-end 0)))))
1190 (run-hooks 'html-mode-hook))
1193 (define-skeleton html-href-anchor
1194 "HTML anchor tag with href attribute."
1196 '(setq input "http:")
1197 "<a href=\"" str "\">" _ "</a>")
1199 (define-skeleton html-name-anchor
1200 "HTML anchor tag with name attribute."
1202 "<a name=\"" str "\">" _ "</a>")
1204 (define-skeleton html-headline-1
1205 "HTML level 1 headline tags."
1209 (define-skeleton html-headline-2
1210 "HTML level 2 headline tags."
1214 (define-skeleton html-headline-3
1215 "HTML level 3 headline tags."
1219 (define-skeleton html-headline-4
1220 "HTML level 4 headline tags."
1224 (define-skeleton html-headline-5
1225 "HTML level 5 headline tags."
1229 (define-skeleton html-headline-6
1230 "HTML level 6 headline tags."
1234 (define-skeleton html-horizontal-rule
1235 "HTML horizontal rule tag."
1239 (define-skeleton html-image
1242 "<img src=\"" _ "\">")
1244 (define-skeleton html-line
1245 "HTML line break tag."
1249 (define-skeleton html-ordered-list
1250 "HTML ordered list tags."
1256 (define-skeleton html-unordered-list
1257 "HTML unordered list tags."
1263 (define-skeleton html-list-item
1264 "HTML list item tag."
1269 (define-skeleton html-paragraph
1270 "HTML paragraph tag."
1275 (define-skeleton html-checkboxes
1276 "Group of connected checkbox inputs."
1281 "<input type=\"" (identity "checkbox") ; see comment above about identity
1282 "\" name=\"" (or v1 (setq v1 (skeleton-read "Name: ")))
1283 "\" value=\"" str ?\"
1284 (if (y-or-n-p "Set \"checked\" attribute? ")
1285 (funcall skeleton-transformation " checked")) ">"
1286 (skeleton-read "Text: " (capitalize str))
1287 (or v2 (setq v2 (if (y-or-n-p "Newline after text? ")
1288 (funcall skeleton-transformation "<br>")
1292 (define-skeleton html-radio-buttons
1293 "Group of connected radio button inputs."
1298 "<input type=\"" (identity "radio") ; see comment above about identity
1299 "\" name=\"" (or (car v2) (setcar v2 (skeleton-read "Name: ")))
1300 "\" value=\"" str ?\"
1301 (if (and (not v1) (setq v1 (y-or-n-p "Set \"checked\" attribute? ")))
1302 (funcall skeleton-transformation " checked") ">")
1303 (skeleton-read "Text: " (capitalize str))
1304 (or (cdr v2) (setcdr v2 (if (y-or-n-p "Newline after text? ")
1305 (funcall skeleton-transformation "<br>")
1310 (defun html-autoview-mode (&optional arg)
1311 "Toggle automatic viewing via `html-viewer' upon saving buffer.
1312 With positive prefix ARG always turns viewing on, with negative ARG always off.
1313 Can be used as a value for `html-mode-hook'."
1315 (if (setq arg (if arg
1316 (< (prefix-numeric-value arg) 0)
1317 (and (boundp 'after-save-hook)
1318 (memq 'browse-url-of-buffer after-save-hook))))
1319 (setq after-save-hook (delq 'browse-url-of-buffer after-save-hook))
1320 (make-local-hook 'after-save-hook)
1321 (add-hook 'after-save-hook 'browse-url-of-buffer nil t))
1322 (message "Autoviewing turned %s."
1323 (if arg "off" "on")))
1325 ;;; sgml-mode.el ends here