diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index bbedb2e..87bc281 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -44,6 +44,8 @@ jobs: - 26.3 - 27.1 - 27.2 + - 28.1 + - 28.2 - snapshot steps: - uses: purcell/setup-emacs@master diff --git a/Makefile b/Makefile index 9ba1358..64c4516 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # * makem.sh/Makefile --- Script to aid building and testing Emacs Lisp packages # URL: https://github.com/alphapapa/makem.sh -# Version: 0.3 +# Version: 0.5 # * Arguments @@ -38,7 +38,9 @@ endif verbose = $(v) -ifneq (,$(findstring vv,$(verbose))) +ifneq (,$(findstring vvv,$(verbose))) + VERBOSE = "-vvv" +else ifneq (,$(findstring vv,$(verbose))) VERBOSE = "-vv" else ifneq (,$(findstring v,$(verbose))) VERBOSE = "-v" diff --git a/README.org b/README.org index 1a20b83..2817dcc 100644 --- a/README.org +++ b/README.org @@ -94,15 +94,32 @@ Lisp code examples are in [[examples.org]]. :TOC: ignore-children :END: ++ *Jumping to an entry:* + - [[#org-ql-find][org-ql-find]] and related commands + - [[#helm-org-ql][helm-org-ql]] + *Showing an agenda-like view:* - - [[#org-ql-search][org-ql-search]] (command) - - [[#org-ql-view][org-ql-view]] (command) - - [[#org-ql-view-sidebar][org-ql-view-sidebar]] (command) - - [[#org-ql-view-recent-items][org-ql-view-recent-items]] (command) + - [[#org-ql-search][org-ql-search]] + - [[#org-ql-view][org-ql-view]] + - [[#org-ql-view-sidebar][org-ql-view-sidebar]] + - [[#org-ql-view-recent-items][org-ql-view-recent-items]] + *Showing a tree in a buffer:* - - [[#org-ql-sparse-tree][org-ql-sparse-tree]] (command) -+ *Showing results with Helm*: - - [[#helm-org-ql][helm-org-ql]] (command) + - [[#org-ql-sparse-tree][org-ql-sparse-tree]] + +*** org-ql-find + +/Note: These commands use [[#non-sexp-query-syntax][non-sexp queries]]./ + +These commands jump to a heading selected using Emacs's built-in completion facilities with an Org QL query: + +- ~org-ql-find~ searches in the current buffer. +- ~org-ql-find-in-agenda~ searches in ~(org-agenda-files)~. +- ~org-ql-find-in-org-directory~ searches in ~org-directory~. + +[[images/org-ql-find.png]] + +*** org-ql-refile + +This command refiles the current Org entry to one selected by searching with Org QL completion. It searches files listed in ~org-refile-targets~ as well as the current buffer. *** org-ql-search @@ -220,10 +237,13 @@ Arguments are listed next to predicate names, where applicable. - Aliases: ~olps~. + =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 (&rest args)= :: Return non-nil if current heading has a certain priority. ~ARGS~ may be either a list of one or more priority letters as strings, or a comparator function symbol followed by a priority letter string. For example: ~(priority "A") (priority "A" "B") (priority '>= "B")~ Note that items without a priority cookie never match this predicate (while Org itself considers items without a cookie to have the default priority, which, by default, is equal to priority ~B~). -+ =property (property &optional value)= :: Return non-nil if current entry has ~PROPERTY~ (a string), and optionally ~VALUE~ (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)~. ++ =property (property &optional value &key inherit)= :: Return non-nil if current entry has ~PROPERTY~ (a string), and optionally ~VALUE~ (a string). If ~INHERIT~ is nil, only match entries with ~PROPERTY~ set on the entry; if t, also match entries with inheritance. If ~INHERIT~ is not specified, use the Boolean value of ~org-use-property-inheritance~, which see (i.e. it is only interpreted as nil or non-nil). + =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. - Aliases: =r=. -+ ~src (&key lang regexps)~ :: Return non-nil if current entry contains an Org Babel source block. If ~LANG~ is non-nil, match blocks of that language. If ~REGEXPS~ is non-nil, require that block's contents match all regexps. ++ =rifle (&rest strings)= :: Return non-nil if each string is found in either the entry or its outline path. Works like =org-rifle=. This is probably the most useful, intuitive, general-purpose predicate. + - Aliases: ~smart~. + - *Note:* By default, this is the default predicate used for plain-string query tokens (i.e. given without a specified predicate). This can be customized with the option ~org-ql-default-predicate~. ++ ~src (&key lang regexps)~ :: Return non-nil if current entry contains an Org Babel source block. If ~LANG~ is non-nil, match blocks of that language. If ~REGEXPS~ is non-nil, require that block's contents match all regexps. Matching is done case-insensitively. + =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~. @@ -522,6 +542,49 @@ Simple links may also be written manually in either sexp or non-sexp form, like: /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. +** 0.7-pre + +*Added* ++ Commands ~org-ql-find~, ~org-ql-find-heading~, and ~org-ql-find-path~, which jump to entries selected using Emacs's built-in completion facilities and Org QL queries (like ~helm-org-ql~, but doesn't require Helm.). ++ Command ~org-ql-refile~, which refiles the entry at point to one selected using Org QL completion. ++ Predicate ~rifle~, which matches an entry if each of the given arguments is found in either the entry's contents or its outline path. This provides very intuitive results, mimicing the behavior of [[https://github.com/alphapapa/org-rifle][=org-rifle=]]. In fact, the results are so useful that it's now the default predicate for plain-string query tokens. (It is also aliased to ~smart~, since it's so "smart," and not all users have used =org-rifle=.) ++ Option ~org-ql-default-predicate~, applied to plain-string query tokens (before, the ~regexp~ predicate was always used, but now it may be customized). ++ Alias ~c~ for predicate ~category~. ++ Predicate ~property~ now accepts the argument ~:inherit~ to match entries with property inheritance, and when unspecified, the option ~org-use-property-inheritance~ controls whether inheritance is used. + +*Changed* ++ Give more useful error message for invalid queries. ++ Predicate ~src~ now matches case-insensitively. + +*Fixed* ++ Predicate ~src~'s matching of begin/end block lines, normalization of arguments, and handling in non-sexp queries. + +*Internal* ++ Certain query predicates, when called multiple times in an ~and~ sub-expression, are optimized to a single call. ++ Use ~buffer-chars-modified-tick~ instead of ~buffer-modified-tick~. (Thanks to [[https://github.com/yantar92][Ihor Radchenko]].) + +*Credits* ++ Thanks to [[https://github.com/chasecaleb][Caleb Chase]] for help with [[https://github.com/alphapapa/org-ql/pull/285][#285]], fixed in [[https://github.com/alphapapa/org-ql/commit/91908186fcca4b5fd2e9d26da5bc0375c2b41acf][9190818]]. + +** 0.6.3 + +*Fixed* ++ Non-sexp query parsing with updated version 1.0.1 of the ~peg~ package. (Fixes [[https://github.com/alphapapa/org-ql/issues/314][#314]], [[https://github.com/alphapapa/org-ql/issues/316][#316]]. Thanks to [[https://github.com/akirak][Akira Komamura]] and [[https://github.com/joonro][Joon Ro]] for reporting.) ++ Require library ~org-duration~ (apparently necessary in newer Org versions). + +** 0.6.2 + +*Fixed* ++ ~link~ predicate when used in an ~or~'ed query. ([[https://github.com/alphapapa/org-ql/issues/279][#279]]. Thanks to [[https://github.com/telenieko][Marc Fargas]] for reporting.) + +** 0.6.1 + +*Fixed* ++ In dynamic blocks, links to headings with statistics cookies were broken. (Fixes [[https://github.com/alphapapa/org-ql/issues/248][#248]]. Thanks to [[https://github.com/maikol-solis][Maikol Solis]] and [[https://github.com/yantar92][Ihor Radchenko]].) + +*Updated* ++ Compatibility with new macro names in Transient. ([[https://github.com/alphapapa/org-ql/pull/269][#269]]. Thanks to [[https://github.com/tarsius][Jonas Bernoulli]].) + ** 0.6 *Added* diff --git a/helm-org-ql.el b/helm-org-ql.el index feac1c3..38b9722 100644 --- a/helm-org-ql.el +++ b/helm-org-ql.el @@ -2,7 +2,7 @@ ;; Author: Adam Porter ;; URL: https://github.com/alphapapa/org-ql -;; Version: 0.6 +;; Version: 0.6.1 ;; Package-Requires: ((emacs "26.1") (dash "2.18.1") (s "1.12.0") (helm-org "1.0") (org-ql "0.6-pre")) ;;; Commentary: diff --git a/images/org-ql-find.png b/images/org-ql-find.png new file mode 100644 index 0000000..34359d0 Binary files /dev/null and b/images/org-ql-find.png differ diff --git a/makem.sh b/makem.sh index b9fc062..82a2db0 100755 --- a/makem.sh +++ b/makem.sh @@ -3,11 +3,11 @@ # * makem.sh --- Script to aid building and testing Emacs Lisp packages # URL: https://github.com/alphapapa/makem.sh -# Version: 0.3 +# Version: 0.6-pre # * Commentary: -# makem.sh is a script helps to build, lint, and test Emacs Lisp +# makem.sh is a script that helps to build, lint, and test Emacs Lisp # packages. It aims to make linting and testing as simple as possible # without requiring per-package configuration. @@ -79,7 +79,7 @@ Rules: Options: -d, --debug Print debug info. -h, --help I need somebody! - -v, --verbose Increase verbosity, up to -vv. + -v, --verbose Increase verbosity, up to -vvv. --no-color Disable color output. --debug-load-path Print load-path from inside Emacs. @@ -136,6 +136,27 @@ EOF echo $file } +function elisp-elint-file { + local file=$(mktemp) + cat >$file <"$file" </dev/null) if [[ $file_pkg ]] @@ -496,6 +570,8 @@ function sandbox { args_sandbox=( --title "makem.sh: $(basename $(pwd)) (sandbox: $sandbox_dir)" --eval "(setq user-emacs-directory (file-truename \"$sandbox_dir\"))" + --load package + --eval "(setq package-user-dir (expand-file-name \"elpa\" user-emacs-directory))" --eval "(setq user-init-file (file-truename \"$init_file\"))" ) @@ -658,7 +734,8 @@ function verbose { if [[ $verbose -ge $1 ]] then [[ $1 -eq 1 ]] && local color_name=blue - [[ $1 -ge 2 ]] && local color_name=cyan + [[ $1 -eq 2 ]] && local color_name=cyan + [[ $1 -ge 3 ]] && local color_name=white shift log_color $color_name "$@" >&2 @@ -706,9 +783,7 @@ function compile-batch { verbose 2 "Batch-compiling files..." debug "Byte-compile files: ${files_project_byte_compile[@]}" - batch-byte-compile "${files_project_byte_compile[@]}" \ - && success "Compiling finished without errors." \ - || error "Compilation failed." + batch-byte-compile "${files_project_byte_compile[@]}" } function compile-each { @@ -726,9 +801,7 @@ function compile-each { || compile_errors=t done - ! [[ $compile_errors ]] \ - && success "Compiling finished without errors." \ - || error "Compilation failed." + [[ ! $compile_errors ]] } function compile { @@ -738,6 +811,18 @@ function compile { else compile-each "$@" fi + local status=$? + + if [[ $compile_error_on_warn ]] + then + # Linting: just return status code, because lint rule will print messages. + [[ $status = 0 ]] + else + # Not linting: print messages here. + [[ $status = 0 ]] \ + && success "Compiling finished without errors." \ + || error "Compiling failed." + fi } function batch { @@ -752,12 +837,15 @@ function batch { function interactive { # Run Emacs interactively. Most useful with --sandbox and --install-deps. + local load_file_args=$(args-load-files "${files_project_feature[@]}" "${files_project_test[@]}") verbose 1 "Running Emacs interactively..." - verbose 2 "Loading files:" "${files_project_feature[@]}" "${files_project_test[@]}" + verbose 2 "Loading files: ${load_file_args//--load /}" + + [[ $compile ]] && compile unset arg_batch run_emacs \ - $(args-load-files "${files_project_feature[@]}" "${files_project_test[@]}") \ + $load_file_args \ --eval "(load user-init-file)" \ "${args_batch_interactive[@]}" arg_batch="--batch" @@ -769,6 +857,9 @@ function lint { lint-checkdoc lint-compile lint-declare + # NOTE: Elint doesn't seem very useful at the moment. See comment + # in lint-elint function. + # lint-elint lint-indent lint-package lint-regexps @@ -825,6 +916,28 @@ function lint-elsa { || error "Linting with Elsa failed." } +function lint-elint { + # NOTE: Elint gives a lot of spurious warnings, apparently because it doesn't load files + # that are `require'd, so its output isn't very useful. But in case it's improved in + # the future, and since this wrapper code already works, we might as well leave it in. + verbose 1 "Linting with Elint..." + + local errors=0 + for file in "${files_project_feature[@]}" + do + verbose 2 "Linting with Elint: $file..." + run_emacs \ + --load "$(elisp-elint-file)" \ + --eval "(makem-elint-file \"$file\")" \ + && verbose 3 "Linting with Elint found no errors." \ + || { error "Linting with Elint failed: $file"; ((errors++)) ; } + done + + [[ $errors = 0 ]] \ + && success "Linting with Elint finished without errors." \ + || error "Linting with Elint failed." +} + function lint-indent { verbose 1 "Linting indentation..." @@ -896,7 +1009,8 @@ function test-buttercup { run_emacs \ $(args-load-files "${files_project_test[@]}") \ - -f buttercup-run \ + --load "$buttercup_file" \ + --eval "(progn (setq backtrace-on-error-noninteractive nil) (buttercup-run))" \ && success "Buttercup tests finished without errors." \ || error "Buttercup tests failed." } diff --git a/org-ql-completing-read.el b/org-ql-completing-read.el new file mode 100644 index 0000000..81d8557 --- /dev/null +++ b/org-ql-completing-read.el @@ -0,0 +1,227 @@ +;;; org-ql-completing-read.el --- Completing read of Org entries using org-ql -*- lexical-binding: t; -*- + +;; Copyright (C) 2022 Adam Porter + +;; Author: Adam Porter + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +;;; Commentary: + +;; This library provides completing-read of Org entries using `org-ql' +;; search. + +;;; Code: + +(require 'org-ql) + +;;;; Customization + +(defgroup org-ql-completing-read nil + "Completing-read of Org entries using `org-ql' search." + :group 'org-ql) + +(defcustom org-ql-completing-read-reverse-paths t + "Whether to reverse Org outline paths in `org-ql-completing-read' results." + :type 'boolean) + +(defcustom org-ql-completing-read-snippet-function #'org-ql-completing-read--snippet-simple + ;; TODO: I'd like to make the -regexp one the default, but with + ;; default Emacs completion affixation, it can sometimes be a bit + ;; slow, and I don't want that to be a user's first impression. It + ;; may be possible to further optimize the -regexp one so that it + ;; can be used by default. In the meantime, the -simple one seems + ;; fast enough for general use. + "Function used to annotate results in `org-ql-completing-read'. +Function is called at entry beginning. (When set to +`org-ql-completing-read--snippet-regexp', it is called with a +regexp matching plain query tokens.)" + :type '(choice (function-item :tag "Show context around search terms" org-ql-completing-read--snippet-regexp) + (function-item :tag "Show first N characters" org-ql-completing-read--snippet-simple) + (function :tag "Custom function"))) + +(defcustom org-ql-completing-read-snippet-length 51 + "Size of snippets of entry content to include in completion annotations. +Only used when `org-ql-completing-read-snippet-function' is set +to `org-ql-completing-read--snippet-regexp'." + :type 'integer) + +(defcustom org-ql-completing-read-snippet-minimum-token-length 3 + "Query tokens shorter than this many characters are ignored. +That is, they are not included when gathering entry snippets. +This avoids too-small tokens causing performance problems." + :type 'integer) + +(defcustom org-ql-completing-read-snippet-prefix nil + "String prepended to snippets. +For an experience like `org-rifle', use a newline." + :type '(choice (const :tag "None (shown on same line)" nil) + (const :tag "New line (shown under heading)" "\n") + string)) + +(defface org-ql-completing-read-snippet '((t (:inherit font-lock-comment-face))) + "Snippets.") + +;;;; Functions + +;;;;; Completing read + +;;;###autoload +(cl-defun org-ql-completing-read (buffers-files &key query-prefix query-filter + (prompt "Find entry: ")) + "Return marker at Org entry in BUFFERS-FILES selected with `org-ql'. + +QUERY-PREFIX may be a string to prepend to the query entered by +the user (e.g. use \"heading:\" to only search headings, easily +creating a custom command that saves the user from having to type +it). + +QUERY-FILTER may be a function through which the query the user +types is filtered before execution (e.g. it could replace spaces +with commas to turn multiple tokens, which would normally be +treated as multiple predicates, into multiple arguments to a +single predicate)." + (declare (indent defun)) + ;; Emacs's completion API is not always easy to understand, + ;; especially when using "programmed completion." This code was + ;; made possible by the example Clemens Radermacher shared at + ;; . + (let ((table (make-hash-table :test #'equal)) + (window-width (window-width)) + query-tokens snippet-regexp) + (cl-labels ((action + () (font-lock-ensure (point-at-bol) (point-at-eol)) + (let* ((path (thread-first (org-get-outline-path t t) + (org-format-outline-path window-width nil "") + (org-split-string ""))) + (path (if org-ql-completing-read-reverse-paths + (string-join (nreverse path) "\\") + (string-join path "/")))) + (puthash path (point-marker) table) + path)) + (affix (completions) + (cl-loop for completion in completions + for marker = (gethash completion table) + for todo-state = (if-let (it (org-entry-get marker "TODO")) + (concat (propertize it + 'face (org-get-todo-face it)) + " ") + "") + for snippet = (if-let (it (snippet marker)) + (propertize (concat " " it) + 'face 'org-ql-completing-read-snippet) + "") + collect (list completion todo-state snippet))) + (annotate (candidate) + (while-no-input + ;; Using `while-no-input' here doesn't make it as + ;; responsive as, e.g. Helm while typing, but it seems to + ;; help a little when using the org-rifle-style snippets. + (or (snippet (gethash candidate table)) ""))) + (snippet (marker) + (org-with-point-at marker + (or (funcall org-ql-completing-read-snippet-function snippet-regexp) + (org-ql-completing-read--snippet-simple)))) + (group (candidate transform) + (pcase transform + (`nil (buffer-name (marker-buffer (gethash candidate table)))) + (_ candidate))) + (try (string _table _pred point &optional _metadata) + (cons string point)) + (all (string table pred _point) + (all-completions string table pred)) + (collection (str _pred flag) + (when query-prefix + (setf str (concat query-prefix str))) + (pcase flag + ('metadata (list 'metadata + (cons 'group-function #'group) + (cons 'affixation-function #'affix) + (cons 'annotation-function #'annotate))) + (`t (unless (string-empty-p str) + (when query-filter + (setf str (funcall query-filter str))) + (pcase org-ql-completing-read-snippet-function + ('org-ql-completing-read--snippet-regexp + (setf query-tokens + ;; Remove any tokens that specify predicates or are too short. + (--select (not (or (string-match-p (rx bos (1+ (not (any ":"))) ":") it) + (< (length it) org-ql-completing-read-snippet-minimum-token-length))) + (split-string str nil t (rx space))) + snippet-regexp + (when query-tokens + ;; Limiting each context word to 15 characters + ;; prevents excessively long, non-word strings + ;; from ending up in snippets, which can + ;; adversely affect performance. + (rx-to-string `(seq (optional (repeat 1 3 (repeat 1 15 (not space)) (0+ space))) + bow (or ,@query-tokens) (0+ (not space)) + (optional (repeat 1 3 (0+ space) (repeat 1 15 (not space)))))))))) + (org-ql-select buffers-files (org-ql--query-string-to-sexp str) + :action #'action)))))) + ;; NOTE: It seems that the `completing-read' machinery can call, + ;; abort, and re-call the collection function while the user is + ;; typing, which can interrupt the machinery Org uses to prepare + ;; an Org buffer when an Org file is loaded. This results in, + ;; e.g. the buffer being left in fundamental-mode, unprepared to + ;; be used as an Org buffer, which breaks many things and is + ;; very confusing for the user. Ideally, of course, we would + ;; solve this in `org-ql-select', and we already attempt to, but + ;; that function is called by the `completing-read' machinery, + ;; which interrupts it, so we must work around this problem by + ;; ensuring all of the BUFFERS-FILES are loaded and initialized + ;; before calling `completing-read'. + (unless (listp buffers-files) + ;; Since we map across this argument, we ensure it's a list. + (setf buffers-files (list buffers-files))) + (mapc #'org-ql--ensure-buffer buffers-files) + (let* ((completion-styles '(org-ql-completing-read)) + (completion-styles-alist (list (list 'org-ql-completing-read #'try #'all "Org QL Find"))) + (selected (completing-read prompt #'collection nil))) + (gethash selected table))))) + +(defun org-ql-completing-read--snippet-simple (&optional _regexp) + "Return a snippet of the current entry. +Returns up to `org-ql-completing-read-snippet-length' characters." + (save-excursion + (org-end-of-meta-data t) + (unless (org-at-heading-p) + (let ((end (min (+ (point) org-ql-completing-read-snippet-length) + (org-entry-end-position)))) + (concat org-ql-completing-read-snippet-prefix + (truncate-string-to-width + (replace-regexp-in-string "\n" " " (buffer-substring (point) end) + t t) + 50 nil nil t)))))) + +(defun org-ql-completing-read--snippet-regexp (regexp) + "Return a snippet of the current entry's matches for REGEXP." + ;; REGEXP may be nil if there are no qualifying tokens in the query. + (when regexp + (save-excursion + (org-end-of-meta-data t) + (unless (org-at-heading-p) + (let* ((end (org-entry-end-position)) + (snippets (cl-loop while (re-search-forward regexp end t) + concat (match-string 0) concat "…" + do (goto-char (match-end 0))))) + (unless (string-empty-p snippets) + (concat org-ql-completing-read-snippet-prefix + (replace-regexp-in-string (rx (1+ "\n")) " " snippets t t)))))))) + +;;;; Footer + +(provide 'org-ql-completing-read) + +;;; org-ql-completing-read.el ends here diff --git a/org-ql-find.el b/org-ql-find.el new file mode 100644 index 0000000..d9ccad9 --- /dev/null +++ b/org-ql-find.el @@ -0,0 +1,135 @@ +;;; org-ql-find.el --- Find headings with completion using org-ql -*- lexical-binding: t; -*- + +;; Copyright (C) 2022 Adam Porter + +;; Author: Adam Porter + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +;;; Commentary: + +;; This library provides a way to quickly find and go to Org entries +;; selected with Emacs's built-in completions API (so it works with +;; packages that extend it, like Vertico, Marginalia, etc). It works +;; like `helm-org-ql' but does not require Helm. + +;;; Code: + +(require 'cl-lib) + +(require 'org) +(require 'org-ql) +(require 'org-ql-search) +(require 'org-ql-completing-read) + +;;;; Customization + +(defgroup org-ql-find nil + "Options for `org-ql-find'." + :group 'org-ql) + +(defcustom org-ql-find-goto-hook '(org-show-entry org-reveal) + "Functions called when selecting an entry." + :type 'hook) + +(defcustom org-ql-find-display-buffer-action '(display-buffer-same-window) + "Display buffer action list for `org-ql-find'. +See function `display-buffer'." + :type 'sexp) + +;;;; Functions + +;;;###autoload +(cl-defun org-ql-find (buffers-files &key query-prefix query-filter + (prompt "Find entry: ")) + "Go to an Org entry in BUFFERS-FILES selected by searching entries with `org-ql'. +Interactively, with universal prefix, select multiple buffers to +search with completion. + +QUERY-PREFIX may be a string to prepend to the query (e.g. use +\"heading:\" to only search headings, easily creating a custom +command that saves the user from having to type it). + +QUERY-FILTER may be a function through which the query the user +types is filtered before execution (e.g. it could replace spaces +with commas to turn multiple tokens, which would normally be +treated as multiple predicates, into multiple arguments to a +single predicate)." + (interactive + (list (if current-prefix-arg + (mapcar #'get-buffer + (completing-read-multiple + "Buffers: " + (cl-loop for buffer in (buffer-list) + when (eq 'org-mode (buffer-local-value 'major-mode buffer)) + collect (buffer-name buffer)) + nil t)) + (progn + (unless (eq major-mode 'org-mode) + (user-error "This is not an Org buffer: %S" (current-buffer))) + (current-buffer))))) + (let ((marker (org-ql-completing-read buffers-files + :query-prefix query-prefix + :query-filter query-filter + :prompt prompt))) + (with-current-buffer (marker-buffer marker) + (goto-char marker) + (display-buffer (current-buffer) org-ql-find-display-buffer-action) + (select-window (get-buffer-window (current-buffer))) + (run-hook-with-args 'org-ql-find-goto-hook)))) + +;;;###autoload +(defun org-ql-refile (marker) + "Refile current entry to MARKER (interactively, one selected with `org-ql'). +Interactive completion uses files listed in `org-refile-targets', +which see (but only the files are used)." + (interactive (let ((buffers-files (delete-dups + ;; Always include the current buffer. + (cons (current-buffer) + (cl-loop for (files-spec . _candidate-spec) in org-refile-targets + append (cl-typecase files-spec + (null (list (current-buffer))) + (symbol (pcase (funcall files-spec) + ((and (pred stringp) file) (list file)) + ((and (pred listp) files) files))) + (list files-spec))))))) + (list (org-ql-completing-read buffers-files :prompt "Refile to: ")))) + (org-refile nil nil + ;; The RFLOC argument: + (list + ;; Name + (org-with-point-at marker + (nth 4 (org-heading-components))) + ;; File + (buffer-file-name (marker-buffer marker)) + ;; nil + nil + ;; Position + marker))) + +;;;###autoload +(defun org-ql-find-in-agenda () + "Call `org-ql-find' on `org-agenda-files'." + (interactive) + (org-ql-find (org-agenda-files))) + +;;;###autoload +(defun org-ql-find-in-org-directory () + "Call `org-ql-find' on files in `org-directory'." + (interactive) + (org-ql-find (org-ql-search-directories-files))) + +(provide 'org-ql-find) + +;;; org-ql-find.el ends here diff --git a/org-ql-search.el b/org-ql-search.el index 4fb5489..aa75656 100644 --- a/org-ql-search.el +++ b/org-ql-search.el @@ -38,6 +38,15 @@ (require 'org-ql) (require 'org-ql-view) +;;;; Compatibility + +(defalias 'org-ql-search--link-heading-search-string + (cond ((fboundp 'org-link--normalize-string) #'org-link--normalize-string) + ((fboundp 'org-link-heading-search-string) #'org-link-heading-search-string) + ((fboundp 'org-make-org-heading-search-string) #'org-make-org-heading-search-string) + (t (warn "org-ql: Unable to define alias `org-ql-search--link-heading-search-string'. This may affect links in dynamic blocks. Please report this as a bug.") + #'identity))) + ;;;; Variables (defvar org-ql-block-header nil @@ -285,9 +294,9 @@ For example, an org-ql dynamic block header could look like: (list (cons 'todo (lambda (element) (org-element-property :todo-keyword element))) (cons 'heading (lambda (element) - (org-make-link-string (org-element-property :raw-value element) - (org-link-display-format - (org-element-property :raw-value element))))) + (let ((normalized-heading + (org-ql-search--link-heading-search-string (org-element-property :raw-value element)))) + (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)))) diff --git a/org-ql-view.el b/org-ql-view.el index f6cc8c3..c0ccf64 100644 --- a/org-ql-view.el +++ b/org-ql-view.el @@ -718,7 +718,7 @@ When opened, the link searches the buffer it's opened from." (unless (or value transient--prefix) (message "Unset %s" variable)))) -(define-transient-command org-ql-view-dispatch () +(transient-define-prefix org-ql-view-dispatch () "Show Org QL View dispatcher." [["Edit" ("t" org-ql-view--transient-title) @@ -753,7 +753,7 @@ When opened, the link searches the buffer it's opened from." org-ql-view--format-query (org-ql-view--font-lock-string 'emacs-lisp-mode))))) -(define-infix-command org-ql-view--transient-title () +(transient-define-infix org-ql-view--transient-title () ;; TODO: Add an asterisk or something when the view has been modified but not saved. :description (lambda () (org-ql-view--format-transient-key-value "Title" org-ql-view-title)) :class 'org-ql-view--variable @@ -766,7 +766,7 @@ When opened, the link searches the buffer it's opened from." (format "%s" org-ql-view-title)) history))) -(define-infix-command org-ql-view--transient-query () +(transient-define-infix org-ql-view--transient-query () :description (lambda () (org-ql-view--format-transient-lisp-argument "Query" org-ql-view-query)) :class 'org-ql-view--variable :argument "" @@ -784,7 +784,7 @@ When opened, the link searches the buffer it's opened from." ;; Parse non-sexp query into sexp query. (org-ql--query-string-to-sexp query))))) -(define-infix-command org-ql-view--transient-in () +(transient-define-infix org-ql-view--transient-in () :description (lambda () (org-ql-view--format-transient-lisp-argument "In buffers/files" org-ql-view-buffers-files)) :class 'org-ql-view--variable :argument "" @@ -795,7 +795,7 @@ When opened, the link searches the buffer it's opened from." ;; argument, but it gives the same result. (org-ql-view--complete-buffers-files))) -(define-infix-command org-ql-view--transient-super-groups () +(transient-define-infix org-ql-view--transient-super-groups () :description (lambda () (org-ql-view--format-transient-lisp-argument "Group by" org-ql-view-super-groups)) :class 'org-ql-view--variable @@ -806,7 +806,7 @@ When opened, the link searches the buffer it's opened from." ;; FIXME: Figure out how to integrate initial-input and history. (org-ql-view--complete-super-groups))) -(define-infix-command org-ql-view--transient-sort () +(transient-define-infix org-ql-view--transient-sort () :description (lambda () (org-ql-view--format-transient-lisp-argument "Sort by" (or org-ql-view-sort 'buffer-order))) diff --git a/org-ql.el b/org-ql.el index 91d2710..ede80b5 100644 --- a/org-ql.el +++ b/org-ql.el @@ -1,19 +1,13 @@ ;;; org-ql.el --- Org Query Language, search command, and agenda-like view -*- lexical-binding: t; -*- +;; Copyright (C) 2017-2022 Adam Porter + ;; Author: Adam Porter ;; Url: https://github.com/alphapapa/org-ql -;; Version: 0.6 -;; Package-Requires: ((emacs "26.1") (dash "2.18.1") (f "0.17.2") (map "2.1") (org "9.0") (org-super-agenda "1.2") (ov "1.0.6") (peg "1.0") (s "1.12.0") (transient "0.1") (ts "0.2-pre")) +;; Version: 0.7-pre +;; Package-Requires: ((emacs "26.1") (dash "2.18.1") (f "0.17.2") (map "2.1") (org "9.0") (org-super-agenda "1.2") (ov "1.0.6") (peg "1.0.1") (s "1.12.0") (transient "0.1") (ts "0.2-pre")) ;; Keywords: hypermedia, outlines, Org, agenda -;;; Commentary: - -;; `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 a buffer with matching results, similar to an Org Agenda -;; buffer. - ;;; License: ;; This program is free software; you can redistribute it and/or modify @@ -29,12 +23,21 @@ ;; You should have received a copy of the GNU General Public License ;; along with this program. If not, see . +;;; Commentary: + +;; `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 a buffer with matching results, similar to an Org Agenda +;; buffer. + ;;; Code: ;;;; Requirements (require 'cl-lib) (require 'org) +(require 'org-duration) (require 'org-element) (require 'org-habit) (require 'seq) @@ -298,6 +301,16 @@ See Info node `(org-ql)Queries'." :type 'boolean :risky t) +(defcustom org-ql-default-predicate 'rifle + "Predicate used for plain-string tokens without a specified predicate." + :type '(choice (const heading) + (const heading-regexp) + (const regexp) + (const rifle) + (const smart) + (const outline-path) + (const outline-path-segment))) + ;;;; Functions ;;;;; Query execution @@ -352,18 +365,18 @@ each priority the newest items would appear first." (function (funcall buffers-or-files)) (list buffers-or-files) (otherwise (list buffers-or-files))) - (--map (cl-etypecase it - ;; NOTE: This etypecase is essential to opening links safely, - ;; as it rejects, e.g. lambdas in the buffers-files argument. - (buffer it) - (string (or (find-buffer-visiting it) - (when (file-readable-p it) - ;; It feels unintuitive that `find-file-noselect' returns - ;; a buffer if the filename doesn't exist. - (find-file-noselect it)) - (user-error "Can't open file: %s" it))))) - ;; Ignore special/hidden buffers. - (--remove (string-prefix-p " " (buffer-name it))))) + (--map (cl-etypecase it + ;; NOTE: This etypecase is essential to opening links safely, + ;; as it rejects, e.g. lambdas in the buffers-files argument. + (buffer it) + (string (or (find-buffer-visiting it) + (when (file-readable-p it) + ;; It feels unintuitive that `find-file-noselect' returns + ;; a buffer if the filename doesn't exist. + (find-file-noselect it)) + (display-warning 'org-ql-select (format "Can't open file: %s" it) :error))))) + ;; Ignore special/hidden buffers. + (--remove (string-prefix-p " " (buffer-name it))))) (query (org-ql--normalize-query query)) ((&plist :query :preamble :preamble-case-fold) (org-ql--query-preamble query)) (predicate (org-ql--query-predicate query)) @@ -399,12 +412,12 @@ each priority the newest items would appear first." (fset name fn))) ;; Run query on buffers. (->> buffers - (--map (with-current-buffer it - (unless (derived-mode-p 'org-mode) - (user-error "Not an Org buffer: %s" (buffer-name))) - (org-ql--select-cached :query query :preamble preamble :preamble-case-fold preamble-case-fold - :predicate predicate :action action :narrow narrow))) - (-flatten-n 1))) + (--map (with-current-buffer it + (unless (derived-mode-p 'org-mode) + (display-warning 'org-ql-select (format "Not an Org buffer: %s" (buffer-name)) :error)) + (org-ql--select-cached :query query :preamble preamble :preamble-case-fold preamble-case-fold + :predicate predicate :action action :narrow narrow))) + (-flatten-n 1))) (--each orig-fns ;; Restore original function mappings. (-let (((&plist :name :fn) it)) @@ -471,7 +484,7 @@ NARROW corresponds to the `org-ql-select' argument NARROW." (if-let* ((buffer-cache (gethash (current-buffer) org-ql-cache)) (query-cache (cadr buffer-cache)) (modified-tick (car buffer-cache)) - (buffer-unmodified-p (eq (buffer-modified-tick) modified-tick)) + (buffer-unmodified-p (eq (buffer-chars-modified-tick) modified-tick)) (cached-result (gethash query-cache-key query-cache))) (pcase cached-result ('org-ql-nil nil) @@ -480,7 +493,7 @@ NARROW corresponds to the `org-ql-select' argument NARROW." (cond ((or (not query-cache) (not buffer-unmodified-p)) (puthash (current-buffer) - (list (buffer-modified-tick) + (list (buffer-chars-modified-tick) (let ((table (make-hash-table :test 'org-ql-hash-test))) (puthash query-cache-key (or new-result 'org-ql-nil) table) table)) @@ -525,6 +538,20 @@ If NARROW is non-nil, buffer will not be widened." ;;;;; Helpers +(defun org-ql--ensure-buffer (file-or-buffer) + "Ensure a buffer is named or visiting FILE-OR-BUFFER. +If no such buffer exists with the name, and it is the name of a +readable file, `find-file-noselect' it into a buffer." + ;; See comment in `org-ql-find'. + ;; FIXME: Use this in `helm-org-ql' the same way it's used in + ;; `org-ql-find'. + (unless (or (get-buffer file-or-buffer) + (find-buffer-visiting file-or-buffer)) + (if (file-readable-p file-or-buffer) + (with-current-buffer (find-file-noselect file-or-buffer) + (cl-assert (eq 'org-mode major-mode) nil (format "Not an Org buffer: %S" file-or-buffer))) + (display-warning 'org-ql (format "Not a readable file: %S" file-or-buffer) :error)))) + (defun org-ql--tags-at (position) ;; FIXME: This function actually assumes that point is already at POSITION. "Return tags for POSITION in current buffer. @@ -535,7 +562,7 @@ Returns cons (INHERITED-TAGS . LOCAL-TAGS)." (if-let* ((buffer-cache (gethash (current-buffer) org-ql-tags-cache)) (modified-tick (car buffer-cache)) (tags-cache (cdr buffer-cache)) - (buffer-unmodified-p (eq (buffer-modified-tick) modified-tick)) + (buffer-unmodified-p (eq (buffer-chars-modified-tick) modified-tick)) (cached-result (gethash position tags-cache))) ;; Found in cache: return them. ;; FIXME: Isn't `cached-result' a list of (INHERITED . LOCAL)? It @@ -576,12 +603,12 @@ Returns cons (INHERITED-TAGS . LOCAL-TAGS)." (setf buffer-cache (gethash (current-buffer) org-ql-tags-cache) modified-tick (car buffer-cache) tags-cache (cdr buffer-cache) - buffer-unmodified-p (eq (buffer-modified-tick) modified-tick)) + buffer-unmodified-p (eq (buffer-chars-modified-tick) modified-tick)) (unless (and buffer-cache buffer-unmodified-p) ;; Buffer-local tags cache empty or invalid: make new one. (setf tags-cache (make-hash-table)) (puthash (current-buffer) - (cons (buffer-modified-tick) tags-cache) + (cons (buffer-chars-modified-tick) tags-cache) org-ql-tags-cache)) (puthash position all-tags tags-cache)))) @@ -612,7 +639,7 @@ Values compared with `equal'." (pcase (if-let* ((buffer-cache (gethash (current-buffer) org-ql-node-value-cache)) (modified-tick (car buffer-cache)) (position-cache (cdr buffer-cache)) - (buffer-unmodified-p (eq (buffer-modified-tick) modified-tick)) + (buffer-unmodified-p (eq (buffer-chars-modified-tick) modified-tick)) (value-cache (gethash position position-cache)) (cached-value (alist-get fn value-cache nil nil #'equal))) ;; Found in cache: return it. @@ -627,13 +654,13 @@ Values compared with `equal'." position-cache (cdr buffer-cache) value-cache (when position-cache (gethash position position-cache)) - buffer-unmodified-p (eq (buffer-modified-tick) modified-tick)) + buffer-unmodified-p (eq (buffer-chars-modified-tick) modified-tick)) (unless (and buffer-cache buffer-unmodified-p) ;; Buffer-local node cache empty or invalid: make new one. (setf position-cache (make-hash-table) value-cache (gethash position position-cache)) (puthash (current-buffer) - (cons (buffer-modified-tick) position-cache) + (cons (buffer-chars-modified-tick) position-cache) org-ql-node-value-cache)) (setf (alist-get fn value-cache nil nil #'equal) new-value) (puthash position value-cache position-cache) @@ -672,7 +699,8 @@ returns nil." (user-error "Query aborted by user"))))) (defun org-ql--plist-get* (plist property) - "Return the value of PROPERTY in PLIST, or `not-found' if the property is missing." + "Return the value of PROPERTY in PLIST, or `not-found'. +Returns `not-found' if the property is missing." (if-let ((pair (plist-member plist property))) (cadr pair) 'not-found)) @@ -686,6 +714,56 @@ returns nil." ;; partially typed queries in the Helm commands. (define-error 'org-ql-invalid-query "Invalid Org QL query" 'user-error) +(defun org-ql--coalesce-ands (query) + "Return QUERY having coalesced any AND'ed clauses' predicates. +Multiple calls to the same predicate within an `and' expression +are coalesced into a single call to the predicate. + +Note that this is a relatively simple function which does not +comprehensively coalesce every call that could be. For example, +if QUERY contained four calls to the `src' predicate with two +unique language arguments, only the calls for one language would +be coalesced." + ;; TODO: Use a per-predicate alist-getting function that accounts + ;; for arguments which must be unique...maybe...someday... + + ;; NOTE: This implentation can sometimes reorder sub-expressions, + ;; like: + ;; + ;; (and (src :regexps ("foo") :lang "elisp") (src :regexps ("bar"))) + ;; + ;; becomes: + ;; + ;; (and (src :regexps ("bar")) (src :regexps ("foo") :lang "elisp")) + ;; + ;; because the first one could be coalescable, but the second one + ;; can't be coalesced with it since they don't specify the same + ;; language. That could be fixed, but it's probably not worth it. + (cl-labels ((rec (sexp) + (pcase sexp + (`(,(and boolean (or 'or 'not)) . ,sexps) + `(,boolean ,@(mapcar #'rec sexps))) + (`(and . ,sexps) + (anded sexps)) + (_ sexp))) + (anded (sexps) + (let (anded-predicates new-sexp) + (dolist (sexp sexps) + (pcase sexp + (`(,(or 'or 'not) . ,_) + (push (rec sexp) new-sexp)) + (`(,predicate . ,args) + (pcase-exhaustive (plist-get (alist-get predicate org-ql-predicates) :coalesce) + (`nil (push sexp new-sexp)) + (`t (setf (alist-get predicate anded-predicates) + (append (alist-get predicate anded-predicates) args))) + ((and fn (pred functionp)) + (if-let (new-args (funcall fn (alist-get predicate anded-predicates) args)) + (setf (alist-get predicate anded-predicates) new-args) + (push sexp new-sexp))))))) + (delq nil `(and ,@(nreverse new-sexp) ,@(nreverse anded-predicates)))))) + (rec query))) + (defun org-ql--sanity-check-form (form) "Signal error if any forms in FORM do not have preconditions met. Or, when possible, fix the problem." @@ -765,12 +843,12 @@ respectively." "#+end_src") t)) -(defun org-ql--byte-compile-warning (_string _pos _fill level) +(defun org-ql--byte-compile-warning (string _pos _fill level) "Signal an `org-ql-invalid-query' error. Arguments STRING, POS, FILL, and LEVEL are according to `byte-compile-log-warning-function'." ;; Used as the `byte-compile-log-warning-function' in `org-ql--query-preamble'. - (signal 'org-ql-invalid-query level)) + (signal 'org-ql-invalid-query (list string level))) (defun org-ql--query-predicate (query) "Return predicate function for QUERY." @@ -854,15 +932,14 @@ value of `org-ql-predicates')." ;; obscure bug in `peg': when one keyword is a substring of another, ;; and the shorter one is listed first, the shorter one fails to match. (-sort (-on #'> #'length)))) - (pexs `((query (+ term - (opt (+ (syntax-class whitespace) (any))))) + (pexs `((query (+ (and term (* [blank])))) (term (or (and negation (list positive-term) ;; This is a bit confusing, but it seems to work. There's probably a better way. `(pred -- (list 'not (car pred)))) positive-term)) (positive-term (or (and predicate-with-args `(pred args -- (cons (intern pred) args))) (and predicate-without-args `(pred -- (list (intern pred)))) - (and plain-string `(s -- (list 'regexp s))))) + (and plain-string `(s -- (list org-ql-default-predicate s))))) (plain-string (or quoted-arg unquoted-arg)) (predicate-with-args (substring predicate) ":" args) (predicate-without-args (substring predicate) ":") @@ -945,7 +1022,7 @@ defined in `org-ql-predicates' by calling `org-ql-defpred'." (`(unless ,condition . ,clauses) `(unless ,(rec condition) ,@(mapcar #'rec clauses))) ;; TODO: Combine (regexp) when appropriate (i.e. inside an OR, not an AND). - ((pred stringp) `(regexp ,element)) + ((pred stringp) `(,org-ql-default-predicate ,element)) ,@normalizer-patterns @@ -959,7 +1036,7 @@ defined in `org-ql-predicates' by calling `org-ql-defpred'." (setf query new-query) (when (eq (cl-incf count) limit) (error "Query normalization limit exceeded: QUERY:%S" query))) - finally return new-query))))))) + finally return (org-ql--coalesce-ands new-query)))))))) (defun org-ql--define-query-preamble-fn (predicates) "Define function `org-ql--query-preamble' for PREDICATES. @@ -1029,7 +1106,7 @@ defined in `org-ql-predicates' by calling `org-ql-defpred'." ;; function still works. But to avoid the warning, we byte-compile it afterward. (byte-compile 'org-ql--query-preamble))) -(cl-defmacro org-ql-defpred (name args docstring &key body preambles normalizers) +(cl-defmacro org-ql-defpred (name args docstring &key body preambles normalizers coalesce) "Define an `org-ql' selector predicate named `org-ql--predicate-NAME'. NAME may be a symbol or a list of symbols: if a list, the first is used as NAME and the rest are aliases. A function is only @@ -1083,6 +1160,24 @@ to variables bound in the pattern: query expression with no work to do, which improves performance. +When COALESCE is t, multiple calls to this predicate within a +boolean AND query clause may be combined into a single call to +this predicate (so it is expected that this predicate treats +multiple arguments as being boolean AND'ed together; if it does +not, then it should not be set to coalesce). + +This argument may also be a function called to do coalescing of +two predicate expressions. It is called with two arguments: the +list of already-coalesced arguments to an expression, and the +list of arguments to the call being coalesced (note that a +query's arguments are normalized before the query is coalesced). +If it returns nil, the expression is not coalesced; otherwise, it +should return a new list of arguments coalescing the given +arguments, with new arguments being first. (Using a +predicate-specific function to coalesce arguments is useful when, +e.g. a predicate takes keyword arguments, so arguments to +multiple calls can't be simply appended.) + For convenience, within the `pcase' patterns, the symbol `predicate-names' is a special form which is replaced with a pattern matching any of the predicate's name and aliases. For @@ -1099,6 +1194,8 @@ It would be expanded to: ((`(,(or 'heading 'h) . ,args) `(heading ,@args)))" + ;; FIXME: Update defpred tutorial to include :coalesce. + ;; NOTE: The debug form works, completely! For example, use `edebug-defun' ;; on the `heading' predicate, then evaluate this form: ;; (let* ((query '(heading "HEADING")) @@ -1108,7 +1205,8 @@ It would be expanded to: ;; :normalized normalized ;; :preamble preamble)) (declare (debug ([&or symbolp listp] listp stringp - &rest [&or [":body" def-body] + &rest [&or [":coalesce" form] + [":body" def-body] [":normalizers" (&rest (sexp def-body))] [":preambles" (&rest (sexp def-body))]])) (indent defun)) @@ -1124,12 +1222,20 @@ It would be expanded to: normalizers)) (preambles (cl-sublis (list (cons 'predicate-names (cons 'or (--map (list 'quote it) predicate-names)))) preambles))) + ;; Ensure that a normalizer is defined if aliases are defined. + ;; (This doesn't ensure that the normalizer does what is intended, + ;; but it's at least some safeguard.) + ;; TODO: Add per-predicate test in suite to ensure that aliases + ;; are normalized to the full name. + (when (and aliases (not normalizers)) + (user-error "org-ql-defpred: Aliases defined for predicate `%s' without a normalizer (which must be used to replace aliases with the predicate's full name)" name)) `(progn (cl-defun ,fn-name ,args ,docstring ,body) ;; SOMEDAY: Use `map-elt' here, after map 2.1 can be automatically installed in CI sandbox... (setf (alist-get ',predicate-name org-ql-predicates) `(:name ,',name :aliases ,',aliases :fn ,',fn-name :docstring ,(\, docstring) :args ,',args - :normalizers ,',normalizers :preambles ,',preambles)) + :normalizers ,',normalizers :preambles ,',preambles + :coalesce ,,coalesce)) (unless org-ql-defpred-defer ;; Reversing preserves the order in which predicates were defined. (org-ql--define-normalize-query-fn (reverse org-ql-predicates)) @@ -1220,8 +1326,10 @@ result form." ;; redefinitions until all of the predicates have been defined. (setf org-ql-defpred-defer t) -(org-ql-defpred category (&rest categories) +(org-ql-defpred (category c) (&rest categories) "Return non-nil if current heading is in one or more of CATEGORIES (a list of strings)." + :normalizers ((`(,predicate-names . ,rest) + `(category ,@rest))) :body (when-let ((category (org-get-category (point)))) (cl-typecase categories (null t) @@ -1312,11 +1420,20 @@ Org effort string, like \"5\" or \"0:05\"." (org-ql-defpred (heading h) (&rest strings) "Return non-nil if current entry's heading matches all STRINGS. Matching is done case-insensitively." + :coalesce t :normalizers ((`(,predicate-names . ,args) ;; "h" alias. `(heading ,@args))) ;; TODO: Adjust regexp to avoid matching in tag list. - :preambles ((`(,predicate-names ,string) + :preambles ((`(,predicate-names) + ;; This clause protects against the case in which the + ;; arguments are nil, which would cause an error in + ;; `rx-to-string' in other clauses. This can happen + ;; with `org-ql-completing-read', e.g. when the input + ;; is "h:" while the user is typing. + (list :regexp (rx bol (1+ "*") (1+ blank) (0+ nonl)) + :case-fold t :query query)) + (`(,predicate-names ,string) ;; Only one string: match with preamble, then let predicate confirm (because ;; the match could be in e.g. the tags rather than the heading text). (list :regexp (rx-to-string `(seq bol (1+ "*") (1+ blank) (0+ nonl) @@ -1338,11 +1455,20 @@ Matching is done case-insensitively." (org-ql-defpred (heading-regexp h*) (&rest regexps) "Return non-nil if current entry's heading matches all REGEXPS (regexp strings). Matching is done case-insensitively." + :coalesce t :normalizers ((`(,predicate-names . ,args) ;; "h" alias. `(heading-regexp ,@args))) ;; MAYBE: Adjust regexp to avoid matching in tag list. - :preambles ((`(,predicate-names ,regexp) + :preambles ((`(,predicate-names) + ;; This clause protects against the case in which the + ;; arguments are nil, which would cause an error in + ;; `rx-to-string' in other clauses. This can happen + ;; with `org-ql-completing-read', e.g. when the input + ;; is "h:" while the user is typing. + (list :regexp (rx bol (1+ "*") (1+ blank) (0+ nonl)) + :case-fold t :query query)) + (`(,predicate-names ,regexp) ;; Only one regexp: match with preamble, then let predicate confirm (because ;; the match could be in e.g. the tags rather than the heading text). (list :regexp (rx-to-string `(seq bol (1+ "*") (1+ blank) (0+ nonl) @@ -1378,7 +1504,15 @@ COMPARATOR may be `<', `<=', `>', or `>='." ((pred stringp) (string-to-number it)) (_ it)) args)))) - :preambles ((`(,predicate-names ,comparator-or-num ,num) + :preambles ((`(,predicate-names) + ;; This clause protects against the case in which the + ;; arguments are nil, which would cause an error in + ;; `rx-to-string' in other clauses. This can happen + ;; with `org-ql-completing-read', e.g. when the input + ;; is "h:" while the user is typing. + (list :regexp (rx bol (1+ "*") " ") + :case-fold t)) + (`(,predicate-names ,comparator-or-num ,num) (let ((repeat (pcase comparator-or-num ('< `(repeat 1 ,(1- num) "*")) ('<= `(repeat 1 ,num "*")) @@ -1461,7 +1595,8 @@ any link is found." (setf description (regexp-quote description))) (when target (setf target (regexp-quote target)))) - (when (re-search-forward org-ql-link-regexp (org-entry-end-position) t) + (when (save-excursion + (re-search-forward org-ql-link-regexp (org-entry-end-position) t)) (pcase description-or-target ('nil (and (or (null target) (string-match-p target (match-string 1))) @@ -1474,6 +1609,27 @@ any link is found." (string-match-p description-or-target (match-string org-ql-link-description-group))))))))) +(org-ql-defpred (rifle smart) (&rest strings) + "Return non-nil if each string argument is found in either the entry or its outline path. +Works like `org-rifle'. This is probably the most useful, +intuitive, general-purpose predicate." + ;; NOTE: This predicate advertises that it takes strings, but they + ;; are normalized to regexps. Because of that, we must use a + ;; coalescing function. + :coalesce (lambda (coalesced-args current-args) + (plist-put coalesced-args :regexps + (list 'quote (append (cadr (plist-get coalesced-args :regexps)) + (cadr (plist-get current-args :regexps)))))) + :normalizers ((`(,predicate-names . ,(and rest (guard (cl-every #'stringp rest)))) + ;; If this doesn't match, it's already normalized. + `(rifle :regexps ',(mapcar #'regexp-quote rest)))) + :preambles ((`(,predicate-names :regexps ',regexps) + (list :regexp (rx-to-string `(seq bow (or ,@(mapcar (lambda (s) `(regexp ,s)) regexps)))) + :case-fold t :query query))) + :body (cl-loop for regexp in (plist-get strings :regexps) + always (or (org-ql--predicate-regexp regexp) + (org-ql--predicate-outline-path regexp)))) + ;; MAYBE: Preambles for outline-path predicates. Not sure if possible without complicated logic. ;; FIXME: These preds say they accept regexps but the strings get regexp-quoted. They should probably just take strings. @@ -1489,6 +1645,7 @@ the following queries: (olp \"Food\" \"Fruits\") (olp \"Fruits\" \"Grapes\") (olp \"Food\" \"Grapes\")" + :coalesce t :normalizers ((`(,predicate-names . ,strings) ;; Regexp quote headings. `(org-ql--predicate-outline-path ,@(mapcar #'regexp-quote strings)))) @@ -1522,6 +1679,7 @@ contiguous segment of the outline path: (org-ql-defpred 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." + ;; FIXME: This should AND the regexps together, not OR them. :body (when (buffer-file-name) (cl-typecase regexps (null t) @@ -1605,30 +1763,54 @@ priority B)." (cl-loop for priority-arg in args thereis (= item-priority (* 1000 (- org-lowest-priority (string-to-char priority-arg))))))))) -(org-ql-defpred property (property &optional value) - "Return non-nil if current entry has PROPERTY (a string), and optionally VALUE (a string)." - :normalizers ((`(,predicate-names ,property . ,value) +(org-ql-defpred property (property &optional value &key inherit) + "Return non-nil if current entry has PROPERTY (a string), and optionally VALUE (a string). +If INHERIT is nil, only match entries with PROPERTY set on the +entry; if t, also match entries with inheritance. If INHERIT is +not specified, use the Boolean value of +`org-use-property-inheritance', which see (i.e. it is only +interpreted as nil or non-nil)." + :normalizers ((`(,predicate-names) + ;; HACK: This clause protects against the case in + ;; which the arguments are nil, which would cause an + ;; error in `rx-to-string' in other clauses. This + ;; can happen with `org-ql-completing-read', + ;; e.g. when the input is "property:" while the user + ;; is typing. + ;; FIXME: Instead of this being moot, make this + ;; predicate test for whether an entry has local + ;; properties when no arguments are given. + (list 'property "")) + (`(,predicate-names ,property ,value . ,plist) ;; Convert keyword property arguments to strings. Non-sexp ;; queries result in keyword property arguments (because to do ;; otherwise would require ugly special-casing in the parsing). (when (keywordp property) (setf property (substring (symbol-name property) 1))) - (cons 'property (cons property value)))) + (list 'property property value + :inherit (if (plist-member plist :inherit) + (plist-get plist :inherit) + org-use-property-inheritance)))) ;; MAYBE: Should case folding be disabled for properties? What about values? ;; MAYBE: Support (property) without args. - :preambles ((`(,predicate-names ,property ,value) + + ;; NOTE: When inheritance is enabled, the preamble can't be used, + ;; which will make the search slower. + :preambles ((`(,predicate-names ,property ,value . ,(map :inherit)) ;; We do NOT return nil, because the predicate still needs to be tested, ;; because the regexp could match a string not inside a property drawer. - (list :regexp (rx-to-string `(seq bol (0+ space) ":" ,property ":" - (1+ space) ,value (0+ space) eol)) + (list :regexp (unless inherit + (rx-to-string `(seq bol (0+ space) ":" ,property ":" + (1+ space) ,value (0+ space) eol))) :query query)) - (`(,predicate-names ,property) + (`(,predicate-names ,property . ,(map :inherit)) ;; We do NOT return nil, because the predicate still needs to be tested, ;; because the regexp could match a string not inside a property drawer. ;; NOTE: The preamble only matches if there appears to be a value. ;; A line like ":ID: " without any other text does not match. - (list :regexp (rx-to-string `(seq bol (0+ space) ":" ,property ":" (1+ space) - (minimal-match (1+ not-newline)) eol)) + (list :regexp (unless inherit + (rx-to-string `(seq bol (0+ space) ":" ,property ":" (1+ space) + (minimal-match (1+ not-newline)) eol))) :query query))) :body (pcase property @@ -1636,13 +1818,25 @@ priority B)." (_ (pcase value ('nil ;; Check that PROPERTY exists - (org-entry-get (point) property)) + (org-ql--value-at + (point) (lambda () + (org-entry-get (point) property)))) (_ - ;; Check that PROPERTY has VALUE - (string-equal value (org-entry-get (point) property 'selective))))))) + ;; Check that PROPERTY has VALUE. + + ;; TODO: Since --value-at doesn't account for inheritance, + ;; we should generalize --tags-at to also work for property + ;; inheritance and use it here, which should be much faster. + (string-equal value (org-ql--value-at + (point) (lambda () + (org-entry-get (point) property inherit))))))))) + +;; TODO: Add property-local, property-inherit, etc. to match tags predicates. +;; TODO: Add tests for property inheritance. (org-ql-defpred (regexp r) (&rest regexps) "Return non-nil if current entry matches all of REGEXPS (regexp strings)." + :coalesce t :normalizers ((`(,predicate-names . ,args) `(regexp ,@args))) ;; MAYBE: Separate case-sensitive (Regexp) predicate. @@ -1664,27 +1858,70 @@ priority B)." (org-ql-defpred src (&key regexps lang) "Return non-nil if current entry contains an Org source block matching all of REGEXPS. If keyword argument LANG is non-nil, the block must be in that -language." - :normalizers ((`(,predicate-names . ,args) +language. Matching is done case-insensitively." + :coalesce (lambda (coalesced-args current-args) + (when (or (not coalesced-args) + (equal (plist-get current-args :lang) + (plist-get coalesced-args :lang))) + (setf coalesced-args + (plist-put coalesced-args :lang (plist-get current-args :lang))) + (setf coalesced-args + (plist-put coalesced-args + :regexps (list 'quote + (append (car (delq 'quote (plist-get coalesced-args :regexps))) + ;; A bit awkward, but necessary. + (car (delq 'quote (plist-get current-args :regexps))))))))) + :normalizers ((`(,predicate-names) + ;; This clause protects against the case in which the + ;; arguments are nil, which would cause an error in + ;; `rx-to-string' in other clauses. This can happen + ;; with `org-ql-completing-read', e.g. when the input + ;; is "src:" while the user is typing. + (list 'src)) + ;; NOTE: The :regexps argument is a quoted list, + ;; because we call the byte-compiler at runtime, and + ;; without quoting, it would interpret it as a + ;; function call. This requires some awkwardness in + ;; other places to deal with the quoting. + (`(,predicate-names . ,args) ;; Rewrite to use keyword args. - (-let (regexps lang keyword-index) - (cond ((plist-get args :lang) - ;; Lang given first, or only lang given. - (setf lang (plist-get args :lang) - regexps (seq-difference args (list :lang lang)))) - ((setf keyword-index (-find-index #'keywordp args)) - ;; Regexps and lang given. - (setf lang (plist-get (cl-subseq args keyword-index) :lang) - regexps (cl-subseq args 0 keyword-index))) - (t ;; Only regexps given. - (setf regexps args))) - (when regexps - ;; This feels awkward and wrong, but we have to quote lists - ;; and avoid quoting nil. There must be a better way. - (setf regexps `(',regexps))) - `(src :lang ,lang :regexps ,@regexps)))) - :preambles ((`(,predicate-names . ,args) + (cond ((cl-every #'stringp args) + ;; No keywords, only regexps. + `(src :regexps ',args)) + ((and (stringp (car args)) (cl-some #'keywordp args)) + ;; Regexp as first arg with keyword later. + (let* ((keyword-pos (cl-position :lang args)) + (regexps (cl-subseq args 0 keyword-pos)) + ;; We assume that if :lang is given, the string argument follows. + (lang (nth (1+ (cl-position :lang args)) args))) + `(src :lang ,lang + :regexps ',regexps))) + ((keywordp (car args)) + ;; All plist args. + `(src ,@(delq nil + (append (when (plist-get args :lang) + (list :lang (plist-get args :lang))) + (pcase (plist-get args :regexps) + (`(quote . ,_) + ;; Already quoted: return as-is to stop further normalization. + (list :regexps (plist-get args :regexps))) + (_ (list :regexps `(quote ,(plist-get args :regexps)))))))))))) + ;; NOTE: We match case-insensitively since the + ;; "#+BEGIN_SRC/#+END_SRC" lines could be either upper- or + ;; lowercase, as well as the language name. + :preambles ((`(,predicate-names) + ;; This clause protects against the case in which the + ;; arguments are nil, which would cause an error in + ;; `rx-to-string' in other clauses. This can happen + ;; with `org-ql-completing-read', e.g. when the input + ;; is "src:" while the user is typing. + (list :regexp (org-ql--format-src-block-regexp) + :case-fold t + ;; Always check contents with predicate. + :query query)) + (`(,predicate-names . ,args) (list :regexp (org-ql--format-src-block-regexp (plist-get args :lang)) + :case-fold t ;; Always check contents with predicate. :query query))) :body @@ -1728,6 +1965,7 @@ Tests both inherited and local tags." (org-ql-defpred (tags-all tags&) (&rest tags) "Return non-nil if current heading has all of TAGS (a list of strings). Tests both inherited and local tags." + :coalesce t ;; MAYBE: -all versions for inherited and local. :normalizers ((`(,predicate-names . ,tags) `(and ,@(--map `(tags ,it) tags)))) diff --git a/org-ql.info b/org-ql.info index 09826ca..0fd3b9f 100644 --- a/org-ql.info +++ b/org-ql.info @@ -11,6 +11,14 @@ File: README.info, Node: Top, Next: Contents, Up: (dir) org-ql ****** +This package provides a query language for Org files. It offers two +syntax styles: Lisp-like sexps and search engine-like keywords. + + It includes three libraries: The ‘org-ql’ library is flexible and may +be used as a backend for other tools. The libraries ‘org-ql-search’ and +‘helm-org-ql’ (a separate package) provide interactive search commands +and saved views. + * Menu: * Contents:: @@ -23,15 +31,11 @@ org-ql — The Detailed Node Listing — - - Installation * Quelpa:: * Helm support:: - - Usage * Commands:: @@ -43,6 +47,8 @@ Usage Commands +* org-ql-find:: +* org-ql-refile:: * org-ql-search:: * helm-org-ql:: * org-ql-view:: @@ -50,8 +56,6 @@ Commands * org-ql-view-recent-items:: * org-ql-sparse-tree:: - - Queries * Non-sexp query syntax:: @@ -59,8 +63,6 @@ Queries * Ancestor/descendant predicates:: * Date/time predicates:: - - Functions / Macros * Agenda-like views:: @@ -69,6 +71,10 @@ Functions / Macros Changelog +* 0.7-pre: 07-pre. +* 0.6.3: 063. +* 0.6.2: 062. +* 0.6.1: 061. * 0.6: 06. * 0.5.2: 052. * 0.5.1: 051. @@ -92,20 +98,11 @@ Changelog * 0.2: 02. * 0.1: 01. - - Notes * Comparison with Org Agenda searches:: * org-sidebar:: -This package provides a query language for Org files. It offers two -syntax styles: Lisp-like sexps and search engine-like keywords. - - It includes three libraries: The org-ql library is flexible and may -be used as a backend for other tools. The libraries org-ql-search and -helm-org-ql (a separate package) provide interactive search commands and -saved views.  File: README.info, Node: Contents, Next: Screenshots, Prev: Top, Up: Top @@ -126,13 +123,13 @@ File: README.info, Node: Installation, Next: Usage, Prev: Screenshots, Up: T 3 Installation ************** -The package org-ql may be installed directly from MELPA +The package ‘org-ql’ may be installed directly from MELPA (https://melpa.org/#/org-ql) or with other tools like Quelpa (https://framagit.org/steckerhalter/quelpa). After installation, you can use the commands without additional configuration. To use the functions and macros in your own Elisp code, -use libraries org-ql and org-ql-view. +use libraries ‘org-ql’ and ‘org-ql-view’. * Menu: @@ -163,8 +160,8 @@ File: README.info, Node: Helm support, Prev: Quelpa, Up: Installation 3.2 Helm support ================ -The command helm-org-ql is available in the package helm-org-ql. It may -be installed from MELPA, or with Quelpa, like so: +The command ‘helm-org-ql’ is available in the package ‘helm-org-ql’. It +may be installed from MELPA, or with Quelpa, like so: (use-package helm-org-ql :quelpa (helm-org-ql :fetcher github :repo "alphapapa/org-ql" @@ -198,18 +195,15 @@ File: README.info, Node: Commands, Next: Queries, Up: Usage 4.1 Commands ============ - • *Showing an agenda-like view:* - • (command) - • (command) - • (command) - • (command) - • *Showing a tree in a buffer:* - • (command) - • *Showing results with Helm*: - • (command) - + • *Jumping to an entry:* + • and related commands + • • *Showing an agenda-like view:* + • • • • • *Showing a tree in a buffer:* + • * Menu: +* org-ql-find:: +* org-ql-refile:: * org-ql-search:: * helm-org-ql:: * org-ql-view:: @@ -218,9 +212,34 @@ File: README.info, Node: Commands, Next: Queries, Up: Usage * org-ql-sparse-tree::  -File: README.info, Node: org-ql-search, Next: helm-org-ql, Up: Commands +File: README.info, Node: org-ql-find, Next: org-ql-refile, Up: Commands -4.1.1 org-ql-search +4.1.1 org-ql-find +----------------- + +_Note: These commands use ._ + + These commands jump to a heading selected using Emacs’s built-in +completion facilities with an Org QL query: + + • ‘org-ql-find’ searches in the current buffer. + • ‘org-ql-find-in-agenda’ searches in ‘(org-agenda-files)’. + • ‘org-ql-find-in-org-directory’ searches in ‘org-directory’. + + +File: README.info, Node: org-ql-refile, Next: org-ql-search, Prev: org-ql-find, Up: Commands + +4.1.2 org-ql-refile +------------------- + +This command refiles the current Org entry to one selected by searching +with Org QL completion. It searches files listed in +‘org-refile-targets’ as well as the current buffer. + + +File: README.info, Node: org-ql-search, Next: helm-org-ql, Prev: org-ql-refile, Up: Commands + +4.1.3 org-ql-search ------------------- _Note: This command supports both sexp queries and ._ @@ -247,10 +266,10 @@ Interactively, with prefix, leave narrowed. ‘priority’. *Bindings:* Keys bound in results buffer. - • r: Refresh results. With prefix, prompt to adjust search + • ‘r’: Refresh results. With prefix, prompt to adjust search parameters. - • v: Show transient view dispatcher (like Magit’s popups). - • C-x C-s: Save query to variable ‘org-ql-views’ (accessible with + • ‘v’: Show ‘transient’ view dispatcher (like Magit’s popups). + • ‘C-x C-s’: Save query to variable ‘org-ql-views’ (accessible with command ‘org-ql-view’). *Note:* The view buffer is currently put in ‘org-agenda-mode’, which @@ -263,57 +282,57 @@ text properties are placed on each item, imitating an Agenda buffer.)  File: README.info, Node: helm-org-ql, Next: org-ql-view, Prev: org-ql-search, Up: Commands -4.1.2 helm-org-ql +4.1.4 helm-org-ql ----------------- _Note: This command uses . It is available separately in the package -helm-org-ql._ +‘helm-org-ql’._ This command displays matches with Helm. - • Press C-x C-s in the Helm session to save the results to an - org-ql-search buffer. + • Press ‘C-x C-s’ in the Helm session to save the results to an + ‘org-ql-search’ buffer.  File: README.info, Node: org-ql-view, Next: org-ql-view-sidebar, Prev: helm-org-ql, Up: Commands -4.1.3 org-ql-view +4.1.5 org-ql-view ----------------- Choose and display a view stored in ‘org-ql-views’. *Bindings:* Keys bound in view buffer. - • g, r: Refresh results. With prefix, prompt to adjust search + • ‘g’, ‘r’: Refresh results. With prefix, prompt to adjust search parameters. - • v: Show transient view dispatcher (like Magit’s popups). - • C-x C-s: Save query to variable ‘org-ql-views’ (accessible with + • ‘v’: Show ‘transient’ view dispatcher (like Magit’s popups). + • ‘C-x C-s’: Save query to variable ‘org-ql-views’ (accessible with command ‘org-ql-view’).  File: README.info, Node: org-ql-view-sidebar, Next: org-ql-view-recent-items, Prev: org-ql-view, Up: Commands -4.1.4 org-ql-view-sidebar +4.1.6 org-ql-view-sidebar ------------------------- -Show a sidebar window listing views stored in org-ql-views for easy -access. In the sidebar, press RET or mouse-1 to show the view at point, -and press c to customize the view at point. +Show a sidebar window listing views stored in ‘org-ql-views’ for easy +access. In the sidebar, press ‘RET’ or ‘mouse-1’ to show the view at +point, and press ‘c’ to customize the view at point.  File: README.info, Node: org-ql-view-recent-items, Next: org-ql-sparse-tree, Prev: org-ql-view-sidebar, Up: Commands -4.1.5 org-ql-view-recent-items +4.1.7 org-ql-view-recent-items ------------------------------ 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. +‘deadline’, ‘planning’, or ‘scheduled’. ‘FILES’ defaults to those +returned by the function ‘org-agenda-files’.  File: README.info, Node: org-ql-sparse-tree, Prev: org-ql-view-recent-items, Up: Commands -4.1.6 org-ql-sparse-tree +4.1.8 org-ql-sparse-tree ------------------------ Arguments: ‘(query &key keep-previous (buffer (current-buffer)))’ @@ -335,7 +354,7 @@ File: README.info, Node: Queries, Next: Functions / Macros, Prev: Commands, =========== • • • • - An org-ql query is a Lisp expression which may contain arbitrary + An ‘org-ql’ query is a Lisp expression which may contain arbitrary expressions, as well as calling 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 @@ -364,11 +383,11 @@ File: README.info, Node: Non-sexp query syntax, Next: General predicates, Up: 4.2.1 Non-sexp query syntax --------------------------- -The command org-ql-search also accepts, and the command helm-org-ql only -accepts, an alternative, non-sexp query syntax. The syntax is simple, -and a few examples of queries in both syntaxes should suffice. By -default, when multiple predicates are used, they are combined with -boolean and. +The command ‘org-ql-search’ also accepts, and the command ‘helm-org-ql’ +only accepts, an alternative, non-sexp query syntax. The syntax is +simple, and a few examples of queries in both syntaxes should suffice. +By default, when multiple predicates are used, they are combined with +boolean ‘and’. Sexp syntax Non-sexp syntax ------------------------------------------------------------------------------------------------------- @@ -384,9 +403,9 @@ Sexp syntax Non-sexp syntax ‘(and (tags "space") (not (regexp "moon")))’ ‘tags:space !moon’ ‘(priority >= B)’ ‘priority:A,B’ - Note that the effort, level, and priority predicates do not support -comparators in the non-sexp syntax, so multiple arguments should be -passed instead, as seen in the last example. + Note that the ‘effort’, ‘level’, and ‘priority’ predicates do not +support comparators in the non-sexp syntax, so multiple arguments should +be passed instead, as seen in the last example.  File: README.info, Node: General predicates, Next: Ancestor/descendant predicates, Prev: Non-sexp query syntax, Up: Queries @@ -408,14 +427,14 @@ Arguments are listed next to predicate names, where applicable. Matches if effort is between DURATIONs, inclusive. ‘(effort COMPARATOR DURATION)’: Matches if effort compares to ‘DURATION’ with ‘COMPARATOR’. ‘COMPARATOR’ may be ‘<’, ‘<=’, ‘>’, or ‘>=’. - ‘DURATION’ should be an Org effort string, like 5 or 0:05. + ‘DURATION’ should be an Org effort string, like ‘5’ or ‘0:05’. ‘habit’ Return non-nil if entry is a habit. ‘heading (&rest strings)’ Return non-nil if current entry’s heading matches all ‘STRINGS’. Matching is done case-insensitively. - • Aliases: h. -‘‘heading-regexp (&rest regexps)’’ + • Aliases: ‘h’. +‘heading-regexp (&rest regexps)’ Return non-nil if current entry’s heading matches all ‘REGEXPS’ (regexp strings). Matching is done case-insensitively. • Aliases: ‘h*’. @@ -435,8 +454,8 @@ Arguments are listed next to predicate names, where applicable. ‘outline-path (&rest strings)’ Return non-nil if current node’s outline path matches all of ‘STRINGS’. Each string may appear as a substring in any part of - the node’s outline path. For example, the path Food/Fruits/Grapes - would match ‘(olp "Fruit" "Grape")’. + the node’s outline path. For example, the path + ‘Food/Fruits/Grapes’ would match ‘(olp "Fruit" "Grape")’. • Aliases: ‘olp’. ‘outline-path-segment (&rest strings)’ Return non-nil if current node’s outline path matches ‘STRINGS’. @@ -458,21 +477,32 @@ Arguments are listed next to predicate names, where applicable. predicate (while Org itself considers items without a cookie to have the default priority, which, by default, is equal to priority ‘B’). -‘property (property &optional value)’ +‘property (property &optional value &key inherit)’ Return non-nil if current entry has ‘PROPERTY’ (a string), and - optionally ‘VALUE’ (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)’. + optionally ‘VALUE’ (a string). If ‘INHERIT’ is nil, only match + entries with ‘PROPERTY’ set on the entry; if t, also match entries + with inheritance. If ‘INHERIT’ is not specified, use the Boolean + value of ‘org-use-property-inheritance’, which see (i.e. it is + only interpreted as nil or non-nil). ‘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. - • Aliases: r. -‘‘src (&key lang regexps)’’ + • Aliases: ‘r’. +‘rifle (&rest strings)’ + Return non-nil if each string is found in either the entry or its + outline path. Works like ‘org-rifle’. This is probably the most + useful, intuitive, general-purpose predicate. + • Aliases: ‘smart’. + • *Note:* By default, this is the default predicate used for + plain-string query tokens (i.e. given without a specified + predicate). This can be customized with the option + ‘org-ql-default-predicate’. +‘src (&key lang regexps)’ Return non-nil if current entry contains an Org Babel source block. If ‘LANG’ is non-nil, match blocks of that language. If ‘REGEXPS’ is non-nil, require that block’s contents match all regexps. + Matching is done case-insensitively. ‘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. @@ -551,10 +581,10 @@ to look forward, or negative to look backward), a string parseable by Return non-nil if current entry has a timestamp in given period. Without arguments, return non-nil if entry has a timestamp. - ‘ts-active, ts-a’ - Like ts, but only matches active timestamps. - ‘ts-inactive, ts-i’ - Like ts, but only matches inactive timestamps. + ‘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 @@ -582,8 +612,8 @@ searching for items planned in the next few days: • *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 - within org-deadline-warning-days. Without arguments, return + If argument is ‘auto’, return non-nil if entry has deadline + within ‘org-deadline-warning-days’. Without arguments, return non-nil if entry has any deadline. ‘planning’ Return non-nil if current entry has planning timestamp (i.e. @@ -621,7 +651,7 @@ File: README.info, Node: Agenda-like views, Next: Listing / acting-on results, 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 + items tagged ‘Emacs’ with to-do keyword ‘SOMEDAY’, followed by the standard agenda view, in a single buffer: (setq org-agenda-custom-commands @@ -641,8 +671,8 @@ File: README.info, Node: Agenda-like views, Next: Listing / acting-on results, 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. + The variable ‘org-ql-block-header’ may be bound to a string to use + as the block header, otherwise the header is formed automatically.  File: README.info, Node: Listing / acting-on results, Next: Custom predicates, Prev: Agenda-like views, Up: Functions / Macros @@ -790,7 +820,7 @@ File: README.info, Node: Custom predicates, Prev: Listing / acting-on results, • See: Custom predicate tutorial (examples/defpred.org) - 1. Macro: org-ql-defpred + 1. Macro: ‘org-ql-defpred’ _Arguments:_ ‘(name args docstring &key body preambles normalizers)’ @@ -879,9 +909,9 @@ supported: • 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 + • 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 @@ -896,7 +926,7 @@ in the following example). For example, this dynamic block shows the first seven headings that are to-do items with priority A or B, sorted by deadline then priority, -with certain columns (including the value of the agenda-group property +with certain columns (including the value of the ‘agenda-group’ property with a custom header) and timestamp format: #+BEGIN: org-ql :query "todo: priority:A,B" :columns (todo (priority "P") ((property "agenda-group") "Group") deadline heading) :sort (deadline priority) :take 7 :ts-format "%Y-%m-%d %H:%M" @@ -939,7 +969,7 @@ File: README.info, Node: Tips, Prev: Links, Up: Usage ======== • Org QL View buffers can be bookmarked with Emacs bookmark commands, - e.g. C-x r m. This also integrates with org-sidebar + e.g. ‘C-x r m’. This also integrates with org-sidebar (https://github.com/alphapapa/org-sidebar) and Burly (https://github.com/alphapapa/burly.el). @@ -957,6 +987,10 @@ releases. * Menu: +* 0.7-pre: 07-pre. +* 0.6.3: 063. +* 0.6.2: 062. +* 0.6.1: 061. * 0.6: 06. * 0.5.2: 052. * 0.5.1: 051. @@ -981,13 +1015,107 @@ releases. * 0.1: 01.  -File: README.info, Node: 06, Next: 052, Up: Changelog +File: README.info, Node: 07-pre, Next: 063, Up: Changelog -5.1 0.6 +5.1 0.7-pre +=========== + +*Added* + • Commands ‘org-ql-find’, ‘org-ql-find-heading’, and + ‘org-ql-find-path’, which jump to entries selected using Emacs’s + built-in completion facilities and Org QL queries (like + ‘helm-org-ql’, but doesn’t require Helm.). + • Command ‘org-ql-refile’, which refiles the entry at point to one + selected using Org QL completion. + • Predicate ‘rifle’, which matches an entry if each of the given + arguments is found in either the entry’s contents or its outline + path. This provides very intuitive results, mimicing the behavior + of ‘org-rifle’ (https://github.com/alphapapa/org-rifle). In fact, + the results are so useful that it’s now the default predicate for + plain-string query tokens. (It is also aliased to ‘smart’, since + it’s so "smart," and not all users have used ‘org-rifle’.) + • Option ‘org-ql-default-predicate’, applied to plain-string query + tokens (before, the ‘regexp’ predicate was always used, but now it + may be customized). + • Alias ‘c’ for predicate ‘category’. + • Predicate ‘property’ now accepts the argument ‘:inherit’ to match + entries with property inheritance, and when unspecified, the option + ‘org-use-property-inheritance’ controls whether inheritance is + used. + + *Changed* + • Give more useful error message for invalid queries. + • Predicate ‘src’ now matches case-insensitively. + + *Fixed* + • Predicate ‘src’’s matching of begin/end block lines, normalization + of arguments, and handling in non-sexp queries. + + *Internal* + • Certain query predicates, when called multiple times in an ‘and’ + sub-expression, are optimized to a single call. + • Use ‘buffer-chars-modified-tick’ instead of ‘buffer-modified-tick’. + (Thanks to Ihor Radchenko (https://github.com/yantar92).) + + *Credits* + • Thanks to Caleb Chase (https://github.com/chasecaleb) for help with + #285 (https://github.com/alphapapa/org-ql/pull/285), fixed in + 9190818 + (https://github.com/alphapapa/org-ql/commit/91908186fcca4b5fd2e9d26da5bc0375c2b41acf). + + +File: README.info, Node: 063, Next: 062, Prev: 07-pre, Up: Changelog + +5.2 0.6.3 +========= + +*Fixed* + • Non-sexp query parsing with updated version 1.0.1 of the ‘peg’ + package. (Fixes #314 + (https://github.com/alphapapa/org-ql/issues/314), #316 + (https://github.com/alphapapa/org-ql/issues/316). Thanks to Akira + Komamura (https://github.com/akirak) and Joon Ro + (https://github.com/joonro) for reporting.) + • Require library ‘org-duration’ (apparently necessary in newer Org + versions). + + +File: README.info, Node: 062, Next: 061, Prev: 063, Up: Changelog + +5.3 0.6.2 +========= + +*Fixed* + • ‘link’ predicate when used in an ‘or’’ed query. (#279 + (https://github.com/alphapapa/org-ql/issues/279). Thanks to Marc + Fargas (https://github.com/telenieko) for reporting.) + + +File: README.info, Node: 061, Next: 06, Prev: 062, Up: Changelog + +5.4 0.6.1 +========= + +*Fixed* + • In dynamic blocks, links to headings with statistics cookies were + broken. (Fixes #248 + (https://github.com/alphapapa/org-ql/issues/248). Thanks to Maikol + Solis (https://github.com/maikol-solis) and Ihor Radchenko + (https://github.com/yantar92).) + + *Updated* + • Compatibility with new macro names in Transient. (#269 + (https://github.com/alphapapa/org-ql/pull/269). Thanks to Jonas + Bernoulli (https://github.com/tarsius).) + + +File: README.info, Node: 06, Next: 052, Prev: 061, Up: Changelog + +5.5 0.6 ======= *Added* - • Macro org-ql-defpred, used to define search predicates. (See + • Macro ‘org-ql-defpred’, used to define search predicates. (See tutorial (examples/defpred.org).) • Predicate ‘effort’. • Predicate ‘heading-regexp’, which matches regular expressions @@ -1008,21 +1136,21 @@ File: README.info, Node: 06, Next: 052, Up: Changelog For example, ‘:sort '(todo priority date)’ now does what ‘:sort '(date priority todo)’ did in earlier versions. (This change is made to enable the new ‘reverse’ sorting method.) Users who have - customized org-ql-views will need to update the stored views’ + customized ‘org-ql-views’ will need to update the stored views’ sorting methods to preserve the desired sort order. - • Helm support (including the command helm-org-ql) has been moved to - a separate package, helm-org-ql. + • Helm support (including the command ‘helm-org-ql’) has been moved + to a separate package, ‘helm-org-ql’. • Predicate ‘heading’ now matches plain strings instead of regular expressions. - • Update dash dependency, and remove dependency on obsolete - dash-functional. (Fixes #179 + • Update ‘dash’ dependency, and remove dependency on obsolete + ‘dash-functional’. (Fixes #179 (https://github.com/alphapapa/org-ql/issues/179), #209 (https://github.com/alphapapa/org-ql/issues/209). Thanks to Mark Hudnall (https://github.com/landakram), Akira Komamura (https://github.com/akirak), Nathanael kinfe (https://github.com/natask), Pablo Stafforini (https://github.com/benthamite), Jason May - (https://github.com/jmay), and Basil L. Contovounesios + (https://github.com/jmay), and Basil L. Contovounesios (https://github.com/basil-conto).) *Removed* @@ -1037,7 +1165,7 @@ File: README.info, Node: 06, Next: 052, Up: Changelog *Internal* • Predicates are now defined more cleanly with a macro - (org-ql-defpred) that consolidates functionality related to each + (‘org-ql-defpred’) that consolidates functionality related to each predicate. This will also allow users to more easily define custom predicates. • Version 1.0 of library ‘peg’ is now required. @@ -1050,7 +1178,7 @@ File: README.info, Node: 06, Next: 052, Up: Changelog  File: README.info, Node: 052, Next: 051, Prev: 06, Up: Changelog -5.2 0.5.2 +5.6 0.5.2 ========= *Fixed* @@ -1061,12 +1189,12 @@ File: README.info, Node: 052, Next: 051, Prev: 06, Up: Changelog  File: README.info, Node: 051, Next: 05, Prev: 052, Up: Changelog -5.3 0.5.1 +5.7 0.5.1 ========= *Fixed* • Custom sorting functions could corrupt the cache, causing items to - disappear after refreshing an org-ql-search buffer. (#186 + disappear after refreshing an ‘org-ql-search’ buffer. (#186 (https://github.com/alphapapa/org-ql/issues/186), #187 (https://github.com/alphapapa/org-ql/issues/187). Thanks to Nathanael kinfe (https://github.com/natask).) @@ -1074,19 +1202,19 @@ File: README.info, Node: 051, Next: 05, Prev: 052, Up: Changelog  File: README.info, Node: 05, Next: 049, Prev: 051, Up: Changelog -5.4 0.5 +5.8 0.5 ======= *Added* - • View dispatcher using transient.el (like Magit), bound to v in + • View dispatcher using ‘transient.el’ (like Magit), bound to ‘v’ in search/view buffers. - • Predicate link, which matches descriptions and targets in Org + • Predicate ‘link’, which matches descriptions and targets in Org links. • Predicate ‘tags-regexp’ (alias: ‘tags*’), which matches regexps against entry tags (e.g, helpful when a tag might end in "s"). • Emacs bookmark support: Org QL View buffers can be bookmarked with, - e.g. C-x r m and shown with, e.g. C-x r b. (This also enables - view restoration with Burly + e.g. ‘C-x r m’ and shown with, e.g. ‘C-x r b’. (This also + enables view restoration with Burly (https://github.com/alphapapa/burly.el).) • Dynamic block support. • Org link support (storing and opening links to Org QL View @@ -1094,7 +1222,7 @@ File: README.info, Node: 05, Next: 049, Prev: 051, Up: Changelog • Mascot. *Changed* - • Binding to refresh search/view buffers changed to r. + • Binding to refresh search/view buffers changed to ‘r’. *Internal* • When formatting entries for Org QL View buffers, use internal @@ -1103,9 +1231,9 @@ File: README.info, Node: 05, Next: 049, Prev: 051, Up: Changelog Org versions before 9.3. *Deprecated* - • Macro org-ql is marked obsolete. It will be removed in v0.7. - Functions org-ql-select and org-ql-query should be used instead. - (The macro serves only to confuse with regard to quoting + • Macro ‘org-ql’ is marked obsolete. It will be removed in v0.7. + Functions ‘org-ql-select’ and ‘org-ql-query’ should be used + instead. (The macro serves only to confuse with regard to quoting arguments.) *Acknowledgments* @@ -1115,19 +1243,19 @@ File: README.info, Node: 05, Next: 049, Prev: 051, Up: Changelog  File: README.info, Node: 049, Next: 048, Prev: 05, Up: Changelog -5.5 0.4.9 +5.9 0.4.9 ========= *Fixed* - • Agenda restriction in org-ql-block. (Fixes #84 + • Agenda restriction in ‘org-ql-block’. (Fixes #84 (https://github.com/alphapapa/org-ql/issues/84). Thanks to Ihor Radchenko (https://github.com/yantar92).)  File: README.info, Node: 048, Next: 047, Prev: 049, Up: Changelog -5.6 0.4.8 -========= +5.10 0.4.8 +========== *Fixed* • Multiple sorters not preserved when refreshing views. (Fixes #136 @@ -1138,12 +1266,12 @@ File: README.info, Node: 048, Next: 047, Prev: 049, Up: Changelog  File: README.info, Node: 047, Next: 046, Prev: 048, Up: Changelog -5.7 0.4.7 -========= +5.11 0.4.7 +========== *Fixed* - • Give a useful error if org-ql-search-directories-files is called - without a directories argument and org-directory doesn’t exist. + • Give a useful error if ‘org-ql-search-directories-files’ is called + without a directories argument and ‘org-directory’ doesn’t exist. (Fixes #139 (https://github.com/alphapapa/org-ql/issues/139). Thanks to Matt Huszagh (https://github.com/matthuszagh) for reporting.) @@ -1151,12 +1279,12 @@ File: README.info, Node: 047, Next: 046, Prev: 048, Up: Changelog  File: README.info, Node: 046, Next: 045, Prev: 047, Up: Changelog -5.8 0.4.6 -========= +5.12 0.4.6 +========== *Fixed* - • Compatibility with newer versions of the peg library, which removed - a macro used by this package. (Fixes #75 + • Compatibility with newer versions of the ‘peg’ library, which + removed a macro used by this package. (Fixes #75 (https://github.com/alphapapa/org-ql/issues/75). Thanks to Karl Voit (https://github.com/novoid) and @karlicoss (https://github.com/karlicoss) for reporting.) @@ -1164,8 +1292,8 @@ File: README.info, Node: 046, Next: 045, Prev: 047, Up: Changelog  File: README.info, Node: 045, Next: 044, Prev: 046, Up: Changelog -5.9 0.4.5 -========= +5.13 0.4.5 +========== *Fixed* • Non-case-folding predicates like ‘(todo)’ unnecessarily disabled @@ -1176,7 +1304,7 @@ File: README.info, Node: 045, Next: 044, Prev: 046, Up: Changelog  File: README.info, Node: 044, Next: 043, Prev: 045, Up: Changelog -5.10 0.4.4 +5.14 0.4.4 ========== *Fixed* @@ -1188,17 +1316,17 @@ File: README.info, Node: 044, Next: 043, Prev: 045, Up: Changelog  File: README.info, Node: 043, Next: 042, Prev: 044, Up: Changelog -5.11 0.4.3 +5.15 0.4.3 ========== *Fixed* - • When org-ql-view-refresh is called, ensure the buffer is an Org QL - View buffer. + • When ‘org-ql-view-refresh’ is called, ensure the buffer is an Org + QL View buffer.  File: README.info, Node: 042, Next: 041, Prev: 043, Up: Changelog -5.12 0.4.2 +5.16 0.4.2 ========== *Fixed* @@ -1207,24 +1335,24 @@ File: README.info, Node: 042, Next: 041, Prev: 043, Up: Changelog  File: README.info, Node: 041, Next: 04, Prev: 042, Up: Changelog -5.13 0.4.1 +5.17 0.4.1 ========== *Fixed* - • level predicate used with arguments in plain queries. (Thanks to + • ‘level’ predicate used with arguments in plain queries. (Thanks to Akira Komamura (https://github.com/akirak) for reporting.)  File: README.info, Node: 04, Next: 032, Prev: 041, Up: Changelog -5.14 0.4 +5.18 0.4 ======== _Note:_ The next release, 0.5, may include changes which will require minor updates to written queries (e.g. a few predicates may be renamed). Users who wish to avoid those changes happening unexpectedly -in their configs should avoid upgrading org-ql beyond 0.4 automatically, -as they will be pushed to the master branch when ready. +in their configs should avoid upgrading ‘org-ql’ beyond 0.4 +automatically, as they will be pushed to the ‘master’ branch when ready. *Added* • *Commands* @@ -1238,13 +1366,13 @@ as they will be pushed to the master branch when ready. • *Queries* • Negation of terms in plain queries using ‘!’. For example, ‘tags:space !moon’ to exclude entries which contain ‘moon’. - • Predicates outline-path (alias olp) and outline-path-segment - (alias olps). + • Predicates ‘outline-path’ (alias ‘olp’) and + ‘outline-path-segment’ (alias ‘olps’). • Predicate ‘src’, which matches Org Babel source blocks. - • Predicates parent and ancestors. (Thanks to Josh Moller-Mara - (https://github.com/mm--).) - • Alias h for heading predicate. - • Alias r for regexp predicate. (Thanks to Feng Shu + • Predicates ‘parent’ and ‘ancestors’. (Thanks to Josh + Moller-Mara (https://github.com/mm--).) + • Alias ‘h’ for ‘heading’ predicate. + • Alias ‘r’ for ‘regexp’ predicate. (Thanks to Feng Shu (https://github.com/tumashu).) • Info manual. • Function ‘helm-org-ql-source’, which returns a Helm source that @@ -1252,40 +1380,40 @@ as they will be pushed to the master branch when ready. for custom Helm commands that search certain files. • Display a message when views are refreshed. (Thanks to xeijin (https://github.com/xeijin).) - • Respect Org Agenda restriction in org-ql-block. (Thanks to Ihor + • Respect Org Agenda restriction in ‘org-ql-block’. (Thanks to Ihor Radchenko (https://github.com/yantar92) for reporting.) - • Option org-ql-view-sidebar-sort-views. - • Mouseover help-echo text for org-ql-views default view names. - • "Dangling tasks" default view in org-ql-views. (Users who have - modified org-ql-views from the default will not see the new view + • Option ‘org-ql-view-sidebar-sort-views’. + • Mouseover ‘help-echo’ text for ‘org-ql-views’ default view names. + • "Dangling tasks" default view in ‘org-ql-views’. (Users who have + modified ‘org-ql-views’ from the default will not see the new view unless they copy it into their config.) *Changed* - • Some default org-ql-view views (users who have modified - org-ql-views from the default will not see the new views unless + • Some default ‘org-ql-view’ views (users who have modified + ‘org-ql-views’ from the default will not see the new views unless they copy them into their config): • Rename some views. - • "Stuck projects" view (now uses descendants instead of - children, which is more useful. + • "Stuck projects" view (now uses ‘descendants’ instead of + ‘children’, which is more useful. *Fixed* - • Inherit file tags when org-tag-inheritance is enabled. (Fixes #55 - (https://github.com/alphapapa/org-ql/issues/55). Thanks to Mikhail - Skorzhinskiy (https://github.com/mskorzhinskiy).) - • Call helm-make-source directly instead of using - helm-build-sync-source macro. (Fixes #60 + • Inherit file tags when ‘org-tag-inheritance’ is enabled. (Fixes + #55 (https://github.com/alphapapa/org-ql/issues/55). Thanks to + Mikhail Skorzhinskiy (https://github.com/mskorzhinskiy).) + • Call ‘helm-make-source’ directly instead of using + ‘helm-build-sync-source’ macro. (Fixes #60 (https://github.com/alphapapa/org-ql/issues/60). Thanks to Matt Huszagh (https://github.com/matthuszagh) for reporting.) • Search/view buffers now always end with a newline, which prevents - side-scrolling of the window when calling end-of-buffer. - • Face for done to-do keywords in org-ql-view buffers. (Thanks to + side-scrolling of the window when calling ‘end-of-buffer’. + • Face for done to-do keywords in ‘org-ql-view’ buffers. (Thanks to Yiming Chen (https://github.com/dsdshcym).) • Make view buffers read-only. (Fixes #72 (https://github.com/alphapapa/org-ql/issues/72). Thanks to xeijin (https://github.com/xeijin).) • Sorting with single sorter specified as an atom. (Thanks to Jeff Filipovits (https://github.com/legalnonsense).) - • Autoload for org-ql-block agenda block. (Fixes #53 + • Autoload for ‘org-ql-block’ agenda block. (Fixes #53 (https://github.com/alphapapa/org-ql/issues/53). Thanks to reports from Gus Cantieni (https://github.com/gcantieni), Karl Voit (https://github.com/novoid), rieje (https://github.com/rieje), and @@ -1298,20 +1426,20 @@ as they will be pushed to the master branch when ready.  File: README.info, Node: 032, Next: 031, Prev: 04, Up: Changelog -5.15 0.3.2 +5.19 0.3.2 ========== *Fixed* - • In org-ql-search, accept symbol as ‘:super-groups’ argument. - • In the This week and Next week default org-ql-views views, set - timestamps for beginning-of-week to 00:00:00 and end-of-week to + • In ‘org-ql-search’, accept symbol as ‘:super-groups’ argument. + • In the ‘This week’ and ‘Next week’ default ‘org-ql-views’ views, + set timestamps for beginning-of-week to 00:00:00 and end-of-week to 23:59:59. • Plain quoted-phrases in non-sexp queries.  File: README.info, Node: 031, Next: 03, Prev: 032, Up: Changelog -5.16 0.3.1 +5.20 0.3.1 ========== *Fixed* @@ -1321,103 +1449,106 @@ File: README.info, Node: 031, Next: 03, Prev: 032, Up: Changelog  File: README.info, Node: 03, Next: 023, Prev: 031, Up: Changelog -5.17 0.3 +5.21 0.3 ======== *Added* - • Alternative, non-sexp query syntax for commands org-ql-search and - helm-org-ql. See . - • Command helm-org-ql. - • Command org-ql-sparse-tree, like org-sparse-tree for org-ql + • Alternative, non-sexp query syntax for commands ‘org-ql-search’ and + ‘helm-org-ql’. See . + • Command ‘helm-org-ql’. + • Command ‘org-ql-sparse-tree’, like ‘org-sparse-tree’ for ‘org-ql’ queries. (Thanks to Akira Komamura (https://github.com/akirak).) - • Command org-ql-view-sidebar. + • Command ‘org-ql-view-sidebar’. • 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 boolean AND - (rather than boolean OR, which the tags predicate uses). - • Variable org-ql-block-header, which overrides the default header in - org-ql-block agenda blocks. - • Predicate (path). - • Option org-ql-views may now be customized in a guided, structured - way with the customization UI (e.g. - M-x customize-option RET org-ql-views RET, or press c in the - org-ql-view-sidebar buffer). - • Enable more Org Agenda commands in org-ql-view buffers (e.g. + • 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 boolean + ‘AND’ (rather than boolean ‘OR’, which the ‘tags’ predicate + uses). + • Variable ‘org-ql-block-header’, which overrides the default header + in ‘org-ql-block’ agenda blocks. + • Predicate ‘(path)’. + • Option ‘org-ql-views’ may now be customized in a guided, structured + way with the customization UI (e.g. ‘M-x customize-option RET + org-ql-views RET’, or press ‘c’ in the ‘org-ql-view-sidebar’ + buffer). + • Enable more Org Agenda commands in ‘org-ql-view’ buffers (e.g. setting deadlines and scheduling). (Fixes #35 (https://github.com/alphapapa/org-ql/issues/35). Thanks to Milan Zamazal (https://github.com/mz-pdm) and Mikhail Skorzhinskii (https://github.com/mskorzhinskiy).) - • Function org-ql-select’s buffers-files argument can be a function - which returns a list of buffers and/or files. + • Function ‘org-ql-select’’s ‘buffers-files’ argument can be a + function which returns a list of buffers and/or files. *Changed* - • Predicate heading now accepts multiple regexps, which are matched - with boolean AND. - • Predicate regexp now matches its regexp arguments with boolean AND. - • Package org-super-agenda is now a dependency. This removes the + • Predicate ‘heading’ now accepts multiple regexps, which are matched + with boolean ‘AND’. + • Predicate ‘regexp’ now matches its regexp arguments with boolean + ‘AND’. + • Package ‘org-super-agenda’ is now a dependency. This removes the need for awkward code to handle the case where it’s not installed, and makes grouping features always available. Of course, the - global minor mode org-super-agenda-mode is not activated by org-ql, - so no behavior is changed in Org Agenda or org-ql; it only means - that commands like org-ql-search will always provide grouping when - called with the appropriate arguments. + global minor mode ‘org-super-agenda-mode’ is not activated by + ‘org-ql’, so no behavior is changed in Org Agenda or ‘org-ql’; it + only means that commands like ‘org-ql-search’ will always provide + grouping when called with the appropriate arguments. *Removed* - • Macro org-ql-agenda. Instead, use function org-ql-search. See - also command org-ql-view, etc. + • Macro ‘org-ql-agenda’. Instead, use function ‘org-ql-search’. See + also command ‘org-ql-view’, etc. *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 begins Waiting on - will no longer match for a todo keyword WAITING). - • Interactive completion in org-ql-search. + • 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 begins ‘Waiting on’ + will no longer match for a todo keyword ‘WAITING’). + • Interactive completion in ‘org-ql-search’. *Internal* - • Refactored code from file org-ql-agenda.el into files - org-ql-search.el and org-ql-view.el. Function and variable names - have been changed accordingly. + • Refactored code from file ‘org-ql-agenda.el’ into files + ‘org-ql-search.el’ and ‘org-ql-view.el’. Function and variable + names have been changed accordingly.  File: README.info, Node: 023, Next: 022, Prev: 03, Up: Changelog -5.18 0.2.3 +5.22 0.2.3 ========== *Fixed* • Priority queries could fail to match headings whose to-do keywords - had non-alphabetic characters, like TO-READ. + had non-alphabetic characters, like ‘TO-READ’.  File: README.info, Node: 022, Next: 021, Prev: 023, Up: Changelog -5.19 0.2.2 +5.23 0.2.2 ========== *Fixed* - • (deadline auto) selector matched entries whose deadlines had a + • ‘(deadline auto)’ selector matched entries whose deadlines had a warning period that had not yet been entered - (org-deadline-warning-days too soon). + (‘org-deadline-warning-days’ too soon).  File: README.info, Node: 021, Next: 02, Prev: 022, Up: Changelog -5.20 0.2.1 +5.24 0.2.1 ========== *Fixed* - • (descendants) selector matched against parent heading instead of + • ‘(descendants)’ selector matched against parent heading instead of only descendants.  File: README.info, Node: 02, Next: 01, Prev: 021, Up: Changelog -5.21 0.2 +5.25 0.2 ======== *Added* @@ -1438,7 +1569,7 @@ File: README.info, Node: 02, Next: 01, Prev: 021, Up: Changelog • Selectors ‘ts-a’ and ‘ts-i’, aliases for ‘ts-active’ and ‘ts-inactive’. • Selector ‘ts’ now accepts a ‘:type’ argument. - • Face org-ql-agenda-due-date. + • Face ‘org-ql-agenda-due-date’. • Selectors ‘(children)’ and ‘(descendants)’. • Function ‘org-ql-search’ and macro ‘org-ql-agenda’ accept a ‘:title’ argument, which is displayed in the header. @@ -1447,11 +1578,11 @@ File: README.info, Node: 02, Next: 01, Prev: 021, Up: Changelog • Customization group ‘org-ql’. • Command ‘org-ql-view’, which displays views saved to variable ‘org-ql-views’, which can be saved from ‘org-ql-search’ buffers - with command ‘org-ql-search-save’, which is bound to C-x C-s in + with command ‘org-ql-search-save’, which is bound to ‘C-x C-s’ in view buffers. • Variable ‘org-ql-view-map’, active in view buffers displayed by ‘org-ql-search’, ‘org-ql-agenda’, and ‘org-ql-view’. - • random sort method. + • ‘random’ sort method. • Save position when refreshing search buffers. *Changed* @@ -1470,14 +1601,14 @@ File: README.info, Node: 02, Next: 01, Prev: 021, Up: Changelog *Fixed* • Handle date ranges in date-based selectors. (Thanks to Cody - Goodman (https://github.com/codygman), Samuel W. Flint + Goodman (https://github.com/codygman), Samuel W. Flint (https://github.com/swflint), and Vikas Rawal (https://github.com/vikasrawal).) - • Don’t overwrite bindings in org-agenda-mode-map. + • 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. + • Properly accept arbitrary sort functions in ‘org-ql-select’, etc. (Fixes #37 (https://github.com/alphapapa/org-ql/issues/37). Thanks to Milan Zamazal (https://github.com/mz-pdm).) • Planning-line-related predicates searched too far into entries. @@ -1491,7 +1622,7 @@ File: README.info, Node: 02, Next: 01, Prev: 021, Up: Changelog (https://github.com/dakra).) *Internal* - • Optimizations for some query selectors, e.g. regexp and todo. + • Optimizations for some query selectors, e.g. ‘regexp’ and ‘todo’. These can provide a significant improvement for some queries. See benchmarks in notes.org (notes.org). • Library ts (https://github.com/alphapapa/ts.el) is now used for @@ -1500,7 +1631,7 @@ File: README.info, Node: 02, Next: 01, Prev: 021, Up: Changelog  File: README.info, Node: 01, Prev: 02, Up: Changelog -5.22 0.1 +5.26 0.1 ======== First tagged release. @@ -1531,10 +1662,10 @@ 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-} + +lisp +{^\*+\s-+TO-READ\s-} - But with org-ql-search, you would write a query like -lisp todo:TO-READ, or in Lisp syntax, ‘(and "lisp" (todo "TO-READ"))’. + But with ‘org-ql-search’, you would write a query like ‘lisp +todo:TO-READ’, or in Lisp syntax, ‘(and "lisp" (todo "TO-READ"))’.  File: README.info, Node: org-sidebar, Prev: Comparison with Org Agenda searches, Up: Notes @@ -1558,58 +1689,70 @@ GPLv3  Tag Table: Node: Top225 -Node: Contents1689 -Node: Screenshots1812 -Node: Installation1930 -Node: Quelpa2426 -Node: Helm support2954 -Node: Usage3345 -Node: Commands3743 -Node: org-ql-search4216 -Node: helm-org-ql5866 -Node: org-ql-view6226 -Node: org-ql-view-sidebar6726 -Node: org-ql-view-recent-items7082 -Node: org-ql-sparse-tree7566 -Node: Queries8366 -Node: Non-sexp query syntax9477 -Node: General predicates11201 -Node: Ancestor/descendant predicates17422 -Node: Date/time predicates18550 -Node: Functions / Macros21638 -Node: Agenda-like views21936 -Node: Listing / acting-on results23341 -Node: Custom predicates28977 -Node: Dynamic block32636 -Node: Links35348 -Node: Tips36035 -Node: Changelog36353 -Node: 0637071 -Node: 05240070 -Node: 05140370 -Node: 0540787 -Node: 04942261 -Node: 04842535 -Node: 04742882 -Node: 04643277 -Node: 04543677 -Node: 04444036 -Node: 04344395 -Node: 04244592 -Node: 04144753 -Node: 0444994 -Node: 03248927 -Node: 03149306 -Node: 0349503 -Node: 02352478 -Node: 02252706 -Node: 02152974 -Node: 0253173 -Node: 0157208 -Node: Notes57309 -Node: Comparison with Org Agenda searches57471 -Node: org-sidebar58343 -Node: License58622 +Node: Contents1797 +Node: Screenshots1920 +Node: Installation2038 +Node: Quelpa2552 +Node: Helm support3080 +Node: Usage3483 +Node: Commands3881 +Node: org-ql-find4325 +Node: org-ql-refile4791 +Node: org-ql-search5114 +Node: helm-org-ql6810 +Node: org-ql-view7188 +Node: org-ql-view-sidebar7718 +Node: org-ql-view-recent-items8098 +Node: org-ql-sparse-tree8594 +Node: Queries9394 +Node: Non-sexp query syntax10511 +Node: General predicates12270 +Node: Ancestor/descendant predicates19142 +Node: Date/time predicates20270 +Node: Functions / Macros23394 +Node: Agenda-like views23692 +Ref: Function ‘org-ql-block’23854 +Node: Listing / acting-on results25115 +Ref: Caching25323 +Ref: Function ‘org-ql-select’26236 +Ref: Function ‘org-ql-query’28662 +Ref: Macro ‘org-ql’ (deprecated)30436 +Node: Custom predicates30751 +Ref: Macro ‘org-ql-defpred’30975 +Node: Dynamic block34416 +Node: Links37140 +Node: Tips37827 +Node: Changelog38151 +Node: 07-pre38934 +Node: 06341209 +Node: 06241744 +Node: 06142049 +Node: 0642617 +Node: 05245671 +Node: 05145971 +Node: 0546394 +Node: 04947923 +Node: 04848203 +Node: 04748552 +Node: 04648961 +Node: 04549369 +Node: 04449730 +Node: 04350089 +Node: 04250292 +Node: 04150453 +Node: 0450700 +Node: 03254801 +Node: 03155204 +Node: 0355401 +Node: 02358701 +Node: 02258935 +Node: 02159215 +Node: 0259420 +Node: 0163498 +Node: Notes63599 +Node: Comparison with Org Agenda searches63761 +Node: org-sidebar64650 +Node: License64929  End Tag Table diff --git a/tests/test-org-ql.el b/tests/test-org-ql.el index 0b7f2bb..25ac617 100644 --- a/tests/test-org-ql.el +++ b/tests/test-org-ql.el @@ -205,465 +205,556 @@ with keyword arg NOW in PLIST." (expect (org-ql--value-at (point-min) #'org-get-local-tags) :to-be nil)))) - (describe "Query functions/macros" - - (it "org-ql-select" - (expect (length (org-ql-select org-ql-test-buffer - '(category) - :sort 'deadline)) - :to-equal org-ql-test-num-headings)) - (it "org-ql-query" - (expect (length (org-ql-query :select 'element - :from org-ql-test-buffer - :where '(category) - :order-by 'date)) - :to-equal org-ql-test-num-headings))) - (describe "Query pre-processing" - (describe "(level)" - (it "with one level" - (expect (org-ql--normalize-query '(level "1")) - :to-equal '(level 1))) - (it "with two levels" - (expect (org-ql--normalize-query '(level "1" "2")) - :to-equal '(level 1 2))) - (it "with a comparator and a level" - (expect (org-ql--normalize-query '(level ">" "1")) - :to-equal '(level > 1)))) + (describe "Coalescing" + ;; NOTE: Queries are expected to be normalized before being coalesced, + ;; and `org-ql--normalize-query' calls `org-ql--coalesce-ands' as its + ;; final step, so in these tests we just call the former. + (it "coalesces a single AND clause that uses one predicate (and preserves argument order)" + (expect (org-ql--normalize-query '(and (rifle "foo") (rifle "bar"))) + :to-equal '(and (rifle :regexps '("foo" "bar"))))) + (it "coalesces a single AND clause that uses two predicates (and preserves predicate order)" + (expect (org-ql--normalize-query '(and (rifle "foo") (rifle "bar") + (heading "baz") (heading "buz"))) + :to-equal '(and (rifle :regexps '("foo" "bar")) (heading "baz" "buz")))) + (it "preserves independent OR clauses" + (expect (org-ql--normalize-query '(and (or (rifle "foo") (rifle "bar")) + (or (rifle "baz") (rifle "buz")))) + :to-equal '(and (or (rifle :regexps '("foo")) (rifle :regexps '("bar"))) (or (rifle :regexps '("baz")) (rifle :regexps '("buz")))))) + (it "coalesces an AND clause within an OR clause" + (expect (org-ql--normalize-query '(or (regexp "bar") (and (rifle "foo") (rifle "bar")))) + :to-equal '(or (regexp "bar") (and (rifle :regexps '("foo" "bar")))))) + (it "coalesces multiple AND clauses within an OR clause" + (expect (org-ql--normalize-query '(or (and (rifle "foo") (rifle "bar")) + (and (rifle "baz") (rifle "buz")))) + :to-equal '(or (and (rifle :regexps '("foo" "bar"))) (and (rifle :regexps '("baz" "buz")))))) + (it "coalesces arguments to predicates which use coalescing functions and whose calls are eligible for coalescing" + (expect (org-ql--normalize-query '(and (src "foo") (src "bar"))) + :to-equal '(and (src :lang nil :regexps '("foo" "bar")))) + (expect (org-ql--normalize-query '(and (src "foo" :lang "elisp") (src "bar" :lang "elisp"))) + :to-equal '(and (src :lang "elisp" :regexps '("foo" "bar"))))) + (it "does not coalesce arguments to predicates which use coalescing functions and whose calls are ineligible for coalescing" + (expect (org-ql--normalize-query '(and (src "foo" :lang "elisp") (src "bar"))) + ;; NOTE: The current implementation of `org-ql--normalize-query' + ;; reorders clauses in this case. Fixing that would probably + ;; not be worth the effort in code or runtime. + :to-equal '(and (src :regexps '("bar")) (src :lang "elisp" :regexps '("foo")))) + (expect (org-ql--normalize-query '(and (src "foo" :lang "elisp") (src "bar" :lang "python"))) + :to-equal '(and (src :lang "python" :regexps '("bar")) (src :lang "elisp" :regexps '("foo")))))) - (describe "(link)" - (it "with one argument" - (expect (org-ql--normalize-query '(link "DESC-OR-TARGET")) - :to-equal '(link "DESC-OR-TARGET"))) - (it "with one argument and :regexp-p" - (expect (org-ql--normalize-query '(link "DESC-OR-TARGET" :regexp-p t)) - :to-equal '(link "DESC-OR-TARGET" :regexp-p t))) - (it "with keyword arguments" - (expect (org-ql--normalize-query '(link :description "DESCRIPTION" :target "TARGET" - :regexp-p t)) - :to-equal '(link :description "DESCRIPTION" :target "TARGET" - :regexp-p t)))) + (describe "Normalization" - (describe "(outline-path)" - (it "with a regexp metacharacter" - ;; Ensures that normalizer doesn't infinitely loop. - (expect (org-ql--normalize-query '(olp "a." "b")) - :to-equal '(org-ql--predicate-outline-path "a\\." "b")))) + (it "Default predicate" + (expect (org-ql--normalize-query "scheduled") + ;; No colon after keyword, so not a predicate query. + :to-equal '(rifle :regexps '("scheduled"))) + (expect (org-ql--normalize-query "\"quoted phrase\"") + :to-equal '(rifle :regexps '("\"quoted phrase\"")))) - (describe "(tags-inherited)" - (expect (org-ql--normalize-query '(tags-inherited)) - :to-equal '(tags-inherited)) - (expect (org-ql--normalize-query '(itags)) - :to-equal '(tags-inherited)) - (expect (org-ql--normalize-query '(tags-inherited "foo")) - :to-equal '(tags-inherited "foo")) - (expect (org-ql--normalize-query '(itags "foo")) - :to-equal '(tags-inherited "foo")) - (expect (org-ql--normalize-query '(tags-inherited "foo" "bar")) - :to-equal '(tags-inherited "foo" "bar")) - (expect (org-ql--normalize-query '(itags "foo" "bar")) - :to-equal '(tags-inherited "foo" "bar"))) + (describe "Plain strings" + (it "normalizes plain strings to the default predicate (using AND)" + (expect (org-ql--normalize-query '(and "string1" "string2")) + :to-equal '(and (rifle :regexps '("string1" "string2"))))) + (it "normalizes plain strings to the default predicate (using OR)" + (expect (org-ql--normalize-query '(or "string1" "string2")) + :to-equal '(or (rifle :regexps '("string1")) (rifle :regexps '("string2"))))) + (it "normalizes plain strings within sub-expressions to the default predicate" + (expect (org-ql--normalize-query '(and (todo "TODO") + (or "string1" "string2"))) + :to-equal '(and (todo "TODO") (or (rifle :regexps '("string1")) (rifle :regexps '("string2"))))) + (expect (org-ql--normalize-query '(when (todo "TODO") + (or "string1" "string2"))) + :to-equal '(when (todo "TODO") (or (rifle :regexps '("string1")) (rifle :regexps '("string2"))))) + (expect (org-ql--normalize-query '(when "string-cond1" + (or "string1" "string2"))) + :to-equal '(when (rifle :regexps '("string-cond1")) (or (rifle :regexps '("string1")) (rifle :regexps '("string2"))))) + (expect (org-ql--normalize-query '(when (and "string-cond1" "string-cond2") + (or "string1" "string2"))) + :to-equal '(when (and (rifle :regexps '("string-cond1")) (rifle :regexps '("string-cond2"))) (or (rifle :regexps '("string1")) (rifle :regexps '("string2"))))) + (expect (org-ql--normalize-query '(unless (and "stringcondition1" "stringcond2") + (or "string1" "string2"))) + :to-equal '(unless (and (rifle :regexps '("stringcondition1")) (rifle :regexps '("stringcond2"))) (or (rifle :regexps '("string1")) (rifle :regexps '("string2"))))))) - (describe "timestamp predicates" - ;; NOTE: (clocked) and (closed) don't accept :with-time arguments. - (describe "(clocked)" - (let ((beg-of-today-ts (->> (ts-now) - (ts-apply :hour 0 :minute 0 :second 0))) - (end-of-today-ts (->> (ts-now) - (ts-apply :hour 23 :minute 59 :second 59)))) - ;; MAYBE: While it seems helpful for (clocked) and (close) to - ;; implicitly look into the past (because entries can't be - ;; clocked or closed in the future), it makes the API - ;; inconsistent. It would be better to be consistent and - ;; require the user to pass these predicates a negative number. - (it "with a number" - (expect (org-ql--normalize-query '(clocked 0)) - :to-equal `(clocked :from ,beg-of-today-ts)) - (expect (org-ql--normalize-query '(clocked 1)) - :to-equal `(clocked :from ,(ts-inc 'day -1 beg-of-today-ts)))) - (it ":from/:to/:on" - (expect (org-ql--normalize-query '(clocked :on today)) - :to-equal `(clocked :from ,beg-of-today-ts :to ,end-of-today-ts)) - (expect (org-ql--normalize-query '(clocked :on 0)) - :to-equal `(clocked :from ,beg-of-today-ts :to ,end-of-today-ts)) - (expect (org-ql--normalize-query '(clocked :on 1)) - :to-equal `(clocked :from ,(ts-inc 'day 1 beg-of-today-ts) - :to ,(ts-inc 'day 1 end-of-today-ts)))))) - (describe "(closed)" - (let ((beg-of-today-ts (->> (ts-now) - (ts-apply :hour 0 :minute 0 :second 0))) - (end-of-today-ts (->> (ts-now) - (ts-apply :hour 23 :minute 59 :second 59)))) - (it "with a number" - (expect (org-ql--normalize-query '(closed 0)) - :to-equal `(closed :from ,beg-of-today-ts)) - (expect (org-ql--normalize-query '(closed 1)) - :to-equal `(closed :from ,(ts-inc 'day -1 beg-of-today-ts)))) - (it ":from/:to/:on" - (expect (org-ql--normalize-query '(closed :on today)) - :to-equal `(closed :from ,beg-of-today-ts :to ,end-of-today-ts)) - (expect (org-ql--normalize-query '(closed :on 0)) - :to-equal `(closed :from ,beg-of-today-ts :to ,end-of-today-ts)) - (expect (org-ql--normalize-query '(closed :on 1)) - :to-equal `(closed :from ,(ts-inc 'day 1 beg-of-today-ts) - :to ,(ts-inc 'day 1 end-of-today-ts)))))) + (describe "(rifle)" + (it "with one argument" + (expect (org-ql--normalize-query '(rifle "foo.")) + :to-equal '(rifle :regexps '("foo\\.")))) + (it "with two arguments" + (expect (org-ql--normalize-query '(rifle "foo." "bar")) + :to-equal '(rifle :regexps '("foo\\." "bar"))))) - ;; NOTE: The rest of them do accept :with-time. - (describe "(deadline)" - (let ((beg-of-today-ts (->> (ts-now) - (ts-apply :hour 0 :minute 0 :second 0))) - (end-of-today-ts (->> (ts-now) - (ts-apply :hour 23 :minute 59 :second 59)))) - (it "with auto" - (let ((auto-ts (->> (ts-now) - (ts-adjust 'day org-deadline-warning-days) - (ts-apply :hour 23 :minute 59 :second 59)))) - (expect (org-ql--normalize-query '(deadline auto)) - :to-equal `(deadline-warning :to ,auto-ts)) - (expect (org-ql--normalize-query '(deadline auto :with-time t)) - :to-equal `(deadline-warning :to ,auto-ts :with-time t)) - (expect (org-ql--normalize-query '(deadline auto :with-time nil)) - :to-equal `(deadline-warning :to ,auto-ts :with-time nil)))) - (it "with a number" - (expect (org-ql--normalize-query '(deadline 0)) - :to-equal `(deadline :to ,end-of-today-ts)) - (expect (org-ql--normalize-query '(deadline 0 :with-time t)) - :to-equal `(deadline :to ,end-of-today-ts :with-time t)) - (expect (org-ql--normalize-query '(deadline 0 :with-time nil)) - :to-equal `(deadline :to ,end-of-today-ts :with-time nil)) - (expect (org-ql--normalize-query '(deadline 2)) - :to-equal `(deadline :to ,(ts-inc 'day 2 end-of-today-ts))) - (expect (org-ql--normalize-query '(deadline 2 :with-time nil)) - :to-equal `(deadline :to ,(ts-inc 'day 2 end-of-today-ts) - :with-time nil)) - (expect (org-ql--normalize-query '(deadline 2 :with-time t)) - :to-equal `(deadline :to ,(ts-inc 'day 2 end-of-today-ts) - :with-time t))) - (it ":from/:to/:on" - (expect (org-ql--normalize-query '(deadline :on today)) - :to-equal `(deadline :from ,beg-of-today-ts :to ,end-of-today-ts)) - (expect (org-ql--normalize-query '(deadline :on today :with-time t)) - :to-equal `(deadline :from ,beg-of-today-ts :to ,end-of-today-ts - :with-time t)) - (expect (org-ql--normalize-query '(deadline :on today :with-time nil)) - :to-equal `(deadline :from ,beg-of-today-ts :to ,end-of-today-ts - :with-time nil)) - (expect (org-ql--normalize-query '(deadline :on 0)) - :to-equal `(deadline :from ,beg-of-today-ts :to ,end-of-today-ts)) - (expect (org-ql--normalize-query '(deadline :on 0 :with-time t)) - :to-equal `(deadline :from ,beg-of-today-ts :to ,end-of-today-ts - :with-time t)) - (expect (org-ql--normalize-query '(deadline :on 1)) - :to-equal `(deadline :from ,(ts-inc 'day 1 beg-of-today-ts) - :to ,(ts-inc 'day 1 end-of-today-ts)))))) - (describe "(scheduled)" - (let ((beg-of-today-ts (->> (ts-now) - (ts-apply :hour 0 :minute 0 :second 0))) - (end-of-today-ts (->> (ts-now) - (ts-apply :hour 23 :minute 59 :second 59)))) - (it "with a number" - (expect (org-ql--normalize-query '(scheduled 0)) - :to-equal `(scheduled :from ,beg-of-today-ts :to ,end-of-today-ts)) - (expect (org-ql--normalize-query '(scheduled 0 :with-time t)) - :to-equal `(scheduled :from ,beg-of-today-ts :to ,end-of-today-ts - :with-time t)) - (expect (org-ql--normalize-query '(scheduled 0 :with-time nil)) - :to-equal `(scheduled :from ,beg-of-today-ts :to ,end-of-today-ts - :with-time nil)) - (expect (org-ql--normalize-query '(scheduled 1)) - :to-equal `(scheduled :from ,(ts-inc 'day 1 beg-of-today-ts) - :to ,(ts-inc 'day 1 end-of-today-ts)))) - (it ":from/:to/:on" - (expect (org-ql--normalize-query '(scheduled :on today)) - :to-equal `(scheduled :from ,beg-of-today-ts :to ,end-of-today-ts)) - (expect (org-ql--normalize-query '(scheduled :on today :with-time t)) - :to-equal `(scheduled :from ,beg-of-today-ts :to ,end-of-today-ts - :with-time t)) - (expect (org-ql--normalize-query '(scheduled :on today :with-time nil)) - :to-equal `(scheduled :from ,beg-of-today-ts :to ,end-of-today-ts - :with-time nil)) - (expect (org-ql--normalize-query '(scheduled :on 0)) - :to-equal `(scheduled :from ,beg-of-today-ts :to ,end-of-today-ts)) - (expect (org-ql--normalize-query '(scheduled :on 0 :with-time t)) - :to-equal `(scheduled :from ,beg-of-today-ts :to ,end-of-today-ts - :with-time t)) - (expect (org-ql--normalize-query '(scheduled :on 1)) - :to-equal `(scheduled :from ,(ts-inc 'day 1 beg-of-today-ts) + (describe "(level)" + (it "with one level" + (expect (org-ql--normalize-query '(level "1")) + :to-equal '(level 1))) + (it "with two levels" + (expect (org-ql--normalize-query '(level "1" "2")) + :to-equal '(level 1 2))) + (it "with a comparator and a level" + (expect (org-ql--normalize-query '(level ">" "1")) + :to-equal '(level > 1)))) + + (describe "(link)" + (it "with one argument" + (expect (org-ql--normalize-query '(link "DESC-OR-TARGET")) + :to-equal '(link "DESC-OR-TARGET"))) + (it "with one argument and :regexp-p" + (expect (org-ql--normalize-query '(link "DESC-OR-TARGET" :regexp-p t)) + :to-equal '(link "DESC-OR-TARGET" :regexp-p t))) + (it "with keyword arguments" + (expect (org-ql--normalize-query '(link :description "DESCRIPTION" :target "TARGET" + :regexp-p t)) + :to-equal '(link :description "DESCRIPTION" :target "TARGET" + :regexp-p t)))) + + (describe "(outline-path)" + (it "with a regexp metacharacter" + ;; Ensures that normalizer doesn't infinitely loop. + (expect (org-ql--normalize-query '(olp "a." "b")) + :to-equal '(org-ql--predicate-outline-path "a\\." "b")))) + + (describe "(src)" + (it "normalizes a non-keyword arg to keywords" + (expect (org-ql--normalize-query '(src "foo")) + :to-equal '(src :regexps '("foo")))) + (it "normalizes non-keyword args to keywords" + (expect (org-ql--normalize-query '(src "foo" "bar")) + :to-equal '(src :regexps '("foo" "bar")))) + (it "normalizes a non-keyword arg with a :lang keyword arg to keywords" + (expect (org-ql--normalize-query '(src "foo" :lang "bar")) + :to-equal '(src :lang "bar" :regexps '("foo")))) + (it "normalizes non-keyword args with a :lang keyword arg to keywords" + (expect (org-ql--normalize-query '(src "foo" "bar" :lang "baz")) + :to-equal '(src :lang "baz" :regexps '("foo" "bar")))) + (it "normalizes all-keyword args without looping" + (expect (org-ql--normalize-query '(src :regexps ("foo") :lang "bar")) + :to-equal '(src :lang "bar" :regexps '("foo"))) + (expect (org-ql--normalize-query '(src :regexps ("foo") :lang)) + :to-equal '(src :regexps '("foo"))))) + + (describe "(tags-inherited)" + (it "handles 0 arguments" + (expect (org-ql--normalize-query '(tags-inherited)) + :to-equal '(tags-inherited))) + (it "handles 1 argument" + (expect (org-ql--normalize-query '(tags-inherited "foo")) + :to-equal '(tags-inherited "foo"))) + (it "handles 2 arguments" + (expect (org-ql--normalize-query '(tags-inherited "foo" "bar")) + :to-equal '(tags-inherited "foo" "bar"))) + (it "aliases to `itags'" + (expect (org-ql--normalize-query '(itags)) + :to-equal '(tags-inherited))) + (it "aliases to `itags' with one argument" + (expect (org-ql--normalize-query '(itags "foo")) + :to-equal '(tags-inherited "foo"))) + (it "aliases to `itags' with two arguments" + (expect (org-ql--normalize-query '(itags "foo" "bar")) + :to-equal '(tags-inherited "foo" "bar")))) + + (describe "timestamp predicates" + ;; NOTE: (clocked) and (closed) don't accept :with-time arguments. + (describe "(clocked)" + (let ((beg-of-today-ts (->> (ts-now) + (ts-apply :hour 0 :minute 0 :second 0))) + (end-of-today-ts (->> (ts-now) + (ts-apply :hour 23 :minute 59 :second 59)))) + ;; MAYBE: While it seems helpful for (clocked) and (close) to + ;; implicitly look into the past (because entries can't be + ;; clocked or closed in the future), it makes the API + ;; inconsistent. It would be better to be consistent and + ;; require the user to pass these predicates a negative number. + (it "with a number" + (expect (org-ql--normalize-query '(clocked 0)) + :to-equal `(clocked :from ,beg-of-today-ts)) + (expect (org-ql--normalize-query '(clocked 1)) + :to-equal `(clocked :from ,(ts-inc 'day -1 beg-of-today-ts)))) + (it ":from/:to/:on" + (expect (org-ql--normalize-query '(clocked :on today)) + :to-equal `(clocked :from ,beg-of-today-ts :to ,end-of-today-ts)) + (expect (org-ql--normalize-query '(clocked :on 0)) + :to-equal `(clocked :from ,beg-of-today-ts :to ,end-of-today-ts)) + (expect (org-ql--normalize-query '(clocked :on 1)) + :to-equal `(clocked :from ,(ts-inc 'day 1 beg-of-today-ts) :to ,(ts-inc 'day 1 end-of-today-ts)))))) - (describe "(planning)" - (let ((beg-of-today-ts (->> (ts-now) - (ts-apply :hour 0 :minute 0 :second 0))) - (end-of-today-ts (->> (ts-now) - (ts-apply :hour 23 :minute 59 :second 59)))) - (it "with a number" - (expect (org-ql--normalize-query '(planning 0)) - :to-equal `(planning :to ,end-of-today-ts)) - (expect (org-ql--normalize-query '(planning 0 :with-time t)) - :to-equal `(planning :to ,end-of-today-ts :with-time t)) - (expect (org-ql--normalize-query '(planning 0 :with-time nil)) - :to-equal `(planning :to ,end-of-today-ts :with-time nil)) - (expect (org-ql--normalize-query '(planning 1)) - :to-equal `(planning :to ,(ts-inc 'day 1 end-of-today-ts)))) - (it ":from/:to/:on" - (expect (org-ql--normalize-query '(planning :on today)) - :to-equal `(planning :from ,beg-of-today-ts :to ,end-of-today-ts)) - (expect (org-ql--normalize-query '(planning :on today :with-time t)) - :to-equal `(planning :from ,beg-of-today-ts :to ,end-of-today-ts - :with-time t)) - (expect (org-ql--normalize-query '(planning :on today :with-time nil)) - :to-equal `(planning :from ,beg-of-today-ts :to ,end-of-today-ts - :with-time nil)) - (expect (org-ql--normalize-query '(planning :on 0)) - :to-equal `(planning :from ,beg-of-today-ts :to ,end-of-today-ts)) - (expect (org-ql--normalize-query '(planning :on 0 :with-time t)) - :to-equal `(planning :from ,beg-of-today-ts :to ,end-of-today-ts - :with-time t)) - (expect (org-ql--normalize-query '(planning :on 1)) - :to-equal `(planning :from ,(ts-inc 'day 1 beg-of-today-ts) + (describe "(closed)" + (let ((beg-of-today-ts (->> (ts-now) + (ts-apply :hour 0 :minute 0 :second 0))) + (end-of-today-ts (->> (ts-now) + (ts-apply :hour 23 :minute 59 :second 59)))) + (it "with a number" + (expect (org-ql--normalize-query '(closed 0)) + :to-equal `(closed :from ,beg-of-today-ts)) + (expect (org-ql--normalize-query '(closed 1)) + :to-equal `(closed :from ,(ts-inc 'day -1 beg-of-today-ts)))) + (it ":from/:to/:on" + (expect (org-ql--normalize-query '(closed :on today)) + :to-equal `(closed :from ,beg-of-today-ts :to ,end-of-today-ts)) + (expect (org-ql--normalize-query '(closed :on 0)) + :to-equal `(closed :from ,beg-of-today-ts :to ,end-of-today-ts)) + (expect (org-ql--normalize-query '(closed :on 1)) + :to-equal `(closed :from ,(ts-inc 'day 1 beg-of-today-ts) :to ,(ts-inc 'day 1 end-of-today-ts)))))) - (describe "(ts)" - (let ((beg-of-today-ts (->> (ts-now) - (ts-apply :hour 0 :minute 0 :second 0))) - (end-of-today-ts (->> (ts-now) - (ts-apply :hour 23 :minute 59 :second 59)))) - (it "with a number" - ;; NOTE: For consistency with the other ts-related predicates, a number argument means ":to NUMBER". - ;; MAYBE: Mention this in docs. Or remove this, because it's ambiguous. - (expect (org-ql--normalize-query '(ts 0)) - :to-equal `(ts :to ,end-of-today-ts)) - (expect (org-ql--normalize-query '(ts 0 :with-time t)) - :to-equal `(ts :to ,end-of-today-ts :with-time t)) - (expect (org-ql--normalize-query '(ts 0 :with-time nil)) - :to-equal `(ts :to ,end-of-today-ts :with-time nil)) - (expect (org-ql--normalize-query '(ts 1)) - :to-equal `(ts :to ,(ts-inc 'day 1 end-of-today-ts)))) - (it ":from/:to/:on" - (expect (org-ql--normalize-query '(ts :on today)) - :to-equal `(ts :from ,beg-of-today-ts :to ,end-of-today-ts)) - (expect (org-ql--normalize-query '(ts :on today :with-time t)) - :to-equal `(ts :from ,beg-of-today-ts :to ,end-of-today-ts - :with-time t)) - (expect (org-ql--normalize-query '(ts :on today :with-time nil)) - :to-equal `(ts :from ,beg-of-today-ts :to ,end-of-today-ts - :with-time nil)) - (expect (org-ql--normalize-query '(ts :on 0)) - :to-equal `(ts :from ,beg-of-today-ts :to ,end-of-today-ts)) - (expect (org-ql--normalize-query '(ts :on 0 :with-time t)) - :to-equal `(ts :from ,beg-of-today-ts :to ,end-of-today-ts - :with-time t)) - (expect (org-ql--normalize-query '(ts :on 1)) - :to-equal `(ts :from ,(ts-inc 'day 1 beg-of-today-ts) - :to ,(ts-inc 'day 1 end-of-today-ts))))))) - (expect (org-ql--normalize-query '(and "string1" "string2")) - :to-equal '(and (regexp "string1") (regexp "string2"))) - (expect (org-ql--normalize-query '(or "string1" "string2")) - :to-equal '(or (regexp "string1") (regexp "string2"))) - (expect (org-ql--normalize-query '(and (todo "TODO") - (or "string1" "string2"))) - :to-equal '(and (todo "TODO") (or (regexp "string1") (regexp "string2")))) - (expect (org-ql--normalize-query '(when (todo "TODO") - (or "string1" "string2"))) - :to-equal '(when (todo "TODO") (or (regexp "string1") (regexp "string2")))) - (expect (org-ql--normalize-query '(when "string-cond1" - (or "string1" "string2"))) - :to-equal '(when (regexp "string-cond1") (or (regexp "string1") (regexp "string2")))) - (expect (org-ql--normalize-query '(when (and "string-cond1" "string-cond2") - (or "string1" "string2"))) - :to-equal '(when (and (regexp "string-cond1") (regexp "string-cond2")) (or (regexp "string1") (regexp "string2")))) - (expect (org-ql--normalize-query '(unless (and "stringcondition1" "stringcond2") - (or "string1" "string2"))) - :to-equal '(unless (and (regexp "stringcondition1") (regexp "stringcond2")) (or (regexp "string1") (regexp "string2")))) + ;; NOTE: The rest of them do accept :with-time. + (describe "(deadline)" + (let ((beg-of-today-ts (->> (ts-now) + (ts-apply :hour 0 :minute 0 :second 0))) + (end-of-today-ts (->> (ts-now) + (ts-apply :hour 23 :minute 59 :second 59)))) + (it "with auto" + (let ((auto-ts (->> (ts-now) + (ts-adjust 'day org-deadline-warning-days) + (ts-apply :hour 23 :minute 59 :second 59)))) + (expect (org-ql--normalize-query '(deadline auto)) + :to-equal `(deadline-warning :to ,auto-ts)) + (expect (org-ql--normalize-query '(deadline auto :with-time t)) + :to-equal `(deadline-warning :to ,auto-ts :with-time t)) + (expect (org-ql--normalize-query '(deadline auto :with-time nil)) + :to-equal `(deadline-warning :to ,auto-ts :with-time nil)))) + (it "with a number" + (expect (org-ql--normalize-query '(deadline 0)) + :to-equal `(deadline :to ,end-of-today-ts)) + (expect (org-ql--normalize-query '(deadline 0 :with-time t)) + :to-equal `(deadline :to ,end-of-today-ts :with-time t)) + (expect (org-ql--normalize-query '(deadline 0 :with-time nil)) + :to-equal `(deadline :to ,end-of-today-ts :with-time nil)) + (expect (org-ql--normalize-query '(deadline 2)) + :to-equal `(deadline :to ,(ts-inc 'day 2 end-of-today-ts))) + (expect (org-ql--normalize-query '(deadline 2 :with-time nil)) + :to-equal `(deadline :to ,(ts-inc 'day 2 end-of-today-ts) + :with-time nil)) + (expect (org-ql--normalize-query '(deadline 2 :with-time t)) + :to-equal `(deadline :to ,(ts-inc 'day 2 end-of-today-ts) + :with-time t))) + (it ":from/:to/:on" + (expect (org-ql--normalize-query '(deadline :on today)) + :to-equal `(deadline :from ,beg-of-today-ts :to ,end-of-today-ts)) + (expect (org-ql--normalize-query '(deadline :on today :with-time t)) + :to-equal `(deadline :from ,beg-of-today-ts :to ,end-of-today-ts + :with-time t)) + (expect (org-ql--normalize-query '(deadline :on today :with-time nil)) + :to-equal `(deadline :from ,beg-of-today-ts :to ,end-of-today-ts + :with-time nil)) + (expect (org-ql--normalize-query '(deadline :on 0)) + :to-equal `(deadline :from ,beg-of-today-ts :to ,end-of-today-ts)) + (expect (org-ql--normalize-query '(deadline :on 0 :with-time t)) + :to-equal `(deadline :from ,beg-of-today-ts :to ,end-of-today-ts + :with-time t)) + (expect (org-ql--normalize-query '(deadline :on 1)) + :to-equal `(deadline :from ,(ts-inc 'day 1 beg-of-today-ts) + :to ,(ts-inc 'day 1 end-of-today-ts)))))) + (describe "(scheduled)" + (let ((beg-of-today-ts (->> (ts-now) + (ts-apply :hour 0 :minute 0 :second 0))) + (end-of-today-ts (->> (ts-now) + (ts-apply :hour 23 :minute 59 :second 59)))) + (it "with a number" + (expect (org-ql--normalize-query '(scheduled 0)) + :to-equal `(scheduled :from ,beg-of-today-ts :to ,end-of-today-ts)) + (expect (org-ql--normalize-query '(scheduled 0 :with-time t)) + :to-equal `(scheduled :from ,beg-of-today-ts :to ,end-of-today-ts + :with-time t)) + (expect (org-ql--normalize-query '(scheduled 0 :with-time nil)) + :to-equal `(scheduled :from ,beg-of-today-ts :to ,end-of-today-ts + :with-time nil)) + (expect (org-ql--normalize-query '(scheduled 1)) + :to-equal `(scheduled :from ,(ts-inc 'day 1 beg-of-today-ts) + :to ,(ts-inc 'day 1 end-of-today-ts)))) + (it ":from/:to/:on" + (expect (org-ql--normalize-query '(scheduled :on today)) + :to-equal `(scheduled :from ,beg-of-today-ts :to ,end-of-today-ts)) + (expect (org-ql--normalize-query '(scheduled :on today :with-time t)) + :to-equal `(scheduled :from ,beg-of-today-ts :to ,end-of-today-ts + :with-time t)) + (expect (org-ql--normalize-query '(scheduled :on today :with-time nil)) + :to-equal `(scheduled :from ,beg-of-today-ts :to ,end-of-today-ts + :with-time nil)) + (expect (org-ql--normalize-query '(scheduled :on 0)) + :to-equal `(scheduled :from ,beg-of-today-ts :to ,end-of-today-ts)) + (expect (org-ql--normalize-query '(scheduled :on 0 :with-time t)) + :to-equal `(scheduled :from ,beg-of-today-ts :to ,end-of-today-ts + :with-time t)) + (expect (org-ql--normalize-query '(scheduled :on 1)) + :to-equal `(scheduled :from ,(ts-inc 'day 1 beg-of-today-ts) + :to ,(ts-inc 'day 1 end-of-today-ts)))))) + (describe "(planning)" + (let ((beg-of-today-ts (->> (ts-now) + (ts-apply :hour 0 :minute 0 :second 0))) + (end-of-today-ts (->> (ts-now) + (ts-apply :hour 23 :minute 59 :second 59)))) + (it "with a number" + (expect (org-ql--normalize-query '(planning 0)) + :to-equal `(planning :to ,end-of-today-ts)) + (expect (org-ql--normalize-query '(planning 0 :with-time t)) + :to-equal `(planning :to ,end-of-today-ts :with-time t)) + (expect (org-ql--normalize-query '(planning 0 :with-time nil)) + :to-equal `(planning :to ,end-of-today-ts :with-time nil)) + (expect (org-ql--normalize-query '(planning 1)) + :to-equal `(planning :to ,(ts-inc 'day 1 end-of-today-ts)))) + (it ":from/:to/:on" + (expect (org-ql--normalize-query '(planning :on today)) + :to-equal `(planning :from ,beg-of-today-ts :to ,end-of-today-ts)) + (expect (org-ql--normalize-query '(planning :on today :with-time t)) + :to-equal `(planning :from ,beg-of-today-ts :to ,end-of-today-ts + :with-time t)) + (expect (org-ql--normalize-query '(planning :on today :with-time nil)) + :to-equal `(planning :from ,beg-of-today-ts :to ,end-of-today-ts + :with-time nil)) + (expect (org-ql--normalize-query '(planning :on 0)) + :to-equal `(planning :from ,beg-of-today-ts :to ,end-of-today-ts)) + (expect (org-ql--normalize-query '(planning :on 0 :with-time t)) + :to-equal `(planning :from ,beg-of-today-ts :to ,end-of-today-ts + :with-time t)) + (expect (org-ql--normalize-query '(planning :on 1)) + :to-equal `(planning :from ,(ts-inc 'day 1 beg-of-today-ts) + :to ,(ts-inc 'day 1 end-of-today-ts)))))) + (describe "(ts)" + (let ((beg-of-today-ts (->> (ts-now) + (ts-apply :hour 0 :minute 0 :second 0))) + (end-of-today-ts (->> (ts-now) + (ts-apply :hour 23 :minute 59 :second 59)))) + (it "with a number" + ;; NOTE: For consistency with the other ts-related predicates, a number argument means ":to NUMBER". + ;; MAYBE: Mention this in docs. Or remove this, because it's ambiguous. + (expect (org-ql--normalize-query '(ts 0)) + :to-equal `(ts :to ,end-of-today-ts)) + (expect (org-ql--normalize-query '(ts 0 :with-time t)) + :to-equal `(ts :to ,end-of-today-ts :with-time t)) + (expect (org-ql--normalize-query '(ts 0 :with-time nil)) + :to-equal `(ts :to ,end-of-today-ts :with-time nil)) + (expect (org-ql--normalize-query '(ts 1)) + :to-equal `(ts :to ,(ts-inc 'day 1 end-of-today-ts)))) + (it ":from/:to/:on" + (expect (org-ql--normalize-query '(ts :on today)) + :to-equal `(ts :from ,beg-of-today-ts :to ,end-of-today-ts)) + (expect (org-ql--normalize-query '(ts :on today :with-time t)) + :to-equal `(ts :from ,beg-of-today-ts :to ,end-of-today-ts + :with-time t)) + (expect (org-ql--normalize-query '(ts :on today :with-time nil)) + :to-equal `(ts :from ,beg-of-today-ts :to ,end-of-today-ts + :with-time nil)) + (expect (org-ql--normalize-query '(ts :on 0)) + :to-equal `(ts :from ,beg-of-today-ts :to ,end-of-today-ts)) + (expect (org-ql--normalize-query '(ts :on 0 :with-time t)) + :to-equal `(ts :from ,beg-of-today-ts :to ,end-of-today-ts + :with-time t)) + (expect (org-ql--normalize-query '(ts :on 1)) + :to-equal `(ts :from ,(ts-inc 'day 1 beg-of-today-ts) + :to ,(ts-inc 'day 1 end-of-today-ts))) - ;; FIXME: This test fails, but only on GitHub CI; it works fine - ;; locally. It seems to be something to do with Buttercup and - ;; comparing the structs. It only started happening after I moved - ;; the predicate argument processing into the newer - ;; --normalize-from-to-on macro. I can't explain why it works - ;; fine locally, even in a clean sandbox with newly installed - ;; packages, yet fails entirely on GitHub CI. The only - ;; possibility I can think of might be a difference in how the - ;; Emacs being installed into CI is built, but that seems very - ;; unlikely, so I don't know. For now, I have no alternative but - ;; to comment out the test. But it doesn't matter much, anyway, - ;; because I know it works properly. + ;; FIXME: This test fails, but only on GitHub CI; it works fine + ;; locally. It seems to be something to do with Buttercup and + ;; comparing the structs. It only started happening after I moved + ;; the predicate argument processing into the newer + ;; --normalize-from-to-on macro. I can't explain why it works + ;; fine locally, even in a clean sandbox with newly installed + ;; packages, yet fails entirely on GitHub CI. The only + ;; possibility I can think of might be a difference in how the + ;; Emacs being installed into CI is built, but that seems very + ;; unlikely, so I don't know. For now, I have no alternative but + ;; to comment out the test. But it doesn't matter much, anyway, + ;; because I know it works properly. - ;; (expect (org-ql--normalize-query '(or (ts-active :on "2019-01-01") - ;; (ts-a :on "2019-01-01") - ;; (ts-inactive :on "2019-01-01") - ;; (ts-i :on "2019-01-01"))) - ;; :to-equal `(or (ts :type active - ;; :from ,(make-ts :unix 1546322400.0) - ;; :to ,(make-ts :unix 1546408799.0)) - ;; (ts :type active - ;; :from ,(make-ts :unix 1546322400.0) - ;; :to ,(make-ts :unix 1546408799.0)) - ;; (ts :type inactive - ;; :from ,(make-ts :unix 1546322400.0) - ;; :to ,(make-ts :unix 1546408799.0)) - ;; (ts :type inactive - ;; :from ,(make-ts :unix 1546322400.0) - ;; :to ,(make-ts :unix 1546408799.0)))) - ) + ;; (expect (org-ql--normalize-query '(or (ts-active :on "2019-01-01") + ;; (ts-a :on "2019-01-01") + ;; (ts-inactive :on "2019-01-01") + ;; (ts-i :on "2019-01-01"))) + ;; :to-equal `(or (ts :type active + ;; :from ,(make-ts :unix 1546322400.0) + ;; :to ,(make-ts :unix 1546408799.0)) + ;; (ts :type active + ;; :from ,(make-ts :unix 1546322400.0) + ;; :to ,(make-ts :unix 1546408799.0)) + ;; (ts :type inactive + ;; :from ,(make-ts :unix 1546322400.0) + ;; :to ,(make-ts :unix 1546408799.0)) + ;; (ts :type inactive + ;; :from ,(make-ts :unix 1546322400.0) + ;; :to ,(make-ts :unix 1546408799.0))))) + ))))) - (describe "Query preambles" + (describe "Query preambles" - ;; TODO: Other predicates. + ;; TODO: Other predicates. - (describe "(clocked)" - (it "without arguments" - (expect (org-ql--query-preamble '(clocked)) - :to-equal (list :query t - :preamble org-ql-clock-regexp - :preamble-case-fold nil))) - (it "with a number of days" - (expect (org-ql--query-preamble '(clocked 1)) - :to-equal (list :query t - :preamble org-ql-clock-regexp - :preamble-case-fold nil))) - ;; TODO: Other arguments for (clocked). - ) + (describe "(rifle)" + (it "with one argument" + (expect (org-ql--query-preamble (org-ql--normalize-query '(rifle "foo."))) + :to-equal (list :query '(rifle :regexps '("foo\\.")) + :preamble "\\(?:\\<\\(?:foo\\.\\)\\)" + :preamble-case-fold t))) + (it "with two arguments" + (expect (org-ql--query-preamble (org-ql--normalize-query '(rifle "foo." "bar"))) + :to-equal (list :query '(rifle :regexps '("foo\\." "bar")) + :preamble "\\(?:\\<\\(?:foo\\.\\|bar\\)\\)" + :preamble-case-fold t)))) - (describe "(level)" - (it "with a number" - (expect (org-ql--query-preamble '(level 2)) - :to-equal (list :query t - :preamble (rx bol (repeat 2 "*") " ") - :preamble-case-fold t))) - (it "with two numbers" - (expect (org-ql--query-preamble '(level 2 4)) - :to-equal (list :query t - :preamble (rx bol (repeat 2 4 "*") " ") - :preamble-case-fold t))) - (it "<" - (expect (org-ql--query-preamble '(level < 3)) - :to-equal (list :query t - :preamble (rx bol (repeat 1 2 "*") " ") - :preamble-case-fold t))) - (it "<=" - (expect (org-ql--query-preamble '(level <= 2)) - :to-equal (list :query t - :preamble (rx bol (repeat 1 2 "*") " ") - :preamble-case-fold t))) - (it ">" - (expect (org-ql--query-preamble '(level > 2)) - :to-equal (list :query t - :preamble (rx bol (>= 3 "*") " ") - :preamble-case-fold t))) - (it ">=" - (expect (org-ql--query-preamble '(level >= 2)) - :to-equal (list :query t - :preamble (rx bol (>= 2 "*") " ") - :preamble-case-fold t))))) + (describe "(clocked)" + (it "without arguments" + (expect (org-ql--query-preamble '(clocked)) + :to-equal (list :query t + :preamble org-ql-clock-regexp + :preamble-case-fold nil))) + (it "with a number of days" + (expect (org-ql--query-preamble '(clocked 1)) + :to-equal (list :query t + :preamble org-ql-clock-regexp + :preamble-case-fold nil))) + ;; TODO: Other arguments for (clocked). + ) - (describe "Plain query parsing" + (describe "(level)" + (it "with a number" + (expect (org-ql--query-preamble '(level 2)) + :to-equal (list :query t + :preamble (rx bol (repeat 2 "*") " ") + :preamble-case-fold t))) + (it "with two numbers" + (expect (org-ql--query-preamble '(level 2 4)) + :to-equal (list :query t + :preamble (rx bol (repeat 2 4 "*") " ") + :preamble-case-fold t))) + (it "<" + (expect (org-ql--query-preamble '(level < 3)) + :to-equal (list :query t + :preamble (rx bol (repeat 1 2 "*") " ") + :preamble-case-fold t))) + (it "<=" + (expect (org-ql--query-preamble '(level <= 2)) + :to-equal (list :query t + :preamble (rx bol (repeat 1 2 "*") " ") + :preamble-case-fold t))) + (it ">" + (expect (org-ql--query-preamble '(level > 2)) + :to-equal (list :query t + :preamble (rx bol (>= 3 "*") " ") + :preamble-case-fold t))) + (it ">=" + (expect (org-ql--query-preamble '(level >= 2)) + :to-equal (list :query t + :preamble (rx bol (>= 2 "*") " ") + :preamble-case-fold t))))) - ;; TODO: Other predicates. + (describe "Plain query parsing" - (it "Negated terms" - (expect (org-ql--query-string-to-sexp "todo: !todo:CHECK,SOMEDAY") - :to-equal '(and (todo) (not (todo "CHECK" "SOMEDAY")))) - (expect (org-ql--query-string-to-sexp "!todo:CHECK,SOMEDAY todo:") - :to-equal '(and (not (todo "CHECK" "SOMEDAY")) (todo))) - (expect (org-ql--query-string-to-sexp "tags:universe !moon") - :to-equal '(and (tags "universe") (not (regexp "moon")))) - (expect (org-ql--query-string-to-sexp "!moon tags:universe") - :to-equal '(and (not (regexp "moon")) (tags "universe"))) - (expect (org-ql--query-string-to-sexp "mars !ts:on=today") - :to-equal '(and (regexp "mars") (not (ts :on "today")))) - (expect (org-ql--query-string-to-sexp "!\"quoted phrase\"") - :to-equal '(not (regexp "quoted phrase")))) - (it "Regexp predicates" - (expect (org-ql--query-string-to-sexp "scheduled") - ;; No colon after keyword, so not a predicate query. - :to-equal '(regexp "scheduled")) - (expect (org-ql--query-string-to-sexp "\"quoted phrase\"") - :to-equal '(regexp "quoted phrase")) - (expect (org-ql--query-string-to-sexp "regexp:word") - :to-equal '(regexp "word")) - (expect (org-ql--query-string-to-sexp "regexp:\"quoted phrase\"") - :to-equal '(regexp "quoted phrase"))) - (it "Timestamp-based predicates" - (expect (org-ql--query-string-to-sexp "scheduled:on=2017-07-07") - :to-equal '(scheduled :on "2017-07-07")) - (expect (org-ql--query-string-to-sexp "deadline:from=2017-07-07,to=2017-07-09") - :to-equal '(deadline :from "2017-07-07" :to "2017-07-09")) - (expect (org-ql--query-string-to-sexp "planning:from=2017-07-07") - :to-equal '(planning :from "2017-07-07")) - (expect (org-ql--query-string-to-sexp "closed:from=2017-07-07") - :to-equal '(closed :from "2017-07-07")) - (expect (org-ql--query-string-to-sexp "ts-active:to=2017-07-07") - :to-equal '(ts-active :to "2017-07-07")) - (expect (org-ql--query-string-to-sexp "ts-inactive:to=2017-07-07") - :to-equal '(ts-inactive :to "2017-07-07")) - (expect (org-ql--query-string-to-sexp "ts-a:to=2017-07-07") - :to-equal '(ts-a :to "2017-07-07")) - (expect (org-ql--query-string-to-sexp "ts-i:on=2017-07-07") - :to-equal '(ts-i :on "2017-07-07")) - (expect (org-ql--query-string-to-sexp "ts:") - :to-equal '(ts)) - (expect (org-ql--query-string-to-sexp "clocked:") - :to-equal '(clocked))) - (it "To-do predicates" - (expect (org-ql--query-string-to-sexp "todo:") - :to-equal '(todo)) - (expect (org-ql--query-string-to-sexp "todo:TODO") - :to-equal '(todo "TODO")) - (expect (org-ql--query-string-to-sexp "todo:TODO,SOMEDAY") - :to-equal '(todo "TODO" "SOMEDAY"))) - (it "Compound queries" - (expect (org-ql--query-string-to-sexp "todo:SOMEDAY ts-a:from=2020-01-01,to=2021-01-01") - :to-equal '(and (todo "SOMEDAY") (ts-a :from "2020-01-01" :to "2021-01-01"))) - (expect (org-ql--query-string-to-sexp "regexp:\"quoted phrase\" todo:SOMEDAY") - :to-equal '(and (regexp "quoted phrase") (todo "SOMEDAY"))))) + ;; TODO: Other predicates. - (describe "Convert sexp queries to non-sexp queries" + (it "Negated terms" + (expect (org-ql--query-string-to-sexp "todo: !todo:CHECK,SOMEDAY") + :to-equal '(and (todo) (not (todo "CHECK" "SOMEDAY")))) + (expect (org-ql--query-string-to-sexp "!todo:CHECK,SOMEDAY todo:") + :to-equal '(and (not (todo "CHECK" "SOMEDAY")) (todo))) + (expect (org-ql--query-string-to-sexp "tags:universe !moon") + :to-equal '(and (tags "universe") (not (rifle "moon")))) + (expect (org-ql--query-string-to-sexp "!moon tags:universe") + :to-equal '(and (not (rifle "moon")) (tags "universe"))) + (expect (org-ql--query-string-to-sexp "mars !ts:on=today") + :to-equal '(and (rifle "mars") (not (ts :on "today")))) + (expect (org-ql--query-string-to-sexp "!\"quoted phrase\"") + :to-equal '(not (rifle "quoted phrase")))) + (it "Regexp predicates" + (expect (org-ql--query-string-to-sexp "regexp:word") + :to-equal '(regexp "word")) + (expect (org-ql--query-string-to-sexp "regexp:\"quoted phrase\"") + :to-equal '(regexp "quoted phrase"))) + (it "Timestamp-based predicates" + (expect (org-ql--query-string-to-sexp "scheduled:on=2017-07-07") + :to-equal '(scheduled :on "2017-07-07")) + (expect (org-ql--query-string-to-sexp "deadline:from=2017-07-07,to=2017-07-09") + :to-equal '(deadline :from "2017-07-07" :to "2017-07-09")) + (expect (org-ql--query-string-to-sexp "planning:from=2017-07-07") + :to-equal '(planning :from "2017-07-07")) + (expect (org-ql--query-string-to-sexp "closed:from=2017-07-07") + :to-equal '(closed :from "2017-07-07")) + (expect (org-ql--query-string-to-sexp "ts-active:to=2017-07-07") + :to-equal '(ts-active :to "2017-07-07")) + (expect (org-ql--query-string-to-sexp "ts-inactive:to=2017-07-07") + :to-equal '(ts-inactive :to "2017-07-07")) + (expect (org-ql--query-string-to-sexp "ts-a:to=2017-07-07") + :to-equal '(ts-a :to "2017-07-07")) + (expect (org-ql--query-string-to-sexp "ts-i:on=2017-07-07") + :to-equal '(ts-i :on "2017-07-07")) + (expect (org-ql--query-string-to-sexp "ts:") + :to-equal '(ts)) + (expect (org-ql--query-string-to-sexp "clocked:") + :to-equal '(clocked))) + (it "To-do predicates" + (expect (org-ql--query-string-to-sexp "todo:") + :to-equal '(todo)) + (expect (org-ql--query-string-to-sexp "todo:TODO") + :to-equal '(todo "TODO")) + (expect (org-ql--query-string-to-sexp "todo:TODO,SOMEDAY") + :to-equal '(todo "TODO" "SOMEDAY"))) + (it "Compound queries" + (expect (org-ql--query-string-to-sexp "todo:SOMEDAY ts-a:from=2020-01-01,to=2021-01-01") + :to-equal '(and (todo "SOMEDAY") (ts-a :from "2020-01-01" :to "2021-01-01"))) + (expect (org-ql--query-string-to-sexp "regexp:\"quoted phrase\" todo:SOMEDAY") + :to-equal '(and (regexp "quoted phrase") (todo "SOMEDAY"))))) - ;; FIXME: Test (src) after converting it is implemented. - ;; (src :lang "elisp" :regexps ("defun")) + (describe "Convert sexp queries to non-sexp queries" - ;; MAYBE: Other predicates? Or should these cover the other - ;; cases, because the others use the same format? + ;; FIXME: Test (src) after converting it is implemented. + ;; (src :lang "elisp" :regexps ("defun")) - (it "(heading)" - (expect (org-ql--query-sexp-to-string '(heading "quoted phrase" "word")) - :to-equal "heading:word,\"quoted phrase\"")) - (it "(priority)" - (expect (org-ql--query-sexp-to-string '(priority >= B)) - :to-equal "priority:A,B") - (expect (org-ql--query-sexp-to-string '(priority > B)) - :to-equal "priority:A") - (expect (org-ql--query-sexp-to-string '(priority < B)) - :to-equal "priority:C") - (expect (org-ql--query-sexp-to-string '(priority < A)) - :to-equal "priority:B,C") - (expect (org-ql--query-sexp-to-string '(priority <= B)) - :to-equal "priority:B,C") - (expect (org-ql--query-sexp-to-string '(priority = A)) - :to-equal "priority:A")) - (it "(todo)" - (expect (org-ql--query-sexp-to-string '(todo)) - :to-equal "todo:") - (expect (org-ql--query-sexp-to-string '(todo "TODO")) - :to-equal "todo:TODO") - (expect (org-ql--query-sexp-to-string '(todo "TODO" "NEXT")) - :to-equal "todo:NEXT,TODO")) - (it "(ts)" - (expect (org-ql--query-sexp-to-string '(ts :from -1 :to 1)) - :to-equal "ts:from=-1,to=1") - (expect (org-ql--query-sexp-to-string '(ts :on today)) - :to-equal "ts:on=today") - (expect (org-ql--query-sexp-to-string '(ts-active :from "2017-01-01" :to "2018-01-01")) - :to-equal "ts-active:from=2017-01-01,to=2018-01-01")) - (it "(and ...)" - (expect (org-ql--query-sexp-to-string '(and (tags "book" "books") (priority "A"))) - :to-equal "tags:books,book priority:A") - (expect (org-ql--query-sexp-to-string '(and (tags "space") (not (regexp "moon")))) - :to-equal "tags:space !regexp:moon")) - (it "(or ...)" - (expect (org-ql--query-sexp-to-string '(or (tags "book" "books") (priority "A"))) - :to-equal nil))) + ;; MAYBE: Other predicates? Or should these cover the other + ;; cases, because the others use the same format? + + (it "(heading)" + (expect (org-ql--query-sexp-to-string '(heading "quoted phrase" "word")) + :to-equal "heading:word,\"quoted phrase\"")) + (it "(priority)" + (expect (org-ql--query-sexp-to-string '(priority >= B)) + :to-equal "priority:A,B") + (expect (org-ql--query-sexp-to-string '(priority > B)) + :to-equal "priority:A") + (expect (org-ql--query-sexp-to-string '(priority < B)) + :to-equal "priority:C") + (expect (org-ql--query-sexp-to-string '(priority < A)) + :to-equal "priority:B,C") + (expect (org-ql--query-sexp-to-string '(priority <= B)) + :to-equal "priority:B,C") + (expect (org-ql--query-sexp-to-string '(priority = A)) + :to-equal "priority:A")) + (it "(todo)" + (expect (org-ql--query-sexp-to-string '(todo)) + :to-equal "todo:") + (expect (org-ql--query-sexp-to-string '(todo "TODO")) + :to-equal "todo:TODO") + (expect (org-ql--query-sexp-to-string '(todo "TODO" "NEXT")) + :to-equal "todo:NEXT,TODO")) + (it "(ts)" + (expect (org-ql--query-sexp-to-string '(ts :from -1 :to 1)) + :to-equal "ts:from=-1,to=1") + (expect (org-ql--query-sexp-to-string '(ts :on today)) + :to-equal "ts:on=today") + (expect (org-ql--query-sexp-to-string '(ts-active :from "2017-01-01" :to "2018-01-01")) + :to-equal "ts-active:from=2017-01-01,to=2018-01-01")) + (it "(and ...)" + (expect (org-ql--query-sexp-to-string '(and (tags "book" "books") (priority "A"))) + :to-equal "tags:books,book priority:A") + (expect (org-ql--query-sexp-to-string '(and (tags "space") (not (regexp "moon")))) + :to-equal "tags:space !regexp:moon")) + (it "(or ...)" + (expect (org-ql--query-sexp-to-string '(or (tags "book" "books") (priority "A"))) + :to-equal nil)))) + + (describe "Query functions" + + (describe "org-ql-select" + (it "returns matching entries" + (expect (length (org-ql-select org-ql-test-buffer + '(category) + :sort 'deadline)) + :to-equal org-ql-test-num-headings))) + (describe "org-ql-query" + (it "returns matching entries" + (expect (length (org-ql-query :select 'element + :from org-ql-test-buffer + :where '(category) + :order-by 'date)) + :to-equal org-ql-test-num-headings)))) (describe "Query results" @@ -719,7 +810,7 @@ with keyword arg NOW in PLIST." (org-ql-expect ('(descendants (todo "CHECK"))) '("Recurring"))) (org-ql-it "with granddescendants query" - (org-ql-expect ('(descendants (descendants "moon"))) + (org-ql-expect ('(descendants (descendants (regexp "moon")))) '("Take over the universe"))) (org-ql-it "with query that should not match parent" ;; This test would fail if the `descendants' predicate did not properly exclude @@ -1166,6 +1257,7 @@ with keyword arg NOW in PLIST." (org-ql-it ">= a priority" (org-ql-expect ('(priority >= "B")) '("Take over the universe" "Take over the world" "Skype with president of Antarctica" "Take over Mars" "Renew membership in supervillain club" "Learn universal sign language" "Internet" "Spaceship lease" "Fix flux capacitor")))) + (describe "(property)" ;; MAYBE: Add support for (property) without arguments. @@ -1192,16 +1284,6 @@ with keyword arg NOW in PLIST." :sort 'todo) '("Take over the universe"))) - (org-ql-it "with a plain string" - (org-ql-expect ("Take over" - :sort 'todo) - '("Take over the universe" "Take over the world" "Take over Mars" "Take over the moon" "Get haircut"))) - - (org-ql-it "with two plain strings in an OR" - (org-ql-expect ('(or "Take over" "universe") - :sort 'todo) - '("Take over the universe" "Take over the world" "Take over Mars" "Take over the moon" "Get haircut"))) - (org-ql-it "case-folding predicate with non-case-folding preamble" ;; e.g. the (todo) predicate disables case-folding in its preamble, but that ;; should not prevent case-folding in this and other predicates (issue #114). @@ -1209,6 +1291,19 @@ with keyword arg NOW in PLIST." :sort 'todo) '("Take over the universe" "Take over the world" "Take over Mars" "Take over the moon" "Get haircut")))) + (describe "(rifle)" + (org-ql-it "with one argument" + (org-ql-expect ('(rifle "weekend")) + ;; In entry text. + '("Take over the world")) + (org-ql-expect ('(rifle "moon")) + ;; In outline path. + '("Take over the moon" "Visit the moon"))) + (org-ql-it "with two arguments" + (org-ql-expect ('(rifle "Take" "world")) + ;; In entry text (including tags) and/or outline path. + '("Take over the world" "Skype with president of Antarctica" "Get haircut")))) + (describe "(scheduled)" (org-ql-it "without arguments" @@ -2105,7 +2200,7 @@ with keyword arg NOW in PLIST." (it "Refuses to link to non-file-backed buffer" (expect (var-after-link-save-open 'org-ql-view-buffers-files link-buffer query :buffer link-buffer) - :to-throw 'user-error '("Views that search non-file-backed buffers can’t be linked to")))))) + :to-throw 'user-error '("Views that search non-file-backed buffers can't be linked to")))))) ;; MAYBE: Also test `org-ql-views', although I already know it works now. ;; (describe "org-ql-views")