Skip to content

Instantly share code, notes, and snippets.

@teaforthecat
Created October 18, 2015 18:10
Show Gist options
  • Save teaforthecat/454784f099d49a474e15 to your computer and use it in GitHub Desktop.
Save teaforthecat/454784f099d49a474e15 to your computer and use it in GitHub Desktop.
one function with a few lines addded to keep the structure of lisp forms when commenting a form inside another form
(defun comment-sexp--raw ()
"Comment the sexp at point or ahead of point."
(pcase (or (bounds-of-thing-at-point 'sexp)
(save-excursion
(skip-chars-forward "\r\n[:blank:]")
(bounds-of-thing-at-point 'sexp)))
(`(,l . ,r)
(goto-char r)
(skip-chars-forward "\r\n[:blank:]")
(save-excursion
;; comment internal forms without breaking the structure
(if (looking-at " *\)") ;;[:blank:] didn't work here(?)
(progn
(goto-char r)
(newline-and-indent)))
(comment-region l r))
(skip-chars-forward "\r\n[:blank:]"))))
@Malabarba
Copy link

Hey there. I'm having a look at this now.
Could you exemplify a case where the previous code didn't work?

@Malabarba
Copy link

What I mean is: comment-region is usually supposed to take care of inserting newlines and ensuring the code remains valid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment