krz/orgstar

A native macOS editor for org-mode files. editor org-mode swift

Sources/OrgCore/Agenda/AgendaSource.swift

191ddb506f3a91b93c755ba6e8123a4e8ac4f90d
orgstar/Sources/OrgCore/Agenda/AgendaSource.swift history · blame · raw

380 lines · 20079 bytes

  1import Foundation
  2
  3/// What the agenda needs from one file, read once from its text: headings with their planning,
  4/// and the active timestamps and date ranges org's agenda would consider. Positions are UTF-16.
  5public struct AgendaSource: Sendable {
  6    public let path: String
  7    /// The file names `#+SETUPFILE`s.
  8    public let readsSetupFiles: Bool
  9
 10    struct Heading: Sendable {
 11        /// Start of the heading line.
 12        let start: Int
 13        let level: Int
 14        /// In an archived or commented tree, which the agenda skips.
 15        let skipped: Bool
 16        /// The title without keyword, priority and tags (`ITEM`).
 17        let title: String
 18        /// The priority cookie's value.
 19        let priority: String?
 20        let localTags: [String]
 21        /// Upper-cased keys of the entry's own drawer.
 22        let properties: [String: String]
 23        /// Planning timestamps with brackets, for property matches.
 24        let planning: [String: String]
 25        /// The first active and inactive timestamps in the entry (`TIMESTAMP`, `TIMESTAMP_IA`).
 26        let firstActive: String?
 27        let firstInactive: String?
 28        /// The heading line without its newline.
 29        let line: String
 30        /// The line after the stars.
 31        let head: String
 32        let todo: String?
 33        let isDone: Bool
 34        /// Inherited tags first; the flag marks inherited ones.
 35        let tags: [(name: String, inherited: Bool)]
 36        let category: String
 37        /// The planning timestamp without brackets, and the position of its `<`.
 38        let scheduled: (raw: String, offset: Int)?
 39        let deadline: (raw: String, offset: Int)?
 40        /// `STYLE: habit`.
 41        let isHabit: Bool
 42        /// `org-habit-parse-todo`, nil where it signals an error.
 43        let habit: Habit?
 44        /// `org-entry-blocked-p` with only `org-enforce-todo-checkbox-dependencies`, and with
 45        /// only `org-enforce-todo-dependencies`.
 46        let blockedByCheckboxes: Bool
 47        let blockedByHierarchy: Bool
 48    }
 49
 50    struct Stamp: Sendable {
 51        let offset: Int
 52        let heading: Int
 53        /// From the `<` to the end of the line.
 54        let rest: String
 55    }
 56
 57    struct Block: Sendable {
 58        let start: String
 59        let end: String
 60        /// End of the range; org's marker sits there.
 61        let marker: Int
 62        let heading: Int
 63    }
 64
 65    /// A `CLOSED:`, `CLOCK:` or `- State "…"` line, for log mode (`org-agenda-get-progress`).
 66    struct Progress: Sendable {
 67        enum Kind: Sendable { case closed, clock, state }
 68        let kind: Kind
 69        let offset: Int
 70        let heading: Int
 71        /// `YYYY-MM-DD` of the first timestamp.
 72        let date: String
 73        /// The timestamp, from the keyword; a clock's ends with its end time (`[… 10:00-11:30]`).
 74        let timestamp: String
 75        /// A clock's duration, or `-`.
 76        let clocked: String
 77        /// The state a note records.
 78        let state: String?
 79        /// The first line of the note under it.
 80        let note: String?
 81    }
 82
 83    /// A `%%(…)` line (`org-agenda-get-sexps`).
 84    struct SexpLine: Sendable {
 85        /// Start of the line.
 86        let offset: Int
 87        /// The heading the line is under.
 88        let heading: Int
 89        let sexp: Sexp
 90        /// The rest of the line after the sexp.
 91        let entry: String
 92    }
 93
 94    let headings: [Heading]
 95    let stamps: [Stamp]
 96    let sexps: [SexpLine]
 97    let blocks: [Block]
 98    let progress: [Progress]
 99    /// The file's TODO keywords, active and done.
100    let keywords: [String]
101    /// The active ones (`org-not-done-keywords`).
102    let notDoneKeywords: [String]
103    /// The file's text, for the search view.
104    let text: String
105    let priorities: Priorities
106
107    public init(path: String, text: String, defaults: OrgSettings = .default) {
108        self.path = path
109        readsSetupFiles = text.range(of: "#+setupfile:", options: .caseInsensitive) != nil
110        let tree = OrgParser.parse(text, defaults: defaults)
111        let model = DocumentModel(tree: tree)
112        let ns = text as NSString
113        let settings = tree.settings
114        keywords = settings.todoSequences.flatMap { $0.active.map(\.name) + $0.done.map(\.name) }
115        notDoneKeywords = settings.todoSequences.flatMap { $0.active.map(\.name) }
116        self.text = text
117        priorities = settings.priorities
118
119        var stampsByHeading: [Int: [String]] = [:]
120        var headingIndex = -1
121        func collect(_ node: SyntaxNode) {
122            for child in node.children {
123                if child.kind == .heading { headingIndex += 1 }
124                if child.kind == .timestamp, headingIndex >= 0 {
125                    stampsByHeading[headingIndex, default: []].append(child.text)
126                } else if child.kind != .block, child.kind != .planning {
127                    collect(child)
128                }
129            }
130        }
131        collect(tree.root)
132        func sectionStamps(_ index: Int) -> [String] { stampsByHeading[index] ?? [] }
133
134        var keywordCategory = SetupFile.keywords(tree.settings.setupKeywords).last { $0.key == "CATEGORY" }?.value.trimmingCharacters(in: .whitespaces)
135        var srcBodies: [Range<Int>] = []
136        var planningLines: [Range<Int>] = []
137        var timestampNodes: [Int] = []
138        var propertyLines: [Range<Int>] = []
139        func walk(_ node: SyntaxNode) {
140            for child in node.children {
141                switch child.kind {
142                case .keyword:
143                    let line = child.text.trimmingCharacters(in: .whitespacesAndNewlines)
144                    if line.uppercased().hasPrefix("#+CATEGORY:") {
145                        keywordCategory = line.dropFirst("#+CATEGORY:".count).trimmingCharacters(in: .whitespaces)
146                    }
147                case .block:
148                    if DocumentModel.isSrcBlock(child) {
149                        let lines = child.range
150                        let firstEnd = ns.range(of: "\n", range: NSRange(location: lines.lowerBound, length: lines.count)).location
151                        let lastStart = ns.lineRange(for: NSRange(location: max(lines.lowerBound, lines.upperBound - 1), length: 0)).location
152                        if firstEnd != NSNotFound, firstEnd + 1 <= lastStart { srcBodies.append((firstEnd + 1)..<lastStart) }
153                    }
154                case .planning:
155                    planningLines.append(child.range)
156                case .clock:
157                    break
158                case .nodeProperty:
159                    propertyLines.append(child.range)
160                case .timestamp:
161                    if child.text.hasPrefix("<") { timestampNodes.append(child.offset) }
162                default:
163                    walk(child)
164                }
165            }
166        }
167        walk(tree.root)
168
169        let fileName = ((path as NSString).lastPathComponent as NSString).deletingPathExtension
170        let fileCategory = keywordCategory ?? model.property("CATEGORY", of: nil)?.value ?? fileName
171        let fileTags = model.fileTags
172        let fileArchived = fileTags.contains("ARCHIVE")
173
174        // Archived and commented trees are skipped whole, as `org-agenda-skip` does.
175        var skipped: [Bool] = []
176        var headings: [Heading] = []
177        var options = EditingOptions.org
178        options.listAllowAlphabetical = settings.listAllowAlphabetical
179        let buffer = EmacsBuffer(text, point: 0, settings: settings, options: options)
180        for (index, info) in model.headings.enumerated() {
181            let own = info.localTags.contains("ARCHIVE") || Self.isCommented(info.title)
182            skipped.append(fileArchived || own || (info.parent.map { skipped[$0] } ?? false))
183            let line = ns.substring(with: NSRange(location: info.headingRange.lowerBound, length: info.headingRange.count))
184            let head = line.replacingOccurrences(of: "^\\*+[ \\t]+", with: "", options: .regularExpression)
185                .trimmingCharacters(in: .newlines)
186
187            var itags: [String] = fileTags
188            for ancestor in model.ancestors(of: index) { itags += model.headings[ancestor].localTags }
189            var tags: [(String, Bool)] = itags.map { ($0, true) } + info.localTags.map { ($0, false) }
190            // `delete-dups` over the reversed list: the most local copy of a tag stays.
191            var seen: Set<String> = []
192            tags = tags.reversed().filter { seen.insert($0.0).inserted }.reversed()
193
194            var category = fileCategory
195            for layer in ([index] + model.ancestors(of: index).reversed()) {
196                if let value = model.headings[layer].properties.last(where: { $0.key.uppercased() == "CATEGORY" && !$0.additive })?.value {
197                    category = value
198                    break
199                }
200            }
201
202            var scheduled: (String, Int)?
203            var deadline: (String, Int)?
204            var planningValues: [String: String] = [:]
205            let afterHeading = info.headingRange.upperBound
206            if let planning = planningLines.first(where: { $0.lowerBound == afterHeading }) {
207                let planningText = ns.substring(with: NSRange(location: planning.lowerBound, length: planning.count))
208                let regex = try! NSRegularExpression(pattern: "\\b(SCHEDULED|DEADLINE): *<([^>]+)>")
209                let pns = planningText as NSString
210                for m in regex.matches(in: planningText, range: NSRange(location: 0, length: pns.length)) {
211                    let value = (pns.substring(with: m.range(at: 2)), planning.lowerBound + m.range(at: 2).location - 1)
212                    if pns.substring(with: m.range(at: 1)) == "SCHEDULED" { scheduled = scheduled ?? value } else { deadline = deadline ?? value }
213                }
214                let any = try! NSRegularExpression(pattern: "\\b(SCHEDULED|DEADLINE|CLOSED): *([\\[<]\(Self.tsInternal)[\\]>])")
215                for m in any.matches(in: planningText, range: NSRange(location: 0, length: pns.length)) {
216                    let key = pns.substring(with: m.range(at: 1))
217                    if planningValues[key] == nil { planningValues[key] = pns.substring(with: m.range(at: 2)) }
218                }
219            }
220            var properties: [String: String] = [:]
221            for property in info.properties {
222                let key = property.key.uppercased()
223                if property.additive, let value = properties[key] {
224                    properties[key] = value + " " + property.value
225                } else {
226                    properties[key] = property.value
227                }
228            }
229            let ownStamps = sectionStamps(index)
230            let isHabit = properties["STYLE"]?.trimmingCharacters(in: .whitespaces) == "habit"
231            var habit: Habit?
232            if isHabit, let scheduled {
233                let end = index + 1 < model.headings.count ? model.headings[index + 1].headingRange.lowerBound : ns.length
234                let entry = ns.substring(with: NSRange(location: info.headingRange.lowerBound, length: end - info.headingRange.lowerBound))
235                habit = Habit.parse(entry: entry, scheduled: scheduled.0, doneKeywords: settings.todoSequences.flatMap { $0.done.map(\.name) })
236            }
237            var blockedByCheckboxes = false
238            var blockedByHierarchy = false
239            if info.todo != nil, !info.isDone, buffer.localProperty("NOBLOCKING", heading: info.headingRange.lowerBound).map({ $0 == "nil" }) ?? true {
240                buffer.point = info.headingRange.lowerBound
241                blockedByCheckboxes = buffer.hasOpenCheckbox()
242                blockedByHierarchy = buffer.hierarchyBlocker() != nil
243            }
244            headings.append(Heading(
245                start: info.headingRange.lowerBound, level: info.level, skipped: skipped[index],
246                title: info.title.trimmingCharacters(in: .whitespaces), priority: info.priority, localTags: info.localTags,
247                properties: properties, planning: planningValues,
248                firstActive: ownStamps.first { $0.hasPrefix("<") }, firstInactive: ownStamps.first { $0.hasPrefix("[") },
249                line: line.trimmingCharacters(in: .newlines), head: head, todo: info.todo, isDone: info.isDone,
250                tags: tags, category: category, scheduled: scheduled, deadline: deadline, isHabit: isHabit, habit: habit,
251                blockedByCheckboxes: blockedByCheckboxes, blockedByHierarchy: blockedByHierarchy
252            ))
253        }
254        self.headings = headings
255
256        let starts = model.headings.map(\.headingRange.lowerBound)
257        func heading(at offset: Int) -> Int? {
258            var low = 0
259            var high = starts.count
260            while low < high {
261                let mid = (low + high) / 2
262                if starts[mid] <= offset { low = mid + 1 } else { high = mid }
263            }
264            return low == 0 ? nil : low - 1
265        }
266        func restOfLine(_ offset: Int) -> String {
267            let line = ns.lineRange(for: NSRange(location: offset, length: 0))
268            return ns.substring(with: NSRange(location: offset, length: NSMaxRange(line) - offset)).trimmingCharacters(in: .newlines)
269        }
270
271        // Timestamps: those the parser sees as timestamp objects, and dates in property lines.
272        var stampOffsets = timestampNodes
273        let datePattern = try! NSRegularExpression(pattern: "<[0-9]{4}-[0-9]{2}-[0-9]{2}")
274        for line in propertyLines {
275            for m in datePattern.matches(in: text, range: NSRange(location: line.lowerBound, length: line.count)) {
276                stampOffsets.append(m.range.location)
277            }
278        }
279        var stamps: [Stamp] = []
280        for offset in stampOffsets.sorted() {
281            guard let owner = heading(at: offset), !skipped[owner] else { continue }
282            let rest = restOfLine(offset)
283            guard rest.range(of: "^<([0-9]{4}-[0-9]{2}-[0-9]{2}|%%\\()", options: .regularExpression) != nil else { continue }
284            if Self.inDateRange(offset, ns) { continue }
285            stamps.append(Stamp(offset: offset, heading: owner, rest: rest))
286        }
287        self.stamps = stamps
288
289        // Sexp lines: org searches the raw text for `%%(` at the start of a line. Before the
290        // first heading org's agenda signals an error; they're left out.
291        var sexps: [SexpLine] = []
292        let sexpLine = try! NSRegularExpression(pattern: "^&?%%\\(", options: .anchorsMatchLines)
293        for m in sexpLine.matches(in: text, range: NSRange(location: 0, length: ns.length)) {
294            guard let owner = heading(at: m.range.location), !skipped[owner] else { continue }
295            let open = NSMaxRange(m.range) - 1
296            let tail = ns.substring(with: NSRange(location: open, length: min(ns.length - open, 4096)))
297            guard let (sexp, length) = try? LispReader.readFirst(tail) else { continue }
298            let afterSexp = String(tail.dropFirst(length))
299            let lineRest = afterSexp.prefix { $0 != "\n" && $0 != "\r" }
300            let entry = lineRest.trimmingCharacters(in: CharacterSet(charactersIn: " \t"))
301            sexps.append(SexpLine(offset: m.range.location, heading: owner, sexp: sexp, entry: entry))
302        }
303        self.sexps = sexps
304
305        // Ranges: org scans the raw text, skipping comment lines, skipped trees and src blocks.
306        var blocks: [Block] = []
307        let range = try! NSRegularExpression(pattern: "<(\(Self.tsInternal))>--?-?<(\(Self.tsInternal))>")
308        for m in range.matches(in: text, range: NSRange(location: 0, length: ns.length)) {
309            let point = NSMaxRange(m.range)
310            let line = ns.substring(with: ns.lineRange(for: NSRange(location: point, length: 0)))
311            if line.range(of: "^\\s*#( |$|\\n)", options: .regularExpression) != nil { continue }
312            guard let owner = heading(at: max(0, point - 1)), !skipped[owner] else { continue }
313            if srcBodies.contains(where: { $0.contains(point) }) { continue }
314            blocks.append(Block(start: ns.substring(with: m.range(at: 1)), end: ns.substring(with: m.range(at: 2)), marker: point, heading: owner))
315        }
316        self.blocks = blocks
317
318        // Log lines: org searches the raw text, then the heading above each match.
319        let keywordPattern = keywords.map(NSRegularExpression.escapedPattern(for:)).joined(separator: "|")
320        let logLine = try! NSRegularExpression(pattern: "(\\bCLOSED:|\\bCLOCK:|- +State \"(\(keywordPattern.isEmpty ? "(?!)" : keywordPattern))\".*?) *\\[([0-9]{4}-[0-9]{2}-[0-9]{2})")
321        let inactive = try! NSRegularExpression(pattern: "\\[([0-9]{4}-[0-9]{2}-[0-9]{2}( +[^\\]+0-9>\\r\\n -]+)?( +([0-9]{1,2}):([0-9]{2}))?)\\]")
322        let clockEnd = try! NSRegularExpression(pattern: "([0-9]{1,2}:[0-9]{2})\\].*?([0-9]{1,2}:[0-9]{2})")
323        let stateNote = try! NSRegularExpression(pattern: ".*\\\\\\n[ \\t]*([^-\\n \\t].*?)[ \\t]*$", options: .anchorsMatchLines)
324        let clockNote = try! NSRegularExpression(pattern: ".*\\n[ \\t]*-[ \\t]+([^-\\n \\t].*?)[ \\t]*$", options: .anchorsMatchLines)
325        var progress: [Progress] = []
326        for m in logLine.matches(in: text, range: NSRange(location: 0, length: ns.length)) {
327            guard let owner = heading(at: m.range.location), !skipped[owner] else { continue }
328            let keyword = ns.substring(with: m.range(at: 1))
329            let kind: Progress.Kind = keyword == "CLOSED:" ? .closed : keyword == "CLOCK:" ? .clock : .state
330            var timestamp = restOfLine(m.range.location)
331            var clocked = "-"
332            let tns = timestamp as NSString
333            if let ts = inactive.firstMatch(in: timestamp, range: NSRange(location: 0, length: tns.length)) {
334                let rest = tns.substring(from: NSMaxRange(ts.range))
335                timestamp = tns.substring(to: NSMaxRange(ts.range))
336                let rns = rest as NSString
337                if kind == .clock, let end = clockEnd.firstMatch(in: rest, range: NSRange(location: 0, length: rns.length)) {
338                    timestamp = String(timestamp.dropLast()) + "-" + rns.substring(with: end.range(at: 1)) + "]"
339                    clocked = rns.substring(with: end.range(at: 2))
340                }
341            }
342            // The note: after a state line ending in `\\`, or a list item under a clock line.
343            var note: String?
344            let after = NSMaxRange(m.range)
345            let notePattern: NSRegularExpression? = switch kind {
346            case .state: stateNote
347            case .clock: clockNote
348            case .closed: nil
349            }
350            if let notePattern, let n = notePattern.firstMatch(in: text, options: .anchored, range: NSRange(location: after, length: ns.length - after)) {
351                note = ns.substring(with: n.range(at: 1))
352            }
353            progress.append(Progress(
354                kind: kind, offset: m.range.location, heading: owner, date: ns.substring(with: m.range(at: 3)), timestamp: timestamp,
355                clocked: clocked, state: m.range(at: 2).location == NSNotFound ? nil : ns.substring(with: m.range(at: 2)), note: note
356            ))
357        }
358        self.progress = progress
359    }
360
361    /// `org-ts--internal-regexp`.
362    static let tsInternal = "[0-9]{4}-[0-9]{2}-[0-9]{2}(?: .*?)?"
363
364    /// `org-element` reads a title starting with `COMMENT` as a commented heading.
365    static func isCommented(_ title: String) -> Bool {
366        title == "COMMENT" || title.hasPrefix("COMMENT ")
367    }
368
369    /// `org-at-date-range-p` with point just after the date that starts at `offset`.
370    static func inDateRange(_ offset: Int, _ ns: NSString) -> Bool {
371        let point = offset + 11
372        let line = ns.lineRange(for: NSRange(location: offset, length: 0))
373        let regex = try! NSRegularExpression(pattern: "[\\[<](\(tsInternal))[\\]>]--?-?[\\[<](\(tsInternal))[\\]>]")
374        for m in regex.matches(in: ns as String, range: line) {
375            if m.range.location > point { break }
376            if NSMaxRange(m.range) >= point { return true }
377        }
378        return false
379    }
380}