Emacs is a great editor, I won't elaborate on this as there's countless blog posts already celebrating its countless features. After some years of usage, anyone using Emacs end up with a bloated .emacs file that may contains some good tips. I'll be explaining some of the tricks I'm using in a series of posts of which this one is the first.
Web Search Shortcuts
It's a known fact (in the sense that it's a common Intertubes meme) that some people use their computer exclusively through Emacs. I'm not that fond of this editor and prefer to do things like navigating the web with a real browser. Emacs as a way to send URLs to one, using the browse-url function. Feeding an unencoded URL to it could yield a bad request though. To fix that, we can use url-hexify-string from the url library. Here's a simple example of putting these functions to good use.
(require 'url) (defun google () (interactive) (let ((url (if (and transient-mark-mode mark-active) (buffer-substring-no-properties (region-beginning) (region-end)) (thing-at-point 'symbol)))) (browse-url (concat "http://www.google.com/search?" (format "q=%s" (url-hexify-string url))))))
This interactive function let us do a simple search with the text under the currently active region or with the symbol found under the cursor if there's no active region. This can be useful, you're no longer forced to copy-paste text from Emacs to your browser.
Next, we could try to send more complex queries to our preferred search engine, which in this case is Google. Standard library documentation such as Java's API specification generally have URLs that are easily queryable. With Google's allinurl keyword, we can make a function to search for a specific class symbol.
(defun search-all-in-url (query site inurl) (concat "http://www.google.com/" (format "search?q=allinurl:%s+site:%s+inurl:%s&btnI" (url-hexify-string query) (url-hexify-string site) (url-hexify-string inurl)))) (defun web-help (site inurl) (browse-url (search-all-in-url (thing-at-point 'symbol) site inurl)))
With web-help, we can now define custom interactive functions. Currently in my .emacs file, I've got two such functions, one for Java and the other for Ruby.
(defun java-help () (interactive) (web-help "java.sun.com" "/javase/6/docs/api/")) (defun ruby-help () (interactive) (web-help "www.ruby-doc.org" "/core/"))
Then, it's a simple matter of adding key bindings for our new interactive functions. Personally, I like to have them bound to the lower F* keys.
(global-set-key [(f1)] 'google) (global-set-key [(f2)] 'java-help) (global-set-key [(f3)] 'ruby-help)
You might already use these shortcuts for other things (I think that F3 serves to start recording macros) but you certainly still have some unbound key combinations!
Update: I've corrected the second code example that I messed up while writing this post.
No comments:
Post a Comment