The context
I'm editing some text files that contain paragraphs of text. Some of these paragraphs are not correctly separated (paragraphs are separated with two newlines), so I need to manually edit the file to get the correct separation.
This is a sample file before editing.
line 1 of paragraph 1
line 2 of paragraph 1
line 3 of paragraph 1
line 1 of paragraph 2
line 2 of paragraph 2
line 3 of paragraph 2
This is the result after I edit it.
line 1 of paragraph 1
line 2 of paragraph 1
line 3 of paragraph 1
line 1 of paragraph 2
line 2 of paragraph 2
line 3 of paragraph 2
When I edit these files, it would be convenient for me to show an indicator whenever there are two consecutive newlines as a separator between paragraphs. Thus, it is more convenient for my eyes to know when a paragraph starts and when a paragraph ends in the file.
Current workarounds
whitespace-mode
whitespace-mode
can be used to show indicators for different whitespace characters. However, this is not enough for my eyes. I need something bigger that I can effortlessly notice.
Customize whitespace-mode
I customized whitespace-mode
in order for the indicators to be more noticeable.
(setf (alist-get 'newline-mark whitespace-display-mappings)
'(?\n [?$ ?$ ?$ ?$ ?$ ?$ ?$ ?$ ?$ ?$ ?\n]))
(set-face-attribute
'whitespace-newline nil
:background (face-attribute 'default :foreground))
The problem with this approach is that all newlines are highlighted and I only want to highlight those newlines that appear at the beginning of a file (since they indicate the separation between paragraphs in the use case that I explained above.)
line-prefix
text property
I used replace-regexp
to insert text properties when two newlines appear (please see image below).
The problem with this approach is that I need to manually execute replace-regexp
to point out newly inserted consecutive new lines. Is there any way to make Emacs automatically apply text properties whenever a given regexp is inserted? (Note the XY problem here, the X problem is to make Emacs show a clear indicator for consecutive new lines, and the Y solution is to make Emacs automatically apply text properties, I don't know if this is possible, but I'd be happy with another way to solve this problem).
The white indicator was inserted by executing replace-regexp
interactively and using the following replacement
<<newline>><<newline>>→ \,(propertize "<<newline>><<newline>>" 'line-prefix (propertize " " 'face 'ansi-color-inverse))
highlight-regexp
, e.g. viaM-:
(highlight-regexp "line" 'hi-pink)
. If you use a mode where font-lock is enabled (e.g. text-mode), then this uses font-lock to dynamically update the highlights. Now you could useM-:
(highlight-regexp "\n\n" 'hi-pink)
(or tryhl-line
face, or customizing the 'extend' property of some other face) to highlight consecutive newlines. The only problem is, that for some reason the dynamic updates don't work for the consecutive newlines (but in principle the answer is 'highlight-regexp' :).