Commit 97cc1d7024
Verified · cmc
Layout: unified · split
Sources/OrgCore/Compute/TableFormulas.swift +12 −3
| @@ -615,7 +615,7 @@ extension EmacsBuffer { | |||
| 615 | 615 | ||
| 616 | var result: String | 616 | var result: String |
| 617 | if mode.lisp != .none { | 617 | if mode.lisp != .none { |
| 618 | result = try evalLisp(form, duration: duration) | 618 | if let value = try evalLisp(form, duration: duration) { result = value } else { (result, fmt) = ("#ERROR", nil) } |
| 619 | } else { | 619 | } else { |
| 620 | form = form.replacingOccurrences(of: "\\[([0-9]{4}-[0-9]{2}-[0-9]{2}(?: [^\\]>\\n]*)?)\\]", with: "<$1>", options: .regularExpression) | 620 | form = form.replacingOccurrences(of: "\\[([0-9]{4}-[0-9]{2}-[0-9]{2}(?: [^\\]>\\n]*)?)\\]", with: "<$1>", options: .regularExpression) |
| 621 | form = Self.normalizeTimestamps(form) | 621 | form = Self.normalizeTimestamps(form) |
| @@ -647,8 +647,9 @@ extension EmacsBuffer { | |||
| 647 | tableSetField(result) | 647 | tableSetField(result) |
| 648 | } | 648 | } |
| 649 | 649 | ||
| 650 | /// `(eval (eval (read form)))` for a Lisp formula; an error is `#ERROR`. | 650 | /// `(eval (eval (read form)))` for a Lisp formula; an error is `#ERROR`, and a cons is nil |
| 651 | func evalLisp(_ form: String, duration: DurationFormat?) throws -> String { | 651 | /// (`#ERROR` without the format). |
| 652 | func evalLisp(_ form: String, duration: DurationFormat?) throws -> String? { | ||
| 652 | let lisp = Elisp() | 653 | let lisp = Elisp() |
| 653 | lisp.standardTime() | 654 | lisp.standardTime() |
| 654 | Self.defineLookups(lisp) | 655 | Self.defineLookups(lisp) |
| @@ -663,6 +664,14 @@ extension EmacsBuffer { | |||
| 663 | } | 664 | } |
| 664 | // Emacs prints functions as `#[ARGS BODY ENV]`. | 665 | // Emacs prints functions as `#[ARGS BODY ENV]`. |
| 665 | if Elisp.containsClosure(value) { throw Calc.Unsupported("Lisp formula with a function value") } | 666 | if Elisp.containsClosure(value) { throw Calc.Unsupported("Lisp formula with a function value") } |
| 667 | switch value { | ||
| 668 | case .list([]): break | ||
| 669 | case .list, .dotted: | ||
| 670 | // Org converts a duration before checking for a cons, which signals. | ||
| 671 | if duration != nil { throw Calc.Unsupported("Lisp formula with a list value and a duration") } | ||
| 672 | return nil | ||
| 673 | default: break | ||
| 674 | } | ||
| 666 | var result = Elisp.printed(value) | 675 | var result = Elisp.printed(value) |
| 667 | if let duration { result = try Self.secondsToString(Elisp.stringToNumber(result), duration) } | 676 | if let duration { result = try Self.secondsToString(Elisp.stringToNumber(result), duration) } |
| 668 | return result | 677 | return result |
Tests/OrgCoreTests/TableFormulaTests.swift +16
| @@ -45,6 +45,7 @@ struct TableFormulaTests { | |||
| 45 | "| a | b | c |\n|---+---+---|\n| 1 | 2 | |\n| 3 | 4 | |\n#+TBLFM: $3='(+ $1 $2);L\n", | 45 | "| a | b | c |\n|---+---+---|\n| 1 | 2 | |\n| 3 | 4 | |\n#+TBLFM: $3='(+ $1 $2);L\n", |
| 46 | "| a |\n|---|\n| 1 |\n| 2 |\n|---|\n| |\n#+TBLFM: @>$1='(apply '+ '(@I..@II));N\n", | 46 | "| a |\n|---|\n| 1 |\n| 2 |\n|---|\n| |\n#+TBLFM: @>$1='(apply '+ '(@I..@II));N\n", |
| 47 | "| a | b |\n|---+---|\n| 1 | |\n#+TBLFM: $2='(car 1)\n", | 47 | "| a | b |\n|---+---|\n| 1 | |\n#+TBLFM: $2='(car 1)\n", |
| 48 | "| a | b | c | d | e | f |\n|---+---+---+---+---+---|\n| 1 | | | | | |\n#+TBLFM: $2='(list 1 2)::$3='(list \"a\" \"b\")::$4='(cons \"a\" \"b\");%.2f::$5='(list \"a b\" (list \"c\"))::$6='(list)\n", | ||
| 48 | "| a | b | c |\n|---+---+---|\n| 3.14159 | | |\n| 2 | | |\n#+TBLFM: $2='(format \"%05.1f\" $1);N::$3='(+ $1 1);N%.2f\n", | 49 | "| a | b | c |\n|---+---+---|\n| 3.14159 | | |\n| 2 | | |\n#+TBLFM: $2='(format \"%05.1f\" $1);N::$3='(+ $1 1);N%.2f\n", |
| 49 | "| a | b |\n|---+---|\n| 5 | |\n| 12 | |\n#+TBLFM: $2='(if (> $1 10) \"big\" \"small\");N\n", | 50 | "| a | b |\n|---+---|\n| 5 | |\n| 12 | |\n#+TBLFM: $2='(if (> $1 10) \"big\" \"small\");N\n", |
| 50 | "| ! | qty | price | total |\n|---+-----+-------+-------|\n| | 2 | 3 | |\n| | 4 | 0.5 | |\n#+TBLFM: $4=$qty*$price\n", | 51 | "| ! | qty | price | total |\n|---+-----+-------+-------|\n| | 2 | 3 | |\n| | 4 | 0.5 | |\n#+TBLFM: $4=$qty*$price\n", |
| @@ -222,6 +223,21 @@ struct TableFormulaTests { | |||
| 222 | /// Two formulas, then one only Emacs can do: in row order, as Emacs applies them. | 223 | /// Two formulas, then one only Emacs can do: in row order, as Emacs applies them. |
| 223 | static let unsupported = "| a | b | c | d |\n|---+---+---+---|\n| 1 | | | |\n| 2 | | | |\n#+TBLFM: $2=$1*2::$3=$1+1::$4='(shell-command-to-string $1)\n" | 224 | static let unsupported = "| a | b | c | d |\n|---+---+---+---|\n| 1 | | | |\n| 2 | | | |\n#+TBLFM: $2=$1*2::$3=$1+1::$4='(shell-command-to-string $1)\n" |
| 224 | 225 | ||
| 226 | @Test func lispConsIsAnError() { | ||
| 227 | let text = "| a | b | c | d | e |\n|---+---+---+---+---|\n| 1 | | | | |\n#+TBLFM: $2='(list 1 2)::$3='(cons \"a\" \"b\");N%.2f::$4='(list 'quote '(\"a\" \"b\"))::$5='(list)\n" | ||
| 228 | let caret = (text as NSString).range(of: "#+TBLFM").location | ||
| 229 | let result = Self.recalculate(TableRecalculate(all: true), text, caret: caret) | ||
| 230 | #expect(result.failure == nil) | ||
| 231 | #expect(result.text.hasPrefix("| a | b | c | d | e |\n|---+--------+--------+--------+-----|\n| 1 | #ERROR | #ERROR | #ERROR | nil |\n")) | ||
| 232 | let duration = "| a | b |\n|---+---|\n| 1 | |\n#+TBLFM: $2='(list 1 2);T\n" | ||
| 233 | let context = EditContext(revision: 0, text: duration, tree: OrgParser.parse(duration), selection: [3..<3]) | ||
| 234 | guard case .external(.emacsTable(_, _, _, let reason, _, _, _)) = TableRecalculate(all: true).run(in: context) else { | ||
| 235 | Issue.record("not a request") | ||
| 236 | return | ||
| 237 | } | ||
| 238 | #expect(reason == "Lisp formula with a list value and a duration") | ||
| 239 | } | ||
| 240 | |||
| 225 | @Test func keepsFieldsWrittenBeforeAFormulaOnlyEmacsCanDo() { | 241 | @Test func keepsFieldsWrittenBeforeAFormulaOnlyEmacsCanDo() { |
| 226 | let text = Self.unsupported | 242 | let text = Self.unsupported |
| 227 | let caret = (text as NSString).range(of: "#+TBLFM").location + 3 | 243 | let caret = (text as NSString).range(of: "#+TBLFM").location + 3 |
docs/manual/guide/10-tables.org +2 −1
| @@ -574,7 +574,8 @@ Any other function, or a macro not listed, needs Emacs. As in Emacs 31, =lambda= | |||
| 574 | =function= make function objects rather than lists, so a formula whose value is one, or | 574 | =function= make function objects rather than lists, so a formula whose value is one, or |
| 575 | that prints one or takes it apart as a list, needs Emacs; calling one, or passing it to a | 575 | that prints one or takes it apart as a list, needs Emacs; calling one, or passing it to a |
| 576 | function that calls it, doesn't. An error inside a Lisp formula | 576 | function that calls it, doesn't. An error inside a Lisp formula |
| 577 | writes =#ERROR= in the field, as does a Calc error. | 577 | writes =#ERROR= in the field, as does a Calc error. So does a value that is a non-empty |
| 578 | list, ignoring the format; with =T=, =U= or =t= such a value needs Emacs. | ||
| 578 | 579 | ||
| 579 | =org-sbe= writes the value of a named src block, run with the variables given: | 580 | =org-sbe= writes the value of a named src block, run with the variables given: |
| 580 | 581 | ||