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

org-ql-search.el: Extend org-dblock-write:org-ql to indicate query scope #212

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions org-ql-search.el
Original file line number Diff line number Diff line change
@@ -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
`(list-of-files ...)' a list of org files
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm glad you listed these choices explicitly. However, this list-of-files one needs clarification: is it an arbitrary Lisp list? Arbitrary Lisp code? A list of strings? A list of symbols which may evaluate to strings? etc.

Of course, it should probably only accept one string or a list of strings; any other types of value or a list of any other types of value should be rejected. However, we might consider accepting variables and lists of variables, which would make it easier for the user to refer to sets of files.

Either way, we should be very careful to avoid security issues with executing arbitrary code when the block is updated. Please see the tests related to the other block arguments for examples.

`all' all agenda files, and org-mode buffers

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

@@ -271,23 +280,30 @@ 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))
(string (org-ql--plain-query query))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change probably indicates that this patch is based on an outdated version of org-ql.

(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 ((listp scope) scope)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See first comment about arbitrary Lisp values.

((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)))))
(let ((m (plist-get (cadr element) :org-hd-marker)))
(with-current-buffer (marker-buffer m)
(save-excursion
(goto-char m)
(org-store-link nil nil))))))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for these changes? They seem unrelated to the PR's purpose.

(cons 'priority (lambda (element)
(--when-let (org-element-property :priority element)
(char-to-string it))))
@@ -299,9 +315,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 'element-with-markers
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this change made?

:order-by sort)))
(when take
(setf elements (cl-etypecase take