emacs - 存储 sexp 评估,以便可以在其他地方调用它 (Emacs Lisp)

标签 emacs lisp eval

我在暂存缓冲区中进行了很多编程,我经常在一个点计算一些表达式,然后想在其他地方计算相同的表达式(不必为它编写函数)。

一个例子是我想测试 (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>")

并且您有两个宏 f9f9ef9e 类似于 f9 除了 它在迷你缓冲区中显示返回值。在你的情况下你评估

(f9e (looking-at "\\<"))

现在,每次您按 f9 时都会评估表单。

你可能想看看我的 emacs 配置 here我有几个宏在做或多或少相同的事情。

关于emacs - 存储 sexp 评估,以便可以在其他地方调用它 (Emacs Lisp),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14298637/

相关文章:

emacs - 如何在elisp中使用过滤器执行sudo命令

emacs python 模式 : designate which python shell to send commands to

emacs - 如何高效地查找 Coq 中定义的标识符?

lambda - 如何在 Scheme 中使用列表中的 lambda

javascript - 包括带有 html 和 node.js 的 js 文件

emacs - 在整个 Emacs 缓冲区中突出显示名称

emacs - 如何在 Emacs 的 lisp-mode 中高亮显示所有函数的名称?

lisp - 如何编译和运行用 T(Lisp 的一种方言)编写的程序

linux - 这两件事有什么区别?

php - eval() 不返回函数结果