Sources/OrgApp/ProgramRunning.swift

28 lines · 1374 bytes

 1import Foundation
 2import OrgCore
 3
 4/// Runs Babel jobs and Emacs as processes, for an app allowed to start them.
 5public protocol ProgramRunning: Sendable {
 6    /// Runs `job` in `fileDirectory`, or its `:dir`.
 7    func runBabel(_ job: BabelJob, fileDirectory: URL) async throws -> BabelOutput
 8    /// Runs `form` in Emacs on a copy of `text`; returns the table that starts at `table.lowerBound`.
 9    func recalculateTable(text: String, directory: URL, table: Range<Int>, point: Int, form: String) async throws -> String
10    /// Evaluates `form` in Emacs, in a copy of `text` with point at `point`, and returns its
11    /// value as `%s` prints it; nil when there is no Emacs to run.
12    func evaluateLisp(_ form: String, text: String, point: Int, directory: URL) async throws -> String?
13}
14
15extension ProgramRunning {
16    public func evaluateLisp(_ form: String, text: String, point: Int, directory: URL) async throws -> String? { nil }
17}
18
19extension DocumentSession {
20    /// The runner for programs, set at launch by an app that may start them. Without one,
21    /// only what runs natively runs.
22    public static var programs: (any ProgramRunning)?
23
24    /// Overrides `programs` for the current task and the tasks it starts.
25    @TaskLocal public static var scopedPrograms: (any ProgramRunning)?
26
27    static var runner: (any ProgramRunning)? { scopedPrograms ?? programs }
28}