#if os(macOS) import Foundation import OrgApp import OrgCore /// Babel and Emacs as processes, for `DocumentSession.programs`. public struct Programs: ProgramRunning { /// Where Emacs is, or nil when it isn't installed. let emacs: @Sendable () -> URL? public init(emacs: @escaping @Sendable () -> URL? = { EmacsRunner.executable() }) { self.emacs = emacs } public func runBabel(_ job: BabelJob, fileDirectory: URL) async throws -> BabelOutput { // js in JavaScriptCore when node (or `:cmd`) isn't installed. A `:cmd` with arguments runs // as written, since JavaScriptCore can't take node's options. switch job.runner { case .process(let executable, _) where job.language == "js" || job.language == "javascript": let words = BabelRunner.words(executable) if words.count == 1, !BabelRunner.installed(words[0]), let output = try await JavaScriptBlocks.run(job, directory: fileDirectory) { return output } case .session(_, let executable, _): if !executable.contains(where: \.isWhitespace), !BabelRunner.installed(executable), let output = try await JavaScriptBlocks.run(job, directory: fileDirectory) { return output } case .process where job.language == "sqlite": // `sqlite3` when it's installed, as Emacs runs it; the SQLite library otherwise. if !BabelRunner.installed("sqlite3"), let output = try SQLiteBlocks.run(job, directory: fileDirectory) { return output } case .process where job.diagram != nil: // `dot` and `mmdc` when they're installed, as Emacs runs them; a web view otherwise. if !BabelRunner.installed(job.language == "dot" ? "dot" : "mmdc"), let output = try await DiagramBlocks.run(job, directory: fileDirectory) { return output } case .process: break case .emacs where job.calc != nil: // calc natively, and in Emacs what Calc here refuses. do { return try Babel.evaluateCalc(job) } catch let refusal as Calc.Unsupported { guard emacs() != nil else { throw BabelRunner.Failure(description: TableReason.calcRefusal(refusal.reason)) } } let output = try await BabelRunner.run(job, fileDirectory: fileDirectory) if output.status != 0 { throw BabelRunner.Failure(description: output.stderr.trimmingCharacters(in: .whitespacesAndNewlines)) } return output case .emacs: // Emacs Lisp in `Elisp` when Emacs isn't installed. if emacs() == nil { guard let output = try Babel.evaluateNatively(job) else { throw BabelRunner.Failure(description: "This block uses Emacs Lisp that runs only in Emacs, and Emacs isn't installed.") } return output } } return try await BabelRunner.run(job, fileDirectory: fileDirectory) } public func recalculateTable(text: String, directory: URL, table: Range, point: Int, form: String) async throws -> String { try await EmacsRunner.recalculateTable(text: text, directory: directory, table: table, point: point, form: form) } /// `elisp:` links in Emacs; nil without it, so they run in `Elisp`. public func evaluateLisp(_ form: String, text: String, point: Int, directory: URL) async throws -> String? { guard emacs() != nil else { return nil } return try await EmacsRunner.evaluate(form, text: text, point: point, directory: directory) } } #endif