Emacs – Changing show-paren-mode Areas

elispemacssyntax highlighting

I like show-paren-mode in Emacs, but I would really like to change the highlighting behavior for closing brackets.

That is, I want the opening bracket to be highlighted when the point is on the closing bracket. The default behavior highlights the opening bracket when the point is on the character following the closing bracket.

Is this easy to change? Also, I would be interested in potential benefits of keeping the show-paren-mode behavior as it is.

Best Answer

As of Emacs 24.3, this functionality is not available in Show Paren mode.

Here's some completely untested code (typed directly in my browser) that tweaks Show Paren mode to match a closing parenthesis before the cursor instead of after.

(defadvice show-paren-function 
  (around show-paren-closing-before
          activate compile)
  (if (eq (syntax-class (syntax-after (point))) 5)
      (save-excursion
        (forward-char)
        ad-do-it)
    ad-do-it))

This picks up closing parentheses before the cursor as well, but if the cursor is on a closing parenthesis that follows a closing parenthesis, the closing parenthesis under the cursor has precedence. Fixing this to never look at a closing parenthesis before the cursor looks tricker (it could be done with a gross hack such as (flet ((char-syntax …)) ad-do-it)).

Related Question