Subject: bug#77967: [PATCH] Fallback `eww-switch-to-buffer' to `eww'

From: Manuel Giraud <[email-masked]>
Date: Sun, 27 Apr 2025 13:08:57 +0200

Eli Zaretskii <[email-masked]> writes:

>> Cc: Lars Magne Ingebrigtsen <[email-masked]>
>> From: Manuel Giraud <[email-masked]>
>> Date: Mon, 21 Apr 2025 19:43:47 +0200
>> 
>> The following patch calls `eww' interactively from
>> `eww-switch-to-buffer' when there is no EWW buffers.  Then, one can set
>> a global shortcut on `eww-switch-to-buffer' and always be able to launch
>> EWW.
>
> Thanks.
>
>>  (defun eww-switch-to-buffer ()
>> -  "Prompt for an EWW buffer to display in the selected window."
>> +  "Prompt for an EWW buffer to display in the selected window.  If no such
>> +buffer exist, fallback to calling `eww'."
>
> The first line of a doc string should be a single complete sentence.
>
> Doesn't this change warrant a NEWS entry, and a suitable change to the
> EWW manual?

Thanks.  Here is a new patch.  While here, I also modified
`eww-list-buffers' to not pop to *eww-buffers* when there is no such
buffer.
>From f8235beab112226cbe8bf43e30fcb31935824911 Mon Sep 17 00:00:00 2001
From: Manuel Giraud <[email-masked]>
Date: Mon, 21 Apr 2025 19:01:25 +0200
Subject: [PATCH] Fallback `eww-switch-to-buffer' to `eww'

* lisp/net/eww.el (eww-switch-to-buffer): When no EWW buffers
exist, call `eww' instead.
(eww-list-buffers): Do not pop to *eww buffers* when there is no
EWW buffers.
---
 doc/misc/eww.texi |  3 +++
 etc/NEWS          |  5 +++++
 lisp/net/eww.el   | 38 ++++++++++++++++++++++----------------
 3 files changed, 30 insertions(+), 16 deletions(-)

diff --git a/doc/misc/eww.texi b/doc/misc/eww.texi
index 5be59972811..d092fc1f230 100644
--- a/doc/misc/eww.texi
+++ b/doc/misc/eww.texi
@@ -259,6 +259,9 @@ Basics
 switch EWW buffers through a minibuffer prompt, press @kbd{s}
 (@code{eww-switch-to-buffer}).
 
+The @code{eww-switch-to-buffer} command will fallback to @code{eww}
+when there is no EWW buffers.
+
 @findex eww-browse-with-external-browser
 @vindex browse-url-secondary-browser-function
 @vindex eww-use-external-browser-for-content-type
diff --git a/etc/NEWS b/etc/NEWS
index 56a45068daf..787ab606276 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1214,6 +1214,11 @@ content-type of Web pages which don't have a valid 'Content-Type'
 header.  The default value is a function that considers a page with an
 HTML 'doctype' declaration to have context-type "text/html".
 
++++
+*** 'eww-switch-to-buffer' falls back to 'eww'.
+When there is no EWW buffers, 'eww-switch-to-buffer' falls back to
+calling 'eww'.
+
 ** CC mode
 
 +++
diff --git a/lisp/net/eww.el b/lisp/net/eww.el
index 7edd1acbcf9..c11e378c714 100644
--- a/lisp/net/eww.el
+++ b/lisp/net/eww.el
@@ -2321,25 +2321,29 @@ eww-set-character-encoding
     (eww-reload nil charset)))
 
 (defun eww-switch-to-buffer ()
-  "Prompt for an EWW buffer to display in the selected window."
+  "Prompt for an EWW buffer to display in the selected window.
+If no such buffer exist, fallback to calling `eww'."
   (interactive nil eww-mode)
   (let ((completion-extra-properties
          `(:annotation-function
            ,(lambda (buf)
               (with-current-buffer buf
                 (format " %s" (eww-current-url))))))
-        (curbuf (current-buffer)))
-    (pop-to-buffer-same-window
-     (read-buffer "Switch to EWW buffer: "
-                  (cl-loop for buf in (nreverse (buffer-list))
+        (curbuf (current-buffer))
+        (list (cl-loop for buf in (nreverse (buffer-list))
                            if (with-current-buffer buf (derived-mode-p 'eww-mode))
-                           return buf)
-                  t
-                  (lambda (bufn)
-                    (setq bufn (if (consp bufn) (cdr bufn) (get-buffer bufn)))
-                    (and (with-current-buffer bufn
-                           (derived-mode-p 'eww-mode))
-                         (not (eq bufn curbuf))))))))
+                           return buf)))
+    (if list
+        (pop-to-buffer-same-window
+         (read-buffer "Switch to EWW buffer: "
+                      list
+                      t
+                      (lambda (bufn)
+                        (setq bufn (if (consp bufn) (cdr bufn) (get-buffer bufn)))
+                        (and (with-current-buffer bufn
+                               (derived-mode-p 'eww-mode))
+                             (not (eq bufn curbuf))))))
+      (call-interactively 'eww))))
 
 (defun eww-toggle-fonts ()
   "Toggle whether to use monospaced or font-enabled layouts."
@@ -2718,10 +2722,12 @@ eww-buffer-list
 (defun eww-list-buffers ()
   "Pop a buffer with a list of eww buffers."
   (interactive)
-  (with-current-buffer (get-buffer-create "*eww buffers*")
-    (eww-buffers-mode)
-    (eww--list-buffers-display-table))
-  (pop-to-buffer "*eww buffers*"))
+  (if (null (eww-buffer-list))
+      (message "No EWW buffers.")
+    (with-current-buffer (get-buffer-create "*eww buffers*")
+      (eww-buffers-mode)
+      (eww--list-buffers-display-table))
+    (pop-to-buffer "*eww buffers*")))
 
 (defun eww--list-buffers-display-table (&optional _ignore-auto _noconfirm)
   "Display a table with the list of eww buffers.
-- 
2.49.0

-- 
Manuel Giraud