Remove unused import
[llpp.git] / config.ml
blob2804648008a142687a580b61ad49a0977a2804c1
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";
66 (n, a, b)
70 type keymap =
71 | KMinsrt of key
72 | KMinsrl of key list
73 | KMmulti of key list * key list
74 and key = int * int
75 and keyhash = (key, keymap) Hashtbl.t
76 and keystate =
77 | KSnone
78 | KSinto of (key list * key list)
79 and interpagespace = int
80 and multicolumns = multicol * pagegeom
81 and singlecolumn = pagegeom
82 and splitcolumns = columncount * pagegeom
83 and pagegeom = (pdimno * x * y * (pageno * width * height * leftx)) array
84 and multicol = columncount * covercount * covercount
85 and pdimno = int
86 and columncount = int
87 and covercount = int
88 and fitmodel = | FitWidth | FitProportional | FitPage
89 and trimmargins = bool
90 and irect = (int * int * int * int)
91 and memsize = int
92 and texcount = int
93 and sliceheight = int
94 and angle = int
95 and params = (angle * fitmodel * trimparams
96 * texcount * sliceheight * memsize
97 * colorspace * fontpath * trimcachepath
98 * haspbo * usefontconfig)
99 and width = int
100 and height = int
101 and leftx = int
102 and opaque = Opaque.t
103 and recttype = int
104 and pixmapsize = int
105 and gen = int
106 and top = float
107 and dtop = float
108 and fontpath = string
109 and trimcachepath = string
110 and aalevel = int
111 and trimparams = (trimmargins * irect)
112 and colorspace = | Rgb | Bgr | Gray
113 and haspbo = bool
114 and usefontconfig = bool
115 and uri = string
116 and caption = string
117 and x = int
118 and y = int
119 and tilex = int
120 and tiley = int
121 and tileparams = (x * y * width * height * tilex * tiley)
122 and under =
123 | Unone
124 | Ulinkuri of string
125 | Ulinkgoto of (int * int)
126 | Utext of facename
127 | Uunexpected of string
128 | Ulaunch of launchcommand
129 | Unamed of destname
130 | Uremote of (filename * pageno)
131 | Uremotedest of (filename * destname)
132 | Uannotation of (opaque * slinkindex)
133 and slinkindex = int
134 and facename = string
135 and launchcommand = string
136 and filename = string
137 and pageno = int
138 and destname = string
139 and mark =
140 | Mark_page
141 | Mark_block
142 | Mark_line
143 | Mark_word
144 and link =
145 | Lnotfound
146 | Lfound of int
147 and linkdir =
148 | LDfirst
149 | LDlast
150 | LDfirstvisible of (int * int * int)
151 | LDleft of int
152 | LDright of int
153 | LDdown of int
154 | LDup of int
155 and pagewithlinks =
156 | Pwlnotfound
157 | Pwl of int
158 and scrollb = int
159 and anchor = pageno * top * dtop
160 and rect = float * float * float * float * float * float * float * float
161 and infochange = | Memused | Docinfo | Pdim
164 class type uioh = object
165 method display : unit
166 method key : int -> int -> uioh
167 method button : int -> bool -> int -> int -> int -> uioh
168 method multiclick : int -> int -> int -> int -> uioh
169 method motion : int -> int -> uioh
170 method pmotion : int -> int -> uioh
171 method infochanged : infochange -> unit
172 method scrollpw : (int * float * float)
173 method scrollph : (int * float * float)
174 method modehash : keyhash
175 method eformsgs : bool
176 method alwaysscrolly : bool
177 end;;
179 module type TextEnumType =
181 type t
182 val name : string
183 val names : string array
184 end;;
186 module TextEnumMake (Ten : TextEnumType) =
187 struct
188 let names = Ten.names;;
189 let to_int (t : Ten.t) = Obj.magic t;;
190 let to_string t = names.(to_int t);;
191 let of_int n : Ten.t = Obj.magic n;;
192 let of_string s =
193 let rec find i =
194 if i = Array.length names
195 then failwith ("invalid " ^ Ten.name ^ ": " ^ s)
196 else (
197 if Ten.names.(i) = s
198 then of_int i
199 else find (i+1)
201 in find 0;;
202 end;;
204 module CSTE = TextEnumMake (struct
205 type t = colorspace;;
206 let name = "colorspace";;
207 let names = [|"rgb"; "bgr"; "gray"|];;
208 end);;
210 module MTE = TextEnumMake (struct
211 type t = mark;;
212 let name = "mark";;
213 let names = [|"page"; "block"; "line"; "word"|];;
214 end);;
216 module FMTE = TextEnumMake (struct
217 type t = fitmodel;;
218 let name = "fitmodel";;
219 let names = [|"width"; "proportional"; "page"|];;
220 end);;
222 type conf =
223 { mutable scrollbw : int
224 ; mutable scrollh : int
225 ; mutable scrollb : scrollb
226 ; mutable icase : bool
227 ; mutable preload : bool
228 ; mutable pagebias : int
229 ; mutable verbose : bool
230 ; mutable debug : bool
231 ; mutable scrollstep : int
232 ; mutable hscrollstep : int
233 ; mutable maxhfit : bool
234 ; mutable crophack : bool
235 ; mutable autoscrollstep : int
236 ; mutable maxwait : float option
237 ; mutable hlinks : bool
238 ; mutable underinfo : bool
239 ; mutable interpagespace : interpagespace
240 ; mutable zoom : float
241 ; mutable presentation : bool
242 ; mutable angle : angle
243 ; mutable cwinw : int
244 ; mutable cwinh : int
245 ; mutable savebmarks : bool
246 ; mutable fitmodel : fitmodel
247 ; mutable trimmargins : trimmargins
248 ; mutable trimfuzz : irect
249 ; mutable memlimit : memsize
250 ; mutable texcount : texcount
251 ; mutable sliceheight : sliceheight
252 ; mutable thumbw : width
253 ; mutable jumpback : bool
254 ; mutable bgcolor : (float * float * float)
255 ; mutable bedefault : bool
256 ; mutable tilew : int
257 ; mutable tileh : int
258 ; mutable mustoresize : memsize
259 ; mutable checkers : bool
260 ; mutable aalevel : int
261 ; mutable urilauncher : string
262 ; mutable pathlauncher : string
263 ; mutable colorspace : colorspace
264 ; mutable invert : bool
265 ; mutable colorscale : float
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 * filename))
300 | Oaction of (unit -> unit)
301 | Oreaction of (unit -> outline array)
302 and outline = (caption * outlinelevel * outlinekind)
303 and outlinelevel = int
306 type page =
307 { pageno : int
308 ; pagedimno : int
309 ; pagew : int
310 ; pageh : int
311 ; pagex : int
312 ; pagey : int
313 ; pagevw : int
314 ; pagevh : int
315 ; pagedispx : int
316 ; pagedispy : int
317 ; pagecol : int
321 type tile = opaque * pixmapsize * elapsed
322 and elapsed = float;;
323 type pagemapkey = pageno * gen;;
324 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
325 and row = int
326 and col = int
327 and currently =
328 | Idle
329 | Loading of (page * gen)
330 | Tiling of (
331 page * opaque * colorspace * angle * gen * col * row * width * height
333 | Outlining of outline list
336 type mpos = int * int
337 and mstate =
338 | Msel of (mpos * mpos)
339 | Mpan of mpos
340 | Mscrolly | Mscrollx
341 | Mzoom of (buttonno * step)
342 | Mzoomrect of (mpos * mpos)
343 | Mnone
344 and buttonno = int
345 and step = int
348 type mode =
349 | Birdseye of (conf * leftx * pageno * pageno * anchor)
350 | Textentry of (textentry * onleave)
351 | View
352 | LinkNav of linktarget
353 and onleave = leavetextentrystatus -> unit
354 and leavetextentrystatus = | Cancel | Confirm
355 and helpitem = string * int * action
356 and action =
357 | Noaction
358 | Action of (uioh -> uioh)
359 and linktarget =
360 | Ltexact of (pageno * direction)
361 | Ltgendir of direction
362 | Ltnotready of (pageno * direction)
363 and direction = int (* -1, 0, 1 *)
364 and textentry = string * string * onhist option * onkey * ondone * cancelonempty
365 and onkey = string -> int -> te
366 and ondone = string -> unit
367 and histcancel = unit -> unit
368 and onhist = ((histcmd -> string) * histcancel)
369 and histcmd = HCnext | HCprev | HCfirst | HClast
370 and cancelonempty = bool
371 and te =
372 | TEstop
373 | TEdone of string
374 | TEcont of string
375 | TEswitch of textentry
378 type 'a circbuf =
379 { store : 'a array
380 ; mutable rc : int
381 ; mutable wc : int
382 ; mutable len : int
386 type state =
387 { mutable ss : Unix.file_descr
388 ; mutable wsfd : Unix.file_descr
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 defconf =
488 { scrollbw = 7
489 ; scrollh = 12
490 ; scrollb = scrollbhv lor scrollbvv
491 ; icase = true
492 ; preload = true
493 ; pagebias = 0
494 ; verbose = false
495 ; debug = false
496 ; scrollstep = 24
497 ; hscrollstep = 24
498 ; maxhfit = true
499 ; crophack = false
500 ; autoscrollstep = 2
501 ; maxwait = None
502 ; hlinks = false
503 ; underinfo = false
504 ; interpagespace = 2
505 ; zoom = 1.0
506 ; presentation = false
507 ; angle = 0
508 ; cwinw = 900
509 ; cwinh = 900
510 ; savebmarks = true
511 ; fitmodel = FitProportional
512 ; trimmargins = false
513 ; trimfuzz = (0,0,0,0)
514 ; memlimit = 32 lsl 20
515 ; texcount = 256
516 ; sliceheight = 24
517 ; thumbw = 76
518 ; jumpback = true
519 ; bgcolor = (0.5, 0.5, 0.5)
520 ; bedefault = false
521 ; tilew = 2048
522 ; tileh = 2048
523 ; mustoresize = 256 lsl 20
524 ; checkers = true
525 ; aalevel = 8
526 ; urilauncher =
527 (match platform with
528 | Plinux | Psun | Pbsd -> "xdg-open \"%s\""
529 | Posx -> "open \"%s\""
530 | Pcygwin -> "cygstart \"%s\""
531 | Punknown -> "echo %s")
532 ; pathlauncher = "lp \"%s\""
533 ; selcmd =
534 (match platform with
535 | Plinux | Pbsd | Psun -> "xsel -i"
536 | Posx -> "pbcopy"
537 | Pcygwin -> "wsel"
538 | Punknown -> "cat")
539 ; paxcmd = "cat"
540 ; passcmd = E.s
541 ; savecmd = E.s
542 ; colorspace = Rgb
543 ; invert = false
544 ; colorscale = 1.0
545 ; ghyllscroll = None
546 ; columns = Csingle [||]
547 ; beyecolumns = None
548 ; updatecurs = false
549 ; hfsize = 12
550 ; pgscale = 1.0
551 ; usepbo = false
552 ; wheelbypage = false
553 ; stcmd = "echo SyncTex"
554 ; riani = false
555 ; pax = None
556 ; paxmark = Mark_word
557 ; leftscroll = false
558 ; title = E.s
559 ; lastvisit = 0.0
560 ; annotinline = true
561 ; keyhashes =
562 let mk n = (n, Hashtbl.create 1) in
563 [ mk "global"
564 ; mk "info"
565 ; mk "help"
566 ; mk "outline"
567 ; mk "listview"
568 ; mk "birdseye"
569 ; mk "textentry"
570 ; mk "links"
571 ; mk "view"
576 let conf = { defconf with angle = defconf.angle };;
578 let gotourl url =
579 let command = Str.global_replace percentsre url conf.urilauncher in
580 try ignore @@ spawn command []
581 with exn -> dolog "failed to execute `%s': %s" command @@ exntos exn
584 let gotouri uri =
585 if emptystr conf.urilauncher
586 then dolog "%s" uri
587 else (
588 let url = geturl uri in
589 if emptystr url
590 then dolog "obtained empty url from uri %S\n" uri
591 else gotourl url
595 let makehelp () =
596 let strings =
597 version ()
598 :: "(searching in this text works just by typing (i.e. no initial '/'))"
599 :: E.s :: Help.keys
601 Array.of_list (
602 List.map (fun s ->
603 let url = geturl s in
604 if nonemptystr url
605 then (s, 0, Action (fun uioh -> gotourl url; uioh))
606 else (s, 0, Noaction)
607 ) strings);
610 let cbnew n v =
611 { store = Array.make n v
612 ; rc = 0
613 ; wc = 0
614 ; len = 0
618 let cbcap b = Array.length b.store;;
620 let cbput b v =
621 let cap = cbcap b in
622 b.store.(b.wc) <- v;
623 b.wc <- (b.wc + 1) mod cap;
624 b.rc <- b.wc;
625 b.len <- min (b.len + 1) cap;
628 let cbempty b = b.len = 0;;
630 let cbgetg b circular dir =
631 if cbempty b
632 then b.store.(0)
633 else
634 let rc = b.rc + dir in
635 let rc =
636 if circular
637 then (
638 if rc = -1
639 then b.len-1
640 else (
641 if rc >= b.len
642 then 0
643 else rc
646 else bound rc 0 (b.len-1)
648 b.rc <- rc;
649 b.store.(rc);
652 let cbget b = cbgetg b false;;
653 let cbgetc b = cbgetg b true;;
655 let state =
656 { ss = Unix.stdin
657 ; wsfd = Unix.stdin
658 ; stderr = Unix.stderr
659 ; errmsgs = Buffer.create 0
660 ; newerrmsgs = false
661 ; x = 0
662 ; y = 0
663 ; w = 0
664 ; anchor = emptyanchor
665 ; ranchors = []
666 ; layout = []
667 ; maxy = max_int
668 ; tilelru = Queue.create ()
669 ; pagemap = Hashtbl.create 10
670 ; tilemap = Hashtbl.create 10
671 ; pdims = []
672 ; pagecount = 0
673 ; currently = Idle
674 ; mstate = Mnone
675 ; rects = []
676 ; rects1 = []
677 ; text = E.s
678 ; mode = View
679 ; winstate = []
680 ; searchpattern = E.s
681 ; outlines = E.a
682 ; bookmarks = []
683 ; path = E.s
684 ; password = E.s
685 ; nameddest = E.s
686 ; geomcmds = E.s, []
687 ; hists =
688 { nav = cbnew 10 emptyanchor
689 ; pat = cbnew 10 E.s
690 ; pag = cbnew 10 E.s
691 ; sel = cbnew 10 E.s
693 ; memused = 0
694 ; gen = 0
695 ; throttle = None
696 ; autoscroll = None
697 ; ghyll = noghyll
698 ; help = makehelp ()
699 ; docinfo = []
700 ; checkerstexid = None
701 ; prevzoom = (1.0, 0)
702 ; progress = -1.0
703 ; uioh = nouioh
704 ; redisplay = true
705 ; mpos = (-1, -1)
706 ; keystate = KSnone
707 ; glinks = false
708 ; prevcolumns = None
709 ; winw = -1
710 ; winh = -1
711 ; reprf = noreprf
712 ; origin = E.s
713 ; roam = noroam
714 ; bzoom = false
715 ; traw = Raw.create_static `float ~len:8
716 ; vraw = Raw.create_static `float ~len:8
720 let copykeyhashes c =
721 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
724 let calcips h =
725 let d = state.winh - h in
726 max conf.interpagespace ((d + 1) / 2)
729 let rowyh (c, coverA, coverB) b n =
730 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
731 then
732 let _, _, vy, (_, _, h, _) = b.(n) in
733 (vy, h)
734 else
735 let n' = n - coverA in
736 let d = n' mod c in
737 let s = n - d in
738 let e = min state.pagecount (s + c) in
739 let rec find m miny maxh = if m = e then miny, maxh else
740 let _, _, y, (_, _, h, _) = b.(m) in
741 let miny = min miny y in
742 let maxh = max maxh h in
743 find (m+1) miny maxh
744 in find s max_int 0
747 let page_of_y y =
748 let ((c, coverA, coverB) as cl), b =
749 match conf.columns with
750 | Csingle b -> (1, 0, 0), b
751 | Cmulti (c, b) -> c, b
752 | Csplit (_, b) -> (1, 0, 0), b
754 if Array.length b = 0
755 then -1
756 else
757 let rec bsearch nmin nmax =
758 if nmin > nmax
759 then bound nmin 0 (state.pagecount-1)
760 else
761 let n = (nmax + nmin) / 2 in
762 let vy, h = rowyh cl b n in
763 let y0, y1 =
764 if conf.presentation
765 then
766 let ips = calcips h in
767 let y0 = vy - ips in
768 let y1 = vy + h + ips in
769 y0, y1
770 else (
771 if n = 0
772 then 0, vy + h + conf.interpagespace
773 else
774 let y0 = vy - conf.interpagespace in
775 y0, y0 + h + conf.interpagespace
778 if y >= y0 && y < y1
779 then (
780 if c = 1
781 then n
782 else (
783 if n > coverA
784 then
785 if n < state.pagecount - coverB
786 then ((n-coverA)/c)*c + coverA
787 else n
788 else n
791 else (
792 if y > y0
793 then bsearch (n+1) nmax
794 else bsearch nmin (n-1)
797 bsearch 0 (state.pagecount-1);
800 let calcheight () =
801 match conf.columns with
802 | Cmulti ((_, _, _) as cl, b) ->
803 if Array.length b > 0
804 then
805 let y, h = rowyh cl b (Array.length b - 1) in
806 y + h + (if conf.presentation then calcips h else 0)
807 else 0
808 | Csingle b ->
809 if Array.length b > 0
810 then
811 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
812 y + h + (if conf.presentation then calcips h else 0)
813 else 0
814 | Csplit (_, b) ->
815 if Array.length b > 0
816 then
817 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
818 y + h
819 else 0
822 let getpageywh pageno =
823 let pageno = bound pageno 0 (state.pagecount-1) in
824 match conf.columns with
825 | Csingle b ->
826 if Array.length b = 0
827 then 0, 0, 0
828 else
829 let (_, _, y, (_, w, h, _)) = b.(pageno) in
830 let y =
831 if conf.presentation
832 then y - calcips h
833 else y
835 y, w, h
836 | Cmulti (cl, b) ->
837 if Array.length b = 0
838 then 0, 0, 0
839 else
840 let y, h = rowyh cl b pageno in
841 let (_, _, _, (_, w, _, _)) = b.(pageno) in
842 let y =
843 if conf.presentation
844 then y - calcips h
845 else y
847 y, w, h
848 | Csplit (c, b) ->
849 if Array.length b = 0
850 then 0, 0, 0
851 else
852 let n = pageno*c in
853 let (_, _, y, (_, w, h, _)) = b.(n) in
854 y, w / c, h
857 let getpageyh pageno =
858 let y,_,h = getpageywh pageno in
859 y, h;
862 let getpagedim pageno =
863 let rec f ppdim l =
864 match l with
865 | (n, _, _, _) as pdim :: rest ->
866 if n >= pageno
867 then (if n = pageno then pdim else ppdim)
868 else f pdim rest
870 | [] -> ppdim
872 f (-1, -1, -1, -1) state.pdims
875 let getpagey pageno = fst (getpageyh pageno);;
877 let getanchor1 l =
878 let top =
879 let coloff = l.pagecol * l.pageh in
880 float (l.pagey + coloff) /. float l.pageh
882 let dtop =
883 if l.pagedispy = 0
884 then
886 else (
887 if conf.presentation
888 then float l.pagedispy /. float (calcips l.pageh)
889 else float l.pagedispy /. float conf.interpagespace
892 (l.pageno, top, dtop)
895 let getanchor () =
896 match state.layout with
897 | l :: _ -> getanchor1 l
898 | [] ->
899 let n = page_of_y state.y in
900 if n = -1
901 then state.anchor
902 else
903 let y, h = getpageyh n in
904 let dy = y - state.y in
905 let dtop =
906 if conf.presentation
907 then
908 let ips = calcips h in
909 float (dy + ips) /. float ips
910 else
911 float dy /. float conf.interpagespace
913 (n, 0.0, dtop)
916 let fontpath = ref E.s;;
918 type historder = [ `lastvisit | `title | `path | `file ];;
920 module KeyMap =
921 Map.Make (struct type t = (int * int) let compare = compare end);;
923 let unentS s =
924 let l = String.length s in
925 let b = Buffer.create l in
926 Parser.unent b s 0 l;
927 Buffer.contents b;
930 let home =
931 try Sys.getenv "HOME"
932 with exn ->
933 dolog "cannot determine home directory location: %s" @@ exntos exn;
937 let modifier_of_string = function
938 | "alt" -> Wsi.altmask
939 | "shift" -> Wsi.shiftmask
940 | "ctrl" | "control" -> Wsi.ctrlmask
941 | "meta" -> Wsi.metamask
942 | _ -> 0
945 let keys_of_string s =
946 let key_of_string r s =
947 let elems = Str.full_split r s in
948 let f n k m =
949 let g s =
950 let m1 = modifier_of_string s in
951 if m1 = 0
952 then (Wsi.namekey s, m)
953 else (k, m lor m1)
954 in function
955 | Str.Delim s when n land 1 = 0 -> g s
956 | Str.Text s -> g s
957 | Str.Delim _ -> (k, m)
959 let rec loop n k m = function
960 | [] -> (k, m)
961 | x :: xs ->
962 let k, m = f n k m x in
963 loop (n+1) k m xs
965 loop 0 0 0 elems
967 let elems = Str.split whitere s in
968 List.map (key_of_string (Str.regexp "-")) elems
971 let config_of c attrs =
972 let apply c k v =
974 match k with
975 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
976 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
977 | "case-insensitive-search" -> { c with icase = bool_of_string v }
978 | "preload" -> { c with preload = bool_of_string v }
979 | "page-bias" -> { c with pagebias = int_of_string v }
980 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
981 | "horizontal-scroll-step" ->
982 { c with hscrollstep = max (int_of_string v) 1 }
983 | "auto-scroll-step" ->
984 { c with autoscrollstep = max 0 (int_of_string v) }
985 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
986 | "crop-hack" -> { c with crophack = bool_of_string v }
987 | "throttle" ->
988 let mw =
989 match String.lowercase v with
990 | "true" -> Some infinity
991 | "false" -> None
992 | f -> Some (float_of_string f)
994 { c with maxwait = mw }
995 | "highlight-links" -> { c with hlinks = bool_of_string v }
996 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
997 | "vertical-margin" ->
998 { c with interpagespace = max 0 (int_of_string v) }
999 | "zoom" ->
1000 let zoom = float_of_string v /. 100. in
1001 let zoom = max zoom 0.0 in
1002 { c with zoom = zoom }
1003 | "presentation" -> { c with presentation = bool_of_string v }
1004 | "rotation-angle" -> { c with angle = int_of_string v }
1005 | "width" -> { c with cwinw = max 20 (int_of_string v) }
1006 | "height" -> { c with cwinh = max 20 (int_of_string v) }
1007 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
1008 | "proportional-display" ->
1009 let fm =
1010 if bool_of_string v
1011 then FitProportional
1012 else FitWidth
1014 { c with fitmodel = fm }
1015 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
1016 | "pixmap-cache-size" ->
1017 { c with memlimit = max 2 (int_of_string_with_suffix v) }
1018 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
1019 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
1020 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
1021 | "persistent-location" -> { c with jumpback = bool_of_string v }
1022 | "background-color" -> { c with bgcolor = color_of_string v }
1023 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
1024 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
1025 | "mupdf-store-size" ->
1026 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
1027 | "checkers" -> { c with checkers = bool_of_string v }
1028 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
1029 | "trim-margins" -> { c with trimmargins = bool_of_string v }
1030 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
1031 | "uri-launcher" -> { c with urilauncher = unentS v }
1032 | "path-launcher" -> { c with pathlauncher = unentS v }
1033 | "color-space" -> { c with colorspace = CSTE.of_string v }
1034 | "invert-colors" -> { c with invert = bool_of_string v }
1035 | "brightness" -> { c with colorscale = float_of_string v }
1036 | "ghyllscroll" -> { c with ghyllscroll = ghyllscroll_of_string v }
1037 | "columns" ->
1038 let (n, _, _) as nab = multicolumns_of_string v in
1039 if n < 0
1040 then { c with columns = Csplit (-n, E.a) }
1041 else { c with columns = Cmulti (nab, E.a) }
1042 | "birds-eye-columns" ->
1043 { c with beyecolumns = Some (max (int_of_string v) 2) }
1044 | "selection-command" -> { c with selcmd = unentS v }
1045 | "synctex-command" -> { c with stcmd = unentS v }
1046 | "pax-command" -> { c with paxcmd = unentS v }
1047 | "askpass-command" -> { c with passcmd = unentS v }
1048 | "savepath-command" -> { c with savecmd = unentS v }
1049 | "update-cursor" -> { c with updatecurs = bool_of_string v }
1050 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
1051 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
1052 | "use-pbo" -> { c with usepbo = bool_of_string v }
1053 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
1054 | "horizontal-scrollbar-visible" ->
1055 let b =
1056 if bool_of_string v
1057 then c.scrollb lor scrollbhv
1058 else c.scrollb land (lnot scrollbhv)
1060 { c with scrollb = b }
1061 | "vertical-scrollbar-visible" ->
1062 let b =
1063 if bool_of_string v
1064 then c.scrollb lor scrollbvv
1065 else c.scrollb land (lnot scrollbvv)
1067 { c with scrollb = b }
1068 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
1069 | "point-and-x" ->
1070 { c with pax =
1071 if bool_of_string v
1072 then Some (ref (0.0, 0, 0))
1073 else None }
1074 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
1075 | "scroll-bar-on-the-left" -> { c with leftscroll = bool_of_string v }
1076 | "title" -> { c with title = unentS v }
1077 | "last-visit" -> { c with lastvisit = float_of_string v }
1078 | "edit-annotations-inline" -> { c with annotinline = bool_of_string v }
1079 | _ -> c
1080 with exn ->
1081 dolog "error processing attribute (`%S' = `%S'): %s" k v @@ exntos exn;
1084 let rec fold c = function
1085 | [] -> c
1086 | (k, v) :: rest ->
1087 let c = apply c k v in
1088 fold c rest
1090 fold { c with keyhashes = copykeyhashes c } attrs;
1093 let fromstring f pos n v d =
1094 try f v
1095 with exn ->
1096 dolog "error processing attribute (%S=%S) at %d\n%s" n v pos @@ exntos exn;
1100 let bookmark_of attrs =
1101 let rec fold title page rely visy = function
1102 | ("title", v) :: rest -> fold v page rely visy rest
1103 | ("page", v) :: rest -> fold title v rely visy rest
1104 | ("rely", v) :: rest -> fold title page v visy rest
1105 | ("visy", v) :: rest -> fold title page rely v rest
1106 | _ :: rest -> fold title page rely visy rest
1107 | [] -> title, page, rely, visy
1109 fold "invalid" "0" "0" "0" attrs
1112 let doc_of attrs =
1113 let rec fold path page rely pan visy origin = function
1114 | ("path", v) :: rest -> fold v page rely pan visy origin rest
1115 | ("page", v) :: rest -> fold path v rely pan visy origin rest
1116 | ("rely", v) :: rest -> fold path page v pan visy origin rest
1117 | ("pan", v) :: rest -> fold path page rely v visy origin rest
1118 | ("visy", v) :: rest -> fold path page rely pan v origin rest
1119 | ("origin", v) :: rest -> fold path page rely pan visy v rest
1120 | _ :: rest -> fold path page rely pan visy origin rest
1121 | [] -> path, page, rely, pan, visy, origin
1123 fold E.s "0" "0" "0" "0" E.s attrs
1126 let map_of attrs =
1127 let rec fold rs ls = function
1128 | ("out", v) :: rest -> fold v ls rest
1129 | ("in", v) :: rest -> fold rs v rest
1130 | _ :: rest -> fold ls rs rest
1131 | [] -> ls, rs
1133 fold E.s E.s attrs
1136 let setconf dst src =
1137 dst.scrollbw <- src.scrollbw;
1138 dst.scrollh <- src.scrollh;
1139 dst.icase <- src.icase;
1140 dst.preload <- src.preload;
1141 dst.pagebias <- src.pagebias;
1142 dst.verbose <- src.verbose;
1143 dst.scrollstep <- src.scrollstep;
1144 dst.maxhfit <- src.maxhfit;
1145 dst.crophack <- src.crophack;
1146 dst.autoscrollstep <- src.autoscrollstep;
1147 dst.maxwait <- src.maxwait;
1148 dst.hlinks <- src.hlinks;
1149 dst.underinfo <- src.underinfo;
1150 dst.interpagespace <- src.interpagespace;
1151 dst.zoom <- src.zoom;
1152 dst.presentation <- src.presentation;
1153 dst.angle <- src.angle;
1154 dst.cwinw <- src.cwinw;
1155 dst.cwinh <- src.cwinh;
1156 dst.savebmarks <- src.savebmarks;
1157 dst.memlimit <- src.memlimit;
1158 dst.fitmodel <- src.fitmodel;
1159 dst.texcount <- src.texcount;
1160 dst.sliceheight <- src.sliceheight;
1161 dst.thumbw <- src.thumbw;
1162 dst.jumpback <- src.jumpback;
1163 dst.bgcolor <- src.bgcolor;
1164 dst.tilew <- src.tilew;
1165 dst.tileh <- src.tileh;
1166 dst.mustoresize <- src.mustoresize;
1167 dst.checkers <- src.checkers;
1168 dst.aalevel <- src.aalevel;
1169 dst.trimmargins <- src.trimmargins;
1170 dst.trimfuzz <- src.trimfuzz;
1171 dst.urilauncher <- src.urilauncher;
1172 dst.colorspace <- src.colorspace;
1173 dst.invert <- src.invert;
1174 dst.colorscale <- src.colorscale;
1175 dst.ghyllscroll <- src.ghyllscroll;
1176 dst.columns <- src.columns;
1177 dst.beyecolumns <- src.beyecolumns;
1178 dst.selcmd <- src.selcmd;
1179 dst.updatecurs <- src.updatecurs;
1180 dst.pathlauncher <- src.pathlauncher;
1181 dst.keyhashes <- copykeyhashes src;
1182 dst.hfsize <- src.hfsize;
1183 dst.hscrollstep <- src.hscrollstep;
1184 dst.pgscale <- src.pgscale;
1185 dst.usepbo <- src.usepbo;
1186 dst.wheelbypage <- src.wheelbypage;
1187 dst.stcmd <- src.stcmd;
1188 dst.paxcmd <- src.paxcmd;
1189 dst.passcmd <- src.passcmd;
1190 dst.savecmd <- src.savecmd;
1191 dst.scrollb <- src.scrollb;
1192 dst.riani <- src.riani;
1193 dst.paxmark <- src.paxmark;
1194 dst.leftscroll <- src.leftscroll;
1195 dst.title <- src.title;
1196 dst.annotinline <- src.annotinline;
1197 dst.pax <-
1198 if src.pax = None
1199 then None
1200 else Some ((ref (0.0, 0, 0)));
1203 let findkeyhash c name =
1204 try List.assoc name c.keyhashes
1205 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
1208 let get s =
1209 let open Parser in
1210 let h = Hashtbl.create 10 in
1211 let dc = { defconf with angle = defconf.angle } in
1212 let rec toplevel v t spos _ =
1213 match t with
1214 | Vdata | Vcdata | Vend -> v
1215 | Vopen ("llppconfig", _, closed) ->
1216 if closed
1217 then v
1218 else { v with f = llppconfig }
1219 | Vopen _ ->
1220 error "unexpected subelement at top level" s spos
1221 | Vclose _ -> error "unexpected close at top level" s spos
1223 and llppconfig v t spos _ =
1224 match t with
1225 | Vdata | Vcdata -> v
1226 | Vend -> error "unexpected end of input in llppconfig" s spos
1227 | Vopen ("defaults", attrs, closed) ->
1228 let c = config_of dc attrs in
1229 setconf dc c;
1230 if closed
1231 then v
1232 else { v with f = defaults }
1234 | Vopen ("ui-font", attrs, closed) ->
1235 let rec getsize size = function
1236 | [] -> size
1237 | ("size", v) :: rest ->
1238 let size =
1239 fromstring int_of_string spos "size" v fstate.fontsize in
1240 getsize size rest
1241 | l -> getsize size l
1243 fstate.fontsize <- getsize fstate.fontsize attrs;
1244 if closed
1245 then v
1246 else { v with f = uifont (Buffer.create 10) }
1248 | Vopen ("doc", attrs, closed) ->
1249 let pathent, spage, srely, span, svisy, origin = doc_of attrs in
1250 let path = unentS pathent
1251 and origin = unentS origin
1252 and pageno = fromstring int_of_string spos "page" spage 0
1253 and rely = fromstring float_of_string spos "rely" srely 0.0
1254 and pan = fromstring int_of_string spos "pan" span 0
1255 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1256 let c = config_of dc attrs in
1257 let anchor = (pageno, rely, visy) in
1258 if closed
1259 then (Hashtbl.add h path (c, [], pan, anchor, origin); v)
1260 else { v with f = doc path origin pan anchor c [] }
1262 | Vopen _ ->
1263 error "unexpected subelement in llppconfig" s spos
1265 | Vclose "llppconfig" -> { v with f = toplevel }
1266 | Vclose _ -> error "unexpected close in llppconfig" s spos
1268 and defaults v t spos _ =
1269 match t with
1270 | Vdata | Vcdata -> v
1271 | Vend -> error "unexpected end of input in defaults" s spos
1272 | Vopen ("keymap", attrs, closed) ->
1273 let modename =
1274 try List.assoc "mode" attrs
1275 with Not_found -> "global" in
1276 if closed
1277 then v
1278 else
1279 let ret keymap =
1280 let h = findkeyhash dc modename in
1281 KeyMap.iter (Hashtbl.replace h) keymap;
1282 defaults
1284 { v with f = pkeymap ret KeyMap.empty }
1286 | Vopen (_, _, _) ->
1287 error "unexpected subelement in defaults" s spos
1289 | Vclose "defaults" ->
1290 { v with f = llppconfig }
1292 | Vclose _ -> error "unexpected close in defaults" s spos
1294 and uifont b v t spos epos =
1295 match t with
1296 | Vdata | Vcdata ->
1297 Buffer.add_substring b s spos (epos - spos);
1299 | Vopen (_, _, _) ->
1300 error "unexpected subelement in ui-font" s spos
1301 | Vclose "ui-font" ->
1302 if emptystr !fontpath
1303 then fontpath := Buffer.contents b;
1304 { v with f = llppconfig }
1305 | Vclose _ -> error "unexpected close in ui-font" s spos
1306 | Vend -> error "unexpected end of input in ui-font" s spos
1308 and doc path origin pan anchor c bookmarks v t spos _ =
1309 match t with
1310 | Vdata | Vcdata -> v
1311 | Vend -> error "unexpected end of input in doc" s spos
1312 | Vopen ("bookmarks", _, closed) ->
1313 if closed
1314 then v
1315 else { v with f = pbookmarks path origin pan anchor c bookmarks }
1317 | Vopen ("keymap", attrs, closed) ->
1318 let modename =
1319 try List.assoc "mode" attrs
1320 with Not_found -> "global"
1322 if closed
1323 then v
1324 else
1325 let ret keymap =
1326 let h = findkeyhash c modename in
1327 KeyMap.iter (Hashtbl.replace h) keymap;
1328 doc path origin pan anchor c bookmarks
1330 { v with f = pkeymap ret KeyMap.empty }
1332 | Vopen (_, _, _) ->
1333 error "unexpected subelement in doc" s spos
1335 | Vclose "doc" ->
1336 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor, origin);
1337 { v with f = llppconfig }
1339 | Vclose _ -> error "unexpected close in doc" s spos
1341 and pkeymap ret keymap v t spos _ =
1342 match t with
1343 | Vdata | Vcdata -> v
1344 | Vend -> error "unexpected end of input in keymap" s spos
1345 | Vopen ("map", attrs, closed) ->
1346 let r, l = map_of attrs in
1347 let kss = fromstring keys_of_string spos "in" r [] in
1348 let lss = fromstring keys_of_string spos "out" l [] in
1349 let keymap =
1350 match kss with
1351 | [] -> keymap
1352 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
1353 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
1355 if closed
1356 then { v with f = pkeymap ret keymap }
1357 else
1358 let f () = v in
1359 { v with f = skip "map" f }
1361 | Vopen _ ->
1362 error "unexpected subelement in keymap" s spos
1364 | Vclose "keymap" ->
1365 { v with f = ret keymap }
1367 | Vclose _ -> error "unexpected close in keymap" s spos
1369 and pbookmarks path origin pan anchor c bookmarks v t spos _ =
1370 match t with
1371 | Vdata | Vcdata -> v
1372 | Vend -> error "unexpected end of input in bookmarks" s spos
1373 | Vopen ("item", attrs, closed) ->
1374 let titleent, spage, srely, svisy = bookmark_of attrs in
1375 let page = fromstring int_of_string spos "page" spage 0
1376 and rely = fromstring float_of_string spos "rely" srely 0.0
1377 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1378 let bookmarks =
1379 (unentS titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
1381 if closed
1382 then { v with f = pbookmarks path origin pan anchor c bookmarks }
1383 else
1384 let f () = v in
1385 { v with f = skip "item" f }
1387 | Vopen _ ->
1388 error "unexpected subelement in bookmarks" s spos
1390 | Vclose "bookmarks" ->
1391 { v with f = doc path origin pan anchor c bookmarks }
1393 | Vclose _ -> Parser.parse_error "unexpected close in bookmarks" s spos
1395 and skip tag f v t spos _ =
1396 match t with
1397 | Vdata | Vcdata -> v
1398 | Vend ->
1399 Parser.parse_error ("unexpected end of input in skipped " ^ tag) s spos
1400 | Vopen (tag', _, closed) ->
1401 if closed
1402 then v
1403 else
1404 let f' () = { v with f = skip tag f } in
1405 { v with f = skip tag' f' }
1406 | Vclose ctag ->
1407 if tag = ctag
1408 then f ()
1409 else Parser.parse_error ("unexpected close in skipped " ^ tag) s spos
1412 parse { f = toplevel; accu = () } s;
1413 h, dc;
1416 let do_load f contents =
1417 try f contents
1418 with
1419 | Parser.Parse_error (msg, s, pos) ->
1420 let subs = Parser.subs s pos in
1421 Utils.error "parse error: %s: at %d [..%S..]" msg pos subs
1423 | exn -> Utils.error "parse error: %s" @@ exntos exn
1426 let defconfpath =
1427 let dir =
1428 let xdgconfdir = Utils.getenvwithdef "XDG_CONFIG_HOME" E.s in
1429 if emptystr xdgconfdir
1430 then
1432 let dir = Filename.concat home ".config" in
1433 if Sys.is_directory dir then dir else home
1434 with _ -> home
1435 else xdgconfdir
1437 Filename.concat dir "llpp.conf"
1440 let confpath = ref defconfpath;;
1442 let load2 f default =
1443 match filecontents !confpath with
1444 | contents -> f @@ do_load get contents
1445 | exception Unix.Unix_error (Unix.ENOENT, "open", _) ->
1446 f (Hashtbl.create 0, defconf)
1447 | exception exn ->
1448 dolog "error loading configuration from `%S': %s" !confpath @@ exntos exn;
1449 default
1452 let load1 f = load2 f false;;
1454 let load openlast =
1455 let f (h, dc) =
1456 if openlast
1457 then (
1458 let path, _ =
1459 Hashtbl.fold
1460 (fun path (conf, _, _, _, _) ((_, besttime) as best) ->
1461 if conf.lastvisit > besttime
1462 then (path, conf.lastvisit)
1463 else best)
1465 (state.path, -.infinity)
1467 state.path <- path;
1469 let pc, pb, px, pa, po =
1471 let absname = abspath state.path in
1472 Hashtbl.find h absname
1473 with Not_found -> dc, [], 0, emptyanchor, state.origin
1475 setconf defconf dc;
1476 setconf conf pc;
1477 state.bookmarks <- pb;
1478 state.x <- px;
1479 state.origin <- po;
1480 if conf.jumpback
1481 then state.anchor <- pa;
1482 cbput state.hists.nav pa;
1483 true
1485 load1 f
1488 let gethist () =
1489 let f (h, _) =
1490 Hashtbl.fold (fun path (pc, pb, px, pa, po) accu ->
1491 (path, pc, pb, px, pa, po) :: accu)
1492 h [];
1494 load2 f []
1497 let add_attrs bb always dc c time =
1498 let o' fmt s =
1499 Buffer.add_string bb "\n ";
1500 Printf.bprintf bb fmt s
1502 let o c fmt s = if c then o' fmt s else ignore in
1503 let ob s a b = o (always || a != b) "%s='%b'" s a
1504 and op s a b = o (always || a <> b) "%s='%b'" s (a != None)
1505 and oi s a b = o (always || a != b) "%s='%d'" s a
1506 and oI s a b = o (always || a != b) "%s='%s'" s (string_with_suffix_of_int a)
1507 and oz s a b = o (always || a <> b) "%s='%g'" s (a*.100.)
1508 and oF s a b = o (always || a <> b) "%s='%f'" s a
1509 and oL s a b = o (always || a <> b) "%s='%Ld'" s a
1510 and oc s a b = o (always || a <> b) "%s='%s'" s (color_to_string a)
1511 and oC s a b = o (always || a <> b) "%s='%s'" s (CSTE.to_string a)
1512 and oR s a b = o (always || a <> b) "%s='%s'" s (irect_to_string a)
1513 and oFm s a b = o (always || a <> b) "%s='%s'" s (FMTE.to_string a)
1514 and oSv s a b m = o (always || a <> b) "%s='%b'" s (a land m != 0)
1515 and oPm s a b = o (always || a <> b) "%s='%s'" s (MTE.to_string a)
1516 and os s a b =
1517 o (always || a <> b) "%s='%s'" s @@ Parser.enent a 0 (String.length a)
1518 and og s a b =
1519 if always || a <> b
1520 then
1521 match a with
1522 | Some (_N, _A, _B) -> o' "%s='%u,%u,%u'" s _N _A _B
1523 | None ->
1524 match b with
1525 | None -> ()
1526 | _ -> o' "%s='none'" s
1527 and oW s a b =
1528 if always || a <> b
1529 then
1530 let v =
1531 match a with
1532 | None -> "false"
1533 | Some f ->
1534 if f = infinity
1535 then "true"
1536 else string_of_float f
1538 o' "%s='%s'" s v
1539 and oco s a b =
1540 if always || a <> b
1541 then
1542 match a with
1543 | Cmulti ((n, a, b), _) when n > 1 -> o' "%s='%d,%d,%d'" s n a b
1544 | Csplit (n, _) when n > 1 -> o' "%s='%d'" s ~-n
1545 | Cmulti _ | Csplit _ | Csingle _ -> ()
1546 and obeco s a b =
1547 if always || a <> b
1548 then
1549 match a with
1550 | Some c when c > 1 -> o' "%s='%d'" s c
1551 | _ -> ()
1553 oi "width" c.cwinw dc.cwinw;
1554 oi "height" c.cwinh dc.cwinh;
1555 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
1556 oi "scroll-handle-height" c.scrollh dc.scrollh;
1557 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
1558 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
1559 ob "case-insensitive-search" c.icase dc.icase;
1560 ob "preload" c.preload dc.preload;
1561 oi "page-bias" c.pagebias dc.pagebias;
1562 oi "scroll-step" c.scrollstep dc.scrollstep;
1563 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
1564 ob "max-height-fit" c.maxhfit dc.maxhfit;
1565 ob "crop-hack" c.crophack dc.crophack;
1566 oW "throttle" c.maxwait dc.maxwait;
1567 ob "highlight-links" c.hlinks dc.hlinks;
1568 ob "under-cursor-info" c.underinfo dc.underinfo;
1569 oi "vertical-margin" c.interpagespace dc.interpagespace;
1570 oz "zoom" c.zoom dc.zoom;
1571 ob "presentation" c.presentation dc.presentation;
1572 oi "rotation-angle" c.angle dc.angle;
1573 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
1574 oFm "fit-model" c.fitmodel dc.fitmodel;
1575 oI "pixmap-cache-size" c.memlimit dc.memlimit;
1576 oi "tex-count" c.texcount dc.texcount;
1577 oi "slice-height" c.sliceheight dc.sliceheight;
1578 oi "thumbnail-width" c.thumbw dc.thumbw;
1579 ob "persistent-location" c.jumpback dc.jumpback;
1580 oc "background-color" c.bgcolor dc.bgcolor;
1581 oi "tile-width" c.tilew dc.tilew;
1582 oi "tile-height" c.tileh dc.tileh;
1583 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
1584 ob "checkers" c.checkers dc.checkers;
1585 oi "aalevel" c.aalevel dc.aalevel;
1586 ob "trim-margins" c.trimmargins dc.trimmargins;
1587 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
1588 os "uri-launcher" c.urilauncher dc.urilauncher;
1589 os "path-launcher" c.pathlauncher dc.pathlauncher;
1590 oC "color-space" c.colorspace dc.colorspace;
1591 ob "invert-colors" c.invert dc.invert;
1592 oF "brightness" c.colorscale dc.colorscale;
1593 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
1594 oco "columns" c.columns dc.columns;
1595 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
1596 os "selection-command" c.selcmd dc.selcmd;
1597 os "synctex-command" c.stcmd dc.stcmd;
1598 os "pax-command" c.paxcmd dc.paxcmd;
1599 os "askpass-command" c.passcmd dc.passcmd;
1600 os "savepath-command" c.savecmd dc.savecmd;
1601 ob "update-cursor" c.updatecurs dc.updatecurs;
1602 oi "hint-font-size" c.hfsize dc.hfsize;
1603 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
1604 oF "page-scroll-scale" c.pgscale dc.pgscale;
1605 ob "use-pbo" c.usepbo dc.usepbo;
1606 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
1607 ob "remote-in-a-new-instance" c.riani dc.riani;
1608 op "point-and-x" c.pax dc.pax;
1609 oPm "point-and-x-mark" c.paxmark dc.paxmark;
1610 ob "scroll-bar-on-the-left" c.leftscroll dc.leftscroll;
1611 if not always
1612 then os "title" c.title dc.title;
1613 oL "last-visit" (Int64.of_float time) 0L;
1614 ob "edit-annotations-inline" c.annotinline dc.annotinline;
1617 let keymapsbuf always dc c =
1618 let bb = Buffer.create 16 in
1619 let rec loop = function
1620 | [] -> ()
1621 | (modename, h) :: rest ->
1622 let dh = findkeyhash dc modename in
1623 if always || h <> dh
1624 then (
1625 if Hashtbl.length h > 0
1626 then (
1627 if Buffer.length bb > 0
1628 then Buffer.add_char bb '\n';
1629 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
1630 Hashtbl.iter (fun i o ->
1631 let isdifferent = always ||
1633 let dO = Hashtbl.find dh i in
1634 dO <> o
1635 with Not_found -> true
1637 if isdifferent
1638 then
1639 let addkm (k, m) =
1640 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
1641 if Wsi.withalt m then Buffer.add_string bb "alt-";
1642 if Wsi.withshift m then Buffer.add_string bb "shift-";
1643 if Wsi.withmeta m then Buffer.add_string bb "meta-";
1644 Buffer.add_string bb (Wsi.keyname k);
1646 let addkms l =
1647 let rec loop = function
1648 | [] -> ()
1649 | km :: [] -> addkm km
1650 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
1652 loop l
1654 Buffer.add_string bb "<map in='";
1655 addkm i;
1656 match o with
1657 | KMinsrt km ->
1658 Buffer.add_string bb "' out='";
1659 addkm km;
1660 Buffer.add_string bb "'/>\n"
1662 | KMinsrl kms ->
1663 Buffer.add_string bb "' out='";
1664 addkms kms;
1665 Buffer.add_string bb "'/>\n"
1667 | KMmulti (ins, kms) ->
1668 Buffer.add_char bb ' ';
1669 addkms ins;
1670 Buffer.add_string bb "' out='";
1671 addkms kms;
1672 Buffer.add_string bb "'/>\n"
1673 ) h;
1674 Buffer.add_string bb "</keymap>";
1677 loop rest
1679 loop c.keyhashes;
1683 let save1 bb leavebirdseye x h dc =
1684 let uifontsize = fstate.fontsize in
1685 let dc = if conf.bedefault then conf else dc in
1686 Buffer.add_string bb "<llppconfig>\n";
1688 if nonemptystr !fontpath
1689 then
1690 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
1691 uifontsize
1692 !fontpath
1693 else (
1694 if uifontsize <> 14
1695 then
1696 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
1699 Buffer.add_string bb "<defaults";
1700 add_attrs bb true dc dc nan;
1701 let kb = keymapsbuf true dc dc in
1702 if Buffer.length kb > 0
1703 then (
1704 Buffer.add_string bb ">\n";
1705 Buffer.add_buffer bb kb;
1706 Buffer.add_string bb "\n</defaults>\n";
1708 else Buffer.add_string bb "/>\n";
1710 let adddoc path pan anchor c bookmarks time origin =
1711 if bookmarks == [] && c = dc && anchor = emptyanchor
1712 then ()
1713 else (
1714 Printf.bprintf bb "<doc path='%s'"
1715 (Parser.enent path 0 (String.length path));
1717 if nonemptystr origin
1718 then
1719 Printf.bprintf bb "\n origin='%s'"
1720 (Parser.enent origin 0 (String.length origin));
1722 if anchor <> emptyanchor
1723 then (
1724 let n, rely, visy = anchor in
1725 Printf.bprintf bb "\n page='%d'" n;
1726 if rely > 1e-6
1727 then
1728 Printf.bprintf bb " rely='%f'" rely
1730 if abs_float visy > 1e-6
1731 then
1732 Printf.bprintf bb " visy='%f'" visy
1736 if pan != 0
1737 then Printf.bprintf bb " pan='%d'" pan;
1739 add_attrs bb false dc c time;
1740 let kb = keymapsbuf false dc c in
1742 begin match bookmarks with
1743 | [] ->
1744 if Buffer.length kb > 0
1745 then (
1746 Buffer.add_string bb ">\n";
1747 Buffer.add_buffer bb kb;
1748 Buffer.add_string bb "\n</doc>\n";
1750 else Buffer.add_string bb "/>\n"
1751 | _ ->
1752 Buffer.add_string bb ">\n<bookmarks>\n";
1753 List.iter (fun (title, _, kind) ->
1754 begin match kind with
1755 | Oanchor (page, rely, visy) ->
1756 Printf.bprintf bb
1757 "<item title='%s' page='%d'"
1758 (Parser.enent title 0 (String.length title))
1759 page
1761 if rely > 1e-6
1762 then
1763 Printf.bprintf bb " rely='%f'" rely
1765 if abs_float visy > 1e-6
1766 then
1767 Printf.bprintf bb " visy='%f'" visy
1769 | Ohistory _ | Onone | Ouri _ | Oremote _
1770 | Oremotedest _ | Olaunch _ | Oaction _ | Oreaction _ ->
1771 failwith "unexpected link in bookmarks"
1772 end;
1773 Buffer.add_string bb "/>\n";
1774 ) bookmarks;
1775 Buffer.add_string bb "</bookmarks>";
1776 if Buffer.length kb > 0
1777 then (
1778 Buffer.add_string bb "\n";
1779 Buffer.add_buffer bb kb;
1781 Buffer.add_string bb "\n</doc>\n";
1782 end;
1786 let pan, conf =
1787 match state.mode with
1788 | Birdseye (c, pan, _, _, _) ->
1789 let beyecolumns =
1790 match conf.columns with
1791 | Cmulti ((c, _, _), _) -> Some c
1792 | Csingle _ -> None
1793 | Csplit _ -> None
1794 and columns =
1795 match c.columns with
1796 | Cmulti (c, _) -> Cmulti (c, E.a)
1797 | Csingle _ -> Csingle E.a
1798 | Csplit _ -> failwith "quit from bird's eye while split"
1800 pan, { c with beyecolumns = beyecolumns; columns = columns }
1801 | Textentry _
1802 | View
1803 | LinkNav _ -> x, conf
1805 let docpath = if nonemptystr state.path then abspath state.path else E.s in
1806 if nonemptystr docpath
1807 then (
1808 adddoc docpath pan (getanchor ())
1810 let autoscrollstep =
1811 match state.autoscroll with
1812 | Some step -> step
1813 | None -> conf.autoscrollstep
1815 begin match state.mode with
1816 | Birdseye beye -> leavebirdseye beye true
1817 | Textentry _
1818 | View
1819 | LinkNav _ -> ()
1820 end;
1821 { conf with autoscrollstep = autoscrollstep }
1823 (if conf.savebmarks then state.bookmarks else [])
1824 (now ())
1825 state.origin
1827 Hashtbl.iter (fun path (c, bookmarks, x, anchor, origin) ->
1828 if docpath <> abspath path
1829 then adddoc path x anchor c bookmarks c.lastvisit origin
1830 ) h;
1831 Buffer.add_string bb "</llppconfig>\n";
1832 true;
1835 let save leavebirdseye =
1836 let relx = float state.x /. float state.winw in
1837 let w, h, x =
1838 let cx w = truncate (relx *. float w) in
1839 List.fold_left
1840 (fun (w, h, x) ws ->
1841 match ws with
1842 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
1843 | Wsi.MaxVert -> (w, conf.cwinh, x)
1844 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
1846 (state.winw, state.winh, state.x) state.winstate
1848 conf.cwinw <- w;
1849 conf.cwinh <- h;
1850 let bb = Buffer.create 32768 in
1851 let save2 (h, dc) =
1852 save1 bb leavebirdseye x h dc
1854 if load1 save2 && Buffer.length bb > 0
1855 then
1857 let tmp = !confpath ^ ".tmp" in
1858 let oc = open_out_bin tmp in
1859 Buffer.output_buffer oc bb;
1860 close_out oc;
1861 Unix.rename tmp !confpath;
1862 with exn ->
1863 dolog "error saving configuration: %s" @@ exntos exn
1866 let gc fd =
1867 let wr s n =
1868 (* This here has a potential of rising SIGPIPE, silently and
1869 seemingly harmlessly (when -gc was supplied an invalid
1870 executable for instance) probably needs revisiting *)
1871 let n' = Unix.write fd (Bytes.of_string s) 0 n in
1872 if n != n'
1873 then Utils.error "Unix.write %d = %d" n n'
1875 let href = ref (Hashtbl.create 0) in
1876 let cref = ref defconf in
1877 let push (h, dc) =
1878 let f path (pc, _pb, _px, _pa, _po) =
1879 let s =
1880 Printf.sprintf "%s\000%ld\000" path (Int32.of_float pc.lastvisit)
1882 wr s (String.length s)
1884 Hashtbl.iter f h;
1885 href := h;
1886 cref := dc;
1887 true
1889 ignore (load1 push);
1890 Unix.shutdown fd Unix.SHUTDOWN_SEND;
1891 let s = fdcontents fd in
1892 let rec f ppos =
1893 match String.index_from s ppos '\000' with
1894 | exception Not_found -> ()
1895 | zpos1 ->
1896 match String.index_from s (zpos1+1) '\000' with
1897 | exception Not_found -> error "invalid gc input in (%S) at %d" s zpos1
1898 | zpos2 ->
1899 let okey = StringLabels.sub s ~pos:ppos ~len:(zpos1-ppos) in
1900 let nkey = StringLabels.sub s ~pos:(zpos1+1) ~len:(zpos2-zpos1-1) in
1901 if emptystr nkey
1902 then (Hashtbl.remove !href okey; f (zpos2+1))
1903 else
1904 match Hashtbl.find !href okey with
1905 | exception Not_found -> Utils.error "gc: cannot find %S" okey
1906 | v ->
1907 Hashtbl.remove !href okey;
1908 Hashtbl.replace !href nkey v;
1909 f (zpos2+1)
1911 f 0;
1912 let bb = Buffer.create 32768 in
1913 let save2 (_h, dc) = save1 bb (fun _ _ -> ()) 0 !href dc in
1914 if load1 save2 && Buffer.length bb > 0
1915 then (
1917 let tmp = !confpath ^ ".tmp" in
1918 let oc = open_out_bin tmp in
1919 Buffer.output_buffer oc bb;
1920 close_out oc;
1921 Unix.rename tmp !confpath;
1922 with exn ->
1923 dolog "error saving configuration: %s" @@ exntos exn
1927 let logcurrently = function
1928 | Idle -> dolog "Idle"
1929 | Loading (l, gen) ->
1930 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1931 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1932 dolog
1933 "Tiling %d[%d,%d] page=%s cs=%s angle"
1934 l.pageno col row (~> pageopaque)
1935 (CSTE.to_string colorspace)
1937 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1938 angle gen conf.angle state.gen
1939 tilew tileh
1940 conf.tilew conf.tileh
1942 | Outlining _ ->
1943 dolog "outlining"