Subject: Re: bug#77016: [PATCH] Use region as suggested URI in eww by default

From: Manuel Giraud <[email-masked]>
Date: Sat, 15 Mar 2025 15:55:42 +0100

Stefan Kangas <[email-masked]> writes:

> Manuel Giraud <[email-masked]> writes:
>
>> Here is a patch for eww.  It adds the current region on top of the
>> suggested URI.  This way when reading a page in eww, one can select a
>> region and hit "M-RET" to open a new window (or tab) with a web search
>> for this selection.  WDYT?
>
> This is a change in behavior, and I can see that it would be useful to
> some users.  However, there are many reasons why one would _not_ want
> this behavior by default, and it feels risky to have this level of DWIM
> by default.
>
> I think it should be optional and/or behind a prompt.
>
> This should be documented in NEWS.

Thanks for your review.  What about this new one?  I'm not quite sure
about the 'deactivate-mark' (and its place) but if I don't do this a
"wrong" region is active in the new "web search" buffer.

>From 32f220030e5a46c11816f43001272a620e8856e5 Mon Sep 17 00:00:00 2001
From: Manuel Giraud <[email-masked]>
Date: Fri, 14 Mar 2025 18:03:20 +0100
Subject: [PATCH] Use region as suggested URI in EWW

* lisp/net/eww.el (eww-current-region): New function to extract
the current region.
(eww-dwim-region): New user option to guard it.
(eww-suggest-uris): Use 'eww-current-region' first as suggested
URI.
(eww--open-url-in-new-buffer): Deactivate the mark in the cloned
buffer.
* etc/NEWS: Annouce user option.
---
 etc/NEWS        | 10 ++++++++--
 lisp/net/eww.el | 18 +++++++++++++++++-
 2 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 7467af6055c..539d1001564 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -363,8 +363,6 @@ invoked standalone or from the 'project-switch-commands' dispatch menu.
 This user option describes projects that should always be skipped by
 'project-remember-project'.
 
-***
-
 ** Registers
 
 *** New functions 'buffer-to-register' and 'file-to-register'.
@@ -373,6 +371,14 @@ Killed buffers stored in a register using 'buffer-to-register' are
 automatically converted to a file-query value if the buffer was visiting
 a file.
 
+** EWW
+
+---
+*** New user option 'eww-dwim-region'.
+If non-nil, the user selected region is used as the default suggested
+URI by EWW.  For example, this would trigger a web search when used with
+'M-RET'.  By default this option is nil.
+
 ** IDLWAVE has been moved to GNU ELPA.
 The version bundled with Emacs is out-of-date, and is now marked as
 obsolete.  Use 'M-x list-packages' to install the 'idlwave' package from
diff --git a/lisp/net/eww.el b/lisp/net/eww.el
index caee50a712f..d64acf37c5b 100644
--- a/lisp/net/eww.el
+++ b/lisp/net/eww.el
@@ -102,9 +102,18 @@ eww-download-directory
   :group 'eww
   :type '(choice directory function))
 
+(defcustom eww-dwim-region nil
+  "When non-nil, the current region is used by `eww' as the default
+suggested URI.  This allows to trigger a web search on the user
+selection."
+  :version "31.1"
+  :group 'eww
+  :type 'boolean)
+
 ;;;###autoload
 (defcustom eww-suggest-uris
-  '(eww-links-at-point
+  '(eww-current-region
+    eww-links-at-point
     thing-at-point-url-at-point
     eww-current-url
     eww-bookmark-urls)
@@ -634,6 +643,7 @@ eww--open-url-in-new-buffer
       (if (eq major-mode 'eww-mode) (clone-buffer)
         (generate-new-buffer "*eww*"))
     (unless (equal url (eww-current-url))
+      (deactivate-mark)
       (eww-mode)
       (eww (if (consp url) (car url) url)))))
 
@@ -1106,6 +1116,12 @@ eww-current-url
   "Return URI of the Web page the current EWW buffer is visiting."
   (plist-get eww-data :url))
 
+(defun eww-current-region ()
+  "Return the current region content if there is one and the user option
+`eww-dwim-region' is non-nil."
+  (when (and eww-dwim-region (use-region-p))
+    (buffer-substring (region-beginning) (region-end))))
+
 (defun eww-links-at-point ()
   "Return list of URIs, if any, linked at point."
   (seq-filter #'stringp
-- 
2.48.1

-- 
Manuel Giraud