Skip to content

Commit 597b413

Browse files
committed
org-ql-search.el: Extend org-dblock-write:org-ql to indicate query scope
1 parent 208e103 commit 597b413

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

Diff for: org-ql-search.el

+23-7
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,15 @@ automatically from the query."
242242
"Insert content for org-ql dynamic block at point according to PARAMS.
243243
Valid parameters include:
244244
245+
:scope The scope to consider for the Org QL query. This can
246+
be one of the following:
247+
248+
`buffer' the current buffer
249+
`org-agenda-files' all agenda files
250+
`org-directory' all org files
251+
`(list-of-files ...)' a list of org files
252+
`all' all agenda files, and org-mode buffers
253+
245254
:query An Org QL query expression in either sexp or string
246255
form.
247256
@@ -271,23 +280,30 @@ Valid parameters include:
271280
For example, an org-ql dynamic block header could look like:
272281
273282
#+BEGIN: org-ql :query (todo \"UNDERWAY\") :columns (priority todo heading) :sort (priority date) :ts-format \"%Y-%m-%d %H:%M\""
274-
(-let* (((&plist :query :columns :sort :ts-format :take) params)
283+
(-let* (((&plist :scope :query :columns :sort :ts-format :take) params)
275284
(query (cl-etypecase query
276-
(string (org-ql--query-string-to-sexp query))
285+
(string (org-ql--plain-query query))
277286
(list ;; SAFETY: Query is in sexp form: ask for confirmation, because it could contain arbitrary code.
278287
(org-ql--ask-unsafe-query query)
279288
query)))
280289
(columns (or columns '(heading todo (priority "P"))))
290+
(scope (cond ((listp scope) scope)
291+
((string-equal scope "org-agenda-files") (org-agenda-files))
292+
((or (not scope) (string-equal scope "buffer")) (current-buffer))
293+
((string-equal scope "org-directory") (org-ql-search-directories-files))
294+
(t (user-error "Unknown scope '%s'" scope))))
281295
;; MAYBE: Custom column functions.
282296
(format-fns
283297
;; NOTE: Backquoting this alist prevents the lambdas from seeing
284298
;; the variable `ts-format', so we use `list' and `cons'.
285299
(list (cons 'todo (lambda (element)
286300
(org-element-property :todo-keyword element)))
287301
(cons 'heading (lambda (element)
288-
(org-make-link-string (org-element-property :raw-value element)
289-
(org-link-display-format
290-
(org-element-property :raw-value element)))))
302+
(let ((m (plist-get (cadr element) :org-hd-marker)))
303+
(with-current-buffer (marker-buffer m)
304+
(save-excursion
305+
(goto-char m)
306+
(org-store-link nil nil))))))
291307
(cons 'priority (lambda (element)
292308
(--when-let (org-element-property :priority element)
293309
(char-to-string it))))
@@ -299,9 +315,9 @@ For example, an org-ql dynamic block header could look like:
299315
(ts-format ts-format (ts-parse-org-element it)))))
300316
(cons 'property (lambda (element property)
301317
(org-element-property (intern (concat ":" (upcase property))) element)))))
302-
(elements (org-ql-query :from (current-buffer)
318+
(elements (org-ql-query :from scope
303319
:where query
304-
:select '(org-element-headline-parser (line-end-position))
320+
:select 'element-with-markers
305321
:order-by sort)))
306322
(when take
307323
(setf elements (cl-etypecase take

0 commit comments

Comments
 (0)