Skip to content

Instantly share code, notes, and snippets.

@chebert chebert/lisp.el
Created Sep 3, 2017

Embed
What would you like to do?
chebert-eval-print-last-sexp
(defun multiline? (string)
(position ?\n string))
(defun multiline-comment (string)
(concat "#||\n" string "\n||#\n"))
;; Note: Redefining to send the error message too.
(defun chebert-eval-async (sexp &optional cont package)
"Evaluate EXPR on the superior Lisp and call CONT with the result."
(declare (indent 1))
(slime-rex (cont (buffer (current-buffer)))
(sexp (or package (slime-current-package)))
((:ok result)
(when cont
(set-buffer buffer)
(funcall cont (cons :ok result))))
((:abort condition)
(message "Evaluation aborted on %s." condition)
(when cont
(set-buffer buffer)
(funcall cont (cons :abort condition)))))
;; Guard against arbitrary return values which once upon a time
;; showed up in the minibuffer spuriously (due to a bug in
;; slime-autodoc.) If this ever happens again, returning the
;; following will make debugging much easier:
:slime-eval-async)
;; Note: Redefining to markup output and value.
(defun chebert-eval-print-last-sexp (string)
(interactive (list (slime-last-expression)))
(chebert-eval-async
`(swank:eval-and-grab-output ,string)
(lambda (status-and-result)
(let ((status (car status-and-result))
(result (cdr status-and-result)))
(case status
(:ok (cl-destructuring-bind (output value) result
(push-mark)
(let ((valstr (if (multiline? value)
(multiline-comment (concat " =>\n" value "\n"))
(if (zerop (length value))
""
(concat ";; => " value "\n")))))
(insert ?\n)
(if (zerop (length output))
(insert valstr)
(if (multiline? value)
(insert (multiline-comment (concat "Output:\n" output valstr)))
(insert (concat (multiline-comment (concat "Output:\n" output)) valstr)))))))
(:abort
(push-mark)
(insert "\n;; >> Evaluation aborted on " condition "\n")))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.