Hi, I've been wanting to add the ability to post-process the results of a code block for some time, and some recent threads (e.g., [1] and [2]) could both have benefited from post-processing of code block output.
The attached patch [3] adds a new :post header argument, which may be used to specify a code block reference to use to post-process the results of the current code block. The following example illustrates how this header argument could be used to add attr_latex lines to table code block results.
#+Title: Proposed =post= Header Argument #+Author: Eric Schulte * a code block which does the post processing This code block adds latex width attributes to whatever =data= is passed to it. #+name: add-attr #+begin_src sh :var data="" :results output drawer :var width="\textwidth" echo "#+attr_latex: width=$width" echo "$data" #+end_src * two code blocks which use the post processing The following two code blocks demonstrate the use of the new =post= header argument, whcih we could add if it seems generally useful and not too confusing. This works by lexically binding the =*this*= variable to the results of the current code block, and then calling the =add-attr= code block to post-process these results. #+begin_src sh :results output wrap :post add-attr(width="5cm",data=(identity *this*)) cat <<EOF | 1 | 2 | | 3 | 4 | EOF #+end_src #+RESULTS: :RESULTS: #+attr_latex: width=5cm | 1 | 2 | | 3 | 4 | :END: This code block does the same thing as the previous one, but using different values and arguments. #+header: :post add-attr(width="0.5\\textwidth",data=(identity *this*)) #+begin_src sh :results output wrap cat <<EOF | 5 | 6 | | 7 | 8 | EOF #+end_src #+RESULTS: :RESULTS: #+attr_latex: width=0.5\textwidth | 5 | 6 | | 7 | 8 | :END: * another unrelated example Say we only care about the size of our results, then we could use the following. #+name: wc #+begin_src sh data="" echo "$data"|wc #+end_src This code block checks the size of some Emacs Lisp data (in this case my =load-path= variable). #+begin_src emacs-lisp :post wc(data=(identity *this*)) load-path #+end_src #+RESULTS: : 160 160 6783
Does this new header argument seem useful? Any suggestions for better syntax which don't add too much conceptual or code complexity? Thanks, Footnotes: [1] http://thread.gmane.org/gmane.emacs.orgmode/69339 [2] http://thread.gmane.org/gmane.emacs.orgmode/68700 [3]
>From f83f31d82c0c660c74a2af7114d4e23c9b37c095 Mon Sep 17 00:00:00 2001 From: Eric Schulte <schulte.e...@gmail.com> Date: Sun, 31 Mar 2013 19:02:11 -0600 Subject: [PATCH] :post header arg post-processes code block results * lisp/ob-core.el (org-babel-common-header-args-w-values): Add :post to the list of header arguments. (org-babel-execute-src-block): Post process results when the :post header argument has been supplied. --- lisp/ob-core.el | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lisp/ob-core.el b/lisp/ob-core.el index 723aa9d..e1321eb 100644 --- a/lisp/ob-core.el +++ b/lisp/ob-core.el @@ -456,6 +456,7 @@ then run `org-babel-pop-to-session'." (noweb-ref . :any) (noweb-sep . :any) (padline . ((yes no))) + (post . :any) (results . ((file list vector table scalar verbatim) (raw html latex org code pp drawer) (replace silent none append prepend) @@ -625,6 +626,11 @@ block." (not (listp result))) (list (list result)) result)) (funcall cmd body params))) + ;; possibly perform post process provided its appropriate + (when (cdr (assoc :post params)) + (let ((*this* result)) + (setq result (org-babel-ref-resolve + (cdr (assoc :post params)))))) ;; if non-empty result and :file then write to :file (when (cdr (assoc :file params)) (when result -- 1.8.2
-- Eric Schulte http://cs.unm.edu/~eschulte