3 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4 ;; This program is free software; you can redistribute it and/or ;;
5 ;; modify it under the terms of the GNU General Public License as ;;
6 ;; published by the Free Software Foundation; either version 3, or (at ;;
7 ;; your option) any later version. ;;
9 ;; This program is distributed in the hope that it will be useful, but ;;
10 ;; WITHOUT ANY WARRANTY; without even the implied warranty of ;;
11 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ;;
12 ;; General Public License for more details. ;;
14 ;; You should have received a copy of the GNU General Public License ;;
15 ;; along with this program. If not, see <http://www.gnu.org/licenses/>. ;;
17 ;; Written by and Copyright (C) Francois Fleuret ;;
18 ;; Contact <francois@fleuret.org> for comments & bug reports ;;
19 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
21 ;; This handy function calls the dict unix command.
23 ;; I put in my ~/.emacs.el
25 ;; (when (load "lookup-dict" t)
26 ;; (define-key global-map [(control \?)] 'lookup-dict))
28 ;; On Debian, install the package dict, and to use it without
29 ;; connection install dictd, dict-foldoc, dict-gcide, dict-jargon and
32 (defun lookup-dict (&optional force)
34 "Gets definitions with the unix 'dict' command. Takes for word
35 either -- in this order, if possible -- the region, the word at
36 point, and a word given interactively. An optional universal
37 argument \\[universal-argument] forces the third."
43 ;; Word given as parameter
46 ;; Region (Emacs 23 has region-active-p)
47 (if (functionp 'region-active-p)
48 (and (region-active-p)
49 (buffer-substring (region-beginning) (region-end)))
51 (buffer-substring (region-beginning) (region-end))
55 (thing-at-point 'word)
59 (when (string= word "") (setq word (read-input "Word: ")))
61 (setq word (replace-regexp-in-string "[^a-zA-Z\- \.]" "" (or word "")))
63 (let ((name (concat "*definition of " word "*")))
65 (if (get-buffer name) (switch-to-buffer name)
67 (switch-to-buffer (generate-new-buffer name))
71 (let ((map (make-sparse-keymap)))
74 (define-key map "q" 'kill-this-buffer)
75 (define-key map (kbd "RET") 'lookup-dict)
76 (define-key map " " (lambda () (interactive)
77 (when (condition-case nil (scroll-up) (error t))
78 (beginning-of-buffer))))
81 (insert "\nPress <space> to go one page down, <enter> to lookup a word and `q' to\nkill this buffer\n\n")
83 (if (string= word "") (insert "Empty word!\n")
85 ;; Insert the response of the 'dict' command
89 (call-process "dict" nil (current-buffer) nil word))
91 (error (insert "Can not find the unix `dict' command, is it installed ?\n\n")))
93 ;; Remove the spurious whitespaces, underline the "From ..."
94 ;; and highlight the searched word
96 (delete-trailing-whitespace)
99 (goto-char (point-min))
100 (while (re-search-forward "^From.*$" nil t)
101 (add-text-properties (match-beginning 0)
104 (goto-char (point-min))
105 (while (re-search-forward (concat "[^a-zA-Z]\\\(" word "\\\)[^a-zA-Z]") nil t)
106 (add-text-properties (match-beginning 1)
111 (setq buffer-read-only t)
112 (set-buffer-modified-p nil)