16.4 Text and Auto Fill Mode

Now we come to the part that turns on Text mode and Auto Fill mode19.

;;; Text mode and Auto Fill mode
;; The next two lines put Emacs into Text mode
;; and Auto Fill mode, and are for writers who
;; want to start writing prose rather than code.
(setq-default major-mode 'text-mode)
(add-hook 'text-mode-hook 'turn-on-auto-fill)

Here is the first part of this .emacs file that does something besides remind a forgetful human!

The first of the two lines in parentheses tells Emacs to turn on Text mode when you find a file, unless that file should go into some other mode, such as C mode.

When Emacs reads a file, it looks at the extension to the file name, if any. (The extension is the part that comes after a ‘.’.) If the file ends with a ‘.c’ or ‘.h’ extension then Emacs turns on C mode. Also, Emacs looks at first nonblank line of the file; if the line says ‘-*- C -*-, Emacs turns on C mode. Emacs possesses a list of extensions and specifications that it uses automatically. In addition, Emacs looks near the last page for a per-buffer, local variables list, if any.

Now, back to the .emacs file.

Here is the line again; how does it work?

(setq-default major-mode 'text-mode)

This line is a short, but complete Emacs Lisp expression.

We are already familiar with setq. We use a similar macro setq-default to set the following variable, major-mode20, to the subsequent value, which is text-mode. The single-quote before text-mode tells Emacs to deal directly with the text-mode symbol, not with whatever it might stand for. See Setting the Value of a Variable, for a reminder of how setq works. The main point is that there is no difference between the procedure you use to set a value in your .emacs file and the procedure you use anywhere else in Emacs.

Here is the next line:

(add-hook 'text-mode-hook 'turn-on-auto-fill)

In this line, the add-hook command adds turn-on-auto-fill to the variable.

turn-on-auto-fill is the name of a program, that, you guessed it!, turns on Auto Fill mode.

Every time Emacs turns on Text mode, Emacs runs the commands hooked onto Text mode. So every time Emacs turns on Text mode, Emacs also turns on Auto Fill mode.

In brief, the first line causes Emacs to enter Text mode when you edit a file, unless the file name extension, a first non-blank line, or local variables to tell Emacs otherwise.

Text mode among other actions, sets the syntax table to work conveniently for writers. In Text mode, Emacs considers an apostrophe as part of a word like a letter; but Emacs does not consider a period or a space as part of a word. Thus, M-f moves you over ‘it's’. On the other hand, in C mode, M-f stops just after the ‘t’ of ‘it's’.

The second line causes Emacs to turn on Auto Fill mode when it turns on Text mode. In Auto Fill mode, Emacs automatically breaks a line that is too wide and brings the excessively wide part of the line down to the next line. Emacs breaks lines between words, not within them.

When Auto Fill mode is turned off, lines continue to the right as you type them. Depending on how you set the value of truncate-lines, the words you type either disappear off the right side of the screen, or else are shown, in a rather ugly and unreadable manner, as a continuation line on the screen.

In addition, in this part of my .emacs file, I tell the Emacs fill commands to insert two spaces after a colon:

(setq colon-double-space t)

Footnotes

(19)

This section suggests settings that are more suitable for writers. For programmers, the default mode will be set to the corresponding prog-mode automatically based on the type of the file. And it’s perfectly fine if you want to keep the fundamental mode as the default mode.

(20)

We use setq-default here because text-mode is buffer-local. If we use setq, it will only apply to the current buffer, whereas using setq-default will also apply to newly created buffers. This is not recommended for programmers.