forked from haguenau/wyrd
-
Notifications
You must be signed in to change notification settings - Fork 2
/
interface_main.ml
2036 lines (1912 loc) · 81.4 KB
/
interface_main.ml
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
(* Wyrd -- a curses-based front-end for Remind
* Copyright (C) 2005, 2006, 2007, 2008, 2010, 2011-2013 Paul Pelzl
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, Version 2,
* as published by the Free Software Foundation.
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Bug reports can be entered at http://bugs.launchpad.net/wyrd .
* For anything else, feel free to contact Paul Pelzl at <[email protected]>.
*)
(* interface_main.ml
* This file has the bulk of the implementation of the curses-based interface,
* including the main program loop that grabs keypresses and processes them.
* The screen rendering code is found in interface_draw.ml . *)
open Curses
open Printf
open Interface
open Interface_draw
exception Interrupt_exception
exception Template_undefined of string
type reminder_type_t = Timed | Untimed | General of int
(*******************************************************************)
(* RESIZE HANDLER *)
(*******************************************************************)
(* create the (new) windows corresponding to the different areas of the screen *)
(* note: to get around curses issues with addch on the last character of the
* last row, all windows are created one line too long. The last lines are
* never used. (Is this the best way to do things?) *)
let create_windows screen =
let height, width = get_size () in
let cal_height = 10
and cal_width = if !Rcfile.untimed_window_width < 34 then 34
else if !Rcfile.untimed_window_width > width - 40 then width - 40
else !Rcfile.untimed_window_width
in
let msg_height = 6 in
let err_height = 1 in
let timed_height = height - 1 - msg_height - err_height
and timed_width = width - cal_width
and untimed_height = height - 1 - msg_height - err_height - cal_height
and untimed_width = cal_width in
if height >= 23 then
if width >= 80 then {
stdscr = screen;
lines = height;
cols = width;
help_win = newwin 2 width 0 0;
hw_cols = width;
timed_win = newwin (succ timed_height) timed_width 1 0;
tw_lines = timed_height;
tw_cols = timed_width;
calendar_win = newwin (succ cal_height) cal_width 1 timed_width;
cw_lines = cal_height;
cw_cols = cal_width;
untimed_win = newwin (succ untimed_height) untimed_width (1 + cal_height)
timed_width;
uw_lines = untimed_height;
uw_cols = untimed_width;
msg_win = newwin (succ msg_height) width (1 + timed_height) 0;
mw_lines = msg_height;
mw_cols = width;
err_win = newwin err_height width (pred height) 0;
ew_lines = err_height;
ew_cols = pred width (* don't print in last line of the last column *)
}
else
(endwin ();
failwith "Wyrd requires at least an 80 column window.")
else
(endwin ();
failwith "Wyrd requires at least a 23 line window.")
(* wresize all the child windows via wresize and mvwin *)
let resize_subwins iface =
let height, width = get_size () in
let cal_height = 10
and cal_width = if !Rcfile.untimed_window_width < 34 then 34
else if !Rcfile.untimed_window_width > width - 40 then width - 40
else 40
in
let msg_height = 6 in
let err_height = 1 in
let timed_height = height - 1 - msg_height - err_height
and timed_width = width - cal_width
and untimed_height = height - 1 - msg_height - err_height - cal_height
and untimed_width = cal_width in
let new_scr =
if height >= 23 then
if width >= 80 then {iface.scr with
lines = height;
cols = width;
hw_cols = width;
tw_lines = timed_height;
tw_cols = timed_width;
cw_lines = cal_height;
cw_cols = cal_width;
uw_lines = untimed_height;
uw_cols = untimed_width;
mw_lines = msg_height;
mw_cols = width;
ew_lines = err_height;
ew_cols = pred width
}
else
(endwin ();
failwith "Wyrd requires at least an 80 column window.")
else
(endwin ();
failwith "Wyrd requires at least a 23 line window.")
in
assert (wresize new_scr.help_win 2 width);
assert (wresize new_scr.timed_win (succ timed_height) timed_width);
assert (wresize new_scr.calendar_win (succ cal_height) cal_width);
assert (mvwin new_scr.calendar_win 1 timed_width);
assert (wresize new_scr.untimed_win (succ untimed_height) untimed_width);
assert (mvwin new_scr.untimed_win (1 + cal_height) timed_width);
assert (wresize new_scr.msg_win (succ msg_height) width);
assert (mvwin new_scr.msg_win (1 + timed_height) 0);
assert (wresize new_scr.err_win err_height width);
assert (mvwin new_scr.err_win (pred height) 0);
new_scr
(* Drop curses mode, invoke a function, and restore curses mode. *)
let drop_curses_across (f : 'a -> 'b) (x : 'a) : 'b =
def_prog_mode ();
endwin ();
let result = f x in
reset_prog_mode ();
let _ = curs_set 0 in
result
(* Invoke an external process as Unix.system, but loop on EINTR while waiting for process
* completion. This is a workaround for the fact that SIGWINCH may be received while curses mode is
* deactivated when we launch an external editor. *)
let system_retry_eintr (command : string) : Unix.process_status =
match Unix.fork () with
| 0 ->
begin try
Unix.execv "/bin/sh" [| "/bin/sh"; "-c"; command |]
with _ ->
exit 127
end
| pid ->
let rec loop_eintr () =
try
snd (Unix.waitpid [] pid)
with Unix.Unix_error (Unix.EINTR, _, _) ->
loop_eintr ()
in
loop_eintr ()
let drop_curses_system (command : string) : Unix.process_status =
drop_curses_across system_retry_eintr command
(* Parse a natural language event description and append
* the result to the specified reminders file. *)
let append_quick_event event_spec remfile =
let (rem_spec, description) =
Time_lang.parse_natural_language_event event_spec
in
let remline =
begin match rem_spec with
| Time_lang.Timed (start, finish) ->
let date_s =
Printf.sprintf "REM %s %d %d " (Utility.string_of_tm_mon start.Unix.tm_mon)
start.Unix.tm_mday (start.Unix.tm_year + 1900)
in
let start_s =
Printf.sprintf "AT %.2d:%.2d " start.Unix.tm_hour start.Unix.tm_min
in
let duration_s =
if start <> finish then
let (start_ts, _) = Unix.mktime start
and (finish_ts, _) = Unix.mktime finish in
let dur = int_of_float ((finish_ts -. start_ts) /. 60.0) in
Printf.sprintf "DURATION %d:%.2d " (dur / 60) (dur mod 60)
else
""
in
date_s ^ start_s ^ duration_s ^ "MSG " ^ description ^ "\n"
| Time_lang.Untimed timespec ->
let date_s =
Printf.sprintf "REM %s %d %d " (Utility.string_of_tm_mon timespec.Unix.tm_mon)
timespec.Unix.tm_mday (timespec.Unix.tm_year + 1900)
in
date_s ^ "MSG " ^ description ^ "\n"
end
in
(* append the remline to the reminders file *)
let remfile_channel =
open_out_gen [Open_append; Open_creat; Open_text] 416 (Utility.expand_file remfile)
in
output_string remfile_channel remline;
close_out remfile_channel;
(rem_spec, remline)
(* refresh the screen *)
let handle_refresh (iface : interface_state_t) reminders =
let _ = wtouchln iface.scr.help_win 0 1 true in
let _ = wtouchln iface.scr.timed_win 0 iface.scr.tw_lines true in
let _ = wtouchln iface.scr.calendar_win 0 iface.scr.cw_lines true in
let _ = wtouchln iface.scr.untimed_win 0 iface.scr.uw_lines true in
let _ = wtouchln iface.scr.msg_win 0 iface.scr.mw_lines true in
draw_help iface;
let new_iface = draw_timed iface reminders.Remind.all_timed in
draw_date_strip new_iface;
draw_calendar new_iface reminders;
let new_iface = draw_untimed new_iface reminders.Remind.curr_untimed in
if String.length reminders.Remind.remind_error > 0 then
let _ = beep () in ()
else
();
draw_error new_iface reminders.Remind.remind_error false;
(new_iface, reminders)
(* handle a curses resize *)
let handle_resize (iface : interface_state_t) reminders =
endwin ();
begin match iface.resize_failed_win with
| None -> ()
| Some w -> let _ = delwin w in ()
end;
begin try
let new_scr = resize_subwins iface in
let resized_iface = {
iface with scr = new_scr;
top_untimed = 0;
left_selection = if !Rcfile.center_cursor then (iface.scr.tw_lines / 2) - 1 else 1;
right_selection = 1;
timed_lineinfo = Array.make new_scr.tw_lines [];
resize_failed_win = None
} in
begin try
assert (curs_set 0)
with _ ->
()
end;
clear ();
assert (refresh ());
handle_refresh resized_iface reminders
with Failure s ->
(* Create a new full-screen window where we can
* paint an error message. *)
let w = newwin 0 0 0 0 in
erase ();
let _ = mvwaddstr w 0 0 s in
let _ = wnoutrefresh w in
let _ = doupdate () in
({iface with resize_failed_win = Some w}, reminders)
end
(* Any time a new item is selected, the reminders
* record needs updating and the screen need redrawing *)
let handle_selection_change iface reminders track_home_setting =
let new_reminders = Remind.update_reminders reminders
(timestamp_of_line iface iface.left_selection) in
let new_iface = draw_untimed iface new_reminders.Remind.curr_untimed in
let new_iface = draw_timed new_iface new_reminders.Remind.all_timed in
draw_date_strip new_iface;
draw_calendar new_iface new_reminders;
draw_error new_iface "" false;
({new_iface with track_home = track_home_setting}, new_reminders)
(* Same as previous, but without calling draw_timed () or
* draw_date_strip (). Optimization for the scrolling case. *)
let handle_selection_change_scroll iface reminders track_home_setting =
let new_reminders = Remind.update_reminders reminders
(timestamp_of_line iface iface.left_selection) in
draw_calendar iface new_reminders;
let new_iface = draw_untimed iface new_reminders.Remind.curr_untimed in
draw_error new_iface "" false;
({new_iface with track_home = track_home_setting}, new_reminders)
(* handle a "scroll down" event when the timed window is focused
* and the center_cursor option is turned on *)
let handle_scrolldown_timed_center (iface : interface_state_t) reminders =
let second_timestamp = timestamp_of_line iface 1 in
let iface2 = {
iface with top_timestamp = second_timestamp
} in
let (new_iface, new_reminders) =
handle_selection_change_scroll iface2 reminders false
in
(* use a curses scroll operation to shift up the timed window *)
assert (wscrl new_iface.scr.timed_win 1);
(* adjust lineinfo array to compensate for scrolling *)
Array.blit iface.timed_lineinfo 1 iface.timed_lineinfo 0
(pred (Array.length iface.timed_lineinfo));
(* do a two-line update to recenter the cursor *)
let new_iface = draw_timed_try_window new_iface new_reminders.Remind.all_timed
(iface.left_selection - 1) 2 in
(* draw in the new line at the bottom of the screen *)
let new_iface = draw_timed_try_window new_iface new_reminders.Remind.all_timed
(iface.scr.tw_lines - 1) 1 in
draw_date_strip new_iface;
(new_iface, new_reminders)
(* handle a "scroll down" event when the timed window is focused
* and the center_cursor option is turned off *)
let handle_scrolldown_timed_nocenter (iface : interface_state_t) reminders =
if iface.left_selection < pred iface.scr.tw_lines then begin
(* case 1: only the cursor moves *)
let iface2 = {
iface with left_selection = succ iface.left_selection
} in
let (new_iface, new_reminders) =
handle_selection_change_scroll iface2 reminders false
in
(* make a two-line update to erase and redraw the cursor *)
let new_iface = draw_timed_try_window new_iface new_reminders.Remind.all_timed
iface.left_selection 2 in
(new_iface, new_reminders)
end else begin
(* case 2: the entire timed window scrolls *)
let second_timestamp = timestamp_of_line iface 1 in
let iface2 = {
iface with top_timestamp = second_timestamp
} in
let (new_iface, new_reminders) =
handle_selection_change_scroll iface2 reminders false
in
(* use a curses scroll operation to shift up the timed window *)
assert (wscrl new_iface.scr.timed_win 1);
(* adjust lineinfo array to compensate for scrolling *)
Array.blit iface.timed_lineinfo 1 iface.timed_lineinfo 0
(pred (Array.length iface.timed_lineinfo));
(* do a two-line update to erase the cursor and draw in the
* new line at the bottom of the screen *)
let new_iface = draw_timed_try_window new_iface new_reminders.Remind.all_timed
(iface.scr.tw_lines - 2) 2 in
draw_date_strip new_iface;
(new_iface, new_reminders)
end
(* handle a "scroll down" event when the timed window is focused *)
let handle_scrolldown_timed (iface : interface_state_t) reminders =
(* watch out for y2k38 *)
let selected_day = Unix.localtime (iface.top_timestamp +.
(float_of_int (succ iface.left_selection)) *. (time_inc iface)) in
if (selected_day.Unix.tm_year + 1900) > 2037 then begin
draw_error iface "requested year is out of range." false;
assert (doupdate ());
(iface, reminders)
end else
let iface = {
iface with top_untimed = 0;
top_desc = 0;
right_selection = 1
} in
if !Rcfile.center_cursor then
handle_scrolldown_timed_center iface reminders
else
handle_scrolldown_timed_nocenter iface reminders
(* handle a "scroll down" event when the untimed window is focused *)
let handle_scrolldown_untimed (iface : interface_state_t) reminders =
if iface.right_selection < pred iface.scr.uw_lines then begin
if iface.right_selection < iface.len_untimed -
iface.top_untimed then begin
let new_iface = {
iface with right_selection = succ iface.right_selection
} in
handle_selection_change new_iface reminders false
end else
(iface, reminders)
end else begin
if iface.right_selection < iface.len_untimed -
iface.top_untimed then begin
let new_iface = {
iface with top_untimed = succ iface.top_untimed
} in
handle_selection_change new_iface reminders false
end else
(iface, reminders)
end
(* handle a "scroll up" event when the timed window is focused
* and the center_cursor option is turned on *)
let handle_scrollup_timed_center (iface : interface_state_t) reminders =
let prev_timestamp = timestamp_of_line iface (-1) in
let iface2 = {
iface with top_timestamp = prev_timestamp
} in
let (new_iface, new_reminders) =
handle_selection_change_scroll iface2 reminders false
in
(* use a curses scroll operation to shift up the timed window *)
assert (wscrl new_iface.scr.timed_win (-1));
(* adjust lineinfo array to compensate for scrolling *)
Array.blit iface.timed_lineinfo 0 iface.timed_lineinfo 1
(pred (Array.length iface.timed_lineinfo));
(* do a two-line update to recenter the cursor *)
let new_iface = draw_timed_try_window new_iface new_reminders.Remind.all_timed
iface.left_selection 2 in
(* draw in the new top line of the schedule *)
let new_iface = draw_timed_try_window new_iface new_reminders.Remind.all_timed 0 1 in
draw_date_strip new_iface;
(new_iface, new_reminders)
(* handle a "scroll up" event when the timed window is focused
* and the center_cursor option is turned off *)
let handle_scrollup_timed_nocenter (iface : interface_state_t) reminders =
if iface.left_selection > 0 then begin
(* case 1: only the cursor moves *)
let iface2 = {
iface with left_selection = pred iface.left_selection
} in
let (new_iface, new_reminders) =
handle_selection_change_scroll iface2 reminders false
in
(* make a two-line update to erase and redraw the cursor *)
let new_iface = draw_timed_try_window new_iface new_reminders.Remind.all_timed
(pred iface.left_selection) 2 in
(new_iface, new_reminders)
end else begin
(* case 2: the entire timed window scrolls *)
let prev_timestamp = timestamp_of_line iface (-1) in
let iface2 = {
iface with top_timestamp = prev_timestamp
} in
let (new_iface, new_reminders) =
handle_selection_change_scroll iface2 reminders false
in
(* use a curses scroll operation to shift up the timed window *)
assert (wscrl new_iface.scr.timed_win (-1));
(* adjust lineinfo array to compensate for scrolling *)
Array.blit iface.timed_lineinfo 0 iface.timed_lineinfo 1
(pred (Array.length iface.timed_lineinfo));
(* do a two-line update to erase the cursor and draw in the
* new line at the bottom of the screen *)
let new_iface = draw_timed_try_window new_iface new_reminders.Remind.all_timed 0 2 in
draw_date_strip new_iface;
(new_iface, new_reminders)
end
(* handle a "scroll up" event when the timed window is focused *)
let handle_scrollup_timed (iface : interface_state_t) reminders =
(* watch out for scrolling out of Remind date range *)
let selected_day = Unix.localtime iface.top_timestamp in
if (selected_day.Unix.tm_year + 1900) < 1991 then begin
draw_error iface "requested year is out of range." false;
assert (doupdate ());
(iface, reminders)
end else
let iface = {
iface with top_untimed = 0;
top_desc = 0;
right_selection = 1
} in
if !Rcfile.center_cursor then
handle_scrollup_timed_center iface reminders
else
handle_scrollup_timed_nocenter iface reminders
(* handle a "scroll up" event when the untimed window is focused *)
let handle_scrollup_untimed (iface : interface_state_t) reminders =
if iface.right_selection > 1 then
let new_iface = {
iface with right_selection = pred iface.right_selection
} in
handle_selection_change new_iface reminders new_iface.track_home
else if iface.top_untimed > 0 then
let new_iface = {
iface with top_untimed = pred iface.top_untimed
} in
handle_selection_change new_iface reminders new_iface.track_home
else
(iface, reminders)
(* handle a jump to a different day *)
let handle_jump (iface : interface_state_t) reminders jump_func =
(* new timestamp for top line of screen *)
let temp = Unix.localtime iface.top_timestamp in
let next_tm = jump_func temp in
let (next_ts, normalized_tm) = Unix.mktime next_tm in
(* new timestamp for bottom line of screen *)
let temp2 = Unix.localtime (iface.top_timestamp +.
(float_of_int (succ iface.left_selection)) *. (time_inc iface)) in
let next_tm2 = jump_func temp2 in
let (_, normalized_tm2) = Unix.mktime next_tm2 in
(* check that the date range is OK for both Remind and *nix *)
if (normalized_tm.Unix.tm_year + 1900) < 1991 ||
(normalized_tm2.Unix.tm_year + 1900) > 2037 then begin
draw_error iface "requested year is out of range." false;
assert (doupdate ());
(iface, reminders)
end else
let new_iface = {
iface with top_timestamp = next_ts;
top_desc = 0
} in
handle_selection_change new_iface reminders false
(* handle switching window focus *)
let handle_switch_focus (iface : interface_state_t) reminders =
let new_iface =
match iface.selected_side with
|Left -> {iface with selected_side = Right}
|Right -> {iface with selected_side = Left}
in
handle_selection_change new_iface reminders new_iface.track_home
(* handle switching to the current timeslot *)
let handle_home (iface : interface_state_t) reminders =
let curr_time = Unix.localtime ((Unix.time ()) -. (time_inc iface)) in
let (rounded_time, _) = Unix.mktime (round_time iface.zoom_level curr_time) in
let new_iface = {
iface with top_timestamp =
if !Rcfile.center_cursor then
rounded_time -. (time_inc iface) *. (float_of_int ((iface.scr.tw_lines / 2) - 2))
else
rounded_time -. (time_inc iface) *. 1.;
top_desc = 0;
selected_side = Left;
left_selection = if !Rcfile.center_cursor then (iface.scr.tw_lines / 2) - 1 else 2;
right_selection = 1
} in
handle_selection_change new_iface reminders true
(* handle a zoom keypress *)
let handle_zoom (iface : interface_state_t) reminders =
let new_iface =
let curr_ts = timestamp_of_line iface iface.left_selection in
let curr_tm = Unix.localtime curr_ts in
let hour_tm = {
curr_tm with Unix.tm_sec = 0;
Unix.tm_min = 0
} in
let (hour_ts, _) = Unix.mktime hour_tm in
match iface.zoom_level with
|Hour ->
let new_top = curr_ts -. (60.0 *. 30.0 *.
(float_of_int iface.left_selection)) in {
iface with top_timestamp = new_top;
top_desc = 0;
zoom_level = HalfHour
}
|HalfHour ->
let new_top = curr_ts -. (60.0 *. 15.0 *.
(float_of_int iface.left_selection)) in {
iface with top_timestamp = new_top;
top_desc = 0;
zoom_level = QuarterHour
}
|QuarterHour ->
let new_top = hour_ts -. (60.0 *. 60.0 *.
(float_of_int iface.left_selection)) in {
iface with top_timestamp = new_top;
top_desc = 0;
zoom_level = Hour
}
in
if new_iface.track_home then
handle_home new_iface reminders
else
let new_iface = draw_timed new_iface reminders.Remind.all_timed in
draw_date_strip new_iface;
(new_iface, reminders)
(* handle creation of a new timed or untimed reminder *)
let handle_new_reminder (iface : interface_state_t) reminders rem_type
remfile =
let ts = timestamp_of_line iface iface.left_selection in
let tm = Unix.localtime ts in
(* take care of the substitution characters in remline templates *)
let substitute template =
let rec substitute_aux subst_list s =
match subst_list with
|[] ->
s
|(regex, replacement) :: tail ->
let new_s = Str.global_replace regex replacement s in
substitute_aux tail new_s
in
substitute_aux [
(Str.regexp "%monname%", Printf.sprintf !Rcfile.monname_fmt
(Utility.string_of_tm_mon tm.Unix.tm_mon));
(Str.regexp "%mon%", Printf.sprintf !Rcfile.mon_fmt (succ tm.Unix.tm_mon));
(Str.regexp "%mday%", Printf.sprintf !Rcfile.mday_fmt tm.Unix.tm_mday);
(Str.regexp "%year%", Printf.sprintf !Rcfile.year_fmt
(tm.Unix.tm_year + 1900));
(Str.regexp "%hour%", Printf.sprintf !Rcfile.hour_fmt tm.Unix.tm_hour);
(Str.regexp "%min%", Printf.sprintf !Rcfile.min_fmt tm.Unix.tm_min);
(Str.regexp "%wdayname%", Printf.sprintf !Rcfile.wdayname_fmt
(Utility.string_of_tm_wday tm.Unix.tm_wday));
(Str.regexp "%wday%", Printf.sprintf !Rcfile.wday_fmt tm.Unix.tm_wday)
] template
in
try
let remline =
match rem_type with
| Timed -> substitute !Rcfile.timed_template
| Untimed -> substitute !Rcfile.untimed_template
| General x ->
let template_status =
match x with
| 0 -> (!Rcfile.template0, "template0")
| 1 -> (!Rcfile.template1, "template1")
| 2 -> (!Rcfile.template2, "template2")
| 3 -> (!Rcfile.template3, "template3")
| 4 -> (!Rcfile.template4, "template4")
| 5 -> (!Rcfile.template5, "template5")
| 6 -> (!Rcfile.template6, "template6")
| 7 -> (!Rcfile.template7, "template7")
| 8 -> (!Rcfile.template8, "template8")
| 9 -> (!Rcfile.template9, "template9")
| _ -> failwith ("unexpected template number " ^ (string_of_int x) ^
" in handle_new_reminder")
in
begin match template_status with
| (None, template_str) -> raise (Template_undefined template_str)
| (Some t, _) -> substitute t
end
in
(* append the remline to the reminders file *)
let remfile_channel =
open_out_gen [Open_append; Open_creat; Open_text] 416 remfile
in
output_string remfile_channel remline;
close_out remfile_channel;
(* open the reminders file in an editor *)
let filename_sub = Str.regexp "%file%" in
let command =
Str.global_replace filename_sub remfile
!Rcfile.edit_new_command
in
let editor_process_status = drop_curses_system command in
let r = Remind.create_three_month iface.top_timestamp in
(* if the untimed list has been altered, change the focus to
* the first element of the list *)
let new_iface =
if List.length r.Remind.curr_untimed <>
List.length reminders.Remind.curr_untimed then {
iface with top_untimed = 0;
top_desc = 0;
right_selection = 1
}
else
iface
in
begin match editor_process_status with
| Unix.WEXITED return_code ->
if return_code <> 0 then begin
let (new_iface, r) = handle_refresh new_iface r in
let _ = beep () in
draw_error new_iface
"Error when launching editor; configure a different editor in ~/.wyrdrc ." false;
assert (doupdate ());
(new_iface, r)
end else
handle_refresh new_iface r
| _ ->
let (new_iface, r) = handle_refresh new_iface r in
draw_error new_iface
"Editor process was interrupted." false;
assert (doupdate ());
(new_iface, r)
end
with Template_undefined template_str ->
draw_error iface (template_str ^ " is undefined.") false;
assert (doupdate ());
(iface, reminders)
(* Search forward for the next occurrence of the current search regex.
*
* First find the date of the occurrence, by matching on the output of 'remind
* -n'. For that date, recompute timed and untimed reminder lists. Filter the
* timed and untimed reminder lists to get only the entries falling on the
* occurrence date. Search through the filtered timed list first, and locate
* the first entry that matches the regex (if any). Then search through the
* filtered untimed list, and locate the first entry that matches the regex (if any).
* If only one of the lists has a match, return that one; if both lists have a match,
* return the one with the earlier timestamp.
* Finally, reconfigure the iface record to highlight the matched entry.
*
* If the user has advance_warning enabled, the algorithm becomes more complicated.
* After finding the correct search date, we have to locate reminders that are
* present when advance warnings are *suppressed*. This is accomplished by
* searching through the reminders list with advanced warning suppressed, then
* searching for an *identical string* within the reminders list with advanced
* warning enabled.
*
* The saved search regex can be ignored by providing Some regex as the
* override_regex parameter.
*
* FIXME: This function is about five times too large. *)
let handle_find_next (iface : interface_state_t) reminders override_regex =
let search_regex =
match override_regex with
|None -> iface.search_regex
|Some regex -> regex
in
try
(* Note: selected_ts is the timestamp of the next timeslot, minus one second.
* This ensures that searching begins immediately after the current
* selection. *)
let selected_ts = (timestamp_of_line iface (succ iface.left_selection)) -. 1.0 in
let occurrence_time = Remind.find_next search_regex selected_ts in
(* Reminders with advance warning included *)
let no_advw_reminders=
if !Rcfile.advance_warning then
Remind.create_three_month ~suppress_advwarn:true occurrence_time
else
Remind.update_reminders reminders occurrence_time
in
(* Reminders with advance warning suppressed *)
let advw_reminders =
if !Rcfile.advance_warning then
Remind.update_reminders reminders occurrence_time
else
no_advw_reminders
in
let occurrence_tm = Unix.localtime occurrence_time in
let temp1 = {
occurrence_tm with Unix.tm_sec = 0;
Unix.tm_min = 0;
Unix.tm_hour = 0
} in
let temp2 = {
occurrence_tm with Unix.tm_sec = 0;
Unix.tm_min = 0;
Unix.tm_hour = 0;
Unix.tm_mday = succ occurrence_tm.Unix.tm_mday
} in
let (day_start_ts, _) = Unix.mktime temp1 in
let (day_end_ts, _) = Unix.mktime temp2 in
(* filter functions to determine reminders falling on the occurrence day *)
let is_current_untimed rem =
rem.Remind.ur_start >= day_start_ts && rem.Remind.ur_start < day_end_ts
in
let is_current_timed rem =
rem.Remind.tr_start >= day_start_ts && rem.Remind.tr_start < day_end_ts
in
(* test the untimed reminders list, with advanced warnings enabled,
* for entries that match the specified string *)
let rec check_untimed_advw match_string untimed n timed_match =
match untimed with
|[] ->
begin match timed_match with
|None ->
let _ = beep () in
draw_error iface "search expression not found." false;
assert (doupdate ());
(iface, reminders)
|Some (timed_match_ts, timed_match_iface) ->
handle_selection_change timed_match_iface advw_reminders false
end
|urem :: tail ->
begin try
if urem.Remind.ur_start > selected_ts && urem.Remind.ur_msg = match_string then
let tm = Unix.localtime urem.Remind.ur_start in
let (rounded_time, _) = Unix.mktime (round_time iface.zoom_level tm) in
let new_iface =
(* take care of highlighting the correct untimed reminder *)
if n >= iface.scr.uw_lines then
if !Rcfile.center_cursor then {
iface with top_timestamp = rounded_time -. (time_inc iface) *.
(float_of_int (iface.scr.tw_lines / 2 - 1));
top_untimed = n - iface.scr.uw_lines + 1;
top_desc = 0;
left_selection = (iface.scr.tw_lines / 2) - 1;
right_selection = pred iface.scr.uw_lines;
selected_side = Right
} else {
iface with top_timestamp = rounded_time;
top_untimed = n - iface.scr.uw_lines + 1;
top_desc = 0;
left_selection = 0;
right_selection = pred iface.scr.uw_lines;
selected_side = Right
}
else
if !Rcfile.center_cursor then {
iface with top_timestamp = rounded_time -. (time_inc iface) *.
(float_of_int (iface.scr.tw_lines / 2 - 1));
top_untimed = 0;
top_desc = 0;
left_selection = (iface.scr.tw_lines / 2) - 1 ;
right_selection = n;
selected_side = Right
} else {
iface with top_timestamp = rounded_time;
top_untimed = 0;
top_desc = 0;
left_selection = 0;
right_selection = n;
selected_side = Right
}
in
(* choose between the timed reminder match and the untimed reminder match *)
begin match timed_match with
|None ->
handle_selection_change new_iface advw_reminders false
|Some (timed_match_ts, timed_match_iface) ->
if timed_match_ts < urem.Remind.ur_start then
handle_selection_change timed_match_iface advw_reminders false
else
handle_selection_change new_iface advw_reminders false
end
else
raise Not_found
with Not_found ->
check_untimed_advw match_string tail (succ n) timed_match
end
in
(* test the untimed reminders list, with advanced warnings suppressed,
* for entries that match the regex *)
let rec check_untimed_no_advw untimed n timed_match =
match untimed with
|[] ->
begin match timed_match with
|None ->
let _ = beep () in
draw_error iface "search expression not found." false;
assert (doupdate ());
(iface, reminders)
|Some (timed_match_ts, timed_match_iface) ->
handle_selection_change timed_match_iface advw_reminders false
end
|urem :: tail ->
begin try
if urem.Remind.ur_start > selected_ts then
(* If we find a match, then look for an identical string in
* the list of untimed reminders with advance warning enabled *)
let _ = Str.search_forward search_regex urem.Remind.ur_msg 0 in
let today_untimed_advw =
List.filter is_current_untimed advw_reminders.Remind.curr_untimed
in
check_untimed_advw urem.Remind.ur_msg today_untimed_advw 1 timed_match
else
raise Not_found
with Not_found ->
check_untimed_no_advw tail (succ n) timed_match
end
in
(* test the timed reminders list for entries that match the regex *)
let rec check_timed timed =
match timed with
|[] ->
let today_untimed_no_advw =
List.filter is_current_untimed no_advw_reminders.Remind.curr_untimed
in
check_untimed_no_advw today_untimed_no_advw 1 None
|trem :: tail ->
begin try
if trem.Remind.tr_start > selected_ts then
let _ = Str.search_forward search_regex trem.Remind.tr_msg 0 in
let tm = Unix.localtime trem.Remind.tr_start in
let (rounded_time, _) = Unix.mktime (round_time iface.zoom_level tm) in
let new_iface =
if !Rcfile.center_cursor then {
iface with top_timestamp = rounded_time -. (time_inc iface) *.
(float_of_int (iface.scr.tw_lines / 2 - 1));
top_desc = 0;
left_selection = (iface.scr.tw_lines / 2) - 1;
right_selection = 1;
selected_side = Left
}
else {
iface with top_timestamp = rounded_time -. (time_inc iface) *. 2.;
top_desc = 0;
left_selection = 2;
right_selection = 1;
selected_side = Left
}
in
let today_untimed_no_advw =
List.filter is_current_untimed no_advw_reminders.Remind.curr_untimed
in
check_untimed_no_advw today_untimed_no_advw 1 (Some (trem.Remind.tr_start, new_iface))
else
raise Not_found
with Not_found ->
check_timed tail
end
in
let merged_rem = Remind.merge_timed no_advw_reminders.Remind.curr_timed in
check_timed (List.filter is_current_timed merged_rem)
with Remind.Occurrence_not_found ->
let _ = beep () in
draw_error iface "search expression not found." false;
(iface, reminders)
(* Begin entry of a search string *)
let handle_begin_search (iface : interface_state_t) reminders =
let new_iface = {
iface with entry_mode = Extended ExtendedSearch
} in
draw_error iface "search expression: " true;
(new_iface, reminders)
(* Find the next reminder after the current selection.
* This algorithm is a dirty hack, but actually seems to work:
* 1) If an untimed reminder is selected, and if it is not the last
* untimed reminder in the list, then highlight the next untimed reminder.
* 2) Otherwise, run find_next with a regexp that matches anything. *)
let handle_next_reminder (iface : interface_state_t) reminders =
if iface.selected_side = Right &&
iface.right_selection < iface.len_untimed - iface.top_untimed then
if !Rcfile.advance_warning then begin
(* Compute the list of untimed reminders currently displayed to the user *)
let displayed_reminders =
Remind.get_untimed_reminders_for_day reminders.Remind.curr_untimed
(timestamp_of_line iface iface.left_selection)
in
(* Compute the same list, with advance warnings suppressed *)
let displayed_reminders_no_advw =
let reminders_no_advw =
Remind.create_three_month ~suppress_advwarn:true
(timestamp_of_line iface iface.left_selection)
in
Remind.get_untimed_reminders_for_day reminders_no_advw.Remind.curr_untimed
(timestamp_of_line iface iface.left_selection)
in
(* Index of currently selected reminder within displayed_reminders *)
let selection_index = iface.right_selection + iface.top_untimed in
(* Simultaneously iterate through the two lists until we reach
* the currently selected element. Theoretically displayed_reminders is a
* superset of displayed_reminders_no_advw with ordering preserved, so we
* should be able to match identical elements as we go. *)
let rec iter_match superset subset n prev_iface prev_rem =
match superset with
| [] ->
(* Some sort of search failure. Just skip over today's untimed
* reminders. *)
handle_find_next iface reminders (Some (Str.regexp ""))
| superset_head :: superset_tail ->
begin match subset with
| [] ->
(* Ran out of reminders in the list with advance warning
* suppressed. Therefore we can skip over untimed reminders. *)
handle_find_next iface reminders (Some (Str.regexp ""))
| subset_head :: subset_tail ->
if n > selection_index then
let (next_iface, next_rem) =
handle_scrolldown_untimed prev_iface prev_rem
in
if superset_head = subset_head then
(* This should be the correct element. End here. *)
(next_iface, next_rem)
else
(* Keep searching forward through superset list
* to try to locate this element. *)
iter_match superset_tail subset (succ n) next_iface next_rem
else
if superset_head = subset_head then begin
(* Iterate both... *)
iter_match superset_tail subset_tail (succ n) prev_iface prev_rem