Generalize +jump/online #140
+ Now uses selection if region is active. Prompts for the query otherwise, with the thing at point as the initial input. + On first use, prompts for provider. On consecutive uses, reuses previous provider. If universal argument is supplied, force the prompt to appear.
This commit is contained in:
parent
5057db93f1
commit
c31167d747
3 changed files with 27 additions and 25 deletions
|
@ -6,12 +6,5 @@
|
|||
reuse it on consecutive uses of this command. If BANG, always prompt for search
|
||||
engine."
|
||||
(interactive "<a><!>")
|
||||
(let ((query (or query (thing-at-point 'symbol t))))
|
||||
(unless query
|
||||
(user-error "The search query is empty"))
|
||||
(+jump/online
|
||||
(or (and (not bang) (bound-and-true-p +jump--online-last))
|
||||
(completing-read (format "Search on (%s): " query)
|
||||
(mapcar #'car +jump-search-url-alist)
|
||||
nil t))
|
||||
query)))
|
||||
(+jump/online (or query (thing-at-point 'symbol t))
|
||||
(+jump--online-get-provider bang)))
|
||||
|
|
|
@ -94,32 +94,41 @@ Tries `xref-find-references' and falls back to rg/ag."
|
|||
(cond ((plist-member +jump-current-functions :documentation)
|
||||
(+jump-to :documentation identifier))
|
||||
(t
|
||||
(+jump/online (caar +jump-search-url-alist) identifier))))
|
||||
(+jump/online (caar +jump-search-provider-alist) identifier))))
|
||||
|
||||
(defun +jump--online-get-provider (&optional force-p)
|
||||
(or (and (not force-p)
|
||||
+jump--online-last)
|
||||
(completing-read "Search on: "
|
||||
(mapcar #'car +jump-search-provider-alist)
|
||||
nil t)))
|
||||
|
||||
(defvar +jump--online-last nil)
|
||||
;;;###autoload
|
||||
(defun +jump/online (where search)
|
||||
"Looks up SEARCH online, in you browser, as dictated by WHERE.
|
||||
(defun +jump/online (search &optional provider)
|
||||
"Looks up SEARCH (a string) in you browser using PROVIDER.
|
||||
|
||||
Interactively, you are prompted to choose a source from
|
||||
`+jump-search-url-alist'."
|
||||
PROVIDER should be a key of `+jump-search-provider-alist'.
|
||||
|
||||
When used interactively, it will prompt for a query and, for the first time, the
|
||||
provider from `+jump-search-provider-alist'. On consecutive uses, the last
|
||||
provider will be reused. If the universal argument is supplied, always prompt
|
||||
for the provider."
|
||||
(interactive
|
||||
(list (or (and (not current-prefix-arg)
|
||||
+jump--online-last)
|
||||
(completing-read (format "Search on (%s): " (thing-at-point 'symbol t))
|
||||
(mapcar #'car +jump-search-url-alist)
|
||||
nil t))
|
||||
(thing-at-point 'symbol t)))
|
||||
(list (or (and (region-active-p)
|
||||
(buffer-substring-no-properties (region-beginning)
|
||||
(region-end)))
|
||||
(read-string "Search for: " (thing-at-point 'symbol t)))
|
||||
(+jump--online-get-provider current-prefix-arg)))
|
||||
(condition-case _ex
|
||||
(let ((url (cdr (assoc where +jump-search-url-alist))))
|
||||
(let ((url (cdr (assoc provider +jump-search-provider-alist))))
|
||||
(unless url
|
||||
(error "'%s' is an invalid search engine" where))
|
||||
(error "'%s' is an invalid search engine" provider))
|
||||
(when (or (functionp url) (symbolp url))
|
||||
(setq url (funcall url)))
|
||||
(cl-assert (and (stringp url) (not (string-empty-p url))))
|
||||
(when (string-empty-p search)
|
||||
(user-error "The search query is empty"))
|
||||
(setq +jump--online-last where)
|
||||
(setq +jump--online-last provider)
|
||||
(browse-url (format url (url-encode-url search))))
|
||||
('error (setq +jump--online-last nil))))
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
;; in the future. When xref can't be depended on it will fall back to
|
||||
;; `dumb-jump' to find what you want.
|
||||
|
||||
(defvar +jump-search-url-alist
|
||||
(defvar +jump-search-provider-alist
|
||||
'(("Google" . "https://google.com/search?q=%s")
|
||||
("DuckDuckGo" . "https://duckduckgo.com/?q=%s")
|
||||
("DevDocs.io" . "http://devdocs.io/#q=%s")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue