Implement simple custom columns for dynamic blocks

Allows passing a function in an org-ql dynamic block's columns list to
act on a matched heading's Org element.
This commit is contained in:
libanp 2026-07-03 23:54:13 +01:00
parent 4b8330a683
commit f9e4ac360f

View file

@ -287,18 +287,37 @@ Valid parameters include:
form. form.
:columns A list of columns, including `heading', `todo', :columns A list of columns, including `heading', `todo',
`property',`priority',`deadline',`scheduled',`closed'. `property', `priority', `deadline', `scheduled',
Each column may also be specified as a list with the `closed', `custom'. Each column may also be
second element being a header string. For example, specified as a list with the second element being a
to abbreviate the priority column: (priority \"P\"). header string. For example, to abbreviate the
For certain columns, like `property', arguments may priority column: (priority \"P\"). For certain
be passed by specifying the column type itself as a columns, like `property', arguments may be passed
list. For example, to display a column showing the by specifying the column type itself as a list. For
values of a property named \"milestone\", with the example, to display a column showing the values of
header being abbreviated to \"M\": a property named \"milestone\", with the header
being abbreviated to \"M\":
((property \"milestone\") \"M\"). ((property \"milestone\") \"M\").
For a `custom' column, the first argument is a
function, followed by any arguments to that
function. A header string is required:
((custom my-func \"arg\") \"MyHeading\").
The custom function must return a string.
Note that the first argument passed to the custom
function will be the Org element of the matched
heading. So the function above will run as:
(my-func element \"arg\")
The alist of column lambdas in this function are
useful starting points for writing your own custom
column functions.
:sort One or a list of Org QL sorting methods :sort One or a list of Org QL sorting methods
(see `org-ql-select'). (see `org-ql-select').
@ -345,7 +364,13 @@ this (must be a single line in the Org buffer):
(--when-let (org-element-property :closed element) (--when-let (org-element-property :closed element)
(ts-format ts-format (ts-parse-org-element it))))) (ts-format ts-format (ts-parse-org-element it)))))
(cons 'property (lambda (element property) (cons 'property (lambda (element property)
(org-element-property (intern (concat ":" (upcase property))) element))))) (org-element-property (intern (concat ":" (upcase property))) element)))
(cons 'custom (lambda (element func &rest args)
(let ((result (apply func element args)))
(if (stringp result)
result
(error "org-dblock-write:org-ql: Custom column %s must return a string"
(if (symbolp func) func "lambda"))))))))
(elements (org-ql-query :from (current-buffer) (elements (org-ql-query :from (current-buffer)
:where query :where query
:select '(org-ql-view--resolve-element-properties :select '(org-ql-view--resolve-element-properties