Recent compilers can output dependencies themselves
[llpp.git] / config.ml
blobedb9c6ca23564e335922f835382caf33a5d0446f
1 open Utils;;
3 external fz_version : unit -> string = "ml_fz_version";;
5 type fontstate =
6 { mutable fontsize : int
7 ; mutable wwidth : float
8 ; mutable maxrows : int
12 let fstate =
13 { fontsize = 20 * Wsi.fontsizefactor ()
14 ; wwidth = nan
15 ; maxrows = -1
19 let scrollbvv = 1;;
20 let scrollbhv = 2;;
21 let fastghyllscroll = (5,1,2);;
22 let neatghyllscroll = (10,1,9);;
24 let irect_of_string s =
25 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
28 let irect_to_string (x0,y0,x1,y1) =
29 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
32 let ghyllscroll_of_string s =
33 match s with
34 | "fast" -> Some fastghyllscroll
35 | "neat" -> Some (10,1,9)
36 | "" | "none" -> None
37 | _ ->
38 let (n,a,b) as nab =
39 Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b) in
40 if n <= a || n <= b || a >= b
41 then error "N(%d),A(%d),B(%d) (N <= A, A < B, N <= B)" n a b;
42 Some nab
45 let ghyllscroll_to_string ((n, a, b) as nab) =
46 if nab = fastghyllscroll then "fast"
47 else if nab = neatghyllscroll then "neat"
48 else Printf.sprintf "%d,%d,%d" n a b;
51 let multicolumns_to_string (n, a, b) =
52 if a = 0 && b = 0
53 then Printf.sprintf "%d" n
54 else Printf.sprintf "%d,%d,%d" n a b;
57 let multicolumns_of_string s =
58 try
59 (int_of_string s, 0, 0)
60 with _ ->
61 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
62 if a > 1 || b > 1
63 then failwith "subtly broken";
64 (n, a, b)
68 type keymap =
69 | KMinsrt of key
70 | KMinsrl of key list
71 | KMmulti of key list * key list
72 and key = int * int
73 and keyhash = (key, keymap) Hashtbl.t
74 and keystate =
75 | KSnone
76 | KSinto of (key list * key list)
77 and interpagespace = int
78 and multicolumns = multicol * pagegeom
79 and singlecolumn = pagegeom
80 and splitcolumns = columncount * pagegeom
81 and pagegeom = (pdimno * x * y * (pageno * width * height * leftx)) array
82 and multicol = columncount * covercount * covercount
83 and pdimno = int
84 and columncount = int
85 and covercount = int
86 and fitmodel = | FitWidth | FitProportional | FitPage
87 and trimmargins = bool
88 and irect = (int * int * int * int)
89 and memsize = int
90 and texcount = int
91 and sliceheight = int
92 and angle = int
93 and initparams =
94 (angle * fitmodel * trimparams * texcount * sliceheight * memsize
95 * colorspace * fontpath * trimcachepath * haspbo)
96 and width = int
97 and height = int
98 and leftx = int
99 and opaque = Opaque.t
100 and rectcolor = (float * float * float * float)
101 and pixmapsize = int
102 and gen = int
103 and top = float
104 and dtop = float
105 and fontpath = string
106 and trimcachepath = string
107 and css = string
108 and aalevel = int
109 and trimparams = (trimmargins * irect)
110 and colorspace = | Rgb | Bgr | Gray
111 and haspbo = bool
112 and usefontconfig = bool
113 and usedoccss = bool
114 and uri = string
115 and caption = string
116 and x = int
117 and y = int
118 and tilex = int
119 and tiley = int
120 and tileparams = (x * y * width * height * tilex * tiley)
121 and under =
122 | Unone
123 | Ulinkuri of string
124 | Utext of facename
125 | Uannotation of (opaque * slinkindex)
126 and slinkindex = int
127 and facename = string
128 and launchcommand = string
129 and filename = string
130 and pageno = int
131 and linkno = int
132 and destname = string
133 and mark =
134 | Mark_page
135 | Mark_block
136 | Mark_line
137 | Mark_word
138 and link =
139 | Lnotfound
140 | Lfound of int
141 and linkdir =
142 | LDfirst
143 | LDlast
144 | LDfirstvisible of (int * int * int)
145 | LDleft of int
146 | LDright of int
147 | LDdown of int
148 | LDup of int
149 and pagewithlinks =
150 | Pwlnotfound
151 | Pwl of int
152 and scrollb = int
153 and anchor = pageno * top * dtop
154 and rect = float * float * float * float * float * float * float * float
155 and infochange = | Memused | Docinfo | Pdim
158 class type uioh =
159 object
160 method display : unit
161 method key : int -> int -> uioh
162 method button : int -> bool -> int -> int -> int -> uioh
163 method multiclick : int -> int -> int -> int -> uioh
164 method motion : int -> int -> uioh
165 method pmotion : int -> int -> uioh
166 method infochanged : infochange -> unit
167 method scrollpw : (int * float * float)
168 method scrollph : (int * float * float)
169 method modehash : keyhash
170 method eformsgs : bool
171 method alwaysscrolly : bool
172 method scroll : int -> int -> uioh
173 method zoom : float -> int -> int -> unit
174 end;;
176 module type TextEnumType =
178 type t
179 val name : string
180 val names : string array
181 end;;
183 module TextEnumMake (Ten : TextEnumType) =
184 struct
185 let names = Ten.names;;
186 let to_int (t : Ten.t) = Obj.magic t;;
187 let to_string t = names.(to_int t);;
188 let of_int n : Ten.t = Obj.magic n;;
189 let of_string s =
190 let rec find i =
191 if i = Array.length names
192 then failwith ("invalid " ^ Ten.name ^ ": " ^ s)
193 else (
194 if Ten.names.(i) = s
195 then of_int i
196 else find (i+1)
198 in find 0;;
199 end;;
201 module CSTE = TextEnumMake (struct
202 type t = colorspace;;
203 let name = "colorspace";;
204 let names = [|"rgb"; "bgr"; "gray"|];;
205 end);;
207 module MTE = TextEnumMake (struct
208 type t = mark;;
209 let name = "mark";;
210 let names = [|"page"; "block"; "line"; "word"|];;
211 end);;
213 module FMTE = TextEnumMake (struct
214 type t = fitmodel;;
215 let name = "fitmodel";;
216 let names = [|"width"; "proportional"; "page"|];;
217 end);;
219 type conf =
220 { mutable scrollbw : int
221 ; mutable scrollh : int
222 ; mutable scrollb : scrollb
223 ; mutable icase : bool
224 ; mutable preload : bool
225 ; mutable pagebias : int
226 ; mutable verbose : bool
227 ; mutable debug : bool
228 ; mutable scrollstep : int
229 ; mutable hscrollstep : int
230 ; mutable maxhfit : bool
231 ; mutable crophack : bool
232 ; mutable autoscrollstep : int
233 ; mutable maxwait : float option
234 ; mutable hlinks : bool
235 ; mutable underinfo : bool
236 ; mutable interpagespace : interpagespace
237 ; mutable zoom : float
238 ; mutable presentation : bool
239 ; mutable angle : angle
240 ; mutable cwinw : int
241 ; mutable cwinh : int
242 ; mutable savebmarks : bool
243 ; mutable fitmodel : fitmodel
244 ; mutable trimmargins : trimmargins
245 ; mutable trimfuzz : irect
246 ; mutable memlimit : memsize
247 ; mutable texcount : texcount
248 ; mutable sliceheight : sliceheight
249 ; mutable thumbw : width
250 ; mutable jumpback : bool
251 ; mutable bgcolor : rgb
252 ; mutable sbarcolor : rgba
253 ; mutable sbarhndlcolor : rgba
254 ; mutable bedefault : bool
255 ; mutable tilew : int
256 ; mutable tileh : int
257 ; mutable mustoresize : memsize
258 ; mutable checkers : bool
259 ; mutable aalevel : int
260 ; mutable urilauncher : string
261 ; mutable pathlauncher : string
262 ; mutable colorspace : colorspace
263 ; mutable invert : bool
264 ; mutable colorscale : float
265 ; mutable ghyllscroll : (int * int * int) option
266 ; mutable columns : columns
267 ; mutable beyecolumns : columncount option
268 ; mutable selcmd : string
269 ; mutable paxcmd : string
270 ; mutable passcmd : string
271 ; mutable savecmd : string
272 ; mutable updatecurs : bool
273 ; mutable keyhashes : (string * keyhash) list
274 ; mutable hfsize : int
275 ; mutable pgscale : float
276 ; mutable usepbo : bool
277 ; mutable wheelbypage : bool
278 ; mutable stcmd : string
279 ; mutable riani : bool
280 ; mutable pax : float option
281 ; mutable paxmark : mark
282 ; mutable leftscroll : bool
283 ; mutable title : string
284 ; mutable lastvisit : float
285 ; mutable annotinline : bool
286 ; mutable coarseprespos : bool
287 ; mutable css : css
288 ; mutable usedoccss : usedoccss
290 and columns =
291 | Csingle of singlecolumn
292 | Cmulti of multicolumns
293 | Csplit of splitcolumns
294 and outlinekind =
295 | Onone
296 | Oanchor of anchor
297 | Ouri of uri
298 | Olaunch of launchcommand
299 | Oremote of (filename * pageno)
300 | Oremotedest of (filename * destname)
301 | Ohistory of (filename * conf * outline list * x * anchor * filename)
302 and outline = (caption * outlinelevel * outlinekind)
303 and outlinelevel = int
304 and rgb = (float * float * float)
305 and rgba = (float * float * float * float)
308 type page =
309 { pageno : int
310 ; pagedimno : int
311 ; pagew : int
312 ; pageh : int
313 ; pagex : int
314 ; pagey : int
315 ; pagevw : int
316 ; pagevh : int
317 ; pagedispx : int
318 ; pagedispy : int
319 ; pagecol : int
323 type tile = opaque * pixmapsize * elapsed
324 and elapsed = float;;
325 type pagemapkey = pageno * gen;;
326 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
327 and row = int
328 and col = int
329 and currently =
330 | Idle
331 | Loading of (page * gen)
332 | Tiling of (
333 page * opaque * colorspace * angle * gen * col * row * width * height
335 | Outlining of outline list
338 type mpos = int * int
339 and mstate =
340 | Msel of (mpos * mpos)
341 | Mpan of mpos
342 | Mscrolly | Mscrollx
343 | Mzoom of (buttonno * step * mpos)
344 | Mzoomrect of (mpos * mpos)
345 | Mnone
346 and buttonno = int
347 and step = int
350 type mode =
351 | Birdseye of (conf * leftx * pageno * pageno * anchor)
352 | Textentry of (textentry * onleave)
353 | View
354 | LinkNav of linktarget
355 and onleave = leavetextentrystatus -> unit
356 and leavetextentrystatus = | Cancel | Confirm
357 and helpitem = string * int * action
358 and action =
359 | Noaction
360 | Action of (uioh -> uioh)
361 and linktarget =
362 | Ltexact of (pageno * direction)
363 | Ltgendir of direction
364 | Ltnotready of (pageno * direction)
365 and direction = int (* -1, 0, 1 *)
366 and textentry = string * string * onhist option
367 * onkey * ondone * cancelonempty
368 and onkey = string -> Keys.t -> te
369 and ondone = string -> unit
370 and histcancel = unit -> unit
371 and onhist = ((histcmd -> string) * histcancel)
372 and histcmd = HCnext | HCprev | HCfirst | HClast
373 and cancelonempty = bool
374 and te =
375 | TEstop
376 | TEdone of string
377 | TEcont of string
378 | TEswitch of textentry
381 type 'a circbuf =
382 { store : 'a array
383 ; mutable rc : int
384 ; mutable wc : int
385 ; mutable len : int
389 type state =
390 { mutable ss : Unix.file_descr
391 ; mutable wsfd : Unix.file_descr
392 ; mutable stderr : Unix.file_descr
393 ; mutable errmsgs : Buffer.t
394 ; mutable newerrmsgs : bool
395 ; mutable w : int
396 ; mutable x : x
397 ; mutable y : y
398 ; mutable anchor : anchor
399 ; mutable ranchors : (string * string * anchor * string) list
400 ; mutable maxy : int
401 ; mutable layout : page list
402 ; pagemap : (pagemapkey, opaque) Hashtbl.t
403 ; tilemap : (tilemapkey, tile) Hashtbl.t
404 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
405 ; mutable pdims : (pageno * width * height * leftx) list
406 ; mutable pagecount : int
407 ; mutable currently : currently
408 ; mutable mstate : mstate
409 ; mutable searchpattern : string
410 ; mutable rects : (pageno * rectcolor * rect) list
411 ; mutable rects1 : (pageno * rectcolor * rect) list
412 ; prects : (pageno, float array) Hashtbl.t
413 ; mutable text : string
414 ; mutable winstate : Wsi.winstate list
415 ; mutable mode : mode
416 ; mutable uioh : uioh
417 ; mutable outlines : outline array
418 ; mutable bookmarks : outline list
419 ; mutable path : string
420 ; mutable password : string
421 ; mutable nameddest : string
422 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
423 ; mutable memused : memsize
424 ; mutable gen : gen
425 ; mutable throttle : (page list * int * float) option
426 ; mutable autoscroll : int option
427 ; mutable ghyll : (int option -> unit)
428 ; mutable help : helpitem array
429 ; mutable docinfo : (int * string) list
430 ; mutable checkerstexid : GlTex.texture_id option
431 ; hists : hists
432 ; mutable prevzoom : (float * int)
433 ; mutable progress : float
434 ; mutable redisplay : bool
435 ; mutable mpos : mpos
436 ; mutable keystate : keystate
437 ; mutable glinks : bool
438 ; mutable prevcolumns : (columns * float) option
439 ; mutable winw : int
440 ; mutable winh : int
441 ; mutable reprf : (unit -> unit)
442 ; mutable origin : string
443 ; mutable roam : (unit -> unit)
444 ; mutable bzoom : bool
445 ; mutable traw : [`float] Raw.t
446 ; mutable vraw : [`float] Raw.t
447 ; mutable lnava : (pageno * linkno) option
448 ; mutable slideshow : int
450 and hists =
451 { pat : string circbuf
452 ; pag : string circbuf
453 ; nav : anchor circbuf
454 ; sel : string circbuf
458 let emptyanchor = (0, 0.0, 0.0);;
459 let emptykeyhash = Hashtbl.create 0;;
460 let noghyll _ = ();;
461 let noreprf () = ();;
462 let noroam () = ();;
464 let nouioh : uioh = object (self)
465 method display = ()
466 method key _ _ = self
467 method multiclick _ _ _ _ = self
468 method button _ _ _ _ _ = self
469 method motion _ _ = self
470 method pmotion _ _ = self
471 method infochanged _ = ()
472 method scrollpw = (0, nan, nan)
473 method scrollph = (0, nan, nan)
474 method modehash = emptykeyhash
475 method eformsgs = false
476 method alwaysscrolly = false
477 method scroll _ _ = self
478 method zoom _ _ _ = ()
479 end;;
481 let platform_to_string = function
482 | Punknown -> "unknown"
483 | Plinux -> "Linux"
484 | Posx -> "OSX"
485 | Psun -> "Sun"
486 | Pbsd -> "BSD"
487 | Pcygwin -> "Cygwin"
490 let version () =
491 Printf.sprintf "llpp version %s, fitz %s, ocaml %s/%d bit"
492 Help.version (fz_version ()) Sys.ocaml_version Sys.word_size
495 let defconf =
496 { scrollbw = 7
497 ; scrollh = 12
498 ; scrollb = scrollbhv lor scrollbvv
499 ; icase = true
500 ; preload = true
501 ; pagebias = 0
502 ; verbose = false
503 ; debug = false
504 ; scrollstep = 24
505 ; hscrollstep = 24
506 ; maxhfit = true
507 ; crophack = false
508 ; autoscrollstep = 2
509 ; maxwait = None
510 ; hlinks = false
511 ; underinfo = false
512 ; interpagespace = 2
513 ; zoom = 1.0
514 ; presentation = false
515 ; angle = 0
516 ; cwinw = 1200
517 ; cwinh = 1000
518 ; savebmarks = true
519 ; fitmodel = FitProportional
520 ; trimmargins = false
521 ; trimfuzz = (0,0,0,0)
522 ; memlimit = 32 lsl 20
523 ; texcount = 256
524 ; sliceheight = 24
525 ; thumbw = 76
526 ; jumpback = true
527 ; bgcolor = (0.5, 0.5, 0.5)
528 ; sbarcolor = (0.64, 0.64, 0.64, 0.7)
529 ; sbarhndlcolor = (0.0, 0.0, 0.0, 0.7)
530 ; bedefault = false
531 ; tilew = 2048
532 ; tileh = 2048
533 ; mustoresize = 256 lsl 20
534 ; checkers = true
535 ; aalevel = 8
536 ; urilauncher =
537 (match platform with
538 | Plinux | Psun | Pbsd -> "xdg-open \"%s\""
539 | Posx -> "open \"%s\""
540 | Pcygwin -> "cygstart \"%s\""
541 | Punknown -> "echo %s")
542 ; pathlauncher = "lp \"%s\""
543 ; selcmd =
544 (match platform with
545 | Plinux | Pbsd | Psun -> "xsel -i"
546 | Posx -> "pbcopy"
547 | Pcygwin -> "wsel"
548 | Punknown -> "cat")
549 ; paxcmd = "cat"
550 ; passcmd = E.s
551 ; savecmd = E.s
552 ; colorspace = Rgb
553 ; invert = false
554 ; colorscale = 1.0
555 ; ghyllscroll = None
556 ; columns = Csingle [||]
557 ; beyecolumns = None
558 ; updatecurs = true
559 ; hfsize = 12 * Wsi.fontsizefactor ()
560 ; pgscale = 1.0
561 ; usepbo = false
562 ; wheelbypage = false
563 ; stcmd = "echo SyncTex"
564 ; riani = false
565 ; pax = None
566 ; paxmark = Mark_word
567 ; leftscroll = false
568 ; title = E.s
569 ; lastvisit = 0.0
570 ; annotinline = true
571 ; coarseprespos = false
572 ; css = E.s
573 ; usedoccss = true
574 ; keyhashes =
575 let mk n = (n, Hashtbl.create 1) in
576 [ mk "global"
577 ; mk "info"
578 ; mk "help"
579 ; mk "outline"
580 ; mk "listview"
581 ; mk "birdseye"
582 ; mk "textentry"
583 ; mk "links"
584 ; mk "view"
589 let conf = { defconf with angle = defconf.angle };;
591 let gotourl url =
592 let command = Str.global_replace percentsre url conf.urilauncher in
593 try ignore @@ spawn command []
594 with exn -> dolog "failed to execute `%s': %s" command @@ exntos exn
597 let gotouri uri =
598 if emptystr conf.urilauncher
599 then dolog "%s" uri
600 else
601 match geturl uri with
602 | "" -> dolog "obtained empty url from uri %S" uri
603 | url -> gotourl url
606 let makehelp () =
607 let strings =
608 version ()
609 :: "(searching in this text works just by typing (i.e. no initial '/'))"
610 :: E.s :: Help.keys
612 List.map (fun s ->
613 match geturl s with
614 | "" -> (s, 0, Noaction)
615 | url -> (s, 0, Action (fun uioh -> gotourl url; uioh))
616 ) strings;
619 let cbnew n v =
620 { store = Array.make n v
621 ; rc = 0
622 ; wc = 0
623 ; len = 0
627 let cbcap b = Array.length b.store;;
629 let cbput b v =
630 let cap = cbcap b in
631 b.store.(b.wc) <- v;
632 b.wc <- (b.wc + 1) mod cap;
633 b.rc <- b.wc;
634 b.len <- min (b.len + 1) cap;
637 let cbempty b = b.len = 0;;
639 let cbgetg b circular dir =
640 if cbempty b
641 then b.store.(0)
642 else
643 let rc = b.rc + dir in
644 let rc =
645 if circular
646 then (
647 if rc = -1
648 then b.len-1
649 else (
650 if rc >= b.len
651 then 0
652 else rc
655 else bound rc 0 (b.len-1)
657 b.rc <- rc;
658 b.store.(rc);
661 let cbget b = cbgetg b false;;
662 let cbgetc b = cbgetg b true;;
664 let state =
665 { ss = Unix.stdin
666 ; wsfd = Unix.stdin
667 ; stderr = Unix.stderr
668 ; errmsgs = Buffer.create 0
669 ; newerrmsgs = false
670 ; x = 0
671 ; y = 0
672 ; w = 0
673 ; anchor = emptyanchor
674 ; ranchors = []
675 ; layout = []
676 ; maxy = max_int
677 ; tilelru = Queue.create ()
678 ; pagemap = Hashtbl.create 10
679 ; tilemap = Hashtbl.create 10
680 ; pdims = []
681 ; pagecount = 0
682 ; currently = Idle
683 ; mstate = Mnone
684 ; rects = []
685 ; rects1 = []
686 ; prects = Hashtbl.create 1
687 ; text = E.s
688 ; mode = View
689 ; winstate = []
690 ; searchpattern = E.s
691 ; outlines = E.a
692 ; bookmarks = []
693 ; path = E.s
694 ; password = E.s
695 ; nameddest = E.s
696 ; geomcmds = E.s, []
697 ; hists =
698 { nav = cbnew 10 emptyanchor
699 ; pat = cbnew 10 E.s
700 ; pag = cbnew 10 E.s
701 ; sel = cbnew 10 E.s
703 ; memused = 0
704 ; gen = 0
705 ; throttle = None
706 ; autoscroll = None
707 ; ghyll = noghyll
708 ; help = E.a
709 ; docinfo = []
710 ; checkerstexid = None
711 ; prevzoom = (1.0, 0)
712 ; progress = -1.0
713 ; uioh = nouioh
714 ; redisplay = true
715 ; mpos = (-1, -1)
716 ; keystate = KSnone
717 ; glinks = false
718 ; prevcolumns = None
719 ; winw = -1
720 ; winh = -1
721 ; reprf = noreprf
722 ; origin = E.s
723 ; roam = noroam
724 ; bzoom = false
725 ; traw = Raw.create_static `float ~len:8
726 ; vraw = Raw.create_static `float ~len:8
727 ; lnava = None
728 ; slideshow = 0
732 let copykeyhashes c =
733 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
736 let calcips h =
737 let d = state.winh - h in
738 max conf.interpagespace ((d + 1) / 2)
741 let rowyh (c, coverA, coverB) b n =
742 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
743 then
744 let _, _, vy, (_, _, h, _) = b.(n) in
745 (vy, h)
746 else
747 let n' = n - coverA in
748 let d = n' mod c in
749 let s = n - d in
750 let e = min state.pagecount (s + c) in
751 let rec find m miny maxh = if m = e then miny, maxh else
752 let _, _, y, (_, _, h, _) = b.(m) in
753 let miny = min miny y in
754 let maxh = max maxh h in
755 find (m+1) miny maxh
756 in find s max_int 0
759 let page_of_y y =
760 let ((c, coverA, coverB) as cl), b =
761 match conf.columns with
762 | Csingle b -> (1, 0, 0), b
763 | Cmulti (c, b) -> c, b
764 | Csplit (_, b) -> (1, 0, 0), b
766 if Array.length b = 0
767 then -1
768 else
769 let rec bsearch nmin nmax =
770 if nmin > nmax
771 then bound nmin 0 (state.pagecount-1)
772 else
773 let n = (nmax + nmin) / 2 in
774 let vy, h = rowyh cl b n in
775 let y0, y1 =
776 if conf.presentation
777 then
778 let ips = calcips h in
779 let y0 = vy - ips in
780 let y1 = vy + h + ips in
781 y0, y1
782 else (
783 if n = 0
784 then 0, vy + h + conf.interpagespace
785 else
786 let y0 = vy - conf.interpagespace in
787 y0, y0 + h + conf.interpagespace
790 if y >= y0 && y < y1
791 then (
792 if c = 1
793 then n
794 else (
795 if n > coverA
796 then
797 if n < state.pagecount - coverB
798 then ((n-coverA)/c)*c + coverA
799 else n
800 else n
803 else (
804 if y > y0
805 then bsearch (n+1) nmax
806 else bsearch nmin (n-1)
809 bsearch 0 (state.pagecount-1);
812 let calcheight () =
813 match conf.columns with
814 | Cmulti ((_, _, _) as cl, b) ->
815 if Array.length b > 0
816 then
817 let y, h = rowyh cl b (Array.length b - 1) in
818 y + h + (if conf.presentation then calcips h else 0)
819 else 0
820 | Csingle b ->
821 if Array.length b > 0
822 then
823 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
824 y + h + (if conf.presentation then calcips h else 0)
825 else 0
826 | Csplit (_, b) ->
827 if Array.length b > 0
828 then
829 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
830 y + h
831 else 0
834 let getpageywh pageno =
835 let pageno = bound pageno 0 (state.pagecount-1) in
836 match conf.columns with
837 | Csingle b ->
838 if Array.length b = 0
839 then 0, 0, 0
840 else
841 let (_, _, y, (_, w, h, _)) = b.(pageno) in
842 let y =
843 if conf.presentation
844 then y - calcips h
845 else y
847 y, w, h
848 | Cmulti (cl, b) ->
849 if Array.length b = 0
850 then 0, 0, 0
851 else
852 let y, h = rowyh cl b pageno in
853 let (_, _, _, (_, w, _, _)) = b.(pageno) in
854 let y =
855 if conf.presentation
856 then y - calcips h
857 else y
859 y, w, h
860 | Csplit (c, b) ->
861 if Array.length b = 0
862 then 0, 0, 0
863 else
864 let n = pageno*c in
865 let (_, _, y, (_, w, h, _)) = b.(n) in
866 y, w / c, h
869 let getpageyh pageno =
870 let y,_,h = getpageywh pageno in
871 y, h;
874 let getpagedim pageno =
875 let rec f ppdim l =
876 match l with
877 | (n, _, _, _) as pdim :: rest ->
878 if n >= pageno
879 then (if n = pageno then pdim else ppdim)
880 else f pdim rest
882 | [] -> ppdim
884 f (-1, -1, -1, -1) state.pdims
887 let getpdimno pageno =
888 let rec f p l =
889 let np = succ p in
890 match l with
891 | (n, _, _, _) :: rest ->
892 if n >= pageno
893 then (if n = pageno then np else p)
894 else f np rest
896 | [] -> p
898 f ~-1 state.pdims
901 let getpagey pageno = fst (getpageyh pageno);;
903 let getanchor1 l =
904 let top =
905 let coloff = l.pagecol * l.pageh in
906 float (l.pagey + coloff) /. float l.pageh
908 let dtop =
909 if l.pagedispy = 0
910 then
912 else (
913 if conf.presentation
914 then float l.pagedispy /. float (calcips l.pageh)
915 else float l.pagedispy /. float conf.interpagespace
918 (l.pageno, top, dtop)
921 let getanchor () =
922 match state.layout with
923 | l :: _ -> getanchor1 l
924 | [] ->
925 let n = page_of_y state.y in
926 if n = -1
927 then state.anchor
928 else
929 let y, h = getpageyh n in
930 let dy = y - state.y in
931 let dtop =
932 if conf.presentation
933 then
934 let ips = calcips h in
935 float (dy + ips) /. float ips
936 else
937 float dy /. float conf.interpagespace
939 (n, 0.0, dtop)
942 let fontpath = ref E.s;;
944 type historder = [ `lastvisit | `title | `path | `file ];;
946 module KeyMap =
947 Map.Make (struct type t = (int * int) let compare = compare end);;
949 let unentS s =
950 let l = String.length s in
951 let b = Buffer.create l in
952 Parser.unent b s 0 l;
953 Buffer.contents b;
956 let home =
957 try Sys.getenv "HOME"
958 with exn ->
959 dolog "cannot determine home directory location: %s" @@ exntos exn;
963 let modifier_of_string = function
964 | "alt" -> Wsi.altmask
965 | "shift" -> Wsi.shiftmask
966 | "ctrl" | "control" -> Wsi.ctrlmask
967 | "meta" -> Wsi.metamask
968 | _ -> 0
971 let keys_of_string s =
972 let key_of_string r s =
973 let elems = Str.full_split r s in
974 let f n k m =
975 let g s =
976 let m1 = modifier_of_string s in
977 if m1 = 0
978 then (Wsi.namekey s, m)
979 else (k, m lor m1)
980 in function
981 | Str.Delim s when n land 1 = 0 -> g s
982 | Str.Text s -> g s
983 | Str.Delim _ -> (k, m)
985 let rec loop n k m = function
986 | [] -> (k, m)
987 | x :: xs ->
988 let k, m = f n k m x in
989 loop (n+1) k m xs
991 loop 0 0 0 elems
993 let elems = Str.split whitere s in
994 List.map (key_of_string (Str.regexp "-")) elems
997 let config_of c attrs =
998 let apply c k v =
1000 match k with
1001 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
1002 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
1003 | "case-insensitive-search" -> { c with icase = bool_of_string v }
1004 | "preload" -> { c with preload = bool_of_string v }
1005 | "page-bias" -> { c with pagebias = int_of_string v }
1006 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
1007 | "horizontal-scroll-step" ->
1008 { c with hscrollstep = max (int_of_string v) 1 }
1009 | "auto-scroll-step" ->
1010 { c with autoscrollstep = max 0 (int_of_string v) }
1011 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
1012 | "crop-hack" -> { c with crophack = bool_of_string v }
1013 | "throttle" ->
1014 let mw =
1015 match String.map asciilower v with
1016 | "true" -> Some infinity
1017 | "false" -> None
1018 | f -> Some (float_of_string f)
1020 { c with maxwait = mw }
1021 | "highlight-links" -> { c with hlinks = bool_of_string v }
1022 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
1023 | "vertical-margin" ->
1024 { c with interpagespace = max 0 (int_of_string v) }
1025 | "zoom" ->
1026 let zoom = float_of_string v /. 100. in
1027 let zoom = max zoom 0.0 in
1028 { c with zoom = zoom }
1029 | "presentation" -> { c with presentation = bool_of_string v }
1030 | "rotation-angle" -> { c with angle = int_of_string v }
1031 | "width" -> { c with cwinw = max 20 (int_of_string v) }
1032 | "height" -> { c with cwinh = max 20 (int_of_string v) }
1033 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
1034 | "proportional-display" ->
1035 let fm =
1036 if bool_of_string v
1037 then FitProportional
1038 else FitWidth
1040 { c with fitmodel = fm }
1041 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
1042 | "pixmap-cache-size" ->
1043 { c with memlimit = max 2 (int_of_string_with_suffix v) }
1044 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
1045 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
1046 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
1047 | "persistent-location" -> { c with jumpback = bool_of_string v }
1048 | "background-color" -> { c with bgcolor = color_of_string v }
1049 | "scrollbar-color" -> { c with sbarcolor = rgba_of_string v }
1050 | "scrollbar-handle-color" -> { c with sbarhndlcolor = rgba_of_string v }
1051 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
1052 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
1053 | "mupdf-store-size" ->
1054 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
1055 | "checkers" -> { c with checkers = bool_of_string v }
1056 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
1057 | "trim-margins" -> { c with trimmargins = bool_of_string v }
1058 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
1059 | "uri-launcher" -> { c with urilauncher = unentS v }
1060 | "path-launcher" -> { c with pathlauncher = unentS v }
1061 | "color-space" -> { c with colorspace = CSTE.of_string v }
1062 | "invert-colors" -> { c with invert = bool_of_string v }
1063 | "brightness" -> { c with colorscale = float_of_string v }
1064 | "ghyllscroll" -> { c with ghyllscroll = ghyllscroll_of_string v }
1065 | "columns" ->
1066 let (n, _, _) as nab = multicolumns_of_string v in
1067 if n < 0
1068 then { c with columns = Csplit (-n, E.a) }
1069 else { c with columns = Cmulti (nab, E.a) }
1070 | "birds-eye-columns" ->
1071 { c with beyecolumns = Some (max (int_of_string v) 2) }
1072 | "selection-command" -> { c with selcmd = unentS v }
1073 | "synctex-command" -> { c with stcmd = unentS v }
1074 | "pax-command" -> { c with paxcmd = unentS v }
1075 | "askpass-command" -> { c with passcmd = unentS v }
1076 | "savepath-command" -> { c with savecmd = unentS v }
1077 | "update-cursor" -> { c with updatecurs = bool_of_string v }
1078 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
1079 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
1080 | "use-pbo" -> { c with usepbo = bool_of_string v }
1081 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
1082 | "horizontal-scrollbar-visible" ->
1083 let b =
1084 if bool_of_string v
1085 then c.scrollb lor scrollbhv
1086 else c.scrollb land (lnot scrollbhv)
1088 { c with scrollb = b }
1089 | "vertical-scrollbar-visible" ->
1090 let b =
1091 if bool_of_string v
1092 then c.scrollb lor scrollbvv
1093 else c.scrollb land (lnot scrollbvv)
1095 { c with scrollb = b }
1096 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
1097 | "point-and-x" ->
1098 { c with pax =
1099 if bool_of_string v
1100 then Some 0.0
1101 else None }
1102 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
1103 | "scroll-bar-on-the-left" -> { c with leftscroll = bool_of_string v }
1104 | "title" -> { c with title = unentS v }
1105 | "last-visit" -> { c with lastvisit = float_of_string v }
1106 | "edit-annotations-inline" -> { c with annotinline = bool_of_string v }
1107 | "coarse-presentation-positioning" ->
1108 { c with coarseprespos = bool_of_string v }
1109 | "use-document-css" -> { c with usedoccss = bool_of_string v }
1110 | _ -> c
1111 with exn ->
1112 dolog "error processing attribute (`%S' = `%S'): %s" k v @@ exntos exn;
1115 let rec fold c = function
1116 | [] -> c
1117 | (k, v) :: rest ->
1118 let c = apply c k v in
1119 fold c rest
1121 fold { c with keyhashes = copykeyhashes c } attrs;
1124 let fromstring f pos n v d =
1125 try f v
1126 with exn ->
1127 dolog "error processing attribute (%S=%S) at %d\n%s" n v pos @@ exntos exn;
1131 let bookmark_of attrs =
1132 let rec fold title page rely visy = function
1133 | ("title", v) :: rest -> fold v page rely visy rest
1134 | ("page", v) :: rest -> fold title v rely visy rest
1135 | ("rely", v) :: rest -> fold title page v visy rest
1136 | ("visy", v) :: rest -> fold title page rely v rest
1137 | _ :: rest -> fold title page rely visy rest
1138 | [] -> title, page, rely, visy
1140 fold "invalid" "0" "0" "0" attrs
1143 let doc_of attrs =
1144 let rec fold path page rely pan visy origin = function
1145 | ("path", v) :: rest -> fold v page rely pan visy origin rest
1146 | ("page", v) :: rest -> fold path v rely pan visy origin rest
1147 | ("rely", v) :: rest -> fold path page v pan visy origin rest
1148 | ("pan", v) :: rest -> fold path page rely v visy origin rest
1149 | ("visy", v) :: rest -> fold path page rely pan v origin rest
1150 | ("origin", v) :: rest -> fold path page rely pan visy v rest
1151 | _ :: rest -> fold path page rely pan visy origin rest
1152 | [] -> path, page, rely, pan, visy, origin
1154 fold E.s "0" "0" "0" "0" E.s attrs
1157 let map_of attrs =
1158 let rec fold rs ls = function
1159 | ("out", v) :: rest -> fold v ls rest
1160 | ("in", v) :: rest -> fold rs v rest
1161 | _ :: rest -> fold ls rs rest
1162 | [] -> ls, rs
1164 fold E.s E.s attrs
1167 let setconf dst src =
1168 dst.scrollbw <- src.scrollbw;
1169 dst.scrollh <- src.scrollh;
1170 dst.icase <- src.icase;
1171 dst.preload <- src.preload;
1172 dst.pagebias <- src.pagebias;
1173 dst.verbose <- src.verbose;
1174 dst.scrollstep <- src.scrollstep;
1175 dst.maxhfit <- src.maxhfit;
1176 dst.crophack <- src.crophack;
1177 dst.autoscrollstep <- src.autoscrollstep;
1178 dst.maxwait <- src.maxwait;
1179 dst.hlinks <- src.hlinks;
1180 dst.underinfo <- src.underinfo;
1181 dst.interpagespace <- src.interpagespace;
1182 dst.zoom <- src.zoom;
1183 dst.presentation <- src.presentation;
1184 dst.angle <- src.angle;
1185 dst.cwinw <- src.cwinw;
1186 dst.cwinh <- src.cwinh;
1187 dst.savebmarks <- src.savebmarks;
1188 dst.memlimit <- src.memlimit;
1189 dst.fitmodel <- src.fitmodel;
1190 dst.texcount <- src.texcount;
1191 dst.sliceheight <- src.sliceheight;
1192 dst.thumbw <- src.thumbw;
1193 dst.jumpback <- src.jumpback;
1194 dst.bgcolor <- src.bgcolor;
1195 dst.tilew <- src.tilew;
1196 dst.tileh <- src.tileh;
1197 dst.mustoresize <- src.mustoresize;
1198 dst.checkers <- src.checkers;
1199 dst.aalevel <- src.aalevel;
1200 dst.trimmargins <- src.trimmargins;
1201 dst.trimfuzz <- src.trimfuzz;
1202 dst.urilauncher <- src.urilauncher;
1203 dst.colorspace <- src.colorspace;
1204 dst.invert <- src.invert;
1205 dst.colorscale <- src.colorscale;
1206 dst.ghyllscroll <- src.ghyllscroll;
1207 dst.columns <- src.columns;
1208 dst.beyecolumns <- src.beyecolumns;
1209 dst.selcmd <- src.selcmd;
1210 dst.updatecurs <- src.updatecurs;
1211 dst.pathlauncher <- src.pathlauncher;
1212 dst.keyhashes <- copykeyhashes src;
1213 dst.hfsize <- src.hfsize;
1214 dst.hscrollstep <- src.hscrollstep;
1215 dst.pgscale <- src.pgscale;
1216 dst.usepbo <- src.usepbo;
1217 dst.wheelbypage <- src.wheelbypage;
1218 dst.stcmd <- src.stcmd;
1219 dst.paxcmd <- src.paxcmd;
1220 dst.passcmd <- src.passcmd;
1221 dst.savecmd <- src.savecmd;
1222 dst.scrollb <- src.scrollb;
1223 dst.riani <- src.riani;
1224 dst.paxmark <- src.paxmark;
1225 dst.leftscroll <- src.leftscroll;
1226 dst.title <- src.title;
1227 dst.annotinline <- src.annotinline;
1228 dst.coarseprespos <- src.coarseprespos;
1229 dst.css <- src.css;
1230 dst.usedoccss <- src.usedoccss;
1231 dst.sbarcolor <- src.sbarcolor;
1232 dst.sbarhndlcolor <- src.sbarhndlcolor;
1233 dst.pax <-
1234 if src.pax = None
1235 then None
1236 else Some 0.0;
1239 let findkeyhash c name =
1240 try List.assoc name c.keyhashes
1241 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
1244 let get s =
1245 let open Parser in
1246 let h = Hashtbl.create 10 in
1247 let dc = { defconf with angle = defconf.angle } in
1248 let rec toplevel v t spos _ =
1249 match t with
1250 | Vdata | Vcdata | Vend -> v
1251 | Vopen ("llppconfig", _, closed) ->
1252 if closed
1253 then v
1254 else { v with f = llppconfig }
1255 | Vopen _ -> parse_error "unexpected subelement at top level" s spos
1256 | Vclose _ -> parse_error "unexpected close at top level" s spos
1258 and llppconfig v t spos _ =
1259 match t with
1260 | Vdata | Vcdata -> v
1261 | Vend -> parse_error "unexpected end of input in llppconfig" s spos
1262 | Vopen ("defaults", attrs, closed) ->
1263 let c = config_of dc attrs in
1264 setconf dc c;
1265 if closed
1266 then v
1267 else { v with f = defaults }
1269 | Vopen ("ui-font", attrs, closed) ->
1270 let rec getsize size = function
1271 | [] -> size
1272 | ("size", v) :: rest ->
1273 let size =
1274 fromstring int_of_string spos "size" v fstate.fontsize in
1275 getsize size rest
1276 | l -> getsize size l
1278 fstate.fontsize <- getsize fstate.fontsize attrs;
1279 if closed
1280 then v
1281 else { v with f = uifont (Buffer.create 10) }
1283 | Vopen ("doc", attrs, closed) ->
1284 let pathent, spage, srely, span, svisy, origin = doc_of attrs in
1285 let path = unentS pathent
1286 and origin = unentS origin
1287 and pageno = fromstring int_of_string spos "page" spage 0
1288 and rely = fromstring float_of_string spos "rely" srely 0.0
1289 and pan = fromstring int_of_string spos "pan" span 0
1290 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1291 let c = config_of dc attrs in
1292 let anchor = (pageno, rely, visy) in
1293 if closed
1294 then (Hashtbl.add h path (c, [], pan, anchor, origin); v)
1295 else { v with f = doc path origin pan anchor c [] }
1297 | Vopen _ ->
1298 parse_error "unexpected subelement in llppconfig" s spos
1300 | Vclose "llppconfig" -> { v with f = toplevel }
1301 | Vclose _ -> parse_error "unexpected close in llppconfig" s spos
1303 and defaults v t spos _ =
1304 match t with
1305 | Vdata | Vcdata -> v
1306 | Vend -> parse_error "unexpected end of input in defaults" s spos
1307 | Vopen ("keymap", attrs, closed) ->
1308 let modename =
1309 try List.assoc "mode" attrs
1310 with Not_found -> "global" in
1311 if closed
1312 then v
1313 else
1314 let ret keymap =
1315 let h = findkeyhash dc modename in
1316 KeyMap.iter (Hashtbl.replace h) keymap;
1317 defaults
1319 { v with f = pkeymap ret KeyMap.empty }
1321 | Vopen (_, _, _) ->
1322 parse_error "unexpected subelement in defaults" s spos
1324 | Vclose "defaults" ->
1325 { v with f = llppconfig }
1327 | Vclose _ -> parse_error "unexpected close in defaults" s spos
1329 and uifont b v t spos epos =
1330 match t with
1331 | Vdata | Vcdata ->
1332 Buffer.add_substring b s spos (epos - spos);
1334 | Vopen (_, _, _) ->
1335 parse_error "unexpected subelement in ui-font" s spos
1336 | Vclose "ui-font" ->
1337 if emptystr !fontpath
1338 then fontpath := Buffer.contents b;
1339 { v with f = llppconfig }
1340 | Vclose _ -> parse_error "unexpected close in ui-font" s spos
1341 | Vend -> parse_error "unexpected end of input in ui-font" s spos
1343 and doc path origin pan anchor c bookmarks v t spos _ =
1344 match t with
1345 | Vdata | Vcdata -> v
1346 | Vend -> parse_error "unexpected end of input in doc" s spos
1347 | Vopen ("bookmarks", _, closed) ->
1348 if closed
1349 then v
1350 else { v with f = pbookmarks path origin pan anchor c bookmarks }
1352 | Vopen ("keymap", attrs, closed) ->
1353 let modename =
1354 try List.assoc "mode" attrs
1355 with Not_found -> "global"
1357 if closed
1358 then v
1359 else
1360 let ret keymap =
1361 let h = findkeyhash c modename in
1362 KeyMap.iter (Hashtbl.replace h) keymap;
1363 doc path origin pan anchor c bookmarks
1365 { v with f = pkeymap ret KeyMap.empty }
1367 | Vopen ("css", [], false) ->
1368 { v with f = pcss path origin pan anchor c bookmarks }
1370 | Vopen (_, _, _) ->
1371 parse_error "unexpected subelement in doc" s spos
1373 | Vclose "doc" ->
1374 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor, origin);
1375 { v with f = llppconfig }
1377 | Vclose _ -> parse_error "unexpected close in doc" s spos
1379 and pcss path origin pan anchor c bookmarks v t spos epos =
1380 match t with
1381 | Vdata | Vcdata ->
1382 let b = Buffer.create 10 in
1383 Buffer.add_substring b s spos (epos - spos);
1384 { v with f = pcss path origin pan anchor
1385 { c with css = Buffer.contents b }
1386 bookmarks }
1387 | Vend -> parse_error "unexpected end of input in css" s spos
1388 | Vopen _ -> parse_error "unexpected subelement in css" s spos
1389 | Vclose "css" -> { v with f = doc path origin pan anchor c bookmarks }
1390 | Vclose _ -> parse_error "unexpected close in css" s spos
1392 and pkeymap ret keymap v t spos _ =
1393 match t with
1394 | Vdata | Vcdata -> v
1395 | Vend -> parse_error "unexpected end of input in keymap" s spos
1396 | Vopen ("map", attrs, closed) ->
1397 let r, l = map_of attrs in
1398 let kss = fromstring keys_of_string spos "in" r [] in
1399 let lss = fromstring keys_of_string spos "out" l [] in
1400 let keymap =
1401 match kss with
1402 | [] -> keymap
1403 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
1404 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
1406 if closed
1407 then { v with f = pkeymap ret keymap }
1408 else
1409 let f () = v in
1410 { v with f = skip "map" f }
1412 | Vopen _ ->
1413 parse_error "unexpected subelement in keymap" s spos
1415 | Vclose "keymap" ->
1416 { v with f = ret keymap }
1418 | Vclose _ -> parse_error "unexpected close in keymap" s spos
1420 and pbookmarks path origin pan anchor c bookmarks v t spos _ =
1421 match t with
1422 | Vdata | Vcdata -> v
1423 | Vend -> parse_error "unexpected end of input in bookmarks" s spos
1424 | Vopen ("item", attrs, closed) ->
1425 let titleent, spage, srely, svisy = bookmark_of attrs in
1426 let page = fromstring int_of_string spos "page" spage 0
1427 and rely = fromstring float_of_string spos "rely" srely 0.0
1428 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1429 let bookmarks =
1430 (unentS titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
1432 if closed
1433 then { v with f = pbookmarks path origin pan anchor c bookmarks }
1434 else
1435 let f () = v in
1436 { v with f = skip "item" f }
1438 | Vopen _ ->
1439 parse_error "unexpected subelement in bookmarks" s spos
1441 | Vclose "bookmarks" ->
1442 { v with f = doc path origin pan anchor c bookmarks }
1444 | Vclose _ -> parse_error "unexpected close in bookmarks" s spos
1446 and skip tag f v t spos _ =
1447 match t with
1448 | Vdata | Vcdata -> v
1449 | Vend ->
1450 parse_error ("unexpected end of input in skipped " ^ tag) s spos
1451 | Vopen (tag', _, closed) ->
1452 if closed
1453 then v
1454 else
1455 let f' () = { v with f = skip tag f } in
1456 { v with f = skip tag' f' }
1457 | Vclose ctag ->
1458 if tag = ctag
1459 then f ()
1460 else parse_error ("unexpected close in skipped " ^ tag) s spos
1463 parse { f = toplevel; accu = () } s;
1464 h, dc;
1467 let do_load f contents =
1468 try f contents
1469 with
1470 | Parser.Parse_error (msg, s, pos) ->
1471 let subs = Parser.subs s pos in
1472 Utils.error "parse error: %s: at %d [..%S..]" msg pos subs
1474 | exn -> Utils.error "parse error: %s" @@ exntos exn
1477 let defconfpath =
1478 let dir =
1479 let xdgconfdir = Utils.getenvwithdef "XDG_CONFIG_HOME" E.s in
1480 if emptystr xdgconfdir
1481 then
1483 let dir = Filename.concat home ".config" in
1484 if Sys.is_directory dir then dir else home
1485 with _ -> home
1486 else xdgconfdir
1488 Filename.concat dir "llpp.conf"
1491 let confpath = ref defconfpath;;
1493 let load2 f default =
1494 match filecontents !confpath with
1495 | contents -> f @@ do_load get contents
1496 | exception Unix.Unix_error (Unix.ENOENT, "open", _) ->
1497 f (Hashtbl.create 0, defconf)
1498 | exception exn ->
1499 dolog "error loading configuration from `%S': %s" !confpath @@ exntos exn;
1500 default
1503 let load1 f = load2 f false;;
1505 let load openlast =
1506 let f (h, dc) =
1507 if openlast
1508 then (
1509 let path, _ =
1510 Hashtbl.fold
1511 (fun path (conf, _, _, _, _) ((_, besttime) as best) ->
1512 if conf.lastvisit > besttime
1513 then (path, conf.lastvisit)
1514 else best)
1516 (state.path, -.infinity)
1518 state.path <- path;
1520 let pc, pb, px, pa, po =
1522 let absname = abspath state.path in
1523 Hashtbl.find h absname
1524 with Not_found -> dc, [], 0, emptyanchor, state.origin
1526 setconf defconf dc;
1527 setconf conf pc;
1528 state.bookmarks <- pb;
1529 state.x <- px;
1530 state.origin <- po;
1531 if conf.jumpback
1532 then state.anchor <- pa;
1533 cbput state.hists.nav pa;
1534 true
1536 load1 f
1539 let gethist () =
1540 let f (h, _) =
1541 Hashtbl.fold (fun path (pc, pb, px, pa, po) accu ->
1542 (path, pc, pb, px, pa, po) :: accu)
1543 h [];
1545 load2 f []
1548 let add_attrs bb always dc c time =
1549 let o' fmt s =
1550 Buffer.add_string bb "\n ";
1551 Printf.bprintf bb fmt s
1553 let o c fmt s = if c then o' fmt s else ignore in
1554 let ob s a b = o (always || a != b) "%s='%b'" s a
1555 and op s a b = o (always || a <> b) "%s='%b'" s (a != None)
1556 and oi s a b = o (always || a != b) "%s='%d'" s a
1557 and oI s a b = o (always || a != b) "%s='%s'" s (string_with_suffix_of_int a)
1558 and oz s a b = o (always || a <> b) "%s='%g'" s (a*.100.)
1559 and oF s a b = o (always || a <> b) "%s='%f'" s a
1560 and oL s a b = o (always || a <> b) "%s='%Ld'" s a
1561 and oc s a b = o (always || a <> b) "%s='%s'" s (color_to_string a)
1562 and oA s a b = o (always || a <> b) "%s='%s'" s (rgba_to_string a)
1563 and oC s a b = o (always || a <> b) "%s='%s'" s (CSTE.to_string a)
1564 and oR s a b = o (always || a <> b) "%s='%s'" s (irect_to_string a)
1565 and oFm s a b = o (always || a <> b) "%s='%s'" s (FMTE.to_string a)
1566 and oSv s a b m =
1567 o (always || a land m <> b land m) "%s='%b'" s (a land m != 0)
1568 and oPm s a b = o (always || a <> b) "%s='%s'" s (MTE.to_string a)
1569 and os s a b =
1570 o (always || a <> b) "%s='%s'" s @@ Parser.enent a 0 (String.length a)
1571 and og s a b =
1572 if always || a <> b
1573 then
1574 match a with
1575 | Some (_N, _A, _B) -> o' "%s='%u,%u,%u'" s _N _A _B
1576 | None ->
1577 match b with
1578 | None -> ()
1579 | _ -> o' "%s='none'" s
1580 and oW s a b =
1581 if always || a <> b
1582 then
1583 let v =
1584 match a with
1585 | None -> "false"
1586 | Some f ->
1587 if f = infinity
1588 then "true"
1589 else string_of_float f
1591 o' "%s='%s'" s v
1592 and oco s a b =
1593 if always || a <> b
1594 then
1595 match a with
1596 | Cmulti ((n, a, b), _) when n > 1 -> o' "%s='%d,%d,%d'" s n a b
1597 | Csplit (n, _) when n > 1 -> o' "%s='%d'" s ~-n
1598 | Cmulti _ | Csplit _ | Csingle _ -> ()
1599 and obeco s a b =
1600 if always || a <> b
1601 then
1602 match a with
1603 | Some c when c > 1 -> o' "%s='%d'" s c
1604 | _ -> ()
1606 oi "width" c.cwinw dc.cwinw;
1607 oi "height" c.cwinh dc.cwinh;
1608 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
1609 oi "scroll-handle-height" c.scrollh dc.scrollh;
1610 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
1611 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
1612 ob "case-insensitive-search" c.icase dc.icase;
1613 ob "preload" c.preload dc.preload;
1614 oi "page-bias" c.pagebias dc.pagebias;
1615 oi "scroll-step" c.scrollstep dc.scrollstep;
1616 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
1617 ob "max-height-fit" c.maxhfit dc.maxhfit;
1618 ob "crop-hack" c.crophack dc.crophack;
1619 oW "throttle" c.maxwait dc.maxwait;
1620 ob "highlight-links" c.hlinks dc.hlinks;
1621 ob "under-cursor-info" c.underinfo dc.underinfo;
1622 oi "vertical-margin" c.interpagespace dc.interpagespace;
1623 oz "zoom" c.zoom dc.zoom;
1624 ob "presentation" c.presentation dc.presentation;
1625 oi "rotation-angle" c.angle dc.angle;
1626 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
1627 oFm "fit-model" c.fitmodel dc.fitmodel;
1628 oI "pixmap-cache-size" c.memlimit dc.memlimit;
1629 oi "tex-count" c.texcount dc.texcount;
1630 oi "slice-height" c.sliceheight dc.sliceheight;
1631 oi "thumbnail-width" c.thumbw dc.thumbw;
1632 ob "persistent-location" c.jumpback dc.jumpback;
1633 oc "background-color" c.bgcolor dc.bgcolor;
1634 oA "scrollbar-color" c.sbarcolor dc.sbarcolor;
1635 oA "scrollbar-handle-color" c.sbarhndlcolor dc.sbarhndlcolor;
1636 oi "tile-width" c.tilew dc.tilew;
1637 oi "tile-height" c.tileh dc.tileh;
1638 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
1639 ob "checkers" c.checkers dc.checkers;
1640 oi "aalevel" c.aalevel dc.aalevel;
1641 ob "trim-margins" c.trimmargins dc.trimmargins;
1642 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
1643 os "uri-launcher" c.urilauncher dc.urilauncher;
1644 os "path-launcher" c.pathlauncher dc.pathlauncher;
1645 oC "color-space" c.colorspace dc.colorspace;
1646 ob "invert-colors" c.invert dc.invert;
1647 oF "brightness" c.colorscale dc.colorscale;
1648 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
1649 oco "columns" c.columns dc.columns;
1650 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
1651 os "selection-command" c.selcmd dc.selcmd;
1652 os "synctex-command" c.stcmd dc.stcmd;
1653 os "pax-command" c.paxcmd dc.paxcmd;
1654 os "askpass-command" c.passcmd dc.passcmd;
1655 os "savepath-command" c.savecmd dc.savecmd;
1656 ob "update-cursor" c.updatecurs dc.updatecurs;
1657 oi "hint-font-size" c.hfsize dc.hfsize;
1658 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
1659 oF "page-scroll-scale" c.pgscale dc.pgscale;
1660 ob "use-pbo" c.usepbo dc.usepbo;
1661 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
1662 ob "remote-in-a-new-instance" c.riani dc.riani;
1663 op "point-and-x" c.pax dc.pax;
1664 oPm "point-and-x-mark" c.paxmark dc.paxmark;
1665 ob "scroll-bar-on-the-left" c.leftscroll dc.leftscroll;
1666 if not always
1667 then os "title" c.title dc.title;
1668 oL "last-visit" (Int64.of_float time) 0L;
1669 ob "edit-annotations-inline" c.annotinline dc.annotinline;
1670 ob "coarse-presentation-positioning" c.coarseprespos dc.coarseprespos;
1671 ob "use-document-css" c.usedoccss dc.usedoccss;
1674 let keymapsbuf always dc c =
1675 let open Buffer in
1676 let bb = create 16 in
1677 let rec loop = function
1678 | [] -> ()
1679 | (modename, h) :: rest ->
1680 let dh = findkeyhash dc modename in
1681 if always || h <> dh
1682 then (
1683 if Hashtbl.length h > 0
1684 then (
1685 if length bb > 0 then add_char bb '\n';
1686 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
1687 Hashtbl.iter (fun i o ->
1688 if always || match Hashtbl.find dh i
1689 with | dO -> dO <> o | exception Not_found -> false
1690 then
1691 let addkm (k, m) =
1692 if Wsi.withctrl m then add_string bb "ctrl-";
1693 if Wsi.withalt m then add_string bb "alt-";
1694 if Wsi.withshift m then add_string bb "shift-";
1695 if Wsi.withmeta m then add_string bb "meta-";
1696 add_string bb (Wsi.keyname k);
1698 let addkms l =
1699 let rec loop = function
1700 | [] -> ()
1701 | km :: [] -> addkm km
1702 | km :: rest -> addkm km; add_char bb ' '; loop rest
1704 loop l
1706 add_string bb "<map in='";
1707 addkm i;
1708 match o with
1709 | KMinsrt km ->
1710 add_string bb "' out='"; addkm km; add_string bb "'/>\n"
1712 | KMinsrl kms ->
1713 add_string bb "' out='"; addkms kms; add_string bb "'/>\n"
1715 | KMmulti (ins, kms) ->
1716 add_char bb ' '; addkms ins; add_string bb "' out='";
1717 addkms kms; add_string bb "'/>\n"
1718 ) h;
1719 add_string bb "</keymap>";
1722 loop rest
1724 loop c.keyhashes;
1728 let keystostrlist c =
1729 let rec loop accu = function
1730 | [] -> accu
1731 | (modename, h) :: rest ->
1732 let accu =
1733 if Hashtbl.length h > 0
1734 then (
1735 let accu = Printf.sprintf "\xc2\xb7Keys for %s" modename :: accu in
1736 Hashtbl.fold (fun i o a ->
1737 let bb = Buffer.create 10 in
1738 let addkm (k, m) =
1739 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
1740 if Wsi.withalt m then Buffer.add_string bb "alt-";
1741 if Wsi.withshift m then Buffer.add_string bb "shift-";
1742 if Wsi.withmeta m then Buffer.add_string bb "meta-";
1743 Buffer.add_string bb (Wsi.keyname k);
1745 let addkms l =
1746 let rec loop = function
1747 | [] -> ()
1748 | km :: [] -> addkm km
1749 | km :: rest ->
1750 addkm km; Buffer.add_char bb ' ';
1751 loop rest
1753 loop l
1755 addkm i;
1756 Buffer.add_char bb '\t';
1757 begin match o with
1758 | KMinsrt km ->
1759 addkm km
1761 | KMinsrl kms ->
1762 addkms kms
1764 | KMmulti (ins, kms) ->
1765 Buffer.add_char bb ' ';
1766 addkms ins;
1767 Buffer.add_string bb "\t";
1768 addkms kms
1769 end;
1770 Buffer.contents bb :: a
1771 ) h accu
1773 else accu
1775 loop accu rest
1777 loop [] c.keyhashes
1780 let save1 bb leavebirdseye x h dc =
1781 let uifontsize = fstate.fontsize in
1782 let dc = if conf.bedefault then conf else dc in
1783 Buffer.add_string bb "<llppconfig>\n";
1785 if nonemptystr !fontpath
1786 then
1787 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
1788 uifontsize
1789 !fontpath
1790 else (
1791 if uifontsize <> 14
1792 then
1793 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
1796 Buffer.add_string bb "<defaults";
1797 add_attrs bb true dc dc nan;
1798 let kb = keymapsbuf true dc dc in
1799 if Buffer.length kb > 0
1800 then (
1801 Buffer.add_string bb ">\n";
1802 Buffer.add_buffer bb kb;
1803 Buffer.add_string bb "\n</defaults>\n";
1805 else Buffer.add_string bb "/>\n";
1807 let adddoc path pan anchor c bookmarks time origin =
1808 if bookmarks == [] && c = dc && anchor = emptyanchor
1809 then ()
1810 else (
1811 Printf.bprintf bb "<doc path='%s'"
1812 (Parser.enent path 0 (String.length path));
1814 if nonemptystr origin
1815 then Printf.bprintf bb "\n origin='%s'"
1816 (Parser.enent origin 0 (String.length origin));
1818 if anchor <> emptyanchor
1819 then (
1820 let n, rely, visy = anchor in
1821 Printf.bprintf bb "\n page='%d'" n;
1823 if rely > 1e-6
1824 then Printf.bprintf bb " rely='%f'" rely;
1826 if abs_float visy > 1e-6
1827 then Printf.bprintf bb " visy='%f'" visy;
1830 if pan != 0
1831 then Printf.bprintf bb " pan='%d'" pan;
1833 add_attrs bb false dc c time;
1834 if nonemptystr c.css
1835 then Printf.bprintf bb ">\n <css><![CDATA[%s]]></css>" c.css;
1836 let kb = keymapsbuf false dc c in
1838 begin match bookmarks with
1839 | [] ->
1840 if Buffer.length kb > 0
1841 then (
1842 Buffer.add_string bb ">\n";
1843 Buffer.add_buffer bb kb;
1844 Buffer.add_string bb "\n</doc>\n";
1846 else
1847 if nonemptystr c.css
1848 then Buffer.add_string bb "\n</doc>\n"
1849 else Buffer.add_string bb "/>\n"
1850 | _ ->
1851 Buffer.add_string bb ">\n<bookmarks>\n";
1852 List.iter (fun (title, _, kind) ->
1853 begin match kind with
1854 | Oanchor (page, rely, visy) ->
1855 Printf.bprintf bb
1856 "<item title='%s' page='%d'"
1857 (Parser.enent title 0 (String.length title))
1858 page
1860 if rely > 1e-6
1861 then
1862 Printf.bprintf bb " rely='%f'" rely
1864 if abs_float visy > 1e-6
1865 then
1866 Printf.bprintf bb " visy='%f'" visy
1868 | Ohistory _ | Onone | Ouri _ | Oremote _
1869 | Oremotedest _ | Olaunch _ ->
1870 failwith "unexpected link in bookmarks"
1871 end;
1872 Buffer.add_string bb "/>\n";
1873 ) bookmarks;
1874 Buffer.add_string bb "</bookmarks>";
1875 if Buffer.length kb > 0
1876 then (
1877 Buffer.add_string bb "\n";
1878 Buffer.add_buffer bb kb;
1880 Buffer.add_string bb "\n</doc>\n";
1881 end;
1885 let pan, conf =
1886 match state.mode with
1887 | Birdseye (c, pan, _, _, _) ->
1888 let beyecolumns =
1889 match conf.columns with
1890 | Cmulti ((c, _, _), _) -> Some c
1891 | Csingle _ -> None
1892 | Csplit _ -> None
1893 and columns =
1894 match c.columns with
1895 | Cmulti (c, _) -> Cmulti (c, E.a)
1896 | Csingle _ -> Csingle E.a
1897 | Csplit _ -> failwith "quit from bird's eye while split"
1899 pan, { c with beyecolumns = beyecolumns; columns = columns }
1900 | Textentry _
1901 | View
1902 | LinkNav _ -> x, conf
1904 let docpath = if nonemptystr state.path then abspath state.path else E.s in
1905 if nonemptystr docpath
1906 then (
1907 adddoc docpath pan (getanchor ())
1909 let autoscrollstep =
1910 match state.autoscroll with
1911 | Some step -> step
1912 | None -> conf.autoscrollstep
1914 begin match state.mode with
1915 | Birdseye beye -> leavebirdseye beye true
1916 | Textentry _
1917 | View
1918 | LinkNav _ -> ()
1919 end;
1920 { conf with autoscrollstep = autoscrollstep }
1922 (if conf.savebmarks then state.bookmarks else [])
1923 (now ())
1924 state.origin
1926 Hashtbl.iter (fun path (c, bookmarks, x, anchor, origin) ->
1927 if docpath <> abspath path
1928 then adddoc path x anchor c bookmarks c.lastvisit origin
1929 ) h;
1930 Buffer.add_string bb "</llppconfig>\n";
1931 true;
1934 let save leavebirdseye =
1935 let relx = float state.x /. float state.winw in
1936 let w, h, x =
1937 let cx w = truncate (relx *. float w) in
1938 List.fold_left
1939 (fun (w, h, x) ws ->
1940 match ws with
1941 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
1942 | Wsi.MaxVert -> (w, conf.cwinh, x)
1943 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
1945 (state.winw, state.winh, state.x) state.winstate
1947 conf.cwinw <- w;
1948 conf.cwinh <- h;
1949 let bb = Buffer.create 32768 in
1950 let save2 (h, dc) =
1951 save1 bb leavebirdseye x h dc
1953 if load1 save2 && Buffer.length bb > 0
1954 then
1956 let tmp = !confpath ^ ".tmp" in
1957 let oc = open_out_bin tmp in
1958 Buffer.output_buffer oc bb;
1959 close_out oc;
1960 Unix.rename tmp !confpath;
1961 with exn ->
1962 dolog "error saving configuration: %s" @@ exntos exn
1965 let gc () =
1966 let href = ref @@ Hashtbl.create 0 in
1967 let cref = ref defconf in
1968 let push (h, dc) =
1969 let f path v =
1970 if Sys.file_exists path
1971 then Some v
1972 else (dolog "removing %s" path; None) in
1973 Hashtbl.filter_map_inplace f h;
1974 href := h;
1975 cref := dc;
1976 true
1978 ignore (load1 push);
1979 let bb = Buffer.create 32768 in
1980 let save2 (_h, dc) = save1 bb (fun _ _ -> ()) 0 !href dc in
1981 if load1 save2 && Buffer.length bb > 0
1982 then (
1984 let tmp = !confpath ^ ".tmp" in
1985 let oc = open_out_bin tmp in
1986 Buffer.output_buffer oc bb;
1987 close_out oc;
1988 Unix.rename tmp !confpath;
1989 with exn ->
1990 dolog "error saving configuration: %s" @@ exntos exn
1994 let logcurrently = function
1995 | Idle -> dolog "Idle"
1996 | Loading (l, gen) ->
1997 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1998 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1999 dolog
2000 "Tiling %d[%d,%d] page=%s cs=%s angle=%d"
2001 l.pageno col row (~> pageopaque)
2002 (CSTE.to_string colorspace) angle;
2003 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2004 angle gen conf.angle state.gen
2005 tilew tileh
2006 conf.tilew conf.tileh;
2007 | Outlining _ ->
2008 dolog "outlining"