Emacs:如何计算光标所在的最小 s 表达式,或以下 s 表达式
评估 中的 (+ 100 (+ 100 100))
部分的好方法是什么
(+ (+ 1 2) (+ 100 (+ 100 100)))
?
目前,我通过 Cx Ce 进行操作,这意味着我需要找到结束括号,这在大多数情况下很困难。 选项> Paren 匹配突出显示
有帮助,但我仍然需要将光标移向结束括号,直到突出显示的匹配是开始括号。
一种方法是使用 Cx Ce 的反向版本,这样我就可以将光标放在起始括号处,如下所示:
(+ (+ 1 2) |(+ 100 (+ 100 100)))
然后按适当的键绑定。
或者,我可以将光标放在表达式内,但不能放在较小的表达式内:
(+ (+ 1 2) (+ | 100 (+ 100 100)))
然后按键绑定。因为目标越大,瞄准目标就越容易。
我怎样才能发出这样的命令?或者已经提供了吗?
旁注:条形光标和方框光标
使用方框光标(默认)的 Emacsers 可能想知道我将带有上面条形符号的光标放在哪里。在 emacs 中,您可以选择框光标或条光标,(bar-cursor-mode t)
。当条形光标位于字母 A 和 B 之间时,框光标位于 B 上。因此,条形是框的左壁。
顺便说一句,条形光标的概念在一些不寻常的方面很有用: 编程中从index1迭代到index2-1的做法让初学者感到惊讶。它有助于将索引 1 和索引 2 想象为指示条(左墙)而不是框。
What is a good way to evaluate the (+ 100 (+ 100 100))
part in
(+ (+ 1 2) (+ 100 (+ 100 100)))
?
For now, I do it by C-x C-e, which means I need to locate the ending parenthesis, which is difficult in most cases. Options > Paren Matching Highlighting
helps, but still I need to move the cursor toward the ending parenthesis until the highlighted match is the starting parenthesis.
One way would be to have the reverse version of C-x C-e, so that I can place the cursor at the starting parenthesis like this:
(+ (+ 1 2) |(+ 100 (+ 100 100)))
and then press the appropriate keybinding.
Or I could place the cursor inside the expression, but not inside smaller expressions,:
(+ (+ 1 2) (+ | 100 (+ 100 100)))
and press a keybinding. Because aiming at a target is easier if the target is big.
How can I make such a command? Or is there one already provided?
Sidenote: bar cursor and box cursor
Emacsers who use box cursor (default) might wonder where I'm putting the cursor with the bar notation above. In emacs, you can choose box cursor or bar cursor, (bar-cursor-mode t)
. When the bar cursor is between the letters A and B, the box cursor is on B. So the bar is the left wall of the box.
BTW, the concept of bar cursor is useful in some unusual way:
The practice of iterating from index1 to index2-1 in programming surprises beginners. It helps to imagine index1 and index2 as indicating bars (left walls) rather than boxes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(4)
你可以这样写这样的命令:
(require 'thingatpt)
(defun eval-sexp-at-or-surrounding-pt ()
"evaluate the sexp following the point, or surrounding the point"
(interactive)
(save-excursion
(forward-char 1)
(if (search-backward "(" nil t)
(message "%s" (eval (read-from-whole-string (thing-at-point 'sexp)))))))
在 Icicles 中是做你想做的事情的通用方法。
默认情况下,在迷你缓冲区中 M-. 绑定到一个命令,该命令将文本在点(或附近)插入到迷你缓冲区中(不输入它或对其进行其他操作;只是插入它)。
例如,您可以使用 M-: 来计算 Lisp sexp,然后使用 M-. 来获取位于/附近点的 sexp。
如果您重复M-.,那么它会丢弃刚刚抓取的内容,并在/附近抓取一些其他类型的东西(文本)并将其插入。默认情况下,它按顺序运行这些类型的事物
: Lisp 符号或文件名。
b.活动区域(选定的文本)或单词。
c.最直接的列表。
d.下一个最大的列表。
e.下一个最大的列表。
f.无论函数 ffap-guesser
猜测哪个文件或 URL。
g。无论函数 thing-at-point-url-at-point
猜测什么 URL。
这对于 (+ (+ 1 2) (+ 100 (+ 100 100)))
示例意味着什么?
如果点是例如,在倒数第二个 100
的 1
之前,这些是当您点击 M-.< 时连续插入到迷你缓冲区中的 sexp。 /kbd> 重复,按顺序
: +
b. 100
c. (+ 100 100)
d. (+ 100 (+ 100 100))
(+ (+ 1 2) (+ 100 (+ 100 100)))
?
因此,要插入最大的封闭列表,您可以执行 M-: M-。 M-。 M-。 M-。 M-.,即按M-.五次。
对于此行为,特别是为了准确抓取列表,您还需要库 Thing At点+。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
将键绑定到其中一个或两个:
切线相关,我强烈建议使用 paredit 来使用s-表达式。结构编辑命令和绑定使编辑 s 表达式变得轻而易举。它将 C-down 绑定到
up-list
,这样上面的eval-surrounding-sexp
几乎和 C-down Cx Ce 完全一样(唯一的区别是函数使用save-excursion
来防止移动)。Bind a key to one or both of these:
Tangentially related, I highly recommend paredit for working with s-expressions. The structure editing commands and bindings make editing s-expressions a breeze. It binds C-down to
up-list
, so thateval-surrounding-sexp
above is almost exactly the same as C-down C-x C-e (the only difference is that the function usessave-excursion
to prevent movement).