Dictionary and Thesaurus Lookup in Emacs

Or, adding a feature to a piece of software that already has too many features.

Tagged: Software

Emacs is my preferred tool for editing LATEX documents. A few years ago I got tired of switching back and forth to a terminal or web browser every time I wanted to look up a word in the dictionary or thesaurus. To remedy this, I wrote a simple, 100-line Emacs Lisp plugin to look up words using the DICT protocol.

One can simply cut/paste this into their .emacs file or make it a separate file in their elisp directory.

; -*-Emacs-Lisp-*-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; File:         dict.el
; Description:  Dict access functions
; Author:       Evan Sultanik
; Created:      Sun Aug 14 15:44:57 2005
; Modified:     Thu Nov 15 08:45:48 2007
; Language:     Emacs-Lisp
; Package:      N/A
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; *******************************************************
;; ***** THIS IS AN ALPHA TEST VERSION (Version 0.2) *****
;; *******************************************************
;;
;; dict.el
;; Copyright (C) 2005 Evan Sultanik (http://www.sultanik.com/)
;; 
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 1, or (at your option)
;; any later version.
;; 
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;; 
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
;; 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defvar dict-protocol-client “dict”
  “This is the name and/or path to the local copy of the DICT protocol client.”)

(defconst dict-scratch-buffer-name “*dict*”
  “This is the name of the buffer in which the dict output is displayed.”)

(defun dict-extract-word ()
  “From the current buffer, extract and return the word under the cursor.”
  (let (start word)
    (save-excursion
      (forward-char 1)
      (backward-word 1)
      (setq start (point))
      (forward-char 1)
      (if (not (re-search-forward “\\b”))
	  (error “Can’t find end of word”))
      (buffer-substring start (point))
      )))

(defun dict-lookup-word (word dict)
  “Look up the word WORD using the client, given by
`dict-protocol-client’.  The results will be displayed in the buffer
given by `dict-scratch-buffer-name’.  If DICT is nil, WORD is looked
up from a thesaurus only.”
  (interactive “sWord to lookup? \nP”)
  (let ((dict-buffer (get-buffer-create dict-scratch-buffer-name)))
    (save-excursion
      (buffer-disable-undo (set-buffer dict-buffer))
      (setq buffer-read-only nil)
      (setq disable-point-adjustment t)
      (erase-buffer)
      (display-buffer dict-buffer)
      (if (null dict)
	  (call-process dict-protocol-client
			nil ;; no infile
			t   ;; put output in the current buffer
			t   ;; re-display as we get more output
			“-P” “-” “-d” “moby-thes” word)
	  (call-process dict-protocol-client
			nil ;; no infile
			t   ;; put output in the current buffer
			t   ;; re-display as we get more output
			“-P” “-” word)
	  )
      (setq buffer-read-only t)
      (goto-char (point-min))
      )
    ))

(defun thesaurus-lookup-word (word)
  (dict-lookup-word word nil))

(defun dictionary-lookup-word (word)
  (dict-lookup-word word t))

(defun thesaurus-lookup-word-in-text (exact)
  “Like `dict-lookup-word’, but uses the word under the cursor.”
  (interactive “P”)
  (thesaurus-lookup-word (dict-extract-word)))

(defun dictionary-lookup-word-in-text (exact)
  “Like `dict-lookup-word’, but uses the word under the cursor.”
  (interactive “P”)
  (dictionary-lookup-word (dict-extract-word)))

This code assumes that you have the command dict available.

You can set a keyboard shortcut as follows:

(global-set-key (quote [f7]) ‘thesaurus-lookup-word-in-text)
(global-set-key (quote [f8]) ‘dictionary-lookup-word-in-text)

Pressing the F7 and F8 keys will then look up the word under the cursor in the thesaurus or dictionary, respectively.

← Older Post Blog Archive Newer Post →