Skip to content

Instantly share code, notes, and snippets.

@yugaego
Forked from Superbil/init-dictionary.el
Created April 30, 2020 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yugaego/c32dcb5057652a0317fad298ff0d3cdb to your computer and use it in GitHub Desktop.
Save yugaego/c32dcb5057652a0317fad298ff0d3cdb to your computer and use it in GitHub Desktop.
lookup dictionary in emacs on mac
;; from http://larkery.tumblr.com/post/465585528/emacs-dictionary-app
(defun mac-open-dictionary (the-word)
"Open Dictionary.app for the-word"
(interactive "Dictionary Lookup: ")
(shell-command
(concat "open \"dict:///" (replace-regexp-in-string "\"" "\\\\\"" the-word) "\"")))
;; see https://gist.github.com/1228110
;; dict.py is from http://sakito.jp/mac/dictionary.html
(defun dictionary ()
"lookup from Dictionary.app"
(interactive "Dictionary Lookup: ")
(let ((word (if (and transient-mark-mode mark-active)
(buffer-substring-no-properties (region-beginning) (region-end))
(read-string "Dictionary: ")))
(cur-buffer (current-buffer))
(tmpbuf "* dict-process *"))
(set-buffer (get-buffer-create tmpbuf))
(erase-buffer)
(insert word "\n")
(let ((coding-system-for-read 'utf-8-mac)
(coding-system-for-write 'utf-8-mac))
(call-process "~/.emacs.d/site-lisp/dict.py" nil tmpbuf nil word) ;; specify full pass of dict.py
(let ((str (buffer-substring (point-min) (- (point-max) 2))))
(set-buffer cur-buffer)
(popup-tip str :scroll-bar t)))))
(when *is-a-mac*
(global-set-key (kbd "C-c d")
'(lambda ()
(mac-open-dictionary (current-word))))
(global-set-key (kbd "C-c D") 'dictionary))
(provide 'init-dictionay)
@dotemacs
Copy link

dotemacs commented Dec 2, 2022

Thanks for sharing this, but this is a bit shorter and does the same thing:

(defun dict (beg end)
  "Look up a word in macOS Dictionary.app"
  (interactive "r")
  (thread-last (buffer-substring-no-properties beg end)
    url-hexify-string
    (concat "dict:///")
    browse-url))

Just select a region in a buffer, and then M-x dict RET and it'll look up a word.

@yugaego
Copy link
Author

yugaego commented Dec 3, 2022

You're welcome, though note this is just a fork 😉

For reference, here's the version I'm currently using.
The approach with the current-word call has been working fine for me so far, so I don't see a reason to change that.

Still, feel free to submit a PR if you think that version could be improved meaningfully.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment