diff --git a/docs/index.org b/docs/index.org index a623084de..eff52c0cd 100644 --- a/docs/index.org +++ b/docs/index.org @@ -237,7 +237,8 @@ Small modules that give Emacs access to external tools & services. + flycheck - Live error/warning highlights + flyspell - Spell checking + 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 + macos - TODO + magit - TODO diff --git a/modules/tools/lookup/README.org b/modules/tools/lookup/README.org index 53d7feb4b..c541cba8b 100644 --- a/modules/tools/lookup/README.org +++ b/modules/tools/lookup/README.org @@ -32,8 +32,10 @@ up definitions, references and documentation. + Documentation lookup for a variety of online sources (like devdocs.io, stackoverflow or youtube). + Integration with Dash.app docsets. ++ Support for online dictionaries and thesauruses. ** Module Flags ++ ~+dictionary~ Enable word definition and thesaurus lookup functionality. + ~+docsets~ Enable integration with Dash.app docsets. ** Plugins diff --git a/modules/tools/lookup/autoload/lookup.el b/modules/tools/lookup/autoload/lookup.el index 46726eca0..a2ba7a534 100644 --- a/modules/tools/lookup/autoload/lookup.el +++ b/modules/tools/lookup/autoload/lookup.el @@ -320,3 +320,47 @@ Otherwise, falls back on `find-file-at-point'." (run-hooks 'projectile-find-file-hook)))) (#'doom-project-browse)))) (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"))) diff --git a/modules/tools/lookup/config.el b/modules/tools/lookup/config.el index a34773187..c80e09c52 100644 --- a/modules/tools/lookup/config.el +++ b/modules/tools/lookup/config.el @@ -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 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 @@ -165,3 +176,22 @@ See https://github.com/magit/ghub/issues/81" (use-package! counsel-dash :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)) diff --git a/modules/tools/lookup/packages.el b/modules/tools/lookup/packages.el index 6c0b50565..92113d696 100644 --- a/modules/tools/lookup/packages.el +++ b/modules/tools/lookup/packages.el @@ -23,3 +23,9 @@ (package! helm-dash)) (when (featurep! :completion ivy) (package! counsel-dash))) + +(when (featurep! +dictionary) + (if IS-MAC + (package! osx-dictionary) + (package! define-word)) + (package! powerthesaurus))