[Emacs] Emacs の popup.el を使って、 MacOSX の Dictionary.app から辞書をひく
Mac の Dictionary.app 便利ですね。
http://sakito.jp/mac/dictionary.html
なんかに使い方が書いてあって、 Emacs との連携も書いてあるのですがいかんせん新しいウィンドウを作ってしまうのがスマートでない。
popup.el なんていう素晴しいインターフェイスもあることだし、ここはひとつツールチップの中で表示させてみてはどうか。
と思って書いたのが以下です。
上記ページの dict.py が必要です。
;; dict.py is from http://sakito.jp/mac/dictionary.html (defun dictionary () "dictionary.app" (interactive) (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 "~/scripts/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-M-d") 'dictionary)
だいたい思い通りに動いているんだけど、問題点が2つほど。
- ツールチップに入りきらない場合を考えて popup-tip のスクロールバーを表示させているのだけど、実際にスクロールする方法がわからない。
- dict.py から辞書をひくとき、日本語で辞書をひくと例文まで取得できるけど英語でひくと例文までは取得できない。
1つめはキーマップ定義したりしてなんとかなるんじゃないかなーとか思っているんだけど。
とりあえず応急処置としては、
(popup-tip str :scroll-bar t :height 30)
とかやれば広くなるので、表示量が増えます。デフォルトは 15 です。
2つめは Dictionary.app の API が悪くて、 dict.py 中の
DCSCopyTextDefinition(None, word, (0, len(word)))
という箇所。こいつの挙動がよくわからない。
他に API あるんじゃないのと思って調べてみたけどよくわからなかった。
なので、あくまでざっくりと調べる用で本当にきちんと調べたいときは Dictionary.app から調べましょうということで。