tools/lookup: add autocompletion for google/duckduckgo providers

For +lookup/online, on `SPC s o`

- Change the signature of `+lookup/online`
- Change format of +lookup-provider-url-alist.
This commit is contained in:
Henrik Lissner 2019-12-15 17:59:20 -05:00
parent c522ca4fc0
commit dfb5a0cc54
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
3 changed files with 47 additions and 40 deletions

View file

@ -25,8 +25,8 @@ argument is non-nil)."
(+lookup--online-provider (not current-prefix-arg)))) (+lookup--online-provider (not current-prefix-arg))))
;;;###autoload ;;;###autoload
(defun +lookup/online (search &optional provider) (defun +lookup/online (arg &optional query provider)
"Looks up SEARCH (a string) in you browser using PROVIDER. "Looks up QUERY (a string) in you browser using PROVIDER.
PROVIDER should be a key of `+lookup-provider-url-alist'. PROVIDER should be a key of `+lookup-provider-url-alist'.
@ -34,29 +34,29 @@ When used interactively, it will prompt for a query and, for the first time, the
provider from `+lookup-provider-url-alist'. On consecutive uses, the last provider from `+lookup-provider-url-alist'. On consecutive uses, the last
provider will be reused. If the universal argument is supplied, always prompt provider will be reused. If the universal argument is supplied, always prompt
for the provider." for the provider."
(interactive (interactive "P")
(let ((provider (+lookup--online-provider current-prefix-arg))) (let* ((provider (or provider (+lookup--online-provider arg)))
(list (or (and (use-region-p) (query (or query (+lookup-symbol-or-region)))
(buffer-substring-no-properties (region-beginning) (backend (cl-find-if (lambda (x) (or (stringp x) (fboundp x)))
(region-end))) (cdr (assoc provider +lookup-provider-url-alist)))))
(read-string (format "Search for (on %s): " provider) (if (commandp backend)
(thing-at-point 'symbol t))) (call-interactively backend)
provider))) (unless backend
(condition-case-unless-debug e (user-error "%S is an invalid query engine backend for %S provider"
(let ((url (cdr (assoc provider +lookup-provider-url-alist)))) backend provider))
(unless url (cl-check-type backend (or string function))
(user-error "'%s' is an invalid search engine" provider)) (condition-case-unless-debug e
(when (or (functionp url) (symbolp url)) (progn
(setq url (funcall url))) (when (or (functionp backend) (symbolp backend))
(cl-assert (stringp url)) (setq backend (funcall backend)))
(when (string-empty-p search) (when (string-empty-p query)
(user-error "The search query is empty")) (user-error "The query query is empty"))
(funcall +lookup-open-url-fn (format url (url-encode-url search)))) (funcall +lookup-open-url-fn (format url (url-encode-url query))))
(error (error
(setq +lookup--last-provider (setq +lookup--last-provider
(delq (assq major-mode +lookup--last-provider) (delq (assq major-mode +lookup--last-provider)
+lookup--last-provider)) +lookup--last-provider))
(signal (car e) (cdr e))))) (signal (car e) (cdr e)))))))
;;;###autoload ;;;###autoload
(defun +lookup/online-select () (defun +lookup/online-select ()

View file

@ -13,21 +13,26 @@
;; `dumb-jump' to find what you want. ;; `dumb-jump' to find what you want.
(defvar +lookup-provider-url-alist (defvar +lookup-provider-url-alist
(append '(("Google" . "https://google.com/search?q=%s") (append '(("Google" counsel-search helm-google "https://google.com/search?q=%s")
("Google images" . "https://www.google.com/images?q=%s") ("Google images" "https://www.google.com/images?q=%s")
("Google maps" . "https://maps.google.com/maps?q=%s") ("Google maps" "https://maps.google.com/maps?q=%s")
("Project Gutenberg" . "http://www.gutenberg.org/ebooks/search/?query=%s") ("Project Gutenberg" "http://www.gutenberg.org/ebooks/search/?query=%s")
("DuckDuckGo" . "https://duckduckgo.com/?q=%s") ("DuckDuckGo" counsel-search "https://duckduckgo.com/?q=%s")
("DevDocs.io" . "https://devdocs.io/#q=%s") ("DevDocs.io" "https://devdocs.io/#q=%s")
("StackOverflow" . "https://stackoverflow.com/search?q=%s") ("StackOverflow" "https://stackoverflow.com/search?q=%s")
("Github" . "https://github.com/search?ref=simplesearch&q=%s") ("Github" "https://github.com/search?ref=simplesearch&q=%s")
("Youtube" . "https://youtube.com/results?aq=f&oq=&search_query=%s") ("Youtube" "https://youtube.com/results?aq=f&oq=&search_query=%s")
("Wolfram alpha" . "https://wolframalpha.com/input/?i=%s") ("Wolfram alpha" "https://wolframalpha.com/input/?i=%s")
("Wikipedia" . "https://wikipedia.org/search-redirect.php?language=en&go=Go&search=%s")) ("Wikipedia" "https://wikipedia.org/search-redirect.php?language=en&go=Go&search=%s"))
(when (featurep! :lang rust) (when (featurep! :lang rust)
'(("Rust Docs" . "https://doc.rust-lang.org/edition-guide/?search=%s")))) '(("Rust Docs" "https://doc.rust-lang.org/edition-guide/?search=%s"))))
"An alist that maps online resources to their search url or a function that "An alist that maps online resources to either:
produces an url. Used by `+lookup/online'.")
1. A search url (needs on '%s' to substitute with an url encoded query),
2. A non-interactive function that returns the search url in #1,
3. An interactive command that does its own search for that provider.
Used by `+lookup/online'.")
(defvar +lookup-open-url-fn #'browse-url (defvar +lookup-open-url-fn #'browse-url
"Function to use to open search urls.") "Function to use to open search urls.")

View file

@ -10,7 +10,9 @@
;; ;;
(package! dumb-jump) (package! dumb-jump)
(when (featurep! :completion ivy) (when (featurep! :completion ivy)
(package! ivy-xref)) (package! ivy-xref)
;; Need for Google/DuckDuckGo auto-completion on `+lookup/online'
(package! request))
(when (featurep! :completion helm) (when (featurep! :completion helm)
(package! helm-xref)) (package! helm-xref))