Sources/OrgApp/AgendaFiles.swift

58f3b73e8ff337e5a15111fbb32c0d57cf2346da
orgstar/Sources/OrgApp/AgendaFiles.swift history · blame · raw

58 lines · 3089 bytes

6 symbols in this file
 1import Foundation
 2import OrgCore
 3
 4/// `org-agenda-files` as the `(org-agenda-files)` function reads it: each entry a folder (its
 5/// `.org` files not starting with a dot, in name order, not below it), an `.org` file, or a
 6/// file listing more entries one per line, as the variable's string form names one.
 7/// Relative paths count from `base`, standing in for `org-directory`. Missing files are left out.
 8public enum AgendaFiles {
 9    public static func resolve(_ entries: [String], base: String?, fileManager: FileManager = .default) -> [String] {
10        var out: [String] = []
11        var seen: Set<String> = []
12        func expand(_ entry: String, from base: String?) -> String {
13            let path = OrgPaths.expandingTilde(entry)
14            if path.hasPrefix("/") || base == nil { return (path as NSString).standardizingPath }
15            return ((base! as NSString).appendingPathComponent(path) as NSString).standardizingPath
16        }
17        func add(_ path: String) {
18            if seen.insert(path).inserted { out.append(path) }
19        }
20        func visit(_ entry: String, listed: Bool) {
21            let path = expand(entry, from: base)
22            var isDirectory: ObjCBool = false
23            guard fileManager.fileExists(atPath: path, isDirectory: &isDirectory) else { return }
24            if isDirectory.boolValue {
25                let names = (try? fileManager.contentsOfDirectory(atPath: path)) ?? []
26                for name in names.sorted() where !name.hasPrefix(".") && name.hasSuffix(".org") {
27                    let file = (path as NSString).appendingPathComponent(name)
28                    var dir: ObjCBool = false
29                    if fileManager.fileExists(atPath: file, isDirectory: &dir), !dir.boolValue { add(file) }
30                }
31            } else if path.hasSuffix(".org") || listed {
32                add(path)
33            } else if let text = try? String(contentsOfFile: path, encoding: .utf8) {
34                // `org-read-agenda-file-list`.
35                for line in text.components(separatedBy: .newlines) {
36                    let trimmed = line.trimmingCharacters(in: .whitespaces)
37                    if !trimmed.isEmpty { visit(trimmed, listed: true) }
38                }
39            }
40        }
41        for entry in entries { visit(entry, listed: false) }
42        return out
43    }
44}
45
46extension WorkspaceModel {
47    /// `(org-agenda-files)`: `list` resolved, or when it is empty the org files at the top of
48    /// each folder (or below it, with `includeSubfolders`), by path, as Emacs lists a
49    /// directory.
50    public func agendaFilePaths(list: [String] = OrgPreferences.agendaFiles(), includeSubfolders: Bool = OrgPreferences.agendaSubfolders()) -> [String] {
51        if !list.isEmpty { return AgendaFiles.resolve(list, base: rootPaths.first) }
52        return files
53            // Dotfiles stay out, as `org-agenda-file-regexp` leaves them out.
54            .filter { $0.kind == .org && !$0.name.hasPrefix(".") && (includeSubfolders || !$0.relativePath.contains("/")) }
55            .map(\.path)
56            .sorted()
57    }
58}