Flowed text with Mutt and Emacs

Users of the mail reader Mutt probably know that there is some incompatibility in how plain text mail messages are displayed in windows and screens of various widths. Fixed-length lines are often displayed badly on screens narrower than the lines, notably on smartphones. On the other hand, some software makes each paragraph a single long line, and word-wraps it to fit, but that text looks bad when displayed by software that doesn't word-wrap it.

The MIME type text/plain format=flowed (RFC 3676) is an attempt to fix this. It encodes plain text so that it can be automatically word-wrapped to fit wherever it's displayed, but will be displayed reasonably, at a fixed width, by mail readers that know nothing but fixed text. Mutt can send such flowed-text messages, but relies on an external text editor to compose all messages, fixed text or flowed.

The text editor vim can be configured to compose flowed text, at least partially. The text editor Emacs doesn't have that ability built in.

I've written some code that makes it possible for Emacs to do that. It adds an Emacs major mode that composes flowed text in coordination with Mutt.

This mode is WYSIWYG; it displays the text being edited approximately as it would be displayed by a mail reader that understands flowed text. Each paragraph is a single line, maybe very long, and is automatically word-wrapped to fit the width of the Emacs window.

Here is a sample message being composed with mutt-flowed-text-mode, in an Emacs X window 80 columns wide.

Here is that same edit with the window wider.

Here is that message after it was sent and received, displayed by Mutt, wide and narrow.

How it works

Flowed text makes each paragraph logically a single very long line, but with soft line breaks every 78 characters or less. A soft line break is CRLF preceded by a space character -- the space is the last character of the line. Software that knows how to display flowed text drops those soft line breaks, and word-wraps the paragraph to fit. Software that knows only fixed text displays the flowed text as it is, lines about 70-78 characters long, many of those lines having a space character at the end.

Mutt flowed text mode converts flowed text in the temporary file from Mutt to single-line paragraphs in the editing buffer, and turns on the minor mode visual-line-mode to word-wrap those long lines and edit them easily. When the file is saved, the text in the buffer is converted back to flowed text in the file. That is, the code removes soft line breaks on read, and inserts them on write.

Mutt flowed text mode adds these key bindings, to help handle very long lines:


	M-n	next-logical-line
	M-p	previous-logical-line
    

Mutt flowed text mode can be used with Mutt configuration variable $edit_headers set to yes or no. If a header section is included, the text conversion functions detect and avoid it, and convert only the message body.

Emacs text fill would break single-line paragraphs into multiple lines, and so convert flowed text to fixed text. So, flowed text mode turns off auto-fill mode, and the user should not use the text fill commands.

See the Emacs manual for details of visual-line-mode and text filling.

Mutt flowed text mode does not completely implement text/plain format=flowed: it does not space-stuff the flowed text. Mutt expects and requires it not to; Mutt itself space-stuffs the text. Also, Mutt flowed text mode assumes DelSp=no, because Mutt implements only that.

Here is Emacs directly displaying the received sample message in its maildir message file, with trailing whitespace highlighted red. This shows that it was sent as flowed text.

How to use it

To configure Mutt to display flowed text messages, word-wrapped to the width of the screen or window, put this in your .muttrc:


      set reflow_text = yes	#(default) display flowed text as flowed
      set smart_wrap = yes	#(default) word-wrap
      set reflow_wrap = 0	#wrap at right edge (default is 78)
      set reflow_space_quotes = no    #adjust to taste (default is yes)
    

To configure Mutt to compose and send messages as text/plain format=flowed, with Mutt flowed text mode for Emacs, download this file of Emacs Lisp and put it somewhere convenient, and put this in your .muttrc:


	set text_flowed = yes	#make f=f hdr, space-stuff what comes back from editor

	#or wherever you put the Lisp file
	set editor = 'emacs --no-splash --load=$HOME/mutt-flowed-text-mode.el %s --funcall=mft-setup'