Cosmetics
[llpp.git] / config.ml
blob9d3e57f777a10c5a1237e01b29380115c3d5e16f
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 addpid = ignore;;
25 let irect_of_string s =
26 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
29 let irect_to_string (x0,y0,x1,y1) =
30 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
33 let ghyllscroll_of_string s =
34 match s with
35 | "fast" -> Some fastghyllscroll
36 | "neat" -> Some (10,1,9)
37 | "" | "none" -> None
38 | _ ->
39 let (n,a,b) as nab =
40 Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b) in
41 if n <= a || n <= b || a >= b
42 then error "invalid ghyll N(%d),A(%d),B(%d) (N <= A, A < B, N <= B)"
43 n a b;
44 Some nab
47 let ghyllscroll_to_string ((n, a, b) as nab) =
48 (**) if nab = fastghyllscroll then "fast"
49 else if nab = neatghyllscroll then "neat"
50 else Printf.sprintf "%d,%d,%d" n a b;
53 let multicolumns_to_string (n, a, b) =
54 if a = 0 && b = 0
55 then Printf.sprintf "%d" n
56 else Printf.sprintf "%d,%d,%d" n a b;
59 let multicolumns_of_string s =
60 try
61 (int_of_string s, 0, 0)
62 with _ ->
63 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
64 if a > 1 || b > 1
65 then failwith "subtly broken"; (n, a, b)
69 type keymap =
70 | KMinsrt of key
71 | KMinsrl of key list
72 | KMmulti of key list * key list
73 and key = int * int
74 and keyhash = (key, keymap) Hashtbl.t
75 and keystate =
76 | KSnone
77 | KSinto of (key list * key list)
78 and interpagespace = int
79 and multicolumns = multicol * pagegeom
80 and singlecolumn = pagegeom
81 and splitcolumns = columncount * pagegeom
82 and pagegeom = (pdimno * x * y * (pageno * width * height * leftx)) array
83 and multicol = columncount * covercount * covercount
84 and pdimno = int
85 and columncount = int
86 and covercount = int
87 and fitmodel = | FitWidth | FitProportional | FitPage
88 and trimmargins = bool
89 and irect = (int * int * int * int)
90 and memsize = int
91 and texcount = int
92 and sliceheight = int
93 and angle = int
94 and params = (angle * fitmodel * trimparams
95 * texcount * sliceheight * memsize
96 * colorspace * fontpath * trimcachepath
97 * haspbo * usefontconfig)
98 and width = int
99 and height = int
100 and leftx = int
101 and opaque = Opaque.t
102 and recttype = int
103 and pixmapsize = int
104 and gen = int
105 and top = float
106 and dtop = float
107 and fontpath = string
108 and trimcachepath = string
109 and aalevel = int
110 and trimparams = (trimmargins * irect)
111 and colorspace = | Rgb | Bgr | Gray
112 and haspbo = bool
113 and usefontconfig = 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 | Ulinkgoto of (int * int)
125 | Utext of facename
126 | Uunexpected of string
127 | Ulaunch of launchcommand
128 | Unamed of destname
129 | Uremote of (filename * pageno)
130 | Uremotedest of (filename * destname)
131 | Uannotation of (opaque * slinkindex)
132 and slinkindex = int
133 and facename = string
134 and launchcommand = string
135 and filename = string
136 and pageno = int
137 and destname = string
138 and mark =
139 | Mark_page
140 | Mark_block
141 | Mark_line
142 | Mark_word
143 and link =
144 | Lnotfound
145 | Lfound of int
146 and linkdir =
147 | LDfirst
148 | LDlast
149 | LDfirstvisible of (int * int * int)
150 | LDleft of int
151 | LDright of int
152 | LDdown of int
153 | LDup of int
154 and pagewithlinks =
155 | Pwlnotfound
156 | Pwl of int
157 and scrollb = int
158 and anchor = pageno * top * dtop
159 and rect = float * float * float * float * float * float * float * float
160 and infochange = | Memused | Docinfo | Pdim
163 class type uioh = object
164 method display : unit
165 method key : int -> int -> uioh
166 method button : int -> bool -> int -> int -> int -> uioh
167 method multiclick : int -> int -> int -> int -> uioh
168 method motion : int -> int -> uioh
169 method pmotion : int -> int -> uioh
170 method infochanged : infochange -> unit
171 method scrollpw : (int * float * float)
172 method scrollph : (int * float * float)
173 method modehash : keyhash
174 method eformsgs : bool
175 method alwaysscrolly : bool
176 end;;
178 module type TextEnumType =
180 type t
181 val name : string
182 val names : string array
183 end;;
185 module TextEnumMake (Ten : TextEnumType) =
186 struct
187 let names = Ten.names;;
188 let to_int (t : Ten.t) = Obj.magic t;;
189 let to_string t = names.(to_int t);;
190 let of_int n : Ten.t = Obj.magic n;;
191 let of_string s =
192 let rec find i =
193 if i = Array.length names
194 then failwith ("invalid " ^ Ten.name ^ ": " ^ s)
195 else (
196 if Ten.names.(i) = s
197 then of_int i
198 else find (i+1)
200 in find 0;;
201 end;;
203 module CSTE = TextEnumMake (struct
204 type t = colorspace;;
205 let name = "colorspace";;
206 let names = [|"rgb"; "bgr"; "gray"|];;
207 end);;
209 module MTE = TextEnumMake (struct
210 type t = mark;;
211 let name = "mark";;
212 let names = [|"page"; "block"; "line"; "word"|];;
213 end);;
215 module FMTE = TextEnumMake (struct
216 type t = fitmodel;;
217 let name = "fitmodel";;
218 let names = [|"width"; "proportional"; "page"|];;
219 end);;
221 type conf =
222 { mutable scrollbw : int
223 ; mutable scrollh : int
224 ; mutable scrollb : scrollb
225 ; mutable icase : bool
226 ; mutable preload : bool
227 ; mutable pagebias : int
228 ; mutable verbose : bool
229 ; mutable debug : bool
230 ; mutable scrollstep : int
231 ; mutable hscrollstep : int
232 ; mutable maxhfit : bool
233 ; mutable crophack : bool
234 ; mutable autoscrollstep : int
235 ; mutable maxwait : float option
236 ; mutable hlinks : bool
237 ; mutable underinfo : bool
238 ; mutable interpagespace : interpagespace
239 ; mutable zoom : float
240 ; mutable presentation : bool
241 ; mutable angle : angle
242 ; mutable cwinw : int
243 ; mutable cwinh : int
244 ; mutable savebmarks : bool
245 ; mutable fitmodel : fitmodel
246 ; mutable trimmargins : trimmargins
247 ; mutable trimfuzz : irect
248 ; mutable memlimit : memsize
249 ; mutable texcount : texcount
250 ; mutable sliceheight : sliceheight
251 ; mutable thumbw : width
252 ; mutable jumpback : bool
253 ; mutable bgcolor : (float * float * float)
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 redirectstderr : bool
266 ; mutable ghyllscroll : (int * int * int) option
267 ; mutable columns : columns
268 ; mutable beyecolumns : columncount option
269 ; mutable selcmd : string
270 ; mutable paxcmd : string
271 ; mutable passcmd : string
272 ; mutable savecmd : string
273 ; mutable updatecurs : bool
274 ; mutable keyhashes : (string * keyhash) list
275 ; mutable hfsize : int
276 ; mutable pgscale : float
277 ; mutable usepbo : bool
278 ; mutable wheelbypage : bool
279 ; mutable stcmd : string
280 ; mutable riani : bool
281 ; mutable pax : (float * int * int) ref option
282 ; mutable paxmark : mark
283 ; mutable leftscroll : bool
284 ; mutable title : string
285 ; mutable lastvisit : float
286 ; mutable annotinline : bool
288 and columns =
289 | Csingle of singlecolumn
290 | Cmulti of multicolumns
291 | Csplit of splitcolumns
292 and outlinekind =
293 | Onone
294 | Oanchor of anchor
295 | Ouri of uri
296 | Olaunch of launchcommand
297 | Oremote of (filename * pageno)
298 | Oremotedest of (filename * destname)
299 | Ohistory of (filename * (conf * outline list * x * anchor))
300 | Oaction of (unit -> unit)
301 and outline = (caption * outlinelevel * outlinekind)
302 and outlinelevel = int
305 type page =
306 { pageno : int
307 ; pagedimno : int
308 ; pagew : int
309 ; pageh : int
310 ; pagex : int
311 ; pagey : int
312 ; pagevw : int
313 ; pagevh : int
314 ; pagedispx : int
315 ; pagedispy : int
316 ; pagecol : int
320 type tile = opaque * pixmapsize * elapsed
321 and elapsed = float;;
322 type pagemapkey = pageno * gen;;
323 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
324 and row = int
325 and col = int
326 and currently =
327 | Idle
328 | Loading of (page * gen)
329 | Tiling of (
330 page * opaque * colorspace * angle * gen * col * row * width * height
332 | Outlining of outline list
335 type mpos = int * int
336 and mstate =
337 | Msel of (mpos * mpos)
338 | Mpan of mpos
339 | Mscrolly | Mscrollx
340 | Mzoom of (buttonno * step)
341 | Mzoomrect of (mpos * mpos)
342 | Mnone
343 and buttonno = int
344 and step = int
347 type mode =
348 | Birdseye of (conf * leftx * pageno * pageno * anchor)
349 | Textentry of (textentry * onleave)
350 | View
351 | LinkNav of linktarget
352 and onleave = leavetextentrystatus -> unit
353 and leavetextentrystatus = | Cancel | Confirm
354 and helpitem = string * int * action
355 and action =
356 | Noaction
357 | Action of (uioh -> uioh)
358 and linktarget =
359 | Ltexact of (pageno * direction)
360 | Ltgendir of direction
361 | Ltnotready of (pageno * direction)
362 and direction = int (* -1, 0, 1 *)
363 and textentry = string * string * onhist option * onkey * ondone * cancelonempty
364 and onkey = string -> int -> te
365 and ondone = string -> unit
366 and histcancel = unit -> unit
367 and onhist = ((histcmd -> string) * histcancel)
368 and histcmd = HCnext | HCprev | HCfirst | HClast
369 and cancelonempty = bool
370 and te =
371 | TEstop
372 | TEdone of string
373 | TEcont of string
374 | TEswitch of textentry
377 type 'a circbuf =
378 { store : 'a array
379 ; mutable rc : int
380 ; mutable wc : int
381 ; mutable len : int
385 type state =
386 { mutable ss : Unix.file_descr
387 ; mutable wsfd : Unix.file_descr
388 ; mutable errfd : Unix.file_descr option
389 ; mutable stderr : Unix.file_descr
390 ; mutable errmsgs : Buffer.t
391 ; mutable newerrmsgs : bool
392 ; mutable w : int
393 ; mutable x : int
394 ; mutable y : int
395 ; mutable anchor : anchor
396 ; mutable ranchors : (string * string * anchor * string) list
397 ; mutable maxy : int
398 ; mutable layout : page list
399 ; pagemap : (pagemapkey, opaque) Hashtbl.t
400 ; tilemap : (tilemapkey, tile) Hashtbl.t
401 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
402 ; mutable pdims : (pageno * width * height * leftx) list
403 ; mutable pagecount : int
404 ; mutable currently : currently
405 ; mutable mstate : mstate
406 ; mutable searchpattern : string
407 ; mutable rects : (pageno * recttype * rect) list
408 ; mutable rects1 : (pageno * recttype * rect) list
409 ; mutable text : string
410 ; mutable winstate : Wsi.winstate list
411 ; mutable mode : mode
412 ; mutable uioh : uioh
413 ; mutable outlines : outline array
414 ; mutable bookmarks : outline list
415 ; mutable path : string
416 ; mutable password : string
417 ; mutable nameddest : string
418 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
419 ; mutable memused : memsize
420 ; mutable gen : gen
421 ; mutable throttle : (page list * int * float) option
422 ; mutable autoscroll : int option
423 ; mutable ghyll : (int option -> unit)
424 ; mutable help : helpitem array
425 ; mutable docinfo : (int * string) list
426 ; mutable checkerstexid : GlTex.texture_id option
427 ; hists : hists
428 ; mutable prevzoom : (float * int)
429 ; mutable progress : float
430 ; mutable redisplay : bool
431 ; mutable mpos : mpos
432 ; mutable keystate : keystate
433 ; mutable glinks : bool
434 ; mutable prevcolumns : (columns * float) option
435 ; mutable winw : int
436 ; mutable winh : int
437 ; mutable reprf : (unit -> unit)
438 ; mutable origin : string
439 ; mutable roam : (unit -> unit)
440 ; mutable bzoom : bool
441 ; mutable traw : [`float] Raw.t
442 ; mutable vraw : [`float] Raw.t
444 and hists =
445 { pat : string circbuf
446 ; pag : string circbuf
447 ; nav : anchor circbuf
448 ; sel : string circbuf
452 let emptyanchor = (0, 0.0, 0.0);;
453 let emptykeyhash = Hashtbl.create 0;;
454 let firstgeomcmds = E.s, [];;
455 let noghyll _ = ();;
456 let noreprf () = ();;
457 let noroam () = ();;
459 let nouioh : uioh = object (self)
460 method display = ()
461 method key _ _ = self
462 method multiclick _ _ _ _ = self
463 method button _ _ _ _ _ = self
464 method motion _ _ = self
465 method pmotion _ _ = self
466 method infochanged _ = ()
467 method scrollpw = (0, nan, nan)
468 method scrollph = (0, nan, nan)
469 method modehash = emptykeyhash
470 method eformsgs = false
471 method alwaysscrolly = false
472 end;;
474 let platform_to_string = function
475 | Punknown -> "unknown"
476 | Plinux -> "Linux"
477 | Posx -> "OSX"
478 | Psun -> "Sun"
479 | Pbsd -> "BSD"
480 | Pcygwin -> "Cygwin"
483 let version () =
484 Printf.sprintf "llpp version %s, fitz %s, ocaml %s/%d bit"
485 Help.version (fz_version ()) Sys.ocaml_version Sys.word_size
488 let geturl s =
489 let colonpos = try String.index s ':' with Not_found -> -1 in
490 let len = String.length s in
491 if colonpos >= 0 && colonpos + 3 < len
492 then (
493 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
494 then
495 let schemestartpos =
496 try String.rindex_from s colonpos ' '
497 with Not_found -> -1
499 let scheme =
500 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
502 match scheme with
503 | "http" | "ftp" | "mailto" ->
504 let epos =
505 try String.index_from s colonpos ' '
506 with Not_found -> len
508 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
509 | _ -> E.s
510 else E.s
512 else E.s
515 let defconf =
516 { scrollbw = 7
517 ; scrollh = 12
518 ; scrollb = scrollbhv lor scrollbvv
519 ; icase = true
520 ; preload = true
521 ; pagebias = 0
522 ; verbose = false
523 ; debug = false
524 ; scrollstep = 24
525 ; hscrollstep = 24
526 ; maxhfit = true
527 ; crophack = false
528 ; autoscrollstep = 2
529 ; maxwait = None
530 ; hlinks = false
531 ; underinfo = false
532 ; interpagespace = 2
533 ; zoom = 1.0
534 ; presentation = false
535 ; angle = 0
536 ; cwinw = 900
537 ; cwinh = 900
538 ; savebmarks = true
539 ; fitmodel = FitProportional
540 ; trimmargins = false
541 ; trimfuzz = (0,0,0,0)
542 ; memlimit = 32 lsl 20
543 ; texcount = 256
544 ; sliceheight = 24
545 ; thumbw = 76
546 ; jumpback = true
547 ; bgcolor = (0.5, 0.5, 0.5)
548 ; bedefault = false
549 ; tilew = 2048
550 ; tileh = 2048
551 ; mustoresize = 256 lsl 20
552 ; checkers = true
553 ; aalevel = 8
554 ; urilauncher =
555 (match platform with
556 | Plinux | Psun | Pbsd -> "xdg-open \"%s\""
557 | Posx -> "open \"%s\""
558 | Pcygwin -> "cygstart \"%s\""
559 | Punknown -> "echo %s")
560 ; pathlauncher = "lp \"%s\""
561 ; selcmd =
562 (match platform with
563 | Plinux | Pbsd | Psun -> "xsel -i"
564 | Posx -> "pbcopy"
565 | Pcygwin -> "wsel"
566 | Punknown -> "cat")
567 ; paxcmd = "cat"
568 ; passcmd = E.s
569 ; savecmd = E.s
570 ; colorspace = Rgb
571 ; invert = false
572 ; colorscale = 1.0
573 ; redirectstderr = false
574 ; ghyllscroll = None
575 ; columns = Csingle [||]
576 ; beyecolumns = None
577 ; updatecurs = false
578 ; hfsize = 12
579 ; pgscale = 1.0
580 ; usepbo = false
581 ; wheelbypage = false
582 ; stcmd = "echo SyncTex"
583 ; riani = false
584 ; pax = None
585 ; paxmark = Mark_word
586 ; leftscroll = false
587 ; title = E.s
588 ; lastvisit = 0.0
589 ; annotinline = true
590 ; keyhashes =
591 let mk n = (n, Hashtbl.create 1) in
592 [ mk "global"
593 ; mk "info"
594 ; mk "help"
595 ; mk "outline"
596 ; mk "listview"
597 ; mk "birdseye"
598 ; mk "textentry"
599 ; mk "links"
600 ; mk "view"
605 let conf = { defconf with angle = defconf.angle };;
607 let gotouri uri =
608 if emptystr conf.urilauncher
609 then print_endline uri
610 else (
611 let url = geturl uri in
612 if emptystr url
613 then (Printf.eprintf "obtained empty url from uri %S\n" uri)
614 else
615 let re = Str.regexp "%s" in
616 let command = Str.global_replace re url conf.urilauncher in
617 try ignore (popen command [])
618 with exn ->
619 Printf.eprintf
620 "failed to execute `%s': %s\n" command (exntos exn);
621 flush stderr;
625 let makehelp () =
626 let strings =
627 version ()
628 :: "(searching in this text works just by typing (i.e. no initial '/'))"
629 :: E.s :: Help.keys
631 Array.of_list (
632 List.map (fun s ->
633 let url = geturl s in
634 if nonemptystr url
635 then (s, 0, Action (fun u -> gotouri url; u))
636 else (s, 0, Noaction)
637 ) strings);
640 let cbnew n v =
641 { store = Array.make n v
642 ; rc = 0
643 ; wc = 0
644 ; len = 0
648 let cbcap b = Array.length b.store;;
650 let cbput b v =
651 let cap = cbcap b in
652 b.store.(b.wc) <- v;
653 b.wc <- (b.wc + 1) mod cap;
654 b.rc <- b.wc;
655 b.len <- min (b.len + 1) cap;
658 let cbempty b = b.len = 0;;
660 let cbgetg b circular dir =
661 if cbempty b
662 then b.store.(0)
663 else
664 let rc = b.rc + dir in
665 let rc =
666 if circular
667 then (
668 if rc = -1
669 then b.len-1
670 else (
671 if rc >= b.len
672 then 0
673 else rc
676 else bound rc 0 (b.len-1)
678 b.rc <- rc;
679 b.store.(rc);
682 let cbget b = cbgetg b false;;
683 let cbgetc b = cbgetg b true;;
685 let state =
686 { ss = Unix.stdin
687 ; wsfd = Unix.stdin
688 ; errfd = None
689 ; stderr = Unix.stderr
690 ; errmsgs = Buffer.create 0
691 ; newerrmsgs = false
692 ; x = 0
693 ; y = 0
694 ; w = 0
695 ; anchor = emptyanchor
696 ; ranchors = []
697 ; layout = []
698 ; maxy = max_int
699 ; tilelru = Queue.create ()
700 ; pagemap = Hashtbl.create 10
701 ; tilemap = Hashtbl.create 10
702 ; pdims = []
703 ; pagecount = 0
704 ; currently = Idle
705 ; mstate = Mnone
706 ; rects = []
707 ; rects1 = []
708 ; text = E.s
709 ; mode = View
710 ; winstate = []
711 ; searchpattern = E.s
712 ; outlines = E.a
713 ; bookmarks = []
714 ; path = E.s
715 ; password = E.s
716 ; nameddest = E.s
717 ; geomcmds = firstgeomcmds
718 ; hists =
719 { nav = cbnew 10 emptyanchor
720 ; pat = cbnew 10 E.s
721 ; pag = cbnew 10 E.s
722 ; sel = cbnew 10 E.s
724 ; memused = 0
725 ; gen = 0
726 ; throttle = None
727 ; autoscroll = None
728 ; ghyll = noghyll
729 ; help = makehelp ()
730 ; docinfo = []
731 ; checkerstexid = None
732 ; prevzoom = (1.0, 0)
733 ; progress = -1.0
734 ; uioh = nouioh
735 ; redisplay = true
736 ; mpos = (-1, -1)
737 ; keystate = KSnone
738 ; glinks = false
739 ; prevcolumns = None
740 ; winw = -1
741 ; winh = -1
742 ; reprf = noreprf
743 ; origin = E.s
744 ; roam = noroam
745 ; bzoom = false
746 ; traw = Raw.create_static `float ~len:8
747 ; vraw = Raw.create_static `float ~len:8
751 let copykeyhashes c =
752 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
755 let calcips h =
756 let d = state.winh - h in
757 max conf.interpagespace ((d + 1) / 2)
760 let rowyh (c, coverA, coverB) b n =
761 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
762 then
763 let _, _, vy, (_, _, h, _) = b.(n) in
764 (vy, h)
765 else
766 let n' = n - coverA in
767 let d = n' mod c in
768 let s = n - d in
769 let e = min state.pagecount (s + c) in
770 let rec find m miny maxh = if m = e then miny, maxh else
771 let _, _, y, (_, _, h, _) = b.(m) in
772 let miny = min miny y in
773 let maxh = max maxh h in
774 find (m+1) miny maxh
775 in find s max_int 0
778 let page_of_y y =
779 let ((c, coverA, coverB) as cl), b =
780 match conf.columns with
781 | Csingle b -> (1, 0, 0), b
782 | Cmulti (c, b) -> c, b
783 | Csplit (_, b) -> (1, 0, 0), b
785 if Array.length b = 0
786 then -1
787 else
788 let rec bsearch nmin nmax =
789 if nmin > nmax
790 then bound nmin 0 (state.pagecount-1)
791 else
792 let n = (nmax + nmin) / 2 in
793 let vy, h = rowyh cl b n in
794 let y0, y1 =
795 if conf.presentation
796 then
797 let ips = calcips h in
798 let y0 = vy - ips in
799 let y1 = vy + h + ips in
800 y0, y1
801 else (
802 if n = 0
803 then 0, vy + h + conf.interpagespace
804 else
805 let y0 = vy - conf.interpagespace in
806 y0, y0 + h + conf.interpagespace
809 if y >= y0 && y < y1
810 then (
811 if c = 1
812 then n
813 else (
814 if n > coverA
815 then
816 if n < state.pagecount - coverB
817 then ((n-coverA)/c)*c + coverA
818 else n
819 else n
822 else (
823 if y > y0
824 then bsearch (n+1) nmax
825 else bsearch nmin (n-1)
828 bsearch 0 (state.pagecount-1);
831 let calcheight () =
832 match conf.columns with
833 | Cmulti ((_, _, _) as cl, b) ->
834 if Array.length b > 0
835 then
836 let y, h = rowyh cl b (Array.length b - 1) in
837 y + h + (if conf.presentation then calcips h else 0)
838 else 0
839 | Csingle b ->
840 if Array.length b > 0
841 then
842 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
843 y + h + (if conf.presentation then calcips h else 0)
844 else 0
845 | Csplit (_, b) ->
846 if Array.length b > 0
847 then
848 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
849 y + h
850 else 0
853 let getpageywh pageno =
854 let pageno = bound pageno 0 (state.pagecount-1) in
855 match conf.columns with
856 | Csingle b ->
857 if Array.length b = 0
858 then 0, 0, 0
859 else
860 let (_, _, y, (_, w, h, _)) = b.(pageno) in
861 let y =
862 if conf.presentation
863 then y - calcips h
864 else y
866 y, w, h
867 | Cmulti (cl, b) ->
868 if Array.length b = 0
869 then 0, 0, 0
870 else
871 let y, h = rowyh cl b pageno in
872 let (_, _, _, (_, w, _, _)) = b.(pageno) in
873 let y =
874 if conf.presentation
875 then y - calcips h
876 else y
878 y, w, h
879 | Csplit (c, b) ->
880 if Array.length b = 0
881 then 0, 0, 0
882 else
883 let n = pageno*c in
884 let (_, _, y, (_, w, h, _)) = b.(n) in
885 y, w / c, h
888 let getpageyh pageno =
889 let y,_,h = getpageywh pageno in
890 y, h;
893 let getpagedim pageno =
894 let rec f ppdim l =
895 match l with
896 | (n, _, _, _) as pdim :: rest ->
897 if n >= pageno
898 then (if n = pageno then pdim else ppdim)
899 else f pdim rest
901 | [] -> ppdim
903 f (-1, -1, -1, -1) state.pdims
906 let getpagey pageno = fst (getpageyh pageno);;
908 let getanchor1 l =
909 let top =
910 let coloff = l.pagecol * l.pageh in
911 float (l.pagey + coloff) /. float l.pageh
913 let dtop =
914 if l.pagedispy = 0
915 then
917 else (
918 if conf.presentation
919 then float l.pagedispy /. float (calcips l.pageh)
920 else float l.pagedispy /. float conf.interpagespace
923 (l.pageno, top, dtop)
926 let getanchor () =
927 match state.layout with
928 | l :: _ -> getanchor1 l
929 | [] ->
930 let n = page_of_y state.y in
931 if n = -1
932 then state.anchor
933 else
934 let y, h = getpageyh n in
935 let dy = y - state.y in
936 let dtop =
937 if conf.presentation
938 then
939 let ips = calcips h in
940 float (dy + ips) /. float ips
941 else
942 float dy /. float conf.interpagespace
944 (n, 0.0, dtop)
947 let fontpath = ref E.s;;
949 type historder = [ `lastvisit | `title | `path | `file ];;
950 let historder : historder ref = ref `lastvisit;;
952 module KeyMap =
953 Map.Make (struct type t = (int * int) let compare = compare end);;
955 open Parser;;
957 let unent s =
958 let l = String.length s in
959 let b = Buffer.create l in
960 unent b s 0 l;
961 Buffer.contents b;
964 let home =
965 try Sys.getenv "HOME"
966 with exn ->
967 prerr_endline
968 ("Can not determine home directory location: " ^ exntos exn);
972 let modifier_of_string = function
973 | "alt" -> Wsi.altmask
974 | "shift" -> Wsi.shiftmask
975 | "ctrl" | "control" -> Wsi.ctrlmask
976 | "meta" -> Wsi.metamask
977 | _ -> 0
980 let key_of_string =
981 let r = Str.regexp "-" in
982 fun s ->
983 let elems = Str.full_split r s in
984 let f n k m =
985 let g s =
986 let m1 = modifier_of_string s in
987 if m1 = 0
988 then (Wsi.namekey s, m)
989 else (k, m lor m1)
990 in function
991 | Str.Delim s when n land 1 = 0 -> g s
992 | Str.Text s -> g s
993 | Str.Delim _ -> (k, m)
995 let rec loop n k m = function
996 | [] -> (k, m)
997 | x :: xs ->
998 let k, m = f n k m x in
999 loop (n+1) k m xs
1001 loop 0 0 0 elems
1004 let keys_of_string =
1005 let r = Str.regexp "[ \t]" in
1006 fun s ->
1007 let elems = Str.split r s in
1008 List.map key_of_string elems
1011 let config_of c attrs =
1012 let apply c k v =
1014 match k with
1015 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
1016 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
1017 | "case-insensitive-search" -> { c with icase = bool_of_string v }
1018 | "preload" -> { c with preload = bool_of_string v }
1019 | "page-bias" -> { c with pagebias = int_of_string v }
1020 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
1021 | "horizontal-scroll-step" ->
1022 { c with hscrollstep = max (int_of_string v) 1 }
1023 | "auto-scroll-step" ->
1024 { c with autoscrollstep = max 0 (int_of_string v) }
1025 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
1026 | "crop-hack" -> { c with crophack = bool_of_string v }
1027 | "throttle" ->
1028 let mw =
1029 match String.lowercase v with
1030 | "true" -> Some infinity
1031 | "false" -> None
1032 | f -> Some (float_of_string f)
1034 { c with maxwait = mw}
1035 | "highlight-links" -> { c with hlinks = bool_of_string v }
1036 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
1037 | "vertical-margin" ->
1038 { c with interpagespace = max 0 (int_of_string v) }
1039 | "zoom" ->
1040 let zoom = float_of_string v /. 100. in
1041 let zoom = max zoom 0.0 in
1042 { c with zoom = zoom }
1043 | "presentation" -> { c with presentation = bool_of_string v }
1044 | "rotation-angle" -> { c with angle = int_of_string v }
1045 | "width" -> { c with cwinw = max 20 (int_of_string v) }
1046 | "height" -> { c with cwinh = max 20 (int_of_string v) }
1047 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
1048 | "proportional-display" ->
1049 let fm =
1050 if bool_of_string v
1051 then FitProportional
1052 else FitWidth
1054 { c with fitmodel = fm }
1055 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
1056 | "pixmap-cache-size" ->
1057 { c with memlimit = max 2 (int_of_string_with_suffix v) }
1058 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
1059 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
1060 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
1061 | "persistent-location" -> { c with jumpback = bool_of_string v }
1062 | "background-color" -> { c with bgcolor = color_of_string v }
1063 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
1064 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
1065 | "mupdf-store-size" ->
1066 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
1067 | "checkers" -> { c with checkers = bool_of_string v }
1068 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
1069 | "trim-margins" -> { c with trimmargins = bool_of_string v }
1070 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
1071 | "uri-launcher" -> { c with urilauncher = unent v }
1072 | "path-launcher" -> { c with pathlauncher = unent v }
1073 | "color-space" -> { c with colorspace = CSTE.of_string v }
1074 | "invert-colors" -> { c with invert = bool_of_string v }
1075 | "brightness" -> { c with colorscale = float_of_string v }
1076 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
1077 | "ghyllscroll" -> { c with ghyllscroll = ghyllscroll_of_string v }
1078 | "columns" ->
1079 let (n, _, _) as nab = multicolumns_of_string v in
1080 if n < 0
1081 then { c with columns = Csplit (-n, E.a) }
1082 else { c with columns = Cmulti (nab, E.a) }
1083 | "birds-eye-columns" ->
1084 { c with beyecolumns = Some (max (int_of_string v) 2) }
1085 | "selection-command" -> { c with selcmd = unent v }
1086 | "synctex-command" -> { c with stcmd = unent v }
1087 | "pax-command" -> { c with paxcmd = unent v }
1088 | "askpass-command" -> { c with passcmd = unent v }
1089 | "savepath-command" -> { c with savecmd = unent v }
1090 | "update-cursor" -> { c with updatecurs = bool_of_string v }
1091 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
1092 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
1093 | "use-pbo" -> { c with usepbo = bool_of_string v }
1094 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
1095 | "horizontal-scrollbar-visible" ->
1096 let b =
1097 if bool_of_string v
1098 then c.scrollb lor scrollbhv
1099 else c.scrollb land (lnot scrollbhv)
1101 { c with scrollb = b }
1102 | "vertical-scrollbar-visible" ->
1103 let b =
1104 if bool_of_string v
1105 then c.scrollb lor scrollbvv
1106 else c.scrollb land (lnot scrollbvv)
1108 { c with scrollb = b }
1109 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
1110 | "point-and-x" ->
1111 { c with pax =
1112 if bool_of_string v
1113 then Some (ref (0.0, 0, 0))
1114 else None }
1115 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
1116 | "scroll-bar-on-the-left" -> { c with leftscroll = bool_of_string v }
1117 | "title" -> { c with title = unent v }
1118 | "last-visit" -> { c with lastvisit = float_of_string v }
1119 | "edit-annots-inline" -> { c with annotinline = bool_of_string v }
1120 | _ -> c
1121 with exn ->
1122 prerr_endline ("Error processing attribute (`" ^
1123 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
1126 let rec fold c = function
1127 | [] -> c
1128 | (k, v) :: rest ->
1129 let c = apply c k v in
1130 fold c rest
1132 fold { c with keyhashes = copykeyhashes c } attrs;
1135 let fromstring f pos n v d =
1136 try f v
1137 with exn ->
1138 dolog "Error processing attribute (%S=%S) at %d\n%s"
1139 n v pos (exntos exn)
1144 let bookmark_of attrs =
1145 let rec fold title page rely visy = function
1146 | ("title", v) :: rest -> fold v page rely visy rest
1147 | ("page", v) :: rest -> fold title v rely visy rest
1148 | ("rely", v) :: rest -> fold title page v visy rest
1149 | ("visy", v) :: rest -> fold title page rely v rest
1150 | _ :: rest -> fold title page rely visy rest
1151 | [] -> title, page, rely, visy
1153 fold "invalid" "0" "0" "0" attrs
1156 let doc_of attrs =
1157 let rec fold path page rely pan visy = function
1158 | ("path", v) :: rest -> fold v page rely pan visy rest
1159 | ("page", v) :: rest -> fold path v rely pan visy rest
1160 | ("rely", v) :: rest -> fold path page v pan visy rest
1161 | ("pan", v) :: rest -> fold path page rely v visy rest
1162 | ("visy", v) :: rest -> fold path page rely pan v rest
1163 | _ :: rest -> fold path page rely pan visy rest
1164 | [] -> path, page, rely, pan, visy
1166 fold E.s "0" "0" "0" "0" attrs
1169 let map_of attrs =
1170 let rec fold rs ls = function
1171 | ("out", v) :: rest -> fold v ls rest
1172 | ("in", v) :: rest -> fold rs v rest
1173 | _ :: rest -> fold ls rs rest
1174 | [] -> ls, rs
1176 fold E.s E.s attrs
1179 let setconf dst src =
1180 dst.scrollbw <- src.scrollbw;
1181 dst.scrollh <- src.scrollh;
1182 dst.icase <- src.icase;
1183 dst.preload <- src.preload;
1184 dst.pagebias <- src.pagebias;
1185 dst.verbose <- src.verbose;
1186 dst.scrollstep <- src.scrollstep;
1187 dst.maxhfit <- src.maxhfit;
1188 dst.crophack <- src.crophack;
1189 dst.autoscrollstep <- src.autoscrollstep;
1190 dst.maxwait <- src.maxwait;
1191 dst.hlinks <- src.hlinks;
1192 dst.underinfo <- src.underinfo;
1193 dst.interpagespace <- src.interpagespace;
1194 dst.zoom <- src.zoom;
1195 dst.presentation <- src.presentation;
1196 dst.angle <- src.angle;
1197 dst.cwinw <- src.cwinw;
1198 dst.cwinh <- src.cwinh;
1199 dst.savebmarks <- src.savebmarks;
1200 dst.memlimit <- src.memlimit;
1201 dst.fitmodel <- src.fitmodel;
1202 dst.texcount <- src.texcount;
1203 dst.sliceheight <- src.sliceheight;
1204 dst.thumbw <- src.thumbw;
1205 dst.jumpback <- src.jumpback;
1206 dst.bgcolor <- src.bgcolor;
1207 dst.tilew <- src.tilew;
1208 dst.tileh <- src.tileh;
1209 dst.mustoresize <- src.mustoresize;
1210 dst.checkers <- src.checkers;
1211 dst.aalevel <- src.aalevel;
1212 dst.trimmargins <- src.trimmargins;
1213 dst.trimfuzz <- src.trimfuzz;
1214 dst.urilauncher <- src.urilauncher;
1215 dst.colorspace <- src.colorspace;
1216 dst.invert <- src.invert;
1217 dst.colorscale <- src.colorscale;
1218 dst.redirectstderr <- src.redirectstderr;
1219 dst.ghyllscroll <- src.ghyllscroll;
1220 dst.columns <- src.columns;
1221 dst.beyecolumns <- src.beyecolumns;
1222 dst.selcmd <- src.selcmd;
1223 dst.updatecurs <- src.updatecurs;
1224 dst.pathlauncher <- src.pathlauncher;
1225 dst.keyhashes <- copykeyhashes src;
1226 dst.hfsize <- src.hfsize;
1227 dst.hscrollstep <- src.hscrollstep;
1228 dst.pgscale <- src.pgscale;
1229 dst.usepbo <- src.usepbo;
1230 dst.wheelbypage <- src.wheelbypage;
1231 dst.stcmd <- src.stcmd;
1232 dst.paxcmd <- src.paxcmd;
1233 dst.passcmd <- src.passcmd;
1234 dst.savecmd <- src.savecmd;
1235 dst.scrollb <- src.scrollb;
1236 dst.riani <- src.riani;
1237 dst.paxmark <- src.paxmark;
1238 dst.leftscroll <- src.leftscroll;
1239 dst.title <- src.title;
1240 dst.annotinline <- src.annotinline;
1241 dst.pax <-
1242 if src.pax = None
1243 then None
1244 else Some ((ref (0.0, 0, 0)));
1247 let findkeyhash c name =
1248 try List.assoc name c.keyhashes
1249 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
1252 let get s =
1253 let h = Hashtbl.create 10 in
1254 let dc = { defconf with angle = defconf.angle } in
1255 let rec toplevel v t spos _ =
1256 match t with
1257 | Vdata | Vcdata | Vend -> v
1258 | Vopen ("llppconfig", _, closed) ->
1259 if closed
1260 then v
1261 else { v with f = llppconfig }
1262 | Vopen _ ->
1263 error "unexpected subelement at top level" s spos
1264 | Vclose _ -> error "unexpected close at top level" s spos
1266 and llppconfig v t spos _ =
1267 match t with
1268 | Vdata | Vcdata -> v
1269 | Vend -> error "unexpected end of input in llppconfig" s spos
1270 | Vopen ("defaults", attrs, closed) ->
1271 let c = config_of dc attrs in
1272 setconf dc c;
1273 if closed
1274 then v
1275 else { v with f = defaults }
1277 | Vopen ("ui-font", attrs, closed) ->
1278 let rec getsize size = function
1279 | [] -> size
1280 | ("size", v) :: rest ->
1281 let size =
1282 fromstring int_of_string spos "size" v fstate.fontsize in
1283 getsize size rest
1284 | l -> getsize size l
1286 fstate.fontsize <- getsize fstate.fontsize attrs;
1287 if closed
1288 then v
1289 else { v with f = uifont (Buffer.create 10) }
1291 | Vopen ("doc", attrs, closed) ->
1292 let pathent, spage, srely, span, svisy = doc_of attrs in
1293 let path = unent pathent
1294 and pageno = fromstring int_of_string spos "page" spage 0
1295 and rely = fromstring float_of_string spos "rely" srely 0.0
1296 and pan = fromstring int_of_string spos "pan" span 0
1297 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1298 let c = config_of dc attrs in
1299 let anchor = (pageno, rely, visy) in
1300 if closed
1301 then (Hashtbl.add h path (c, [], pan, anchor); v)
1302 else { v with f = doc path pan anchor c [] }
1304 | Vopen _ ->
1305 error "unexpected subelement in llppconfig" s spos
1307 | Vclose "llppconfig" -> { v with f = toplevel }
1308 | Vclose _ -> error "unexpected close in llppconfig" s spos
1310 and defaults v t spos _ =
1311 match t with
1312 | Vdata | Vcdata -> v
1313 | Vend -> error "unexpected end of input in defaults" s spos
1314 | Vopen ("keymap", attrs, closed) ->
1315 let modename =
1316 try List.assoc "mode" attrs
1317 with Not_found -> "global" in
1318 if closed
1319 then v
1320 else
1321 let ret keymap =
1322 let h = findkeyhash dc modename in
1323 KeyMap.iter (Hashtbl.replace h) keymap;
1324 defaults
1326 { v with f = pkeymap ret KeyMap.empty }
1328 | Vopen (_, _, _) ->
1329 error "unexpected subelement in defaults" s spos
1331 | Vclose "defaults" ->
1332 { v with f = llppconfig }
1334 | Vclose _ -> error "unexpected close in defaults" s spos
1336 and uifont b v t spos epos =
1337 match t with
1338 | Vdata | Vcdata ->
1339 Buffer.add_substring b s spos (epos - spos);
1341 | Vopen (_, _, _) ->
1342 error "unexpected subelement in ui-font" s spos
1343 | Vclose "ui-font" ->
1344 if emptystr !fontpath
1345 then fontpath := Buffer.contents b;
1346 { v with f = llppconfig }
1347 | Vclose _ -> error "unexpected close in ui-font" s spos
1348 | Vend -> error "unexpected end of input in ui-font" s spos
1350 and doc path pan anchor c bookmarks v t spos _ =
1351 match t with
1352 | Vdata | Vcdata -> v
1353 | Vend -> error "unexpected end of input in doc" s spos
1354 | Vopen ("bookmarks", _, closed) ->
1355 if closed
1356 then v
1357 else { v with f = pbookmarks path pan anchor c bookmarks }
1359 | Vopen ("keymap", attrs, closed) ->
1360 let modename =
1361 try List.assoc "mode" attrs
1362 with Not_found -> "global"
1364 if closed
1365 then v
1366 else
1367 let ret keymap =
1368 let h = findkeyhash c modename in
1369 KeyMap.iter (Hashtbl.replace h) keymap;
1370 doc path pan anchor c bookmarks
1372 { v with f = pkeymap ret KeyMap.empty }
1374 | Vopen (_, _, _) ->
1375 error "unexpected subelement in doc" s spos
1377 | Vclose "doc" ->
1378 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
1379 { v with f = llppconfig }
1381 | Vclose _ -> error "unexpected close in doc" s spos
1383 and pkeymap ret keymap v t spos _ =
1384 match t with
1385 | Vdata | Vcdata -> v
1386 | Vend -> error "unexpected end of input in keymap" s spos
1387 | Vopen ("map", attrs, closed) ->
1388 let r, l = map_of attrs in
1389 let kss = fromstring keys_of_string spos "in" r [] in
1390 let lss = fromstring keys_of_string spos "out" l [] in
1391 let keymap =
1392 match kss with
1393 | [] -> keymap
1394 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
1395 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
1397 if closed
1398 then { v with f = pkeymap ret keymap }
1399 else
1400 let f () = v in
1401 { v with f = skip "map" f }
1403 | Vopen _ ->
1404 error "unexpected subelement in keymap" s spos
1406 | Vclose "keymap" ->
1407 { v with f = ret keymap }
1409 | Vclose _ -> error "unexpected close in keymap" s spos
1411 and pbookmarks path pan anchor c bookmarks v t spos _ =
1412 match t with
1413 | Vdata | Vcdata -> v
1414 | Vend -> error "unexpected end of input in bookmarks" s spos
1415 | Vopen ("item", attrs, closed) ->
1416 let titleent, spage, srely, svisy = bookmark_of attrs in
1417 let page = fromstring int_of_string spos "page" spage 0
1418 and rely = fromstring float_of_string spos "rely" srely 0.0
1419 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1420 let bookmarks =
1421 (unent titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
1423 if closed
1424 then { v with f = pbookmarks path pan anchor c bookmarks }
1425 else
1426 let f () = v in
1427 { v with f = skip "item" f }
1429 | Vopen _ ->
1430 error "unexpected subelement in bookmarks" s spos
1432 | Vclose "bookmarks" ->
1433 { v with f = doc path pan anchor c bookmarks }
1435 | Vclose _ -> Parser.parse_error "unexpected close in bookmarks" s spos
1437 and skip tag f v t spos _ =
1438 match t with
1439 | Vdata | Vcdata -> v
1440 | Vend ->
1441 Parser.parse_error ("unexpected end of input in skipped " ^ tag) s spos
1442 | Vopen (tag', _, closed) ->
1443 if closed
1444 then v
1445 else
1446 let f' () = { v with f = skip tag f } in
1447 { v with f = skip tag' f' }
1448 | Vclose ctag ->
1449 if tag = ctag
1450 then f ()
1451 else Parser.parse_error ("unexpected close in skipped " ^ tag) s spos
1454 parse { f = toplevel; accu = () } s;
1455 h, dc;
1458 let do_load f ic =
1460 let len = in_channel_length ic in
1461 let s = really_input_string ic len in
1462 f s;
1463 with
1464 | Parse_error (msg, s, pos) ->
1465 let subs = subs s pos in
1466 Utils.error "parse error: %s: at %d [..%s..]" msg pos subs
1468 | exn ->
1469 failwith ("config load error: " ^ exntos exn)
1472 let defconfpath =
1473 let dir =
1474 let xdgconfdir = Utils.getenvwithdef "XDG_CONFIG_HOME" E.s in
1475 if xdgconfdir == E.s
1476 then
1478 let dir = Filename.concat home ".config" in
1479 if Sys.is_directory dir then dir else home
1480 with _ -> home
1481 else xdgconfdir
1483 Filename.concat dir "llpp.conf"
1486 let confpath = ref defconfpath;;
1488 let load1 f =
1489 if Sys.file_exists !confpath
1490 then
1491 match
1492 (try Some (open_in_bin !confpath)
1493 with exn ->
1494 prerr_endline
1495 ("Error opening configuration file `" ^ !confpath ^ "': " ^
1496 exntos exn);
1497 None
1499 with
1500 | Some ic ->
1501 let success =
1503 f (do_load get ic)
1504 with exn ->
1505 prerr_endline
1506 ("Error loading configuration from `" ^ !confpath ^ "': " ^
1507 exntos exn);
1508 false
1510 close_in ic;
1511 success
1513 | None -> false
1514 else
1515 f (Hashtbl.create 0, defconf)
1518 let load openlast =
1519 let f (h, dc) =
1520 if openlast
1521 then (
1522 let path, _ =
1523 Hashtbl.fold
1524 (fun path (conf, _, _, _) ((_, besttime) as best) ->
1525 if conf.lastvisit > besttime
1526 then (path, conf.lastvisit)
1527 else best)
1529 (state.path, -.infinity)
1531 state.path <- path;
1533 let pc, pb, px, pa =
1535 let path =
1536 if emptystr state.origin
1537 then state.path
1538 else state.origin
1540 let absname = abspath path in
1541 Hashtbl.find h absname
1542 with Not_found -> dc, [], 0, emptyanchor
1544 setconf defconf dc;
1545 setconf conf pc;
1546 state.bookmarks <- pb;
1547 state.x <- px;
1548 if conf.jumpback
1549 then state.anchor <- pa;
1550 cbput state.hists.nav pa;
1551 true
1553 load1 f
1556 let gethist listref =
1557 let f (h, _) =
1558 listref :=
1559 Hashtbl.fold (fun path (pc, pb, px, pa) accu ->
1560 (path, pc, pb, px, pa) :: accu)
1561 h [];
1562 true
1564 load1 f
1567 let add_attrs bb always dc c time =
1568 let ob s a b =
1569 if always || a != b
1570 then Printf.bprintf bb "\n %s='%b'" s a
1571 and op s a b =
1572 if always || a <> b
1573 then Printf.bprintf bb "\n %s='%b'" s (a != None)
1574 and oi s a b =
1575 if always || a != b
1576 then Printf.bprintf bb "\n %s='%d'" s a
1577 and oI s a b =
1578 if always || a != b
1579 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
1580 and oz s a b =
1581 if always || a <> b
1582 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
1583 and oF s a b =
1584 if always || a <> b
1585 then Printf.bprintf bb "\n %s='%f'" s a
1586 and oL s a b =
1587 if always || a <> b
1588 then Printf.bprintf bb "\n %s='%Ld'" s a
1589 and oc s a b =
1590 if always || a <> b
1591 then Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
1592 and oC s a b =
1593 if always || a <> b
1594 then Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
1595 and oR s a b =
1596 if always || a <> b
1597 then Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
1598 and os s a b =
1599 if always || a <> b
1600 then Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
1601 and og s a b =
1602 if always || a <> b
1603 then
1604 match a with
1605 | Some (_N, _A, _B) ->
1606 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
1607 | None ->
1608 match b with
1609 | None -> ()
1610 | _ ->
1611 Printf.bprintf bb "\n %s='none'" s
1612 and oW s a b =
1613 if always || a <> b
1614 then
1615 let v =
1616 match a with
1617 | None -> "false"
1618 | Some f ->
1619 if f = infinity
1620 then "true"
1621 else string_of_float f
1623 Printf.bprintf bb "\n %s='%s'" s v
1624 and oco s a b =
1625 if always || a <> b
1626 then
1627 match a with
1628 | Cmulti ((n, a, b), _) when n > 1 ->
1629 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
1630 | Csplit (n, _) when n > 1 ->
1631 Printf.bprintf bb "\n %s='%d'" s ~-n
1632 | Cmulti _ | Csplit _ | Csingle _ -> ()
1633 and obeco s a b =
1634 if always || a <> b
1635 then
1636 match a with
1637 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
1638 | _ -> ()
1639 and oFm s a b =
1640 if always || a <> b
1641 then Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
1642 and oSv s a b m =
1643 if always || a <> b
1644 then Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
1645 and oPm s a b =
1646 if always || a <> b
1647 then Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
1649 oi "width" c.cwinw dc.cwinw;
1650 oi "height" c.cwinh dc.cwinh;
1651 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
1652 oi "scroll-handle-height" c.scrollh dc.scrollh;
1653 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
1654 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
1655 ob "case-insensitive-search" c.icase dc.icase;
1656 ob "preload" c.preload dc.preload;
1657 oi "page-bias" c.pagebias dc.pagebias;
1658 oi "scroll-step" c.scrollstep dc.scrollstep;
1659 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
1660 ob "max-height-fit" c.maxhfit dc.maxhfit;
1661 ob "crop-hack" c.crophack dc.crophack;
1662 oW "throttle" c.maxwait dc.maxwait;
1663 ob "highlight-links" c.hlinks dc.hlinks;
1664 ob "under-cursor-info" c.underinfo dc.underinfo;
1665 oi "vertical-margin" c.interpagespace dc.interpagespace;
1666 oz "zoom" c.zoom dc.zoom;
1667 ob "presentation" c.presentation dc.presentation;
1668 oi "rotation-angle" c.angle dc.angle;
1669 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
1670 oFm "fit-model" c.fitmodel dc.fitmodel;
1671 oI "pixmap-cache-size" c.memlimit dc.memlimit;
1672 oi "tex-count" c.texcount dc.texcount;
1673 oi "slice-height" c.sliceheight dc.sliceheight;
1674 oi "thumbnail-width" c.thumbw dc.thumbw;
1675 ob "persistent-location" c.jumpback dc.jumpback;
1676 oc "background-color" c.bgcolor dc.bgcolor;
1677 oi "tile-width" c.tilew dc.tilew;
1678 oi "tile-height" c.tileh dc.tileh;
1679 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
1680 ob "checkers" c.checkers dc.checkers;
1681 oi "aalevel" c.aalevel dc.aalevel;
1682 ob "trim-margins" c.trimmargins dc.trimmargins;
1683 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
1684 os "uri-launcher" c.urilauncher dc.urilauncher;
1685 os "path-launcher" c.pathlauncher dc.pathlauncher;
1686 oC "color-space" c.colorspace dc.colorspace;
1687 ob "invert-colors" c.invert dc.invert;
1688 oF "brightness" c.colorscale dc.colorscale;
1689 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
1690 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
1691 oco "columns" c.columns dc.columns;
1692 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
1693 os "selection-command" c.selcmd dc.selcmd;
1694 os "synctex-command" c.stcmd dc.stcmd;
1695 os "pax-command" c.paxcmd dc.paxcmd;
1696 os "askpass-command" c.passcmd dc.passcmd;
1697 os "savepath-command" c.savecmd dc.savecmd;
1698 ob "update-cursor" c.updatecurs dc.updatecurs;
1699 oi "hint-font-size" c.hfsize dc.hfsize;
1700 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
1701 oF "page-scroll-scale" c.pgscale dc.pgscale;
1702 ob "use-pbo" c.usepbo dc.usepbo;
1703 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
1704 ob "remote-in-a-new-instance" c.riani dc.riani;
1705 op "point-and-x" c.pax dc.pax;
1706 oPm "point-and-x-mark" c.paxmark dc.paxmark;
1707 ob "scroll-bar-on-the-left" c.leftscroll dc.leftscroll;
1708 if not always
1709 then os "title" c.title dc.title;
1710 oL "last-visit" (Int64.of_float time) 0L;
1711 ob "edit-annotations-inline" c.annotinline dc.annotinline;
1714 let keymapsbuf always dc c =
1715 let bb = Buffer.create 16 in
1716 let rec loop = function
1717 | [] -> ()
1718 | (modename, h) :: rest ->
1719 let dh = findkeyhash dc modename in
1720 if always || h <> dh
1721 then (
1722 if Hashtbl.length h > 0
1723 then (
1724 if Buffer.length bb > 0
1725 then Buffer.add_char bb '\n';
1726 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
1727 Hashtbl.iter (fun i o ->
1728 let isdifferent = always ||
1730 let dO = Hashtbl.find dh i in
1731 dO <> o
1732 with Not_found -> true
1734 if isdifferent
1735 then
1736 let addkm (k, m) =
1737 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
1738 if Wsi.withalt m then Buffer.add_string bb "alt-";
1739 if Wsi.withshift m then Buffer.add_string bb "shift-";
1740 if Wsi.withmeta m then Buffer.add_string bb "meta-";
1741 Buffer.add_string bb (Wsi.keyname k);
1743 let addkms l =
1744 let rec loop = function
1745 | [] -> ()
1746 | km :: [] -> addkm km
1747 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
1749 loop l
1751 Buffer.add_string bb "<map in='";
1752 addkm i;
1753 match o with
1754 | KMinsrt km ->
1755 Buffer.add_string bb "' out='";
1756 addkm km;
1757 Buffer.add_string bb "'/>\n"
1759 | KMinsrl kms ->
1760 Buffer.add_string bb "' out='";
1761 addkms kms;
1762 Buffer.add_string bb "'/>\n"
1764 | KMmulti (ins, kms) ->
1765 Buffer.add_char bb ' ';
1766 addkms ins;
1767 Buffer.add_string bb "' out='";
1768 addkms kms;
1769 Buffer.add_string bb "'/>\n"
1770 ) h;
1771 Buffer.add_string bb "</keymap>";
1774 loop rest
1776 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 =
1808 if bookmarks == [] && c = dc && anchor = emptyanchor
1809 then ()
1810 else (
1811 Printf.bprintf bb "<doc path='%s'"
1812 (enent path 0 (String.length path));
1814 if anchor <> emptyanchor
1815 then (
1816 let n, rely, visy = anchor in
1817 Printf.bprintf bb "\n page='%d'" n;
1818 if rely > 1e-6
1819 then
1820 Printf.bprintf bb " rely='%f'" rely
1822 if abs_float visy > 1e-6
1823 then
1824 Printf.bprintf bb " visy='%f'" visy
1828 if pan != 0
1829 then Printf.bprintf bb " pan='%d'" pan;
1831 add_attrs bb false dc c time;
1832 let kb = keymapsbuf false dc c in
1834 begin match bookmarks with
1835 | [] ->
1836 if Buffer.length kb > 0
1837 then (
1838 Buffer.add_string bb ">\n";
1839 Buffer.add_buffer bb kb;
1840 Buffer.add_string bb "\n</doc>\n";
1842 else Buffer.add_string bb "/>\n"
1843 | _ ->
1844 Buffer.add_string bb ">\n<bookmarks>\n";
1845 List.iter (fun (title, _, kind) ->
1846 begin match kind with
1847 | Oanchor (page, rely, visy) ->
1848 Printf.bprintf bb
1849 "<item title='%s' page='%d'"
1850 (enent title 0 (String.length title))
1851 page
1853 if rely > 1e-6
1854 then
1855 Printf.bprintf bb " rely='%f'" rely
1857 if abs_float visy > 1e-6
1858 then
1859 Printf.bprintf bb " visy='%f'" visy
1861 | Ohistory _ | Onone | Ouri _ | Oremote _
1862 | Oremotedest _ | Olaunch _ | Oaction _ ->
1863 failwith "unexpected link in bookmarks"
1864 end;
1865 Buffer.add_string bb "/>\n";
1866 ) bookmarks;
1867 Buffer.add_string bb "</bookmarks>";
1868 if Buffer.length kb > 0
1869 then (
1870 Buffer.add_string bb "\n";
1871 Buffer.add_buffer bb kb;
1873 Buffer.add_string bb "\n</doc>\n";
1874 end;
1878 let pan, conf =
1879 match state.mode with
1880 | Birdseye (c, pan, _, _, _) ->
1881 let beyecolumns =
1882 match conf.columns with
1883 | Cmulti ((c, _, _), _) -> Some c
1884 | Csingle _ -> None
1885 | Csplit _ -> None
1886 and columns =
1887 match c.columns with
1888 | Cmulti (c, _) -> Cmulti (c, E.a)
1889 | Csingle _ -> Csingle E.a
1890 | Csplit _ -> failwith "quit from bird's eye while split"
1892 pan, { c with beyecolumns = beyecolumns; columns = columns }
1893 | Textentry _
1894 | View
1895 | LinkNav _ -> x, conf
1897 let docpath = if nonemptystr state.path then abspath state.path else E.s in
1898 adddoc docpath pan (getanchor ())
1900 let autoscrollstep =
1901 match state.autoscroll with
1902 | Some step -> step
1903 | None -> conf.autoscrollstep
1905 begin match state.mode with
1906 | Birdseye beye -> leavebirdseye beye true
1907 | Textentry _
1908 | View
1909 | LinkNav _ -> ()
1910 end;
1911 { conf with autoscrollstep = autoscrollstep }
1913 (if conf.savebmarks then state.bookmarks else [])
1914 (now ());
1916 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
1917 if docpath <> abspath path
1918 then adddoc path x anchor c bookmarks c.lastvisit
1919 ) h;
1920 Buffer.add_string bb "</llppconfig>\n";
1921 true;
1924 let save leavebirdseye =
1925 let relx = float state.x /. float state.winw in
1926 let w, h, x =
1927 let cx w = truncate (relx *. float w) in
1928 List.fold_left
1929 (fun (w, h, x) ws ->
1930 match ws with
1931 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
1932 | Wsi.MaxVert -> (w, conf.cwinh, x)
1933 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
1935 (state.winw, state.winh, state.x) state.winstate
1937 conf.cwinw <- w;
1938 conf.cwinh <- h;
1939 let bb = Buffer.create 32768 in
1940 let save2 (h, dc) =
1941 save1 bb leavebirdseye x h dc
1943 if load1 save2 && Buffer.length bb > 0
1944 then
1946 let tmp = !confpath ^ ".tmp" in
1947 let oc = open_out_bin tmp in
1948 Buffer.output_buffer oc bb;
1949 close_out oc;
1950 Unix.rename tmp !confpath;
1951 with exn ->
1952 prerr_endline
1953 ("error while saving configuration: " ^ exntos exn)
1956 let gc fdi fdo =
1957 let wr s n =
1958 let n' = Unix.write fdo (Bytes.of_string s) 0 n in
1959 if n != n'
1960 then Utils.error "Unix.write %d = %d" n n'
1962 let rd s n =
1963 try Unix.read fdi s 0 n
1964 with exn -> Utils.error "reading gc pipe %s" (exntos exn) in
1965 let href = ref (Hashtbl.create 0) in
1966 let cref = ref defconf in
1967 let push (h, dc) =
1968 let f path (pc, _pb, _px, _pa) =
1969 let s =
1970 Printf.sprintf "%s\000%ld\000" path (Int32.of_float pc.lastvisit)
1972 wr s (String.length s)
1974 Hashtbl.iter f h;
1975 href := h;
1976 cref := dc;
1977 Unix.shutdown fdo Unix.SHUTDOWN_SEND;
1978 true
1980 ignore (load1 push);
1981 let s =
1982 let b = Buffer.create 32768 in
1983 let s = Bytes.create 4096 in
1984 let rec f () =
1985 let n = rd s (Bytes.length s) in
1986 if n = 0
1987 then Buffer.contents b
1988 else (Buffer.add_subbytes b s 0 n; f ())
1990 f ()
1992 let rec f ppos =
1993 match String.index_from s ppos '\000' with
1994 | zpos1 ->
1995 let zpos2 =
1996 try String.index_from s (zpos1+1) '\000' with Not_found -> -1 in
1997 if zpos2 = -1
1998 then error "Incorrect gc input in (%S) at %d" s zpos1
1999 else
2000 let okey = StringLabels.sub s ~pos:ppos ~len:(zpos1-ppos) in
2001 let nkey = StringLabels.sub s ~pos:(zpos1+1) ~len:(zpos2-zpos1-1) in
2002 if emptystr nkey
2003 then (Hashtbl.remove !href okey; f (zpos2+1))
2004 else
2005 let v =
2006 try Hashtbl.find !href okey
2007 with Not_found -> Utils.error "gc: could not find %S" okey
2009 Hashtbl.remove !href okey;
2010 Hashtbl.replace !href nkey v;
2011 f (zpos2+1)
2012 | exception Not_found -> ()
2014 f 0;
2015 let bb = Buffer.create 32768 in
2016 let save2 (_h, dc) = save1 bb (fun _ _ -> ()) 0 !href dc in
2017 if load1 save2 && Buffer.length bb > 0
2018 then (
2020 let tmp = !confpath ^ ".tmp" in
2021 let oc = open_out_bin tmp in
2022 Buffer.output_buffer oc bb;
2023 close_out oc;
2024 Unix.rename tmp !confpath;
2025 with exn ->
2026 prerr_endline
2027 ("error while saving configuration: " ^ exntos exn)
2031 let logcurrently = function
2032 | Idle -> dolog "Idle"
2033 | Loading (l, gen) ->
2034 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2035 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2036 dolog
2037 "Tiling %d[%d,%d] page=%s cs=%s angle"
2038 l.pageno col row (~> pageopaque)
2039 (CSTE.to_string colorspace)
2041 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2042 angle gen conf.angle state.gen
2043 tilew tileh
2044 conf.tilew conf.tileh
2046 | Outlining _ ->
2047 dolog "outlining"