Subject: bug#65346: 30.0.50; *lisp/net/eww.el: new function 'eww-open-in-new-buffer-background'

From: Lin Sun <[email-masked]>
Date: Thu, 17 Aug 2023 22:51:04 +0000

Hi Eli,
> "Background" is a problematic word for this, since that's not what you
> mean: you mean not to select the window displaying the URL.
>
> And I don't think we need a separate command for that: how about doing
> this in eww-open-in-new-buffer instead, if the user invokes it with a
> prefix argument C-u?
>
> And finally, what happens if the following condition, tested by
> eww-open-in-new-buffer, is fulfilled:
>
>       (when (or (eq eww-browse-url-new-window-is-tab t)
>                 (and (eq eww-browse-url-new-window-is-tab 'tab-bar)
>                      tab-bar-mode))
>
> In this case, AFAIU the URL is opened in a new tab.

Thank you for the comment, and agree with you, so I rewrote the
function `eww-open-in-new-buffer' with "C-u" support (also with an
optional argument "url"), then there is no "background" description.

I also tested the new patch with tab-bar-mode on and off, it works for
both scenarios.

Please help review again. Thanks.

Best regards
Lin
From 317019bcc1f659bf71dc20c1998dc8b4623b0fe1 Mon Sep 17 00:00:00 2001
From: Lin Sun <[email-masked]>
Date: Wed, 16 Aug 2023 01:00:07 +0000
Subject: [PATCH] *lisp/net/eww.el: `eww-open-in-new-buffer' able to stay on
 current buffer

---
 lisp/net/eww.el | 50 +++++++++++++++++++++++++++++++------------------
 1 file changed, 32 insertions(+), 18 deletions(-)

diff --git a/lisp/net/eww.el b/lisp/net/eww.el
index cb73926f46..23b4fe3117 100644
--- a/lisp/net/eww.el
+++ b/lisp/net/eww.el
@@ -542,24 +542,38 @@ eww-search-words
           (call-interactively #'eww)))
     (call-interactively #'eww)))
 
-(defun eww-open-in-new-buffer ()
-  "Fetch link at point in a new EWW buffer."
-  (interactive)
-  (let ((url (eww-suggested-uris)))
-    (if (null url) (user-error "No link at point")
-      (when (or (eq eww-browse-url-new-window-is-tab t)
-                (and (eq eww-browse-url-new-window-is-tab 'tab-bar)
-                     tab-bar-mode))
-        (let ((tab-bar-new-tab-choice t))
-          (tab-new)))
-      ;; clone useful to keep history, but
-      ;; should not clone from non-eww buffer
-      (with-current-buffer
-          (if (eq major-mode 'eww-mode) (clone-buffer)
-            (generate-new-buffer "*eww*"))
-        (unless (equal url (eww-current-url))
-          (eww-mode)
-          (eww (if (consp url) (car url) url)))))))
+(defun eww--open-url-in-new-buffer (url)
+  "Open the URL in a new EWW buffer."
+  ;; clone useful to keep history, but
+  ;; should not clone from non-eww buffer
+  (with-current-buffer
+      (if (eq major-mode 'eww-mode) (clone-buffer)
+        (generate-new-buffer "*eww*"))
+    (unless (equal url (eww-current-url))
+      (eww-mode)
+      (eww (if (consp url) (car url) url)))))
+
+(defun eww-open-in-new-buffer (stay &optional url)
+  "Fetch URL in a new EWW buffer.
+
+If the STAY is not `nil', the forcus will stay on current buffer.
+
+If the URL is `nil', it will try `eww-suggested-uris' under current cursor."
+  (interactive "P")
+  (if-let ((url (or url (eww-suggested-uris))))
+      (if (or (eq eww-browse-url-new-window-is-tab t)
+              (and (eq eww-browse-url-new-window-is-tab 'tab-bar)
+                   tab-bar-mode))
+          (let ((tab-bar-new-tab-choice t))
+            (tab-new)
+            (eww--open-url-in-new-buffer url)
+            (when stay
+              (tab-bar-switch-to-prev-tab)))
+
+        (if stay
+            (save-window-excursion (eww--open-url-in-new-buffer url))
+          (eww--open-url-in-new-buffer url)))
+    (user-error "No avaliable link")))
 
 (defun eww-html-p (content-type)
   "Return non-nil if CONTENT-TYPE designates an HTML content type.
-- 
2.20.5