An Emacs Minor Mode Tutorial

Over at the Google G+ Emacs Community, Agu Monkey has a pointer to an excellent tutorial on writing Emacs minor modes by Christopher Wellons. Most of us probably have no interest in writing a full-fledged minor mode (although Wellons' tutorial is complete enough for that) but it's often an easy way to provide some custom key bindings for a major mode or otherwise provide additional functionality.

Wellons takes us through the development of a trivial minor mode that merely inserts the word “foo” when called with a 【Ctrl+c f】 key sequence and keeps a count of how many foos are inserted. He shows how to make a buffer-local variable to count the insertions, how to instantiate a hook-function variable that users can insert hook functions into to run their own code when the minor mode is toggled, how to define a keymap for the minor mode, and how to activate the minor mode when a major mode is activated.

This is a really useful tutorial that you should read and then bookmark for the day that you want to add a little extra functionality to some mode or when you want to write a full blown minor mode. Highly recommended.

This entry was posted in Programming and tagged . Bookmark the permalink.
  • https://github.com/Fuco1/ Fuco

    Well, I wish he wrote that 3 months ago before I wrote 3500 lines long minor mode of my own :/

    Nice little tutorial, thanks for sharing (I don't really understand the g+ thing so I wouldn't get it from there)

  • http://nullprogram.com/ Chris Wellons

    That post was started just to document the hook caveat, since I had just discovered it, and it grew into a tutorial. It's often surprising to me which posts turn out to be the most useful for other people.