Commit 21d1bae1db

21d1bae1dbfcaa2ed62e78ab08bd6ef6a15b89c6

parent: 8a70221722

Verified · cmc

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

Refuse functions from elisp: links and give them the org-lookup functions

Layout: unified · split

Sources/OrgCore/Config/Elisp.swift +11
@@ -194,6 +194,17 @@ public final class Elisp {
194 } 194 }
195 } 195 }
196 196
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
197 /// The variable named by the first argument of the function being called from `eval`, read 208 /// The variable named by the first argument of the function being called from `eval`, read
198 /// once. 209 /// once.
199 func takeInPlaceVariable() -> String? { 210 func takeInPlaceVariable() -> String? {
Sources/OrgCore/Links/Links.swift +2 −1
@@ -278,9 +278,10 @@ extension Links {
278 public static func evaluateElisp(_ form: String) -> String { 278 public static func evaluateElisp(_ form: String) -> String {
279 let lisp = Elisp() 279 let lisp = Elisp()
280 lisp.standardTime() 280 lisp.standardTime()
281 EmacsBuffer.defineLookups(lisp)
281 do { 282 do {
282 let value = try lisp.eval(try LispReader.readFirst(form).sexp) 283 let value = try lisp.eval(try LispReader.readFirst(form).sexp)
283 if Elisp.containsClosure(value) { return "This link uses Emacs Lisp that runs only in Emacs." } 284 if Elisp.containsFunction(value) { return "This link uses Emacs Lisp that runs only in Emacs." }
284 return "\(form) => \(try Elisp.format([.string("%s"), value]))" 285 return "\(form) => \(try Elisp.format([.string("%s"), value]))"
285 } catch is Elisp.Unsupported { 286 } catch is Elisp.Unsupported {
286 return "This link uses Emacs Lisp that runs only in Emacs." 287 return "This link uses Emacs Lisp that runs only in Emacs."
Tests/OrgCoreTests/ElispLinkTests.swift +3
@@ -7,6 +7,7 @@ struct ElispLinkTests {
7 static let forms = [ 7 static let forms = [
8 "(+ 1 2)", "(list 1 \"a\" 'b 2.5)", "(concat \"a\" \"b\")", "(message \"hi\")", "(let ((x 2)) (* x x))", 8 "(+ 1 2)", "(list 1 \"a\" 'b 2.5)", "(concat \"a\" \"b\")", "(message \"hi\")", "(let ((x 2)) (* x x))",
9 "(mapcar (lambda (n) (* n 10)) '(1 2 3))", "nil", "(car 1)", "(/ 1 0)", "(error \"Nope %d\" 3)", "(format \"%S\" \"q\")", 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 ] 11 ]
11 12
12 @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil)) 13 @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
@@ -24,5 +25,7 @@ struct ElispLinkTests {
24 25
25 @Test func refusesWhatOnlyEmacsHas() { 26 @Test func refusesWhatOnlyEmacsHas() {
26 #expect(Links.evaluateElisp("(org-agenda nil \"a\")") == "This link uses Emacs Lisp that runs only in Emacs.") 27 #expect(Links.evaluateElisp("(org-agenda nil \"a\")") == "This link uses Emacs Lisp that runs only in Emacs.")
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.")
27 } 30 }
28} 31}