Skip to content

Instantly share code, notes, and snippets.

@nivekuil
Created May 23, 2015 20:52
Show Gist options
  • Save nivekuil/20a9fc63f1785f040ec8 to your computer and use it in GitHub Desktop.
Save nivekuil/20a9fc63f1785f040ec8 to your computer and use it in GitHub Desktop.
(defun insert-parentheses-backward ()
"Insert parentheses around the sexp near point.
Move parentheses backward by sexp if used repeatedly."
(interactive)
(cond ((string-match-p "\\\w" (char-to-string (char-after)))
(forward-char) (insert-parentheses-backward))
((equal (char-before) 41)
(backward-sexp) (insert-parentheses-backward))
((equal (char-after) 40)
(if (equal (char-before) 40)
(list (backward-char) (insert-parentheses 1))
(delete-char 1) (backward-sexp) (insert-char 40) (backward-char)))
((equal (char-before) 40)
(insert-parentheses 1) (backward-char))
((string-match-p "\\^_\W" (char-to-string (char-before)))
(insert-parentheses 1) (backward-char))
((string-match-p "\\^_\W" (char-to-string (char-after)))
(forward-char) (insert-parentheses 1) (backward-char))
(t (backward-sexp) (insert-parentheses 1) (backward-char))))
(defun insert-parentheses-forward ()
"Insert parentheses around the sexp around point.
Move parentheses forward by sexp if used repeatedly."
(interactive)
(cond ((equal (char-before) 41)
(if (equal (char-after) 41)
(list (forward-char) (insert-parentheses-forward))
(delete-char -1) (forward-sexp) (insert-char 41)))
((equal (char-after) 40)
(forward-sexp) (insert-parentheses-forward))
((equal (char-before) 40)
(insert-parentheses 1) (forward-sexp) (forward-char))
((string-match-p "\\^_\W" (char-to-string (char-before)))
(insert-parentheses 1) (forward-sexp) (forward-char))
((string-match-p "\\^_\W" (char-to-string (char-after)))
(backward-sexp) (insert-parentheses 1) (forward-sexp) (forward-char))
(t (backward-sexp) (insert-parentheses 1)
(forward-sexp) (forward-char))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment