enhanced parentheses matching function

;; go to the matching parenthesis or square bracket
(defun da-match-paren (arg)
“Go to the matching paren if on a paren.”
(interactive “p”)
(cond ((and mark-active (looking-at “\\s\(“)) (forward-list 1))
((and mark-active (looking-back “\\s\)”)) (backward-list 1))
((looking-at “\\s\(“) (forward-list 1) (backward-char 1))
((looking-at “\\s\)”) (forward-char 1) (backward-list 1))
))
(global-set-key (kbd “M-[“) ‘da-match-paren)

As compared with the previous version I used (following the post “match parenthesis” at http://grok2.tripod.com/), this new version has the following features:

(1) it does not insert character if there is no “(” or “)” at the current point
(2) if the marker is activated and the “)” is behind the current position, it simply mark the whole () region for you.
(3) if the marker is activated and the “(” is at the current position, it simply mark the whole () region for you.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s