Add with-output-to! macro

For piping output to both stdout and a file/buffer. Used in the coming
CLI rewrite.
This commit is contained in:
Henrik Lissner 2020-06-05 14:49:53 -04:00
parent 68815a6a37
commit 0f03c32a12
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -242,10 +242,31 @@ Uses faces in interactive sessions and ANSI codes otherwise."
"Like `user-error', but with the power of `format!'." "Like `user-error', but with the power of `format!'."
`(user-error (format! ,message ,@args))) `(user-error (format! ,message ,@args)))
;; ;;;###autoload ;;;###autoload
;; (defmacro with-output-to-buffer! (dest &rest body) (defmacro with-output-to! (dest &rest body)
;; "Send all output produced in BODY to DEST. "Send all output produced in BODY to DEST.
;; DEST can be one or more of `standard-output', a buffer, a file DEST can be one or more of `standard-output', a buffer, a file"
(declare (indent 1))
`(let* ((log-buffer (generate-new-buffer " *doom log*"))
;; ) (standard-output
(lambda (out)
(with-current-buffer log-buffer
(insert-char out))
(send-string-to-terminal (char-to-string out)))))
(letf! (defun message (msg &rest args)
(princ (apply #'format msg args))
(terpri))
(unwind-protect
,(macroexp-progn body)
(with-current-buffer log-buffer
(require 'ansi-color)
(ansi-color-filter-region (point-min) (point-max)))
(let ((dest ,dest))
(cond ((bufferp dest)
(with-current-buffer dest
(insert-buffer-substring log-buffer)))
((stringp dest)
(make-directory (file-name-directory dest) 'parents)
(with-temp-file dest
(insert-buffer-substring log-buffer))))
(kill-buffer log-buffer))))))