Ihor Radchenko <[email protected]> writes:

> Pete Watt-Trump <[email protected]> writes:
>
>> This is arguably an ob-jupyter bug, but the broader issue remains: there
>> is no reliable way to attach captions to results across babel backends.
>> A :result-caption header arg (or similar) would solve it portably.

I was able to reproduce the bug on my end when executing the following:

#+begin_src jupyter-python :session /jpy::test :async yes
from IPython.display import publish_display_data
publish_display_data({'text/org': "| A | B |\n|---+---|\n| 1 | 2 |"})
#+end_src

#+name: tab:summary
#+caption: Summary statistics.
#+RESULTS:
| A | B |
|---+---|
| 1 | 2 |

It doesn't happen for synchronous source blocks since that goes through
the normal Org result insertion path.  It also doesn't happen for named
source blocks that are asynchronous.  I was able to trace the difference
back to org-babel-where-is-src-block-result and the fact that ob-jupyter
would have the intermediate state (during asynchronous execution)

#+name: tab:summary
#+caption: Summary statistics.
#+RESULTS:

when org-babel-where-is-src-block-result is called when the buffer is in
this state it sees the NAME keyword as the next element after the source
block so it doesn't think that there is a result as it doesn't know
about such sequences of keywords with a terminating RESULTS keyword as
being an empty result.

>
> I'd say that
> #+caption: ...
> #+results:
> is rather reliable.
>

Attached is a patch with a proposed solution which handles contiguous
keyword sequences so that org-babel-where-is-src-block-result can
recognize empty, anonymous, results.

Let me know what you think.

-- 
Nathaniel Nicandro
>From fb3377a8837508dd0e152d7e33e924fe5cd522ec Mon Sep 17 00:00:00 2001
From: Nathaniel Nicandro <[email protected]>
Date: Wed, 4 Mar 2026 16:13:07 -0600
Subject: [PATCH] org-babel-where-is-src-block-result: Handle consecutive
 keywords

* lisp/ob-core.el (org-babel-where-is-src-block-result): A RESULTS
keyword can have multiple contiguous keywords before it, e.g. a NAME
and a CAPTION, that would also be affiliated with the result if it
were present. If only the keywords are present, without a result
element, the position of the RESULTS keyword should still be
returned. Make this behavior consistent across named and anonymous
results.
---
 lisp/ob-core.el | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index a8ca1ccd08..34c4bb2571 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -2369,6 +2369,14 @@ (defun org-babel-where-is-src-block-result (&optional insert _info hash)
 	     ;; the current block.
 	     ((let* ((next (org-element-at-point))
 		     (end (save-excursion
+                            ;; Handle a consecutive sequence of
+                            ;; keywords without whitespace in between.
+                            (when (eq (org-element-type next) 'keyword)
+                              (while (zerop (org-element-post-blank next))
+                                (let ((after (org-element-at-point
+                                              (org-element-end next))))
+                                  (when (eq (org-element-type after) 'keyword)
+                                    (setq next after)))))
 			    (goto-char
 			     (org-element-post-affiliated next))
 			    (line-end-position)))
-- 
2.52.0

Reply via email to