Generalize lookup thing-at-pt fn; fix dict/synonym commands

This commit is contained in:
Henrik Lissner 2020-01-02 21:08:04 -05:00
parent 6938f8b97d
commit 8f6161f480
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -163,13 +163,18 @@ This can be passed nil as its second argument to unset handlers for MODES. e.g.
result)))
;;;###autoload
(defun +lookup-symbol-or-region (&optional initial)
"Grab the symbol at point or selected region."
(defun +lookup-thing-or-region (&optional initial thing)
"Return INITIAL, grab the THING at point, or contents of selection.
If THING is nil, uses `xref-backend-identifier-at-point' (like symbol, but
smarter)."
(cond ((stringp initial)
initial)
((use-region-p)
(buffer-substring-no-properties (region-beginning)
(region-end)))
(thing
(thing-at-point thing t))
((require 'xref nil t)
;; A little smarter than using `symbol-at-point', though in most cases,
;; xref ends up using `symbol-at-point' anyway.
@ -245,7 +250,7 @@ Each function in `+lookup-definition-functions' is tried until one changes the
point or current buffer. Falls back to dumb-jump, naive
ripgrep/the_silver_searcher text search, then `evil-goto-definition' if
evil-mode is active."
(interactive (list (+lookup-symbol-or-region)
(interactive (list (+lookup-thing-or-region)
current-prefix-arg))
(cond ((null identifier) (user-error "Nothing under point"))
((+lookup--jump-to :definition identifier nil arg))
@ -258,7 +263,7 @@ evil-mode is active."
Tries each function in `+lookup-references-functions' until one changes the
point and/or current buffer. Falls back to a naive ripgrep/the_silver_searcher
search otherwise."
(interactive (list (+lookup-symbol-or-region)
(interactive (list (+lookup-thing-or-region)
current-prefix-arg))
(cond ((null identifier) (user-error "Nothing under point"))
((+lookup--jump-to :references identifier nil arg))
@ -271,7 +276,7 @@ search otherwise."
First attempts the :documentation handler specified with `set-lookup-handlers!'
for the current mode/buffer (if any), then falls back to the backends in
`+lookup-documentation-functions'."
(interactive (list (+lookup-symbol-or-region)
(interactive (list (+lookup-thing-or-region)
current-prefix-arg))
(cond ((+lookup--jump-to :documentation identifier #'pop-to-buffer arg))
((user-error "Couldn't find documentation for %S" identifier))))
@ -292,7 +297,7 @@ Otherwise, falls back on `find-file-at-point'."
(or (ffap-guesser)
(ffap-read-file-or-url
(if ffap-url-regexp "Find file or URL: " "Find file: ")
(+lookup-symbol-or-region))))))
(+lookup-thing-or-region))))))
(require 'ffap)
(cond ((not path)
(call-interactively #'find-file-at-point))
@ -331,33 +336,27 @@ Otherwise, falls back on `find-file-at-point'."
(defun +lookup/dictionary-definition (identifier &optional arg)
"Look up the definition of the word at point (or selection)."
(interactive
(list (+lookup-symbol-or-region)
(list (or (+lookup-thing-or-region nil 'word)
(read-string "Look up in dictionary: "))
current-prefix-arg))
(unless (featurep! +dictionary)
(user-error "The +dictionary feature hasn't be enabled on :tools lookup module"))
(cond ((and IS-MAC (require 'osx-dictionary nil t))
(osx-dictionary--view-result identifier))
(+lookup-dictionary-enable-online
((and +lookup-dictionary-enable-online (require 'define-word nil t))
(message "Looking up definition of %S" identifier)
(define-word identifier nil arg))
;; TODO Implement offline dictionary backend
((user-error "No offline dictionary defined yet"))))
((user-error "No dictionary backend is available"))))
;;;###autoload
(defun +lookup/synonyms (identifier &optional arg)
"Look up and insert a synonym for the word at point (or selection)."
(interactive
(list (+lookup-symbol-or-region)
(list (+lookup-thing-or-region nil 'word) ; TODO actually use this
current-prefix-arg))
(unless (featurep! +dictionary)
(user-error "The +dictionary feature hasn't be enabled on :tools lookup module"))
(unless (require 'powerthesaurus nil t)
(user-error "No dictionary backend is available"))
(unless +lookup-dictionary-enable-online
;; TODO Implement offline synonyms backend
(user-error "No offline dictionary implemented yet"))
;; Necessary because powerthesaurus lacks a way to pass text directly
(with-temp-buffer
(insert identifier)
;; HACK I'd rather use `+lookup-symbol-or-region' for consistency, but
;; powerthesaurus lacks a simple way to pass a string directly to any
;; of its lookup functions, so...
(cl-letf (((symbol-function 'use-region-p) (lambda () t)))
(powerthesaurus-lookup-word (point-min) (point-max)))))
(message "Looking up synonyms for %S" identifier)
(powerthesaurus-lookup-word-dwim))