-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchime.el
More file actions
1892 lines (1683 loc) · 78 KB
/
chime.el
File metadata and controls
1892 lines (1683 loc) · 78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; chime.el --- CHIME Heralds Imminent Modeline Events -*- lexical-binding: t -*-
;; Copyright (C) 2017 Artem Khramov
;; Copyright (C) 2024-2026 Craig Jennings
;; Current Author/Maintainer: Craig Jennings <c@cjennings.net>
;; Original Author: Artem Khramov <akhramov+emacs@pm.me>
;; Created: 6 Jan 2017
;; Version: 0.6.0
;; Package-Requires: ((alert "1.2") (async "1.9.3") (dash "2.18.0") (emacs "27.1"))
;; Keywords: notification alert org org-agenda agenda calendar chime sound
;; URL: https://github.com/cjennings/chime
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; CHIME (CHIME Heralds Imminent Modeline Events) - Customizable org-agenda notifications
;;
;; This package provides visual and audible notifications for upcoming org-agenda
;; events with modeline display of the next upcoming event.
;;
;; Features:
;; - Visual notifications with customizable alert times
;; - Audible chime sound when notifications appear
;; - Modeline display of next upcoming event
;; - Support for SCHEDULED, DEADLINE, and plain timestamps
;; - Repeating timestamp support (+1w, .+1d, ++1w)
;; - Async background checking (runs every minute)
;;
;; Quick Start:
;; (require 'chime)
;; (setq chime-alert-intervals '((5 . medium) (0 . high))) ; 5 min before and at event time
;; (chime-mode 1)
;;
;; Manual check: M-x chime-check
;;
;; Notification intervals and severity can be customized globally via
;; `chime-alert-intervals'.
;;
;; Filter notifications using `chime-keyword-whitelist' and
;; `chime-keyword-blacklist' variables.
;;
;; See README.org for complete documentation.
;;; Code:
;;;; Dependencies
(require 'dash)
(require 'alert)
(require 'async)
(require 'org-agenda)
(require 'org-duration)
(require 'cl-lib)
(require 'subr-x)
;; Declare functions from chime-debug.el (loaded conditionally)
(declare-function chime-debug-monitor-event-loading "chime-debug")
(declare-function chime-debug-enable-async-monitoring "chime-debug")
(declare-function chime--debug-log-async-error "chime-debug")
(declare-function chime--debug-log-async-complete "chime-debug")
;;;; Customization Variables
(defgroup chime nil
"Chime customization options."
:group 'org)
(defcustom chime-alert-intervals '((10 . medium))
"Alert intervals with severity levels for upcoming events.
Each element is a cons cell (MINUTES . SEVERITY) where:
- MINUTES: Number of minutes before event to notify (0 = at event time)
- SEVERITY: Alert urgency level (high, medium, or low)
Example configurations:
;; Single notification at event time with high urgency
\\='((0 . high))
;; Multiple notifications with escalating urgency
\\='((60 . low) ;; 1 hour before: low urgency
(30 . low) ;; 30 min before: low urgency
(10 . medium) ;; 10 min before: medium urgency
(0 . high)) ;; At event: high urgency
;; Same severity for all notifications
\\='((15 . medium) (5 . medium) (0 . medium))
Each interval's severity affects how the notification is displayed
by your system's notification daemon."
:package-version '(chime . "0.7.0")
:group 'chime
:type '(repeat (cons (integer :tag "Minutes before event")
(symbol :tag "Severity")))
:set (lambda (symbol value)
(unless (listp value)
(user-error "chime-alert-intervals must be a list of cons cells, got: %S" value))
(dolist (interval value)
(unless (consp interval)
(user-error "Each interval must be a cons cell (MINUTES . SEVERITY), got: %S" interval))
(let ((minutes (car interval))
(severity (cdr interval)))
(unless (integerp minutes)
(user-error "Alert time must be an integer, got: %S" minutes))
(when (< minutes 0)
(user-error "Alert time cannot be negative, got: %d" minutes))
(unless (memq severity '(high medium low))
(user-error "Severity must be high, medium, or low, got: %S" severity))))
(set-default symbol value)))
(defcustom chime-check-interval 60
"How often to check for upcoming events, in seconds.
Chime will poll your agenda files at this interval to check for
notifications. Lower values make notifications more responsive but
increase system load. Higher values reduce polling overhead but may
delay notifications slightly.
Minimum recommended value: 10 seconds.
Default: 60 seconds (1 minute).
Note: Changes take effect after restarting chime-mode."
:package-version '(chime . "0.6.0")
:group 'chime
:type 'integer
:set (lambda (symbol value)
(unless (integerp value)
(user-error "Check interval must be an integer, got: %S" value))
(when (< value 10)
(warn "chime-check-interval: Values below 10 seconds may cause excessive polling and system load"))
(when (<= value 0)
(user-error "Check interval must be positive, got: %d" value))
(set-default symbol value)))
(defcustom chime-notification-title "Agenda"
"Notifications title."
:package-version '(chime . "0.1.0")
:group 'chime
:type 'string)
(defcustom chime-notification-icon nil
"Path to notification icon file."
:package-version '(chime . "0.4.1")
:group 'chime
:type '(choice (const :tag "No icon" nil)
(file :tag "Icon file path")))
(defcustom chime-keyword-whitelist nil
"Receive notifications for these keywords only.
Leave this variable blank if you do not want to filter anything."
:package-version '(chime . "0.2.2")
:group 'chime
:type '(repeat string))
(defcustom chime-keyword-blacklist nil
"Never receive notifications for these keywords."
:package-version '(chime . "0.2.2")
:group 'chime
:type '(repeat string))
(defcustom chime-tags-whitelist nil
"Receive notifications for these tags only.
Leave this variable blank if you do not want to filter anything."
:package-version '(chime . "0.3.1")
:group 'chime
:type '(repeat string))
(defcustom chime-tags-blacklist nil
"Never receive notifications for these tags."
:package-version '(chime . "0.3.1")
:group 'chime
:type '(repeat string))
(defcustom chime-display-time-format-string "%I:%M %p"
"Format string for displaying event times.
Passed to `format-time-string' when displaying notification times.
Uses standard time format codes:
%I - Hour (01-12, 12-hour format)
%H - Hour (00-23, 24-hour format)
%M - Minutes (00-59)
%p - AM/PM designation (uppercase)
%P - am/pm designation (lowercase)
Common formats:
\"%I:%M %p\" -> \"02:30 PM\" (12-hour with AM/PM, default)
\"%H:%M\" -> \"14:30\" (24-hour)
\"%I:%M%p\" -> \"02:30PM\" (12-hour, no space before AM/PM)
\"%l:%M %p\" -> \" 2:30 PM\" (12-hour, space-padded hour)
Note: Avoid using seconds (%S) as chime polls once per minute."
:package-version '(chime . "0.5.0")
:group 'chime
:type 'string
:set (lambda (symbol value)
(when (and value (stringp value) (string-match-p "%S" value))
(warn "chime-display-time-format-string: Using seconds (%%S) is not recommended as chime polls once per minute"))
(set-default symbol value)))
(defcustom chime-time-left-format-at-event "right now"
"Format string for when event time has arrived (0 or negative seconds).
This is a literal string with no format codes."
:package-version '(chime . "0.6.0")
:group 'chime
:type 'string)
(defcustom chime-time-left-format-short "in %M"
"Format string for times under 1 hour.
Uses `format-seconds' codes:
%m - minutes as number only (e.g., \"37\")
%M - minutes with unit name (e.g., \"37 minutes\")
Examples:
\"in %M\" -> \"in 37 minutes\"
\"in %mm\" -> \"in 37m\"
\"%m min\" -> \"37 min\""
:package-version '(chime . "0.6.0")
:group 'chime
:type 'string)
(defcustom chime-time-left-format-long "in %H %M"
"Format string for times 1 hour or longer.
Uses `format-seconds' codes:
%h - hours as number only (e.g., \"1\")
%H - hours with unit name (e.g., \"1 hour\")
%m - minutes as number only (e.g., \"37\")
%M - minutes with unit name (e.g., \"37 minutes\")
Examples:
\"in %H %M\" -> \"in 1 hour 37 minutes\"
\"in %hh %mm\" -> \"in 1h 37m\"
\"(%h hr %m min)\" -> \"(1 hr 37 min)\"
\"%hh%mm\" -> \"1h37m\""
:package-version '(chime . "0.6.0")
:group 'chime
:type 'string)
(defcustom chime-predicate-whitelist nil
"Receive notifications for events matching these predicates only.
Each function should take an event POM and return non-nil iff that event should
trigger a notification. Leave this variable blank if you do not want to filter
anything."
:package-version '(chime . "0.5.0")
:group 'chime
:type '(repeat function))
(defcustom chime-additional-environment-regexes nil
"Additional regular expressions for async environment injection.
These regexes are provided to `async-inject-environment' before
running the async command to check notifications."
:package-version '(chime . "0.5.0")
:group 'chime
:type '(repeat string))
(defcustom chime-predicate-blacklist
'(chime-done-keywords-predicate)
"Never receive notifications for events matching these predicates.
Each function should take an event POM and return non-nil iff that event should
not trigger a notification."
:package-version '(chime . "0.5.0")
:group 'chime
:type '(repeat function))
(defcustom chime-extra-alert-plist nil
"Additional arguments that should be passed to invocations of `alert'."
:package-version '(chime . "0.5.0")
:group 'chime
:type 'plist)
(defcustom chime-day-wide-alert-times '("08:00")
"List of time strings for day-wide event alerts.
Each string specifies a time of day when day-wide events should trigger.
Defaults to 08:00 (morning reminder for all-day events happening today).
Set to nil to disable all-day event notifications entirely.
Example: \\='(\"08:00\" \"17:00\") for morning and evening reminders."
:package-version '(chime . "0.6.0")
:group 'chime
:type '(repeat string))
(defcustom chime-show-any-overdue-with-day-wide-alerts t
"Show any overdue TODO items along with day wide alerts whenever they are shown."
:package-version '(chime . "0.5.0")
:group 'chime
:type 'boolean)
(defcustom chime-day-wide-advance-notice nil
"Number of days before all-day events to show advance notifications.
When nil, only notify on the day of the event.
When 1, also notify the day before at `chime-day-wide-alert-times'.
When 2, notify two days before, etc.
Useful for events requiring preparation, such as birthdays (buying gifts)
or multi-day conferences (packing, travel arrangements).
Note: This only affects notifications, not tooltip/modeline display.
Example: With value 1 and alert times \\='(\"08:00\"), you'll get:
- \"Blake's birthday is tomorrow\" at 08:00 the day before
- \"Blake's birthday is today\" at 08:00 on the day"
:package-version '(chime . "0.6.0")
:group 'chime
:type '(choice (const :tag "Same day only" nil)
(integer :tag "Days in advance")))
(defcustom chime-tooltip-show-all-day-events t
"Whether to show all-day events in the tooltip.
When nil, all-day events (birthdays, multi-day conferences, etc.) are
hidden from the tooltip but can still trigger notifications.
When t, all-day events appear in the tooltip for planning purposes.
All-day events are never shown in the modeline (only in tooltip).
This is useful for seeing upcoming birthdays, holidays, and multi-day
events without cluttering the modeline with non-time-sensitive items."
:package-version '(chime . "0.6.0")
:group 'chime
:type 'boolean)
(defcustom chime-enable-modeline t
"Whether to display upcoming events in the modeline.
When nil, chime will not modify the modeline at all."
:package-version '(chime . "0.6.0")
:group 'chime
:type 'boolean)
(defcustom chime-modeline-lighter " 🔔"
"Text to display in the modeline when chime-mode is enabled.
This is the mode lighter that appears in the modeline to indicate
chime-mode is active."
:package-version '(chime . "0.7.0")
:group 'chime
:type 'string)
(defcustom chime-modeline-lookahead-minutes 60
"Minutes ahead to look for next event to display in modeline.
Should be larger than notification alert times for advance awareness.
Set to 0 to disable modeline display.
This setting only takes effect when `chime-enable-modeline' is non-nil."
:package-version '(chime . "0.6.0")
:group 'chime
:type '(integer :tag "Minutes"))
(defcustom chime-modeline-format " ⏰ %s"
"Format string for modeline display.
%s will be replaced with the event description (time and title)."
:package-version '(chime . "0.5.1")
:group 'chime
:type 'string)
(defcustom chime-calendar-url nil
"URL to your calendar for browser access.
When set, left-clicking the modeline icon/text opens this URL in your
browser. Right-clicking jumps to the next event in your org file.
Set this to your calendar's web interface, such as:
- Google Calendar: \"https://calendar.google.com\"
- Outlook: \"https://outlook.office.com/calendar\"
- Custom calendar URL
When nil (default), left-click does nothing."
:package-version '(chime . "0.7.0")
:group 'chime
:type '(choice (const :tag "No calendar URL" nil)
(string :tag "Calendar URL")))
(defcustom chime-tooltip-lookahead-hours 8760
"Hours ahead to look for events in tooltip.
Separate from modeline lookahead window.
Default is 8760 hours (1 year), showing all future events.
The actual number of events shown is limited by
`chime-modeline-tooltip-max-events'.
Set to a smaller value to limit tooltip by time as well as count.
Example: Set to 24 to show only today's and tomorrow's events,
or keep at default to show next N events regardless of distance."
:package-version '(chime . "0.6.0")
:group 'chime
:type '(integer :tag "Hours"))
(defcustom chime-modeline-tooltip-max-events 5
"Maximum number of events to show in modeline tooltip.
Set to nil to show all events within tooltip lookahead window."
:package-version '(chime . "0.6.0")
:group 'chime
:type '(choice (integer :tag "Maximum events")
(const :tag "Show all" nil)))
(defcustom chime-modeline-no-events-text " ⏰"
"Text to display in modeline when no events are within lookahead window.
Shows an alarm icon by default.
When nil, nothing is shown in the modeline when no upcoming events.
When a string, that text is displayed.
This only applies when events exist beyond the lookahead window.
If there are no events at all, the modeline is always empty.
Examples:
\" ⏰\" - Alarm icon (default)
\" 🔕\" - Muted bell emoji
nil - Show nothing (clean modeline)
\" No events\" - Show text message"
:package-version '(chime . "0.6.0")
:group 'chime
:type '(choice (const :tag "Show nothing" nil)
(string :tag "Custom text")))
(defcustom chime-notification-text-format "%t at %T (%u)"
"Format string for notification text display.
Available placeholders:
%t - Event title
%T - Event time (formatted per `chime-display-time-format-string')
%u - Time until event (formatted per time-left format settings)
Examples:
\"%t at %T (%u)\" -> \"Team Meeting at 02:30 PM (in 10 minutes)\" (default)
\"%t at %T\" -> \"Team Meeting at 02:30 PM\" (no countdown)
\"%t (%u)\" -> \"Team Meeting (in 10 minutes)\" (no time)
\"%t - %T\" -> \"Team Meeting - 02:30 PM\" (custom separator)
\"%t\" -> \"Team Meeting\" (title only)"
:package-version '(chime . "0.6.0")
:group 'chime
:type 'string)
(defcustom chime-max-title-length nil
"Maximum length for event titles in notifications.
When non-nil, truncate titles longer than this value with \"...\".
When nil, show full title without truncation.
This affects ONLY the event title (%t in `chime-notification-text-format'),
NOT the icon, time, or countdown. The icon is part of
`chime-modeline-format' and is added separately.
Examples (assuming format \"%t (%u)\" and icon \" ⏰ \"):
nil -> \" ⏰ Very Long Meeting Title That Goes On ( in 10m)\"
25 -> \" ⏰ Very Long Meeting Titl... ( in 10m)\"
15 -> \" ⏰ Very Long Me... ( in 10m)\"
10 -> \" ⏰ Very Lo... ( in 10m)\"
The limit includes the \"...\" suffix (3 chars), so a limit of 15
means up to 12 chars of title plus \"...\".
Minimum recommended value: 10 characters."
:package-version '(chime . "0.6.0")
:group 'chime
:type '(choice (const :tag "No truncation (show full title)" nil)
(integer :tag "Maximum title length"))
:set (lambda (symbol value)
(when (and value (integerp value) (< value 5))
(warn "chime-max-title-length: Values below 5 may produce illegible titles"))
(set-default symbol value)))
(defcustom chime-tooltip-header-format "Upcoming Events as of %a %b %d %Y @ %I:%M %p"
"Format string for tooltip header showing current date/time.
Uses `format-time-string' codes.
See Info node `(elisp)Time Parsing' for details.
Common format codes:
%a - Abbreviated weekday (Mon, Tue, ...)
%A - Full weekday name (Monday, Tuesday, ...)
%b - Abbreviated month (Jan, Feb, ...)
%B - Full month name (January, February, ...)
%d - Day of month, zero-padded (01-31)
%e - Day of month, space-padded ( 1-31)
%Y - Four-digit year (2025)
%I - Hour (01-12, 12-hour format)
%H - Hour (00-23, 24-hour format)
%M - Minute (00-59)
%p - AM/PM indicator
Default: \"Upcoming Events as of %a %b %d %Y @ %I:%M %p\"
Result: \"Upcoming Events as of Tue Nov 04 2025 @ 08:25 PM\""
:package-version '(chime . "0.7.0")
:group 'chime
:type 'string)
(defcustom chime-play-sound t
"Whether to play a sound when notifications are displayed.
When non-nil, plays the sound file specified in `chime-sound-file'."
:package-version '(chime . "0.6.0")
:group 'chime
:type 'boolean)
(defcustom chime-sound-file
(expand-file-name "sounds/chime.wav"
(file-name-directory
(or load-file-name
(locate-library "chime")
buffer-file-name)))
"Path to sound file to play when notifications are displayed.
Defaults to the bundled chime.wav file.
Set to nil to disable sound completely (no sound file, no beep).
Should be an absolute path to a .wav, .au, or other sound file
supported by your system."
:package-version '(chime . "0.6.0")
:group 'chime
:type '(choice (const :tag "No sound" nil)
(file :tag "Sound file path")))
(defcustom chime-startup-delay 10
"Seconds to wait before first event check after chime-mode is enabled.
This delay allows org-agenda-files and related infrastructure to finish
loading before chime attempts to check for events.
Default of 10 seconds works well for most configurations. Adjust if:
- You have custom org-agenda-files setup that takes longer to initialize
- You want faster startup (reduce to 5) and know org is ready
- You see \"found 0 events\" messages (increase to 15 or 20)
Set to 0 to check immediately (not recommended unless you're sure
org-agenda-files is populated at startup)."
:package-version '(chime . "0.6.0")
:group 'chime
:type 'integer
:set (lambda (symbol value)
(unless (and (integerp value) (>= value 0))
(user-error "chime-startup-delay must be a non-negative integer, got: %s" value))
(set-default symbol value)))
(defcustom chime-max-consecutive-failures 5
"Number of consecutive async failures before displaying a warning.
When event checks fail this many times in a row, a warning is shown
via `display-warning'. The counter resets on any successful check.
Set to 0 to disable failure warnings."
:package-version '(chime . "0.6.0")
:group 'chime
:type 'integer)
(defcustom chime-debug nil
"Enable debug functions for troubleshooting chime behavior.
When non-nil, loads chime-debug.el which provides:
- `chime--debug-dump-events' - Show all stored upcoming events
- `chime--debug-dump-tooltip' - Show tooltip content
- `chime--debug-config' - Show complete configuration dump
- `chime-debug-monitor-event-loading' - Monitor event loading timing
These functions write detailed information to the *Messages* buffer
without cluttering the echo area.
When enabled, automatically monitors event loading to help diagnose
timing issues where the modeline takes a while to populate after
Emacs startup.
Set to t to enable debug functions:
(setq chime-debug t)
(require \\='chime)"
:package-version '(chime . "0.6.0")
:group 'chime
:type 'boolean)
;; Load debug functions if enabled
(when chime-debug
(require 'chime-debug
(expand-file-name "chime-debug.el"
(file-name-directory (or load-file-name buffer-file-name)))
t))
;; Load org-contacts integration if configured
;; Note: The actual template setup happens in chime-org-contacts.el
;; when org-capture is loaded, so users can defer org loading
(with-eval-after-load 'org-capture
(when (and (boundp 'chime-org-contacts-file)
chime-org-contacts-file)
(require 'chime-org-contacts
(expand-file-name "chime-org-contacts.el"
(file-name-directory (or load-file-name buffer-file-name)))
t)))
;;;; Internal State
(defvar chime--timer nil
"Timer value.")
(defvar chime--process nil
"Currently-running async process.")
(defvar chime--consecutive-async-failures 0
"Count of consecutive async check failures.
After `chime-max-consecutive-failures' failures, a warning is displayed.")
(defvar chime--agenda-buffer-name "*chime-agenda*"
"Name for temporary \\='org-agenda\\=' buffer.")
(defvar chime--last-check-time (seconds-to-time 0)
"Last time checked for events.")
(defvar chime--upcoming-events nil
"List of upcoming events with full data for tooltip and clicking.
Each event includes marker, title, times, and intervals.")
(defvar chime--validation-done nil
"Whether configuration validation has been performed.
Validation runs on the first call to `chime-check', after `chime-startup-delay'
has elapsed. This gives startup hooks time to populate org-agenda-files.")
(defvar chime--validation-retry-count 0
"Number of times validation has failed and been retried.
Reset to 0 when validation succeeds. Used to provide graceful retry
behavior for users with async org-agenda-files initialization.")
(defcustom chime-validation-max-retries 3
"Maximum number of times to retry validation before showing error.
When org-agenda-files is empty on startup, chime will retry validation
on each check cycle (every `chime-check-interval' seconds) until either:
- Validation succeeds (org-agenda-files is populated)
- This retry limit is exceeded (error is shown)
This accommodates users with async initialization code that populates
org-agenda-files after a delay (e.g., via idle timers).
Set to 0 to show errors immediately without retrying.
Default is 3 retries (with 30-60s check intervals, this gives ~1.5-3 minutes
for org-agenda-files to be populated)."
:type 'integer
:group 'chime)
(defvar chime-modeline-string nil
"Modeline string showing next upcoming event.")
;;;###autoload(put 'chime-modeline-string 'risky-local-variable t)
(put 'chime-modeline-string 'risky-local-variable t)
;;;; Time/Date Utilities
(defun chime--time= (&rest list)
"Compare timestamps.
Comparison is performed by converting each element of LIST to a string
in order to ignore seconds."
(->> list
(--map (format-time-string "%Y-%m-%d %H:%M" it))
(-uniq)
(length)
(= 1)))
(defun chime--today ()
"Get the timestamp for the beginning of current day."
(apply 'encode-time
(append '(0 0 0) (nthcdr 3 (decode-time (current-time))))))
(defun chime--timestamp-within-interval-p (timestamp interval)
"Check whether TIMESTAMP is within notification INTERVAL.
Returns non-nil if TIMESTAMP matches current time plus INTERVAL minutes.
Returns nil if TIMESTAMP or INTERVAL is invalid."
(and timestamp
interval
(numberp interval)
;; Validate timestamp is a proper time value (accepts list, integer, or float)
(or (listp timestamp) (numberp timestamp))
(chime--time=
(time-add (current-time) (seconds-to-time (* 60 interval)))
timestamp)))
(defun chime--notifications (event)
"Get notifications for given EVENT.
Returns a list of time information interval pairs.
Each pair is ((TIMESTAMP . TIME-VALUE) (MINUTES . SEVERITY))."
(->> (list
(chime--filter-day-wide-events (cdr (assoc 'times event)))
(cdr (assoc 'intervals event)))
(apply '-table-flat (lambda (ts int) (list ts int)))
;; When no values are provided for table flat, we get the second values
;; paired with nil.
(--filter (not (null (car it))))
;; Extract minutes from (minutes . severity) cons for time matching
(--filter (chime--timestamp-within-interval-p (cdar it) (car (cadr it))))))
(defun chime--has-timestamp (s)
"Check if S contain a timestamp with a time component.
Returns non-nil only if the timestamp includes HH:MM time information."
(and s
(stringp s)
(string-match org-ts-regexp0 s)
(match-beginning 7)))
(defun chime--filter-day-wide-events (times)
"Filter TIMES list to include only events with timestamps."
(--filter (chime--has-timestamp (car it)) times))
(defun chime--time-left (seconds)
"Human-friendly representation for SECONDS.
Format is controlled by `chime-time-left-format-at-event',
`chime-time-left-format-short', and `chime-time-left-format-long'."
(-> seconds
(pcase
((pred (>= 0)) chime-time-left-format-at-event)
((pred (>= 3600)) chime-time-left-format-short)
(_ chime-time-left-format-long))
(format-seconds seconds)))
(defun chime--get-hh-mm-from-org-time-string (time-string)
"Convert given org time-string TIME-STRING into string with \\='hh:mm\\=' format."
(format-time-string
chime-display-time-format-string
(encode-time (org-parse-time-string time-string))))
(defun chime--truncate-title (title)
"Truncate TITLE to `chime-max-title-length' if set.
Returns the truncated title with \"...\" appended if truncated,
or the original title if no truncation is needed.
Returns empty string if TITLE is nil."
(let ((title-str (or title "")))
(if (and chime-max-title-length
(integerp chime-max-title-length)
(> chime-max-title-length 0)
(> (length title-str) chime-max-title-length))
(concat (substring title-str 0 (max 0 (- chime-max-title-length 3))) "...")
title-str)))
(defun chime--notification-text (str-interval event)
"For given STR-INTERVAL list and EVENT get notification wording.
STR-INTERVAL is (TIMESTAMP-STRING . (MINUTES . SEVERITY)).
Format is controlled by `chime-notification-text-format'.
Title is truncated per `chime-max-title-length' if set."
(let* ((title (cdr (assoc 'title event)))
(minutes (car (cdr str-interval))))
(format-spec chime-notification-text-format
`((?t . ,(chime--truncate-title title))
(?T . ,(chime--get-hh-mm-from-org-time-string (car str-interval)))
(?u . ,(chime--time-left (* 60 minutes)))))))
(defun chime-get-minutes-into-day (time)
"Get minutes elapsed since midnight for TIME string."
(org-duration-to-minutes (org-get-time-of-day time t)))
(defun chime-get-hours-minutes-from-time (time-string)
"Extract hours and minutes from TIME-STRING.
Returns a list of (HOURS MINUTES)."
(let ((total-minutes (truncate (chime-get-minutes-into-day time-string))))
(list (/ total-minutes 60)
(mod total-minutes 60))))
(defun chime-set-hours-minutes-for-time (time hours minutes)
"Set HOURS and MINUTES for TIME, preserving date components."
(cl-destructuring-bind (_s _m _h day month year dow dst utcoff) (decode-time time)
(encode-time 0 minutes hours day month year dow dst utcoff)))
(defun chime-current-time-matches-time-of-day-string (time-of-day-string)
"Check if current time matches TIME-OF-DAY-STRING."
(let ((now (current-time)))
(chime--time=
now
(apply 'chime-set-hours-minutes-for-time
now
(chime-get-hours-minutes-from-time time-of-day-string)))))
(defun chime-current-time-is-day-wide-time ()
"Check if current time matches any day-wide alert time."
(--any (chime-current-time-matches-time-of-day-string it)
chime-day-wide-alert-times))
;;;; All-Day Event Handling
(defun chime-day-wide-notifications (events)
"Generate notification texts for day-wide EVENTS.
Returns a list of (MESSAGE . SEVERITY) cons cells with \\='medium severity."
(->> events
(-filter 'chime-display-as-day-wide-event)
(-map 'chime--day-wide-notification-text)
(-uniq)
;; Wrap messages in cons cells with default 'medium' severity
(--map (cons it 'medium))))
(defun chime-display-as-day-wide-event (event)
"Check if EVENT should be displayed as a day-wide event.
Considers both events happening today and advance notices for future events.
When `chime-show-any-overdue-with-day-wide-alerts' is t (default):
- Shows overdue TODO items (timed events that passed)
- Shows all-day events from today or earlier
When nil:
- Shows only today's events (both timed and all-day)
- Hides overdue items from past days"
(or
;; Events happening today or in the past
(and (chime-event-has-any-passed-time event)
(or chime-show-any-overdue-with-day-wide-alerts
;; When overdue alerts disabled, only show today's events
(chime-event-is-today event)))
;; Advance notice for upcoming all-day events
(and chime-day-wide-advance-notice
(chime-event-has-any-day-wide-timestamp event)
(chime-event-within-advance-notice-window event))))
(defun chime-event-has-any-day-wide-timestamp (event)
"Check if EVENT has any day-wide (no time component) timestamps."
(--any (not (chime--has-timestamp (car it)))
(cdr (assoc 'times event))))
(defun chime-event-within-advance-notice-window (event)
"Check if EVENT has any day-wide timestamps within advance notice window.
Returns t if any all-day timestamp is between tomorrow and N days from now,
where N is `chime-day-wide-advance-notice'."
(when chime-day-wide-advance-notice
(let* ((now (current-time))
;; Calculate time range: start of tomorrow to end of N days from now
(window-end (time-add now (seconds-to-time
(* 86400 (1+ chime-day-wide-advance-notice)))))
(all-times (cdr (assoc 'times event))))
(--any
(when-let* ((timestamp-str (car it))
;; Only check all-day events (those without time component)
(is-all-day (not (chime--has-timestamp timestamp-str)))
;; Parse the date portion even without time
(parsed (org-parse-time-string timestamp-str))
(year (nth 5 parsed))
(month (nth 4 parsed))
(day (nth 3 parsed)))
;; Convert to time at start of day (00:00:00)
(let ((event-time (encode-time 0 0 0 day month year)))
;; Check if event is within the advance notice window
(and (time-less-p now event-time) ;; Event is in future
(time-less-p event-time window-end)))) ;; Event is within window
all-times))))
(defun chime-event-has-any-passed-time (event)
"Check if EVENT has any timestamps in the past or today.
For all-day events, checks if the date is today or earlier."
(--any
(let ((timestamp-str (car it))
(parsed-time (cdr it)))
(if parsed-time
;; Timed event: check if time has passed
(time-less-p parsed-time (current-time))
;; All-day event: check if date is today or earlier
(when-let* ((parsed (org-parse-time-string timestamp-str))
(year (nth 5 parsed))
(month (nth 4 parsed))
(day (nth 3 parsed)))
(let* ((event-date (encode-time 0 0 0 day month year))
(today-start (let ((now (decode-time (current-time))))
(encode-time 0 0 0
(decoded-time-day now)
(decoded-time-month now)
(decoded-time-year now)))))
(not (time-less-p today-start event-date))))))
(cdr (assoc 'times event))))
(defun chime-event-is-today (event)
"Check if EVENT has any timestamps that are specifically today (not past days).
For all-day events, checks if the date is exactly today.
For timed events, checks if the time is today (past or future)."
(--any
(let ((timestamp-str (car it))
(parsed-time (cdr it)))
(if parsed-time
;; Timed event: check if it's today (could be future time today)
(let* ((decoded (decode-time parsed-time))
(event-day (decoded-time-day decoded))
(event-month (decoded-time-month decoded))
(event-year (decoded-time-year decoded))
(today (decode-time))
(today-day (decoded-time-day today))
(today-month (decoded-time-month today))
(today-year (decoded-time-year today)))
(and (= event-day today-day)
(= event-month today-month)
(= event-year today-year)))
;; All-day event: check if date is exactly today
(when-let* ((parsed (org-parse-time-string timestamp-str))
(year (nth 5 parsed))
(month (nth 4 parsed))
(day (nth 3 parsed)))
(let* ((event-date (encode-time 0 0 0 day month year))
(today-start (let ((now (decode-time (current-time))))
(encode-time 0 0 0
(decoded-time-day now)
(decoded-time-month now)
(decoded-time-year now)))))
(time-equal-p event-date today-start)))))
(cdr (assoc 'times event))))
(defun chime--day-wide-notification-text (event)
"Generate notification text for day-wide EVENT.
Handles both same-day events and advance notices."
(let* ((title (cdr (assoc 'title event)))
(all-times (cdr (assoc 'times event)))
(is-today (chime-event-has-any-passed-time event))
(is-advance-notice (and chime-day-wide-advance-notice
(chime-event-within-advance-notice-window event))))
(cond
;; Event is today
(is-today
(format "%s is due or scheduled today" title))
;; Event is within advance notice window
(is-advance-notice
;; Calculate days until event
(let* ((now (current-time))
(days-until
(-min
(--map
(when-let* ((timestamp-str (car it))
(is-all-day (not (chime--has-timestamp timestamp-str)))
(parsed (org-parse-time-string timestamp-str))
(year (nth 5 parsed))
(month (nth 4 parsed))
(day (nth 3 parsed)))
(let* ((event-time (encode-time 0 0 0 day month year))
(seconds-until (time-subtract event-time now))
(days (/ (float-time seconds-until) 86400.0)))
(ceiling days)))
all-times))))
(cond
((= days-until 1)
(format "%s is tomorrow" title))
((= days-until 2)
(format "%s is in 2 days" title))
(t
(format "%s is in %d days" title days-until)))))
;; Fallback (shouldn't happen)
(t
(format "%s is due or scheduled today" title)))))
;;;; Event Checking & Navigation
(defun chime--check-event (event)
"Get notifications for given EVENT.
Returns a list of (MESSAGE . SEVERITY) cons cells."
(->> (chime--notifications event)
(--map (let* ((notif it)
(timestamp-str (caar notif))
(interval-cons (cadr notif)) ; (minutes . severity)
(severity (cdr interval-cons))
(message (chime--notification-text
`(,timestamp-str . ,interval-cons)
event)))
(cons message severity)))))
(defun chime--jump-to-event (event)
"Jump to EVENT's org entry in its file.
Reconstructs marker from serialized file path and position."
(interactive)
(when-let* ((file (cdr (assoc 'marker-file event)))
(pos (cdr (assoc 'marker-pos event))))
(when (file-exists-p file)
(find-file file)
(goto-char pos)
;; Use org-fold-show-entry (Org 9.6+) if available, fallback to org-show-entry
(if (fboundp 'org-fold-show-entry)
(org-fold-show-entry)
(with-no-warnings
(org-show-entry))))))
(defun chime--open-calendar-url ()
"Open calendar URL in browser if `chime-calendar-url' is set."
(interactive)
(when chime-calendar-url
(browse-url chime-calendar-url)))
(defun chime--jump-to-first-event ()
"Jump to first event in `chime--upcoming-events' list."
(interactive)
(when-let* ((first-event (car chime--upcoming-events))
(event (car first-event)))
(chime--jump-to-event event)))
;;;; Modeline & Tooltip Display
(defun chime--format-event-for-tooltip (event-time-str minutes-until title)
"Format a single event line for tooltip display.
EVENT-TIME-STR is the time string, MINUTES-UNTIL is minutes until event,
TITLE is the event title."
(let ((time-display (chime--get-hh-mm-from-org-time-string event-time-str))
(countdown (cond
((< minutes-until 1440) ;; Less than 24 hours
(format "(%s)" (chime--time-left (* minutes-until 60))))
(t
;; 24+ hours: show days and hours
(let* ((days (truncate (/ minutes-until 1440)))
(remaining-minutes (truncate (mod minutes-until 1440)))
(hours (truncate (/ remaining-minutes 60))))
(if (> hours 0)
(format "(in %d day%s %d hour%s)"
days (if (= days 1) "" "s")
hours (if (= hours 1) "" "s"))
(format "(in %d day%s)"
days (if (= days 1) "" "s"))))))))
(format "%s at %s %s" title time-display countdown)))
(defun chime--group-events-by-day (upcoming-events)
"Group UPCOMING-EVENTS by day.
Returns an alist of (DATE-STRING . EVENTS-LIST)."
(let ((grouped '())
(now (current-time)))
(dolist (item upcoming-events)
(let* ((event-time (cdr (nth 1 item)))
(_minutes-until (nth 2 item))
;; Get date components for calendar day comparison
(now-decoded (decode-time now))