Sources/OrgPrograms/Programs.swift
64 lines · 3584 bytes
4 symbols in this file
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 where job.diagram != nil:
28 // `dot` and `mmdc` when they're installed, as Emacs runs them; a web view otherwise.
29 if !BabelRunner.installed(job.language == "dot" ? "dot" : "mmdc"), let output = try await DiagramBlocks.run(job, directory: fileDirectory) { return output }
30 case .process:
31 break
32 case .emacs where job.calc != nil:
33 // calc natively, and in Emacs what Calc here refuses.
34 do {
35 return try Babel.evaluateCalc(job)
36 } catch let refusal as Calc.Unsupported {
37 guard emacs() != nil else { throw BabelRunner.Failure(description: TableReason.calcRefusal(refusal.reason)) }
38 }
39 let output = try await BabelRunner.run(job, fileDirectory: fileDirectory)
40 if output.status != 0 { throw BabelRunner.Failure(description: output.stderr.trimmingCharacters(in: .whitespacesAndNewlines)) }
41 return output
42 case .emacs:
43 // Emacs Lisp in `Elisp` when Emacs isn't installed.
44 if emacs() == nil {
45 guard let output = try Babel.evaluateNatively(job) else {
46 throw BabelRunner.Failure(description: "This block uses Emacs Lisp that runs only in Emacs, and Emacs isn't installed.")
47 }
48 return output
49 }
50 }
51 return try await BabelRunner.run(job, fileDirectory: fileDirectory)
52 }
53
54 public func recalculateTable(text: String, directory: URL, table: Range<Int>, point: Int, form: String) async throws -> String {
55 try await EmacsRunner.recalculateTable(text: text, directory: directory, table: table, point: point, form: form)
56 }
57
58 /// `elisp:` links in Emacs; nil without it, so they run in `Elisp`.
59 public func evaluateLisp(_ form: String, text: String, point: Int, directory: URL) async throws -> String? {
60 guard emacs() != nil else { return nil }
61 return try await EmacsRunner.evaluate(form, text: text, point: point, directory: directory)
62 }
63}
64#endif