Avoid saving config for fairy files
[llpp.git] / config.ml
blobb1370277ceca939ef9af73ecd0c4a8fe016e230b
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 noghyll _ = ();;
455 let noreprf () = ();;
456 let noroam () = ();;
458 let nouioh : uioh = object (self)
459 method display = ()
460 method key _ _ = self
461 method multiclick _ _ _ _ = self
462 method button _ _ _ _ _ = self
463 method motion _ _ = self
464 method pmotion _ _ = self
465 method infochanged _ = ()
466 method scrollpw = (0, nan, nan)
467 method scrollph = (0, nan, nan)
468 method modehash = emptykeyhash
469 method eformsgs = false
470 method alwaysscrolly = false
471 end;;
473 let platform_to_string = function
474 | Punknown -> "unknown"
475 | Plinux -> "Linux"
476 | Posx -> "OSX"
477 | Psun -> "Sun"
478 | Pbsd -> "BSD"
479 | Pcygwin -> "Cygwin"
482 let version () =
483 Printf.sprintf "llpp version %s, fitz %s, ocaml %s/%d bit"
484 Help.version (fz_version ()) Sys.ocaml_version Sys.word_size
487 let geturl s =
488 let colonpos = try String.index s ':' with Not_found -> -1 in
489 let len = String.length s in
490 if colonpos >= 0 && colonpos + 3 < len
491 then (
492 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
493 then
494 let schemestartpos =
495 try String.rindex_from s colonpos ' '
496 with Not_found -> -1
498 let scheme =
499 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
501 match scheme with
502 | "http" | "ftp" | "mailto" ->
503 let epos =
504 try String.index_from s colonpos ' '
505 with Not_found -> len
507 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
508 | _ -> E.s
509 else E.s
511 else E.s
514 let defconf =
515 { scrollbw = 7
516 ; scrollh = 12
517 ; scrollb = scrollbhv lor scrollbvv
518 ; icase = true
519 ; preload = true
520 ; pagebias = 0
521 ; verbose = false
522 ; debug = false
523 ; scrollstep = 24
524 ; hscrollstep = 24
525 ; maxhfit = true
526 ; crophack = false
527 ; autoscrollstep = 2
528 ; maxwait = None
529 ; hlinks = false
530 ; underinfo = false
531 ; interpagespace = 2
532 ; zoom = 1.0
533 ; presentation = false
534 ; angle = 0
535 ; cwinw = 900
536 ; cwinh = 900
537 ; savebmarks = true
538 ; fitmodel = FitProportional
539 ; trimmargins = false
540 ; trimfuzz = (0,0,0,0)
541 ; memlimit = 32 lsl 20
542 ; texcount = 256
543 ; sliceheight = 24
544 ; thumbw = 76
545 ; jumpback = true
546 ; bgcolor = (0.5, 0.5, 0.5)
547 ; bedefault = false
548 ; tilew = 2048
549 ; tileh = 2048
550 ; mustoresize = 256 lsl 20
551 ; checkers = true
552 ; aalevel = 8
553 ; urilauncher =
554 (match platform with
555 | Plinux | Psun | Pbsd -> "xdg-open \"%s\""
556 | Posx -> "open \"%s\""
557 | Pcygwin -> "cygstart \"%s\""
558 | Punknown -> "echo %s")
559 ; pathlauncher = "lp \"%s\""
560 ; selcmd =
561 (match platform with
562 | Plinux | Pbsd | Psun -> "xsel -i"
563 | Posx -> "pbcopy"
564 | Pcygwin -> "wsel"
565 | Punknown -> "cat")
566 ; paxcmd = "cat"
567 ; passcmd = E.s
568 ; savecmd = E.s
569 ; colorspace = Rgb
570 ; invert = false
571 ; colorscale = 1.0
572 ; redirectstderr = false
573 ; ghyllscroll = None
574 ; columns = Csingle [||]
575 ; beyecolumns = None
576 ; updatecurs = false
577 ; hfsize = 12
578 ; pgscale = 1.0
579 ; usepbo = false
580 ; wheelbypage = false
581 ; stcmd = "echo SyncTex"
582 ; riani = false
583 ; pax = None
584 ; paxmark = Mark_word
585 ; leftscroll = false
586 ; title = E.s
587 ; lastvisit = 0.0
588 ; annotinline = true
589 ; keyhashes =
590 let mk n = (n, Hashtbl.create 1) in
591 [ mk "global"
592 ; mk "info"
593 ; mk "help"
594 ; mk "outline"
595 ; mk "listview"
596 ; mk "birdseye"
597 ; mk "textentry"
598 ; mk "links"
599 ; mk "view"
604 let conf = { defconf with angle = defconf.angle };;
606 let gotouri uri =
607 if emptystr conf.urilauncher
608 then print_endline uri
609 else (
610 let url = geturl uri in
611 if emptystr url
612 then (Printf.eprintf "obtained empty url from uri %S\n" uri)
613 else
614 let re = Str.regexp "%s" in
615 let command = Str.global_replace re url conf.urilauncher in
616 try ignore (popen command [])
617 with exn ->
618 Printf.eprintf
619 "failed to execute `%s': %s\n" command (exntos exn);
620 flush stderr;
624 let makehelp () =
625 let strings =
626 version ()
627 :: "(searching in this text works just by typing (i.e. no initial '/'))"
628 :: E.s :: Help.keys
630 Array.of_list (
631 List.map (fun s ->
632 let url = geturl s in
633 if nonemptystr url
634 then (s, 0, Action (fun u -> gotouri url; u))
635 else (s, 0, Noaction)
636 ) strings);
639 let cbnew n v =
640 { store = Array.make n v
641 ; rc = 0
642 ; wc = 0
643 ; len = 0
647 let cbcap b = Array.length b.store;;
649 let cbput b v =
650 let cap = cbcap b in
651 b.store.(b.wc) <- v;
652 b.wc <- (b.wc + 1) mod cap;
653 b.rc <- b.wc;
654 b.len <- min (b.len + 1) cap;
657 let cbempty b = b.len = 0;;
659 let cbgetg b circular dir =
660 if cbempty b
661 then b.store.(0)
662 else
663 let rc = b.rc + dir in
664 let rc =
665 if circular
666 then (
667 if rc = -1
668 then b.len-1
669 else (
670 if rc >= b.len
671 then 0
672 else rc
675 else bound rc 0 (b.len-1)
677 b.rc <- rc;
678 b.store.(rc);
681 let cbget b = cbgetg b false;;
682 let cbgetc b = cbgetg b true;;
684 let state =
685 { ss = Unix.stdin
686 ; wsfd = Unix.stdin
687 ; errfd = None
688 ; stderr = Unix.stderr
689 ; errmsgs = Buffer.create 0
690 ; newerrmsgs = false
691 ; x = 0
692 ; y = 0
693 ; w = 0
694 ; anchor = emptyanchor
695 ; ranchors = []
696 ; layout = []
697 ; maxy = max_int
698 ; tilelru = Queue.create ()
699 ; pagemap = Hashtbl.create 10
700 ; tilemap = Hashtbl.create 10
701 ; pdims = []
702 ; pagecount = 0
703 ; currently = Idle
704 ; mstate = Mnone
705 ; rects = []
706 ; rects1 = []
707 ; text = E.s
708 ; mode = View
709 ; winstate = []
710 ; searchpattern = E.s
711 ; outlines = E.a
712 ; bookmarks = []
713 ; path = E.s
714 ; password = E.s
715 ; nameddest = E.s
716 ; geomcmds = E.s, []
717 ; hists =
718 { nav = cbnew 10 emptyanchor
719 ; pat = cbnew 10 E.s
720 ; pag = cbnew 10 E.s
721 ; sel = cbnew 10 E.s
723 ; memused = 0
724 ; gen = 0
725 ; throttle = None
726 ; autoscroll = None
727 ; ghyll = noghyll
728 ; help = makehelp ()
729 ; docinfo = []
730 ; checkerstexid = None
731 ; prevzoom = (1.0, 0)
732 ; progress = -1.0
733 ; uioh = nouioh
734 ; redisplay = true
735 ; mpos = (-1, -1)
736 ; keystate = KSnone
737 ; glinks = false
738 ; prevcolumns = None
739 ; winw = -1
740 ; winh = -1
741 ; reprf = noreprf
742 ; origin = E.s
743 ; roam = noroam
744 ; bzoom = false
745 ; traw = Raw.create_static `float ~len:8
746 ; vraw = Raw.create_static `float ~len:8
750 let copykeyhashes c =
751 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
754 let calcips h =
755 let d = state.winh - h in
756 max conf.interpagespace ((d + 1) / 2)
759 let rowyh (c, coverA, coverB) b n =
760 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
761 then
762 let _, _, vy, (_, _, h, _) = b.(n) in
763 (vy, h)
764 else
765 let n' = n - coverA in
766 let d = n' mod c in
767 let s = n - d in
768 let e = min state.pagecount (s + c) in
769 let rec find m miny maxh = if m = e then miny, maxh else
770 let _, _, y, (_, _, h, _) = b.(m) in
771 let miny = min miny y in
772 let maxh = max maxh h in
773 find (m+1) miny maxh
774 in find s max_int 0
777 let page_of_y y =
778 let ((c, coverA, coverB) as cl), b =
779 match conf.columns with
780 | Csingle b -> (1, 0, 0), b
781 | Cmulti (c, b) -> c, b
782 | Csplit (_, b) -> (1, 0, 0), b
784 if Array.length b = 0
785 then -1
786 else
787 let rec bsearch nmin nmax =
788 if nmin > nmax
789 then bound nmin 0 (state.pagecount-1)
790 else
791 let n = (nmax + nmin) / 2 in
792 let vy, h = rowyh cl b n in
793 let y0, y1 =
794 if conf.presentation
795 then
796 let ips = calcips h in
797 let y0 = vy - ips in
798 let y1 = vy + h + ips in
799 y0, y1
800 else (
801 if n = 0
802 then 0, vy + h + conf.interpagespace
803 else
804 let y0 = vy - conf.interpagespace in
805 y0, y0 + h + conf.interpagespace
808 if y >= y0 && y < y1
809 then (
810 if c = 1
811 then n
812 else (
813 if n > coverA
814 then
815 if n < state.pagecount - coverB
816 then ((n-coverA)/c)*c + coverA
817 else n
818 else n
821 else (
822 if y > y0
823 then bsearch (n+1) nmax
824 else bsearch nmin (n-1)
827 bsearch 0 (state.pagecount-1);
830 let calcheight () =
831 match conf.columns with
832 | Cmulti ((_, _, _) as cl, b) ->
833 if Array.length b > 0
834 then
835 let y, h = rowyh cl b (Array.length b - 1) in
836 y + h + (if conf.presentation then calcips h else 0)
837 else 0
838 | Csingle b ->
839 if Array.length b > 0
840 then
841 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
842 y + h + (if conf.presentation then calcips h else 0)
843 else 0
844 | Csplit (_, b) ->
845 if Array.length b > 0
846 then
847 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
848 y + h
849 else 0
852 let getpageywh pageno =
853 let pageno = bound pageno 0 (state.pagecount-1) in
854 match conf.columns with
855 | Csingle b ->
856 if Array.length b = 0
857 then 0, 0, 0
858 else
859 let (_, _, y, (_, w, h, _)) = b.(pageno) in
860 let y =
861 if conf.presentation
862 then y - calcips h
863 else y
865 y, w, h
866 | Cmulti (cl, b) ->
867 if Array.length b = 0
868 then 0, 0, 0
869 else
870 let y, h = rowyh cl b pageno in
871 let (_, _, _, (_, w, _, _)) = b.(pageno) in
872 let y =
873 if conf.presentation
874 then y - calcips h
875 else y
877 y, w, h
878 | Csplit (c, b) ->
879 if Array.length b = 0
880 then 0, 0, 0
881 else
882 let n = pageno*c in
883 let (_, _, y, (_, w, h, _)) = b.(n) in
884 y, w / c, h
887 let getpageyh pageno =
888 let y,_,h = getpageywh pageno in
889 y, h;
892 let getpagedim pageno =
893 let rec f ppdim l =
894 match l with
895 | (n, _, _, _) as pdim :: rest ->
896 if n >= pageno
897 then (if n = pageno then pdim else ppdim)
898 else f pdim rest
900 | [] -> ppdim
902 f (-1, -1, -1, -1) state.pdims
905 let getpagey pageno = fst (getpageyh pageno);;
907 let getanchor1 l =
908 let top =
909 let coloff = l.pagecol * l.pageh in
910 float (l.pagey + coloff) /. float l.pageh
912 let dtop =
913 if l.pagedispy = 0
914 then
916 else (
917 if conf.presentation
918 then float l.pagedispy /. float (calcips l.pageh)
919 else float l.pagedispy /. float conf.interpagespace
922 (l.pageno, top, dtop)
925 let getanchor () =
926 match state.layout with
927 | l :: _ -> getanchor1 l
928 | [] ->
929 let n = page_of_y state.y in
930 if n = -1
931 then state.anchor
932 else
933 let y, h = getpageyh n in
934 let dy = y - state.y in
935 let dtop =
936 if conf.presentation
937 then
938 let ips = calcips h in
939 float (dy + ips) /. float ips
940 else
941 float dy /. float conf.interpagespace
943 (n, 0.0, dtop)
946 let fontpath = ref E.s;;
948 type historder = [ `lastvisit | `title | `path | `file ];;
949 let historder : historder ref = ref `lastvisit;;
951 module KeyMap =
952 Map.Make (struct type t = (int * int) let compare = compare end);;
954 open Parser;;
956 let unent s =
957 let l = String.length s in
958 let b = Buffer.create l in
959 unent b s 0 l;
960 Buffer.contents b;
963 let home =
964 try Sys.getenv "HOME"
965 with exn ->
966 prerr_endline
967 ("Can not determine home directory location: " ^ exntos exn);
971 let modifier_of_string = function
972 | "alt" -> Wsi.altmask
973 | "shift" -> Wsi.shiftmask
974 | "ctrl" | "control" -> Wsi.ctrlmask
975 | "meta" -> Wsi.metamask
976 | _ -> 0
979 let key_of_string =
980 let r = Str.regexp "-" in
981 fun s ->
982 let elems = Str.full_split r s in
983 let f n k m =
984 let g s =
985 let m1 = modifier_of_string s in
986 if m1 = 0
987 then (Wsi.namekey s, m)
988 else (k, m lor m1)
989 in function
990 | Str.Delim s when n land 1 = 0 -> g s
991 | Str.Text s -> g s
992 | Str.Delim _ -> (k, m)
994 let rec loop n k m = function
995 | [] -> (k, m)
996 | x :: xs ->
997 let k, m = f n k m x in
998 loop (n+1) k m xs
1000 loop 0 0 0 elems
1003 let keys_of_string =
1004 let r = Str.regexp "[ \t]" in
1005 fun s ->
1006 let elems = Str.split r s in
1007 List.map key_of_string elems
1010 let config_of c attrs =
1011 let apply c k v =
1013 match k with
1014 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
1015 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
1016 | "case-insensitive-search" -> { c with icase = bool_of_string v }
1017 | "preload" -> { c with preload = bool_of_string v }
1018 | "page-bias" -> { c with pagebias = int_of_string v }
1019 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
1020 | "horizontal-scroll-step" ->
1021 { c with hscrollstep = max (int_of_string v) 1 }
1022 | "auto-scroll-step" ->
1023 { c with autoscrollstep = max 0 (int_of_string v) }
1024 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
1025 | "crop-hack" -> { c with crophack = bool_of_string v }
1026 | "throttle" ->
1027 let mw =
1028 match String.lowercase v with
1029 | "true" -> Some infinity
1030 | "false" -> None
1031 | f -> Some (float_of_string f)
1033 { c with maxwait = mw}
1034 | "highlight-links" -> { c with hlinks = bool_of_string v }
1035 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
1036 | "vertical-margin" ->
1037 { c with interpagespace = max 0 (int_of_string v) }
1038 | "zoom" ->
1039 let zoom = float_of_string v /. 100. in
1040 let zoom = max zoom 0.0 in
1041 { c with zoom = zoom }
1042 | "presentation" -> { c with presentation = bool_of_string v }
1043 | "rotation-angle" -> { c with angle = int_of_string v }
1044 | "width" -> { c with cwinw = max 20 (int_of_string v) }
1045 | "height" -> { c with cwinh = max 20 (int_of_string v) }
1046 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
1047 | "proportional-display" ->
1048 let fm =
1049 if bool_of_string v
1050 then FitProportional
1051 else FitWidth
1053 { c with fitmodel = fm }
1054 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
1055 | "pixmap-cache-size" ->
1056 { c with memlimit = max 2 (int_of_string_with_suffix v) }
1057 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
1058 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
1059 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
1060 | "persistent-location" -> { c with jumpback = bool_of_string v }
1061 | "background-color" -> { c with bgcolor = color_of_string v }
1062 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
1063 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
1064 | "mupdf-store-size" ->
1065 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
1066 | "checkers" -> { c with checkers = bool_of_string v }
1067 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
1068 | "trim-margins" -> { c with trimmargins = bool_of_string v }
1069 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
1070 | "uri-launcher" -> { c with urilauncher = unent v }
1071 | "path-launcher" -> { c with pathlauncher = unent v }
1072 | "color-space" -> { c with colorspace = CSTE.of_string v }
1073 | "invert-colors" -> { c with invert = bool_of_string v }
1074 | "brightness" -> { c with colorscale = float_of_string v }
1075 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
1076 | "ghyllscroll" -> { c with ghyllscroll = ghyllscroll_of_string v }
1077 | "columns" ->
1078 let (n, _, _) as nab = multicolumns_of_string v in
1079 if n < 0
1080 then { c with columns = Csplit (-n, E.a) }
1081 else { c with columns = Cmulti (nab, E.a) }
1082 | "birds-eye-columns" ->
1083 { c with beyecolumns = Some (max (int_of_string v) 2) }
1084 | "selection-command" -> { c with selcmd = unent v }
1085 | "synctex-command" -> { c with stcmd = unent v }
1086 | "pax-command" -> { c with paxcmd = unent v }
1087 | "askpass-command" -> { c with passcmd = unent v }
1088 | "savepath-command" -> { c with savecmd = unent v }
1089 | "update-cursor" -> { c with updatecurs = bool_of_string v }
1090 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
1091 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
1092 | "use-pbo" -> { c with usepbo = bool_of_string v }
1093 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
1094 | "horizontal-scrollbar-visible" ->
1095 let b =
1096 if bool_of_string v
1097 then c.scrollb lor scrollbhv
1098 else c.scrollb land (lnot scrollbhv)
1100 { c with scrollb = b }
1101 | "vertical-scrollbar-visible" ->
1102 let b =
1103 if bool_of_string v
1104 then c.scrollb lor scrollbvv
1105 else c.scrollb land (lnot scrollbvv)
1107 { c with scrollb = b }
1108 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
1109 | "point-and-x" ->
1110 { c with pax =
1111 if bool_of_string v
1112 then Some (ref (0.0, 0, 0))
1113 else None }
1114 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
1115 | "scroll-bar-on-the-left" -> { c with leftscroll = bool_of_string v }
1116 | "title" -> { c with title = unent v }
1117 | "last-visit" -> { c with lastvisit = float_of_string v }
1118 | "edit-annots-inline" -> { c with annotinline = bool_of_string v }
1119 | _ -> c
1120 with exn ->
1121 prerr_endline ("Error processing attribute (`" ^
1122 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
1125 let rec fold c = function
1126 | [] -> c
1127 | (k, v) :: rest ->
1128 let c = apply c k v in
1129 fold c rest
1131 fold { c with keyhashes = copykeyhashes c } attrs;
1134 let fromstring f pos n v d =
1135 try f v
1136 with exn ->
1137 dolog "Error processing attribute (%S=%S) at %d\n%s"
1138 n v pos (exntos exn)
1143 let bookmark_of attrs =
1144 let rec fold title page rely visy = function
1145 | ("title", v) :: rest -> fold v page rely visy rest
1146 | ("page", v) :: rest -> fold title v rely visy rest
1147 | ("rely", v) :: rest -> fold title page v visy rest
1148 | ("visy", v) :: rest -> fold title page rely v rest
1149 | _ :: rest -> fold title page rely visy rest
1150 | [] -> title, page, rely, visy
1152 fold "invalid" "0" "0" "0" attrs
1155 let doc_of attrs =
1156 let rec fold path page rely pan visy = function
1157 | ("path", v) :: rest -> fold v page rely pan visy rest
1158 | ("page", v) :: rest -> fold path v rely pan visy rest
1159 | ("rely", v) :: rest -> fold path page v pan visy rest
1160 | ("pan", v) :: rest -> fold path page rely v visy rest
1161 | ("visy", v) :: rest -> fold path page rely pan v rest
1162 | _ :: rest -> fold path page rely pan visy rest
1163 | [] -> path, page, rely, pan, visy
1165 fold E.s "0" "0" "0" "0" attrs
1168 let map_of attrs =
1169 let rec fold rs ls = function
1170 | ("out", v) :: rest -> fold v ls rest
1171 | ("in", v) :: rest -> fold rs v rest
1172 | _ :: rest -> fold ls rs rest
1173 | [] -> ls, rs
1175 fold E.s E.s attrs
1178 let setconf dst src =
1179 dst.scrollbw <- src.scrollbw;
1180 dst.scrollh <- src.scrollh;
1181 dst.icase <- src.icase;
1182 dst.preload <- src.preload;
1183 dst.pagebias <- src.pagebias;
1184 dst.verbose <- src.verbose;
1185 dst.scrollstep <- src.scrollstep;
1186 dst.maxhfit <- src.maxhfit;
1187 dst.crophack <- src.crophack;
1188 dst.autoscrollstep <- src.autoscrollstep;
1189 dst.maxwait <- src.maxwait;
1190 dst.hlinks <- src.hlinks;
1191 dst.underinfo <- src.underinfo;
1192 dst.interpagespace <- src.interpagespace;
1193 dst.zoom <- src.zoom;
1194 dst.presentation <- src.presentation;
1195 dst.angle <- src.angle;
1196 dst.cwinw <- src.cwinw;
1197 dst.cwinh <- src.cwinh;
1198 dst.savebmarks <- src.savebmarks;
1199 dst.memlimit <- src.memlimit;
1200 dst.fitmodel <- src.fitmodel;
1201 dst.texcount <- src.texcount;
1202 dst.sliceheight <- src.sliceheight;
1203 dst.thumbw <- src.thumbw;
1204 dst.jumpback <- src.jumpback;
1205 dst.bgcolor <- src.bgcolor;
1206 dst.tilew <- src.tilew;
1207 dst.tileh <- src.tileh;
1208 dst.mustoresize <- src.mustoresize;
1209 dst.checkers <- src.checkers;
1210 dst.aalevel <- src.aalevel;
1211 dst.trimmargins <- src.trimmargins;
1212 dst.trimfuzz <- src.trimfuzz;
1213 dst.urilauncher <- src.urilauncher;
1214 dst.colorspace <- src.colorspace;
1215 dst.invert <- src.invert;
1216 dst.colorscale <- src.colorscale;
1217 dst.redirectstderr <- src.redirectstderr;
1218 dst.ghyllscroll <- src.ghyllscroll;
1219 dst.columns <- src.columns;
1220 dst.beyecolumns <- src.beyecolumns;
1221 dst.selcmd <- src.selcmd;
1222 dst.updatecurs <- src.updatecurs;
1223 dst.pathlauncher <- src.pathlauncher;
1224 dst.keyhashes <- copykeyhashes src;
1225 dst.hfsize <- src.hfsize;
1226 dst.hscrollstep <- src.hscrollstep;
1227 dst.pgscale <- src.pgscale;
1228 dst.usepbo <- src.usepbo;
1229 dst.wheelbypage <- src.wheelbypage;
1230 dst.stcmd <- src.stcmd;
1231 dst.paxcmd <- src.paxcmd;
1232 dst.passcmd <- src.passcmd;
1233 dst.savecmd <- src.savecmd;
1234 dst.scrollb <- src.scrollb;
1235 dst.riani <- src.riani;
1236 dst.paxmark <- src.paxmark;
1237 dst.leftscroll <- src.leftscroll;
1238 dst.title <- src.title;
1239 dst.annotinline <- src.annotinline;
1240 dst.pax <-
1241 if src.pax = None
1242 then None
1243 else Some ((ref (0.0, 0, 0)));
1246 let findkeyhash c name =
1247 try List.assoc name c.keyhashes
1248 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
1251 let get s =
1252 let h = Hashtbl.create 10 in
1253 let dc = { defconf with angle = defconf.angle } in
1254 let rec toplevel v t spos _ =
1255 match t with
1256 | Vdata | Vcdata | Vend -> v
1257 | Vopen ("llppconfig", _, closed) ->
1258 if closed
1259 then v
1260 else { v with f = llppconfig }
1261 | Vopen _ ->
1262 error "unexpected subelement at top level" s spos
1263 | Vclose _ -> error "unexpected close at top level" s spos
1265 and llppconfig v t spos _ =
1266 match t with
1267 | Vdata | Vcdata -> v
1268 | Vend -> error "unexpected end of input in llppconfig" s spos
1269 | Vopen ("defaults", attrs, closed) ->
1270 let c = config_of dc attrs in
1271 setconf dc c;
1272 if closed
1273 then v
1274 else { v with f = defaults }
1276 | Vopen ("ui-font", attrs, closed) ->
1277 let rec getsize size = function
1278 | [] -> size
1279 | ("size", v) :: rest ->
1280 let size =
1281 fromstring int_of_string spos "size" v fstate.fontsize in
1282 getsize size rest
1283 | l -> getsize size l
1285 fstate.fontsize <- getsize fstate.fontsize attrs;
1286 if closed
1287 then v
1288 else { v with f = uifont (Buffer.create 10) }
1290 | Vopen ("doc", attrs, closed) ->
1291 let pathent, spage, srely, span, svisy = doc_of attrs in
1292 let path = unent pathent
1293 and pageno = fromstring int_of_string spos "page" spage 0
1294 and rely = fromstring float_of_string spos "rely" srely 0.0
1295 and pan = fromstring int_of_string spos "pan" span 0
1296 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1297 let c = config_of dc attrs in
1298 let anchor = (pageno, rely, visy) in
1299 if closed
1300 then (Hashtbl.add h path (c, [], pan, anchor); v)
1301 else { v with f = doc path pan anchor c [] }
1303 | Vopen _ ->
1304 error "unexpected subelement in llppconfig" s spos
1306 | Vclose "llppconfig" -> { v with f = toplevel }
1307 | Vclose _ -> error "unexpected close in llppconfig" s spos
1309 and defaults v t spos _ =
1310 match t with
1311 | Vdata | Vcdata -> v
1312 | Vend -> error "unexpected end of input in defaults" s spos
1313 | Vopen ("keymap", attrs, closed) ->
1314 let modename =
1315 try List.assoc "mode" attrs
1316 with Not_found -> "global" in
1317 if closed
1318 then v
1319 else
1320 let ret keymap =
1321 let h = findkeyhash dc modename in
1322 KeyMap.iter (Hashtbl.replace h) keymap;
1323 defaults
1325 { v with f = pkeymap ret KeyMap.empty }
1327 | Vopen (_, _, _) ->
1328 error "unexpected subelement in defaults" s spos
1330 | Vclose "defaults" ->
1331 { v with f = llppconfig }
1333 | Vclose _ -> error "unexpected close in defaults" s spos
1335 and uifont b v t spos epos =
1336 match t with
1337 | Vdata | Vcdata ->
1338 Buffer.add_substring b s spos (epos - spos);
1340 | Vopen (_, _, _) ->
1341 error "unexpected subelement in ui-font" s spos
1342 | Vclose "ui-font" ->
1343 if emptystr !fontpath
1344 then fontpath := Buffer.contents b;
1345 { v with f = llppconfig }
1346 | Vclose _ -> error "unexpected close in ui-font" s spos
1347 | Vend -> error "unexpected end of input in ui-font" s spos
1349 and doc path pan anchor c bookmarks v t spos _ =
1350 match t with
1351 | Vdata | Vcdata -> v
1352 | Vend -> error "unexpected end of input in doc" s spos
1353 | Vopen ("bookmarks", _, closed) ->
1354 if closed
1355 then v
1356 else { v with f = pbookmarks path pan anchor c bookmarks }
1358 | Vopen ("keymap", attrs, closed) ->
1359 let modename =
1360 try List.assoc "mode" attrs
1361 with Not_found -> "global"
1363 if closed
1364 then v
1365 else
1366 let ret keymap =
1367 let h = findkeyhash c modename in
1368 KeyMap.iter (Hashtbl.replace h) keymap;
1369 doc path pan anchor c bookmarks
1371 { v with f = pkeymap ret KeyMap.empty }
1373 | Vopen (_, _, _) ->
1374 error "unexpected subelement in doc" s spos
1376 | Vclose "doc" ->
1377 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
1378 { v with f = llppconfig }
1380 | Vclose _ -> error "unexpected close in doc" s spos
1382 and pkeymap ret keymap v t spos _ =
1383 match t with
1384 | Vdata | Vcdata -> v
1385 | Vend -> error "unexpected end of input in keymap" s spos
1386 | Vopen ("map", attrs, closed) ->
1387 let r, l = map_of attrs in
1388 let kss = fromstring keys_of_string spos "in" r [] in
1389 let lss = fromstring keys_of_string spos "out" l [] in
1390 let keymap =
1391 match kss with
1392 | [] -> keymap
1393 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
1394 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
1396 if closed
1397 then { v with f = pkeymap ret keymap }
1398 else
1399 let f () = v in
1400 { v with f = skip "map" f }
1402 | Vopen _ ->
1403 error "unexpected subelement in keymap" s spos
1405 | Vclose "keymap" ->
1406 { v with f = ret keymap }
1408 | Vclose _ -> error "unexpected close in keymap" s spos
1410 and pbookmarks path pan anchor c bookmarks v t spos _ =
1411 match t with
1412 | Vdata | Vcdata -> v
1413 | Vend -> error "unexpected end of input in bookmarks" s spos
1414 | Vopen ("item", attrs, closed) ->
1415 let titleent, spage, srely, svisy = bookmark_of attrs in
1416 let page = fromstring int_of_string spos "page" spage 0
1417 and rely = fromstring float_of_string spos "rely" srely 0.0
1418 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1419 let bookmarks =
1420 (unent titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
1422 if closed
1423 then { v with f = pbookmarks path pan anchor c bookmarks }
1424 else
1425 let f () = v in
1426 { v with f = skip "item" f }
1428 | Vopen _ ->
1429 error "unexpected subelement in bookmarks" s spos
1431 | Vclose "bookmarks" ->
1432 { v with f = doc path pan anchor c bookmarks }
1434 | Vclose _ -> Parser.parse_error "unexpected close in bookmarks" s spos
1436 and skip tag f v t spos _ =
1437 match t with
1438 | Vdata | Vcdata -> v
1439 | Vend ->
1440 Parser.parse_error ("unexpected end of input in skipped " ^ tag) s spos
1441 | Vopen (tag', _, closed) ->
1442 if closed
1443 then v
1444 else
1445 let f' () = { v with f = skip tag f } in
1446 { v with f = skip tag' f' }
1447 | Vclose ctag ->
1448 if tag = ctag
1449 then f ()
1450 else Parser.parse_error ("unexpected close in skipped " ^ tag) s spos
1453 parse { f = toplevel; accu = () } s;
1454 h, dc;
1457 let do_load f ic =
1459 let len = in_channel_length ic in
1460 let s = really_input_string ic len in
1461 f s;
1462 with
1463 | Parse_error (msg, s, pos) ->
1464 let subs = subs s pos in
1465 Utils.error "parse error: %s: at %d [..%s..]" msg pos subs
1467 | exn ->
1468 failwith ("config load error: " ^ exntos exn)
1471 let defconfpath =
1472 let dir =
1473 let xdgconfdir = Utils.getenvwithdef "XDG_CONFIG_HOME" E.s in
1474 if xdgconfdir == E.s
1475 then
1477 let dir = Filename.concat home ".config" in
1478 if Sys.is_directory dir then dir else home
1479 with _ -> home
1480 else xdgconfdir
1482 Filename.concat dir "llpp.conf"
1485 let confpath = ref defconfpath;;
1487 let load1 f =
1488 if Sys.file_exists !confpath
1489 then
1490 match
1491 (try Some (open_in_bin !confpath)
1492 with exn ->
1493 prerr_endline
1494 ("Error opening configuration file `" ^ !confpath ^ "': " ^
1495 exntos exn);
1496 None
1498 with
1499 | Some ic ->
1500 let success =
1502 f (do_load get ic)
1503 with exn ->
1504 prerr_endline
1505 ("Error loading configuration from `" ^ !confpath ^ "': " ^
1506 exntos exn);
1507 false
1509 close_in ic;
1510 success
1512 | None -> false
1513 else
1514 f (Hashtbl.create 0, defconf)
1517 let load openlast =
1518 let f (h, dc) =
1519 if openlast
1520 then (
1521 let path, _ =
1522 Hashtbl.fold
1523 (fun path (conf, _, _, _) ((_, besttime) as best) ->
1524 if conf.lastvisit > besttime
1525 then (path, conf.lastvisit)
1526 else best)
1528 (state.path, -.infinity)
1530 state.path <- path;
1532 let pc, pb, px, pa =
1534 let absname = abspath state.path in
1535 Hashtbl.find h absname
1536 with Not_found -> dc, [], 0, emptyanchor
1538 setconf defconf dc;
1539 setconf conf pc;
1540 state.bookmarks <- pb;
1541 state.x <- px;
1542 if conf.jumpback
1543 then state.anchor <- pa;
1544 cbput state.hists.nav pa;
1545 true
1547 load1 f
1550 let gethist listref =
1551 let f (h, _) =
1552 listref :=
1553 Hashtbl.fold (fun path (pc, pb, px, pa) accu ->
1554 (path, pc, pb, px, pa) :: accu)
1555 h [];
1556 true
1558 load1 f
1561 let add_attrs bb always dc c time =
1562 let ob s a b =
1563 if always || a != b
1564 then Printf.bprintf bb "\n %s='%b'" s a
1565 and op s a b =
1566 if always || a <> b
1567 then Printf.bprintf bb "\n %s='%b'" s (a != None)
1568 and oi s a b =
1569 if always || a != b
1570 then Printf.bprintf bb "\n %s='%d'" s a
1571 and oI s a b =
1572 if always || a != b
1573 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
1574 and oz s a b =
1575 if always || a <> b
1576 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
1577 and oF s a b =
1578 if always || a <> b
1579 then Printf.bprintf bb "\n %s='%f'" s a
1580 and oL s a b =
1581 if always || a <> b
1582 then Printf.bprintf bb "\n %s='%Ld'" s a
1583 and oc s a b =
1584 if always || a <> b
1585 then Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
1586 and oC s a b =
1587 if always || a <> b
1588 then Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
1589 and oR s a b =
1590 if always || a <> b
1591 then Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
1592 and os s a b =
1593 if always || a <> b
1594 then Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
1595 and og s a b =
1596 if always || a <> b
1597 then
1598 match a with
1599 | Some (_N, _A, _B) ->
1600 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
1601 | None ->
1602 match b with
1603 | None -> ()
1604 | _ ->
1605 Printf.bprintf bb "\n %s='none'" s
1606 and oW s a b =
1607 if always || a <> b
1608 then
1609 let v =
1610 match a with
1611 | None -> "false"
1612 | Some f ->
1613 if f = infinity
1614 then "true"
1615 else string_of_float f
1617 Printf.bprintf bb "\n %s='%s'" s v
1618 and oco s a b =
1619 if always || a <> b
1620 then
1621 match a with
1622 | Cmulti ((n, a, b), _) when n > 1 ->
1623 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
1624 | Csplit (n, _) when n > 1 ->
1625 Printf.bprintf bb "\n %s='%d'" s ~-n
1626 | Cmulti _ | Csplit _ | Csingle _ -> ()
1627 and obeco s a b =
1628 if always || a <> b
1629 then
1630 match a with
1631 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
1632 | _ -> ()
1633 and oFm s a b =
1634 if always || a <> b
1635 then Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
1636 and oSv s a b m =
1637 if always || a <> b
1638 then Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
1639 and oPm s a b =
1640 if always || a <> b
1641 then Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
1643 oi "width" c.cwinw dc.cwinw;
1644 oi "height" c.cwinh dc.cwinh;
1645 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
1646 oi "scroll-handle-height" c.scrollh dc.scrollh;
1647 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
1648 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
1649 ob "case-insensitive-search" c.icase dc.icase;
1650 ob "preload" c.preload dc.preload;
1651 oi "page-bias" c.pagebias dc.pagebias;
1652 oi "scroll-step" c.scrollstep dc.scrollstep;
1653 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
1654 ob "max-height-fit" c.maxhfit dc.maxhfit;
1655 ob "crop-hack" c.crophack dc.crophack;
1656 oW "throttle" c.maxwait dc.maxwait;
1657 ob "highlight-links" c.hlinks dc.hlinks;
1658 ob "under-cursor-info" c.underinfo dc.underinfo;
1659 oi "vertical-margin" c.interpagespace dc.interpagespace;
1660 oz "zoom" c.zoom dc.zoom;
1661 ob "presentation" c.presentation dc.presentation;
1662 oi "rotation-angle" c.angle dc.angle;
1663 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
1664 oFm "fit-model" c.fitmodel dc.fitmodel;
1665 oI "pixmap-cache-size" c.memlimit dc.memlimit;
1666 oi "tex-count" c.texcount dc.texcount;
1667 oi "slice-height" c.sliceheight dc.sliceheight;
1668 oi "thumbnail-width" c.thumbw dc.thumbw;
1669 ob "persistent-location" c.jumpback dc.jumpback;
1670 oc "background-color" c.bgcolor dc.bgcolor;
1671 oi "tile-width" c.tilew dc.tilew;
1672 oi "tile-height" c.tileh dc.tileh;
1673 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
1674 ob "checkers" c.checkers dc.checkers;
1675 oi "aalevel" c.aalevel dc.aalevel;
1676 ob "trim-margins" c.trimmargins dc.trimmargins;
1677 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
1678 os "uri-launcher" c.urilauncher dc.urilauncher;
1679 os "path-launcher" c.pathlauncher dc.pathlauncher;
1680 oC "color-space" c.colorspace dc.colorspace;
1681 ob "invert-colors" c.invert dc.invert;
1682 oF "brightness" c.colorscale dc.colorscale;
1683 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
1684 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
1685 oco "columns" c.columns dc.columns;
1686 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
1687 os "selection-command" c.selcmd dc.selcmd;
1688 os "synctex-command" c.stcmd dc.stcmd;
1689 os "pax-command" c.paxcmd dc.paxcmd;
1690 os "askpass-command" c.passcmd dc.passcmd;
1691 os "savepath-command" c.savecmd dc.savecmd;
1692 ob "update-cursor" c.updatecurs dc.updatecurs;
1693 oi "hint-font-size" c.hfsize dc.hfsize;
1694 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
1695 oF "page-scroll-scale" c.pgscale dc.pgscale;
1696 ob "use-pbo" c.usepbo dc.usepbo;
1697 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
1698 ob "remote-in-a-new-instance" c.riani dc.riani;
1699 op "point-and-x" c.pax dc.pax;
1700 oPm "point-and-x-mark" c.paxmark dc.paxmark;
1701 ob "scroll-bar-on-the-left" c.leftscroll dc.leftscroll;
1702 if not always
1703 then os "title" c.title dc.title;
1704 oL "last-visit" (Int64.of_float time) 0L;
1705 ob "edit-annotations-inline" c.annotinline dc.annotinline;
1708 let keymapsbuf always dc c =
1709 let bb = Buffer.create 16 in
1710 let rec loop = function
1711 | [] -> ()
1712 | (modename, h) :: rest ->
1713 let dh = findkeyhash dc modename in
1714 if always || h <> dh
1715 then (
1716 if Hashtbl.length h > 0
1717 then (
1718 if Buffer.length bb > 0
1719 then Buffer.add_char bb '\n';
1720 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
1721 Hashtbl.iter (fun i o ->
1722 let isdifferent = always ||
1724 let dO = Hashtbl.find dh i in
1725 dO <> o
1726 with Not_found -> true
1728 if isdifferent
1729 then
1730 let addkm (k, m) =
1731 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
1732 if Wsi.withalt m then Buffer.add_string bb "alt-";
1733 if Wsi.withshift m then Buffer.add_string bb "shift-";
1734 if Wsi.withmeta m then Buffer.add_string bb "meta-";
1735 Buffer.add_string bb (Wsi.keyname k);
1737 let addkms l =
1738 let rec loop = function
1739 | [] -> ()
1740 | km :: [] -> addkm km
1741 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
1743 loop l
1745 Buffer.add_string bb "<map in='";
1746 addkm i;
1747 match o with
1748 | KMinsrt km ->
1749 Buffer.add_string bb "' out='";
1750 addkm km;
1751 Buffer.add_string bb "'/>\n"
1753 | KMinsrl kms ->
1754 Buffer.add_string bb "' out='";
1755 addkms kms;
1756 Buffer.add_string bb "'/>\n"
1758 | KMmulti (ins, kms) ->
1759 Buffer.add_char bb ' ';
1760 addkms ins;
1761 Buffer.add_string bb "' out='";
1762 addkms kms;
1763 Buffer.add_string bb "'/>\n"
1764 ) h;
1765 Buffer.add_string bb "</keymap>";
1768 loop rest
1770 loop c.keyhashes;
1774 let save1 bb leavebirdseye x h dc =
1775 let uifontsize = fstate.fontsize in
1776 let dc = if conf.bedefault then conf else dc in
1777 Buffer.add_string bb "<llppconfig>\n";
1779 if nonemptystr !fontpath
1780 then
1781 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
1782 uifontsize
1783 !fontpath
1784 else (
1785 if uifontsize <> 14
1786 then
1787 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
1790 Buffer.add_string bb "<defaults";
1791 add_attrs bb true dc dc nan;
1792 let kb = keymapsbuf true dc dc in
1793 if Buffer.length kb > 0
1794 then (
1795 Buffer.add_string bb ">\n";
1796 Buffer.add_buffer bb kb;
1797 Buffer.add_string bb "\n</defaults>\n";
1799 else Buffer.add_string bb "/>\n";
1801 let adddoc path pan anchor c bookmarks time =
1802 if bookmarks == [] && c = dc && anchor = emptyanchor
1803 then ()
1804 else (
1805 Printf.bprintf bb "<doc path='%s'"
1806 (enent path 0 (String.length path));
1808 if anchor <> emptyanchor
1809 then (
1810 let n, rely, visy = anchor in
1811 Printf.bprintf bb "\n page='%d'" n;
1812 if rely > 1e-6
1813 then
1814 Printf.bprintf bb " rely='%f'" rely
1816 if abs_float visy > 1e-6
1817 then
1818 Printf.bprintf bb " visy='%f'" visy
1822 if pan != 0
1823 then Printf.bprintf bb " pan='%d'" pan;
1825 add_attrs bb false dc c time;
1826 let kb = keymapsbuf false dc c in
1828 begin match bookmarks with
1829 | [] ->
1830 if Buffer.length kb > 0
1831 then (
1832 Buffer.add_string bb ">\n";
1833 Buffer.add_buffer bb kb;
1834 Buffer.add_string bb "\n</doc>\n";
1836 else Buffer.add_string bb "/>\n"
1837 | _ ->
1838 Buffer.add_string bb ">\n<bookmarks>\n";
1839 List.iter (fun (title, _, kind) ->
1840 begin match kind with
1841 | Oanchor (page, rely, visy) ->
1842 Printf.bprintf bb
1843 "<item title='%s' page='%d'"
1844 (enent title 0 (String.length title))
1845 page
1847 if rely > 1e-6
1848 then
1849 Printf.bprintf bb " rely='%f'" rely
1851 if abs_float visy > 1e-6
1852 then
1853 Printf.bprintf bb " visy='%f'" visy
1855 | Ohistory _ | Onone | Ouri _ | Oremote _
1856 | Oremotedest _ | Olaunch _ | Oaction _ ->
1857 failwith "unexpected link in bookmarks"
1858 end;
1859 Buffer.add_string bb "/>\n";
1860 ) bookmarks;
1861 Buffer.add_string bb "</bookmarks>";
1862 if Buffer.length kb > 0
1863 then (
1864 Buffer.add_string bb "\n";
1865 Buffer.add_buffer bb kb;
1867 Buffer.add_string bb "\n</doc>\n";
1868 end;
1872 let pan, conf =
1873 match state.mode with
1874 | Birdseye (c, pan, _, _, _) ->
1875 let beyecolumns =
1876 match conf.columns with
1877 | Cmulti ((c, _, _), _) -> Some c
1878 | Csingle _ -> None
1879 | Csplit _ -> None
1880 and columns =
1881 match c.columns with
1882 | Cmulti (c, _) -> Cmulti (c, E.a)
1883 | Csingle _ -> Csingle E.a
1884 | Csplit _ -> failwith "quit from bird's eye while split"
1886 pan, { c with beyecolumns = beyecolumns; columns = columns }
1887 | Textentry _
1888 | View
1889 | LinkNav _ -> x, conf
1891 let docpath = if nonemptystr state.path then abspath state.path else E.s in
1892 if docpath <> E.s
1893 then (
1894 adddoc docpath pan (getanchor ())
1896 let autoscrollstep =
1897 match state.autoscroll with
1898 | Some step -> step
1899 | None -> conf.autoscrollstep
1901 begin match state.mode with
1902 | Birdseye beye -> leavebirdseye beye true
1903 | Textentry _
1904 | View
1905 | LinkNav _ -> ()
1906 end;
1907 { conf with autoscrollstep = autoscrollstep }
1909 (if conf.savebmarks then state.bookmarks else [])
1910 (now ())
1913 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
1914 if docpath <> abspath path
1915 then adddoc path x anchor c bookmarks c.lastvisit
1916 ) h;
1917 Buffer.add_string bb "</llppconfig>\n";
1918 true;
1921 let save leavebirdseye =
1922 let relx = float state.x /. float state.winw in
1923 let w, h, x =
1924 let cx w = truncate (relx *. float w) in
1925 List.fold_left
1926 (fun (w, h, x) ws ->
1927 match ws with
1928 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
1929 | Wsi.MaxVert -> (w, conf.cwinh, x)
1930 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
1932 (state.winw, state.winh, state.x) state.winstate
1934 conf.cwinw <- w;
1935 conf.cwinh <- h;
1936 let bb = Buffer.create 32768 in
1937 let save2 (h, dc) =
1938 save1 bb leavebirdseye x h dc
1940 if load1 save2 && Buffer.length bb > 0
1941 then
1943 let tmp = !confpath ^ ".tmp" in
1944 let oc = open_out_bin tmp in
1945 Buffer.output_buffer oc bb;
1946 close_out oc;
1947 Unix.rename tmp !confpath;
1948 with exn ->
1949 prerr_endline
1950 ("error while saving configuration: " ^ exntos exn)
1953 let gc fdi fdo =
1954 let wr s n =
1955 let n' = Unix.write fdo (Bytes.of_string s) 0 n in
1956 if n != n'
1957 then Utils.error "Unix.write %d = %d" n n'
1959 let rd s n =
1960 try Unix.read fdi s 0 n
1961 with exn -> Utils.error "reading gc pipe %s" (exntos exn) in
1962 let href = ref (Hashtbl.create 0) in
1963 let cref = ref defconf in
1964 let push (h, dc) =
1965 let f path (pc, _pb, _px, _pa) =
1966 let s =
1967 Printf.sprintf "%s\000%ld\000" path (Int32.of_float pc.lastvisit)
1969 wr s (String.length s)
1971 Hashtbl.iter f h;
1972 href := h;
1973 cref := dc;
1974 Unix.shutdown fdo Unix.SHUTDOWN_SEND;
1975 true
1977 ignore (load1 push);
1978 let s =
1979 let b = Buffer.create 32768 in
1980 let s = Bytes.create 4096 in
1981 let rec f () =
1982 let n = rd s (Bytes.length s) in
1983 if n = 0
1984 then Buffer.contents b
1985 else (Buffer.add_subbytes b s 0 n; f ())
1987 f ()
1989 let rec f ppos =
1990 match String.index_from s ppos '\000' with
1991 | zpos1 ->
1992 let zpos2 =
1993 try String.index_from s (zpos1+1) '\000' with Not_found -> -1 in
1994 if zpos2 = -1
1995 then error "Incorrect gc input in (%S) at %d" s zpos1
1996 else
1997 let okey = StringLabels.sub s ~pos:ppos ~len:(zpos1-ppos) in
1998 let nkey = StringLabels.sub s ~pos:(zpos1+1) ~len:(zpos2-zpos1-1) in
1999 if emptystr nkey
2000 then (Hashtbl.remove !href okey; f (zpos2+1))
2001 else
2002 let v =
2003 try Hashtbl.find !href okey
2004 with Not_found -> Utils.error "gc: could not find %S" okey
2006 Hashtbl.remove !href okey;
2007 Hashtbl.replace !href nkey v;
2008 f (zpos2+1)
2009 | exception Not_found -> ()
2011 f 0;
2012 let bb = Buffer.create 32768 in
2013 let save2 (_h, dc) = save1 bb (fun _ _ -> ()) 0 !href dc in
2014 if load1 save2 && Buffer.length bb > 0
2015 then (
2017 let tmp = !confpath ^ ".tmp" in
2018 let oc = open_out_bin tmp in
2019 Buffer.output_buffer oc bb;
2020 close_out oc;
2021 Unix.rename tmp !confpath;
2022 with exn ->
2023 prerr_endline
2024 ("error while saving configuration: " ^ exntos exn)
2028 let logcurrently = function
2029 | Idle -> dolog "Idle"
2030 | Loading (l, gen) ->
2031 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2032 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2033 dolog
2034 "Tiling %d[%d,%d] page=%s cs=%s angle"
2035 l.pageno col row (~> pageopaque)
2036 (CSTE.to_string colorspace)
2038 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2039 angle gen conf.angle state.gen
2040 tilew tileh
2041 conf.tilew conf.tileh
2043 | Outlining _ ->
2044 dolog "outlining"