bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#61028: 30.0.50; [PATCH] [FEATURE] Balanced fill mode


From: Andrew Kensler
Subject: bug#61028: 30.0.50; [PATCH] [FEATURE] Balanced fill mode
Date: Mon, 23 Jan 2023 01:40:40 -0800
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.2

Greetings all,

The proposed patch attached adds a new minor balanced-fill-mode with an alternate line breaking algorithm for paragraph filling.  When enabled, it will try to neatly balance line lengths to reduce raggedness, avoid widows, avoid starting a new sentence on the last word of a line, avoid ending a sentence on the first word of a line, and so forth.  It is heavily inspired by the Knuth-Plass line breaking algorithm and uses dynamic programming to try to choose line breaks to minimize a cost function.

For example, consider the following mock paragraph as filled by the current greedy algorithm with the fill-column set to 15:

Ccc ccc a bb
dddd bb bb a
ccc a
jjjjjjjjjj a
eeeee a
hhhhhhhh bb
dddd.

With the new minor mode enabled, it would instead be filled much more nicely as:

Ccc ccc a
bb dddd bb
bb a ccc a
jjjjjjjjjj
a eeeee a
hhhhhhhh
bb dddd.

Often, the result is similar to simply having used a particular slightly narrower fill-column with the current greedy algorithm.  The advantage, however, is that it figures out the correct column automatically and per-paragraph.

The main piece of implementation is in the new (balanced-fill-break-lines) function in fill.el, with a modification to (fill-region-as-paragraph) to optionally call this before its current line breaking loop.  If it runs successfully, then point will be set to the end of the paragraph and that loop skipped.

Note that (fill-region-as-paragraph) has no hooks and is too monolithic to advise for this kind of thing, hence my hoping to upstream this change.

This is my first time contributing anything significant to Emacs or writing here.  I believe that the proposed patch covers all the major needs: the code itself, commit message, info documentation, announcement in NEWS, and a basic ERT test.  If there's anything I've missed or suggestions for improvements, please let me know.  (And I hope this is the correct mailing list and message format, too.)  I'll be happy to sign the copyright assignment paperwork if this looks like something you'd like to accept.

Cheers,
- Andrew

Attachment: 0001-Add-new-minor-balanced-fill-mode-to-filling.patch
Description: Text Data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]