Subject: Re: bug#51176: eww switch buffer by title

From: Protesilaos Stavrou <[email-masked]>
Date: Fri, 15 Oct 2021 09:31:55 +0300

On 2021-10-13, 22:33 +0200, Lars Ingebrigtsen <[email-masked]> wrote:

> Protesilaos Stavrou <[email-masked]> writes:
>
>> In terms of user options, there would be a toggle to enable the
>> behaviour (off by default to not upset existing users) as well as a
>> defcustom with ":type 'function", so users can specify exactly how the
>> renaming scheme should work.
>
> Sounds good to me.

Hello again!

Please find attached the patch which implements a renaming scheme for
EWW buffers.  I include Abhiseck, the co-author of this patch, in Cc.

What do you think?

We had entertained the notion of a single defcustom with more options,
but ultimately decided against it on the grounds that it was more
complex.

-- 
Protesilaos Stavrou
https://protesilaos.com
>From add8dabc4f3f33707f069865794ec53b2e0e4037 Mon Sep 17 00:00:00 2001
Message-Id: <[email-masked]>
From: Protesilaos Stavrou <[email-masked]>
Date: Fri, 15 Oct 2021 09:24:04 +0300
Subject: [PATCH] Implement auto-renaming scheme for eww buffers

* etc/NEWS: Document the new feature.

* lisp/net/eww.el (eww-auto-rename-buffer-function): Introduce user
option to specify the function which performs the renaming.
(eww-auto-rename-buffer-flag): Add toggle to control the behaviour,
keeping it off by default in order not to surprise existing users.
(eww--rename-buffer): Write function to rename buffers.
(eww-render): Call 'eww--rename-buffer' at the post-render phase, if
appropriate.

Fix bug#51176.

Co-authored-by: Abhiseck Paira <[email-masked]>
Co-authored-by: Protesilaos Stavrou <[email-masked]>
---
 etc/NEWS        | 11 +++++++++++
 lisp/net/eww.el | 26 ++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)

diff --git a/etc/NEWS b/etc/NEWS
index 7dd4d14274..54d554ee0b 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -89,6 +89,17 @@ Customize this option to limit the amount of entries in the menu
 
 * Changes in Specialized Modes and Packages in Emacs 29.1
 
+** eww
+
+---
+*** New user option to automatically rename EWW buffers
+When 'eww-auto-rename-buffer-flag' is set to a non-nil value, all
+rendered web pages will have their buffer renamed from the generic
+"*eww*" to a name that includes the web page's title or, if that is
+absent, its URL.  The renaming scheme is controlled by the user option
+'eww-auto-rename-buffer-function'.  By default, no automatic renaming
+is performed.
+
 ** image-dired
 
 ---
diff --git a/lisp/net/eww.el b/lisp/net/eww.el
index 24c6335210..2104d09696 100644
--- a/lisp/net/eww.el
+++ b/lisp/net/eww.el
@@ -178,6 +178,21 @@ (defcustom eww-after-render-hook nil
   :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-flag' is non-nil."
+  :version "29.1"
+  :group 'eww
+  :type 'function)
+
+(defcustom eww-auto-rename-buffer-flag 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'."
@@ -502,6 +517,15 @@ (defun eww-html-p (content-type)
   (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
@@ -556,6 +580,8 @@ (defun eww-render (status url &optional point buffer encode)
 	    (setq eww-history-position 0)
 	    (and last-coding-system-used
 		 (set-buffer-file-coding-system last-coding-system-used))
+            (and eww-auto-rename-buffer-flag
+                 (funcall eww-auto-rename-buffer-function))
 	    (run-hooks 'eww-after-render-hook)))
       (kill-buffer data-buffer))))
 
-- 
2.33.0