Commit 97cc1d7024

97cc1d70247c3503926316adfa1169de23523e45

parent: 24bd979c0d

Verified · cmc

cmc <hello@cleberg.net> · 2026-11-21 11:48 UTC

Write #ERROR for a Lisp formula whose value is a cons

Layout: unified · split

Sources/OrgCore/Compute/TableFormulas.swift +12 −3
@@ -615,7 +615,7 @@ extension EmacsBuffer {
615615
616616 var result: String
617617 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) }
619619 } else {
620620 form = form.replacingOccurrences(of: "\\[([0-9]{4}-[0-9]{2}-[0-9]{2}(?: [^\\]>\\n]*)?)\\]", with: "<$1>", options: .regularExpression)
621621 form = Self.normalizeTimestamps(form)
@@ -647,8 +647,9 @@ extension EmacsBuffer {
647647 tableSetField(result)
648648 }
649649
650 /// `(eval (eval (read form)))` for a Lisp formula; an error is `#ERROR`.
651 func evalLisp(_ form: String, duration: DurationFormat?) throws -> String {
650 /// `(eval (eval (read form)))` for a Lisp formula; an error is `#ERROR`, and a cons is nil
651 /// (`#ERROR` without the format).
652 func evalLisp(_ form: String, duration: DurationFormat?) throws -> String? {
652653 let lisp = Elisp()
653654 lisp.standardTime()
654655 Self.defineLookups(lisp)
@@ -663,6 +664,14 @@ extension EmacsBuffer {
663664 }
664665 // Emacs prints functions as `#[ARGS BODY ENV]`.
665666 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 }
666675 var result = Elisp.printed(value)
667676 if let duration { result = try Self.secondsToString(Elisp.stringToNumber(result), duration) }
668677 return result
Tests/OrgCoreTests/TableFormulaTests.swift +16
@@ -45,6 +45,7 @@ struct TableFormulaTests {
4545 "| a | b | c |\n|---+---+---|\n| 1 | 2 | |\n| 3 | 4 | |\n#+TBLFM: $3='(+ $1 $2);L\n",
4646 "| a |\n|---|\n| 1 |\n| 2 |\n|---|\n| |\n#+TBLFM: @>$1='(apply '+ '(@I..@II));N\n",
4747 "| 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",
4849 "| a | b | c |\n|---+---+---|\n| 3.14159 | | |\n| 2 | | |\n#+TBLFM: $2='(format \"%05.1f\" $1);N::$3='(+ $1 1);N%.2f\n",
4950 "| a | b |\n|---+---|\n| 5 | |\n| 12 | |\n#+TBLFM: $2='(if (> $1 10) \"big\" \"small\");N\n",
5051 "| ! | qty | price | total |\n|---+-----+-------+-------|\n| | 2 | 3 | |\n| | 4 | 0.5 | |\n#+TBLFM: $4=$qty*$price\n",
@@ -222,6 +223,21 @@ struct TableFormulaTests {
222223 /// Two formulas, then one only Emacs can do: in row order, as Emacs applies them.
223224 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"
224225
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
225241 @Test func keepsFieldsWrittenBeforeAFormulaOnlyEmacsCanDo() {
226242 let text = Self.unsupported
227243 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=
574574=function= make function objects rather than lists, so a formula whose value is one, or
575575that prints one or takes it apart as a list, needs Emacs; calling one, or passing it to a
576576function that calls it, doesn't. An error inside a Lisp formula
577writes =#ERROR= in the field, as does a Calc error.
577writes =#ERROR= in the field, as does a Calc error. So does a value that is a non-empty
578list, ignoring the format; with =T=, =U= or =t= such a value needs Emacs.
578579
579580=org-sbe= writes the value of a named src block, run with the variables given:
580581