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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
//! Capture templates: the `%^{prompt}` fields a template asks for
//! ([`capture_prompts`]), and the entry text it renders for a draft, body and
//! answers ([`render_capture_template`]), expanding org-capture's escapes
//! (`%?`, `%i`, `%U`, `%u`, `%T`, `%t`, `%^g`, date prompts) and merging the
//! draft's dates into one planning line.
//!
//! Title, body and answers are inserted literally and never expanded again,
//! and a `%(sexp)` form is refused: a template never runs code. Where the
//! entry goes in a file is the write engine's placement, not this module's.

use std::collections::BTreeMap;
use std::sync::LazyLock;

use chrono::{NaiveDate, NaiveDateTime};
use regex::Regex;

use crate::date::format_inactive_timestamp;
use crate::write::line_ending;
use crate::{
    AgdError, CaptureDraft, CaptureTemplate, ErrorCode, format_active_timestamp, parse_date_input,
};

static ESCAPE_RE: LazyLock<Regex> = LazyLock::new(|| {
    // %^{name}t/T/u/U and a bare %^t/T/u/U prompt for a date, as Org does.
    Regex::new(r"%(\?|i|U|u|T|t|%|\^g|\^\{([^}]+)\}([tTuU])?|\^([tTuU]))")
        .expect("capture escape regex")
});

/// One `%^{...}` field a template asks for. A date prompt (`%^{name}t`,
/// `%^t`, ...) takes the same input as `due:` and may be left empty.
/// `planning` names the keyword (`DEADLINE`, `SCHEDULED`) when the prompt
/// is that keyword's date on the template's planning line: `due:` or `on:`
/// then answers it, so the prompt need not be asked.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CapturePrompt {
    pub name: String,
    pub date: bool,
    pub planning: Option<String>,
}

impl CapturePrompt {
    /// True when the capture line's own `due:`/`on:` already gives this
    /// prompt's date.
    pub fn answered_by(&self, draft: &CaptureDraft) -> bool {
        match self.planning.as_deref() {
            Some("DEADLINE") => draft.deadline.is_some(),
            Some("SCHEDULED") => draft.scheduled.is_some(),
            _ => false,
        }
    }
}

/// The `%^{prompt}` fields a template asks for, in order and without
/// duplicates; `%^g` asks for "tags" and a bare `%^t` for "date".
pub fn capture_prompts(template: &CaptureTemplate) -> Vec<CapturePrompt> {
    let text = &template.template;
    let mut prompts: Vec<CapturePrompt> = Vec::new();
    for captures in ESCAPE_RE.captures_iter(text) {
        let start = captures.get(0).expect("escape match").start();
        let line_start = text[..start].rfind('\n').map_or(0, |index| index + 1);
        let before = &text[line_start..start];
        // The planning keyword closest before the prompt on its line.
        let planning = ["DEADLINE:", "SCHEDULED:"]
            .iter()
            .filter_map(|keyword| before.rfind(keyword).map(|at| (at, *keyword)))
            .max()
            .map(|(_, keyword)| keyword.trim_end_matches(':').to_string());
        let prompt = match (&captures[1], captures.get(2), captures.get(4)) {
            ("^g", _, _) => CapturePrompt {
                name: "tags".to_string(),
                date: false,
                planning: None,
            },
            (_, Some(name), _) => {
                let date = captures.get(3).is_some();
                CapturePrompt {
                    name: name.as_str().to_string(),
                    date,
                    planning: if date { planning } else { None },
                }
            }
            (_, None, Some(_)) => CapturePrompt {
                name: "date".to_string(),
                date: true,
                planning,
            },
            _ => continue,
        };
        if !prompts.iter().any(|existing| existing.name == prompt.name) {
            prompts.push(prompt);
        }
    }
    prompts
}

pub fn render_capture_template(
    template: &CaptureTemplate,
    draft: &CaptureDraft,
    body: &str,
    prompts: &BTreeMap<String, String>,
    now: NaiveDateTime,
) -> Result<String, AgdError> {
    if template.template.contains("%(") {
        return Err(AgdError::new(
            ErrorCode::InvalidTemplate,
            "capture templates may not execute %(sexp) forms",
        ));
    }
    let rendered = expand_capture_escapes(&template.template, draft, body, prompts, now)?;

    let newline = line_ending(&rendered);
    let mut lines: Vec<String> = rendered.split(newline).map(str::to_string).collect();
    if let Some(first) = lines.first_mut() {
        if let Some(priority) = &draft.priority {
            insert_before_title(first, &format!("[#{}]", priority));
        }
        if !draft.tags.is_empty() {
            first.push_str(&format!(" :{}:", draft.tags.join(":")));
        }
    }
    // One planning line, as Org writes it: SCHEDULED then DEADLINE. A
    // template's own planning line (often `DEADLINE: %^{deadline}t`) is
    // merged with the due:/on: dates, which win; a keyword left without a
    // date is dropped.
    let template_planning = lines
        .get(1)
        .filter(|line| is_schedule_or_deadline_line(line))
        .map(|line| planning_dates(line));
    if template_planning.is_some() {
        lines.remove(1);
    }
    let (mut scheduled, mut deadline) = template_planning.unwrap_or_default();
    if let Some(timestamp) = &draft.scheduled {
        scheduled = Some(format_active_timestamp(timestamp)?);
    }
    if let Some(timestamp) = &draft.deadline {
        deadline = Some(format_active_timestamp(timestamp)?);
    }
    let mut planning = Vec::new();
    if let Some(timestamp) = scheduled {
        planning.push(format!("SCHEDULED: {timestamp}"));
    }
    if let Some(timestamp) = deadline {
        planning.push(format!("DEADLINE: {timestamp}"));
    }
    if !planning.is_empty() {
        lines.insert(1.min(lines.len()), planning.join(" "));
    }
    add_properties(
        &mut lines,
        &draft.properties,
        usize::from(!planning.is_empty()) + 1,
    )?;
    Ok(lines.join(newline))
}

// Expands every escape in one pass over the template so that user-supplied
// title, body and prompt text is inserted literally and never re-expanded.
fn expand_capture_escapes(
    template: &str,
    draft: &CaptureDraft,
    body: &str,
    prompts: &BTreeMap<String, String>,
    now: NaiveDateTime,
) -> Result<String, AgdError> {
    let mut rendered = String::with_capacity(template.len() + draft.title.len() + body.len());
    let mut copied_to = 0;
    for captures in ESCAPE_RE.captures_iter(template) {
        let escape = captures.get(0).expect("escape match");
        rendered.push_str(&template[copied_to..escape.start()]);
        copied_to = escape.end();
        match &captures[1] {
            "?" => rendered.push_str(&draft.title),
            "i" => rendered.push_str(body),
            "U" => rendered.push_str(&format!("[{}]", now.format("%Y-%m-%d %a %H:%M"))),
            "u" => rendered.push_str(&format!("[{}]", now.format("%Y-%m-%d %a"))),
            "T" => rendered.push_str(&format!("<{}>", now.format("%Y-%m-%d %a %H:%M"))),
            "t" => rendered.push_str(&format!("<{}>", now.format("%Y-%m-%d %a"))),
            "%" => rendered.push('%'),
            "^g" => rendered.push_str(prompts.get("tags").ok_or_else(|| {
                AgdError::new(
                    ErrorCode::InvalidPrompt,
                    "capture tag prompt has no 'tags' value",
                )
            })?),
            _ => {
                let date_kind = captures
                    .get(3)
                    .or(captures.get(4))
                    .map(|kind| kind.as_str());
                let prompt = captures.get(2).map_or("date", |name| name.as_str());
                match date_kind {
                    Some(kind) => {
                        let value = prompts.get(prompt).map_or("", String::as_str);
                        let date = prompted_date(value, kind, now.date()).map_err(|error| {
                            AgdError::new(ErrorCode::InvalidDate, format!("{prompt}: {error}"))
                        })?;
                        rendered.push_str(&date);
                    }
                    None => {
                        let value = prompts.get(prompt).ok_or_else(|| {
                            AgdError::new(
                                ErrorCode::InvalidPrompt,
                                format!("capture prompt '{prompt}' has no value"),
                            )
                        })?;
                        rendered.push_str(value);
                    }
                }
            }
        }
    }
    rendered.push_str(&template[copied_to..]);
    Ok(rendered)
}

/// A date prompt's answer as an Org timestamp: `t` active date, `T` active
/// with time, `u`/`U` the inactive forms. Empty stays empty.
fn prompted_date(value: &str, kind: &str, today: NaiveDate) -> Result<String, AgdError> {
    if value.trim().is_empty() {
        return Ok(String::new());
    }
    let mut timestamp = parse_date_input(value, today)?;
    if kind == "t" || kind == "u" {
        timestamp.time = None;
    }
    if kind == "u" || kind == "U" {
        format_inactive_timestamp(&timestamp)
    } else {
        format_active_timestamp(&timestamp)
    }
}

fn insert_before_title(line: &mut String, value: &str) {
    let stars_end = line
        .find(|character: char| !matches!(character, '*' | ' ' | '\t'))
        .unwrap_or(line.len());
    if stars_end >= line.len() {
        line.push_str(value);
        return;
    }
    let rest = &line[stars_end..];
    let first_end = rest.find(char::is_whitespace).unwrap_or(rest.len());
    let insertion = if rest[..first_end]
        .bytes()
        .all(|byte| byte.is_ascii_uppercase() || matches!(byte, b'_' | b'-'))
    {
        stars_end + first_end
    } else {
        stars_end
    };
    line.insert_str(insertion, &format!(" {value}"));
}

/// A template's own planning line, the one a capture's dates merge into:
/// it starts with SCHEDULED: or DEADLINE:, the dates a capture sets.
fn is_schedule_or_deadline_line(line: &str) -> bool {
    let trimmed = line.trim_start();
    trimmed.starts_with("SCHEDULED:") || trimmed.starts_with("DEADLINE:")
}

/// The SCHEDULED and DEADLINE timestamps on a template's planning line;
/// a keyword with no timestamp after it yields None.
fn planning_dates(line: &str) -> (Option<String>, Option<String>) {
    let date_after = |keyword: &str| {
        let rest = line.split_once(keyword)?.1.trim_start();
        let close = match rest.chars().next()? {
            '<' => '>',
            '[' => ']',
            _ => return None,
        };
        let end = rest.find(close)?;
        Some(rest[..=end].to_string())
    };
    (date_after("SCHEDULED:"), date_after("DEADLINE:"))
}

/// Adds properties to the template's own `:PROPERTIES:` drawer, or opens one
/// right after the headline and planning line (`after`), keeping keys the
/// template already sets.
fn add_properties(
    lines: &mut Vec<String>,
    properties: &BTreeMap<String, String>,
    after: usize,
) -> Result<(), AgdError> {
    if properties.is_empty() {
        return Ok(());
    }
    let mut entries = Vec::new();
    for (key, value) in properties {
        if key.is_empty() || key.contains([' ', '\t', ':']) || value.contains(['\n', '\r']) {
            return Err(AgdError::new(
                ErrorCode::InvalidProperty,
                format!("capture property '{key}' needs a plain key and a one-line value"),
            ));
        }
        let present = lines.iter().any(|line| {
            line.trim_start()
                .to_ascii_uppercase()
                .starts_with(&format!(":{}:", key.to_ascii_uppercase()))
        });
        if !present {
            entries.push(format!(":{key}: {value}"));
        }
    }
    let drawer = lines.iter().position(|line| line.trim() == ":PROPERTIES:");
    let end = drawer.and_then(|start| {
        lines[start..]
            .iter()
            .position(|line| line.trim() == ":END:")
            .map(|offset| start + offset)
    });
    match end {
        Some(end) => {
            lines.splice(end..end, entries);
        }
        None => {
            let at = after.min(lines.len());
            let mut drawer = vec![":PROPERTIES:".to_string()];
            drawer.extend(entries);
            drawer.push(":END:".to_string());
            lines.splice(at..at, drawer);
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::date::simple_date;

    #[test]
    fn user_text_is_inserted_literally() {
        let template = CaptureTemplate {
            template: "* TODO %?\n%i\n:CREATED: %U\n100%%".to_string(),
            ..CaptureTemplate::default()
        };
        let draft = CaptureDraft {
            title: "Literal %U and %i".to_string(),
            scheduled: None,
            deadline: None,
            tags: Vec::new(),
            priority: None,
            properties: BTreeMap::new(),
        };
        let now = NaiveDate::from_ymd_opt(2026, 10, 13)
            .unwrap()
            .and_hms_opt(14, 22, 0)
            .unwrap();
        let rendered =
            render_capture_template(&template, &draft, "body %? %t", &BTreeMap::new(), now)
                .unwrap();
        assert_eq!(
            rendered,
            "* TODO Literal %U and %i\nbody %? %t\n:CREATED: [2026-10-13 Tue 14:22]\n100%"
        );
    }

    #[test]
    fn capture_properties_join_the_template_drawer_or_open_one() {
        let now = NaiveDate::from_ymd_opt(2026, 10, 13)
            .unwrap()
            .and_hms_opt(14, 22, 0)
            .unwrap();
        let draft = CaptureDraft {
            title: "Fix flaky test".to_string(),
            properties: BTreeMap::from([
                ("REPO".to_string(), "agd".to_string()),
                (
                    "SOURCE".to_string(),
                    "[[file:~/src/agd/write.rs::42]]".to_string(),
                ),
            ]),
            ..CaptureDraft::default()
        };
        let with_drawer = render_capture_template(
            &CaptureTemplate::default(),
            &draft,
            "",
            &BTreeMap::new(),
            now,
        )
        .unwrap();
        assert_eq!(
            with_drawer,
            "* TODO Fix flaky test\n:PROPERTIES:\n:CREATED: [2026-10-13 Tue 14:22]\n:REPO: agd\n:SOURCE: [[file:~/src/agd/write.rs::42]]\n:END:"
        );
        let plain = CaptureTemplate {
            template: "* TODO %?\nNotes".to_string(),
            ..CaptureTemplate::default()
        };
        let opened = render_capture_template(&plain, &draft, "", &BTreeMap::new(), now).unwrap();
        assert!(opened.starts_with("* TODO Fix flaky test\n:PROPERTIES:\n:REPO: agd\n"));
        assert!(opened.ends_with(":END:\nNotes"));
    }

    #[test]
    fn template_preview_is_exact_org_text() {
        let template = CaptureTemplate::default();
        let draft = CaptureDraft {
            title: "Renew passport".to_string(),
            scheduled: None,
            deadline: Some(simple_date(NaiveDate::from_ymd_opt(2026, 10, 30).unwrap())),
            tags: vec!["admin".to_string()],
            priority: Some("A".to_string()),
            properties: BTreeMap::new(),
        };
        let rendered = render_capture_template(
            &template,
            &draft,
            "",
            &BTreeMap::new(),
            NaiveDate::from_ymd_opt(2026, 10, 13)
                .unwrap()
                .and_hms_opt(9, 30, 0)
                .unwrap(),
        )
        .unwrap();
        assert_eq!(
            rendered,
            "* TODO [#A] Renew passport :admin:\nDEADLINE: <2026-10-30 Fri>\n:PROPERTIES:\n:CREATED: [2026-10-13 Tue 09:30]\n:END:"
        );
    }

    fn deadline_template() -> CaptureTemplate {
        CaptureTemplate {
            template: "* TODO %?\nDEADLINE: %^{deadline}t\nCREATED: %U".to_string(),
            ..CaptureTemplate::default()
        }
    }

    fn render(
        template: &CaptureTemplate,
        draft: &CaptureDraft,
        deadline: &str,
    ) -> Result<String, AgdError> {
        let now = NaiveDate::from_ymd_opt(2026, 10, 13)
            .unwrap()
            .and_hms_opt(9, 30, 0)
            .unwrap();
        let prompts = BTreeMap::from([("deadline".to_string(), deadline.to_string())]);
        render_capture_template(template, draft, "", &prompts, now)
    }

    #[test]
    fn a_date_prompt_is_a_date_and_lists_as_one() {
        assert_eq!(
            capture_prompts(&deadline_template()),
            vec![CapturePrompt {
                name: "deadline".to_string(),
                date: true,
                planning: Some("DEADLINE".to_string()),
            }]
        );
        let draft = CaptureDraft {
            title: "Renew passport".to_string(),
            ..CaptureDraft::default()
        };
        assert_eq!(
            render(&deadline_template(), &draft, "fri").unwrap(),
            "* TODO Renew passport\nDEADLINE: <2026-10-16 Fri>\nCREATED: [2026-10-13 Tue 09:30]"
        );
        let error = render(&deadline_template(), &draft, "someday").unwrap_err();
        assert!(error.to_string().contains("deadline"), "{error}");
    }

    #[test]
    fn token_dates_merge_into_the_template_planning_line() {
        let draft = CaptureDraft {
            title: "Renew passport".to_string(),
            scheduled: Some(simple_date(NaiveDate::from_ymd_opt(2026, 10, 19).unwrap())),
            deadline: Some(simple_date(NaiveDate::from_ymd_opt(2026, 10, 28).unwrap())),
            ..CaptureDraft::default()
        };
        let expected = "* TODO Renew passport\nSCHEDULED: <2026-10-19 Mon> DEADLINE: <2026-10-28 Wed>\nCREATED: [2026-10-13 Tue 09:30]";
        // The prompt left empty: the due: token fills the deadline.
        assert_eq!(render(&deadline_template(), &draft, "").unwrap(), expected);
        // Both given: the token wins, and there is still one planning line.
        assert_eq!(
            render(&deadline_template(), &draft, "fri").unwrap(),
            expected
        );
        // Neither: the empty DEADLINE keyword is dropped.
        let plain = CaptureDraft {
            title: "Renew passport".to_string(),
            ..CaptureDraft::default()
        };
        assert_eq!(
            render(&deadline_template(), &plain, "").unwrap(),
            "* TODO Renew passport\nCREATED: [2026-10-13 Tue 09:30]"
        );
    }

    #[test]
    fn timed_and_inactive_date_prompts_render_as_org_does() {
        let template = CaptureTemplate {
            template: "* Meeting %?\n%^{when}T\nLogged %^{deadline}u".to_string(),
            ..CaptureTemplate::default()
        };
        let draft = CaptureDraft {
            title: "Sync".to_string(),
            ..CaptureDraft::default()
        };
        let now = NaiveDate::from_ymd_opt(2026, 10, 13)
            .unwrap()
            .and_hms_opt(9, 30, 0)
            .unwrap();
        let prompts = BTreeMap::from([
            ("when".to_string(), "2026-10-30 14:00".to_string()),
            ("deadline".to_string(), "2026-10-30 14:00".to_string()),
        ]);
        assert_eq!(
            render_capture_template(&template, &draft, "", &prompts, now).unwrap(),
            "* Meeting Sync\n<2026-10-30 Fri 14:00>\nLogged [2026-10-30 Fri]"
        );
    }
}