我正在尝试编写一个elisp函数,如果它没有运行,它将自动启动史莱姆,然后在将它们加载到repl中之后更改为某个quicklisp软件包。当史莱姆已经运行时,以下代码将起作用。当史莱姆重新开始时,quicklisp软件包会由史莱姆加载,但是对SLime-eval-expression-in-repl的调用似乎没有执行。
(defun cm ()
"Start CM"
(interactive)
(cond ((slime-connected-p)
(slime-eval '(ql:quickload "cm"))
(slime-eval-expression-in-repl "(cm)")
(switch-to-buffer (slime-repl-buffer)))
(t
(slime)
(eval-after-load 'slime
(progn
(slime-eval '(ql:quickload "cm"))
(slime-eval-expression-in-repl "(cm)")
(switch-to-buffer (slime-repl-buffer)))))))
以下是更改为SLIM-EVAL-EXPRESS-IN-REPL的包和功能定义的代码(改编自SLIME-EVAL-LAST-EXPRESSION-REPL)。我该如何解决?
常用的Lisp功能:
(defun cm (&rest systems)
(flet ((cmcall (fn &rest args)
(apply (find-symbol (string fn) :cm) args))
(cmvar (var)
(symbol-value (find-symbol (string var) :cm))))
(setf *package* (find-package :cm))
(setf *readtable* (cmvar :*cm-readtable*))
;; add slime readtable mapping...
(let ((swank-pkg (find-package :swank)))
(when swank-pkg
(let ((sym (intern (symbol-name :*readtable-alist*) swank-pkg)))
(setf (symbol-value sym)
(cons (cons (symbol-name :cm) (cmvar :*cm-readtable*))
(symbol-value sym))))))
(let (#-sbcl (*trace-output* nil))
(dolist (s systems) (use-system s :verbose nil)))
(cmcall :cm-logo)))
这里是slime-eval-expression-in-repl的定义:
(defun slime-eval-expression-in-repl (expr)
"Evaluates expression in the Slime REPL.
Switches REPL to current package of the source buffer for the duration. If
used with a prefix argument (C-u), doesn't switch back afterwards."
(interactive "P")
(let ((buffer-name (buffer-name (current-buffer)))
(new-package (slime-current-package))
(old-package (slime-lisp-package))
(slime-repl-suppress-prompt t)
(yank-back nil))
(with-current-buffer (slime-output-buffer)
(unless (eq (current-buffer) (window-buffer))
(pop-to-buffer (current-buffer) t))
(goto-char (point-max))
;; Kill pending input in the REPL
(when (< (marker-position slime-repl-input-start-mark) (point))
(kill-region slime-repl-input-start-mark (point))
(setq yank-back t))
(unwind-protect
(progn
(insert-before-markers (format "\n;;; from %s\n" buffer-name))
(when new-package
(slime-repl-set-package new-package))
(let ((slime-repl-suppress-prompt nil))
(slime-repl-insert-prompt))
(insert expr)
(slime-repl-return)))
;; Put pending input back.
(when yank-back
(yank)))))
(顺便说一句:我知道关于stackoverflow的类似问题,但是给出的答案似乎在这里并不适用)
我解决了它:有必要使用'slime-connected-hook,而不是eval-after-load。另外,函数slime-eval-expression-in-repl已作为'slime-repl-send-string存在。下面是我的解决方案。这有点棘手,因为我不想永久添加粘液连接钩。
(defun load-and-start-cm ()
(slime-eval '(ql:quickload "cm"))
(slime-repl-send-string "(cm)")
(switch-to-buffer (slime-repl-buffer)))
(defun load-and-start-cm-remove-hook ()
"after starting cm remove this hook from slime-connected-hook"
(load-and-start-cm)
(setq slime-connected-hook
(remove 'load-and-start-cm-remove-hook slime-connected-hook)))
(defun cm ()
"Start CM"
(interactive)
(message "entering cm function.")
(cond ((slime-connected-p)
(load-and-start-cm))
(t ;;; temporarily set slime-connected-hook to start cm
(unless (member 'load-and-start-cm slime-connected-hook)
(setq slime-connected-hook
(append slime-connected-hook '(load-and-start-cm-remove-hook))))
(slime))))