Sources/OrgCore/Agenda/StuckProjects.swift

06941b2cf25c6ccc1629723cd7951910ce15e67a
orgstar/Sources/OrgCore/Agenda/StuckProjects.swift history · blame · raw

65 lines · 3343 bytes

 1import Foundation
 2
 3/// `org-stuck-projects` (Org 9.8.7): projects are the entries `match` finds; one is not stuck
 4/// when its subtree, its own heading included, has a heading with one of `todo` (`*` for any
 5/// open keyword), a heading with one of `tags` set on it (`*` for any tag), or text matching
 6/// `regexp`.
 7public struct StuckProjects: Sendable, Hashable {
 8    public var match: String
 9    public var todo: [String]
10    public var tags: [String]
11    /// In Emacs syntax; empty for none.
12    public var regexp: String
13
14    public init(match: String = "+LEVEL=2/-DONE", todo: [String] = ["TODO", "NEXT", "NEXTACTION"], tags: [String] = [], regexp: String = "") {
15        self.match = match
16        self.todo = todo
17        self.tags = tags
18        self.regexp = regexp
19    }
20
21    /// The `skip-re` of `org-agenda-list-stuck-projects`, compiled; nil when the definition
22    /// names nothing that unsticks a project.
23    func unstuck(_ sources: [AgendaSource]) -> NSRegularExpression? {
24        var parts: [String] = []
25        if !todo.isEmpty {
26            var words = todo
27            if words.contains("*") {
28                var open: [String] = []
29                for source in sources { for k in source.notDoneKeywords where !open.contains(k) { open.append(k) } }
30                words = open
31            }
32            parts.append("^\\*+[ \\t]+(" + words.map(NSRegularExpression.escapedPattern(for:)).joined(separator: "|") + ")(?:[ \\t]|$)")
33        }
34        if tags.contains("*") {
35            parts.append(SetTags.tagLine)
36        } else if !tags.isEmpty {
37            let other = "(?:[[:alnum:]_@#%]+:)*"
38            parts.append("^\\*+ .*?[ \\t]:" + other + "(" + tags.map(NSRegularExpression.escapedPattern(for:)).joined(separator: "|") + "):" + other + "[ \\t]*$")
39        }
40        if regexp.contains(where: { !$0.isWhitespace }) { parts.append(EmacsRegex.translate(regexp)) }
41        guard !parts.isEmpty else { return nil }
42        return try? NSRegularExpression(pattern: parts.map { "(?:" + $0 + ")" }.joined(separator: "|"), options: .anchorsMatchLines)
43    }
44}
45
46extension Agenda {
47    /// `org-agenda-list-stuck-projects`: the tags view of `definition.match`, without the
48    /// projects that something in their subtree unsticks. With nothing to unstick them (Org
49    /// signals an error), every match is listed.
50    public static func stuckProjects(
51        _ sources: [AgendaSource], _ definition: StuckProjects, now: Date = Date(), calendar: Calendar = .current, options: AgendaOptions = AgendaOptions()
52    ) -> [AgendaItem] {
53        let unstuck = definition.unstuck(sources)
54        return tagsMatch(sources, match: definition.match, now: now, calendar: calendar, options: options) { source, index in
55            guard let unstuck else { return false }
56            let heading = source.headings[index]
57            let next = source.headings[(index + 1)...].first { $0.level <= heading.level }?.start
58            let ns = source.text as NSString
59            // `org-end-of-subtree`: before the line break ending the subtree's last line.
60            var end = next ?? ns.length
61            if end > heading.start, ns.character(at: end - 1) == 10 { end -= 1 }
62            return unstuck.firstMatch(in: source.text, range: NSRange(location: heading.start, length: end - heading.start)) != nil
63        }
64    }
65}