Emacsから辞書を引くsdicをさらに便利に

Emacsを使っていると英単語の意味を調べたくなることはよくある.そこで,sdicを使ってEmacs上で辞書を引けるようにする.
最終的にこんな感じになった!

インストールと基本的な使い方

Ubuntuならaptでインストールできる.

$ sudo aptitude install sdic sdic-edict sdic-gene95

.emacs に以下を記述.

(when (require 'sdic nil t)
  (global-set-key "\C-c\C-w" 'sdic))

Ctrl-c, Ctrl-w でsdicが起動するので,単語を入力したら調べられる.ちなみにカーソルの下の単語を自動で認識して,それがデフォルトで入力されている.
この時点でこんな感じ.別バッファに表示してくれる.

ツールチップで表示する

英単語の意味を知りたいと思うことは多くても,がっつり調べたいと思うことは少ない.いちいちバッファを作ってそこに表示するのはおおげさな気がする.そこで,ツールチップで表示させることにした.
.emacsに以下を記述.

(defun temp-cancel-read-only (function &optional jaspace-off)
  "eval temporarily cancel buffer-read-only
&optional t is turn of jaspace-mode"
  (let ((read-only-p nil)
        (jaspace-mode-p nil))
    (when (and jaspace-off jaspace-mode)
      (jaspace-mode)
      (setq jaspace-mode-p t))
    (when buffer-read-only
      (toggle-read-only)
      (setq read-only-p t))
    (eval function)
    (when read-only-p
      (toggle-read-only))
    (when jaspace-mode-p
      (jaspace-mode))))

(defun my-sdic-describe-word-with-popup (word &optional search-function)
  "Display the meaning of word."
  (interactive
   (let ((f (if current-prefix-arg (sdic-select-search-function)))
         (w (sdic-read-from-minibuffer)))
     (list w f)))
  (let ((old-buf (current-buffer))
        (dict-data))
    (set-buffer (get-buffer-create sdic-buffer-name))
    (or (string= mode-name sdic-mode-name) (sdic-mode))
    (erase-buffer)
    (let ((case-fold-search t)
          (sdic-buffer-start-point (point-min)))
      (if (prog1 (funcall (or search-function
                              (if (string-match "\\cj" word)
                                  'sdic-search-waei-dictionary
                                'sdic-search-eiwa-dictionary))
                          word)
            (set-buffer-modified-p nil)
            (setq dict-data (buffer-string))
            (set-buffer old-buf))
          (temp-cancel-read-only
           '(popup-tip dict-data :scroll-bar t :truncate nil))
        (message "Can't find word, \"%s\"." word))))
    )

(defadvice sdic-describe-word-at-point (around sdic-popup-advice activate)
  (letf (((symbol-function 'sdic-describe-word) (symbol-function 'my-sdic-describe-word-with-popup)))
    ad-do-it))
sdic の内容をツールチップにしてみた. - deruiの日記

さらに以下を追記.

(global-set-key "\C-cp" 'sdic-describe-word-at-point)

これで,単語の上で Ctrl-c,p を押すとツールチップで表示されるようになる(一番上の画像).
超便利だしかっこいい!