By default, Emacs highlights the non-rectangular selection; alternatively, you can use (rectangle-mark-mode)
to show the rectangular selection. Instead, I'd like to always show both, by highlighting the rectangle in one face, and the normal selection outside the rectangle with another. How do I set that up?
1 Answer
There’s no simple variable you can set to control this, but you can do it with some programming if you want.
There is a variable called redisplay-highlight-region-function
whose value is a function to be called whenever the region needs to be shown. It creates an overlay that applies the region
face to the selection (or moves it to a new start/end positions if one already exists).
Meanwhile the rectangle code in rect.el
adds an :around
method to this function. An :around
method is always called instead of the original function, and it can call the original if it wants. This is a way to extend the behavior of an existing function without modifying it. The new method is called rectangle--highlight-for-redisplay
. The first thing it does is check to see if the variable rectangle-mark-mode
is set. If it isn’t, then it calls the original redisplay-highlight-region-function
and does nothing else. Otherwise it goes on to add overlays to the buffer to display the rectangular region (a surprisingly complicated task, as you’ll see from the code).
What you will want to do is make rectangle--highlight-for-redisplay
do both things, rather than just one or the other.
Take note that both of these functions return the overlay that they create, and that the caller saves this overlay and passes it back the next time it calls them. Presumably this is a performance optimization. When you create two overlays, you will want to return a cons containing both of them so that this optimization is maintained. Of course, when you get back a cons you will want to pass just the first region to the original redisplay-highlight-region-function
.