Come invocare un interprete elisp interattivo in Emacs?
-
02-07-2019 - |
Domanda
In questo momento scrivo espressioni nel buffer * scratch *
e le collaudo valutando con C-x C-e . Gradirei molto avere un interprete interattivo come SLIME o irb, in cui ho potuto testare le espressioni di Emacs Lisp.
Soluzione
È facile valutare le espressioni Lisp in Inferior Emacs-Lisp Mode:
M-x ielm
Puoi leggere ulteriori informazioni su questa funzione negli Emacs sezione manuale su " Interazione Lisp &
Altri suggerimenti
Eshell è un'altra opzione per un Elisp interattivo interprete.
M-x eshell
Non solo è una shell di comandi come bash (o cmd.exe se su Windows) ma è anche possibile scrivere ed eseguire interattivamente il codice Elisp.
~ $ ls
foo.txt
bar.txt
~ $ (+ 1 1)
2
La tua scommessa migliore è il buffer * scratch *
. Puoi renderlo più simile a un REPL attivando prima il debugger:
M-x set-variable debug-on-error t
Quindi usa C-j
invece di C-x C-e
, che inserirà il risultato della valutazione dell'espressione nel buffer sulla riga dopo l'espressione. Invece di cose come la cronologia dei comandi, * * *
e così via, basta spostarsi nel buffer * scratch *
e modificarlo.
Se vuoi che funzioni come * * *
, più come un normale REPL, prova ielm
.
M-x ielm
Nel buffer * scratch *
, basta digitare C-j per valutare l'espressione prima del punto.
Bene, se sei veramente interessato a un REPL letterale per emacs è possibile scriverne uno usando la modalità -batch di emacs:
(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)
Puoi chiamarlo dalla riga di comando o, come sembri volere, da un buffer emacs che esegue una shell:
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$
Per eseguire solo un'espressione elisp è possibile utilizzare M-: collegamento e immettere expression nel mini-buffer. Per altri casi puoi usare scratch buffer