Commit 8ce7e08926

8ce7e089269b399a25d000a5650c5d7f1b1653d9

parent: 480e2db2d0

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-12 00:15 UTC

Sort agenda by time before habits

Timed habits go among timed entries; the now line and calendar events
place around them by start time.

Layout: unified · split

Sources/OrgCore/Agenda/AgendaSort.swift +2 −1
@@ -10,8 +10,9 @@ public enum AgendaSort {
1010 "todo-state-down", "habit-up", "habit-down", "alpha-up", "alpha-down",
1111 ]
1212
13 /// Org's, except the agenda sorts by time before habits, so timed habits go among timed entries.
1314 public static let defaults: [String: [String]] = [
14 "agenda": ["habit-down", "time-up", "urgency-down", "category-keep"],
15 "agenda": ["time-up", "habit-down", "urgency-down", "category-keep"],
1516 "todo": ["urgency-down", "category-keep"],
1617 "tags": ["urgency-down", "category-keep"],
1718 "search": ["category-keep"],
Sources/OrgPresentation/AgendaRows.swift +6 −5
@@ -327,7 +327,8 @@ public enum AgendaRows {
327327 /// today always stays.
328328 /// - Today's past-scheduled entries and past deadlines (not habits) go in `overdue`,
329329 /// oldest date first, Org's order among the same date.
330 /// - Other rows keep Org's order (timed by time, then untimed by urgency, habits last).
330 /// - Other rows keep the agenda's order (timed by time, then untimed by urgency, habits
331 /// after other entries at the same time).
331332 /// All-day events come first, timed events go among the timed entries by start time,
332333 /// after entries at the same time. Reminders due before today show in today's
333334 /// `overdue`.
@@ -372,8 +373,8 @@ public enum AgendaRows {
372373 return groups
373374 }
374375
375 /// `org` (in Org's order) with event rows added: untimed events first, timed ones before
376 /// the first untimed or habit entry, or the first timed entry starting later.
376 /// `org` (in agenda order) with event rows added: untimed events first, timed ones before
377 /// the first untimed entry, or the first timed entry starting later.
377378 static func merge(_ org: [AgendaRow], events: [AgendaRow]) -> [AgendaRow] {
378379 let allDay = events.filter { $0.start == nil }
379380 var result = org
@@ -383,7 +384,7 @@ public enum AgendaRows {
383384 for event in timed {
384385 let at = result.firstIndex { row in
385386 if row.isEvent { return row.start.map { $0 > event.start! } ?? false }
386 guard let start = row.start, row.item?.habit == nil else { return true }
387 guard let start = row.start else { return true }
387388 return start > event.start!
388389 } ?? result.count
389390 result.insert(event, at: at)
@@ -405,7 +406,7 @@ public enum AgendaRows {
405406
406407 /// See `AgendaDayGroup.nowIndex(now:)`.
407408 public static func nowIndex(_ rows: [AgendaRow], now: Int) -> Int? {
408 let timed = rows.indices.filter { rows[$0].start != nil && rows[$0].item?.habit == nil }
409 let timed = rows.indices.filter { rows[$0].start != nil }
409410 guard let last = timed.last else { return nil }
410411 return timed.first { rows[$0].start! > now } ?? last + 1
411412 }
Tests/OrgCoreTests/AgendaTests.swift +1
@@ -123,6 +123,7 @@ enum AgendaOracle {
123123 if logMode { form = "(let ((org-agenda-start-with-log-mode t)) \(form))" }
124124 if let logItems { form = "(let ((org-agenda-log-mode-items '(\(logItems.sorted().joined(separator: " "))))) \(form))" }
125125 if skipIfDone { form = "(let ((org-agenda-skip-scheduled-if-done t) (org-agenda-skip-deadline-if-done t)) \(form))" }
126 form = "(let ((org-agenda-sorting-strategy '((agenda \(AgendaSort.defaults["agenda"]!.joined(separator: " ")))))) \(form))"
126127 let emacs = try run(files: files, form: form, today: today, hour: hour, minute: minute)
127128 let defaults = OrgSettings.default.withTodoKeywords(keywords)!
128129 let sources = files.map { AgendaSource(path: $0.name, text: $0.text, defaults: defaults) }
Tests/OrgPresentationTests/AgendaRowsTests.swift +26 −1
@@ -165,6 +165,31 @@ struct AgendaRowsTests {
165165 #expect(later.nowIndex(now: 900) == nil)
166166 }
167167
168 @Test func timedHabitsGoAmongTimedRows() {
169 let file = """
170 * TODO Lunch
171 SCHEDULED: <2026-10-05 Mon 12:00>
172 * TODO Medication
173 SCHEDULED: <2026-10-05 Mon 20:00>
174 * TODO Bathe
175 SCHEDULED: <2026-10-05 Mon 18:30 .+1d>
176 :PROPERTIES:
177 :STYLE: habit
178 :END:
179 """
180 let source = AgendaSource(path: "life.org", text: file, defaults: OrgSettings.default.withTodoKeywords("TODO | DONE")!)
181 let days = Agenda.list([source], start: Self.today, days: 1, today: Self.today, now: (10, 0))
182 let day = Self.today
183 let events = [
184 AgendaEvent(id: "a", title: "Call", calendarID: "c1", calendarTitle: "Work", color: nil, start: Self.date(day, 19), end: Self.date(day, 19, 30), isAllDay: false),
185 ]
186 let today = AgendaRows.groups(days, events: events, today: day, calendar: Self.utc)[0]
187 #expect(today.rows.map(\.title) == ["Lunch", "Bathe", "Call", "Medication"])
188 #expect(today.nowIndex(now: 1800) == 1)
189 #expect(today.nowIndex(now: 1904) == 3)
190 #expect(today.grid.first { $0.time == 1800 }?.before == 1)
191 }
192
168193 @Test func gridLinesGoBeforeLaterRows() {
169194 let today = AgendaRows.groups(Self.days(), today: Self.today)[0]
170195 #expect(today.grid.map(\.time) == [800, 1000, 1200, 1400, 1600, 1800, 2000])
@@ -175,7 +200,7 @@ struct AgendaRowsTests {
175200 for line in lines {
176201 switch line {
177202 case .row(let index):
178 if let start = today.rows[index].start, today.rows[index].item?.habit == nil { #expect(start >= last) ; last = start }
203 if let start = today.rows[index].start { #expect(start >= last) ; last = start }
179204 case .grid(let time): #expect(time >= last); last = time
180205 case .now: #expect(1100 >= last); last = 1100
181206 }