Emacs: Command to Lookup Reference: {Dictionary, Wikipedia, Google, PHP, Perl, …}

, , …,

This page shows you how to setup emacs so that you can press a button and emacs will switch to your web browser and show the documentation for the word under cursor.

The documentation can be the Google Search result, or English definition of a word, programing language documentation of a function, Wikipedia lookup, emacswiki, or any online reference site.

lookup-word-on-internet.el

Download at: http://code.google.com/p/ergoemacs/source/browse/packages/lookup-word-on-internet.el

Call lookup-google, your web browser will come to front and show the Google Search results of the word under cursor in emacs.

The following are explanation of the lisp code.

Perl, PHP, Ruby, Google, Wikipedia, …

The following is a example of looking up Wikipedia:

(defun lookup-wikipedia ()
  "Look up the word under cursor in Wikipedia.
If there is a text selection (a phrase), use that.

This command switches you to your browser."
 (interactive)
 (let (myWord myUrl)
   (setq myWord
         (if (region-active-p)
             (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)
   ))

You can setup a key such as F8 for easy access. See: Emacs: How to Define Keys.

You can change the URL in the above definition to other reference sites. Here's the URL syntax for some popular language or reference sites. The “�” should be your search string.

SourceURL Format
Perlhttp://perldoc.perl.org/search.html?q=�
PHPhttp://us.php.net/�
LSLhttp://wiki.secondlife.com/wiki/�
AutoHotkeyhttp://www.autohotkey.com/docs/commands/�.htm
Wikipediahttp://en.wikipedia.org/wiki/�
Googlehttp://www.google.com/search?q=�
binghttp://www.bing.com/search?q=�
Wolfram|Alphahttp://www.wolframalpha.com/input/?i=�

If you know a URL syntax for Ruby, Python, lisp, or other langs, please add at the comment below. Thanks.

Looking Up English Dictionary

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 (region-active-p)
             (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. Or you can define your own key.

List of Dictionary Websites

You can change the URL to a different online dictionary reference website. For a list of online dictionaries and their URLs, see: Online English Dictionary Tools.

Lookup Word Definition Within Emacs

If you prefer to lookup definitions within emacs, see: Emacs Dictionary Lookup.

Launching a Specific Browser

See: Emacs Lisp: browse-url Function to View URL in a Web Browser.

blog comments powered by Disqus