Emacs: Command to Lookup Doc or Search Web 💠

By Xah Lee. Date: . Last updated: .

Command to Search Web, or Lookup Function Doc, Word Definition

Here's how to write a command to lookup the web of the word under cursor.

This is useful when you are writing a major mode for a programing language. You want to provide a command that lookup function (the word under cursor)'s documentation.

To make this command search different search engine, such as google, yandex, baidu, dictionary, image search, YouTube search etc, you just need to change the URL.

(require 'browse-url) ; part of gnu emacs

(defun my-lookup-wikipedia ()
  "Look up the current word or selection in Wikipedia, in a web browser."
  (interactive)
  (let (xword xw2)
    (setq xword
          (if (region-active-p)
              (buffer-substring-no-properties (region-beginning) (region-end))
            (thing-at-point 'word)))
    (setq xw2 (replace-regexp-in-string " " "_" xword))
    (browse-url (concat "http://en.wikipedia.org/wiki/" xw2))
    ;; (eww myUrl) ; emacs's own browser
    ))

Emacs, Web Browser

Emacs Lisp, writing a major mode. Essentials