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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
//! The configuration's types: `Settings`, the view, capture and agent profile
//! records, the value enums (`IdPolicy`, `WeekStart`, `LogMode`, `Notation`), the lists of
//! accepted values (`SORTING_KEYS`, `VIEW_BLOCK_KINDS`, `VIEW_GROUP_BY`,
//! `COLOR_NAMES`), the defaults, the built-in `n` view, and lookups such as
//! `Settings::view`.
//!
//! The serde shapes are the config file format: field names are the keys.
//! Checks the types cannot express live in `validation`.

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

use chrono::NaiveDate;
use serde::{Deserialize, Serialize};

use crate::{AgdError, AgendaSpan, ErrorCode};

/// Everything agd reads from `config.toml`, one flat table: every setting is
/// a top-level key, and only capture templates, views and agent profiles are
/// lists of records (`[[capture]]`, `[[view]]`, `[[agent_profile]]`).
/// Missing keys take the defaults below; unknown keys are errors.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(default, deny_unknown_fields)]
pub struct Settings {
    /// The only directories agd reads or writes.
    pub roots: Vec<String>,
    /// org-agenda-files: files, directories (their `*.org` files) or globs.
    /// Empty means every `.org` file under the roots.
    pub agenda_files: Vec<String>,

    /// org-todo-keywords, one `#+TODO:` line per sequence, such as
    /// "TODO NEXT WAIT(w@/!) | DONE CANCELLED": read by the same code as a
    /// file's own `#+TODO:` lines, which replace these. A line without `|`
    /// makes its last keyword the done state, as in Org.
    #[serde(deserialize_with = "deserialize_todo_lines")]
    pub todo_keywords: Vec<String>,
    /// org-todo-keyword-faces for the TUI: a color name (COLOR_NAMES) per
    /// keyword. A view section takes the color its rows' keywords share.
    /// The default colors the default workflow's WAIT yellow and CANCELLED
    /// dim-red; a map of your own replaces it.
    pub todo_keyword_colors: BTreeMap<String, String>,
    /// "preserve" leaves headings alone; "add-on-write" adds an org-id :ID:
    /// on a heading's first edit.
    pub ids: IdPolicy,
    /// org-tags-column: negative right-aligns tags to end at that column,
    /// positive starts them there, as Emacs does when it rewrites a headline.
    pub tags_column: i64,
    pub tag_inheritance_exclusions: Vec<String>,
    pub highest_priority: String,
    pub lowest_priority: String,
    pub default_priority: String,

    /// org-agenda-span: "day", "week", "month" or a number of days
    /// (`agenda_span = 10`).
    pub agenda_span: AgendaSpan,
    pub agenda_start_day: Option<String>,
    /// org-agenda-start-on-weekday: "monday" or "sunday".
    pub week_start: WeekStart,
    pub deadline_warning_days: i64,
    pub skip_scheduled_if_done: bool,
    pub skip_deadline_if_done: bool,
    pub skip_timestamp_if_done: bool,
    /// org-agenda-sorting-strategy for agenda days, from SORTING_KEYS.
    pub sorting_strategy: Vec<String>,

    /// org-log-done, org-log-repeat, org-log-reschedule, org-log-redeadline:
    /// "off" (Emacs's nil), "time" or "note". `#+STARTUP` options win per
    /// file.
    pub log_done: LogMode,
    pub log_repeat: LogMode,
    pub log_reschedule: LogMode,
    pub log_redeadline: LogMode,
    pub log_into_drawer: bool,

    /// Program and arguments to open a file; `{file}`, `{line}` and `{col}`
    /// are substituted. Run directly, never through a shell.
    pub editor: Vec<String>,
    /// auto (follow the terminal background), dark or light.
    pub theme: String,
    /// Round pill and chip caps with Nerd Font glyphs; turn on when your
    /// terminal font has them (Ghostty, WezTerm and kitty bundle them).
    pub nerd_font: bool,
    /// How a capture line takes tags and a priority, and how agd shows
    /// tags: "agd" (`#work`, `!A`) or "org" (`:work:`, `#A`).
    pub notation: Notation,
    /// The view key `agd` opens on, such as your dashboard or the built-in
    /// `n`; empty opens Today.
    pub start_view: String,
    /// A `[[capture]]` key: every agent capture goes through that template
    /// without dates, so agent work reaches your day only through triage.
    /// Absent, agents choose a template.
    pub agent_inbox: Option<String>,
    pub someday_tag: String,
    /// Someday also removes the dates that put the task on the agenda
    /// (SCHEDULED, and an overdue DEADLINE), so it leaves the agenda.
    pub someday_clears_dates: bool,
    /// Triage suggests Someday after this many reschedules.
    pub reschedule_nudge: usize,

    #[serde(rename = "capture")]
    pub captures: Vec<CaptureTemplate>,
    /// The user's own views only; `all_views` and `view` add the built-in
    /// ones.
    #[serde(rename = "view")]
    pub views: Vec<ViewSettings>,
    #[serde(rename = "agent_profile")]
    pub agent_profiles: Vec<AgentProfile>,
}

/// A saved agenda view, the declarative part of an org-agenda custom
/// command: one block, or several shown one after another like a block
/// agenda.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ViewSettings {
    pub key: String,
    pub desc: String,
    pub blocks: Vec<ViewBlock>,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ViewBlock {
    /// agenda, todo, alltodo, tags or tags-todo, as in org-agenda.
    pub kind: String,
    /// Tags/TODO match; for `todo` blocks, TODO keywords such as `NEXT|WAIT`.
    #[serde(default, rename = "match", skip_serializing_if = "Option::is_none")]
    pub match_expression: Option<String>,
    /// For agenda blocks: "day", "week", "month" or a number of days;
    /// without one, `agenda_span`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub span: Option<AgendaSpan>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub header: Option<String>,
    /// The header's color in the TUI, one of COLOR_NAMES; by default it
    /// follows the rows' TODO keywords.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub color: Option<String>,
    /// org-super-agenda style groups over the block's rows.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub groups: Vec<ViewGroup>,
}

/// One group of a view block, as an org-super-agenda group: each row goes
/// to the first group, in list order, whose match it passes; `discard`
/// drops those rows; `by` makes one group per category, parent heading,
/// TODO keyword or priority cookie. Groups are shown by `order`, then list
/// order, and rows no group takes come last as "Other items".
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ViewGroup {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Tags/TODO match, as for a tags block; without one the group takes
    /// every remaining row.
    #[serde(default, rename = "match", skip_serializing_if = "Option::is_none")]
    pub match_expression: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub by: Option<String>,
    /// org-super-agenda's `:effort<` and `:effort>`: the heading's own
    /// `:Effort:` is at most / at least this duration (`0:15`, `30`, `1h`).
    /// A heading without an effort matches neither.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub effort_max: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub effort_min: Option<String>,
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub discard: bool,
    #[serde(default, skip_serializing_if = "is_zero")]
    pub order: i64,
    /// As for a block; a `by` group's color applies to each group it makes.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub color: Option<String>,
}

fn is_zero(value: &i64) -> bool {
    *value == 0
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct CaptureTemplate {
    pub key: String,
    pub desc: String,
    /// The target, as org-capture's: `id` alone, or `file` alone
    /// (file), with `heading` (file+headline) or with `olpath`
    /// (file+olp). `datetree` goes with `file` alone (file+datetree) or with
    /// `olpath` (file+olp+datetree).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub file: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub heading: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub olpath: Option<Vec<String>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub datetree: bool,
    /// The entry's text with org-capture escapes (`%?`, `%U`, `%^{prompt}`).
    /// A template has exactly one of `template` and `template_file`; empty
    /// is none. `load_settings` reads a `template_file` into `template`.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub template: String,
    /// A file holding the template text; a relative path is under the first
    /// root, as Org's are under org-directory.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub template_file: Option<String>,
}

/// A named, filtered agenda an agent may read at session start.
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct AgentProfile {
    pub name: String,
    #[serde(default)]
    pub files: Vec<String>,
    #[serde(default, rename = "match", skip_serializing_if = "Option::is_none")]
    pub match_expression: Option<String>,
}

#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum IdPolicy {
    #[default]
    Preserve,
    AddOnWrite,
}

/// The day a week agenda starts on.
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum WeekStart {
    #[default]
    Monday,
    Sunday,
}

/// How tags and a priority are written in a capture line, and how agd
/// draws tags: `agd` is `#work` and `!A`; `org` is Org's own `:work:` (or
/// `:a:b:`) and `#A`. Org's files, `--csv` and the agenda text are Org's
/// either way.
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Notation {
    #[default]
    Agd,
    Org,
}

/// What a state or planning change records, as an org-log-* variable: no
/// log, a timestamp, or a timestamp and a note.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum LogMode {
    Off,
    Time,
    Note,
}

/// Agenda sorting keys agd implements; habit-* and category-keep keep
/// collection order.
pub const SORTING_KEYS: [&str; 15] = [
    "time-up",
    "time-down",
    "urgency-up",
    "urgency-down",
    "priority-up",
    "priority-down",
    "category-keep",
    "category-up",
    "category-down",
    "alpha-up",
    "alpha-down",
    "todo-state-up",
    "todo-state-down",
    "habit-up",
    "habit-down",
];

pub const VIEW_BLOCK_KINDS: [&str; 5] = ["agenda", "todo", "alltodo", "tags", "tags-todo"];
pub const VIEW_GROUP_BY: [&str; 4] = ["category", "parent", "todo", "priority"];
/// Colors a keyword or view section may name; the TUI maps them onto its
/// light or dark palette.
pub const COLOR_NAMES: [&str; 10] = [
    "blue", "green", "yellow", "orange", "red", "dim-red", "purple", "pink", "teal", "grey",
];

impl Default for Settings {
    fn default() -> Self {
        Self {
            roots: vec!["~/org".to_string()],
            agenda_files: Vec::new(),
            todo_keywords: strings(&["TODO NEXT WAIT | DONE CANCELLED"]),
            todo_keyword_colors: BTreeMap::from([
                ("WAIT".to_string(), "yellow".to_string()),
                ("CANCELLED".to_string(), "dim-red".to_string()),
            ]),
            ids: IdPolicy::Preserve,
            tags_column: -77,
            tag_inheritance_exclusions: Vec::new(),
            highest_priority: "A".to_string(),
            lowest_priority: "C".to_string(),
            default_priority: "B".to_string(),
            agenda_span: AgendaSpan::Week,
            agenda_start_day: None,
            week_start: WeekStart::Monday,
            deadline_warning_days: 14,
            skip_scheduled_if_done: false,
            skip_deadline_if_done: false,
            skip_timestamp_if_done: false,
            // Emacs's default for agenda days (Org 9.7+ names).
            sorting_strategy: strings(&["habit-down", "time-up", "urgency-down", "category-keep"]),
            log_done: LogMode::Time,
            log_repeat: LogMode::Time,
            log_reschedule: LogMode::Off,
            log_redeadline: LogMode::Off,
            log_into_drawer: false,
            editor: vec![
                std::env::var("EDITOR").unwrap_or_else(|_| "vi".to_string()),
                "+{line}".to_string(),
                "{file}".to_string(),
            ],
            theme: "auto".to_string(),
            nerd_font: false,
            notation: Notation::Agd,
            start_view: String::new(),
            agent_inbox: None,
            someday_tag: "someday".to_string(),
            someday_clears_dates: true,
            reschedule_nudge: 9,
            captures: vec![CaptureTemplate::default()],
            views: Vec::new(),
            agent_profiles: Vec::new(),
        }
    }
}

/// Org's built-in agenda commands that agd runs, beside the user's
/// `[[view]]`s: `n`, "Agenda and all TODOs". A `[[view]]` with the same key
/// replaces one.
static BUILTIN_VIEWS: LazyLock<Vec<ViewSettings>> = LazyLock::new(|| {
    let block = |kind: &str| ViewBlock {
        kind: kind.to_string(),
        match_expression: None,
        span: None,
        header: None,
        color: None,
        groups: Vec::new(),
    };
    vec![ViewSettings {
        key: "n".to_string(),
        desc: "Agenda and all TODOs".to_string(),
        blocks: vec![block("agenda"), block("alltodo")],
    }]
});

/// The default `t` template: a TODO in inbox.org under Inbox, stamped with
/// its creation time.
impl Default for CaptureTemplate {
    fn default() -> Self {
        Self {
            key: "t".to_string(),
            desc: "Todo".to_string(),
            file: Some("inbox.org".to_string()),
            heading: Some("Inbox".to_string()),
            olpath: None,
            id: None,
            datetree: false,
            template: "* TODO %?\n:PROPERTIES:\n:CREATED: %U\n:END:".to_string(),
            template_file: None,
        }
    }
}

pub(super) fn strings(values: &[&str]) -> Vec<String> {
    values.iter().map(|value| value.to_string()).collect()
}

impl Settings {
    /// The first agenda day: `agenda_start_day` from `today`, or today.
    pub fn agenda_start(&self, today: NaiveDate) -> Result<NaiveDate, AgdError> {
        match self.agenda_start_day.as_deref() {
            Some(value) => parse_agenda_day(value, today),
            None => Ok(today),
        }
    }

    pub fn capture_template(&self, key: &str) -> Option<&CaptureTemplate> {
        self.captures.iter().find(|template| template.key == key)
    }

    /// The template a capture goes through when none is named: the first
    /// `[[capture]]`. (An agent's capture then goes to `agent_inbox` when
    /// it is set, as every agent capture does.)
    pub fn default_capture_template(&self) -> Result<&CaptureTemplate, AgdError> {
        self.captures.first().ok_or_else(|| {
            AgdError::new(
                ErrorCode::UnknownTemplate,
                "no [[capture]] template is configured",
            )
        })
    }

    /// How a capture line names its template, the one rule for every
    /// surface: the first word is the template key when it names a
    /// `[[capture]]` and more text follows; otherwise the whole line is the
    /// text for the default template. Returns the template and the text.
    pub fn capture_line_template<'a>(
        &self,
        line: &'a str,
    ) -> Result<(&CaptureTemplate, &'a str), AgdError> {
        let line = line.trim();
        if let Some((key, text)) = line.split_once(char::is_whitespace)
            && let Some(template) = self.capture_template(key)
        {
            return Ok((template, text.trim_start()));
        }
        Ok((self.default_capture_template()?, line))
    }

    /// The template `key` names, or an `unknown_template` error that lists
    /// the templates there are.
    pub fn find_capture_template(&self, key: &str) -> Result<&CaptureTemplate, AgdError> {
        self.capture_template(key).ok_or_else(|| {
            let keys = self.captures.iter().map(|template| template.key.as_str());
            let known = if self.captures.is_empty() {
                "no [[capture]] template is configured".to_string()
            } else {
                format!("the templates are {}", quoted_list(keys))
            };
            AgdError::new(
                ErrorCode::UnknownTemplate,
                format!("unknown capture template '{key}'; {known}"),
            )
        })
    }

    /// The view `key` runs, or an `unknown_view` error that lists every
    /// view, the built-in ones too.
    pub fn find_view(&self, key: &str) -> Result<&ViewSettings, AgdError> {
        self.view(key).ok_or_else(|| {
            AgdError::new(
                ErrorCode::UnknownView,
                format!("unknown view '{key}'; the views are {}", self.view_keys()),
            )
        })
    }

    /// The view `agd agenda --view` without a key runs: start_view.
    pub fn find_start_view(&self) -> Result<&ViewSettings, AgdError> {
        if self.start_view.is_empty() {
            return Err(AgdError::new(
                ErrorCode::UnknownView,
                format!(
                    "--view without a key runs start_view, which is not set; name one of {}",
                    self.view_keys()
                ),
            ));
        }
        self.find_view(&self.start_view)
    }

    /// Every view's key, quoted, for messages: `'w' and 'n'`.
    pub(super) fn view_keys(&self) -> String {
        quoted_list(self.all_views().iter().map(|view| view.key.as_str()))
    }

    /// The view `key` runs: the user's `[[view]]`, else a built-in one.
    pub fn view(&self, key: &str) -> Option<&ViewSettings> {
        self.all_views().into_iter().find(|view| view.key == key)
    }

    /// Every view a key can run: the user's `[[view]]`s in config order,
    /// then the built-in views they do not replace.
    pub fn all_views(&self) -> Vec<&ViewSettings> {
        let builtins = BUILTIN_VIEWS
            .iter()
            .filter(|builtin| self.views.iter().all(|view| view.key != builtin.key));
        self.views.iter().chain(builtins).collect()
    }

    /// Whether `key` runs a built-in view rather than one of the user's.
    pub fn is_builtin_view(&self, key: &str) -> bool {
        self.views.iter().all(|view| view.key != key)
            && BUILTIN_VIEWS.iter().any(|builtin| builtin.key == key)
    }

    /// Narrows these settings to `[[agent_profile]]` `name`, as `agd
    /// agenda --profile` and MCP `agenda` do: its files replace
    /// agenda_files, and its match is the match unless `match_expression`
    /// gives one, which this returns.
    pub fn apply_agent_profile(
        &mut self,
        name: &str,
        match_expression: Option<String>,
    ) -> Result<Option<String>, AgdError> {
        let profile = self.find_agent_profile(name)?.clone();
        if !profile.files.is_empty() {
            self.agenda_files = profile.files;
        }
        Ok(match_expression.or(profile.match_expression))
    }

    /// The `[[agent_profile]]` `name` names, or an `unknown_profile` error
    /// that lists the profiles there are.
    pub fn find_agent_profile(&self, name: &str) -> Result<&AgentProfile, AgdError> {
        let profile = self
            .agent_profiles
            .iter()
            .find(|profile| profile.name == name);
        profile.ok_or_else(|| {
            let names = self
                .agent_profiles
                .iter()
                .map(|profile| profile.name.as_str());
            let known = if self.agent_profiles.is_empty() {
                "no [[agent_profile]] is configured".to_string()
            } else {
                format!("the profiles are {}", quoted_list(names))
            };
            AgdError::new(
                ErrorCode::UnknownProfile,
                format!("unknown agent profile '{name}'; {known}"),
            )
        })
    }

    /// The editor program and its arguments with `{file}`, `{line}` and
    /// `{col}` (both 1-based) substituted inside each argument. The result
    /// goes straight to the process API, never to a shell, so file names
    /// with spaces or quotes stay one argument.
    pub fn editor_command(&self, file: &str, line: usize, col: usize) -> (String, Vec<String>) {
        let mut parts = self.editor.iter().map(|argument| {
            argument
                .replace("{file}", file)
                .replace("{line}", &line.to_string())
                .replace("{col}", &col.to_string())
        });
        let program = parts.next().unwrap_or_else(|| "vi".to_string());
        (program, parts.collect())
    }
}

/// Names for a message, each in single quotes: `'a'`, `'a' and 'b'`,
/// `'a', 'b' and 'c'`.
pub(crate) fn quoted_list<'a>(names: impl IntoIterator<Item = &'a str>) -> String {
    let quoted: Vec<String> = names.into_iter().map(|name| format!("'{name}'")).collect();
    match quoted.split_last() {
        None => String::new(),
        Some((last, [])) => last.clone(),
        Some((last, rest)) => format!("{} and {last}", rest.join(", ")),
    }
}

/// `todo_keywords` as strings. A list of words instead of a line gets an
/// error that spells out the line to write.
fn deserialize_todo_lines<'de, D>(deserializer: D) -> Result<Vec<String>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    #[derive(Deserialize)]
    #[serde(untagged)]
    enum TodoLine {
        Line(String),
        Words(Vec<String>),
    }
    Vec::<TodoLine>::deserialize(deserializer)?
        .into_iter()
        .map(|line| match line {
            TodoLine::Line(line) => Ok(line),
            TodoLine::Words(words) => Err(serde::de::Error::custom(format!(
                "todo_keywords takes one #+TODO: line per sequence; write \"{}\"",
                words.join(" ")
            ))),
        })
        .collect()
}

/// A day as org-agenda-start-day and `agd agenda --from` take it: a date,
/// an offset from `today` such as -1d or +2w, or what `parse_date_input`
/// reads, such as "fri".
pub fn parse_agenda_day(value: &str, today: NaiveDate) -> Result<NaiveDate, AgdError> {
    crate::date::parse_org_date(&crate::parse_date_input(value, today)?.date)
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    use super::*;
    use crate::config::test_support::{load_body, load_error};
    use crate::render_config;

    #[test]
    fn editor_command_substitutes_every_placeholder_per_argument() {
        let settings = Settings {
            editor: strings(&["emacsclient", "-n", "+{line}:{col}", "{file}"]),
            ..Settings::default()
        };
        assert_eq!(
            settings.editor_command("/org/my tasks.org", 42, 1),
            (
                "emacsclient".to_string(),
                strings(&["-n", "+42:1", "/org/my tasks.org"])
            )
        );
    }

    #[test]
    fn a_capture_line_names_its_template_only_with_text_after_the_key() {
        let settings = Settings {
            captures: vec![
                CaptureTemplate::default(),
                CaptureTemplate {
                    key: "n".to_string(),
                    ..CaptureTemplate::default()
                },
            ],
            ..Settings::default()
        };
        let read = |line: &str| {
            let (template, text) = settings.capture_line_template(line).unwrap();
            (template.key.clone(), text.to_string())
        };
        assert_eq!(
            read("n Call back"),
            ("n".to_string(), "Call back".to_string())
        );
        assert_eq!(
            read("Renew passport"),
            ("t".to_string(), "Renew passport".to_string())
        );
        assert_eq!(read("n"), ("t".to_string(), "n".to_string()));
        assert_eq!(
            read("x -1 point"),
            ("t".to_string(), "x -1 point".to_string())
        );
    }

    #[test]
    fn unknown_names_list_what_is_configured_or_say_nothing_is() {
        let settings = Settings {
            agent_profiles: vec![AgentProfile {
                name: "coding".to_string(),
                ..AgentProfile::default()
            }],
            ..Settings::default()
        };
        let message = |error: AgdError| (error.code(), error.to_string());
        assert_eq!(
            message(settings.find_view("x").unwrap_err()),
            (
                ErrorCode::UnknownView,
                "unknown view 'x'; the views are 'n'".to_string()
            )
        );
        assert_eq!(
            message(settings.find_start_view().unwrap_err()).1,
            "--view without a key runs start_view, which is not set; name one of 'n'"
        );
        assert_eq!(
            message(settings.find_agent_profile("x").unwrap_err()).1,
            "unknown agent profile 'x'; the profiles are 'coding'"
        );
        assert_eq!(
            message(Settings::default().find_agent_profile("x").unwrap_err()).1,
            "unknown agent profile 'x'; no [[agent_profile]] is configured"
        );
        let none = Settings {
            captures: Vec::new(),
            ..Settings::default()
        };
        assert_eq!(
            message(none.find_capture_template("t").unwrap_err()),
            (
                ErrorCode::UnknownTemplate,
                "unknown capture template 't'; no [[capture]] template is configured".to_string()
            )
        );
        assert_eq!(quoted_list(["a", "b", "c"]), "'a', 'b' and 'c'");
    }

    #[test]
    fn builtin_n_stays_beside_user_views_until_replaced() {
        let defaults = Settings::default();
        assert!(defaults.views.is_empty());
        assert_eq!(defaults.view("n").unwrap().desc, "Agenda and all TODOs");
        assert!(defaults.is_builtin_view("n"));

        let settings = load_body(concat!(
            "start_view = \"n\"\n",
            "[[view]]\nkey = \"w\"\ndesc = \"Work\"\n",
            "blocks = [{ kind = \"tags-todo\", match = \"work\" }]\n",
        ))
        .unwrap();
        let keys: Vec<&str> = settings
            .all_views()
            .iter()
            .map(|view| view.key.as_str())
            .collect();
        assert_eq!(keys, ["w", "n"]);
        assert!(!settings.is_builtin_view("w"));
        let rendered = render_config(&settings, "views").unwrap();
        assert!(rendered.contains("key = \"w\""), "{rendered}");
        assert!(!rendered.contains("key = \"n\""), "{rendered}");

        let replaced = load_body(concat!(
            "[[view]]\nkey = \"n\"\ndesc = \"Next\"\n",
            "blocks = [{ kind = \"todo\", match = \"NEXT\" }]\n",
        ))
        .unwrap();
        assert_eq!(replaced.all_views().len(), 1);
        assert_eq!(replaced.view("n").unwrap().desc, "Next");
        assert!(!replaced.is_builtin_view("n"));
    }

    #[test]
    fn week_start_is_monday_or_sunday() {
        let settings = load_body("week_start = \"sunday\"\n").unwrap();
        assert_eq!(settings.week_start, WeekStart::Sunday);
        assert_eq!(
            load_error("week_start = \"friday\"\n"),
            concat!(
                "cannot parse CONFIG: TOML parse error at line 2, column 14\n",
                "  |\n",
                "2 | week_start = \"friday\"\n",
                "  |              ^^^^^^^^\n",
                "unknown variant `friday`, expected `monday` or `sunday`\n",
            )
        );
    }

    #[test]
    fn notation_is_agd_or_org() {
        assert_eq!(load_body("").unwrap().notation, Notation::Agd);
        assert_eq!(
            load_body("notation = \"org\"\n").unwrap().notation,
            Notation::Org
        );
        assert_eq!(
            load_error("notation = \"emacs\"\n"),
            concat!(
                "cannot parse CONFIG: TOML parse error at line 2, column 12\n",
                "  |\n",
                "2 | notation = \"emacs\"\n",
                "  |            ^^^^^^^\n",
                "unknown variant `emacs`, expected `agd` or `org`\n",
            )
        );
    }

    #[test]
    fn log_settings_are_off_time_or_note() {
        let settings = load_body("log_done = \"note\"\nlog_repeat = \"off\"\n").unwrap();
        assert_eq!(
            (settings.log_done, settings.log_repeat),
            (LogMode::Note, LogMode::Off)
        );
        assert_eq!(
            load_error("log_reschedule = \"yes\"\n"),
            concat!(
                "cannot parse CONFIG: TOML parse error at line 2, column 18\n",
                "  |\n",
                "2 | log_reschedule = \"yes\"\n",
                "  |                  ^^^^^\n",
                "unknown variant `yes`, expected one of `off`, `time`, `note`\n",
            )
        );
        assert!(
            load_error("log_done = \"nil\"\n")
                .ends_with("unknown variant `nil`, expected one of `off`, `time`, `note`\n")
        );
    }

    #[test]
    fn todo_keyword_lines_read_like_a_files_todo_lines() {
        let settings = Settings {
            todo_keywords: strings(&["TODO(t) WAIT(w@/!) | DONE(d!)", "IDEA DROPPED"]),
            ..Settings::default()
        };
        let document = crate::IndexedDocument::parse(
            PathBuf::from("tasks.org"),
            b"* WAIT Reply\n".to_vec(),
            &settings,
        )
        .unwrap();
        let keywords: Vec<(&str, bool, usize)> = document
            .todo_keywords()
            .iter()
            .map(|keyword| (keyword.name.as_str(), keyword.done, keyword.sequence))
            .collect();
        assert_eq!(
            keywords,
            [
                ("TODO", false, 0),
                ("WAIT", false, 0),
                ("DONE", true, 0),
                ("IDEA", false, 1),
                ("DROPPED", true, 1)
            ]
        );
        let wait = &document.todo_keywords()[1];
        assert_eq!(
            (wait.log_enter, wait.log_leave),
            (Some(crate::LogKind::Note), Some(crate::LogKind::Time))
        );
    }
}