Revision: miles@gnu.org--gnu-2005/emacs--unicode--0--patch-65
[bpt/emacs.git] / lisp / textmodes / sgml-mode.el
index f5bd323..71ef616 100644 (file)
@@ -1,6 +1,7 @@
 ;;; sgml-mode.el --- SGML- and HTML-editing modes
 
-;; Copyright (C) 1992,95,96,98,2001,2002  Free Software Foundation, Inc.
+;; Copyright (C) 1992, 1995, 1996, 1998, 2001, 2002, 2003, 2004, 2005
+;;           Free Software Foundation, Inc.
 
 ;; Author: James Clark <jjc@jclark.com>
 ;; Maintainer: FSF
@@ -111,8 +112,6 @@ This takes effect when first loading the `sgml-mode' library.")
         (define-key map "\"" 'sgml-name-self))
       (when (memq ?' sgml-specials)
         (define-key map "'" 'sgml-name-self)))
-    (define-key map (vector (make-char 'latin-iso8859-1))
-      'sgml-maybe-name-self)
     (let ((c 127)
          (map (nth 1 map)))
       (while (< (setq c (1+ c)) 256)
@@ -208,7 +207,7 @@ This takes effect when first loading the `sgml-mode' library.")
   (let ((table (make-char-table 'sgml-table))
        (i 32)
        elt)
-    (while (< i 256)
+    (while (< i 128)
       (setq elt (aref sgml-char-names i))
       (if elt (aset table (make-char 'latin-iso8859-1 i) elt))
       (setq i (1+ i)))
@@ -239,6 +238,7 @@ separated by a space."
   :type '(choice (const nil) integer)
   :group 'sgml)
 
+(defconst sgml-namespace-re "[_[:alpha:]][-_.[:alnum:]]*")
 (defconst sgml-name-re "[_:[:alpha:]][-_.:[:alnum:]]*")
 (defconst sgml-tag-name-re (concat "<\\([!/?]?" sgml-name-re "\\)"))
 (defconst sgml-attrs-re "\\(?:[^\"'/><]\\|\"[^\"]*\"\\|'[^']*'\\)*")
@@ -246,13 +246,27 @@ separated by a space."
   "Regular expression that matches a non-empty start tag.
 Any terminating `>' or `/' is not matched.")
 
+(defface sgml-namespace
+  '((t (:inherit font-lock-builtin-face)))
+  "`sgml-mode' face used to highlight the namespace part of identifiers."
+  :group 'sgml)
+;; backward-compatibility alias
+(put 'sgml-namespace-face 'face-alias 'sgml-namespace)
+(defvar sgml-namespace-face 'sgml-namespace)
 
 ;; internal
 (defconst sgml-font-lock-keywords-1
   `((,(concat "<\\([!?]" sgml-name-re "\\)") 1 font-lock-keyword-face)
-    (,(concat "<\\(/?" sgml-name-re"\\)") 1 font-lock-function-name-face)
+    ;; We could use the simpler "\\(" sgml-namespace-re ":\\)?" instead,
+    ;; but it would cause a bit more backtracking in the re-matcher.
+    (,(concat "</?\\(" sgml-namespace-re "\\)\\(?::\\(" sgml-name-re "\\)\\)?")
+     (1 (if (match-end 2) sgml-namespace-face font-lock-function-name-face))
+     (2 font-lock-function-name-face nil t))
     ;; FIXME: this doesn't cover the variables using a default value.
-    (,(concat "\\(" sgml-name-re "\\)=[\"']") 1 font-lock-variable-name-face)
+    (,(concat "\\(" sgml-namespace-re "\\)\\(?::\\("
+             sgml-name-re "\\)\\)?=[\"']")
+     (1 (if (match-end 2) sgml-namespace-face font-lock-variable-name-face))
+     (2 font-lock-variable-name-face nil t))
     (,(concat "[&%]" sgml-name-re ";?") . font-lock-variable-name-face)))
 
 (defconst sgml-font-lock-keywords-2
@@ -263,7 +277,7 @@ Any terminating `>' or `/' is not matched.")
                      (regexp-opt (mapcar 'car sgml-tag-face-alist) t)
                      "\\([ \t][^>]*\\)?>\\([^<]+\\)</\\1>")
              '(3 (cdr (assoc (downcase (match-string 1))
-                             sgml-tag-face-alist))))))))
+                             sgml-tag-face-alist)) prepend))))))
 
 ;; for font-lock, but must be defvar'ed after
 ;; sgml-font-lock-keywords-1 and sgml-font-lock-keywords-2 above
@@ -337,9 +351,9 @@ an optional alist of possible values."
   "*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."
+a DOCTYPE or an XML declaration."
   :type 'boolean
-  :version "21.4"
+  :version "22.1"
   :group 'sgml)
 
 (defvar sgml-empty-tags nil
@@ -356,8 +370,8 @@ Otherwise, it is set to be buffer-local when the file has
              (looking-at "\\s-*<\\?xml")
              (when (re-search-forward
                     (eval-when-compile
-                      (mapconcat 'identity
-                                 '("<!DOCTYPE" "\\(\\w+\\)" "\\(\\w+\\)"
+                (mapconcat 'identity
+                           '("<!DOCTYPE" "\\(\\w+\\)" "\\(\\w+\\)"
                                    "\"\\([^\"]+\\)\"" "\"\\([^\"]+\\)\"")
                                  "\\s-+"))
                     nil t)
@@ -381,6 +395,14 @@ Otherwise, it is set to be buffer-local when the file has
        (concat "<" face ">"))
     (error "Face not configured for %s mode" mode-name)))
 
+(defun sgml-fill-nobreak ()
+  ;; Don't break between a tag name and its first argument.
+  (save-excursion
+    (skip-chars-backward " \t")
+    (and (not (zerop (skip-syntax-backward "w_")))
+        (skip-chars-backward "/?!")
+        (eq (char-before) ?<))))
+
 ;;;###autoload
 (define-derived-mode sgml-mode text-mode "SGML"
   "Major mode for editing SGML documents.
@@ -411,6 +433,7 @@ Do \\[describe-key] on the following bindings to discover what they do.
   (set (make-local-variable 'paragraph-separate)
        (concat paragraph-start "$"))
   (set (make-local-variable 'adaptive-fill-regexp) "[ \t]*")
+  (add-hook 'fill-nobreak-predicate 'sgml-fill-nobreak nil t)
   (set (make-local-variable 'indent-line-function) 'sgml-indent-line)
   (set (make-local-variable 'comment-start) "<!-- ")
   (set (make-local-variable 'comment-end) " -->")
@@ -441,13 +464,26 @@ Do \\[describe-key] on the following bindings to discover what they do.
   ;; recognized.
   (set (make-local-variable 'comment-start-skip) "\\(?:<!\\)?--[ \t]*")
   (set (make-local-variable 'comment-end-skip) "[ \t]*--\\([ \t\n]*>\\)?")
-  ;; This definition probably is not useful in derived modes.
-  (set (make-local-variable 'imenu-generic-expression)
-       (concat "<!\\(element\\|entity\\)[ \t\n]+%?[ \t\n]*\\("
-              sgml-name-re "\\)")))
+  ;; This definition has an HTML leaning but probably fits well for other modes.
+  (setq imenu-generic-expression
+       `((nil
+          ,(concat "<!\\(element\\|entity\\)[ \t\n]+%?[ \t\n]*\\("
+                   sgml-name-re "\\)")
+          2)
+         ("Id"
+          ,(concat "<[^>]+[ \t\n]+[Ii][Dd]=\\(['\"]"
+                   (if sgml-xml-mode "" "?")
+                   "\\)\\(" sgml-name-re "\\)\\1")
+          2)
+         ("Name"
+          ,(concat "<[^>]+[ \t\n]+[Nn][Aa][Mm][Ee]=\\(['\"]"
+                   (if sgml-xml-mode "" "?")
+                   "\\)\\(" sgml-name-re "\\)\\1")
+          2))))
 
 ;; Some programs (such as Glade 2) generate XML which has
 ;; -*- mode: xml -*-.
+;;;###autoload
 (defalias 'xml-mode 'sgml-mode)
 
 (defun sgml-comment-indent ()
@@ -524,21 +560,23 @@ encoded keyboard operation."
   (delete-backward-char 1)
   (insert char)
   (undo-boundary)
-  (delete-backward-char 1)
-  (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) ?\;))
-   ((let ((c (encode-char char 'ucs)))
-      (when c
-       (insert (format "&#%d;" c))
-       t)))
-   (t                                  ; should be an error?  -- fx
-    (insert char))))
+  (sgml-namify-char))
+
+(defun sgml-namify-char ()
+  "Change the char before point into its `&name;' equivalent.
+Uses `sgml-char-names'."
+  (interactive)
+  (let* ((char (char-before))
+        (name
+         (cond
+          ((null char) (error "No char before point"))
+          ((< char 256) (or (aref sgml-char-names char) char))
+          ((aref sgml-char-names-table char))
+          ((encode-char char 'ucs)))))
+    (if (not name)
+       (error "Don't know the name of `%c'" char)
+      (delete-backward-char 1)
+      (insert (format (if (numberp name) "&#%d;" "&%s;") name)))))
 
 (defun sgml-name-self ()
   "Insert a symbolic character name according to `sgml-char-names'."
@@ -569,6 +607,8 @@ This only works for Latin-1 input."
 ;; inserted literally, one should obtain it as the return value of a
 ;; function, e.g. (identity "str").
 
+(defvar sgml-tag-last nil)
+(defvar sgml-tag-history nil)
 (define-skeleton sgml-tag
   "Prompt for a tag and insert it, optionally with attributes.
 Completion and configuration are done according to `sgml-tag-alist'.
@@ -576,7 +616,12 @@ If you like tags and attributes in uppercase do \\[set-variable]
 skeleton-transformation RET upcase RET, or put this in your `.emacs':
   (setq sgml-transformation 'upcase)"
   (funcall (or skeleton-transformation 'identity)
-           (completing-read "Tag: " sgml-tag-alist))
+           (setq sgml-tag-last
+                (completing-read
+                 (if (> (length sgml-tag-last) 0)
+                     (format "Tag (default %s): " sgml-tag-last)
+                   "Tag: ")
+                 sgml-tag-alist nil nil nil 'sgml-tag-history sgml-tag-last)))
   ?< str |
   (("") -1 '(undo-boundary) (identity "&lt;")) |       ; see comment above
   `(("") '(setq v2 (sgml-attributes ,str t)) ?>
@@ -585,7 +630,7 @@ skeleton-transformation RET upcase RET, or put this in your `.emacs':
       (backward-char)
       '(("") " [ " _ " ]]"))
      ((and (eq v2 t) sgml-xml-mode (member ,str sgml-empty-tags))
-      '(("") -1 "/>"))
+      '(("") -1 " />"))
      ((or (and (eq v2 t) (not sgml-xml-mode)) (string-match "^[/!?]" ,str))
       nil)
      ((symbolp v2)
@@ -686,50 +731,61 @@ With prefix argument, only self insert."
   "Skip to beginning of tag or matching opening tag if present.
 With prefix argument ARG, repeat this ARG times."
   (interactive "p")
+  ;; FIXME: use sgml-get-context or something similar.
   (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)))))
+             (re (concat "</?" (regexp-quote (match-string 1))
+                         ;; Ignore empty tags like <foo/>.
+                         "\\([^>]*[^/>]\\)?>")))
          (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)
+(defun sgml-skip-tag-forward (arg)
   "Skip to end of tag or matching closing tag if present.
 With prefix argument ARG, repeat this ARG times.
 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)
+  ;; FIXME: Use sgml-get-context or something similar.
+  ;; It currently might jump to an unrelated </P> if the <P>
+  ;; we're skipping has no matching </P>.
+  (let ((return t))
+    (with-syntax-table sgml-tag-syntax-table
+      (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))
+                             ;; Ignore empty tags like <foo/>.
+                             "\\([^>]*[^/>]\\)?>"))
+                 point close)
+             (forward-list 1)
+             (setq point (point))
+             ;; FIXME: This re-search-forward will mistakenly match
+             ;; tag-like text inside attributes.
+             (while (and (re-search-forward re nil t)
+                         (not (setq close
+                                    (eq (char-after (1+ (match-beginning 0))) ?/)))
+                         (goto-char (match-beginning 0))
+                         (sgml-skip-tag-forward 1))
+               (setq close nil))
+             (unless close
+               (goto-char point)
+               (setq return nil)))
+         (forward-list 1))
+       (setq arg (1- arg)))
+      return)))
 
 (defun sgml-delete-tag (arg)
+  ;; FIXME: Should be called sgml-kill-tag or should not touch the kill-ring.
   "Delete tag on or after cursor, and matching closing or opening tag.
 With prefix argument ARG, repeat this ARG times."
   (interactive "p")
@@ -763,13 +819,17 @@ With prefix argument ARG, repeat this ARG times."
              (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)))
+         (when (and (sgml-skip-tag-forward 1)
+                    (not (looking-back "/>")))
+           (kill-sexp -1)))
+       ;; Delete any resulting empty line.  If we didn't kill-sexp,
+       ;; this *should* do nothing, because we're right after the tag.
+       (if (progn (forward-line 0) (looking-at "\\(?:[ \t]*$\\)\n?"))
+           (delete-region (match-beginning 0) (match-end 0)))
        (goto-char open)
-       (kill-sexp 1)))
+       (kill-sexp 1)
+       (if (progn (forward-line 0) (looking-at "\\(?:[ \t]*$\\)\n?"))
+           (delete-region (match-beginning 0) (match-end 0)))))
     (setq arg (1- arg))))
 
 \f
