
org-ql
is a lispy query language for Org files. It allows you to find Org entries matching certain criteria and return a list of them or perform actions on them. Commands are also provided which display matching results.
More examples are available in examples.org.
;; 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)))
;; Show a GTD-style "stuck projects" view: PROJECT tasks that have no
;; descendants with the NEXT keyword. If you use a "project" tag
;; instead of the to-do keyword, you could replace (todo "PROJECT")
;; with (tags "project").
(org-ql-search (org-agenda-files)
'(and (todo "PROJECT")
(not (descendants (todo "NEXT"))))
:title "Stuck Projects")
;; Integrate `org-ql' into a custom Org Agenda command which inserts
;; an `org-ql' block before the regular agenda:
(setq org-agenda-custom-commands
'(("ces" "Custom: Agenda and Emacs SOMEDAY [#A] items"
((org-ql-block '(and (todo "SOMEDAY")
(tags "Emacs")
(priority "A")))
(agenda)))))
;; Return a list of bills coming due, searching all Org Agenda files,
;; sorted by deadline. The `auto' argument to `deadline' means to match
;; entries whose deadlines fall within `org-deadline-warning-days'.
;; `org-ql-query' works like `org-ql-select' but offers arguments named
;; like SQL queries.
(org-ql-query
:select #'org-get-heading
:from (org-agenda-files)
:where '(and (not (done))
(tags "bills")
(deadline auto))
:order-by 'deadline)
;;=> ("TODO Electric bill" "TODO Water bill")
;; 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' works like
;; `org-ql-select' and displays results in an agenda-like buffer:
(org-ql-search "~/org/music.org"
'(and (property "genre" "classical")
(property "composer" "Chopin")
(not (property "key"))))
;; Set the tag "Emacs" on every entry in the inbox file that mentions
;; "Emacs". `org-ql-select' works like `org-ql' but is a function
;; rather than a macro. The bare-string query "Emacs" is equivalent
;; to (regexp "Emacs").
(org-ql-select "~/org/inbox.org"
"Emacs"
:action '(org-toggle-tag "Emacs" 'on))
;; 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 "~/org/main.org"
(and (todo "SOMEDAY")
(tags "Emacs")
(priority >= "B")))
;;=> ((headline (:raw-value "org-board" :begin 1220270 :end 1220403 ...)) ...)
The package may be installed directly from MELPA or with other tools like Quelpa.
After installation, you can use commands like org-ql-search
immediately.
To use the functions and macros in your own Elisp code, load the libraries org-ql
and/or org-ql-agenda
with e.g. (require 'org-ql)
.
Installing with Quelpa is easy:
- Install quelpa-use-package (which can be installed directly from MELPA).
- Add this form to your init file:
(use-package org-ql
:quelpa (org-ql :fetcher github :repo "alphapapa/org-ql"))
The functionality provided may be grouped by:
- Interactive commands:
org-ql-search
,org-ql-view
,org-ql-sparse-tree
,helm-org-ql
. - Non-interactive functions and macros:
org-ql
(macro)org-ql-select
(function)org-ql-query
(function)org-ql-agenda
(macro)org-ql-block
(agenda function)
Alternatively, they may be grouped by:
- Showing an agenda-like view:
org-ql-search
(command)org-ql-view
(command)org-ql-block
(agenda function)org-ql-agenda
(macro)
- Showing a tree in a buffer:
org-ql-sparse-tree
(command)
- Showing results with Helm:
helm-org-ql
(command)
- Returning a list of matches or acting on them:
org-ql
(macro)org-ql-select
(function)org-ql-query
(function)
Feedback on these APIs is welcome. Eventually, after being tested and polished, they will be considered stable.
Read QUERY
and search with org-ql
. Interactively, prompt for these variables:
BUFFERS-FILES
: A
list of buffers and/or files to search. Interactively, may also be:
buffer
: search the current bufferall
: search all Org buffersagenda
: search buffers returned by the functionorg-agenda-files
- An expression which evaluates to a list of files/buffers
- A space-separated list of file or buffer names
GROUPS
: An org-super-agenda
group set. See variable org-super-agenda-groups
.
NARROW
: When non-nil, don’t widen buffers before searching. Interactively, with prefix, leave narrowed.
SORT
: One or a list of org-ql
sorting functions, like date
or priority
.
Bindings: Keys bound in results buffer.
g
: Refresh results.C-x C-s
: Save query to variableorg-ql-views
(accessible with commandorg-ql-view
).
Here’s an example of using it to generate an agenda-like view for certain files in a directory tree:
This command displays matches with Helm. Note: Helm is not a package dependency, so this command only works if the package helm-org
is installed.
Note also that queries in this command are specially handled so that quotes around strings may be omitted for ease of typing.
- Press
C-x C-s
in the Helm session to save the results to anorg-ql-search
buffer.
Choose and display a view stored in org-ql-views
.
Show items in FILES
from last DAYS
days with timestamps of TYPE
. TYPE
may be ts
, ts-active
, ts-inactive
, clocked
, closed
, deadline
, planning
, or scheduled
. FILES
defaults to those returned by the function org-agenda-files
.
Show a sparse tree for QUERY
in BUFFER
and return number of results. The tree will show the lines where the query matches, and any other context defined in org-show-context-detail
, which see.
QUERY
is an org-ql
query sexp (quoted, since this is a function). BUFFER
defaults to the current buffer. When KEEP-PREVIOUS
is non-nil (interactively, with prefix), the outline is not reset to the overview state before finding matches, which allows stacking calls to this command. Runs org-occur-hook
after making the sparse tree.
A query is a lisp form which may contain arbitrary lisp forms, as well as certain built-in predicates. It is byte-compiled into a predicate function which is tested with point on each heading in an Org buffer; when it returns non-nil, the heading matches the query.
Notes:
- Bare strings like
"string"
are automatically converted to(regexp "string")
predicates. - Standard numeric comparator function symbols (
<
,<=
,>
,>=
,=
) need not be quoted when passed as an argument to these predicates. The resemblance to infix notation is coincidental. See examples in documentation.
Arguments are listed next to predicate names, where applicable.
category (&optional categories)
- Return non-nil if current heading is in one or more of
CATEGORIES
(a list of strings). children (&optional query)
- Return non-nil if current heading has direct child headings. If
QUERY
, test it against child headings. This selector may be nested, e.g. to match grandchild headings. descendants (&optional query)
- Return non-nil if current heading has descendant headings. If
QUERY
, test it against descendant headings. This selector may be nested (if you can grok the nesting!). done
- Return non-nil if entry’s
TODO
keyword is inorg-done-keywords
. habit
- Return non-nil if entry is a habit.
heading (&rest regexps)
- Return non-nil if current entry’s heading matches all
REGEXPS
(regexp strings). level (level-or-comparator &optional level)
- Return non-nil if current heading’s outline level matches arguments. The following forms are accepted:
(level NUMBER)
: Matches if heading level isNUMBER
.(level NUMBER NUMBER)
: Matches if heading level is equal to or between NUMBERs.(level COMPARATOR NUMBER)
: Matches if heading level compares toNUMBER
withCOMPARATOR
.COMPARATOR
may be<
,<=
,>
, or>=
. path (&rest regexps)
- Return non-nil if current heading’s buffer’s filename path matches any of
REGEXPS
(regexp strings). Without arguments, return non-nil if buffer is file-backed. priority (&optional comparator-or-priority priority)
- Return non-nil if current heading has a certain priority.
COMPARATOR-OR-PRIORITY
should be either a comparator function, like<=
, or a priority string, like “A” (in which case (=
will be the comparator). IfCOMPARATOR-OR-PRIORITY
is a comparator,PRIORITY
should be a priority string. If both arguments are nil, return non-nil if heading has any defined priority. property (property &optional value)
- Return non-nil if current entry has
PROPERTY
(a string), and optionallyVALUE
(a string). Note that property inheritance is currently not enabled for this predicate. If you need to test with inheritance, you could use a custom predicate form, like(org-entry-get (point) "PROPERTY" 'inherit)
. regexp (&rest regexps)
- Return non-nil if current entry matches all of
REGEXPS
(regexp strings). Matches against entire entry, from beginning of its heading to the next heading. tags (&optional tags)
- Return non-nil if current heading has one or more of
TAGS
(a list of strings). Tests both inherited and local tags. tags-inherited (&optional tags)
- Return non-nil if current heading’s inherited tags include one or more of
TAGS
(a list of strings). If TAGS is nil, return non-nil if heading has any inherited tags.- Aliases:
inherited-tags
,tags-i
,itags
.
- Aliases:
tags-local (&optional tags)
- Return non-nil if current heading’s local tags include one or more of
TAGS
(a list of strings). If TAGS is nil, return non-nil if heading has any local tags.- Aliases:
local-tags
,tags-l
,ltags
.
- Aliases:
tags-all (tags)
- Return non-nil if current heading includes all of
TAGS
. Tests both inherited and local tags.- Aliases:
tags&
.
- Aliases:
todo (&optional keywords)
- Return non-nil if current heading is a
TODO
item. WithKEYWORDS
, return non-nil if its keyword is one ofKEYWORDS
(a list of strings). When called without arguments, only matches non-done tasks (i.e. does not match keywords inorg-done-keywords
).
All of these predicates take optional keyword arguments :from
, :to:
, and :on
:
- If
:from
, return non-nil if entry has a timestamp on or after:from
. - If
:to
, return non-nil if entry has a timestamp on or before:to
. - If
:on
, return non-nil if entry has a timestamp on date:on
.
Argument values should be either a number of days (positive to look forward, or negative to look backward), a ts
struct, or a string parseable by parse-time-string
(the string may omit the time value).
Predicates:
ts
- Return non-nil if current entry has a timestamp in given period. If no arguments are specified, return non-nil if entry has any timestamp.
ts-active
,ts-a
- Like
ts
, but only matches active timestamps. ts-inactive
,ts-i
- Like
ts
, but only matches inactive timestamps.
The following predicates, in addition to the keyword arguments, can also take a single argument, a number, which looks backward or forward a number of days. The number can be negative to invert the direction.
Backward-looking:
clocked
- Return non-nil if current entry was clocked in given period. If no arguments are specified, return non-nil if entry was clocked at any time. Note: Clock entries are expected to be clocked out. Currently clocked entries (i.e. with unclosed timestamp ranges) are ignored.
closed
- Return non-nil if current entry was closed in given period. If no arguments are specified, return non-nil if entry was closed at any time.
Forward-looking:
deadline
- Return non-nil if current entry has deadline in given period. If argument is
auto
, return non-nil if entry has deadline withinorg-deadline-warning-days
. If no arguments are specified, return non-nil if entry has any deadline. planning
- Return non-nil if current entry has planning timestamp in given period (i.e. its deadline, scheduled, or closed timestamp). If no arguments are specified, return non-nil if entry is scheduled at any time.
scheduled
- Return non-nil if current entry is scheduled in given period. If no arguments are specified, return non-nil if entry is scheduled at any time.
For use as a custom agenda block type in org-agenda-custom-commands
. For example, you could define a custom series command like this, which would list all priority A items tagged Emacs
with to-do keyword SOMEDAY
, followed by the standard agenda view, in a single buffer:
(setq org-agenda-custom-commands
'(("ces" "Custom: Agenda and Emacs SOMEDAY [#A] items"
((org-ql-block '(and (todo "SOMEDAY")
(tags "Emacs")
(priority "A"))
((org-ql-block-header "SOMEDAY :Emacs: High-priority")))
(agenda)))))
Which would be equivalent to a tags-todo
search like this:
(setq org-agenda-custom-commands
'(("ces" "Custom: Agenda and Emacs SOMEDAY [#A] items"
((tags-todo "PRIORITY=\"A\"+Emacs/!SOMEDAY")
(agenda)))))
However, the org-ql-block
version runs in about 1/5th the time.
The variable org-ql-block-header
may be bound to a string to use as the block header, otherwise the header is formed automatically.
This macro is like org-ql
, but it presents matching entries in an Agenda-like view. It’s compatible with org-super-agenda, which provides grouping. For example:
(org-ql-agenda "~/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 presents this buffer:
Note: The view buffer is currently put in org-agenda-mode
, which means that some Org Agenda commands work, such as jumping to entries and changing item priorities (without necessarily updating the view). This feature is experimental and not guaranteed to work correctly with all commands. (It works to the extent it does because the appropriate text properties are placed on each item, imitating an Agenda buffer.)
Here are some other examples:
;; Show an agenda-like view of items in "~/org/main.org" with TODO and
;; SOMEDAY keywords which are tagged "computer" or "Emacs" and in the
;; category "main":
(org-ql-agenda "~/org/main.org"
(and (todo "TODO" "SOMEDAY")
(tags "computer" "Emacs")
(category "main")))
;; Show an agenda-like view of all habits in all agenda files:
(org-ql-agenda
(habit))
;; Show an agenda-like view similar to a "traditional" Org Agenda with
;; Log Mode turned on.
(org-ql-agenda
(or (and (not (done))
(or (habit)
(deadline auto)
(scheduled :to today)
(ts-active :on today)))
(closed :on today))
:sort (date priority todo))
Arguments: (buffers-or-files query &key action narrow sort)
Return items matching QUERY
in BUFFERS-OR-FILES
.
BUFFERS-OR-FILES
is a one or a list of files and/or buffers.
QUERY
is an org-ql
query sexp (quoted, since this is a function).
ACTION
is a function which is called on each matching entry with point at the beginning of its heading. It may be:
element
or nil: Equivalent toorg-element-headline-parser
.element-with-markers
: Equivalent to callingorg-element-headline-parser
, with markers added usingorg-ql--add-markers
. Suitable for formatting withorg-ql-agenda--format-element
, allowing insertion into an Org Agenda-like buffer.- A sexp, which will be byte-compiled into a lambda function.
- A function symbol.
If NARROW
is non-nil, buffers are not widened (the default is to widen and search the entire buffer).
SORT
is either nil, in which case items are not sorted; or one or a list of defined org-ql
sorting methods (date
, deadline
, scheduled
, todo
, priority
, or random
); or a user-defined comparator function that accepts two items as arguments and returns nil or non-nil.
Examples:
;; Return list of to-do headings in inbox file with tags and to-do keywords:
(org-ql-select "~/org/inbox.org"
'(todo)
:action #'org-get-heading)
;; => ("TODO Practice leaping tall buildings in a single bound :personal:" ...)
;; Without tags and to-do keywords:
(org-ql-select "~/org/inbox.org"
'(todo)
:action '(org-get-heading t t))
;; => ("Practice leaping tall buildings in a single bound" ...)
;; Return WAITING heading elements in agenda files:
(org-ql-select (org-agenda-files)
'(todo "WAITING")
:action 'element)
;; => ((headline (:raw-value "Visit the moon" ...) ...) ...)
;; Since `element' is the default for ACTION, it may be omitted:
(org-ql-select (org-agenda-files)
'(todo "WAITING"))
;; => ((headline (:raw-value "Visit the moon" ...) ...) ...)
Arguments: (&key (select 'element-with-markers) from where order-by narrow)
Like org-ql-select
, but arguments are named more like a SQL
query.
SELECT
corresponds to theorg-ql-select
argumentACTION
.FROM
corresponds to theorg-ql-select
argumentBUFFERS-OR-FILES
.WHERE
corresponds to theorg-ql-select
argumentQUERY
.ORDER-BY
corresponds to theorg-ql-select
argumentSORT
, which see.NARROW
corresponds to theorg-ql-select
argumentNARROW
.
Examples:
;; Return list of to-do headings in inbox file with tags and to-do keywords:
(org-ql-query
:select #'org-get-heading
:from "~/org/inbox.org"
:where '(todo))
;; => ("TODO Practice leaping tall buildings in a single bound :personal:" ...)
;; Without tags and to-do keywords:
(org-ql-query
:select '(org-get-heading t t)
:from "~/org/inbox.org"
:where '(todo))
;; => ("Practice leaping tall buildings in a single bound" ...)
;; Return WAITING heading elements in agenda files:
(org-ql-query
:select 'element
:from (org-agenda-files)
:where '(todo "WAITING"))
;; => ((headline (:raw-value "Visit the moon" ...) ...) ...)
;; Since `element' is the default for SELECT, it may be omitted:
(org-ql-query
:from (org-agenda-files)
:where '(todo "WAITING"))
;; => ((headline (:raw-value "Visit the moon" ...) ...) ...)
Arguments: (buffers-or-files query &key sort narrow markers action)
Expands into a call to org-ql-select
with the same arguments. For convenience, arguments should be unquoted.
Note: Breaking changes may be made before version 1.0, but in the event of major changes, attempts at backward compatibility will be made with obsolescence declarations, translation of arguments, etc. Users who need stability guarantees before 1.0 may choose to use tagged stable releases.
Added
- Command
helm-org-ql
. - Command
org-ql-sparse-tree
, likeorg-sparse-tree
fororg-ql
queries. (Thanks to Akira Komamura.) - Per-buffer, per-heading tag caching, which increases the speed of tags-related queries by 6-7x.
- More tags-related predicates and aliases:
- For inherited tags:
tags-inherited
,inherited-tags
,tags-i
,itags
. - For heading-local tags:
tags-local
,local-tags
,tags-l
,ltags
. tags-all
,tags&
: Matches all given tags using booleanAND
(rather than booleanOR
, which thetags
predicate uses).
- For inherited tags:
- Variable
org-ql-block-header
, which overrides the default header inorg-ql-block
agenda blocks. - Predicate
(path)
.
Changed
- Predicate
heading
now accepts multiple regexps, which are matched with booleanAND
. - Predicate
regexp
now matches its regexp arguments with booleanAND
.
Fixed
- Predicate
heading
now matches only against heading text, i.e. not including tags at the end of the line, to-do keyword, etc. - Predicate
todo
now matches case-sensitively, avoiding non-todo-keyword matches (e.g. a heading which beginsWaiting on
will no longer match for a todo keywordWAITING
).
Fixed
(descendants)
selector matched against parent heading instead of only descendants.
Added
- Function
org-ql-query
, likeorg-ql-select
but with arguments named more like a SQL query. - Bare strings like
"string"
can be used in queries, which are converted to(regexp "string")
automatically. - Selector
(regexp)
accepts multiple regexps to test. - Macro
org-ql
and functionsorg-ql-query
andorg-ql-select
now also accept a comparator function in their:sort
argument. - Function
org-ql-block
, which works as an Org Agenda series/composite/block command, usable in custom agenda commands defined in variableorg-agenda-custom-commands
. (Inspired by Benson Chu’s config.) - Function
org-ql-agenda--agenda
optionally takes a list of entries as an argument. - Selectors
ts-a
andts-i
, aliases forts-active
andts-inactive
. - Selector
ts
now accepts a:type
argument. - Face
org-ql-agenda-due-date
. - Selectors
(children)
and(descendants)
. - Function
org-ql-search
and macroorg-ql-agenda
accept a:title
argument, which is displayed in the header. - Command
org-ql-search
offers globalorg-super-agenda-groups
in completion. - Customization group
org-ql
. - Command
org-ql-view
, which displays views saved to variableorg-ql-views
, which can be saved fromorg-ql-search
buffers with commandorg-ql-search-save
, which is bound toC-x C-s
in view buffers. - Variable
org-ql-view-map
, active in view buffers displayed byorg-ql-search
,org-ql-agenda
, andorg-ql-view
. random
sort method.- Save position when refreshing search buffers.
Changed
- Function
org-ql-query
renamed toorg-ql-select
.org-ql-query
now refers to a new function. - Macro
org-ql
no longer accepts a:markers
argument. Instead, use argument:action element-with-markers
. See functionorg-ql-select
, whichorg-ql
calls. - Selector
(todo)
no longer matches “done” keywords when used without arguments (i.e. the ones in variableorg-done-keywords
). - Overhauled date/time-based predicates. See documentation for new argument signatures.
Removed
- Selector
(date)
, replaced by(ts)
.
Fixed
- Handle date ranges in date-based selectors. (Thanks to Cody Goodman, Samuel W. Flint, and Vikas Rawal.)
- Don’t overwrite bindings in
org-agenda-mode-map
. - Don’t search buffers without headings, and show a message if the user attempts it.
- Don’t search hidden/special buffers.
- Properly accept arbitrary sort functions in
org-ql-select
, etc. (Fixes #37. Thanks to Milan Zamazal.) - Planning-line-related predicates searched too far into entries.
- Add autoloads. (Fixes #36. Thanks to Akira Komamura.)
Compatibility
- Fixes for compatibility with Org 9.2. (Thanks to Ataias Pereira Reis and Daniel Kraus.)
Internal
- Optimizations for some query selectors, e.g.
regexp
andtodo
. These can provide a significant improvement for some queries. See benchmarks in notes.org. - Library ts is now used for parsing and comparing timestamps.
First tagged release.
Of course, queries like these can already be written with Org Agenda searches, but the syntax can be complex. For example, this query would be difficult to write in a standard Org Agenda search, because it matches against a to-do keyword and a plain-text search. As described in the advanced searching tutorial, it would require using org-search-view
with a query with specific regular expression syntax, like this:
+lisp +{^\*+\s-+TO-READ\s-}
But with org-ql-agenda
, you would write:
(org-ql-agenda
(and (regexp "lisp")
(todo "TO-READ")))
This package is used by org-sidebar, which presents a customizable agenda-like view in a sidebar window.
GPLv3