Repository navigation
Expand file tree
/
Copy pathorg-timegrid.el
More file actions
2735 lines (2547 loc) · 129 KB
/
Copy pathorg-timegrid.el
File metadata and controls
2735 lines (2547 loc) · 129 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
;;; org-timegrid.el --- SVG week calendar with mouse and keyboard editing -*- lexical-binding: t; -*-
;; Copyright (C) 2026 Umar Ahmad
;; Author: Umar Ahmad <Gleek@users.noreply.github.com>
;; Assisted-by: Codex:gpt-5.6-sol
;; Maintainer: Umar Ahmad <Gleek@users.noreply.github.com>
;; Version: 0.1.1
;; Package-Requires: ((emacs "29.1") (org "9.6"))
;; Keywords: calendar, outlines, convenience
;; URL: https://github.com/Gleek/org-timegrid
;; This file is not part of GNU Emacs.
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Draw timed events as an interactive SVG week calendar and edit them by
;; dragging or from the keyboard. This file has no Org dependency: it renders
;; whatever a backend supplies, through the protocol in `org-timegrid-model'.
;; `org-timegrid-org' is the Org implementation of that protocol.
;; Drawing data lives in `org-timegrid-scene', SVG interpretation in
;; `org-timegrid-svg', and cache/resource ownership in `org-timegrid-display'.
;;; Code:
(require 'cl-lib)
(require 'subr-x)
(require 'org-timegrid-model)
(require 'org-timegrid-scene)
(require 'org-timegrid-display)
(defcustom org-timegrid-range-alignment 'week
"How to position the range when opening or jumping to a date.
With `week', start on `calendar-week-start-day', or end on the target
date if that week start would leave the target outside the range.
With `center', place the target date in the middle. For an even
number of days, use the column just right of the middle.
The target date defaults to today."
:group 'org-timegrid
:type '(choice (const :tag "Align to week start" week)
(const :tag "Center target date" center)))
(defun org-timegrid--range-start (&optional absolute-date)
"Return the first visible day for a range containing ABSOLUTE-DATE.
Position the range according to `org-timegrid-range-alignment'.
ABSOLUTE-DATE defaults to today."
(let ((absolute (or absolute-date
(calendar-absolute-from-gregorian
(calendar-current-date)))))
(if (eq org-timegrid-range-alignment 'center)
(- absolute (/ org-timegrid-days 2))
(max (org-timegrid-week-start absolute)
(- absolute (org-timegrid--last-day-index))))))
(defvar org-timegrid-preview-prefetch t
"Prepare nearby timed drag and resize positions between input events.
This uses extra CPU and memory to make upcoming snapped positions faster.
Set to nil to disable preparation and its image cache.")
(defcustom org-timegrid-default-duration-minutes 30
"Duration assumed wherever no end time is given.
Keyboard creation offers it, and a backend event that has a start but no
end is shown this long."
:group 'org-timegrid
:type 'integer)
(defvar org-timegrid-data-refresh-seconds 300
"Seconds between visible backend data refreshes.")
(defvar org-timegrid-keyboard-commit-delay 0.65
"Idle seconds before a keyboard block movement is written to its backend.")
(defvar org-timegrid-copy-date-format "%a %d %b %Y"
"Date format used for day headings in copied calendar text.")
(defvar org-timegrid-copy-item-template "%{start}–%{end} %{title}"
"Template for one item in copied calendar text.
The tokens `%{title}', `%{start}', and `%{end}' are replaced.")
(defvar org-timegrid--header-map
(let ((map (make-sparse-keymap)))
(define-key map [header-line down-mouse-1] #'org-timegrid-header-press)
(define-key map [header-line double-down-mouse-1]
#'org-timegrid-ignore-double-press)
(define-key map [header-line mouse-1] #'org-timegrid-header-click)
(define-key map [header-line double-mouse-1] #'org-timegrid-header-visit)
(define-key map [down-mouse-1] #'org-timegrid-header-press)
(define-key map [double-down-mouse-1] #'org-timegrid-ignore-double-press)
(define-key map [mouse-1] #'org-timegrid-header-click)
(define-key map [double-mouse-1] #'org-timegrid-header-visit)
(dolist (area '(calendar-rail-block calendar-rail-resize))
(define-key map (vector area 'down-mouse-1) #'org-timegrid-header-press)
(define-key map (vector area 'double-down-mouse-1) #'org-timegrid-ignore-double-press)
(define-key map (vector area 'mouse-1) #'org-timegrid-header-click)
(define-key map (vector area 'double-mouse-1) #'org-timegrid-header-visit))
map)
"Mouse map installed directly on the sticky calendar header image.")
(defvar-local org-timegrid--pointer-overlay nil)
(defvar-local org-timegrid--keyboard-edit-timer nil)
(defvar-local org-timegrid--keyboard-edit nil)
(defvar org-timegrid--defer-refresh nil)
(defconst org-timegrid--scroll-rebound-seconds 0.35
"Seconds to suppress a reversed wheel event after hitting an edge.")
(defvar-local org-timegrid--scroll-boundary nil
"Most recently hit scroll boundary as (SIDE . TIME).")
(defvar-local org-timegrid--fringe-remap-cookie nil)
(defvar-local org-timegrid--backend nil)
(defun org-timegrid--make-block
(id day start end title &optional color done time-kind)
"Construct a renderer block from ID, DAY, START, END, TITLE and COLOR.
DONE and TIME-KIND describe the block's completion and timestamp kind."
(org-timegrid-block-create
:id id :day day :start start :end end :title title
:time-kind (or time-kind 'timed) :color color :done done))
(defun org-timegrid--load-state (week-start)
"Load renderer state for WEEK-START from the current backend."
(let* ((start (* week-start 1440))
(end (* (+ week-start org-timegrid-days) 1440))
(anchor (or org-timegrid--cache-anchor start))
(events (org-timegrid-backend-list
org-timegrid--backend start end
(list :force org-timegrid--force-data-refresh
:cache-range (cons (- anchor (* 14 1440))
(+ (max end (+ anchor (- end start))) (* 7 1440))))))
(blocks (org-timegrid-events-to-blocks events week-start)))
;; The cursor has a remembered position and a separate visibility, so
;; hiding it with C-g keeps the place, and block selection can record a
;; position without drawing anything.
(org-timegrid--calendar-state-create
:week-start week-start :events events :blocks blocks)))
(defun org-timegrid--blocks-at-slot (day minute)
"Return committed timed blocks overlapping DAY's slot at MINUTE.
Shortest first, so a nested child is offered before its parent. Lanes
make every overlapping block reachable, including off-grid starts."
(when (and (numberp day) (numberp minute))
(let ((slot-start (+ (* day 1440) minute)))
(sort (seq-filter
(lambda (block)
(let ((start (org-timegrid--block-absolute-start block)))
(and (not (org-timegrid-block-preview block))
(< start (+ slot-start org-timegrid-slot-minutes))
(> (+ start (- (org-timegrid-block-end block)
(org-timegrid-block-start block)))
slot-start))))
(org-timegrid--timed-blocks))
(lambda (left right)
(< (- (org-timegrid-block-end left) (org-timegrid-block-start left))
(- (org-timegrid-block-end right) (org-timegrid-block-start right))))))))
(defun org-timegrid--reveal-cursor ()
"Show the cursor, returning non-nil when it was hidden until now.
The first movement key reveals the cursor where it was left rather than
also moving it, so its position is visible before it is used."
(unless (org-timegrid--cursor-visible-p)
(org-timegrid--ensure-cursor)
(setf (org-timegrid--calendar-state-cursor-visible org-timegrid--state) t)
t))
(defun org-timegrid--region-message ()
"Return the active calendar region as a readable time range."
(when-let* ((range (org-timegrid--region-range)))
(let ((start (car range))
(end (cdr range)))
(if (= (floor start 1440) (floor end 1440))
(format "%s–%s" (org-timegrid--format-minute start)
(org-timegrid--format-minute end))
(format "%s %s–%s %s"
(org-timegrid--format-copy-date start)
(org-timegrid--format-minute start)
(org-timegrid--format-copy-date end)
(org-timegrid--format-minute end))))))
(defun org-timegrid-set-mark-command ()
"Set or deactivate the calendar mark at the cursor."
(interactive)
(org-timegrid--with-update nil
(org-timegrid--reveal-cursor)
(if (org-timegrid-region-active-p)
(setf (org-timegrid--calendar-state-region-active org-timegrid--state) nil)
(setf (org-timegrid--calendar-state-mark org-timegrid--state)
(org-timegrid--cursor-calendar-minute)
(org-timegrid--calendar-state-region-active org-timegrid--state) t))
(if (org-timegrid-region-active-p)
(message "%s" (org-timegrid--region-message))
(message nil))))
(defun org-timegrid-exchange-point-and-mark ()
"Exchange the calendar cursor and its remembered mark."
(interactive)
(org-timegrid--with-update 'reset
(unless (integerp (org-timegrid--calendar-state-mark org-timegrid--state))
(user-error "No calendar mark set"))
(let* ((point (org-timegrid--cursor-calendar-minute))
(mark (org-timegrid--calendar-state-mark org-timegrid--state))
(day (floor mark 1440)))
(org-timegrid--reload-state (org-timegrid--range-start day))
(org-timegrid--set-cursor
(- day (org-timegrid--calendar-state-week-start org-timegrid--state))
(% mark 1440))
(setf (org-timegrid--calendar-state-mark org-timegrid--state) point
(org-timegrid--calendar-state-region-active org-timegrid--state) t
(org-timegrid--calendar-state-cursor-visible org-timegrid--state) t)
(message "%s" (org-timegrid--region-message)))))
(defun org-timegrid--set-cursor (day minute &optional lane)
"Move the cursor to DAY and MINUTE, clamped to the visible week.
LANE picks between blocks overlapping that slot, and defaults to zero."
(let* ((day (max 0 (min (org-timegrid--last-day-index) day)))
(minute (org-timegrid--visible-minute minute))
(lane (or lane 0))
(candidates (org-timegrid--blocks-at-slot day minute))
(selected (and candidates
(org-timegrid-block-id
(nth (min lane (1- (length candidates))) candidates)))))
(setf (org-timegrid--calendar-state-cursor org-timegrid--state)
(org-timegrid--cursor-state-create
:surface 'grid :day day :minute minute :lane lane)
(org-timegrid--calendar-state-selected-id org-timegrid--state)
selected))
(org-timegrid--cursor))
(defun org-timegrid--reload-state (week-start &optional state)
"Reload WEEK-START from the backend, keeping cursor and selection.
Navigation and refresh drop preview and history, but they must not throw
the cursor back to today, and they must not clear the selection: every
edit refreshes, so a cleared selection would make repeated keyboard
nudges of one block impossible. A selection that no longer resolves
after the reload is dropped. Use STATE when already fetched."
(let ((cursor (org-timegrid--calendar-state-cursor org-timegrid--state))
(visible (org-timegrid--calendar-state-cursor-visible
org-timegrid--state))
(selected (org-timegrid--calendar-state-selected-id
org-timegrid--state))
(mark (org-timegrid--calendar-state-mark org-timegrid--state))
(region-active
(org-timegrid--calendar-state-region-active org-timegrid--state)))
(setq-local org-timegrid--state (or state (org-timegrid--load-state week-start)))
(when cursor
(setf (org-timegrid--calendar-state-cursor org-timegrid--state) cursor
(org-timegrid--calendar-state-cursor-visible org-timegrid--state)
visible
(org-timegrid--calendar-state-selected-id org-timegrid--state)
(and (org-timegrid--block selected) selected)
(org-timegrid--calendar-state-mark org-timegrid--state) mark
(org-timegrid--calendar-state-region-active org-timegrid--state)
region-active)
;; Heading edits can change position-based IDs. Select at the cursor.
(when (and selected (not (org-timegrid--selected-id)))
(let ((day (org-timegrid--cursor-state-day cursor))
(lane (org-timegrid--cursor-state-lane cursor)))
(if (eq (org-timegrid--cursor-state-surface cursor) 'rail)
(org-timegrid--set-all-day-cursor day lane)
(org-timegrid--set-cursor
day (org-timegrid--cursor-state-minute cursor) lane)))))))
(defun org-timegrid--proposal (origin target copy-kind &optional push-context)
"Return a drag proposal from ORIGIN to TARGET.
COPY-KIND is nil for a move, `duplicate-entry' for an independent copy,
or `add-occurrence' for another time on the source entry.
PUSH-CONTEXT makes this a push instead of an ordinary drag."
(let* ((id (plist-get origin :block-id))
(copying (memq copy-kind '(duplicate-entry add-occurrence)))
(origin-surface (or (plist-get origin :surface) 'grid))
(target-surface (or (plist-get target :surface) 'grid))
(origin-day (plist-get origin :day))
(target-day (plist-get target :day))
(origin-minute (plist-get origin :minute))
(target-minute (plist-get target :minute))
(edge (plist-get origin :edge)))
(cond
((or (null origin-day) (null origin-minute)
(null target-day) (null target-minute))
(org-timegrid--operation-create
:error "Release inside a calendar cell"))
(push-context
(if (eq target-surface 'grid)
(org-timegrid--push-proposal
push-context (- (+ (* target-day 1440) target-minute)
(+ (* origin-day 1440) origin-minute)))
(org-timegrid--operation-create :error "Release inside the time grid")))
((and (null id) (not (eq origin-surface target-surface)))
(org-timegrid--operation-create
:error "Create a range on one calendar surface"))
((null id)
(if (/= origin-day target-day)
(org-timegrid--operation-create
:error "New ranges currently stay within one day")
(let ((block (org-timegrid--make-block
'preview origin-day
(min origin-minute target-minute)
(+ (max origin-minute target-minute)
org-timegrid-slot-minutes)
"New block")))
(setf (org-timegrid-block-preview block) t)
(org-timegrid--operation-create :kind 'create :block block))))
((not (eq origin-surface target-surface))
(let ((source (org-timegrid--block id)))
(cond
((null source)
(org-timegrid--operation-create
:error "Drag an existing block between the all-day rail and time grid"))
((and (eq origin-surface 'rail)
(/= (- (org-timegrid-block-end source)
(org-timegrid-block-start source))
1440))
(org-timegrid--operation-create
:error "Multi-day blocks cannot move into the time grid"))
(t
(let* ((block (copy-org-timegrid-block source))
(start (+ (* target-day 1440)
(if (eq target-surface 'rail) 0 target-minute)))
(duration (if (eq target-surface 'rail)
1440
org-timegrid-default-duration-minutes)))
(org-timegrid--set-absolute-range block start (+ start duration))
(setf (org-timegrid-block-time-kind block)
(if (eq target-surface 'rail) 'all-day 'timed)
(org-timegrid-block-preview block) t)
(org-timegrid--operation-create
:kind (if copying copy-kind 'move) :block block
:replace-id (and (not copying) id)))))))
(t
(let ((source (org-timegrid--block id)))
(if (null source)
(org-timegrid--operation-create
:error "That calendar block changed; refresh and try again")
(let* ((absolute-start (org-timegrid--block-absolute-start source))
(absolute-end (+ absolute-start
(- (org-timegrid-block-end source)
(org-timegrid-block-start source))))
(absolute-origin (+ (* origin-day 1440) origin-minute))
(absolute-target (+ (* target-day 1440) target-minute))
(edge (and (not copying) edge))
(delta (pcase edge
('top (- absolute-target absolute-start))
('bottom (- (+ absolute-target
(if (org-timegrid-block-all-day-p source)
1440 org-timegrid-slot-minutes))
absolute-end))
(_ (- absolute-target absolute-origin)))))
(org-timegrid--operation-create
:kind (if copying copy-kind (if edge 'resize 'move)) :edge edge
:block (org-timegrid--transform-block-range source delta edge)
:replace-id (and (not copying) id)))))))))
(defun org-timegrid--push-context (block)
"Retain BLOCK's original event and visible overlap groups for one gesture."
(let* ((event (and block (org-timegrid-block-event block)))
(metadata (and event (org-timegrid-event-metadata event))))
(unless (functionp (org-timegrid-backend-update-function org-timegrid--backend))
(user-error "This backend cannot move calendar entries"))
(unless (and event (not (org-timegrid-block-all-day-p block)))
(user-error "Select a timed task to push"))
(when (plist-get metadata :diary)
(org-timegrid--prepare-block-edit block))
(when (or (plist-get metadata :repeater-type) (plist-get metadata :clock))
(user-error "Use ordinary movement for repeating tasks and clocks"))
(cons event
(org-timegrid--push-groups
(seq-remove
(lambda (other)
(or (and (< (org-timegrid-event-start other) (org-timegrid-event-end event))
(> (org-timegrid-event-end other) (org-timegrid-event-start event)))
(org-timegrid-event-all-day other)
(plist-get (org-timegrid-event-metadata other) :repeater-type)
(plist-get (org-timegrid-event-metadata other) :diary)))
(org-timegrid--calendar-state-events org-timegrid--state))))))
(defun org-timegrid--push-groups (events)
"Group EVENTS by connected strict overlaps, in chronological order.
Each group is (START END EVENTS); merely touching tasks remain separate."
(let (groups)
(dolist (event (sort events (lambda (a b) (< (org-timegrid-event-start a)
(org-timegrid-event-start b)))))
(let ((start (org-timegrid-event-start event))
(end (org-timegrid-event-end event)))
(if (and groups (< start (cadar groups)))
(setf (cadar groups) (max end (cadar groups))
(nth 2 (car groups)) (cons event (nth 2 (car groups))))
(push (list start end (list event)) groups))))
(nreverse groups)))
(defun org-timegrid--push-proposal (context delta)
"Push CONTEXT's task by DELTA minutes, absorbing gaps in either direction.
Rebuild from original ranges every time and stop at the visible date limits."
(catch 'push-barrier
(let* ((root (car context))
(week (org-timegrid--calendar-state-week-start org-timegrid--state))
(low (* week 1440))
(high (+ low (* org-timegrid-days 1440)))
(start (org-timegrid-event-start root))
(end (org-timegrid-event-end root))
(delta (max (- low start) (min delta (- high end))))
(forward (>= delta 0))
(frontier (+ (if forward end start) delta))
(moves (list (cons root delta))))
(unless (<= low start end high)
(user-error "Push tasks fully inside the visible dates"))
(dolist (group (if forward (cdr context) (reverse (cdr context))))
(pcase-let* ((`(,from ,to ,members) group)
(shift (if forward (- frontier from) (- frontier to))))
(when (if forward (and (>= from end) (> shift 0))
(and (<= to start) (< shift 0)))
(when (or (< from low) (> to high)
(seq-some (lambda (event)
(plist-get (org-timegrid-event-metadata event) :clock))
members))
(throw 'push-barrier (org-timegrid--push-proposal context (- delta shift))))
(let ((overflow (if forward (max 0 (- (+ to shift) high))
(min 0 (- (+ from shift) low)))))
(unless (zerop overflow)
(throw 'push-barrier
(org-timegrid--push-proposal context (- delta overflow)))))
(dolist (event members) (push (cons event shift) moves))
(setq frontier (+ (if forward to from) shift)))))
(let ((blocks
(mapcar (lambda (move)
(let* ((event (car move))
(block (org-timegrid-event-to-block event week))
(offset (- (cdr move) low)))
(org-timegrid--set-absolute-range
block (+ (org-timegrid-event-start event) offset)
(+ (org-timegrid-event-end event) offset))
(setf (org-timegrid-block-preview block) t)
block))
(nreverse moves))))
(org-timegrid--operation-create :kind 'push :block (car blocks) :blocks blocks
:replace-id (org-timegrid-event-id root))))))
(defun org-timegrid-day-blocks (backend absolute-day)
"Return BACKEND blocks on ABSOLUTE-DAY, clipped to that day.
Coordinates are minutes within that day, so a range running past midnight
arrives clipped at 1440 rather than spilling into a day that is not being
drawn. Layout belongs to the shared renderer used by both calendar views.
Date-only events are omitted: day images have no all-day rail, and
rendering them as midnight-to-midnight timed blocks would be misleading."
(let* ((start (* absolute-day 1440))
(events
(seq-remove
#'org-timegrid-event-all-day
(org-timegrid-backend-list backend start (+ start 1440))))
(blocks
(mapcar
(lambda (event)
(org-timegrid-block-create
:id (org-timegrid-event-id event)
:day 0
:start (max 0 (- (org-timegrid-event-start event) start))
:end (min 1440 (- (org-timegrid-event-end event) start))
:time-kind 'timed
:title (org-timegrid-event-title event)
:color (org-timegrid-event-color event)
:done (eq (org-timegrid-event-state event) 'done)
:event event))
events)))
blocks))
(defun org-timegrid--ensure-state ()
"Ensure the current calendar buffer has a usable calendar state.
Keep existing blocks when possible, but reconstruct missing date metadata."
(unless (and (org-timegrid--calendar-state-p org-timegrid--state)
(numberp (org-timegrid--calendar-state-week-start
org-timegrid--state)))
(setq-local org-timegrid--state
(org-timegrid--load-state (org-timegrid--range-start))))
org-timegrid--state)
(defun org-timegrid--rail-edge-at (geometry x)
"Return resize endpoint for rail GEOMETRY at horizontal pixel X.
The operation model calls the start and end endpoints `top' and `bottom';
the rail presents those same endpoints as its left and right edges."
(let* ((left (plist-get geometry :x))
(right (+ left (plist-get geometry :width)))
(edge (min org-timegrid--resize-pixels
(max 1 (/ (plist-get geometry :width) 2.0)))))
(cond ((and (plist-get geometry :allow-left)
(<= left x (+ left edge)))
'top)
((and (plist-get geometry :allow-right)
(<= (- right edge) x right))
'bottom))))
(defun org-timegrid--position-image-xy (position)
"Return stable full-calendar pixel coordinates for mouse POSITION.
Use window-relative X so crossing image map hotspots and day columns cannot
reset the horizontal origin. Add the tile offset to glyph-relative Y."
(let* ((window-xy (posn-x-y position))
(object-xy (posn-object-x-y position))
(point (posn-point position))
(tile (and (integer-or-marker-p point)
(or (get-text-property point 'org-timegrid-tile)
(and (> point (point-min))
(get-text-property (1- point)
'org-timegrid-tile))))))
(when (or window-xy object-xy)
(cons (or (car-safe window-xy)
(and object-xy (+ (car object-xy)
(if tile (car (org-timegrid--tile-x-bounds tile)) 0))))
(+ (if (and tile org-timegrid--tile-count)
(car (org-timegrid--tile-bounds tile))
0)
(or (cdr-safe object-xy) (cdr-safe window-xy)))))))
(defun org-timegrid--target (position)
"Return calendar metadata at mouse POSITION in the SVG image."
(org-timegrid--with-render-context (org-timegrid--display-context)
(let* ((xy (org-timegrid--position-image-xy position))
(x (and xy (car xy)))
(y (and xy (cdr xy)))
(width (org-timegrid--canvas-width))
(column-width (org-timegrid--column-width))
(start-minute (* 60 org-timegrid-start-hour)))
(when (and (numberp x) (numberp y)
(>= x (org-timegrid--label-width))
(< x width) (>= y 0)
(< y org-timegrid--image-height))
(let* ((column (/ (- x (org-timegrid--label-width)) column-width))
(day (max 0 (min (org-timegrid--last-day-index)
(if (plist-get (org-timegrid--calendar-state-gesture org-timegrid--state) :clock)
(1- (round column))
(floor column)))))
(minute (+ start-minute
(* org-timegrid-slot-minutes
(floor (/ (max 0 (- y (org-timegrid--grid-top-inset)))
(* (org-timegrid--pixels-per-minute)
org-timegrid-slot-minutes))))))
(geometry
(cl-find-if (lambda (item)
(let ((slop
org-timegrid--resize-slop))
(and (not (plist-get item :preview))
(<= (plist-get item :x) x
(+ (plist-get item :x)
(plist-get item :width)))
(<= (- (plist-get item :y) slop) y
(+ (plist-get item :y)
(plist-get item :height) slop)))))
org-timegrid--geometry))
(edge (and geometry
(org-timegrid--edge-at geometry y))))
(list :surface 'grid :block-id (plist-get geometry :id)
:day (if (and geometry (not (plist-get (org-timegrid--calendar-state-gesture org-timegrid--state) :clock)))
(plist-get geometry :day) day)
:minute minute :edge edge))))))
(defun org-timegrid--set-preview (proposal)
"Update the preview without committing it to the backend."
(org-timegrid--with-update nil
(setf (org-timegrid--calendar-state-preview org-timegrid--state)
(and proposal (not (org-timegrid--operation-error proposal)) proposal))))
(defun org-timegrid--clear-preview ()
"Discard the pending preview."
(org-timegrid--set-preview nil))
(defun org-timegrid--backend-undo (redo)
"Ask the backend to undo, or REDO, its most recent edit.
The calendar cannot undo for itself: the change lives in a source buffer
it does not own. Repeating the key continues one run rather than
undoing the undo, which is what `undo' does at a keyboard."
(let ((undoer (org-timegrid-backend-undo-function org-timegrid--backend)))
(unless (functionp undoer)
(user-error "This backend cannot undo; undo in the source buffer"))
(funcall undoer
(and (memq last-command '(org-timegrid-undo org-timegrid-redo)) t)
redo)
(org-timegrid--refresh-data)))
(defun org-timegrid-undo ()
"Undo the calendar's most recent edit, in the file it touched."
(interactive)
(org-timegrid--backend-undo nil))
(defun org-timegrid-redo ()
"Redo the calendar's most recently undone edit."
(interactive)
(org-timegrid--backend-undo t))
(defun org-timegrid-remove-selected (&optional keep-entry)
"Remove timestamps in the region or the block at point.
KEEP-ENTRY never offers to remove timestamp-free entries."
(interactive)
(let* ((selection (org-timegrid--logical-selection))
(events (plist-get selection :events))
(deleter (org-timegrid-backend-delete-function org-timegrid--backend))
(entry-deleter
(org-timegrid-backend-delete-entry-function org-timegrid--backend))
(empty-p (org-timegrid-backend-entry-empty-function org-timegrid--backend))
(entry-key (org-timegrid-backend-entry-key-function org-timegrid--backend)))
(unless events (user-error "The calendar region contains no blocks"))
(unless (functionp deleter)
(user-error "This backend cannot remove calendar entries"))
(org-timegrid--transaction
(lambda ()
(dolist (event events) (funcall deleter event))
(unless keep-entry
(let (eligible)
(dolist (event events)
(when (and (functionp empty-p) (funcall empty-p event))
(unless (cl-find (and (functionp entry-key)
(funcall entry-key event))
eligible :key (lambda (candidate)
(and (functionp entry-key)
(funcall entry-key candidate)))
:test #'equal)
(push event eligible))))
(when (and eligible (functionp entry-deleter)
(y-or-n-p
(format "Delete %d timestamp-free entr%s too? "
(length eligible)
(if (= (length eligible) 1) "y" "ies"))))
(dolist (event eligible) (funcall entry-deleter event)))))
(org-timegrid--deactivate-region)
(org-timegrid--deselect-block)))
(message "Removed %d timestamp%s" (length events)
(if (= (length events) 1) "" "s"))))
(defun org-timegrid-edit-selected-title ()
"Edit the selected calendar block's source heading title."
(interactive)
(let* ((id (org-timegrid--selected-id))
(source (and id (org-timegrid--block id))))
(if (null source)
(message "No block selected")
(let ((title (org-timegrid--read-title
(org-timegrid-block-title source))))
(unless (string-empty-p title)
(let ((event (org-timegrid-block-event source))
(updater (org-timegrid-backend-update-function
org-timegrid--backend)))
(unless (and event (functionp updater))
(user-error "This backend cannot rename calendar entries"))
(org-timegrid--call-update
updater event
(org-timegrid-event-start event)
(org-timegrid-event-end event) title
(org-timegrid-event-time-kind event))
(org-timegrid--refresh-data)))))))
(defun org-timegrid--read-title (&optional initial)
"Read a block title in the echo area, prefilled with INITIAL."
(read-string "Title: " initial))
(defun org-timegrid--block-absolute-range (block)
"Return BLOCK's absolute (START . END) minute range."
(let ((day-minute
(* (+ (org-timegrid--calendar-state-week-start org-timegrid--state)
(org-timegrid-block-day block))
1440)))
(cons (+ day-minute (org-timegrid-block-start block))
(+ day-minute (org-timegrid-block-end block)))))
(defun org-timegrid--accepts-arguments-p (function count)
"Return non-nil when FUNCTION accepts COUNT arguments."
(pcase-let ((`(,minimum . ,maximum) (func-arity function)))
(and (<= minimum count)
(or (eq maximum 'many) (>= maximum count)))))
(defun org-timegrid--call-update (updater event start end title time-kind)
"Call UPDATER for EVENT, START, END and TITLE.
Pass an explicit TIME-KIND when UPDATER's contract supports it."
(cond
((org-timegrid--accepts-arguments-p updater 5)
(funcall updater event start end title time-kind))
((or title (org-timegrid--accepts-arguments-p updater 4))
(funcall updater event start end title))
(t (funcall updater event start end))))
(defun org-timegrid--backend-create (title block &optional source-event target)
"Ask the active backend to create TITLE using BLOCK's range.
SOURCE-EVENT identifies an entry to reuse, and TARGET selects its destination."
(let ((creator (org-timegrid-backend-create-function
org-timegrid--backend))
(range (org-timegrid--block-absolute-range block)))
(unless (functionp creator)
(user-error "This backend cannot create calendar entries"))
(if (org-timegrid--accepts-arguments-p creator 6)
(funcall creator title (car range) (cdr range) source-event target
(org-timegrid-block-time-kind block))
(if target
(funcall creator title (car range) (cdr range) source-event target)
(funcall creator title (car range) (cdr range) source-event)))
(unless org-timegrid--defer-refresh (org-timegrid--refresh-data))))
(defun org-timegrid--read-entry ()
"Read a title and optional existing record for a new block."
(let ((reader (and org-timegrid--backend
(org-timegrid-backend-read-entry-function
org-timegrid--backend))))
(if (functionp reader)
(funcall reader)
(cons (org-timegrid--read-title) nil))))
(defvar-local org-timegrid--edit-context nil
"Backend edit scope retained during consecutive block adjustments.")
(defun org-timegrid--prepare-block-edit (block)
"Let BLOCK's backend validate editing and choose an edit scope."
(when-let* ((event (org-timegrid-block-event block))
(prepare (plist-get (org-timegrid-event-metadata event)
:prepare-edit)))
(funcall prepare event)))
(defun org-timegrid--backend-update (block)
"Ask the active backend to apply BLOCK's new range."
(org-timegrid--with-update nil
(let* ((updater (org-timegrid-backend-update-function
org-timegrid--backend))
(event (org-timegrid-event-snapshot (org-timegrid-block-event block)))
(range (org-timegrid--block-absolute-range block)))
(unless (and event (functionp updater))
(user-error "This backend cannot move or resize calendar entries"))
(org-timegrid--prepare-block-edit block)
(condition-case error-data
(progn
(org-timegrid--call-update
updater event (car range) (cdr range) nil
(org-timegrid-block-time-kind block))
(unless org-timegrid--defer-refresh
(setf (org-timegrid-event-start event) (car range)
(org-timegrid-event-end event) (cdr range)
(org-timegrid-event-all-day event)
(eq (org-timegrid-block-time-kind block) 'all-day))
(setf (org-timegrid--calendar-state-events org-timegrid--state)
(mapcar (lambda (old)
(if (equal (org-timegrid-event-id old) (org-timegrid-event-id event))
event old))
(org-timegrid--calendar-state-events org-timegrid--state)))
(if (plist-get (org-timegrid-event-metadata event) :reload-after-edit)
(progn
(org-timegrid--refresh-data)
(when-let* ((updated (org-timegrid--block (org-timegrid-event-id event))))
(org-timegrid--goto-block updated)))
(setf (org-timegrid--calendar-state-blocks org-timegrid--state)
(org-timegrid-events-to-blocks
(org-timegrid--calendar-state-events org-timegrid--state)
(org-timegrid--calendar-state-week-start org-timegrid--state))))))
(error
(unless org-timegrid--defer-refresh (org-timegrid--refresh-data))
(signal (car error-data) (cdr error-data)))))))
(defun org-timegrid--apply (proposal)
"Commit SVG drag PROPOSAL."
(let ((error-message (org-timegrid--operation-error proposal))
(kind (org-timegrid--operation-kind proposal))
(block (and (org-timegrid--operation-block proposal)
(copy-org-timegrid-block
(org-timegrid--operation-block proposal)))))
(cond
(error-message
(org-timegrid--clear-preview)
(message "%s" error-message))
((eq kind 'push)
(org-timegrid--transaction
(lambda ()
(dolist (moved (org-timegrid--operation-blocks proposal))
(let ((event (org-timegrid-block-event moved)))
(unless (equal (org-timegrid--block-absolute-range moved)
(cons (org-timegrid-event-start event) (org-timegrid-event-end event)))
(org-timegrid--backend-update moved))))
(setf (org-timegrid--calendar-state-preview org-timegrid--state) nil))))
((eq kind 'create)
(let (entry)
(unwind-protect
(progn
(org-timegrid--set-preview proposal)
(setq entry (org-timegrid--read-entry)))
(org-timegrid--clear-preview))
(unless (string-empty-p (car entry))
(org-timegrid--backend-create
(car entry) block nil (cdr entry)))))
((memq kind '(duplicate-entry add-occurrence))
(org-timegrid--with-update nil
(setf (org-timegrid--calendar-state-preview org-timegrid--state) nil)
(let ((source (org-timegrid--block (org-timegrid-block-id block))))
(org-timegrid--backend-create
(org-timegrid-block-title source) block
(org-timegrid-block-event source)
(and (eq kind 'add-occurrence)
(org-timegrid-event-source (org-timegrid-block-event source)))))))
((memq kind '(move resize))
(org-timegrid--with-update nil
(setf (org-timegrid--calendar-state-preview org-timegrid--state) nil
(org-timegrid-block-preview block) nil)
(org-timegrid--backend-update block)))
(t (org-timegrid--clear-preview)))))
(defun org-timegrid-click (event)
"Put the cursor where SVG mouse EVENT landed.
Clicking a block moves the cursor to that block's own first slot, which is
what selects it; clicking empty space moves the cursor to that slot and so
selects nothing. The mouse and the keyboard drive one shared cursor."
(interactive "@e")
(org-timegrid--with-update nil
(let* ((extend (memq 'shift (event-modifiers event)))
(old-point (and extend (org-timegrid--cursor-calendar-minute)))
(target (org-timegrid--target (event-start event)))
(block (org-timegrid--block (plist-get target :block-id))))
(if extend
(unless (org-timegrid-region-active-p)
(setf (org-timegrid--calendar-state-mark org-timegrid--state) old-point
(org-timegrid--calendar-state-region-active org-timegrid--state) t))
(org-timegrid--deactivate-region))
(setf (org-timegrid--calendar-state-cursor-visible org-timegrid--state) t)
(if block
(org-timegrid--goto-block block (plist-get target :day))
(when (and (plist-get target :day) (plist-get target :minute))
(org-timegrid--set-cursor (plist-get target :day)
(plist-get target :minute))
(org-timegrid--cursor-moved))))))
(defun org-timegrid--header-target (position)
"Return all-day rail metadata at header-line mouse POSITION."
(org-timegrid--with-render-context (org-timegrid--display-context)
(let* ((xy (or (posn-object-x-y position) (posn-x-y position)))
(x (car-safe xy))
(y (cdr-safe xy))
(window (posn-window position))
(left-offset (org-timegrid--header-left-offset window))
(canvas-width (org-timegrid--canvas-width))
(column-width (org-timegrid--column-width)))
(when (and (numberp x) (numberp y)
(>= y (org-timegrid--rail-top))
(>= x (+ left-offset (org-timegrid--label-width)))
(< x (+ left-offset canvas-width)))
(let ((geometry
(cl-find-if
(lambda (item)
(and (<= (plist-get item :x) x
(+ (plist-get item :x) (plist-get item :width)))
(<= (plist-get item :y) y
(+ (plist-get item :y) (plist-get item :height)))))
org-timegrid--header-geometry)))
(list :surface 'rail
:id (plist-get geometry :id)
:block-id (plist-get geometry :id)
:edge (and geometry (org-timegrid--rail-edge-at geometry x))
:day (min (org-timegrid--last-day-index) (max 0 (floor
(/ (- x left-offset
(org-timegrid--label-width))
column-width))))
:minute 0
:lane (max 0 (floor (/ (- y (org-timegrid--rail-top))
(org-timegrid--all-day-lane-height))))))))))
(defun org-timegrid--header-navigation-action (position &optional coordinates window)
"Return the navigation command assigned to header POSITION, if any.
COORDINATES and WINDOW allow callers to supply already decoded position data."
(let* ((xy (or coordinates
(posn-object-x-y position)
(posn-x-y position)))
(x (car-safe xy))
(y (cdr-safe xy))
(window (or window (posn-window position)))
(left-offset (org-timegrid--header-left-offset window)))
(when (and (numberp x) (numberp y))
(cond
((< y (org-timegrid--header-title-height))
#'org-timegrid-goto-date)
((and (= org-timegrid-days 1)
(< x (+ left-offset (org-timegrid--label-width))))
#'org-timegrid-goto-today)
((and (> org-timegrid-days 1)
(< y (org-timegrid--rail-top))
(>= x (+ left-offset (org-timegrid--label-width))))
(let* ((column-width (org-timegrid--column-width))
(day (floor (/ (- x left-offset
(org-timegrid--label-width))
column-width)))
(absolute (+ (org-timegrid--calendar-state-week-start
org-timegrid--state)
day)))
(and (<= 0 day (org-timegrid--last-day-index))
(= absolute
(calendar-absolute-from-gregorian (org-timegrid--today)))
#'org-timegrid-goto-today)))))))
(defun org-timegrid--mouse-position-xy (position)
"Return comparable POSITION coordinates across the rail and time grid."
(let ((xy (or (posn-x-y position) (posn-object-x-y position))))
(and xy (list (org-timegrid--mouse-surface position)
(car xy) (cdr xy)))))
(defun org-timegrid--rail-area-p (area)
"Return non-nil when mouse AREA belongs to the all-day rail.
Image-map hotspots replace `header-line' in `posn-area', so all of the
rail's area names must be treated as one stable surface during a drag."
(memq area '(header-line calendar-rail-block calendar-rail-resize)))
(defun org-timegrid--mouse-surface (position)
"Return the stable calendar surface containing mouse POSITION."
(if (org-timegrid--rail-area-p (posn-area position)) 'rail 'grid))
(defun org-timegrid--mouse-target (position)
"Return calendar metadata for POSITION on either mouse surface."
(let ((window (posn-window position)))
(when (and (window-live-p window) (eq (window-buffer window) (current-buffer)))
(if (eq (org-timegrid--mouse-surface position) 'rail)
(org-timegrid--header-target position)
(org-timegrid--target position)))))
(defun org-timegrid-header-click (event)
"Move the shared calendar cursor to all-day rail mouse EVENT."
(interactive "@e")
(org-timegrid--with-update nil
(let* ((extend (memq 'shift (event-modifiers event)))
(old-point (and extend (org-timegrid--cursor-calendar-minute)))
(position (event-start event))
(navigation (org-timegrid--header-navigation-action position))
(target (org-timegrid--header-target position)))
(cond
(navigation
(call-interactively navigation))
((null target)
(message "Click inside an all-day cell"))
(t
(if extend
(unless (org-timegrid-region-active-p)
(setf (org-timegrid--calendar-state-mark org-timegrid--state)
old-point
(org-timegrid--calendar-state-region-active
org-timegrid--state) t))
(org-timegrid--deactivate-region))
(let ((day (plist-get target :day))
(lane (plist-get target :lane))
(id (plist-get target :id)))
(org-timegrid--set-all-day-cursor day lane)
(when id
(setf (org-timegrid--calendar-state-selected-id org-timegrid--state)
id))
(setf (org-timegrid--calendar-state-cursor-visible org-timegrid--state)
t)
(org-timegrid--cursor-moved)))))))
(defun org-timegrid--all-day-create-proposal (origin target)
"Return a date-only creation proposal spanning ORIGIN through TARGET.
Both endpoints are rail targets. The mouse-selected final day is inclusive;
the block and backend range use an exclusive midnight endpoint."
(let ((origin-day (plist-get origin :day))
(target-day (plist-get target :day)))
(if (or (null origin-day) (null target-day))
(org-timegrid--operation-create
:error "Release inside an all-day cell")
(let* ((first (min origin-day target-day))
(last (max origin-day target-day))
(block (org-timegrid--make-block
'preview first 0 (* (1+ (- last first)) 1440)
"New all-day block" nil nil 'all-day)))
(setf (org-timegrid-block-preview block) t)
(org-timegrid--operation-create :kind 'create :block block)))))
(defun org-timegrid--header-position-xy (position)
"Return the SVG-local coordinates of header-line POSITION."
(or (posn-object-x-y position) (posn-x-y position)))
(defmacro org-timegrid--with-gesture (origin &rest body)
"Run BODY while ORIGIN's frozen geometry is represented in calendar state."
(declare (indent 1) (debug t))
`(progn
(org-timegrid--with-update nil
(setf (org-timegrid--calendar-state-gesture org-timegrid--state)
(list :rail-rows (org-timegrid--rail-row-count (org-timegrid--all-day-layout))
:clock (when-let* ((block (org-timegrid--block (plist-get ,origin :block-id))))
(org-timegrid--clock-block-p block)))))
(unwind-protect (progn ,@body)
(org-timegrid--with-update nil
(setf (org-timegrid--calendar-state-gesture org-timegrid--state) nil)))))
(defun org-timegrid--track-drag-gesture
(event position-function target-function proposal-function click-function)
"Track one mouse EVENT independently of its calendar surface.
POSITION-FUNCTION returns comparable pixel coordinates, TARGET-FUNCTION
returns model coordinates, PROPOSAL-FUNCTION builds a preview, and
CLICK-FUNCTION handles a press that never becomes a drag."
(let* ((origin-position (event-start event))
(origin (funcall target-function origin-position))
(origin-xy (funcall position-function origin-position))
(end-target origin)
dragged finished next basic failure)