2019-01-20 Filling and version control

It has been said a lot of times that when writing some (natural language) text with version control in Emacs, filling is a bad idea. Any change involving adding or deleting a significant number of characters and then refilling can result in all subsequent lines in a paragraph changed, and the diff looks really ugly then.

The solution usually proposed is putting each sentence on a separate line, and then just use visual-line-mode to wrap your lines on the screen without putting any hard newlines in.

Well, I sort of dislike this idea. The main problem I have with it is that I either tend to write rather longish sentences myself, or work on texts by other people that have long sentences. (This is definitely not optimal with respect to readability etc., but I’ve been working in academia for the past 14 years, and old habits die hard.) Having a wide screen makes visual-line-mode rather uncomfortable (well, I could try longlines-mode, which is deprecated, but reported to work – maybe some day), and looking at diffs with long lines is a pain in the neck anyway.

It occurred to me that one possible solution is to try to have the best of both worlds. Why not fill the paragraph, but introducing a newline after each sentence, too? (After writing this, I realized that I’m definitely neither the only one, not the first one to have this idea.) Then, when a change is large enough to “spill over” to next lines, it will only affect the current sentence, which seems to me an acceptable compromise. Here’s my attempt at the code which you could bind to M-q or some other key:

(defun fill-sentences-in-paragraph ()
  "Fill the current paragraph with a newline after each sentence."
  (interactive)
  (save-excursion
    (save-restriction
      (mark-paragraph)
      (narrow-to-region (point) (mark))
      (while (not (eobp))
	(fill-region-as-paragraph
	 (point)
	 (progn (forward-sentence) (point)))
	(delete-horizontal-space)
	(newline)))))

It is a quick-and-dirty solution (as many other Emacs hacks are), but I plan to start to use it and if there are any problems, I might refine them over time.

And now that I think about it, why not highlight long sentences as I write them? This would be a cool aid when writing prose. And Emacs already has the infrastructure to do that, like flymake. After a quick search, it turns out that I am (obviously) again not the first one to come up with that idea; it turns out that there are quite a few linters for English, and even one that works for more languages, including my mother tongue. So one of the future blog posts of mine will probably be a survey of these tools.

CategoryEnglish, CategoryBlog, CategoryEmacs, CategoryTeX