Emacs: HTML, Delete Tags

By Xah Lee. Date: . Last updated: .

Here's a command to delete html tag pairs.

(defun xah-html-remove-tag-pair ()
  "Remove the previous tag(s) under cursor.
The tags removed is the first angle bracketed tag to the left of cursor. Both begin and end tags are removed, unless it's a self-closing tag such as <br />.
After this command is called, press t to repeat it.

URL `http://xahlee.info/emacs/emacs/emacs_html_delete_tags.html'
Version 2021-01-03 2021-08-13"
  (interactive)
  (require 'sgml-mode)
  (save-excursion
    (let ($isInEndTag $p1 $p2 $elStartP1 $elStartP2 $isSelfCloseTag $elEndP1 $elEndP2 $openTagStr $closeTagStr)
      (progn
        (search-backward "<")
        (setq $p1 (point))
        (forward-char 1)
        (setq $isInEndTag (char-equal (char-after ) ?/))
        (search-forward ">")
        (setq $p2 (point)))
      (if $isInEndTag
          (progn
            (setq $elEndP1 $p1)
            (setq $elEndP2 $p2)
            (goto-char (1+ $p1))
            (sgml-skip-tag-backward 1)
            (setq $elStartP1 (point))
            (search-forward ">" )
            (setq $elStartP2 (point))
            (setq $closeTagStr (buffer-substring $elEndP1 $elEndP2))
            (setq $openTagStr (buffer-substring $elStartP1 $elStartP2))
            (message "Deleted: %s%s" $openTagStr $closeTagStr)
            (delete-region $elEndP1 $elEndP2)
            (delete-region $elStartP1 $elStartP2))
        (progn
          (setq $isSelfCloseTag (char-equal (char-after (- $p2 2)) ?/))
          (if $isSelfCloseTag
              (progn
                (setq $openTagStr (buffer-substring $p1 $p2))
                (message "Deleted:\n%s" $openTagStr )
                (delete-region $p1 $p2))
            (progn
              (setq $elStartP1 $p1)
              (setq $elStartP2 $p2)
              (goto-char (1+ $p1))
              (sgml-skip-tag-forward 1)
              (setq $elEndP2 (point))
              (search-backward "<" )
              (setq $elEndP1 (point))
              (setq $closeTagStr (buffer-substring $elEndP1 $elEndP2))
              (setq $openTagStr (buffer-substring $elStartP1 $elStartP2))
              (message "Deleted:%s%s" $openTagStr $closeTagStr)
              (delete-region $elEndP1 $elEndP2)
              (delete-region $elStartP1 $elStartP2)))))))
  (set-transient-map (let (($kmap (make-sparse-keymap))) (define-key $kmap (kbd "t") 'xah-html-remove-tag-pair ) $kmap)))

HTML