Attached is a patch to allow one to specify that results from executing a block should go to a specific buffer.
When a :buffer is specified, output goes to that buffer, which is erased first, e.g.: #+begin_src sh :results buffer :buffer *foo* echo foo #+end_src When no :buffer is specified, buffer *org results* is used, e.g.: #+begin_src sh :results buffer echo foo #+end_src I've tried to follow the conventions for contributions to Org mode, buf if I've missed something, please let me know. (I'm happy to sign the FSF paperwork.) Thank you for Org mode!
From 52de54b8cf5b91ac01d3a566b3c1ca6176a9cb1d Mon Sep 17 00:00:00 2001 From: "Arthur A. Gleckler" <a...@alum.mit.edu> Date: Wed, 11 Sep 2019 17:03:33 -0700 Subject: [PATCH] ob-core.el: block result output to buffer * lisp/ob-core.el (org-babel-execute-src-block): Support buffers. Allow specifying that results from executing a block should go to a buffer. When a :buffer is specified, output goes to that buffer, which is erased first, e.g.: #+begin_src sh :results buffer :buffer *foo* echo foo #+end_src When no :buffer is specified, buffer "*org results*" is used, e.g.: #+begin_src sh :results buffer echo foo #+end_src Also fixed typo: "it's" vs. "its". --- doc/org-manual.org | 8 ++++++++ lisp/ob-core.el | 44 ++++++++++++++++++++++++++++---------------- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/doc/org-manual.org b/doc/org-manual.org index f2f059e77..8e2715eeb 100644 --- a/doc/org-manual.org +++ b/doc/org-manual.org @@ -17333,6 +17333,14 @@ default behavior is to automatically determine the result type. TAB-delimited output. You can choose a different separator with the =sep= header argument. +- =buffer= :: + + Save results to a specific buffer. Erase the buffer, save the + results of execution of the code block to that buffer, then display + it. The buffer name may be specified in the =:buffer= header + argument. If it's not specified, the buffer =*org results*= is + used. + *** Format :PROPERTIES: :UNNUMBERED: notoc diff --git a/lisp/ob-core.el b/lisp/ob-core.el index 97ec18fd1..1537b4363 100644 --- a/lisp/ob-core.el +++ b/lisp/ob-core.el @@ -707,22 +707,33 @@ block." (not (listp r))) (list (list r)) r))) - (let ((file (and (member "file" result-params) + (let ((buffer (and (member "buffer" result-params) + (get-buffer-create + (or (cdr (assq :buffer params)) + "*org result*")))) + (file (and (member "file" result-params) (cdr (assq :file params))))) ;; If non-empty result and :file then write to :file. - (when file - ;; If `:results' are special types like `link' or - ;; `graphics', don't write result to `:file'. Only - ;; insert a link to `:file'. - (when (and result - (not (or (member "link" result-params) - (member "graphics" result-params)))) - (with-temp-file file - (insert (org-babel-format-result - result - (cdr (assq :sep params)))))) - (setq result file)) - ;; Possibly perform post process provided its + (cond (buffer + (with-current-buffer buffer + (erase-buffer) + (insert (org-babel-format-result + result + (cdr (assq :sep params))))) + (display-buffer buffer)) + (file + ;; If `:results' are special types like `link' or + ;; `graphics', don't write result to `:file'. Only + ;; insert a link to `:file'. + (when (and result + (not (or (member "link" result-params) + (member "graphics" result-params)))) + (with-temp-file file + (insert (org-babel-format-result + result + (cdr (assq :sep params)))))) + (setq result file))) + ;; Possibly perform post process provided it's ;; appropriate. Dynamically bind "*this*" to the ;; actual results of the block. (let ((post (cdr (assq :post params)))) @@ -735,8 +746,9 @@ block." (setq result (org-babel-ref-resolve post)) (when file (setq result-params (remove "file" result-params)))))) - (org-babel-insert-result - result result-params info new-hash lang))) + (unless buffer + (org-babel-insert-result + result result-params info new-hash lang)))) (run-hooks 'org-babel-after-execute-hook) result))))))) -- 2.20.1