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

From: Manuel Giraud <[email-masked]>
Date: Mon, 21 Apr 2025 19:43:47 +0200

Tags: patch

Hi,

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,

In GNU Emacs 31.0.50 (build 5, x86_64-unknown-openbsd7.7, X toolkit) of
 2025-04-20 built on computer
Repository revision: b38fd553eb3291f140801ca158f6ab245496fa69
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12101016
System Description: OpenBSD computer 7.7 GENERIC.MP#625 amd64

Configured using:
 'configure CC=egcc CPPFLAGS=-I/usr/local/include
 LDFLAGS=-L/usr/local/lib MAKEINFO=gmakeinfo --prefix=/home/manuel/emacs
 --bindir=/home/manuel/bin --with-x-toolkit=lucid
 --with-toolkit-scroll-bars=no --without-cairo
 --without-compress-install'

>From d63d4cacdd6bf9b9cbfff43cb07d3268f969bd25 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.
---
 lisp/net/eww.el | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)

diff --git a/lisp/net/eww.el b/lisp/net/eww.el
index 7edd1acbcf9..63425882583 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."
-- 
2.49.0

-- 
Manuel Giraud