Sources/OrgPrograms/Programs.swift

586bad784d2cb2ad1bb43da0e63f0819913ed1dc
orgstar/Sources/OrgPrograms/Programs.swift

43 lines · 2037 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    /// `elisp:` links in Emacs; nil without it, so they run in `Elisp`.
38    public func evaluateLisp(_ form: String, text: String, point: Int, directory: URL) async throws -> String? {
39        guard emacs() != nil else { return nil }
40        return try await EmacsRunner.evaluate(form, text: text, point: point, directory: directory)
41    }
42}
43#endif
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form