@@ -777,7 +837,6 @@ With prefix argument ARG, repeat this ARG times."
 (or (get 'sgml-tag 'invisible)
     (setplist 'sgml-tag
              (append '(invisible t
-                       intangible t
                        point-entered sgml-point-entered
                        rear-nonsticky t
                        read-only t)
@@ -1006,105 +1065,147 @@ You might want to turn on `auto-fill-mode' to get better results."
     (and (>= start (point-min))
          (equal str (buffer-substring-no-properties start (point))))))
 
-(defun sgml-parse-tag-backward ()
+(defun sgml-tag-text-p (start end)
+  "Return non-nil if text between START and END is a tag.
+Checks among other things that the tag does not contain spurious
+unquoted < or > chars inside, which would indicate that it
+really isn't a tag after all."
+  (save-excursion
+    (with-syntax-table sgml-tag-syntax-table
+      (let ((pps (parse-partial-sexp start end 2)))
+       (and (= (nth 0 pps) 0))))))
+
+(defun sgml-parse-tag-backward (&optional limit)
   "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)
-    (or (search-backward ">" nil 'move)
-        (error "No tag found"))
-    (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
-      (setq tag-type 'cdata
-            tag-start (re-search-backward "<!\\[[A-Z]+\\[" nil t)))
-     (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)))
-        (?%                             ; JSP tags
-         (setq tag-type 'jsp))
-        (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)
+  (catch 'found
+    (let (tag-type tag-start tag-end name)
+      (or (re-search-backward "[<>]" limit 'move)
+         (error "No tag found"))
+      (when (eq (char-after) ?<)
+       ;; Oops!! Looks like we were not in a textual context after all!.
+       ;; Let's try to recover.
+       (with-syntax-table sgml-tag-syntax-table
+         (let ((pos (point)))
+           (condition-case nil
+               (forward-sexp)
+             (scan-error
+              ;; This < seems to be just a spurious one, let's ignore it.
+              (goto-char pos)
+              (throw 'found (sgml-parse-tag-backward limit))))
+           ;; Check it is really a tag, without any extra < or > inside.
+           (unless (sgml-tag-text-p pos (point))
+             (goto-char pos)
+             (throw 'found (sgml-parse-tag-backward limit)))
+           (forward-char -1))))
+      (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
+       (setq tag-type 'cdata
+             tag-start (re-search-backward "<!\\[[A-Z]+\\[" nil t)))
+       (t
+       (setq tag-start
+             (with-syntax-table sgml-tag-syntax-table
+               (goto-char tag-end)
+               (condition-case nil
+                   (backward-sexp)
+                 (scan-error
+                  ;; This > isn't really the end of a tag. Skip it.
+                  (goto-char (1- tag-end))
+                  (throw 'found (sgml-parse-tag-backward limit))))
+               (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)))
+         (?%                           ; JSP tags
+          (setq tag-type 'jsp))
+         (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 until)
   "Determine the context of the current position.
-If FULL is `empty', return even if the context is empty (i.e.
+By default, parse until we find a start-tag as the first thing on a line.
+If UNTIL 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."
+immediately enclosing the current position.
+
+Point is assumed to be outside of any tag.  If we discover that it's
+not the case, the first tag returned is the one inside which we are."
   (let ((here (point))
+       (stack nil)
        (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.
+    ;; STACK keeps track of the end tags we've seen (and thus the start-tags
+    ;;   we'll have to ignore) when skipping over matching open..close pairs.
+    ;; IGNORE is a list of tags that can be ignored because they have been
+    ;;   closed implicitly.
     (skip-chars-backward " \t\n")      ; Make sure we're not at indentation.
     (while
-       (and (or ignore
-                 (not (if full (eq full 'empty) context))
+       (and (not (eq until 'now))
+            (or stack
+                (not (if until (eq until 'empty) context))
                 (not (sgml-at-indentation-p))
                 (and context
                      (/= (point) (sgml-tag-start (car context)))
-                      (sgml-unclosed-tag-p (sgml-tag-name (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
+       ((> (sgml-tag-end tag-info) here)
+       ;; Oops!!  Looks like we were not outside of any tag, after all.
+       (push tag-info context)
+       (setq until 'now))
 
        ;; 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)))
+        ((null stack)
+         (if (member-ignore-case (sgml-tag-name tag-info) ignore)
              ;; There was an implicit end-tag.
              nil
-           (push tag-info context)))
+           (push tag-info context)
+           ;; We're changing context so the tags implicitly closed inside
+           ;; the previous context aren't implicitly closed here any more.
+           ;; [ Well, actually it depends, but we don't have the info about
+           ;; when it doesn't and when it does.   --Stef ]
+           (setq ignore nil)))
         ((eq t (compare-strings (sgml-tag-name tag-info) nil nil
-                                (car ignore) nil nil t))
-         (setq ignore (cdr ignore)))
+                                (car stack) nil nil t))
+         (setq stack (cdr stack)))
         (t
          ;; The open and close tags don't match.
          (if (not sgml-xml-mode)
              (unless (sgml-unclosed-tag-p (sgml-tag-name tag-info))
                (message "Unclosed tag <%s>" (sgml-tag-name tag-info))
-               (let ((tmp ignore))
+               (let ((tmp stack))
                  ;; We could just assume that the tag is simply not closed
                  ;; but it's a bad assumption when tags *are* closed but
                  ;; not properly nested.
@@ -1115,13 +1216,19 @@ immediately enclosing the current position."
                    (setq tmp (cdr tmp)))
                  (if (cdr tmp) (setcdr tmp (cddr tmp)))))
            (message "Unmatched tags <%s> and </%s>"
-                    (sgml-tag-name tag-info) (pop ignore))))))
+                    (sgml-tag-name tag-info) (pop stack)))))
+
+       (if (and (null stack) (sgml-unclosed-tag-p (sgml-tag-name tag-info)))
+           ;; This is a top-level open of an implicitly closed tag, so any
+           ;; occurrence of such an open tag at the same level can be ignored
+           ;; because it's been implicitly closed.
+           (push (sgml-tag-name tag-info) 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)))
+         (push (sgml-tag-name tag-info) stack)))
        ))
 
     ;; return context
@@ -1144,7 +1251,9 @@ If FULL is non-nil, parse back to the beginning of the buffer."
 ;; Editing shortcuts
 
 (defun sgml-close-tag ()
-  "Insert an close-tag for the current element."
+  "Close current element.
+Depending on context, inserts a matching close-tag, or closes
+the current start-tag or the current comment or the current cdata, ..."
   (interactive)
   (case (car (sgml-lexical-context))
     (comment   (insert " -->"))
@@ -1171,99 +1280,113 @@ If FULL is non-nil, parse back to the beginning of the buffer."
   (and (not sgml-xml-mode)
        (member-ignore-case tag-name sgml-unclosed-tags)))
 
-(defun sgml-calculate-indent ()
-  "Calculate the column to which this line should be indented."
-  (let ((lcon (sgml-lexical-context)))
-
-    ;; Indent comment-start markers inside <!-- just like comment-end markers.
-    (if (and (eq (car lcon) 'tag)
-            (looking-at "--")
-            (save-excursion (goto-char (cdr lcon)) (looking-at "<!--")))
-       (setq lcon (cons 'comment (+ (cdr lcon) 2))))
-
-    (case (car lcon)
-
-      (string
+(defun sgml-calculate-indent (&optional lcon)
+  "Calculate the column to which this line should be indented.
+LCON is the lexical context, if any."
+  (unless lcon (setq lcon (sgml-lexical-context)))
+
+  ;; Indent comment-start markers inside <!-- just like comment-end markers.
+  (if (and (eq (car lcon) 'tag)
+          (looking-at "--")
+          (save-excursion (goto-char (cdr lcon)) (looking-at "<!--")))
+      (setq lcon (cons 'comment (+ (cdr lcon) 2))))
+
+  (case (car lcon)
+
+    (string
+     ;; Go back to previous non-empty line.
+     (while (and (> (point) (cdr lcon))
+                (zerop (forward-line -1))
+                (looking-at "[ \t]*$")))
+     (if (> (point) (cdr lcon))
+        ;; Previous line is inside the string.
+        (current-indentation)
+       (goto-char (cdr lcon))
+       (1+ (current-column))))
+
+    (comment
+     (let ((mark (looking-at "--")))
        ;; Go back to previous non-empty line.
        (while (and (> (point) (cdr lcon))
                   (zerop (forward-line -1))
-                  (looking-at "[ \t]*$")))
+                  (or (looking-at "[ \t]*$")
+                      (if mark (not (looking-at "[ \t]*--"))))))
        (if (> (point) (cdr lcon))
-          ;; Previous line is inside the string.
-          (current-indentation)
+          ;; Previous line is inside the comment.
+          (skip-chars-forward " \t")
         (goto-char (cdr lcon))
-        (1+ (current-column))))
-
-      (comment
-       (let ((mark (looking-at "--")))
-        ;; Go back to previous non-empty line.
-        (while (and (> (point) (cdr lcon))
-                    (zerop (forward-line -1))
-                    (or (looking-at "[ \t]*$")
-                        (if mark (not (looking-at "[ \t]*--"))))))
-        (if (> (point) (cdr lcon))
-            ;; Previous line is inside the comment.
-            (skip-chars-forward " \t")
-          (goto-char (cdr lcon)))
-        (when (and (not mark) (looking-at "--"))
-          (forward-char 2) (skip-chars-forward " \t"))
-        (current-column)))
-
-      (cdata
-       (current-column))
-
-      (tag
+        ;; Skip `<!' to get to the `--' with which we want to align.
+        (search-forward "--")
+        (goto-char (match-beginning 0)))
+       (when (and (not mark) (looking-at "--"))
+        (forward-char 2) (skip-chars-forward " \t"))
+       (current-column)))
+
+    ;; We don't know how to indent it.  Let's be honest about it.
+    (cdata nil)
+
+    (tag
+     (goto-char (1+ (cdr lcon)))
+     (skip-chars-forward "^ \t\n")     ;Skip tag name.
+     (skip-chars-forward " \t")
+     (if (not (eolp))
+        (current-column)
+       ;; This is the first attribute: indent.
        (goto-char (1+ (cdr lcon)))
-       (skip-chars-forward "^ \t\n")   ;Skip tag name.
-       (skip-chars-forward " \t")
-       (if (not (eolp))
-          (current-column)
-        ;; This is the first attribute: indent.
-        (goto-char (1+ (cdr lcon)))
-        (+ (current-column) sgml-basic-offset)))
-
-      (text
-       (while (looking-at "</")
-        (forward-sexp 1)
-        (skip-chars-forward " \t"))
-       (let* ((here (point))
-             (unclosed (and ;; (not sgml-xml-mode)
-                            (looking-at sgml-tag-name-re)
-                            (member-ignore-case (match-string 1)
-                                                sgml-unclosed-tags)
-                            (match-string 1)))
-             (context
-              ;; If possible, align on the previous non-empty text line.
-              ;; Otherwise, do a more serious parsing to find the
-              ;; tag(s) relative to which we should be indenting.
-              (if (and (not unclosed) (skip-chars-backward " \t")
-                       (< (skip-chars-backward " \t\n") 0)
-                       (back-to-indentation)
-                       (> (point) (cdr lcon)))
-                  nil
-                (goto-char here)
-                (nreverse (sgml-get-context (if unclosed nil 'empty)))))
-             (there (point)))
-        ;; Ignore previous unclosed start-tag in context.
-        (while (and context unclosed
-                    (eq t (compare-strings
-                           (sgml-tag-name (car context)) nil nil
-                           unclosed nil nil t)))
-          (setq context (cdr context)))
-        ;; Indent to reflect nesting.
-        (if (and context
-                 (goto-char (sgml-tag-end (car context)))
-                 (skip-chars-forward " \t\n")
-                 (< (point) here) (sgml-at-indentation-p))
-            (current-column)
-          (goto-char there)
-          (+ (current-column)
-             (* sgml-basic-offset (length context))))))
-      
-      (otherwise
-       (error "Unrecognised context %s" (car lcon)))
-
-      )))
+       (+ (current-column) sgml-basic-offset)))
+
+    (text
+     (while (looking-at "</")
+       (forward-sexp 1)
+       (skip-chars-forward " \t"))
+     (let* ((here (point))
+           (unclosed (and ;; (not sgml-xml-mode)
+                      (looking-at sgml-tag-name-re)
+                      (member-ignore-case (match-string 1)
+                                          sgml-unclosed-tags)
+                      (match-string 1)))
+           (context
+            ;; If possible, align on the previous non-empty text line.
+            ;; Otherwise, do a more serious parsing to find the
+            ;; tag(s) relative to which we should be indenting.
+            (if (and (not unclosed) (skip-chars-backward " \t")
+                     (< (skip-chars-backward " \t\n") 0)
+                     (back-to-indentation)
+                     (> (point) (cdr lcon)))
+                nil
+              (goto-char here)
+              (nreverse (sgml-get-context (if unclosed nil 'empty)))))
+           (there (point)))
+       ;; Ignore previous unclosed start-tag in context.
+       (while (and context unclosed
+                  (eq t (compare-strings
+                         (sgml-tag-name (car context)) nil nil
+                         unclosed nil nil t)))
+        (setq context (cdr context)))
+       ;; Indent to reflect nesting.
+       (cond
+       ;; If we were not in a text context after all, let's try again.
+       ((and context (> (sgml-tag-end (car context)) here))
+        (goto-char here)
+        (sgml-calculate-indent
+         (cons (if (memq (sgml-tag-type (car context)) '(comment cdata))
+                   (sgml-tag-type (car context)) 'tag)
+               (sgml-tag-start (car context)))))
+       ;; Align on the first element after the nearest open-tag, if any.
+       ((and context
+             (goto-char (sgml-tag-end (car context)))
+             (skip-chars-forward " \t\n")
+             (< (point) here) (sgml-at-indentation-p))
+        (current-column))
+       (t
+        (goto-char there)
+        (+ (current-column)
+           (* sgml-basic-offset (length context)))))))
+
+    (otherwise
+     (error "Unrecognized context %s" (car lcon)))
+
+    ))
 
 (defun sgml-indent-line ()
   "Indent the current line as SGML."
@@ -1274,9 +1397,11 @@ If FULL is non-nil, parse back to the beginning of the buffer."
            (back-to-indentation)
            (if (>= (point) savep) (setq savep nil))
            (sgml-calculate-indent))))
-    (if savep
-       (save-excursion (indent-line-to indent-col))
-      (indent-line-to indent-col))))
+    (if (null indent-col)
+       'noindent
+      (if savep
+         (save-excursion (indent-line-to indent-col))
+       (indent-line-to indent-col)))))
 
 (defun sgml-guess-indent ()
   "Guess an appropriate value for `sgml-basic-offset'.
@@ -1449,7 +1574,7 @@ This takes effect when first loading the library.")
       ("dir" ,@list)
       ("font" nil "size" ("-1") ("+1") ("-2") ("+2") ,@1-7)
       ("form" (\n _ \n "<input type=\"submit\" value=\"\""
-              (if sgml-xml-mode "/>" ">"))
+              (if sgml-xml-mode " />" ">"))
        ("action" ,@(cdr href)) ("method" ("get") ("post")))
       ("h1" ,@align)
       ("h2" ,@align)
@@ -1594,7 +1719,7 @@ This takes effect when first loading the library.")
     ("dir" . "Directory list (obsolete)")
     ("dl" . "Definition list")
     ("dt" . "Term to be definined")
-    ("em" . "Emphasised")
+    ("em" . "Emphasized")
     ("embed" . "Embedded data in foreign format")
     ("fig" . "Figure")
     ("figa" . "Figure anchor")
@@ -1680,7 +1805,7 @@ have <h1>Very Major Headlines</h1> through <h6>Very Minor Headlines</h6>
 
 <p>Paragraphs only need an opening tag.  Line breaks and multiple spaces are
 ignored unless the text is <pre>preformatted.</pre>  Text can be marked as
-<b>bold</b>, <i>italic</i> or <u>underlined</u> using the normal  M-g  or
+<b>bold</b>, <i>italic</i> or <u>underlined</u> using the normal M-o or
 Edit/Text Properties/Face commands.
 
 Pages can have <a name=\"SOMENAME\">named points</a> and can link other points
@@ -1740,7 +1865,7 @@ The second `match-string' matches extra tags and is ignored.
 The third `match-string' will be the used in the menu.")
 
 (defun html-imenu-index ()
-  "Return an table of contents for an HTML buffer for use with Imenu."
+  "Return a table of contents for an HTML buffer for use with Imenu."
   (let (toc-index)
     (save-excursion
       (goto-char (point-min))
@@ -1754,31 +1879,29 @@ The third `match-string' will be the used in the menu.")
                    toc-index))))
     (nreverse toc-index)))
 
-(defun html-autoview-mode (&optional arg)
+(define-minor-mode html-autoview-mode
   "Toggle automatic viewing via `browse-url-of-buffer' upon saving buffer.
 With positive prefix ARG always turns viewing on, with negative ARG always off.
 Can be used as a value for `html-mode-hook'."
-  (interactive "P")
-  (if (setq arg (if arg
-                   (< (prefix-numeric-value arg) 0)
-                 (and (boundp 'after-save-hook)
-                      (memq 'browse-url-of-buffer after-save-hook))))
-      (setq after-save-hook (delq 'browse-url-of-buffer after-save-hook))
-    (add-hook 'after-save-hook 'browse-url-of-buffer nil t))
-  (message "Autoviewing turned %s."
-          (if arg "off" "on")))
+  nil nil nil
+  :group 'sgml
+  (if html-autoview-mode
+      (add-hook 'after-save-hook 'browse-url-of-buffer nil t)
+    (remove-hook 'after-save-hook 'browse-url-of-buffer t)))
 
 \f
 (define-skeleton html-href-anchor
   "HTML anchor tag with href attribute."
   "URL: "
-  '(setq input "http:")
+  ;; '(setq input "http:")
   "<a href=\"" str "\">" _ "</a>")
 
 (define-skeleton html-name-anchor
   "HTML anchor tag with name attribute."
   "Name: "
-  "<a name=\"" str "\">" _ "</a>")
+  "<a name=\"" str "\""
+  (if sgml-xml-mode (concat " id=\"" str "\""))
+  ">" _ "</a>")
 
 (define-skeleton html-headline-1
   "HTML level 1 headline tags."
@@ -1813,18 +1936,18 @@ Can be used as a value for `html-mode-hook'."
 (define-skeleton html-horizontal-rule
   "HTML horizontal rule tag."
   nil
-  (if sgml-xml-mode "<hr/>" "<hr>") \n)
+  (if sgml-xml-mode "<hr />" "<hr>") \n)
 
 (define-skeleton html-image
   "HTML image tag."
-  nil
-  "<img src=\"" _ "\""
-  (if sgml-xml-mode "/>" ">"))
+  "Image URL: "
+  "<img src=\"" str "\" alt=\"" _ "\""
+  (if sgml-xml-mode " />" ">"))
 
 (define-skeleton html-line
   "HTML line break tag."
   nil
-  (if sgml-xml-mode "<br/>" "<br>") \n)
+  (if sgml-xml-mode "<br />" "<br>") \n)
 
 (define-skeleton html-ordered-list
   "HTML ordered list tags."
@@ -1850,7 +1973,7 @@ Can be used as a value for `html-mode-hook'."
   "HTML paragraph tag."
   nil
   (if (bolp) nil ?\n)
-  \n "<p>" _ (if sgml-xml-mode "</p>"))
+  "<p>" _ (if sgml-xml-mode "</p>"))
 
 (define-skeleton html-checkboxes
   "Group of connected checkbox inputs."
@@ -1862,12 +1985,13 @@ Can be used as a value for `html-mode-hook'."
    "\" name=\"" (or v1 (setq v1 (skeleton-read "Name: ")))
    "\" value=\"" str ?\"
    (when (y-or-n-p "Set \"checked\" attribute? ")
-     (funcall skeleton-transformation " checked"))
-   (if sgml-xml-mode "/>" ">")
+     (funcall skeleton-transformation
+             (if sgml-xml-mode " checked=\"checked\"" " checked")))
+   (if sgml-xml-mode " />" ">")
    (skeleton-read "Text: " (capitalize str))
    (or v2 (setq v2 (if (y-or-n-p "Newline after text? ")
                       (funcall skeleton-transformation
-                                (if sgml-xml-mode "<br/>" "<br>"))
+                                (if sgml-xml-mode "<br />" "<br>"))
                     "")))
    \n))
 
@@ -1881,15 +2005,17 @@ Can be used as a value for `html-mode-hook'."
    "\" name=\"" (or (car v2) (setcar v2 (skeleton-read "Name: ")))
    "\" value=\"" str ?\"
    (when (and (not v1) (setq v1 (y-or-n-p "Set \"checked\" attribute? ")))
-     (funcall skeleton-transformation " checked"))
-   (if sgml-xml-mode "/>" ">")
+     (funcall skeleton-transformation
+             (if sgml-xml-mode " checked=\"checked\"" " checked")))
+   (if sgml-xml-mode " />" ">")
    (skeleton-read "Text: " (capitalize str))
    (or (cdr v2) (setcdr v2 (if (y-or-n-p "Newline after text? ")
                               (funcall skeleton-transformation
-                                        (if sgml-xml-mode "<br/>" "<br>"))
+                                        (if sgml-xml-mode "<br />" "<br>"))
                             "")))
    \n))
 
 (provide 'sgml-mode)
 
+;; arch-tag: 9675da94-b7f9-4bda-ad19-73ed7b4fb401
 ;;; sgml-mode.el ends here