sgml-mode.el 62.7 KB
Newer Older
1
;;; sgml-mode.el --- SGML- and HTML-editing modes
Eric S. Raymond's avatar
Eric S. Raymond committed
2

Stefan Monnier's avatar
Stefan Monnier committed
3
;; Copyright (C) 1992,95,96,98,2001,2002  Free Software Foundation, Inc.
Eric S. Raymond's avatar
Eric S. Raymond committed
4

5
;; Author: James Clark <jjc@jclark.com>
Stefan Monnier's avatar
Stefan Monnier committed
6
;; Maintainer: FSF
Karl Heuer's avatar
Karl Heuer committed
7
;; Adapted-By: ESR, Daniel Pfeiffer <occitan@esperanto.org>,
8
;;             F.Potorti@cnuce.cnr.it
9
;; Keywords: wp, hypermedia, comm, languages
Eric S. Raymond's avatar
Eric S. Raymond committed
10 11 12 13 14

;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
Karl Heuer's avatar
Karl Heuer committed
15
;; the Free Software Foundation; either version 2, or (at your option)
Eric S. Raymond's avatar
Eric S. Raymond committed
16 17 18 19 20 21 22 23
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
Erik Naggum's avatar
Erik Naggum committed
24 25 26
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
Eric S. Raymond's avatar
Eric S. Raymond committed
27 28 29

;;; Commentary:

30 31 32
;; Configurable major mode for editing document in the SGML standard general
;; markup language.  As an example contains a mode for editing the derived
;; HTML hypertext markup language.
Eric S. Raymond's avatar
Eric S. Raymond committed
33 34 35

;;; Code:

