现在我在 * scratch * 缓冲区中编写表达式,并通过使用 C-x C-e 进行评估来测试它们。我非常感谢有一个像SLIME或irb这样的交互式解释器,我可以在其中测试Emacs Lisp表达式。

有帮助吗?

解决方案

在Inferior Emacs-Lisp模式中评估Lisp表达式很容易:

M-x ielm

您可以在 Emacs中详细了解此功能关于“Lisp Interaction”的手册部分

其他提示

Eshell 是互动式Elisp的另一种选择翻译。

M-x eshell

它不仅是像bash这样的命令shell(如果在Windows上也是cmd.exe),但你也可以交互式地编写和执行Elisp代码。

~ $ ls
foo.txt
bar.txt
~ $ (+ 1 1)
2

最好的选择是 * scratch * 缓冲区。首先打开调试器,你可以使它更像REPL:

M-x set-variable debug-on-error t

然后使用 C-j 代替 C-x C-e ,它会将表达式的计算结果插入表达式后的行缓冲区中。而不是命令历史, * * * 等等,你只需移动 * scratch * 缓冲区并进行编辑。

如果你想要像 * * * 这样的东西,就像通常的REPL一样,试试 ielm

M-x ielm

* scratch * 缓冲区中,只需键入C-j即可评估点之前的表达式。

好吧,如果您真的对emacs的文字REPL感兴趣,可以使用emacs的-batch模式编写一个:

(require 'cl)

(defun read-expression ()
  (condition-case
      err
      (read-string "> ")
    (error
     (message "Error reading '%s'" form)
     (message (format "%s" err)))))

(defun read-expression-from-string (str)
  (condition-case
      err
      (read-from-string str)
    (error
     (message "Error parsing '%s'" str)
     (message (format "%s" err))
     nil)))

(defun repl ()
  (loop for expr = (read-string "> ") then (read-expression)
        do
        (let ((form (car (read-expression-from-string expr))))
          (condition-case
              err
              (message " => %s" (eval form))
            (error
             (message "Error evaluating '%s'" form)
             (message (format "%s" err)))))))

(repl)

你可以从命令行调用它,或者,如你所愿,从运行shell的emacs缓冲区中调用它:

kburton@hypothesis:~/projects/elisp$ emacs -batch -l test.el
Loading 00debian-vars...
> (defvar x '(lambda (y) (* y 100)))
 => x
> (funcall x 0.25)
 => 25.0
> 
kburton@hypothesis:~/projects/elisp$

要运行一个elisp表达式,您可以使用M-:快捷键并在迷你缓冲区中输入表达式。对于其他情况,您可以使用 scratch 缓冲区

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top