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