Improved the code which strips trailing whitespace.

Instead of trying to edit the document visible in the actual buffer, we
implement our own document writer class, which simply strips the
trailing whitespace before writing.

Editing the document in the editor doesn't work very well, due to the
fact that it's hard to maintain the user's cursor position and selection
when saving, if whitespace is being removed all over the place. This
implementation writes trailing-whtespace-clean files, and additionally
avoids messing with the user's cursor, so it's easier to keep working
after saving.