(defun close-defun (arg) "Insert enough close parentheses to close current defun or defmacro. With prefix arg, just close the inmost defun or defmacro. This can be useful if a preceding defun or defmacro is not properly closed -- in this case close-defun without arg would get badly confused." (interactive "P") (if (not (in-defun-p)) (message "Not in defun or defmacro") (if arg (while (not-after-defun) (insert ")")) (while (in-defun-p) (insert ")"))) (blink-matching-open))) (defun not-after-defun () "Returns non-nil if sexp preceding point is not a defun or defmacro." (save-excursion (forward-sexp -1) (not (looking-at "([ \t\n\f]*\\(defun\\|defmacro\\)[ \t\n\f]")))) (defun in-defun-p () "Returns non-nil if point is inside a defun or defmacro, nil otherwise." (save-excursion (condition-case dummy (progn (while (not (looking-at "([ \t\n\f]*\\(defun\\|defmacro\\)[ \t\n\f]")) (backward-up-list 1)) t) (error nil))))