David Avraamides Code and other geekery

Mode-Aware Google Help in Emacs

While surfing the other night, I ran across a nice Emacs Lisp function for using Google to do context-sensitive help from inside Emacs. I liked how simple it was, and had always been curious about writing some Emacs lisp code (my .emacs file is a random collection of snippets I egregiously pilfered from various locations), so I thought I would enhance the solution by making it aware of what major-mode I was in.

Building the Search URL

John Conners, the author of the original post built his URLs with the site: and inurl: keywords to limit the search to a specific site and with a specific string or pattern in the URL. He also appends the "I Feel Lucky" button (btnI) query string parameter so you will jump directly to the top hit. I simply modified his function to make the site, inurl and lucky parameters optional to give me a little more control over the search.

(defun search-site-url (keyword &optional site inurl lucky)
  "Do a Google search for KEYWORD. Restrict to SITE and INURL, if specified.
Jump to best match (I Feel Lucky) if LUCKY set.
"
  (concat "http://www.google.com/"
          (format "search?q=%s" (url-hexify-string keyword))
          (if site (format "+site:%s" (url-hexify-string site)))
          (if inurl (format "+inurl:%s" (url-hexify-string inurl)))
          (if lucky "&btnI")))

Making the Search Mode-Aware

Next I changed John's wxhelp function to check the major-mode you are editing in, and use that to configure the arguments to the search-site-url function (I also renamed the function to the more general context-help). This makes it easy to add new search modes and customize them so they work well based on what you are looking for.

(defun context-help ()
  "Open a browser window showing documentation for the word under the point.
Uses `major-mode' to optionally refine the search to a specific web site,
or a specific pattern in the URL. Defaults to a simple keyword search.
Uses `search-site-url' to do the actual search.
"
  (interactive)
  (browse-url
   (apply 'search-site-url
          (thing-at-point 'symbol)
          (cond
            ((equal major-mode 'css-mode)
             '("www.w3schools.com" "/css/" t))
            ((equal major-mode 'emacs-lisp-mode)
             '("www.gnu.org" "/emacs/"))
            ((or (equal major-mode 'html-mode)
                 (equal major-mode 'html-helper-mode))
             '("www.htmlquick.com" "/reference/" t))
            ((equal major-mode 'javascript-mode)
             '("www.w3schools.com" nil t))
            ((equal major-mode 'python-mode) 
             '("docs.python.org" "/ref/" t))
            (t nil)))))

You can see different uses of the various arguments in the code:

  • CSS: searches www.w3schools.com and looks for URLs with /css/ in them.
  • Emacs Lisp: searches www.gnu.org and looks for /emacs/ in the URL, but does not automatically jump to the top hit. I did this because sometimes I'm looking for the Emacs editor sections of the help and sometimes I'm looking for the Emacs Lisp sections. This way I just get the usual Google search hits and can scan them to find the best one.
  • HTML: for HTML help, I use the same search against www.htmlquick.com in either html-mode or html-helper-mode.
  • JavaScript: searches www.w3schools.com again, but does not limit with the inurl: keyword. This is because JavaScript language pages and JavaScript DOM pages do not have a common URL substring on that site.
  • Python: searches the standard Python documentation web site. This is the one I've been using the most and seems to provide good results most of the time.
  • The final default case just does a Google keyword search. This is reached if none of the major-mode cases match.

This has proved to be really useful in the few days that I've been using it.


2 Comments

Posted by
John Conners
Thursday July 24, 2008
10:27 a.m.

Thats a very handy piece of code!

I wonder if it would be possible to have multiple entries for a single language: say when cpp programming with multiple libraries..

..I guess the key would be to munge multiple sites and keywords into the Google URL..


Posted by
David Avraamides
Thursday July 24, 2008
7:40 p.m.

@John Yes, I've run into that already - where the help I want does not reside all under one site. You can do this with Google's search syntax using OR:

python urllib site:docs.python.org OR site:www.voidspace.org.uk

so it's certainly possible.


  • Comments are moderated and will be visible after a short delay.
    Markdown allowed:
    _emphasized_, __strong__, [text](url), indent four spaces for code, begin code block with indented :::language for highligting (eg: bash, c, html, js, mysql, objc, python, html, ...)