Sort agenda by time before habits !307
4 files changed, +35 −7
Layout: unified · split
Sources/OrgCore/Agenda/AgendaSort.swift +2 −1
| @@ -10,8 +10,9 @@ public enum AgendaSort { | |||
| 10 | "todo-state-down", "habit-up", "habit-down", "alpha-up", "alpha-down", | 10 | "todo-state-down", "habit-up", "habit-down", "alpha-up", "alpha-down", |
| 11 | ] | 11 | ] |
| 12 | 12 | ||
| 13 | /// Org's, except the agenda sorts by time before habits, so timed habits go among timed entries. | ||
| 13 | public static let defaults: [String: [String]] = [ | 14 | 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"], |
| 15 | "todo": ["urgency-down", "category-keep"], | 16 | "todo": ["urgency-down", "category-keep"], |
| 16 | "tags": ["urgency-down", "category-keep"], | 17 | "tags": ["urgency-down", "category-keep"], |
| 17 | "search": ["category-keep"], | 18 | "search": ["category-keep"], |
Sources/OrgPresentation/AgendaRows.swift +6 −5
| @@ -327,7 +327,8 @@ public enum AgendaRows { | |||
| 327 | /// today always stays. | 327 | /// today always stays. |
| 328 | /// - Today's past-scheduled entries and past deadlines (not habits) go in `overdue`, | 328 | /// - Today's past-scheduled entries and past deadlines (not habits) go in `overdue`, |
| 329 | /// oldest date first, Org's order among the same date. | 329 | /// 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). | ||
| 331 | /// All-day events come first, timed events go among the timed entries by start time, | 332 | /// All-day events come first, timed events go among the timed entries by start time, |
| 332 | /// after entries at the same time. Reminders due before today show in today's | 333 | /// after entries at the same time. Reminders due before today show in today's |
| 333 | /// `overdue`. | 334 | /// `overdue`. |
| @@ -372,8 +373,8 @@ public enum AgendaRows { | |||
| 372 | return groups | 373 | return groups |
| 373 | } | 374 | } |
| 374 | 375 | ||
| 375 | /// `org` (in Org's order) with event rows added: untimed events first, timed ones before | 376 | /// `org` (in agenda 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. | 377 | /// the first untimed entry, or the first timed entry starting later. |
| 377 | static func merge(_ org: [AgendaRow], events: [AgendaRow]) -> [AgendaRow] { | 378 | static func merge(_ org: [AgendaRow], events: [AgendaRow]) -> [AgendaRow] { |
| 378 | let allDay = events.filter { $0.start == nil } | 379 | let allDay = events.filter { $0.start == nil } |
| 379 | var result = org | 380 | var result = org |
| @@ -383,7 +384,7 @@ public enum AgendaRows { | |||
| 383 | for event in timed { | 384 | for event in timed { |
| 384 | let at = result.firstIndex { row in | 385 | let at = result.firstIndex { row in |
| 385 | if row.isEvent { return row.start.map { $0 > event.start! } ?? false } | 386 | 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 } |
| 387 | return start > event.start! | 388 | return start > event.start! |
| 388 | } ?? result.count | 389 | } ?? result.count |
| 389 | result.insert(event, at: at) | 390 | result.insert(event, at: at) |
| @@ -405,7 +406,7 @@ public enum AgendaRows { | |||
| 405 | 406 | ||
| 406 | /// See `AgendaDayGroup.nowIndex(now:)`. | 407 | /// See `AgendaDayGroup.nowIndex(now:)`. |
| 407 | public static func nowIndex(_ rows: [AgendaRow], now: Int) -> Int? { | 408 | 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 } |
| 409 | guard let last = timed.last else { return nil } | 410 | guard let last = timed.last else { return nil } |
| 410 | return timed.first { rows[$0].start! > now } ?? last + 1 | 411 | return timed.first { rows[$0].start! > now } ?? last + 1 |
| 411 | } | 412 | } |
Tests/OrgCoreTests/AgendaTests.swift +1
| @@ -123,6 +123,7 @@ enum AgendaOracle { | |||
| 123 | if logMode { form = "(let ((org-agenda-start-with-log-mode t)) \(form))" } | 123 | if logMode { form = "(let ((org-agenda-start-with-log-mode t)) \(form))" } |
| 124 | if let logItems { form = "(let ((org-agenda-log-mode-items '(\(logItems.sorted().joined(separator: " "))))) \(form))" } | 124 | if let logItems { form = "(let ((org-agenda-log-mode-items '(\(logItems.sorted().joined(separator: " "))))) \(form))" } |
| 125 | if skipIfDone { form = "(let ((org-agenda-skip-scheduled-if-done t) (org-agenda-skip-deadline-if-done t)) \(form))" } | 125 | 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))" | ||
| 126 | let emacs = try run(files: files, form: form, today: today, hour: hour, minute: minute) | 127 | let emacs = try run(files: files, form: form, today: today, hour: hour, minute: minute) |
| 127 | let defaults = OrgSettings.default.withTodoKeywords(keywords)! | 128 | let defaults = OrgSettings.default.withTodoKeywords(keywords)! |
| 128 | let sources = files.map { AgendaSource(path: $0.name, text: $0.text, defaults: defaults) } | 129 | 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 { | |||
| 165 | #expect(later.nowIndex(now: 900) == nil) | 165 | #expect(later.nowIndex(now: 900) == nil) |
| 166 | } | 166 | } |
| 167 | 167 | ||
| 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 | |||
| 168 | @Test func gridLinesGoBeforeLaterRows() { | 193 | @Test func gridLinesGoBeforeLaterRows() { |
| 169 | let today = AgendaRows.groups(Self.days(), today: Self.today)[0] | 194 | let today = AgendaRows.groups(Self.days(), today: Self.today)[0] |
| 170 | #expect(today.grid.map(\.time) == [800, 1000, 1200, 1400, 1600, 1800, 2000]) | 195 | #expect(today.grid.map(\.time) == [800, 1000, 1200, 1400, 1600, 1800, 2000]) |
| @@ -175,7 +200,7 @@ struct AgendaRowsTests { | |||
| 175 | for line in lines { | 200 | for line in lines { |
| 176 | switch line { | 201 | switch line { |
| 177 | case .row(let index): | 202 | 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 } |
| 179 | case .grid(let time): #expect(time >= last); last = time | 204 | case .grid(let time): #expect(time >= last); last = time |
| 180 | case .now: #expect(1100 >= last); last = 1100 | 205 | case .now: #expect(1100 >= last); last = 1100 |
| 181 | } | 206 | } |