From 1ebb6881aeeb08bf574bdb688cb1e94af8b5609d Mon Sep 17 00:00:00 2001 From: Philip Kaludercic 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