如何轻松地试验涉及移动光标(插入符号)的 elisp 代码?

发布于 2024-07-25 17:11:25 字数 547 浏览 6 评论 0原文

我通常在暂存缓冲区上使用 elisp 代码。 我发现很难使用以这种方式移动光标的 elisp 代码,因为我不知道如何分离用于编辑代码的缓冲区和用于测试代码的缓冲区。

例如,如果我想使用以下代码:

(backward-up-list 1)
(backward-sexp 1)
(kill-sexp 2)

来自 使用智能括号计数 elisp< /a>,我想一次运行一行并查看每一行的作用。 但是代码将插入符号移动到我粘贴该代码的临时缓冲区中,并且我已经在使用该插入符号来编辑或运行代码。 另一个问题是这段代码应该在 TeX 文档上进行测试,而我的暂存缓冲区不是 TeX 文档。

这一切都取决于如何区分画家和绘画。

尽管在该示例中,只需查看 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 技术交流群。

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

发布评论

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

评论(4

忆沫 2024-08-01 17:11:25

使用 edebug(Cu CMx 而不是 CMx)编译函数,切换到缓冲区进行实验,通过 M-: 调用函数,然后单步执行 (< code>n) 在调试器中。

否则,学会以更大的块来思考,并使用 M-: 在测试缓冲区中进行测试。 这就是我对几乎所有事情所做的事情,包括非常复杂的代码,例如 cperl-mode

Compile the function with edebug (C-u C-M-x instead of C-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.

安静 2024-08-01 17:11:25

除了 M-: (即 eval-expression)之外,还请查看 with-selected-window。 它在给定窗口的上下文中执行其主体。 例如,假设您有两个窗口,

(with-selected-window (next-window)
  (backward-up-list 1))

将在另一个窗口中执行backward-up-list操作。

In addition to M-: (i.e., eval-expression), also look at with-selected-window. It executes its body in the context of a given window. For example, assuming you have two windows,

(with-selected-window (next-window)
  (backward-up-list 1))

will perform the backward-up-list operation in the other window.

丑丑阿 2024-08-01 17:11:25

我发现了一些其他解决方法(类似于 with-selected-window) 将

set-buffer 与 progn 结合使用:

(progn
  (set-buffer "edithere.el")
  (insert "hello")
  (beginning-of-line))

缓冲区名称 edithere.el 必须存在。 按 CMx 评估程序形式。 你也可以使用let。 当你想编写一个编辑缓冲区或移动光标的命令时,你可以像上面那样以“(progn ...”开头,而不是以“(defun ...”开头,完成后,更改为defun。

使用with-current-buffer:(按 CMx 评估 with-current-buffer 形式)

(with-current-buffer "edithere.el"
  (insert "hello")
  (beginning-of-line))

I found some other workarounds (Similar to with-selected-window)

Using set-buffer with progn:

(progn
  (set-buffer "edithere.el")
  (insert "hello")
  (beginning-of-line))

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)

(with-current-buffer "edithere.el"
  (insert "hello")
  (beginning-of-line))
一个人的旅程 2024-08-01 17:11:25

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.

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