aboutsummaryrefslogtreecommitdiffstats
path: root/src/recur.c
diff options
context:
space:
mode:
authorLars Henriksen <LarsHenriksen@get2net.dk>2020-10-07 19:51:18 +0200
committerLukas Fleischer <lfleischer@calcurse.org>2020-10-11 09:59:35 -0400
commit95151e3f0c2a9935abe6e498fd056a57075ea0c5 (patch)
treea989360f2c061902c363a92e00c233f77f735822 /src/recur.c
parent4d9d50722a57be05bd3ba296721e0faf935c5b59 (diff)
downloadcalcurse-95151e3f0c2a9935abe6e498fd056a57075ea0c5.tar.gz
calcurse-95151e3f0c2a9935abe6e498fd056a57075ea0c5.zip
Add p(revious) command
The p(rev) command finds the previous occurrence of a recurrent item, analogous to the n(ext) command. A bug in the next command is corrected.. Signed-off-by: Lars Henriksen <LarsHenriksen@get2net.dk> Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
Diffstat (limited to 'src/recur.c')
-rw-r--r--src/recur.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/recur.c b/src/recur.c
index 3c93de2..3a16d1e 100644
--- a/src/recur.c
+++ b/src/recur.c
@@ -1846,3 +1846,27 @@ int recur_nth_occurrence(time_t s, long d, struct rpt *r, llist_t *e, int n,
}
return !n;
}
+
+/*
+ * Finds the previous occurrence - the most recent before day - and returns it
+ * in the provided buffer.
+ */
+int recur_prev_occurrence(time_t s, long d, struct rpt *r, llist_t *e,
+ time_t day, time_t *prev)
+{
+ time_t prev_day, next;
+
+ if (day <= update_time_in_date(s, 0, 0))
+ return 0;
+ next = *prev = s;
+ while (update_time_in_date(next, 0, 0) < day) {
+ /* Set new previous and next. */
+ *prev = next;
+ prev_day = update_time_in_date(*prev, 0, 0);
+ recur_next_occurrence(s, d, r, e, prev_day, &next);
+ /* Multi-day appointment */
+ if (next == *prev)
+ next = NEXTDAY(*prev);
+ }
+ return 1;
+}