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))) result)))
;;;###autoload ;;;###autoload
(defun +lookup-symbol-or-region (&optional initial) (defun +lookup-thing-or-region (&optional initial thing)
"Grab the symbol at point or selected region." "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) (cond ((stringp initial)
initial) initial)
((use-region-p) ((use-region-p)
(buffer-substring-no-properties (region-beginning) (buffer-substring-no-properties (region-beginning)
(region-end))) (region-end)))
(thing
(thing-at-point thing t))
((require 'xref nil t) ((require 'xref nil t)
;; A little smarter than using `symbol-at-point', though in most cases, ;; A little smarter than using `symbol-at-point', though in most cases,
;; xref ends up using `symbol-at-point' anyway. ;; 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 point or current buffer. Falls back to dumb-jump, naive
ripgrep/the_silver_searcher text search, then `evil-goto-definition' if ripgrep/the_silver_searcher text search, then `evil-goto-definition' if
evil-mode is active." evil-mode is active."
(interactive (list (+lookup-symbol-or-region) (interactive (list (+lookup-thing-or-region)
current-prefix-arg)) current-prefix-arg))
(cond ((null identifier) (user-error "Nothing under point")) (cond ((null identifier) (user-error "Nothing under point"))
((+lookup--jump-to :definition identifier nil arg)) ((+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 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 point and/or current buffer. Falls back to a naive ripgrep/the_silver_searcher
search otherwise." search otherwise."
(interactive (list (+lookup-symbol-or-region) (interactive (list (+lookup-thing-or-region)
current-prefix-arg)) current-prefix-arg))
(cond ((null identifier) (user-error "Nothing under point")) (cond ((null identifier) (user-error "Nothing under point"))
((+lookup--jump-to :references identifier nil arg)) ((+lookup--jump-to :references identifier nil arg))
@ -271,7 +276,7 @@ search otherwise."
First attempts the :documentation handler specified with `set-lookup-handlers!' First attempts the :documentation handler specified with `set-lookup-handlers!'
for the current mode/buffer (if any), then falls back to the backends in for the current mode/buffer (if any), then falls back to the backends in
`+lookup-documentation-functions'." `+lookup-documentation-functions'."
(interactive (list (+lookup-symbol-or-region) (interactive (list (+lookup-thing-or-region)
current-prefix-arg)) current-prefix-arg))
(cond ((+lookup--jump-to :documentation identifier #'pop-to-buffer arg)) (cond ((+lookup--jump-to :documentation identifier #'pop-to-buffer arg))
((user-error "Couldn't find documentation for %S" identifier)))) ((user-error "Couldn't find documentation for %S" identifier))))
@ -292,7 +297,7 @@ Otherwise, falls back on `find-file-at-point'."
(or (ffap-guesser) (or (ffap-guesser)
(ffap-read-file-or-url (ffap-read-file-or-url
(if ffap-url-regexp "Find file or URL: " "Find file: ") (if ffap-url-regexp "Find file or URL: " "Find file: ")
(+lookup-symbol-or-region)))))) (+lookup-thing-or-region))))))
(require 'ffap) (require 'ffap)
(cond ((not path) (cond ((not path)
(call-interactively #'find-file-at-point)) (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) (defun +lookup/dictionary-definition (identifier &optional arg)
"Look up the definition of the word at point (or selection)." "Look up the definition of the word at point (or selection)."
(interactive (interactive
(list (+lookup-symbol-or-region) (list (or (+lookup-thing-or-region nil 'word)
(read-string "Look up in dictionary: "))
current-prefix-arg)) 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)) (cond ((and IS-MAC (require 'osx-dictionary nil t))
(osx-dictionary--view-result identifier)) (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)) (define-word identifier nil arg))
;; TODO Implement offline dictionary backend ;; TODO Implement offline dictionary backend
((user-error "No offline dictionary defined yet")))) ((user-error "No dictionary backend is available"))))
;;;###autoload ;;;###autoload
(defun +lookup/synonyms (identifier &optional arg) (defun +lookup/synonyms (identifier &optional arg)
"Look up and insert a synonym for the word at point (or selection)." "Look up and insert a synonym for the word at point (or selection)."
(interactive (interactive
(list (+lookup-symbol-or-region) (list (+lookup-thing-or-region nil 'word) ; TODO actually use this
current-prefix-arg)) current-prefix-arg))
(unless (featurep! +dictionary) (unless (require 'powerthesaurus nil t)
(user-error "The +dictionary feature hasn't be enabled on :tools lookup module")) (user-error "No dictionary backend is available"))
(unless +lookup-dictionary-enable-online (unless +lookup-dictionary-enable-online
;; TODO Implement offline synonyms backend ;; TODO Implement offline synonyms backend
(user-error "No offline dictionary implemented yet")) (user-error "No offline dictionary implemented yet"))
;; Necessary because powerthesaurus lacks a way to pass text directly (message "Looking up synonyms for %S" identifier)
(with-temp-buffer (powerthesaurus-lookup-word-dwim))
(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)))))