As Tom said, putting a display property on the newlines will kind of work.
The problem is to figure out which lines to wrap.
Here is an example to get you started:
(let ((eol-regexp "[[:graph:]] ?\\(\n\\)[^\t\n ]"))
(font-lock-add-keywords nil
`((,eol-regexp 1 '(face default display " ")))))
(visual-line-mode 1)
This will "remove" newlines if it is preceded by graphical characters and the next line is not indented or empty. You will have to experiment to find something that works for you.
Some more explanations:
font-lock-add-keywords
adds a list of rules to font locking (so font lock must be enabled. The first argument can be a mode (e.g. text-mode
); in our case it is nil, which means add the rule only for the current buffer.
- Our "rule" is
(,eol-regexp 1 '(face default display " "))
. The first element is the regular expression we built earlier; the second is the subexpression we are interested in; the third is the "face" we want to apply.
- While using a face for the newlines is of no use for us, we can also add text properties. So we use the default face and than add a display property witch replaces the substring 1 (i.e. the newline) with a space.
- If you shoot yourself in the foot, delete the buffer or remove the keyword with
font-lock-remove-keywords
.
- You might also want to add
display
to font-lock-extra-managed-props
(I have not tried this).
- The tricky part is to get the regexp right. "\(\n\)[^\n]" would match any newline that is not immediatly followed by another one. ".\{30,\}\(\n\)" would only match newlines preceeded by at least 30 characters.