Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs - how to use colors to visually accentuate the function the cursor is in?

Inspired by ia Writer's focus mode, I'm interested in using font + background colors in emacs to accentuate the function the cursor is in and visually cue the rest of the code as the background (I use C++, but it would be nice if this worked regardless of the programming language).

Ideally the font color of code outside the function would be dimmed (this is how focus mode works). A simpler solution probably be to change the background color slightly for the function that the cursor is currently in. How can this be done?

like image 358
daj Avatar asked Jul 09 '12 11:07

daj


2 Answers

Nothing like this exists AFAIK. If you want it to write it yourself, here is a sketch:

  1. Write a routine that determines the boundaries of the current function. The easiest way to do this is with (bounds-of-thing-at-point 'defun).

  2. Write a routine that, when given the bounds of a region, gets the background face property of the region of the region, darkens it, and applies the new face to the region.

  3. Override font-lock-fontify-region-function (see here) with a routine that calls the original value of this variable, differences the region given with the region of the current defun (using #1), and then applies routine #2 to the remaining region.

I would prefer overriding font lock to, say, using jit-lock-register because you need to control the order of fontification.

HTH!

like image 175
Christopher Monsanto Avatar answered Nov 20 '22 03:11

Christopher Monsanto


Which-function mode is used to highlight the current function. Try it to see if it helps you, and see if this post helps you:

Emacs Setting which-function-mode

like image 23
alinsoar Avatar answered Nov 20 '22 04:11

alinsoar