Subject: bug#52973: Adding a few context-menu-mode commands

From: Philip Kaludercic <[email-masked]>
Date: Thu, 06 Jan 2022 18:59:44 +0000

Juri Linkov <[email-masked]> writes:

>>> Maybe then a better place to add new functions would be in
>>> lisp/net/webjump.el.  webjump is a package created exactly
>>> for this purpose.  Then new functions could share the same
>>> prefix webjump-.
>>
>> I didn't know about webjump, thank you for the hint.  My only fear is
>> that the prefix would make the functionality harder to discover.
>
> The fact that its name starts with the word "web" improves
> discoverability.

How is this?

>From 2a32d1f7df0b8dfdceb3f63476df150d55215b5d Mon Sep 17 00:00:00 2001
From: Philip Kaludercic <[email-masked]>
Date: Tue, 4 Jan 2022 20:36:32 +0100
Subject: [PATCH] Add search engine functionality

* lisp/net/eww.el (eww-search-prefix): Deprecate user option
(eww): Use webjump-search-query
* lisp/net/webjump.el (webjump-default-search-engine): Add user option
(webjump-choose-search-engine): Add function
(webjump-search-query): Add function
(webjump-search): Add autoloaded command
(webjump-search-at-point): Add autoloaded command
(webjump-search-at-mouse): Add command
(webjump-search-context-menu): Add function for context-menu support.
---
 lisp/misc.el        |  1 +
 lisp/net/eww.el     | 10 +++---
 lisp/net/webjump.el | 80 ++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 86 insertions(+), 5 deletions(-)

diff --git a/lisp/misc.el b/lisp/misc.el
index 39ec9497d7..6e5aede789 100644
--- a/lisp/misc.el
+++ b/lisp/misc.el
@@ -27,6 +27,7 @@
 
 (eval-when-compile
   (require 'tabulated-list))
+(require 'cl-lib)
 
 ;;;###autoload
 (defun copy-from-above-command (&optional arg)
diff --git a/lisp/net/eww.el b/lisp/net/eww.el
index 8930eb427d..2ef6b35100 100644
--- a/lisp/net/eww.el
+++ b/lisp/net/eww.el
@@ -33,6 +33,7 @@
 (require 'url)
 (require 'url-queue)
 (require 'xdg)
+(require 'webjump)
 (eval-when-compile (require 'subr-x))
 
 (defgroup eww nil
@@ -55,6 +56,9 @@ eww-search-prefix
   :version "24.4"
   :group 'eww
   :type 'string)
+(make-obsolete-variable 'eww-search-prefix
+                        "webjump-default-search-engine"
+                        "29.1")
 
 (defcustom eww-use-browse-url "\\`mailto:"
   "EWW will use `browse-url' when following links that match this regexp.
@@ -355,7 +359,7 @@ eww-browse
 (defun eww (url &optional new-buffer buffer)
   "Fetch URL and render the page.
 If the input doesn't look like an URL or a domain name, the
-word(s) will be searched for via `eww-search-prefix'.
+word(s) will be searched for via `webjump-search'.
 
 If NEW-BUFFER is non-nil (interactively, the prefix arg), use a
 new buffer instead of reusing the default EWW buffer.
@@ -464,9 +468,7 @@ eww--dwim-expand-url
                ;; Some sites do not redirect final /
                (when (string= (url-filename (url-generic-parse-url url)) "")
                  (setq url (concat url "/"))))
-           (setq url (concat eww-search-prefix
-                             (mapconcat
-                              #'url-hexify-string (split-string url) "+"))))))
+           (setq url (webjump-search-query url)))))
   url)
 
 (defun eww--preprocess-html (start end)
diff --git a/lisp/net/webjump.el b/lisp/net/webjump.el
index 7547f92d7d..17c9312c54 100644
--- a/lisp/net/webjump.el
+++ b/lisp/net/webjump.el
@@ -370,7 +370,85 @@ webjump-url-fix-trailing-slash
       (concat url "/")
     url))
 
-;;-----------------------------------------------------------------------------
+
+;;; Search engine functionality
+
+(defcustom webjump-default-search-engine "DuckDuckGo"
+  "Search engine to use for `webjump-search' and related commands.
+The string should denote an entry in `webjump-sites', with a
+`simple-query' entry."
+  :type 'string)
+
+(defun webjump-choose-search-engine ()
+  "Choose a search engine to use from `webjump-sites'.
+If no prefix argument was used, the function will return the
+entry for `webjump-default-search-engine'."
+  (assoc-string
+   (if current-prefix-arg
+       (completing-read
+        (format-prompt "Use search engine"
+                       webjump-default-search-engine)
+        webjump-sites
+        (lambda (ent)
+          (and (vectorp (cdr ent))
+               (eq (aref (cdr ent) 0) 'simple-query)))
+        t nil nil webjump-default-search-engine)
+     webjump-default-search-engine)
+   webjump-sites t))
+
+(defun webjump-search-query (string)
+  "Generate an URL query to search for STRING."
+  (let ((engine (cdr (webjump-choose-search-engine))))
+    (webjump-url-fix
+     (concat (aref engine 2)
+             (webjump-url-encode string)
+             (aref engine 3)))))
+
+;;;###autoload
+(defun webjump-search (string)
+  "Search for STRING using a search engine.
+If the optional argument ENGINE may be used to override
+`webjump-default-search-engine'."
+  (interactive (list (if (use-region-p)
+                         (buffer-substring (region-beginning) (region-end))
+                       (read-string "Search: "))))
+  (browse-url (webjump-search-query string)))
+
+;;;###autoload
+(defun webjump-search-at-point ()
+  "Search for symbol at point online.
+See `webjump-search' for more details."
+  (interactive)
+  (let ((query (thing-at-point 'symbol)))
+    (unless query
+      (user-error "Nothing to search for"))
+    (webjump-search query)))
+
+;;;###autoload
+(defun webjump-search-at-mouse (click)
+  "Query an online search engine at CLICK.
+If a region is active, the entire region will be sent, otherwise
+the symbol at point will be used.  See `webjump-search' for more
+details."
+  (interactive "e")
+  (let ((query (if (use-region-p)
+                   (buffer-substring (region-beginning)
+                                     (region-end))
+                 (thing-at-mouse click 'symbol))))
+    (unless (or query (string-match-p "\\`[[:space:]\n]*\\'" query))
+      (user-error "Nothing to search for"))
+    (webjump-search query)))
+
+;;;###autoload
+(defun webjump-search-context-menu (menu click)
+  "Populate MENU with command to search online at CLICK."
+  (save-excursion
+    (mouse-set-point click)
+    (define-key-after menu [webjump-search-separator] menu-bar-separator)
+    (define-key-after menu [webjump-search-at-mouse]
+      '(menu-item "Online search" webjump-search-at-mouse
+                  :help "Search for region or word online")))
+  menu)
 
 (provide 'webjump)
 
-- 
2.34.0

While I agree that web helps, I think a search-online defalias for
webjump-search would be appreciated when looking for commands via
apropos.

-- 
	Philip Kaludercic