glasserc/ethan-wspace — pull request #16 by njsmith (2013-04-12 14:06:40+00:00)
Title: Attempt to preserve the position of point when cleaning a buffer.

Closes: #15

This commit does three things:
- Adds a new :clean-fixup function to the general cleaner framework,
which is called from the after-save hook.
- Defines a :clean-fixup function for eol whitespace, which restores
any whitespace immediately preceding point.
- Defines a :clean-fixup function for many-nls-eof whitespace, which
restores newlines at the end of the file if this is necessary to
preserve the position of point.

This seems to work great EXCEPT that if at the end of your file you
have in order: (a) several newlines, (b) several horizontal whitespace
characters, (c) point -- then the newlines, but not the horizontal
whitespace, will disappear on save. I'm not sure what the correct fix
for this is, because it appears to reflect a more fundamental bug in
ethan-wspace. The problem is that the eol whitespace cleaner also
cleans extraneous eof newlines. E.g., add several newlines at the end
of a file, make sure your modeline says "ew:Mnlt", and then save. The
newlines will be cleaned, even though trailing newlines are in
highlight-don't-clean mode. But my fixup code assumes that the eol cleaner only modifies individual lines.

So what causes the bug in this commit is: first the many-nls-eof
cleaner runs, and does nothing. Then the eol cleaner runs, and deletes
not just the trailing whitespace on the current line, but all the
preceding blank lines. Then, my fixup code runs, and re-inserts the
trailing whitespace, but not the trailing blank lines.

Option A ("correct"): fix the eol cleaner so it never deletes whole
lines. Also make sure that it runs before the eof cleaners, because it
might change where the eof is. Now the code in this commit will
magically start working.

Option B ("expedient"): add an explicit workaround to the eol fixup
code that checks for this situation explicitly, and re-adds newlines
in this case.

Thoughts?