Hi Christian

Thanks for your effort! I am not familiar with org-mode intrinsics,
thats why I can not really review your patch. Regarding the update of
the dynamic block, I guess it makes sense to opt-out of this behavior
via a flag if you don't want changed and unsaved buffers?

Best,
Reza

> Hi, Reza,
>
> Reza Housseini <[email protected]> writes:
>> I tried the following
>>
>> #+NAME: my-table
>> #+BEGIN: clocktable :scope file :maxlevel 2
>> #+END:
>>
>> #+begin_src python :var data=my-table
>>   print(data)
>> #+end_src
>>
>> but the evaluation fails with the error
>>
>> Reference not found
>>
>> Why is this not working, is this intentional or a bug?
>
> I'd guess that it's just a missing feature. Babel actually does find
> my-table, it just doesn't know how it's supposed to read a dynamic
> block. I think it's reasonable to expect named dynamic blocks to be
> supported as Babel references.
>
> I submit a patch with one way to solve it: Get org-babel-ref-resolve to
> check if we're at a dynamic block. If so, update it, then move one line
> forward and resolve the first element in the content of the dynamic
> block instead. Then the existing machinery of org-babel-ref-resolve and
> org-babel-read-element can handle it depending on whether it's a table,
> a paragraph with a time stamp, a list, or something else.
>
> A couple of what-could-possibly-go-wrongs I'm unsure about:
>
> - It seems like a good idea that Babel should update a dynamic block
>   before reading it, so I have made it so here. This will however change
>   contents elsewhere in the buffer while executing a src block, and with
>   remote references it may open a file and and leave the buffer open
>   with changed contents. Is this acceptable behavior? Or should it be
>   made optional, and what should be the default?
>
> - This implies a fun metaprogramming side effect: we can also read /and/
>   execute src blocks written by dynamic blocks.
>
> Regards,
> Christian
>
> From 474c0f990677ebdad997b58bb6911189dbcdb93d Mon Sep 17 00:00:00 2001
> From: Christian Moe <[email protected]>
> Date: Sat, 14 Mar 2026 14:00:39 +0100
> Subject: [PATCH] ob-ref.el: Support reading dynamic blocks into Babel
>
> * lisp/ob-ref.el (org-babel-ref-resolve): Check if the reference is a
> dynamic block.  If so, update it and read the first element of the
> content instead.
>
> * doc/org-manual.org (Passing arguments): Mention support for dynamic
> blocks.
>
> This adds support for reading a named dynamic block like a clocktable
> or columnview via the `:var' header.  Previously, references to such
> blocks failed with "Reference not found".
>
> Reported-by: Reza Housseini <[email protected]>
> Link: https://list.orgmode.org/[email protected]
> ---
>  doc/org-manual.org | 8 ++++++++
>  lisp/ob-ref.el     | 5 +++++
>  2 files changed, 13 insertions(+)
>
> diff --git a/doc/org-manual.org b/doc/org-manual.org
> index 904e1270d..0dfdb6c1e 100644
> --- a/doc/org-manual.org
> +++ b/doc/org-manual.org
> @@ -18666,6 +18666,14 @@ a colon, for example: =:var 
> table=other-file.org:example-table=.
>    : on two lines for you.
>    #+end_example
>  
> +- dynamic block ::
> +
> +  A dynamic block, such as a columnview or a clocktable, named with a
> +  =NAME= keyword.  The block is updated before the contents are read.
> +  Depending on how the dynamic block writes content, e.g. as a table
> +  or a list, the content is read accordingly, as described in the
> +  examples above.
> +  
>  Indexing variable values enables referencing portions of a variable.
>  Indexes are 0 based with negative values counting backwards from the
>  end.  If an index is separated by commas then each subsequent section
> diff --git a/lisp/ob-ref.el b/lisp/ob-ref.el
> index 14c5ce4a9..29d23e4f0 100644
> --- a/lisp/ob-ref.el
> +++ b/lisp/ob-ref.el
> @@ -174,6 +174,11 @@ Emacs Lisp representation of the value of the variable."
>                          (when (equal (org-element-property :name e) ref)
>                            (goto-char
>                             (org-element-post-affiliated e))
> +                             ;; Dynamic blocks resolve to content element
> +                             (when (eq (org-element-type e) 'dynamic-block)
> +                               (org-update-dblock)
> +                               (forward-line)
> +                               (setq e (org-element-at-point)))
>                            (pcase (org-element-type e)
>                              (`babel-call
>                               (throw :found
> -- 
> 2.43.0

Reply via email to