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

View file

@ -13,21 +13,26 @@
;; `dumb-jump' to find what you want.
(defvar +lookup-provider-url-alist
(append '(("Google" . "https://google.com/search?q=%s")
("Google images" . "https://www.google.com/images?q=%s")
("Google maps" . "https://maps.google.com/maps?q=%s")
("Project Gutenberg" . "http://www.gutenberg.org/ebooks/search/?query=%s")
("DuckDuckGo" . "https://duckduckgo.com/?q=%s")
("DevDocs.io" . "https://devdocs.io/#q=%s")
("StackOverflow" . "https://stackoverflow.com/search?q=%s")
("Github" . "https://github.com/search?ref=simplesearch&q=%s")
("Youtube" . "https://youtube.com/results?aq=f&oq=&search_query=%s")
("Wolfram alpha" . "https://wolframalpha.com/input/?i=%s")
("Wikipedia" . "https://wikipedia.org/search-redirect.php?language=en&go=Go&search=%s"))
(append '(("Google" counsel-search helm-google "https://google.com/search?q=%s")
("Google images" "https://www.google.com/images?q=%s")
("Google maps" "https://maps.google.com/maps?q=%s")
("Project Gutenberg" "http://www.gutenberg.org/ebooks/search/?query=%s")
("DuckDuckGo" counsel-search "https://duckduckgo.com/?q=%s")
("DevDocs.io" "https://devdocs.io/#q=%s")
("StackOverflow" "https://stackoverflow.com/search?q=%s")
("Github" "https://github.com/search?ref=simplesearch&q=%s")
("Youtube" "https://youtube.com/results?aq=f&oq=&search_query=%s")
("Wolfram alpha" "https://wolframalpha.com/input/?i=%s")
("Wikipedia" "https://wikipedia.org/search-redirect.php?language=en&go=Go&search=%s"))
(when (featurep! :lang rust)
'(("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
produces an url. Used by `+lookup/online'.")
'(("Rust Docs" "https://doc.rust-lang.org/edition-guide/?search=%s"))))
"An alist that maps online resources to either:
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
"Function to use to open search urls.")

View file

@ -10,7 +10,9 @@
;;
(package! dumb-jump)
(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)
(package! helm-xref))