0

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.

An screenshot of a buffer in Emacs in which whitespace-mode has been enabled, the character "$" is shown whenever a newline is shown, the space the character "$" is less than the space that occupies the indicator from the previous image

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))

enter image description here

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))

An screenshot of a buffer in Emacs in which a white separator has been used to separate paragraphs, the white separator is displayed thanks to the text property line-prefix, the white separator was inserted by providing a sexp to replace-regexp

1
  • 1
    For symbols, this can easily be achieved using highlight-regexp, e.g. via M-: (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 use M-: (highlight-regexp "\n\n" 'hi-pink) (or try hl-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' :). Sep 2, 2023 at 10:22

1 Answer 1

1

As suggested by @dalanicolai, you could use highlight-regexp with a face that sets :extend to t. As you edit the file, lines will be colored.

(defface my/face
  '((t (:background "gray" :extend t)))
  "")

(highlight-regexp "^
" 'my/face)

enter image description here

NOTE: In my system, sometimes the regexp is not automatically highlighted when adding new content to the buffer. I solved this by enabling and disabling font-lock-mode and calling font-lock-update. I don't remember the exact order in which I called those functions. I haven't looked into whether this is a bug or some of my configuration affecting font-lock-mode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.