Commit a7dad05235

a7dad05235eff904c208271eb3c2407aa2e04859

parent: c396b3453f

Verified · cmc

cmc <hello@cleberg.net> · 2026-11-19 20:23 UTC

Match Denote 4.2 and denote-journal 0.3 in an Emacs oracle

- Journal title defaults to denote-journal's "%A %-e %B %Y %H:%M"; imported
  styles use %-e.
- Rename from front matter takes title, keywords and signature from the front
  matter only, keeps keyword order, and refuses files without front matter.
- Title slugs keep < and >.
- DenoteOracleTests runs Denote from ORGSTAR_DENOTE or a Doom/package.el
  install when present.

Layout: unified · split

Sources/OrgApp/Dailies.swift +1 −1
@@ -28,7 +28,7 @@ public struct Dailies: Sendable, Equatable {
2828 /// `denote-journal-title-format`, a `format-time-string` format.
2929 public var title: String
3030
31 public init(keyword: String = "journal", title: String = "%A %e %B %Y") {
31 public init(keyword: String = "journal", title: String = "%A %-e %B %Y %H:%M") {
3232 self.keyword = keyword
3333 self.title = title
3434 }
Sources/OrgApp/EmacsImport.swift +2 −2
@@ -561,8 +561,8 @@ public struct EmacsImport: Sendable {
561561 guard let text = value.string else { return note("not a string") }
562562 add(name, .string(text))
563563 case "denote-journal-title-format":
564 let formats = ["day-date-month-year": "%A %e %B %Y", "day-date-month-year-24h": "%A %e %B %Y %H:%M",
565 "day-date-month-year-12h": "%A %e %B %Y %I:%M %p"]
564 let formats = ["day-date-month-year": "%A %-e %B %Y", "day-date-month-year-24h": "%A %-e %B %Y %H:%M",
565 "day-date-month-year-12h": "%A %-e %B %Y %I:%M %p", "day": "%A"]
566566 if let format = value.string ?? value.symbol.flatMap({ formats[$0] }) { add(name, .string(format)) } else { note("not a format or a known style") }
567567 case "org-roam-directory", "org-roam-dailies-directory":
568568 guard let dir = value.string else { return note("not a string") }
Sources/OrgApp/OrgPreferences.swift +1 −1
@@ -180,7 +180,7 @@ public enum OrgPreferences {
180180 public static func denoteJournalDirectory(_ defaults: UserDefaults = .standard) -> String { string("denoteJournalDirectory", defaults).nonEmpty ?? "journal" }
181181 public static func denoteJournalKeyword(_ defaults: UserDefaults = .standard) -> String { string("denoteJournalKeyword", defaults).nonEmpty ?? "journal" }
182182 /// `denote-journal-title-format`, a `format-time-string` format.
183 public static func denoteJournalTitle(_ defaults: UserDefaults = .standard) -> String { string("denoteJournalTitle", defaults).nonEmpty ?? "%A %e %B %Y" }
183 public static func denoteJournalTitle(_ defaults: UserDefaults = .standard) -> String { string("denoteJournalTitle", defaults).nonEmpty ?? "%A %-e %B %Y %H:%M" }
184184 /// `org-attach-id-dir`, relative to an org file's folder unless absolute.
185185 public static func attachIDDir(_ defaults: UserDefaults = .standard) -> String {
186186 let dir = string("attachIDDir", defaults)
Sources/OrgApp/RoamActions.swift +1 −1
@@ -200,7 +200,7 @@ public struct RoamActions {
200200 func renameFromFrontMatter() {
201201 guard let path = session.path else { return show("No file is open") }
202202 guard let text = session.text(of: path) else { return show("Can't read \((path as NSString).lastPathComponent)") }
203 guard let target = Denote.renamed(path, text: text) else { return show("Not a Denote note: no identifier") }
203 guard let target = Denote.renamed(path, text: text) else { return show("Not a Denote note: no front matter or identifier") }
204204 guard target != path else { return show("Nothing to rename") }
205205 guard !FileManager.default.fileExists(atPath: target) else { return show("\((target as NSString).lastPathComponent) is already there") }
206206 guard session.save(path) else { return show("Can't save \((path as NSString).lastPathComponent)") }
Sources/OrgApp/UserConfig.swift +1 −1
@@ -183,7 +183,7 @@ public enum UserConfig {
183183 comment: "the folder denote-journal entries are in, relative to denote-directory", legacy: ""),
184184 Setting(section: "", key: "denote-journal-keyword", defaultsKey: "denoteJournalKeyword", kind: .string, fallback: .string("journal"),
185185 comment: "the keyword journal entries have", legacy: ""),
186 Setting(section: "", key: "denote-journal-title-format", defaultsKey: "denoteJournalTitle", kind: .string, fallback: .string("%A %e %B %Y"),
186 Setting(section: "", key: "denote-journal-title-format", defaultsKey: "denoteJournalTitle", kind: .string, fallback: .string("%A %-e %B %Y %H:%M"),
187187 comment: "a journal entry's title; a format-time-string format", legacy: ""),
188188 Setting(section: "", key: "auto-fill-mode", defaultsKey: "autoFill", kind: .bool, fallback: .bool(false),
189189 comment: "typing a space past fill-column breaks the line (View ▸ Auto Fill)", legacy: ""),
Sources/OrgCore/Links/Denote.swift +26 −10
@@ -28,7 +28,7 @@ public enum Denote {
2828 // MARK: - Naming new files
2929
3030 /// `denote-excluded-punctuation-regexp`.
31 static let punctuation = Set("[]{}!@#$%^&*()+'\"?,.|;:~`‘’“”=<>/")
31 static let punctuation = Set("[]{}!@#$%^&*()+'\"?,.|;:~`‘’“”=/")
3232
3333 /// `denote-sluggify-title`: without punctuation, spaces and underscores as one `-`, none
3434 /// at either end, lower case.
@@ -50,6 +50,20 @@ public enum Denote {
5050 String(word.lowercased().filter { !punctuation.contains($0) && !$0.isWhitespace && $0 != "_" && $0 != "-" })
5151 }
5252
53 /// `denote-sluggify-signature`: like a title, with `=` between words.
54 public static func signature(_ text: String) -> String {
55 var out = ""
56 for character in text.lowercased() where character == "=" || !punctuation.contains(character) && character != "-" {
57 if character.isWhitespace || character == "_" || character == "=" {
58 if !out.isEmpty, out.last != "=" { out.append("=") }
59 } else {
60 out.append(character)
61 }
62 }
63 while out.last == "=" { out.removeLast() }
64 return out
65 }
66
5367 /// Keywords as `denote-keywords-sort` keeps them: sluggified, without empties or repeats,
5468 /// sorted.
5569 public static func keywords(_ words: [String]) -> [String] {
@@ -116,8 +130,8 @@ public enum Denote {
116130 return name
117131 }
118132
119 /// The `#+title`, `#+filetags` and `#+identifier` of a file's front matter.
120 public static func frontMatter(of text: String) -> (title: String?, keywords: [String]?, identifier: String?) {
133 /// The `#+title`, `#+filetags`, `#+identifier` and `#+signature` of a file's front matter.
134 public static func frontMatter(of text: String) -> (title: String?, keywords: [String]?, identifier: String?, signature: String?) {
121135 func value(_ key: String) -> String? {
122136 for line in text.split(separator: "\n", omittingEmptySubsequences: false).prefix(40) {
123137 let trimmed = line.trimmingCharacters(in: .whitespaces)
@@ -127,19 +141,21 @@ public enum Denote {
127141 return nil
128142 }
129143 let keywords = value("filetags").map { $0.split(separator: ":").map(String.init).filter { !$0.isEmpty } }
130 return (value("title"), keywords, value("identifier")?.nonEmptyOrNil)
144 return (value("title"), keywords, value("identifier")?.nonEmptyOrNil, value("signature"))
131145 }
132146
133147 /// `denote-rename-file-using-front-matter`: the name the file at `path` takes from its
134 /// front matter, keeping the identifier and signature. Nil when the file has no identifier
135 /// in its name or front matter.
148 /// front matter's title, keywords and signature, each empty when its line is missing. Nil
149 /// when the file has no front matter, or no identifier in it or its name.
136150 public static func renamed(_ path: String, text: String) -> String? {
137151 let current = name(ofFile: path)
138152 let front = frontMatter(of: text)
139 guard let identifier = current.identifier ?? front.identifier else { return nil }
140 let title = front.title ?? current.title.map { $0.replacingOccurrences(of: "-", with: " ") } ?? ""
141 let keywords = keywords(front.keywords ?? current.keywords)
142 let file = fileName(identifier: identifier, signature: current.signature, title: title, keywords: keywords,
153 guard front.title != nil || front.keywords != nil || front.identifier != nil || front.signature != nil,
154 let identifier = front.identifier ?? current.identifier else { return nil }
155 // In the front matter's order: Denote sorts keywords only as it prompts for them.
156 var seen = Set<String>()
157 let keywords = (front.keywords ?? []).map(keyword).filter { !$0.isEmpty && seen.insert($0).inserted }
158 let file = fileName(identifier: identifier, signature: front.signature.map(signature), title: front.title ?? "", keywords: keywords,
143159 extension: current.ext.isEmpty ? "org" : current.ext)
144160 return ((path as NSString).deletingLastPathComponent as NSString).appendingPathComponent(file)
145161 }
Tests/OrgAppTests/DailiesTests.swift +4 −4
@@ -61,15 +61,15 @@ struct DailiesTests {
6161 defer { try? FileManager.default.removeItem(at: folder) }
6262 let journal = Dailies(directory: folder.path, journal: Dailies.Journal())
6363 let made = try journal.ensure(Self.day)
64 #expect(made.created && (made.path as NSString).lastPathComponent == "20261011T120000--sunday-11-october-2026__journal.org")
65 #expect(try String(contentsOfFile: made.path, encoding: .utf8).hasPrefix("#+title: Sunday 11 October 2026\n#+date: [2026-10-11 Sun 12:00]\n#+filetags: :journal:\n"))
64 #expect(made.created && (made.path as NSString).lastPathComponent == "20261011T120000--sunday-11-october-2026-1200__journal.org")
65 #expect(try String(contentsOfFile: made.path, encoding: .utf8).hasPrefix("#+title: Sunday 11 October 2026 12:00\n#+date: [2026-10-11 Sun 12:00]\n#+filetags: :journal:\n"))
6666 // Another time the same day finds the entry.
6767 let later = Calendar.current.date(byAdding: .hour, value: 3, to: Self.day)!
6868 #expect(try journal.ensure(later) == (made.path, false))
6969 try journal.ensure(Calendar.current.date(byAdding: .day, value: -2, to: Self.day)!)
7070 try "x".write(to: folder.appendingPathComponent("20261010T080000--not-a-journal__work.org"), atomically: true, encoding: .utf8)
71 #expect(journal.files().map { ($0 as NSString).lastPathComponent } == ["20261009T120000--friday-9-october-2026__journal.org", (made.path as NSString).lastPathComponent])
72 #expect(try (journal.neighbor(of: made.path, by: -1) as NSString).lastPathComponent == "20261009T120000--friday-9-october-2026__journal.org")
71 #expect(journal.files().map { ($0 as NSString).lastPathComponent } == ["20261009T120000--friday-9-october-2026-1200__journal.org", (made.path as NSString).lastPathComponent])
72 #expect(try (journal.neighbor(of: made.path, by: -1) as NSString).lastPathComponent == "20261009T120000--friday-9-october-2026-1200__journal.org")
7373 }
7474
7575 @Test func newDenoteNote() throws {
Tests/OrgAppTests/DenoteOracleTests.swift added +95
@@ -0,0 +1,95 @@
1import Foundation
2import Testing
3@testable import OrgApp
4import OrgCore
5
6/// Denote and denote-journal themselves, run in Emacs, when they're installed: the load-path
7/// directories in `ORGSTAR_DENOTE` (colon-separated), else a Doom or package.el install.
8struct DenoteOracleTests {
9 static let loadPath: [String]? = {
10 if let paths = ProcessInfo.processInfo.environment["ORGSTAR_DENOTE"] { return paths.split(separator: ":").map(String.init) }
11 let fm = FileManager.default, home = NSHomeDirectory()
12 let roots = ["\(home)/.config/emacs/.local/straight", "\(home)/.emacs.d/.local/straight"].flatMap { root in
13 ((try? fm.contentsOfDirectory(atPath: root)) ?? []).filter { $0.hasPrefix("build-") }.sorted().reversed().map { "\(root)/\($0)" }
14 } + ["\(home)/.emacs.d/elpa", "\(home)/.config/emacs/elpa"]
15 for root in roots {
16 let names = (try? fm.contentsOfDirectory(atPath: root)) ?? []
17 let dirs = ["denote", "denote-journal"].compactMap { package in
18 names.filter { $0 == package || ($0.hasPrefix(package + "-") && $0.dropFirst(package.count + 1).first?.isNumber == true) }
19 .sorted().last.map { "\(root)/\($0)" }
20 }
21 if dirs.count == 2 { return dirs }
22 }
23 return nil
24 }()
25
26 static var enabled: Bool { loadPath != nil && ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil }
27
28 /// Runs `body` with Denote loaded, `denote-directory` at `folder` and nothing backed up.
29 static func denote(_ folder: URL, _ body: String) throws {
30 let paths = loadPath!.map(\.debugDescription).joined(separator: " ")
31 _ = try DailiesTests.emacs("""
32 (progn (setq load-path (append (list \(paths)) load-path) make-backup-files nil auto-save-default nil create-lockfiles nil
33 coding-system-for-read 'utf-8 coding-system-for-write 'utf-8)
34 (require 'denote) (require 'denote-journal)
35 (setq denote-directory \((folder.path + "/").debugDescription) denote-rename-confirmations nil)
36 \(body))
37 """)
38 }
39
40 static func folder() throws -> URL {
41 let url = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-denote-\(UUID().uuidString)").resolvingSymlinksInPath()
42 try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
43 return url
44 }
45
46 static func lines(_ url: URL) throws -> [String] {
47 try String(contentsOf: url, encoding: .utf8).split(separator: "\n", omittingEmptySubsequences: false).map(String.init)
48 }
49
50 @Test(.enabled(if: enabled)) func slugsMatchDenote() throws {
51 let titles = ["Hello, World: a_test -- Note!", " Café au lait ", "C++ & Rust!", "Привет мир", "x=y/z", "a - b", "It's [done]", "日本語のノート", "a <b> c\\d"]
52 let words = ["Emacs", "org-mode", "two words", "a_b", "Café", "x=1"]
53 let dir = try Self.folder()
54 defer { try? FileManager.default.removeItem(at: dir) }
55 let input = dir.appendingPathComponent("in.txt"), output = dir.appendingPathComponent("out.txt")
56 try (titles + words).joined(separator: "\n").write(to: input, atomically: true, encoding: .utf8)
57 try Self.denote(dir, """
58 (let ((lines (split-string (with-temp-buffer (insert-file-contents \(input.path.debugDescription)) (buffer-string)) "\\n")))
59 (with-temp-file \(output.path.debugDescription)
60 (dotimes (i (length lines))
61 (insert (denote-sluggify (if (< i \(titles.count)) 'title 'keyword) (nth i lines)) "\\n"))))
62 """)
63 let theirs = try Self.lines(output)
64 #expect(Array(theirs.prefix(titles.count)) == titles.map(Denote.slug))
65 #expect(Array(theirs.dropFirst(titles.count).prefix(words.count)) == words.map(Denote.keyword))
66 }
67
68 /// A note, a journal entry and a rename, as files Denote and orgstar each make.
69 @Test(.enabled(if: enabled)) func filesMatchDenote() throws {
70 let theirs = try Self.folder(), ours = try Self.folder()
71 defer { try? FileManager.default.removeItem(at: theirs); try? FileManager.default.removeItem(at: ours) }
72 let front = "#+title: New Title\n#+signature: Two Words\n#+filetags: :Zeta:alpha:\n#+identifier: 20261011T080000\n\nBody\n"
73 let old = "20261011T080000==1a--old__x.org"
74 for dir in [theirs, ours] { try front.write(to: dir.appendingPathComponent(old), atomically: true, encoding: .utf8) }
75 try Self.denote(theirs, """
76 (progn
77 (denote "Field Notes" (denote-keywords-sort (list "work" "ideas")) 'org nil "2026-10-11 12:00:05")
78 (save-buffer)
79 (let ((denote-journal-directory nil)) (denote-journal-new-entry "2026-10-11 12:00:00") (save-buffer))
80 (denote-rename-file-using-front-matter \(theirs.appendingPathComponent(old).path.debugDescription)))
81 """)
82 _ = try DenoteNote.create(title: "Field Notes", keywords: ["work", "ideas"], in: ours.path, now: DailiesTests.day.addingTimeInterval(5))
83 _ = try Dailies(directory: ours.path, journal: Dailies.Journal()).ensure(DailiesTests.day)
84 let renamed = Denote.renamed(ours.appendingPathComponent(old).path, text: front)!
85 try FileManager.default.moveItem(atPath: ours.appendingPathComponent(old).path, toPath: renamed)
86
87 let names = try FileManager.default.contentsOfDirectory(atPath: theirs.path).sorted()
88 #expect(names.count == 3)
89 #expect(try FileManager.default.contentsOfDirectory(atPath: ours.path).sorted() == names)
90 for name in names {
91 #expect(try String(contentsOf: ours.appendingPathComponent(name), encoding: .utf8)
92 == String(contentsOf: theirs.appendingPathComponent(name), encoding: .utf8), "\(name)")
93 }
94 }
95}
Tests/OrgAppTests/EmacsImportTests.swift +1 −1
@@ -277,7 +277,7 @@ struct EmacsImportTests {
277277 #expect(s["denote-directory"] == .string("~/notes/"))
278278 #expect(s["denote-journal-directory"] == .string("~/notes/journal/"))
279279 #expect(s["denote-journal-keyword"] == .string("diary"))
280 #expect(s["denote-journal-title-format"] == .string("%A %e %B %Y %H:%M"))
280 #expect(s["denote-journal-title-format"] == .string("%A %-e %B %Y %H:%M"))
281281 }
282282
283283 @Test func readsPublishingProjects() async throws {
Tests/OrgCoreTests/DenoteTests.swift +7 −3
@@ -54,9 +54,13 @@ struct DenoteTests {
5454
5555 @Test func renamesFromFrontMatter() {
5656 let text = "#+title: New Title\n#+filetags: :Zeta:alpha:\n#+identifier: 20261119T090503\n\nBody\n"
57 #expect(Denote.renamed("/n/20261119T090503==1a--old__x.org", text: text) == "/n/20261119T090503==1a--new-title__alpha_zeta.org")
58 #expect(Denote.renamed("/n/plain.org", text: text) == "/n/20261119T090503--new-title__alpha_zeta.org")
57 // The signature, like the title and keywords, comes from the front matter.
58 #expect(Denote.renamed("/n/20261119T090503==1a--old__x.org", text: text) == "/n/20261119T090503--new-title__zeta_alpha.org")
59 #expect(Denote.renamed("/n/20261119T090503==1a--old__x.org", text: "#+signature: Two Words\n" + text)
60 == "/n/20261119T090503==two=words--new-title__zeta_alpha.org")
61 #expect(Denote.renamed("/n/plain.org", text: text) == "/n/20261119T090503--new-title__zeta_alpha.org")
5962 #expect(Denote.renamed("/n/plain.org", text: "#+title: x\n") == nil)
60 #expect(Denote.renamed("/n/20261119T090503--old-name__x.org", text: "No front matter\n") == "/n/20261119T090503--old-name__x.org")
63 #expect(Denote.renamed("/n/20261119T090503--old-name__x.org", text: "No front matter\n") == nil)
64 #expect(Denote.renamed("/n/20261119T090503--old-name__x.org", text: "#+title: Only a title\n") == "/n/20261119T090503--only-a-title.org")
6165 }
6266}
docs/manual/guide/09-links.org +5 −4
@@ -358,11 +358,12 @@ With =notes-package = "denote"= in =config.toml=, the same commands follow Denot
358358- The daily-note commands work on denote-journal's entries: notes in
359359 =denote-journal-directory= (=journal=, under =denote-directory=) with the
360360 =denote-journal-keyword= (=journal=). A day's entry is the one whose identifier has its
361 date; a new one is titled by =denote-journal-title-format= (="%A %e %B %Y"=). Capturing
361 date; a new one is titled by =denote-journal-title-format= (="%A %-e %B %Y %H:%M"=). Capturing
362362 adds =org-roam-dailies-entry= as for org-roam.
363- Rename Note from Front Matter renames the open file from its =#+title:= and
364 =#+filetags:=, keeping its identifier and signature, as
365 =denote-rename-file-using-front-matter= does. Links keep working, as they use the
363- Rename Note from Front Matter renames the open file from its =#+title:=,
364 =#+filetags:= and =#+signature:=, keeping its identifier, as
365 =denote-rename-file-using-front-matter= does. A missing line leaves that part out of
366 the name, and the keywords keep their order. Links keep working, as they use the
366367 identifier.
367368
368369Import from Emacs reads =denote-directory=, =denote-journal-directory=,
docs/manual/guide/13-configuration.org +1 −1
@@ -243,7 +243,7 @@ A key you remove from the file goes back to its default. A key that isn't in the
243243| =denote-directory= | string | =""= | The folder new Denote notes go in; empty for the first sidebar folder. Mirrors =denote-directory=. |
244244| =denote-journal-directory= | string | ="journal"= | The folder journal entries are in, relative to =denote-directory=. Mirrors =denote-journal-directory=. |
245245| =denote-journal-keyword= | string | ="journal"= | The keyword journal entries have. Mirrors =denote-journal-keyword=. |
246| =denote-journal-title-format= | string | ="%A %e %B %Y"= | A new journal entry's title, a =format-time-string= format. |
246| =denote-journal-title-format= | string | ="%A %-e %B %Y %H:%M"= | A new journal entry's title, a =format-time-string= format. |
247247| =org-timer-format= | string | ="%s "= | What Insert Timer Value writes, with =%s= for the =h:mm:ss= value. Mirrors =org-timer-format=. |
248248| =org-timer-default-timer= | string | ="0"= | The countdown Set Countdown Timer… offers, in minutes or =h:mm:ss=; ="0"= offers none. Mirrors =org-timer-default-timer=. |
249249| =org-attach-id-dir= | string | ="data/"= | The folder ID-based attachment folders go in, relative to the org file's folder unless absolute. See [[file:04-outlines.org][Outlines]]. Mirrors =org-attach-id-dir=. |