tools/lookup: add +dictionary feature

For looking up words in dictionaries and thesauruses. Requires internet
connection (for now).
This commit is contained in:
Henrik Lissner 2019-12-20 00:43:36 -05:00
parent 18d8ea22f6
commit 7742813a06
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
5 changed files with 84 additions and 1 deletions

View file

@ -237,7 +237,8 @@ Small modules that give Emacs access to external tools & services.
+ flycheck - Live error/warning highlights + flycheck - Live error/warning highlights
+ flyspell - Spell checking + flyspell - Spell checking
+ gist - TODO + gist - TODO
+ [[file:../modules/tools/lookup/README.org][lookup]] =+docsets= - Universal jump-to & documentation lookup backend + [[file:../modules/tools/lookup/README.org][lookup]] =+dictionary +docsets= - Universal jump-to & documentation lookup
backend
+ [[file:../modules/tools/lsp/README.org][lsp]] - TODO + [[file:../modules/tools/lsp/README.org][lsp]] - TODO
+ macos - TODO + macos - TODO
+ magit - TODO + magit - TODO

View file

@ -32,8 +32,10 @@ up definitions, references and documentation.
+ Documentation lookup for a variety of online sources (like devdocs.io, + Documentation lookup for a variety of online sources (like devdocs.io,
stackoverflow or youtube). stackoverflow or youtube).
+ Integration with Dash.app docsets. + Integration with Dash.app docsets.
+ Support for online dictionaries and thesauruses.
** Module Flags ** Module Flags
+ ~+dictionary~ Enable word definition and thesaurus lookup functionality.
+ ~+docsets~ Enable integration with Dash.app docsets. + ~+docsets~ Enable integration with Dash.app docsets.
** Plugins ** Plugins

View file

@ -320,3 +320,47 @@ Otherwise, falls back on `find-file-at-point'."
(run-hooks 'projectile-find-file-hook)))) (run-hooks 'projectile-find-file-hook))))
(#'doom-project-browse)))) (#'doom-project-browse))))
(find-file-at-point path)))))) (find-file-at-point path))))))
;;
;;; Dictionary
;;;###autoload
(defun +lookup/word-definition (identifier &optional arg)
"Look up the definition of the word at point (or selection)."
(interactive
(list (+lookup-symbol-or-region)
current-prefix-arg))
(unless (featurep! +dictionary)
(user-error "The +dictionary feature hasn't be enabled on :tools lookup module"))
(cond (IS-MAC
(osx-dictionary-search-input identifier))
(+lookup-dictionary-enable-online
(define-word identifier nil arg))
;; TODO Implement offline dictionary backend
((user-error "No offline dictionary defined yet"))))
;;;###autoload
(defun +lookup/word-synonyms (identifier &optional arg)
"Look up and insert a synonym for the word at point (or selection)."
(interactive
(list (+lookup-symbol-or-region)
current-prefix-arg))
(unless (featurep! +dictionary)
(user-error "The +dictionary feature hasn't be enabled on :tools lookup module"))
(if +lookup-dictionary-enable-online
(request
(powerthesaurus-compose-url identifier)
:parser (lambda () (libxml-parse-html-region (point) (point-max)))
:headers '(("User-Agent" . "Chrome/74.0.3729.169"))
:success (cl-function
(lambda (&key data &allow-other-keys)
;; in order to allow users to quit powerthesaurus prompt
;; with C-g, we need to wrap callback with this
(with-local-quit
(funcall (powerthesaurus-choose-callback
(region-beginning) (region-end))
(powerthesaurus-pick-synonym data)
identifier)))))
;; TODO Implement offline synonyms backend
(user-error "No offline dictionary implemented yet")))

View file

@ -84,6 +84,17 @@ If the argument is interactive (satisfies `commandp'), it is called with
argument: the identifier at point. See `set-lookup-handlers!' about adding to argument: the identifier at point. See `set-lookup-handlers!' about adding to
this list.") this list.")
(defvar +lookup-dictionary-enable-online t
"If non-nil, look up dictionaries online.
Setting this to nil will force it to use offline backends, which may be less
than perfect, but available without an internet connection.
Used by `+lookup/word-definition' and `+lookup/word-synonyms'.
For `+lookup/word-definition', this is ignored on Mac, where Emacs users
Dictionary.app behind the scenes to get definitions.")
;; ;;
;;; dumb-jump ;;; dumb-jump
@ -165,3 +176,22 @@ See https://github.com/magit/ghub/issues/81"
(use-package! counsel-dash (use-package! counsel-dash
:when (featurep! :completion ivy))) :when (featurep! :completion ivy)))
;;
;;; Dictionary integration
(use-package! define-word
:when (featurep! +dictionary)
:unless IS-MAC
:defer t
:config
(setq define-word-displayfn-alist
(cl-loop for (service . _) in define-word-services
collect (cons service #'+eval-display-results-in-popup))))
(when (featurep! +dictionary)
(define-key! text-mode-map
[remap +lookup/definition] #'+lookup/word-definition
[remap +lookup/references] #'+lookup/word-synonyms))

View file

@ -23,3 +23,9 @@
(package! helm-dash)) (package! helm-dash))
(when (featurep! :completion ivy) (when (featurep! :completion ivy)
(package! counsel-dash))) (package! counsel-dash)))
(when (featurep! +dictionary)
(if IS-MAC
(package! osx-dictionary)
(package! define-word))
(package! powerthesaurus))