Sources/OrgCore/Commands/Archive.swift
365 lines · 18738 bytes
1import Foundation
2
3// Archiving (`org-archive-subtree`, Org 9.8.7) with the defaults: location "%s_archive::",
4// context info time, file, olpath, category, todo and itags, inherited tags added only within
5// the same file, no marking DONE, the archive file's header for a new file.
6
7extension EmacsBuffer {
8 /// The first value of `#+NAME:` in the buffer, as `org-collect-keywords` with UNIQUE.
9 func keyword(_ name: String) -> String? {
10 saveExcursion {
11 point = 0
12 guard let match = searchForward("^[ \\t]*#\\+" + NSRegularExpression.escapedPattern(for: name) + ":[ \\t]*(.*?)[ \\t]*$") else { return nil }
13 return substring(match[1]!.lowerBound, match[1]!.upperBound)
14 }
15 }
16
17 /// `org-back-to-heading-or-point-min`.
18 func backToHeadingOrPointMin() {
19 if beforeFirstHeading { point = 0 } else { _ = try? backToHeading() }
20 }
21
22 static let propertyDrawerPattern = "^[ \\t]*:PROPERTIES:[ \\t]*\\n(?:[ \\t]*:\\S+:(?:[ \\t].*)?[ \\t]*\\n)*?[ \\t]*:END:[ \\t]*$"
23
24 /// `org-insert-property-drawer`.
25 func insertPropertyDrawer() {
26 saveExcursion {
27 backToHeadingOrPointMin()
28 if beforeFirstHeading {
29 while bolp, lookingAt("^[ \\t]*#[ \\t]") != nil || lookingAt("^[ \\t]*#$") != nil { if forwardLine(1) != 0 { break } }
30 } else {
31 forwardLine(1)
32 if lookingAt(planningPattern) != nil { forwardLine(1) }
33 }
34 guard looking(Self.propertyDrawerPattern, caseInsensitive: true, setsMatch: false) == nil else { return }
35 if bolp, point > 0 { point -= 1 }
36 let begin = bobp ? point : point + 1
37 if !bobp { insert("\n") }
38 insert(":PROPERTIES:\n:END:")
39 if eobp || begin == 0 { insert("\n") }
40 }
41 }
42
43 /// `org-get-property-block` with FORCE: the body range of the entry's property drawer.
44 func propertyBlock(_ begin: Int) -> Range<Int> {
45 saveExcursion {
46 point = begin
47 if beforeFirstHeading {
48 while bolp, lookingAt("^[ \\t]*#[ \\t]") != nil || lookingAt("^[ \\t]*#$") != nil { if forwardLine(1) != 0 { break } }
49 } else {
50 forwardLine(1)
51 if lookingAt(planningPattern) != nil { forwardLine(1) }
52 }
53 if let match = looking(Self.propertyDrawerPattern) {
54 forwardLine(1)
55 return point..<lineStart(match[0]!.upperBound)
56 }
57 point = begin
58 insertPropertyDrawer()
59 let match = searchForward(Self.propertyDrawerPattern)!
60 let at = lineStart(match[0]!.upperBound)
61 return at..<at
62 }
63 }
64
65 /// `org-entry-put` for an ordinary property, with `org-property-format` "%-10s %s".
66 func entryPut(_ name: String, _ value: String) {
67 saveExcursion {
68 backToHeadingOrPointMin()
69 let range = propertyBlock(point)
70 point = range.lowerBound
71 let end = range.upperBound
72 if let match = searchForward("^[ \\t]*:" + NSRegularExpression.escapedPattern(for: name) + ":(?:[ \\t]+.*?)?[ \\t]*$", bound: end) {
73 deleteRegion(match[0]!.lowerBound, match[0]!.upperBound)
74 point = match[0]!.lowerBound
75 } else {
76 point = end
77 insert("\n")
78 point -= 1
79 }
80 insert(":" + name + ":")
81 insert(" " + value)
82 alignNodeProperty()
83 }
84 }
85
86 /// `org--align-node-property`, with the indentation `org-indent-line` gives property lines
87 /// when `org-adapt-indentation` is nil.
88 func alignNodeProperty() {
89 let start = lineStart(point)
90 let line = substring(start, lineEnd(point))
91 guard let match = line.range(of: "^([ \\t]*)(:\\S+:)(?:[ \\t]+(.*?))?[ \\t]*$", options: .regularExpression) else { return }
92 let parts = line[match]
93 let regex = try! NSRegularExpression(pattern: "^([ \\t]*)(:\\S+:)(?:[ \\t]+(.*?))?[ \\t]*$")
94 let groups = regex.firstMatch(in: String(parts), range: NSRange(location: 0, length: (String(parts) as NSString).length))!
95 let ns = String(parts) as NSString
96 let key = ns.substring(with: groups.range(at: 2))
97 let value = groups.range(at: 3).location == NSNotFound ? "" : ns.substring(with: groups.range(at: 3))
98 let padded = key.padding(toLength: max(10, (key as NSString).length), withPad: " ", startingAt: 0)
99 let new = (padded + " " + value).trimmingCharacters(in: .whitespaces)
100 let end = lineEnd(point)
101 let lineStartOffset = start
102 point = lineStartOffset
103 deleteRegion(lineStartOffset, end)
104 insert(new)
105 }
106
107 /// The entry's tags, and its inherited tags (file tags and ancestors' tags), in org's order.
108 func tags() -> (local: [String], inherited: [String]) {
109 saveExcursion {
110 guard (try? backToHeading()) != nil else { return ([], []) }
111 func lineTags(_ at: Int) -> [String] {
112 saveExcursion {
113 point = at
114 guard let match = lookingAt(SetTags.tagLine), let tags = match[2] else { return [] }
115 return substring(tags.lowerBound, tags.upperBound).split(separator: ":").map(String.init)
116 }
117 }
118 let local = lineTags(point)
119 var ancestors: [[String]] = []
120 while upHeadingSafe() != nil { ancestors.insert(lineTags(point), at: 0) }
121 var inherited: [String] = []
122 let fileTags = (keyword("FILETAGS") ?? "").split(whereSeparator: { $0 == ":" || $0 == " " || $0 == "\t" }).map(String.init)
123 for tag in fileTags + ancestors.flatMap({ $0 }) where !inherited.contains(tag) && !local.contains(tag) { inherited.append(tag) }
124 return (local, inherited)
125 }
126 }
127
128 /// `org-get-outline-path`: ancestors' titles without statistics cookies, links shown as
129 /// their descriptions.
130 func outlinePath() -> [String] {
131 saveExcursion {
132 guard (try? backToHeading()) != nil else { return [] }
133 var path: [String] = []
134 while upHeadingSafe() != nil {
135 let title = saveExcursion { () -> String in
136 guard let match = lookingAt(complexHeadingPattern), let title = match[4] else { return "" }
137 var text = substring(title.lowerBound, title.upperBound)
138 text = text.replacingOccurrences(of: "\\[[0-9]+%\\]|\\[[0-9]+/[0-9]+\\]", with: "", options: .regularExpression)
139 text = text.replacingOccurrences(of: "\\[\\[([^\\]]*)\\]\\[([^\\]]*)\\]\\]", with: "$2", options: .regularExpression)
140 text = text.replacingOccurrences(of: "\\[\\[([^\\]]*)\\]\\]", with: "$1", options: .regularExpression)
141 return text.trimmingCharacters(in: .whitespacesAndNewlines)
142 }
143 path.insert(title, at: 0)
144 }
145 return path
146 }
147 }
148
149 /// `org-update-statistics-cookies` with point on a heading.
150 func updateStatisticsCookies() {
151 guard atHeading else {
152 updateCheckboxCount()
153 return
154 }
155 saveExcursion {
156 let start = try! backToHeading()
157 let level = outlineLevel
158 var childLevel: Int?
159 let end = saveExcursion { () -> Int in
160 if outlineNextHeading() { childLevel = outlineLevel }
161 return point
162 }
163 let hasBoxes = saveExcursion { searchForward("^[ \\t]*([-+*]|[0-9]+[.)]) \\[[- X]\\]", bound: end) != nil }
164 let todoData = saveExcursion { searchForward(":COOKIE_DATA:.*\\btodo\\b", bound: end) != nil }
165 if hasBoxes, !todoData {
166 updateCheckboxCount()
167 } else if let childLevel, childLevel > level {
168 point = end
169 updateParentTodoStatistics()
170 } else {
171 point = start
172 while let match = searchForward("((\\[[0-9]*%\\])|(\\[[0-9]*/[0-9]*\\]))", bound: lineEnd(point)) {
173 replace(match[1]!, with: match[2] != nil ? "[100%]" : "[0/0]")
174 }
175 }
176 }
177 }
178}
179
180public enum Archive {
181 /// Where the entry at the caret goes: the archive file (the source itself for an in-file
182 /// location) and the heading to file it under, if any.
183 public struct Destination: Sendable, Equatable {
184 public let path: String
185 public let heading: String?
186 public var isSameFile: Bool
187 }
188
189 public static func destination(_ context: EditContext, sourcePath: String) -> Result<Destination, ArchiveError> {
190 let buffer = EmacsBuffer(context.text, point: context.caret, settings: context.tree.settings)
191 guard !buffer.beforeFirstHeading else { return .failure(.message("Before first headline")) }
192 let spec = buffer.inheritedProperty("ARCHIVE")?.value ?? buffer.keyword("ARCHIVE") ?? "%s_archive::"
193 guard let separator = spec.range(of: "::") else { return .failure(.message("Invalid archive location: \"\(spec)\"")) }
194 let name = (sourcePath as NSString).lastPathComponent
195 let fileFormat = String(spec[..<separator.lowerBound])
196 let headingFormat = String(spec[separator.upperBound...])
197 let path: String
198 if fileFormat.contains(where: { !$0.isWhitespace }) {
199 let file = fileFormat.replacingOccurrences(of: "%s", with: name)
200 let directory = (sourcePath as NSString).deletingLastPathComponent
201 path = ((file as NSString).isAbsolutePath ? file : (directory as NSString).appendingPathComponent(file)).replacingOccurrences(of: "~", with: NSHomeDirectory(), options: .anchored)
202 } else {
203 path = sourcePath
204 }
205 let heading = headingFormat.replacingOccurrences(of: "%s", with: name)
206 return .success(Destination(path: (path as NSString).standardizingPath, heading: heading.isEmpty ? nil : heading, isSameFile: path == sourcePath))
207 }
208
209 public enum ArchiveError: Error, Equatable {
210 case message(String)
211 }
212
213 public struct Edits: Sendable, Equatable {
214 /// For the source; for an in-file archive, everything happens here.
215 public let source: EditResult
216 /// For the archive file, against its text (empty when it doesn't exist yet).
217 public let archive: TextEdit?
218 }
219
220 /// `~/` for paths in the home folder, as `abbreviate-file-name`.
221 static func abbreviate(_ path: String) -> String {
222 let home = NSHomeDirectory()
223 return path.hasPrefix(home + "/") ? "~" + path.dropFirst(home.count) : path
224 }
225
226 /// Archives the subtree at the caret. `archiveText` is the archive file's text, or nil
227 /// when it doesn't exist; it's ignored for an in-file destination.
228 public static func run(
229 _ context: EditContext, sourcePath: String, destination: Destination, archiveText: String?
230 ) -> Result<Edits, ArchiveError> {
231 let formatter = DateFormatter()
232 formatter.locale = Locale(identifier: "en_US_POSIX")
233 formatter.calendar = context.calendar
234 formatter.timeZone = context.calendar.timeZone
235 formatter.dateFormat = "yyyy-MM-dd EEE HH:mm"
236 let time = formatter.string(from: context.now)
237 let archive = destination.isSameFile ? nil : EmacsBuffer(archiveText ?? "", point: 0, settings: context.tree.settings, options: context.options)
238 var archiveEdit: TextEdit?
239 let step = commitBuffer(context) { source in
240 let target = archive ?? source
241 let level: Int
242 var headingText: String?
243 var heading = destination.heading
244 // `datetree/`: three levels of date headings, stars after it a heading below them.
245 var datetree: DateComponents?
246 var datetreeSubheading = false
247 if let h = heading, let m = h.firstMatch(of: /^datetree\/(\**)/) {
248 let nsub = m.1.count
249 heading = String(repeating: "*", count: 3 + nsub) + h[m.range.upperBound...]
250 datetreeSubheading = nsub > 0
251 let closed = source.saveExcursion { () -> String? in
252 _ = try? source.backToHeading()
253 source.forwardLine(1)
254 guard let line = source.lookingAt("^[ \\t]*(?:SCHEDULED|DEADLINE|CLOSED):.*$") else { return nil }
255 return source.substring(line[0]!.lowerBound, line[0]!.upperBound).firstMatch(of: /CLOSED: *\[([^\]]+)\]/).map { String($0.1) }
256 }
257 datetree = EmacsBuffer.parseTimeString(closed ?? time)
258 }
259 if let heading, let stars = heading.range(of: "^\\*+", options: .regularExpression) {
260 level = heading[stars].count
261 headingText = heading
262 } else {
263 level = 0
264 headingText = nil
265 }
266 let origin = source.marker(source.point)
267 defer { source.release(origin) }
268 try source.backToHeading()
269 let (local, inherited) = source.tags()
270 let category = source.inheritedProperty("CATEGORY")?.value ?? source.keyword("CATEGORY")
271 ?? ((sourcePath as NSString).lastPathComponent as NSString).deletingPathExtension
272 let todo = source.lookingAt(source.todoLinePattern)?[2].map { source.substring($0.lowerBound, $0.upperBound) }
273 let context: [(String, String)] = [
274 ("TIME", time), ("FILE", abbreviate(sourcePath)), ("OLPATH", source.outlinePath().joined(separator: "/")),
275 ("CATEGORY", category), ("TODO", todo ?? ""), ("ITAGS", inherited.joined(separator: " ")),
276 ]
277 let tree = try source.copySubtree()
278 try target.saveExcursion {
279 if !destination.isSameFile, archiveText == nil {
280 target.point = target.length
281 target.insert("\nArchived entries from file \(sourcePath)\n\n")
282 }
283 // The date's entry, as `org-datetree-find-date-create` then `org-narrow-to-subtree`.
284 let low = target.marker(0)
285 let high = target.marker(target.length, advances: true)
286 defer {
287 target.release(low)
288 target.release(high)
289 }
290 if let date = datetree {
291 let day = Datetree.findCreate(target, under: nil, tree: .day, date: date)
292 low.position = day
293 high.position = Datetree.narrowEnd(target, day, limit: target.length)
294 }
295 target.point = low.position
296 if let headingText, !(datetree != nil && !datetreeSubheading) {
297 let pattern = "^" + NSRegularExpression.escapedPattern(for: headingText) + "([ \\t]+:([[:alnum:]_@#%]+:)+)?[ \\t]*$"
298 if let match = target.searchForward(pattern, bound: high.position) {
299 target.point = match[0]!.upperBound
300 } else {
301 target.point = high.position
302 if !target.bolp { target.insert("\n") }
303 target.insert((datetree != nil ? "" : "\n") + headingText + "\n")
304 target.forwardLine(-1)
305 target.endOfLine()
306 }
307 target.endOfSubtree()
308 target.point = min(target.point, high.position)
309 target.skipBackward(EmacsBuffer.whitespace)
310 target.point = max(target.point, low.position)
311 let blanks = target.looking("[ \\t\\r\\n]*")!
312 let clipped = blanks[0]!.lowerBound..<min(blanks[0]!.upperBound, high.position)
313 let separator = datetree != nil ? "\n" : "\n\n"
314 target.replace(clipped, with: separator)
315 target.point = clipped.lowerBound + separator.count
316 } else {
317 target.point = high.position
318 if !(datetree != nil && target.bolp) { target.insert("\n") }
319 }
320 try target.pasteSubtree(level: max(1, level + (headingText == nil ? 0 : 1)), tree: tree, limit: datetree != nil ? high.position : nil)
321 if !inherited.isEmpty, destination.isSameFile {
322 let all = inherited + local
323 target.saveExcursion {
324 let line = target.saveExcursion { target.lookingAt(SetTags.tagLine) }
325 if let tags = line?[1] {
326 target.point = tags.lowerBound
327 target.skipBackward(EmacsBuffer.blanks)
328 target.deleteRegion(target.point, target.lineEnd(target.point))
329 } else {
330 target.endOfLine()
331 }
332 target.saveExcursion { target.insert(" :" + all.joined(separator: ":") + ":") }
333 }
334 target.alignTags(onLineAt: target.point)
335 }
336 for (key, value) in context where value.contains(where: { !$0.isWhitespace }) {
337 target.entryPut("ARCHIVE_" + key, value)
338 }
339 }
340 source.point = origin.position
341 let range = try source.subtreeRange()
342 source.deleteRegion(range.lowerBound, range.upperBound)
343 source.saveExcursion {
344 source.upHeadingSafe()
345 source.updateStatisticsCookies()
346 }
347 if source.looking("^[ \\t]*$", setsMatch: false) != nil {
348 source.endOfLine()
349 source.point = source.nextHeadingStart(after: source.point) ?? source.length
350 }
351 }
352 if let archive {
353 let original = archiveText ?? ""
354 if case .commit(let result) = commitBuffer(EditContext(revision: 0, text: original, tree: OrgParser.parse(original), selection: [0..<0]), { $0.replace(0..<$0.length, with: archive.string) }) {
355 archiveEdit = result.edits.first
356 }
357 }
358 switch step {
359 case .commit(let result): return .success(Edits(source: result, archive: archiveEdit))
360 case .failed(let message): return .failure(.message(message))
361 case .prompt: return .failure(.message("unexpected prompt"))
362 case .external: return .failure(.message("unexpected external request"))
363 }
364 }
365}