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

Dynamic block columns don't respect property inheritance #476

Open
claudiusmueller opened this issue Nov 15, 2024 · 1 comment
Open

Dynamic block columns don't respect property inheritance #476

claudiusmueller opened this issue Nov 15, 2024 · 1 comment
Assignees
Labels
Milestone

Comments

@claudiusmueller
Copy link

OS/platform

openSUSE Leap 15.6

Emacs version and provenance

Emacs version 29.4
Installed via package manager

Emacs command

KRunner -> "emacs"

Org version and provenance

9.6.15 (built-in)

org-ql package version and provenance

20240222.200

Actions taken

Originally, the problem was noticed in a more complex setup where I am setting up meetings by pulling in a dynamic block with previous tasks per meeting attendee. Following a discussion with alphapapa on reddit, he came up with a simple reprex that I have expanded below to demonstrate the problem.

Observed results

In the following example the query is respecting property inheritance of the CATEGORY property, but the shown column is not. The category column should show "food" for both filtered items.

 # Local Variables:
 # org-use-property-inheritance: ("CATEGORY")
 # End:

#+begin: org-ql :query (property "CATEGORY" "food")  :columns (heading ((property "CATEGORY") "category"))
| Heading | category |
|---------+----------|
| Food    | food     |
| Bread   |          |
#+end

* TODO Food
:PROPERTIES:
:CATEGORY: food
:END:
** TODO Bread
** TODO Fork
:PROPERTIES:
:CATEGORY: utensils
:END:

Expected results

In the example posted above, the result should show "food" for both items in the category column.

#+begin: org-ql :query (property "CATEGORY" "food")  :columns (heading ((property "CATEGORY") "category"))
| Heading | category |
|---------+----------|
| Food    | food     |
| Bread   | food     |
#+end

Backtrace

No response

Etc.

No response

@alphapapa
Copy link
Owner

Hi Claudius,

Thanks for persisting with this bug report until I recognized the problem. For my own reference, this new definition of the function includes the fix:

(cl-defun org-dblock-write:org-ql (params)
  "Insert content for org-ql dynamic block at point according to PARAMS.
Valid parameters include:

  :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
this (must be a single line in the Org buffer):

  #+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)
          (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"))))
          ;; 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)
                                  (let ((normalized-heading
                                         (org-ql-search--link-heading-search-string (org-element-property :raw-value element))))
                                    (org-ql-search--org-make-link-string normalized-heading (org-link-display-format normalized-heading)))))
                 (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-with-point-at (org-element-property :begin element)
                                     (org-entry-get (point) property))))))
          (elements (org-ql-query :from (current-buffer)
                                  :where query
                                  :select '(org-ql-view--resolve-element-properties
                                            (org-element-headline-parser (line-end-position)))
                                  :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))))

I'll include it in the next version.

@alphapapa alphapapa self-assigned this Nov 16, 2024
@alphapapa alphapapa added the bug label Nov 16, 2024
@alphapapa alphapapa added this to the 0.9 milestone Nov 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants