org-ql/examples.org
Adam Porter f4918b19dc Merge: wip/helm-query-rewriting
It's finally time to merge this branch, which adds non-sexp queries,
updates the readme, and a few other things.

Squashed commit of the following:

commit ee54e31dfa2c3335e4ef5498fc9714ddc093968b
Author: Adam Porter <adam@alphapapa.net>
Date:   Sun Oct 6 00:10:19 2019 -0500

    WIP: Depend-on/Require dash-functional

commit 42f6a47d86dfb7f8e96f3b1822e17a3eed825897
Author: Adam Porter <adam@alphapapa.net>
Date:   Sat Oct 5 23:45:06 2019 -0500

    WIP: Update docs

commit eb56e46ed8cd9ad6874dc955375be5efadb4fcf2
Author: Adam Porter <adam@alphapapa.net>
Date:   Sat Oct 5 22:04:24 2019 -0500

    WIP: Add demo script

commit cdd394f4865cd49791b2fbebb1913d9210aeaf35
Author: Adam Porter <adam@alphapapa.net>
Date:   Sat Oct 5 20:24:44 2019 -0500

    WIP: Update demo GIF

commit 7f470a7c3c7612b7718dc26044069dc2a6afdff3
Author: Adam Porter <adam@alphapapa.net>
Date:   Sat Oct 5 13:02:09 2019 -0500

    WIP: (property) Convert keyword properties to strings

    I'm a little uncomfortable with how query parsing and processing is
    getting spread out among 2 or 3 functions, but handling this in the
    plain-query parsing function would require uglier special-case
    workarounds, so this seems like the best solution for now.

commit 4e494a03f29a1d0068daa9cddf095dba15252133
Author: Adam Porter <adam@alphapapa.net>
Date:   Fri Oct 4 16:25:00 2019 -0500

    WIP: Handle query errors, move code, add tags-all, tags& aliases

    This prevents query compilation errors from displaying a warning
    buffer, especially in the Helm command.

    Close to done, but now need to figure out the "property:" predicate,
    because its value argument looks like an alternative property rather
    than a value, e.g. "property:agenda-group,plans" in which "plans" is
    the value for the property "agenda-group".

