0

I'd like to highlight only the text in between the leading and trailing whitespace on each line. For example, I'd only like to highlight the region marked by the double arrow <--> for the following line:

    public static void printSum() {...}
    <--------------------------------->

I run Doom Emacs and understand that hl-line-mode is responsible for highlighting the current line. It appears that if I defined my own hl-line-range-function to do what I wanted, I could apply this to any major mode (I want it on the dashboard).

It seems that hl-line-range-function needs to return a cons cell of the starting and ending "positions" of where I'd like to highlight. What is the "position" data type? How is it represented? Can I simply return the value of functions like end-of-visual-line and back-to-indentation?

3
  • emacs.stackexchange.com/tags/elisp/info
    – Drew
    Commented Dec 27, 2022 at 0:15
  • A guess, without looking, is that "position" is just a buffer position. If so, the after moving with either of those functions you need to return (point). And you can wrap the movement and call to point in a save-excursion. On the other hand, maybe there's a function that gives you the position directly. At least the doc of those functions doesn't claim that they do, so even if they might you shouldn't count on that.
    – Drew
    Commented Dec 27, 2022 at 0:19
  • It would be easier to do this by setting the :extend attribute of the hl-line face to nil. When it's set to nil, the hl-line highlight will only affect positions of the line where characters are located (including whitespace). If you want it to only be effective in the dashboard, you can create a new face and use setq-local in dashboard-mode's hook to set that face as the value of hl-line-face only in the dashboard buffer. Commented Feb 1, 2023 at 6:22

1 Answer 1

0

As Drew suggested, this works:

(defun my-hl-line-range-function ()
  (let ((beg (save-excursion
           (back-to-indentation)
           (point)))
        (end (save-excursion
           (end-of-visual-line)
           (point))))
    (cons beg end)))

(setq hl-line-range-function #'my-hl-line-range-function)

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.