Clicky

Harry R. Schwartz

Software engineer, nominal scientist, gentleman of the internet. Member, ←Hotline Webring→.

· 1B41 8F2C 23DE DD9C 807E A74F 841B 3DAE 25AE 721B

engine-mode

Published 12 May 2014. Tags: emacs, lisp.

I’ve been traveling between NYC and Boston pretty frequently for the past couple of months. It’s mostly a hassle, but the upside is that I get a whole lot of time on the train. I usually just read, but sometimes I crank out a little code.

Last Tuesday I decided to take a crack at writing an Emacs minor mode, which I’ve never done before. It’s actually really easy! In the simplest case you just define the minor mode and provide it, which only takes a few lines of code. Once it’s written you can submit it to a package repository.

I had a little elisp function in my dotfiles that searched Google for me, and I decided to pull that out and generalize it into a more useful utility. The end result was engine-mode, a global minor mode that allows you to define arbitrary search engines, bind them to keys, and use them to search the web for the current region. It’s a lot like the keyword search functionality in most web browsers, but integrated into Emacs. It looks a little like this:

(require 'engine-mode)
(engine-mode t)

(defengine github
  "https://github.com/search?ref=simplesearch&q=%s"
  "g")

Boom! Once that’s defined, I can search GitHub by selecting some text and hitting C-c / g. That’ll execute my search in my default browser (Firefox, in my case). I can define as many engines as I like, and each will have a keybinding prefixed with C-c /.

Once the code looked reasonably OK, I forked MELPA and submitted a recipe (which is also a really painless process). Steve gave me some helpful feedback, and after a few tweaks I got it merged in.

Long story short: you can now package-install engine-mode! If you have any problems, please submit an issue or pull request on GitHub.

This whole project was a lot of fun! Having never gone through the process of writing and submitting a minor mode before, I was pleasantly surprised with how easy it was. If you’ve ever thought about creating a minor mode, give it a shot! The barrier to entry is way lower than you think.