emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Post-process table without changing result for empty table(/list)
@ 2022-10-03 19:29 Jonas Bernoulli
  2022-10-04  2:55 ` Ihor Radchenko
  0 siblings, 1 reply; 9+ messages in thread
From: Jonas Bernoulli @ 2022-10-03 19:29 UTC (permalink / raw)
  To: emacs-orgmode

Hello,

If a code-block that is supposed to produce a table returns an empty
list, then nothing at all is inserted, as demonstrated by the second
example below:

#+begin_src emacs-lisp :results table
  ;; Some sql query that returns one or more rows:
  '(("foo" "bar"))
#+end_src
#+RESULTS:
| foo | bar |

#+begin_src emacs-lisp :results table
  ;; Another sql query that returns zero rows:
  '()
#+end_src
#+RESULTS:

I would like to keep it that way, but also use post-processing to add
a header to all non-empty tables (and only those).

Unfortunately post-processing changes how nil aka empty list aka empty
table is handled:

#+name: addheader
#+header: :var rows="" :var header=""
#+begin_src elisp :hlines yes
  (and rows `(("Key" "Value") hline ,@rows))
#+end_src

#+header: :post addheader(*this*,'("Key" "Value"))
#+begin_src emacs-lisp :results table
  ;; Some sql query that returns one or more rows:
  '(("foo" "bar"))
#+end_src
#+RESULTS:
| Key | Value |
|-----+-------|
| foo | bar   |

#+header: :post addheader(*this*,'("Key" "Value"))
#+begin_src emacs-lisp :results table
  ;; Another sql query that returns zero rows:
  '()
#+end_src
#+RESULTS:
: nil

I would like it to result in just

#+RESULTS:

like when no post-processing is involved.

It used to behave like that before 51a628bc5efc from 2009, which started
turning all symbols, including nil, into strings, but without giving any
reason why that should be done.

It has worked like this for a long time now, so reverting that is
probably not feasible in the short run.  However, I feel it would
make sense to change now how nil/'() is treated.  Currently it is
being treated as the symbol nil, but IMO it would make more sense
to treat it as the empty list.  That could be achieved with

diff --git a/lisp/ob-ref.el b/lisp/ob-ref.el
index b79e47900..2b4a16aea 100644
--- a/lisp/ob-ref.el
+++ b/lisp/ob-ref.el
@@ -199,7 +199,7 @@ (defun org-babel-ref-resolve (ref)
                                (org-babel-execute-src-block nil info params))))
                     (error "Reference `%s' not found in this buffer" ref))))
             (cond
-             ((symbolp result) (format "%S" result))
+             ((and result (symbolp result)) (format "%S" result))
              ((and index (listp result))
               (org-babel-ref-index-list index result))
              (t result)))))))))

In the long run you might want to consider not turning any symbols
into strings, at least not when the "regular" block as well as the
post-processing block both use elisp.

     Thanks for considering my suggestions!
     Jonas


^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2022-10-07  4:09 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-10-03 19:29 Post-process table without changing result for empty table(/list) Jonas Bernoulli
2022-10-04  2:55 ` Ihor Radchenko
2022-10-04 14:00   ` Jonas Bernoulli
2022-10-05  7:37     ` Ihor Radchenko
2022-10-05 13:21   ` [PATCH 0/1] Allow returning empty list from post-processing block Jonas Bernoulli
2022-10-05 13:21     ` [PATCH 1/1] " Jonas Bernoulli
2022-10-06  3:15       ` Ihor Radchenko
2022-10-06 11:11         ` [PATCH v1] " Jonas Bernoulli
2022-10-07  4:09           ` Ihor Radchenko

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).