From 6bd3f685b4602aa478b5e987899e7215af20f25e Mon Sep 17 00:00:00 2001 Message-Id: <6bd3f685b4602aa478b5e987899e7215af20f25e.1634295379.git.info@protesilaos.com> From: Protesilaos Stavrou Date: Fri, 15 Oct 2021 13:55:44 +0300 Subject: [PATCH] Implement auto-renaming scheme for EWW buffers * etc/NEWS: Document the new user options. * lisp/net/eww.el (eww-auto-rename-buffer-function) (eww-auto-rename-buffer): Add user options. (eww--rename-buffer): Introduce new function that performs the renaming of buffers. (eww--after-page-change): Add new wrapper function which calls 'eww-update-header-line-format' and 'eww-auto-rename-buffer-function'. (eww, eww-render, eww-tag-title, eww-readable, eww-restore-history): Include eww--after-page-change. Fix bug#51176. Co-authored-by: Abhiseck Paira Co-authored-by: Protesilaos Stavrou --- etc/NEWS | 8 ++++++++ lisp/net/eww.el | 39 ++++++++++++++++++++++++++++++++++----- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 7dd4d14274..906ff4e6ae 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -89,6 +89,14 @@ * Changes in Specialized Modes and Packages in Emacs 29.1 +** eww + +--- +*** New user option to automatically rename EWW buffers +When 'eww-auto-rename-buffer' is set to a non-nil value, it will make +all buffers use either the title or the URL of the rendered web page. +The rendering scheme is handled by 'eww-auto-rename-buffer-function'. + ** image-dired --- diff --git a/lisp/net/eww.el b/lisp/net/eww.el index 24c6335210..14cf207e59 100644 --- a/lisp/net/eww.el +++ b/lisp/net/eww.el @@ -178,6 +178,21 @@ :group 'eww :type 'hook) +(defcustom eww-auto-rename-buffer-function #'eww--rename-buffer + "Function to rename EWW buffer. +The function is called just before the `eww-after-render-hook' if +`eww-auto-rename-buffer' is non-nil." + :version "29.1" + :group 'eww + :type 'function) + +(defcustom eww-auto-rename-buffer nil + "When non-nil rename EWW buffers after they are rendered. +Renaming is controlled by `eww-auto-rename-buffer-function'." + :version "29.1" + :group 'eww + :type 'boolean) + (defcustom eww-form-checkbox-selected-symbol "[X]" "Symbol used to represent a selected checkbox. See also `eww-form-checkbox-symbol'." @@ -353,7 +368,7 @@ (setq url (url-recreate-url parsed))) (plist-put eww-data :url url) (plist-put eww-data :title "") - (eww-update-header-line-format) + (eww--after-page-change) (let ((inhibit-read-only t)) (insert (format "Loading %s..." url)) (goto-char (point-min))) @@ -502,6 +517,15 @@ (member content-type '("text/html" "application/xhtml+xml"))) +(defun eww--rename-buffer () + "Rename the current EWW buffer. +Use the page's title or URL as an identifier." + (when (eq major-mode 'eww-mode) + (when-let ((title (or (plist-get eww-data :title) + (plist-get eww-data :url)))) + (rename-buffer + (format "*%s # eww*" (truncate-string-to-width title 40)) t)))) + (defun eww-render (status url &optional point buffer encode) (let* ((headers (eww-parse-headers)) (content-type @@ -552,7 +576,7 @@ (eww-display-raw buffer (or encode charset 'utf-8)))) (with-current-buffer buffer (plist-put eww-data :url url) - (eww-update-header-line-format) + (eww--after-page-change) (setq eww-history-position 0) (and last-coding-system-used (set-buffer-file-coding-system last-coding-system-used)) @@ -796,12 +820,17 @@ `((?u . ,(or url "")) (?t . ,title)))))))) +(defun eww--after-page-change () + (eww-update-header-line-format) + (when eww-auto-rename-buffer + (funcall eww-auto-rename-buffer-function))) + (defun eww-tag-title (dom) (plist-put eww-data :title (replace-regexp-in-string "^ \\| $" "" (replace-regexp-in-string "[ \t\r\n]+" " " (dom-text dom)))) - (eww-update-header-line-format)) + (eww--after-page-change)) (defun eww-display-raw (buffer &optional encode) (let ((data (buffer-substring (point) (point-max)))) @@ -929,7 +958,7 @@ nil (current-buffer)) (dolist (elem '(:source :url :title :next :previous :up)) (plist-put eww-data elem (plist-get old-data elem))) - (eww-update-header-line-format))) + (eww--after-page-change))) (defun eww-score-readability (node) (let ((score -1)) @@ -1161,7 +1190,7 @@ (goto-char (plist-get elem :point)) ;; Make buffer listings more informative. (setq list-buffers-directory (plist-get elem :url)) - (eww-update-header-line-format)))) + (eww--after-page-change)))) (defun eww-next-url () "Go to the page marked `next'. -- 2.33.0