Subject: [PATCH] Use region as suggested URI in eww by default

From: Manuel Giraud <[email-masked]>
Date: Fri, 14 Mar 2025 18:13:18 +0100

Tags: patch

Hi,

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?

In GNU Emacs 31.0.50 (build 38, x86_64-unknown-openbsd7.7, X toolkit) of
 2025-03-14 built on computer
Repository revision: 1437fc2bca2e42cd2a28754bbb4d9ac1403b87b7
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12101016
System Description: OpenBSD computer 7.7 GENERIC.MP#593 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 49ca25f051ea4d87542204049e0eff25ea6bdbed 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 by default

* lisp/net/eww.el (eww-region): New function to extract the
selected region.
(eww-suggest-uris): Use it first as suggested URI.
---
 lisp/net/eww.el | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/lisp/net/eww.el b/lisp/net/eww.el
index caee50a712f..02583da4399 100644
--- a/lisp/net/eww.el
+++ b/lisp/net/eww.el
@@ -104,7 +104,8 @@ eww-download-directory
 
 ;;;###autoload
 (defcustom eww-suggest-uris
-  '(eww-links-at-point
+  '(eww-region
+    eww-links-at-point
     thing-at-point-url-at-point
     eww-current-url
     eww-bookmark-urls)
@@ -1106,6 +1107,11 @@ eww-current-url
   "Return URI of the Web page the current EWW buffer is visiting."
   (plist-get eww-data :url))
 
+(defun eww-region ()
+  "Return the current region content if there is one."
+  (when (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