Dave Love's avatar
Dave Love committed
36 37
(eval-when-compile
  (require 'skeleton)
Mike Williams's avatar
Mike Williams committed
38 39
  (require 'outline)
  (require 'cl))
Dave Love's avatar
Dave Love committed
40

41 42 43 44
(defgroup sgml nil
  "SGML editing mode"
  :group 'languages)

Stefan Monnier's avatar
Stefan Monnier committed
45 46 47 48 49
(defcustom sgml-basic-offset 2
  "*Specifies the basic indentation level for `sgml-indent-line'."
  :type 'integer
  :group 'sgml)

50
(defcustom sgml-transformation 'identity
51 52
  "*Default value for `skeleton-transformation' (which see) in SGML mode."
  :type 'function
53
  :group 'sgml)
54 55 56 57

(put 'sgml-transformation 'variable-interactive
     "aTransformation function: ")

Dave Love's avatar
Dave Love committed
58 59 60 61 62 63
(defcustom sgml-mode-hook nil
  "Hook run by command `sgml-mode'.
`text-mode-hook' is run first."
  :group 'sgml
  :type 'hook)

64 65 66
;; As long as Emacs' syntax can't be complemented with predicates to context
;; sensitively confirm the syntax of characters, we have to live with this
;; kludgy kind of tradeoff.
67
(defvar sgml-specials '(?\")
Richard M. Stallman's avatar
Richard M. Stallman committed
68
  "List of characters that have a special meaning for SGML mode.
69
This list is used when first loading the `sgml-mode' library.
70 71 72 73 74 75
The supported characters and potential disadvantages are:

  ?\\\"	Makes \" in text start a string.
  ?'	Makes ' in text start a string.
  ?-	Makes -- in text start a comment.

76
When only one of ?\\\" or ?' are included, \"'\" or '\"', as can be found in
77
DTDs, start a string.  To partially avoid this problem this also makes these
78 79 80 81
self insert as named entities depending on `sgml-quick-keys'.

Including ?- has the problem of affecting dashes that have nothing to do
with comments, so we normally turn it off.")
82 83

(defvar sgml-quick-keys nil
84
  "Use <, >, &, /, SPC and `sgml-specials' keys \"electrically\" when non-nil.
85
This takes effect when first loading the `sgml-mode' library.")
86 87

(defvar sgml-mode-map
88
  (let ((map (make-keymap))	;`sparse' doesn't allow binding to charsets.
89 90 91
	(menu-map (make-sparse-keymap "SGML")))
    (define-key map "\C-c\C-i" 'sgml-tags-invisible)
    (define-key map "/" 'sgml-slash)
92 93
    (define-key map "\C-c\C-n" 'sgml-name-char)
    (define-key map "\C-c\C-t" 'sgml-tag)
94 95 96 97 98 99 100 101
    (define-key map "\C-c\C-a" 'sgml-attributes)
    (define-key map "\C-c\C-b" 'sgml-skip-tag-backward)
    (define-key map [?\C-c left] 'sgml-skip-tag-backward)
    (define-key map "\C-c\C-f" 'sgml-skip-tag-forward)
    (define-key map [?\C-c right] 'sgml-skip-tag-forward)
    (define-key map "\C-c\C-d" 'sgml-delete-tag)
    (define-key map "\C-c\^?" 'sgml-delete-tag)
    (define-key map "\C-c?" 'sgml-tag-help)
Mike Williams's avatar
Mike Williams committed
102
    (define-key map "\C-c/" 'sgml-close-tag)
103 104
    (define-key map "\C-c8" 'sgml-name-8bit-mode)
    (define-key map "\C-c\C-v" 'sgml-validate)
105 106 107 108 109 110 111 112 113
    (when sgml-quick-keys
      (define-key map "&" 'sgml-name-char)
      (define-key map "<" 'sgml-tag)
      (define-key map " " 'sgml-auto-attributes)
      (define-key map ">" 'sgml-maybe-end-tag)
      (when (memq ?\" sgml-specials)
        (define-key map "\"" 'sgml-name-self))
      (when (memq ?' sgml-specials)
        (define-key map "'" 'sgml-name-self)))
Stefan Monnier's avatar
Stefan Monnier committed
114 115
    (define-key map (vector (make-char 'latin-iso8859-1))
      'sgml-maybe-name-self)
116 117 118 119
    (let ((c 127)
	  (map (nth 1 map)))
      (while (< (setq c (1+ c)) 256)
	(aset map c 'sgml-maybe-name-self)))
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    (define-key map [menu-bar sgml] (cons "SGML" menu-map))
    (define-key menu-map [sgml-validate] '("Validate" . sgml-validate))
    (define-key menu-map [sgml-name-8bit-mode]
      '("Toggle 8 Bit Insertion" . sgml-name-8bit-mode))
    (define-key menu-map [sgml-tags-invisible]
      '("Toggle Tag Visibility" . sgml-tags-invisible))
    (define-key menu-map [sgml-tag-help]
      '("Describe Tag" . sgml-tag-help))
    (define-key menu-map [sgml-delete-tag]
      '("Delete Tag" . sgml-delete-tag))
    (define-key menu-map [sgml-skip-tag-forward]
      '("Forward Tag" . sgml-skip-tag-forward))
    (define-key menu-map [sgml-skip-tag-backward]
      '("Backward Tag" . sgml-skip-tag-backward))
    (define-key menu-map [sgml-attributes]
      '("Insert Attributes" . sgml-attributes))
    (define-key menu-map [sgml-tag] '("Insert Tag" . sgml-tag))
    map)
  "Keymap for SGML mode.  See also `sgml-specials'.")

140 141
(defun sgml-make-syntax-table (specials)
  (let ((table (make-syntax-table text-mode-syntax-table)))
142 143
    (modify-syntax-entry ?< "(>" table)
    (modify-syntax-entry ?> ")<" table)
144 145 146 147
    (modify-syntax-entry ?: "_" table)
    (modify-syntax-entry ?_ "_" table)
    (modify-syntax-entry ?. "_" table)
    (if (memq ?- specials)
148
	(modify-syntax-entry ?- "_ 1234" table))
149
    (if (memq ?\" specials)
150
	(modify-syntax-entry ?\" "\"\"" table))
151
    (if (memq ?' specials)
152
	(modify-syntax-entry ?\' "\"'" table))
153 154 155
    table))

(defvar sgml-mode-syntax-table (sgml-make-syntax-table sgml-specials)
156 157
  "Syntax table used in SGML mode.  See also `sgml-specials'.")

158 159 160 161 162 163 164
(defconst sgml-tag-syntax-table
  (let ((table (sgml-make-syntax-table '(?- ?\" ?\'))))
    (dolist (char '(?\( ?\) ?\{ ?\} ?\[ ?\] ?$ ?% ?& ?* ?+ ?/))
      (modify-syntax-entry char "." table))
    table)
  "Syntax table used to parse SGML tags.")

165
(defcustom sgml-name-8bit-mode nil
166
  "*When non-nil, insert non-ASCII characters as named entities."
167 168
  :type 'boolean
  :group 'sgml)
Eric S. Raymond's avatar
Eric S. Raymond committed
169

170 171 172 173 174
(defvar sgml-char-names
  [nil nil nil nil nil nil nil nil
   nil nil nil nil nil nil nil nil
   nil nil nil nil nil nil nil nil
   nil nil nil nil nil nil nil nil
175
   "nbsp" "excl" "quot" "num" "dollar" "percnt" "amp" "apos"
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
   "lpar" "rpar" "ast" "plus" "comma" "hyphen" "period" "sol"
   nil nil nil nil nil nil nil nil
   nil nil "colon" "semi" "lt" "eq" "gt" "quest"
   "commat" nil nil nil nil nil nil nil
   nil nil nil nil nil nil nil nil
   nil nil nil nil nil nil nil nil
   nil nil nil "lsqb" nil "rsqb" "uarr" "lowbar"
   "lsquo" nil nil nil nil nil nil nil
   nil nil nil nil nil nil nil nil
   nil nil nil nil nil nil nil nil
   nil nil nil "lcub" "verbar" "rcub" "tilde" nil
   nil nil nil nil nil nil nil nil
   nil nil nil nil nil nil nil nil
   nil nil nil nil nil nil nil nil
   nil nil nil nil nil nil nil nil
   "nbsp" "iexcl" "cent" "pound" "curren" "yen" "brvbar" "sect"
   "uml" "copy" "ordf" "laquo" "not" "shy" "reg" "macr"
   "ring" "plusmn" "sup2" "sup3" "acute" "micro" "para" "middot"
194
   "cedil" "sup1" "ordm" "raquo" "frac14" "frac12" "frac34" "iquest"
195 196 197 198 199 200 201 202 203 204
   "Agrave" "Aacute" "Acirc" "Atilde" "Auml" "Aring" "AElig" "Ccedil"
   "Egrave" "Eacute" "Ecirc" "Euml" "Igrave" "Iacute" "Icirc" "Iuml"
   "ETH" "Ntilde" "Ograve" "Oacute" "Ocirc" "Otilde" "Ouml" nil
   "Oslash" "Ugrave" "Uacute" "Ucirc" "Uuml" "Yacute" "THORN" "szlig"
   "agrave" "aacute" "acirc" "atilde" "auml" "aring" "aelig" "ccedil"
   "egrave" "eacute" "ecirc" "euml" "igrave" "iacute" "icirc" "iuml"
   "eth" "ntilde" "ograve" "oacute" "ocirc" "otilde" "ouml" "divide"
   "oslash" "ugrave" "uacute" "ucirc" "uuml" "yacute" "thorn" "yuml"]
  "Vector of symbolic character names without `&' and `;'.")

205 206 207 208 209 210 211 212 213 214 215 216 217 218
(put 'sgml-table 'char-table-extra-slots 0)

(defvar sgml-char-names-table
  (let ((table (make-char-table 'sgml-table))
	(i 32)
	elt)
    (while (< i 256)
      (setq elt (aref sgml-char-names i))
      (if elt (aset table (make-char 'latin-iso8859-1 i) elt))
      (setq i (1+ i)))
    table)
  "A table for mapping non-ASCII characters into SGML entity names.
Currently, only Latin-1 characters are supported.")

219 220
;; nsgmls is a free SGML parser in the SP suite available from
;; ftp.jclark.com and otherwise packaged for GNU systems.
221 222 223
;; Its error messages can be parsed by next-error.
;; The -s option suppresses output.

224
(defcustom sgml-validate-command "nsgmls -s" ; replaced old `sgmls'
Eric S. Raymond's avatar
Eric S. Raymond committed
225 226
  "*The command to validate an SGML document.
The file name of current buffer file name will be appended to this,
227 228
separated by a space."
  :type 'string
Dave Love's avatar
Dave Love committed
229
  :version "21.1"
230
  :group 'sgml)
Eric S. Raymond's avatar
Eric S. Raymond committed
231 232 233 234

(defvar sgml-saved-validate-command nil
  "The command last used to validate in this buffer.")

235 236
;; I doubt that null end tags are used much for large elements,
;; so use a small distance here.
237
(defcustom sgml-slash-distance 1000
Richard M. Stallman's avatar
Richard M. Stallman committed
238
  "*If non-nil, is the maximum distance to search for matching `/'."
239 240
  :type '(choice (const nil) integer)
  :group 'sgml)
Eric S. Raymond's avatar
Eric S. Raymond committed
241

Stefan Monnier's avatar
Stefan Monnier committed
242 243 244 245
(defconst sgml-name-re "[_:[:alpha:]][-_.:[:alnum:]]*")
(defconst sgml-tag-name-re (concat "<\\([!/?]?" sgml-name-re "\\)"))
(defconst sgml-attrs-re "\\(?:[^\"'/><]\\|\"[^\"]*\"\\|'[^']*'\\)*")
(defconst sgml-start-tag-regex (concat "<" sgml-name-re sgml-attrs-re)
246
  "Regular expression that matches a non-empty start tag.
Richard M. Stallman's avatar
Richard M. Stallman committed
247
Any terminating `>' or `/' is not matched.")
248 249


250 251
;; internal
(defconst sgml-font-lock-keywords-1
Stefan Monnier's avatar
Stefan Monnier committed
252 253
  `((,(concat "<\\([!?]" sgml-name-re "\\)") 1 font-lock-keyword-face)
    (,(concat "<\\(/?" sgml-name-re"\\)") 1 font-lock-function-name-face)
254
    ;; FIXME: this doesn't cover the variables using a default value.
Stefan Monnier's avatar
Stefan Monnier committed
255 256
    (,(concat "\\(" sgml-name-re "\\)=[\"']") 1 font-lock-variable-name-face)
    (,(concat "[&%]" sgml-name-re ";?") . font-lock-variable-name-face)))
257 258 259 260 261 262 263 264 265 266

(defconst sgml-font-lock-keywords-2
  (append
   sgml-font-lock-keywords-1
   '((eval
      . (cons (concat "<"
		      (regexp-opt (mapcar 'car sgml-tag-face-alist) t)
		      "\\([ \t][^>]*\\)?>\\([^<]+\\)</\\1>")
	      '(3 (cdr (assoc (downcase (match-string 1))
			      sgml-tag-face-alist))))))))
267 268 269 270

;; for font-lock, but must be defvar'ed after
;; sgml-font-lock-keywords-1 and sgml-font-lock-keywords-2 above
(defvar sgml-font-lock-keywords sgml-font-lock-keywords-1
271 272
  "*Rules for highlighting SGML code.  See also `sgml-tag-face-alist'.")

273 274 275 276 277 278 279 280
(defvar sgml-font-lock-syntactic-keywords
  ;; Use the `b' style of comments to avoid interference with the -- ... --
  ;; comments recognized when `sgml-specials' includes ?-.
  ;; FIXME: beware of <!--> blabla <!--> !!
  '(("\\(<\\)!--" (1 "< b"))
    ("--[ \t\n]*\\(>\\)" (1 "> b")))
  "Syntactic keywords for `sgml-mode'.")

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
;; internal
(defvar sgml-face-tag-alist ()
  "Alist of face and tag name for facemenu.")

(defvar sgml-tag-face-alist ()
  "Tag names and face or list of faces to fontify with when invisible.
When `font-lock-maximum-decoration' is 1 this is always used for fontifying.
When more these are fontified together with `sgml-font-lock-keywords'.")

(defvar sgml-display-text ()
  "Tag names as lowercase symbols, and display string when invisible.")

;; internal
(defvar sgml-tags-invisible nil)

296
(defcustom sgml-tag-alist
297 298
  '(("![" ("ignore" t) ("include" t))
    ("!attlist")
299 300 301 302 303 304 305 306 307
    ("!doctype")
    ("!element")
    ("!entity"))
  "*Alist of tag names for completing read and insertion rules.
This alist is made up as

  ((\"tag\" . TAGRULE)
   ...)

Mike Williams's avatar
Mike Williams committed
308 309
TAGRULE is a list of optionally t (no endtag) or `\\n' (separate endtag by
newlines) or a skeleton with nil, t or `\\n' in place of the interactor
310 311 312 313 314 315 316 317
followed by an ATTRIBUTERULE (for an always present attribute) or an
attribute alist.

The attribute alist is made up as

  ((\"attribute\" . ATTRIBUTERULE)
   ...)

Mike Williams's avatar
Mike Williams committed
318
ATTRIBUTERULE is a list of optionally t (no value when no input) followed by
319 320 321 322
an optional alist of possible values."
  :type '(repeat (cons (string :tag "Tag Name")
		       (repeat :tag "Tag Rule" sexp)))
  :group 'sgml)
323

324
(defcustom sgml-tag-help
325 326 327 328 329 330
  '(("!" . "Empty declaration for comment")
    ("![" . "Embed declarations with parser directive")
    ("!attlist" . "Tag attributes declaration")
    ("!doctype" . "Document type (DTD) declaration")
    ("!element" . "Tag declaration")
    ("!entity" . "Entity (macro) declaration"))
331 332 333 334
  "*Alist of tag name and short description."
  :type '(repeat (cons (string :tag "Tag Name")
		       (string :tag "Description")))
  :group 'sgml)
335

336
(defcustom sgml-xml-mode nil
337 338 339 340 341 342 343 344
  "*When non-nil, tag insertion functions will be XML-compliant.
If this variable is customized, the custom value is used always.
Otherwise, it is set to be buffer-local when the file has
 a DOCTYPE or an XML declaration."
  :type 'boolean
  :version "21.2"
  :group 'sgml)

Stefan Monnier's avatar
Stefan Monnier committed
345 346 347
(defvar sgml-empty-tags nil
  "List of tags whose !ELEMENT definition says EMPTY.")

Stefan Monnier's avatar
Stefan Monnier committed
348 349 350
(defvar sgml-unclosed-tags nil
  "List of tags whose !ELEMENT definition says the end-tag is optional.")

351 352 353 354
(defun sgml-xml-guess ()
  "Guess whether the current buffer is XML."
  (save-excursion
    (goto-char (point-min))
355 356 357 358 359 360 361 362 363 364 365
    (when (or (string= "xml" (file-name-extension (or buffer-file-name "")))
	      (looking-at "\\s-*<\\?xml")
	      (when (re-search-forward
		     (eval-when-compile
		       (mapconcat 'identity
				  '("<!DOCTYPE" "\\(\\w+\\)" "\\(\\w+\\)"
				    "\"\\([^\"]+\\)\"" "\"\\([^\"]+\\)\"")
				  "\\s-+"))
		     nil t)
		(string-match "X\\(HT\\)?ML" (match-string 3))))
      (set (make-local-variable 'sgml-xml-mode) t))))
366

Dave Love's avatar
Dave Love committed
367 368
(defvar v2)				; free for skeleton

369 370 371 372 373 374 375
(defun sgml-comment-indent-new-line (&optional soft)
  (let ((comment-start "-- ")
	(comment-start-skip "\\(<!\\)?--[ \t]*")
	(comment-end " --")
	(comment-style 'plain))
    (comment-indent-new-line soft)))

376 377 378 379 380 381 382 383 384 385 386 387
(defun sgml-mode-facemenu-add-face-function (face end)
  (if (setq face (cdr (assq face sgml-face-tag-alist)))
      (progn
	(setq face (funcall skeleton-transformation face))
	(setq facemenu-end-add-face (concat "</" face ">"))
	(concat "<" face ">"))
    (error "Face not configured for %s mode" mode-name)))

;;;###autoload
(define-derived-mode sgml-mode text-mode "SGML"
  "Major mode for editing SGML documents.
Makes > match <.
388
Keys <, &, SPC within <>, \", / and ' can be electric depending on
389 390 391 392 393 394 395 396 397 398 399 400 401 402
`sgml-quick-keys'.

An argument of N to a tag-inserting command means to wrap it around
the next N words.  In Transient Mark mode, when the mark is active,
N defaults to -1, which means to wrap it around the current region.

If you like upcased tags, put (setq sgml-transformation 'upcase) in
your `.emacs' file.

Use \\[sgml-validate] to validate your document with an SGML parser.

Do \\[describe-variable] sgml- SPC to see available variables.
Do \\[describe-key] on the following bindings to discover what they do.
\\{sgml-mode-map}"
Eric S. Raymond's avatar
Eric S. Raymond committed
403
  (make-local-variable 'sgml-saved-validate-command)
404 405
  (make-local-variable 'facemenu-end-add-face)
  ;;(make-local-variable 'facemenu-remove-face-function)
406 407 408
  ;; A start or end tag by itself on a line separates a paragraph.
  ;; This is desirable because SGML discards a newline that appears
  ;; immediately after a start tag or immediately before an end tag.
Stefan Monnier's avatar
Stefan Monnier committed
409 410 411 412
  (set (make-local-variable 'paragraph-start) (concat "[ \t]*$\\|\
\[ \t]*</?\\(" sgml-name-re sgml-attrs-re "\\)?>"))
  (set (make-local-variable 'paragraph-separate)
       (concat paragraph-start "$"))
413
  (set (make-local-variable 'adaptive-fill-regexp) "[ \t]*")
414
  (set (make-local-variable 'indent-line-function) 'sgml-indent-line)
415 416 417
  (set (make-local-variable 'comment-start) "<!-- ")
  (set (make-local-variable 'comment-end) " -->")
  (set (make-local-variable 'comment-indent-function) 'sgml-comment-indent)
418 419
  (set (make-local-variable 'comment-line-break-function)
       'sgml-comment-indent-new-line)
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
  (set (make-local-variable 'skeleton-further-elements)
       '((completion-ignore-case t)))
  (set (make-local-variable 'skeleton-end-hook)
       (lambda ()
         (or (eolp)
             (not (or (eq v2 '\n) (eq (car-safe v2) '\n)))
             (newline-and-indent))))
  (set (make-local-variable 'font-lock-defaults)
       '((sgml-font-lock-keywords
          sgml-font-lock-keywords-1
          sgml-font-lock-keywords-2)
         nil t nil nil
         (font-lock-syntactic-keywords
          . sgml-font-lock-syntactic-keywords)))
  (set (make-local-variable 'facemenu-add-face-function)
       'sgml-mode-facemenu-add-face-function)
436 437 438 439
  (sgml-xml-guess)
  (if sgml-xml-mode
      (setq mode-name "XML")
    (set (make-local-variable 'skeleton-transformation) sgml-transformation))
440 441 442
  ;; This will allow existing comments within declarations to be
  ;; recognized.
  (set (make-local-variable 'comment-start-skip) "\\(?:<!\\)?--[ \t]*")
443 444
  (set (make-local-variable 'comment-end-skip) "[ \t]*--\\([ \t\n]*>\\)?")
  ;; This definition probably is not useful in derived modes.
445
  (set (make-local-variable 'imenu-generic-expression)
Stefan Monnier's avatar
Stefan Monnier committed
446 447
       (concat "<!\\(element\\|entity\\)[ \t\n]+%?[ \t\n]*\\("
	       sgml-name-re "\\)")))
448

Eric S. Raymond's avatar
Eric S. Raymond committed
449
(defun sgml-comment-indent ()
450
  (if (looking-at "--") comment-column 0))
Eric S. Raymond's avatar
Eric S. Raymond committed
451 452

(defun sgml-slash (arg)
453 454 455 456 457 458 459 460 461 462 463
  "Insert ARG slash characters.
Behaves electrically if `sgml-quick-keys' is non-nil."
  (interactive "p")
  (cond
   ((not (and (eq (char-before) ?<) (= arg 1)))
    (sgml-slash-matching arg))
   ((eq sgml-quick-keys 'indent)
    (insert-char ?/ 1)
    (indent-according-to-mode))
   ((eq sgml-quick-keys 'close)
    (delete-backward-char 1)
Mike Williams's avatar
Mike Williams committed
464
    (sgml-close-tag))
465 466 467 468
   (t
    (sgml-slash-matching arg))))

(defun sgml-slash-matching (arg)
Richard M. Stallman's avatar
Richard M. Stallman committed
469 470 471
  "Insert `/' and display any previous matching `/'.
Two `/'s are treated as matching if the first `/' ends a net-enabling
start tag, and the second `/' is the corresponding null end tag."
Eric S. Raymond's avatar
Eric S. Raymond committed
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
  (interactive "p")
  (insert-char ?/ arg)
  (if (> arg 0)
      (let ((oldpos (point))
	    (blinkpos)
	    (level 0))
	(save-excursion
	  (save-restriction
	    (if sgml-slash-distance
		(narrow-to-region (max (point-min)
				       (- (point) sgml-slash-distance))
				  oldpos))
	    (if (and (re-search-backward sgml-start-tag-regex (point-min) t)
		     (eq (match-end 0) (1- oldpos)))
		()
	      (goto-char (1- oldpos))
	      (while (and (not blinkpos)
			  (search-backward "/" (point-min) t))
		(let ((tagend (save-excursion
				(if (re-search-backward sgml-start-tag-regex
							(point-min) t)
				    (match-end 0)
				  nil))))
		  (if (eq tagend (point))
		      (if (eq level 0)
			  (setq blinkpos (point))
			(setq level (1- level)))
		    (setq level (1+ level)))))))
500 501 502 503 504 505 506
	  (when blinkpos
            (goto-char blinkpos)
            (if (pos-visible-in-window-p)
                (sit-for 1)
              (message "Matches %s"
                       (buffer-substring (line-beginning-position)
                                         (1+ blinkpos)))))))))
Eric S. Raymond's avatar
Eric S. Raymond committed
507

Stefan Monnier's avatar
Stefan Monnier committed
508 509 510
;; Why doesn't this use the iso-cvt table or, preferably, generate the
;; inverse of the extensive table in the SGML Quail input method?  -- fx
;; I guess that's moot since it only works with Latin-1 anyhow.
511 512
(defun sgml-name-char (&optional char)
  "Insert a symbolic character name according to `sgml-char-names'.
513 514 515
Non-ASCII chars may be inserted either with the meta key, as in M-SPC for
no-break space or M-- for a soft hyphen; or via an input method or
encoded keyboard operation."
516 517 518
  (interactive "*")
  (insert ?&)
  (or char
519
      (setq char (read-quoted-char "Enter char or octal number")))
520 521 522 523
  (delete-backward-char 1)
  (insert char)
  (undo-boundary)
  (delete-backward-char 1)
524 525 526 527 528 529 530 531
  (cond
   ((< char 256)
    (insert ?&
	    (or (aref sgml-char-names char)
		(format "#%d" char))
	    ?\;))
   ((aref sgml-char-names-table char)
    (insert ?& (aref sgml-char-names-table char) ?\;))
Stefan Monnier's avatar
Stefan Monnier committed
532 533 534 535 536
   ((let ((c (encode-char char 'ucs)))
      (when c
	(insert (format "&#%d;" c))
	t)))
   (t					; should be an error?  -- fx
537
    (insert char))))
538 539 540 541 542 543 544 545 546 547

(defun sgml-name-self ()
  "Insert a symbolic character name according to `sgml-char-names'."
  (interactive "*")
  (sgml-name-char last-command-char))

(defun sgml-maybe-name-self ()
  "Insert a symbolic character name according to `sgml-char-names'."
  (interactive "*")
  (if sgml-name-8bit-mode
548 549 550 551 552
      (let ((mc last-command-char))
	(if (< mc 256)
	    (setq mc (unibyte-char-to-multibyte mc)))
	(or mc (setq mc last-command-char))
	(sgml-name-char mc))
553 554 555
    (self-insert-command 1)))

(defun sgml-name-8bit-mode ()
Stefan Monnier's avatar
Stefan Monnier committed
556 557
  "Toggle whether to insert named entities instead of non-ASCII characters.
This only works for Latin-1 input."
558
  (interactive)
559
  (setq sgml-name-8bit-mode (not sgml-name-8bit-mode))
560
  (message "sgml name entity mode is now %s"
561
	   (if sgml-name-8bit-mode "ON" "OFF")))
562

Richard M. Stallman's avatar
Richard M. Stallman committed
563 564 565 566
;; When an element of a skeleton is a string "str", it is passed
;; through skeleton-transformation and inserted.  If "str" is to be
;; inserted literally, one should obtain it as the return value of a
;; function, e.g. (identity "str").
567 568

(define-skeleton sgml-tag
Richard M. Stallman's avatar
Richard M. Stallman committed
569 570
  "Prompt for a tag and insert it, optionally with attributes.
Completion and configuration are done according to `sgml-tag-alist'.
571
If you like tags and attributes in uppercase do \\[set-variable]
Richard M. Stallman's avatar
Richard M. Stallman committed
572 573
skeleton-transformation RET upcase RET, or put this in your `.emacs':
  (setq sgml-transformation 'upcase)"
Stefan Monnier's avatar
Stefan Monnier committed
574 575
  (funcall skeleton-transformation
	   (completing-read "Tag: " sgml-tag-alist))
576
  ?< str |
577
  (("") -1 '(undo-boundary) (identity "&lt;")) |	; see comment above
Stefan Monnier's avatar
Stefan Monnier committed
578 579 580 581 582
  `(("") '(setq v2 (sgml-attributes ,str t)) ?>
    (cond
     ((string= "![" ,str)
      (backward-char)
      '(("") " [ " _ " ]]"))
583
     ((and (eq v2 t) sgml-xml-mode (member ,str sgml-empty-tags))
Stefan Monnier's avatar
Stefan Monnier committed
584
      '(("") -1 "/>"))
585
     ((or (and (eq v2 t) (not sgml-xml-mode)) (string-match "^[/!?]" ,str))
Stefan Monnier's avatar
Stefan Monnier committed
586 587 588 589 590 591 592 593 594 595 596 597 598 599
      nil)
     ((symbolp v2)
      ;; Make sure we don't fall into an infinite loop.
      ;; For xhtml's `tr' tag, we should maybe use \n instead.
      (if (eq v2 t) (setq v2 nil))
      ;; We use `identity' to prevent skeleton from passing
      ;; `str' through skeleton-transformation a second time.
      '(("") v2 _ v2 "</" (identity ',str) ?>))
     ((eq (car v2) t)
      (cons '("") (cdr v2)))
     (t
      (append '(("") (car v2))
	      (cdr v2)
	      '(resume: (car v2) _ "</" (identity ',str) ?>))))))
600 601 602

(autoload 'skeleton-read "skeleton")

603
(defun sgml-attributes (tag &optional quiet)
Richard M. Stallman's avatar
Richard M. Stallman committed
604
  "When at top level of a tag, interactively insert attributes.
605

Richard M. Stallman's avatar
Richard M. Stallman committed
606 607
Completion and configuration of TAG are done according to `sgml-tag-alist'.
If QUIET, do not print a message when there are no attributes for TAG."
608
  (interactive (list (save-excursion (sgml-beginning-of-tag t))))
609 610
  (or (stringp tag) (error "Wrong context for adding attribute"))
  (if tag
611
      (let ((completion-ignore-case t)
612
	    (alist (cdr (assoc (downcase tag) sgml-tag-alist)))
613 614 615 616 617 618 619 620 621
	    car attribute i)
	(if (or (symbolp (car alist))
		(symbolp (car (car alist))))
	    (setq car (car alist)
		  alist (cdr alist)))
	(or quiet
	    (message "No attributes configured."))
	(if (stringp (car alist))
	    (progn
622 623
	      (insert (if (eq (preceding-char) ? ) "" ? )
		      (funcall skeleton-transformation (car alist)))
624 625 626 627 628 629 630
	      (sgml-value alist))
	  (setq i (length alist))
	  (while (> i 0)
	    (insert ? )
	    (insert (funcall skeleton-transformation
			     (setq attribute
				   (skeleton-read '(completing-read
631
						    "Attribute: "
632 633 634
						    alist)))))
	    (if (string= "" attribute)
		(setq i 0)
635
	      (sgml-value (assoc (downcase attribute) alist))
636 637 638 639 640 641
	      (setq i (1- i))))
	  (if (eq (preceding-char) ? )
	      (delete-backward-char 1)))
	car)))

(defun sgml-auto-attributes (arg)
Richard M. Stallman's avatar
Richard M. Stallman committed
642 643
  "Self insert the character typed; at top level of tag, prompt for attributes.
With prefix argument, only self insert."
644 645 646 647 648 649 650 651 652 653 654 655 656 657
  (interactive "*P")
  (let ((point (point))
	tag)
    (if (or arg
	    (not sgml-tag-alist)	; no message when nothing configured
	    (symbolp (setq tag (save-excursion (sgml-beginning-of-tag t))))
	    (eq (aref tag 0) ?/))
	(self-insert-command (prefix-numeric-value arg))
      (sgml-attributes tag)
      (setq last-command-char ? )
      (or (> (point) point)
	  (self-insert-command 1)))))

(defun sgml-tag-help (&optional tag)
Richard M. Stallman's avatar
Richard M. Stallman committed
658
  "Display description of tag TAG.  If TAG is omitted, use the tag at point."
659 660 661 662 663 664 665 666 667
  (interactive)
  (or tag
      (save-excursion
	(if (eq (following-char) ?<)
	    (forward-char))
	(setq tag (sgml-beginning-of-tag))))
  (or (stringp tag)
      (error "No tag selected"))
  (setq tag (downcase tag))
668
  (message "%s"
669
	   (or (cdr (assoc (downcase tag) sgml-tag-help))
670
	       (and (eq (aref tag 0) ?/)
671
		    (cdr (assoc (downcase (substring tag 1)) sgml-tag-help)))
672 673
	       "No description available")))

674 675 676 677 678 679
(defun sgml-maybe-end-tag (&optional arg)
  "Name self unless in position to end a tag or a prefix ARG is given."
  (interactive "P")
  (if (or arg (eq (car (sgml-lexical-context)) 'tag))
      (self-insert-command (prefix-numeric-value arg))
    (sgml-name-self)))
680 681 682

(defun sgml-skip-tag-backward (arg)
  "Skip to beginning of tag or matching opening tag if present.
Richard M. Stallman's avatar
Richard M. Stallman committed
683
With prefix argument ARG, repeat this ARG times."
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
  (interactive "p")
  (while (>= arg 1)
    (search-backward "<" nil t)
    (if (looking-at "</\\([^ \n\t>]+\\)")
	;; end tag, skip any nested pairs
	(let ((case-fold-search t)
	      (re (concat "</?" (regexp-quote (match-string 1)))))
	  (while (and (re-search-backward re nil t)
		      (eq (char-after (1+ (point))) ?/))
	    (forward-char 1)
	    (sgml-skip-tag-backward 1))))
    (setq arg (1- arg))))

(defun sgml-skip-tag-forward (arg &optional return)
  "Skip to end of tag or matching closing tag if present.
Richard M. Stallman's avatar
Richard M. Stallman committed
699
With prefix argument ARG, repeat this ARG times.
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
Return t iff after a closing tag."
  (interactive "p")
  (setq return t)
  (while (>= arg 1)
    (skip-chars-forward "^<>")
    (if (eq (following-char) ?>)
	(up-list -1))
    (if (looking-at "<\\([^/ \n\t>]+\\)")
	;; start tag, skip any nested same pairs _and_ closing tag
	(let ((case-fold-search t)
	      (re (concat "</?" (regexp-quote (match-string 1))))
	      point close)
	  (forward-list 1)
	  (setq point (point))
	  (while (and (re-search-forward re nil t)
		      (not (setq close
				 (eq (char-after (1+ (match-beginning 0))) ?/)))
		      (not (up-list -1))
		      (sgml-skip-tag-forward 1))
	    (setq close nil))
	  (if close
	      (up-list 1)
	    (goto-char point)
	    (setq return)))
      (forward-list 1))
    (setq arg (1- arg)))
  return)

(defun sgml-delete-tag (arg)
  "Delete tag on or after cursor, and matching closing or opening tag.
Richard M. Stallman's avatar
Richard M. Stallman committed
730
With prefix argument ARG, repeat this ARG times."
731 732 733 734
  (interactive "p")
  (while (>= arg 1)
    (save-excursion
      (let* (close open)
735
	(if (looking-at "[ \t\n]*<")
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
	    ;; just before tag
	    (if (eq (char-after (match-end 0)) ?/)
		;; closing tag
		(progn
		  (setq close (point))
		  (goto-char (match-end 0))))
	  ;; on tag?
	  (or (save-excursion (setq close (sgml-beginning-of-tag)
				    close (and (stringp close)
					       (eq (aref close 0) ?/)
					       (point))))
	      ;; not on closing tag
	      (let ((point (point)))
		(sgml-skip-tag-backward 1)
		(if (or (not (eq (following-char) ?<))
			(save-excursion
			  (forward-list 1)
			  (<= (point) point)))
		    (error "Not on or before tag")))))
	(if close
	    (progn
	      (sgml-skip-tag-backward 1)
	      (setq open (point))
	      (goto-char close)
	      (kill-sexp 1))
	  (setq open (point))
	  (sgml-skip-tag-forward 1)
	  (backward-list)
	  (forward-char)
	  (if (eq (aref (sgml-beginning-of-tag) 0) ?/)
	      (kill-sexp 1)))
	(goto-char open)
	(kill-sexp 1)))
    (setq arg (1- arg))))
Mike Williams's avatar
Mike Williams committed
770

771 772 773 774 775 776 777 778 779 780

;; Put read-only last to enable setting this even when read-only enabled.
(or (get 'sgml-tag 'invisible)
    (setplist 'sgml-tag
	      (append '(invisible t
			intangible t
			point-entered sgml-point-entered
			rear-nonsticky t
			read-only t)
		      (symbol-plist 'sgml-tag))))
781 782 783 784 785 786

(defun sgml-tags-invisible (arg)
  "Toggle visibility of existing tags."
  (interactive "P")
  (let ((modified (buffer-modified-p))
	(inhibit-read-only t)
787 788 789
	(inhibit-modification-hooks t)
	;; Avoid spurious the `file-locked' checks.
	(buffer-file-name nil)
790 791
	;; This is needed in case font lock gets called,
	;; since it moves point and might call sgml-point-entered.
792
	;; How could it get called?  -stef
793
	(inhibit-point-motion-hooks t)
794
	string)
795 796 797
    (unwind-protect
	(save-excursion
	  (goto-char (point-min))
Stefan Monnier's avatar
Stefan Monnier committed
798 799 800 801
	  (if (set (make-local-variable 'sgml-tags-invisible)
		   (if arg
		       (>= (prefix-numeric-value arg) 0)
		     (not sgml-tags-invisible)))
802
	      (while (re-search-forward sgml-tag-name-re nil t)
803 804 805
		(setq string
		      (cdr (assq (intern-soft (downcase (match-string 1)))
				 sgml-display-text)))
806
		(goto-char (match-beginning 0))
807
		(and (stringp string)
808
		     (not (overlays-at (point)))
Stefan Monnier's avatar
Stefan Monnier committed
809 810 811
		     (let ((ol (make-overlay (point) (match-beginning 1))))
		       (overlay-put ol 'before-string string)
		       (overlay-put ol 'sgml-tag t)))
812 813 814
		(put-text-property (point)
				   (progn (forward-list) (point))
				   'category 'sgml-tag))
815
	    (let ((pos (point-min)))
816
	      (while (< (setq pos (next-overlay-change pos)) (point-max))
Stefan Monnier's avatar
Stefan Monnier committed
817
		(dolist (ol (overlays-at pos))
Stefan Monnier's avatar
Stefan Monnier committed
818
		  (if (overlay-get ol 'sgml-tag)
Stefan Monnier's avatar
Stefan Monnier committed
819
		      (delete-overlay ol)))))
820
	    (remove-text-properties (point-min) (point-max) '(category nil))))
821
      (restore-buffer-modified-p modified))
822 823 824 825 826 827 828 829
    (run-hooks 'sgml-tags-invisible-hook)
    (message "")))

(defun sgml-point-entered (x y)
  ;; Show preceding or following hidden tag, depending of cursor direction.
  (let ((inhibit-point-motion-hooks t))
    (save-excursion
      (message "Invisible tag: %s"
830 831
	       ;; Strip properties, otherwise, the text is invisible.
	       (buffer-substring-no-properties
832 833 834 835 836 837 838
		(point)
		(if (or (and (> x y)
			     (not (eq (following-char) ?<)))
			(and (< x y)
			     (eq (preceding-char) ?>)))
		    (backward-list)
		  (forward-list)))))))
Mike Williams's avatar
Mike Williams committed
839

840

841 842
(autoload 'compile-internal "compile")

Eric S. Raymond's avatar
Eric S. Raymond committed
843 844 845
(defun sgml-validate (command)
  "Validate an SGML document.
Runs COMMAND, a shell command, in a separate process asynchronously
Richard M. Stallman's avatar
Richard M. Stallman committed
846
with output going to the buffer `*compilation*'.
Eric S. Raymond's avatar
Eric S. Raymond committed
847 848 849 850 851 852 853 854 855 856 857
You can then use the command \\[next-error] to find the next error message
and move to the line in the SGML document that caused it."
  (interactive
   (list (read-string "Validate command: "
		      (or sgml-saved-validate-command
			  (concat sgml-validate-command
				  " "
				  (let ((name (buffer-file-name)))
				    (and name
					 (file-name-nondirectory name))))))))
  (setq sgml-saved-validate-command command)
858
  (save-some-buffers (not compilation-ask-about-save) nil)
859
  (compile-internal command "No more errors"))
Eric S. Raymond's avatar
Eric S. Raymond committed
860

861 862 863
(defun sgml-lexical-context (&optional limit)
  "Return the lexical context at point as (TYPE . START).
START is the location of the start of the lexical element.
Mike Williams's avatar
Mike Williams committed
864
TYPE is one of `string', `comment', `tag', `cdata', or `text'.
865

866 867
Optional argument LIMIT is the position to start parsing from.
If nil, start from a preceding tag at indentation."
868 869
  (save-excursion
    (let ((pos (point))
870
	  text-start state)
871 872 873 874 875
      (if limit
          (goto-char limit)
        ;; Skip tags backwards until we find one at indentation
        (while (and (ignore-errors (sgml-parse-tag-backward))
                    (not (sgml-at-indentation-p)))))
Stefan Monnier's avatar
Stefan Monnier committed
876 877 878
      (with-syntax-table sgml-tag-syntax-table
	(while (< (point) pos)
	  ;; When entering this loop we're inside text.
879
	  (setq text-start (point))
Stefan Monnier's avatar
Stefan Monnier committed
880
	  (skip-chars-forward "^<" pos)
881 882
          (setq state
                (cond
883
                 ((= (point) pos)
884 885 886 887 888 889 890 891
                  ;; We got to the end without seeing a tag.
                  nil)
                 ((looking-at "<!\\[[A-Z]+\\[")
                  ;; We've found a CDATA section or similar.
                  (let ((cdata-start (point)))
                    (unless (search-forward "]]>" pos 'move)
                      (list 0 nil nil 'cdata nil nil nil nil cdata-start))))
                 (t
892
                  ;; We've reached a tag.  Parse it.
893 894 895 896 897 898 899 900
                  ;; FIXME: Handle net-enabling start-tags
                  (parse-partial-sexp (point) pos 0))))))
      (cond
       ((eq (nth 3 state) 'cdata) (cons 'cdata (nth 8 state)))
       ((nth 3 state) (cons 'string (nth 8 state)))
       ((nth 4 state) (cons 'comment (nth 8 state)))
       ((and state (> (nth 0 state) 0)) (cons 'tag (nth 1 state)))
       (t (cons 'text text-start))))))
901

902 903
(defun sgml-beginning-of-tag (&optional top-level)
  "Skip to beginning of tag and return its name.
904 905 906 907 908 909 910 911
If this can't be done, return nil."
  (let ((context (sgml-lexical-context)))
    (if (eq (car context) 'tag)
	(progn
	  (goto-char (cdr context))
	  (when (looking-at sgml-tag-name-re)
	    (match-string-no-properties 1)))
      (if top-level nil
912
	(when (not (eq (car context) 'text))
913 914
	  (goto-char (cdr context))
	  (sgml-beginning-of-tag t))))))
915 916

(defun sgml-value (alist)
917
  "Interactively insert value taken from attribute-rule ALIST.
918
See `sgml-tag-alist' for info about attribute rules."
919 920 921
  (setq alist (cdr alist))
  (if (stringp (car alist))
      (insert "=\"" (car alist) ?\")
922
    (if (and (eq (car alist) t) (not sgml-xml-mode))
923
	(when (cdr alist)
Stefan Monnier's avatar
Stefan Monnier committed
924 925 926 927 928
	  (insert "=\"")
	  (setq alist (skeleton-read '(completing-read "Value: " (cdr alist))))
	  (if (string< "" alist)
	      (insert alist ?\")
	    (delete-backward-char 2)))
929
      (insert "=\"")
930 931
      (when alist
        (insert (skeleton-read '(completing-read "Value: " alist))))
932
      (insert ?\"))))
933 934 935 936 937 938 939 940 941 942

(defun sgml-quote (start end &optional unquotep)
  "Quote SGML text in region.
With prefix argument, unquote the region."
  (interactive "r\np")
  (if (< start end)
      (goto-char start)
    (goto-char end)
    (setq end start))
  (if unquotep
943
      (while (re-search-forward "&\\(amp\\|\\(l\\|\\(g\\)\\)t\\)[;\n]" end t)
944 945 946 947 948
	(replace-match (if (match-end 3) ">" (if (match-end 2) "<" "&"))))
    (while (re-search-forward "[&<>]" end t)
      (replace-match (cdr (assq (char-before) '((?& . "&amp;")
						(?< . "&lt;")
						(?> . "&gt;"))))))))
949

950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
(defsubst sgml-at-indentation-p ()
  "Return true if point is at the first non-whitespace character on the line."
  (save-excursion
    (skip-chars-backward " \t")
    (bolp)))


;; Parsing

(defstruct (sgml-tag
            (:constructor sgml-make-tag (type start end name)))
  type start end name)

(defsubst sgml-parse-tag-name ()
  "Skip past a tag-name, and return the name."
  (buffer-substring-no-properties
   (point) (progn (skip-syntax-forward "w_") (point))))

968 969 970
(defsubst sgml-looking-back-at (str)
  "Return t if the test before point matches STR."
  (let ((start (- (point) (length str))))
971
    (and (>= start (point-min))
972
         (equal str (buffer-substring-no-properties start (point))))))
973 974 975 976 977 978

(defun sgml-parse-tag-backward ()
  "Parse an SGML tag backward, and return information about the tag.
Assume that parsing starts from within a textual context.
Leave point at the beginning of the tag."
  (let (tag-type tag-start tag-end name)
979 980
    (or (search-backward ">" nil 'move)
        (error "No tag found"))
981 982 983 984 985 986
    (setq tag-end (1+ (point)))
    (cond
     ((sgml-looking-back-at "--")   ; comment
      (setq tag-type 'comment
            tag-start (search-backward "<!--" nil t)))
     ((sgml-looking-back-at "]]")   ; cdata
987
      (setq tag-type 'cdata
Mike Williams's avatar
Mike Williams committed
988
            tag-start (re-search-backward "<!\\[[A-Z]+\\[" nil t)))
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
     (t
      (setq tag-start
            (with-syntax-table sgml-tag-syntax-table
              (goto-char tag-end)
              (backward-sexp)
              (point)))
      (goto-char (1+ tag-start))
      (case (char-after)
        (?!                             ; declaration
         (setq tag-type 'decl))
        (??                             ; processing-instruction
         (setq tag-type 'pi))
        (?/                             ; close-tag
         (forward-char 1)
         (setq tag-type 'close
               name (sgml-parse-tag-name)))
Mike Williams's avatar
Mike Williams committed
1005 1006
        (?%                             ; JSP tags
         (setq tag-type 'jsp))
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
        (t                              ; open or empty tag
         (setq tag-type 'open
               name (sgml-parse-tag-name))
         (if (or (eq ?/ (char-before (- tag-end 1)))
                 (sgml-empty-tag-p name))
             (setq tag-type 'empty))))))
    (goto-char tag-start)
    (sgml-make-tag tag-type tag-start tag-end name)))

(defun sgml-get-context (&optional full)
  "Determine the context of the current position.
If FULL is `empty', return even if the context is empty (i.e.
we just skipped over some element and got to a beginning of line).
If FULL is non-nil, parse back to the beginning of the buffer, otherwise
parse until we find a start-tag as the first thing on a line.

The context is a list of tag-info structures.  The last one is the tag
immediately enclosing the current position."
  (let ((here (point))
	(ignore nil)
	(context nil)
	tag-info)
    ;; CONTEXT keeps track of the tag-stack
    ;; IGNORE keeps track of the nesting level of point relative to the
    ;;   first (outermost) tag on the context.  This is the list of
    ;;   enclosing start-tags we'll have to ignore.
    (skip-chars-backward " \t\n")      ; Make sure we're not at indentation.
    (while
1035
	(and (or ignore
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
                 (not (if full (eq full 'empty) context))
		 (not (sgml-at-indentation-p))
		 (and context
		      (/= (point) (sgml-tag-start (car context)))
                      (sgml-unclosed-tag-p (sgml-tag-name (car context)))))
	     (setq tag-info (ignore-errors (sgml-parse-tag-backward))))
      
      ;; This tag may enclose things we thought were tags.  If so,
      ;; discard them.
      (while (and context
                  (> (sgml-tag-end tag-info)
                     (sgml-tag-end (car context))))
        (setq context (cdr context)))
           
      (cond

       ;; start-tag
       ((eq (sgml-tag-type tag-info) 'open)
	(cond
	 ((null ignore)
	  (if (and context
                   (sgml-unclosed-tag-p (sgml-tag-name tag-info))
		   (eq t (compare-strings
			  (sgml-tag-name tag-info) nil nil
			  (sgml-tag-name (car context)) nil nil t)))
	      ;; There was an implicit end-tag.
	      nil
	    (push tag-info context)))
	 ((eq t (compare-strings (sgml-tag-name tag-info) nil nil
				 (car ignore) nil nil t))
	  (setq ignore (cdr ignore)))
	 (t
	  ;; The open and close tags don't match.
	  (if (not sgml-xml-mode)
	      ;; Assume the open tag is simply not closed.
	      (unless (sgml-unclosed-tag-p (sgml-tag-name tag-info))
		(message "Unclosed tag <%s>" (sgml-tag-name tag-info)))
	    (message "Unmatched tags <%s> and </%s>"
		     (sgml-tag-name tag-info) (pop ignore))))))

       ;; end-tag
       ((eq (sgml-tag-type tag-info) 'close)
	(if (sgml-empty-tag-p (sgml-tag-name tag-info))
	    (message "Spurious </%s>: empty tag" (sgml-tag-name tag-info))
	  (push (sgml-tag-name tag-info) ignore)))
       ))

    ;; return context
    context))

(defun sgml-show-context (&optional full)
  "Display the current context.
If FULL is non-nil, parse back to the beginning of the buffer."
  (interactive "P")
  (with-output-to-temp-buffer "*XML Context*"
    (pp (save-excursion (sgml-get-context full)))))


;; Editing shortcuts

Mike Williams's avatar
Mike Williams committed
1096 1097
(defun sgml-close-tag ()
  "Insert an close-tag for the current element."
1098
  (interactive)
Mike Williams's avatar
Mike Williams committed
1099 1100 1101 1102 1103 1104 1105 1106 1107
  (case (car (sgml-lexical-context))
    (comment 	(insert " -->"))
    (cdata 	(insert "]]>"))
    (pi 	(insert " ?>"))
    (jsp 	(insert " %>"))
    (tag 	(insert " />"))
    (text
     (let ((context (save-excursion (sgml-get-context))))
       (if context
1108
           (progn
Mike Williams's avatar
Mike Williams committed
1109 1110 1111 1112
             (insert "</" (sgml-tag-name (car (last context))) ">")
             (indent-according-to-mode)))))
    (otherwise
     (error "Nothing to close"))))
1113

1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
(defun sgml-empty-tag-p (tag-name)
  "Return non-nil if TAG-NAME is an implicitly empty tag."
  (and (not sgml-xml-mode)
       (member-ignore-case tag-name sgml-empty-tags)))

(defun sgml-unclosed-tag-p (tag-name)
  "Return non-nil if TAG-NAME is a tag for which an end-tag is optional."
  (and (not sgml-xml-mode)
       (member-ignore-case tag-name sgml-unclosed-tags)))

1124 1125 1126
(defun sgml-calculate-indent ()
  "Calculate the column to which this line should be indented."
  (let ((lcon (sgml-lexical-context)))
1127

1128 1129 1130 1131 1132 1133 1134
    ;; Indent comment-start markers inside <!-- just like comment-end markers.
    (if (and (eq (car lcon