I have the following:
#+BEGIN_SRC lisp :results value (dolist (x '(1 2 3)) (print x)) #+END_SRC #+RESULTS: : NIL
I want my results block to contain both the output and the value for my source. How do I do that?
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.Sign up to join this community
:results value and
:results output are collection options.
From the org manual:
Collection options specify the results. Choose one of the options; they are mutually exclusive.
So they are mutually exclusive.
If the evaluation takes only little time you can call the source block twice with different collection options:
#+NAME: F #+BEGIN_SRC emacs-lisp :results output (dolist (x '(1 2 3)) (print x)) "Hello World" #+END_SRC #+RESULTS: F : : 1 : : 2 : : 3 #+CALL: F[:results value]() #+RESULTS: : Hello World
If the evaluation of the source block is costly you can redirect the output to the value of the block. I've written a macro
with-cons-value.stdout for that purpose. If you encapsulate your lisp forms by that macro you get a
cons with the last value of the lisp forms as
car and the
stdout string as
cdr. If you need the macro multiple times you can move it from the source block to your init file.
#+BEGIN_SRC emacs-lisp :results value (defmacro with-cons-value.stdout (&rest body) "Evaluate BODY like `progn'. Return `cons' with value of BODY as `car' and the standard output string as `cdr'." (declare (debug body)) `(let ((old-stdout standard-output) stdout) (unwind-protect (progn (setq stdout (generate-new-buffer "stdout") standard-output stdout) (cons (progn ,@body) (with-current-buffer stdout (buffer-string))) ) (setq standard-output old-stdout) (kill-buffer stdout)))) (with-cons-value.stdout (dolist (x '(1 2 3)) (print x)) "Hello World") #+END_SRC #+RESULTS: : (Hello World . : 1 : : 2 : : 3 : )