[OS X Emacs] parens matching

Ian Eure ian at digg.com
Thu Jan 15 12:57:06 EST 2009


On Jan 14, 2009, at 8:23 PM, M. Tamer Özsu wrote:

> I am trying the new Aquamacs 1.6 -- it is very nice. I wonder if  
> there is a way to get parens matching (and quote matching, etc,  
> etc). I have "AutoPairs" system preference installed, but it does  
> not work properly, when I type {, it inserts {}}, highlights the  
> first {} in aqua color and then puts the cursor right after that.  
> When I turn off AutoPairs for Aquamacs, then no pairing occurs.
>
> Any suggestions would be very much appreciated.
>
You want (insert-pair). The default setup gives you a pair of  
parenthesis when you hit M-(.

You can see the known pairs with:
(mapcar (lambda (pair) (mapcar (lambda (char) (char-to-string char))  
pair))
         insert-pair-alist)

I use this code to add some additional useful pairs:

(mapc (lambda (pair) (add-to-list 'insert-pair-alist pair))
       '((?“ ?”) (?‘ ?’) (?+ ?+) (?. ?.)))


Then bind:

(mapc (lambda (key) (define-key (current-global-map) key 'insert-pair))
       '("\M-'" "\M-\"" [?\M-“] [?\M-‘] "\M-[" "\M-+" "\C-c`" "\C-c{"  
"\C-c."))

Giving you M-“ M-‘ M-' M-[ M-+ C-c ` C-c { C-c . to insert pairs.

I prefer explicitly asking for pairs rather than having them inserted  
for me. But if you want that behavior (in Emacs, it's called  
"electric" behavior), you could change that to:

(mapcar (lambda (pair)
           (define-key (current-global-map) (char-to-string (car pair))
             'insert-pair)) insert-pair-alist)

And this will map the opening character of every pair in insert-pair- 
alist to (insert-pair), which will insert the pair for you. If you  
don't want to insert a pair, you'd need to type e.g. C-q (

  - Ian


More information about the MacOSX-Emacs mailing list