Change/Fix: (src) predicate

Use keyword args, fix non-sexp syntax and searching.
This commit is contained in:
Adam Porter 2019-10-09 17:21:47 -05:00
parent af0ef304e1
commit 6b2004e678
3 changed files with 60 additions and 37 deletions

View file

@ -154,7 +154,7 @@ An =org-ql= query is a lisp form which may contain arbitrary lisp forms, as well
The command =org-ql-search= also accepts, and the command =helm-org-ql= only accepts, an alternative, non-sexp query syntax. The syntax is simple, and a few examples of queries in both syntaxes should suffice. By default, when multiple predicates are used, they are combined with boolean =and=. The command =org-ql-search= also accepts, and the command =helm-org-ql= only accepts, an alternative, non-sexp query syntax. The syntax is simple, and a few examples of queries in both syntaxes should suffice. By default, when multiple predicates are used, they are combined with boolean =and=.
| Sexp syntax | Non-sexp syntax | | Sexp syntax | Non-sexp syntax |
|-------------------------------------------------+-----------------------------------------| |-------------------------------------------------+----------------------------------------------|
| ~(todo)~ | ~todo:~ | | ~(todo)~ | ~todo:~ |
| ~(todo "SOMEDAY")~ | ~todo:SOMEDAY~ | | ~(todo "SOMEDAY")~ | ~todo:SOMEDAY~ |
| ~(todo "SOMEDAY" "WAITING")~ | ~todo:SOMEDAY,WAITING~ | | ~(todo "SOMEDAY" "WAITING")~ | ~todo:SOMEDAY,WAITING~ |
@ -163,6 +163,7 @@ The command =org-ql-search= also accepts, and the command =helm-org-ql= only acc
| ~(clocked :on -1)~ | ~clocked:on=-1~ | | ~(clocked :on -1)~ | ~clocked:on=-1~ |
| ~(heading "quoted phrase" "word")~ | ~heading:"quoted phrase",word~ | | ~(heading "quoted phrase" "word")~ | ~heading:"quoted phrase",word~ |
| ~(and (tags "book" "books") (priority "A"))~ | ~tags:book,books priority:A~ | | ~(and (tags "book" "books") (priority "A"))~ | ~tags:book,books priority:A~ |
| ~(src :lang "elisp" :regexps ("defun"))~ | ~src:defun,lang=elisp~ or ~src:lang=elisp,defun~ |
| ~(priority >= B)~ | ~priority:A,B~ | | ~(priority >= B)~ | ~priority:A,B~ |
Note that the =priority= predicate does not support comparators in the non-sexp syntax, so multiple priorities should be passed instead, as seen in the last example. Note that the =priority= predicate does not support comparators in the non-sexp syntax, so multiple priorities should be passed instead, as seen in the last example.
@ -189,7 +190,7 @@ Arguments are listed next to predicate names, where applicable.
+ =priority (&optional comparator-or-priority priority)= :: Return non-nil if current heading has a certain priority. ~COMPARATOR-OR-PRIORITY~ should be either a comparator function, like ~<=~, or a priority string, like "A" (in which case (~=~ will be the comparator). If ~COMPARATOR-OR-PRIORITY~ is a comparator, ~PRIORITY~ should be a priority string. If both arguments are nil, return non-nil if heading has any defined priority. + =priority (&optional comparator-or-priority priority)= :: Return non-nil if current heading has a certain priority. ~COMPARATOR-OR-PRIORITY~ should be either a comparator function, like ~<=~, or a priority string, like "A" (in which case (~=~ will be the comparator). If ~COMPARATOR-OR-PRIORITY~ is a comparator, ~PRIORITY~ should be a priority string. If both arguments are nil, return non-nil if heading has any defined priority.
+ =property (property &optional value)= :: Return non-nil if current entry has ~PROPERTY~ (a string), and 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)= :: 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)~.
+ =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.
+ ~src (&optional 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. + ~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.
+ =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~.

View file

@ -591,6 +591,26 @@ Replaces bare strings with (regexp) selectors, and appropriate
(setf property (substring (symbol-name property) 1))) (setf property (substring (symbol-name property) 1)))
(cons 'property (cons property value))) (cons 'property (cons property value)))
;; Source blocks.
(`(src . ,args)
;; Rewrite to use keyword args.
(-let (regexps lang keyword-index)
(cond ((plist-get args :lang)
;; Lang given first, or only lang given.
(setf lang (plist-get args :lang)
regexps (seq-difference args (list :lang lang))))
((setf keyword-index (-find-index #'keywordp args))
;; Regexps and lang given.
(setf lang (plist-get (cl-subseq args keyword-index) :lang)
regexps (cl-subseq args 0 keyword-index)))
(t ;; Only regexps given.
(setf regexps args)))
(when regexps
;; This feels awkward and wrong, but we have to quote lists
;; and avoid quoting nil. There must be a better way.
(setf regexps `(',regexps)))
`(src :lang ,lang :regexps ,@regexps)))
;; Tags. ;; Tags.
(`(,(or 'tags-all 'tags&) . ,tags) `(and ,@(--map `(tags ,it) tags))) (`(,(or 'tags-all 'tags&) . ,tags) `(and ,@(--map `(tags ,it) tags)))
;; MAYBE: -all versions for inherited and local. ;; MAYBE: -all versions for inherited and local.
@ -747,8 +767,8 @@ replace the clause with a preamble."
;; element) ;; element)
;; Src blocks. ;; Src blocks.
(`(src ,lang . ,_) (`(src . ,args)
(setq org-ql-preamble (org-ql--format-src-block-regexp lang)) (setq org-ql-preamble (org-ql--format-src-block-regexp (plist-get args :lang)))
;; Always check contents with predicate. ;; Always check contents with predicate.
element) element)
@ -795,7 +815,7 @@ replace the clause with a preamble."
(rx-to-string `(seq bol (group (zero-or-more (any " "))) (rx-to-string `(seq bol (group (zero-or-more (any " ")))
"#+begin_src" "#+begin_src"
(one-or-more (any " ")) (one-or-more (any " "))
,lang ,(or lang `(1+ (not (any " \n \f "))))
(zero-or-more (any " ")) (zero-or-more (any " "))
(group (or (seq (zero-or-more (not (any "\n\":"))) (group (or (seq (zero-or-more (not (any "\n\":")))
"\"" "\""
@ -1130,9 +1150,10 @@ priority B)."
;; Check that PROPERTY has VALUE ;; Check that PROPERTY has VALUE
(string-equal value (org-entry-get (point) property 'selective))))))) (string-equal value (org-entry-get (point) property 'selective)))))))
(org-ql--defpred src (&optional lang &rest regexps) (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 LANG is non-nil, the block must be in that language." If keyword argument LANG is non-nil, the block must be in that
language."
(catch 'return (catch 'return
(save-excursion (save-excursion
(save-match-data (save-match-data

View file

@ -329,7 +329,7 @@ default, when multiple predicates are used, they are combined with
boolean and. boolean and.
Sexp syntax Non-sexp syntax Sexp syntax Non-sexp syntax
------------------------------------------------------------------------------------------------ -------------------------------------------------------------------------------------------------------
(todo) todo: (todo) todo:
(todo "SOMEDAY") todo:SOMEDAY (todo "SOMEDAY") todo:SOMEDAY
(todo "SOMEDAY" "WAITING") todo:SOMEDAY,WAITING (todo "SOMEDAY" "WAITING") todo:SOMEDAY,WAITING
@ -338,6 +338,7 @@ Sexp syntax Non-sexp syntax
(clocked :on -1) clocked:on=-1 (clocked :on -1) clocked:on=-1
(heading "quoted phrase" "word") heading:"quoted phrase",word (heading "quoted phrase" "word") heading:"quoted phrase",word
(and (tags "book" "books") (priority "A")) tags:book,books priority:A (and (tags "book" "books") (priority "A")) tags:book,books priority:A
(src :lang "elisp" :regexps ("defun")) src:defun,lang=elisp or src:lang=elisp,defun
(priority >= B) priority:A,B (priority >= B) priority:A,B
Note that the priority predicate does not support comparators in the Note that the priority predicate does not support comparators in the
@ -411,7 +412,7 @@ Arguments are listed next to predicate names, where applicable.
Return non-nil if current entry matches all of REGEXPS (regexp Return non-nil if current entry matches all of REGEXPS (regexp
strings). Matches against entire entry, from beginning of its strings). Matches against entire entry, from beginning of its
heading to the next heading. heading to the next heading.
src (&optional lang regexps) src (&key lang regexps)
Return non-nil if current entry contains an Org Babel source block. Return non-nil if current entry contains an Org Babel source block.
If LANG is non-nil, match blocks of that language. If REGEXPS If LANG is non-nil, match blocks of that language. If REGEXPS
is non-nil, require that blocks contents match all regexps. is non-nil, require that blocks contents match all regexps.
@ -979,24 +980,24 @@ Node: org-ql-view-recent-items6452
Node: org-ql-sparse-tree6936 Node: org-ql-sparse-tree6936
Node: Queries7736 Node: Queries7736
Node: Non-sexp query syntax8578 Node: Non-sexp query syntax8578
Node: Predicates10078 Node: Predicates10198
Node: Date/time predicates15197 Node: Date/time predicates15312
Node: Functions / Macros17832 Node: Functions / Macros17947
Node: Agenda-like views18019 Node: Agenda-like views18134
Node: Listing / acting-on results19424 Node: Listing / acting-on results19539
Node: Changelog24026 Node: Changelog24141
Node: 04-pre24548 Node: 04-pre24663
Node: 03125492 Node: 03125607
Node: 0325690 Node: 0325805
Node: 02328663 Node: 02328778
Node: 02228889 Node: 02229004
Node: 02129155 Node: 02129270
Node: 0229352 Node: 0229467
Node: 0133385 Node: 0133500
Node: Notes33484 Node: Notes33599
Node: Comparison with Org Agenda searches33646 Node: Comparison with Org Agenda searches33761
Node: org-sidebar34517 Node: org-sidebar34632
Node: License34796 Node: License34911
 
End Tag Table End Tag Table