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

Conversation

yantar92
Copy link
Contributor

@yantar92 yantar92 commented Aug 8, 2021

Implements #151

@alphapapa
Copy link
Owner

As I said in #151, this probably needs to wait for #228.

As well, this feature would need tests, including ones that verify that potentially dangerous sexps are not evaluated somewhere in the process of handling the arguments.

@re-f
Copy link

re-f commented Sep 6, 2021

this Implements has a problem: it generate wrong headline link

@yantar92
Copy link
Contributor Author

yantar92 commented Oct 2, 2021 via email

@yantar92
Copy link
Contributor Author

yantar92 commented Oct 4, 2021

this Implements has a problem: it generate wrong headline link

I see what you meant now. Should be fixed, though links may still be wrong for Org buffers without file. Not sure what can be done in the last scenario when the user also does not use org-id.

@telenieko
Copy link

telenieko commented May 17, 2022

Hi,
May I ask what is preventing this PR to be merged? ended here searching precisely about using the dynamic block across org-agenda-files

@alphapapa
Copy link
Owner

alphapapa commented May 18, 2022 via email

@telenieko
Copy link

I work on these projects in my spare time

I understand. I was mostly asking about feedback on the last comment in this PR to see if I could continue @yantar92 's work (or if my, currently very limited, Elisp is not yet enough for it).

@hrehfeld
Copy link

What's the status for this? Any easy way to help?

@hrehfeld
Copy link

hrehfeld commented Aug 12, 2022

For anyone wanting this feature:

(require 'org-ql-search)
(cl-defun org-dblock-write:my-org-ql (params)
    "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.

  :columns  A list of columns, including `heading', `todo',
            `property',`priority',`deadline',`scheduled',`closed'.
            Each column may also be specified as a list with the
            second element being a header string.  For example,
            to abbreviate the priority column: (priority \"P\").
            For certain columns, like `property', arguments may
            be passed by specifying the column type itself as a
            list.  For example, to display a column showing the
            values of a property named \"milestone\", with the
            header being abbreviated to \"M\":

              ((property \"milestone\") \"M\").

  :sort     One or a list of Org QL sorting methods
            (see `org-ql-select').

  :take     Optionally take a number of results from the front (a
            positive number) or the end (a negative number) of
            the results.

  :ts-format  Optional format string used to format
              timestamp-based columns.

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 :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)
                                    (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))))
                   (cons 'deadline (lambda (element)
                                     (--when-let (org-element-property :deadline element)
                                       (ts-format ts-format (ts-parse-org-element it)))))
                   (cons 'scheduled (lambda (element)
                                      (--when-let (org-element-property :scheduled element)
                                        (ts-format ts-format (ts-parse-org-element it)))))
                   (cons 'closed (lambda (element)
                                   (--when-let (org-element-property :closed element)
                                     (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 scope
                                    :where query
                                    :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
                         ((and integer (satisfies cl-minusp)) (-take-last (abs take) elements))
                         (integer (-take take elements)))))
      (cl-labels ((format-element
                   (element) (string-join (cl-loop for column in columns
                                                   collect (or (pcase-exhaustive column
                                                                 ((pred symbolp)
                                                                  (funcall (alist-get column format-fns) element))
                                                                 (`((,column . ,args) ,_header)
                                                                  (apply (alist-get column format-fns) element args))
                                                                 (`(,column ,_header)
                                                                  (funcall (alist-get column format-fns) element)))
                                                               ""))
                                          " | ")))
        ;; Table header
        (insert "| " (string-join (--map (pcase it
                                           ((pred symbolp) (capitalize (symbol-name it)))
                                           (`(,_ ,name) name))
                                         columns)
                                  " | ")
                " |" "\n")
        (insert "|- \n")  ; Separator hline
        (dolist (element elements)
          (insert "| " (format-element element) " |" "\n"))
        (delete-char -1)
        (org-table-align))))

seems to work for me (which is basically diff of this pr pasted into current code). use with my-org-ql instead of org-ql dynamic block.

For my personal version I also patched it to have heading truncate after 70 chars.

@bhuztez bhuztez mentioned this pull request Aug 26, 2022
@akirak
Copy link
Contributor

akirak commented Sep 20, 2022

It would be nice if the parameter accepted a function that returns a list of files. See scope parameter in org-dblock-write:clocktable.

@alphapapa
Copy link
Owner

alphapapa commented Oct 11, 2022 via email

@hrehfeld
Copy link

*patiently waiting for this feature* 👍 🏅 🙈

[I added (require 'org-ql-search) to my previous code block]

@alphapapa alphapapa removed this from the 0.8 milestone Dec 16, 2023
@nickanderson
Copy link

Thanks @hrehfeld that elisp in #239 (comment) works for me.

I made a small change so that it accepts a function name like clocktables.

                           ((string-equal scope "org-directory") (org-ql-search-directories-files))
+                           ((functionp scope) (funcall scope))  ; Call the function if scope is a function
                           (t (user-error "Unknown scope '%s'" scope))))

Anyway, I am stoked about finding this.

@alphapapa alphapapa added this to the 0.9 milestone Feb 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants