Commit e518d4306e

e518d4306efc336557ef948ed8d1902136a49b76

parent: c8fa991f5c

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-11 08:35 UTC

Diary sexps: holidays

calendar-check-holidays, org-calendar-holiday, and org-class skipping holidays (all with 'holidays, or by name) evaluate with Emacs's general (US) and Christian holiday lists, oracle-tested for 2024-2030. The Hebrew, Islamic, Baha'i, Chinese and solar lists are not included.

Closes #47

Layout: unified · split

Sources/OrgCore/Agenda/DiarySexp.swift +42 −2
@@ -71,6 +71,37 @@ public enum DiarySexp {
7171 return 7 * (n + 1) + onOrBefore(Days.absolute(year: year, month: month, day: try day ?? lastDay(month: month, year: year)))
7272 }
7373
74 /// `holiday-easter-etc-abs`.
75 static func easter(_ year: Int) -> Int {
76 let century = year / 100 + 1
77 let shiftedEpact = (14 + 11 * (year % 19) - (3 * century) / 4 + (5 + 8 * century) / 25 + 30 * century) % 30
78 let adjustedEpact = shiftedEpact == 0 || (shiftedEpact == 1 && 10 < year % 19) ? shiftedEpact + 1 : shiftedEpact
79 let paschalMoon = Days.absolute(year: year, month: 4, day: 19) - adjustedEpact
80 let date = paschalMoon + 7
81 return date - date % 7
82 }
83
84 /// `calendar-check-holidays` with the holidays of Emacs's default `calendar-holidays` that
85 /// follow fixed rules: `holiday-general-holidays` (the United States') and
86 /// `holiday-christian-holidays` without `calendar-christian-all-holidays-flag`.
87 static func holidays(_ day: Int) -> [String] {
88 let year = Days.date(day).year
89 func fixed(_ month: Int, _ d: Int) -> Int { Days.absolute(year: year, month: month, day: d) }
90 func float(_ month: Int, _ dayname: Int, _ n: Int) -> Int { (try? nthNamedAbsday(n, dayname, month, year, nil)) ?? 0 }
91 let easter = easter(year)
92 let list: [(Int, String)] = [
93 (fixed(1, 1), "New Year's Day"), (float(1, 1, 3), "Martin Luther King Day"), (fixed(2, 2), "Groundhog Day"),
94 (fixed(2, 14), "Valentine's Day"), (float(2, 1, 3), "President's Day"), (fixed(3, 17), "St. Patrick's Day"),
95 (fixed(4, 1), "April Fools' Day"), (float(5, 0, 2), "Mother's Day"), (float(5, 1, -1), "Memorial Day"),
96 (fixed(6, 14), "Flag Day"), (float(6, 0, 3), "Father's Day"), (fixed(7, 4), "Independence Day"),
97 (float(9, 1, 1), "Labor Day"), (float(10, 1, 2), "Columbus Day"), (fixed(10, 31), "Halloween"),
98 (fixed(11, 11), "Veteran's Day"), (float(11, 4, 4), "Thanksgiving"),
99 (easter - 46, "Ash Wednesday"), (easter - 2, "Good Friday"), (easter, "Easter Sunday"), (fixed(12, 25), "Christmas"),
100 ]
101 // `calendar-holiday-list` puts each form's holidays before the earlier ones'.
102 return list.filter { $0.0 == day }.map(\.1).reversed()
103 }
104
74105 /// `diary-ordinal-suffix`.
75106 static func ordinalSuffix(_ n: Int) throws -> String {
76107 if [11, 12, 13].contains(n % 100) || 3 < n % 10 { return "th" }
@@ -268,6 +299,14 @@ public enum DiarySexp {
268299 lisp.define("org-cyclic") { lisp, args in try iso(lisp) { try lisp.callNamed("diary-cyclic", args) } }
269300 lisp.define("org-block") { lisp, args in try iso(lisp) { try lisp.callNamed("diary-block", args) } }
270301 lisp.define("org-date") { lisp, args in try iso(lisp) { try lisp.callNamed("diary-date", args) } }
302 lisp.define("calendar-check-holidays") { _, args in
303 try arity(args, 1...1, "calendar-check-holidays")
304 return Elisp.list(holidays(try absolute(args[0])).map(Sexp.string))
305 }
306 lisp.define("org-calendar-holiday") { _, _ in
307 let names = holidays(try absolute(try date()))
308 return names.isEmpty ? .nil : .string(names.joined(separator: "; "))
309 }
271310 lisp.define("org-class") { _, args in
272311 try arity(args, 7...Int.max, "org-class")
273312 let ints = try args.prefix(7).map(bounded)
@@ -275,10 +314,11 @@ public enum DiarySexp {
275314 let last = Days.absolute(year: ints[3], month: ints[4], day: ints[5])
276315 let d = try absolute(try date())
277316 let skip = Array(args.dropFirst(7))
278 // Holiday names and `holidays' need the calendar's holiday lists.
279 guard skip.allSatisfy({ $0.integer != nil }) else { throw Elisp.Unsupported(what: "org-class holidays") }
280317 let week = Days.isoWeek(d)
281318 guard first <= d, d <= last, Days.weekday(d) == ints[6], !skip.contains(.integer(week)) else { return .nil }
319 // `holidays' skips every holiday, a holiday's name that one.
320 let names = skip.isEmpty ? [] : holidays(d)
321 if !names.isEmpty, skip.contains(.symbol("holidays")) || names.contains(where: { skip.contains(.string($0)) }) { return .nil }
282322 return try entry()
283323 }
284324 }
Tests/OrgCoreTests/DiarySexpTests.swift +39
@@ -72,3 +72,42 @@ struct DiarySexpTests {
7272 try AgendaOracle.compare(files: files, start: leap, span: 4, today: leap)
7373 }
7474}
75
76/// `calendar-check-holidays` with the lists orgstar has: Emacs's general and Christian holidays.
77struct DiaryHolidayTests {
78 @Test(.enabled(if: DiarySexpTests.oracle))
79 func checksHolidaysLikeEmacs() throws {
80 let first = Days.absolute(year: 2024, month: 1, day: 1)
81 let last = Days.absolute(year: 2030, month: 12, day: 31)
82 let form = """
83 (progn
84 (require 'holidays)
85 (let ((calendar-christian-all-holidays-flag nil)
86 (calendar-holidays (append holiday-general-holidays holiday-christian-holidays))
87 out)
88 (dotimes (i \(last - first + 1))
89 (let ((names (calendar-check-holidays (calendar-gregorian-from-absolute (+ \(first) i)))))
90 (when names (push (format "%d %s" (+ \(first) i) (mapconcat #'identity names "|")) out))))
91 (nreverse out)))
92 """
93 let emacs = try EmacsOracle.evaluate("", form)
94 let ours = (first...last).compactMap { day -> String? in
95 let names = DiarySexp.holidays(day)
96 return names.isEmpty ? nil : "\(day) " + names.joined(separator: "|")
97 }
98 #expect(ours == emacs, "\(Array(Set(ours).symmetricDifference(Set(emacs))).sorted().prefix(10))")
99 }
100
101 @Test func holidaySexps() throws {
102 let thanksgiving = Days.absolute(year: 2026, month: 11, day: 26)
103 func read(_ text: String) throws -> Sexp { try LispReader.read(text)[0].sexp }
104 #expect(DiarySexp.entries(try read("(org-calendar-holiday)"), entry: "", day: thanksgiving) == ["Thanksgiving"])
105 #expect(DiarySexp.entries(try read("(org-calendar-holiday)"), entry: "", day: thanksgiving + 1) == nil)
106 // Thursdays in November, skipping holidays, then only Thanksgiving by name.
107 let thursdays = "(org-class 2026 11 1 2026 11 30 4"
108 #expect(DiarySexp.entries(try read(thursdays + " 'holidays)"), entry: "Class", day: thanksgiving) == nil)
109 #expect(DiarySexp.entries(try read(thursdays + " \"Thanksgiving\")"), entry: "Class", day: thanksgiving) == nil)
110 #expect(DiarySexp.entries(try read(thursdays + " \"Christmas\")"), entry: "Class", day: thanksgiving) == ["Class"])
111 #expect(DiarySexp.entries(try read(thursdays + " 'holidays)"), entry: "Class", day: thanksgiving - 7) == ["Class"])
112 }
113}
docs/manual/guide/07-agenda.org +6 −3
@@ -221,11 +221,14 @@ Lines before the first heading are ignored. A sexp that uses a function orgstar
221221| =org-cyclic= /n year month day/ | As =diary-cyclic= | year month day |
222222| =org-block= /y1 m1 d1 y2 m2 d2/ | As =diary-block= | year month day |
223223| =org-date= /year month day/ | As =diary-date= | year month day |
224| =org-class= /y1 m1 d1 y2 m2 d2 dayname/ [/skip-weeks…/] | On /dayname/ between the dates, except the ISO weeks listed | year month day |
224| =org-class= /y1 m1 d1 y2 m2 d2 dayname/ [/skip-weeks…/] | On /dayname/ between the dates, except the ISO weeks listed, and holidays: all with ='holidays=, or those named | year month day |
225| =org-calendar-holiday= | On holidays, showing their names | n/a |
225226
226The =diary-= functions read dates in =calendar-date-style= =american= (month, day, year); the =org-= functions use ISO order. =org-class= skip lists take ISO week numbers only; holiday names and =holidays= need the Emacs calendar's holiday lists and make the sexp fail.
227The =diary-= functions read dates in =calendar-date-style= =american= (month, day, year); the =org-= functions use ISO order.
227228
228For your own expressions, the variables =date= (as =(month day year)=) and =entry= are bound, and these calendar functions are available: =calendar-extract-month=, =calendar-extract-day=, =calendar-extract-year=, =calendar-absolute-from-gregorian=, =calendar-gregorian-from-absolute=, =calendar-day-of-week=, =calendar-leap-year-p=, =calendar-last-day-of-month=, =calendar-date-equal=, =calendar-day-number=, =calendar-nth-named-absday=, =calendar-nth-named-day=, =calendar-iso-from-absolute=, =diary-ordinal-suffix= and =diary-make-date=. The evaluator also has the common special forms (=if=, =when=, =cond=, =and=, =or=, =let=, =progn= and others) and arithmetic. It is a small subset of Emacs Lisp, not Emacs.
229The holidays are those of Emacs's default =calendar-holidays= that follow fixed rules: =holiday-general-holidays= (New Year's Day, Martin Luther King Day, Groundhog Day, Valentine's Day, President's Day, St. Patrick's Day, April Fools' Day, Mother's Day, Memorial Day, Flag Day, Father's Day, Independence Day, Labor Day, Columbus Day, Halloween, Veteran's Day, Thanksgiving) and =holiday-christian-holidays= (Ash Wednesday, Good Friday, Easter Sunday, Christmas). The Hebrew, Islamic, Bahá'í, Chinese and solar holidays, and holidays you add to =calendar-holidays= in Emacs, are not included.
230
231For your own expressions, the variables =date= (as =(month day year)=) and =entry= are bound, and these calendar functions are available: =calendar-extract-month=, =calendar-extract-day=, =calendar-extract-year=, =calendar-absolute-from-gregorian=, =calendar-gregorian-from-absolute=, =calendar-day-of-week=, =calendar-leap-year-p=, =calendar-last-day-of-month=, =calendar-date-equal=, =calendar-day-number=, =calendar-nth-named-absday=, =calendar-nth-named-day=, =calendar-iso-from-absolute=, =calendar-check-holidays=, =diary-ordinal-suffix= and =diary-make-date=. The evaluator also has the common special forms (=if=, =when=, =cond=, =and=, =or=, =let=, =progn= and others) and arithmetic. It is a small subset of Emacs Lisp, not Emacs.
229232
230233A sexp line that returns a string shows that string; one that returns a list of strings shows one line per string; otherwise the line's text after the sexp shows.
231234