Wait for any child process
[llpp.git] / config.ml
blob4d4b60c167452e6a79c5a41fcc94e1771222d12b
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 = 14
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);;
23 let pidset = ref IntSet.empty;;
24 let addpid pid = if pid > 0 then pidset := IntSet.add pid !pidset;;
25 let haspid pid = IntSet.mem pid !pidset;;
26 let delpid pid = pidset := IntSet.remove pid !pidset;;
27 let ispidsetempty () = IntSet.is_empty !pidset;;
29 let irect_of_string s =
30 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
33 let irect_to_string (x0,y0,x1,y1) =
34 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
37 let ghyllscroll_of_string s =
38 match s with
39 | "fast" -> Some fastghyllscroll
40 | "neat" -> Some (10,1,9)
41 | "" | "none" -> None
42 | _ ->
43 let (n,a,b) as nab =
44 Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b) in
45 if n <= a || n <= b || a >= b
46 then error "invalid ghyll N(%d),A(%d),B(%d) (N <= A, A < B, N <= B)"
47 n a b;
48 Some nab
51 let ghyllscroll_to_string ((n, a, b) as nab) =
52 (**) if nab = fastghyllscroll then "fast"
53 else if nab = neatghyllscroll then "neat"
54 else Printf.sprintf "%d,%d,%d" n a b;
57 let multicolumns_to_string (n, a, b) =
58 if a = 0 && b = 0
59 then Printf.sprintf "%d" n
60 else Printf.sprintf "%d,%d,%d" n a b;
63 let multicolumns_of_string s =
64 try
65 (int_of_string s, 0, 0)
66 with _ ->
67 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
68 if a > 1 || b > 1
69 then failwith "subtly broken"; (n, a, b)
73 type keymap =
74 | KMinsrt of key
75 | KMinsrl of key list
76 | KMmulti of key list * key list
77 and key = int * int
78 and keyhash = (key, keymap) Hashtbl.t
79 and keystate =
80 | KSnone
81 | KSinto of (key list * key list)
82 and interpagespace = int
83 and multicolumns = multicol * pagegeom
84 and singlecolumn = pagegeom
85 and splitcolumns = columncount * pagegeom
86 and pagegeom = (pdimno * x * y * (pageno * width * height * leftx)) array
87 and multicol = columncount * covercount * covercount
88 and pdimno = int
89 and columncount = int
90 and covercount = int
91 and fitmodel = | FitWidth | FitProportional | FitPage
92 and trimmargins = bool
93 and irect = (int * int * int * int)
94 and memsize = int
95 and texcount = int
96 and sliceheight = int
97 and angle = int
98 and params = (angle * fitmodel * trimparams
99 * texcount * sliceheight * memsize
100 * colorspace * fontpath * trimcachepath
101 * haspbo * usefontconfig)
102 and width = int
103 and height = int
104 and leftx = int
105 and opaque = Opaque.t
106 and recttype = int
107 and pixmapsize = int
108 and gen = int
109 and top = float
110 and dtop = float
111 and fontpath = string
112 and trimcachepath = string
113 and aalevel = int
114 and trimparams = (trimmargins * irect)
115 and colorspace = | Rgb | Bgr | Gray
116 and haspbo = bool
117 and usefontconfig = bool
118 and uri = string
119 and caption = string
120 and x = int
121 and y = int
122 and tilex = int
123 and tiley = int
124 and tileparams = (x * y * width * height * tilex * tiley)
125 and under =
126 | Unone
127 | Ulinkuri of string
128 | Ulinkgoto of (int * int)
129 | Utext of facename
130 | Uunexpected of string
131 | Ulaunch of launchcommand
132 | Unamed of destname
133 | Uremote of (filename * pageno)
134 | Uremotedest of (filename * destname)
135 | Uannotation of (opaque * slinkindex)
136 and slinkindex = int
137 and facename = string
138 and launchcommand = string
139 and filename = string
140 and pageno = int
141 and destname = string
142 and mark =
143 | Mark_page
144 | Mark_block
145 | Mark_line
146 | Mark_word
147 and link =
148 | Lnotfound
149 | Lfound of int
150 and linkdir =
151 | LDfirst
152 | LDlast
153 | LDfirstvisible of (int * int * int)
154 | LDleft of int
155 | LDright of int
156 | LDdown of int
157 | LDup of int
158 and pagewithlinks =
159 | Pwlnotfound
160 | Pwl of int
161 and scrollb = int
162 and anchor = pageno * top * dtop
163 and rect = float * float * float * float * float * float * float * float
164 and infochange = | Memused | Docinfo | Pdim
167 class type uioh = object
168 method display : unit
169 method key : int -> int -> uioh
170 method button : int -> bool -> int -> int -> int -> uioh
171 method multiclick : int -> int -> int -> int -> uioh
172 method motion : int -> int -> uioh
173 method pmotion : int -> int -> uioh
174 method infochanged : infochange -> unit
175 method scrollpw : (int * float * float)
176 method scrollph : (int * float * float)
177 method modehash : keyhash
178 method eformsgs : bool
179 method alwaysscrolly : bool
180 end;;
182 module type TextEnumType =
184 type t
185 val name : string
186 val names : string array
187 end;;
189 module TextEnumMake (Ten : TextEnumType) =
190 struct
191 let names = Ten.names;;
192 let to_int (t : Ten.t) = Obj.magic t;;
193 let to_string t = names.(to_int t);;
194 let of_int n : Ten.t = Obj.magic n;;
195 let of_string s =
196 let rec find i =
197 if i = Array.length names
198 then failwith ("invalid " ^ Ten.name ^ ": " ^ s)
199 else (
200 if Ten.names.(i) = s
201 then of_int i
202 else find (i+1)
204 in find 0;;
205 end;;
207 module CSTE = TextEnumMake (struct
208 type t = colorspace;;
209 let name = "colorspace";;
210 let names = [|"rgb"; "bgr"; "gray"|];;
211 end);;
213 module MTE = TextEnumMake (struct
214 type t = mark;;
215 let name = "mark";;
216 let names = [|"page"; "block"; "line"; "word"|];;
217 end);;
219 module FMTE = TextEnumMake (struct
220 type t = fitmodel;;
221 let name = "fitmodel";;
222 let names = [|"width"; "proportional"; "page"|];;
223 end);;
225 type conf =
226 { mutable scrollbw : int
227 ; mutable scrollh : int
228 ; mutable scrollb : scrollb
229 ; mutable icase : bool
230 ; mutable preload : bool
231 ; mutable pagebias : int
232 ; mutable verbose : bool
233 ; mutable debug : bool
234 ; mutable scrollstep : int
235 ; mutable hscrollstep : int
236 ; mutable maxhfit : bool
237 ; mutable crophack : bool
238 ; mutable autoscrollstep : int
239 ; mutable maxwait : float option
240 ; mutable hlinks : bool
241 ; mutable underinfo : bool
242 ; mutable interpagespace : interpagespace
243 ; mutable zoom : float
244 ; mutable presentation : bool
245 ; mutable angle : angle
246 ; mutable cwinw : int
247 ; mutable cwinh : int
248 ; mutable savebmarks : bool
249 ; mutable fitmodel : fitmodel
250 ; mutable trimmargins : trimmargins
251 ; mutable trimfuzz : irect
252 ; mutable memlimit : memsize
253 ; mutable texcount : texcount
254 ; mutable sliceheight : sliceheight
255 ; mutable thumbw : width
256 ; mutable jumpback : bool
257 ; mutable bgcolor : (float * float * float)
258 ; mutable bedefault : bool
259 ; mutable tilew : int
260 ; mutable tileh : int
261 ; mutable mustoresize : memsize
262 ; mutable checkers : bool
263 ; mutable aalevel : int
264 ; mutable urilauncher : string
265 ; mutable pathlauncher : string
266 ; mutable colorspace : colorspace
267 ; mutable invert : bool
268 ; mutable colorscale : float
269 ; mutable redirectstderr : bool
270 ; mutable ghyllscroll : (int * int * int) option
271 ; mutable columns : columns
272 ; mutable beyecolumns : columncount option
273 ; mutable selcmd : string
274 ; mutable paxcmd : string
275 ; mutable passcmd : string
276 ; mutable savecmd : string
277 ; mutable updatecurs : bool
278 ; mutable keyhashes : (string * keyhash) list
279 ; mutable hfsize : int
280 ; mutable pgscale : float
281 ; mutable usepbo : bool
282 ; mutable wheelbypage : bool
283 ; mutable stcmd : string
284 ; mutable riani : bool
285 ; mutable pax : (float * int * int) ref option
286 ; mutable paxmark : mark
287 ; mutable leftscroll : bool
288 ; mutable title : string
289 ; mutable lastvisit : float
291 and columns =
292 | Csingle of singlecolumn
293 | Cmulti of multicolumns
294 | Csplit of splitcolumns
295 and outlinekind =
296 | Onone
297 | Oanchor of anchor
298 | Ouri of uri
299 | Olaunch of launchcommand
300 | Oremote of (filename * pageno)
301 | Oremotedest of (filename * destname)
302 | Ohistory of (filename * (conf * outline list * x * anchor))
303 | Oaction of (unit -> unit)
304 and outline = (caption * outlinelevel * outlinekind)
305 and outlinelevel = int
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)
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 * onkey * ondone * cancelonempty
367 and onkey = string -> int -> te
368 and ondone = string -> unit
369 and histcancel = unit -> unit
370 and onhist = ((histcmd -> string) * histcancel)
371 and histcmd = HCnext | HCprev | HCfirst | HClast
372 and cancelonempty = bool
373 and te =
374 | TEstop
375 | TEdone of string
376 | TEcont of string
377 | TEswitch of textentry
380 type 'a circbuf =
381 { store : 'a array
382 ; mutable rc : int
383 ; mutable wc : int
384 ; mutable len : int
388 type state =
389 { mutable ss : Unix.file_descr
390 ; mutable wsfd : Unix.file_descr
391 ; mutable errfd : Unix.file_descr option
392 ; mutable stderr : Unix.file_descr
393 ; mutable errmsgs : Buffer.t
394 ; mutable newerrmsgs : bool
395 ; mutable w : int
396 ; mutable x : int
397 ; mutable y : int
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 * recttype * rect) list
411 ; mutable rects1 : (pageno * recttype * rect) list
412 ; mutable text : string
413 ; mutable winstate : Wsi.winstate list
414 ; mutable mode : mode
415 ; mutable uioh : uioh
416 ; mutable outlines : outline array
417 ; mutable bookmarks : outline list
418 ; mutable path : string
419 ; mutable password : string
420 ; mutable nameddest : string
421 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
422 ; mutable memused : memsize
423 ; mutable gen : gen
424 ; mutable throttle : (page list * int * float) option
425 ; mutable autoscroll : int option
426 ; mutable ghyll : (int option -> unit)
427 ; mutable help : helpitem array
428 ; mutable docinfo : (int * string) list
429 ; mutable checkerstexid : GlTex.texture_id option
430 ; hists : hists
431 ; mutable prevzoom : (float * int)
432 ; mutable progress : float
433 ; mutable redisplay : bool
434 ; mutable mpos : mpos
435 ; mutable keystate : keystate
436 ; mutable glinks : bool
437 ; mutable prevcolumns : (columns * float) option
438 ; mutable winw : int
439 ; mutable winh : int
440 ; mutable reprf : (unit -> unit)
441 ; mutable origin : string
442 ; mutable roam : (unit -> unit)
443 ; mutable bzoom : bool
444 ; mutable traw : [`float] Raw.t
445 ; mutable vraw : [`float] Raw.t
447 and hists =
448 { pat : string circbuf
449 ; pag : string circbuf
450 ; nav : anchor circbuf
451 ; sel : string circbuf
455 let emptyanchor = (0, 0.0, 0.0);;
456 let emptykeyhash = Hashtbl.create 0;;
457 let firstgeomcmds = E.s, [];;
458 let noghyll _ = ();;
459 let noreprf () = ();;
460 let noroam () = ();;
462 let nouioh : uioh = object (self)
463 method display = ()
464 method key _ _ = self
465 method multiclick _ _ _ _ = self
466 method button _ _ _ _ _ = self
467 method motion _ _ = self
468 method pmotion _ _ = self
469 method infochanged _ = ()
470 method scrollpw = (0, nan, nan)
471 method scrollph = (0, nan, nan)
472 method modehash = emptykeyhash
473 method eformsgs = false
474 method alwaysscrolly = false
475 end;;
477 let platform_to_string = function
478 | Punknown -> "unknown"
479 | Plinux -> "Linux"
480 | Posx -> "OSX"
481 | Psun -> "Sun"
482 | Pbsd -> "BSD"
483 | Pcygwin -> "Cygwin"
486 let version () =
487 Printf.sprintf "llpp version %s, fitz %s, ocaml %s/%d bit"
488 Help.version (fz_version ()) Sys.ocaml_version Sys.word_size
491 let geturl s =
492 let colonpos = try String.index s ':' with Not_found -> -1 in
493 let len = String.length s in
494 if colonpos >= 0 && colonpos + 3 < len
495 then (
496 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
497 then
498 let schemestartpos =
499 try String.rindex_from s colonpos ' '
500 with Not_found -> -1
502 let scheme =
503 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
505 match scheme with
506 | "http" | "ftp" | "mailto" ->
507 let epos =
508 try String.index_from s colonpos ' '
509 with Not_found -> len
511 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
512 | _ -> E.s
513 else E.s
515 else E.s
518 let defconf =
519 { scrollbw = 7
520 ; scrollh = 12
521 ; scrollb = scrollbhv lor scrollbvv
522 ; icase = true
523 ; preload = true
524 ; pagebias = 0
525 ; verbose = false
526 ; debug = false
527 ; scrollstep = 24
528 ; hscrollstep = 24
529 ; maxhfit = true
530 ; crophack = false
531 ; autoscrollstep = 2
532 ; maxwait = None
533 ; hlinks = false
534 ; underinfo = false
535 ; interpagespace = 2
536 ; zoom = 1.0
537 ; presentation = false
538 ; angle = 0
539 ; cwinw = 900
540 ; cwinh = 900
541 ; savebmarks = true
542 ; fitmodel = FitProportional
543 ; trimmargins = false
544 ; trimfuzz = (0,0,0,0)
545 ; memlimit = 32 lsl 20
546 ; texcount = 256
547 ; sliceheight = 24
548 ; thumbw = 76
549 ; jumpback = true
550 ; bgcolor = (0.5, 0.5, 0.5)
551 ; bedefault = false
552 ; tilew = 2048
553 ; tileh = 2048
554 ; mustoresize = 256 lsl 20
555 ; checkers = true
556 ; aalevel = 8
557 ; urilauncher =
558 (match platform with
559 | Plinux | Psun | Pbsd -> "xdg-open \"%s\""
560 | Posx -> "open \"%s\""
561 | Pcygwin -> "cygstart \"%s\""
562 | Punknown -> "echo %s")
563 ; pathlauncher = "lp \"%s\""
564 ; selcmd =
565 (match platform with
566 | Plinux | Pbsd | Psun -> "xsel -i"
567 | Posx -> "pbcopy"
568 | Pcygwin -> "wsel"
569 | Punknown -> "cat")
570 ; paxcmd = "cat"
571 ; passcmd = E.s
572 ; savecmd = E.s
573 ; colorspace = Rgb
574 ; invert = false
575 ; colorscale = 1.0
576 ; redirectstderr = false
577 ; ghyllscroll = None
578 ; columns = Csingle [||]
579 ; beyecolumns = None
580 ; updatecurs = false
581 ; hfsize = 12
582 ; pgscale = 1.0
583 ; usepbo = false
584 ; wheelbypage = false
585 ; stcmd = "echo SyncTex"
586 ; riani = false
587 ; pax = None
588 ; paxmark = Mark_word
589 ; leftscroll = false
590 ; title = E.s
591 ; lastvisit = 0.0
592 ; keyhashes =
593 let mk n = (n, Hashtbl.create 1) in
594 [ mk "global"
595 ; mk "info"
596 ; mk "help"
597 ; mk "outline"
598 ; mk "listview"
599 ; mk "birdseye"
600 ; mk "textentry"
601 ; mk "links"
602 ; mk "view"
607 let conf = { defconf with angle = defconf.angle };;
609 let gotouri uri =
610 if emptystr conf.urilauncher
611 then (print_endline uri; -1)
612 else (
613 let url = geturl uri in
614 if emptystr url
615 then (Printf.eprintf "obtained empty url from uri %S\n" uri; -1)
616 else
617 let re = Str.regexp "%s" in
618 let command = Str.global_replace re url conf.urilauncher in
619 try popen command []
620 with exn ->
621 Printf.eprintf
622 "failed to execute `%s': %s\n" command (exntos exn);
623 flush stderr;
628 let makehelp () =
629 let strings =
630 version ()
631 :: "(searching in this text works just by typing (i.e. no initial '/'))"
632 :: E.s :: Help.keys
634 Array.of_list (
635 List.map (fun s ->
636 let url = geturl s in
637 if nonemptystr url
638 then (s, 0, Action (fun u -> addpid @@ gotouri url; u))
639 else (s, 0, Noaction)
640 ) strings);
643 let cbnew n v =
644 { store = Array.make n v
645 ; rc = 0
646 ; wc = 0
647 ; len = 0
651 let cbcap b = Array.length b.store;;
653 let cbput b v =
654 let cap = cbcap b in
655 b.store.(b.wc) <- v;
656 b.wc <- (b.wc + 1) mod cap;
657 b.rc <- b.wc;
658 b.len <- min (b.len + 1) cap;
661 let cbempty b = b.len = 0;;
663 let cbgetg b circular dir =
664 if cbempty b
665 then b.store.(0)
666 else
667 let rc = b.rc + dir in
668 let rc =
669 if circular
670 then (
671 if rc = -1
672 then b.len-1
673 else (
674 if rc >= b.len
675 then 0
676 else rc
679 else bound rc 0 (b.len-1)
681 b.rc <- rc;
682 b.store.(rc);
685 let cbget b = cbgetg b false;;
686 let cbgetc b = cbgetg b true;;
688 let state =
689 { ss = Unix.stdin
690 ; wsfd = Unix.stdin
691 ; errfd = None
692 ; stderr = Unix.stderr
693 ; errmsgs = Buffer.create 0
694 ; newerrmsgs = false
695 ; x = 0
696 ; y = 0
697 ; w = 0
698 ; anchor = emptyanchor
699 ; ranchors = []
700 ; layout = []
701 ; maxy = max_int
702 ; tilelru = Queue.create ()
703 ; pagemap = Hashtbl.create 10
704 ; tilemap = Hashtbl.create 10
705 ; pdims = []
706 ; pagecount = 0
707 ; currently = Idle
708 ; mstate = Mnone
709 ; rects = []
710 ; rects1 = []
711 ; text = E.s
712 ; mode = View
713 ; winstate = []
714 ; searchpattern = E.s
715 ; outlines = E.a
716 ; bookmarks = []
717 ; path = E.s
718 ; password = E.s
719 ; nameddest = E.s
720 ; geomcmds = firstgeomcmds
721 ; hists =
722 { nav = cbnew 10 emptyanchor
723 ; pat = cbnew 10 E.s
724 ; pag = cbnew 10 E.s
725 ; sel = cbnew 10 E.s
727 ; memused = 0
728 ; gen = 0
729 ; throttle = None
730 ; autoscroll = None
731 ; ghyll = noghyll
732 ; help = makehelp ()
733 ; docinfo = []
734 ; checkerstexid = None
735 ; prevzoom = (1.0, 0)
736 ; progress = -1.0
737 ; uioh = nouioh
738 ; redisplay = true
739 ; mpos = (-1, -1)
740 ; keystate = KSnone
741 ; glinks = false
742 ; prevcolumns = None
743 ; winw = -1
744 ; winh = -1
745 ; reprf = noreprf
746 ; origin = E.s
747 ; roam = noroam
748 ; bzoom = false
749 ; traw = Raw.create_static `float ~len:8
750 ; vraw = Raw.create_static `float ~len:8
754 let copykeyhashes c =
755 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
758 let calcips h =
759 let d = state.winh - h in
760 max conf.interpagespace ((d + 1) / 2)
763 let rowyh (c, coverA, coverB) b n =
764 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
765 then
766 let _, _, vy, (_, _, h, _) = b.(n) in
767 (vy, h)
768 else
769 let n' = n - coverA in
770 let d = n' mod c in
771 let s = n - d in
772 let e = min state.pagecount (s + c) in
773 let rec find m miny maxh = if m = e then miny, maxh else
774 let _, _, y, (_, _, h, _) = b.(m) in
775 let miny = min miny y in
776 let maxh = max maxh h in
777 find (m+1) miny maxh
778 in find s max_int 0
781 let page_of_y y =
782 let ((c, coverA, coverB) as cl), b =
783 match conf.columns with
784 | Csingle b -> (1, 0, 0), b
785 | Cmulti (c, b) -> c, b
786 | Csplit (_, b) -> (1, 0, 0), b
788 if Array.length b = 0
789 then -1
790 else
791 let rec bsearch nmin nmax =
792 if nmin > nmax
793 then bound nmin 0 (state.pagecount-1)
794 else
795 let n = (nmax + nmin) / 2 in
796 let vy, h = rowyh cl b n in
797 let y0, y1 =
798 if conf.presentation
799 then
800 let ips = calcips h in
801 let y0 = vy - ips in
802 let y1 = vy + h + ips in
803 y0, y1
804 else (
805 if n = 0
806 then 0, vy + h + conf.interpagespace
807 else
808 let y0 = vy - conf.interpagespace in
809 y0, y0 + h + conf.interpagespace
812 if y >= y0 && y < y1
813 then (
814 if c = 1
815 then n
816 else (
817 if n > coverA
818 then
819 if n < state.pagecount - coverB
820 then ((n-coverA)/c)*c + coverA
821 else n
822 else n
825 else (
826 if y > y0
827 then bsearch (n+1) nmax
828 else bsearch nmin (n-1)
831 bsearch 0 (state.pagecount-1);
834 let calcheight () =
835 match conf.columns with
836 | Cmulti ((_, _, _) as cl, b) ->
837 if Array.length b > 0
838 then
839 let y, h = rowyh cl b (Array.length b - 1) in
840 y + h + (if conf.presentation then calcips h else 0)
841 else 0
842 | Csingle b ->
843 if Array.length b > 0
844 then
845 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
846 y + h + (if conf.presentation then calcips h else 0)
847 else 0
848 | Csplit (_, b) ->
849 if Array.length b > 0
850 then
851 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
852 y + h
853 else 0
856 let getpageywh pageno =
857 let pageno = bound pageno 0 (state.pagecount-1) in
858 match conf.columns with
859 | Csingle b ->
860 if Array.length b = 0
861 then 0, 0, 0
862 else
863 let (_, _, y, (_, w, h, _)) = b.(pageno) in
864 let y =
865 if conf.presentation
866 then y - calcips h
867 else y
869 y, w, h
870 | Cmulti (cl, b) ->
871 if Array.length b = 0
872 then 0, 0, 0
873 else
874 let y, h = rowyh cl b pageno in
875 let (_, _, _, (_, w, _, _)) = b.(pageno) in
876 let y =
877 if conf.presentation
878 then y - calcips h
879 else y
881 y, w, h
882 | Csplit (c, b) ->
883 if Array.length b = 0
884 then 0, 0, 0
885 else
886 let n = pageno*c in
887 let (_, _, y, (_, w, h, _)) = b.(n) in
888 y, w / c, h
891 let getpageyh pageno =
892 let y,_,h = getpageywh pageno in
893 y, h;
896 let getpagedim pageno =
897 let rec f ppdim l =
898 match l with
899 | (n, _, _, _) as pdim :: rest ->
900 if n >= pageno
901 then (if n = pageno then pdim else ppdim)
902 else f pdim rest
904 | [] -> ppdim
906 f (-1, -1, -1, -1) state.pdims
909 let getpagey pageno = fst (getpageyh pageno);;
911 let getanchor1 l =
912 let top =
913 let coloff = l.pagecol * l.pageh in
914 float (l.pagey + coloff) /. float l.pageh
916 let dtop =
917 if l.pagedispy = 0
918 then
920 else (
921 if conf.presentation
922 then float l.pagedispy /. float (calcips l.pageh)
923 else float l.pagedispy /. float conf.interpagespace
926 (l.pageno, top, dtop)
929 let getanchor () =
930 match state.layout with
931 | l :: _ -> getanchor1 l
932 | [] ->
933 let n = page_of_y state.y in
934 if n = -1
935 then state.anchor
936 else
937 let y, h = getpageyh n in
938 let dy = y - state.y in
939 let dtop =
940 if conf.presentation
941 then
942 let ips = calcips h in
943 float (dy + ips) /. float ips
944 else
945 float dy /. float conf.interpagespace
947 (n, 0.0, dtop)
950 let fontpath = ref E.s;;
952 type historder = [ `lastvisit | `title | `path | `file ];;
953 let historder : historder ref = ref `lastvisit;;
955 module KeyMap =
956 Map.Make (struct type t = (int * int) let compare = compare end);;
958 open Parser;;
960 let unent s =
961 let l = String.length s in
962 let b = Buffer.create l in
963 unent b s 0 l;
964 Buffer.contents b;
967 let home =
968 try Sys.getenv "HOME"
969 with exn ->
970 prerr_endline
971 ("Can not determine home directory location: " ^ exntos exn);
975 let modifier_of_string = function
976 | "alt" -> Wsi.altmask
977 | "shift" -> Wsi.shiftmask
978 | "ctrl" | "control" -> Wsi.ctrlmask
979 | "meta" -> Wsi.metamask
980 | _ -> 0
983 let key_of_string =
984 let r = Str.regexp "-" in
985 fun s ->
986 let elems = Str.full_split r s in
987 let f n k m =
988 let g s =
989 let m1 = modifier_of_string s in
990 if m1 = 0
991 then (Wsi.namekey s, m)
992 else (k, m lor m1)
993 in function
994 | Str.Delim s when n land 1 = 0 -> g s
995 | Str.Text s -> g s
996 | Str.Delim _ -> (k, m)
998 let rec loop n k m = function
999 | [] -> (k, m)
1000 | x :: xs ->
1001 let k, m = f n k m x in
1002 loop (n+1) k m xs
1004 loop 0 0 0 elems
1007 let keys_of_string =
1008 let r = Str.regexp "[ \t]" in
1009 fun s ->
1010 let elems = Str.split r s in
1011 List.map key_of_string elems
1014 let config_of c attrs =
1015 let apply c k v =
1017 match k with
1018 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
1019 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
1020 | "case-insensitive-search" -> { c with icase = bool_of_string v }
1021 | "preload" -> { c with preload = bool_of_string v }
1022 | "page-bias" -> { c with pagebias = int_of_string v }
1023 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
1024 | "horizontal-scroll-step" ->
1025 { c with hscrollstep = max (int_of_string v) 1 }
1026 | "auto-scroll-step" ->
1027 { c with autoscrollstep = max 0 (int_of_string v) }
1028 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
1029 | "crop-hack" -> { c with crophack = bool_of_string v }
1030 | "throttle" ->
1031 let mw =
1032 match String.lowercase v with
1033 | "true" -> Some infinity
1034 | "false" -> None
1035 | f -> Some (float_of_string f)
1037 { c with maxwait = mw}
1038 | "highlight-links" -> { c with hlinks = bool_of_string v }
1039 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
1040 | "vertical-margin" ->
1041 { c with interpagespace = max 0 (int_of_string v) }
1042 | "zoom" ->
1043 let zoom = float_of_string v /. 100. in
1044 let zoom = max zoom 0.0 in
1045 { c with zoom = zoom }
1046 | "presentation" -> { c with presentation = bool_of_string v }
1047 | "rotation-angle" -> { c with angle = int_of_string v }
1048 | "width" -> { c with cwinw = max 20 (int_of_string v) }
1049 | "height" -> { c with cwinh = max 20 (int_of_string v) }
1050 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
1051 | "proportional-display" ->
1052 let fm =
1053 if bool_of_string v
1054 then FitProportional
1055 else FitWidth
1057 { c with fitmodel = fm }
1058 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
1059 | "pixmap-cache-size" ->
1060 { c with memlimit = max 2 (int_of_string_with_suffix v) }
1061 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
1062 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
1063 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
1064 | "persistent-location" -> { c with jumpback = bool_of_string v }
1065 | "background-color" -> { c with bgcolor = color_of_string v }
1066 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
1067 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
1068 | "mupdf-store-size" ->
1069 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
1070 | "checkers" -> { c with checkers = bool_of_string v }
1071 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
1072 | "trim-margins" -> { c with trimmargins = bool_of_string v }
1073 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
1074 | "uri-launcher" -> { c with urilauncher = unent v }
1075 | "path-launcher" -> { c with pathlauncher = unent v }
1076 | "color-space" -> { c with colorspace = CSTE.of_string v }
1077 | "invert-colors" -> { c with invert = bool_of_string v }
1078 | "brightness" -> { c with colorscale = float_of_string v }
1079 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
1080 | "ghyllscroll" -> { c with ghyllscroll = ghyllscroll_of_string v }
1081 | "columns" ->
1082 let (n, _, _) as nab = multicolumns_of_string v in
1083 if n < 0
1084 then { c with columns = Csplit (-n, E.a) }
1085 else { c with columns = Cmulti (nab, E.a) }
1086 | "birds-eye-columns" ->
1087 { c with beyecolumns = Some (max (int_of_string v) 2) }
1088 | "selection-command" -> { c with selcmd = unent v }
1089 | "synctex-command" -> { c with stcmd = unent v }
1090 | "pax-command" -> { c with paxcmd = unent v }
1091 | "askpass-command" -> { c with passcmd = unent v }
1092 | "savepath-command" -> { c with savecmd = unent v }
1093 | "update-cursor" -> { c with updatecurs = bool_of_string v }
1094 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
1095 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
1096 | "use-pbo" -> { c with usepbo = bool_of_string v }
1097 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
1098 | "horizontal-scrollbar-visible" ->
1099 let b =
1100 if bool_of_string v
1101 then c.scrollb lor scrollbhv
1102 else c.scrollb land (lnot scrollbhv)
1104 { c with scrollb = b }
1105 | "vertical-scrollbar-visible" ->
1106 let b =
1107 if bool_of_string v
1108 then c.scrollb lor scrollbvv
1109 else c.scrollb land (lnot scrollbvv)
1111 { c with scrollb = b }
1112 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
1113 | "point-and-x" ->
1114 { c with pax =
1115 if bool_of_string v
1116 then Some (ref (0.0, 0, 0))
1117 else None }
1118 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
1119 | "scroll-bar-on-the-left" -> { c with leftscroll = bool_of_string v }
1120 | "title" -> { c with title = unent v }
1121 | "last-visit" -> { c with lastvisit = float_of_string v }
1122 | _ -> c
1123 with exn ->
1124 prerr_endline ("Error processing attribute (`" ^
1125 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
1128 let rec fold c = function
1129 | [] -> c
1130 | (k, v) :: rest ->
1131 let c = apply c k v in
1132 fold c rest
1134 fold { c with keyhashes = copykeyhashes c } attrs;
1137 let fromstring f pos n v d =
1138 try f v
1139 with exn ->
1140 dolog "Error processing attribute (%S=%S) at %d\n%s"
1141 n v pos (exntos exn)
1146 let bookmark_of attrs =
1147 let rec fold title page rely visy = function
1148 | ("title", v) :: rest -> fold v page rely visy rest
1149 | ("page", v) :: rest -> fold title v rely visy rest
1150 | ("rely", v) :: rest -> fold title page v visy rest
1151 | ("visy", v) :: rest -> fold title page rely v rest
1152 | _ :: rest -> fold title page rely visy rest
1153 | [] -> title, page, rely, visy
1155 fold "invalid" "0" "0" "0" attrs
1158 let doc_of attrs =
1159 let rec fold path page rely pan visy = function
1160 | ("path", v) :: rest -> fold v page rely pan visy rest
1161 | ("page", v) :: rest -> fold path v rely pan visy rest
1162 | ("rely", v) :: rest -> fold path page v pan visy rest
1163 | ("pan", v) :: rest -> fold path page rely v visy rest
1164 | ("visy", v) :: rest -> fold path page rely pan v rest
1165 | _ :: rest -> fold path page rely pan visy rest
1166 | [] -> path, page, rely, pan, visy
1168 fold E.s "0" "0" "0" "0" attrs
1171 let map_of attrs =
1172 let rec fold rs ls = function
1173 | ("out", v) :: rest -> fold v ls rest
1174 | ("in", v) :: rest -> fold rs v rest
1175 | _ :: rest -> fold ls rs rest
1176 | [] -> ls, rs
1178 fold E.s E.s attrs
1181 let setconf dst src =
1182 dst.scrollbw <- src.scrollbw;
1183 dst.scrollh <- src.scrollh;
1184 dst.icase <- src.icase;
1185 dst.preload <- src.preload;
1186 dst.pagebias <- src.pagebias;
1187 dst.verbose <- src.verbose;
1188 dst.scrollstep <- src.scrollstep;
1189 dst.maxhfit <- src.maxhfit;
1190 dst.crophack <- src.crophack;
1191 dst.autoscrollstep <- src.autoscrollstep;
1192 dst.maxwait <- src.maxwait;
1193 dst.hlinks <- src.hlinks;
1194 dst.underinfo <- src.underinfo;
1195 dst.interpagespace <- src.interpagespace;
1196 dst.zoom <- src.zoom;
1197 dst.presentation <- src.presentation;
1198 dst.angle <- src.angle;
1199 dst.cwinw <- src.cwinw;
1200 dst.cwinh <- src.cwinh;
1201 dst.savebmarks <- src.savebmarks;
1202 dst.memlimit <- src.memlimit;
1203 dst.fitmodel <- src.fitmodel;
1204 dst.texcount <- src.texcount;
1205 dst.sliceheight <- src.sliceheight;
1206 dst.thumbw <- src.thumbw;
1207 dst.jumpback <- src.jumpback;
1208 dst.bgcolor <- src.bgcolor;
1209 dst.tilew <- src.tilew;
1210 dst.tileh <- src.tileh;
1211 dst.mustoresize <- src.mustoresize;
1212 dst.checkers <- src.checkers;
1213 dst.aalevel <- src.aalevel;
1214 dst.trimmargins <- src.trimmargins;
1215 dst.trimfuzz <- src.trimfuzz;
1216 dst.urilauncher <- src.urilauncher;
1217 dst.colorspace <- src.colorspace;
1218 dst.invert <- src.invert;
1219 dst.colorscale <- src.colorscale;
1220 dst.redirectstderr <- src.redirectstderr;
1221 dst.ghyllscroll <- src.ghyllscroll;
1222 dst.columns <- src.columns;
1223 dst.beyecolumns <- src.beyecolumns;
1224 dst.selcmd <- src.selcmd;
1225 dst.updatecurs <- src.updatecurs;
1226 dst.pathlauncher <- src.pathlauncher;
1227 dst.keyhashes <- copykeyhashes src;
1228 dst.hfsize <- src.hfsize;
1229 dst.hscrollstep <- src.hscrollstep;
1230 dst.pgscale <- src.pgscale;
1231 dst.usepbo <- src.usepbo;
1232 dst.wheelbypage <- src.wheelbypage;
1233 dst.stcmd <- src.stcmd;
1234 dst.paxcmd <- src.paxcmd;
1235 dst.passcmd <- src.passcmd;
1236 dst.savecmd <- src.savecmd;
1237 dst.scrollb <- src.scrollb;
1238 dst.riani <- src.riani;
1239 dst.paxmark <- src.paxmark;
1240 dst.leftscroll <- src.leftscroll;
1241 dst.title <- src.title;
1242 dst.pax <-
1243 if src.pax = None
1244 then None
1245 else Some ((ref (0.0, 0, 0)));
1248 let findkeyhash c name =
1249 try List.assoc name c.keyhashes
1250 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
1253 let get s =
1254 let h = Hashtbl.create 10 in
1255 let dc = { defconf with angle = defconf.angle } in
1256 let rec toplevel v t spos _ =
1257 match t with
1258 | Vdata | Vcdata | Vend -> v
1259 | Vopen ("llppconfig", _, closed) ->
1260 if closed
1261 then v
1262 else { v with f = llppconfig }
1263 | Vopen _ ->
1264 error "unexpected subelement at top level" s spos
1265 | Vclose _ -> error "unexpected close at top level" s spos
1267 and llppconfig v t spos _ =
1268 match t with
1269 | Vdata | Vcdata -> v
1270 | Vend -> error "unexpected end of input in llppconfig" s spos
1271 | Vopen ("defaults", attrs, closed) ->
1272 let c = config_of dc attrs in
1273 setconf dc c;
1274 if closed
1275 then v
1276 else { v with f = defaults }
1278 | Vopen ("ui-font", attrs, closed) ->
1279 let rec getsize size = function
1280 | [] -> size
1281 | ("size", v) :: rest ->
1282 let size =
1283 fromstring int_of_string spos "size" v fstate.fontsize in
1284 getsize size rest
1285 | l -> getsize size l
1287 fstate.fontsize <- getsize fstate.fontsize attrs;
1288 if closed
1289 then v
1290 else { v with f = uifont (Buffer.create 10) }
1292 | Vopen ("doc", attrs, closed) ->
1293 let pathent, spage, srely, span, svisy = doc_of attrs in
1294 let path = unent pathent
1295 and pageno = fromstring int_of_string spos "page" spage 0
1296 and rely = fromstring float_of_string spos "rely" srely 0.0
1297 and pan = fromstring int_of_string spos "pan" span 0
1298 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1299 let c = config_of dc attrs in
1300 let anchor = (pageno, rely, visy) in
1301 if closed
1302 then (Hashtbl.add h path (c, [], pan, anchor); v)
1303 else { v with f = doc path pan anchor c [] }
1305 | Vopen _ ->
1306 error "unexpected subelement in llppconfig" s spos
1308 | Vclose "llppconfig" -> { v with f = toplevel }
1309 | Vclose _ -> error "unexpected close in llppconfig" s spos
1311 and defaults v t spos _ =
1312 match t with
1313 | Vdata | Vcdata -> v
1314 | Vend -> error "unexpected end of input in defaults" s spos
1315 | Vopen ("keymap", attrs, closed) ->
1316 let modename =
1317 try List.assoc "mode" attrs
1318 with Not_found -> "global" in
1319 if closed
1320 then v
1321 else
1322 let ret keymap =
1323 let h = findkeyhash dc modename in
1324 KeyMap.iter (Hashtbl.replace h) keymap;
1325 defaults
1327 { v with f = pkeymap ret KeyMap.empty }
1329 | Vopen (_, _, _) ->
1330 error "unexpected subelement in defaults" s spos
1332 | Vclose "defaults" ->
1333 { v with f = llppconfig }
1335 | Vclose _ -> error "unexpected close in defaults" s spos
1337 and uifont b v t spos epos =
1338 match t with
1339 | Vdata | Vcdata ->
1340 Buffer.add_substring b s spos (epos - spos);
1342 | Vopen (_, _, _) ->
1343 error "unexpected subelement in ui-font" s spos
1344 | Vclose "ui-font" ->
1345 if emptystr !fontpath
1346 then fontpath := Buffer.contents b;
1347 { v with f = llppconfig }
1348 | Vclose _ -> error "unexpected close in ui-font" s spos
1349 | Vend -> error "unexpected end of input in ui-font" s spos
1351 and doc path pan anchor c bookmarks v t spos _ =
1352 match t with
1353 | Vdata | Vcdata -> v
1354 | Vend -> error "unexpected end of input in doc" s spos
1355 | Vopen ("bookmarks", _, closed) ->
1356 if closed
1357 then v
1358 else { v with f = pbookmarks path pan anchor c bookmarks }
1360 | Vopen ("keymap", attrs, closed) ->
1361 let modename =
1362 try List.assoc "mode" attrs
1363 with Not_found -> "global"
1365 if closed
1366 then v
1367 else
1368 let ret keymap =
1369 let h = findkeyhash c modename in
1370 KeyMap.iter (Hashtbl.replace h) keymap;
1371 doc path pan anchor c bookmarks
1373 { v with f = pkeymap ret KeyMap.empty }
1375 | Vopen (_, _, _) ->
1376 error "unexpected subelement in doc" s spos
1378 | Vclose "doc" ->
1379 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
1380 { v with f = llppconfig }
1382 | Vclose _ -> error "unexpected close in doc" s spos
1384 and pkeymap ret keymap v t spos _ =
1385 match t with
1386 | Vdata | Vcdata -> v
1387 | Vend -> error "unexpected end of input in keymap" s spos
1388 | Vopen ("map", attrs, closed) ->
1389 let r, l = map_of attrs in
1390 let kss = fromstring keys_of_string spos "in" r [] in
1391 let lss = fromstring keys_of_string spos "out" l [] in
1392 let keymap =
1393 match kss with
1394 | [] -> keymap
1395 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
1396 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
1398 if closed
1399 then { v with f = pkeymap ret keymap }
1400 else
1401 let f () = v in
1402 { v with f = skip "map" f }
1404 | Vopen _ ->
1405 error "unexpected subelement in keymap" s spos
1407 | Vclose "keymap" ->
1408 { v with f = ret keymap }
1410 | Vclose _ -> error "unexpected close in keymap" s spos
1412 and pbookmarks path pan anchor c bookmarks v t spos _ =
1413 match t with
1414 | Vdata | Vcdata -> v
1415 | Vend -> error "unexpected end of input in bookmarks" s spos
1416 | Vopen ("item", attrs, closed) ->
1417 let titleent, spage, srely, svisy = bookmark_of attrs in
1418 let page = fromstring int_of_string spos "page" spage 0
1419 and rely = fromstring float_of_string spos "rely" srely 0.0
1420 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1421 let bookmarks =
1422 (unent titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
1424 if closed
1425 then { v with f = pbookmarks path pan anchor c bookmarks }
1426 else
1427 let f () = v in
1428 { v with f = skip "item" f }
1430 | Vopen _ ->
1431 error "unexpected subelement in bookmarks" s spos
1433 | Vclose "bookmarks" ->
1434 { v with f = doc path pan anchor c bookmarks }
1436 | Vclose _ -> Parser.parse_error "unexpected close in bookmarks" s spos
1438 and skip tag f v t spos _ =
1439 match t with
1440 | Vdata | Vcdata -> v
1441 | Vend ->
1442 Parser.parse_error ("unexpected end of input in skipped " ^ tag) s spos
1443 | Vopen (tag', _, closed) ->
1444 if closed
1445 then v
1446 else
1447 let f' () = { v with f = skip tag f } in
1448 { v with f = skip tag' f' }
1449 | Vclose ctag ->
1450 if tag = ctag
1451 then f ()
1452 else Parser.parse_error ("unexpected close in skipped " ^ tag) s spos
1455 parse { f = toplevel; accu = () } s;
1456 h, dc;
1459 let do_load f ic =
1461 let len = in_channel_length ic in
1462 let s = really_input_string ic len in
1463 f s;
1464 with
1465 | Parse_error (msg, s, pos) ->
1466 let subs = subs s pos in
1467 Utils.error "parse error: %s: at %d [..%s..]" msg pos subs
1469 | exn ->
1470 failwith ("config load error: " ^ exntos exn)
1473 let defconfpath =
1474 let dir =
1475 let xdgconfdir = Utils.getenvwithdef "XDG_CONFIG_HOME" E.s in
1476 if xdgconfdir == E.s
1477 then
1479 let dir = Filename.concat home ".config" in
1480 if Sys.is_directory dir then dir else home
1481 with _ -> home
1482 else xdgconfdir
1484 Filename.concat dir "llpp.conf"
1487 let confpath = ref defconfpath;;
1489 let load1 f =
1490 if Sys.file_exists !confpath
1491 then
1492 match
1493 (try Some (open_in_bin !confpath)
1494 with exn ->
1495 prerr_endline
1496 ("Error opening configuration file `" ^ !confpath ^ "': " ^
1497 exntos exn);
1498 None
1500 with
1501 | Some ic ->
1502 let success =
1504 f (do_load get ic)
1505 with exn ->
1506 prerr_endline
1507 ("Error loading configuration from `" ^ !confpath ^ "': " ^
1508 exntos exn);
1509 false
1511 close_in ic;
1512 success
1514 | None -> false
1515 else
1516 f (Hashtbl.create 0, defconf)
1519 let load openlast =
1520 let f (h, dc) =
1521 if openlast
1522 then (
1523 let path, _ =
1524 Hashtbl.fold
1525 (fun path (conf, _, _, _) ((_, besttime) as best) ->
1526 if conf.lastvisit > besttime
1527 then (path, conf.lastvisit)
1528 else best)
1530 (state.path, -.infinity)
1532 state.path <- path;
1534 let pc, pb, px, pa =
1536 let path =
1537 if emptystr state.origin
1538 then state.path
1539 else state.origin
1541 let absname = abspath path in
1542 Hashtbl.find h absname
1543 with Not_found -> dc, [], 0, emptyanchor
1545 setconf defconf dc;
1546 setconf conf pc;
1547 state.bookmarks <- pb;
1548 state.x <- px;
1549 if conf.jumpback
1550 then state.anchor <- pa;
1551 cbput state.hists.nav pa;
1552 true
1554 load1 f
1557 let gethist listref =
1558 let f (h, _) =
1559 listref :=
1560 Hashtbl.fold (fun path (pc, pb, px, pa) accu ->
1561 (path, pc, pb, px, pa) :: accu)
1562 h [];
1563 true
1565 load1 f
1568 let add_attrs bb always dc c time =
1569 let ob s a b =
1570 if always || a != b
1571 then Printf.bprintf bb "\n %s='%b'" s a
1572 and op s a b =
1573 if always || a <> b
1574 then Printf.bprintf bb "\n %s='%b'" s (a != None)
1575 and oi s a b =
1576 if always || a != b
1577 then Printf.bprintf bb "\n %s='%d'" s a
1578 and oI s a b =
1579 if always || a != b
1580 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
1581 and oz s a b =
1582 if always || a <> b
1583 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
1584 and oF s a b =
1585 if always || a <> b
1586 then Printf.bprintf bb "\n %s='%f'" s a
1587 and oc s a b =
1588 if always || a <> b
1589 then
1590 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
1591 and oC s a b =
1592 if always || a <> b
1593 then
1594 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
1595 and oR s a b =
1596 if always || a <> b
1597 then
1598 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
1599 and os s a b =
1600 if always || a <> b
1601 then
1602 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
1603 and og s a b =
1604 if always || a <> b
1605 then
1606 match a with
1607 | Some (_N, _A, _B) ->
1608 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
1609 | None ->
1610 match b with
1611 | None -> ()
1612 | _ ->
1613 Printf.bprintf bb "\n %s='none'" s
1614 and oW s a b =
1615 if always || a <> b
1616 then
1617 let v =
1618 match a with
1619 | None -> "false"
1620 | Some f ->
1621 if f = infinity
1622 then "true"
1623 else string_of_float f
1625 Printf.bprintf bb "\n %s='%s'" s v
1626 and oco s a b =
1627 if always || a <> b
1628 then
1629 match a with
1630 | Cmulti ((n, a, b), _) when n > 1 ->
1631 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
1632 | Csplit (n, _) when n > 1 ->
1633 Printf.bprintf bb "\n %s='%d'" s ~-n
1634 | Cmulti _ | Csplit _ | Csingle _ -> ()
1635 and obeco s a b =
1636 if always || a <> b
1637 then
1638 match a with
1639 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
1640 | _ -> ()
1641 and oFm s a b =
1642 if always || a <> b
1643 then
1644 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
1645 and oSv s a b m =
1646 if always || a <> b
1647 then
1648 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
1649 and oPm s a b =
1650 if always || a <> b
1651 then
1652 Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
1654 oi "width" c.cwinw dc.cwinw;
1655 oi "height" c.cwinh dc.cwinh;
1656 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
1657 oi "scroll-handle-height" c.scrollh dc.scrollh;
1658 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
1659 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
1660 ob "case-insensitive-search" c.icase dc.icase;
1661 ob "preload" c.preload dc.preload;
1662 oi "page-bias" c.pagebias dc.pagebias;
1663 oi "scroll-step" c.scrollstep dc.scrollstep;
1664 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
1665 ob "max-height-fit" c.maxhfit dc.maxhfit;
1666 ob "crop-hack" c.crophack dc.crophack;
1667 oW "throttle" c.maxwait dc.maxwait;
1668 ob "highlight-links" c.hlinks dc.hlinks;
1669 ob "under-cursor-info" c.underinfo dc.underinfo;
1670 oi "vertical-margin" c.interpagespace dc.interpagespace;
1671 oz "zoom" c.zoom dc.zoom;
1672 ob "presentation" c.presentation dc.presentation;
1673 oi "rotation-angle" c.angle dc.angle;
1674 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
1675 oFm "fit-model" c.fitmodel dc.fitmodel;
1676 oI "pixmap-cache-size" c.memlimit dc.memlimit;
1677 oi "tex-count" c.texcount dc.texcount;
1678 oi "slice-height" c.sliceheight dc.sliceheight;
1679 oi "thumbnail-width" c.thumbw dc.thumbw;
1680 ob "persistent-location" c.jumpback dc.jumpback;
1681 oc "background-color" c.bgcolor dc.bgcolor;
1682 oi "tile-width" c.tilew dc.tilew;
1683 oi "tile-height" c.tileh dc.tileh;
1684 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
1685 ob "checkers" c.checkers dc.checkers;
1686 oi "aalevel" c.aalevel dc.aalevel;
1687 ob "trim-margins" c.trimmargins dc.trimmargins;
1688 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
1689 os "uri-launcher" c.urilauncher dc.urilauncher;
1690 os "path-launcher" c.pathlauncher dc.pathlauncher;
1691 oC "color-space" c.colorspace dc.colorspace;
1692 ob "invert-colors" c.invert dc.invert;
1693 oF "brightness" c.colorscale dc.colorscale;
1694 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
1695 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
1696 oco "columns" c.columns dc.columns;
1697 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
1698 os "selection-command" c.selcmd dc.selcmd;
1699 os "synctex-command" c.stcmd dc.stcmd;
1700 os "pax-command" c.paxcmd dc.paxcmd;
1701 os "askpass-command" c.passcmd dc.passcmd;
1702 os "savepath-command" c.savecmd dc.savecmd;
1703 ob "update-cursor" c.updatecurs dc.updatecurs;
1704 oi "hint-font-size" c.hfsize dc.hfsize;
1705 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
1706 oF "page-scroll-scale" c.pgscale dc.pgscale;
1707 ob "use-pbo" c.usepbo dc.usepbo;
1708 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
1709 ob "remote-in-a-new-instance" c.riani dc.riani;
1710 op "point-and-x" c.pax dc.pax;
1711 oPm "point-and-x-mark" c.paxmark dc.paxmark;
1712 ob "scroll-bar-on-the-left" c.leftscroll dc.leftscroll;
1713 if not always
1714 then os "title" c.title dc.title;
1715 oF "last-visit" (snd (modf time)) 0.0;
1718 let keymapsbuf always dc c =
1719 let bb = Buffer.create 16 in
1720 let rec loop = function
1721 | [] -> ()
1722 | (modename, h) :: rest ->
1723 let dh = findkeyhash dc modename in
1724 if always || h <> dh
1725 then (
1726 if Hashtbl.length h > 0
1727 then (
1728 if Buffer.length bb > 0
1729 then Buffer.add_char bb '\n';
1730 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
1731 Hashtbl.iter (fun i o ->
1732 let isdifferent = always ||
1734 let dO = Hashtbl.find dh i in
1735 dO <> o
1736 with Not_found -> true
1738 if isdifferent
1739 then
1740 let addkm (k, m) =
1741 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
1742 if Wsi.withalt m then Buffer.add_string bb "alt-";
1743 if Wsi.withshift m then Buffer.add_string bb "shift-";
1744 if Wsi.withmeta m then Buffer.add_string bb "meta-";
1745 Buffer.add_string bb (Wsi.keyname k);
1747 let addkms l =
1748 let rec loop = function
1749 | [] -> ()
1750 | km :: [] -> addkm km
1751 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
1753 loop l
1755 Buffer.add_string bb "<map in='";
1756 addkm i;
1757 match o with
1758 | KMinsrt km ->
1759 Buffer.add_string bb "' out='";
1760 addkm km;
1761 Buffer.add_string bb "'/>\n"
1763 | KMinsrl kms ->
1764 Buffer.add_string bb "' out='";
1765 addkms kms;
1766 Buffer.add_string bb "'/>\n"
1768 | KMmulti (ins, kms) ->
1769 Buffer.add_char bb ' ';
1770 addkms ins;
1771 Buffer.add_string bb "' out='";
1772 addkms kms;
1773 Buffer.add_string bb "'/>\n"
1774 ) h;
1775 Buffer.add_string bb "</keymap>";
1778 loop rest
1780 loop c.keyhashes;
1784 let save1 bb leavebirdseye x h dc =
1785 let uifontsize = fstate.fontsize in
1786 let dc = if conf.bedefault then conf else dc in
1787 Buffer.add_string bb "<llppconfig>\n";
1789 if nonemptystr !fontpath
1790 then
1791 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
1792 uifontsize
1793 !fontpath
1794 else (
1795 if uifontsize <> 14
1796 then
1797 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
1800 Buffer.add_string bb "<defaults";
1801 add_attrs bb true dc dc nan;
1802 let kb = keymapsbuf true dc dc in
1803 if Buffer.length kb > 0
1804 then (
1805 Buffer.add_string bb ">\n";
1806 Buffer.add_buffer bb kb;
1807 Buffer.add_string bb "\n</defaults>\n";
1809 else Buffer.add_string bb "/>\n";
1811 let adddoc path pan anchor c bookmarks time =
1812 if bookmarks == [] && c = dc && anchor = emptyanchor
1813 then ()
1814 else (
1815 Printf.bprintf bb "<doc path='%s'"
1816 (enent path 0 (String.length path));
1818 if anchor <> emptyanchor
1819 then (
1820 let n, rely, visy = anchor in
1821 Printf.bprintf bb "\n page='%d'" n;
1822 if rely > 1e-6
1823 then
1824 Printf.bprintf bb " rely='%f'" rely
1826 if abs_float visy > 1e-6
1827 then
1828 Printf.bprintf bb " visy='%f'" visy
1832 if pan != 0
1833 then Printf.bprintf bb " pan='%d'" pan;
1835 add_attrs bb false dc c time;
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 Buffer.add_string bb "/>\n"
1847 | _ ->
1848 Buffer.add_string bb ">\n<bookmarks>\n";
1849 List.iter (fun (title, _, kind) ->
1850 begin match kind with
1851 | Oanchor (page, rely, visy) ->
1852 Printf.bprintf bb
1853 "<item title='%s' page='%d'"
1854 (enent title 0 (String.length title))
1855 page
1857 if rely > 1e-6
1858 then
1859 Printf.bprintf bb " rely='%f'" rely
1861 if abs_float visy > 1e-6
1862 then
1863 Printf.bprintf bb " visy='%f'" visy
1865 | Ohistory _ | Onone | Ouri _ | Oremote _
1866 | Oremotedest _ | Olaunch _ | Oaction _ ->
1867 failwith "unexpected link in bookmarks"
1868 end;
1869 Buffer.add_string bb "/>\n";
1870 ) bookmarks;
1871 Buffer.add_string bb "</bookmarks>";
1872 if Buffer.length kb > 0
1873 then (
1874 Buffer.add_string bb "\n";
1875 Buffer.add_buffer bb kb;
1877 Buffer.add_string bb "\n</doc>\n";
1878 end;
1882 let pan, conf =
1883 match state.mode with
1884 | Birdseye (c, pan, _, _, _) ->
1885 let beyecolumns =
1886 match conf.columns with
1887 | Cmulti ((c, _, _), _) -> Some c
1888 | Csingle _ -> None
1889 | Csplit _ -> None
1890 and columns =
1891 match c.columns with
1892 | Cmulti (c, _) -> Cmulti (c, E.a)
1893 | Csingle _ -> Csingle E.a
1894 | Csplit _ -> failwith "quit from bird's eye while split"
1896 pan, { c with beyecolumns = beyecolumns; columns = columns }
1897 | Textentry _
1898 | View
1899 | LinkNav _ -> x, conf
1901 let docpath = abspath state.path in
1902 adddoc docpath pan (getanchor ())
1904 let autoscrollstep =
1905 match state.autoscroll with
1906 | Some step -> step
1907 | None -> conf.autoscrollstep
1909 begin match state.mode with
1910 | Birdseye beye -> leavebirdseye beye true
1911 | Textentry _
1912 | View
1913 | LinkNav _ -> ()
1914 end;
1915 { conf with autoscrollstep = autoscrollstep }
1917 (if conf.savebmarks then state.bookmarks else [])
1918 (now ());
1920 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
1921 if docpath <> abspath path
1922 then adddoc path x anchor c bookmarks c.lastvisit
1923 ) h;
1924 Buffer.add_string bb "</llppconfig>\n";
1925 true;
1928 let save leavebirdseye =
1929 let relx = float state.x /. float state.winw in
1930 let w, h, x =
1931 let cx w = truncate (relx *. float w) in
1932 List.fold_left
1933 (fun (w, h, x) ws ->
1934 match ws with
1935 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
1936 | Wsi.MaxVert -> (w, conf.cwinh, x)
1937 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
1939 (state.winw, state.winh, state.x) state.winstate
1941 conf.cwinw <- w;
1942 conf.cwinh <- h;
1943 let bb = Buffer.create 32768 in
1944 let save2 (h, dc) =
1945 save1 bb leavebirdseye x h dc
1947 if load1 save2 && Buffer.length bb > 0
1948 then
1950 let tmp = !confpath ^ ".tmp" in
1951 let oc = open_out_bin tmp in
1952 Buffer.output_buffer oc bb;
1953 close_out oc;
1954 Unix.rename tmp !confpath;
1955 with exn ->
1956 prerr_endline
1957 ("error while saving configuration: " ^ exntos exn)
1960 let gc fdi fdo =
1961 let wr s n =
1962 let n' = Unix.write fdo (Bytes.of_string s) 0 n in
1963 if n != n'
1964 then Utils.error "Unix.write %d = %d" n n'
1966 let rd s n =
1967 try Unix.read fdi s 0 n
1968 with exn -> Utils.error "reading gc pipe %s" (exntos exn) in
1969 let href = ref (Hashtbl.create 0) in
1970 let cref = ref defconf in
1971 let push (h, dc) =
1972 let f path (pc, _pb, _px, _pa) =
1973 let s =
1974 Printf.sprintf "%s\000%ld\000" path (Int32.of_float pc.lastvisit)
1976 wr s (String.length s)
1978 Hashtbl.iter f h;
1979 href := h;
1980 cref := dc;
1981 Unix.shutdown fdo Unix.SHUTDOWN_SEND;
1982 true
1984 ignore (load1 push);
1985 let s =
1986 let b = Buffer.create 32768 in
1987 let s = Bytes.create 4096 in
1988 let rec f () =
1989 let n = rd s (Bytes.length s) in
1990 if n = 0
1991 then Buffer.contents b
1992 else (Buffer.add_subbytes b s 0 n; f ())
1994 f ()
1996 let rec f ppos =
1997 match String.index_from s ppos '\000' with
1998 | zpos1 ->
1999 let zpos2 =
2000 try String.index_from s (zpos1+1) '\000' with Not_found -> -1 in
2001 if zpos2 = -1
2002 then error "Incorrect gc input in (%S) at %d" s zpos1
2003 else
2004 let okey = StringLabels.sub s ~pos:ppos ~len:(zpos1-ppos) in
2005 let nkey = StringLabels.sub s ~pos:(zpos1+1) ~len:(zpos2-zpos1-1) in
2006 if emptystr nkey
2007 then (Hashtbl.remove !href okey; f (zpos2+1))
2008 else
2009 let v =
2010 try Hashtbl.find !href okey
2011 with Not_found -> Utils.error "gc: could not find %S" okey
2013 Hashtbl.remove !href okey;
2014 Hashtbl.replace !href nkey v;
2015 f (zpos2+1)
2016 | exception Not_found -> ()
2018 f 0;
2019 let bb = Buffer.create 32768 in
2020 let save2 (_h, dc) = save1 bb (fun _ _ -> ()) 0 !href dc in
2021 if load1 save2 && Buffer.length bb > 0
2022 then (
2024 let tmp = !confpath ^ ".tmp" in
2025 let oc = open_out_bin tmp in
2026 Buffer.output_buffer oc bb;
2027 close_out oc;
2028 Unix.rename tmp !confpath;
2029 with exn ->
2030 prerr_endline
2031 ("error while saving configuration: " ^ exntos exn)
2035 let logcurrently = function
2036 | Idle -> dolog "Idle"
2037 | Loading (l, gen) ->
2038 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2039 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2040 dolog
2041 "Tiling %d[%d,%d] page=%s cs=%s angle"
2042 l.pageno col row (~> pageopaque)
2043 (CSTE.to_string colorspace)
2045 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2046 angle gen conf.angle state.gen
2047 tilew tileh
2048 conf.tilew conf.tileh
2050 | Outlining _ ->
2051 dolog "outlining"