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 <occitan@esperanto.org>,
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.
43 (defcustom sgml-transformation
'identity
44 "*Default value for `skeleton-transformation' (which see) in SGML mode."
48 (put 'sgml-transformation
'variable-interactive
49 "aTransformation function: ")
51 (defcustom sgml-mode-hook nil
52 "Hook run by command `sgml-mode'.
53 `text-mode-hook' is run first."
57 ;; As long as Emacs' syntax can't be complemented with predicates to context
58 ;; sensitively confirm the syntax of characters, we have to live with this
59 ;; kludgy kind of tradeoff.
60 (defvar sgml-specials
'(?
\")
61 "List of characters that have a special meaning for SGML mode.
62 This list is used when first loading the sgml-mode library.
63 The supported characters and potential disadvantages are:
65 ?\\\" Makes \" in text start a string.
66 ?' Makes ' in text start a string.
67 ?- Makes -- in text start a comment.
69 When only one of ?\\\" or ?' are included, \"'\" or '\"', as can be found in
70 DTDs, start a string. To partially avoid this problem this also makes these
71 self insert as named entities depending on `sgml-quick-keys'.
73 Including ?- has the problem of affecting dashes that have nothing to do
74 with comments, so we normally turn it off.")
76 (defvar sgml-quick-keys nil
77 "Use <, >, &, SPC and `sgml-specials' keys \"electrically\" when non-nil.
78 This takes effect when first loading the sgml-mode library.")
82 (let ((map (list 'keymap
(make-vector 256 nil
)))
83 (menu-map (make-sparse-keymap "SGML")))
84 (define-key map
"\t" 'indent-relative-maybe
)
85 (define-key map
"\C-c\C-i" 'sgml-tags-invisible
)
86 (define-key map
"/" 'sgml-slash
)
87 (define-key map
"\C-c\C-n" 'sgml-name-char
)
88 (define-key map
"\C-c\C-t" 'sgml-tag
)
89 (define-key map
"\C-c\C-a" 'sgml-attributes
)
90 (define-key map
"\C-c\C-b" 'sgml-skip-tag-backward
)
91 (define-key map
[?\C-c left
] 'sgml-skip-tag-backward
)
92 (define-key map
"\C-c\C-f" 'sgml-skip-tag-forward
)
93 (define-key map
[?\C-c right
] 'sgml-skip-tag-forward
)
94 (define-key map
"\C-c\C-d" 'sgml-delete-tag
)
95 (define-key map
"\C-c\^?" 'sgml-delete-tag
)
96 (define-key map
"\C-c?" 'sgml-tag-help
)
97 (define-key map
"\C-c8" 'sgml-name-8bit-mode
)
98 (define-key map
"\C-c\C-v" 'sgml-validate
)
101 (define-key map
"&" 'sgml-name-char
)
102 (define-key map
"<" 'sgml-tag
)
103 (define-key map
" " 'sgml-auto-attributes
)
104 (define-key map
">" 'sgml-maybe-end-tag
)
105 (if (memq ?
\" sgml-specials
)
106 (define-key map
"\"" 'sgml-name-self
))
107 (if (memq ?
' sgml-specials
)
108 (define-key map
"'" 'sgml-name-self
))))
111 (while (< (setq c
(1+ c
)) 256)
112 (aset map c
'sgml-maybe-name-self
)))
113 (define-key map
[menu-bar sgml
] (cons "SGML" menu-map
))
114 (define-key menu-map
[sgml-validate
] '("Validate" . sgml-validate
))
115 (define-key menu-map
[sgml-name-8bit-mode
]
116 '("Toggle 8 Bit Insertion" . sgml-name-8bit-mode
))
117 (define-key menu-map
[sgml-tags-invisible
]
118 '("Toggle Tag Visibility" . sgml-tags-invisible
))
119 (define-key menu-map
[sgml-tag-help
]
120 '("Describe Tag" . sgml-tag-help
))
121 (define-key menu-map
[sgml-delete-tag
]
122 '("Delete Tag" . sgml-delete-tag
))
123 (define-key menu-map
[sgml-skip-tag-forward
]
124 '("Forward Tag" . sgml-skip-tag-forward
))
125 (define-key menu-map
[sgml-skip-tag-backward
]
126 '("Backward Tag" . sgml-skip-tag-backward
))
127 (define-key menu-map
[sgml-attributes
]
128 '("Insert Attributes" . sgml-attributes
))
129 (define-key menu-map
[sgml-tag
] '("Insert Tag" . sgml-tag
))
131 "Keymap for SGML mode. See also `sgml-specials'.")
134 (defvar sgml-mode-syntax-table
135 (let ((table (copy-syntax-table text-mode-syntax-table
)))
136 (modify-syntax-entry ?
< "(>" table
)
137 (modify-syntax-entry ?
> ")<" table
)
138 (if (memq ?- sgml-specials
)
139 (modify-syntax-entry ?-
"_ 1234" table
))
140 (if (memq ?
\" sgml-specials
)
141 (modify-syntax-entry ?
\" "\"\"" table
))
142 (if (memq ?
' sgml-specials
)
143 (modify-syntax-entry ?
\' "\"'" table
))
145 "Syntax table used in SGML mode. See also `sgml-specials'.")
148 (defcustom sgml-name-8bit-mode nil
149 "*When non-nil, insert 8 bit characters with their names."
153 (defvar sgml-char-names
154 [nil nil nil nil nil nil nil nil
155 nil nil nil nil nil nil nil nil
156 nil nil nil nil nil nil nil nil
157 nil nil nil nil nil nil nil nil
158 "nbsp" "excl" "quot" "num" "dollar" "percnt" "amp" "apos"
159 "lpar" "rpar" "ast" "plus" "comma" "hyphen" "period" "sol"
160 nil nil nil nil nil nil nil nil
161 nil nil
"colon" "semi" "lt" "eq" "gt" "quest"
162 "commat" nil nil nil nil nil nil nil
163 nil nil nil nil nil nil nil nil
164 nil nil nil nil nil nil nil nil
165 nil nil nil
"lsqb" nil
"rsqb" "uarr" "lowbar"
166 "lsquo" nil nil nil nil nil nil nil
167 nil nil nil nil nil nil nil nil
168 nil nil nil nil nil nil nil nil
169 nil nil nil
"lcub" "verbar" "rcub" "tilde" nil
170 nil nil nil nil nil nil nil nil
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 "nbsp" "iexcl" "cent" "pound" "curren" "yen" "brvbar" "sect"
175 "uml" "copy" "ordf" "laquo" "not" "shy" "reg" "macr"
176 "ring" "plusmn" "sup2" "sup3" "acute" "micro" "para" "middot"
177 "cedil" "sup1" "ordm" "raquo" "frac14" "frac12" "frac34" "iquest"
178 "Agrave" "Aacute" "Acirc" "Atilde" "Auml" "Aring" "AElig" "Ccedil"
179 "Egrave" "Eacute" "Ecirc" "Euml" "Igrave" "Iacute" "Icirc" "Iuml"
180 "ETH" "Ntilde" "Ograve" "Oacute" "Ocirc" "Otilde" "Ouml" nil
181 "Oslash" "Ugrave" "Uacute" "Ucirc" "Uuml" "Yacute" "THORN" "szlig"
182 "agrave" "aacute" "acirc" "atilde" "auml" "aring" "aelig" "ccedil"
183 "egrave" "eacute" "ecirc" "euml" "igrave" "iacute" "icirc" "iuml"
184 "eth" "ntilde" "ograve" "oacute" "ocirc" "otilde" "ouml" "divide"
185 "oslash" "ugrave" "uacute" "ucirc" "uuml" "yacute" "thorn" "yuml"]
186 "Vector of symbolic character names without `&' and `;'.")
189 ;; nsgmls is a free SGML parser in the SP suite available from
190 ;; ftp.jclark.com and otherwise packaged for GNU systems.
191 ;; Its error messages can be parsed by next-error.
192 ;; The -s option suppresses output.
194 (defcustom sgml-validate-command
"nsgmls -s" ; replaced old `sgmls'
195 "*The command to validate an SGML document.
196 The file name of current buffer file name will be appended to this,
197 separated by a space."
202 (defvar sgml-saved-validate-command nil
203 "The command last used to validate in this buffer.")
206 ;;; I doubt that null end tags are used much for large elements,
207 ;;; so use a small distance here.
208 (defcustom sgml-slash-distance
1000
209 "*If non-nil, is the maximum distance to search for matching `/'."
210 :type
'(choice (const nil
) integer
)
213 (defconst sgml-start-tag-regex
214 "<[A-Za-z]\\([-.A-Za-z0-9= \n\t]\\|\"[^\"]*\"\\|'[^']*'\\)*"
215 "Regular expression that matches a non-empty start tag.
216 Any terminating `>' or `/' is not matched.")
220 (defconst sgml-font-lock-keywords-1
221 '(("<\\([!?][a-z][-.a-z0-9]*\\)" 1 font-lock-keyword-face
)
222 ("<\\(/?[a-z][-.a-z0-9]*\\)" 1 font-lock-function-name-face
)
223 ("[&%][a-z][-.a-z0-9]*;?" . font-lock-variable-name-face
)
224 ("<! *--.*-- *>" . font-lock-comment-face
)))
226 (defconst sgml-font-lock-keywords-2
())
228 ;; for font-lock, but must be defvar'ed after
229 ;; sgml-font-lock-keywords-1 and sgml-font-lock-keywords-2 above
230 (defvar sgml-font-lock-keywords sgml-font-lock-keywords-1
231 "*Rules for highlighting SGML code. See also `sgml-tag-face-alist'.")
234 (defvar sgml-face-tag-alist
()
235 "Alist of face and tag name for facemenu.")
237 (defvar sgml-tag-face-alist
()
238 "Tag names and face or list of faces to fontify with when invisible.
239 When `font-lock-maximum-decoration' is 1 this is always used for fontifying.
240 When more these are fontified together with `sgml-font-lock-keywords'.")
243 (defvar sgml-display-text
()
244 "Tag names as lowercase symbols, and display string when invisible.")
247 (defvar sgml-tags-invisible nil
)
250 (defcustom sgml-tag-alist
251 '(("![" ("ignore" t
) ("include" t
))
256 "*Alist of tag names for completing read and insertion rules.
257 This alist is made up as
262 TAGRULE is a list of optionally `t' (no endtag) or `\\n' (separate endtag by
263 newlines) or a skeleton with `nil', `t' or `\\n' in place of the interactor
264 followed by an ATTRIBUTERULE (for an always present attribute) or an
267 The attribute alist is made up as
269 ((\"attribute\" . ATTRIBUTERULE)
272 ATTRIBUTERULE is a list of optionally `t' (no value when no input) followed by
273 an optional alist of possible values."
274 :type
'(repeat (cons (string :tag
"Tag Name")
275 (repeat :tag
"Tag Rule" sexp
)))
278 (defcustom sgml-tag-help
279 '(("!" .
"Empty declaration for comment")
280 ("![" .
"Embed declarations with parser directive")
281 ("!attlist" .
"Tag attributes declaration")
282 ("!doctype" .
"Document type (DTD) declaration")
283 ("!element" .
"Tag declaration")
284 ("!entity" .
"Entity (macro) declaration"))
285 "*Alist of tag name and short description."
286 :type
'(repeat (cons (string :tag
"Tag Name")
287 (string :tag
"Description")))
290 (defvar v2
) ; free for skeleton
292 (defun sgml-mode-common (sgml-tag-face-alist sgml-display-text
)
293 "Common code for setting up `sgml-mode' and derived modes.
294 SGML-TAG-FACE-ALIST is used for calculating `sgml-font-lock-keywords-2'.
295 SGML-DISPLAY-TEXT sets up alternate text for when tags are invisible (see
296 varables of same name)."
297 (setq local-abbrev-table text-mode-abbrev-table
)
298 (set-syntax-table sgml-mode-syntax-table
)
299 (make-local-variable 'indent-line-function
)
300 (make-local-variable 'paragraph-start
)
301 (make-local-variable 'paragraph-separate
)
302 (make-local-variable 'adaptive-fill-regexp
)
303 (make-local-variable 'sgml-saved-validate-command
)
304 (make-local-variable 'comment-start
)
305 (make-local-variable 'comment-end
)
306 (make-local-variable 'comment-indent-function
)
307 (make-local-variable 'comment-indent-function
)
308 (make-local-variable 'sgml-tags-invisible
)
309 (make-local-variable 'skeleton-transformation
)
310 (make-local-variable 'skeleton-further-elements
)
311 (make-local-variable 'skeleton-end-hook
)
312 (make-local-variable 'font-lock-defaults
)
313 (make-local-variable 'sgml-font-lock-keywords-1
)
314 (make-local-variable 'sgml-font-lock-keywords-2
)
315 (make-local-variable 'facemenu-add-face-function
)
316 (make-local-variable 'facemenu-end-add-face
)
317 ;;(make-local-variable 'facemenu-remove-face-function)
318 (and sgml-tag-face-alist
319 (not (assq 1 sgml-tag-face-alist
))
320 (nconc sgml-tag-face-alist
321 `((1 (,(concat "<\\("
322 (mapconcat 'car sgml-tag-face-alist
"\\|")
323 "\\)\\([ \t].+\\)?>\\(.+\\)</\\1>")
324 3 (cdr (assoc (downcase (match-string 1))
325 ',sgml-tag-face-alist
)))))))
326 (setq indent-line-function
'indent-relative-maybe
327 ;; A start or end tag by itself on a line separates a paragraph.
328 ;; This is desirable because SGML discards a newline that appears
329 ;; immediately after a start tag or immediately before an end tag.
330 paragraph-separate
"[ \t]*$\\|\
331 \[ \t]*</?\\([A-Za-z]\\([-.A-Za-z0-9= \t\n]\\|\"[^\"]*\"\\|'[^']*'\\)*\\)?>$"
332 paragraph-start
"[ \t]*$\\|\
333 \[ \t]*</?\\([A-Za-z]\\([-.A-Za-z0-9= \t\n]\\|\"[^\"]*\"\\|'[^']*'\\)*\\)?>"
334 adaptive-fill-regexp
"[ \t]*"
335 comment-start
"<!-- "
337 comment-indent-function
'sgml-comment-indent
338 skeleton-transformation sgml-transformation
339 skeleton-further-elements
'((completion-ignore-case t
))
340 skeleton-end-hook
(lambda ()
343 (eq (car-safe v2
) '\n)))
344 (newline-and-indent)))
345 sgml-font-lock-keywords-2
(append
346 sgml-font-lock-keywords-1
347 (cdr (assq 1 sgml-tag-face-alist
)))
348 font-lock-defaults
'((sgml-font-lock-keywords
349 sgml-font-lock-keywords-1
350 sgml-font-lock-keywords-2
)
353 facemenu-add-face-function
'sgml-mode-facemenu-add-face-function
)
354 ;; This will allow existing comments within declarations to be
356 (set (make-local-variable 'comment-start-skip
) "\\(?:<!\\)?--[ \t]*")
357 (set (make-local-variable 'comment-end-skip
) "[ \t]*--\\([ \t\n]*>\\)?")
358 (dolist (pair sgml-display-text
)
359 (put (car pair
) 'before-string
(cdr pair
))))
362 (defun sgml-mode-facemenu-add-face-function (face end
)
363 (if (setq face
(cdr (assq face sgml-face-tag-alist
)))
365 (setq face
(funcall skeleton-transformation face
))
366 (setq facemenu-end-add-face
(concat "</" face
">"))
367 (concat "<" face
">"))
368 (error "Face not configured for %s mode." mode-name
)))
373 "Major mode for editing SGML documents.
374 Makes > match <. Makes / blink matching /.
375 Keys <, &, SPC within <>, \" and ' can be electric depending on
378 An argument of N to a tag-inserting command means to wrap it around
379 the next N words. In Transient Mark mode, when the mark is active,
380 N defaults to -1, which means to wrap it around the current region.
382 If you like upcased tags, put (setq sgml-transformation 'upcase) in
385 Use \\[sgml-validate] to validate your document with an SGML parser.
387 Do \\[describe-variable] sgml- SPC to see available variables.
388 Do \\[describe-key] on the following bindings to discover what they do.
391 (kill-all-local-variables)
392 (setq mode-name
"SGML"
393 major-mode
'sgml-mode
)
394 (sgml-mode-common sgml-tag-face-alist sgml-display-text
)
395 ;; Set imenu-generic-expression here, rather than in sgml-mode-common,
396 ;; because this definition probably is not useful in HTML mode.
397 (make-local-variable 'imenu-generic-expression
)
398 (setq imenu-generic-expression
399 "<!\\(element\\|entity\\)[ \t\n]+%?[ \t\n]*\\([A-Za-z][-A-Za-z.0-9]*\\)")
400 (use-local-map sgml-mode-map
)
401 (run-hooks 'text-mode-hook
'sgml-mode-hook
))
404 (defun sgml-comment-indent ()
405 (if (looking-at "--") comment-column
0))
409 (defun sgml-slash (arg)
410 "Insert `/' and display any previous matching `/'.
411 Two `/'s are treated as matching if the first `/' ends a net-enabling
412 start tag, and the second `/' is the corresponding null end tag."
416 (let ((oldpos (point))
421 (if sgml-slash-distance
422 (narrow-to-region (max (point-min)
423 (- (point) sgml-slash-distance
))
425 (if (and (re-search-backward sgml-start-tag-regex
(point-min) t
)
426 (eq (match-end 0) (1- oldpos
)))
428 (goto-char (1- oldpos
))
429 (while (and (not blinkpos
)
430 (search-backward "/" (point-min) t
))
431 (let ((tagend (save-excursion
432 (if (re-search-backward sgml-start-tag-regex
436 (if (eq tagend
(point))
438 (setq blinkpos
(point))
439 (setq level
(1- level
)))
440 (setq level
(1+ level
)))))))
444 (if (pos-visible-in-window-p)
446 (message "Matches %s"
447 (buffer-substring (progn
450 (1+ blinkpos
))))))))))
453 (defun sgml-name-char (&optional char
)
454 "Insert a symbolic character name according to `sgml-char-names'.
455 8 bit chars may be inserted with the meta key as in M-SPC for no break space,
456 or M-- for a soft hyphen."
460 (setq char
(read-quoted-char "Enter char or octal number")))
461 (delete-backward-char 1)
464 (delete-backward-char 1)
466 (or (aref sgml-char-names char
)
471 (defun sgml-name-self ()
472 "Insert a symbolic character name according to `sgml-char-names'."
474 (sgml-name-char last-command-char
))
477 (defun sgml-maybe-name-self ()
478 "Insert a symbolic character name according to `sgml-char-names'."
480 (if sgml-name-8bit-mode
481 (sgml-name-char last-command-char
)
482 (self-insert-command 1)))
485 (defun sgml-name-8bit-mode ()
486 "Toggle insertion of 8 bit characters."
488 (setq sgml-name-8bit-mode
(not sgml-name-8bit-mode
))
489 (message "sgml name 8 bit mode is now %s"
490 (if sgml-name-8bit-mode
"ON" "OFF")))
493 ;; When an element of a skeleton is a string "str", it is passed
494 ;; through skeleton-transformation and inserted. If "str" is to be
495 ;; inserted literally, one should obtain it as the return value of a
496 ;; function, e.g. (identity "str").
498 (define-skeleton sgml-tag
499 "Prompt for a tag and insert it, optionally with attributes.
500 Completion and configuration are done according to `sgml-tag-alist'.
501 If you like tags and attributes in uppercase do \\[set-variable]
502 skeleton-transformation RET upcase RET, or put this in your `.emacs':
503 (setq sgml-transformation 'upcase)"
504 (completing-read "Tag: " sgml-tag-alist
)
506 (("") -
1 '(undo-boundary) (identity "<")) |
; see comment above
507 `(("") '(setq v2
(sgml-attributes ,str t
)) ?
>
508 (if (string= "![" ,str
)
509 (prog1 '(("") " [ " _
" ]]")
512 (string-match "^[/!?]" ,str
))
515 '(("") v2 _ v2
"</" ,str ?
>)
517 (cons '("") (cdr v2
))
518 (append '(("") (car v2
))
520 '(resume: (car v2
) _
"</" ,str ?
>))))))))
522 (autoload 'skeleton-read
"skeleton")
524 (defun sgml-attributes (tag &optional quiet
)
525 "When at top level of a tag, interactively insert attributes.
527 Completion and configuration of TAG are done according to `sgml-tag-alist'.
528 If QUIET, do not print a message when there are no attributes for TAG."
529 (interactive (list (save-excursion (sgml-beginning-of-tag t
))))
530 (or (stringp tag
) (error "Wrong context for adding attribute"))
532 (let ((completion-ignore-case t
)
533 (alist (cdr (assoc (downcase tag
) sgml-tag-alist
)))
535 (if (or (symbolp (car alist
))
536 (symbolp (car (car alist
))))
537 (setq car
(car alist
)
540 (message "No attributes configured."))
541 (if (stringp (car alist
))
543 (insert (if (eq (preceding-char) ?
) "" ?
)
544 (funcall skeleton-transformation
(car alist
)))
546 (setq i
(length alist
))
549 (insert (funcall skeleton-transformation
551 (skeleton-read '(completing-read
554 (if (string= "" attribute
)
556 (sgml-value (assoc (downcase attribute
) alist
))
558 (if (eq (preceding-char) ?
)
559 (delete-backward-char 1)))
562 (defun sgml-auto-attributes (arg)
563 "Self insert the character typed; at top level of tag, prompt for attributes.
564 With prefix argument, only self insert."
566 (let ((point (point))
569 (not sgml-tag-alist
) ; no message when nothing configured
570 (symbolp (setq tag
(save-excursion (sgml-beginning-of-tag t
))))
571 (eq (aref tag
0) ?
/))
572 (self-insert-command (prefix-numeric-value arg
))
573 (sgml-attributes tag
)
574 (setq last-command-char ?
)
575 (or (> (point) point
)
576 (self-insert-command 1)))))
579 (defun sgml-tag-help (&optional tag
)
580 "Display description of tag TAG. If TAG is omitted, use the tag at point."
584 (if (eq (following-char) ?
<)
586 (setq tag
(sgml-beginning-of-tag))))
588 (error "No tag selected"))
589 (setq tag
(downcase tag
))
591 (or (cdr (assoc (downcase tag
) sgml-tag-help
))
592 (and (eq (aref tag
0) ?
/)
593 (cdr (assoc (downcase (substring tag
1)) sgml-tag-help
)))
594 "No description available")))
597 (defun sgml-maybe-end-tag ()
598 "Name self unless in position to end a tag."
600 (or (condition-case nil
601 (save-excursion (up-list -
1))
607 (save-excursion (up-list 1))
609 (error (self-insert-command 1)))))
612 (defun sgml-skip-tag-backward (arg)
613 "Skip to beginning of tag or matching opening tag if present.
614 With prefix argument ARG, repeat this ARG times."
617 (search-backward "<" nil t
)
618 (if (looking-at "</\\([^ \n\t>]+\\)")
619 ;; end tag, skip any nested pairs
620 (let ((case-fold-search t
)
621 (re (concat "</?" (regexp-quote (match-string 1)))))
622 (while (and (re-search-backward re nil t
)
623 (eq (char-after (1+ (point))) ?
/))
625 (sgml-skip-tag-backward 1))))
626 (setq arg
(1- arg
))))
628 (defun sgml-skip-tag-forward (arg &optional return
)
629 "Skip to end of tag or matching closing tag if present.
630 With prefix argument ARG, repeat this ARG times.
631 Return t iff after a closing tag."
635 (skip-chars-forward "^<>")
636 (if (eq (following-char) ?
>)
638 (if (looking-at "<\\([^/ \n\t>]+\\)")
639 ;; start tag, skip any nested same pairs _and_ closing tag
640 (let ((case-fold-search t
)
641 (re (concat "</?" (regexp-quote (match-string 1))))
645 (while (and (re-search-forward re nil t
)
647 (eq (char-after (1+ (match-beginning 0))) ?
/)))
649 (sgml-skip-tag-forward 1))
659 (defun sgml-delete-tag (arg)
660 "Delete tag on or after cursor, and matching closing or opening tag.
661 With prefix argument ARG, repeat this ARG times."
666 (if (looking-at "[ \t\n]*<")
668 (if (eq (char-after (match-end 0)) ?
/)
672 (goto-char (match-end 0))))
674 (or (save-excursion (setq close
(sgml-beginning-of-tag)
675 close
(and (stringp close
)
676 (eq (aref close
0) ?
/)
678 ;; not on closing tag
679 (let ((point (point)))
680 (sgml-skip-tag-backward 1)
681 (if (or (not (eq (following-char) ?
<))
685 (error "Not on or before tag")))))
688 (sgml-skip-tag-backward 1)
693 (sgml-skip-tag-forward 1)
696 (if (eq (aref (sgml-beginning-of-tag) 0) ?
/)
700 (setq arg
(1- arg
))))
702 ;; Put read-only last to enable setting this even when read-only enabled.
703 (or (get 'sgml-tag
'invisible
)
705 (append '(invisible t
707 point-entered sgml-point-entered
710 (symbol-plist 'sgml-tag
))))
712 (defun sgml-tags-invisible (arg)
713 "Toggle visibility of existing tags."
715 (let ((modified (buffer-modified-p))
716 (inhibit-read-only t
)
717 ;; This is needed in case font lock gets called,
718 ;; since it moves point and might call sgml-point-entered.
719 (inhibit-point-motion-hooks t
)
722 (goto-char (point-min))
723 (if (setq sgml-tags-invisible
725 (>= (prefix-numeric-value arg
) 0)
726 (not sgml-tags-invisible
)))
727 (while (re-search-forward "<\\([!/?A-Za-z][-A-Za-z0-9]*\\)"
729 (setq symbol
(intern-soft (downcase (match-string 1))))
730 (goto-char (match-beginning 0))
731 (and (get symbol
'before-string
)
732 (not (overlays-at (point)))
733 (overlay-put (make-overlay (point)
736 (put-text-property (point)
737 (progn (forward-list) (point))
738 'category
'sgml-tag
))
740 (while (< (setq pos
(next-overlay-change pos
)) (point-max))
741 (delete-overlay (car (overlays-at pos
)))))
742 (remove-text-properties (point-min) (point-max)
743 '(category sgml-tag intangible t
))))
744 (set-buffer-modified-p modified
)
745 (run-hooks 'sgml-tags-invisible-hook
)
748 (defun sgml-point-entered (x y
)
749 ;; Show preceding or following hidden tag, depending of cursor direction.
750 (let ((inhibit-point-motion-hooks t
))
752 (message "Invisible tag: %s"
756 (not (eq (following-char) ?
<)))
758 (eq (preceding-char) ?
>)))
762 (autoload 'compile-internal
"compile")
764 (defun sgml-validate (command)
765 "Validate an SGML document.
766 Runs COMMAND, a shell command, in a separate process asynchronously
767 with output going to the buffer `*compilation*'.
768 You can then use the command \\[next-error] to find the next error message
769 and move to the line in the SGML document that caused it."
771 (list (read-string "Validate command: "
772 (or sgml-saved-validate-command
773 (concat sgml-validate-command
775 (let ((name (buffer-file-name)))
777 (file-name-nondirectory name
))))))))
778 (setq sgml-saved-validate-command command
)
779 (save-some-buffers (not compilation-ask-about-save
) nil
)
780 (compile-internal command
"No more errors"))
783 (defun sgml-beginning-of-tag (&optional top-level
)
784 "Skip to beginning of tag and return its name.
785 If this can't be done, return t."
791 (if (search-backward "<" nil t
)
796 (if (looking-at "<[!?/]?[[A-Za-z][A-Za-z0-9]*")
797 (buffer-substring-no-properties
802 (defun sgml-value (alist)
803 "Interactively insert value taken from attributerule ALIST.
804 See `sgml-tag-alist' for info about attributerules.."
805 (setq alist
(cdr alist
))
806 (if (stringp (car alist
))
807 (insert "=\"" (car alist
) ?
\")
808 (if (eq (car alist
) t
)
812 (setq alist
(skeleton-read '(completing-read
813 "Value: " (cdr alist
))))
814 (if (string< "" alist
)
816 (delete-backward-char 2))))
819 (insert (skeleton-read '(completing-read "Value: " alist
))))
824 (defcustom html-mode-hook nil
825 "Hook run by command `html-mode'.
826 `text-mode-hook' and `sgml-mode-hook' are run first."
829 :options
'(html-autoview-mode))
831 (defvar html-quick-keys sgml-quick-keys
832 "Use C-c X combinations for quick insertion of frequent tags when non-nil.
833 This defaults to `sgml-quick-keys'.
834 This takes effect when first loading the library.")
836 (defvar html-mode-map
837 (let ((map (nconc (make-sparse-keymap) sgml-mode-map
))
838 (menu-map (make-sparse-keymap "HTML")))
839 (define-key map
"\C-c6" 'html-headline-6
)
840 (define-key map
"\C-c5" 'html-headline-5
)
841 (define-key map
"\C-c4" 'html-headline-4
)
842 (define-key map
"\C-c3" 'html-headline-3
)
843 (define-key map
"\C-c2" 'html-headline-2
)
844 (define-key map
"\C-c1" 'html-headline-1
)
845 (define-key map
"\C-c\r" 'html-paragraph
)
846 (define-key map
"\C-c\n" 'html-line
)
847 (define-key map
"\C-c\C-c-" 'html-horizontal-rule
)
848 (define-key map
"\C-c\C-co" 'html-ordered-list
)
849 (define-key map
"\C-c\C-cu" 'html-unordered-list
)
850 (define-key map
"\C-c\C-cr" 'html-radio-buttons
)
851 (define-key map
"\C-c\C-cc" 'html-checkboxes
)
852 (define-key map
"\C-c\C-cl" 'html-list-item
)
853 (define-key map
"\C-c\C-ch" 'html-href-anchor
)
854 (define-key map
"\C-c\C-cn" 'html-name-anchor
)
855 (define-key map
"\C-c\C-ci" 'html-image
)
858 (define-key map
"\C-c-" 'html-horizontal-rule
)
859 (define-key map
"\C-co" 'html-ordered-list
)
860 (define-key map
"\C-cu" 'html-unordered-list
)
861 (define-key map
"\C-cr" 'html-radio-buttons
)
862 (define-key map
"\C-cc" 'html-checkboxes
)
863 (define-key map
"\C-cl" 'html-list-item
)
864 (define-key map
"\C-ch" 'html-href-anchor
)
865 (define-key map
"\C-cn" 'html-name-anchor
)
866 (define-key map
"\C-ci" 'html-image
)))
867 (define-key map
"\C-c\C-s" 'html-autoview-mode
)
868 (define-key map
"\C-c\C-v" 'browse-url-of-buffer
)
869 (define-key map
[menu-bar html
] (cons "HTML" menu-map
))
870 (define-key menu-map
[html-autoview-mode
]
871 '("Toggle Autoviewing" . html-autoview-mode
))
872 (define-key menu-map
[browse-url-of-buffer
]
873 '("View Buffer Contents" . browse-url-of-buffer
))
874 (define-key menu-map
[nil] '("--"))
875 ;;(define-key menu-map "6" '("Heading 6" . html-headline-6))
876 ;;(define-key menu-map "5" '("Heading 5" . html-headline-5))
877 ;;(define-key menu-map "4" '("Heading 4" . html-headline-4))
878 (define-key menu-map "3" '("Heading 3" . html-headline-3))
879 (define-key menu-map "2" '("Heading 2" . html-headline-2))
880 (define-key menu-map "1" '("Heading 1" . html-headline-1))
881 (define-key menu-map "l" '("Radio Buttons" . html-radio-buttons))
882 (define-key menu-map "c" '("Checkboxes" . html-checkboxes))
883 (define-key menu-map "l" '("List Item" . html-list-item))
884 (define-key menu-map "u" '("Unordered List" . html-unordered-list))
885 (define-key menu-map "o" '("Ordered List" . html-ordered-list))
886 (define-key menu-map "-" '("Horizontal Rule" . html-horizontal-rule))
887 (define-key menu-map "\n" '("Line Break" . html-line))
888 (define-key menu-map "\r" '("Paragraph" . html-paragraph))
889 (define-key menu-map "i" '("Image" . html-image))
890 (define-key menu-map "h" '("Href Anchor" . html-href-anchor))
891 (define-key menu-map "n" '("Name Anchor" . html-name-anchor))
893 "Keymap for commands for use in HTML mode.")
896 (defvar html-face-tag-alist
901 "Value of `sgml-face-tag-alist' for HTML mode.")
903 (defvar html-tag-face-alist
906 ("blink" . highlight)
909 ("h1" bold underline)
910 ("h2" bold-italic underline)
911 ("h3" italic underline)
920 ("title" bold underline)
924 "Value of `sgml-tag-face-alist' for HTML mode.")
927 (defvar html-display-text
931 "Value of `sgml-display-text' for HTML mode.")
933 ;; should code exactly HTML 3 here when that is finished
934 (defvar html-tag-alist
935 (let* ((1-7 '(("1") ("2") ("3") ("4") ("5") ("6") ("7")))
936 (1-9 '(,@1-7 ("8") ("9")))
937 (align '(("align" ("left") ("center") ("right"))))
938 (valign '(("top") ("middle") ("bottom") ("baseline")))
939 (rel '(("next") ("previous") ("parent") ("subdocument") ("made")))
940 (href '("href" ("ftp:") ("file:") ("finger:") ("gopher:") ("http:")
941 ("mailto:") ("news:") ("rlogin:") ("telnet:") ("tn3270:")
942 ("wais:") ("/cgi-bin/")))
948 (list '((nil \n ( "List item: "
956 ;; put ,-expressions first, else byte-compile chokes (as of V19.29)
957 ;; and like this it's more efficient anyway
961 ("font" nil "size" ("-1") ("+1") ("-2") ("+2") ,@1-7)
962 ("form" (\n _ \n "<input type=\"submit\" value=\"\">")
963 ("action" ,@(cdr href)) ("method" ("get") ("post")))
970 ("hr" t ("size" ,@1-9) ("width") ("noshade" t) ,@align)
971 ("img" t ("align" ,@valign ("texttop") ("absmiddle") ("absbottom"))
972 ("src") ("alt") ("width" "1") ("height" "1")
973 ("border" "1") ("vspace" "1") ("hspace" "1") ("ismap" t))
974 ("input" t ("size" ,@1-9) ("maxlength" ,@1-9) ("checked" t) ,name
975 ("type" ("text") ("password") ("checkbox") ("radio")
976 ("submit") ("reset"))
980 ("ol" ,@list ("type" ("A") ("a") ("I") ("i") ("1")))
985 ,name ("size" ,@1-9) ("multiple" t))
987 ((completing-read "Cell kind: " '(("td") ("th"))
989 "<tr><" str ?> _ \n))
990 ("border" t ,@1-9) ("width" "10") ("cellpadding"))
992 ("textarea" ,name ("rows" ,@1-9) ("cols" ,@1-9))
994 ("ul" ,@list ("type" ("disc") ("circle") ("square")))
1002 ("Item: " "<item>" str \n))
1009 ("body" \n ("background" ".gif") ("bgcolor" "#") ("text" "#")
1010 ("link" "#") ("alink" "#") ("vlink" "#"))
1011 ("box" (nil _ "<over>" _))
1012 ("br" t ("clear" ("left") ("right")))
1013 ("caption" ("valign" ("top") ("bottom")))
1022 "<dt>" str "<dd>" _ \n)))
1025 ;("fn" "id" "fn") ; ???
1029 "<title>" (setq str (read-input "Title: ")) "</title>\n"
1031 "<body>\n<h1>" str "</h1>\n" _
1032 "\n<address>\n<a href=\"mailto:"
1034 "\">" (user-full-name) "</a>\n</address>\n"
1039 ("isindex" t ("action") ("prompt"))
1045 ("option" t ("value") ("label") ("selected" t))
1063 "*Value of `sgml-tag-alist' for HTML mode.")
1065 (defvar html-tag-help
1067 ("a" . "Anchor of point or link elsewhere")
1070 ("address" . "Formatted mail address")
1071 ("array" . "Math array")
1074 ("base" . "Base address for URLs")
1075 ("big" . "Font size")
1076 ("blink" . "Blinking text")
1077 ("blockquote" . "Indented quotation")
1078 ("body" . "Document body")
1079 ("box" . "Math fraction")
1080 ("br" . "Line break")
1081 ("caption" . "Table caption")
1082 ("center" . "Centered text")
1083 ("changed" . "Change bars")
1084 ("cite" . "Citation of a document")
1085 ("code" . "Formatted source code")
1086 ("dd" . "Definition of term")
1089 ("dir" . "Directory list (obsolete)")
1090 ("dl" . "Definition list")
1091 ("dt" . "Term to be definined")
1092 ("em" . "Emphasised")
1093 ("embed" . "Embedded data in foreign format")
1095 ("figa" . "Figure anchor")
1096 ("figd" . "Figure description")
1097 ("figt" . "Figure text")
1099 ("font" . "Font size")
1100 ("form" . "Form with input fields")
1101 ("group" . "Document grouping")
1102 ("h1" . "Most important section headline")
1103 ("h2" . "Important section headline")
1104 ("h3" . "Section headline")
1105 ("h4" . "Minor section headline")
1106 ("h5" . "Unimportant section headline")
1107 ("h6" . "Least important section headline")
1108 ("head" . "Document header")
1109 ("hr" . "Horizontal rule")
1110 ("html" . "HTML Document")
1111 ("i" . "Italic face")
1112 ("img" . "Graphic image")
1113 ("input" . "Form input field")
1115 ("isindex" . "Input field for index search")
1116 ("kbd" . "Keybard example face")
1117 ("lang" . "Natural language")
1118 ("li" . "List item")
1119 ("link" . "Link relationship")
1120 ("math" . "Math formula")
1121 ("menu" . "Menu list (obsolete)")
1122 ("mh" . "Form mail header")
1123 ("nextid" . "Allocate new id")
1124 ("nobr" . "Text without line break")
1125 ("ol" . "Ordered list")
1126 ("option" . "Selection list item")
1127 ("over" . "Math fraction rule")
1128 ("p" . "Paragraph start")
1129 ("panel" . "Floating panel")
1131 ("pre" . "Preformatted fixed width text")
1133 ("rev" . "Reverse video")
1135 ("samp" . "Sample text")
1136 ("select" . "Selection list")
1137 ("small" . "Font size")
1138 ("sp" . "Nobreak space")
1139 ("strong" . "Standout text")
1140 ("sub" . "Subscript")
1141 ("sup" . "Superscript")
1142 ("table" . "Table with rows and columns")
1143 ("tb" . "Table vertical break")
1144 ("td" . "Table data cell")
1145 ("textarea" . "Form multiline edit area")
1146 ("th" . "Table header cell")
1147 ("title" . "Document title")
1148 ("tr" . "Table row separator")
1149 ("tt" . "Typewriter face")
1150 ("u" . "Underlined text")
1151 ("ul" . "Unordered list")
1152 ("var" . "Math variable face")
1153 ("wbr" . "Enable <br> within <nobr>"))
1154 "*Value of `sgml-tag-help' for HTML mode.")
1158 "Major mode based on SGML mode for editing HTML documents.
1159 This allows inserting skeleton constructs used in hypertext documents with
1160 completion. See below for an introduction to HTML. Use
1161 \\[browse-url-of-buffer] to see how this comes out. See also `sgml-mode' on
1162 which this is based.
1164 Do \\[describe-variable] html- SPC and \\[describe-variable] sgml- SPC to see available variables.
1166 To write fairly well formatted pages you only need to know few things. Most
1167 browsers have a function to read the source code of the page being seen, so
1168 you can imitate various tricks. Here's a very short HTML primer which you
1169 can also view with a browser to see what happens:
1171 <title>A Title Describing Contents</title> should be on every page. Pages can
1172 have <h1>Very Major Headlines</h1> through <h6>Very Minor Headlines</h6>
1173 <hr> Parts can be separated with horizontal rules.
1175 <p>Paragraphs only need an opening tag. Line breaks and multiple spaces are
1176 ignored unless the text is <pre>preformatted.</pre> Text can be marked as
1177 <b>bold</b>, <i>italic</i> or <u>underlined</u> using the normal M-g or
1178 Edit/Text Properties/Face commands.
1180 Pages can have <a name=\"SOMENAME\">named points</a> and can link other points
1181 to them with <a href=\"#SOMENAME\">see also somename</a>. In the same way <a
1182 href=\"URL\">see also URL</a> where URL is a filename relative to current
1183 directory, or absolute as in `http://www.cs.indiana.edu/elisp/w3/docs.html'.
1185 Images in many formats can be inlined with <img src=\"URL\">.
1187 If you mainly create your own documents, `sgml-specials' might be
1188 interesting. But note that some HTML 2 browsers can't handle `''.
1189 To work around that, do:
1190 (eval-after-load \"sgml-mode\" '(aset sgml-char-names ?' nil))
1194 (kill-all-local-variables)
1195 (setq mode-name "HTML"
1196 major-mode 'html-mode)
1197 (sgml-mode-common html-tag-face-alist html-display-text)
1198 (use-local-map html-mode-map)
1199 (make-local-variable 'sgml-tag-alist)
1200 (make-local-variable 'sgml-face-tag-alist)
1201 (make-local-variable 'sgml-tag-help)
1202 (make-local-variable 'outline-regexp)
1203 (make-local-variable 'outline-heading-end-regexp)
1204 (make-local-variable 'outline-level)
1205 (make-local-variable 'sentence-end)
1207 (if sentence-end-double-space
1208 "[.?!][]\"')}]*\\(<[^>]*>\\)*\\($\\| $\\|\t\\| \\)[ \t\n]*"
1210 "[.?!][]\"')}]*\\(<[^>]*>\\)*\\($\\| \\|\t\\)[ \t\n]*"))
1211 (setq sgml-tag-alist html-tag-alist
1212 sgml-face-tag-alist html-face-tag-alist
1213 sgml-tag-help html-tag-help
1214 outline-regexp "^.*<[Hh][1-6]\\>"
1215 outline-heading-end-regexp "</[Hh][1-6]>"
1216 outline-level (lambda ()
1217 (char-after (1- (match-end 0)))))
1218 (setq imenu-create-index-function 'html-imenu-index)
1219 (make-local-variable 'imenu-sort-function)
1220 (setq imenu-sort-function nil) ; sorting the menu defeats the purpose
1221 (run-hooks 'text-mode-hook 'sgml-mode-hook 'html-mode-hook))
1223 (defvar html-imenu-regexp
1224 "\\s-*<h\\([1-9]\\)[^\n<>]*>\\(<[^\n<>]*>\\)*\\s-*\\([^\n<>]*\\)"
1225 "*A regular expression matching a head line to be added to the menu.
1226 The first `match-string' should be a number from 1-9.
1227 The second `match-string' matches extra tags and is ignored.
1228 The third `match-string' will be the used in the menu.")
1230 (defun html-imenu-index ()
1231 "Return an table of contents for an HTML buffer for use with Imenu."
1234 (goto-char (point-min))
1235 (while (re-search-forward html-imenu-regexp nil t)
1237 (cons (cons (concat (make-string
1238 (* 2 (1- (string-to-number (match-string 1))))
1241 (save-excursion (beginning-of-line) (point)))
1243 (nreverse toc-index)))
1245 (defun html-autoview-mode (&optional arg)
1246 "Toggle automatic viewing via `browse-url-of-buffer' upon saving buffer.
1247 With positive prefix ARG always turns viewing on, with negative ARG always off.
1248 Can be used as a value for `html-mode-hook'."
1250 (if (setq arg (if arg
1251 (< (prefix-numeric-value arg) 0)
1252 (and (boundp 'after-save-hook)
1253 (memq 'browse-url-of-buffer after-save-hook))))
1254 (setq after-save-hook (delq 'browse-url-of-buffer after-save-hook))
1255 (add-hook 'after-save-hook 'browse-url-of-buffer nil t))
1256 (message "Autoviewing turned %s."
1257 (if arg "off" "on")))
1259 (define-skeleton html-href-anchor
1260 "HTML anchor tag with href attribute."
1262 '(setq input "http:")
1263 "<a href=\"" str "\">" _ "</a>")
1265 (define-skeleton html-name-anchor
1266 "HTML anchor tag with name attribute."
1268 "<a name=\"" str "\">" _ "</a>")
1270 (define-skeleton html-headline-1
1271 "HTML level 1 headline tags."
1275 (define-skeleton html-headline-2
1276 "HTML level 2 headline tags."
1280 (define-skeleton html-headline-3
1281 "HTML level 3 headline tags."
1285 (define-skeleton html-headline-4
1286 "HTML level 4 headline tags."
1290 (define-skeleton html-headline-5
1291 "HTML level 5 headline tags."
1295 (define-skeleton html-headline-6
1296 "HTML level 6 headline tags."
1300 (define-skeleton html-horizontal-rule
1301 "HTML horizontal rule tag."
1305 (define-skeleton html-image
1308 "<img src=\"" _ "\">")
1310 (define-skeleton html-line
1311 "HTML line break tag."
1315 (define-skeleton html-ordered-list
1316 "HTML ordered list tags."
1322 (define-skeleton html-unordered-list
1323 "HTML unordered list tags."
1329 (define-skeleton html-list-item
1330 "HTML list item tag."
1335 (define-skeleton html-paragraph
1336 "HTML paragraph tag."
1341 (define-skeleton html-checkboxes
1342 "Group of connected checkbox inputs."
1347 "<input type=\"" (identity "checkbox") ; see comment above about identity
1348 "\" name=\"" (or v1 (setq v1 (skeleton-read "Name: ")))
1349 "\" value=\"" str ?\"
1350 (if (y-or-n-p "Set \"checked\" attribute? ")
1351 (funcall skeleton-transformation " checked")) ">"
1352 (skeleton-read "Text: " (capitalize str))
1353 (or v2 (setq v2 (if (y-or-n-p "Newline after text? ")
1354 (funcall skeleton-transformation "<br>")
1358 (define-skeleton html-radio-buttons
1359 "Group of connected radio button inputs."
1364 "<input type=\"" (identity "radio") ; see comment above about identity
1365 "\" name=\"" (or (car v2) (setcar v2 (skeleton-read "Name: ")))
1366 "\" value=\"" str ?\"
1367 (if (and (not v1) (setq v1 (y-or-n-p "Set \"checked\" attribute? ")))
1368 (funcall skeleton-transformation " checked") ">")
1369 (skeleton-read "Text: " (capitalize str))
1370 (or (cdr v2) (setcdr v2 (if (y-or-n-p "Newline after text? ")
1371 (funcall skeleton-transformation "<br>")
1375 ;;; sgml-mode.el ends here