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

后端 未结 4 1924
伪装坚强ぢ
伪装坚强ぢ 2020-12-29 09:30

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)
;  ...)


        
相关标签:
4条回答
  • 2020-12-29 09:32

    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)))
    
    0 讨论(0)
  • 2020-12-29 09:32

    As a stopgap measure, you can use C-q (quoted-insert) to insert an arbitrary character without triggering any mode-related magic. For example, in java-mode, typing parentheses reindents the current line, which is not always what I want; in such cases, I'll insert a parenthesis with C-q to preserve my indentation. (Or more often, I'll type a parenthesis, observe the indentation change, curse, undo, and re-enter with C-q.)

    For commenting in general, it would probably be easier to use M-; (comment-dwim) rather than typing the semicolons manually.

    0 讨论(0)
  • 2020-12-29 09:41

    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).

    0 讨论(0)
  • 2020-12-29 09:51

    You can use C-M-SPC M-; to mark the S-expression (C-M-SPC for mark-sexp) and then comment it (M-; for comment-dwim).

    In paredit 23, just typing ; won't push anything it doesn't have to off the line. So it will do the right thing for your second example. And if you wanted to comment out z instead of y it would push only the closing delimiter to another line.

    0 讨论(0)
提交回复
热议问题