Skip to content

Instantly share code, notes, and snippets.

@teaforthecat
Created October 18, 2015 18:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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:]"))))
@teaforthecat
Copy link
Author

I added lines 11-15. The uncommenting part already works! yeah! ( As long as you are in the right position - you have to be "looking-at" a comment character)
This way the structure of the surrounding forms is still valid.
for example:

(let (x y)
;;  (not-sure) ;<- newline added after running `comment-or-uncomment-sexp`
) 

@Malabarba what do you think about incorporating these few lines?

@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