Commit bd76469f57

bd76469f57536b705cbb6f7a4c2aa17f8bbb1448

parent: e1afdafd04

Verified · cmc

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

Ask each time and require Babel when Emacs runs org-sbe blocks; load ob-fortran

Layout: unified · split

Sources/OrgPrograms/EmacsRunner.swift +8 −3
@@ -36,11 +36,10 @@ public enum EmacsRunner {
36 let script = folder.appendingPathComponent("recalc.el") 36 let script = folder.appendingPathComponent("recalc.el")
37 try text.write(to: input, atomically: true, encoding: .utf8) 37 try text.write(to: input, atomically: true, encoding: .utf8)
38 func lisp(_ s: String) -> String { "\"" + s.replacingOccurrences(of: "\\", with: "\\\\").replacingOccurrences(of: "\"", with: "\\\"") + "\"" } 38 func lisp(_ s: String) -> String { "\"" + s.replacingOccurrences(of: "\\", with: "\\\\").replacingOccurrences(of: "\"", with: "\\\"") + "\"" }
39 // `org-sbe` runs blocks: the user agreed to the table's Lisp, and Babel needs the languages. 39 // `org-sbe` runs blocks: the user agreed to the table and its blocks, and Babel needs the languages.
40 let blocks = text.contains("org-sbe") ? """ 40 let blocks = text.contains("org-sbe") ? """
41 (setq org-confirm-babel-evaluate nil) 41 (setq org-confirm-babel-evaluate nil)
42 (dolist (language '(C awk clojure java js python R ruby shell)) 42 \(sbeLanguages)
43 (require (intern (format "ob-%s" language)) nil t))
44 """ : "" 43 """ : ""
45 try """ 44 try """
46 ;;; -*- lexical-binding: t -*- 45 ;;; -*- lexical-binding: t -*-
@@ -89,6 +88,12 @@ public enum EmacsRunner {
89 return String(decoding: data, as: UTF8.self) 88 return String(decoding: data, as: UTF8.self)
90 } 89 }
91 90
91 /// Loads the Babel languages orgstar runs, for the blocks `org-sbe` runs in Emacs.
92 static let sbeLanguages = """
93 (dolist (language '(C awk clojure fortran java js python R ruby shell))
94 (require (intern (format "ob-%s" language)) nil t))
95 """
96
92 /// Evaluates `form` as `org-link--open-elisp` does, in a copy of `text` with point at 97 /// Evaluates `form` as `org-link--open-elisp` does, in a copy of `text` with point at
93 /// `point` and `directory` as its directory; returns the value as `%s` prints it, or throws 98 /// `point` and `directory` as its directory; returns the value as `%s` prints it, or throws
94 /// the error's message. 99 /// the error's message.
Sources/Orgstar/ContentView.swift +14 −2
@@ -1068,6 +1068,10 @@ struct ContentView: View {
1068 case .emacsTable(_, _, _, let reason, _, let formula, let partial): 1068 case .emacsTable(_, _, _, let reason, _, let formula, let partial):
1069 // The fields written before the formula stay, as Emacs leaves them when a formula fails. 1069 // The fields written before the formula stay, as Emacs leaves them when a formula fails.
1070 if let partial { commands.editor?.perform(PrecomputedEdit(partial)) } 1070 if let partial { commands.editor?.perform(PrecomputedEdit(partial)) }
1071 if reason == TableBlocks.lockedReason {
1072 ProAccess.shared.allows(.babel)
1073 return
1074 }
1071 if let formula { 1075 if let formula {
1072 commands.show(String(localized: "The table was recalculated up to \(formula), which can't be calculated here: \(TableReason.localized(reason)).", comment: "Table formulas orgstar calculated up to one it can't")) 1076 commands.show(String(localized: "The table was recalculated up to \(formula), which can't be calculated here: \(TableReason.localized(reason)).", comment: "Table formulas orgstar calculated up to one it can't"))
1073 } else { 1077 } else {
@@ -1075,6 +1079,13 @@ struct ContentView: View {
1075 } 1079 }
1076 #else 1080 #else
1077 case .emacsTable(let table, let point, let form, let reason, let lisp, let formula, let partial): 1081 case .emacsTable(let table, let point, let form, let reason, let lisp, let formula, let partial):
1082 // Emacs runs the blocks `org-sbe` names without asking, so they need Babel, and
1083 // the question below each time.
1084 let blocks = reason == TableBlocks.lockedReason || session.document?.text.contains("org-sbe") == true
1085 if blocks, !ProAccess.shared.allows(.babel) {
1086 if let partial { commands.editor?.perform(PrecomputedEdit(partial)) }
1087 return
1088 }
1078 guard EmacsRunner.executable() != nil else { 1089 guard EmacsRunner.executable() != nil else {
1079 // Without Emacs this is the App Store build's case: keep the fields written so far. 1090 // Without Emacs this is the App Store build's case: keep the fields written so far.
1080 if let partial { commands.editor?.perform(PrecomputedEdit(partial)) } 1091 if let partial { commands.editor?.perform(PrecomputedEdit(partial)) }
@@ -1089,8 +1100,9 @@ struct ContentView: View {
1089 } 1100 }
1090 guard lisp, let path = session.path, let text = session.document?.text else { return run() } 1101 guard lisp, let path = session.path, let text = session.document?.text else { return run() }
1091 let content = (text as NSString).substring(with: NSRange(location: table.lowerBound, length: min((text as NSString).length, table.upperBound + 2000) - table.lowerBound)) 1102 let content = (text as NSString).substring(with: NSRange(location: table.lowerBound, length: min((text as NSString).length, table.upperBound + 2000) - table.lowerBound))
1092 if trust.isTrusted(path: path, content: content) { return run() } 1103 if !blocks, trust.isTrusted(path: path, content: content) { return run() }
1093 commands.ask(Prompt(key: "confirm", message: "This table's formulas run Lisp. Run them? (yes, no, always)", choices: ["yes", "no", "always"])) { answer in 1104 let message = blocks ? "This table's formulas run Lisp and source blocks. Run them? (yes, no)" : "This table's formulas run Lisp. Run them? (yes, no, always)"
1105 commands.ask(Prompt(key: "confirm", message: message, choices: blocks ? ["yes", "no"] : ["yes", "no", "always"])) { answer in
1094 switch answer { 1106 switch answer {
1095 case "always": 1107 case "always":
1096 trust.trust(path: path, content: content) 1108 trust.trust(path: path, content: content)
Sources/Orgstar/OrgstarApp.swift +2
@@ -384,6 +384,8 @@ struct OrgstarApp: App {
384 MacPro.resolve() 384 MacPro.resolve()
385 MacPro.renewInBackground() 385 MacPro.renewInBackground()
386 #endif 386 #endif
387 // `org-sbe` in table formulas runs blocks, so it needs Babel as source blocks do.
388 TableBlocks.allowed = { ProAccess.isUnlockedNow }
387 // Debug builds, and runs with ORGSTAR_PERF set: main-thread stalls in the console. 389 // Debug builds, and runs with ORGSTAR_PERF set: main-thread stalls in the console.
388 #if DEBUG 390 #if DEBUG
389 Perf.watchMainThread() 391 Perf.watchMainThread()
Sources/OrgstarMobile/MobileEditor.swift +5
@@ -337,6 +337,11 @@ final class MobileEditor: NSObject, DocumentBuffer {
337 // The fields written before a formula only Emacs can do stay, as Emacs leaves them 337 // The fields written before a formula only Emacs can do stay, as Emacs leaves them
338 // when a formula fails. 338 // when a formula fails.
339 if case .emacsTable(_, _, _, _, _, _, let partial?) = request { _ = perform(PrecomputedEdit(partial)) } 339 if case .emacsTable(_, _, _, _, _, _, let partial?) = request { _ = perform(PrecomputedEdit(partial)) }
340 // `org-sbe` without Babel shows its paywall.
341 if case .emacsTable(_, _, _, TableBlocks.lockedReason, _, _, _) = request {
342 ProAccess.shared.allows(.babel)
343 return step
344 }
340 onMessage?(Self.unavailable(request, title: command.title)) 345 onMessage?(Self.unavailable(request, title: command.title))
341 } 346 }
342 return step 347 return step
Sources/OrgstarMobile/MobileModel.swift +2
@@ -72,6 +72,8 @@ final class MobileModel {
72 UserConfig.directoryOverride = folder 72 UserConfig.directoryOverride = folder
73 let (problems, colors) = Self.readConfig(folder) 73 let (problems, colors) = Self.readConfig(folder)
74 let settings = OrgPreferences.orgSettings() 74 let settings = OrgPreferences.orgSettings()
75 // `org-sbe` in table formulas runs blocks, so it needs Babel as source blocks do.
76 TableBlocks.allowed = { ProAccess.isUnlockedNow }
75 do { 77 do {
76 let workspace = try WorkspaceModel(directory: WorkspaceModel.defaultDirectory, settings: .with(todoKeywords: settings)) 78 let workspace = try WorkspaceModel(directory: WorkspaceModel.defaultDirectory, settings: .with(todoKeywords: settings))
77 // Recently Deleted where the folder's provider keeps one (iCloud Drive); otherwise gone. 79 // Recently Deleted where the folder's provider keeps one (iCloud Drive); otherwise gone.
Tests/OrgProgramsTests/ProgramsTests.swift +16
@@ -58,6 +58,22 @@ struct EmacsFallbackTests {
58 } 58 }
59 } 59 }
60 60
61 /// Emacs can run each compiled language and interpreter orgstar runs, for `org-sbe`.
62 @Test(.enabled(if: EmacsRunner.executable() != nil))
63 func loadsEveryLanguageForOrgSbe() throws {
64 let languages = ["C", "C++", "D", "fortran", "java", "clojure", "awk", "js", "python", "R", "ruby", "sh", "bash"]
65 let process = Process()
66 process.executableURL = EmacsRunner.executable()
67 process.arguments = ["-Q", "--batch", "--eval", "(progn (require 'org) " + EmacsRunner.sbeLanguages
68 + " (princ (mapconcat (lambda (l) (if (fboundp (intern (concat \"org-babel-execute:\" l))) \"\" l)) '(" + languages.map { "\"\($0)\"" }.joined(separator: " ") + ") \"\")))"]
69 let out = Pipe()
70 process.standardOutput = out
71 process.standardError = FileHandle.nullDevice
72 try process.run()
73 process.waitUntilExit()
74 #expect(String(decoding: out.fileHandleForReading.readDataToEndOfFile(), as: UTF8.self) == "")
75 }
76
61 /// `org-sbe` on a block only Emacs runs: Emacs runs it without asking again. 77 /// `org-sbe` on a block only Emacs runs: Emacs runs it without asking again.
62 @Test(.enabled(if: EmacsRunner.executable() != nil && ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil)) 78 @Test(.enabled(if: EmacsRunner.executable() != nil && ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
63 func runsOtherLanguagesForOrgSbe() async throws { 79 func runsOtherLanguagesForOrgSbe() async throws {