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

update #239 #298

Closed
wants to merge 9 commits into from
42 changes: 38 additions & 4 deletions org-ql-search.el
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,16 @@ automatically from the query."
"Insert content for org-ql dynamic block at point according to PARAMS.
Valid parameters include:

:buffers-files The buffers or files to consider for the Org QL
query. This can be one of the following:

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

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

Expand Down Expand Up @@ -280,13 +290,25 @@ 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 :buffers-files :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"))))
(buffers-files (pcase buffers-files
('buffer (current-buffer))
('all (--select (equal (buffer-local-value 'major-mode it) 'org-mode)
(buffer-list)))
('org-agenda-files (org-agenda-files))
('oorg-directory (org-ql-search-directories-files))
((and (pred listp) (pred seq-every-p #'stringp)) buffers-files)
((pred stringp)
(if (file-directory-p buffers-files)
(directory-files-recursively buffers-files "\\.org$")
buffers-files))
(_ (user-error "Unknown buffers-files '%s'" buffers-files))))
;; MAYBE: Custom column functions.
(format-fns
;; NOTE: Backquoting this alist prevents the lambdas from seeing
Expand All @@ -296,7 +318,19 @@ For example, an org-ql dynamic block header could look like:
(cons 'heading (lambda (element)
(let ((normalized-heading
(org-ql-search--link-heading-search-string (org-element-property :raw-value element))))
(org-make-link-string normalized-heading (org-link-display-format normalized-heading)))))
(org-make-link-string
(cond
((or (eq org-id-link-to-org-use-id t)
(and org-id-link-to-org-use-id
(org-element-property :ID element)))
(format "id:%s" (org-element-property :ID element)))
((string-equal (org-element-property :file element) (buffer-file-name))
normalized-heading)
(t
(format "file:%s::*%s"
(org-element-property :file element)
normalized-heading)))
(org-link-display-format normalized-heading)))))
(cons 'priority (lambda (element)
(--when-let (org-element-property :priority element)
(char-to-string it))))
Expand All @@ -311,9 +345,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 buffers-files
: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