Merge branch 'alphapapa:master' into fix/org-category-property
This commit is contained in:
commit
4b6b8f4795
13 changed files with 1886 additions and 858 deletions
2
.github/workflows/test.yml
vendored
2
.github/workflows/test.yml
vendored
|
|
@ -44,6 +44,8 @@ jobs:
|
||||||
- 26.3
|
- 26.3
|
||||||
- 27.1
|
- 27.1
|
||||||
- 27.2
|
- 27.2
|
||||||
|
- 28.1
|
||||||
|
- 28.2
|
||||||
- snapshot
|
- snapshot
|
||||||
steps:
|
steps:
|
||||||
- uses: purcell/setup-emacs@master
|
- uses: purcell/setup-emacs@master
|
||||||
|
|
|
||||||
6
Makefile
6
Makefile
|
|
@ -1,7 +1,7 @@
|
||||||
# * makem.sh/Makefile --- Script to aid building and testing Emacs Lisp packages
|
# * makem.sh/Makefile --- Script to aid building and testing Emacs Lisp packages
|
||||||
|
|
||||||
# URL: https://github.com/alphapapa/makem.sh
|
# URL: https://github.com/alphapapa/makem.sh
|
||||||
# Version: 0.3
|
# Version: 0.5
|
||||||
|
|
||||||
# * Arguments
|
# * Arguments
|
||||||
|
|
||||||
|
|
@ -38,7 +38,9 @@ endif
|
||||||
|
|
||||||
verbose = $(v)
|
verbose = $(v)
|
||||||
|
|
||||||
ifneq (,$(findstring vv,$(verbose)))
|
ifneq (,$(findstring vvv,$(verbose)))
|
||||||
|
VERBOSE = "-vvv"
|
||||||
|
else ifneq (,$(findstring vv,$(verbose)))
|
||||||
VERBOSE = "-vv"
|
VERBOSE = "-vv"
|
||||||
else ifneq (,$(findstring v,$(verbose)))
|
else ifneq (,$(findstring v,$(verbose)))
|
||||||
VERBOSE = "-v"
|
VERBOSE = "-v"
|
||||||
|
|
|
||||||
81
README.org
81
README.org
|
|
@ -94,15 +94,32 @@ Lisp code examples are in [[examples.org]].
|
||||||
:TOC: ignore-children
|
:TOC: ignore-children
|
||||||
:END:
|
: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:*
|
+ *Showing an agenda-like view:*
|
||||||
- [[#org-ql-search][org-ql-search]] (command)
|
- [[#org-ql-search][org-ql-search]]
|
||||||
- [[#org-ql-view][org-ql-view]] (command)
|
- [[#org-ql-view][org-ql-view]]
|
||||||
- [[#org-ql-view-sidebar][org-ql-view-sidebar]] (command)
|
- [[#org-ql-view-sidebar][org-ql-view-sidebar]]
|
||||||
- [[#org-ql-view-recent-items][org-ql-view-recent-items]] (command)
|
- [[#org-ql-view-recent-items][org-ql-view-recent-items]]
|
||||||
+ *Showing a tree in a buffer:*
|
+ *Showing a tree in a buffer:*
|
||||||
- [[#org-ql-sparse-tree][org-ql-sparse-tree]] (command)
|
- [[#org-ql-sparse-tree][org-ql-sparse-tree]]
|
||||||
+ *Showing results with Helm*:
|
|
||||||
- [[#helm-org-ql][helm-org-ql]] (command)
|
*** 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
|
*** org-ql-search
|
||||||
|
|
||||||
|
|
@ -220,10 +237,13 @@ Arguments are listed next to predicate names, where applicable.
|
||||||
- Aliases: ~olps~.
|
- 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.
|
+ =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~).
|
+ =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.
|
+ =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=.
|
- 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 (&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.
|
+ =tags-inherited (&optional tags)= :: Return non-nil if current heading's inherited tags include one or more of ~TAGS~ (a list of strings). If ~TAGS~ is nil, return non-nil if heading has any inherited tags.
|
||||||
- Aliases: ~inherited-tags~, ~tags-i~, ~itags~.
|
- Aliases: ~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.
|
/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
|
** 0.6
|
||||||
|
|
||||||
*Added*
|
*Added*
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
;; Author: Adam Porter <adam@alphapapa.net>
|
;; Author: Adam Porter <adam@alphapapa.net>
|
||||||
;; URL: https://github.com/alphapapa/org-ql
|
;; 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"))
|
;; Package-Requires: ((emacs "26.1") (dash "2.18.1") (s "1.12.0") (helm-org "1.0") (org-ql "0.6-pre"))
|
||||||
|
|
||||||
;;; Commentary:
|
;;; Commentary:
|
||||||
|
|
|
||||||
BIN
images/org-ql-find.png
Normal file
BIN
images/org-ql-find.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 92 KiB |
168
makem.sh
168
makem.sh
|
|
@ -3,11 +3,11 @@
|
||||||
# * makem.sh --- Script to aid building and testing Emacs Lisp packages
|
# * makem.sh --- Script to aid building and testing Emacs Lisp packages
|
||||||
|
|
||||||
# URL: https://github.com/alphapapa/makem.sh
|
# URL: https://github.com/alphapapa/makem.sh
|
||||||
# Version: 0.3
|
# Version: 0.6-pre
|
||||||
|
|
||||||
# * Commentary:
|
# * 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
|
# packages. It aims to make linting and testing as simple as possible
|
||||||
# without requiring per-package configuration.
|
# without requiring per-package configuration.
|
||||||
|
|
||||||
|
|
@ -79,7 +79,7 @@ Rules:
|
||||||
Options:
|
Options:
|
||||||
-d, --debug Print debug info.
|
-d, --debug Print debug info.
|
||||||
-h, --help I need somebody!
|
-h, --help I need somebody!
|
||||||
-v, --verbose Increase verbosity, up to -vv.
|
-v, --verbose Increase verbosity, up to -vvv.
|
||||||
--no-color Disable color output.
|
--no-color Disable color output.
|
||||||
|
|
||||||
--debug-load-path Print load-path from inside Emacs.
|
--debug-load-path Print load-path from inside Emacs.
|
||||||
|
|
@ -136,6 +136,27 @@ EOF
|
||||||
echo $file
|
echo $file
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function elisp-elint-file {
|
||||||
|
local file=$(mktemp)
|
||||||
|
cat >$file <<EOF
|
||||||
|
(require 'cl-lib)
|
||||||
|
(require 'elint)
|
||||||
|
(defun makem-elint-file (file)
|
||||||
|
(let ((errors 0))
|
||||||
|
(cl-letf (((symbol-function 'orig-message) (symbol-function 'message))
|
||||||
|
((symbol-function 'message) (symbol-function 'ignore))
|
||||||
|
((symbol-function 'elint-output)
|
||||||
|
(lambda (string)
|
||||||
|
(cl-incf errors)
|
||||||
|
(orig-message "%s" string))))
|
||||||
|
(elint-file file)
|
||||||
|
;; NOTE: \`errors' is not actually the number of errors, because
|
||||||
|
;; it's incremented for non-error header strings as well.
|
||||||
|
(kill-emacs errors))))
|
||||||
|
EOF
|
||||||
|
echo "$file"
|
||||||
|
}
|
||||||
|
|
||||||
function elisp-checkdoc-file {
|
function elisp-checkdoc-file {
|
||||||
# Since checkdoc doesn't have a batch function that exits non-zero
|
# Since checkdoc doesn't have a batch function that exits non-zero
|
||||||
# when errors are found, we make one.
|
# when errors are found, we make one.
|
||||||
|
|
@ -154,7 +175,8 @@ function elisp-checkdoc-file {
|
||||||
": " text)))
|
": " text)))
|
||||||
(message msg)
|
(message msg)
|
||||||
(setq makem-checkdoc-errors-p t)
|
(setq makem-checkdoc-errors-p t)
|
||||||
(list text start end unfixable)))))
|
;; Return nil because we *are* generating a buffered list of errors.
|
||||||
|
nil))))
|
||||||
(mapcar #'checkdoc-file files)
|
(mapcar #'checkdoc-file files)
|
||||||
(when makem-checkdoc-errors-p
|
(when makem-checkdoc-errors-p
|
||||||
(kill-emacs 1))))
|
(kill-emacs 1))))
|
||||||
|
|
@ -165,6 +187,51 @@ EOF
|
||||||
echo $file
|
echo $file
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function elisp-byte-compile-file {
|
||||||
|
# This seems to be the only way to make byte-compilation signal
|
||||||
|
# errors for warnings AND display all warnings rather than only
|
||||||
|
# the first one.
|
||||||
|
local file=$(mktemp)
|
||||||
|
# TODO: Add file to $paths_temp in other elisp- functions.
|
||||||
|
paths_temp+=("$file")
|
||||||
|
|
||||||
|
cat >"$file" <<EOF
|
||||||
|
(defun makem-batch-byte-compile (&rest args)
|
||||||
|
""
|
||||||
|
(let ((num-errors 0)
|
||||||
|
(num-warnings 0))
|
||||||
|
;; NOTE: Only accepts files as args, not directories.
|
||||||
|
(dolist (file command-line-args-left)
|
||||||
|
(pcase-let ((\`(,errors ,warnings) (makem-byte-compile-file file)))
|
||||||
|
(cl-incf num-errors errors)
|
||||||
|
(cl-incf num-warnings warnings)))
|
||||||
|
(zerop num-errors)))
|
||||||
|
|
||||||
|
(defun makem-byte-compile-file (filename &optional load)
|
||||||
|
"Call \`byte-compile-warn', returning the number of errors and the number of warnings."
|
||||||
|
(let ((num-warnings 0)
|
||||||
|
(num-errors 0))
|
||||||
|
(cl-letf (((symbol-function 'byte-compile-warn)
|
||||||
|
(lambda (format &rest args)
|
||||||
|
;; Copied from \`byte-compile-warn'.
|
||||||
|
(cl-incf num-warnings)
|
||||||
|
(setq format (apply #'format-message format args))
|
||||||
|
(byte-compile-log-warning format t :warning)))
|
||||||
|
((symbol-function 'byte-compile-report-error)
|
||||||
|
(lambda (error-info &optional fill &rest args)
|
||||||
|
(cl-incf num-errors)
|
||||||
|
;; Copied from \`byte-compile-report-error'.
|
||||||
|
(setq byte-compiler-error-flag t)
|
||||||
|
(byte-compile-log-warning
|
||||||
|
(if (stringp error-info) error-info
|
||||||
|
(error-message-string error-info))
|
||||||
|
fill :error))))
|
||||||
|
(byte-compile-file filename load))
|
||||||
|
(list num-errors num-warnings)))
|
||||||
|
EOF
|
||||||
|
echo "$file"
|
||||||
|
}
|
||||||
|
|
||||||
function elisp-check-declare-file {
|
function elisp-check-declare-file {
|
||||||
# Since check-declare doesn't have a batch function that exits
|
# Since check-declare doesn't have a batch function that exits
|
||||||
# non-zero when errors are found, we make one.
|
# non-zero when errors are found, we make one.
|
||||||
|
|
@ -200,20 +267,23 @@ Exits non-zero if mis-indented lines are found. Checks files in
|
||||||
(let ((errors-p))
|
(let ((errors-p))
|
||||||
(cl-labels ((lint-file (file)
|
(cl-labels ((lint-file (file)
|
||||||
(find-file file)
|
(find-file file)
|
||||||
(let ((tick (buffer-modified-tick)))
|
|
||||||
(let ((inhibit-message t))
|
(let ((inhibit-message t))
|
||||||
(indent-region (point-min) (point-max)))
|
(indent-region (point-min) (point-max)))
|
||||||
(when (/= tick (buffer-modified-tick))
|
(when buffer-undo-list
|
||||||
;; Indentation changed: warn for each line.
|
;; Indentation changed: warn for each line.
|
||||||
(dolist (line (undo-lines buffer-undo-list))
|
(dolist (line (undo-lines buffer-undo-list))
|
||||||
(message "%s:%s: Indentation mismatch" (buffer-name) line))
|
(message "%s:%s: Indentation mismatch" (buffer-name) line))
|
||||||
(setf errors-p t))))
|
(setf errors-p t)))
|
||||||
|
(undo-pos (entry)
|
||||||
|
(cl-typecase (car entry)
|
||||||
|
(number (car entry))
|
||||||
|
(string (abs (cdr entry)))))
|
||||||
(undo-lines (undo-list)
|
(undo-lines (undo-list)
|
||||||
;; Return list of lines changed in UNDO-LIST.
|
;; Return list of lines changed in UNDO-LIST.
|
||||||
(nreverse (cl-loop for elt in undo-list
|
(nreverse (cl-loop for elt in undo-list
|
||||||
when (and (consp elt)
|
for pos = (undo-pos elt)
|
||||||
(numberp (car elt)))
|
when pos
|
||||||
collect (line-number-at-pos (car elt))))))
|
collect (line-number-at-pos pos)))))
|
||||||
(mapc #'lint-file (mapcar #'expand-file-name command-line-args-left))
|
(mapc #'lint-file (mapcar #'expand-file-name command-line-args-left))
|
||||||
(when errors-p
|
(when errors-p
|
||||||
(kill-emacs 1)))))
|
(kill-emacs 1)))))
|
||||||
|
|
@ -232,7 +302,6 @@ function elisp-package-initialize-file {
|
||||||
(cons "melpa-stable" "https://stable.melpa.org/packages/")))
|
(cons "melpa-stable" "https://stable.melpa.org/packages/")))
|
||||||
$elisp_org_package_archive
|
$elisp_org_package_archive
|
||||||
(package-initialize)
|
(package-initialize)
|
||||||
(setq load-prefer-newer t)
|
|
||||||
EOF
|
EOF
|
||||||
echo $file
|
echo $file
|
||||||
}
|
}
|
||||||
|
|
@ -245,6 +314,7 @@ function run_emacs {
|
||||||
local emacs_command=(
|
local emacs_command=(
|
||||||
"${emacs_command[@]}"
|
"${emacs_command[@]}"
|
||||||
-Q
|
-Q
|
||||||
|
--eval "(setq load-prefer-newer t)"
|
||||||
"${args_debug[@]}"
|
"${args_debug[@]}"
|
||||||
"${args_sandbox[@]}"
|
"${args_sandbox[@]}"
|
||||||
-l $package_initialize_file
|
-l $package_initialize_file
|
||||||
|
|
@ -286,8 +356,9 @@ function batch-byte-compile {
|
||||||
[[ $compile_error_on_warn ]] && local error_on_warn=(--eval "(setq byte-compile-error-on-warn t)")
|
[[ $compile_error_on_warn ]] && local error_on_warn=(--eval "(setq byte-compile-error-on-warn t)")
|
||||||
|
|
||||||
run_emacs \
|
run_emacs \
|
||||||
|
--load "$(elisp-byte-compile-file)" \
|
||||||
"${error_on_warn[@]}" \
|
"${error_on_warn[@]}" \
|
||||||
--funcall batch-byte-compile \
|
--eval "(unless (makem-batch-byte-compile) (kill-emacs 1))" \
|
||||||
"$@"
|
"$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -297,10 +368,13 @@ function byte-compile-file {
|
||||||
|
|
||||||
[[ $compile_error_on_warn ]] && local error_on_warn=(--eval "(setq byte-compile-error-on-warn t)")
|
[[ $compile_error_on_warn ]] && local error_on_warn=(--eval "(setq byte-compile-error-on-warn t)")
|
||||||
|
|
||||||
|
# FIXME: Why is the line starting with "&& verbose 3" not indented properly? Emacs insists on indenting it back a level.
|
||||||
run_emacs \
|
run_emacs \
|
||||||
|
--load "$(elisp-byte-compile-file)" \
|
||||||
"${error_on_warn[@]}" \
|
"${error_on_warn[@]}" \
|
||||||
--eval "(byte-compile-file \"$file\")" \
|
--eval "(pcase-let ((\`(,num-errors ,num-warnings) (makem-byte-compile-file \"$file\"))) (when (or (and byte-compile-error-on-warn (not (zerop num-warnings))) (not (zerop num-errors))) (kill-emacs 1)))" \
|
||||||
|| error "Compiling file failed: $file"
|
&& verbose 3 "Compiling $file finished without errors." \
|
||||||
|
|| { verbose 3 "Compiling file failed: $file"; return 1; }
|
||||||
}
|
}
|
||||||
|
|
||||||
# ** Files
|
# ** Files
|
||||||
|
|
@ -376,7 +450,8 @@ function args-load-files {
|
||||||
# For file in $@, echo "--load $file".
|
# For file in $@, echo "--load $file".
|
||||||
for file in "$@"
|
for file in "$@"
|
||||||
do
|
do
|
||||||
printf -- '--load %q ' "$file"
|
sans_extension=${file%%.el}
|
||||||
|
printf -- '--load %q ' "$sans_extension"
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -413,8 +488,7 @@ function ert-tests-p {
|
||||||
}
|
}
|
||||||
|
|
||||||
function package-main-file {
|
function package-main-file {
|
||||||
# Echo the package's main file. Helpful for setting package-lint-main-file.
|
# Echo the package's main file.
|
||||||
|
|
||||||
file_pkg=$(git ls-files ./*-pkg.el 2>/dev/null)
|
file_pkg=$(git ls-files ./*-pkg.el 2>/dev/null)
|
||||||
|
|
||||||
if [[ $file_pkg ]]
|
if [[ $file_pkg ]]
|
||||||
|
|
@ -496,6 +570,8 @@ function sandbox {
|
||||||
args_sandbox=(
|
args_sandbox=(
|
||||||
--title "makem.sh: $(basename $(pwd)) (sandbox: $sandbox_dir)"
|
--title "makem.sh: $(basename $(pwd)) (sandbox: $sandbox_dir)"
|
||||||
--eval "(setq user-emacs-directory (file-truename \"$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\"))"
|
--eval "(setq user-init-file (file-truename \"$init_file\"))"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -658,7 +734,8 @@ function verbose {
|
||||||
if [[ $verbose -ge $1 ]]
|
if [[ $verbose -ge $1 ]]
|
||||||
then
|
then
|
||||||
[[ $1 -eq 1 ]] && local color_name=blue
|
[[ $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
|
shift
|
||||||
log_color $color_name "$@" >&2
|
log_color $color_name "$@" >&2
|
||||||
|
|
@ -706,9 +783,7 @@ function compile-batch {
|
||||||
verbose 2 "Batch-compiling files..."
|
verbose 2 "Batch-compiling files..."
|
||||||
debug "Byte-compile files: ${files_project_byte_compile[@]}"
|
debug "Byte-compile files: ${files_project_byte_compile[@]}"
|
||||||
|
|
||||||
batch-byte-compile "${files_project_byte_compile[@]}" \
|
batch-byte-compile "${files_project_byte_compile[@]}"
|
||||||
&& success "Compiling finished without errors." \
|
|
||||||
|| error "Compilation failed."
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function compile-each {
|
function compile-each {
|
||||||
|
|
@ -726,9 +801,7 @@ function compile-each {
|
||||||
|| compile_errors=t
|
|| compile_errors=t
|
||||||
done
|
done
|
||||||
|
|
||||||
! [[ $compile_errors ]] \
|
[[ ! $compile_errors ]]
|
||||||
&& success "Compiling finished without errors." \
|
|
||||||
|| error "Compilation failed."
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function compile {
|
function compile {
|
||||||
|
|
@ -738,6 +811,18 @@ function compile {
|
||||||
else
|
else
|
||||||
compile-each "$@"
|
compile-each "$@"
|
||||||
fi
|
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 {
|
function batch {
|
||||||
|
|
@ -752,12 +837,15 @@ function batch {
|
||||||
|
|
||||||
function interactive {
|
function interactive {
|
||||||
# Run Emacs interactively. Most useful with --sandbox and --install-deps.
|
# 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 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
|
unset arg_batch
|
||||||
run_emacs \
|
run_emacs \
|
||||||
$(args-load-files "${files_project_feature[@]}" "${files_project_test[@]}") \
|
$load_file_args \
|
||||||
--eval "(load user-init-file)" \
|
--eval "(load user-init-file)" \
|
||||||
"${args_batch_interactive[@]}"
|
"${args_batch_interactive[@]}"
|
||||||
arg_batch="--batch"
|
arg_batch="--batch"
|
||||||
|
|
@ -769,6 +857,9 @@ function lint {
|
||||||
lint-checkdoc
|
lint-checkdoc
|
||||||
lint-compile
|
lint-compile
|
||||||
lint-declare
|
lint-declare
|
||||||
|
# NOTE: Elint doesn't seem very useful at the moment. See comment
|
||||||
|
# in lint-elint function.
|
||||||
|
# lint-elint
|
||||||
lint-indent
|
lint-indent
|
||||||
lint-package
|
lint-package
|
||||||
lint-regexps
|
lint-regexps
|
||||||
|
|
@ -825,6 +916,28 @@ function lint-elsa {
|
||||||
|| error "Linting with Elsa failed."
|
|| 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 {
|
function lint-indent {
|
||||||
verbose 1 "Linting indentation..."
|
verbose 1 "Linting indentation..."
|
||||||
|
|
||||||
|
|
@ -896,7 +1009,8 @@ function test-buttercup {
|
||||||
|
|
||||||
run_emacs \
|
run_emacs \
|
||||||
$(args-load-files "${files_project_test[@]}") \
|
$(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." \
|
&& success "Buttercup tests finished without errors." \
|
||||||
|| error "Buttercup tests failed."
|
|| error "Buttercup tests failed."
|
||||||
}
|
}
|
||||||
|
|
|
||||||
227
org-ql-completing-read.el
Normal file
227
org-ql-completing-read.el
Normal file
|
|
@ -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 <adam@alphapapa.net>
|
||||||
|
|
||||||
|
;; 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 <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
;;; 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
|
||||||
|
;; <https://github.com/radian-software/selectrum/issues/114#issuecomment-744041532>.
|
||||||
|
(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
|
||||||
135
org-ql-find.el
Normal file
135
org-ql-find.el
Normal file
|
|
@ -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 <adam@alphapapa.net>
|
||||||
|
|
||||||
|
;; 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 <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
;;; 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
|
||||||
|
|
@ -38,6 +38,15 @@
|
||||||
(require 'org-ql)
|
(require 'org-ql)
|
||||||
(require 'org-ql-view)
|
(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
|
;;;; Variables
|
||||||
|
|
||||||
(defvar org-ql-block-header nil
|
(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)
|
(list (cons 'todo (lambda (element)
|
||||||
(org-element-property :todo-keyword element)))
|
(org-element-property :todo-keyword element)))
|
||||||
(cons 'heading (lambda (element)
|
(cons 'heading (lambda (element)
|
||||||
(org-make-link-string (org-element-property :raw-value element)
|
(let ((normalized-heading
|
||||||
(org-link-display-format
|
(org-ql-search--link-heading-search-string (org-element-property :raw-value element))))
|
||||||
(org-element-property :raw-value element)))))
|
(org-make-link-string normalized-heading (org-link-display-format normalized-heading)))))
|
||||||
(cons 'priority (lambda (element)
|
(cons 'priority (lambda (element)
|
||||||
(--when-let (org-element-property :priority element)
|
(--when-let (org-element-property :priority element)
|
||||||
(char-to-string it))))
|
(char-to-string it))))
|
||||||
|
|
|
||||||
|
|
@ -718,7 +718,7 @@ When opened, the link searches the buffer it's opened from."
|
||||||
(unless (or value transient--prefix)
|
(unless (or value transient--prefix)
|
||||||
(message "Unset %s" variable))))
|
(message "Unset %s" variable))))
|
||||||
|
|
||||||
(define-transient-command org-ql-view-dispatch ()
|
(transient-define-prefix org-ql-view-dispatch ()
|
||||||
"Show Org QL View dispatcher."
|
"Show Org QL View dispatcher."
|
||||||
[["Edit"
|
[["Edit"
|
||||||
("t" org-ql-view--transient-title)
|
("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--format-query
|
||||||
(org-ql-view--font-lock-string 'emacs-lisp-mode)))))
|
(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.
|
;; 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))
|
:description (lambda () (org-ql-view--format-transient-key-value "Title" org-ql-view-title))
|
||||||
:class 'org-ql-view--variable
|
: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))
|
(format "%s" org-ql-view-title))
|
||||||
history)))
|
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))
|
:description (lambda () (org-ql-view--format-transient-lisp-argument "Query" org-ql-view-query))
|
||||||
:class 'org-ql-view--variable
|
:class 'org-ql-view--variable
|
||||||
:argument ""
|
:argument ""
|
||||||
|
|
@ -784,7 +784,7 @@ When opened, the link searches the buffer it's opened from."
|
||||||
;; Parse non-sexp query into sexp query.
|
;; Parse non-sexp query into sexp query.
|
||||||
(org-ql--query-string-to-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))
|
:description (lambda () (org-ql-view--format-transient-lisp-argument "In buffers/files" org-ql-view-buffers-files))
|
||||||
:class 'org-ql-view--variable
|
:class 'org-ql-view--variable
|
||||||
:argument ""
|
:argument ""
|
||||||
|
|
@ -795,7 +795,7 @@ When opened, the link searches the buffer it's opened from."
|
||||||
;; argument, but it gives the same result.
|
;; argument, but it gives the same result.
|
||||||
(org-ql-view--complete-buffers-files)))
|
(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 ()
|
:description (lambda ()
|
||||||
(org-ql-view--format-transient-lisp-argument "Group by" org-ql-view-super-groups))
|
(org-ql-view--format-transient-lisp-argument "Group by" org-ql-view-super-groups))
|
||||||
:class 'org-ql-view--variable
|
: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.
|
;; FIXME: Figure out how to integrate initial-input and history.
|
||||||
(org-ql-view--complete-super-groups)))
|
(org-ql-view--complete-super-groups)))
|
||||||
|
|
||||||
(define-infix-command org-ql-view--transient-sort ()
|
(transient-define-infix org-ql-view--transient-sort ()
|
||||||
:description
|
:description
|
||||||
(lambda ()
|
(lambda ()
|
||||||
(org-ql-view--format-transient-lisp-argument "Sort by" (or org-ql-view-sort 'buffer-order)))
|
(org-ql-view--format-transient-lisp-argument "Sort by" (or org-ql-view-sort 'buffer-order)))
|
||||||
|
|
|
||||||
374
org-ql.el
374
org-ql.el
|
|
@ -1,19 +1,13 @@
|
||||||
;;; org-ql.el --- Org Query Language, search command, and agenda-like view -*- lexical-binding: t; -*-
|
;;; org-ql.el --- Org Query Language, search command, and agenda-like view -*- lexical-binding: t; -*-
|
||||||
|
|
||||||
|
;; Copyright (C) 2017-2022 Adam Porter
|
||||||
|
|
||||||
;; Author: Adam Porter <adam@alphapapa.net>
|
;; Author: Adam Porter <adam@alphapapa.net>
|
||||||
;; Url: https://github.com/alphapapa/org-ql
|
;; Url: https://github.com/alphapapa/org-ql
|
||||||
;; Version: 0.6
|
;; 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") (s "1.12.0") (transient "0.1") (ts "0.2-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
|
;; 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:
|
;;; License:
|
||||||
|
|
||||||
;; This program is free software; you can redistribute it and/or modify
|
;; 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
|
;; You should have received a copy of the GNU General Public License
|
||||||
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
|
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
;;; 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:
|
;;; Code:
|
||||||
|
|
||||||
;;;; Requirements
|
;;;; Requirements
|
||||||
|
|
||||||
(require 'cl-lib)
|
(require 'cl-lib)
|
||||||
(require 'org)
|
(require 'org)
|
||||||
|
(require 'org-duration)
|
||||||
(require 'org-element)
|
(require 'org-element)
|
||||||
(require 'org-habit)
|
(require 'org-habit)
|
||||||
(require 'seq)
|
(require 'seq)
|
||||||
|
|
@ -298,6 +301,16 @@ See Info node `(org-ql)Queries'."
|
||||||
:type 'boolean
|
:type 'boolean
|
||||||
:risky t)
|
: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
|
;;;; Functions
|
||||||
|
|
||||||
;;;;; Query execution
|
;;;;; Query execution
|
||||||
|
|
@ -361,7 +374,7 @@ each priority the newest items would appear first."
|
||||||
;; It feels unintuitive that `find-file-noselect' returns
|
;; It feels unintuitive that `find-file-noselect' returns
|
||||||
;; a buffer if the filename doesn't exist.
|
;; a buffer if the filename doesn't exist.
|
||||||
(find-file-noselect it))
|
(find-file-noselect it))
|
||||||
(user-error "Can't open file: %s" it)))))
|
(display-warning 'org-ql-select (format "Can't open file: %s" it) :error)))))
|
||||||
;; Ignore special/hidden buffers.
|
;; Ignore special/hidden buffers.
|
||||||
(--remove (string-prefix-p " " (buffer-name it)))))
|
(--remove (string-prefix-p " " (buffer-name it)))))
|
||||||
(query (org-ql--normalize-query query))
|
(query (org-ql--normalize-query query))
|
||||||
|
|
@ -401,7 +414,7 @@ each priority the newest items would appear first."
|
||||||
(->> buffers
|
(->> buffers
|
||||||
(--map (with-current-buffer it
|
(--map (with-current-buffer it
|
||||||
(unless (derived-mode-p 'org-mode)
|
(unless (derived-mode-p 'org-mode)
|
||||||
(user-error "Not an Org buffer: %s" (buffer-name)))
|
(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
|
(org-ql--select-cached :query query :preamble preamble :preamble-case-fold preamble-case-fold
|
||||||
:predicate predicate :action action :narrow narrow)))
|
:predicate predicate :action action :narrow narrow)))
|
||||||
(-flatten-n 1)))
|
(-flatten-n 1)))
|
||||||
|
|
@ -471,7 +484,7 @@ NARROW corresponds to the `org-ql-select' argument NARROW."
|
||||||
(if-let* ((buffer-cache (gethash (current-buffer) org-ql-cache))
|
(if-let* ((buffer-cache (gethash (current-buffer) org-ql-cache))
|
||||||
(query-cache (cadr buffer-cache))
|
(query-cache (cadr buffer-cache))
|
||||||
(modified-tick (car 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)))
|
(cached-result (gethash query-cache-key query-cache)))
|
||||||
(pcase cached-result
|
(pcase cached-result
|
||||||
('org-ql-nil nil)
|
('org-ql-nil nil)
|
||||||
|
|
@ -480,7 +493,7 @@ NARROW corresponds to the `org-ql-select' argument NARROW."
|
||||||
(cond ((or (not query-cache)
|
(cond ((or (not query-cache)
|
||||||
(not buffer-unmodified-p))
|
(not buffer-unmodified-p))
|
||||||
(puthash (current-buffer)
|
(puthash (current-buffer)
|
||||||
(list (buffer-modified-tick)
|
(list (buffer-chars-modified-tick)
|
||||||
(let ((table (make-hash-table :test 'org-ql-hash-test)))
|
(let ((table (make-hash-table :test 'org-ql-hash-test)))
|
||||||
(puthash query-cache-key (or new-result 'org-ql-nil) table)
|
(puthash query-cache-key (or new-result 'org-ql-nil) table)
|
||||||
table))
|
table))
|
||||||
|
|
@ -525,6 +538,20 @@ If NARROW is non-nil, buffer will not be widened."
|
||||||
|
|
||||||
;;;;; Helpers
|
;;;;; 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)
|
(defun org-ql--tags-at (position)
|
||||||
;; FIXME: This function actually assumes that point is already at POSITION.
|
;; FIXME: This function actually assumes that point is already at POSITION.
|
||||||
"Return tags for POSITION in current buffer.
|
"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))
|
(if-let* ((buffer-cache (gethash (current-buffer) org-ql-tags-cache))
|
||||||
(modified-tick (car buffer-cache))
|
(modified-tick (car buffer-cache))
|
||||||
(tags-cache (cdr 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)))
|
(cached-result (gethash position tags-cache)))
|
||||||
;; Found in cache: return them.
|
;; Found in cache: return them.
|
||||||
;; FIXME: Isn't `cached-result' a list of (INHERITED . LOCAL)? It
|
;; 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)
|
(setf buffer-cache (gethash (current-buffer) org-ql-tags-cache)
|
||||||
modified-tick (car buffer-cache)
|
modified-tick (car buffer-cache)
|
||||||
tags-cache (cdr 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)
|
(unless (and buffer-cache buffer-unmodified-p)
|
||||||
;; Buffer-local tags cache empty or invalid: make new one.
|
;; Buffer-local tags cache empty or invalid: make new one.
|
||||||
(setf tags-cache (make-hash-table))
|
(setf tags-cache (make-hash-table))
|
||||||
(puthash (current-buffer)
|
(puthash (current-buffer)
|
||||||
(cons (buffer-modified-tick) tags-cache)
|
(cons (buffer-chars-modified-tick) tags-cache)
|
||||||
org-ql-tags-cache))
|
org-ql-tags-cache))
|
||||||
(puthash position all-tags 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))
|
(pcase (if-let* ((buffer-cache (gethash (current-buffer) org-ql-node-value-cache))
|
||||||
(modified-tick (car buffer-cache))
|
(modified-tick (car buffer-cache))
|
||||||
(position-cache (cdr 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))
|
(value-cache (gethash position position-cache))
|
||||||
(cached-value (alist-get fn value-cache nil nil #'equal)))
|
(cached-value (alist-get fn value-cache nil nil #'equal)))
|
||||||
;; Found in cache: return it.
|
;; Found in cache: return it.
|
||||||
|
|
@ -627,13 +654,13 @@ Values compared with `equal'."
|
||||||
position-cache (cdr buffer-cache)
|
position-cache (cdr buffer-cache)
|
||||||
value-cache (when position-cache
|
value-cache (when position-cache
|
||||||
(gethash position 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)
|
(unless (and buffer-cache buffer-unmodified-p)
|
||||||
;; Buffer-local node cache empty or invalid: make new one.
|
;; Buffer-local node cache empty or invalid: make new one.
|
||||||
(setf position-cache (make-hash-table)
|
(setf position-cache (make-hash-table)
|
||||||
value-cache (gethash position position-cache))
|
value-cache (gethash position position-cache))
|
||||||
(puthash (current-buffer)
|
(puthash (current-buffer)
|
||||||
(cons (buffer-modified-tick) position-cache)
|
(cons (buffer-chars-modified-tick) position-cache)
|
||||||
org-ql-node-value-cache))
|
org-ql-node-value-cache))
|
||||||
(setf (alist-get fn value-cache nil nil #'equal) new-value)
|
(setf (alist-get fn value-cache nil nil #'equal) new-value)
|
||||||
(puthash position value-cache position-cache)
|
(puthash position value-cache position-cache)
|
||||||
|
|
@ -672,7 +699,8 @@ returns nil."
|
||||||
(user-error "Query aborted by user")))))
|
(user-error "Query aborted by user")))))
|
||||||
|
|
||||||
(defun org-ql--plist-get* (plist property)
|
(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)))
|
(if-let ((pair (plist-member plist property)))
|
||||||
(cadr pair)
|
(cadr pair)
|
||||||
'not-found))
|
'not-found))
|
||||||
|
|
@ -686,6 +714,56 @@ returns nil."
|
||||||
;; partially typed queries in the Helm commands.
|
;; partially typed queries in the Helm commands.
|
||||||
(define-error 'org-ql-invalid-query "Invalid Org QL query" 'user-error)
|
(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)
|
(defun org-ql--sanity-check-form (form)
|
||||||
"Signal error if any forms in FORM do not have preconditions met.
|
"Signal error if any forms in FORM do not have preconditions met.
|
||||||
Or, when possible, fix the problem."
|
Or, when possible, fix the problem."
|
||||||
|
|
@ -765,12 +843,12 @@ respectively."
|
||||||
"#+end_src")
|
"#+end_src")
|
||||||
t))
|
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.
|
"Signal an `org-ql-invalid-query' error.
|
||||||
Arguments STRING, POS, FILL, and LEVEL are according to
|
Arguments STRING, POS, FILL, and LEVEL are according to
|
||||||
`byte-compile-log-warning-function'."
|
`byte-compile-log-warning-function'."
|
||||||
;; Used as the `byte-compile-log-warning-function' in `org-ql--query-preamble'.
|
;; 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)
|
(defun org-ql--query-predicate (query)
|
||||||
"Return predicate function for 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,
|
;; 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.
|
;; and the shorter one is listed first, the shorter one fails to match.
|
||||||
(-sort (-on #'> #'length))))
|
(-sort (-on #'> #'length))))
|
||||||
(pexs `((query (+ term
|
(pexs `((query (+ (and term (* [blank]))))
|
||||||
(opt (+ (syntax-class whitespace) (any)))))
|
|
||||||
(term (or (and negation (list positive-term)
|
(term (or (and negation (list positive-term)
|
||||||
;; This is a bit confusing, but it seems to work. There's probably a better way.
|
;; This is a bit confusing, but it seems to work. There's probably a better way.
|
||||||
`(pred -- (list 'not (car pred))))
|
`(pred -- (list 'not (car pred))))
|
||||||
positive-term))
|
positive-term))
|
||||||
(positive-term (or (and predicate-with-args `(pred args -- (cons (intern pred) args)))
|
(positive-term (or (and predicate-with-args `(pred args -- (cons (intern pred) args)))
|
||||||
(and predicate-without-args `(pred -- (list (intern pred))))
|
(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))
|
(plain-string (or quoted-arg unquoted-arg))
|
||||||
(predicate-with-args (substring predicate) ":" args)
|
(predicate-with-args (substring predicate) ":" args)
|
||||||
(predicate-without-args (substring predicate) ":")
|
(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)
|
(`(unless ,condition . ,clauses) `(unless ,(rec condition)
|
||||||
,@(mapcar #'rec clauses)))
|
,@(mapcar #'rec clauses)))
|
||||||
;; TODO: Combine (regexp) when appropriate (i.e. inside an OR, not an AND).
|
;; 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
|
,@normalizer-patterns
|
||||||
|
|
||||||
|
|
@ -959,7 +1036,7 @@ defined in `org-ql-predicates' by calling `org-ql-defpred'."
|
||||||
(setf query new-query)
|
(setf query new-query)
|
||||||
(when (eq (cl-incf count) limit)
|
(when (eq (cl-incf count) limit)
|
||||||
(error "Query normalization limit exceeded: QUERY:%S" query)))
|
(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)
|
(defun org-ql--define-query-preamble-fn (predicates)
|
||||||
"Define function `org-ql--query-preamble' for 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.
|
;; function still works. But to avoid the warning, we byte-compile it afterward.
|
||||||
(byte-compile 'org-ql--query-preamble)))
|
(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'.
|
"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
|
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
|
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
|
query expression with no work to do, which improves
|
||||||
performance.
|
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
|
For convenience, within the `pcase' patterns, the symbol
|
||||||
`predicate-names' is a special form which is replaced with a
|
`predicate-names' is a special form which is replaced with a
|
||||||
pattern matching any of the predicate's name and aliases. For
|
pattern matching any of the predicate's name and aliases. For
|
||||||
|
|
@ -1099,6 +1194,8 @@ It would be expanded to:
|
||||||
|
|
||||||
((`(,(or 'heading 'h) . ,args)
|
((`(,(or 'heading 'h) . ,args)
|
||||||
`(heading ,@args)))"
|
`(heading ,@args)))"
|
||||||
|
;; FIXME: Update defpred tutorial to include :coalesce.
|
||||||
|
|
||||||
;; NOTE: The debug form works, completely! For example, use `edebug-defun'
|
;; NOTE: The debug form works, completely! For example, use `edebug-defun'
|
||||||
;; on the `heading' predicate, then evaluate this form:
|
;; on the `heading' predicate, then evaluate this form:
|
||||||
;; (let* ((query '(heading "HEADING"))
|
;; (let* ((query '(heading "HEADING"))
|
||||||
|
|
@ -1108,7 +1205,8 @@ It would be expanded to:
|
||||||
;; :normalized normalized
|
;; :normalized normalized
|
||||||
;; :preamble preamble))
|
;; :preamble preamble))
|
||||||
(declare (debug ([&or symbolp listp] listp stringp
|
(declare (debug ([&or symbolp listp] listp stringp
|
||||||
&rest [&or [":body" def-body]
|
&rest [&or [":coalesce" form]
|
||||||
|
[":body" def-body]
|
||||||
[":normalizers" (&rest (sexp def-body))]
|
[":normalizers" (&rest (sexp def-body))]
|
||||||
[":preambles" (&rest (sexp def-body))]]))
|
[":preambles" (&rest (sexp def-body))]]))
|
||||||
(indent defun))
|
(indent defun))
|
||||||
|
|
@ -1124,12 +1222,20 @@ It would be expanded to:
|
||||||
normalizers))
|
normalizers))
|
||||||
(preambles (cl-sublis (list (cons 'predicate-names (cons 'or (--map (list 'quote it) predicate-names))))
|
(preambles (cl-sublis (list (cons 'predicate-names (cons 'or (--map (list 'quote it) predicate-names))))
|
||||||
preambles)))
|
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
|
`(progn
|
||||||
(cl-defun ,fn-name ,args ,docstring ,body)
|
(cl-defun ,fn-name ,args ,docstring ,body)
|
||||||
;; SOMEDAY: Use `map-elt' here, after map 2.1 can be automatically installed in CI sandbox...
|
;; SOMEDAY: Use `map-elt' here, after map 2.1 can be automatically installed in CI sandbox...
|
||||||
(setf (alist-get ',predicate-name org-ql-predicates)
|
(setf (alist-get ',predicate-name org-ql-predicates)
|
||||||
`(:name ,',name :aliases ,',aliases :fn ,',fn-name :docstring ,(\, docstring) :args ,',args
|
`(: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
|
(unless org-ql-defpred-defer
|
||||||
;; Reversing preserves the order in which predicates were defined.
|
;; Reversing preserves the order in which predicates were defined.
|
||||||
(org-ql--define-normalize-query-fn (reverse org-ql-predicates))
|
(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.
|
;; redefinitions until all of the predicates have been defined.
|
||||||
(setf org-ql-defpred-defer t)
|
(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)."
|
"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))))
|
:body (when-let ((category (org-get-category (point))))
|
||||||
(cl-typecase categories
|
(cl-typecase categories
|
||||||
(null t)
|
(null t)
|
||||||
|
|
@ -1312,11 +1420,20 @@ Org effort string, like \"5\" or \"0:05\"."
|
||||||
(org-ql-defpred (heading h) (&rest strings)
|
(org-ql-defpred (heading h) (&rest strings)
|
||||||
"Return non-nil if current entry's heading matches all STRINGS.
|
"Return non-nil if current entry's heading matches all STRINGS.
|
||||||
Matching is done case-insensitively."
|
Matching is done case-insensitively."
|
||||||
|
:coalesce t
|
||||||
:normalizers ((`(,predicate-names . ,args)
|
:normalizers ((`(,predicate-names . ,args)
|
||||||
;; "h" alias.
|
;; "h" alias.
|
||||||
`(heading ,@args)))
|
`(heading ,@args)))
|
||||||
;; TODO: Adjust regexp to avoid matching in tag list.
|
;; 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
|
;; 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).
|
;; 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)
|
(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)
|
(org-ql-defpred (heading-regexp h*) (&rest regexps)
|
||||||
"Return non-nil if current entry's heading matches all REGEXPS (regexp strings).
|
"Return non-nil if current entry's heading matches all REGEXPS (regexp strings).
|
||||||
Matching is done case-insensitively."
|
Matching is done case-insensitively."
|
||||||
|
:coalesce t
|
||||||
:normalizers ((`(,predicate-names . ,args)
|
:normalizers ((`(,predicate-names . ,args)
|
||||||
;; "h" alias.
|
;; "h" alias.
|
||||||
`(heading-regexp ,@args)))
|
`(heading-regexp ,@args)))
|
||||||
;; MAYBE: Adjust regexp to avoid matching in tag list.
|
;; 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
|
;; 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).
|
;; 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)
|
(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))
|
((pred stringp) (string-to-number it))
|
||||||
(_ it))
|
(_ it))
|
||||||
args))))
|
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
|
(let ((repeat (pcase comparator-or-num
|
||||||
('< `(repeat 1 ,(1- num) "*"))
|
('< `(repeat 1 ,(1- num) "*"))
|
||||||
('<= `(repeat 1 ,num "*"))
|
('<= `(repeat 1 ,num "*"))
|
||||||
|
|
@ -1461,7 +1595,8 @@ any link is found."
|
||||||
(setf description (regexp-quote description)))
|
(setf description (regexp-quote description)))
|
||||||
(when target
|
(when target
|
||||||
(setf target (regexp-quote 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
|
(pcase description-or-target
|
||||||
('nil (and (or (null target)
|
('nil (and (or (null target)
|
||||||
(string-match-p target (match-string 1)))
|
(string-match-p target (match-string 1)))
|
||||||
|
|
@ -1474,6 +1609,27 @@ any link is found."
|
||||||
(string-match-p description-or-target
|
(string-match-p description-or-target
|
||||||
(match-string org-ql-link-description-group)))))))))
|
(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.
|
;; 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.
|
;; 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 \"Food\" \"Fruits\")
|
||||||
(olp \"Fruits\" \"Grapes\")
|
(olp \"Fruits\" \"Grapes\")
|
||||||
(olp \"Food\" \"Grapes\")"
|
(olp \"Food\" \"Grapes\")"
|
||||||
|
:coalesce t
|
||||||
:normalizers ((`(,predicate-names . ,strings)
|
:normalizers ((`(,predicate-names . ,strings)
|
||||||
;; Regexp quote headings.
|
;; Regexp quote headings.
|
||||||
`(org-ql--predicate-outline-path ,@(mapcar #'regexp-quote strings))))
|
`(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)
|
(org-ql-defpred path (&rest regexps)
|
||||||
"Return non-nil if current heading's buffer's filename path matches any of REGEXPS (regexp strings).
|
"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."
|
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)
|
:body (when (buffer-file-name)
|
||||||
(cl-typecase regexps
|
(cl-typecase regexps
|
||||||
(null t)
|
(null t)
|
||||||
|
|
@ -1605,30 +1763,54 @@ priority B)."
|
||||||
(cl-loop for priority-arg in args
|
(cl-loop for priority-arg in args
|
||||||
thereis (= item-priority (* 1000 (- org-lowest-priority (string-to-char priority-arg)))))))))
|
thereis (= item-priority (* 1000 (- org-lowest-priority (string-to-char priority-arg)))))))))
|
||||||
|
|
||||||
(org-ql-defpred property (property &optional 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)."
|
"Return non-nil if current entry has PROPERTY (a string), and optionally VALUE (a string).
|
||||||
:normalizers ((`(,predicate-names ,property . ,value)
|
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
|
;; Convert keyword property arguments to strings. Non-sexp
|
||||||
;; queries result in keyword property arguments (because to do
|
;; queries result in keyword property arguments (because to do
|
||||||
;; otherwise would require ugly special-casing in the parsing).
|
;; otherwise would require ugly special-casing in the parsing).
|
||||||
(when (keywordp property)
|
(when (keywordp property)
|
||||||
(setf property (substring (symbol-name property) 1)))
|
(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: Should case folding be disabled for properties? What about values?
|
||||||
;; MAYBE: Support (property) without args.
|
;; 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,
|
;; 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.
|
;; because the regexp could match a string not inside a property drawer.
|
||||||
(list :regexp (rx-to-string `(seq bol (0+ space) ":" ,property ":"
|
(list :regexp (unless inherit
|
||||||
(1+ space) ,value (0+ space) eol))
|
(rx-to-string `(seq bol (0+ space) ":" ,property ":"
|
||||||
|
(1+ space) ,value (0+ space) eol)))
|
||||||
:query query))
|
:query query))
|
||||||
(`(,predicate-names ,property)
|
(`(,predicate-names ,property . ,(map :inherit))
|
||||||
;; We do NOT return nil, because the predicate still needs to be tested,
|
;; 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.
|
;; because the regexp could match a string not inside a property drawer.
|
||||||
;; NOTE: The preamble only matches if there appears to be a value.
|
;; NOTE: The preamble only matches if there appears to be a value.
|
||||||
;; A line like ":ID: " without any other text does not match.
|
;; A line like ":ID: " without any other text does not match.
|
||||||
(list :regexp (rx-to-string `(seq bol (0+ space) ":" ,property ":" (1+ space)
|
(list :regexp (unless inherit
|
||||||
(minimal-match (1+ not-newline)) eol))
|
(rx-to-string `(seq bol (0+ space) ":" ,property ":" (1+ space)
|
||||||
|
(minimal-match (1+ not-newline)) eol)))
|
||||||
:query query)))
|
:query query)))
|
||||||
:body
|
:body
|
||||||
(pcase property
|
(pcase property
|
||||||
|
|
@ -1636,13 +1818,25 @@ priority B)."
|
||||||
(_ (pcase value
|
(_ (pcase value
|
||||||
('nil
|
('nil
|
||||||
;; Check that PROPERTY exists
|
;; 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
|
;; Check that PROPERTY has VALUE.
|
||||||
(string-equal value (org-entry-get (point) property 'selective)))))))
|
|
||||||
|
;; 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)
|
(org-ql-defpred (regexp r) (&rest regexps)
|
||||||
"Return non-nil if current entry matches all of REGEXPS (regexp strings)."
|
"Return non-nil if current entry matches all of REGEXPS (regexp strings)."
|
||||||
|
:coalesce t
|
||||||
:normalizers ((`(,predicate-names . ,args)
|
:normalizers ((`(,predicate-names . ,args)
|
||||||
`(regexp ,@args)))
|
`(regexp ,@args)))
|
||||||
;; MAYBE: Separate case-sensitive (Regexp) predicate.
|
;; MAYBE: Separate case-sensitive (Regexp) predicate.
|
||||||
|
|
@ -1664,27 +1858,70 @@ priority B)."
|
||||||
(org-ql-defpred src (&key regexps lang)
|
(org-ql-defpred src (&key regexps lang)
|
||||||
"Return non-nil if current entry contains an Org source block matching all of REGEXPS.
|
"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
|
If keyword argument LANG is non-nil, the block must be in that
|
||||||
language."
|
language. Matching is done case-insensitively."
|
||||||
:normalizers ((`(,predicate-names . ,args)
|
: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.
|
;; Rewrite to use keyword args.
|
||||||
(-let (regexps lang keyword-index)
|
(cond ((cl-every #'stringp args)
|
||||||
(cond ((plist-get args :lang)
|
;; No keywords, only regexps.
|
||||||
;; Lang given first, or only lang given.
|
`(src :regexps ',args))
|
||||||
(setf lang (plist-get args :lang)
|
((and (stringp (car args)) (cl-some #'keywordp args))
|
||||||
regexps (seq-difference args (list :lang lang))))
|
;; Regexp as first arg with keyword later.
|
||||||
((setf keyword-index (-find-index #'keywordp args))
|
(let* ((keyword-pos (cl-position :lang args))
|
||||||
;; Regexps and lang given.
|
(regexps (cl-subseq args 0 keyword-pos))
|
||||||
(setf lang (plist-get (cl-subseq args keyword-index) :lang)
|
;; We assume that if :lang is given, the string argument follows.
|
||||||
regexps (cl-subseq args 0 keyword-index)))
|
(lang (nth (1+ (cl-position :lang args)) args)))
|
||||||
(t ;; Only regexps given.
|
`(src :lang ,lang
|
||||||
(setf regexps args)))
|
:regexps ',regexps)))
|
||||||
(when regexps
|
((keywordp (car args))
|
||||||
;; This feels awkward and wrong, but we have to quote lists
|
;; All plist args.
|
||||||
;; and avoid quoting nil. There must be a better way.
|
`(src ,@(delq nil
|
||||||
(setf regexps `(',regexps)))
|
(append (when (plist-get args :lang)
|
||||||
`(src :lang ,lang :regexps ,@regexps))))
|
(list :lang (plist-get args :lang)))
|
||||||
:preambles ((`(,predicate-names . ,args)
|
(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))
|
(list :regexp (org-ql--format-src-block-regexp (plist-get args :lang))
|
||||||
|
:case-fold t
|
||||||
;; Always check contents with predicate.
|
;; Always check contents with predicate.
|
||||||
:query query)))
|
:query query)))
|
||||||
:body
|
:body
|
||||||
|
|
@ -1728,6 +1965,7 @@ Tests both inherited and local tags."
|
||||||
(org-ql-defpred (tags-all tags&) (&rest tags)
|
(org-ql-defpred (tags-all tags&) (&rest tags)
|
||||||
"Return non-nil if current heading has all of TAGS (a list of strings).
|
"Return non-nil if current heading has all of TAGS (a list of strings).
|
||||||
Tests both inherited and local tags."
|
Tests both inherited and local tags."
|
||||||
|
:coalesce t
|
||||||
;; MAYBE: -all versions for inherited and local.
|
;; MAYBE: -all versions for inherited and local.
|
||||||
:normalizers ((`(,predicate-names . ,tags)
|
:normalizers ((`(,predicate-names . ,tags)
|
||||||
`(and ,@(--map `(tags ,it) tags))))
|
`(and ,@(--map `(tags ,it) tags))))
|
||||||
|
|
|
||||||
685
org-ql.info
685
org-ql.info
File diff suppressed because it is too large
Load diff
|
|
@ -205,22 +205,85 @@ with keyword arg NOW in PLIST."
|
||||||
(expect (org-ql--value-at (point-min) #'org-get-local-tags)
|
(expect (org-ql--value-at (point-min) #'org-get-local-tags)
|
||||||
:to-be nil))))
|
: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 "Query pre-processing"
|
||||||
|
|
||||||
|
(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 "Normalization"
|
||||||
|
|
||||||
|
(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 "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 "(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")))))
|
||||||
|
|
||||||
(describe "(level)"
|
(describe "(level)"
|
||||||
(it "with one level"
|
(it "with one level"
|
||||||
(expect (org-ql--normalize-query '(level "1"))
|
(expect (org-ql--normalize-query '(level "1"))
|
||||||
|
|
@ -251,19 +314,44 @@ with keyword arg NOW in PLIST."
|
||||||
(expect (org-ql--normalize-query '(olp "a." "b"))
|
(expect (org-ql--normalize-query '(olp "a." "b"))
|
||||||
:to-equal '(org-ql--predicate-outline-path "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)"
|
(describe "(tags-inherited)"
|
||||||
|
(it "handles 0 arguments"
|
||||||
(expect (org-ql--normalize-query '(tags-inherited))
|
(expect (org-ql--normalize-query '(tags-inherited))
|
||||||
:to-equal '(tags-inherited))
|
:to-equal '(tags-inherited)))
|
||||||
(expect (org-ql--normalize-query '(itags))
|
(it "handles 1 argument"
|
||||||
:to-equal '(tags-inherited))
|
|
||||||
(expect (org-ql--normalize-query '(tags-inherited "foo"))
|
(expect (org-ql--normalize-query '(tags-inherited "foo"))
|
||||||
:to-equal '(tags-inherited "foo"))
|
:to-equal '(tags-inherited "foo")))
|
||||||
(expect (org-ql--normalize-query '(itags "foo"))
|
(it "handles 2 arguments"
|
||||||
:to-equal '(tags-inherited "foo"))
|
|
||||||
(expect (org-ql--normalize-query '(tags-inherited "foo" "bar"))
|
(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")))
|
: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"
|
(describe "timestamp predicates"
|
||||||
;; NOTE: (clocked) and (closed) don't accept :with-time arguments.
|
;; NOTE: (clocked) and (closed) don't accept :with-time arguments.
|
||||||
|
|
@ -454,27 +542,7 @@ with keyword arg NOW in PLIST."
|
||||||
:with-time t))
|
:with-time t))
|
||||||
(expect (org-ql--normalize-query '(ts :on 1))
|
(expect (org-ql--normalize-query '(ts :on 1))
|
||||||
:to-equal `(ts :from ,(ts-inc 'day 1 beg-of-today-ts)
|
:to-equal `(ts :from ,(ts-inc 'day 1 beg-of-today-ts)
|
||||||
:to ,(ts-inc 'day 1 end-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"))))
|
|
||||||
|
|
||||||
;; FIXME: This test fails, but only on GitHub CI; it works fine
|
;; FIXME: This test fails, but only on GitHub CI; it works fine
|
||||||
;; locally. It seems to be something to do with Buttercup and
|
;; locally. It seems to be something to do with Buttercup and
|
||||||
|
|
@ -504,13 +572,25 @@ with keyword arg NOW in PLIST."
|
||||||
;; :to ,(make-ts :unix 1546408799.0))
|
;; :to ,(make-ts :unix 1546408799.0))
|
||||||
;; (ts :type inactive
|
;; (ts :type inactive
|
||||||
;; :from ,(make-ts :unix 1546322400.0)
|
;; :from ,(make-ts :unix 1546322400.0)
|
||||||
;; :to ,(make-ts :unix 1546408799.0))))
|
;; :to ,(make-ts :unix 1546408799.0)))))
|
||||||
)
|
)))))
|
||||||
|
|
||||||
(describe "Query preambles"
|
(describe "Query preambles"
|
||||||
|
|
||||||
;; TODO: Other predicates.
|
;; TODO: Other predicates.
|
||||||
|
|
||||||
|
(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 "(clocked)"
|
(describe "(clocked)"
|
||||||
(it "without arguments"
|
(it "without arguments"
|
||||||
(expect (org-ql--query-preamble '(clocked))
|
(expect (org-ql--query-preamble '(clocked))
|
||||||
|
|
@ -567,19 +647,14 @@ with keyword arg NOW in PLIST."
|
||||||
(expect (org-ql--query-string-to-sexp "!todo:CHECK,SOMEDAY todo:")
|
(expect (org-ql--query-string-to-sexp "!todo:CHECK,SOMEDAY todo:")
|
||||||
:to-equal '(and (not (todo "CHECK" "SOMEDAY")) (todo)))
|
:to-equal '(and (not (todo "CHECK" "SOMEDAY")) (todo)))
|
||||||
(expect (org-ql--query-string-to-sexp "tags:universe !moon")
|
(expect (org-ql--query-string-to-sexp "tags:universe !moon")
|
||||||
:to-equal '(and (tags "universe") (not (regexp "moon"))))
|
:to-equal '(and (tags "universe") (not (rifle "moon"))))
|
||||||
(expect (org-ql--query-string-to-sexp "!moon tags:universe")
|
(expect (org-ql--query-string-to-sexp "!moon tags:universe")
|
||||||
:to-equal '(and (not (regexp "moon")) (tags "universe")))
|
:to-equal '(and (not (rifle "moon")) (tags "universe")))
|
||||||
(expect (org-ql--query-string-to-sexp "mars !ts:on=today")
|
(expect (org-ql--query-string-to-sexp "mars !ts:on=today")
|
||||||
:to-equal '(and (regexp "mars") (not (ts :on "today"))))
|
:to-equal '(and (rifle "mars") (not (ts :on "today"))))
|
||||||
(expect (org-ql--query-string-to-sexp "!\"quoted phrase\"")
|
(expect (org-ql--query-string-to-sexp "!\"quoted phrase\"")
|
||||||
:to-equal '(not (regexp "quoted phrase"))))
|
:to-equal '(not (rifle "quoted phrase"))))
|
||||||
(it "Regexp predicates"
|
(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")
|
(expect (org-ql--query-string-to-sexp "regexp:word")
|
||||||
:to-equal '(regexp "word"))
|
:to-equal '(regexp "word"))
|
||||||
(expect (org-ql--query-string-to-sexp "regexp:\"quoted phrase\"")
|
(expect (org-ql--query-string-to-sexp "regexp:\"quoted phrase\"")
|
||||||
|
|
@ -663,7 +738,23 @@ with keyword arg NOW in PLIST."
|
||||||
:to-equal "tags:space !regexp:moon"))
|
:to-equal "tags:space !regexp:moon"))
|
||||||
(it "(or ...)"
|
(it "(or ...)"
|
||||||
(expect (org-ql--query-sexp-to-string '(or (tags "book" "books") (priority "A")))
|
(expect (org-ql--query-sexp-to-string '(or (tags "book" "books") (priority "A")))
|
||||||
:to-equal nil)))
|
: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"
|
(describe "Query results"
|
||||||
|
|
||||||
|
|
@ -719,7 +810,7 @@ with keyword arg NOW in PLIST."
|
||||||
(org-ql-expect ('(descendants (todo "CHECK")))
|
(org-ql-expect ('(descendants (todo "CHECK")))
|
||||||
'("Recurring")))
|
'("Recurring")))
|
||||||
(org-ql-it "with granddescendants query"
|
(org-ql-it "with granddescendants query"
|
||||||
(org-ql-expect ('(descendants (descendants "moon")))
|
(org-ql-expect ('(descendants (descendants (regexp "moon"))))
|
||||||
'("Take over the universe")))
|
'("Take over the universe")))
|
||||||
(org-ql-it "with query that should not match parent"
|
(org-ql-it "with query that should not match parent"
|
||||||
;; This test would fail if the `descendants' predicate did not properly exclude
|
;; 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-it ">= a priority"
|
||||||
(org-ql-expect ('(priority >= "B"))
|
(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"))))
|
'("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)"
|
(describe "(property)"
|
||||||
|
|
||||||
;; MAYBE: Add support for (property) without arguments.
|
;; MAYBE: Add support for (property) without arguments.
|
||||||
|
|
@ -1192,16 +1284,6 @@ with keyword arg NOW in PLIST."
|
||||||
:sort 'todo)
|
:sort 'todo)
|
||||||
'("Take over the universe")))
|
'("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"
|
(org-ql-it "case-folding predicate with non-case-folding preamble"
|
||||||
;; e.g. the (todo) predicate disables case-folding in its preamble, but that
|
;; 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).
|
;; should not prevent case-folding in this and other predicates (issue #114).
|
||||||
|
|
@ -1209,6 +1291,19 @@ with keyword arg NOW in PLIST."
|
||||||
:sort 'todo)
|
:sort 'todo)
|
||||||
'("Take over the universe" "Take over the world" "Take over Mars" "Take over the moon" "Get haircut"))))
|
'("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)"
|
(describe "(scheduled)"
|
||||||
|
|
||||||
(org-ql-it "without arguments"
|
(org-ql-it "without arguments"
|
||||||
|
|
@ -2105,7 +2200,7 @@ with keyword arg NOW in PLIST."
|
||||||
(it "Refuses to link to non-file-backed buffer"
|
(it "Refuses to link to non-file-backed buffer"
|
||||||
(expect (var-after-link-save-open 'org-ql-view-buffers-files link-buffer query
|
(expect (var-after-link-save-open 'org-ql-view-buffers-files link-buffer query
|
||||||
:buffer link-buffer)
|
: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.
|
;; MAYBE: Also test `org-ql-views', although I already know it works now.
|
||||||
;; (describe "org-ql-views")
|
;; (describe "org-ql-views")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue