Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you comment out all or part of a Lisp s-exp using Paredit?

When editing Lisp code, occasionally it's useful to entirely comment out a top-level definition, like this:

;(defun some-fn-which-is-broken (x)
;  ...)

... or comment out only part of an s-expression, like this:

(foo x
;    y
     z)

... and then recompile the file and test something in the REPL, etc.

With paredit-mode enabled, this doesn't work. Here's what happens, if the point is right before the first paren below:

(defun some-fn (x)
  ...)

and you type a semicolon, what is entered is a semicolon and a newline:

;
(defun some-fn (x)
  ...)

Same with commenting out part of the s-expression:

(foo x
;    
     y
     z)

I think that if the definition is all on one line, this works:

;(defparameter *foo* 10)

... but otherwise I can't find out how to do this. Paredit is great, I would really like to keep using it. Are there any Lispers who know a way around this, or Emacs-wizards who can whip up a bit of Emacs Lisp to bind to something like paredit-comment-out-s-expr?

If there is a more Lispy or Emacsy way of accomplishing essentially the same thing, commenting out parts of source to recompile, please, don't hesitate to suggest them!

like image 714
michiakig Avatar asked Nov 26 '10 20:11

michiakig


People also ask

How do you comment multiple lines on a lisp?

You can just select the block and use M-; or M-x comment-region . This will comment out all the selected lines.

What is ParEdit?

ParEdit ( paredit. el ) is a minor mode for performing structured editing of S-expression data. The typical example of this would be Lisp or Scheme source code. ParEdit helps **keep parentheses balanced** and adds many keys for moving S-expressions and moving around in S-expressions.


2 Answers

Position the point on the first character of the whole sexp, mark the whole sexp with C-M-space, and issue M-; to do the commenting. If it is necessary to do so, your source code will also be re-formatted so that only the sexp you marked, and nothing that was also on the same line, is in a comment.

You can very easily make a simple command or even a macro to do that:

(defun comment-sexp ()
  "Comment out the sexp at point."
  (interactive)
  (save-excursion
    (mark-sexp)
    (paredit-comment-dwim)))
like image 175
Nietzche-jou Avatar answered Nov 16 '22 01:11

Nietzche-jou


Just a side note:

The #+ and #- reader macros are pretty nice for commenting out sexps. They allow ignoring the following sexp, if the given symbol isn't/is found in *FEATURES*. Just pick a symbol not in *FEATURES*, and use it with #+ like this:

#+nil
(defun foo ()
  ...)

Now, the function definition will be ignored (unless NIL is in *FEATURES*, which is not very likely).

like image 34
danlei Avatar answered Nov 16 '22 01:11

danlei