Commit 524b5afe95
Verified · cmc
Layout: unified · split
Sources/OrgApp/ClockModel.swift +37
| @@ -53,6 +53,8 @@ public final class ClockModel { | |||
| 53 | /// The clocked entry's heading line as last seen, and its keyword, for | 53 | /// The clocked entry's heading line as last seen, and its keyword, for |
| 54 | /// `org-clock-out-when-done`. | 54 | /// `org-clock-out-when-done`. |
| 55 | @ObservationIgnored private var watched: (line: String, keyword: String?)? | 55 | @ObservationIgnored private var watched: (line: String, keyword: String?)? |
| 56 | /// The open clocks `sync` has seen, by path and start: only new ones start running. | ||
| 57 | @ObservationIgnored private var seenOpen: Set<String> = [] | ||
| 56 | private let file: URL | 58 | private let file: URL |
| 57 | private let defaults: UserDefaults | 59 | private let defaults: UserDefaults |
| 58 | 60 | ||
| @@ -471,6 +473,41 @@ public final class ClockModel { | |||
| 471 | return DocumentModel(tree: OrgParser.parse(text)).headings.last { $0.headingRange.lowerBound <= location.offset }?.headingRange.lowerBound | 473 | return DocumentModel(tree: OrgParser.parse(text)).headings.last { $0.headingRange.lowerBound <= location.offset }?.headingRange.lowerBound |
| 472 | } | 474 | } |
| 473 | 475 | ||
| 476 | // MARK: - Clocks started and stopped elsewhere | ||
| 477 | |||
| 478 | /// Brings the running clock in line with the open `CLOCK:` lines of `paths`, so that a | ||
| 479 | /// clock started or stopped in Emacs, or on another device, is the one orgstar knows: the | ||
| 480 | /// running clock ends when its line was closed or removed, and with none running, the most | ||
| 481 | /// recently started of the open clocks not seen before becomes the running one. Others stay | ||
| 482 | /// dangling, for `org-resolve-clocks` at the next clock in. Returns a message when | ||
| 483 | /// something changed. | ||
| 484 | @discardableResult | ||
| 485 | public func sync(_ session: DocumentSession, paths: [String]) -> String? { | ||
| 486 | guard !isResolving else { return nil } | ||
| 487 | let open = openClocks(session, paths: paths) | ||
| 488 | let fresh = open.filter { !seenOpen.contains($0.path + "\u{0}" + $0.start) } | ||
| 489 | seenOpen.formUnion(open.map { $0.path + "\u{0}" + $0.start }) | ||
| 490 | if let running { | ||
| 491 | guard paths.contains(running.path) || session.openDocuments.contains(where: { $0.path == running.path }) else { return nil } | ||
| 492 | // Its line was closed or removed elsewhere; a file that can't be read says nothing. | ||
| 493 | guard !open.contains(where: isActive), Self.text(running.path, session) != nil else { return nil } | ||
| 494 | notifications?.remove([Self.effortReminderID]) | ||
| 495 | self.running = nil | ||
| 496 | watched = nil | ||
| 497 | save() | ||
| 498 | return "The clock in \"\(running.heading)\" was stopped elsewhere" | ||
| 499 | } | ||
| 500 | guard let newest = fresh.max(by: { $0.startedAt < $1.startedAt }) else { return nil } | ||
| 501 | running = Running(path: newest.path, start: newest.start, heading: newest.heading, startedAt: newest.startedAt) | ||
| 502 | if let text = Self.text(newest.path, session), case let offset = (text as NSString).range(of: "CLOCK: [\(newest.start)]").location, offset != NSNotFound { | ||
| 503 | let heading = DocumentModel(tree: OrgParser.parse(text)).headings.last { $0.headingRange.lowerBound <= offset }?.headingRange.lowerBound | ||
| 504 | if let heading, let task = ClockTask.at(heading, in: text, path: newest.path) { push(task, session) } | ||
| 505 | } | ||
| 506 | save() | ||
| 507 | watched = clockedHeading(newest.path, session) | ||
| 508 | return "Clock running in \"\(newest.heading)\" since [\(newest.start)]" | ||
| 509 | } | ||
| 510 | |||
| 474 | // MARK: - Resolving clocks | 511 | // MARK: - Resolving clocks |
| 475 | 512 | ||
| 476 | /// `org-find-open-clocks` over `paths` and the open org buffers. | 513 | /// `org-find-open-clocks` over `paths` and the open org buffers. |
Sources/Orgstar/ContentView.swift +16 −3
| @@ -371,13 +371,26 @@ struct ContentView: View { | |||
| 371 | commands.showingConflict = false | 371 | commands.showingConflict = false |
| 372 | if session.conflict != nil { panel = .conflict(UUID()) } | 372 | if session.conflict != nil { panel = .conflict(UUID()) } |
| 373 | } | 373 | } |
| 374 | .onChange(of: workspace.changedFile?.version) { | 374 | .onChange(of: workspace.changedFile?.version) { fileChangedOnDisk() } |
| 375 | if let path = workspace.changedFile?.path { session.fileChanged(path) } | 375 | } |
| 376 | } | 376 | |
| 377 | /// An open file takes in what changed on disk, and a clock started or stopped in Emacs | ||
| 378 | /// becomes the one shown. | ||
| 379 | private func fileChangedOnDisk() { | ||
| 380 | guard let path = workspace.changedFile?.path else { return } | ||
| 381 | session.fileChanged(path) | ||
| 382 | if path.hasSuffix(".org") { syncClock(paths: [path]) } | ||
| 383 | } | ||
| 384 | |||
| 385 | /// Open clocks in `paths`: at launch and as folders load, the agenda files. | ||
| 386 | private func syncClock(paths: [String]) { | ||
| 387 | guard !workspace.files.isEmpty, let message = clock.sync(session, paths: paths) else { return } | ||
| 388 | commands.show(message) | ||
| 377 | } | 389 | } |
| 378 | 390 | ||
| 379 | var body: some View { | 391 | var body: some View { |
| 380 | main | 392 | main |
| 393 | .task(id: workspace.files.count) { syncClock(paths: workspace.agendaFilePaths()) } | ||
| 381 | .onChange(of: ClockMenuState(running: clock.running, history: clock.history), initial: true) { _, state in | 394 | .onChange(of: ClockMenuState(running: clock.running, history: clock.history), initial: true) { _, state in |
| 382 | if statusItem == nil { statusItem = ClockStatusItem { commands.run($0) } } | 395 | if statusItem == nil { statusItem = ClockStatusItem { commands.run($0) } } |
| 383 | statusItem?.update(state.running, recent: state.history.map(\.title)) | 396 | statusItem?.update(state.running, recent: state.history.map(\.title)) |
Sources/OrgstarMobile/OrgstarMobileApp.swift +10 −1
| @@ -211,7 +211,16 @@ struct RootView: View { | |||
| 211 | } | 211 | } |
| 212 | .onChange(of: workspace.changedFile?.version) { | 212 | .onChange(of: workspace.changedFile?.version) { |
| 213 | // An open file takes in what changed on disk. | 213 | // An open file takes in what changed on disk. |
| 214 | if let path = workspace.changedFile?.path { session.fileChanged(path) } | 214 | if let path = workspace.changedFile?.path { |
| 215 | session.fileChanged(path) | ||
| 216 | // A clock started or stopped in Emacs or on another device. | ||
| 217 | if path.hasSuffix(".org"), let text = model.clock.sync(session, paths: [path]) { message = text } | ||
| 218 | } | ||
| 219 | } | ||
| 220 | // At launch and as folders load: open clocks in the agenda files. | ||
| 221 | .task(id: workspace.files.count) { | ||
| 222 | guard !workspace.files.isEmpty, let text = model.clock.sync(session, paths: workspace.agendaFilePaths()) else { return } | ||
| 223 | message = text | ||
| 215 | } | 224 | } |
| 216 | // Initially too: the capture control may have launched the app. | 225 | // Initially too: the capture control may have launched the app. |
| 217 | .onChange(of: model.captureRequest?.id, initial: true) { | 226 | .onChange(of: model.captureRequest?.id, initial: true) { |
Tests/OrgAppTests/AppTests.swift +25
| @@ -1119,6 +1119,31 @@ struct CaptureAppTests { | |||
| 1119 | 1119 | ||
| 1120 | @MainActor | 1120 | @MainActor |
| 1121 | struct ClockModelTests { | 1121 | struct ClockModelTests { |
| 1122 | /// A clock started in Emacs becomes the running one; stopped there, it ends here. | ||
| 1123 | @Test func followsClocksStartedElsewhere() throws { | ||
| 1124 | let notes = try Folder() | ||
| 1125 | let work = try notes.write("work.org", "* Task\n:LOGBOOK:\nCLOCK: [2026-10-05 Mon 09:00]\n:END:\n* Older\nCLOCK: [2026-10-04 Sun 09:00]\n") | ||
| 1126 | let session = DocumentSession(recovery: FileRecoveryStore(directory: notes.url.appendingPathComponent(".r"))) | ||
| 1127 | let clock = ClockModel(file: notes.url.appendingPathComponent("clock.json")) | ||
| 1128 | #expect(clock.sync(session, paths: [work.path]) == "Clock running in \"Task\" since [2026-10-05 Mon 09:00]") | ||
| 1129 | #expect(clock.running?.path == work.path && clock.running?.start == "2026-10-05 Mon 09:00") | ||
| 1130 | #expect(clock.history.first?.title == "Task") | ||
| 1131 | #expect(clock.sync(session, paths: [work.path]) == nil) | ||
| 1132 | // Emacs clocks out. | ||
| 1133 | try "* Task\n:LOGBOOK:\nCLOCK: [2026-10-05 Mon 09:00]--[2026-10-05 Mon 10:00] => 1:00\n:END:\n* Older\nCLOCK: [2026-10-04 Sun 09:00]\n" | ||
| 1134 | .write(to: work, atomically: true, encoding: .utf8) | ||
| 1135 | #expect(clock.sync(session, paths: [work.path]) == "The clock in \"Task\" was stopped elsewhere") | ||
| 1136 | #expect(clock.running == nil) | ||
| 1137 | // The dangling one stays for resolution; a clock Emacs starts later runs. | ||
| 1138 | #expect(clock.sync(session, paths: [work.path]) == nil) | ||
| 1139 | try "* Task\n:LOGBOOK:\nCLOCK: [2026-10-05 Mon 11:00]\nCLOCK: [2026-10-05 Mon 09:00]--[2026-10-05 Mon 10:00] => 1:00\n:END:\n* Older\nCLOCK: [2026-10-04 Sun 09:00]\n" | ||
| 1140 | .write(to: work, atomically: true, encoding: .utf8) | ||
| 1141 | #expect(clock.sync(session, paths: [work.path]) == "Clock running in \"Task\" since [2026-10-05 Mon 11:00]") | ||
| 1142 | // Another file's change says nothing about the running clock. | ||
| 1143 | #expect(clock.sync(session, paths: [notes.url.appendingPathComponent("other.org").path]) == nil) | ||
| 1144 | #expect(clock.running?.start == "2026-10-05 Mon 11:00") | ||
| 1145 | } | ||
| 1146 | |||
| 1122 | @Test func clocksInAndOutAcrossFiles() throws { | 1147 | @Test func clocksInAndOutAcrossFiles() throws { |
| 1123 | let notes = try Folder() | 1148 | let notes = try Folder() |
| 1124 | let work = try notes.write("work.org", "* Task\nBody\n") | 1149 | let work = try notes.write("work.org", "* Task\nBody\n") |
docs/manual/guide/06-dates-and-clocking.org +8 −1
| @@ -251,9 +251,16 @@ With no clock running, Clock In first asks about open clocks; see Open clocks be | |||
| 251 | 251 | ||
| 252 | orgstar remembers the running clock and the clock history across restarts, in =clock.json= in =~/Library/Application Support/Orgstar=. If the open =CLOCK:= line is deleted from the file, Clock Out reports "Clock start time is gone" and forgets the clock. | 252 | orgstar remembers the running clock and the clock history across restarts, in =clock.json= in =~/Library/Application Support/Orgstar=. If the open =CLOCK:= line is deleted from the file, Clock Out reports "Clock start time is gone" and forgets the clock. |
| 253 | 253 | ||
| 254 | orgstar follows the open =CLOCK:= lines in the files, so a clock started or stopped in Emacs, or on another device, is the one it shows: | ||
| 255 | |||
| 256 | - At launch, and as your folders load, it looks for open clock lines in the agenda files; when a file changes on disk, in that file. | ||
| 257 | - With no clock running, the most recently started open clock it hasn't seen before becomes the running clock, and the status line says so. Older open clocks stay dangling; Clock In asks about them, as described in Open clocks below. | ||
| 258 | - When the running clock's line is closed (Emacs clocked out) or removed, the clock stops here too, with the message "The clock in … was stopped elsewhere". | ||
| 259 | |||
| 260 | Clocking out in orgstar closes the line as Org does, so Emacs sees a finished clock when it reverts the file. orgstar does not read Emacs's =org-clock-persist= file, so Emacs learns of a clock started here only from the line, when it next resolves clocks. | ||
| 261 | |||
| 254 | These parts of Org's clocking are not implemented: | 262 | These parts of Org's clocking are not implemented: |
| 255 | 263 | ||
| 256 | - sharing the running clock with Emacs: orgstar does not read or write Emacs's =org-clock-persist= file, so a clock started in one is not known to the other, although both see the open =CLOCK:= line; | ||
| 257 | - =org-clock-in-switch-to-state= and =org-clock-out-switch-to-state= as functions, and =org-clock-sound=: the notification uses the system's sound. | 264 | - =org-clock-in-switch-to-state= and =org-clock-out-switch-to-state= as functions, and =org-clock-sound=: the notification uses the system's sound. |
| 258 | 265 | ||
| 259 | ** The clock drawer | 266 | ** The clock drawer |