The simplest solution would be to write an elisp function that adds the table header and to wrap the code in the source blocks with this function.
It may be that this is not possible, because the Elisp blocks in your question are just replacements for blocks in other programming languages.
In that case we need to take a closer look at the cause of the result output : nil
:
The :post
header argument is evaluated by org-babel-ref-resolve
close to the end of org-babel-execute-src-block
.
The problem is that org-babel-ref-resolve
returns the string "nil"
instead of the symbol nil
. That result is printed as : nil
rather than dismissing it.
This is caused by the final lines of org-babel-ref-resolve
:
(defun org-babel-ref-resolve (ref)
...
(cond
((symbolp result) (format "%S" result))
((and index (listp result))
(org-babel-ref-index-list index result))
(t result)))))))))
I wonder whether the formatting of symbols as string is really appropriate there.
Maybe, one should remove that format
-line at all:
(defun org-babel-ref-resolve (ref)
...
(cond
((and index (listp result))
(org-babel-ref-index-list index result))
(t result)))))))))
(BTW: The cond
could be replaced by an if
in this case.)
If you are not so adventurous you could only keep the symbol nil
and transform other symbols to strings:
(defun org-babel-ref-resolve (ref)
...
(cond
((and result (symbolp result)) (format "%S" result))
((and index (listp result))
(org-babel-ref-index-list index result))
(t result)))))))))
If you do not want to tinker with the source code directly,
you can use the following code in your init file.
It is a bit like advising a function, but one can replace specific syntactic expressions within function definitions, such as (symbolp return)
, with other expressions, such as (and return (symbolp return))
.
(require 'ob-ref)
(defun lispTZA-replace-in-fundef (fun from to)
"Replace sexp FROM with sexp TO in function definition of symbol FUN.
Note, that the source file for FUN must be available.
Return the modified symbol-function."
(condition-case err
(let* ((pos (find-function-noselect fun t))
(buf (car pos))
(pt (cdr pos)))
(with-current-buffer buf
(save-excursion
(goto-char pt)
(eval (read buf) t))))
(error nil))
(when (setq fun (and (symbolp fun) (symbol-function fun)))
(let (transform-tree)
(setq transform-tree
(lambda (tree)
(cond
((equal tree from)
to)
((consp tree)
(cons ;; This does a copy for us.
(funcall transform-tree (car tree))
(funcall transform-tree (cdr tree))))
(t
tree))))
(funcall transform-tree fun))))
;; For reloading the init file:
(advice-remove 'org-babel-ref-resolve #'ad-org-babel-ref-resolve)
(defalias 'ad-org-babel-ref-resolve
(lispTZA-replace-in-fundef
'org-babel-ref-resolve
'(symbolp result) '(and result (symbolp result)))
"Replacement for `org-babel-ref-resolve' not turning a nil result into a string.")
(advice-add 'org-babel-ref-resolve :override #'ad-org-babel-ref-resolve)
Tested with Org-version 9.3.7.