Emacs Lisp: HTML, Update Title

By Xah Lee. Date: . Last updated: .

Here's a command to update current HTML file's title.

(defun xah-html-update-title (@title)
  "Update the <title>…</title> of current buffer.
URL `http://xahlee.info/emacs/emacs/elisp_update-html-title.html'
Version 2019-01-11 2021-02-09 2021-07-31"
  (interactive
   (let ($oldTitle)
     (save-excursion
       (goto-char (point-min))
       (re-search-forward "<title>\\([^<]+?\\)</title>")
       (setq $oldTitle (match-string 1 )))
     (list (read-string "New title:" $oldTitle nil $oldTitle "INHERIT-INPUT-METHOD"))))
  (let ($p1 $p2)
    (save-excursion
      (goto-char (point-min))
      (search-forward "<title>")
      (setq $p1 (point))
      (search-forward "</title>")
      (setq $p2 (- (point) (length "</title>")))
      (delete-region $p1 $p2 )
      (goto-char $p1)
      (insert @title ))))
(defun xah-html-update-first-h1 (@h1Text)
  "Update the first <h1>…</h1> of current buffer.
When called in elisp code, @h1Text is new title, a string.
URL `http://xahlee.info/emacs/emacs/elisp_update-html-title.html'
Version 2019-01-11 2021-07-31"
  (interactive
   (let ($oldTitle)
     (save-excursion
       (goto-char (point-min))
       (re-search-forward "<h1>\\([^<]+?\\)</h1>")
       (setq $oldTitle (match-string 1 )))
     (list (read-string "New title:" $oldTitle nil $oldTitle "INHERIT-INPUT-METHOD"))))
  (let ($p1 $p2)
    (save-excursion
      (goto-char (point-min))
      (when (search-forward "<h1>")
        (setq $p1 (point))
        (search-forward "</h1>")
        (setq $p2 (- (point) (length "</h1>")))
        (delete-region $p1 $p2 )
        (goto-char $p1)
        (insert @h1Text )))))
(defun xah-html-update-title-h1 (@title)
  "Update the <title>…</title> and first <h1>…</h1> of current buffer.
When called in elisp code, @title is new title, a string.
URL `http://xahlee.info/emacs/emacs/elisp_update-html-title.html'
Version 2019-01-11 2021-07-31"
  (interactive
   (let ($oldTitle)
     (save-excursion
       (goto-char (point-min))
       (re-search-forward "<title>\\([^<]+?\\)</title>")
       (setq $oldTitle (match-string 1 )))
     (list (read-string "New title:" $oldTitle nil $oldTitle "INHERIT-INPUT-METHOD"))))
  (progn
    (xah-html-update-title @title)
    (xah-html-update-first-h1 @title)))

HTML