Xah Lee, 2008-11, 2010-01-09
This page shows you how to use emacs as a dictionary application, that allows you to lookup the definitions of a word under cursor, or any general reference such as Wikipedia, Google, or lookup documentation of computer language's keywords.
You can setup your emacs so that, pressing a button will lookup the definition of the word under cursor.
Place the following in your emacs init file:
(defun lookup-word-definition () "Look up the current word's definition in a browser. If a region is active (a phrase), lookup that phrase." (interactive) (let (myword myurl) (setq myword (if (and transient-mark-mode mark-active) (buffer-substring-no-properties (region-beginning) (region-end)) (thing-at-point 'symbol))) (setq myword (replace-regexp-in-string " " "%20" myword)) (setq myurl (concat "http://www.answers.com/main/ntquery?s=" myword)) (browse-url myurl) ;; (w3m-browse-url myurl) ;; if you want to browse using w3m )) (global-set-key (kbd "<f6>") 'lookup-word-definition)
With the above, pressing F6 will launch your browser and lookup definition of the word under cursor.
If you do a lot vocabulary research, you can have emacs open several dictionaries in one shot. (just add more of the “browse-url” line.)
You can change the url to a different online dictionary reference website.
Here are some other online dictionary sites and their url search syntax, using sample word “curlicue”. AHD means American Heritage Dictionary.
http://education.yahoo.com/reference/dictionary/entry/curlicue (AHD) http://www.answers.com/main/ntquery?s=curlicue (AHD) http://en.wiktionary.org/wiki/curlicue (wiktionary) http://m-w.com/dictionary/curlicue (Merriam Webster) http://www.askoxford.com/concise_oed/curlicue (Compact Oxford Eng Dict ) http://www.yourdictionary.com/curlicue http://dictionary.reference.com/browse/curlicue (AHD, Random House, WordNet, ...) http://www.dict.org/bin/Dict?Form=Dict2&Database=*&Query=curlicue (OpenSource Dicts)
The following is a example of looking up Wikipedia:
(defun lookup-wikipedia () "Look up the word under cursor in Wikipedia. This command generates a url for Wikipedia.com and switches you to browser. If a region is active (a phrase), lookup that phrase." (interactive) (let (myword myurl) (setq myword (if (and transient-mark-mode mark-active) (buffer-substring-no-properties (region-beginning) (region-end)) (thing-at-point 'symbol))) (setq myword (replace-regexp-in-string " " "_" myword)) (setq myurl (concat "http://en.wikipedia.org/wiki/" myword)) (browse-url myurl) ))
Here are some example urls for some reference lookup sites.
Perl http://perldoc.perl.org/search.html?q=‹SearchWord› PHP http://us.php.net/‹SearchWord› LSL http://wiki.secondlife.com/wiki/‹SearchWord› AHK http://www.autohotkey.com/docs/commands/‹SearchWord›.htm Wikipedia http://en.wikipedia.org/wiki/‹SearchWord› Google http://www.google.com/search?q=‹SearchWord›
If you want to lookup definition entirely in emacs without a web browser, you can use a dictionary client. You can download it at: http://www.myrkr.in-berlin.de/dictionary/index.html by Torsten Hilbrich.
Follow the installation instructions in the file. Once you installed it, you can put this in your emacs init file:
;; press 0 on keypad to lookup definition (global-set-key (kbd "<kp-0>") 'dictionary-lookup-definition)
You can set up the default dictionary to use. You do this by typing 【Alt+x customize-group】, then “dictionary”, then scroll to “Dictionary Default Dictionary”. If you want WordNet to be your default dictionary, put “wn” in the field. Then, click “Save for Future Sessions” at the top, then “Finish”.
To search all available dictionaries on the server, use the keyword “*”, which is default. (The result of looking up all dictionaries is sometimes confusing.) To see a list of available dictionaries, first lookup a word, then move your cursor to the “[Select Dictionary]” and hit 【Enter】. The word before the colon is the keyword to use.
There are few minor problems with “dictionary.el”.
The following wrapper code fixes above problems:
(defun lookup-word-def (&optional arg) "Look up the word's definition in WordNet, Webster 1913, Moby Thesaurus. If there is a text selection (a phrase), lookup that phrase. This command needs “dictionary.el” installed." (interactive) (require 'dictionary) (let (myurl meat navigText pos) (setq myword (if arg arg (if (and transient-mark-mode mark-active) (buffer-substring-no-properties (region-beginning) (region-end)) (thing-at-point 'symbol)) ) ) (setq myword (replace-regexp-in-string "á\\|à\\|â\\|ä" "a" myword)) (setq myword (replace-regexp-in-string "é\\|è\\|ê\\|ë" "e" myword)) (setq myword (replace-regexp-in-string "í\\|ì\\|î\\|ï" "i" myword)) (setq myword (replace-regexp-in-string "ó\\|ò\\|ô\\|ö" "o" myword)) (setq myword (replace-regexp-in-string "ú\\|ù\\|û\\|ü" "u" myword)) ;; (dictionary-do-search "fancy" "web1913" 'dictionary-display-search-result) (dictionary-new-search (cons myword "wn")) (setq meat (buffer-substring (point-min) (point-max))) (dictionary-new-search (cons myword "web1913")) (setq meat (concat meat (buffer-substring (point-min) (point-max)))) (dictionary-new-search (cons myword "moby-thes")) (setq meat (concat meat (buffer-substring (point-min) (point-max)))) (switch-to-buffer "*my dict results*") (erase-buffer) (dictionary-mode) (insert meat) (goto-char 1) (forward-line 2) (setq pos (point)) (setq navigText (buffer-substring 1 pos)) (goto-char 1) ;; remove navigation text (while (search-forward "[Back] [Search Definition] [Matching words] [Quit] [Select Dictionary] [Select Match Strategy] " nil t ) (replace-match "-----------------------------------------------------------------" t t)) ;; remove message about how many def found. e.g. “1 definition found”. (goto-char 1) (while (search-forward-regexp "[0-9]+ definitions* found\n\n" nil t) (replace-match "" t t)) (goto-char (point-max)) (insert navigText) (goto-char 1) (other-window 1) )) ;; press 0 on keypad to lookup definition (global-set-key (kbd "<kp-0>") 'dictionary-lookup-definition)
The above code looks up WordNet, Webster 1913, Moby Thesaurus, and combine their results in one pane. You can change or add more by modifying the above code.
Note: 2008-11-03. Sometimes if there is no result in one of the dictionary, the function will choke. I haven't had time to dig, but for now a work-around is to comment out the following section in “dictionary.el”:
(unless nomatching
(beep)
(insert "Word not found, maybe you are looking "
"for one of these words\n\n")
(dictionary-do-matching word
dictionary
"."
'dictionary-display-only-match-result)
(dictionary-post-buffer))