如何在Emacs中调用交互式elisp解释器?

时间:2021-04-08 20:46:55

Right now I write expressions in the *scratch* buffer and test them by evaluating with C-x C-e. I would really appreciate having an interactive interpreter like SLIME or irb, in which I could test Emacs Lisp expressions.

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

6 个解决方案

#1


55  

It's easy to evaluate Lisp expressions in Inferior Emacs-Lisp Mode:

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

M-x ielm

You can read more about this feature in the Emacs manual section on "Lisp Interaction"

您可以在“Lisp Interaction”的Emacs手册部分中阅读有关此功能的更多信息。

#2


14  

Eshell is another option for an interactive Elisp interpreter.

Eshell是交互式Elisp解释器的另一种选择。

M-x eshell

Not only is it a command shell like bash (or cmd.exe if on Windows) but you can also interactively write and execute Elisp code.

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

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

#3


8  

Your best bet is the *scratch* buffer. You can make it more like a REPL by first turning on the debugger:

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

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

Then use C-j instead of C-x C-e, which will insert the result of evaluating the expression into the buffer on the line after the expression. Instead of things like command history, * * * and so forth, you just move around the *scratch* buffer and edit.

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

If you want things like * * * to work, more like a usual REPL, try ielm.

如果你想要* * *之类的东西工作,更像普通的REPL,试试ielm。

M-x ielm

#4


1  

In the *scratch* buffer, just type C-j to evaluate the expression before point.

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

#5


1  

Well, if you're really interested in a literal REPL for emacs it is possible to write one using the -batch mode of emacs:

好吧,如果你真的对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)

You can call this from the command line, or, as you seem to want, from within an emacs buffer running a shell:

您可以从命令行调用它,或者,如您所愿,从运行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$

#6


1  

To run just one elisp expression you can use M-: shortcut and enter expression in mini-buffer. For other cases you can use scratch buffer

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

#1


55  

It's easy to evaluate Lisp expressions in Inferior Emacs-Lisp Mode:

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

M-x ielm

You can read more about this feature in the Emacs manual section on "Lisp Interaction"

您可以在“Lisp Interaction”的Emacs手册部分中阅读有关此功能的更多信息。

#2


14  

Eshell is another option for an interactive Elisp interpreter.

Eshell是交互式Elisp解释器的另一种选择。

M-x eshell

Not only is it a command shell like bash (or cmd.exe if on Windows) but you can also interactively write and execute Elisp code.

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

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

#3


8  

Your best bet is the *scratch* buffer. You can make it more like a REPL by first turning on the debugger:

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

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

Then use C-j instead of C-x C-e, which will insert the result of evaluating the expression into the buffer on the line after the expression. Instead of things like command history, * * * and so forth, you just move around the *scratch* buffer and edit.

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

If you want things like * * * to work, more like a usual REPL, try ielm.

如果你想要* * *之类的东西工作,更像普通的REPL,试试ielm。

M-x ielm

#4


1  

In the *scratch* buffer, just type C-j to evaluate the expression before point.

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

#5


1  

Well, if you're really interested in a literal REPL for emacs it is possible to write one using the -batch mode of emacs:

好吧,如果你真的对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)

You can call this from the command line, or, as you seem to want, from within an emacs buffer running a shell:

您可以从命令行调用它,或者,如您所愿,从运行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$

#6


1  

To run just one elisp expression you can use M-: shortcut and enter expression in mini-buffer. For other cases you can use scratch buffer

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