如何轻松地试验涉及移动光标(插入符号)的 elisp 代码?
我通常在暂存缓冲区上使用 elisp 代码。 我发现很难使用以这种方式移动光标的 elisp 代码,因为我不知道如何分离用于编辑代码的缓冲区和用于测试代码的缓冲区。
例如,如果我想使用以下代码:
(backward-up-list 1)
(backward-sexp 1)
(kill-sexp 2)
这一切都取决于如何区分画家和绘画。
尽管在该示例中,只需查看 Ch f 手册就足以了解发生了什么。 但这只是因为这个示例代码足够简单。
I usually play with elisp code on my scratch buffer. I find it hard to play with elisp code that moves cursors in this way because I don't know how to separate the buffer for editing code and the buffer for testing the code.
For example if I want to play with the following code:
(backward-up-list 1)
(backward-sexp 1)
(kill-sexp 2)
from searching with intelligent bracket counting elisp, I'd like to run one line at a time and see what each line does. But the code moves the caret in the very scratch buffer I pasted that code in and I'm already using that caret to edit or run the code. Another problem is that this code is supposed to be tested on a TeX document and my scratch buffer is not a TeX document.
It all comes down to how to separate the painter and the painting.
Though in that example, just looking at the C-h f manual would be enough to understand what's going on. But that's only because this example code is simple enough.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 edebug(
Cu CMx
而不是CMx
)编译函数,切换到缓冲区进行实验,通过 M-: 调用函数,然后单步执行 (< code>n) 在调试器中。否则,学会以更大的块来思考,并使用 M-: 在测试缓冲区中进行测试。 这就是我对几乎所有事情所做的事情,包括非常复杂的代码,例如 cperl-mode。
Compile the function with edebug (
C-u C-M-x
instead ofC-M-x
), switch to a buffer to experiment in, invoke the function via M-:, and then single-step (n
) in the debugger.Otherwise, learn to think in bigger chunks, and test in your test buffer with M-:. This is what I do for nearly everything, including very complicated code like cperl-mode.
除了
M-:
(即 eval-expression)之外,还请查看with-selected-window
。 它在给定窗口的上下文中执行其主体。 例如,假设您有两个窗口,将在另一个窗口中执行
backward-up-list
操作。In addition to
M-:
(i.e., eval-expression), also look atwith-selected-window
. It executes its body in the context of a given window. For example, assuming you have two windows,will perform the
backward-up-list
operation in the other window.我发现了一些其他解决方法(类似于 with-selected-window) 将
set-buffer 与 progn 结合使用:
缓冲区名称 edithere.el 必须存在。 按 CMx 评估程序形式。 你也可以使用let。 当你想编写一个编辑缓冲区或移动光标的命令时,你可以像上面那样以“(progn ...”开头,而不是以“(defun ...”开头,完成后,更改为defun。
使用with-current-buffer:(按 CMx 评估 with-current-buffer 形式)
I found some other workarounds (Similar to with-selected-window)
Using set-buffer with progn:
The buffer name edithere.el must be present. Press C-M-x to evaluate the progn form. You can also use let. When you want to write a command that edits buffer or moves cursor, instead of starting with "(defun ...", you may start with "(progn ..." like above, and once it's finished, change to defun.
Using with-current-buffer: (Press C-M-x to evaluate the with-current-buffer form)
Cx 4 f。 输入文件名:
foo.el
。 将您的代码放在那里,然后在那里进行测试。 比*scratch*
缓冲区(Emacs-Lisp 模式,一方面)好多了,而且您可以更轻松地保存您的工作。C-x 4 f. Type a file name:
foo.el
. Put your code there, and test it there. Lot's better than the*scratch*
buffer (Emacs-Lisp mode, for one thing), and you can more easily save your work.