Commit e1d0c11c63

e1d0c11c63698d6380ce5ab3cabec4e9ccd0a191

parent: 9eb6f4c04a

Verified · cmc

cmc <hello@cleberg.net> · 2026-11-21 21:25 UTC

Read CRLF and CR line ends in LaTeX export and includes as Emacs decodes them

Layout: unified · split

Sources/OrgCore/Export/ExportOptions.swift +11 −1
@@ -116,6 +116,16 @@ public struct ExportOptions: Sendable, Equatable {
116116/// What happens to the text before it's parsed for export: `#+INCLUDE` (ox.el's
117117/// `org-export-expand-include-keyword`) and macros (`org-macro-replace-all`).
118118public enum ExportPreprocess {
119 /// `text` with line ends as Emacs decodes the file: CRLF becomes LF when no line ends in a
120 /// bare LF (dos), CR becomes LF when there is no LF (mac); otherwise every CR stays (unix).
121 public static func decodedLineEnds(_ text: String) -> String {
122 let scalars = Array(text.unicodeScalars)
123 guard scalars.contains("\r") else { return text }
124 guard scalars.contains("\n") else { return text.replacingOccurrences(of: "\r", with: "\n") }
125 for (i, s) in scalars.enumerated() where s == "\n" && (i == 0 || scalars[i - 1] != "\r") { return text }
126 return text.replacingOccurrences(of: "\r\n", with: "\n")
127 }
128
119129 /// `#+INCLUDE: "file" [example|export FORMAT|src LANG] [:lines "A-B"] [:minlevel N]`, with
120130 /// `file::*Heading` or `file::#custom-id` for one subtree. Relative to `directory`.
121131 /// `includer` is the exported file's directory, `directory` at the top.
@@ -137,7 +147,7 @@ public enum ExportPreprocess {
137147 target = String(spec[r.upperBound...])
138148 }
139149 let url = includeURL(file, directory: directory)
140 guard var included = try? String(contentsOf: url, encoding: .utf8) else {
150 guard var included = (try? String(contentsOf: url, encoding: .utf8)).map(decodedLineEnds) else {
141151 lines.append("")
142152 continue
143153 }
Sources/OrgCore/Export/LaTeXExport.swift +10 −8
@@ -44,7 +44,7 @@ public enum LaTeXExport {
4444 _ text: String, settings: OrgSettings = .default, path: String? = nil, now: Date = Date(), bodyOnly: Bool = false,
4545 author: String = "", language: String = "en", timeZone: TimeZone = .current, file: Bool = false
4646 ) throws -> String {
47 let text = try expanded(text, settings: settings, path: path, now: now)
47 let text = try expanded(ExportPreprocess.decodedLineEnds(text), settings: settings, path: path, now: now)
4848 let tree = OrgParser.parse(text, defaults: settings)
4949 let keywords = settings.setupKeywords.split(separator: "\n").map(String.init)
5050 + tree.root.descendants().filter { $0.kind == .keyword }.map { $0.text.trimmingCharacters(in: .newlines) }
@@ -269,20 +269,22 @@ final class LaTeXRenderer {
269269 /// `org-element-normalize-string`: one final newline, none for the empty string.
270270 static func normalize(_ s: String) -> String {
271271 guard !s.isEmpty else { return "" }
272 var end = s.endIndex
273 var cut = s.endIndex
274 while end > s.startIndex {
275 let before = s.index(before: end)
276 if s[before] == " " || s[before] == "\t" {
272 // By scalar: a CR the file kept stays, as in Emacs, where "\r\n" isn't one character.
273 let scalars = s.unicodeScalars
274 var end = scalars.endIndex
275 var cut = scalars.endIndex
276 while end > scalars.startIndex {
277 let before = scalars.index(before: end)
278 if scalars[before] == " " || scalars[before] == "\t" {
277279 end = before
278 } else if s[before] == "\n" {
280 } else if scalars[before] == "\n" {
279281 end = before
280282 cut = before
281283 } else {
282284 break
283285 }
284286 }
285 return String(s[..<cut]) + "\n"
287 return String(scalars[..<cut]) + "\n"
286288 }
287289
288290 /// `org-trim`.
Tests/OrgCoreTests/LaTeXExportTests.swift +19
@@ -335,6 +335,25 @@ struct LaTeXExportTests {
335335 #expect(body.hasPrefix("Text \\url{elisp:(+ 1 2)} and \\url{https://a.b/c d} ok."))
336336 }
337337
338 /// Line ends as Emacs decodes the file: CRLF throughout (dos), CR only (mac), and mixed (unix, CRs kept).
339 @Test(.enabled(if: oracle))
340 func lineEndsMatchEmacs() throws {
341 let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-crlf-\(UUID().uuidString)")
342 try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
343 defer { try? FileManager.default.removeItem(at: folder) }
344 try "Included line.\r\n\r\n- item\r\n".write(to: folder.appendingPathComponent("inc.org"), atomically: true, encoding: .utf8)
345 let docs = [
346 "#+TITLE: T\r\n* H\r\nText ok.\r\n\r\nSecond *b* paragraph.\r\n| a | b |\r\n|---+---|\r\n| 1 | 2 |\r\n",
347 "Text ok.\r\nNo final newline",
348 "Mixed\r\nends.\nhere\r\n",
349 "Old\rMac\r\rends\r",
350 "Lone\rCR\r\nin dos.\r\n",
351 "#+INCLUDE: \"\(folder.path)/inc.org\"\r\nAfter.\r\n",
352 "#+INCLUDE: \"\(folder.path)/inc.org\"\nAfter.\n",
353 ]
354 try compare(docs.map { LaTeXOracle.Case(text: $0, bodyOnly: true) }, names: docs.indices.map { "line ends \($0)" })
355 }
356
338357 /// What Emacs exports differently or refuses is refused, naming it.
339358 @Test func refusesWhatItCannotMatch() {
340359 func reason(_ org: String) -> String? {
docs/manual/guide/01-files-and-folders.org +1
@@ -305,6 +305,7 @@ orgstar edits UTF-8 files, with or without a byte order mark.
305305- A file that is not valid UTF-8 opens read-only. Its text shows with replacement characters where bytes don't decode, the window subtitle reads =Read-only: not UTF-8=, the modeline shows =Read-only=, and commands that would change it report =This file is read-only.= orgstar never writes such a file, and refiling or archiving into it is refused.
306306- A file with a UTF-8 byte order mark keeps it when saved; the modeline shows =BOM=.
307307- A file containing CRLF line endings shows =CRLF= in the modeline. Existing line endings are kept.
308- LaTeX export, and =#+INCLUDE= in every export, read line endings as Emacs does: a file whose lines all end in CRLF, or all in CR, is read as LF lines; in a file with mixed endings the CRs stay in the text.
308309
309310orgstar does not convert between encodings or line endings.
310311