forked from ghennequin/llpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.ml
1962 lines (1843 loc) · 57.5 KB
/
config.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
open Utils;;
external fz_version : unit -> string = "ml_fz_version";;
type fontstate =
{ mutable fontsize : int
; mutable wwidth : float
; mutable maxrows : int
}
;;
let fstate =
{ fontsize = 14
; wwidth = nan
; maxrows = -1
}
;;
let scrollbvv = 1;;
let scrollbhv = 2;;
let fastghyllscroll = (5,1,2);;
let neatghyllscroll = (10,1,9);;
let irect_of_string s =
Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
;;
let irect_to_string (x0,y0,x1,y1) =
Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
;;
let ghyllscroll_of_string s =
match s with
| "fast" -> Some fastghyllscroll
| "neat" -> Some (10,1,9)
| "" | "none" -> None
| _ ->
let (n,a,b) as nab =
Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b) in
if n <= a || n <= b || a >= b
then error "N(%d),A(%d),B(%d) (N <= A, A < B, N <= B)" n a b;
Some nab
;;
let ghyllscroll_to_string ((n, a, b) as nab) =
(**) if nab = fastghyllscroll then "fast"
else if nab = neatghyllscroll then "neat"
else Printf.sprintf "%d,%d,%d" n a b;
;;
let multicolumns_to_string (n, a, b) =
if a = 0 && b = 0
then Printf.sprintf "%d" n
else Printf.sprintf "%d,%d,%d" n a b;
;;
let multicolumns_of_string s =
try
(int_of_string s, 0, 0)
with _ ->
Scanf.sscanf s "%u,%u,%u" (fun n a b ->
if a > 1 || b > 1
then failwith "subtly broken";
(n, a, b)
);
;;
type keymap =
| KMinsrt of key
| KMinsrl of key list
| KMmulti of key list * key list
and key = int * int
and keyhash = (key, keymap) Hashtbl.t
and keystate =
| KSnone
| KSinto of (key list * key list)
and interpagespace = int
and multicolumns = multicol * pagegeom
and singlecolumn = pagegeom
and splitcolumns = columncount * pagegeom
and pagegeom = (pdimno * x * y * (pageno * width * height * leftx)) array
and multicol = columncount * covercount * covercount
and pdimno = int
and columncount = int
and covercount = int
and fitmodel = | FitWidth | FitProportional | FitPage
and trimmargins = bool
and irect = (int * int * int * int)
and memsize = int
and texcount = int
and sliceheight = int
and angle = int
and params = (angle * fitmodel * trimparams
* texcount * sliceheight * memsize
* colorspace * fontpath * trimcachepath
* haspbo * usefontconfig)
and width = int
and height = int
and leftx = int
and opaque = Opaque.t
and rectcolor = (float * float * float * float)
and pixmapsize = int
and gen = int
and top = float
and dtop = float
and fontpath = string
and trimcachepath = string
and aalevel = int
and trimparams = (trimmargins * irect)
and colorspace = | Rgb | Bgr | Gray
and haspbo = bool
and usefontconfig = bool
and uri = string
and caption = string
and x = int
and y = int
and tilex = int
and tiley = int
and tileparams = (x * y * width * height * tilex * tiley)
and under =
| Unone
| Ulinkuri of string
| Ulinkgoto of (int * int)
| Utext of facename
| Uunexpected of string
| Ulaunch of launchcommand
| Unamed of destname
| Uremote of (filename * pageno)
| Uremotedest of (filename * destname)
| Uannotation of (opaque * slinkindex)
and slinkindex = int
and facename = string
and launchcommand = string
and filename = string
and pageno = int
and destname = string
and mark =
| Mark_page
| Mark_block
| Mark_line
| Mark_word
and link =
| Lnotfound
| Lfound of int
and linkdir =
| LDfirst
| LDlast
| LDfirstvisible of (int * int * int)
| LDleft of int
| LDright of int
| LDdown of int
| LDup of int
and pagewithlinks =
| Pwlnotfound
| Pwl of int
and scrollb = int
and anchor = pageno * top * dtop
and rect = float * float * float * float * float * float * float * float
and infochange = | Memused | Docinfo | Pdim
;;
class type uioh = object
method display : unit
method key : int -> int -> uioh
method button : int -> bool -> int -> int -> int -> uioh
method multiclick : int -> int -> int -> int -> uioh
method motion : int -> int -> uioh
method pmotion : int -> int -> uioh
method infochanged : infochange -> unit
method scrollpw : (int * float * float)
method scrollph : (int * float * float)
method modehash : keyhash
method eformsgs : bool
method alwaysscrolly : bool
end;;
module type TextEnumType =
sig
type t
val name : string
val names : string array
end;;
module TextEnumMake (Ten : TextEnumType) =
struct
let names = Ten.names;;
let to_int (t : Ten.t) = Obj.magic t;;
let to_string t = names.(to_int t);;
let of_int n : Ten.t = Obj.magic n;;
let of_string s =
let rec find i =
if i = Array.length names
then failwith ("invalid " ^ Ten.name ^ ": " ^ s)
else (
if Ten.names.(i) = s
then of_int i
else find (i+1)
)
in find 0;;
end;;
module CSTE = TextEnumMake (struct
type t = colorspace;;
let name = "colorspace";;
let names = [|"rgb"; "bgr"; "gray"|];;
end);;
module MTE = TextEnumMake (struct
type t = mark;;
let name = "mark";;
let names = [|"page"; "block"; "line"; "word"|];;
end);;
module FMTE = TextEnumMake (struct
type t = fitmodel;;
let name = "fitmodel";;
let names = [|"width"; "proportional"; "page"|];;
end);;
type conf =
{ mutable scrollbw : int
; mutable scrollh : int
; mutable scrollb : scrollb
; mutable icase : bool
; mutable preload : bool
; mutable pagebias : int
; mutable verbose : bool
; mutable debug : bool
; mutable scrollstep : int
; mutable hscrollstep : int
; mutable maxhfit : bool
; mutable crophack : bool
; mutable autoscrollstep : int
; mutable maxwait : float option
; mutable hlinks : bool
; mutable underinfo : bool
; mutable interpagespace : interpagespace
; mutable zoom : float
; mutable presentation : bool
; mutable angle : angle
; mutable cwinw : int
; mutable cwinh : int
; mutable savebmarks : bool
; mutable fitmodel : fitmodel
; mutable trimmargins : trimmargins
; mutable trimfuzz : irect
; mutable memlimit : memsize
; mutable texcount : texcount
; mutable sliceheight : sliceheight
; mutable thumbw : width
; mutable jumpback : bool
; mutable bgcolor : (float * float * float)
; mutable bedefault : bool
; mutable tilew : int
; mutable tileh : int
; mutable mustoresize : memsize
; mutable checkers : bool
; mutable aalevel : int
; mutable urilauncher : string
; mutable pathlauncher : string
; mutable colorspace : colorspace
; mutable invert : bool
; mutable colorscale : float
; mutable ghyllscroll : (int * int * int) option
; mutable columns : columns
; mutable beyecolumns : columncount option
; mutable selcmd : string
; mutable paxcmd : string
; mutable passcmd : string
; mutable savecmd : string
; mutable updatecurs : bool
; mutable keyhashes : (string * keyhash) list
; mutable hfsize : int
; mutable pgscale : float
; mutable usepbo : bool
; mutable wheelbypage : bool
; mutable stcmd : string
; mutable riani : bool
; mutable pax : (float * int * int) ref option
; mutable paxmark : mark
; mutable leftscroll : bool
; mutable title : string
; mutable lastvisit : float
; mutable annotinline : bool
; mutable coarseprespos : bool
}
and columns =
| Csingle of singlecolumn
| Cmulti of multicolumns
| Csplit of splitcolumns
and outlinekind =
| Onone
| Oanchor of anchor
| Ouri of uri
| Olaunch of launchcommand
| Oremote of (filename * pageno)
| Oremotedest of (filename * destname)
| Ohistory of (filename * conf * outline list * x * anchor * filename)
and outline = (caption * outlinelevel * outlinekind)
and outlinelevel = int
;;
type page =
{ pageno : int
; pagedimno : int
; pagew : int
; pageh : int
; pagex : int
; pagey : int
; pagevw : int
; pagevh : int
; pagedispx : int
; pagedispy : int
; pagecol : int
}
;;
type tile = opaque * pixmapsize * elapsed
and elapsed = float;;
type pagemapkey = pageno * gen;;
type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
and row = int
and col = int
and currently =
| Idle
| Loading of (page * gen)
| Tiling of (
page * opaque * colorspace * angle * gen * col * row * width * height
)
| Outlining of outline list
;;
type mpos = int * int
and mstate =
| Msel of (mpos * mpos)
| Mpan of mpos
| Mscrolly | Mscrollx
| Mzoom of (buttonno * step * mpos)
| Mzoomrect of (mpos * mpos)
| Mnone
and buttonno = int
and step = int
;;
type mode =
| Birdseye of (conf * leftx * pageno * pageno * anchor)
| Textentry of (textentry * onleave)
| View
| LinkNav of linktarget
and onleave = leavetextentrystatus -> unit
and leavetextentrystatus = | Cancel | Confirm
and helpitem = string * int * action
and action =
| Noaction
| Action of (uioh -> uioh)
and linktarget =
| Ltexact of (pageno * direction)
| Ltgendir of direction
| Ltnotready of (pageno * direction)
and direction = int (* -1, 0, 1 *)
and textentry = string * string * onhist option * onkey * ondone * cancelonempty
and onkey = string -> int -> te
and ondone = string -> unit
and histcancel = unit -> unit
and onhist = ((histcmd -> string) * histcancel)
and histcmd = HCnext | HCprev | HCfirst | HClast
and cancelonempty = bool
and te =
| TEstop
| TEdone of string
| TEcont of string
| TEswitch of textentry
;;
type 'a circbuf =
{ store : 'a array
; mutable rc : int
; mutable wc : int
; mutable len : int
}
;;
type state =
{ mutable ss : Unix.file_descr
; mutable wsfd : Unix.file_descr
; mutable stderr : Unix.file_descr
; mutable errmsgs : Buffer.t
; mutable newerrmsgs : bool
; mutable w : int
; mutable x : x
; mutable y : y
; mutable anchor : anchor
; mutable ranchors : (string * string * anchor * string) list
; mutable maxy : int
; mutable layout : page list
; pagemap : (pagemapkey, opaque) Hashtbl.t
; tilemap : (tilemapkey, tile) Hashtbl.t
; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
; mutable pdims : (pageno * width * height * leftx) list
; mutable pagecount : int
; mutable currently : currently
; mutable mstate : mstate
; mutable searchpattern : string
; mutable rects : (pageno * rectcolor * rect) list
; mutable rects1 : (pageno * rectcolor * rect) list
; prects : (pageno, float array) Hashtbl.t
; mutable text : string
; mutable winstate : Wsi.winstate list
; mutable mode : mode
; mutable uioh : uioh
; mutable outlines : outline array
; mutable bookmarks : outline list
; mutable path : string
; mutable password : string
; mutable nameddest : string
; mutable geomcmds : (string * ((string * (unit -> unit)) list))
; mutable memused : memsize
; mutable gen : gen
; mutable throttle : (page list * int * float) option
; mutable autoscroll : int option
; mutable ghyll : (int option -> unit)
; mutable help : helpitem array
; mutable docinfo : (int * string) list
; mutable checkerstexid : GlTex.texture_id option
; hists : hists
; mutable prevzoom : (float * int)
; mutable progress : float
; mutable redisplay : bool
; mutable mpos : mpos
; mutable keystate : keystate
; mutable glinks : bool
; mutable prevcolumns : (columns * float) option
; mutable winw : int
; mutable winh : int
; mutable reprf : (unit -> unit)
; mutable origin : string
; mutable roam : (unit -> unit)
; mutable bzoom : bool
; mutable traw : [`float] Raw.t
; mutable vraw : [`float] Raw.t
}
and hists =
{ pat : string circbuf
; pag : string circbuf
; nav : anchor circbuf
; sel : string circbuf
}
;;
let emptyanchor = (0, 0.0, 0.0);;
let emptykeyhash = Hashtbl.create 0;;
let noghyll _ = ();;
let noreprf () = ();;
let noroam () = ();;
let nouioh : uioh = object (self)
method display = ()
method key _ _ = self
method multiclick _ _ _ _ = self
method button _ _ _ _ _ = self
method motion _ _ = self
method pmotion _ _ = self
method infochanged _ = ()
method scrollpw = (0, nan, nan)
method scrollph = (0, nan, nan)
method modehash = emptykeyhash
method eformsgs = false
method alwaysscrolly = false
end;;
let platform_to_string = function
| Punknown -> "unknown"
| Plinux -> "Linux"
| Posx -> "OSX"
| Psun -> "Sun"
| Pbsd -> "BSD"
| Pcygwin -> "Cygwin"
;;
let version () =
Printf.sprintf "llpp version %s, fitz %s, ocaml %s/%d bit"
Help.version (fz_version ()) Sys.ocaml_version Sys.word_size
;;
let defconf =
{ scrollbw = 7
; scrollh = 12
; scrollb = scrollbhv lor scrollbvv
; icase = true
; preload = true
; pagebias = 0
; verbose = false
; debug = false
; scrollstep = 24
; hscrollstep = 24
; maxhfit = true
; crophack = false
; autoscrollstep = 2
; maxwait = None
; hlinks = false
; underinfo = false
; interpagespace = 2
; zoom = 1.0
; presentation = false
; angle = 0
; cwinw = 900
; cwinh = 900
; savebmarks = true
; fitmodel = FitProportional
; trimmargins = false
; trimfuzz = (0,0,0,0)
; memlimit = 32 lsl 20
; texcount = 256
; sliceheight = 24
; thumbw = 76
; jumpback = true
; bgcolor = (0.5, 0.5, 0.5)
; bedefault = false
; tilew = 2048
; tileh = 2048
; mustoresize = 256 lsl 20
; checkers = true
; aalevel = 8
; urilauncher =
(match platform with
| Plinux | Psun | Pbsd -> "xdg-open \"%s\""
| Posx -> "open \"%s\""
| Pcygwin -> "cygstart \"%s\""
| Punknown -> "echo %s")
; pathlauncher = "lp \"%s\""
; selcmd =
(match platform with
| Plinux | Pbsd | Psun -> "xsel -i"
| Posx -> "pbcopy"
| Pcygwin -> "wsel"
| Punknown -> "cat")
; paxcmd = "cat"
; passcmd = E.s
; savecmd = E.s
; colorspace = Rgb
; invert = false
; colorscale = 1.0
; ghyllscroll = None
; columns = Csingle [||]
; beyecolumns = None
; updatecurs = false
; hfsize = 12
; pgscale = 1.0
; usepbo = false
; wheelbypage = false
; stcmd = "echo SyncTex"
; riani = false
; pax = None
; paxmark = Mark_word
; leftscroll = false
; title = E.s
; lastvisit = 0.0
; annotinline = true
; coarseprespos = false
; keyhashes =
let mk n = (n, Hashtbl.create 1) in
[ mk "global"
; mk "info"
; mk "help"
; mk "outline"
; mk "listview"
; mk "birdseye"
; mk "textentry"
; mk "links"
; mk "view"
]
}
;;
let conf = { defconf with angle = defconf.angle };;
let gotourl url =
let command = Str.global_replace percentsre url conf.urilauncher in
try ignore @@ spawn command []
with exn -> dolog "failed to execute `%s': %s" command @@ exntos exn
;;
let gotouri uri =
if emptystr conf.urilauncher
then dolog "%s" uri
else (
let url = geturl uri in
if emptystr url
then dolog "obtained empty url from uri %S" uri
else gotourl url
);
;;
let makehelp () =
let strings =
version ()
:: "(searching in this text works just by typing (i.e. no initial '/'))"
:: E.s :: Help.keys
in
Array.of_list (
List.map (fun s ->
let url = geturl s in
if nonemptystr url
then (s, 0, Action (fun uioh -> gotourl url; uioh))
else (s, 0, Noaction)
) strings);
;;
let cbnew n v =
{ store = Array.make n v
; rc = 0
; wc = 0
; len = 0
}
;;
let cbcap b = Array.length b.store;;
let cbput b v =
let cap = cbcap b in
b.store.(b.wc) <- v;
b.wc <- (b.wc + 1) mod cap;
b.rc <- b.wc;
b.len <- min (b.len + 1) cap;
;;
let cbempty b = b.len = 0;;
let cbgetg b circular dir =
if cbempty b
then b.store.(0)
else
let rc = b.rc + dir in
let rc =
if circular
then (
if rc = -1
then b.len-1
else (
if rc >= b.len
then 0
else rc
)
)
else bound rc 0 (b.len-1)
in
b.rc <- rc;
b.store.(rc);
;;
let cbget b = cbgetg b false;;
let cbgetc b = cbgetg b true;;
let state =
{ ss = Unix.stdin
; wsfd = Unix.stdin
; stderr = Unix.stderr
; errmsgs = Buffer.create 0
; newerrmsgs = false
; x = 0
; y = 0
; w = 0
; anchor = emptyanchor
; ranchors = []
; layout = []
; maxy = max_int
; tilelru = Queue.create ()
; pagemap = Hashtbl.create 10
; tilemap = Hashtbl.create 10
; pdims = []
; pagecount = 0
; currently = Idle
; mstate = Mnone
; rects = []
; rects1 = []
; prects = Hashtbl.create 1
; text = E.s
; mode = View
; winstate = []
; searchpattern = E.s
; outlines = E.a
; bookmarks = []
; path = E.s
; password = E.s
; nameddest = E.s
; geomcmds = E.s, []
; hists =
{ nav = cbnew 10 emptyanchor
; pat = cbnew 10 E.s
; pag = cbnew 10 E.s
; sel = cbnew 10 E.s
}
; memused = 0
; gen = 0
; throttle = None
; autoscroll = None
; ghyll = noghyll
; help = makehelp ()
; docinfo = []
; checkerstexid = None
; prevzoom = (1.0, 0)
; progress = -1.0
; uioh = nouioh
; redisplay = true
; mpos = (-1, -1)
; keystate = KSnone
; glinks = false
; prevcolumns = None
; winw = -1
; winh = -1
; reprf = noreprf
; origin = E.s
; roam = noroam
; bzoom = false
; traw = Raw.create_static `float ~len:8
; vraw = Raw.create_static `float ~len:8
}
;;
let copykeyhashes c =
List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
;;
let calcips h =
let d = state.winh - h in
max conf.interpagespace ((d + 1) / 2)
;;
let rowyh (c, coverA, coverB) b n =
if c = 1 || (n < coverA || n >= state.pagecount - coverB)
then
let _, _, vy, (_, _, h, _) = b.(n) in
(vy, h)
else
let n' = n - coverA in
let d = n' mod c in
let s = n - d in
let e = min state.pagecount (s + c) in
let rec find m miny maxh = if m = e then miny, maxh else
let _, _, y, (_, _, h, _) = b.(m) in
let miny = min miny y in
let maxh = max maxh h in
find (m+1) miny maxh
in find s max_int 0
;;
let page_of_y y =
let ((c, coverA, coverB) as cl), b =
match conf.columns with
| Csingle b -> (1, 0, 0), b
| Cmulti (c, b) -> c, b
| Csplit (_, b) -> (1, 0, 0), b
in
if Array.length b = 0
then -1
else
let rec bsearch nmin nmax =
if nmin > nmax
then bound nmin 0 (state.pagecount-1)
else
let n = (nmax + nmin) / 2 in
let vy, h = rowyh cl b n in
let y0, y1 =
if conf.presentation
then
let ips = calcips h in
let y0 = vy - ips in
let y1 = vy + h + ips in
y0, y1
else (
if n = 0
then 0, vy + h + conf.interpagespace
else
let y0 = vy - conf.interpagespace in
y0, y0 + h + conf.interpagespace
)
in
if y >= y0 && y < y1
then (
if c = 1
then n
else (
if n > coverA
then
if n < state.pagecount - coverB
then ((n-coverA)/c)*c + coverA
else n
else n
)
)
else (
if y > y0
then bsearch (n+1) nmax
else bsearch nmin (n-1)
)
in
bsearch 0 (state.pagecount-1);
;;
let calcheight () =
match conf.columns with
| Cmulti ((_, _, _) as cl, b) ->
if Array.length b > 0
then
let y, h = rowyh cl b (Array.length b - 1) in
y + h + (if conf.presentation then calcips h else 0)
else 0
| Csingle b ->
if Array.length b > 0
then
let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
y + h + (if conf.presentation then calcips h else 0)
else 0
| Csplit (_, b) ->
if Array.length b > 0
then
let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
y + h
else 0
;;
let getpageywh pageno =
let pageno = bound pageno 0 (state.pagecount-1) in
match conf.columns with
| Csingle b ->
if Array.length b = 0
then 0, 0, 0
else
let (_, _, y, (_, w, h, _)) = b.(pageno) in
let y =
if conf.presentation
then y - calcips h
else y
in
y, w, h
| Cmulti (cl, b) ->
if Array.length b = 0
then 0, 0, 0
else
let y, h = rowyh cl b pageno in
let (_, _, _, (_, w, _, _)) = b.(pageno) in
let y =
if conf.presentation
then y - calcips h
else y
in
y, w, h
| Csplit (c, b) ->
if Array.length b = 0
then 0, 0, 0
else
let n = pageno*c in
let (_, _, y, (_, w, h, _)) = b.(n) in
y, w / c, h
;;
let getpageyh pageno =
let y,_,h = getpageywh pageno in
y, h;
;;
let getpagedim pageno =
let rec f ppdim l =
match l with
| (n, _, _, _) as pdim :: rest ->
if n >= pageno
then (if n = pageno then pdim else ppdim)
else f pdim rest
| [] -> ppdim
in
f (-1, -1, -1, -1) state.pdims
;;
let getpdimno pageno =
let rec f p l =
let np = succ p in
match l with
| (n, _, _, _) :: rest ->
if n >= pageno
then (if n = pageno then np else p)
else f np rest
| [] -> p
in
f ~-1 state.pdims
;;
let getpagey pageno = fst (getpageyh pageno);;
let getanchor1 l =
let top =
let coloff = l.pagecol * l.pageh in
float (l.pagey + coloff) /. float l.pageh
in
let dtop =
if l.pagedispy = 0
then
0.0
else (
if conf.presentation
then float l.pagedispy /. float (calcips l.pageh)
else float l.pagedispy /. float conf.interpagespace
)
in
(l.pageno, top, dtop)
;;
let getanchor () =
match state.layout with
| l :: _ -> getanchor1 l
| [] ->
let n = page_of_y state.y in
if n = -1
then state.anchor
else
let y, h = getpageyh n in
let dy = y - state.y in
let dtop =
if conf.presentation
then
let ips = calcips h in
float (dy + ips) /. float ips
else
float dy /. float conf.interpagespace
in
(n, 0.0, dtop)
;;
let fontpath = ref E.s;;
type historder = [ `lastvisit | `title | `path | `file ];;
module KeyMap =
Map.Make (struct type t = (int * int) let compare = compare end);;
let unentS s =
let l = String.length s in
let b = Buffer.create l in
Parser.unent b s 0 l;
Buffer.contents b;
;;
let home =
try Sys.getenv "HOME"
with exn ->
dolog "cannot determine home directory location: %s" @@ exntos exn;
E.s
;;
let modifier_of_string = function
| "alt" -> Wsi.altmask
| "shift" -> Wsi.shiftmask
| "ctrl" | "control" -> Wsi.ctrlmask
| "meta" -> Wsi.metamask
| _ -> 0
;;
let keys_of_string s =
let key_of_string r s =
let elems = Str.full_split r s in
let f n k m =
let g s =
let m1 = modifier_of_string s in
if m1 = 0
then (Wsi.namekey s, m)
else (k, m lor m1)
in function
| Str.Delim s when n land 1 = 0 -> g s
| Str.Text s -> g s
| Str.Delim _ -> (k, m)
in
let rec loop n k m = function
| [] -> (k, m)
| x :: xs ->
let k, m = f n k m x in
loop (n+1) k m xs
in
loop 0 0 0 elems
in
let elems = Str.split whitere s in
List.map (key_of_string (Str.regexp "-")) elems
;;
let config_of c attrs =
let apply c k v =
try
match k with
| "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
| "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
| "case-insensitive-search" -> { c with icase = bool_of_string v }
| "preload" -> { c with preload = bool_of_string v }
| "page-bias" -> { c with pagebias = int_of_string v }
| "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
| "horizontal-scroll-step" ->
{ c with hscrollstep = max (int_of_string v) 1 }
| "auto-scroll-step" ->
{ c with autoscrollstep = max 0 (int_of_string v) }
| "max-height-fit" -> { c with maxhfit = bool_of_string v }
| "crop-hack" -> { c with crophack = bool_of_string v }