我在暂存缓冲区中进行了很多编程,我经常在一个点计算一些表达式,然后想在其他地方计算相同的表达式(不必为它编写函数)。
一个例子是我想测试 (looking-at "\\<")
,看看我是否在看单词的开头。使用 eval-last-sexp
时它评估 (point)
之前的东西.
所以,这意味着我可以测试:
(looking-at "\\<")<(point)>someword
但我无法在 (point)
之前测试表达式:
someword<(point)>(looking-at "\\<")
为了测试这一点,我实际上做了类似的事情:
(defun foo ()
(interactive)
(when (looking-at "\\<") (message "t"))
)
然后在其他地方调用它(有时,当我测试很多时,我什至会把它绑定(bind)到一个键上)。
其实不难回答“(looking-at)
是如何表现的”,但我感兴趣的问题是是否可以存储 the-last-user-called-sexp
某处,以便可以通过使用以下函数在缓冲区的其他地方调用它:
(defun invoke-last-sexp (sexp)
(interactive)
(evaluate the last sexp) ; maybe even (call-interactively) is sometimes needed
)
最佳答案
我也希望能够快速评估表单,无论我在哪里 是。为此,我有一个宏灵感来自 magnars ' 配置。
(defmacro create-simple-keybinding-command (name key)
`(progn (defmacro ,name (&rest fns)
(list 'global-set-key (kbd ,key)
`(lambda ()
(interactive)
,@fns)))
(defmacro ,(intern (concat (symbol-name name) "e")) (&rest fns)
(list 'global-set-key (kbd ,key)
`(lambda ()
(interactive)
(message "%s"
(progn
,@fns)))))))
例如,你评估
(create-simple-keybinding-command f9 "<f9>")
并且您有两个宏 f9
和 f9e
。 f9e
类似于 f9
除了
它在迷你缓冲区中显示返回值。在你的情况下你评估
(f9e (looking-at "\\<"))
现在,每次您按 f9 时都会评估表单。
你可能想看看我的 emacs 配置 here我有几个宏在做或多或少相同的事情。
关于emacs - 存储 sexp 评估,以便可以在其他地方调用它 (Emacs Lisp),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14298637/