agd-core 0.2.0

Org-mode parsing and task operations for agd
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! Emacs's tags/TODO match syntax (`work-someday|home/!NEXT`), with property
//! terms (`TODO="NEXT"`, `Effort<15`, `CLOSED>="<-3w>"`) compared as Org
//! compares them.
//!
//! Regexp terms (`{...}`) are refused with `invalid_match` rather than
//! guessed at. A property is read from the heading itself, not inherited, as
//! Org's matcher does by default.

use std::cmp::Ordering;
use std::collections::BTreeSet;
use std::sync::LazyLock;

use chrono::{Duration, Local, NaiveDate, NaiveDateTime, NaiveTime};
use regex::Regex;

use crate::{AgdError, ErrorCode, HeadlineSpan, PlanningTimestamp, Settings};

#[derive(Clone, Debug)]
// Emacs tags/todo match syntax: `|`-separated alternatives of terms joined by
// `&`, `+`, `-` or nothing, then an optional `/` TODO part (`NEXT|WAIT`,
// `-DONE`, a leading `!` for not-done states). A term is a tag (`work`) or a
// property comparison (`TODO="NEXT"`, `Effort<15`, `CLOSED>="<-3w>"`) with
// Org's semantics; regexp terms (`{...}`) are rejected explicitly.
pub(super) struct TagTodoMatcher {
    alternatives: Vec<TagTerms>,
    todo_include: BTreeSet<String>,
    todo_exclude: BTreeSet<String>,
    todo_not_done: bool,
}

#[derive(Clone, Debug, Default)]
struct TagTerms {
    include: BTreeSet<String>,
    exclude: BTreeSet<String>,
    properties: Vec<PropertyTerm>,
}

#[derive(Clone, Debug)]
struct PropertyTerm {
    negated: bool,
    /// Upcased, as Org compares property names.
    name: String,
    op: Ordering,
    /// `=`/`<>` hold on equal/unequal; `<=`/`>=` also hold on equal.
    or_equal: bool,
    not_equal: bool,
    value: PropertyValue,
    /// `OP*`: the property must exist.
    require_present: bool,
}

#[derive(Clone, Debug)]
enum PropertyValue {
    Number(f64),
    Text(String),
    /// The text inside `"<...>"`, resolved against the match's now.
    Time(String),
}

// org-make-tags-matcher treats a quoted value as a time when it starts with
// a digit, now, today, tomorrow or a [+-]N[dmwy] offset inside <> or [].
static MATCHER_TIME: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"^[\[<](?:[0-9]+|now|today|tomorrow|[+-][0-9]+[dmwy]).*[\]>]$").unwrap()
});
static TIME_OFFSET: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"^([+-][0-9]+)([hdwmy])$").unwrap());
static TIMESTAMP_DATE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"(\d{4})-(\d{2})-(\d{2})(?:[^\]>\d]*?(\d{1,2}):(\d{2}))?").unwrap()
});
static LEADING_NUMBER: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"^\s*[-+]?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?").unwrap());

/// The match's "now" for `<now>`: the agenda day at the current clock time.
pub(super) fn matcher_now(today: NaiveDate) -> NaiveDateTime {
    today.and_time(Local::now().time())
}

impl TagTodoMatcher {
    pub(super) fn parse(input: &str) -> Result<Self, AgdError> {
        let (tags, todo) = input.split_once('/').unwrap_or((input, ""));
        let mut alternatives = Vec::new();
        if !tags.trim().is_empty() {
            for alternative in tags.split('|') {
                alternatives.push(parse_tag_terms(alternative, input)?);
            }
        }
        let todo_not_done = todo.starts_with('!');
        let mut todo_include = BTreeSet::new();
        let mut todo_exclude = BTreeSet::new();
        for term in todo.trim_start_matches('!').split('|') {
            let term = term.trim();
            if let Some(keyword) = term.strip_prefix('-') {
                todo_exclude.insert(keyword.to_string());
            } else if !term.is_empty() {
                todo_include.insert(term.trim_start_matches('+').to_string());
            }
        }
        Ok(Self {
            alternatives,
            todo_include,
            todo_exclude,
            todo_not_done,
        })
    }

    pub(super) fn matches(
        &self,
        headline: &HeadlineSpan,
        now: NaiveDateTime,
        settings: &Settings,
    ) -> bool {
        let item = &headline.item;
        let tags: BTreeSet<&str> = item
            .tags
            .iter()
            .chain(&item.inherited_tags)
            .map(String::as_str)
            .collect();
        let tags_match = self.alternatives.is_empty()
            || self.alternatives.iter().any(|terms| {
                terms.include.iter().all(|tag| tags.contains(tag.as_str()))
                    && terms.exclude.iter().all(|tag| !tags.contains(tag.as_str()))
                    && terms
                        .properties
                        .iter()
                        .all(|term| term.negated != term.holds(headline, now, settings))
            });
        let todo = item.todo.as_deref();
        tags_match
            && (!self.todo_not_done || (todo.is_some() && !item.done))
            && (self.todo_include.is_empty()
                || todo.is_some_and(|todo| self.todo_include.contains(todo)))
            && todo.is_none_or(|todo| !self.todo_exclude.contains(todo))
    }
}

