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

From: Lin Sun <[email-masked]>
Date: Fri, 18 Aug 2023 19:03:08 +0000

On Fri, Aug 18, 2023 at 5:52 AM Eli Zaretskii <[email-masked]> wrote:
> This changes the API of a public function in incompatible ways, which
> we try very hard not to do.  Instead, the signature or
> eww-open-in-new-buffer should be like this:
>
>   (defun eww-open-in-new-buffer (&optional no-select)
>
> and the new NO-SELECT argument should be set non-nil by C-u.
>
> > +    (user-error "No avaliable link")))
>
> This also changes the text of the error message in this case.  Was
> that really necessary?  The original text was more accurate, I think.
>
> Also, this new feature needs a NEWS entry and a suitable addition to
> the EWW manual.

The attached patch included changes for the function, error message,
NEWS and EWW manual.
Please help review it.

Thanks. Regards
From 2fb95d761d4d53e8b34253d89fd06ea267f3eb0d 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

---
 doc/misc/eww.texi |  3 ++-
 etc/NEWS          |  5 +++++
 lisp/net/eww.el   | 49 ++++++++++++++++++++++++++++++-----------------
 3 files changed, 38 insertions(+), 19 deletions(-)

diff --git a/doc/misc/eww.texi b/doc/misc/eww.texi
index b67624af9f..2181355a57 100644
--- a/doc/misc/eww.texi
+++ b/doc/misc/eww.texi
@@ -137,7 +137,8 @@ Basics
 ``tab'' in other browsers.  When @code{global-tab-line-mode} is
 enabled, this buffer is displayed in the tab on the window tab line.
 When @code{tab-bar-mode} is enabled, a new tab is created on the frame
-tab bar.
+tab bar.  If the prefix key @kbd{C-u} is avaliable, it will stay on
+current buffer.
 
 @findex eww-readable
 @kindex R
diff --git a/etc/NEWS b/etc/NEWS
index 808b599672..13aedee2d5 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -452,6 +452,11 @@ the kill ring.  Alternate links are optional metadata that HTML pages
 use for linking to their alternative representations, such as
 translated versions or associated RSS feeds.
 
++++
+*** 'eww-open-in-new-buffer' support prefix key "C-u" to stay current buffer.
+The command accept the prefix key "C-u" to open the url in a new
+buffer but stay in current buffer, won't jump to the new buffer.
+
 ** go-ts-mode
 
 +++
diff --git a/lisp/net/eww.el b/lisp/net/eww.el
index cb73926f46..f2ed60f172 100644
--- a/lisp/net/eww.el
+++ b/lisp/net/eww.el
@@ -542,24 +542,37 @@ 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 (&optional no-select url)
+  "Fetch URL in a new EWW buffer.
+
+If the NO-SELECT 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 no-select
+              (tab-bar-switch-to-prev-tab)))
+        (if no-select
+            (save-window-excursion (eww--open-url-in-new-buffer url))
+          (eww--open-url-in-new-buffer url)))
+    (user-error "No link at point")))
 
 (defun eww-html-p (content-type)
   "Return non-nil if CONTENT-TYPE designates an HTML content type.
-- 
2.20.5