GNU Emacs
Emacs
Dashboard

25.6.4 Adaptive Filling

The fill commands can deduce the proper fill prefix for a paragraph automatically in certain cases: either whitespace or certain punctuation characters at the beginning of a line are propagated to all lines of the paragraph.

If the paragraph has two or more lines, the fill prefix is taken from the paragraph’s second line, but only if it appears on the first line as well.

If a paragraph has just one line, fill commands may take a prefix from that line. The decision is complicated because there are three reasonable things to do in such a case:

  • Use the first line’s prefix on all the lines of the paragraph.

  • Indent subsequent lines with whitespace, so that they line up under the text that follows the prefix on the first line, but don’t actually copy the prefix from the first line.

  • Don’t do anything special with the second and following lines.

All three of these styles of formatting are commonly used. So the fill commands try to determine what you would like, based on the prefix that appears and on the major mode. Here is how.

If the prefix found on the first line matches adaptive-fill-first-line-regexp, or if it appears to be a comment-starting sequence (this depends on the major mode), then the prefix found is used for filling the paragraph, provided it would not act as a paragraph starter on subsequent lines.

Otherwise, the prefix found is converted to an equivalent number of spaces, and those spaces are used as the fill prefix for the rest of the lines, provided they would not act as a paragraph starter on subsequent lines.

In Text mode, and other modes where only blank lines and page delimiters separate paragraphs, the prefix chosen by adaptive filling never acts as a paragraph starter, so it can always be used for filling.

The variable adaptive-fill-regexp determines what kinds of line beginnings can serve as a fill prefix: any characters at the start of the line that match this regular expression are used. If you set the variable adaptive-fill-mode to nil, the fill prefix is never chosen automatically.

You can specify more complex ways of choosing a fill prefix automatically by setting the variable adaptive-fill-function to a function. This function is called with point after the left margin of a line, and it should return the appropriate fill prefix based on that line. If it returns nil, adaptive-fill-regexp gets a chance to find a prefix.