impl PropertyTerm {
    fn holds(&self, headline: &HeadlineSpan, now: NaiveDateTime, settings: &Settings) -> bool {
        let actual = property_value(headline, &self.name, settings);
        if self.require_present && actual.is_none() {
            return false;
        }
        let actual = actual.unwrap_or_default();
        let ordering = match &self.value {
            PropertyValue::Number(value) => leading_number(&actual).partial_cmp(value),
            PropertyValue::Text(value) => Some(actual.as_str().cmp(value)),
            // org-time* compare float times and fail when either side is
            // missing or unparseable, whatever the operator.
            PropertyValue::Time(value) => match (timestamp_time(&actual), matcher_time(value, now))
            {
                (Some(actual), Some(value)) => Some(actual.cmp(&value)),
                _ => return false,
            },
        };
        let Some(ordering) = ordering else {
            return false;
        };
        if self.not_equal {
            ordering != Ordering::Equal
        } else {
            ordering == self.op || (self.or_equal && ordering == Ordering::Equal)
        }
    }
}

/// The value Org's matcher reads for NAME: LEVEL, CATEGORY and TODO are the
/// heading's; PRIORITY falls back to the default priority; planning lines
/// give their timestamps; anything else is the heading's own property
/// drawer entry (Org's matcher does not inherit properties by default).
fn property_value(headline: &HeadlineSpan, name: &str, settings: &Settings) -> Option<String> {
    let item = &headline.item;
    let planning = |timestamp: &Option<PlanningTimestamp>, open: char, close: char| {
        timestamp.as_ref().map(|timestamp| {
            timestamp
                .raw
                .clone()
                .unwrap_or_else(|| format!("{open}{}{close}", timestamp.date))
        })
    };
    match name {
        "LEVEL" => Some(headline.level.to_string()),
        "CATEGORY" => Some(item.category.clone()),
        "TODO" => item.todo.clone(),
        "PRIORITY" => Some(
            item.priority
                .clone()
                .unwrap_or_else(|| settings.default_priority.clone()),
        ),
        "SCHEDULED" => planning(&item.planning.scheduled, '<', '>'),
        "DEADLINE" => planning(&item.planning.deadline, '<', '>'),
        "CLOSED" => planning(&item.planning.closed, '[', ']'),
        _ => headline
            .properties
            .iter()
            .find(|property| property.key.eq_ignore_ascii_case(name))
            .map(|property| property.value.trim().to_string()),
    }
}

/// Emacs's string-to-number: the leading number, or 0.
fn leading_number(value: &str) -> f64 {
    LEADING_NUMBER
        .find(value)
        .and_then(|number| number.as_str().trim().parse().ok())
        .unwrap_or(0.0)
}

/// The first Org timestamp in VALUE, at midnight when it has no time.
fn timestamp_time(value: &str) -> Option<NaiveDateTime> {
    let captures = TIMESTAMP_DATE.captures(value)?;
    let number = |index: usize| -> Option<u32> { captures.get(index)?.as_str().parse().ok() };
    let date = NaiveDate::from_ymd_opt(captures[1].parse().ok()?, number(2)?, number(3)?)?;
    let time = NaiveTime::from_hms_opt(number(4).unwrap_or(0), number(5).unwrap_or(0), 0)?;
    Some(date.and_time(time))
}

/// org-matcher-time: now, today, tomorrow, an offset from today's midnight
/// (h from now; a month is 31 days, a year 365.25), or a timestamp.
fn matcher_time(value: &str, now: NaiveDateTime) -> Option<NaiveDateTime> {
    let inner = &value[1..value.len() - 1];
    let midnight = now.date().and_time(NaiveTime::MIN);
    match inner {
        "now" => return Some(now),
        "today" => return Some(midnight),
        "tomorrow" => return Some(midnight + Duration::days(1)),
        _ => {}
    }
    if let Some(captures) = TIME_OFFSET.captures(inner) {
        let count: f64 = captures[1].parse().ok()?;
        let (base, seconds) = match &captures[2] {
            "h" => (now, 3_600.0),
            "d" => (midnight, 86_400.0),
            "w" => (midnight, 7.0 * 86_400.0),
            "m" => (midnight, 31.0 * 86_400.0),
            _ => (midnight, 365.25 * 86_400.0),
        };
        return Some(base + Duration::seconds((count * seconds) as i64));
    }
    timestamp_time(inner)
}

fn parse_tag_terms(alternative: &str, input: &str) -> Result<TagTerms, AgdError> {
    let invalid =
        |message: String| AgdError::new(ErrorCode::InvalidMatch, format!("{message} in '{input}'"));
    let mut terms = TagTerms::default();
    let mut chars = alternative.trim().chars().peekable();
    while let Some(&next) = chars.peek() {
        let include = match next {
            '+' | '&' => {
                chars.next();
                if next == '&' {
                    continue;
                }
                true
            }
            '-' => {
                chars.next();
                false
            }
            _ => true,
        };
        if chars.peek() == Some(&'{') {
            return Err(invalid(
                "regexp matches ({...}) are not supported".to_string(),
            ));
        }
        let mut tag = String::new();
        while let Some(&character) = chars.peek() {
            if character.is_alphanumeric() || matches!(character, '_' | '@' | '#' | '%') {
                tag.push(character);
                chars.next();
            } else {
                break;
            }
        }
        if tag.is_empty() {
            let found = chars.peek().map_or("end".to_string(), |c| format!("'{c}'"));
            return Err(invalid(format!(
                "expected a tag or property before {found}"
            )));
        }
        if chars
            .peek()
            .is_some_and(|c| matches!(c, '=' | '<' | '>' | '!'))
        {
            let term = parse_property_term(&tag, !include, &mut chars).map_err(invalid)?;
            terms.properties.push(term);
        } else if include {
            terms.include.insert(tag);
        } else {
            terms.exclude.insert(tag);
        }
    }
    Ok(terms)
}

/// Reads `OP[*]VALUE` after a property NAME.
fn parse_property_term(
    name: &str,
    negated: bool,
    chars: &mut std::iter::Peekable<std::str::Chars<'_>>,
) -> Result<PropertyTerm, String> {
    let mut op = String::new();
    while let Some(&character) = chars.peek() {
        if matches!(character, '=' | '<' | '>' | '!') {
            op.push(character);
            chars.next();
        } else {
            break;
        }
    }
    let (op_ordering, or_equal, not_equal) = match op.as_str() {
        "=" | "==" => (Ordering::Equal, false, false),
        "<>" | "!=" => (Ordering::Equal, false, true),
        "<" => (Ordering::Less, false, false),
        "<=" | "=<" => (Ordering::Less, true, false),
        ">" => (Ordering::Greater, false, false),
        ">=" | "=>" => (Ordering::Greater, true, false),
        _ => return Err(format!("unknown operator '{op}' after {name}")),
    };
    let require_present = chars.next_if_eq(&'*').is_some();
    let value = match chars.peek() {
        Some('"') => {
            chars.next();
            let mut text = String::new();
            loop {
                match chars.next() {
                    Some('"') => break,
                    Some(character) => text.push(character),
                    None => return Err(format!("unterminated string after {name}{op}")),
                }
            }
            if MATCHER_TIME.is_match(&text) {
                PropertyValue::Time(text)
            } else {
                PropertyValue::Text(text)
            }
        }
        Some('{') => return Err("regexp matches ({...}) are not supported".to_string()),
        _ => {
            let mut number = String::new();
            while let Some(&character) = chars.peek() {
                if character.is_ascii_digit()
                    || matches!(character, '.' | 'e' | 'E')
                    || (character == '-' && (number.is_empty() || number.ends_with(['e', 'E'])))
                {
                    number.push(character);
                    chars.next();
                } else {
                    break;
                }
            }
            if number.is_empty() {
                return Err(format!("expected a \"string\" or number after {name}{op}"));
            }
            PropertyValue::Number(leading_number(&number))
        }
    };
    Ok(PropertyTerm {
        negated,
        name: name.to_uppercase(),
        op: op_ordering,
        or_equal,
        not_equal,
        value,
        require_present,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::agenda::test_support::fixture;

    #[test]
    fn tags_todo_match_follows_emacs_syntax() {
        let (_temp, index) = fixture(
            "#+TODO: TODO NEXT WAIT | DONE\n* TODO A :work:\n* NEXT B :work:urgent:\n* WAIT C :home:\n* DONE D :home:\n* TODO E :work:someday:\n",
        );
        let titles = |expression: &str| -> Vec<String> {
            let matcher = TagTodoMatcher::parse(expression).unwrap();
            let now = NaiveDate::from_ymd_opt(2026, 10, 13)
                .unwrap()
                .and_hms_opt(12, 0, 0)
                .unwrap();
            index
                .documents()
                .flat_map(|document| document.headlines())
                .filter(|headline| matcher.matches(headline, now, index.settings()))
                .map(|headline| headline.item.title.clone())
                .collect()
        };
        assert_eq!(titles("work"), ["A", "B", "E"]);
        assert_eq!(titles("work-someday"), ["A", "B"]);
        assert_eq!(titles("work&urgent|home"), ["B", "C", "D"]);
        assert_eq!(titles("+work/NEXT|TODO"), ["A", "B", "E"]);
        assert_eq!(titles("home/!"), ["C"]);
        assert_eq!(titles("/-WAIT"), ["A", "B", "D", "E"]);
        assert_eq!(titles("work+TODO<>\"NEXT\""), ["A", "E"]);
        assert_eq!(titles("PRIORITY=\"B\"+home"), ["C", "D"]);
        assert_eq!(
            TagTodoMatcher::parse("TODO={NE}")
                .err()
                .unwrap()
                .code()
                .as_str(),
            "invalid_match"
        );
    }
}