Skip to content

Instantly share code, notes, and snippets.

@benhsu
Created January 22, 2013 02:03
Show Gist options
  • Save benhsu/4591417 to your computer and use it in GitHub Desktop.
Save benhsu/4591417 to your computer and use it in GitHub Desktop.
closes the s-expression at point
(defun close-sexp ()
;; closes the s-expression at point
;; by computing how many open parens and close parens
;; there are and inserting the appropriate number
;; bug: doesnt handle backquoted parens like \( \) properly
(interactive)
(let ((pt (point))
(numopen 0)
(numclose 0))
(save-excursion
(beginning-of-buffer)
(while (< (point) pt)
(if (eq (char-after) ?\()
(setq numopen (+ 1 numopen)))
(if (eq (char-after) ?\))
(setq numclose (+ 1 numclose)))
(forward-char 1) ))
(dotimes (n (- numopen numclose))
(insert-char ?\)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment