Skip to content

Commit

Permalink
More sensible scheduled & deadline predicates
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmed-shariff committed Apr 30, 2024
1 parent 2bd7c59 commit d8f217c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ Example configuration:
- =level (LEVEL)= :: Tests if the ~level~ of a node is equal to ~LEVEL~.
- =todo (REGEXP &optional EXACT)= :: Similar to ~file~, tests the todo state of a node.
- =priority (REGEXP &optional EXACT)= :: Similar to ~file~, tests the priority of a node.
- =scheduled= :: TBD
- =deadline= :: TBD
- =scheduled (COMPARISON TIME-STRING)= :: Compares the ~scheduled~ of the node with ~TIME-STAMP~ based on ~COMPARISON~. ~TIME-STAMP~ is any valid value for [[https://orgmode.org/manual/The-date_002ftime-prompt.html][org date-time prompt]]. ~COMPARISON~ is either ~<~ or ~>~. Example: ~(scheduled > "-3w")~
- =deadline= :: Same as ~scheduled~, tests the ~deadline~ of a node.
- =title (REGRXP &optional EXACT)= :: Similar to ~file~, tests the title of a node.
- =properties (PROP PROP-VAL)= :: Tests if the value of the property of a node PROP is a match to PROP-VAL. PROP-VAL can be a regular expression.
- =tags (TAG1 TAG2 ...)= :: Tests if the tags of a node have TAG1, TAG2, etc.
Expand Down
34 changes: 32 additions & 2 deletions org-roam-ql.el
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,36 @@ backlinks"
"Return the value of calling the function F with VALUE as its parameter."
(funcall f value))

(defun org-roam-ql--read-date-to-ts (read-date)
"Convert org date prompt to timestamp."
(time-convert
(encode-time (org-read-date-analyze read-date
(current-time)
(decode-time (current-time))))
'integer))

(defun org-roam-ql--time-convert-to-ts (time)
"Convert TIME to ts.
This uses `org-time-string-to-seconds' or `time-convert' based on the type."
(cond
((stringp time) (org-time-string-to-seconds time))
((and (not (null time)) (listp time)) (time-convert time 'integer))
(t (user-error "Unknown value for `time'"))))

(defun org-roam-ql--predicate-compare-time (value comparison time-string)
"Compare VALUE to TIME-STRING based on COMPARISON.
VALUE is a time-string (see `org-time-string-to-seconds' or valid value for `time-convert').
TIME-STRING is any valid value for a org date/time prompt.
COMPARISON can be either '< or '>"
(when value
(let* ((val1 (org-roam-ql--time-convert-to-ts value))
(val2 (org-roam-ql--read-date-to-ts time-string))
(time-less (time-less-p val1 val2)))
(pcase comparison
('< time-less)
('> (not time-less))
(_ (user-error "Unknown value for `comparison'. Should be '< or '>"))))))

;; *****************************************************************************
;; Functions for sorting
;; *****************************************************************************
Expand Down Expand Up @@ -1033,8 +1063,8 @@ Can be used in the minibuffer or when writting querries."
(point "Compare to `point' of a node" org-roam-node-point . equal)
(todo "Compare to `todo' of a node" org-roam-node-todo . org-roam-ql--predicate-s-match)
(priority "Compare to `priority' of a node" org-roam-node-priority . org-roam-ql--predicate-s-match)
(scheduled "Compare to `scheduled' of a node" org-roam-node-scheduled . time-less-p)
(deadline "Compare to `deadline' of a node" org-roam-node-deadline . time-less-p)
(scheduled "Compare `scheduled' of a node to arg based on comparison parsed (< or >)" org-roam-node-scheduled . org-roam-ql--predicate-compare-time)
(deadline "Compare `deadline' of a node to arg based on comparison parsed (< or >)" org-roam-node-deadline . org-roam-ql--predicate-time-less-p)
(title "Compare to `title' of a node" org-roam-node-title . org-roam-ql--predicate-s-match)
(properties "Compare to `properties' of a node"
org-roam-node-properties . org-roam-ql--predicate-property-match)
Expand Down

0 comments on commit d8f217c

Please sign in to comment.