如何在 Emacs Lisp 中使用 Slime 运行 Common Lisp 代码

sav*_*ior 3 emacs common-lisp

出于某种原因,我编写了一些 Common Lisp 代码来完成我想要的操作。我使用 QuickLisp 和 Slime。现在我希望能与Emacs Lisp集成。

我尝试使用

(slime)
(slime-eval-region start end)
...
Run Code Online (Sandbox Code Playgroud)

在我的 el 文件中,但它不起作用。

我只是运行 Common Lisp 代码并捕获返回值,仅此而已。所以我该怎么做?

abo*_*abo 5

如果我理解正确的话,您想要将 Common Lisp 代码作为 Elisp 字符串,在 SLIME 中对其进行评估,并获得作为 Elisp 字符串加上副作用的输出。

您可以使用以下设置代码来执行此操作:

(require 'slime)
(defun lispy--eval-lisp (str)
  "Eval STR as Common Lisp code."
  (unless (slime-current-connection)
    (let ((wnd (current-window-configuration)))
      (slime)
      (while (not (and (slime-current-connection)
                       (get-buffer-window (slime-output-buffer))))
        (sit-for 0.2))
      (set-window-configuration wnd)))
  (let (deactivate-mark)
    (cadr (slime-eval `(swank:eval-and-grab-output ,str)))))
Run Code Online (Sandbox Code Playgroud)

或者,(require 'le-lisp)如果您已经从 MELPA 或githublispy安装了软件包 。

以下是 中的示例用法*scratch*

(lispy--eval-lisp "(load \"~/quicklisp/setup\")")
;; "T"

(lispy--eval-lisp "(ql:quickload 'png)")
;; "(PNG)"

(lispy--eval-lisp "(png:make-image 5 5 1)")
;; "#3A(((0) (0) (0) (0) (0))
;;     ((0) (0) (0) (0) (0))
;;     ((0) (0) (0) (0) (0))
;;     ((0) (0) (0) (0) (0))
;;     ((0) (0) (0) (0) (0)))"
Run Code Online (Sandbox Code Playgroud)