The following advice does what you want. When the block cursor is "on" the opening parenthesis, the closing parenthesis is highlighted. When the block cursor is "on" the closing parenthesis, the opening parenthesis is highlighted.
(advice-add show-paren-data-function
:around
(lambda (orig-fun)
(cond ((looking-at "\\s(")
(funcall orig-fun))
((looking-at "\\s)")
(save-excursion (forward-char 1) (funcall orig-fun))))))
The first cond clause handles an opening delimiter character (e.g. when the cursor is "on" an opening parenthesis). Since the default Emacs behavior already does what you want, we do not need to change the behavior, so we just reuse the original function.
The second cond clause handles a closing delimiter character (e.g. when the cursor is "on" a closing parenthesis). The default Emacs behavior is not doing what you want. To solve the problem, we "trick" the original function into believing that the cursor is right after the closing parenthesis whenever the cursor is "on" the closing parenthesis.
Otherwise, we return nil
.
You may also want to consider this variant:
(advice-add show-paren-data-function
:around
(lambda (orig-fun)
(cond ((looking-at "\\s)")
(save-excursion (forward-char 1) (funcall orig-fun)))
(t (funcall orig-fun)))))
When the block cursor is "on" a closing parenthesis, it will highlight the opening parenthesis. When the block cursor is not "on" a closing parenthesis, but the previous character is a closing parenthesis, it will highlight the opening parenthesis.
I have tried these code snippets in GNU Emacs 26.3.
x
is a number in your phrase "cursor is at position x". The cursor represents point and point represents positions between characters. That makes the difference between(buffer-size)
and(point-max)
. The first point position is that one before the first character the last one is that one behind the last character. So(point-max)
is(1+ (buffer-size))
. If you setcursor-type
tobar
. That actually represents the cursor position better thanbox
orhollow
. Position actually means kind of "Insertion Position".show-paren-when-point-inside-paren
to t. Let|
be the cursor. In(|ignore)
highlight()
ofignore
. In(ignore|)
highlight()
ofignore
. In(progn (ignore)|)
highlight()
ofignore
. In(progn (ignore))|
highlight()
ofprogn
.show-paren-mode
withhighlight-parentheses-mode
and only highlight one level of surrounding parens using(setq hl-paren-colors '("steel blue"))
, this way you always get the surrounding parens highlighted (which includes the situation where the point is right before the closing paren as in you example), too.