Created
July 7, 2013 21:40
-
-
Save maximvl/5945091 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(setq correct-paren-alist | |
'(( "[" . "]" ) | |
( "(" . ")" ) | |
( "{" . "}" ) | |
( "<<" . ">>" ))) | |
(defun insert-correct-paren () | |
(interactive) | |
(let* ((closed-parens 0) | |
(correct-paren ")" ) | |
(op-parens (mapcar 'car correct-paren-alist)) | |
(cl-parens (mapcar 'cdr correct-paren-alist)) | |
(parens-reg (mapconcat 'regexp-quote | |
(append op-parens cl-parens) | |
"\\|")) | |
(keep-loop t)) | |
(save-excursion | |
(save-match-data | |
(while (and keep-loop | |
(re-search-backward parens-reg nil t)) | |
(let ((cur (buffer-substring-no-properties (match-beginning 0) | |
(match-end 0)))) | |
(cond | |
((member cur op-parens) | |
(if (zerop closed-parens) | |
(progn | |
(setq keep-loop nil) | |
(setq correct-paren (cdr (assoc cur correct-paren-alist)))) | |
(setq closed-parens (1- closed-parens)))) | |
((member cur cl-parens) (setq closed-parens (1+ closed-parens)))))))) | |
(insert-string correct-paren))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment