Commit 3070ac3f44
Verified · cmc
Layout: unified · split
Sources/OrgApp/BabelRunner.swift +2 −2
| @@ -34,9 +34,9 @@ public enum BabelRunner { | ||
| 34 | 34 | defer { try? FileManager.default.removeItem(at: folder) } |
| 35 | 35 | let resultFile = folder.appendingPathComponent("result") |
| 36 | 36 | let input = job.input.replacingOccurrences(of: Babel.resultFileToken, with: resultFile.path) |
| 37 | if case .session(let name, let executable, let python) = job.runner { | |
| 37 | if case .session(let name, let executable, let kind) = job.runner { | |
| 38 | 38 | let directory = job.directory.map { URL(fileURLWithPath: $0, relativeTo: fileDirectory).standardizedFileURL } ?? fileDirectory |
| 39 | return try await BabelSessions.shared.run(job, name: name, executable: executable, python: python, directory: directory, resultFile: resultFile) | |
| 39 | return try await BabelSessions.shared.run(job, name: name, executable: executable, kind: kind, directory: directory, resultFile: resultFile) | |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | let process = Process() |
Sources/OrgApp/BabelSessions.swift +55 −5
| @@ -81,24 +81,74 @@ public final class BabelSessions: @unchecked Sendable { | ||
| 81 | 81 | func stop() { if process.isRunning { process.terminate() } } |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | /// The Ruby and Node sessions' loops: each line names a program file, the marker and the | |
| 85 | /// file the value goes to (empty for output), and the program runs where earlier ones left | |
| 86 | /// their variables. The value is written as the non-session wrappers write it. | |
| 87 | static let rubyDriver = #""" | |
| 88 | $stdout.sync = true | |
| 89 | $stderr.sync = true | |
| 90 | __orgstar_binding = binding | |
| 91 | while (__orgstar_line = $stdin.gets) | |
| 92 | __orgstar_file, __orgstar_marker, __orgstar_result = __orgstar_line.chomp.split("\t", 3) | |
| 93 | begin | |
| 94 | __orgstar_value = eval(File.read(__orgstar_file), __orgstar_binding, "org-babel") | |
| 95 | if __orgstar_result && !__orgstar_result.empty? | |
| 96 | File.open(__orgstar_result, "w") { |f| f.write(__orgstar_value.class == String ? __orgstar_value : __orgstar_value.inspect) } | |
| 97 | end | |
| 98 | rescue Exception => __orgstar_error | |
| 99 | $stderr.puts __orgstar_error.full_message(highlight: false) | |
| 100 | end | |
| 101 | $stdout.print "\n#{__orgstar_marker} 0\n" | |
| 102 | end | |
| 103 | """# | |
| 104 | static let nodeDriver = #""" | |
| 105 | const vm = require('vm'), fs = require('fs'), util = require('util'); | |
| 106 | globalThis.require = require; | |
| 107 | require('readline').createInterface({ input: process.stdin }).on('line', line => { | |
| 108 | const [file, marker, result] = line.split('\t'); | |
| 109 | try { | |
| 110 | const value = vm.runInThisContext(fs.readFileSync(file, 'utf8'), { filename: 'org-babel' }); | |
| 111 | if (result) fs.writeFileSync(result, util.inspect(value)); | |
| 112 | } catch (e) { | |
| 113 | process.stderr.write(String(e && e.stack || e) + '\n'); | |
| 114 | } | |
| 115 | process.stdout.write('\n' + marker + ' 0\n'); | |
| 116 | }); | |
| 117 | """# | |
| 118 | ||
| 84 | 119 | /// Runs a session job: the session starts in `directory` the first time. |
| 85 | func run(_ job: BabelJob, name: String, executable: String, python: Bool, directory: URL, resultFile: URL) async throws -> BabelOutput { | |
| 120 | func run(_ job: BabelJob, name: String, executable: String, kind: BabelJob.SessionKind, directory: URL, resultFile: URL) async throws -> BabelOutput { | |
| 86 | 121 | let key = [directory.path, executable, name].joined(separator: "\u{1}") |
| 87 | 122 | let session: Session = try lock.withLock { |
| 88 | 123 | if let existing = sessions[key], existing.process.isRunning { return existing } |
| 89 | let created = try Session(executable: executable, arguments: python ? ["-u", "-i", "-q"] : [], directory: directory) | |
| 124 | let arguments: [String] = switch kind { | |
| 125 | case .shell: [] | |
| 126 | case .python: ["-u", "-i", "-q"] | |
| 127 | case .ruby: ["-e", Self.rubyDriver] | |
| 128 | case .node: ["-e", Self.nodeDriver] | |
| 129 | case .r: ["--slave", "--no-save", "--no-restore"] | |
| 130 | } | |
| 131 | let created = try Session(executable: executable, arguments: arguments, directory: directory) | |
| 90 | 132 | sessions[key] = created |
| 91 | 133 | return created |
| 92 | 134 | } |
| 93 | 135 | let marker = "__orgstar_done_\(UUID().uuidString.replacingOccurrences(of: "-", with: ""))__" |
| 94 | 136 | let program = job.input.replacingOccurrences(of: Babel.resultFileToken, with: resultFile.path) |
| 137 | let file = resultFile.deletingLastPathComponent().appendingPathComponent("program") | |
| 95 | 138 | let code: String |
| 96 | if python { | |
| 97 | let file = resultFile.deletingLastPathComponent().appendingPathComponent("program.py") | |
| 139 | switch kind { | |
| 140 | case .python: | |
| 98 | 141 | try program.write(to: file, atomically: true, encoding: .utf8) |
| 99 | 142 | let path = file.path.replacingOccurrences(of: "'", with: "\\'") |
| 100 | 143 | code = "try:\n exec(compile(open('\(path)').read(), '<org-babel>', 'exec'), globals())\nfinally:\n print('\\n\(marker) 0', flush=True)\n\n" |
| 101 | } else { | |
| 144 | case .ruby, .node: | |
| 145 | try program.write(to: file, atomically: true, encoding: .utf8) | |
| 146 | code = [file.path, marker, job.finish.output ? "" : resultFile.path].joined(separator: "\t") + "\n" | |
| 147 | case .r: | |
| 148 | try program.write(to: file, atomically: true, encoding: .utf8) | |
| 149 | let path = file.path.replacingOccurrences(of: "\\", with: "\\\\").replacingOccurrences(of: "\"", with: "\\\"") | |
| 150 | code = "tryCatch(source(\"\(path)\", echo = FALSE, print.eval = TRUE), error = function(e) message(\"Error: \", conditionMessage(e)))\ncat(\"\\n\(marker) 0\\n\")\n" | |
| 151 | case .shell: | |
| 102 | 152 | code = program + (program.hasSuffix("\n") ? "" : "\n") + "printf '\\n%s %d\\n' \(marker) $?\n" |
| 103 | 153 | } |
| 104 | 154 | let (stdout, stderr, status) = try await withTaskCancellationHandler { |
Sources/OrgCore/Compute/Babel.swift +28 −9
| @@ -13,9 +13,13 @@ public struct BabelJob: Sendable, Equatable { | ||
| 13 | 13 | case process(executable: String, arguments: [String]) |
| 14 | 14 | /// `emacs --batch` loading `input` as a script. |
| 15 | 15 | case emacs |
| 16 | /// A long-running interpreter named by `:session`: a shell gets `input` as commands, Python | |
| 17 | /// runs `input` as a program in its globals. | |
| 18 | case session(name: String, executable: String, python: Bool) | |
| 16 | /// A long-running interpreter named by `:session`: a shell gets `input` as commands, the | |
| 17 | /// others run `input` as a program where earlier ones left their variables. | |
| 18 | case session(name: String, executable: String, kind: SessionKind) | |
| 19 | } | |
| 20 | ||
| 21 | public enum SessionKind: String, Sendable, Equatable { | |
| 22 | case shell, python, ruby, node, r | |
| 19 | 23 | } |
| 20 | 24 | |
| 21 | 25 | public let language: String |
| @@ -60,11 +64,11 @@ public struct BabelJob: Sendable, Equatable { | ||
| 60 | 64 | /// `compiled`: C, C++, D, Java, Fortran and Clojure, by `language`. |
| 61 | 65 | /// `script`: ruby and js, whose value comes back as their `inspect` prints it, from |
| 62 | 66 | /// the result file for ruby and the output for js. |
| 63 | enum Kind: Sendable, Equatable { case shell(valueIsExitStatus: Bool), python, generic, emacsLisp, compiled, script(js: Bool) } | |
| 67 | enum Kind: Sendable, Equatable { case shell(valueIsExitStatus: Bool), python, generic, emacsLisp, compiled, script(js: Bool, session: Bool = false) } | |
| 64 | 68 | let kind: Kind |
| 65 | 69 | var language = "" |
| 66 | 70 | public internal(set) var params: [String] |
| 67 | let output: Bool | |
| 71 | public let output: Bool | |
| 68 | 72 | let wrap: String? |
| 69 | 73 | let name: String? |
| 70 | 74 | /// An inline block: the result goes after it, in `{{{results(…)}}}`. |
| @@ -360,7 +364,7 @@ public enum Babel { | ||
| 360 | 364 | } |
| 361 | 365 | // The compiled languages' `org-babel-execute:` ignore `:session`. |
| 362 | 366 | let session = compiled ? nil : params.single[":session"].flatMap { $0 == "none" ? nil : ($0.isEmpty ? "*\(language)*" : $0) } |
| 363 | if session != nil, !shells.contains(language), language != "python" { | |
| 367 | if session != nil, !shells.contains(language), !["python", "ruby", "js", "javascript", "R"].contains(language) { | |
| 364 | 368 | return .failure(.message(":session isn't supported for \(language) yet; nothing was run.")) |
| 365 | 369 | } |
| 366 | 370 | var body = normalizedBody(block) |
| @@ -450,7 +454,7 @@ public enum Babel { | ||
| 450 | 454 | } |
| 451 | 455 | let full = expandGeneric(body, lines, params) + (valueIsExitStatus ? "\necho $?" : "") |
| 452 | 456 | return .success(BabelJob( |
| 453 | language: language, runner: session.map { .session(name: $0, executable: executable, python: false) } ?? .process(executable: executable, arguments: []), | |
| 457 | language: language, runner: session.map { .session(name: $0, executable: executable, kind: .shell) } ?? .process(executable: executable, arguments: []), | |
| 454 | 458 | input: full.trimmingCharacters(in: .whitespacesAndNewlines) + "\n", directory: dir, |
| 455 | 459 | block: location, blockText: blockText, alwaysAsk: alwaysAsk, |
| 456 | 460 | finish: finishBase(.shell(valueIsExitStatus: valueIsExitStatus)) |
| @@ -477,7 +481,7 @@ public enum Babel { | ||
| 477 | 481 | """ + params.results.map { "\"\($0)\"" }.joined(separator: ", ") + "])\n" |
| 478 | 482 | } |
| 479 | 483 | return .success(BabelJob( |
| 480 | language: language, runner: .session(name: session, executable: params.single[":python"] ?? options.python, python: true), | |
| 484 | language: language, runner: .session(name: session, executable: params.single[":python"] ?? options.python, kind: .python), | |
| 481 | 485 | input: program, directory: dir, block: location, blockText: blockText, alwaysAsk: alwaysAsk, finish: finishBase(.python) |
| 482 | 486 | )) |
| 483 | 487 | } |
| @@ -561,6 +565,12 @@ public enum Babel { | ||
| 561 | 565 | switch language { |
| 562 | 566 | case "ruby": |
| 563 | 567 | let full = expandGeneric(body, vars.map { "\($0.0)=\(rubyLiteral($0.1))" }, params) |
| 568 | if let session { | |
| 569 | return .success(BabelJob( | |
| 570 | language: language, runner: .session(name: session, executable: params.single[":ruby"] ?? executable, kind: .ruby), | |
| 571 | input: full, directory: dir, block: location, blockText: blockText, alwaysAsk: alwaysAsk, finish: finishBase(.script(js: false, session: true)) | |
| 572 | )) | |
| 573 | } | |
| 564 | 574 | return .success(BabelJob( |
| 565 | 575 | language: language, runner: .process(executable: params.single[":ruby"] ?? executable, arguments: arguments), |
| 566 | 576 | input: output ? full : rubyWrapper(full, pp: params.results.contains("pp")), directory: dir, block: location, blockText: blockText, |
| @@ -568,6 +578,12 @@ public enum Babel { | ||
| 568 | 578 | )) |
| 569 | 579 | case "js", "javascript": |
| 570 | 580 | let full = expandGeneric(body, vars.map { "var \($0.0)=\(jsLiteral($0.1));" }, params) |
| 581 | if let session { | |
| 582 | return .success(BabelJob( | |
| 583 | language: language, runner: .session(name: session, executable: params.single[":cmd"] ?? executable, kind: .node), | |
| 584 | input: full, directory: dir, block: location, blockText: blockText, alwaysAsk: alwaysAsk, finish: finishBase(.script(js: true, session: true)) | |
| 585 | )) | |
| 586 | } | |
| 571 | 587 | return .success(BabelJob( |
| 572 | 588 | language: language, runner: .process(executable: params.single[":cmd"] ?? executable, arguments: arguments), |
| 573 | 589 | input: output ? full : jsWrapper(full), directory: dir, block: location, blockText: blockText, alwaysAsk: alwaysAsk, |
| @@ -586,8 +602,11 @@ public enum Babel { | ||
| 586 | 602 | rAssignment(name, value, colnames: tables.colnames.first { $0.0 == name }?.1, colnamesYes: params.single[":colnames"] == "yes", |
| 587 | 603 | rownamesYes: params.single[":rownames"] == "yes") |
| 588 | 604 | } : [] |
| 605 | // An R session runs `R` itself, which reads its commands as they come. | |
| 606 | let runner: BabelJob.Runner = session.map { .session(name: $0, executable: params.single[":cmd"] ?? "R", kind: .r) } | |
| 607 | ?? .process(executable: params.single[":cmd"] ?? executable, arguments: arguments) | |
| 589 | 608 | return .success(BabelJob( |
| 590 | language: language, runner: .process(executable: params.single[":cmd"] ?? executable, arguments: arguments), | |
| 609 | language: language, runner: runner, | |
| 591 | 610 | input: expandGeneric(body, lines, params) + "\n", directory: dir, block: location, blockText: blockText, alwaysAsk: alwaysAsk, |
| 592 | 611 | finish: BabelJob.Finish(kind: .generic, language: language, params: params.results + (output ? [] : ["output"]), output: true, wrap: params.wrap, name: name, inline: inline, hash: hash) |
| 593 | 612 | )) |
Sources/OrgCore/Compute/BabelResults.swift +2 −2
| @@ -85,7 +85,7 @@ extension Babel { | ||
| 85 | 85 | return printed == "nil" ? .none : .string(printed) |
| 86 | 86 | case .compiled: |
| 87 | 87 | return compiledValue(finish, output, scalar: scalar) |
| 88 | case .script(let js): | |
| 88 | case .script(let js, _): | |
| 89 | 89 | let raw = rawResult(finish, output) |
| 90 | 90 | if scalar { return .string(raw) } |
| 91 | 91 | // `org-babel-ruby-table-or-string`, its nils rules. |
| @@ -104,7 +104,7 @@ extension Babel { | ||
| 104 | 104 | return valueIsExitStatus ? output.stdout.split(separator: "\n").last.map(String.init) ?? "" : output.stdout |
| 105 | 105 | case .generic: return output.stdout |
| 106 | 106 | case .compiled: return compiledResult(finish, output) |
| 107 | case .script(let js): return finish.output || js ? output.stdout : (output.resultFile ?? "") | |
| 107 | case .script(let js, let session): return finish.output || (js && !session) ? output.stdout : (output.resultFile ?? "") | |
| 108 | 108 | } |
| 109 | 109 | } |
| 110 | 110 | |
Tests/OrgAppTests/BabelSessionTests.swift +25
| @@ -3,6 +3,8 @@ import Testing | ||
| 3 | 3 | import OrgCore |
| 4 | 4 | @testable import OrgApp |
| 5 | 5 | |
| 6 | /// Serial: the sessions are shared, and each test stops them all when it ends. | |
| 7 | @Suite(.serialized) | |
| 6 | 8 | struct BabelSessionStateTests { |
| 7 | 9 | func run(_ text: String, in folder: URL) async throws -> String { |
| 8 | 10 | let context = EditContext(revision: 0, text: text, tree: OrgParser.parse(text), selection: [2..<2]) |
| @@ -29,6 +31,29 @@ struct BabelSessionStateTests { | ||
| 29 | 31 | _ = try await run("#+begin_src python :session py :results output\nraise ValueError('x')\n#+end_src\n", in: folder) |
| 30 | 32 | #expect(try await run("#+begin_src python :session py\n[a, a]\n#+end_src\n", in: folder) == "\n#+RESULTS:\n| 2 | 2 |\n") |
| 31 | 33 | } |
| 34 | ||
| 35 | @Test(.enabled(if: BabelRunner.installed("ruby") && BabelRunner.installed("node"))) | |
| 36 | func rubyAndNodeSessionsKeepState() async throws { | |
| 37 | let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-session-\(UUID().uuidString)") | |
| 38 | try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true) | |
| 39 | defer { | |
| 40 | BabelSessions.shared.stopAll() | |
| 41 | try? FileManager.default.removeItem(at: folder) | |
| 42 | } | |
| 43 | _ = try await run("#+begin_src ruby :session rb :var n=4\nm = n * 2\n#+end_src\n", in: folder) | |
| 44 | #expect(try await run("#+begin_src ruby :session rb\nm + 1\n#+end_src\n", in: folder) == "\n#+RESULTS:\n: 9\n") | |
| 45 | #expect(try await run("#+begin_src ruby :session rb :results output\nputs [m, n].inspect\n#+end_src\n", in: folder) == "\n#+RESULTS:\n: [8, 4]\n") | |
| 46 | #expect(try await run("#+begin_src ruby :session rb\n[[m, n], nil, [1, 2]]\n#+end_src\n", in: folder) == "\n#+RESULTS:\n| 8 | 4 |\n|---+---|\n| 1 | 2 |\n") | |
| 47 | // An error leaves the session running. | |
| 48 | _ = try await run("#+begin_src ruby :session rb\nraise 'x'\n#+end_src\n", in: folder) | |
| 49 | #expect(try await run("#+begin_src ruby :session rb\n\"still #{m}\"\n#+end_src\n", in: folder) == "\n#+RESULTS:\n: still 8\n") | |
| 50 | #expect(try await run("#+begin_src ruby :session other\ndefined?(m).inspect\n#+end_src\n", in: folder) == "\n#+RESULTS:\n: nil\n") | |
| 51 | ||
| 52 | _ = try await run("#+begin_src js :session js :var k=3\nvar total = k * 10;\n#+end_src\n", in: folder) | |
| 53 | #expect(try await run("#+begin_src js :session js\ntotal + 1\n#+end_src\n", in: folder) == "\n#+RESULTS:\n: 31\n") | |
| 54 | #expect(try await run("#+begin_src js :session js :results output\nconsole.log(total)\n#+end_src\n", in: folder) == "\n#+RESULTS:\n: 30\n") | |
| 55 | #expect(try await run("#+begin_src js :session js\n[total, k]\n#+end_src\n", in: folder) == "\n#+RESULTS:\n| 30 | 3 |\n") | |
| 56 | } | |
| 32 | 57 | } |
| 33 | 58 | |
| 34 | 59 | struct BabelRunnerTests { |
Tests/OrgCoreTests/BabelScriptTests.swift +16
| @@ -96,3 +96,19 @@ struct BabelScriptTests { | ||
| 96 | 96 | } |
| 97 | 97 | } |
| 98 | 98 | } |
| 99 | ||
| 100 | struct BabelSessionPlanTests { | |
| 101 | @Test func rubyNodeAndRSessionsPlanSessionJobs() throws { | |
| 102 | for (text, kind, executable) in [ | |
| 103 | ("#+begin_src ruby :session\n1\n#+end_src\n", BabelJob.SessionKind.ruby, "ruby"), | |
| 104 | ("#+begin_src js :session work\n1\n#+end_src\n", .node, "node"), | |
| 105 | ("#+begin_src R :session\n1\n#+end_src\n", .r, "R"), | |
| 106 | ] { | |
| 107 | let context = EditContext(revision: 0, text: text, tree: OrgParser.parse(text), selection: [2..<2]) | |
| 108 | guard case .success(let job) = Babel.plan(context) else { Issue.record("not planned: \(text)"); continue } | |
| 109 | guard case .session(let name, let program, let planned) = job.runner else { Issue.record("not a session: \(text)"); continue } | |
| 110 | #expect(planned == kind && program == executable) | |
| 111 | #expect(name == (text.contains("work") ? "work" : "*\(text.dropFirst(12).prefix { $0 != " " })*")) | |
| 112 | } | |
| 113 | } | |
| 114 | } | |
Tests/OrgCoreTests/BabelTests.swift +1 −1
| @@ -324,7 +324,7 @@ struct BabelTests { | ||
| 324 | 324 | @Test func refusesWhatWouldRunDifferently() { |
| 325 | 325 | for (text, message) in [ |
| 326 | 326 | ("#+begin_src sh :eval never\necho\n#+end_src\n", "Evaluation of this sh code block is disabled."), |
| 327 | ("#+begin_src ruby :session s\nputs 1\n#+end_src\n", ":session isn't supported for ruby yet; nothing was run."), | |
| 327 | ("#+begin_src awk :session s\nBEGIN { print 1 }\n#+end_src\n", ":session isn't supported for awk yet; nothing was run."), | |
| 328 | 328 | ("#+begin_src sh :noweb yes\n<<x()>>\n#+end_src\n", "Noweb references that run a block (x()) aren't supported yet; nothing was run."), |
| 329 | 329 | ("#+begin_src python :cmdline -x\nreturn 1\n#+end_src\n", ":cmdline isn't supported for python yet; nothing was run."), |
| 330 | 330 | ("#+begin_src cobol\nDISPLAY\n#+end_src\n", "No way to run cobol blocks yet."), |
docs/manual/guide/11-code-blocks.org +8 −3
| @@ -205,6 +205,7 @@ What "value" means depends on the language: | ||
| 205 | 205 | | Shells, =:results value= written out | The exit status of the last command. | |
| 206 | 206 | | Python | What the block returns. The body runs as a function, so it needs =return=. =:return expr= adds a final =return expr=. | |
| 207 | 207 | | Python in a =:session= | The value of the last expression. | |
| 208 | | =ruby=, =js= in a =:session= | The value of the last expression, read as without a session. | | |
| 208 | 209 | | Emacs Lisp | The value of the last form. | |
| 209 | 210 | | =ruby= | The value of the body, run as a lambda; strings as they are, other values as =inspect= prints them, arrays read as tables with =nil= rows as rules. =:results pp= prints it with =pp=. | |
| 210 | 211 | | =js=, =javascript= | What the body returns, as =util.inspect= prints it, arrays read as tables; it runs as a function, so it needs =return=. | |
| @@ -359,7 +360,7 @@ A string or number takes the place of the form. Lisp that the interpreter cannot | ||
| 359 | 360 | | =:separator= | text | Column separator for tables in shell variables; a tab by default. | |
| 360 | 361 | | =:hline-string= | text | What a rule becomes in a shell variable with =:hlines yes=; =hline= by default. | |
| 361 | 362 | | =:dir= | folder | Folder to run in, relative to the file's folder. It must exist. | |
| 362 | | =:session= | name, =none= | Runs in a long-lived interpreter (see Sessions below). Shells and Python only; ignored for the compiled languages. | | |
| 363 | | =:session= | name, =none= | Runs in a long-lived interpreter (see Sessions below). Shells, Python, =ruby=, =js= and =R=; ignored for the compiled languages. | | |
| 363 | 364 | | =:stdin= | name of a table, list or block | Feeds the value to standard input. Shells only. | |
| 364 | 365 | | =:cmdline= | arguments | Command-line arguments. Shells, =dot=, =plantuml=, =mermaid= and the compiled languages only. | |
| 365 | 366 | | =:shebang= | =#!…= line | Runs the block as a script with this first line. Shells only; also used by tangling. | |
| @@ -387,7 +388,7 @@ orgstar does not run a block whose arguments it would handle differently from Em | ||
| 387 | 388 | | =:post= | every language | "=:post= isn't supported yet; nothing was run." | |
| 388 | 389 | | =:stdin=, =:shebang= | languages other than shells | "=:stdin= isn't supported yet; nothing was run." | |
| 389 | 390 | | =:cmdline= | languages other than shells, graphics and the compiled languages | "=:cmdline= isn't supported for /language/ yet; nothing was run." | |
| 390 | | =:session= | languages other than shells, Python and the compiled languages, which ignore it | "=:session= isn't supported for /language/ yet; nothing was run." | | |
| 391 | | =:session= | =awk=, graphics and Emacs Lisp; the compiled languages ignore it | "=:session= isn't supported for /language/ yet; nothing was run." | | |
| 391 | 392 | |
| 392 | 393 | * Variables |
| 393 | 394 | |
| @@ -480,13 +481,17 @@ for a in "$@"; do echo "[$a]"; done | ||
| 480 | 481 | |
| 481 | 482 | * Sessions |
| 482 | 483 | |
| 483 | =:session name= runs the block in an interpreter that stays running, so variables, functions and the working folder carry over to the next block in the same session. Sessions work for shells and Python on the Mac. | |
| 484 | =:session name= runs the block in an interpreter that stays running, so variables, functions and the working folder carry over to the next block in the same session. Sessions work for shells, Python, Ruby, JavaScript (Node) and R on the Mac. | |
| 484 | 485 | |
| 485 | 486 | - =:session= with no name uses =*language*=, such as =*sh*=. =:session none= runs without one. |
| 486 | 487 | - A session belongs to one folder, one interpreter and one name. Blocks in different folders, or with different names, do not share state. |
| 487 | 488 | - A session starts in the block's =:dir=, or the file's folder, the first time it is used. |
| 488 | 489 | - A shell session's output is what the block prints. =:results value= gives the exit status of the last command. |
| 489 | 490 | - A Python session runs the block at top level. Its value is the value of the last expression, so =return= is not used. |
| 491 | - A Ruby session runs each block in the same binding, and a JavaScript session in Node's global context, so local variables and =var= and =let= declarations carry over. Their value is the value of the last expression, written as without a session (=inspect=, =util.inspect=); =return= is not used. Errors go to the block's error output and leave the session running. | |
| 492 | - An R session runs each block with =source(…, print.eval = TRUE)= in one =R --slave= process; its result is the output, as without a session. | |
| 493 | ||
| 494 | Emacs runs these with packages beyond Org: inf-ruby for Ruby, ESS for R, and js-comint or skewer for JavaScript; with plain =node=, ob-js refuses sessions. orgstar's sessions don't need them, and their results can differ in detail from those packages' echoed output. | |
| 490 | 495 | - Sessions stop when you quit orgstar, or when you cancel a block running in one. |
| 491 | 496 | |
| 492 | 497 | #+BEGIN_SRC org |