org-ql icon indicating copy to clipboard operation
org-ql copied to clipboard

Allow :file parameter in org-ql dynamic blocks

Open yantar92 opened this issue 4 years ago • 13 comments

Implements #151

yantar92 avatar Aug 08 '21 08:08 yantar92

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.

alphapapa avatar Aug 09 '21 06:08 alphapapa

this Implements has a problem: it generate wrong headline link

re-f avatar Sep 06 '21 10:09 re-f

this Implements has a problem: it generate wrong headline link

Can you elaborate?

yantar92 avatar Oct 02 '21 08:10 yantar92

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.

yantar92 avatar Oct 04 '21 07:10 yantar92

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

telenieko avatar May 17 '22 10:05 telenieko

I work on these projects in my spare time, which I don't have as much of as I used to. If you would like to sponsor work on a particular feature, let me know.

On Tue, May 17, 2022, 05:54 Marc Fargas @.***> wrote:

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

— Reply to this email directly, view it on GitHub https://github.com/alphapapa/org-ql/pull/239#issuecomment-1128722481, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAES2FJAOEOASRT52XOIB3LVKN3HPANCNFSM5BYIVKCA . You are receiving this because you commented.Message ID: @.***>

alphapapa avatar May 18 '22 10:05 alphapapa

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).

telenieko avatar May 18 '22 10:05 telenieko

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

hrehfeld avatar Aug 12 '22 11:08 hrehfeld

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.

hrehfeld avatar Aug 12 '22 12:08 hrehfeld

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

akirak avatar Sep 20 '22 19:09 akirak

I don't know. I'll have to review it when I have time.

On Wed, May 18, 2022, 05:17 Marc Fargas @.***> wrote:

I work on these projects in my spare time

I understand. I was mostly asking about feedback on the last comment https://github.com/alphapapa/org-ql/pull/239#issuecomment-933213204 in this PR to see if I could continue @yantar92 https://github.com/yantar92 's work (or if my, currently very limited, Elisp is not yet enough for it).

— Reply to this email directly, view it on GitHub https://github.com/alphapapa/org-ql/pull/239#issuecomment-1129831490, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAES2FKXBE64UYNXKQIKAFTVKS7UZANCNFSM5BYIVKCA . You are receiving this because you commented.Message ID: @.***>

alphapapa avatar Oct 11 '22 07:10 alphapapa

*patiently waiting for this feature* :+1: :medal_sports: :see_no_evil:

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

hrehfeld avatar Apr 24 '23 10:04 hrehfeld

Thanks @hrehfeld that elisp in https://github.com/alphapapa/org-ql/pull/239#issuecomment-1213052966 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.

nickanderson avatar Feb 09 '24 14:02 nickanderson