4

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.

Drew
  • 376
  • 2
  • 8
  • Related: [show-paren-mode only highlights when cursor is one character after closing parenthesis](https://emacs.stackexchange.com/q/51986) – Flux Feb 18 '21 at 16:12

3 Answers3

3

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)).

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
  • I tried this, but unfortunately, it made the behavior I want to remove occur on the opening parenthesis in addition to the closing one. Additionally, I'm fairly new to Elisp, but I've seen people advise (no pun intended) against using defadvice. Would its use be justified in this instance? –  Nov 05 '13 at 04:12
  • 1
    @mellowmaroon I was going in the wrong direction, I've fixed the bug. Regarding `defadvice`, it redefines a function. That can be risky if you change the behavior of a function that's used by other functions, but it's ok to redefine functions that are only called manually or in known contexts, such as here. `defadvice` exists for precisely this sort of things. – Gilles 'SO- stop being evil' Nov 06 '13 at 01:10
  • Can I also apply this to highlight closing bracket when point is on the beginning braker `foo(^ )`? @Gilles'SO-stopbeingevil' – alper Jun 17 '20 at 01:48
1

In 25.1, there is a variable enabling that:

(setq show-paren-when-point-inside-paren t)

xwl
  • 111
  • 2
0

You can provide your own function as the value of show-paren-data-function:

,----
| show-paren-data-function is a variable defined in `paren.el'.
| Its value is show-paren--default
| 
|   This variable can be risky when used as a file-local variable.
| 
| Documentation:
| Function to find the opener/closer at point and its match.
| The function is called with no argument and should return either nil
| if there's no opener/closer at point, or a list of the form
| (HERE-BEG HERE-END THERE-BEG THERE-END MISMATCH)
| Where HERE-BEG..HERE-END is expected to be around point.
 ----

See the definition of show-paren--default for inspiration.

As for the advantage: you see the matching opening paren each time you add a closing paren. Makes sense, no?

Drew
  • 376
  • 2
  • 8
  • Where is `show-paren-data-function`? It isn't in the current [Emacs 24](http://bzr.savannah.gnu.org/lh/emacs/emacs-24/annotate/head:/lisp/paren.el) tree. – Gilles 'SO- stop being evil' Nov 04 '13 at 22:28
  • It is in recent development versions of Emacs 24. It is not in Emacs 24.3. If you have 24.3 or earlier then advise or redefine `show-paren-function`. The newer code for `show-paren-function` uses `show-paren-data-function`. – Drew Nov 05 '13 at 03:06
  • I'm sorry, but I don't fully understand your answer. Does this method entail modifying paren.el? –  Nov 05 '13 at 04:07
  • If your Emacs has `show-paren-function` then you can use that to define the behavior you want. If not then you can redefine `show-paren-function` (or advise it). It is generally better to do that in your own code, not by modifying `paren.el` directly. – Drew Nov 05 '13 at 15:21