commit 9d8222d8fa9a584b1ee648c5aa38999200e7c54f
Author: Adam Porter <adam@alphapapa.net>
Date:   Thu Oct 3 22:18:29 2019 -0500

    WIP: Rewrite (priority) predicate to accept multiple priorities

    This commit also fixes a latent bug in the preamble for (priority), which
    wasn't being hit because the query (priority) was being rewritten by
    pre-process to (priority '>= C).

commit 4900470ba52b0fcf4786ab857e316403dec00481
Author: Adam Porter <adam@alphapapa.net>
Date:   Thu Oct 3 20:41:54 2019 -0500

    WIP: More documentation

commit 5041a901f6c1071a4233e5671a0926e65e29a5c0
Author: Adam Porter <adam@alphapapa.net>
Date:   Thu Oct 3 12:30:50 2019 -0500

    WIP: Mention query types for commands

commit bb2d18532cb972a54b5b9056b76b0582eb4cad03
Author: Adam Porter <adam@alphapapa.net>
Date:   Thu Oct 3 11:01:56 2019 -0500

    WIP: Remove old code

commit 8de048e531bfa9ba7058e7d540aae2d332f9be68
Author: Adam Porter <adam@alphapapa.net>
Date:   Thu Oct 3 11:01:03 2019 -0500

    WIP: TODO done

commit 6a38fc845aeb05fe3f03174c04a7011830873705
Author: Adam Porter <adam@alphapapa.net>
Date:   Thu Oct 3 10:59:45 2019 -0500

    WIP: Tidy docstrings

commit 04d0c254fc8694b1363c91a1691f3303fcf7755a
Author: Adam Porter <adam@alphapapa.net>
Date:   Thu Oct 3 10:58:21 2019 -0500

    WIP: Document plain queries

commit 23dcc9bad4a8e6660e48632a20a04adb2654e20d
Author: Adam Porter <adam@alphapapa.net>
Date:   Thu Oct 3 10:27:41 2019 -0500

    WIP: Move ts aliases into defpreds, improve docstrings and comments

commit 6ef710f25bbd430b56ce205b8f314b17cd9ac7c3
Author: Adam Porter <adam@alphapapa.net>
Date:   Thu Oct 3 10:11:17 2019 -0500

    Tidy: Whitespace

commit 1022c4b36fb03a6724d97ba4dd1b5bb68025ed7b
Author: Adam Porter <adam@alphapapa.net>
Date:   Thu Oct 3 10:01:53 2019 -0500

    WIP: (org-ql-search) Accept plain queries

    This way, helm-org-ql and org-ql-search accept queries in the same
    format.

commit 1903e8f5684269c272a2ff3984d4d262f41323c5
Author: Adam Porter <adam@alphapapa.net>
Date:   Thu Oct 3 09:22:35 2019 -0500

    WIP: Add more plain-query tests

commit 9d58e3395e871f3536296ccfbb3901f98416bd9a
Author: Adam Porter <adam@alphapapa.net>
Date:   Thu Oct 3 09:17:07 2019 -0500

    WIP: Return boolean from --plain-query, and only when needed

commit 3770e18e68eaf776aef6fbb94e6ac5648c869c12
Author: Adam Porter <adam@alphapapa.net>
Date:   Thu Oct 3 08:59:55 2019 -0500

    WIP: Parse numbers in key/value args

    e.g. "ts-a:on=-1" for yesterday, or "ts:on=0" or "ts:on=today" for
    today.

commit 0f2b71bb869980f413c7366d1b15fa35f3f6a1c5
Author: Adam Porter <adam@alphapapa.net>
Date:   Thu Oct 3 08:59:43 2019 -0500

    WIP: Rename plain-query function

commit be410a36f4a1c043de24a2d0d2fbbe22135c3385
Author: Adam Porter <adam@alphapapa.net>
Date:   Wed Oct 2 19:21:07 2019 -0500

    WIP: Reverse query properly

    PEG returns queries backwards for some reason, so reverse them back to
    the order they were input in.

commit c184091ac2e96a76696055509ca6245e9a020b03
Author: Adam Porter <adam@alphapapa.net>
Date:   Wed Oct 2 19:17:20 2019 -0500

    WIP: Fix previous commit

    Something weird happens when defining the function that's defined by
    the macro in the eval-when block: It seems that I have to eval it
    twice before it takes effect.  So when I made the previous commit, the
    function defined at the time was working properly, but the function
    defined at the time was apparently not what was written in the file,
    but some intermediate state that had been written before, so I don't
    even know what the code was that was working properly.

    After some more fiddling, this seems to work properly.

commit 4e7288d494c6cfe69f82f3aea4b88feced7232f7
Author: Adam Porter <adam@alphapapa.net>
Date:   Wed Oct 2 18:42:59 2019 -0500

    WIP: Handle keyword/value queries

commit f603d199f47c1115cf11f8661c19c73f056b414d
Author: Adam Porter <adam@alphapapa.net>
Date:   Tue Oct 1 16:45:33 2019 -0500

    WIP: Define pred aliases and input-query function with macros

commit 217ef7dff13e8464baaa50539c82843e48bc3fe8
Author: Adam Porter <adam@alphapapa.net>
Date:   Tue Oct 1 16:16:04 2019 -0500

    WIP: Tidy: Don't quote the lambda

    I don't even know why I did that.

commit 3130f6519d7c21e47a026404e3929f118b003092
Author: Adam Porter <adam@alphapapa.net>
Date:   Fri Sep 27 23:20:50 2019 -0500

    WIP: Parsing with peg

    Seems to be working pretty well.

commit 0762825c7ba8026768aec74b4fffd32b8530c42c
Author: Adam Porter <adam@alphapapa.net>
Date:   Tue Sep 10 16:17:50 2019 -0500

    WIP: Rewriting Helm queries

    Some people are unaccustomed to parentheses, so we'll try to
    accommodate them.
2019-10-06 00:17:14 -05:00

6.7 KiB

org-ql examples

Agenda-like view

Show an agenda-like view, similar to a "traditional" Org Agenda with Log Mode turned on.

  (org-ql-search (org-agenda-files)
    '(or (and (not (done))
              (or (habit)
                  (deadline auto)
                  (scheduled :to today)
                  (ts-active :on today)))
         (closed :on today))
    :sort '(date priority todo))

