Re: [FEATURE REQUEST] auto insert close paren

Surendra Singhi <[email protected]> Wed, 09 Mar 2005 19:54:27 -0700
Newsgroups gmane.emacs.auc-tex
Message-ID <[email protected]>
Surendra Singhi wrote:

> Masayuki Ataka wrote:
> 
>>
>> When we write open paren `(', automatically close paren `)' is
>> inserted and cursor is move into inside them.  
>> ex.
>>   (             -> (-!-)
>>   $             -> $-!-$
>>   \[            -> \[ -!- \]
>>   \{            -> \{-!-\}
>>   \left(        -> \left( -!- \right)
>>   \bigl(        -> \bigl( -!- \bigr)
>>   \bigl\langle  -> \bigl\langle -!- \bigr\rangle
>> `-!-' means a cursor.

> I have written code to do a part of above:
> This will insert closing pair for (,$,[,",{.
> And what is \left(, \bigl(, etc.?
> 
> One drawback of this code is that unlike in eclipse, if the user also 
> type a closing pair the automatically inserted closing pair is not removed.
> 
Now it supports \(,\[,\{ also.
(require 'skeleton)
(defun self-insert-pair()
   "Insert characters (,{,[,\",\(,\{,\[
  and their closing counterparts, and then put the cursor in the middle 
position."
   (interactive)
   (setq skeleton-pair t)
   (let ((char last-command-char))
     (case char
       (?\( (if (= ?\\ (char-before))
	       (skeleton-insert '(nil -1 "\\(" _ "\\)"))
	     (skeleton-insert '(nil "(" _ ")"))))
       (?\[ (if (= ?\\ (char-before))
	       (skeleton-insert '(nil -1 "\\[" _ "\\]"))
	     (skeleton-insert '(nil "[" _ "]"))))
       (?\$ (skeleton-pair-insert-maybe nil))
       (?\{ (if (= ?\\ (char-before))
	       (skeleton-insert '(nil -1 "\\{" _ "\\}"))
	     (skeleton-insert '(nil "{" _ "}"))))
       (?\" (skeleton-pair-insert-maybe nil)))))

(local-set-key [\(] 'self-insert-pair)
(local-set-key [\$] 'self-insert-pair)
(local-set-key [\{] 'self-insert-pair)
(local-set-key [\[] 'self-insert-pair)
(local-set-key [\"] 'self-insert-pair)


-- 
Surendra Singhi
www.public.asu.edu/~sksinghi/

"O thou my friend! The prosperity of Crime is like unto the lightning, 
whose traitorous brilliancies embellish the atmosphere but for an 
instant, in order to hurl into death's very depths the luckless one they 
have dazzled." -- Marquis de Sade