Chapter 7. Minor Mode

In this chapter we’ll ratchet our Emacs programming dexterity up a notch by considering times when we don’t want extensions to apply to all buffers, but just to a particular type of buffer. For instance, when you’re in Lisp mode it’s nice to press C-M-a and have Emacs jump backwards to the beginning of the nearest function definition, but you don’t want or need that ability when you’re editing a textual document. The Emacs “mode” mechanism arranges things so that C-M-a does its magic only when you’re in Lisp mode.

This subject of modes in Emacs is a complex one. We’ll ease into it by first studying so-called minor modes. A minor mode coexists with a major mode in a buffer, adding a typically small amount of new editing behavior. Every Emacs user is familiar with major modes like Lisp and C and Text, but they may not be aware of little strings that appear on the “mode line” saying things like Fill when you’re also in Auto Fill minor mode.

We’ll create a minor mode that builds on Emacs’s idea of filling paragraphs. Our minor mode, Refill, dynamically fills paragraphs as you edit them.

Paragraph Filling

Filling a paragraph is the process of making all the lines in the paragraph the right length. Every line should be more or less equally long without extending past the right margin. Long lines should be split up at the spaces between words. Short lines should be lengthened with text from subsequent lines. Filling optionally includes justification, which is the process of adding whitespace throughout each line to make both margins come out even.

Most modern word processors keep paragraphs filled at all times. After every change, the text in the paragraph “flows” to keep the layout correct. Some detractors of Emacs point out that Emacs isn't as good as these other applications when it comes to filling paragraphs. Emacs does provide auto-fill-mode, but that only wraps the current line, and only when you insert whitespace beyond the ”right margin.“ It doesn’t keep paragraphs filled after deletions; it doesn’t fill any lines besides the current one; and it does nothing when insertions that occur near the left margin push other text past the right margin.

As an Emacs enthusiast, you can give one of three responses to the detractor who holds up some other program as the ne plus ultra of text editing:

  1. Glitzy features like on-the-fly filling of paragraphs are needed only to hide the program’s many inadequacies compared to Emacs (which you may feel free to list).

  2. You value content over form, so don’t need to see a paragraph continually refilled, but when you do feel the need, it’s a simple matter of pressing M-q to invoke fill-paragraph.

  3. Given a little Lisp hacking, Emacs can do on-the-fly paragraph filling just like the other program (and you may ask whether the other program can likewise be made to emulate Emacs).

This chapter is about option 3.

In order to make sure that the current paragraph is correctly filled at all times, we’ll need to recheck it after each insertion and each deletion. This may be computationally expensive, so we’ll want to be able to turn it on or off at will; and when we turn it on, we’ll want the behavior only in the current buffer, since it may not be suitable behavior for all buffers.

Modes

Defining a Minor Mode

Mode Meat

Naïve First Try

Constraining refill

Minor Adjustments

Eliminating Unwanted Filling

Trailing Whitespace