Another example, showing grouping with org-super-agenda:

  (org-ql-search "~/src/emacs/org-super-agenda/test/test.org"
    '(and (or (ts-active :on today)
              (deadline auto)
              (scheduled :to today))
          (not (done)))
    :title "My Agenda View"
    ;; The `org-super-agenda-groups' setting is used automatically when set, or it
    ;; may be overriden by specifying it here:
    :super-groups '((:name "Bills"
                           :tag "bills")
                    (:todo ("SOMEDAY" "TO-READ" "CHECK" "TO-WATCH" "WATCHING")
                           :order 7)
                    (:name "Personal"
                           :habit t
                           :tag "personal"
                           :order 3)
                    (:todo "WAITING"
                           :order 6)
                    (:priority "A" :order 1)
                    (:priority "B" :order 2)
                    (:priority "C" :order 2)))

Which displays this buffer:

/jiri/org-ql/media/tag/0.3.2/images/screenshot.png

Entries from the past week

Show entries that have any timestamp within the past week. Group by date using org-super-agenda with the :auto-ts group.

  (org-ql-search (org-agenda-files)
    '(ts :from -7 :to today)
    :title "Recent Items"
    :sort '(date priority todo)
    :groups '((:auto-ts t)))

Find entries matching a certain CUSTOM_ID

Since queries can contain both built-in org-ql predicate expressions and arbitrary expressions, they can be combined in useful ways. This example uses the built-in property predicate to quickly locate entries that have the CUSTOM_ID property set, and then compares the value of that property to the string issue.

  (org-ql-query
    :select #'org-get-heading
    :from "~/org/tickets.org"
    :where '(and (property "CUSTOM_ID")
                 (string-match "issue" (org-entry-get (point) "CUSTOM_ID"))))

Using the property predicate as the first clause of the two clauses joined with and allows org-ql to optimize the query by searching through the buffer directly to entries that set the CUSTOM_ID property, which is much faster than testing every entry in a buffer. Also, If the query were only the string-match call, it would signal an error on entries that didn't have the property set, because org-entry-get would return nil.

Listing bills coming due

This uses the example in the readme file, but maps across the elements returned by org-ql to present a simple list of titles and deadlines.

  (org-ql-query
    :select '(list (substring-no-properties (org-get-heading t t))
                   (org-entry-get (point) "DEADLINE"))
    :from (org-agenda-files)
    :where '(and (not (done))
                 (tags "bills")
                 (deadline auto))
    :order-by 'deadline)
  ;;=> (("Electric bill" "<2018-08-23 Thu +1m>")
  ;; ("Rent" "<2018-09-01 Sat +1m>"))

This could also be put in a script, which could use desktop notifications to remind of bills coming due: org-bills-due.el.

Music database

If you kept a database of music in an Org file, you could run a query like this to find tracks composed by Chopin that do not have their key recorded in the database.

  (org-ql-search "~/org/music.org"
    '(and (property "genre" "classical")
          (property "composer" "Chopin")
          (not (property "key"))))

Return Org elements

Return a list of Org entry elements in the file ~/org/main.org which have the SOMEDAY to-do keyword, are tagged Emacs, and have priority B or higher. org-ql is the macro version of org-ql-select; it does not require quoting the query sexp.

  (org-ql "~/org/main.org"
    (and (todo "SOMEDAY")
         (tags "Emacs")
         (priority >= "B")))
  ;;=> ((headline (:raw-value "org-board" :begin 1220270 :end 1220403 ...)) ...)

Set tags on certain entries

Set the tag Emacs on every entry in the inbox file that mentions Emacs. The bare-string query "Emacs" is equivalent to (regexp "Emacs").

  (org-ql-select "~/org/inbox.org"
    "Emacs"
    :action '(org-toggle-tag "Emacs" 'on))

Show entries with recent timestamps

You can also access these views with the command org-ql-view.

  ;; Show entries with any timestamp from last 7 days:
  (org-ql-view-recent-items 7)

  ;; Show entries clocked in last 30 days:
  (org-ql-view-recent-items 30 'clocked)

  ;; Show entries closed in last 30 days:
  (org-ql-view-recent-items 30 'closed)

Stuck projects block agenda

Reddit user emptymatrix shared this example of replacing a traditional org-stuck-projects view like:

  (setq org-stuck-projects
        '("+@project/-DONE" ("NEXT") nil "SCHEDULED:"))

With this org-ql-block agenda view, like:

  (setq org-agenda-custom-commands
        '(("s" "Stuck Projects"
           ((org-ql-block '(and (tags "@project")
                                (not (done))
                                (not (descendants (todo "NEXT")))
                                (not (descendants (scheduled))))
                          ((org-ql-block-header "Stuck Projects")))))))