Last active
March 10, 2016 09:38
-
-
Save Superbil/5003454 to your computer and use it in GitHub Desktop.
lookup word on Mac's Dictionary in Emacs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
''' | |
must intall PyObjC, pyobjc-framework-DictionaryServices | |
source from http://sakito.jp/mac/dictionary.html | |
''' | |
import sys | |
from DictionaryServices import * | |
def main(): | |
word = sys.argv[1].decode('utf-8') | |
result = DCSCopyTextDefinition(None, word, (0, len(word))) | |
print result.encode('utf-8') | |
if __name__ == '__main__': | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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: ") | |
(concat "open \"dict:///" (replace-regexp-in-string "\"" "\\\\\"" the-word) "\""))) | |
(shell-command | |
(global-set-key (kbd "C-c d") | |
'(lambda () | |
(mac-open-dictionary (current-word)))) | |
(interactive) | |
;; 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)) | |
))) | |
(global-set-key (kbd "C-c D") 'dictionary) | |
(provide 'init-dictionay) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment