Sources/OrgPrograms/Programs.swift
cmc Run Emacs Lisp natively when Emacs is missing; translate the new Lisp reasons
db6552840c 6 days ago37 lines · 1695 bytes
1#if os(macOS)
2import Foundation
3import OrgApp
4import OrgCore
5
6/// Babel and Emacs as processes, for `DocumentSession.programs`.
7public struct Programs: ProgramRunning {
8 /// Where Emacs is, or nil when it isn't installed.
9 let emacs: @Sendable () -> URL?
10
11 public init(emacs: @escaping @Sendable () -> URL? = { EmacsRunner.executable() }) {
12 self.emacs = emacs
13 }
14
15 public func runBabel(_ job: BabelJob, fileDirectory: URL) async throws -> BabelOutput {
16 // js in JavaScriptCore when node (or `:cmd`) isn't installed. A `:cmd` with arguments runs
17 // as written, since JavaScriptCore can't take node's options.
18 switch job.runner {
19 case .process(let executable, _), .session(_, let executable, _):
20 if !executable.contains(where: \.isWhitespace), !BabelRunner.installed(executable), let output = try await JavaScriptBlocks.run(job, directory: fileDirectory) { return output }
21 case .emacs:
22 // Emacs Lisp in `Elisp` when Emacs isn't installed.
23 if emacs() == nil {
24 guard let output = try Babel.evaluateNatively(job) else {
25 throw BabelRunner.Failure(description: "This block uses Emacs Lisp that runs only in Emacs, and Emacs isn't installed.")
26 }
27 return output
28 }
29 }
30 return try await BabelRunner.run(job, fileDirectory: fileDirectory)
31 }
32
33 public func recalculateTable(text: String, directory: URL, table: Range<Int>, point: Int, form: String) async throws -> String {
34 try await EmacsRunner.recalculateTable(text: text, directory: directory, table: table, point: point, form: form)
35 }
36}
37#endif