Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow :file parameter in org-ql dynamic blocks #239

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 30 additions & 6 deletions org-ql-search.el
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,15 @@ automatically from the query."
"Insert content for org-ql dynamic block at point according to PARAMS.
Valid parameters include:

:scope The scope to consider for the Org QL query. This can
be one of the following:

`buffer' the current buffer
`org-agenda-files' all agenda files
`org-directory' all org files
`(\"path\" ...)' list of buffer names or file paths
`all' all agenda files, and org-mode buffers

:query An Org QL query expression in either sexp or string
form.

Expand Down Expand Up @@ -271,23 +280,38 @@ Valid parameters include:
For example, an org-ql dynamic block header could look like:

#+BEGIN: org-ql :query (todo \"UNDERWAY\") :columns (priority todo heading) :sort (priority date) :ts-format \"%Y-%m-%d %H:%M\""
(-let* (((&plist :query :columns :sort :ts-format :take) params)
(-let* (((&plist :scope :query :columns :sort :ts-format :take) params)
(query (cl-etypecase query
(string (org-ql--query-string-to-sexp query))
(list ;; SAFETY: Query is in sexp form: ask for confirmation, because it could contain arbitrary code.
(org-ql--ask-unsafe-query query)
query)))
(columns (or columns '(heading todo (priority "P"))))
(scope (cond ((and (listp scope) (seq-every-p #'stringp scope)) scope)
((string-equal scope "org-agenda-files") (org-agenda-files))
((or (not scope) (string-equal scope "buffer")) (current-buffer))
((string-equal scope "org-directory") (org-ql-search-directories-files))
(t (user-error "Unknown scope '%s'" scope))))
;; MAYBE: Custom column functions.
(format-fns
;; NOTE: Backquoting this alist prevents the lambdas from seeing
;; the variable `ts-format', so we use `list' and `cons'.
(list (cons 'todo (lambda (element)
(org-element-property :todo-keyword element)))
(cons 'heading (lambda (element)
(org-make-link-string (org-element-property :raw-value element)
(org-link-display-format
(org-element-property :raw-value element)))))
(cond
((and org-id-link-to-org-use-id
(org-element-property :ID element))
(org-make-link-string (format "id:%s" (org-element-property :ID element))
(org-element-property :raw-value element)))
((org-element-property :file element)
(org-make-link-string (format "file:%s::*%s"
(org-element-property :file element)
(org-element-property :raw-value element))
(org-element-property :raw-value element)))
(t (org-make-link-string (org-element-property :raw-value element)
(org-link-display-format
(org-element-property :raw-value element)))))))
(cons 'priority (lambda (element)
(--when-let (org-element-property :priority element)
(char-to-string it))))
Expand All @@ -302,9 +326,9 @@ For example, an org-ql dynamic block header could look like:
(ts-format ts-format (ts-parse-org-element it)))))
(cons 'property (lambda (element property)
(org-element-property (intern (concat ":" (upcase property))) element)))))
(elements (org-ql-query :from (current-buffer)
(elements (org-ql-query :from scope
:where query
:select '(org-element-headline-parser (line-end-position))
:select '(org-element-put-property (org-element-headline-parser (line-end-position)) :file (buffer-file-name))
:order-by sort)))
(when take
(setf elements (cl-etypecase take
Expand Down