Emacs: Click to Search in Bing Dictionary

bing-dict.el provides a simple Emacs client for Bing Dictionary. I bind C-c D to bing-dict-brief, but sometimes I would like to use mouse to search the word by clicking. Here is a really simple code snippet that I've used for some time:

(require 'thingatpt)
(defun qjp-search-word-at-mouse ()
  "Search the word at mouse using `bing-dict-brief'."
  (interactive)
  (save-excursion
    (mouse-set-point last-input-event)
    (let ((word (word-at-point)))
      (when word
        (bing-dict-brief word)))))
(global-set-key (kbd "<C-mouse-1>") 'qjp-search-word-at-mouse)
(global-unset-key (kbd "<C-down-mouse-1>"))

Now you can move the cursor over the word that you want to search in Emacs and hold Ctrl and click the word. The search result in Bing Dictionary will be shown shortly in the echo area.

Note that I intentionally use save-excursion in the function qjp-search-word-at-mouse to reserve the original cursor position so that searching a word by clicking will not change the cursor location.

Junpeng Qiu 07 January 2016
blog comments powered by Disqus