Emacs:如何计算光标所在的最小 s 表达式,或以下 s 表达式

发布于 2024-08-19 22:41:46 字数 807 浏览 3 评论 0原文

评估 中的 (+ 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 技术交流群。

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

发布评论

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

评论(4

玩心态 2024-08-26 22:41:46

将键绑定到其中一个或两个:

(defun eval-next-sexp ()
  (interactive)
  (save-excursion
    (forward-sexp)
    (eval-last-sexp nil)))

(defun eval-surrounding-sexp (levels)
  (interactive "p")
  (save-excursion
    (up-list (abs levels))
    (eval-last-sexp nil)))

切线相关,我强烈建议使用 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:

(defun eval-next-sexp ()
  (interactive)
  (save-excursion
    (forward-sexp)
    (eval-last-sexp nil)))

(defun eval-surrounding-sexp (levels)
  (interactive "p")
  (save-excursion
    (up-list (abs levels))
    (eval-last-sexp nil)))

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 that eval-surrounding-sexp above is almost exactly the same as C-down C-x C-e (the only difference is that the function uses save-excursion to prevent movement).

莫相离 2024-08-26 22:41:46

你可以这样写这样的命令:

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

You could write such a command like so:

(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)))))))
撩发小公举 2024-08-26 22:41:46

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))) 示例意味着什么?

如果点是例如,在倒数第二个 1001 之前,这些是当您点击 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点+

In Icicles there is a general way to do what you want.

By default, in the minibuffer M-. is bound to a command that inserts text at (or near) point into the minibuffer (doesn't enter it or doing else with it; just inserts it).

You can, for example, use M-: to evaluate a Lisp sexp, and then you use M-. to grab a sexp at/near point.

If you repeat M-. then it drops what it just grabbed and grabs some other kind of THING (text) at/near point and inserts that. By default, it runs through these kinds of THING, in order:

a. A Lisp symbol or file name.

b. The active region (selected text) or a word.

c. The most immediate list.

d. The next largest list.

e. The next largest list.

f. Whichever file or URL the function ffap-guesser guesses.

g. Whatever URL the function thing-at-point-url-at-point guesses.

What does this mean for your example of (+ (+ 1 2) (+ 100 (+ 100 100)))?

If point is before the 1 of the second-to-last 100, for example, these are the sexps that are consecutively inserted in the minibuffer when you hit M-. repeatedly, in order:

a. +

b. 100

c. (+ 100 100)

d. (+ 100 (+ 100 100))

e. (+ (+ 1 2) (+ 100 (+ 100 100)))?

So to insert the largest of the enclosing lists you would do M-: M-. M-. M-. M-. M-., that is, hit M-. five times.

For this behavior, in particular for the accurate grabbing of lists, you also need library Thing At Point+.

陌上芳菲 2024-08-26 22:41:46

有一个内置的eval-defun。默认情况下,它绑定到 C-M-x。它与您想要的类似,但评估了顶级 defun。也许你可以适应这一点。

There is an inbuilt eval-defun. It's bound by default to C-M-x. It's similar to what you want but evals the top-level defun. Perhaps you can adapt that.

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