Commit a6d26284f0
Verified · cmc
Layout: unified · split
Sources/OrgCore/Commands/ClockTable.swift +2 −1
| @@ -675,9 +675,10 @@ public struct ShowColumnView: OrgCommand { | ||
| 675 | 675 | public var title: String { global ? "Column View of File" : "Column View" } |
| 676 | 676 | public func applies(in context: EditContext) -> Bool { true } |
| 677 | 677 | public func run(in context: EditContext) -> CommandStep { |
| 678 | let view: ColumnView.View | |
| 678 | var view: ColumnView.View | |
| 679 | 679 | do { |
| 680 | 680 | view = try ColumnView.capture(text: context.text, tree: context.tree, at: context.caret, global: global, now: context.now, calendar: context.calendar).view |
| 681 | view.global = global | |
| 681 | 682 | } catch let error as EmacsBuffer.UserError { |
| 682 | 683 | return .failed(error.message) |
| 683 | 684 | } catch { |
Sources/OrgCore/Commands/ColumnEdit.swift added +121
| @@ -0,0 +1,121 @@ | ||
| 1 | import Foundation | |
| 2 | ||
| 3 | // Editing in column view (org-colview.el, Org 9.8.7): `org-columns-edit-value` and | |
| 4 | // `org-columns-next-allowed-value` for the entry whose heading holds the caret. | |
| 5 | ||
| 6 | extension EditContext { | |
| 7 | /// The same context with the caret at `offset`. | |
| 8 | func at(_ offset: Int) -> EditContext { | |
| 9 | EditContext(revision: revision, text: text, tree: tree, selection: [offset..<offset], now: now, calendar: calendar, | |
| 10 | answers: answers, hidden: hidden, options: options, knownTags: knownTags, lastCommand: lastCommand) | |
| 11 | } | |
| 12 | ||
| 13 | /// The same context with `more` answers. | |
| 14 | func answering(_ more: [String: String]) -> EditContext { | |
| 15 | EditContext(revision: revision, text: text, tree: tree, selection: selection, now: now, calendar: calendar, | |
| 16 | answers: answers.merging(more) { $1 }, hidden: hidden, options: options, knownTags: knownTags, lastCommand: lastCommand) | |
| 17 | } | |
| 18 | } | |
| 19 | ||
| 20 | /// `e` in column view: `org-columns-edit-value` with the value given. ITEM edits the headline, | |
| 21 | /// TODO, PRIORITY, TAGS, SCHEDULED and DEADLINE go through their commands, and other | |
| 22 | /// properties are set in the entry's drawer. | |
| 23 | public struct SetColumnValue: OrgCommand { | |
| 24 | public let property: String | |
| 25 | public let value: String | |
| 26 | public init(property: String, value: String) { | |
| 27 | self.property = property | |
| 28 | self.value = value | |
| 29 | } | |
| 30 | public var id: String { "org.columns.edit-value" } | |
| 31 | public var title: String { "Edit Column Value" } | |
| 32 | public func applies(in context: EditContext) -> Bool { entryHeading(at: context.caret, in: context.tree) != nil } | |
| 33 | ||
| 34 | public func run(in context: EditContext) -> CommandStep { | |
| 35 | guard let heading = entryHeading(at: context.caret, in: context.tree) else { return .failed("Before first headline") } | |
| 36 | let value = self.value.trimmingCharacters(in: .whitespaces) | |
| 37 | let here = context.at(heading.range.lowerBound) | |
| 38 | switch property.uppercased() { | |
| 39 | case "CLOCKSUM", "CLOCKSUM_T", "ALLTAGS", "BLOCKED", "CLOSED", "FILE", "TIMESTAMP", "TIMESTAMP_IA": | |
| 40 | return .failed("This special column cannot be edited") | |
| 41 | case "ITEM": | |
| 42 | // `org-edit-headline`. | |
| 43 | return commitBuffer(here) { buffer in | |
| 44 | guard let match = buffer.lookingAt(buffer.complexHeadingPattern) else { return } | |
| 45 | let old = match[4].map { buffer.substring($0.lowerBound, $0.upperBound) } | |
| 46 | guard old != value else { return } | |
| 47 | if let title = match[4] { | |
| 48 | buffer.replace(title.lowerBound..<title.upperBound, with: value) | |
| 49 | } else { | |
| 50 | buffer.point = (match[3] ?? match[2] ?? match[1])!.upperBound | |
| 51 | buffer.insert(" " + value) | |
| 52 | } | |
| 53 | buffer.alignTags(onLineAt: heading.range.lowerBound) | |
| 54 | } | |
| 55 | case "TODO": | |
| 56 | return SetTodo(keyword: value.isEmpty ? nil : value).run(in: here) | |
| 57 | case "PRIORITY": | |
| 58 | return SetPriority(value: value.isEmpty ? " " : nil).run(in: value.isEmpty ? here : here.answering(["priority": value])) | |
| 59 | case "TAGS": | |
| 60 | let tags = value.split(whereSeparator: { $0 == ":" || $0 == " " }).map(String.init) | |
| 61 | return SetTags.set(tags, in: here) | |
| 62 | case "SCHEDULED", "DEADLINE": | |
| 63 | let deadline = property.uppercased() == "DEADLINE" | |
| 64 | if value.isEmpty { return RemovePlanning(deadline: deadline).run(in: here) } | |
| 65 | return SetPlanning(deadline: deadline).run(in: here.answering(["date": value])) | |
| 66 | default: | |
| 67 | // Column keys are upper case, as `org-columns-compile-format` reads them, and so is | |
| 68 | // the line `org-entry-put` writes. | |
| 69 | return SetProperty(key: property.uppercased(), value: value).run(in: here) | |
| 70 | } | |
| 71 | } | |
| 72 | } | |
| 73 | ||
| 74 | /// `n` and `p` in column view: `org-columns-next-allowed-value`, the next (or previous) of the | |
| 75 | /// property's allowed values, `[ ]` and `[X]` for a checkbox summary, or a day later (or | |
| 76 | /// earlier) for SCHEDULED and DEADLINE. | |
| 77 | public struct NextColumnValue: OrgCommand { | |
| 78 | public let property: String | |
| 79 | /// The column's summary operator, for checkbox columns. | |
| 80 | public let summary: String? | |
| 81 | public let previous: Bool | |
| 82 | public init(property: String, summary: String? = nil, previous: Bool = false) { | |
| 83 | self.property = property | |
| 84 | self.summary = summary | |
| 85 | self.previous = previous | |
| 86 | } | |
| 87 | public var id: String { previous ? "org.columns.previous-value" : "org.columns.next-value" } | |
| 88 | public var title: String { previous ? "Previous Allowed Value" : "Next Allowed Value" } | |
| 89 | public func applies(in context: EditContext) -> Bool { entryHeading(at: context.caret, in: context.tree) != nil } | |
| 90 | ||
| 91 | public func run(in context: EditContext) -> CommandStep { | |
| 92 | guard let heading = entryHeading(at: context.caret, in: context.tree) else { return .failed("Before first headline") } | |
| 93 | let key = property.uppercased() | |
| 94 | if key == "ITEM" { return .failed("Cannot edit item headline from here") } | |
| 95 | if key == "SCHEDULED" || key == "DEADLINE" { | |
| 96 | // The planning timestamp a day later or earlier. | |
| 97 | let ns = context.text as NSString | |
| 98 | let next = NSMaxRange(ns.lineRange(for: NSRange(location: heading.range.lowerBound, length: 0))) | |
| 99 | guard next < ns.length else { return .failed("No \(key) in this entry") } | |
| 100 | let line = ns.lineRange(for: NSRange(location: next, length: 0)) | |
| 101 | let found = ns.range(of: key + ": *<", options: .regularExpression, range: line) | |
| 102 | guard found.location != NSNotFound else { return .failed("No \(key) in this entry") } | |
| 103 | return ShiftTimestampDay(later: !previous).run(in: context.at(NSMaxRange(found))) | |
| 104 | } | |
| 105 | var allowed = PropertyValues.allowed(property, at: heading.range.lowerBound, text: context.text, tree: context.tree)?.values ?? [] | |
| 106 | if allowed.isEmpty, let summary, ["X", "X/", "X%"].contains(summary) { allowed = ["[ ]", "[X]"] } | |
| 107 | guard !allowed.isEmpty else { return .failed("Allowed values for this property have not been defined") } | |
| 108 | if previous { allowed.reverse() } | |
| 109 | let model = DocumentModel(tree: context.tree) | |
| 110 | guard let index = model.headings.firstIndex(where: { $0.headingRange.lowerBound == heading.range.lowerBound }) else { return .failed("Before first headline") } | |
| 111 | let current = ColumnView.value(key, heading: index, model: model, text: context.text as NSString, clocksum: [:]) | |
| 112 | let new: String | |
| 113 | if let i = allowed.firstIndex(of: current) { | |
| 114 | if allowed.count == 1 { return .failed("Only one allowed value for this property") } | |
| 115 | new = i + 1 < allowed.count ? allowed[i + 1] : allowed[0] | |
| 116 | } else { | |
| 117 | new = allowed[0] | |
| 118 | } | |
| 119 | return SetColumnValue(property: property, value: new).run(in: context) | |
| 120 | } | |
| 121 | } | |
Sources/OrgCore/Commands/ColumnView.swift +15 −4
| @@ -18,11 +18,22 @@ public enum ColumnView { | ||
| 18 | 18 | /// Start of the heading line. |
| 19 | 19 | public let start: Int |
| 20 | 20 | public let values: [String] |
| 21 | /// The values before formatting, as editing starts from them; summaries included. | |
| 22 | public var raw: [String] = [] | |
| 21 | 23 | } |
| 22 | 24 | |
| 23 | 25 | public struct View: Sendable, Equatable { |
| 24 | 26 | public let specs: [Spec] |
| 25 | 27 | public let rows: [Row] |
| 28 | /// The whole file, as `org-columns` with a prefix shows it. | |
| 29 | public var global = false | |
| 30 | } | |
| 31 | ||
| 32 | /// The view captured again from `text` after an edit, over the same entries. | |
| 33 | public static func refresh(_ view: View, text: String, tree: OrgTree, now: Date = Date(), calendar: Calendar = .current) throws -> View { | |
| 34 | var new = try capture(text: text, tree: tree, at: view.global ? 0 : view.rows.first?.start ?? 0, global: view.global, now: now, calendar: calendar).view | |
| 35 | new.global = view.global | |
| 36 | return new | |
| 26 | 37 | } |
| 27 | 38 | |
| 28 | 39 | public static let defaultFormat = "%25ITEM %TODO %3PRIORITY %TAGS" |
| @@ -329,10 +340,10 @@ public enum ColumnView { | ||
| 329 | 340 | for i in scope { |
| 330 | 341 | let level = headings[i].level |
| 331 | 342 | if let maxLevel, level > maxLevel { continue } |
| 332 | let values = specs.enumerated().map { column, spec -> String in | |
| 333 | let raw = summaries[i]?[column] ?? value(spec.property, heading: i, model: model, text: ns, clocksum: clocksum) | |
| 334 | return displayed(spec, raw) | |
| 343 | let raw = specs.enumerated().map { column, spec -> String in | |
| 344 | summaries[i]?[column] ?? value(spec.property, heading: i, model: model, text: ns, clocksum: clocksum) | |
| 335 | 345 | } |
| 346 | let values = zip(specs, raw).map { displayed($0.0, $0.1) } | |
| 336 | 347 | if skipEmpty { |
| 337 | 348 | let others = Set(zip(specs, values).filter { $0.0.property != "ITEM" }.map(\.1)).subtracting([""]) |
| 338 | 349 | if others.isEmpty { continue } |
| @@ -341,7 +352,7 @@ public enum ColumnView { | ||
| 341 | 352 | let tags = model.tags(of: i).map(\.value) |
| 342 | 353 | if tags.contains(where: excludeTags.contains) { continue } |
| 343 | 354 | } |
| 344 | rows.append(Row(level: level, start: headings[i].headingRange.lowerBound, values: values)) | |
| 355 | rows.append(Row(level: level, start: headings[i].headingRange.lowerBound, values: values, raw: raw)) | |
| 345 | 356 | } |
| 346 | 357 | return (View(specs: specs, rows: rows), updates) |
| 347 | 358 | } |
Sources/Orgstar/ColumnViewSheet.swift +167 −19
| @@ -1,18 +1,37 @@ | ||
| 1 | 1 | import OrgCore |
| 2 | 2 | import SwiftUI |
| 3 | 3 | |
| 4 | /// `org-columns`, read-only: one row per entry, one column per column-format field, with | |
| 5 | /// summaries. Clicking a row goes to its heading. | |
| 4 | /// `org-columns`: one row per entry, one column per column-format field, with summaries. | |
| 5 | /// Click a cell to select it; Return or a double-click edits it (`e`), `n` and `p` move to the | |
| 6 | /// next or previous allowed value, and Go to Entry shows its heading. | |
| 6 | 7 | struct ColumnViewSheet: View { |
| 7 | let view: ColumnView.View | |
| 8 | @State var view: ColumnView.View | |
| 9 | /// Runs a command at a heading's start; the step tells what came of it. | |
| 10 | let run: (any OrgCommand, Int, [String: String]) -> CommandStep | |
| 11 | /// The document's text and tree now, to capture the view again and offer allowed values. | |
| 12 | let document: () -> (text: String, tree: OrgTree)? | |
| 8 | 13 | let jump: (Int) -> Void |
| 9 | 14 | @Environment(\.dismiss) private var dismiss |
| 15 | @State private var selected: (row: Int, column: Int)? | |
| 16 | @State private var editing: (row: Int, column: Int)? | |
| 17 | @State private var draft = "" | |
| 18 | @State private var message: String? | |
| 19 | /// A question the edit asks (a state note), with the command to run again. | |
| 20 | @State private var pending: (prompt: Prompt, command: any OrgCommand, start: Int, answers: [String: String])? | |
| 21 | @State private var reply = "" | |
| 22 | @FocusState private var focused: Bool | |
| 10 | 23 | |
| 11 | 24 | var body: some View { |
| 12 | 25 | VStack(alignment: .leading, spacing: 0) { |
| 13 | 26 | HStack { |
| 14 | 27 | Text("Column View").font(.headline) |
| 15 | 28 | Spacer() |
| 29 | Button("Go to Entry") { | |
| 30 | guard let selected else { return } | |
| 31 | jump(view.rows[selected.row].start) | |
| 32 | dismiss() | |
| 33 | } | |
| 34 | .disabled(selected == nil) | |
| 16 | 35 | Button("Done") { dismiss() }.keyboardShortcut(.cancelAction) |
| 17 | 36 | } |
| 18 | 37 | .padding(10) |
| @@ -26,23 +45,8 @@ struct ColumnViewSheet: View { | ||
| 26 | 45 | } |
| 27 | 46 | Divider() |
| 28 | 47 | ForEach(view.rows.indices, id: \.self) { r in |
| 29 | let row = view.rows[r] | |
| 30 | 48 | GridRow { |
| 31 | ForEach(view.specs.indices, id: \.self) { i in | |
| 32 | Text(cell(row, i)) | |
| 33 | .lineLimit(1) | |
| 34 | .frame(maxWidth: view.specs[i].width.map { CGFloat($0) * 8.5 }, alignment: .leading) | |
| 35 | } | |
| 36 | } | |
| 37 | .contentShape(Rectangle()) | |
| 38 | .accessibilityAddTraits(.isButton) | |
| 39 | .accessibilityAction { | |
| 40 | jump(row.start) | |
| 41 | dismiss() | |
| 42 | } | |
| 43 | .onTapGesture { | |
| 44 | jump(row.start) | |
| 45 | dismiss() | |
| 49 | ForEach(view.specs.indices, id: \.self) { c in cellView(r, c) } | |
| 46 | 50 | } |
| 47 | 51 | } |
| 48 | 52 | } |
| @@ -50,8 +54,74 @@ struct ColumnViewSheet: View { | ||
| 50 | 54 | .padding(10) |
| 51 | 55 | } |
| 52 | 56 | .defaultScrollAnchor(.topLeading) |
| 57 | if let pending { | |
| 58 | HStack { | |
| 59 | Text(pending.prompt.message) | |
| 60 | TextField("", text: $reply).onSubmit(answerPending) | |
| 61 | Button("OK", action: answerPending) | |
| 62 | } | |
| 63 | .padding(8) | |
| 64 | } | |
| 65 | if let message { | |
| 66 | Text(message).font(.callout).foregroundStyle(.secondary).padding(8) | |
| 67 | } | |
| 53 | 68 | } |
| 54 | 69 | .frame(minWidth: 640, minHeight: 360) |
| 70 | .focusable() | |
| 71 | .focused($focused) | |
| 72 | .focusEffectDisabled() | |
| 73 | .onAppear { focused = true } | |
| 74 | .onKeyPress(phases: .down) { press in key(press) } | |
| 75 | } | |
| 76 | ||
| 77 | private func cellView(_ r: Int, _ c: Int) -> some View { | |
| 78 | let row = view.rows[r] | |
| 79 | let isSelected = selected.map { $0 == (r, c) } ?? false | |
| 80 | return Group { | |
| 81 | if let editing, editing == (r, c) { | |
| 82 | editor(r, c) | |
| 83 | } else { | |
| 84 | Text(cell(row, c)) | |
| 85 | .lineLimit(1) | |
| 86 | .frame(maxWidth: view.specs[c].width.map { CGFloat($0) * 8.5 }, alignment: .leading) | |
| 87 | .padding(.horizontal, 2) | |
| 88 | .background(isSelected ? Color.accentColor.opacity(0.25) : .clear, in: RoundedRectangle(cornerRadius: 3)) | |
| 89 | .contentShape(Rectangle()) | |
| 90 | .onTapGesture(count: 2) { startEditing(r, c) } | |
| 91 | .onTapGesture { | |
| 92 | selected = (r, c) | |
| 93 | focused = true | |
| 94 | } | |
| 95 | .accessibilityAddTraits(.isButton) | |
| 96 | .accessibilityAction(named: "Edit") { startEditing(r, c) } | |
| 97 | .accessibilityAction(named: "Go to Entry") { | |
| 98 | jump(row.start) | |
| 99 | dismiss() | |
| 100 | } | |
| 101 | } | |
| 102 | } | |
| 103 | } | |
| 104 | ||
| 105 | /// A field for the value, with the allowed values in a menu. | |
| 106 | private func editor(_ r: Int, _ c: Int) -> some View { | |
| 107 | let spec = view.specs[c] | |
| 108 | let allowed = document().flatMap { PropertyValues.allowed(spec.property, at: view.rows[r].start, text: $0.text, tree: $0.tree)?.values } ?? [] | |
| 109 | return HStack(spacing: 4) { | |
| 110 | TextField(spec.title, text: $draft) | |
| 111 | .textFieldStyle(.roundedBorder) | |
| 112 | .frame(minWidth: 120) | |
| 113 | .onSubmit { commit(r, c, draft) } | |
| 114 | .onExitCommand { editing = nil } | |
| 115 | if !allowed.isEmpty { | |
| 116 | Menu { | |
| 117 | ForEach(allowed, id: \.self) { value in Button(value.isEmpty ? "(none)" : value) { commit(r, c, value) } } | |
| 118 | } label: { | |
| 119 | Image(systemName: "chevron.down") | |
| 120 | } | |
| 121 | .menuStyle(.borderlessButton) | |
| 122 | .fixedSize() | |
| 123 | } | |
| 124 | } | |
| 55 | 125 | } |
| 56 | 126 | |
| 57 | 127 | /// ITEM with the heading's stars, as org shows it. |
| @@ -59,4 +129,82 @@ struct ColumnViewSheet: View { | ||
| 59 | 129 | guard view.specs[i].property == "ITEM" else { return row.values[i] } |
| 60 | 130 | return String(repeating: "*", count: row.level) + " " + row.values[i] |
| 61 | 131 | } |
| 132 | ||
| 133 | private func key(_ press: KeyPress) -> KeyPress.Result { | |
| 134 | guard editing == nil, pending == nil else { return .ignored } | |
| 135 | guard let current = selected else { | |
| 136 | if [.downArrow, .upArrow, .leftArrow, .rightArrow].contains(press.key), !view.rows.isEmpty { | |
| 137 | selected = (0, 0) | |
| 138 | return .handled | |
| 139 | } | |
| 140 | return .ignored | |
| 141 | } | |
| 142 | switch press.key { | |
| 143 | case .downArrow: selected = (min(current.row + 1, view.rows.count - 1), current.column) | |
| 144 | case .upArrow: selected = (max(current.row - 1, 0), current.column) | |
| 145 | case .rightArrow: selected = (current.row, min(current.column + 1, view.specs.count - 1)) | |
| 146 | case .leftArrow: selected = (current.row, max(current.column - 1, 0)) | |
| 147 | case .return: startEditing(current.row, current.column) | |
| 148 | default: | |
| 149 | switch press.characters { | |
| 150 | case "e": startEditing(current.row, current.column) | |
| 151 | case "n", "p": | |
| 152 | let spec = view.specs[current.column] | |
| 153 | perform(NextColumnValue(property: spec.property, summary: spec.summary, previous: press.characters == "p"), row: current.row) | |
| 154 | default: return .ignored | |
| 155 | } | |
| 156 | } | |
| 157 | return .handled | |
| 158 | } | |
| 159 | ||
| 160 | private func startEditing(_ r: Int, _ c: Int) { | |
| 161 | selected = (r, c) | |
| 162 | let spec = view.specs[c] | |
| 163 | if ["CLOCKSUM", "CLOCKSUM_T", "ALLTAGS", "BLOCKED", "CLOSED", "FILE", "TIMESTAMP", "TIMESTAMP_IA"].contains(spec.property) { | |
| 164 | message = "This special column cannot be edited" | |
| 165 | return | |
| 166 | } | |
| 167 | let row = view.rows[r] | |
| 168 | draft = row.raw.indices.contains(c) ? row.raw[c] : row.values[c] | |
| 169 | editing = (r, c) | |
| 170 | } | |
| 171 | ||
| 172 | private func commit(_ r: Int, _ c: Int, _ value: String) { | |
| 173 | editing = nil | |
| 174 | perform(SetColumnValue(property: view.specs[c].property, value: value), row: r) | |
| 175 | } | |
| 176 | ||
| 177 | private func perform(_ command: any OrgCommand, row: Int, answers: [String: String] = [:]) { | |
| 178 | handle(run(command, view.rows[row].start, answers), command: command, start: view.rows[row].start, answers: answers) | |
| 179 | } | |
| 180 | ||
| 181 | private func handle(_ step: CommandStep, command: any OrgCommand, start: Int, answers: [String: String]) { | |
| 182 | switch step { | |
| 183 | case .commit: | |
| 184 | message = nil | |
| 185 | refresh() | |
| 186 | case .failed(let text): | |
| 187 | message = text | |
| 188 | case .prompt(let prompt): | |
| 189 | reply = prompt.initial ?? "" | |
| 190 | pending = (prompt, command, start, answers) | |
| 191 | default: | |
| 192 | message = "\(command.title) can't run here" | |
| 193 | } | |
| 194 | focused = true | |
| 195 | } | |
| 196 | ||
| 197 | private func answerPending() { | |
| 198 | guard let pending else { return } | |
| 199 | self.pending = nil | |
| 200 | let answers = pending.answers.merging([pending.prompt.key: reply]) { $1 } | |
| 201 | handle(run(pending.command, pending.start, answers), command: pending.command, start: pending.start, answers: answers) | |
| 202 | } | |
| 203 | ||
| 204 | /// The view again, over the same entries, keeping the selected cell. | |
| 205 | private func refresh() { | |
| 206 | guard let document = document(), let new = try? ColumnView.refresh(view, text: document.text, tree: document.tree) else { return } | |
| 207 | view = new | |
| 208 | if let selected, selected.row >= new.rows.count { self.selected = nil } | |
| 209 | } | |
| 62 | 210 | } |
Sources/Orgstar/ContentView.swift +9 −1
| @@ -330,7 +330,9 @@ struct ContentView: View { | ||
| 330 | 330 | .sheet(item: $panel) { panel in |
| 331 | 331 | switch panel { |
| 332 | 332 | case .columns(_, let view): |
| 333 | ColumnViewSheet(view: view) { offset in commands.editor?.reveal(offset) } | |
| 333 | ColumnViewSheet(view: view, run: runInColumnView, document: { session.document.map { ($0.text, $0.tree) } }) { offset in | |
| 334 | commands.editor?.reveal(offset) | |
| 335 | } | |
| 334 | 336 | case .export(_, let source): |
| 335 | 337 | ExportSheet(source: source, inSubtree: exportSubtreeAvailable, target: exportTarget) { format, destination, open, subtree, bodyOnly in |
| 336 | 338 | export(format, to: destination, open: open, subtree: subtree, bodyOnly: bodyOnly) |
| @@ -374,6 +376,12 @@ struct ContentView: View { | ||
| 374 | 376 | .onChange(of: workspace.changedFile?.version) { fileChangedOnDisk() } |
| 375 | 377 | } |
| 376 | 378 | |
| 379 | /// A column view edit at the heading starting at `start` in the open file. | |
| 380 | private func runInColumnView(_ command: any OrgCommand, _ start: Int, _ answers: [String: String]) -> CommandStep { | |
| 381 | guard let path = session.path else { return .failed("No file is open") } | |
| 382 | return session.perform(command, path: path, at: start, expect: nil, answers: answers) | |
| 383 | } | |
| 384 | ||
| 377 | 385 | /// An open file takes in what changed on disk, and a clock started or stopped in Emacs |
| 378 | 386 | /// becomes the one shown. |
| 379 | 387 | private func fileChangedOnDisk() { |
Tests/OrgCoreTests/ColumnEditTests.swift added +108
| @@ -0,0 +1,108 @@ | ||
| 1 | import Foundation | |
| 2 | import Testing | |
| 3 | @testable import OrgCore | |
| 4 | ||
| 5 | /// `org-columns-next-allowed-value` and `org-columns-edit-value` against Emacs. | |
| 6 | struct ColumnEditTests { | |
| 7 | static let text = """ | |
| 8 | #+COLUMNS: %25ITEM %TODO %PRIORITY %Effort %Status %Done(Done){X} %SCHEDULED %DEADLINE | |
| 9 | #+PROPERTY: Effort_ALL 0:10 0:30 1:00 | |
| 10 | * TODO [#B] Task one :work: | |
| 11 | SCHEDULED: <2026-10-05 Mon> DEADLINE: <2026-10-09 Fri> | |
| 12 | :PROPERTIES: | |
| 13 | :Effort: 0:30 | |
| 14 | :Status_ALL: new open closed | |
| 15 | :Status: open | |
| 16 | :END: | |
| 17 | * Task two | |
| 18 | Body. | |
| 19 | ||
| 20 | """ | |
| 21 | ||
| 22 | /// Tags one space after the title: Emacs aligns them against column view's overlays. | |
| 23 | static func untagged(_ text: String) -> String { | |
| 24 | text.replacingOccurrences(of: "[ \\t]+(:\\S+:)$", with: " $1", options: [.regularExpression]) | |
| 25 | .components(separatedBy: "\n").map { $0.replacingOccurrences(of: "[ \\t]+(:[^ \\t]+:)$", with: " $1", options: .regularExpression) } | |
| 26 | .joined(separator: "\n") | |
| 27 | } | |
| 28 | ||
| 29 | static func run(_ command: any OrgCommand, at offset: Int) -> String { | |
| 30 | var utc = Calendar(identifier: .gregorian) | |
| 31 | utc.timeZone = TimeZone(identifier: "UTC")! | |
| 32 | let context = EditContext(revision: 0, text: text, tree: OrgParser.parse(text), selection: [offset..<offset], calendar: utc) | |
| 33 | guard case .commit(let result) = command.run(in: context) else { return "failed" } | |
| 34 | var new = text | |
| 35 | for edit in result.edits.sorted(by: { $0.range.lowerBound > $1.range.lowerBound }) { new = edit.apply(to: new) } | |
| 36 | return new | |
| 37 | } | |
| 38 | ||
| 39 | @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil)) | |
| 40 | func nextValuesLikeOrg() throws { | |
| 41 | let ns = Self.text as NSString | |
| 42 | let one = ns.range(of: "* TODO").location | |
| 43 | let two = ns.range(of: "* Task two").location | |
| 44 | let cases: [(Int, String, String?, Bool)] = [ | |
| 45 | (one, "Effort", nil, false), (one, "Effort", nil, true), (one, "Status", nil, false), (one, "TODO", nil, false), | |
| 46 | (one, "PRIORITY", nil, false), (one, "PRIORITY", nil, true), (two, "Effort", nil, false), (two, "Done", "X", false), | |
| 47 | (two, "TODO", nil, true), | |
| 48 | ] | |
| 49 | var oracle: [EmacsOracle.Case] = [] | |
| 50 | var ours: [String] = [] | |
| 51 | for (offset, key, summary, previous) in cases { | |
| 52 | let form = """ | |
| 53 | (progn (org-columns) | |
| 54 | (while (not (equal (get-char-property (point) 'org-columns-key) "\(key.uppercased())")) (forward-char 1)) | |
| 55 | (org-columns-next-allowed-value \(previous ? "t" : "nil")) | |
| 56 | (org-columns-quit)) | |
| 57 | """ | |
| 58 | oracle.append(EmacsOracle.Case(text: Self.text, point: EmacsOracle.point(offset, in: Self.text), form: form)) | |
| 59 | ours.append(Self.run(NextColumnValue(property: key, summary: summary, previous: previous), at: offset + 2)) | |
| 60 | } | |
| 61 | for (i, (mine, theirs)) in zip(ours, try EmacsOracle.run(oracle)).enumerated() { | |
| 62 | #expect(Self.untagged(mine) == Self.untagged(theirs.text), "\(cases[i]) \(theirs.error)") | |
| 63 | } | |
| 64 | } | |
| 65 | ||
| 66 | @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil)) | |
| 67 | func editsLikeOrg() throws { | |
| 68 | let ns = Self.text as NSString | |
| 69 | let one = ns.range(of: "* TODO").location | |
| 70 | let two = ns.range(of: "* Task two").location | |
| 71 | let cases: [(Int, String, String)] = [ | |
| 72 | (one, "ITEM", "Renamed task"), (two, "ITEM", "Second"), (one, "Status", "closed"), (one, "Effort", "2:00"), (two, "Effort", "0:10"), | |
| 73 | (two, "Status", "new"), | |
| 74 | ] | |
| 75 | var oracle: [EmacsOracle.Case] = [] | |
| 76 | var ours: [String] = [] | |
| 77 | for (offset, key, value) in cases { | |
| 78 | let form = """ | |
| 79 | (cl-letf (((symbol-function 'read-string) (lambda (&rest _) "\(value)")) | |
| 80 | ((symbol-function 'completing-read) (lambda (&rest _) "\(value)"))) | |
| 81 | (org-columns) | |
| 82 | (while (not (equal (get-char-property (point) 'org-columns-key) "\(key.uppercased())")) (forward-char 1)) | |
| 83 | (org-columns-edit-value) | |
| 84 | (org-columns-quit)) | |
| 85 | """ | |
| 86 | oracle.append(EmacsOracle.Case(text: Self.text, point: EmacsOracle.point(offset, in: Self.text), form: form)) | |
| 87 | ours.append(Self.run(SetColumnValue(property: key, value: value), at: offset)) | |
| 88 | } | |
| 89 | for (i, (mine, theirs)) in zip(ours, try EmacsOracle.run(oracle)).enumerated() { | |
| 90 | #expect(Self.untagged(mine) == Self.untagged(theirs.text), "\(cases[i]) \(theirs.error)") | |
| 91 | } | |
| 92 | } | |
| 93 | ||
| 94 | /// Org 9.8.7 signals "Properties values should be strings" here; orgstar moves the date. | |
| 95 | @Test func shiftsPlanningDates() { | |
| 96 | let at = (Self.text as NSString).range(of: "* TODO").location + 3 | |
| 97 | #expect(Self.run(NextColumnValue(property: "SCHEDULED"), at: at).contains("SCHEDULED: <2026-10-06 Tue>")) | |
| 98 | #expect(Self.run(NextColumnValue(property: "DEADLINE", previous: true), at: at).contains("DEADLINE: <2026-10-08 Thu>")) | |
| 99 | } | |
| 100 | ||
| 101 | @Test func specialsAndUnknowns() { | |
| 102 | let at = (Self.text as NSString).range(of: "* TODO").location + 3 | |
| 103 | #expect(Self.run(SetColumnValue(property: "CLOCKSUM", value: "1:00"), at: at) == "failed") | |
| 104 | #expect(Self.run(NextColumnValue(property: "ITEM"), at: at) == "failed") | |
| 105 | #expect(Self.run(SetColumnValue(property: "TODO", value: "DONE"), at: at).contains("* DONE [#B] Task one")) | |
| 106 | #expect(Self.run(SetColumnValue(property: "TAGS", value: ":home:work:"), at: at).contains(":home:work:")) | |
| 107 | } | |
| 108 | } | |
docs/manual/guide/03-keys.org +1 −1
| @@ -872,7 +872,7 @@ When a key appears in several rows, it runs the command whose condition holds at | ||
| 872 | 872 | | Next Allowed Value (=org.property.next-value=) | =S-<right>= | =⌃⇧⌘→= | =C-S-l= (N, I) | On a property line, sets the next allowed value (=org-property-next-allowed-value=). | |
| 873 | 873 | | Previous Allowed Value (=org.property.previous-value=) | =S-<left>= | =⌃⇧⌘←= | =C-S-h= (N, I) | On a property line, sets the previous allowed value (=org-property-previous-allowed-value=). | |
| 874 | 874 | | Set Effort (=org.effort.set=) | =C-c C-x e= | =⌃⇧⌘E= | =SPC m c E= (N) | Sets the =Effort= property, offering =Effort_ALL= values (=org-set-effort=). | |
| 875 | | Column View (=org.columns=) | =C-c C-x C-c= | — | — | Shows the column view of the entries, read-only (=org-columns=). | | |
| 875 | | Column View (=org.columns=) | =C-c C-x C-c= | — | — | Shows the column view of the entries, where values can be edited (=org-columns=). | | |
| 876 | 876 | | Insert Column View Table (=org.columns.insert-dblock=) | =C-c C-x i= | — | — | Inserts a =columnview= dynamic block (=org-columns-insert-dblock=). | |
| 877 | 877 | | Update Dynamic Block (=org.dblock.update=) | =C-c C-x C-u=, =C-c C-c= | =⌃⌘X= | — | Updates the dynamic block at point, such as a clock table; =C-c C-c= and =⌃⌘X= do it on the block's =#+BEGIN= line (=org-update-dblock=). | |
| 878 | 878 | |
docs/manual/guide/04-outlines.org +26 −6
| @@ -651,12 +651,32 @@ Subtrees tagged =ARCHIVE= and commented subtrees are left out. | ||
| 651 | 651 | |
| 652 | 652 | Column View opens a sheet with the entries from the view's top (the entry with the |
| 653 | 653 | =COLUMNS= property, or the entry at the caret; the whole file before the first |
| 654 | heading). Column View of File shows every entry in the file. Click a row to go to its | |
| 655 | heading. Done (or Escape) closes the sheet. | |
| 656 | ||
| 657 | The sheet is read-only. Org's column view lets you edit values in place and writes | |
| 658 | parent summaries back into the file; orgstar's does neither. Change values with Set | |
| 659 | Property, or edit the drawer. | |
| 654 | heading). Column View of File shows every entry in the file. Done (or Escape) closes | |
| 655 | the sheet. | |
| 656 | ||
| 657 | Click a cell to select it; the arrow keys move the selection. Go to Entry shows the | |
| 658 | selected entry's heading in the editor. | |
| 659 | ||
| 660 | | Key | Does | Org command | | |
| 661 | |-------------------------+------------------------------------------------------+-------------------------------------| | |
| 662 | | =e=, Return, double-click | Edits the value: type it, or pick an allowed value from the menu beside the field; Return sets it, Escape cancels | =org-columns-edit-value= | | |
| 663 | | =n= | The next allowed value | =org-columns-next-allowed-value= | | |
| 664 | | =p= | The previous allowed value | =org-columns-previous-allowed-value= | | |
| 665 | ||
| 666 | Editing goes through the commands Org uses: =ITEM= changes the headline, keeping its | |
| 667 | keyword, priority and tags (=org-edit-headline=); =TODO= sets the keyword, with logging | |
| 668 | and state notes (a note is asked for at the bottom of the sheet); =PRIORITY=, =TAGS= | |
| 669 | (tags separated by spaces or colons), =SCHEDULED= and =DEADLINE= set those, an empty value | |
| 670 | removing them; other columns set the property in the entry's drawer, under the column's | |
| 671 | name in upper case, as Org writes it. =CLOCKSUM=, =ALLTAGS=, =CLOSED= and the other computed | |
| 672 | columns can't be edited. | |
| 673 | ||
| 674 | The allowed values are the property's =_ALL= values from the entry, an ancestor or a | |
| 675 | =#+PROPERTY:= line, the TODO keywords (and none) for =TODO=, the priorities for | |
| 676 | =PRIORITY=, and =[ ]= and =[X]= for a column with an =X= summary. Without allowed values, | |
| 677 | =n= and =p= say so. On =SCHEDULED= and =DEADLINE= they move the date a day later or | |
| 678 | earlier; Org 9.8.7 signals an error there instead. After each change the sheet captures | |
| 679 | the view again, so summaries follow. Parent summaries are not written into the file. | |
| 660 | 680 | |
| 661 | 681 | ** The Columns inspector |
| 662 | 682 | |