import Foundation // `org-babel-tangle` (Org 9.8.7 ob-tangle.el): source blocks written to the files their // `:tangle` names, with `:noweb`, `:padline`, `:shebang`, `:mkdirp`, `:tangle-mode`, // `:prologue` and `:epilogue`, and `:var` for shells, Python and Emacs Lisp. Comments // (`:comments`) and header values Org would evaluate as Lisp aren't supported. public enum Tangle { /// All blocks (C-c C-v t), the block at an offset (C-u C-c C-v t), or the blocks going to /// the same file as the block at an offset (C-u C-u C-c C-v t). public enum Scope: Sendable, Equatable { case all case block(Int) case target(Int) } public struct File: Sendable, Equatable { /// Absolute. public var path: String public var contents: String /// From `:tangle-mode`, or 0o755 for a file with a `:shebang`; nil leaves the mode alone. public var mode: Int? /// `:mkdirp`: the file's directory is made if missing. public var makeDirectories: Bool } public struct Output: Sendable, Equatable { public var files: [File] /// Blocks written, for "Tangled N code blocks". public var blocks: Int } /// `org-babel-tangle-lang-exts`. static let extensions = ["emacs-lisp": "el", "elisp": "el", "bibtex": "bib"] /// `org-babel-shell-names`, and `shell` itself. static let shells: Set = ["sh", "bash", "zsh", "fish", "csh", "ash", "dash", "ksh", "mksh", "posh", "shell"] /// The files tangling `text`, the contents of the Org file at `path`, writes. public static func run(text: String, path: String, scope: Scope = .all, tree: OrgTree? = nil) -> Result { let ns = text as NSString let model = DocumentModel(tree: tree ?? OrgParser.parse(text)) do { var order: [String] = [] var specs: [String: [(language: String?, body: String, params: Babel.Params)]] = [:] func add(_ block: SrcBlockInfo, _ params: Babel.Params) throws { guard let file = try fileName(path: path, language: block.language, tangle: params.single[":tangle"] ?? "no") else { return } let body = try body(block, params: params, model: model, text: ns) if specs[file] == nil { order.append(file) } specs[file, default: []].append((block.language, body, params)) } switch scope { case .block(let offset): guard let block = block(at: offset, in: model) else { throw Babel.Failure.message("Point is not in a source code block") } try add(block, try params(block, model: model, text: ns)) case .all, .target: var target: String? if case .target(let offset) = scope { guard let block = block(at: offset, in: model) else { throw Babel.Failure.message("Point is not in a source code block") } target = try params(block, model: model, text: ns).single[":tangle"] ?? "no" } for block in model.srcBlocks where !excluded(block, model: model) { let params = try params(block, model: model, text: ns) let tangle = params.single[":tangle"] ?? "no" if tangle == "no" || (block.language == nil && tangle == "yes") { continue } if let target, target != tangle { continue } try add(block, params) } } var files: [File] = [] var blocks = 0 for file in order { var contents = "" var mode: Int? var makeDirectories = false var sheBanged = false for spec in specs[file]! { let shebang = spec.params.single[":shebang"].flatMap { $0.isEmpty ? nil : $0 } var tangleMode = try spec.params.single[":tangle-mode"].map(fileMode) if shebang != nil, tangleMode == nil { tangleMode = 0o755 } // `add-to-list` then `mapc`: the first mode given is the one left. if mode == nil { mode = tangleMode } if let mkdirp = spec.params.single[":mkdirp"], mkdirp != "no" { makeDirectories = true } if spec.params.single[":padline"] != "no", !contents.isEmpty { contents += "\n" } if let shebang, !sheBanged { contents += shebang + "\n" sheBanged = true } contents += spec.body + "\n" blocks += 1 } files.append(File(path: file, contents: contents, mode: mode, makeDirectories: makeDirectories)) } return .success(Output(files: files, blocks: blocks)) } catch let error as Babel.Failure { return .failure(error) } catch { return .failure(.message("\(error)")) } } /// `org-babel-where-is-src-block-head`. static func block(at offset: Int, in model: DocumentModel) -> SrcBlockInfo? { model.srcBlocks.first { $0.range.contains(offset) || $0.range.upperBound == offset } } /// `org-in-commented-heading-p` and `org-in-archived-heading-p`. static func excluded(_ block: SrcBlockInfo, model: DocumentModel) -> Bool { guard let heading = block.heading else { return false } return ([heading] + model.ancestors(of: heading)).contains { index in let h = model.headings[index] return AgendaSource.isCommented(h.title) || h.localTags.contains("ARCHIVE") } } static func params(_ block: SrcBlockInfo, model: DocumentModel, text: NSString) throws -> Babel.Params { let params = try Babel.Params(block: block, model: model, text: text) for (key, value) in params.single where value.hasPrefix("(") && key != ":tangle-mode" { throw Babel.Failure.message("\(key) \(value) is Lisp to evaluate, which tangling doesn't support yet; nothing was tangled.") } if let comments = params.single[":comments"], comments != "no" { throw Babel.Failure.message(":comments \(comments) isn't supported yet; nothing was tangled.") } return params } /// `org-babel-effective-tangled-filename`. static func fileName(path: String, language: String?, tangle: String) throws -> String? { let directory = (path as NSString).deletingLastPathComponent let base: String switch tangle { case "no", "": return nil case "yes": base = (path as NSString).deletingPathExtension default: base = expand(tangle, in: directory) } guard tangle == "yes", let language else { return base } return base + "." + (extensions[language] ?? language) } /// `expand-file-name`. static func expand(_ name: String, in directory: String) -> String { let expanded = (name as NSString).expandingTildeInPath let absolute = expanded.hasPrefix("/") ? expanded : (directory as NSString).appendingPathComponent(expanded) return (absolute as NSString).standardizingPath } /// `org-babel-tangle-single-block`'s body: noweb references expanded or stripped, the /// language's expansion with `:var`, `:prologue` and `:epilogue`, `-r` coderefs taken /// out, and indentation and surrounding blank space trimmed. static func body(_ block: SrcBlockInfo, params: Babel.Params, model: DocumentModel, text: NSString) throws -> String { var body = Babel.normalizedBody(block) let noweb = params.single[":noweb"] if Babel.nowebTangles(noweb) { if noweb == "strip-tangle" { body = body.replacingOccurrences(of: "<<([^ \\t\\n](?:.*?[^ \\t\\n])?)>>", with: "", options: .regularExpression) } else { body = try Babel.expandNoweb(block, params: params, model: model, text: text, tangling: true) } } if params.single[":no-expand"] == nil { body = try expandBody(body, language: block.language ?? "", params: params, model: model, text: text) } // `(string-match "-r" extra)`. if block.switches.joined(separator: " ").contains("-r") { if block.switches.contains("-l") { throw Babel.Failure.message("-l coderef formats aren't supported yet; nothing was tangled.") } body = body.components(separatedBy: "\n") .map { $0.replacingOccurrences(of: "[ \\t]*\\(ref:[-a-zA-Z0-9_][-a-zA-Z0-9_ ]*\\)[ \\t]*$", with: "", options: .regularExpression) } .joined(separator: "\n") } let preserve = block.switches.contains("-i") if !preserve { body = removeIndentation(body) } body = body.replacingOccurrences(of: "[ \\t\\n\\r]+$", with: "", options: .regularExpression) return preserve ? body.replacingOccurrences(of: "^([ \\t]*\\n)+", with: "", options: .regularExpression) : body.replacingOccurrences(of: "^[ \\t\\n\\r]+", with: "", options: .regularExpression) } /// `org-remove-indentation`. static func removeIndentation(_ s: String) -> String { let lines = s.components(separatedBy: "\n") let indents = lines.filter { $0.contains { !$0.isWhitespace } }.map { $0.prefix { $0 == " " || $0 == "\t" }.count } let common = indents.min() ?? 0 guard common > 0 else { return s } return lines.map { String($0.dropFirst(min(common, $0.prefix { $0 == " " || $0 == "\t" }.count))) }.joined(separator: "\n") } /// `org-babel-expand-body:LANG`, or the generic expansion with the language's variable /// assignments. static func expandBody(_ body: String, language: String, params: Babel.Params, model: DocumentModel, text: NSString) throws -> String { let refs = Babel.References(resolved: [:], options: Babel.Options()) let vars = try params.vars.map { variable in do { return (variable.name, try Babel.resolve(variable.value, model: model, text: text, refs: refs)) } catch is Babel.Needed { throw Babel.Failure.message(":var \(variable.value) would run a block while tangling, which isn't supported yet; nothing was tangled.") } } let prologue = params.single[":prologue"] let epilogue = params.single[":epilogue"] if language == "emacs-lisp" || language == "elisp" { guard !vars.isEmpty else { return body + "\n" } let bindings = vars.map { "(\($0.0) '\(Babel.lispLiteral($0.1)))" }.joined(separator: "\n ") return "(let (\(bindings))\n" + (prologue.map { $0 + "\n " } ?? "") + body + (epilogue.map { "\n " + $0 + "\n" } ?? "") + "\n)" } var lines: [String] = [] if shells.contains(language) { lines = try vars.map { name, value in switch value { case .scalar(let s), .number(let s): return "\(name)='" + s.replacingOccurrences(of: "'", with: "'\"'\"'") + "'" default: throw Babel.Failure.message("Tables and lists in :var aren't supported for tangling \(language) yet; nothing was tangled.") } } } else if language == "python" { lines = vars.map { "\($0.0)=\(Babel.pythonLiteral($0.1))" } } else if !vars.isEmpty { throw Babel.Failure.message(":var isn't supported for tangling \(language) yet; nothing was tangled.") } return ((prologue.map { [$0] } ?? []) + lines + [body] + (epilogue.map { [$0] } ?? [])).joined(separator: "\n") } /// `org-babel-interpret-file-mode`, for `:tangle-mode`. static func fileMode(_ value: String) throws -> Int { let v = value.trimmingCharacters(in: .whitespaces) if let m = v.wholeMatch(of: /\(identity\s+#o([0-7]{3})\)/) ?? v.wholeMatch(of: /#o([0-7]{3})/) ?? v.wholeMatch(of: /o0?([0-7]{3})/) { return Int(m.1, radix: 8)! } if v.wholeMatch(of: /[r-][w-][xs-][r-][w-][xs-][r-][w-][x-]/) != nil { let chars = Array(v) var mode = 0 for (i, bit) in [0o400, 0o200, 0o100, 0o40, 0o20, 0o10, 0o4, 0o2, 0o1].enumerated() where chars[i] != "-" { mode |= bit } return mode } if v.wholeMatch(of: /[ugoa]*[+=-][rwx]*(,[ugoa]*[+=-][rwx]*)*/) != nil { var mode = 0o644 for clause in v.split(separator: ",") { let who = clause.prefix { "ugoa".contains($0) } let rest = clause.dropFirst(who.count) let op = rest.first! let perms = rest.dropFirst() var mask = 0 for w in (who.isEmpty ? "a" : String(who)) { switch w { case "u": mask |= 0o700 case "g": mask |= 0o070 case "o": mask |= 0o007 default: mask |= 0o777 } } var bits = 0 for p in perms { switch p { case "r": bits |= 0o444 case "w": bits |= 0o222 default: bits |= 0o111 } } switch op { case "+": mode |= bits & mask case "-": mode &= ~(bits & mask) default: mode = (mode & ~mask) | (bits & mask) } } return mode } throw Babel.Failure.message("File mode \(value) isn't one tangling understands; nothing was tangled.") } } /// C-c C-v t (`org-babel-tangle`): the app writes the files. public struct TangleCommand: OrgCommand { public let scope: Scope public enum Scope: Sendable { case all, block, target } public init(_ scope: Scope = .all) { self.scope = scope } public var id: String { switch scope { case .all: "org.babel.tangle" case .block: "org.babel.tangle-block" case .target: "org.babel.tangle-target" } } public var title: String { switch scope { case .all: "Tangle File" case .block: "Tangle Block" case .target: "Tangle Block's Target" } } public func applies(in context: EditContext) -> Bool { scope == .all ? !DocumentModel(tree: context.tree).srcBlocks.isEmpty : KeyContext.holds("src", in: context) } public func run(in context: EditContext) -> CommandStep { switch scope { case .all: .external(.tangle(.all)) case .block: .external(.tangle(.block(context.caret))) case .target: .external(.tangle(.target(context.caret))) } } }