Skip to content

Instantly share code, notes, and snippets.

@spacebat
Created July 11, 2020 01:51
Show Gist options
  • Save spacebat/c3502ab32fba91f440f89f5ab0f41c74 to your computer and use it in GitHub Desktop.
Save spacebat/c3502ab32fba91f440f89f5ab0f41c74 to your computer and use it in GitHub Desktop.
Flexible macro to output to a stream (default returns a string)
(defmacro with-output-to-stream ((var &optional stream &key (element-type 'character)) &body body)
(let ((gstream (gensym "STREAM"))
(gresult (gensym "RESULT")))
`(let* ((,gstream ,stream)
(,var (case ,gstream
((t) *standard-output*)
((nil) (make-string-output-stream :element-type ',element-type))
(otherwise ,gstream)))
(,gresult (multiple-value-list (progn ,@body))))
(if ,gstream
(values-list ,gresult)
(get-output-stream-string ,var)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment