todo_lib 10.1.0

Collection of utilities for todo.txt format
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
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
use std::cmp::Ordering;
use std::collections::HashMap;
use std::ffi::OsStr;
use std::fs;
use std::fs::File;
use std::fs::OpenOptions;
use std::io::BufRead;
use std::io::BufReader;
use std::io::Write;
use std::path::Path;

use crate::date_expr;
use crate::terr;
use crate::timer;
use crate::todotxt;
use crate::todotxt::CompletionConfig;
use crate::tsort;

/// The ID value returned instead of new todo ID if adding a new todo fails
pub const INVALID_ID: usize = 1_999_999_999;
pub const TIMER_TAG: &str = "tmr";
pub const SPENT_TAG: &str = "spent";
pub const TIMER_OFF: &str = "off";

pub type TaskVec = Vec<todotxt::Task>;
pub type TaskSlice = [todotxt::Task];
pub type IDVec = Vec<usize>;
pub type IDSlice = [usize];
pub type ChangedVec = Vec<bool>;
pub type ChangedSlice = [bool];

/// The new value for a date-like tag.
/// Date - a fixed date, one for all tasks
/// Expr - an expression that is calculated for each of selected tasks. E.g, `due+1w`
#[derive(Debug, Clone)]
pub enum NewDateValue {
    Date(chrono::NaiveDate),
    Expr(String),
    None,
}

/// Type of operation applied to todo properties. Every field supports
/// its own set of operations (except `None` that can be used for all of them):
/// * priority: `Set`, `Delete`, `Increase`, `Decrease`;
/// * due date: `Set`, `Delete`;
/// * recurrence: `Set`, `Delete`;
/// * projects: `Set`, `Delete`, `Replace`;
/// * contexts: `Set`, `Delete`, `Replace`;
/// * tags: `Set`, `Delete`;
#[derive(Debug, Clone, Copy)]
pub enum Action {
    /// do not touch the property
    None,
    /// Priority, due date, and recurrence: set the new value;
    /// Projects and contexts: add a new value to the list;
    Set,
    /// Remove the value
    Delete,
    /// Replace old value with a new one. The format for the new value:
    /// projects: `old_value+new_value`
    /// contexts: `old_value@new_value`
    Replace,
    /// Only for priority: increases the priority by one level. If a todo
    /// has A priority the todo is not changed. If a todo does not have a
    /// priority it gets the lowest one `Z`
    Increase,
    /// Only for priority: decreases the priority by one level. If a todo
    /// has no priority the todo is not changed. If a todo has the lowest
    /// priority `Z` the priority is removed
    Decrease,
}

/// Describes how the date-like tag should be changed.
/// action == Action::None means no changes.
#[derive(Debug, Clone)]
pub struct DateTagChange {
    pub action: Action,
    pub value: NewDateValue,
}

impl Default for DateTagChange {
    fn default() -> DateTagChange {
        DateTagChange { action: Action::None, value: NewDateValue::None }
    }
}

/// Describes how the list-like tag should be changed.
#[derive(Debug, Clone)]
pub struct ListTagChange {
    pub action: Action,
    pub value: Vec<String>,
}

impl Default for ListTagChange {
    fn default() -> ListTagChange {
        ListTagChange { action: Action::None, value: Vec::new() }
    }
}

/// Describes how the priority tag should be changed.
#[derive(Clone, Copy, Debug)]
pub struct PriorityTagChange {
    pub action: Action,
    pub value: u8,
}

impl Default for PriorityTagChange {
    fn default() -> PriorityTagChange {
        PriorityTagChange { action: Action::None, value: todotxt::NO_PRIORITY }
    }
}

/// Describes how the recurrency tag should be changed.
#[derive(Clone, Debug)]
pub struct RecurrencyTagChange {
    pub action: Action,
    pub value: Option<todotxt::Recurrence>,
}

impl Default for RecurrencyTagChange {
    fn default() -> RecurrencyTagChange {
        RecurrencyTagChange { action: Action::None, value: None }
    }
}

/// Describes how tags should be changed.
#[derive(Clone, Debug)]
pub struct TagValuesChange {
    pub action: Action,
    pub value: Option<HashMap<String, String>>,
}

impl Default for TagValuesChange {
    fn default() -> TagValuesChange {
        TagValuesChange { action: Action::None, value: None }
    }
}

/// The list of changes to apply to all records in a list. All operations with
/// text are case insensitive, so if you, e.g., try to replace a project
/// `projectone` to `ProjectOne` no todo is updated
#[derive(Debug, Clone)]
pub struct Conf {
    done: bool,
    /// New subject replaces the old one. Trying to change the subject for more
    /// than one todo results in that only the first todo in list is changed,
    /// all the rest todos are skipped.
    ///
    /// NOTE: the new subject must contain all required attributes: projects,
    /// contexts, recurrence, due date etc because after assigning a new
    /// subject it is parsed and all todo properties are filled with parsed data
    pub subject: Option<String>,
    /// New priority (is not used if action is `Delete, `Increase` or `Decrease`)
    pub priority: PriorityTagChange,
    /// New due date
    pub due: DateTagChange,
    /// New threshold date
    pub thr: DateTagChange,
    /// New recurrence
    pub recurrence: RecurrencyTagChange,
    /// List of projects.
    /// For `Set` and `Delete` is a list of strings;
    /// For `Replace` it is a list of strings containing pairs in format:
    /// `old_project+new_project`
    pub projects: ListTagChange,
    /// List of contexts.
    /// For `Set` and `Delete` is a list of strings;
    /// For `Replace` it is a list of strings containing pairs in format:
    /// `old_context@new_context`
    pub contexts: ListTagChange,
    /// Automatically set creation date to today if it is not defined in subject
    /// when adding a new todo
    pub auto_create_date: bool,
    /// Update tags with new values. When editing, empty value means
    /// deleting the tag. When filtering, empty value means `any`.
    /// Hashmap: <TagName> - <TagValue>.
    /// Use it only to change tags that are not standard ones(due, recurrence,
    /// and threshold). These tags are ignored by the function 'edit'.
    pub tags: TagValuesChange,
    /// Update hashtags
    pub hashtags: ListTagChange,
    /// Rule to update priority when a task is done or undone
    pub completion_mode: todotxt::CompletionMode,
    /// Rule to set a completion date when a task is done
    pub completion_date_mode: todotxt::CompletionDateMode,
    /// The value of `soon` for calculating expression like `soon`.
    pub soon_days: u8,
}

impl Default for Conf {
    fn default() -> Conf {
        Conf {
            subject: None,
            done: true,
            priority: PriorityTagChange::default(),
            due: DateTagChange::default(),
            thr: DateTagChange::default(),
            recurrence: RecurrencyTagChange::default(),
            projects: ListTagChange::default(),
            contexts: ListTagChange::default(),
            auto_create_date: false,
            tags: TagValuesChange::default(),
            hashtags: ListTagChange::default(),
            completion_mode: todotxt::CompletionMode::JustMark,
            completion_date_mode: todotxt::CompletionDateMode::WhenCreationDateIsPresent,
            soon_days: 0,
        }
    }
}

pub(crate) fn make_id_vec(sz: usize) -> IDVec {
    let mut v: IDVec = Vec::new();
    for i in 0..sz {
        v.push(i);
    }
    v
}

/// Returns if the tag needs special processing. All 'special' tags cannot be
/// changed via 'update_tags' function because they usually requires running
/// some extra operations on the data after a change (e.g, update internal
/// fields in addition to updating the tag HashMap).
pub fn is_tag_special(tag: &str) -> bool {
    tag == todotxt::DUE_TAG
        || tag == todotxt::DUE_TAG_FULL
        || tag == todotxt::THR_TAG
        || tag == todotxt::THR_TAG_FULL
        || tag == todotxt::REC_TAG
        || tag == todotxt::REC_TAG_FULL
}

/// Load a list of todo from a file in todo.txt format. If the file does not
/// exist or cannot be opened the function returns empty list
pub fn load(filename: &Path) -> Result<TaskVec, terr::TodoError> {
    let mut tasks = Vec::new();
    if !filename.exists() {
        return Ok(tasks);
    }

    let file = File::open(filename).map_err(|_| terr::TodoError::LoadFailed)?;
    let now = chrono::Local::now().date_naive();

    let br = BufReader::new(&file);
    for l in br.lines().map_while(Result::ok) {
        let t = todotxt::Task::parse(&l, now);
        tasks.push(t);
    }

    Ok(tasks)
}

/// Saves the list of todos into a local file. Returns an error if saving
/// fails.
pub fn save(tasks: &TaskSlice, filename: &Path) -> Result<(), terr::TodoError> {
    let tmpname = filename.with_extension(OsStr::new("todo.tmp"));

    let mut output = File::create(&tmpname).map_err(|_| terr::TodoError::SaveFailed)?;
    for t in tasks {
        let line = format!("{t}\n");
        write!(output, "{line}").map_err(|_| terr::TodoError::FileWriteFailed)?;
    }

    fs::rename(tmpname, filename).map_err(|e| terr::TodoError::IOError(e.to_string()))?;
    Ok(())
}

/// Appends todos to a file. If file does not exist it is created.
///
/// * `tasks` - todo list to append to the file
/// * `filename` - the name of the file to save the data (usually it is `done.txt`)
///
/// Returns true if all todos are saved successfully
pub fn archive(tasks: &TaskSlice, filename: &Path) -> Result<(), terr::TodoError> {
    let mut output =
        OpenOptions::new().append(true).create(true).open(filename).map_err(|_| terr::TodoError::AppendFailed)?;

    for t in tasks {
        let line = format!("{t}\n");
        write!(output, "{line}").map_err(|_| terr::TodoError::FileWriteFailed)?;
    }

    Ok(())
}

/// Makes a clones of selected todos
///
/// * `tasks` - the full list of todos
/// * `ids` - the list of todo IDs that must be clones. Invalid IDs (e.g, ID
///   greater than the number of items in `tasks`) are skipped
///
/// Returns the list of cloned todos. Size of the list is equal to or less than
/// size of `ids` vector
pub fn clone_tasks(tasks: &TaskSlice, ids: &IDSlice) -> TaskVec {
    // TODO: clone all if ids is empty?
    let mut v: TaskVec = Vec::new();
    for id in ids.iter() {
        if *id < tasks.len() {
            let t = tasks[*id].clone();
            v.push(t);
        }
    }
    v
}

/// Appends a new todo to todo list
///
/// * `tasks` - a list of todos for adding a new item
/// * `c` - information about new todo. At this moment only `subject` field
///   is used - it should contains all info including due date, priority etc.
///   The `subject` field should be in todo.txt format
///
/// Returns:
/// * INVALID_ID if the subject is empty or cannot be parsed as todo.txt entry
/// * id of the new todo
pub fn add(tasks: &mut TaskVec, c: &Conf) -> usize {
    let s = match &c.subject {
        None => return INVALID_ID,
        Some(subj) => subj,
    };

    let now = chrono::Local::now().date_naive();
    let mut t = todotxt::Task::parse(s, now);
    if c.auto_create_date && t.create_date.is_none() {
        t.create_date = Some(chrono::Local::now().date_naive());
    }
    tasks.push(t);
    tasks.len() - 1
}

fn done_undone(tasks: &mut TaskVec, ids: Option<&IDVec>, c: &Conf) -> ChangedVec {
    if tasks.is_empty() {
        return Vec::new();
    }
    let longvec = make_id_vec(tasks.len());
    let id_iter = if let Some(v) = ids { v } else { &longvec };
    let mut bools = vec![false; id_iter.len()];
    let now = chrono::Local::now().date_naive();

    for (i, idx) in id_iter.iter().enumerate() {
        if *idx >= tasks.len() {
            continue;
        }

        if c.done {
            bools[i] = timer::stop_timer(&mut tasks[*idx]);
            let mut next_task = (tasks[*idx]).clone();
            let completion_config =
                CompletionConfig { completion_mode: c.completion_mode, completion_date_mode: c.completion_date_mode };
            let completed = tasks[*idx].complete_with_config(now, completion_config);
            if completed
                && next_task.recurrence.is_some()
                && (next_task.due_date.is_some() || next_task.threshold_date.is_some())
            {
                if next_task.create_date.is_some() {
                    next_task.create_date = Some(now);
                }
                next_task.next_dates(now);
                let do_add = if let (Some(rec_until), Some(new_due)) = (tasks[*idx].rec_until(), next_task.due_date) {
                    rec_until > new_due
                } else {
                    true
                };
                if do_add {
                    next_task.cleanup_cloned_task();
                    tasks.push(next_task);
                }
            }
            bools[i] = bools[i] || completed;
        } else {
            bools[i] = tasks[*idx].uncomplete(c.completion_mode);
        }
    }

    bools
}

/// Marks todos completed.
///
/// It works differently for regular and recurrent ones.
/// If a todo is a regular one and is not done yet, the function sets flag
/// `done` and marks the todo as modified.
/// If a todo is a recurrent one and any of due and threshold dates exist,
/// the function marks the current task done and appends a new task with
/// changed due and threshold dates (current values increased by recurrence value).
///
/// * `tasks` - the task list
/// * `ids` - the list of todo IDs which should be completed. If it is `None`
///   the entire task list is marked completed
/// * `completion_config` = how additional fields are set during completion (see todotxt::CompletionConfig)
///
/// Returns a list of boolean values: a value per each ID in `ids` or `tasks`.
/// The length of the result list equals either length of `ids`(if `ids` is
/// `Some`) or  length of `tasks`(if `ids` is `None`). Value `true` in this
/// array means that corresponding item from `ids` or `tasks` was modified.
pub fn done(tasks: &mut TaskVec, ids: Option<&IDVec>, completion_config: todotxt::CompletionConfig) -> ChangedVec {
    let c = Conf {
        done: true,
        completion_mode: completion_config.completion_mode,
        completion_date_mode: completion_config.completion_date_mode,
        ..Default::default()
    };
    done_undone(tasks, ids, &c)
}

/// Removes flag `done` from todos.
///
/// * `tasks` - the task list
/// * `ids` - the list of todo IDs which should be undone. If it is `None`
///   the entire task list is marked undone.
/// * `completion_mode` = what to do with a priority on completion (see todotxt::CompletionMode)
///
/// Returns a list of boolean values: a value per each ID in `ids` or `tasks`.
/// The length of the result list equals either length of `ids`(if `ids` is
/// `Some`) or  length of `tasks`(if `ids` is `None`). Value `true` in this
/// array means that corresponding item from `ids` or `tasks` was modified.
pub fn undone(tasks: &mut TaskVec, ids: Option<&IDVec>, mode: todotxt::CompletionMode) -> ChangedVec {
    let c = Conf { done: false, completion_mode: mode, ..Default::default() };
    done_undone(tasks, ids, &c)
}

/// Removes todos from the list
///
/// * `tasks` - the task list
/// * `ids` - the list of todo IDs which should be removed. If it is `None`
///   the task list is cleared.
///
/// Returns a list of boolean values: a value per each ID in `ids` or `tasks`.
/// The length of the result list equals either length of `ids`(if `ids` is
/// `Some`) or  length of `tasks`(if `ids` is `None`). Value `true` in this
/// array means that corresponding item from `ids` or `tasks` was modified.
/// Note: all items in `ChangedVec` are always `true`.
pub fn remove(tasks: &mut TaskVec, ids: Option<&IDVec>) -> ChangedVec {
    if tasks.is_empty() {
        return vec![];
    }
    let longvec = make_id_vec(tasks.len());
    let idlist = if let Some(v) = ids { v } else { &longvec };
    let mut bools = vec![false; idlist.len()];

    for (i, id) in idlist.iter().enumerate() {
        if *id < tasks.len() {
            bools[i] = true;
        }
    }

    let mut remained: TaskVec = Vec::new();
    for (i, t) in tasks.iter().enumerate() {
        let mut found: bool = false;
        for id in idlist.iter() {
            if *id == i {
                found = true;
                break;
            }
        }
        if !found {
            remained.push(t.clone());
        }
    }

    std::mem::swap(tasks, &mut remained);
    bools
}

fn update_priority(task: &mut todotxt::Task, c: &Conf) -> bool {
    match c.priority.action {
        Action::Set => {
            if task.priority != c.priority.value {
                task.priority = c.priority.value;
                return true;
            }
        }
        Action::Delete => {
            if task.priority != todotxt::NO_PRIORITY {
                task.priority = todotxt::NO_PRIORITY;
                return true;
            }
        }
        Action::Increase => {
            if task.priority != 0 && task.priority != todotxt::NO_PRIORITY {
                task.priority -= 1u8;
                return true;
            }
        }
        Action::Decrease => {
            if task.priority != todotxt::NO_PRIORITY {
                task.priority += 1u8;
                return true;
            }
        }
        _ => {}
    }

    false
}

fn update_due_date(task: &mut todotxt::Task, base: chrono::NaiveDate, c: &Conf) -> bool {
    match c.due.action {
        Action::Set => {
            let new_due = match &c.due.value {
                NewDateValue::None => None,
                NewDateValue::Date(dt) => Some(*dt),
                NewDateValue::Expr(expr) => {
                    let mut tlist = date_expr::TaskTagList::from_task(task);
                    match date_expr::calculate_expr(base, expr, &mut tlist, c.soon_days) {
                        Err(e) => {
                            eprintln!("Failed to calculate due date expression [{expr}]: {e:?}");
                            return false;
                        }
                        Ok(d) => Some(d),
                    }
                }
            };
            if tsort::cmp_opt_dates(task.due_date, new_due) != Ordering::Equal {
                match new_due {
                    None => task.update_tag_with_value(todotxt::DUE_TAG, ""),
                    Some(dt) => task.update_tag_with_value(todotxt::DUE_TAG, &todotxt::format_date(dt)),
                };
                return true;
            }
        }
        Action::Delete => {
            if task.due_date.is_some() {
                task.update_tag_with_value(todotxt::DUE_TAG, "");
                return true;
            }
        }
        _ => {}
    }

    false
}

fn update_thr_date(task: &mut todotxt::Task, base: chrono::NaiveDate, c: &Conf) -> bool {
    match c.thr.action {
        Action::Set => {
            let new_thr = match &c.thr.value {
                NewDateValue::None => None,
                NewDateValue::Date(dt) => Some(*dt),
                NewDateValue::Expr(expr) => {
                    let mut tlist = date_expr::TaskTagList::from_task(task);
                    match date_expr::calculate_expr(base, expr, &mut tlist, c.soon_days) {
                        Err(e) => {
                            eprintln!("Failed to calculate threshold date expression [{expr}]: {e:?}");
                            return false;
                        }
                        Ok(d) => Some(d),
                    }
                }
            };
            if tsort::cmp_opt_dates(task.threshold_date, new_thr) != Ordering::Equal {
                match new_thr {
                    None => task.update_tag_with_value(todotxt::THR_TAG, ""),
                    Some(dt) => task.update_tag_with_value(todotxt::THR_TAG, &todotxt::format_date(dt)),
                };
                return true;
            }
        }
        Action::Delete => {
            if task.threshold_date.is_some() {
                task.update_tag_with_value(todotxt::THR_TAG, "");
                return true;
            }
        }
        _ => {}
    }

    false
}

fn update_recurrence(task: &mut todotxt::Task, c: &Conf) -> bool {
    match c.recurrence.action {
        Action::Set => {
            if !tsort::equal_opt_rec(&task.recurrence, &c.recurrence.value)
                && let Some(nr) = &c.recurrence.value
            {
                let new_rec = format!("{nr}");
                let updated = task.update_tag(&new_rec);
                if updated && task.finished {
                    task.uncomplete(c.completion_mode);
                }
                return updated;
            }
        }
        Action::Delete => {
            if task.recurrence.is_some() {
                task.update_tag_with_value(todotxt::REC_TAG, "");
                return true;
            }
        }
        _ => {}
    }

    false
}

fn update_projects(task: &mut todotxt::Task, c: &Conf) -> bool {
    let mut changed = false;

    for new_p in &c.projects.value {
        match c.projects.action {
            Action::Set => {
                let old_subj = task.subject.clone();
                task.replace_project("", new_p);
                changed = old_subj != task.subject;
            }
            Action::Delete => {
                let old_subj = task.subject.clone();
                task.replace_project(new_p, "");
                changed = old_subj != task.subject;
            }
            Action::Replace => {
                let pair: Vec<&str> = new_p.split_terminator('+').collect();
                if pair.len() == 2 && pair[0] != pair[1] && !pair[0].is_empty() && !pair[1].is_empty() {
                    let old_subj = task.subject.clone();
                    task.replace_project(pair[0], pair[1]);
                    changed = old_subj != task.subject;
                }
            }
            _ => {}
        }
    }

    changed
}

fn update_contexts(task: &mut todotxt::Task, c: &Conf) -> bool {
    let mut changed = false;

    for new_c in &c.contexts.value {
        match c.contexts.action {
            Action::Set => {
                let old_subj = task.subject.clone();
                task.replace_context("", new_c);
                changed = old_subj != task.subject;
            }
            Action::Delete => {
                let old_subj = task.subject.clone();
                task.replace_context(new_c, "");
                changed = old_subj != task.subject;
            }
            Action::Replace => {
                let pair: Vec<&str> = new_c.split_terminator('@').collect();
                if pair.len() == 2 && pair[0] != pair[1] && !pair[0].is_empty() && !pair[1].is_empty() {
                    let old_subj = task.subject.clone();
                    task.replace_context(pair[0], pair[1]);
                    changed = old_subj != task.subject;
                }
            }
            _ => {}
        }
    }

    changed
}

fn tag_update_check(task: &mut todotxt::Task, tag: &str, value: &str) -> bool {
    let old_subj = task.subject.clone();
    let updated = task.update_tag_with_value(tag, value);
    if value.is_empty() {
        task.tags.remove(tag);
    } else {
        task.tags.insert(tag.to_string(), value.to_string());
    }
    updated && old_subj != task.subject
}

fn update_tags(task: &mut todotxt::Task, c: &Conf) -> bool {
    let mut changed = false;
    if let Some(tag_list) = &c.tags.value {
        for (tag, value) in tag_list {
            let tag = tag.trim_end_matches(':');
            if is_tag_special(tag) {
                continue;
            }
            match c.tags.action {
                Action::Delete => {
                    changed |= tag_update_check(task, tag, "");
                }
                Action::Set => {
                    changed |= tag_update_check(task, tag, value);
                }
                _ => {}
            }
        }
    }
    changed
}

fn hashtag_update_check(task: &mut todotxt::Task, hashtag: &str, act: Action) -> bool {
    let old_subj = task.subject.clone();
    let mut new_subj = old_subj.clone();
    let hashtag = hashtag.trim_start_matches('#');
    let hashtag_full = format!("#{hashtag}");
    match act {
        Action::Delete => {
            task.hashtags.retain(|h| h != hashtag);
            todotxt::replace_word(&mut new_subj, &hashtag_full, "");
            if new_subj != old_subj {
                task.subject = new_subj;
                return true;
            }
        }
        Action::Set => {
            if !task.hashtags.contains(&hashtag.to_string()) {
                task.hashtags.push(hashtag.to_string());
                task.subject = format!("{} {}", task.subject, hashtag_full);
                return true;
            }
        }
        Action::Replace => {
            if let Some((old, new)) = todotxt::split_tag(hashtag) {
                let old = old.trim_start_matches('#');
                let old_str = old.to_string();
                let new = new.trim_start_matches('#');
                if old != new && task.hashtags.contains(&old.to_string()) {
                    task.hashtags.retain(|h| h != &old_str);
                    task.hashtags.push(new.to_string());
                    let old = format!("#{old}");
                    let new = format!("#{new}");
                    todotxt::replace_word(&mut new_subj, &old, &new);
                    task.subject = new_subj;
                    return true;
                }
            }
        }
        _ => {}
    }
    false
}

fn update_hashtags(task: &mut todotxt::Task, c: &Conf) -> bool {
    let mut changed = false;
    for hashtag in &c.hashtags.value {
        let hashtag = hashtag.trim_start_matches('#');
        match c.hashtags.action {
            Action::Delete | Action::Set | Action::Replace => {
                changed |= hashtag_update_check(task, hashtag, c.hashtags.action)
            }
            _ => {}
        }
    }
    changed
}

/// Modifies existing todos.
///
/// A powerful function to transform todo list. List of operations:
/// - for priority: change, remove, increase or decrease by one;
/// - for subject: set a new subject;
/// - for due date: remove or set a new one;
/// - for recurrence: remove or set a new one (setting recurrence clears
///   `done` flag;
/// - for project: add a new, remove old, replace old with new one;
/// - for context: add a new, remove old, replace old with new one;
/// - for an arbitrary tag: add a new, remove old, replace old with a new one;
///
/// * `tasks` - the task list
/// * `ids` - the list of todo IDs which should be undone. If it is `None`
///   the entire task list is marked undone.
/// * `c` - what to modify and how
///
/// if `c` contains a new subject then only the first todo from `ids` is
/// modified. For all other cases the function processes all todos from `ids`.
///
/// Returns a list of boolean values: a value per each ID in `ids` or `tasks`.
/// The length of the result list equals either length of `ids`(if `ids` is
/// `Some`) or  length of `tasks`(if `ids` is `None`). Value `true` in this
/// array means that corresponding item from `ids` or `tasks` was modified.
pub fn edit(tasks: &mut TaskVec, ids: Option<&IDVec>, c: &Conf) -> ChangedVec {
    if tasks.is_empty() {
        return vec![];
    }

    let longvec = make_id_vec(tasks.len());
    let idlist = if let Some(v) = ids { v } else { &longvec };
    let now = chrono::Local::now().date_naive();

    let mut bools = vec![false; idlist.len()];
    for (i, idx) in idlist.iter().enumerate() {
        let id = *idx;
        if id >= tasks.len() {
            continue;
        }

        if let Some(subj) = c.subject.as_ref() {
            let mut t = todotxt::Task::parse(subj, now);
            if t.create_date.is_none() && tasks[id].create_date.is_some() {
                t.create_date = tasks[id].create_date;
            }
            tasks[id] = t;
            bools[i] = true;
            // it does not make sense to replace more than 1 todo's subject
            // with the same text. So, replace for the first one and stop
            break;
        }

        bools[i] = update_priority(&mut tasks[id], c);
        bools[i] |= update_due_date(&mut tasks[id], now, c);
        bools[i] |= update_thr_date(&mut tasks[id], now, c);
        bools[i] |= update_recurrence(&mut tasks[id], c);
        bools[i] |= update_projects(&mut tasks[id], c);
        bools[i] |= update_contexts(&mut tasks[id], c);
        bools[i] |= update_tags(&mut tasks[id], c);
        bools[i] |= update_hashtags(&mut tasks[id], c);
    }

    bools
}

/// Starts timers of all toods that are not done
pub fn start(tasks: &mut TaskVec, ids: Option<&IDVec>) -> ChangedVec {
    if tasks.is_empty() {
        return vec![];
    }

    let longvec = make_id_vec(tasks.len());
    let idlist = if let Some(v) = ids { v } else { &longvec };

    let mut bools = vec![false; idlist.len()];
    for (i, idx) in idlist.iter().enumerate() {
        let id = *idx;
        if id >= tasks.len() {
            continue;
        }

        bools[i] = timer::start_timer(&mut tasks[id]);
    }

    bools
}

/// Stops timers of all toods that are running
pub fn stop(tasks: &mut TaskVec, ids: Option<&IDVec>) -> ChangedVec {
    if tasks.is_empty() {
        return vec![];
    }

    let longvec = make_id_vec(tasks.len());
    let idlist = if let Some(v) = ids { v } else { &longvec };

    let mut bools = vec![false; idlist.len()];
    for (i, idx) in idlist.iter().enumerate() {
        let id = *idx;
        if id >= tasks.len() {
            continue;
        }

        bools[i] = timer::stop_timer(&mut tasks[id]);
    }

    bools
}