Completing end-tag on '/'

2001-10-15
2001-10-15
  • Tony Graham
    Tony Graham
    2001-10-15

    A common complaint of people who use both PSGML and the xslide XSL mode package is that the two modes have different mechanisms for inserting end-tags.

    Adding the following forms to your .emacs enables xslide-style auto-completion of end-tags in XML mode as well as PSGML-style C-c / insertion of end-tags.

    Regards,

    Tony Graham.

    (defun my-xml-mode-slash ()
      "Do-the-right-thing-in-XML-mode slash"
      (interactive)
      (backward-char)
      (if (looking-at "<")
          (progn
            (delete-char 1)
            (sgml-insert-end-tag))
        (progn
          (forward-char)
          (insert "/"))))

    (add-hook 'xml-mode-hook
              (lambda () (define-key xml-mode-map "/" `my-xml-mode-slash)))

     
    • Norman Walsh
      Norman Walsh
      2001-10-15

      I found the following slightly different solution was necessary: (I forget why I was unhappy with calling sgml-insert-end-tag directly, maybe something to do with how it behaves inside comments and PIs?)

      (defun sgml-slash-check ()
        "For psgml-mode, if you type `/' after `<', insert the appropriate
      end tag, if there is an open element."
        (interactive)
        (if (= (char-before) 60)
            ;; Slash after <, let's end the current open element if we can
            (progn
          (sgml-parse-to-here)
          (cond
           ((eq sgml-current-tree sgml-top-tree)
            (insert "/"))
           ((not (sgml-final-p sgml-current-state))
            (insert "/"))
           (t (progn
                (delete-backward-char 1)
                (insert (sgml-end-tag-of sgml-current-tree))))))
          (insert "/")))

      I also found that the following (perhaps crude) patch to psgml-parse.el was necessary to make the XML predefined entities acceptable to the PSGML parser:
      1688c1688,1703
      <     (cond ((and (null entity)
      ---
      >     (cond ((and sgml-xml-p (string= name "amp"))
      >        (sgml-push-to-entity (sgml-make-entity name "text" "&#38;")
      >                 sgml-markup-start))
      >       ((and sgml-xml-p (string= name "lt"))
      >        (sgml-push-to-entity (sgml-make-entity name "text" "&#60;")
      >                 sgml-markup-start))
      >       ((and sgml-xml-p (string= name "gt"))
      >        (sgml-push-to-entity (sgml-make-entity name "text" "&#62;")
      >                 sgml-markup-start))
      >       ((and sgml-xml-p (string= name "apos"))
      >        (sgml-push-to-entity (sgml-make-entity name "text" "&#39;")
      >                 sgml-markup-start))
      >       ((and sgml-xml-p (string= name "quot"))
      >        (sgml-push-to-entity (sgml-make-entity name "text" "&#34;")
      >                 sgml-markup-start))
      >       ((and (null entity)