Commit dc4642653b

dc4642653b367fc9783696f3e25cb7f59d71c9b7

parent: 913aa7e847

Verified · cmc

cmc <hello@cleberg.net> · 2026-11-20 16:58 UTC

Number code lines by block, not by call order

Layout: unified · split

Sources/OrgCore/Export/LaTeXExport.swift +25 −33
@@ -504,13 +504,8 @@ final class LaTeXRenderer {
504504 let subsections = children.filter { $0.kind == .section }
505505 var contents: String?
506506 if export == .all {
507 var out = Self.normalize(try elements(body, end: subsections.first?.range.lowerBound ?? node.range.upperBound))
508 let exported = try subsections.compactMap { try section($0) }
509 for (index, sub) in exported.enumerated() {
510 out += sub
511 _ = index
512 }
513 contents = Self.normalize(out)
507 let out = Self.normalize(try elements(body, end: subsections.first?.range.lowerBound ?? node.range.upperBound))
508 contents = Self.normalize(out + (try subsections.compactMap { try section($0) }.joined()))
514509 }
515510 let first = children.first?.range.lowerBound ?? node.range.upperBound
516511 let preBlank = children.isEmpty ? 0 : blankLines(from: heading.range.lowerBound, to: first)
@@ -566,19 +561,10 @@ final class LaTeXRenderer {
566561 /// headline opens its list when it is the first and closes it when it is the last.
567562 private func exportedSiblings(_ section: SyntaxNode) -> [Int] {
568563 guard let parent = section.parent else { return [section.range.lowerBound] }
569 // A section with a body before it means the headline isn't first.
570 var starts: [Int] = []
571 var bodyBefore = false
572 for child in parent.children where child.kind != .heading {
573 if child.kind != .section {
574 if starts.isEmpty { bodyBefore = true }
575 continue
576 }
577 guard let heading = child.firstChild(.heading), HTMLRenderer.sectionExport(heading, options: options, selected: selected) != .nothing else { continue }
578 starts.append(child.range.lowerBound)
579 }
580 _ = bodyBefore
581 return starts
564 return parent.children.filter { child in
565 guard child.kind == .section, let heading = child.firstChild(.heading) else { return false }
566 return HTMLRenderer.sectionExport(heading, options: options, selected: selected) != .nothing
567 }.map(\.range.lowerBound)
582568 }
583569
584570 // MARK: - Elements
@@ -719,8 +705,6 @@ final class LaTeXRenderer {
719705 return try wrapLabel(affiliated, "\\begin{\(type)}\(affiliated.value("options") ?? "")\n\(Self.normalize(contents))\\end{\(type)}")
720706 }
721707
722 private var listDepth = 0
723
724708 /// `org-latex-item`.
725709 private func item(_ item: SyntaxNode, ordered: Bool, depth: Int) throws -> String {
726710 var counter = ""
@@ -962,13 +946,14 @@ final class LaTeXRenderer {
962946 /// `org-export-format-code-default` for a src or example block's lines.
963947 private func formatCode(_ lines: [String]) throws -> String {
964948 guard let parsed = Coderef.block(lines) else { return "" }
949 let blockLoc = try blockLoc(lines, numbered: parsed.switches.numberLines != nil)
965950 var value = parsed.body.joined(separator: "\n")
966951 if !value.isEmpty { value += "\n" }
967952 let preserve = lines[0].firstMatch(of: /^[ \t]*#\+(?i:begin_(?:src|example))\b.*\s-i\b/) != nil
968953 var code = preserve ? value : try Self.removeIndentation(value)
969954 if code.hasSuffix("\n") { code.removeLast() }
970955 let (lines, refs) = Coderef.unravel(code.components(separatedBy: "\n"), format: parsed.switches.labelFormat)
971 let loc = parsed.switches.numberLines == nil ? nil : blockLoc(lines: lines)
956 let loc = parsed.switches.numberLines == nil ? nil : blockLoc
972957 guard loc != nil || (parsed.switches.retainLabels && !refs.isEmpty) else { return lines.joined(separator: "\n") + "\n" }
973958 let width = loc.map { String($0 + lines.count).count } ?? 0
974959 let numberWidth = loc == nil ? 0 : width + 2
@@ -983,18 +968,25 @@ final class LaTeXRenderer {
983968 }.joined(separator: "\n") + "\n"
984969 }
985970
986 private var blockLocs: [Int?]?
987 private var blockIndex = 0
971 /// Each src and example block's `org-export-get-loc`, by the block's text, in order.
972 private lazy var blockLocs: [String: [Int?]] = {
973 let blocks = Coderef.blocks(tree)
974 var locs: [String: [Int?]] = [:]
975 for (lines, loc) in zip(blocks, Coderef.locs(blocks.compactMap(Coderef.block))) {
976 locs[lines.joined(separator: "\n"), default: []].append(loc)
977 }
978 return locs
979 }()
988980
989 /// `org-export-get-loc` of the next numbered block.
990 private func blockLoc(lines: [String]) -> Int? {
991 if blockLocs == nil {
992 let blocks = Coderef.blocks(tree).compactMap(Coderef.block)
993 blockLocs = Coderef.locs(blocks)
981 /// `org-export-get-loc` of the block with `lines`, the next of those with the same text.
982 private func blockLoc(_ lines: [String], numbered: Bool) throws -> Int? {
983 let key = lines.joined(separator: "\n")
984 guard let queue = blockLocs[key], !queue.isEmpty else {
985 if numbered { throw Unsupported(reason: "Numbered blocks inside other blocks") }
986 return nil
994987 }
995 _ = lines
996 defer { blockIndex += 1 }
997 return blockIndex < blockLocs!.count ? blockLocs![blockIndex] : nil
988 blockLocs[key] = Array(queue.dropFirst())
989 return queue[0]
998990 }
999991
1000992 /// `org-latex-verse-block` and the verse rules of `org-latex-plain-text`.
Tests/OrgCoreTests/LaTeXExportTests.swift +2
@@ -154,6 +154,8 @@ struct LaTeXExportTests {
154154 "| a | =b\\\\|c= |\n\nThe {{{title}}} macro and ={{{title}}}= as written.\n",
155155 "#+begin_src emacs-lisp :exports none\n(hidden)\n#+end_src\n\n#+RESULTS:\n: gone\n\nafter\n\n#+RESULTS: orphan\n: kept\n",
156156 "Plain LaTeX's own words and TeXbook.\n",
157 "#+begin_example\nplain\n#+end_example\n\n#+begin_src sh -n\none\ntwo\n#+end_src\n\n#+begin_example\nplain\n#+end_example\n\n#+begin_src sh +n\nthree\n#+end_src\n",
158 "* Deep lists\n** two\n*** three\n**** four a\n**** COMMENT skipped\n**** four b :noexport:\n**** four c\n",
157159 ]
158160
159161 @Test(.enabled(if: oracle))