Commit 95111c0832
Verified · cmc
Layout: unified · split
Sources/OrgCore/Compute/BabelExpand.swift +2 −2
| @@ -41,7 +41,7 @@ extension Babel { | ||
| 41 | 41 | guard let raw = params.single[key] else { return nil } |
| 42 | 42 | var value = params.evaluated[key] |
| 43 | 43 | if value == nil, params.lisp.contains(key) { |
| 44 | guard let form = try? LispReader.readFirst(raw).sexp, let result = try? Elisp().eval(form) else { | |
| 44 | guard let form = try? LispReader.readFirst(raw).sexp, let result = try? Elisp().eval(form), !Elisp.containsClosure(result) else { | |
| 45 | 45 | throw Failure.message("\(key) \(raw) is Lisp that only Emacs can evaluate.") |
| 46 | 46 | } |
| 47 | 47 | value = result |
| @@ -61,7 +61,7 @@ extension Babel { | ||
| 61 | 61 | guard case .string(let s) = read(value) else { return .string(value) } |
| 62 | 62 | return .string(s) |
| 63 | 63 | } |
| 64 | guard let form = try? LispReader.readFirst(value).sexp, let result = try? Elisp().eval(form) else { | |
| 64 | guard let form = try? LispReader.readFirst(value).sexp, let result = try? Elisp().eval(form), !Elisp.containsClosure(result) else { | |
| 65 | 65 | throw Failure.message("The property value \(value) is Lisp that only Emacs can evaluate.") |
| 66 | 66 | } |
| 67 | 67 | if let s = result.string { return .string(s) } |
Sources/OrgCore/Compute/BabelHeaders.swift +1 −1
| @@ -21,7 +21,7 @@ extension Babel { | ||
| 21 | 21 | mutating func evaluateLisp(file: String?) { |
| 22 | 22 | for key in lisp { |
| 23 | 23 | guard let form = try? LispReader.readFirst(single[key] ?? "").sexp, |
| 24 | let value = try? (file.map { Tangle.fileLisp(path: $0) } ?? Elisp()).eval(form) else { continue } | |
| 24 | let value = try? (file.map { Tangle.fileLisp(path: $0) } ?? Elisp()).eval(form), !Elisp.containsClosure(value) else { continue } | |
| 25 | 25 | let printed = Elisp.printed(value) |
| 26 | 26 | if let s = value.string { |
| 27 | 27 | single[key] = s |
Sources/OrgCore/Compute/BabelVars.swift +1 −1
| @@ -88,7 +88,7 @@ extension Babel { | ||
| 88 | 88 | /// table. |
| 89 | 89 | static func lispValue(_ value: String) throws -> VarValue { |
| 90 | 90 | let refusal = Failure.message(":var value \(value) is Lisp that only Emacs can evaluate.") |
| 91 | guard let form = try? LispReader.readFirst(value).sexp, let result = try? Elisp().eval(form) else { throw refusal } | |
| 91 | guard let form = try? LispReader.readFirst(value).sexp, let result = try? Elisp().eval(form), !Elisp.containsClosure(result) else { throw refusal } | |
| 92 | 92 | func cell(_ v: Sexp) throws -> Cell { |
| 93 | 93 | if let s = v.string { return .text(s) } |
| 94 | 94 | if let n = number(Elisp.printed(v)), Elisp.printed(v) == n { return .number(n) } |
Sources/OrgCore/Compute/TableFormulas.swift +2
| @@ -661,6 +661,8 @@ extension EmacsBuffer { | ||
| 661 | 661 | } catch { |
| 662 | 662 | return "#ERROR" |
| 663 | 663 | } |
| 664 | // Emacs prints functions as `#[ARGS BODY ENV]`. | |
| 665 | if Elisp.containsClosure(value) { throw Calc.Unsupported("Lisp formula with a function value") } | |
| 664 | 666 | var result = Elisp.printed(value) |
| 665 | 667 | if let duration { result = try Self.secondsToString(Elisp.stringToNumber(result), duration) } |
| 666 | 668 | return result |
Sources/OrgCore/Compute/TableSbe.swift +9 −6
| @@ -90,9 +90,11 @@ extension Babel { | ||
| 90 | 90 | let model = DocumentModel(tree: OrgParser.parse(text)) |
| 91 | 91 | let options = Options() |
| 92 | 92 | // The first `#+NAME:` line with the name outside COMMENTed headings that names an |
| 93 | // element: a src block's, or another element's. | |
| 94 | let pattern = "(?im)^[ \\t]*#\\+name:[ \\t]*" + NSRegularExpression.escapedPattern(for: name) + "[ \\t]*$" | |
| 95 | let matches = try NSRegularExpression(pattern: pattern).matches(in: text, range: NSRange(location: 0, length: ns.length)).map(\.range) | |
| 93 | // element: a src block's, or another element's. The keyword's case doesn't matter; the | |
| 94 | // name's does. | |
| 95 | let pattern = "(?im)^[ \\t]*#\\+name:[ \\t]*(" + NSRegularExpression.escapedPattern(for: name) + ")[ \\t]*$" | |
| 96 | let found = try NSRegularExpression(pattern: pattern).matches(in: text, range: NSRange(location: 0, length: ns.length)) | |
| 97 | let matches = found.filter { ns.substring(with: $0.range(at: 1)) == name }.map(\.range) | |
| 96 | 98 | let headingStarts = model.headings.map(\.headingRange.lowerBound) |
| 97 | 99 | func commented(_ location: Int) -> Bool { |
| 98 | 100 | guard let h = headingStarts.lastIndex(where: { $0 <= location }) else { return false } |
| @@ -119,8 +121,9 @@ extension Babel { | ||
| 119 | 121 | return between.split(separator: "\n").allSatisfy { $0.trimmingCharacters(in: .whitespaces).hasPrefix("#+") } |
| 120 | 122 | } |
| 121 | 123 | guard let block else { |
| 122 | // `resolve` reads the first element with the name, and runs a src block before it. | |
| 123 | guard named == matches.first, !model.srcBlocks.contains(where: { $0.name == name }) else { | |
| 124 | // `resolve` reads the first element with the name in any case, and runs a src block | |
| 125 | // before it. | |
| 126 | guard named == found.first?.range, !model.srcBlocks.contains(where: { $0.name == name }) else { | |
| 124 | 127 | throw Elisp.Unsupported(what: "org-sbe with a name used more than once") |
| 125 | 128 | } |
| 126 | 129 | do { |
| @@ -193,7 +196,7 @@ extension Babel { | ||
| 193 | 196 | lisp.syntax = .emacsLisp |
| 194 | 197 | lisp.standardTime() |
| 195 | 198 | result = try lisp.eval(try LispReader.readFirst(form).sexp) |
| 196 | if Elisp.containsFunction(result) { throw Elisp.Unsupported(what: "org-sbe with a function value") } | |
| 199 | if Elisp.containsClosure(result) { throw Elisp.Unsupported(what: "org-sbe with a function value") } | |
| 197 | 200 | if scalar { |
| 198 | 201 | result = .string(params.contains("scalar") || params.contains("verbatim") ? Elisp.printed(result, escape: true) : try Elisp.format([.string("%s"), result])) |
| 199 | 202 | } |
Sources/OrgCore/Config/Elisp.swift +15 −16
| @@ -49,7 +49,7 @@ public final class Elisp { | ||
| 49 | 49 | /// Variables bound dynamically when `lexical` is set. |
| 50 | 50 | var specials: Set<String> = [] |
| 51 | 51 | /// Under lexical binding, running a lambda list, which Emacs evaluates with dynamic binding: |
| 52 | /// every variable is bound dynamically and lambdas aren't closures. | |
| 52 | /// every variable is bound dynamically and lambdas capture nothing. | |
| 53 | 53 | private var dynamicOnly = false |
| 54 | 54 | /// The functions `defun` defined, which may take closures. |
| 55 | 55 | var defined: Set<String> = [] |
| @@ -148,11 +148,19 @@ public final class Elisp { | ||
| 148 | 148 | scopes[0].vars[name] = v |
| 149 | 149 | } |
| 150 | 150 | |
| 151 | /// The ids in `closures` of functions made under dynamic binding, which capture nothing. | |
| 152 | var dynamicClosures: Set<Int> = [] | |
| 153 | ||
| 151 | 154 | /// A lambda as `function` makes it: a closure over the local frames under lexical |
| 152 | /// binding, else the list itself. | |
| 155 | /// binding, else one that captures nothing. Emacs 31 makes both function objects, not lists. | |
| 153 | 156 | func closure(_ lambda: Sexp) -> Sexp { |
| 154 | guard lexical, !dynamicOnly, case .list(let items) = lambda, items.first == .symbol("lambda"), items.count >= 2 else { return lambda } | |
| 155 | closures.append(Array(scopes.dropFirst())) | |
| 157 | guard case .list(let items) = lambda, items.first == .symbol("lambda"), items.count >= 2 else { return lambda } | |
| 158 | if lexical, !dynamicOnly { | |
| 159 | closures.append(Array(scopes.dropFirst())) | |
| 160 | } else { | |
| 161 | closures.append([]) | |
| 162 | dynamicClosures.insert(closures.count - 1) | |
| 163 | } | |
| 156 | 164 | return .list([.symbol("closure"), .integer(closures.count - 1)] + items.dropFirst()) |
| 157 | 165 | } |
| 158 | 166 | |
| @@ -166,7 +174,7 @@ public final class Elisp { | ||
| 166 | 174 | static let closureArguments: [String: Int] = [ |
| 167 | 175 | "funcall": 0, "apply": 0, "mapcar": 0, "mapc": 0, "mapconcat": 0, "seq-map": 0, "seq-filter": 0, "seq-remove": 0, |
| 168 | 176 | "seq-reduce": 0, "cl-reduce": 0, "cl-remove-if": 0, "cl-remove-if-not": 0, "cl-find": 1, "cl-position": 1, "sort": 0, |
| 169 | "replace-regexp-in-string": 1, "list": 0, "identity": 0, "ignore": 0, "null": 0, "not": 0, | |
| 177 | "replace-regexp-in-string": 1, "org-lookup-first": 3, "org-lookup-last": 3, "org-lookup-all": 3, "list": 0, "identity": 0, "ignore": 0, "null": 0, "not": 0, | |
| 170 | 178 | ] |
| 171 | 179 | /// Builtins that compare or print what lists hold. |
| 172 | 180 | static let closureInspectors: Set<String> = [ |
| @@ -194,17 +202,6 @@ public final class Elisp { | ||
| 194 | 202 | } |
| 195 | 203 | } |
| 196 | 204 | |
| 197 | /// Whether `v` contains a closure or a `(lambda ...)` list, which Emacs 31 prints as an | |
| 198 | /// interpreted-function object. | |
| 199 | static func containsFunction(_ v: Sexp) -> Bool { | |
| 200 | switch v { | |
| 201 | case .list(let items): isClosure(v) || items.first == .symbol("lambda") || items.contains(where: containsFunction) | |
| 202 | case .dotted(let items, let last): items.first == .symbol("lambda") || items.contains(where: containsFunction) || containsFunction(last) | |
| 203 | case .vector(let items): items.contains(where: containsFunction) | |
| 204 | default: false | |
| 205 | } | |
| 206 | } | |
| 207 | ||
| 208 | 205 | /// The variable named by the first argument of the function being called from `eval`, read |
| 209 | 206 | /// once. |
| 210 | 207 | func takeInPlaceVariable() -> String? { |
| @@ -338,6 +335,7 @@ public final class Elisp { | ||
| 338 | 335 | case .string(let s): return escape ? "\"" + s.replacingOccurrences(of: "\\", with: "\\\\").replacingOccurrences(of: "\"", with: "\\\"") + "\"" : s |
| 339 | 336 | case .float(let d): return floatText(d) |
| 340 | 337 | case .character(let c): return String(c) |
| 338 | case .list(let items) where items.isEmpty: return "nil" | |
| 341 | 339 | case .list(let items): return "(" + items.map { printed($0, escape: escape) }.joined(separator: " ") + ")" |
| 342 | 340 | case .dotted(let items, let last): return "(" + items.map { printed($0, escape: escape) }.joined(separator: " ") + " . " + printed(last, escape: escape) + ")" |
| 343 | 341 | case .vector(let items): return "[" + items.map { printed($0, escape: escape) }.joined(separator: " ") + "]" |
| @@ -583,6 +581,7 @@ public final class Elisp { | ||
| 583 | 581 | case .symbol(let name): return try callNamed(name, args) |
| 584 | 582 | case .list(let items) where Self.isClosure(function): |
| 585 | 583 | guard closures.indices.contains(items[1].integer!) else { throw Unsupported(what: "a closure as a list") } |
| 584 | if dynamicClosures.contains(items[1].integer!) { return try call(.list([.symbol("lambda")] + items.dropFirst(2)), args) } | |
| 586 | 585 | let saved = dynamicOnly |
| 587 | 586 | dynamicOnly = false |
| 588 | 587 | defer { dynamicOnly = saved } |
Sources/OrgCore/Links/Links.swift +1 −1
| @@ -281,7 +281,7 @@ extension Links { | ||
| 281 | 281 | EmacsBuffer.defineLookups(lisp) |
| 282 | 282 | do { |
| 283 | 283 | let value = try lisp.eval(try LispReader.readFirst(form).sexp) |
| 284 | if Elisp.containsFunction(value) { return "This link uses Emacs Lisp that runs only in Emacs." } | |
| 284 | if Elisp.containsClosure(value) { return "This link uses Emacs Lisp that runs only in Emacs." } | |
| 285 | 285 | return "\(form) => \(try Elisp.format([.string("%s"), value]))" |
| 286 | 286 | } catch is Elisp.Unsupported { |
| 287 | 287 | return "This link uses Emacs Lisp that runs only in Emacs." |
Tests/OrgCoreTests/ElispLinkTests.swift +4 −2
| @@ -7,7 +7,7 @@ struct ElispLinkTests { | ||
| 7 | 7 | static let forms = [ |
| 8 | 8 | "(+ 1 2)", "(list 1 \"a\" 'b 2.5)", "(concat \"a\" \"b\")", "(message \"hi\")", "(let ((x 2)) (* x x))", |
| 9 | 9 | "(mapcar (lambda (n) (* n 10)) '(1 2 3))", "nil", "(car 1)", "(/ 1 0)", "(error \"Nope %d\" 3)", "(format \"%S\" \"q\")", |
| 10 | "(org-lookup-first 2 '(1 2 3) '(a b c))", "(org-lookup-all 1 '(1 2 1) '(x y z))", | |
| 10 | "(org-lookup-first 2 '(1 2 3) '(a b c))", "(org-lookup-all 1 '(1 2 1) '(x y z))", "(format \"%S\" '(lambda () 1))", | |
| 11 | 11 | ] |
| 12 | 12 | |
| 13 | 13 | @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil)) |
| @@ -26,6 +26,8 @@ struct ElispLinkTests { | ||
| 26 | 26 | @Test func refusesWhatOnlyEmacsHas() { |
| 27 | 27 | #expect(Links.evaluateElisp("(org-agenda nil \"a\")") == "This link uses Emacs Lisp that runs only in Emacs.") |
| 28 | 28 | // Emacs 31 prints a function made under dynamic binding as `#[nil (1) nil]`. |
| 29 | #expect(Links.evaluateElisp("(let ((f (lambda () 1))) f)") == "This link uses Emacs Lisp that runs only in Emacs.") | |
| 29 | for form in ["(let ((f (lambda () 1))) f)", "(prin1-to-string (lambda () 1))", "(format \"%S\" (lambda () 1))", "(car (lambda () 1))"] { | |
| 30 | #expect(Links.evaluateElisp(form) == "This link uses Emacs Lisp that runs only in Emacs.", "\(form)") | |
| 31 | } | |
| 30 | 32 | } |
| 31 | 33 | } |
Tests/OrgCoreTests/TableSbeTests.swift +13 −3
| @@ -85,6 +85,12 @@ struct TableSbeTests { | ||
| 85 | 85 | + "| t | yes | no | cm | orph |\n|---+-----+----+----+------|\n| | | | | |\n" |
| 86 | 86 | + "#+TBLFM: $1='(org-sbe lext)::$2='(org-sbe lexy)::$3='(org-sbe dyn)::$4='(org-sbe cm)::$5='(org-sbe orph)\n\n" |
| 87 | 87 | + "* COMMENT Old\n#+NAME: cm\n| old |\n\n* New\n#+NAME: cm\n#+begin_src emacs-lisp\n\"new\"\n#+end_src\n", |
| 88 | // A quoted lambda stays a list, a name in another case names nothing, and a lambda | |
| 89 | // can be a lookup's predicate. | |
| 90 | "#+NAME: quo\n#+begin_src emacs-lisp :lexical yes\n(list 1 '(lambda () 1))\n#+end_src\n\n" | |
| 91 | + "| q | up | lk |\n|---+----+----|\n| | | |\n" | |
| 92 | + "#+TBLFM: $1='(org-sbe quo)::$2='(org-sbe x)::$3='(org-lookup-first 2 '(1 2 3) '(a b c) (lambda (a b) (= a b)))\n\n" | |
| 93 | + "#+NAME: X\n| up |\n", | |
| 88 | 94 | ] |
| 89 | 95 | |
| 90 | 96 | @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil)) |
| @@ -113,15 +119,19 @@ struct TableSbeTests { | ||
| 113 | 119 | return reason |
| 114 | 120 | } |
| 115 | 121 | |
| 116 | /// Values Emacs 31 prints as interpreted-function objects, `:lexical` values Emacs reads as | |
| 122 | /// Values Emacs 31 prints or reads as interpreted-function objects, `:lexical` values Emacs reads as | |
| 117 | 123 | /// Lisp, and a name whose first use is skipped and that doesn't name a src block go to Emacs. |
| 118 | 124 | @Test func refusesWhatPrintsOrReadsDifferently() { |
| 119 | 125 | let lambda = "#+NAME: lam\n#+begin_src emacs-lisp\n(lambda () 1)\n#+end_src\n\n" |
| 120 | let quoted = "#+NAME: lam\n#+begin_src emacs-lisp :lexical yes\n(list 1 '(lambda () 1))\n#+end_src\n\n" | |
| 126 | let printed = "#+NAME: lam\n#+begin_src emacs-lisp\n(format \"%S\" (lambda () 1))\n#+end_src\n\n" | |
| 127 | let car = "#+NAME: lam\n#+begin_src emacs-lisp\n(car (lambda () 1))\n#+end_src\n\n" | |
| 121 | 128 | let lexical = "#+NAME: lam\n#+begin_src emacs-lisp :lexical (identity t)\n1\n#+end_src\n\n" |
| 122 | 129 | let twice = "* COMMENT Old\n#+NAME: lam\n| a |\n\n* New\n#+NAME: lam\n| b |\n\n" |
| 123 | 130 | #expect(Self.reason(lambda, "'(org-sbe lam)") == "Lisp org-sbe with a function value") |
| 124 | #expect(Self.reason(quoted, "'(org-sbe lam)") == "Lisp org-sbe with a function value") | |
| 131 | #expect(Self.reason(printed, "'(org-sbe lam)") == "Lisp format on a closure") | |
| 132 | #expect(Self.reason(car, "'(org-sbe lam)") == "Lisp car on a closure") | |
| 133 | #expect(Self.reason("", "'(format \"%S\" (lambda () 1))") == "Lisp format on a closure") | |
| 134 | #expect(Self.reason("", "'(lambda () 1)") == "Lisp formula with a function value") | |
| 125 | 135 | #expect(Self.reason(lexical, "'(org-sbe lam)") == "Lisp org-sbe with this :lexical") |
| 126 | 136 | #expect(Self.reason(twice, "'(org-sbe lam)") == "Lisp org-sbe with a name used more than once") |
| 127 | 137 | #expect(Self.reason("#+NAME: lam\n| a |\n\n", "'(org-sbe lam)") == nil) |
docs/manual/guide/10-tables.org +6 −3
| @@ -570,7 +570,10 @@ when the error isn't one of =wrong-type-argument=, =args-out-of-range=, =arith-e | ||
| 570 | 570 | widths; other codes need Emacs, as do float times in =time-add= and =time-subtract=. =date-to-time= and =days-between= read dates as |
| 571 | 571 | =YYYY-MM-DD=, with an optional =HH:MM= or =HH:MM:SS=. |
| 572 | 572 | |
| 573 | Any other function, or a macro not listed, needs Emacs. An error inside a Lisp formula | |
| 573 | Any other function, or a macro not listed, needs Emacs. As in Emacs 31, =lambda= and | |
| 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 | |
| 576 | function that calls it, doesn't. An error inside a Lisp formula | |
| 574 | 577 | writes =#ERROR= in the field, as does a Calc error. |
| 575 | 578 | |
| 576 | 579 | =org-sbe= writes the value of a named src block, run with the variables given: |
| @@ -590,7 +593,7 @@ writes =#ERROR= in the field, as does a Calc error. | ||
| 590 | 593 | As in Org, a field's text is read as a value, so =abc= names another block or table; |
| 591 | 594 | write =$$1= instead of =$1= to pass the text as a string. A string before the variables |
| 592 | 595 | holds header arguments, and the name may also be a named table or list in the file. As in |
| 593 | Org, a name under a =COMMENT= heading is skipped. =org-sbe= needs Pro's source blocks | |
| 596 | Org, a name under a =COMMENT= heading is skipped, and the name's case must match. =org-sbe= needs Pro's source blocks | |
| 594 | 597 | feature. |
| 595 | 598 | |
| 596 | 599 | Emacs Lisp blocks run in orgstar's interpreter, with dynamic binding unless the block |
| @@ -599,7 +602,7 @@ without asking, since neither can reach files or other programs. A =js= block th | ||
| 599 | 602 | longer than five seconds is stopped, and the table needs Emacs. Blocks in other languages, blocks with =:cache yes=, |
| 600 | 603 | =:file= results or column and row names, other =:lexical= values, values that hold a |
| 601 | 604 | function, Lisp values with symbols in a variable with several values, a name given to |
| 602 | more than one element when the first isn't a src block, references to an =ID= or | |
| 605 | more than one element, in any case, when the first isn't a src block, references to an =ID= or | |
| 603 | 606 | =CUSTOM_ID=, and =js= values that are arrays need Emacs. When Emacs runs a table in a file |
| 604 | 607 | that uses =org-sbe=, it runs the blocks without asking again, so orgstar asks |
| 605 | 608 | =This table's formulas run Lisp and source blocks. Run them? (yes, no)= each time. |