如何使用 Paredit 注释掉全部或部分 Lisp s-exp?

发布于 2024-10-05 00:58:59 字数 813 浏览 2 评论 0原文

编辑 Lisp 代码时,有时完全注释掉顶级定义会很有用,如下所示:

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

... 或仅注释掉 s 表达式的一部分,如下所示:

(foo x
;    y
     z)

... 然后重新编译文件并测试某些内容REPL 等。

启用 paredit 模式后,这不起作用。如果该点位于下面第一个括号之前,则会发生以下情况:

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

并且您键入一个分号,输入的是一个分号和一个换行符:

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

与注释掉 s 表达式的一部分相同:

(foo x
;    
     y
     z)

我认为如果定义全部一方面,这是有效的:

;(defparameter *foo* 10)

...但除此之外我不知道如何做到这一点。 Paredit 很棒,我真的很想继续使用它。是否有任何 Lispers 知道解决这个问题的方法,或者 Emacs 向导可以快速编写一些 Emacs Lisp 来绑定到类似 paredit-comment-out-s-expr 的东西?

如果有更多 Lispy 或 Emacsy 的方法来完成基本相同的事情,注释掉部分源代码以重新编译,请毫不犹豫地建议它们!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

信仰 2024-10-12 00:58:59

将点定位在整个 sexp 的第一个字符上,用 CM-space 标记整个 sexp,并发出 M-; 进行注释。如果有必要这样做,您的源代码也将被重新格式化,以便只有您标记的 sexp 以及同一行上的任何内容都不会出现在注释中。

您可以非常轻松地创建一个简单的命令甚至宏来执行此操作:

(defun comment-sexp ()
  "Comment out the sexp at point."
  (interactive)
  (save-excursion
    (mark-sexp)
    (paredit-comment-dwim)))

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)))
给妤﹃绝世温柔 2024-10-12 00:58:59

附注:

#+#- 读取器宏非常适合注释 sexp。如果在 *FEATURES* 中未找到给定符号,它们允许忽略以下 sexp。只需选择一个不在 *FEATURES* 中的符号,然后将其与 #+ 一起使用,如下所示:

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

现在,函数定义将被忽略(除非 NIL > 位于 *FEATURES* 中,这不太可能)。

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

長街聽風 2024-10-12 00:58:59

作为权宜之计,您可以使用 Cq (quoted-insert) 插入任意字符,而不会触发任何与模式相关的魔法。例如,在java模式下,输入括号会重新缩进当前行,这并不总是我想要的;在这种情况下,我将插入带有 Cq 的括号以保留缩进。 (或者更常见的是,我会键入括号,观察缩进变化,诅咒,撤消,然后使用 Cq 重新输入。)

对于一般注释,使用 M-; (comment-dwim) 而不是手动输入分号。

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.

抹茶夏天i‖ 2024-10-12 00:58:59

您可以使用 CM-SPC M-; 标记 S 表达式(CM-SPC 表示 mark-sexp),然后对其进行注释( M-; 代表 comment-dwim)。

在 paredit 23 中,仅输入 ; 不会推送任何不需要下线的内容。所以它会为你的第二个例子做正确的事情。如果您想注释掉 z 而不是 y,它只会将结束分隔符推到另一行。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文