2

I am using Emacs: Highlight Brackets ()[]{}.

I want to highlight matching brackets when the cursor is behind and front of starting and ending brackets. How can I accomplish this?

foo ( "hello" ) 
   ^ ^       ^ ^
   1 2       3 4 // cursor points

I have followed @Gilles 'SO- stop being evil's answer for Changing show-paren-mode Areas:

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

which helped me to highlight brackets cursor points 1, 3, 4, but it does not highlight when on 2nd cursor point.

My setup is:

(show-paren-mode 1)
(setq show-paren-delay 0)
(setq show-paren-style 'parenthesis)

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

(require 'paren)
(setq show-paren-style 'parenthesis)
2
  • The only time when this works for me is at cursor position 3 - and reading the code that's the only time when it should work. So I'm not sure how it helps with positions 1 and 4. The syntax class of an opening paren is 4 and the syntax class of a closing paren is 5 (see Syntax Table Internals).
    – NickD
    Jun 17 '20 at 21:23
  • I am not sure how I end up working it on positions [1,3,4]. Could there be any other solution to make it work on all positions?
    – alper
    Jun 18 '20 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.