Commit fa36a13d84
Verified · cmc
Layout: unified · split
Sources/OrgCore/Commands/Table.swift +7 −8
| @@ -603,17 +603,16 @@ public struct TableCommand: OrgCommand { | |||
| 603 | public func applies(in context: EditContext) -> Bool { KeyContext.holds("table", in: context) } | 603 | public func applies(in context: EditContext) -> Bool { KeyContext.holds("table", in: context) } |
| 604 | 604 | ||
| 605 | public func run(in context: EditContext) -> CommandStep { | 605 | public func run(in context: EditContext) -> CommandStep { |
| 606 | let step = tableCommand(context) { buffer in | 606 | let body = { (buffer: EmacsBuffer) throws in |
| 607 | guard buffer.atTable else { throw EmacsBuffer.UserError(message: "Not at a table") } | 607 | guard buffer.atTable else { throw EmacsBuffer.UserError(message: "Not at a table") } |
| 608 | do { | 608 | try perform(buffer) |
| 609 | try perform(buffer) | ||
| 610 | } catch let error as Calc.Unsupported { | ||
| 611 | throw EmacsBuffer.UserError(message: TableRecalculate.emacsMessage + error.reason) | ||
| 612 | } | ||
| 613 | } | 609 | } |
| 614 | // A row marked `#` that recalculates with formulas only Emacs can do. | 610 | // These recalculate a row marked `#` first, whose written fields stay after an error. |
| 615 | let forms: [Action: String] = [.nextField: "(org-table-next-field)", .previousField: "(org-table-previous-field)", .nextRow: "(org-table-next-row)"] | 611 | let forms: [Action: String] = [.nextField: "(org-table-next-field)", .previousField: "(org-table-previous-field)", .nextRow: "(org-table-next-row)"] |
| 616 | return forms[action].map { TableRecalculate.inEmacs(step, form: $0, in: context) } ?? step | 612 | guard let form = forms[action], EmacsBuffer(context.text, point: context.caret).lookingAtLine("^[ \\t]*\\| *# *(\\||$)") else { |
| 613 | return tableCommand(context, body) | ||
| 614 | } | ||
| 615 | return TableRecalculate.recalculating(context, form: form, body) | ||
| 617 | } | 616 | } |
| 618 | 617 | ||
| 619 | private func perform(_ buffer: EmacsBuffer) throws { | 618 | private func perform(_ buffer: EmacsBuffer) throws { |
Sources/OrgCore/Compute/TableFormulaCommands.swift +2 −8
| @@ -68,17 +68,11 @@ public struct TableRecalculate: OrgCommand { | |||
| 68 | return request(form: form, reason: unsupported.reason, formula: wrote ? unsupported.formula : nil, partial: wrote ? result : nil, in: context) | 68 | return request(form: form, reason: unsupported.reason, formula: wrote ? unsupported.formula : nil, partial: wrote ? result : nil, in: context) |
| 69 | } | 69 | } |
| 70 | guard let failure else { return step } | 70 | guard let failure else { return step } |
| 71 | guard !result.edits.isEmpty else { return .failed(failure) } | 71 | // `org-table-save-field` moves point to the field's start even when nothing was written. |
| 72 | guard !result.edits.isEmpty || result.selection != [context.caret..<context.caret] else { return .failed(failure) } | ||
| 72 | return .commit(EditResult(baseRevision: result.baseRevision, edits: result.edits, selection: result.selection, effects: result.effects + [.message(failure)])) | 73 | return .commit(EditResult(baseRevision: result.baseRevision, edits: result.edits, selection: result.selection, effects: result.effects + [.message(failure)])) |
| 73 | } | 74 | } |
| 74 | 75 | ||
| 75 | /// A step that failed with `emacsMessage` as a request to run `form` on the table in | ||
| 76 | /// Emacs, whose text then replaces the table here; other steps unchanged. | ||
| 77 | static func inEmacs(_ step: CommandStep, form: String, in context: EditContext) -> CommandStep { | ||
| 78 | guard case .failed(let message) = step, message.hasPrefix(emacsMessage) else { return step } | ||
| 79 | return request(form: form, reason: String(message.dropFirst(emacsMessage.count)), formula: nil, partial: nil, in: context) | ||
| 80 | } | ||
| 81 | |||
| 82 | static func request(form: String, reason: String, formula: String?, partial: EditResult?, in context: EditContext) -> CommandStep { | 76 | static func request(form: String, reason: String, formula: String?, partial: EditResult?, in context: EditContext) -> CommandStep { |
| 83 | let buffer = EmacsBuffer(context.text, point: context.caret, settings: context.tree.settings, options: context.options) | 77 | let buffer = EmacsBuffer(context.text, point: context.caret, settings: context.tree.settings, options: context.options) |
| 84 | let onTblfm = tblfmLine(in: context) != nil | 78 | let onTblfm = tblfmLine(in: context) != nil |
Tests/OrgCoreTests/HeadingCommandTests.swift +11 −5
| @@ -131,14 +131,18 @@ struct HeadingOracleTests { | |||
| 131 | func compareWithEmacs(_ inputs: [(text: String, carets: [Int])], _ commands: [(command: any OrgCommand, form: String, headingLineOnly: Bool)], options: EditingOptions = .org) throws { | 131 | func compareWithEmacs(_ inputs: [(text: String, carets: [Int])], _ commands: [(command: any OrgCommand, form: String, headingLineOnly: Bool)], options: EditingOptions = .org) throws { |
| 132 | try #require(EmacsOracle.isAvailable, "Emacs is required for the oracle tests; set ORGSTAR_SKIP_ORACLE to skip") | 132 | try #require(EmacsOracle.isAvailable, "Emacs is required for the oracle tests; set ORGSTAR_SKIP_ORACLE to skip") |
| 133 | var cases: [EmacsOracle.Case] = [] | 133 | var cases: [EmacsOracle.Case] = [] |
| 134 | var ours: [(label: String, text: String, caret: Int, failed: Bool)] = [] | 134 | var ours: [(label: String, text: String, caret: Int, failed: Bool, message: String?)] = [] |
| 135 | for (text, carets) in inputs { | 135 | for (text, carets) in inputs { |
| 136 | for offset in carets { | 136 | for offset in carets { |
| 137 | let onHeadingLine = headingOnLine(at: offset, in: OrgParser.parse(text)) != nil | 137 | let onHeadingLine = headingOnLine(at: offset, in: OrgParser.parse(text)) != nil |
| 138 | for (command, form, headingLineOnly) in commands where onHeadingLine || !headingLineOnly { | 138 | for (command, form, headingLineOnly) in commands where onHeadingLine || !headingLineOnly { |
| 139 | cases.append(EmacsOracle.Case(text: text, point: EmacsOracle.point(offset, in: text), form: withOptions(options, form))) | 139 | cases.append(EmacsOracle.Case(text: text, point: EmacsOracle.point(offset, in: text), form: withOptions(options, form))) |
| 140 | let result = runCommand(command, text, caret: offset, options: options) | 140 | let result = runCommand(command, text, caret: offset, options: options) |
| 141 | ours.append(("\(command.id) at \(offset) in \(text.debugDescription)", result.text, result.caret, result.failure != nil)) | 141 | // An error after edits that stay, as in Emacs, comes as their last message. |
| 142 | let context = EditContext(revision: 0, text: text, tree: OrgParser.parse(text), selection: [offset..<offset], calendar: .current, answers: [:], options: options) | ||
| 143 | var message: String? | ||
| 144 | if case .commit(let commit) = command.run(in: context), case .message(let last)? = commit.effects.last { message = last } | ||
| 145 | ours.append(("\(command.id) at \(offset) in \(text.debugDescription)", result.text, result.caret, result.failure != nil, message)) | ||
| 142 | } | 146 | } |
| 143 | } | 147 | } |
| 144 | } | 148 | } |
| @@ -147,15 +151,17 @@ func compareWithEmacs(_ inputs: [(text: String, carets: [Int])], _ commands: [(c | |||
| 147 | for (mine, emacs) in zip(ours, theirs) { | 151 | for (mine, emacs) in zip(ours, theirs) { |
| 148 | let emacsFailed = !emacs.error.isEmpty | 152 | let emacsFailed = !emacs.error.isEmpty |
| 149 | let emacsCaret = EmacsOracle.offset(emacs.point, in: emacs.text) | 153 | let emacsCaret = EmacsOracle.offset(emacs.point, in: emacs.text) |
| 150 | let same = mine.failed == emacsFailed && (emacsFailed || (mine.text == emacs.text && mine.caret == emacsCaret)) | 154 | let keptEdits = emacsFailed && !mine.failed && mine.message == emacs.error |
| 155 | let same = keptEdits ? mine.text == emacs.text && mine.caret == emacsCaret | ||
| 156 | : mine.failed == emacsFailed && (emacsFailed || (mine.text == emacs.text && mine.caret == emacsCaret)) | ||
| 151 | if !same { | 157 | if !same { |
| 152 | mismatches += 1 | 158 | mismatches += 1 |
| 153 | if mismatches <= 15 { | 159 | if mismatches <= 15 { |
| 154 | let (a, b) = excerpts(mine.text, emacs.text) | 160 | let (a, b) = excerpts(mine.text, emacs.text) |
| 155 | Issue.record(""" | 161 | Issue.record(""" |
| 156 | \(mine.label.prefix(100)) | 162 | \(mine.label.prefix(100)) |
| 157 | ours: \(mine.failed ? "failed" : "\(a.debugDescription) @\(mine.caret)") | 163 | ours: \(mine.failed ? "failed" : "\(a.debugDescription) @\(mine.caret) \(mine.message ?? "")") |
| 158 | emacs: \(emacsFailed ? "failed: \(emacs.error)" : "\(b.debugDescription) @\(emacsCaret)") | 164 | emacs: \(emacsFailed && !keptEdits ? "failed: \(emacs.error)" : "\(b.debugDescription) @\(emacsCaret) \(emacs.error)") |
| 159 | """) | 165 | """) |
| 160 | } | 166 | } |
| 161 | } | 167 | } |
Tests/OrgCoreTests/TableFormulaTests.swift +33 −1
| @@ -188,6 +188,7 @@ struct TableFormulaTests { | |||
| 188 | Self.unsupported, | 188 | Self.unsupported, |
| 189 | "| a | b | c |\n|---+---+---|\n| 1 | | |\n| 2 | | |\n| 3 | | |\n#+TBLFM: $2=$1*3::@3$3=\(lisp)::@2$3=7::@4$3=8\n", | 189 | "| a | b | c |\n|---+---+---|\n| 1 | | |\n| 2 | | |\n| 3 | | |\n#+TBLFM: $2=$1*3::@3$3=\(lisp)::@2$3=7::@4$3=8\n", |
| 190 | "| a | b |\n|---+---|\n| # | |\n| 2 | |\n#+TBLFM: $2=@0$1+1::@3$2=\(lisp)\n", | 190 | "| a | b |\n|---+---|\n| # | |\n| 2 | |\n#+TBLFM: $2=@0$1+1::@3$2=\(lisp)\n", |
| 191 | "| | a | b | c |\n|---+---+---+---|\n| # | 1 | | |\n#+TBLFM: $3=$2*2::$4=\(lisp)\n", | ||
| 191 | ] | 192 | ] |
| 192 | var cases: [EmacsOracle.Case] = [] | 193 | var cases: [EmacsOracle.Case] = [] |
| 193 | var ours: [(text: String, caret: Int)] = [] | 194 | var ours: [(text: String, caret: Int)] = [] |
| @@ -197,7 +198,8 @@ struct TableFormulaTests { | |||
| 197 | let onTblfm = (text as NSString).substring(with: (text as NSString).lineRange(for: NSRange(location: caret, length: 0))).contains("#+TBLFM") | 198 | let onTblfm = (text as NSString).substring(with: (text as NSString).lineRange(for: NSRange(location: caret, length: 0))).contains("#+TBLFM") |
| 198 | let commands: [(any OrgCommand, String)] = onTblfm | 199 | let commands: [(any OrgCommand, String)] = onTblfm |
| 199 | ? [(TableRecalculate(all: true), "(org-ctrl-c-ctrl-c)")] | 200 | ? [(TableRecalculate(all: true), "(org-ctrl-c-ctrl-c)")] |
| 200 | : [(TableRecalculate(all: true), "(org-table-recalculate t)"), (TableRecalculate(all: false), "(org-table-recalculate nil)"), (TableIterate(), "(org-table-iterate)")] | 201 | : [(TableRecalculate(all: true), "(org-table-recalculate t)"), (TableRecalculate(all: false), "(org-table-recalculate nil)"), (TableIterate(), "(org-table-iterate)"), |
| 202 | (TableCommand(.nextField), "(org-table-next-field)"), (TableCommand(.previousField), "(org-table-previous-field)"), (TableCommand(.nextRow), "(org-table-next-row)")] | ||
| 201 | for (command, form) in commands { | 203 | for (command, form) in commands { |
| 202 | let context = EditContext(revision: 0, text: text, tree: OrgParser.parse(text), selection: [caret..<caret]) | 204 | let context = EditContext(revision: 0, text: text, tree: OrgParser.parse(text), selection: [caret..<caret]) |
| 203 | let step = command.run(in: context) | 205 | let step = command.run(in: context) |
| @@ -210,6 +212,7 @@ struct TableFormulaTests { | |||
| 210 | } | 212 | } |
| 211 | } | 213 | } |
| 212 | #expect(cases.count >= 8) | 214 | #expect(cases.count >= 8) |
| 215 | #expect(cases.filter { ["(org-table-next-field)", "(org-table-previous-field)", "(org-table-next-row)"].contains($0.form) }.count == 3) | ||
| 213 | for (i, (theirs, mine)) in zip(try EmacsOracle.run(cases), ours).enumerated() { | 216 | for (i, (theirs, mine)) in zip(try EmacsOracle.run(cases), ours).enumerated() { |
| 214 | #expect(theirs.error.contains("leads outside table"), "\(theirs.error)") | 217 | #expect(theirs.error.contains("leads outside table"), "\(theirs.error)") |
| 215 | let text = theirs.text.replacingOccurrences(of: failing, with: lisp) | 218 | let text = theirs.text.replacingOccurrences(of: failing, with: lisp) |
| @@ -219,6 +222,35 @@ struct TableFormulaTests { | |||
| 219 | } | 222 | } |
| 220 | } | 223 | } |
| 221 | 224 | ||
| 225 | /// TAB, S-TAB and RET in a row marked `#`, and recalculations with point inside a field, | ||
| 226 | /// when a formula fails: the fields written before it stay and point goes to the field's start. | ||
| 227 | @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil)) | ||
| 228 | func errorsKeepFieldsAndMovePointAsEmacs() throws { | ||
| 229 | let marked = "| | a | b | c |\n|---+---+---+---|\n| # | 1 | | |\n#+TBLFM: $3=$2*2::$4=@-9$2\n" | ||
| 230 | let early = "| a | b |\n|---+---|\n| 1 |x|\n| 2 |y |\n#+TBLFM: $2=$1*2::@2$2=7::@3$2=8::@2$1=@-9$1\n" | ||
| 231 | let fine = "| | a | b |\n|---+---+---|\n| # | 1 | |\n| | 2 | |\n#+TBLFM: $3=$2*2\n" | ||
| 232 | func after(_ needle: String, in text: String) -> Int { NSMaxRange((text as NSString).range(of: needle)) } | ||
| 233 | let runs: [(text: String, caret: Int, command: any OrgCommand, form: String)] = [ | ||
| 234 | (marked, after("| # | 1", in: marked), TableCommand(.nextField), "(org-table-next-field)"), | ||
| 235 | (marked, after("| # | 1", in: marked), TableCommand(.previousField), "(org-table-previous-field)"), | ||
| 236 | (marked, after("| # | 1", in: marked), TableCommand(.nextRow), "(org-table-next-row)"), | ||
| 237 | (marked, after("| # | 1 | ", in: marked), TableCommand(.nextField), "(org-table-next-field)"), | ||
| 238 | (early, after("| 2", in: early), TableRecalculate(all: true), "(org-table-recalculate t)"), | ||
| 239 | (early, after("| 2 |y", in: early), TableRecalculate(all: true), "(org-table-recalculate t)"), | ||
| 240 | (early, after("| 1 |", in: early), TableRecalculate(all: false), "(org-table-recalculate nil)"), | ||
| 241 | (fine, after("| # | 1", in: fine), TableCommand(.nextField), "(org-table-next-field)"), | ||
| 242 | (fine, after("| # | 1", in: fine), TableCommand(.previousField), "(org-table-previous-field)"), | ||
| 243 | (fine, after("| # | 1", in: fine), TableCommand(.nextRow), "(org-table-next-row)"), | ||
| 244 | ] | ||
| 245 | let cases = runs.map { EmacsOracle.Case(text: $0.text, point: EmacsOracle.point($0.caret, in: $0.text), form: $0.form) } | ||
| 246 | for (i, theirs) in try EmacsOracle.run(cases).enumerated() { | ||
| 247 | let mine = Self.recalculate(runs[i].command, runs[i].text, caret: runs[i].caret) | ||
| 248 | let expected = (theirs.text, EmacsOracle.offset(theirs.point, in: theirs.text), theirs.error.isEmpty ? nil : theirs.error) | ||
| 249 | #expect(mine.text == expected.0 && mine.caret == expected.1 && mine.failure == expected.2, | ||
| 250 | "\(runs[i].form) at \(runs[i].caret) in\n\(runs[i].text)\nours:\n\(mine.text)@\(mine.caret) \(mine.failure ?? "")\nemacs:\n\(theirs.text)@\(expected.1) \(theirs.error)") | ||
| 251 | } | ||
| 252 | } | ||
| 253 | |||
| 222 | /// `runCommand`, with the message of an error that left fields written as the failure. | 254 | /// `runCommand`, with the message of an error that left fields written as the failure. |
| 223 | static func recalculate(_ command: any OrgCommand, _ text: String, caret: Int) -> (text: String, caret: Int, failure: String?) { | 255 | static func recalculate(_ command: any OrgCommand, _ text: String, caret: Int) -> (text: String, caret: Int, failure: String?) { |
| 224 | let result = runCommand(command, text, caret: caret) | 256 | let result = runCommand(command, text, caret: caret) |
docs/manual/guide/10-tables.org +3 −1
| @@ -570,7 +570,9 @@ the whole table in the Emacs preset. | |||
| 570 | 570 | ||
| 571 | A formula that fails, such as one whose reference leads outside the table, stops the | 571 | A formula that fails, such as one whose reference leads outside the table, stops the |
| 572 | command there. As in Emacs, the fields written before it stay, the table isn't aligned, | 572 | command there. As in Emacs, the fields written before it stay, the table isn't aligned, |
| 573 | and the echo area shows the error. | 573 | the caret goes to the start of its field (on a =#+TBLFM= line it stays put), and the echo |
| 574 | area shows the error. | ||
| 575 | =TAB=, =S-TAB= and =RET= in a row marked =#= stop there too, without moving on. | ||
| 574 | 576 | ||
| 575 | A row marked =#= is recalculated when you press =TAB=, =S-TAB= or =RET= in it, before | 577 | A row marked =#= is recalculated when you press =TAB=, =S-TAB= or =RET= in it, before |
| 576 | the caret moves (=org-table-maybe-recalculate-line=), and with =C-c C-c= (Mac =⌃⌘X=). | 578 | the caret moves (=org-table-maybe-recalculate-line=), and with =C-c C-c= (Mac =⌃⌘X=). |