Sources/OrgPrograms/Programs.swift
61 lines · 3269 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, _) where job.language == "js" || job.language == "javascript":
20 let words = BabelRunner.words(executable)
21 if words.count == 1, !BabelRunner.installed(words[0]), let output = try await JavaScriptBlocks.run(job, directory: fileDirectory) { return output }
22 case .session(_, let executable, _):
23 if !executable.contains(where: \.isWhitespace), !BabelRunner.installed(executable), let output = try await JavaScriptBlocks.run(job, directory: fileDirectory) { return output }
24 case .process where job.language == "sqlite":
25 // `sqlite3` when it's installed, as Emacs runs it; the SQLite library otherwise.
26 if !BabelRunner.installed("sqlite3"), let output = try SQLiteBlocks.run(job, directory: fileDirectory) { return output }
27 case .process:
28 break
29 case .emacs where job.calc != nil:
30 // calc natively, and in Emacs what Calc here refuses.
31 do {
32 return try Babel.evaluateCalc(job)
33 } catch let refusal as Calc.Unsupported {
34 guard emacs() != nil else { throw BabelRunner.Failure(description: TableReason.calcRefusal(refusal.reason)) }
35 }
36 let output = try await BabelRunner.run(job, fileDirectory: fileDirectory)
37 if output.status != 0 { throw BabelRunner.Failure(description: output.stderr.trimmingCharacters(in: .whitespacesAndNewlines)) }
38 return output
39 case .emacs:
40 // Emacs Lisp in `Elisp` when Emacs isn't installed.
41 if emacs() == nil {
42 guard let output = try Babel.evaluateNatively(job) else {
43 throw BabelRunner.Failure(description: "This block uses Emacs Lisp that runs only in Emacs, and Emacs isn't installed.")
44 }
45 return output
46 }
47 }
48 return try await BabelRunner.run(job, fileDirectory: fileDirectory)
49 }
50
51 public func recalculateTable(text: String, directory: URL, table: Range<Int>, point: Int, form: String) async throws -> String {
52 try await EmacsRunner.recalculateTable(text: text, directory: directory, table: table, point: point, form: form)
53 }
54
55 /// `elisp:` links in Emacs; nil without it, so they run in `Elisp`.
56 public func evaluateLisp(_ form: String, text: String, point: Int, directory: URL) async throws -> String? {
57 guard emacs() != nil else { return nil }
58 return try await EmacsRunner.evaluate(form, text: text, point: point, directory: directory)
59 }
60}
61#endif