你如何使用 Paredit 注释掉全部或部分 Lisp s-exp? [英] How do you comment out all or part of a Lisp s-exp using Paredit?

查看:11
本文介绍了你如何使用 Paredit 注释掉全部或部分 Lisp s-exp?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

(foo x;是z)

...然后重新编译文件并在 REPL 等中测试一些内容

启用 paredit-mode 后,这不起作用.如果该点正好位于下面的第一个括号之前,则会发生以下情况:

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

然后你输入一个分号,输入的是一个分号和一个换行符:

<代码>;(defun some-fn (x)...)

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

(foo x;是z)

我认为,如果定义都在一行上,这行得通:

;(defparameter *foo* 10)

...但除此之外我不知道如何做到这一点.Paredit 很棒,我真的很想继续使用它.有没有知道解决这个问题的 Lispers 的人,或者可以使用一些 Emacs Lisp 绑定到诸如 paredit-comment-out-s-expr 之类的东西的 Emacs 向导?

如果有更 Lispy 或 Emacsy 的方式来完成基本相同的事情,注释掉部分源代码以重新编译,请不要犹豫,提出建议!

解决方案

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

你可以很容易地制作一个简单的命令甚至一个宏来做到这一点:

<前>(defun 评论-sexp()在这一点上注释掉性别."(交互的)(节省游览(标记性)(paredit-评论-dwim)))

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!

解决方案

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

这篇关于你如何使用 Paredit 注释掉全部或部分 Lisp s-exp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