7

I would like to start a different background color beginning from a function to the end.

Like this:

enter image description here

I'm aware you can define background colors from some faces like function and function names like foobar. But it affects only the background color of the words function and foobar. I would like to change the background color of the whole line where the function stats and end with it.

Why, you would ask? It would make it a lot easier for me to distinct which parts belongs to a funcion.

So I'm wondering if there is a pratical way to manage this in Emacs, of there is a Lisp package to achieve something like that?

1
  • An overlay with a face would be the easiest in my opinion -- similar to highlighting a particular region -- you can choose the color and the priority. For an example, take a look at the functions isearch-highlight and isearch-dehighlight
    – lawlist
    Nov 29, 2015 at 23:08

1 Answer 1

4
  1. You can use library Highlight to highlight a region using an overlay or text properties -- in particular, property face. To highlight the background, use a face that uses attribute background but not foreground.

    If you use text property face then you can also copy properties from some text and paste them to other text. So if your definition changes and becomes larger you can just copy some of the highlighted part and then paste its property face to the unhighlighted part.

    You could also use the functions defined in the library to do automatically (e.g. as a buffer-change hook or periodically, on a timer) extend the highlighting of a defun to accommodate changes to it.

    If you use an overlay then you can use the highlight-erasing commands (e.g., hlt-eraser) to remove only the last face you highlighted with. Drag the mouse over text where you want to remove the highlighting.

  2. You could use ordinary font-locking. Add a pattern to font-lock-keywords that uses a function that recognizes the start and end of your definition. See the Elisp manual, node Customizing Keywords as well as the parent node, Font-Lock Mode and its other children.


UPDATE after your comment

I think maybe you are asking about highlighting not the region but the lines touched by the region, and extending highlighting past the end of text (including past any whitespace chars). Is that it? If so, try this:

(defun hlt-highlight-lines (start end face msgp)
  "Highlight full lines in region, window-wide."
  (interactive (list (save-excursion (goto-char (region-beginning))
                                     (line-beginning-position))
                     (save-excursion (goto-char (region-end))
                                     (line-beginning-position 2))
                     nil
                     t))
  (hlt-highlight-region start end face msgp))
4
  • Drew, thanks for your reply. It took some time for me to reply to your answer because I wanted to try these applications. However, the highlight library didn't worked out for me. It only applies a background on where the text is. When the text ends, it stops with the background coloring. Any suggestion?
    – ReneFroger
    Dec 4, 2015 at 22:35
  • 1. I think maybe you are asking about extending highlighting past the end of text (including past any whitespace chars). Is that it? If so, try the command I added above. 2. Did you also try the font-lock answer? That should work too.
    – Drew
    Dec 5, 2015 at 0:51
  • In any case, I added that simple command to the highlight.el. If it helps you, good.
    – Drew
    Dec 5, 2015 at 0:59
  • Note that this works as long as you have a newline after the last line you want to highlight.
    – zck
    Feb 20, 2018 at 0:14

Your Answer

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

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