Skip to content

Instantly share code, notes, and snippets.

@peccu
Forked from hitode909/dict.py
Created January 6, 2012 07:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peccu/1569550 to your computer and use it in GitHub Desktop.
Save peccu/1569550 to your computer and use it in GitHub Desktop.
非同期に辞書を引く
// copy from https://gist.github.com/1203094
// gcc -o dict -Wall -fobjc-gc -O2 -std=c99 dict.m -framework carbon -framework cocoa
#import <Cocoa/Cocoa.h>
#import <CoreServices/CoreServices.h>
int main(int argc, char *argv[])
{
NSString *word;
NSString *result;
if (argc > 1) {
word = [[NSString alloc] initWithUTF8String:argv[1]];
result = (NSString *)DCSCopyTextDefinition(NULL, (CFStringRef)word, CFRangeMake(0, [word length]));
// NSLog(@"word: %@", word);
// NSLog(@"result: %@", result);
if (result) {
printf("%s", [result UTF8String]);
return 0;
}
}
return 1;
}
#!/usr/bin/python2.5
import sys
from DictionaryServices import *
def main():
word = sys.argv[1].decode('utf-8')
result = DCSCopyTextDefinition(None, word, (0, len(word)))
if result:
print result.encode('utf-8')
else:
print "nil"
if __name__ == '__main__':
main()
;; 非同期に辞書を引く
;;
;; (+ "http://d.hatena.ne.jp/a666666/20100529/1275138722"
;; "http://sakito.jp/mac/dictionary.html"
;; "http://d.hatena.ne.jp/tomoya/20091218/1261138091"
;; "http://d.hatena.ne.jp/tomoya/20100103/1262482873")
(require 'cl)
(require 'deferred)
(require 'popup)
(defvar dict-bin "~/bin/dict.py"
"dict 実行ファイルのパス")
(defun my-dictionary ()
(interactive)
(lexical-let ((current-word (thing-at-point 'word)))
(when current-word
(deferred:$
(deferred:process dict-bin current-word)
(deferred:nextc it
(lambda (res) (popup-tip (concat current-word "\n" res))))))))
;; mac-key-modeを参考にしたのでリージョンとかいらん処理かも
(defun my-dictionary-at-mouse-cursor (event)
"クリックした単語かリージョンの辞書を引く"
(interactive "e")
(let ((editable (not buffer-read-only))
(pt (save-excursion (mouse-set-point last-nonmenu-event)))
beg end
)
;; getting word boundaries
(if (and mark-active
(<= (region-beginning) pt) (<= pt (region-end)) )
(setq beg (region-beginning)
end (region-end))
(save-excursion
(goto-char pt)
(setq end (progn (forward-word) (point)))
(setq beg (progn (backward-word) (point)))
))
(lexical-let ((current-word (buffer-substring-no-properties beg end))
(pt pt))
(deferred:$
(deferred:process dict-bin current-word)
(deferred:nextc it
(lambda (res)
(popup-tip (concat current-word "\n" res) :point pt :truncate t)))))))
(provide 'dictionary)
(require 'dictionary)
;; https://gist.github.com/1203094 (このgistのdict.m)をコンパイルしたものに置き換え
(setq dict-bin "~/bin/dict")
;; popupの背景色を白にする
(set-face-background 'popup-tip-face "white")
;; 右クリックした位置の単語の辞書を引く
(global-set-key (kbd "<mouse-3>") 'my-dictionary-at-mouse-cursor)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment