unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Philip Kaludercic <philipk@posteo.net>
To: Juri Linkov <juri@linkov.net>
Cc: 52973@debbugs.gnu.org
Subject: bug#52973: Adding a few context-menu-mode commands
Date: Tue, 04 Jan 2022 19:39:48 +0000	[thread overview]
Message-ID: <87czl75lyz.fsf@posteo.net> (raw)
In-Reply-To: <868rvwsezi.fsf@mail.linkov.net> (Juri Linkov's message of "Mon,  03 Jan 2022 23:12:53 +0200")

[-- Attachment #1: Type: text/plain, Size: 971 bytes --]

Juri Linkov <juri@linkov.net> writes:

>> I am attaching a few patches adding a few features for
>> context-menu-mode, that I mentioned and explained here:
>> https://ruzkuku.com/texts/emacs-mouse.html.
>>
>> Would it be OK to add these to Emacs itself?
>
> Thanks, it would be beneficial to enrich the packages with the
> context-menus.  Man-context-menu is a good addition to the package
> man.el, and hi-lock-context-menu to hi-lock.el.  However,
> mouse-online-search-at-point looks out of place in mouse.el.
> Since it relies on eww, wouldn't the right place for it be
> in eww.el?

It doesn't really depend on eww in any substantial way, rather eww is
the only place where a search engine is defined.  What I was thinking
about was adding a command like online-search (that could then have both
a keyboard and mouse-oriented command), a online-search-engine
user option.  While one is at it, eww could be updated to use this too,
by deprecating eww-search-prefix:


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-search-engine-functionality.patch --]
[-- Type: text/x-patch, Size: 6289 bytes --]

From 1ebb6881aeeb08bf574bdb688cb1e94af8b5609d Mon Sep 17 00:00:00 2001
From: Philip Kaludercic <philipk@posteo.net>
Date: Tue, 4 Jan 2022 20:36:32 +0100
Subject: [PATCH] Add search engine functionality

* lisp/misc.el (online-search): Add command to query an online
search engine.
(online-search-engine-alist): Add user option to configure
`online-search'.
(online-search-engine): Add user option to configure
`online-search'.
(online-search-query): Add internal command to generate a
search engine query, to be used elsewhere.
(online-search-at-point): Add command.
(online-search-at-mouse): Add command.
(online-search-context-menu): Add context menu support.
* lisp/net/eww.el (eww-search-prefix): Mark as obsolete
(eww--dwim-expand-url): Use `online-search-query'.
---
 lisp/misc.el    | 93 +++++++++++++++++++++++++++++++++++++++++++++++++
 lisp/net/eww.el |  7 ++--
 2 files changed, 96 insertions(+), 4 deletions(-)

diff --git a/lisp/misc.el b/lisp/misc.el
index 39ec9497d7..473fcac33f 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)
@@ -209,6 +210,98 @@ list-dynamic-libraries
   (display-buffer buffer)
   nil)
 
+(defgroup online-search ()
+  "Search engine options."
+  :group 'browse-url)
+
+(defcustom online-search-engine-alist
+  '(("DuckDuckGo" . "https://duckduckgo.com/html/?q=")
+    ("Google" . "https://www.google.com/search?q=")
+    ("Wikipedia" . "https://en.wikipedia.org/w/index.php?search="))
+  "Alist of search engines, mapping strings to queries.
+A query is either a string, interpreted as a URL prefix ending
+with an incomplete query string, onto which the query will be
+concatenated, or a function that accepts one argument and returns
+a URL."
+  :type '(alist :key-type string :value-type string)
+  :group 'online-search)
+
+(defcustom online-search-engine "DuckDuckGo"
+  "Search engine to use for `online-search' and related commands.
+The string should denote a key in `online-search-engine-alist'."
+  :type 'string
+  :group 'online-search)
+
+;;;###autoload
+(defun online-search-query (string &optional engine)
+  "Generate an URL query to search for STRING with ENGINE.
+See `online-search' for more details on ENGINE."
+  (let ((query (alist-get (or engine online-search-engine)
+                          online-search-engine-alist
+                          nil nil #'string=)))
+    (cond
+     ((stringp query)
+      (concat query (mapconcat #'url-hexify-string
+                               (split-string string nil t)
+                               "+")))
+     ((functionp query)
+      (funcall query string))
+     ((error "Unknown query %S" query)))))
+
+;;;###autoload
+(defun online-search (string &optional engine)
+  "Search for STRING using a search engine.
+If the optional argument ENGINE may be used to override
+`online-search-engine', and will be used to look up a query in
+`online-search-engine-alist'."
+  (interactive (list (if (use-region-p)
+                         (buffer-substring (region-beginning) (region-end))
+                       (read-string "Search: "))
+                     (and current-prefix-arg
+                          (completing-read
+                           "Use search engine: "
+                           online-search-engine-alist))))
+  (browse-url (online-search-query string engine)))
+
+;;;###autoload
+(defun online-search-at-point (&optional engine)
+  "Search for symbol at point using ENGINE.
+See `online-search' for more details."
+  (interactive (list (and current-prefix-arg
+                          (completing-read
+                           "Use search engine: "
+                           online-search-engine-alist))))
+  (let ((query (thing-at-point 'symbol)))
+    (unless query
+      (user-error "Nothing to search for"))
+    (online-search query engine)))
+
+;;;###autoload
+(defun online-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 `online-search' for more
+details."
+  (interactive "e")
+  (let ((query (if (use-region-p)
+                   (buffer-substring (region-beginning)
+                                     (region-end))
+                 (thing-at-mouse click 'symbol))))
+    (unless query
+      (user-error "Nothing to search for"))
+    (online-search query)))
+
+;;;###autoload
+(defun online-search-context-menu (menu click)
+  "Populate MENU with command to search online at CLICK."
+  (save-excursion
+    (mouse-set-point click)
+    (define-key-after menu [online-search-separator] menu-bar-separator)
+    (define-key-after menu [online-search-at-mouse]
+      '(menu-item "Online search" online-search-at-mouse
+                  :help "Search for region or word online")))
+  menu)
+
 (provide 'misc)
 
 ;;; misc.el ends here
diff --git a/lisp/net/eww.el b/lisp/net/eww.el
index 8930eb427d..37e84d160b 100644
--- a/lisp/net/eww.el
+++ b/lisp/net/eww.el
@@ -55,6 +55,7 @@ eww-search-prefix
   :version "24.4"
   :group 'eww
   :type 'string)
+(make-obsolete-variable 'eww-search-prefix "online-search-engine" "29.1")
 
 (defcustom eww-use-browse-url "\\`mailto:"
   "EWW will use `browse-url' when following links that match this regexp.
@@ -355,7 +356,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 `online-search'.
 
 If NEW-BUFFER is non-nil (interactively, the prefix arg), use a
 new buffer instead of reusing the default EWW buffer.
@@ -464,9 +465,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 (online-search-query url)))))
   url)
 
 (defun eww--preprocess-html (start end)
-- 
2.34.0


[-- Attachment #3: Type: text/plain, Size: 990 bytes --]


I added this to misc.el, but it could be added to a separate file as
well.

>> +(defun Man-at-mouse (e)
>> ...
>> +(defun Man-context-menu (menu click)
>> +  "Populate MENU with commands that open a man page at point."
>> +  (save-excursion
>> +    (mouse-set-point click)
>> +    (when (save-excursion
>
> It seems the second 'save-excursion' is superfluous?
>
>> +            (skip-syntax-backward "^ ")
>> +            (and (looking-at
>> +                  "[[:space:]]*\\([[:alnum:]_-]+([[:alnum:]]+)\\)")
>> +                  (match-string 1)))
>> +      (define-key-after menu [man-separator] menu-bar-separator)
>> +      (define-key-after menu [man-at-mouse]
>> +    '(menu-item "Open man page" man-at-mouse
>
> Typo: the command name is capitalized 'Man-at-mouse'.
>
>> +    (mouse-set-point click)
>> +    (when (symbol-at-point)
>
> This can be simplified to:
>
>        (thing-at-mouse click 'symbol)

Thanks for the hints, I have fixed them locally.

-- 
	Philip Kaludercic

  reply	other threads:[~2022-01-04 19:39 UTC|newest]

Thread overview: 75+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-03  8:34 bug#52973: Adding a few context-menu-mode commands Philip Kaludercic
2022-01-03 12:45 ` Eli Zaretskii
2022-01-03 13:52   ` Philip Kaludercic
2022-01-03 14:32     ` Eli Zaretskii
2022-01-03 17:23       ` Philip Kaludercic
2022-01-03 17:36         ` Eli Zaretskii
2022-01-03 21:17         ` Juri Linkov
2022-01-04 12:26           ` Eli Zaretskii
2022-01-04 17:46             ` Juri Linkov
2022-01-04 20:01               ` Eli Zaretskii
2022-01-04 20:08                 ` Philip Kaludercic
2022-01-04 20:15                   ` Eli Zaretskii
2022-01-05 19:09                     ` Juri Linkov
2022-01-05 19:29                       ` Eli Zaretskii
2022-01-05 20:57                         ` Juri Linkov
2022-01-06  6:54                           ` Eli Zaretskii
2022-01-06  8:20                             ` Juri Linkov
2022-01-06  9:10                               ` Eli Zaretskii
2022-01-03 21:12 ` Juri Linkov
2022-01-04 19:39   ` Philip Kaludercic [this message]
2022-01-05 18:58     ` Juri Linkov
2022-01-05 20:14       ` Philip Kaludercic
2022-01-06  8:17         ` Juri Linkov
2022-01-06 18:59           ` Philip Kaludercic
2022-01-06 20:03             ` Juri Linkov
2022-01-06 20:32               ` Eli Zaretskii
2022-01-06 20:35                 ` Juri Linkov
2022-01-07  6:38                   ` Eli Zaretskii
2022-01-07  6:46                     ` Lars Ingebrigtsen
2022-01-07  8:20                       ` Eli Zaretskii
2022-01-07  8:29                     ` Philip Kaludercic
2022-01-07  8:37                       ` Eli Zaretskii
2022-01-08 18:30                         ` Juri Linkov
2022-01-08 18:44                           ` Eli Zaretskii
2022-01-08 19:01                             ` Philip Kaludercic
2022-01-08 19:10                               ` Eli Zaretskii
2022-01-08 19:39                                 ` Philip Kaludercic
2022-01-08 20:09                                   ` Eli Zaretskii
2022-01-12  6:16                           ` Lars Ingebrigtsen
2022-01-12 13:12                             ` Eli Zaretskii
2022-01-12 17:16                               ` Juri Linkov
2022-01-12 17:36                                 ` Eli Zaretskii
2022-01-12 18:03                                   ` Juri Linkov
2022-01-12 19:27                                     ` Eli Zaretskii
2022-01-12 19:41                                       ` Juri Linkov
2022-01-12 19:53                                         ` Eli Zaretskii
2022-01-12 20:00                                           ` Juri Linkov
2022-01-12 20:42                                             ` Eli Zaretskii
2022-01-13  8:39                                               ` Juri Linkov
2022-01-13 10:06                                                 ` Eli Zaretskii
2022-01-13  6:03                                 ` Lars Ingebrigtsen
2022-01-13  8:35                                   ` Juri Linkov
2022-01-13  8:53                                     ` Lars Ingebrigtsen
2022-01-13  9:10                                       ` Juri Linkov
2022-01-13  9:23                                         ` Lars Ingebrigtsen
2022-01-13 10:11                                         ` Eli Zaretskii
2022-01-13  8:54                                   ` Eli Zaretskii
2022-01-06 20:19             ` Lars Ingebrigtsen
2022-01-06 20:32               ` Juri Linkov
2022-01-22 19:39             ` Philip Kaludercic
2022-01-23  9:08               ` Juri Linkov
2022-01-23 12:09                 ` Philip Kaludercic
2022-01-23 12:13                   ` Lars Ingebrigtsen
2022-01-23 16:30                     ` Philip Kaludercic
2022-01-23 18:06                       ` Juri Linkov
2022-01-23 19:49                         ` Philip Kaludercic
2022-01-23 20:04                           ` Juri Linkov
2022-01-24  9:14                       ` Lars Ingebrigtsen
2022-01-25 12:06                         ` Philip Kaludercic
2022-01-25 12:24                           ` Lars Ingebrigtsen
2022-01-04 20:09   ` Philip Kaludercic
2022-01-05 19:03     ` Juri Linkov
2022-01-14  8:52 ` Lars Ingebrigtsen
2022-01-14 14:33   ` Philip Kaludercic
2022-01-15  8:07     ` Lars Ingebrigtsen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87czl75lyz.fsf@posteo.net \
    --to=philipk@posteo.net \
    --cc=52973@debbugs.gnu.org \
    --cc=juri@linkov.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).