Mode-Aware Google Help in Emacs
While surfing the other night, I ran across a nice Emacs Lisp
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
.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
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.
You can see different uses of the various arguments in the code:
- CSS: searches www.w3schools.com and looks for URLs with
- 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
- 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
This has proved to be really useful in the few days that I've been using it.