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

3
出于某些原因,我编写了一些Common Lisp代码以实现我的需求。我使用QuickLisp和Slime。现在我希望与Emacs Lisp集成。
我尝试使用
(slime)
(slime-eval-region start end)
...

我在我的el文件中编写了代码,但是它并没有起作用。

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

1个回答

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)))))

或者,如果您已经从MELPA或github安装了lispy包,则只需输入(require 'le-lisp)即可。

下面是在*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)))"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接