-
Notifications
You must be signed in to change notification settings - Fork 1
/
lorg-calendar.el
executable file
·1456 lines (1386 loc) · 60.6 KB
/
lorg-calendar.el
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
;; Scroll a yearly calendar by month -- in a forwards or backwards direction.
;; INSTRUCTIONS:
;; M-x lorg-calendar-generate
;;
;; To scroll month by month forward, use: >
;; To scroll month by month backward, use: <
;; To mark a new date not yet selected, use the left click on the mouse,
;; which will also copy the date to the kill-ring. Left-clicking on a
;; date previously marked with the left-click will remove the overlay.
(require 'calendar)
(require 'holidays)
;;; FIXME: The version of `org-mode' that ships with Emacs 26 is finicky --
;;; it is probably a bug in the loading process of the `org-element.el' library.
;;; The following is a workaround to avoid a loading error.
(defun org-element-update-syntax ()
"Update parser internals."
(interactive)
(require 'org-element)
(org-element--set-regexps)
(org-element-cache-reset 'all))
(require 'org)
(require 'org-element)
(eval-after-load "calendar" '(progn
(define-key calendar-mode-map "<" 'lorg-calendar-backward-month)
(define-key calendar-mode-map ">" 'lorg-calendar-forward-month)
(define-key calendar-mode-map "." 'lorg-calendar-goto-today)
(define-key calendar-mode-map [mouse-1] 'lorg-mark-mouse-set-point)
(define-key calendar-mode-map [up] 'lorg-calendar-backward-week)
(define-key calendar-mode-map [down] 'lorg-calendar-forward-week)
(define-key calendar-mode-map [right] 'lorg-calendar-forward-day)
(define-key calendar-mode-map [left] 'lorg-calendar-backward-day) ))
(defun lorg-calendar-forward-day (arg)
"Move the cursor forward ARG days. Moves backward if ARG is negative."
(interactive "p")
(unless (zerop arg)
(let* ((cursor-date (or (lorg-calendar-cursor-to-date)
(progn
(if (> arg 0) (setq arg (1- arg)))
(lorg-calendar-cursor-to-nearest-date))))
(new-cursor-date
(calendar-gregorian-from-absolute
(+ (calendar-absolute-from-gregorian cursor-date) arg)))
(new-displayed-month (calendar-extract-month new-cursor-date))
(new-displayed-year (calendar-extract-year new-cursor-date)))
;; Put the new month on the screen, if needed.
(unless (and (lorg-calendar-date-is-visible-p new-cursor-date)
(= new-displayed-month displayed-month)
(= new-displayed-year displayed-year))
(let ((old-date (lorg-calendar-cursor-to-date))
(today (lorg-calendar-current-date)))
(lorg-calendar-generate new-displayed-month new-displayed-year)
(lorg-calendar-cursor-to-visible-date
(cond
((lorg-calendar-date-is-visible-p old-date) old-date)
((lorg-calendar-date-is-visible-p today) today)
(t (list month 1 year))))))
;; Go to the new date.
(lorg-calendar-cursor-to-visible-date new-cursor-date)))
(run-hooks 'lorg-calendar-move-hook))
(defun lorg-calendar-backward-day (arg)
"Move the cursor back ARG days.
Moves forward if ARG is negative."
(interactive "p")
(lorg-calendar-forward-day (- arg)))
(defun lorg-calendar-forward-week (arg)
"Move the cursor forward ARG weeks.
Moves backward if ARG is negative."
(interactive "p")
(lorg-calendar-forward-day (* arg 7)))
(defun lorg-calendar-backward-week (arg)
"Move the cursor back ARG weeks.
Moves forward if ARG is negative."
(interactive "p")
(lorg-calendar-forward-day (* arg -7)))
(defvar lorg-calendar-buffer "*Calendar*"
"This is the `buffer-name` of the calendar buffer.")
(defvar lorg-calendar-style nil
"A symbol -- 'twelve-months or 'three-months")
(make-variable-buffer-local 'lorg-calendar-style)
(defmacro lorg-calendar-for-loop (var from init to final do &rest body)
"Execute a for loop.
Evaluate BODY with VAR bound to successive integers from INIT to FINAL,
inclusive. The standard macro `dotimes' is preferable in most cases."
`(let ((,var (1- ,init)))
(while (>= ,final (setq ,var (1+ ,var)))
,@body)))
;;;###autoload
(defun lorg-calendar-generate (&optional month year)
"Generate a 3-month or 12-month Gregorian calendar. The `displayed-month`
and `displayed-year` in a 3-month calendar are the month and year in the
center. The `displayed-month` and `displayed-year` in a 12-month calendar
are the month and year in the upper left-hand corner of the buffer.
This is a modification of: http://homepage3.nifty.com/oatu/emacs/calendar.html"
(interactive)
(let* (
(eval-sexp-fn
(lambda (list)
(let (res h err)
(sort
(dolist (p list res)
(if (setq h (eval p))
(setq res (append h res))))
'calendar-date-compare))))
(org-startup-folded nil)
(today (lorg-calendar-current-date))
(current-year (number-to-string (nth 5 (decode-time (current-time)))))
(month
(cond
((called-interactively-p)
(read-number
"Please choose the `displayed-month` (1 through 12): " 1))
(t
(if month month (calendar-extract-month today)))))
(year
(cond
((called-interactively-p)
(read-number
"Please choose the `displayed-year` (e.g., 2014): " 2014))
(t
(if year year (calendar-extract-year today)))))
(style
(cond
((called-interactively-p)
(lorg-message
"Please choose letter a or b: [a] 3-month calendar; or, [b] 12-month calendar ?")
(let ((selected-calendar-style (read-char-exclusive)))
(cond
((eq selected-calendar-style ?a)
'three-months)
((eq selected-calendar-style ?b)
'twelve-months)
(t
'twelve-months) )))
((get-buffer lorg-calendar-buffer)
(with-current-buffer (get-buffer lorg-calendar-buffer)
lorg-calendar-style))
(t 'three-months)))
(lorg-court-holiday t)
(appointment t)
(lorg-birthday t)
(lorg-court-appearance t)
lorg-court-appearances
;; lorg-birthdays
lorg-appointments
narrowed
element
todo-state
deadline
scheduled
day
title
(old-date
(when (get-buffer lorg-calendar-buffer)
(with-current-buffer (get-buffer lorg-calendar-buffer)
(lorg-calendar-cursor-to-nearest-date))))
(org-todo-keywords '((sequence
"Active(a)"
"Next Action(n)"
"Canceled(c)"
"Hold(h)"
"Reference(r)"
"Delegated(d)"
"Waiting(w)"
"Postponed(P)"
"Someday(s)"
"Planning(p)"
"|"
"None(N)") ))
(sample-todo (concat
"* TASKS\n\n"
"** Active [#A] albert @ Dune (Frank Herbert). :albert:@context_one:\n"
" DEADLINE: <2013-12-21 Sat 17:00> SCHEDULED: <2013-12-21 Sat>\n"
" :PROPERTIES:\n"
" :Effort: 15\n"
" :Hash:\n"
" :ToodledoFolder: TASKS\n"
" :ToodledoGoal: Goal #1\n"
" :ToodledoID:\n"
" :ToodledoLocation: Location #1\n"
" :END:\n\n"
"** Next Action [#B] 0 @ Ender's Game (Orson Scott Card). :lorg:@context_two:\n"
" DEADLINE: <2014-02-22 Sat 08:00> SCHEDULED: <2014-02-22 Sat>\n"
" :PROPERTIES:\n"
" :Effort: 30\n"
" :Hash:\n"
" :ToodledoFolder: TASKS\n"
" :ToodledoGoal: Goal #2\n"
" :ToodledoID:\n"
" :ToodledoLocation: Location #2\n"
" :END:\n"
"• This novel was of particular interest because . . .\n"
"• Orson Scott Card has written other books . . . \n"
"• Compare and contrast Ender's brother and sister.\n\n"
"** Hold [#C] wells @ War of the Worlds (H G Wells). :lorg:@context_three:\n"
" DEADLINE: <2014-04-03 Thu 08:30> SCHEDULED: <2014-04-03 Thu>\n"
" :PROPERTIES:\n"
" :Effort: 45\n"
" :Hash:\n"
" :ToodledoFolder: TASKS\n"
" :ToodledoGoal: Goal #3\n"
" :ToodledoID:\n"
" :ToodledoLocation: Location #3\n"
" :END:\n\n"
"** Canceled [#D] wells @ The Invisible Man (H G Wells). :lorg:@context_four:\n"
" DEADLINE: <2014-04-07 Mon>\n"
" :PROPERTIES:\n"
" :Effort: 60\n"
" :Hash:\n"
" :ToodledoFolder: TASKS\n"
" :ToodledoGoal: Goal #4\n"
" :ToodledoID:\n"
" :ToodledoLocation: Location #4\n"
" :END:\n\n"
"* EVENTS\n\n"
"** Delegated [#A] candice @ meeting -- February 8, 2014; 08:00. :candice:@context_five:\n"
" DEADLINE: <2014-02-08 Sat 08:00> SCHEDULED: <2014-02-08 Sat>\n"
" :PROPERTIES:\n"
" :Effort: 75\n"
" :Hash:\n"
" :ToodledoFolder: EVENTS\n"
" :ToodledoGoal: Goal #5\n"
" :ToodledoID:\n"
" :ToodledoLocation: Location #5\n"
" :END:\n\n"
"** Postponed [#B] george @ 2001 - A Space Odyssey (Arthur C Clarke). :george:@context_six:\n"
" DEADLINE: <2014-02-12 Wed>\n"
" :PROPERTIES:\n"
" :Effort: 90\n"
" :Hash:\n"
" :ToodledoFolder: EVENTS\n"
" :ToodledoGoal: Goal #6\n"
" :ToodledoID:\n"
" :ToodledoLocation: Location #6\n"
" :END:\n\n"
"** Waiting [#C] edward @ birthday -- February 15, 2014. :edward:@context_seven:\n"
" DEADLINE: <2014-02-15 Sat>\n"
" :PROPERTIES:\n"
" :Effort: 105\n"
" :Hash:\n"
" :ToodledoFolder: EVENTS\n"
" :ToodledoGoal: Goal #7\n"
" :ToodledoID:\n"
" :ToodledoLocation: Location #7\n"
" :END:\n\n"
"** Reference [#D] fred @ lorg-court-appearance -- February 26, 2014 at 8:30 a.m. :fred:@context_eight:\n"
" DEADLINE: <2014-02-26 Wed 08:30> SCHEDULED: <2014-02-26 Wed>\n"
" :PROPERTIES:\n"
" :Effort: 120\n"
" :Hash:\n"
" :ToodledoFolder: EVENTS\n"
" :ToodledoGoal: Goal #8\n"
" :ToodledoID:\n"
" :ToodledoLocation: Location #8\n"
" :END:\n\n"
"* UNDATED\n\n"
"** Someday [#D] harold @ The Foundation Trilogy (Isaac Asimov). :harold:@context_nine:\n"
" :PROPERTIES:\n"
" :Effort: 135\n"
" :Hash:\n"
" :ToodledoFolder: UNDATED\n"
" :ToodledoGoal: Goal #9\n"
" :ToodledoID:\n"
" :ToodledoLocation: Location #9\n"
" :END:\n\n"
"* CONTACTS\n\n"
"** Planning [#D] John H. Smith :smith:@context_ten:\n"
" :PROPERTIES:\n"
" :Effort: 150\n"
" :Hash:\n"
" :ToodledoFolder: CONTACTS\n"
" :ToodledoGoal: Goal #10\n"
" :ToodledoID:\n"
" :ToodledoLocation: Location #10\n"
" :END:\n"
"1234 Civic Center Drive, Suite 300\n"
"New York, NY 12345\n"
" work: (212) 123-4567\n"
" email: [email protected]\n\n"
"* DONE\n\n"
"** None [#E] 0 @ Provide a working example on StackOverflow. :lorg:@context_eleven:\n"
" :PROPERTIES:\n"
" :Effort: 165\n"
" :Hash:\n"
" :ToodledoFolder: DONE\n"
" :ToodledoGoal: Goal #11\n"
" :ToodledoID:\n"
" :ToodledoLocation: Location #11\n"
" :END:")))
(with-current-buffer (get-buffer-create lorg-calendar-buffer)
(when (not (eq major-mode 'calendar-mode))
(calendar-mode))
(setq displayed-month month)
(setq displayed-year year)
(setq buffer-read-only nil)
(erase-buffer)
(cond
((eq style 'twelve-months)
(setq lorg-calendar-style 'twelve-months)
;; horizontal row
(lorg-calendar-for-loop j from 0 to 3 do
;; vertical column
(lorg-calendar-for-loop i from 0 to 2 do
(calendar-generate-month
;; month
(cond
((> (+ (* j 3) i month) 12)
(- (+ (* j 3) i month) 12))
(t
(+ (* j 3) i month)))
;; year
(cond
((> (+ (* j 3) i month) 12)
(+ year 1))
(t
year))
;; indentation / spacing between months
(+ 5 (* 25 i))))
(goto-char (point-max))
(insert (make-string (- 10 (count-lines (point-min) (point-max))) ?\n))
(widen)
(goto-char (point-max))
(narrow-to-region (point-max) (point-max)))
(widen) )
((eq style 'three-months)
(setq lorg-calendar-style 'three-months)
(if (< (+ month (* 12 (1- year))) 2)
(error "Months before January, 1 AD cannot be displayed"))
(calendar-increment-month month year -1)
(dotimes (i 3)
(calendar-generate-month month year (+ calendar-left-margin (* calendar-month-width i)))
(calendar-increment-month month year 1))))
(setq buffer-read-only t)
(setq cursor-type '(hbar . 2))
(calendar-update-mode-line) )
;; lorg mark calendar -- BEGIN
(if (get-buffer "foo.org")
(with-current-buffer "foo.org"
(erase-buffer))
(with-current-buffer (get-buffer-create "foo.org")
(org-mode)))
(with-current-buffer (get-buffer "foo.org")
(insert sample-todo)
(goto-char (point-min))
(save-excursion
(when (lorg-narrow-p)
(setq narrowed t)
(widen))
(goto-char (point-max))
(while (re-search-backward "^\\*\\* \\(Reference\\|Delegated\\|Waiting\\)" nil t)
(setq element (org-element-at-point))
(setq todo-state (org-element-property :todo-keyword element))
(setq title (org-element-property :title element))
(setq deadline
(ignore-errors
(org-element-property :deadline element) ))
(setq scheduled
(ignore-errors
(org-element-property :scheduled element) ))
(setq day (ignore-errors (org-element-property :day-start deadline)))
(setq month (ignore-errors (org-element-property :month-start deadline)))
(setq year (ignore-errors (org-element-property :year-start deadline)))
(when (and
scheduled
deadline
(string= todo-state "Reference"))
;; formatting of `lorg-cal-sexp` code provided by @phils
;; http://stackoverflow.com/q/20715445/2112489
(setq lorg-court-appearances
(append lorg-court-appearances
`((lorg-cal-sexp '(list ,month ,day ,year) (regexp-quote ,title))))))
(when (and
scheduled
deadline
(string= todo-state "Delegated"))
;; formatting of `lorg-cal-sexp` code provided by @phils
;; http://stackoverflow.com/q/20715445/2112489
(setq lorg-appointments
(append lorg-appointments
`((lorg-cal-sexp '(list ,month ,day ,year) (regexp-quote ,title))))))
;; (when (and
;; deadline
;; (string= todo-state "Waiting"))
;; ;; formatting of `lorg-cal-sexp` code provided by @phils
;; ;; http://stackoverflow.com/q/20715445/2112489
;; (setq lorg-birthdays
;; (append lorg-birthdays
;; `((lorg-cal-sexp '(list ,month ,day ,year) (regexp-quote ,title))))))
))
(if narrowed (org-narrow-to-subtree)))
(with-current-buffer (get-buffer lorg-calendar-buffer)
;; lorg-court-holidays
(dolist (holiday (funcall eval-sexp-fn lorg-court-holidays))
(lorg-calendar-mark-visible-date (car holiday) lorg-court-holiday-face nil lorg-court-holiday nil nil nil))
;; lorg-court-appearances
(dolist (appearance (funcall eval-sexp-fn lorg-court-appearances))
(lorg-calendar-mark-visible-date (car appearance) lorg-court-appearance-face nil nil lorg-court-appearance nil nil))
;; lorg-appointments
(dolist (appointment (funcall eval-sexp-fn lorg-appointments))
(lorg-calendar-mark-visible-date (car appointment) lorg-appointment-face nil nil nil appointment nil))
;; lorg-birthdays
(dolist (birthday (funcall eval-sexp-fn lorg-birthdays))
(lorg-calendar-mark-visible-date (car birthday) lorg-birthday-face nil nil nil nil lorg-birthday))
;; lorg-mouse-calendar-face markings
(dolist (mouse-mark (funcall eval-sexp-fn lorg-mouse-marked))
(lorg-calendar-mark-visible-date (car mouse-mark) lorg-mouse-calendar-face))
;; lorg mark calendar -- END
(cond
((and
old-date
(lorg-calendar-date-is-visible-p old-date))
(lorg-calendar-cursor-to-visible-date old-date))
((lorg-calendar-date-is-visible-p today)
(lorg-calendar-cursor-to-visible-date today))
(t
(lorg-calendar-cursor-to-visible-date (list displayed-month 1 displayed-year)))))
(cond
((eq style 'twelve-months)
(lorg-display-buffer-right (get-buffer lorg-calendar-buffer) '((window-width . 82))))
((eq style 'three-months)
(lorg-display-buffer-below (get-buffer lorg-calendar-buffer) '((window-height . 12)))))
(lorg-display-buffer-left (get-buffer "foo.org") nil)
(select-window (get-buffer-window lorg-calendar-buffer)) ))
(defvar lorg-mouse-marked nil
"A list of the calendar dates that have been marked with the mouse.")
(make-variable-buffer-local 'lorg-mouse-marked)
(defun lorg-mouse-marked ()
(let (lorg-mouse-marked-date
lorg-mouse-marked-day
lorg-mouse-marked-month
lorg-mouse-marked-year)
(setq lorg-mouse-marked nil)
(when (get-buffer lorg-calendar-buffer)
(with-current-buffer (get-buffer lorg-calendar-buffer)
(save-excursion
(goto-char (point-max))
(while (re-search-backward "[0-9]" nil t)
(when
(memq lorg-mouse-calendar-face
(mapcar (function (lambda (ovr)
(overlay-get ovr 'face))) (overlays-at (point))))
(setq lorg-mouse-marked-date (lorg-calendar-cursor-to-nearest-date))
(setq lorg-mouse-marked-day (calendar-extract-day lorg-mouse-marked-date))
(setq lorg-mouse-marked-month (calendar-extract-month lorg-mouse-marked-date))
(setq lorg-mouse-marked-year (calendar-extract-year lorg-mouse-marked-date))
;; formatting of `lorg-cal-sexp` code provided by @phils
;; http://stackoverflow.com/q/20715445/2112489
(setq lorg-mouse-marked
(append lorg-mouse-marked
`((lorg-cal-sexp '(list
,lorg-mouse-marked-month
,lorg-mouse-marked-day
,lorg-mouse-marked-year) ""))))) )) ))))
(defun lorg-mark-mouse-set-point (event)
(interactive "e")
(mouse-set-point event)
(if (not (overlays-at (point)))
(progn
(lorg-calendar-mark-visible-date (lorg-calendar-cursor-to-date t) lorg-mouse-calendar-face)
(lorg-mouse-marked)
(lorg-calendar-copy-date nil))
(lorg-calendar-copy-date nil)
(let (beg end)
(cond
;; This mapcar overlay snippet was borrowed from a function written by Drew Adams
;; `mark-visible-calendar-date` at http://www.emacswiki.org/emacs/calendar%2B.el
((memq lorg-mouse-calendar-face
(mapcar (function (lambda (ovr)
(overlay-get ovr 'face))) (overlays-at (point))))
(cond
;; cursor is one whitespace to the left of 1 to 9
((and
(save-excursion (< 0 (skip-chars-forward " \t")))
(not (save-excursion (< 0 (skip-syntax-forward "w")))))
(save-excursion
(setq beg (point))
(skip-chars-forward " \t")
(skip-syntax-forward "w")
(setq end (point))
(mapc 'delete-overlay (overlays-in beg end))))
;; cursor is sandwiched between a digit on each side.
((and
(save-excursion (> 0 (skip-syntax-backward "w")))
(save-excursion (< 0 (skip-syntax-forward "w"))))
(save-excursion
(skip-syntax-backward "w")
(setq beg (point))
(skip-syntax-forward "w")
(setq end (point))
(mapc 'delete-overlay (overlays-in beg end))))
;; cursor is to the far right of one or two digit dates
((and
(save-excursion (> 0 (skip-syntax-backward "w")))
(not (save-excursion (< 0 (skip-syntax-forward "w")))))
(save-excursion
(skip-syntax-backward "w")
(setq beg (point))
(skip-syntax-forward "w")
(setq end (point))
(mapc 'delete-overlay (overlays-in beg end))))
;; cursor to the far left of one or two digits dates
((and
(save-excursion (< 0 (skip-syntax-forward "w")))
(not (save-excursion (> 0 (skip-syntax-backward "w")))))
(save-excursion
(skip-syntax-forward "w")
(setq end (point))
(skip-syntax-backward "w")
(setq beg (1- (point))) ;; I am using `1-' for days 1 to 9
(mapc 'delete-overlay (overlays-in beg end)))) )
(lorg-mouse-marked))
(t
(lorg-message "This fall-back feature has not yet been defined.") ) ))))
(defun lorg-calendar-mark-visible-date (date &optional mark priority lorg-court-holiday lorg-court-appearance lorg-appointment lorg-birthday)
"A modification of the function `mark-visible-calendar-date` by Drew Adams: http://www.emacswiki.org/emacs/calendar%2B.el"
(when (calendar-date-is-valid-p date)
(save-excursion
(set-buffer lorg-calendar-buffer)
(lorg-calendar-cursor-to-visible-date date)
(let ((mark (or mark diary-entry-marker)))
(if (stringp mark)
(let ((buffer-read-only nil))
(forward-char 1)
(delete-char 1)
(insert mark)
(forward-char -2))
(cond
((and
lorg-appointment
(memq lorg-court-appearance-face
(mapcar (function (lambda (ovr)
(overlay-get ovr 'face))) (overlays-at (point)))) )
(let ((ovrly (make-overlay (1-(point)) (1+ (point)))))
(overlay-put ovrly 'face lorg-appointment-appearance-face)
(overlay-put ovrly 'priority 1001)))
((and
lorg-appointment
(memq lorg-court-holiday-face
(mapcar (function (lambda (ovr)
(overlay-get ovr 'face))) (overlays-at (point)))))
(let ((ovrly (make-overlay (1-(point)) (1+ (point)))))
(overlay-put ovrly 'face lorg-holiday-appointment-face)
(overlay-put ovrly 'priority 1001)))
((and
lorg-appointment
(not (memq lorg-court-holiday-face
(mapcar (function (lambda (ovr)
(overlay-get ovr 'face))) (overlays-at (point)))))
(not (memq lorg-court-appearance-face
(mapcar (function (lambda (ovr)
(overlay-get ovr 'face))) (overlays-at (point)))) ))
(let ((ovrly (make-overlay (1-(point)) (1+ (point)))))
(overlay-put ovrly 'face lorg-appointment-face)
(overlay-put ovrly 'priority 1000)))
(lorg-court-appearance
(let ((ovrly (make-overlay (1-(point)) (1+ (point)))))
(overlay-put ovrly 'face lorg-court-appearance-face)
(overlay-put ovrly 'priority 1000)))
((and
lorg-birthday
(not (memq lorg-court-holiday-face
(mapcar (function (lambda (ovr)
(overlay-get ovr 'face))) (overlays-at (point)))))
(not (memq lorg-court-appearance-face
(mapcar (function (lambda (ovr)
(overlay-get ovr 'face))) (overlays-at (point)))))
(not (memq lorg-appointment-face
(mapcar (function (lambda (ovr)
(overlay-get ovr 'face))) (overlays-at (point))))))
(let ((ovrly (make-overlay (1-(point)) (1+ (point)))))
(overlay-put ovrly 'face lorg-birthday-face)
(overlay-put ovrly 'priority 1001)))
((and
lorg-birthday
(or
(memq lorg-court-holiday-face
(mapcar (function (lambda (ovr)
(overlay-get ovr 'face))) (overlays-at (point))))
(memq lorg-court-appearance-face
(mapcar (function (lambda (ovr)
(overlay-get ovr 'face))) (overlays-at (point))))
(memq lorg-appointment-face
(mapcar (function (lambda (ovr)
(overlay-get ovr 'face))) (overlays-at (point))))))
(let ((ovrly (make-overlay (1-(point)) (1+ (point)))))
(overlay-put ovrly 'face lorg-birthday+others-face)
(overlay-put ovrly 'priority 1001)))
(t
(let ((ovrly (make-overlay (1-(point)) (1+ (point)))))
(overlay-put ovrly 'face mark)
(when priority (overlay-put ovrly 'priority 1000)))) ))))))
(defun lorg-calendar-copy-date (arg)
"http://www.emacswiki.org/emacs-se/IanYang
Copy date under the cursor. Read format from minibuffer if ARG, use
recently used format if no ARG. See the function `format-time-string'
for the document of time format string"
(interactive "P")
(let ((date (lorg-calendar-cursor-to-date t))
(format (if arg
(completing-read
"Date Format:"
(list "%B %d, %Y") nil nil nil
'(list "%B %d, %Y") nil nil)
(car (list "%B %d, %Y"))))
string)
(setq date (encode-time 0 0 0 (cadr date) (car date) (nth 2 date)))
(setq string (format-time-string format date))
(if (eq last-command 'kill-region)
(kill-append string nil)
(kill-new string))
(message "%s" string)))
(defun lorg-calendar-date-is-visible-p (date)
"Return non-nil if DATE is valid and is visible in the lorg-calendar window."
(let ((style (and (get-buffer lorg-calendar-buffer)
(with-current-buffer (get-buffer lorg-calendar-buffer)
lorg-calendar-style))))
(cond
((eq style 'twelve-months)
(let* ((incoming-test-date (calendar-absolute-from-gregorian date))
(beginning-date (calendar-absolute-from-gregorian (list displayed-month 1 displayed-year)))
(ending-date (+ 364 beginning-date)))
(and
(>= incoming-test-date beginning-date)
(<= incoming-test-date ending-date))))
(t
(and (calendar-date-is-valid-p date)
(< (abs (calendar-interval
displayed-month displayed-year
(calendar-extract-month date) (calendar-extract-year date)))
2))))))
(defun lorg-calendar-cursor-to-date (&optional error event)
"Return a list (month day year) of current cursor position.
If cursor is not on a specific date, signals an error if optional parameter
ERROR is non-nil, otherwise just returns nil.
If EVENT is non-nil, it's an event indicating the buffer position to
use instead of point."
(let ((style (and (get-buffer lorg-calendar-buffer)
(with-current-buffer (get-buffer lorg-calendar-buffer)
lorg-calendar-style))))
(cond
((eq style 'twelve-months)
(let* ((column-segment (/ (current-column) 25))
(line-segment (/ (count-lines (point-min) (point)) 9))
(current-block (+ (+ (* line-segment 3) column-segment) 1))
(tentative-month (- (+ current-block displayed-month) 1))
(month
(cond
((> tentative-month 12)
(- tentative-month 12))
(t
tentative-month)))
(year
(cond
((>= tentative-month 13)
(+ displayed-year 1))
(t
displayed-year))) )
(if (and (looking-at "[ 0-9]?[0-9][^0-9]")
(< 2 (count-lines (point-min) (point))))
(save-excursion
(if (not (looking-at " "))
(re-search-backward "[^0-9]"))
(list month
(string-to-number
(buffer-substring (1+ (point)) (+ 4 (point))))
year))
(if (looking-at "\\*")
(save-excursion
(re-search-backward "[^*]")
(if (looking-at ".\\*\\*")
(list month calendar-starred-day year)
(if error
(let ((debug-on-quit nil))
(signal 'quit `("Not on a date!"))))))
(if error
(let ((debug-on-quit nil))
(signal 'quit `("Not on a date!"))))))))
(t
(with-current-buffer
(if event (window-buffer (posn-window (event-start event)))
(current-buffer))
(save-excursion
(and event (setq event (event-start event))
(goto-char (posn-point event)))
(let* ((segment (calendar-column-to-segment))
(month (% (+ displayed-month (1- segment)) 12)))
;; Call with point on either of the two digits in a 2-digit date,
;; or on or before the digit of a 1-digit date.
(if (not (and (looking-at "[ 0-9]?[0-9][^0-9]")
(get-text-property (point) 'date)))
(if error
(let ((debug-on-quit nil))
(signal 'quit `("Not on a date!"))))
;; Convert segment to real month and year.
(if (zerop month) (setq month 12))
;; Go back to before the first date digit.
(or (looking-at " ")
(re-search-backward "[^0-9]"))
(list month
(string-to-number
(buffer-substring (1+ (point))
(+ 1 calendar-day-digit-width (point))))
(cond
((and (= 12 month) (zerop segment)) (1- displayed-year))
((and (= 1 month) (= segment 2)) (1+ displayed-year))
(t displayed-year)))))))))))
(defun lorg-calendar-cursor-to-visible-date (date)
"Move the cursor to DATE that is on the screen."
(let ((style (and (get-buffer lorg-calendar-buffer)
(with-current-buffer (get-buffer lorg-calendar-buffer)
lorg-calendar-style))))
(cond
((eq style 'twelve-months)
(let* (
(month (calendar-extract-month date))
(day (calendar-extract-day date))
(year (calendar-extract-year date))
(first-of-month-weekday (calendar-day-of-week (list month 1 year)))
;; algorithm for rowmove and colmove written by @AShelly
;; http://stackoverflow.com/a/21709710/2112489
(difference
(if (>= month displayed-month)
(- month displayed-month)
(- (+ month 12) displayed-month)))
(rows (/ difference 3))
(cols (% difference 3))
(rowmove (* 9 rows))
(colmove (+ 6 (* 25 cols))) )
(goto-line
(+ 3
rowmove
(/ (+ day -1
(mod
(- (calendar-day-of-week (list month 1 year)) calendar-week-start-day)
7))
7)))
(move-to-column
(+
colmove
(* 3 (mod
(- (calendar-day-of-week date) calendar-week-start-day)
7))))))
(t
(let ((month (calendar-extract-month date))
(day (calendar-extract-day date))
(year (calendar-extract-year date)))
(goto-char (point-min))
(forward-line (+ calendar-first-date-row -1
(/ (+ day -1
(mod
(- (calendar-day-of-week (list month 1 year))
calendar-week-start-day)
7))
7)))
(move-to-column (+ calendar-left-margin (1- calendar-day-digit-width)
(* calendar-month-width
(1+ (calendar-interval
displayed-month displayed-year month year)))
(* calendar-column-width
(mod
(- (calendar-day-of-week date)
calendar-week-start-day)
7)))))))
(if (get-buffer-window lorg-calendar-buffer (selected-frame))
(set-window-point (get-buffer-window lorg-calendar-buffer (selected-frame)) (point)))))
(defun lorg-target-year-function (target-month)
"Algorithm written by @legoscia on stackoverflow:
http://stackoverflow.com/a/21834918/2112489"
(if (>= target-month displayed-month)
displayed-year
(1+ displayed-year)))
(defun lorg-holiday-fixed (month day string)
"Holiday on MONTH, DAY (Gregorian) called STRING.
If MONTH, DAY is visible, the value returned is the list (((MONTH DAY year)
STRING)). Returns nil if it is not visible in the current calendar window."
(cond
((eq lorg-calendar-style 'twelve-months)
(let ((y (lorg-target-year-function month)))
(list (list (list month day y) string))))
((eq lorg-calendar-style 'three-months)
(let ((m displayed-month)
(y displayed-year))
(calendar-increment-month m y (- 11 month))
(if (> m 9) ; Is November visible?
(list (list (list month day y) string)))))))
(defcustom lorg-birthdays (mapcar 'purecopy '(
(lorg-holiday-fixed 1 2 "Jane Doe -- 01/02/1940")
(lorg-holiday-fixed 2 15 "John Doe -- 02/15/1963")
(lorg-holiday-fixed 3 2 "Seymoure Hersh -- 03/03/1999")
(lorg-holiday-fixed 3 3 "Jashua Smith -- 03/03/1964")
(lorg-holiday-fixed 3 5 "Frederick Holmes -- 03/05/1966")
(lorg-holiday-fixed 4 7 "Fannie Mae -- 04/07/1970")
(lorg-holiday-fixed 4 25 "Freddie Mack -- 04/25/1952")
(holiday-float 5 0 2 "Mother's Day -- the second Sunday in May")
(lorg-holiday-fixed 5 11 "George Lucas -- 05/11/1976")
(lorg-holiday-fixed 5 18 "Harry Potter -- 05/18")
(lorg-holiday-fixed 5 30 "Darth Vader -- 05/30/1972")
(lorg-holiday-fixed 6 7 "Jabba the Hut -- 06/07/2007")
(lorg-holiday-fixed 6 19 "Princess Lea -- 06/19/1983")
(lorg-holiday-fixed 7 14 "Super Man -- 07/14/1970")
(lorg-holiday-fixed 7 18 "Wonder Woman -- 07/18/1993")
(lorg-holiday-fixed 10 3 "Jenifer Lopez (DOB: 10/03/2011)")
(lorg-holiday-fixed 10 8 "Samuel Jacks (10/08/1965)")
(lorg-holiday-fixed 10 25 "C3PO -- 10/25/2007")
(lorg-holiday-fixed 11 14 "R2D2 -- 11/14/1981")
(lorg-holiday-fixed 12 21 "Yoda -- 12/21/1958")
(lorg-holiday-fixed 12 22 "Wookie -- 12/22/1967") ))
"Birthdays."
:type 'sexp
:group 'holidays)
(defcustom lorg-court-holidays
(mapcar 'purecopy
'(
(lorg-new-year-day)
(lorg-holiday-float 1 1 3 "Martin Luther King Day")
(lorg-lincoln-day)
(lorg-holiday-float 2 1 3 "President's Day")
(lorg-cesar-chavez-day)
(lorg-holiday-float 5 1 -1 "Memorial Day")
(lorg-independence-day)
(lorg-holiday-float 9 1 1 "Labor Day")
(lorg-holiday-float 10 1 2 "Columbus Day")
(lorg-veterans-day)
(lorg-holiday-float 11 4 4 "Thanksgiving")
(lorg-day-after-thanksgiving)
(lorg-christmas-day) ))
"Court holidays."
:type 'sexp
:group 'holidays)
(defun lorg-new-year-day ()
(cond
((eq lorg-calendar-style 'twelve-months)
(let* ((y (lorg-target-year-function 1))
(d (calendar-day-of-week (list 1 1 y))) )
(cond
((= d 6) ;; Saturday
(list (list (list 1 3 y) "New Year + 2")))
((= d 0) ;; Sunday
(list (list (list 1 2 y) "New Year + 1")))
(t
(list (list (list 1 1 y) "New Year"))))) )
((eq lorg-calendar-style 'three-months)
(let* ((y displayed-year)
(d (calendar-day-of-week (list 1 1 y))))
(cond
((<= displayed-month 2)
(cond
((= d 6) ;; Saturday
(list (list (list 1 3 y) "New Year + 2")))
((= d 0) ;; Sunday
(list (list (list 1 2 y) "New Year + 1")))
(t
(list (list (list 1 1 y) "New Year")))))
((= displayed-month 12)
(setq y (+ y 1))
(setq d (calendar-day-of-week (list 1 1 y)))
(cond
((= d 6) ;; Saturday
(list (list (list 1 3 y) "New Year + 2")))
((= d 0) ;; Sunday
(list (list (list 1 2 y) "New Year + 1")))
(t
(list (list (list 1 1 y) "New Year"))))) )) )))
(defun lorg-lincoln-day ()
(cond
((eq lorg-calendar-style 'twelve-months)
(let* ((y (lorg-target-year-function 2))
(d (calendar-day-of-week (list 2 12 y))))
(cond
((= d 6)
(list (list (list 2 11 y) "Lincoln Day")))
((= d 0)
(list (list (list 2 13 y) "Lincoln Day")))
(t
(list (list (list 2 12 y) "Lincoln Day")) ))))
((eq lorg-calendar-style 'three-months)
(let* ((y displayed-year)
(d (calendar-day-of-week (list 2 12 y))))
(when (or (= displayed-month 1) (= displayed-month 2) (= displayed-month 3))
(cond
((= d 6)
(list (list (list 2 11 y) "Lincoln Day")))
((= d 0)
(list (list (list 2 13 y) "Lincoln Day")))
(t
(list (list (list 2 12 y) "Lincoln Day")) )))))))
(defun lorg-cesar-chavez-day ()
(cond
((eq lorg-calendar-style 'twelve-months)
(let* ((y (lorg-target-year-function 3))
(d (calendar-day-of-week (list 3 31 y))))
(cond
((= d 6)
(list (list (list 3 30 y) "Cesar Chavez Day")))
((= d 0)
(list (list (list 4 1 y) "Cesar Chavez Day")))
(t
(list (list (list 3 31 y) "Cesar Chavez Day")) ))))
((eq lorg-calendar-style 'three-months)
(let* ((y displayed-year)
(d (calendar-day-of-week (list 3 31 y))))
(cond
((and (= d 6) (or (= displayed-month 2) (= displayed-month 3) (= displayed-month 4)))
(list (list (list 3 30 y) "Cesar Chavez Day")))
((and (= d 0) (or (= displayed-month 3) (= displayed-month 4) (= displayed-month 5)))
(list (list (list 4 1 y) "Cesar Chavez Day")))
((and (not (or (= d 6) (= d 0))) (or (= displayed-month 2) (= displayed-month 3) (= displayed-month 4)))
(list (list (list 3 31 y) "Cesar Chavez Day")) ))))))
(defun lorg-independence-day ()
(cond
((eq lorg-calendar-style 'twelve-months)
(let* ((y (lorg-target-year-function 7))
(d (calendar-day-of-week (list 7 4 y))) )
(cond
((= d 6)
(list (list (list 7 3 y) "Independence Day")))
((= d 0)
(list (list (list 7 5 y) "Independence Day")))
(t
(list (list (list 7 4 y) "Independence Day")) ))))
((eq lorg-calendar-style 'three-months)
(let* ((y displayed-year)
(d (calendar-day-of-week (list 7 4 y))))
(when (or (= displayed-month 6) (= displayed-month 7) (= displayed-month 8))
(cond
((= d 6)
(list (list (list 7 3 y) "Independence Day")))
((= d 0)
(list (list (list 7 5 y) "Independence Day")))
(t
(list (list (list 7 4 y) "Independence Day")) )))))))
(defun lorg-veterans-day ()
(cond
((eq lorg-calendar-style 'twelve-months)
(let* ((y (lorg-target-year-function 11))
(d (calendar-day-of-week (list 11 11 y))))
(cond
((= d 6)
(list (list (list 11 10 y) "Veteran's Day")))
((= d 0)
(list (list (list 11 12 y) "Veteran's Day")))
(t
(list (list (list 11 11 y) "Veteran's Day")) ))))
((eq lorg-calendar-style 'three-months)
(let* ((y displayed-year)
(d (calendar-day-of-week (list 11 11 y))))
(when (>= displayed-month 10)
(cond
((= d 6)
(list (list (list 11 10 y) "Veteran's Day")))
((= d 0)
(list (list (list 11 12 y) "Veteran's Day")))
(t
(list (list (list 11 11 y) "Veteran's Day")) )))))))
(defun lorg-day-after-thanksgiving ()
(cond
((eq lorg-calendar-style 'twelve-months)
(let* ((y (lorg-target-year-function 11))
(d (calendar-day-of-week (list 11 1 y))))
(cond
((= d 5)
(list (list (list 11 29 y) "Day After Thanksgiving")))
((= d 6)
(list (list (list 11 28 y) "Day After Thanksgiving")))
(t
(lorg-holiday-float 11 5 4 "Day After Thanksgiving")))) )
((eq lorg-calendar-style 'three-months)
(let* ((y displayed-year)
(d (calendar-day-of-week (list 11 1 y))))