Skip to content

Instantly share code, notes, and snippets.

@chebert
Created September 3, 2017 01:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chebert/e8e54bac7d9c21c056433cc9fc956317 to your computer and use it in GitHub Desktop.
Save chebert/e8e54bac7d9c21c056433cc9fc956317 to your computer and use it in GitHub Desktop.
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