alphapapa / org-ql

A searching tool for Org-mode, including custom query languages, commands, saved searches and agenda-like views, etc.
GNU General Public License v3.0
1.35k stars 104 forks source link

Default date sort does not take into account the :closed property #397

Open TristanCacqueray opened 5 months ago

TristanCacqueray commented 5 months ago

OS/platform

Linux

Emacs version and provenance

GNU Emacs 30.0.50 built with nix

Emacs command

emacs

Org version and provenance

Org mode version 9.6.7 from nix

org-ql package version and provenance

0.7.1-pre from nix emacs-overlay

Actions taken

Custom format of a org-ql-select results

Observed results

The entries with a :closed date appears out of order

Expected results

The entries should be order similarly than in org-ql-search

Backtrace

Here is the problematic call:

(org-ql-select "~/org/projects.org"
                    '(and (done) (ts :from -3 :to today))
                    :action 'element-with-markers
                    :sort '(date reversed))

And here is a workaround:

(defun tc/compare-entry (b a)
  "Order entry A and B so that they appears from newest to oldest.
This is like org-ql--date< but considering closed date too."
  (cl-macrolet ((ts (item)
                  `(or (org-element-property :closed ,item)
                       (org-element-property :deadline ,item)
                       (org-element-property :scheduled ,item))))
    (org-ql--org-timestamp-element< (ts a) (ts b))))

(org-ql-select "~/org/projects.org"
                    '(and (done) (ts :from -3 :to today))
                    :action 'element-with-markers
                    :sort 'tc/compare-entry)

Etc.

Not sure if that is a bug of feature request.

alphapapa commented 5 months ago

Thanks for the request.

TristanCacqueray commented 5 months ago

You are welcome, thank you for org-ql, it works great! By the way, I wrote a little post about this use-case here: https://tristancacqueray.github.io/blog/no-frills-daily-project-management-with-org-mode

alphapapa commented 5 months ago

Thanks for sharing that. I'm always glad to learn about how people are using org-ql. BTW, you might find the dynamic blocks helpful for generating and updating reports like that without having to write much custom code: https://github.com/alphapapa/org-ql/#dynamic-block

TristanCacqueray commented 5 months ago

@alphapapa sure, here I have another use case for org-ql: https://github.com/TristanCacqueray/gnome-org-next-schedule/

alphapapa commented 5 months ago

@alphapapa sure, here I have another use case for org-ql: https://github.com/TristanCacqueray/gnome-org-next-schedule/

Very cool! FYI, I think you could simplify it slightly by writing the query like this:

`(and (not (done)) (not (habit)) (scheduled :from ,(ts-now))))

Note also the use of backquoting/unquoting and (ts-now), because now is not a supported value for the :from argument.