Another small step towards genericity
[llpp.git] / config.ml
blobe274b2da42d69c6e10bb1f50b22cf5b7e08b5466
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 = 20 * Wsi.fontsizefactor ()
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)
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 method scroll : int -> int -> uioh
173 method zoom : float -> int -> int -> unit
174 end;;
176 module type TextEnumType =
178 type t
179 val name : string
180 val names : string array
181 end;;
183 module TextEnumMake (Ten : TextEnumType) =
184 struct
185 let names = Ten.names;;
186 let to_int (t : Ten.t) = Obj.magic t;;
187 let to_string t = names.(to_int t);;
188 let of_int n : Ten.t = Obj.magic n;;
189 let of_string s =
190 let rec find i =
191 if i = Array.length names
192 then failwith ("invalid " ^ Ten.name ^ ": " ^ s)
193 else (
194 if Ten.names.(i) = s
195 then of_int i
196 else find (i+1)
198 in find 0;;
199 end;;
201 module CSTE = TextEnumMake (struct
202 type t = colorspace;;
203 let name = "colorspace";;
204 let names = [|"rgb"; "bgr"; "gray"|];;
205 end);;
207 module MTE = TextEnumMake (struct
208 type t = mark;;
209 let name = "mark";;
210 let names = [|"page"; "block"; "line"; "word"|];;
211 end);;
213 module FMTE = TextEnumMake (struct
214 type t = fitmodel;;
215 let name = "fitmodel";;
216 let names = [|"width"; "proportional"; "page"|];;
217 end);;
219 type conf =
220 { mutable scrollbw : int
221 ; mutable scrollh : int
222 ; mutable scrollb : scrollb
223 ; mutable icase : bool
224 ; mutable preload : bool
225 ; mutable pagebias : int
226 ; mutable verbose : bool
227 ; mutable debug : bool
228 ; mutable scrollstep : int
229 ; mutable hscrollstep : int
230 ; mutable maxhfit : bool
231 ; mutable crophack : bool
232 ; mutable autoscrollstep : int
233 ; mutable maxwait : float option
234 ; mutable hlinks : bool
235 ; mutable underinfo : bool
236 ; mutable interpagespace : interpagespace
237 ; mutable zoom : float
238 ; mutable presentation : bool
239 ; mutable angle : angle
240 ; mutable cwinw : int
241 ; mutable cwinh : int
242 ; mutable savebmarks : bool
243 ; mutable fitmodel : fitmodel
244 ; mutable trimmargins : trimmargins
245 ; mutable trimfuzz : irect
246 ; mutable memlimit : memsize
247 ; mutable texcount : texcount
248 ; mutable sliceheight : sliceheight
249 ; mutable thumbw : width
250 ; mutable jumpback : bool
251 ; mutable bgcolor : rgb
252 ; mutable sbarcolor : rgba
253 ; mutable sbarhndlcolor : rgba
254 ; mutable bedefault : bool
255 ; mutable tilew : int
256 ; mutable tileh : int
257 ; mutable mustoresize : memsize
258 ; mutable checkers : bool
259 ; mutable aalevel : int
260 ; mutable urilauncher : string
261 ; mutable pathlauncher : string
262 ; mutable colorspace : colorspace
263 ; mutable invert : bool
264 ; mutable colorscale : float
265 ; mutable ghyllscroll : (int * int * int) option
266 ; mutable columns : columns
267 ; mutable beyecolumns : columncount option
268 ; mutable selcmd : string
269 ; mutable paxcmd : string
270 ; mutable passcmd : string
271 ; mutable savecmd : string
272 ; mutable updatecurs : bool
273 ; mutable keyhashes : (string * keyhash) list
274 ; mutable hfsize : int
275 ; mutable pgscale : float
276 ; mutable usepbo : bool
277 ; mutable wheelbypage : bool
278 ; mutable stcmd : string
279 ; mutable riani : bool
280 ; mutable pax : float option
281 ; mutable paxmark : mark
282 ; mutable leftscroll : bool
283 ; mutable title : string
284 ; mutable lastvisit : float
285 ; mutable annotinline : bool
286 ; mutable coarseprespos : bool
287 ; mutable css : css
288 ; mutable usedoccss : usedoccss
289 ; mutable key : string
291 and columns =
292 | Csingle of singlecolumn
293 | Cmulti of multicolumns
294 | Csplit of splitcolumns
295 and outlinekind =
296 | Onone
297 | Oanchor of anchor
298 | Ouri of uri
299 | Olaunch of launchcommand
300 | Oremote of (filename * pageno)
301 | Oremotedest of (filename * destname)
302 | Ohistory of (filename * conf * outline list * x * anchor * filename)
303 and outline = (caption * outlinelevel * outlinekind)
304 and outlinelevel = int
305 and rgb = (float * float * float)
306 and rgba = (float * float * float * float)
309 type page =
310 { pageno : int
311 ; pagedimno : int
312 ; pagew : int
313 ; pageh : int
314 ; pagex : int
315 ; pagey : int
316 ; pagevw : int
317 ; pagevh : int
318 ; pagedispx : int
319 ; pagedispy : int
320 ; pagecol : int
324 type tile = opaque * pixmapsize * elapsed
325 and elapsed = float;;
326 type pagemapkey = pageno * gen;;
327 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
328 and row = int
329 and col = int
330 and currently =
331 | Idle
332 | Loading of (page * gen)
333 | Tiling of (
334 page * opaque * colorspace * angle * gen * col * row * width * height
336 | Outlining of outline list
339 type mpos = int * int
340 and mstate =
341 | Msel of (mpos * mpos)
342 | Mpan of mpos
343 | Mscrolly | Mscrollx
344 | Mzoom of (buttonno * step * mpos)
345 | Mzoomrect of (mpos * mpos)
346 | Mnone
347 and buttonno = int
348 and step = int
351 type mode =
352 | Birdseye of (conf * leftx * pageno * pageno * anchor)
353 | Textentry of (textentry * onleave)
354 | View
355 | LinkNav of linktarget
356 and onleave = leavetextentrystatus -> unit
357 and leavetextentrystatus = | Cancel | Confirm
358 and helpitem = string * int * action
359 and action =
360 | Noaction
361 | Action of (uioh -> uioh)
362 and linktarget =
363 | Ltexact of (pageno * direction)
364 | Ltgendir of direction
365 | Ltnotready of (pageno * direction)
366 and direction = int (* -1, 0, 1 *)
367 and textentry = string * string * onhist option
368 * onkey * ondone * cancelonempty
369 and onkey = string -> Keys.t -> te
370 and ondone = string -> unit
371 and histcancel = unit -> unit
372 and onhist = ((histcmd -> string) * histcancel)
373 and histcmd = HCnext | HCprev | HCfirst | HClast
374 and cancelonempty = bool
375 and te =
376 | TEstop
377 | TEdone of string
378 | TEcont of string
379 | TEswitch of textentry
382 type 'a circbuf =
383 { store : 'a array
384 ; mutable rc : int
385 ; mutable wc : int
386 ; mutable len : int
390 type state =
391 { mutable ss : Unix.file_descr
392 ; mutable wsfd : Unix.file_descr
393 ; mutable stderr : Unix.file_descr
394 ; mutable errmsgs : Buffer.t
395 ; mutable newerrmsgs : bool
396 ; mutable w : int
397 ; mutable x : x
398 ; mutable y : y
399 ; mutable anchor : anchor
400 ; mutable ranchors : (string * string * anchor * string) list
401 ; mutable maxy : int
402 ; mutable layout : page list
403 ; pagemap : (pagemapkey, opaque) Hashtbl.t
404 ; tilemap : (tilemapkey, tile) Hashtbl.t
405 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
406 ; mutable pdims : (pageno * width * height * leftx) list
407 ; mutable pagecount : int
408 ; mutable currently : currently
409 ; mutable mstate : mstate
410 ; mutable searchpattern : string
411 ; mutable rects : (pageno * rectcolor * rect) list
412 ; mutable rects1 : (pageno * rectcolor * rect) list
413 ; prects : (pageno, float array) Hashtbl.t
414 ; mutable text : string
415 ; mutable winstate : Wsi.winstate list
416 ; mutable mode : mode
417 ; mutable uioh : uioh
418 ; mutable outlines : outline array
419 ; mutable bookmarks : outline list
420 ; mutable path : string
421 ; mutable password : string
422 ; mutable nameddest : string
423 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
424 ; mutable memused : memsize
425 ; mutable gen : gen
426 ; mutable throttle : (page list * int * float) option
427 ; mutable autoscroll : int option
428 ; mutable ghyll : (int option -> unit)
429 ; mutable help : helpitem array
430 ; mutable docinfo : (int * string) list
431 ; mutable checkerstexid : GlTex.texture_id option
432 ; hists : hists
433 ; mutable prevzoom : (float * int)
434 ; mutable progress : float
435 ; mutable redisplay : bool
436 ; mutable mpos : mpos
437 ; mutable keystate : keystate
438 ; mutable glinks : bool
439 ; mutable prevcolumns : (columns * float) option
440 ; mutable winw : int
441 ; mutable winh : int
442 ; mutable reprf : (unit -> unit)
443 ; mutable origin : string
444 ; mutable roam : (unit -> unit)
445 ; mutable bzoom : bool
446 ; mutable traw : [`float] Raw.t
447 ; mutable vraw : [`float] Raw.t
448 ; mutable lnava : (pageno * linkno) option
449 ; mutable slideshow : int
450 ; mutable statkeyhack : (float * string)
452 and hists =
453 { pat : string circbuf
454 ; pag : string circbuf
455 ; nav : anchor circbuf
456 ; sel : string circbuf
460 let emptyanchor = (0, 0.0, 0.0);;
461 let emptykeyhash = Hashtbl.create 0;;
462 let noghyll _ = ();;
463 let noreprf () = ();;
464 let noroam () = ();;
466 let nouioh : uioh = object (self)
467 method display = ()
468 method key _ _ = self
469 method multiclick _ _ _ _ = self
470 method button _ _ _ _ _ = self
471 method motion _ _ = self
472 method pmotion _ _ = self
473 method infochanged _ = ()
474 method scrollpw = (0, nan, nan)
475 method scrollph = (0, nan, nan)
476 method modehash = emptykeyhash
477 method eformsgs = false
478 method alwaysscrolly = false
479 method scroll _ _ = self
480 method zoom _ _ _ = ()
481 end;;
483 let platform_to_string = function
484 | Punknown -> "unknown"
485 | Plinux -> "Linux"
486 | Posx -> "OSX"
487 | Psun -> "Sun"
488 | Pbsd -> "BSD"
491 let version () =
492 Printf.sprintf "llpp version %s, fitz %s, ocaml %s/%d bit"
493 Help.version (fz_version ()) Sys.ocaml_version Sys.word_size
496 let defconf =
497 { scrollbw = 7
498 ; scrollh = 12
499 ; scrollb = scrollbhv lor scrollbvv
500 ; icase = true
501 ; preload = true
502 ; pagebias = 0
503 ; verbose = false
504 ; debug = false
505 ; scrollstep = 24
506 ; hscrollstep = 24
507 ; maxhfit = true
508 ; crophack = false
509 ; autoscrollstep = 2
510 ; maxwait = None
511 ; hlinks = false
512 ; underinfo = false
513 ; interpagespace = 2
514 ; zoom = 1.0
515 ; presentation = false
516 ; angle = 0
517 ; cwinw = 1200
518 ; cwinh = 1000
519 ; savebmarks = true
520 ; fitmodel = FitProportional
521 ; trimmargins = false
522 ; trimfuzz = (0,0,0,0)
523 ; memlimit = 32 lsl 20
524 ; texcount = 256
525 ; sliceheight = 24
526 ; thumbw = 76
527 ; jumpback = true
528 ; bgcolor = (0.5, 0.5, 0.5)
529 ; sbarcolor = (0.64, 0.64, 0.64, 0.7)
530 ; sbarhndlcolor = (0.0, 0.0, 0.0, 0.7)
531 ; bedefault = false
532 ; tilew = 2048
533 ; tileh = 2048
534 ; mustoresize = 256 lsl 20
535 ; checkers = true
536 ; aalevel = 8
537 ; urilauncher =
538 (match platform with
539 | Plinux | Psun | Pbsd -> "xdg-open \"%s\""
540 | Posx -> "open \"%s\""
541 | Punknown -> "echo %s")
542 ; pathlauncher = "lp \"%s\""
543 ; selcmd =
544 (match platform with
545 | Plinux | Pbsd | Psun -> "xsel -i"
546 | Posx -> "pbcopy"
547 | Punknown -> "cat")
548 ; paxcmd = "cat"
549 ; passcmd = E.s
550 ; savecmd = E.s
551 ; colorspace = Rgb
552 ; invert = false
553 ; colorscale = 1.0
554 ; ghyllscroll = None
555 ; columns = Csingle [||]
556 ; beyecolumns = None
557 ; updatecurs = true
558 ; hfsize = 12 * Wsi.fontsizefactor ()
559 ; pgscale = 1.0
560 ; usepbo = false
561 ; wheelbypage = false
562 ; stcmd = "echo SyncTex"
563 ; riani = false
564 ; pax = None
565 ; paxmark = Mark_word
566 ; leftscroll = false
567 ; title = E.s
568 ; lastvisit = 0.0
569 ; annotinline = true
570 ; coarseprespos = false
571 ; css = E.s
572 ; usedoccss = true
573 ; key = E.s
574 ; keyhashes =
575 let mk n = (n, Hashtbl.create 1) in
576 [ mk "global"
577 ; mk "info"
578 ; mk "help"
579 ; mk "outline"
580 ; mk "listview"
581 ; mk "birdseye"
582 ; mk "textentry"
583 ; mk "links"
584 ; mk "view"
589 let conf = { defconf with angle = defconf.angle };;
591 let gotourl url =
592 let command = Str.global_replace percentsre url conf.urilauncher in
593 try ignore @@ spawn command []
594 with exn -> dolog "failed to execute `%s': %s" command @@ exntos exn
597 let gotouri uri =
598 if emptystr conf.urilauncher
599 then dolog "%s" uri
600 else
601 match geturl uri with
602 | "" -> dolog "obtained empty url from uri %S" uri
603 | url -> gotourl url
606 let makehelp () =
607 let strings =
608 version ()
609 :: "(searching in this text works just by typing (i.e. no initial '/'))"
610 :: E.s :: Help.keys
612 List.map (fun s ->
613 match geturl s with
614 | "" -> (s, 0, Noaction)
615 | url -> (s, 0, Action (fun uioh -> gotourl url; uioh))
616 ) strings;
619 let cbnew n v =
620 { store = Array.make n v
621 ; rc = 0
622 ; wc = 0
623 ; len = 0
627 let cbcap b = Array.length b.store;;
629 let cbput ?(update_rc=true) b v =
630 let cap = cbcap b in
631 b.store.(b.wc) <- v;
632 b.wc <- (b.wc + 1) mod cap;
633 if update_rc
634 then b.rc <- b.wc;
635 b.len <- min (b.len + 1) cap;
638 let cbput_dont_update_rc b v = cbput ~update_rc:false b v;;
640 let cbempty b = b.len = 0;;
642 let cbgetg b circular dir =
643 if cbempty b
644 then b.store.(0)
645 else
646 let rc = b.rc + dir in
647 let rc =
648 if circular
649 then (
650 if rc = -1
651 then b.len-1
652 else (
653 if rc >= b.len
654 then 0
655 else rc
658 else bound rc 0 (b.len-1)
660 b.rc <- rc;
661 b.store.(rc);
664 let cbget b = cbgetg b false;;
665 let cbgetc b = cbgetg b true;;
667 let state =
668 { ss = Unix.stdin
669 ; wsfd = Unix.stdin
670 ; stderr = Unix.stderr
671 ; errmsgs = Buffer.create 0
672 ; newerrmsgs = false
673 ; x = 0
674 ; y = 0
675 ; w = 0
676 ; anchor = emptyanchor
677 ; ranchors = []
678 ; layout = []
679 ; maxy = max_int
680 ; tilelru = Queue.create ()
681 ; pagemap = Hashtbl.create 10
682 ; tilemap = Hashtbl.create 10
683 ; pdims = []
684 ; pagecount = 0
685 ; currently = Idle
686 ; mstate = Mnone
687 ; rects = []
688 ; rects1 = []
689 ; prects = Hashtbl.create 1
690 ; text = E.s
691 ; mode = View
692 ; winstate = []
693 ; searchpattern = E.s
694 ; outlines = E.a
695 ; bookmarks = []
696 ; path = E.s
697 ; password = E.s
698 ; nameddest = E.s
699 ; geomcmds = E.s, []
700 ; hists =
701 { nav = cbnew 10 emptyanchor
702 ; pat = cbnew 10 E.s
703 ; pag = cbnew 10 E.s
704 ; sel = cbnew 10 E.s
706 ; memused = 0
707 ; gen = 0
708 ; throttle = None
709 ; autoscroll = None
710 ; ghyll = noghyll
711 ; help = E.a
712 ; docinfo = []
713 ; checkerstexid = None
714 ; prevzoom = (1.0, 0)
715 ; progress = -1.0
716 ; uioh = nouioh
717 ; redisplay = true
718 ; mpos = (-1, -1)
719 ; keystate = KSnone
720 ; glinks = false
721 ; prevcolumns = None
722 ; winw = -1
723 ; winh = -1
724 ; reprf = noreprf
725 ; origin = E.s
726 ; roam = noroam
727 ; bzoom = false
728 ; traw = Raw.create_static `float ~len:8
729 ; vraw = Raw.create_static `float ~len:8
730 ; lnava = None
731 ; slideshow = 0
732 ; statkeyhack = (nan, E.s)
736 let copykeyhashes c =
737 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
740 let calcips h =
741 let d = state.winh - h in
742 max conf.interpagespace ((d + 1) / 2)
745 let rowyh (c, coverA, coverB) b n =
746 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
747 then
748 let _, _, vy, (_, _, h, _) = b.(n) in
749 (vy, h)
750 else
751 let n' = n - coverA in
752 let d = n' mod c in
753 let s = n - d in
754 let e = min state.pagecount (s + c) in
755 let rec find m miny maxh = if m = e then miny, maxh else
756 let _, _, y, (_, _, h, _) = b.(m) in
757 let miny = min miny y in
758 let maxh = max maxh h in
759 find (m+1) miny maxh
760 in find s max_int 0
763 let page_of_y y =
764 let ((c, coverA, coverB) as cl), b =
765 match conf.columns with
766 | Csingle b -> (1, 0, 0), b
767 | Cmulti (c, b) -> c, b
768 | Csplit (_, b) -> (1, 0, 0), b
770 if Array.length b = 0
771 then -1
772 else
773 let rec bsearch nmin nmax =
774 if nmin > nmax
775 then bound nmin 0 (state.pagecount-1)
776 else
777 let n = (nmax + nmin) / 2 in
778 let vy, h = rowyh cl b n in
779 let y0, y1 =
780 if conf.presentation
781 then
782 let ips = calcips h in
783 let y0 = vy - ips in
784 let y1 = vy + h + ips in
785 y0, y1
786 else (
787 if n = 0
788 then 0, vy + h + conf.interpagespace
789 else
790 let y0 = vy - conf.interpagespace in
791 y0, y0 + h + conf.interpagespace
794 if y >= y0 && y < y1
795 then (
796 if c = 1
797 then n
798 else (
799 if n > coverA
800 then
801 if n < state.pagecount - coverB
802 then ((n-coverA)/c)*c + coverA
803 else n
804 else n
807 else (
808 if y > y0
809 then bsearch (n+1) nmax
810 else bsearch nmin (n-1)
813 bsearch 0 (state.pagecount-1);
816 let calcheight () =
817 match conf.columns with
818 | Cmulti ((_, _, _) as cl, b) ->
819 if Array.length b > 0
820 then
821 let y, h = rowyh cl b (Array.length b - 1) in
822 y + h + (if conf.presentation then calcips h else 0)
823 else 0
824 | Csingle b ->
825 if Array.length b > 0
826 then
827 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
828 y + h + (if conf.presentation then calcips h else 0)
829 else 0
830 | Csplit (_, b) ->
831 if Array.length b > 0
832 then
833 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
834 y + h
835 else 0
838 let getpageywh pageno =
839 let pageno = bound pageno 0 (state.pagecount-1) in
840 match conf.columns with
841 | Csingle b ->
842 if Array.length b = 0
843 then 0, 0, 0
844 else
845 let (_, _, y, (_, w, h, _)) = b.(pageno) in
846 let y =
847 if conf.presentation
848 then y - calcips h
849 else y
851 y, w, h
852 | Cmulti (cl, b) ->
853 if Array.length b = 0
854 then 0, 0, 0
855 else
856 let y, h = rowyh cl b pageno in
857 let (_, _, _, (_, w, _, _)) = b.(pageno) in
858 let y =
859 if conf.presentation
860 then y - calcips h
861 else y
863 y, w, h
864 | Csplit (c, b) ->
865 if Array.length b = 0
866 then 0, 0, 0
867 else
868 let n = pageno*c in
869 let (_, _, y, (_, w, h, _)) = b.(n) in
870 y, w / c, h
873 let getpageyh pageno =
874 let y,_,h = getpageywh pageno in
875 y, h;
878 let getpagedim pageno =
879 let rec f ppdim l =
880 match l with
881 | (n, _, _, _) as pdim :: rest ->
882 if n >= pageno
883 then (if n = pageno then pdim else ppdim)
884 else f pdim rest
886 | [] -> ppdim
888 f (-1, -1, -1, -1) state.pdims
891 let getpdimno pageno =
892 let rec f p l =
893 let np = succ p in
894 match l with
895 | (n, _, _, _) :: rest ->
896 if n >= pageno
897 then (if n = pageno then np else p)
898 else f np rest
900 | [] -> p
902 f ~-1 state.pdims
905 let getpagey pageno = fst (getpageyh pageno);;
907 let getanchor1 l =
908 let top =
909 let coloff = l.pagecol * l.pageh in
910 float (l.pagey + coloff) /. float l.pageh
912 let dtop =
913 if l.pagedispy = 0
914 then
916 else (
917 if conf.presentation
918 then float l.pagedispy /. float (calcips l.pageh)
919 else float l.pagedispy /. float conf.interpagespace
922 (l.pageno, top, dtop)
925 let getanchor () =
926 match state.layout with
927 | l :: _ -> getanchor1 l
928 | [] ->
929 let n = page_of_y state.y in
930 if n = -1
931 then state.anchor
932 else
933 let y, h = getpageyh n in
934 let dy = y - state.y in
935 let dtop =
936 if conf.presentation
937 then
938 let ips = calcips h in
939 float (dy + ips) /. float ips
940 else
941 float dy /. float conf.interpagespace
943 (n, 0.0, dtop)
946 let fontpath = ref E.s;;
948 type historder = [ `lastvisit | `title | `path | `file ];;
950 module KeyMap =
951 Map.Make (struct type t = (int * int) let compare = compare end);;
953 let unentS s =
954 let l = String.length s in
955 let b = Buffer.create l in
956 Parser.unent b s 0 l;
957 Buffer.contents b;
960 let home =
961 try Sys.getenv "HOME"
962 with exn ->
963 dolog "cannot determine home directory location: %s" @@ exntos exn;
967 let modifier_of_string = function
968 | "alt" -> Wsi.altmask
969 | "shift" -> Wsi.shiftmask
970 | "ctrl" | "control" -> Wsi.ctrlmask
971 | "meta" -> Wsi.metamask
972 | _ -> 0
975 let keys_of_string s =
976 let key_of_string r s =
977 let elems = Str.full_split r s in
978 let f n k m =
979 let g s =
980 let m1 = modifier_of_string s in
981 if m1 = 0
982 then (Wsi.namekey s, m)
983 else (k, m lor m1)
984 in function
985 | Str.Delim s when n land 1 = 0 -> g s
986 | Str.Text s -> g s
987 | Str.Delim _ -> (k, m)
989 let rec loop n k m = function
990 | [] -> (k, m)
991 | x :: xs ->
992 let k, m = f n k m x in
993 loop (n+1) k m xs
995 loop 0 0 0 elems
997 let elems = Str.split whitere s in
998 List.map (key_of_string (Str.regexp "-")) elems
1001 let config_of c attrs =
1002 let apply c k v =
1004 match k with
1005 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
1006 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
1007 | "case-insensitive-search" -> { c with icase = bool_of_string v }
1008 | "preload" -> { c with preload = bool_of_string v }
1009 | "page-bias" -> { c with pagebias = int_of_string v }
1010 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
1011 | "horizontal-scroll-step" ->
1012 { c with hscrollstep = max (int_of_string v) 1 }
1013 | "auto-scroll-step" ->
1014 { c with autoscrollstep = max 0 (int_of_string v) }
1015 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
1016 | "crop-hack" -> { c with crophack = bool_of_string v }
1017 | "throttle" ->
1018 let mw =
1019 match String.map asciilower v with
1020 | "true" -> Some infinity
1021 | "false" -> None
1022 | f -> Some (float_of_string f)
1024 { c with maxwait = mw }
1025 | "highlight-links" -> { c with hlinks = bool_of_string v }
1026 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
1027 | "vertical-margin" ->
1028 { c with interpagespace = max 0 (int_of_string v) }
1029 | "zoom" ->
1030 let zoom = float_of_string v /. 100. in
1031 let zoom = max zoom 0.0 in
1032 { c with zoom = zoom }
1033 | "presentation" -> { c with presentation = bool_of_string v }
1034 | "rotation-angle" -> { c with angle = int_of_string v }
1035 | "width" -> { c with cwinw = max 20 (int_of_string v) }
1036 | "height" -> { c with cwinh = max 20 (int_of_string v) }
1037 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
1038 | "proportional-display" ->
1039 let fm =
1040 if bool_of_string v
1041 then FitProportional
1042 else FitWidth
1044 { c with fitmodel = fm }
1045 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
1046 | "pixmap-cache-size" ->
1047 { c with memlimit = max 2 (int_of_string_with_suffix v) }
1048 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
1049 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
1050 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
1051 | "persistent-location" -> { c with jumpback = bool_of_string v }
1052 | "background-color" -> { c with bgcolor = color_of_string v }
1053 | "scrollbar-color" -> { c with sbarcolor = rgba_of_string v }
1054 | "scrollbar-handle-color" -> { c with sbarhndlcolor = rgba_of_string v }
1055 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
1056 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
1057 | "mupdf-store-size" ->
1058 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
1059 | "checkers" -> { c with checkers = bool_of_string v }
1060 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
1061 | "trim-margins" -> { c with trimmargins = bool_of_string v }
1062 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
1063 | "uri-launcher" -> { c with urilauncher = unentS v }
1064 | "path-launcher" -> { c with pathlauncher = unentS v }
1065 | "color-space" -> { c with colorspace = CSTE.of_string v }
1066 | "invert-colors" -> { c with invert = bool_of_string v }
1067 | "brightness" -> { c with colorscale = float_of_string v }
1068 | "ghyllscroll" -> { c with ghyllscroll = ghyllscroll_of_string v }
1069 | "columns" ->
1070 let (n, _, _) as nab = multicolumns_of_string v in
1071 if n < 0
1072 then { c with columns = Csplit (-n, E.a) }
1073 else { c with columns = Cmulti (nab, E.a) }
1074 | "birds-eye-columns" ->
1075 { c with beyecolumns = Some (max (int_of_string v) 2) }
1076 | "selection-command" -> { c with selcmd = unentS v }
1077 | "synctex-command" -> { c with stcmd = unentS v }
1078 | "pax-command" -> { c with paxcmd = unentS v }
1079 | "askpass-command" -> { c with passcmd = unentS v }
1080 | "savepath-command" -> { c with savecmd = unentS v }
1081 | "update-cursor" -> { c with updatecurs = bool_of_string v }
1082 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
1083 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
1084 | "use-pbo" -> { c with usepbo = bool_of_string v }
1085 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
1086 | "horizontal-scrollbar-visible" ->
1087 let b =
1088 if bool_of_string v
1089 then c.scrollb lor scrollbhv
1090 else c.scrollb land (lnot scrollbhv)
1092 { c with scrollb = b }
1093 | "vertical-scrollbar-visible" ->
1094 let b =
1095 if bool_of_string v
1096 then c.scrollb lor scrollbvv
1097 else c.scrollb land (lnot scrollbvv)
1099 { c with scrollb = b }
1100 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
1101 | "point-and-x" ->
1102 { c with pax =
1103 if bool_of_string v
1104 then Some 0.0
1105 else None }
1106 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
1107 | "scroll-bar-on-the-left" -> { c with leftscroll = bool_of_string v }
1108 | "title" -> { c with title = unentS v }
1109 | "last-visit" -> { c with lastvisit = float_of_string v }
1110 | "edit-annotations-inline" -> { c with annotinline = bool_of_string v }
1111 | "coarse-presentation-positioning" ->
1112 { c with coarseprespos = bool_of_string v }
1113 | "use-document-css" -> { c with usedoccss = bool_of_string v }
1114 | _ -> c
1115 with exn ->
1116 dolog "error processing attribute (`%S' = `%S'): %s" k v @@ exntos exn;
1119 let rec fold c = function
1120 | [] -> c
1121 | (k, v) :: rest ->
1122 let c = apply c k v in
1123 fold c rest
1125 fold { c with keyhashes = copykeyhashes c } attrs;
1128 let fromstring f pos n v d =
1129 try f v
1130 with exn ->
1131 dolog "error processing attribute (%S=%S) at %d\n%s" n v pos @@ exntos exn;
1135 let bookmark_of attrs =
1136 let rec fold title page rely visy = function
1137 | ("title", v) :: rest -> fold v page rely visy rest
1138 | ("page", v) :: rest -> fold title v rely visy rest
1139 | ("rely", v) :: rest -> fold title page v visy rest
1140 | ("visy", v) :: rest -> fold title page rely v rest
1141 | _ :: rest -> fold title page rely visy rest
1142 | [] -> title, page, rely, visy
1144 fold "invalid" "0" "0" "0" attrs
1147 let doc_of attrs =
1148 let rec fold path key page rely pan visy origin = function
1149 | ("path", v) :: rest -> fold v key page rely pan visy origin rest
1150 | ("key", v) :: rest -> fold path v page rely pan visy origin rest
1151 | ("page", v) :: rest -> fold path key v rely pan visy origin rest
1152 | ("rely", v) :: rest -> fold path key page v pan visy origin rest
1153 | ("pan", v) :: rest -> fold path key page rely v visy origin rest
1154 | ("visy", v) :: rest -> fold path key page rely pan v origin rest
1155 | ("origin", v) :: rest -> fold path key page rely pan visy v rest
1156 | _ :: rest -> fold path key page rely pan visy origin rest
1157 | [] -> path, key, page, rely, pan, visy, origin
1159 fold E.s E.s "0" "0" "0" "0" E.s attrs
1162 let map_of attrs =
1163 let rec fold rs ls = function
1164 | ("out", v) :: rest -> fold v ls rest
1165 | ("in", v) :: rest -> fold rs v rest
1166 | _ :: rest -> fold ls rs rest
1167 | [] -> ls, rs
1169 fold E.s E.s attrs
1172 let setconf dst src =
1173 dst.scrollbw <- src.scrollbw;
1174 dst.scrollh <- src.scrollh;
1175 dst.icase <- src.icase;
1176 dst.preload <- src.preload;
1177 dst.pagebias <- src.pagebias;
1178 dst.verbose <- src.verbose;
1179 dst.scrollstep <- src.scrollstep;
1180 dst.maxhfit <- src.maxhfit;
1181 dst.crophack <- src.crophack;
1182 dst.autoscrollstep <- src.autoscrollstep;
1183 dst.maxwait <- src.maxwait;
1184 dst.hlinks <- src.hlinks;
1185 dst.underinfo <- src.underinfo;
1186 dst.interpagespace <- src.interpagespace;
1187 dst.zoom <- src.zoom;
1188 dst.presentation <- src.presentation;
1189 dst.angle <- src.angle;
1190 dst.cwinw <- src.cwinw;
1191 dst.cwinh <- src.cwinh;
1192 dst.savebmarks <- src.savebmarks;
1193 dst.memlimit <- src.memlimit;
1194 dst.fitmodel <- src.fitmodel;
1195 dst.texcount <- src.texcount;
1196 dst.sliceheight <- src.sliceheight;
1197 dst.thumbw <- src.thumbw;
1198 dst.jumpback <- src.jumpback;
1199 dst.bgcolor <- src.bgcolor;
1200 dst.tilew <- src.tilew;
1201 dst.tileh <- src.tileh;
1202 dst.mustoresize <- src.mustoresize;
1203 dst.checkers <- src.checkers;
1204 dst.aalevel <- src.aalevel;
1205 dst.trimmargins <- src.trimmargins;
1206 dst.trimfuzz <- src.trimfuzz;
1207 dst.urilauncher <- src.urilauncher;
1208 dst.colorspace <- src.colorspace;
1209 dst.invert <- src.invert;
1210 dst.colorscale <- src.colorscale;
1211 dst.ghyllscroll <- src.ghyllscroll;
1212 dst.columns <- src.columns;
1213 dst.beyecolumns <- src.beyecolumns;
1214 dst.selcmd <- src.selcmd;
1215 dst.updatecurs <- src.updatecurs;
1216 dst.pathlauncher <- src.pathlauncher;
1217 dst.keyhashes <- copykeyhashes src;
1218 dst.hfsize <- src.hfsize;
1219 dst.hscrollstep <- src.hscrollstep;
1220 dst.pgscale <- src.pgscale;
1221 dst.usepbo <- src.usepbo;
1222 dst.wheelbypage <- src.wheelbypage;
1223 dst.stcmd <- src.stcmd;
1224 dst.paxcmd <- src.paxcmd;
1225 dst.passcmd <- src.passcmd;
1226 dst.savecmd <- src.savecmd;
1227 dst.scrollb <- src.scrollb;
1228 dst.riani <- src.riani;
1229 dst.paxmark <- src.paxmark;
1230 dst.leftscroll <- src.leftscroll;
1231 dst.title <- src.title;
1232 dst.annotinline <- src.annotinline;
1233 dst.coarseprespos <- src.coarseprespos;
1234 dst.css <- src.css;
1235 dst.usedoccss <- src.usedoccss;
1236 dst.sbarcolor <- src.sbarcolor;
1237 dst.sbarhndlcolor <- src.sbarhndlcolor;
1238 dst.key <- src.key;
1239 dst.pax <-
1240 if src.pax = None
1241 then None
1242 else Some 0.0;
1245 let findkeyhash c name =
1246 try List.assoc name c.keyhashes
1247 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
1250 let get s =
1251 let open Parser in
1252 let h = Hashtbl.create 10 in
1253 let dc = { defconf with angle = defconf.angle } in
1254 let rec toplevel v t spos _ =
1255 match t with
1256 | Vdata | Vcdata | Vend -> v
1257 | Vopen ("llppconfig", _, closed) ->
1258 if closed
1259 then v
1260 else { v with f = llppconfig }
1261 | Vopen _ -> parse_error "unexpected subelement at top level" s spos
1262 | Vclose _ -> parse_error "unexpected close at top level" s spos
1264 and llppconfig v t spos _ =
1265 match t with
1266 | Vdata | Vcdata -> v
1267 | Vend -> parse_error "unexpected end of input in llppconfig" s spos
1268 | Vopen ("defaults", attrs, closed) ->
1269 let c = config_of dc attrs in
1270 setconf dc c;
1271 if closed
1272 then v
1273 else { v with f = defaults }
1275 | Vopen ("ui-font", attrs, closed) ->
1276 let rec getsize size = function
1277 | [] -> size
1278 | ("size", v) :: rest ->
1279 let size =
1280 fromstring int_of_string spos "size" v fstate.fontsize in
1281 getsize size rest
1282 | l -> getsize size l
1284 fstate.fontsize <- getsize fstate.fontsize attrs;
1285 if closed
1286 then v
1287 else { v with f = uifont (Buffer.create 10) }
1289 | Vopen ("doc", attrs, closed) ->
1290 let pathent, key, spage, srely, span, svisy, origin = doc_of attrs in
1291 let path = unentS pathent
1292 and origin = unentS origin
1293 and pageno = fromstring int_of_string spos "page" spage 0
1294 and rely = fromstring float_of_string spos "rely" srely 0.0
1295 and pan = fromstring int_of_string spos "pan" span 0
1296 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1297 let c = config_of dc attrs in
1298 c.key <- key;
1299 let anchor = (pageno, rely, visy) in
1300 if closed
1301 then (Hashtbl.add h path (c, [], pan, anchor, origin); v)
1302 else { v with f = doc path origin pan anchor c [] }
1304 | Vopen _ ->
1305 parse_error "unexpected subelement in llppconfig" s spos
1307 | Vclose "llppconfig" -> { v with f = toplevel }
1308 | Vclose _ -> parse_error "unexpected close in llppconfig" s spos
1310 and defaults v t spos _ =
1311 match t with
1312 | Vdata | Vcdata -> v
1313 | Vend -> parse_error "unexpected end of input in defaults" s spos
1314 | Vopen ("keymap", attrs, closed) ->
1315 let modename =
1316 try List.assoc "mode" attrs
1317 with Not_found -> "global" in
1318 if closed
1319 then v
1320 else
1321 let ret keymap =
1322 let h = findkeyhash dc modename in
1323 KeyMap.iter (Hashtbl.replace h) keymap;
1324 defaults
1326 { v with f = pkeymap ret KeyMap.empty }
1328 | Vopen (_, _, _) ->
1329 parse_error "unexpected subelement in defaults" s spos
1331 | Vclose "defaults" ->
1332 { v with f = llppconfig }
1334 | Vclose _ -> parse_error "unexpected close in defaults" s spos
1336 and uifont b v t spos epos =
1337 match t with
1338 | Vdata | Vcdata ->
1339 Buffer.add_substring b s spos (epos - spos);
1341 | Vopen (_, _, _) ->
1342 parse_error "unexpected subelement in ui-font" s spos
1343 | Vclose "ui-font" ->
1344 if emptystr !fontpath
1345 then fontpath := Buffer.contents b;
1346 { v with f = llppconfig }
1347 | Vclose _ -> parse_error "unexpected close in ui-font" s spos
1348 | Vend -> parse_error "unexpected end of input in ui-font" s spos
1350 and doc path origin pan anchor c bookmarks v t spos _ =
1351 match t with
1352 | Vdata | Vcdata -> v
1353 | Vend -> parse_error "unexpected end of input in doc" s spos
1354 | Vopen ("bookmarks", _, closed) ->
1355 if closed
1356 then v
1357 else { v with f = pbookmarks path origin pan anchor c bookmarks }
1359 | Vopen ("keymap", attrs, closed) ->
1360 let modename =
1361 try List.assoc "mode" attrs
1362 with Not_found -> "global"
1364 if closed
1365 then v
1366 else
1367 let ret keymap =
1368 let h = findkeyhash c modename in
1369 KeyMap.iter (Hashtbl.replace h) keymap;
1370 doc path origin pan anchor c bookmarks
1372 { v with f = pkeymap ret KeyMap.empty }
1374 | Vopen ("css", [], false) ->
1375 { v with f = pcss path origin pan anchor c bookmarks }
1377 | Vopen (_, _, _) ->
1378 parse_error "unexpected subelement in doc" s spos
1380 | Vclose "doc" ->
1381 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor, origin);
1382 { v with f = llppconfig }
1384 | Vclose _ -> parse_error "unexpected close in doc" s spos
1386 and pcss path origin pan anchor c bookmarks v t spos epos =
1387 match t with
1388 | Vdata | Vcdata ->
1389 let b = Buffer.create 10 in
1390 Buffer.add_substring b s spos (epos - spos);
1391 { v with f = pcss path origin pan anchor
1392 { c with css = Buffer.contents b }
1393 bookmarks }
1394 | Vend -> parse_error "unexpected end of input in css" s spos
1395 | Vopen _ -> parse_error "unexpected subelement in css" s spos
1396 | Vclose "css" -> { v with f = doc path origin pan anchor c bookmarks }
1397 | Vclose _ -> parse_error "unexpected close in css" s spos
1399 and pkeymap ret keymap v t spos _ =
1400 match t with
1401 | Vdata | Vcdata -> v
1402 | Vend -> parse_error "unexpected end of input in keymap" s spos
1403 | Vopen ("map", attrs, closed) ->
1404 let r, l = map_of attrs in
1405 let kss = fromstring keys_of_string spos "in" r [] in
1406 let lss = fromstring keys_of_string spos "out" l [] in
1407 let keymap =
1408 match kss with
1409 | [] -> keymap
1410 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
1411 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
1413 if closed
1414 then { v with f = pkeymap ret keymap }
1415 else
1416 let f () = v in
1417 { v with f = skip "map" f }
1419 | Vopen _ ->
1420 parse_error "unexpected subelement in keymap" s spos
1422 | Vclose "keymap" ->
1423 { v with f = ret keymap }
1425 | Vclose _ -> parse_error "unexpected close in keymap" s spos
1427 and pbookmarks path origin pan anchor c bookmarks v t spos _ =
1428 match t with
1429 | Vdata | Vcdata -> v
1430 | Vend -> parse_error "unexpected end of input in bookmarks" s spos
1431 | Vopen ("item", attrs, closed) ->
1432 let titleent, spage, srely, svisy = bookmark_of attrs in
1433 let page = fromstring int_of_string spos "page" spage 0
1434 and rely = fromstring float_of_string spos "rely" srely 0.0
1435 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1436 let bookmarks =
1437 (unentS titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
1439 if closed
1440 then { v with f = pbookmarks path origin pan anchor c bookmarks }
1441 else
1442 let f () = v in
1443 { v with f = skip "item" f }
1445 | Vopen _ ->
1446 parse_error "unexpected subelement in bookmarks" s spos
1448 | Vclose "bookmarks" ->
1449 { v with f = doc path origin pan anchor c bookmarks }
1451 | Vclose _ -> parse_error "unexpected close in bookmarks" s spos
1453 and skip tag f v t spos _ =
1454 match t with
1455 | Vdata | Vcdata -> v
1456 | Vend ->
1457 parse_error ("unexpected end of input in skipped " ^ tag) s spos
1458 | Vopen (tag', _, closed) ->
1459 if closed
1460 then v
1461 else
1462 let f' () = { v with f = skip tag f } in
1463 { v with f = skip tag' f' }
1464 | Vclose ctag ->
1465 if tag = ctag
1466 then f ()
1467 else parse_error ("unexpected close in skipped " ^ tag) s spos
1470 parse { f = toplevel; accu = () } s;
1471 h, dc;
1474 let do_load f contents =
1475 try f contents
1476 with
1477 | Parser.Parse_error (msg, s, pos) ->
1478 let subs = Parser.subs s pos in
1479 Utils.error "parse error: %s: at %d [..%S..]" msg pos subs
1481 | exn -> Utils.error "parse error: %s" @@ exntos exn
1484 let defconfpath =
1485 let dir =
1486 let xdgconfdir = Utils.getenvwithdef "XDG_CONFIG_HOME" E.s in
1487 if emptystr xdgconfdir
1488 then
1490 let dir = Filename.concat home ".config" in
1491 if Sys.is_directory dir then dir else home
1492 with _ -> home
1493 else xdgconfdir
1495 Filename.concat dir "llpp.conf"
1498 let confpath = ref defconfpath;;
1500 let load2 f default =
1501 match filecontents !confpath with
1502 | contents -> f @@ do_load get contents
1503 | exception Unix.Unix_error (Unix.ENOENT, "open", _) ->
1504 f (Hashtbl.create 0, defconf)
1505 | exception exn ->
1506 dolog "error loading configuration from `%S': %s" !confpath @@ exntos exn;
1507 default
1510 let load1 f = load2 f false;;
1512 let load openlast =
1513 let f (h, dc) =
1514 if openlast
1515 then (
1516 let path, _ =
1517 Hashtbl.fold
1518 (fun path (conf, _, _, _, _) ((_, besttime) as best) ->
1519 if conf.lastvisit > besttime
1520 then (path, conf.lastvisit)
1521 else best)
1523 (state.path, -.infinity)
1525 state.path <- path;
1527 let pc, pb, px, pa, po =
1528 let def = dc, [], 0, emptyanchor, state.origin in
1529 if emptystr state.path
1530 then def
1531 else
1532 let absname = abspath state.path in
1533 match Hashtbl.find h absname with
1534 | (c,b,x,a,_) -> (c,b,x,a,state.origin)
1535 | exception Not_found ->
1536 let exception E of (conf * outline list * int * anchor * string) in
1537 let key = try Digest.file absname |> Digest.to_hex with _ -> E.s in
1538 match (
1539 if emptystr key
1540 then ()
1541 else
1542 Hashtbl.iter (fun p ((c, _, _, _, _) as v) ->
1543 if c.key = key
1544 then (
1545 dolog "will use %s's settings due to matching keys" p;
1546 raise (E v)
1550 with
1551 | _ -> def
1552 | exception E v -> v
1554 setconf defconf dc;
1555 setconf conf pc;
1556 state.bookmarks <- pb;
1557 state.x <- px;
1558 state.origin <- po;
1559 if conf.jumpback
1560 then state.anchor <- pa;
1561 cbput state.hists.nav pa;
1562 true
1564 load1 f
1567 let gethist () =
1568 let f (h, _) =
1569 Hashtbl.fold (fun path (pc, pb, px, pa, po) accu ->
1570 (path, pc, pb, px, pa, po) :: accu)
1571 h [];
1573 load2 f []
1576 let add_attrs bb always dc c time =
1577 let o' fmt s =
1578 Buffer.add_string bb "\n ";
1579 Printf.bprintf bb fmt s
1581 let o c fmt s = if c then o' fmt s else ignore in
1582 let ob s a b = o (always || a != b) "%s='%b'" s a
1583 and op s a b = o (always || a <> b) "%s='%b'" s (a != None)
1584 and oi s a b = o (always || a != b) "%s='%d'" s a
1585 and oI s a b = o (always || a != b) "%s='%s'" s (string_with_suffix_of_int a)
1586 and oz s a b = o (always || a <> b) "%s='%g'" s (a*.100.)
1587 and oF s a b = o (always || a <> b) "%s='%f'" s a
1588 and oL s a b = o (always || a <> b) "%s='%Ld'" s a
1589 and oc s a b = o (always || a <> b) "%s='%s'" s (color_to_string a)
1590 and oA s a b = o (always || a <> b) "%s='%s'" s (rgba_to_string a)
1591 and oC s a b = o (always || a <> b) "%s='%s'" s (CSTE.to_string a)
1592 and oR s a b = o (always || a <> b) "%s='%s'" s (irect_to_string a)
1593 and oFm s a b = o (always || a <> b) "%s='%s'" s (FMTE.to_string a)
1594 and oSv s a b m =
1595 o (always || a land m <> b land m) "%s='%b'" s (a land m != 0)
1596 and oPm s a b = o (always || a <> b) "%s='%s'" s (MTE.to_string a)
1597 and os s a b =
1598 o (always || a <> b) "%s='%s'" s @@ Parser.enent a 0 (String.length a)
1599 and og s a b =
1600 if always || a <> b
1601 then
1602 match a with
1603 | Some (_N, _A, _B) -> o' "%s='%u,%u,%u'" s _N _A _B
1604 | None ->
1605 match b with
1606 | None -> ()
1607 | _ -> o' "%s='none'" s
1608 and oW s a b =
1609 if always || a <> b
1610 then
1611 let v =
1612 match a with
1613 | None -> "false"
1614 | Some f ->
1615 if f = infinity
1616 then "true"
1617 else string_of_float f
1619 o' "%s='%s'" s v
1620 and oco s a b =
1621 if always || a <> b
1622 then
1623 match a with
1624 | Cmulti ((n, a, b), _) when n > 1 -> o' "%s='%d,%d,%d'" s n a b
1625 | Csplit (n, _) when n > 1 -> o' "%s='%d'" s ~-n
1626 | Cmulti _ | Csplit _ | Csingle _ -> ()
1627 and obeco s a b =
1628 if always || a <> b
1629 then
1630 match a with
1631 | Some c when c > 1 -> o' "%s='%d'" s c
1632 | _ -> ()
1634 oi "width" c.cwinw dc.cwinw;
1635 oi "height" c.cwinh dc.cwinh;
1636 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
1637 oi "scroll-handle-height" c.scrollh dc.scrollh;
1638 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
1639 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
1640 ob "case-insensitive-search" c.icase dc.icase;
1641 ob "preload" c.preload dc.preload;
1642 oi "page-bias" c.pagebias dc.pagebias;
1643 oi "scroll-step" c.scrollstep dc.scrollstep;
1644 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
1645 ob "max-height-fit" c.maxhfit dc.maxhfit;
1646 ob "crop-hack" c.crophack dc.crophack;
1647 oW "throttle" c.maxwait dc.maxwait;
1648 ob "highlight-links" c.hlinks dc.hlinks;
1649 ob "under-cursor-info" c.underinfo dc.underinfo;
1650 oi "vertical-margin" c.interpagespace dc.interpagespace;
1651 oz "zoom" c.zoom dc.zoom;
1652 ob "presentation" c.presentation dc.presentation;
1653 oi "rotation-angle" c.angle dc.angle;
1654 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
1655 oFm "fit-model" c.fitmodel dc.fitmodel;
1656 oI "pixmap-cache-size" c.memlimit dc.memlimit;
1657 oi "tex-count" c.texcount dc.texcount;
1658 oi "slice-height" c.sliceheight dc.sliceheight;
1659 oi "thumbnail-width" c.thumbw dc.thumbw;
1660 ob "persistent-location" c.jumpback dc.jumpback;
1661 oc "background-color" c.bgcolor dc.bgcolor;
1662 oA "scrollbar-color" c.sbarcolor dc.sbarcolor;
1663 oA "scrollbar-handle-color" c.sbarhndlcolor dc.sbarhndlcolor;
1664 oi "tile-width" c.tilew dc.tilew;
1665 oi "tile-height" c.tileh dc.tileh;
1666 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
1667 ob "checkers" c.checkers dc.checkers;
1668 oi "aalevel" c.aalevel dc.aalevel;
1669 ob "trim-margins" c.trimmargins dc.trimmargins;
1670 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
1671 os "uri-launcher" c.urilauncher dc.urilauncher;
1672 os "path-launcher" c.pathlauncher dc.pathlauncher;
1673 oC "color-space" c.colorspace dc.colorspace;
1674 ob "invert-colors" c.invert dc.invert;
1675 oF "brightness" c.colorscale dc.colorscale;
1676 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
1677 oco "columns" c.columns dc.columns;
1678 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
1679 os "selection-command" c.selcmd dc.selcmd;
1680 os "synctex-command" c.stcmd dc.stcmd;
1681 os "pax-command" c.paxcmd dc.paxcmd;
1682 os "askpass-command" c.passcmd dc.passcmd;
1683 os "savepath-command" c.savecmd dc.savecmd;
1684 ob "update-cursor" c.updatecurs dc.updatecurs;
1685 oi "hint-font-size" c.hfsize dc.hfsize;
1686 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
1687 oF "page-scroll-scale" c.pgscale dc.pgscale;
1688 ob "use-pbo" c.usepbo dc.usepbo;
1689 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
1690 ob "remote-in-a-new-instance" c.riani dc.riani;
1691 op "point-and-x" c.pax dc.pax;
1692 oPm "point-and-x-mark" c.paxmark dc.paxmark;
1693 ob "scroll-bar-on-the-left" c.leftscroll dc.leftscroll;
1694 if not always
1695 then os "title" c.title dc.title;
1696 oL "last-visit" (Int64.of_float time) 0L;
1697 ob "edit-annotations-inline" c.annotinline dc.annotinline;
1698 ob "coarse-presentation-positioning" c.coarseprespos dc.coarseprespos;
1699 ob "use-document-css" c.usedoccss dc.usedoccss;
1702 let keymapsbuf always dc c =
1703 let open Buffer in
1704 let bb = create 16 in
1705 let rec loop = function
1706 | [] -> ()
1707 | (modename, h) :: rest ->
1708 let dh = findkeyhash dc modename in
1709 if always || h <> dh
1710 then (
1711 if Hashtbl.length h > 0
1712 then (
1713 if length bb > 0 then add_char bb '\n';
1714 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
1715 Hashtbl.iter (fun i o ->
1716 if always || match Hashtbl.find dh i
1717 with | dO -> dO <> o | exception Not_found -> false
1718 then
1719 let addkm (k, m) =
1720 if Wsi.withctrl m then add_string bb "ctrl-";
1721 if Wsi.withalt m then add_string bb "alt-";
1722 if Wsi.withshift m then add_string bb "shift-";
1723 if Wsi.withmeta m then add_string bb "meta-";
1724 add_string bb (Wsi.keyname k);
1726 let addkms l =
1727 let rec loop = function
1728 | [] -> ()
1729 | km :: [] -> addkm km
1730 | km :: rest -> addkm km; add_char bb ' '; loop rest
1732 loop l
1734 add_string bb "<map in='";
1735 addkm i;
1736 match o with
1737 | KMinsrt km ->
1738 add_string bb "' out='"; addkm km; add_string bb "'/>\n"
1740 | KMinsrl kms ->
1741 add_string bb "' out='"; addkms kms; add_string bb "'/>\n"
1743 | KMmulti (ins, kms) ->
1744 add_char bb ' '; addkms ins; add_string bb "' out='";
1745 addkms kms; add_string bb "'/>\n"
1746 ) h;
1747 add_string bb "</keymap>";
1750 loop rest
1752 loop c.keyhashes;
1756 let keystostrlist c =
1757 let rec loop accu = function
1758 | [] -> accu
1759 | (modename, h) :: rest ->
1760 let accu =
1761 if Hashtbl.length h > 0
1762 then (
1763 let accu = Printf.sprintf "\xc2\xb7Keys for %s" modename :: accu in
1764 Hashtbl.fold (fun i o a ->
1765 let bb = Buffer.create 10 in
1766 let addkm (k, m) =
1767 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
1768 if Wsi.withalt m then Buffer.add_string bb "alt-";
1769 if Wsi.withshift m then Buffer.add_string bb "shift-";
1770 if Wsi.withmeta m then Buffer.add_string bb "meta-";
1771 Buffer.add_string bb (Wsi.keyname k);
1773 let addkms l =
1774 let rec loop = function
1775 | [] -> ()
1776 | km :: [] -> addkm km
1777 | km :: rest ->
1778 addkm km; Buffer.add_char bb ' ';
1779 loop rest
1781 loop l
1783 addkm i;
1784 Buffer.add_char bb '\t';
1785 begin match o with
1786 | KMinsrt km ->
1787 addkm km
1789 | KMinsrl kms ->
1790 addkms kms
1792 | KMmulti (ins, kms) ->
1793 Buffer.add_char bb ' ';
1794 addkms ins;
1795 Buffer.add_string bb "\t";
1796 addkms kms
1797 end;
1798 Buffer.contents bb :: a
1799 ) h accu
1801 else accu
1803 loop accu rest
1805 loop [] c.keyhashes
1808 let save1 bb leavebirdseye x h dc =
1809 let uifontsize = fstate.fontsize in
1810 let dc = if conf.bedefault then conf else dc in
1811 Buffer.add_string bb "<llppconfig>\n";
1813 if nonemptystr !fontpath
1814 then
1815 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
1816 uifontsize
1817 !fontpath
1818 else (
1819 if uifontsize <> 14
1820 then
1821 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
1824 Buffer.add_string bb "<defaults";
1825 add_attrs bb true dc dc nan;
1826 let kb = keymapsbuf true dc dc in
1827 if Buffer.length kb > 0
1828 then (
1829 Buffer.add_string bb ">\n";
1830 Buffer.add_buffer bb kb;
1831 Buffer.add_string bb "\n</defaults>\n";
1833 else Buffer.add_string bb "/>\n";
1835 let adddoc path pan anchor c bookmarks time origin =
1836 if bookmarks == [] && c = dc && anchor = emptyanchor
1837 then ()
1838 else (
1839 Printf.bprintf bb "<doc path='%s'"
1840 (Parser.enent path 0 (String.length path));
1842 if nonemptystr c.key
1843 then
1844 Printf.bprintf bb "\n key='%s'" c.key;
1846 if nonemptystr origin
1847 then Printf.bprintf bb "\n origin='%s'"
1848 (Parser.enent origin 0 (String.length origin));
1850 if anchor <> emptyanchor
1851 then (
1852 let n, rely, visy = anchor in
1853 Printf.bprintf bb "\n page='%d'" n;
1855 if rely > 1e-6
1856 then Printf.bprintf bb " rely='%f'" rely;
1858 if abs_float visy > 1e-6
1859 then Printf.bprintf bb " visy='%f'" visy;
1862 if pan != 0
1863 then Printf.bprintf bb " pan='%d'" pan;
1865 add_attrs bb false dc c time;
1866 if nonemptystr c.css
1867 then Printf.bprintf bb ">\n <css><![CDATA[%s]]></css>" c.css;
1868 let kb = keymapsbuf false dc c in
1870 begin match bookmarks with
1871 | [] ->
1872 if Buffer.length kb > 0
1873 then (
1874 Buffer.add_string bb ">\n";
1875 Buffer.add_buffer bb kb;
1876 Buffer.add_string bb "\n</doc>\n";
1878 else
1879 if nonemptystr c.css
1880 then Buffer.add_string bb "\n</doc>\n"
1881 else Buffer.add_string bb "/>\n"
1882 | _ ->
1883 Buffer.add_string bb ">\n<bookmarks>\n";
1884 List.iter (fun (title, _, kind) ->
1885 begin match kind with
1886 | Oanchor (page, rely, visy) ->
1887 Printf.bprintf bb
1888 "<item title='%s' page='%d'"
1889 (Parser.enent title 0 (String.length title))
1890 page
1892 if rely > 1e-6
1893 then
1894 Printf.bprintf bb " rely='%f'" rely
1896 if abs_float visy > 1e-6
1897 then
1898 Printf.bprintf bb " visy='%f'" visy
1900 | Ohistory _ | Onone | Ouri _ | Oremote _
1901 | Oremotedest _ | Olaunch _ ->
1902 failwith "unexpected link in bookmarks"
1903 end;
1904 Buffer.add_string bb "/>\n";
1905 ) bookmarks;
1906 Buffer.add_string bb "</bookmarks>";
1907 if Buffer.length kb > 0
1908 then (
1909 Buffer.add_string bb "\n";
1910 Buffer.add_buffer bb kb;
1912 Buffer.add_string bb "\n</doc>\n";
1913 end;
1917 let pan, conf =
1918 match state.mode with
1919 | Birdseye (c, pan, _, _, _) ->
1920 let beyecolumns =
1921 match conf.columns with
1922 | Cmulti ((c, _, _), _) -> Some c
1923 | Csingle _ -> None
1924 | Csplit _ -> None
1925 and columns =
1926 match c.columns with
1927 | Cmulti (c, _) -> Cmulti (c, E.a)
1928 | Csingle _ -> Csingle E.a
1929 | Csplit _ -> failwith "quit from bird's eye while split"
1931 pan, { c with beyecolumns = beyecolumns; columns = columns }
1932 | Textentry _
1933 | View
1934 | LinkNav _ -> x, conf
1936 let docpath = if nonemptystr state.path then abspath state.path else E.s in
1937 if nonemptystr docpath
1938 then (
1939 adddoc docpath pan (getanchor ())
1941 let autoscrollstep =
1942 match state.autoscroll with
1943 | Some step -> step
1944 | None -> conf.autoscrollstep
1946 begin match state.mode with
1947 | Birdseye beye -> leavebirdseye beye true
1948 | Textentry _
1949 | View
1950 | LinkNav _ -> ()
1951 end;
1952 let key =
1954 let t, h = state.statkeyhack in
1955 let t' = try (Unix.stat docpath).st_mtime with _ -> nan in
1956 if t <> t'
1957 then Digest.file docpath |> Digest.to_hex
1958 else h
1959 with _ -> E.s
1961 { conf with autoscrollstep; key }
1963 (if conf.savebmarks then state.bookmarks else [])
1964 (now ())
1965 state.origin
1967 Hashtbl.iter (fun path (c, bookmarks, x, anchor, origin) ->
1968 if docpath <> abspath path
1969 then adddoc path x anchor c bookmarks c.lastvisit origin
1970 ) h;
1971 Buffer.add_string bb "</llppconfig>\n";
1972 true;
1975 let save leavebirdseye =
1976 let relx = float state.x /. float state.winw in
1977 let w, h, x =
1978 let cx w = truncate (relx *. float w) in
1979 List.fold_left
1980 (fun (w, h, x) ws ->
1981 match ws with
1982 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
1983 | Wsi.MaxVert -> (w, conf.cwinh, x)
1984 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
1986 (state.winw, state.winh, state.x) state.winstate
1988 conf.cwinw <- w;
1989 conf.cwinh <- h;
1990 let bb = Buffer.create 32768 in
1991 let save2 (h, dc) =
1992 save1 bb leavebirdseye x h dc
1994 if load1 save2 && Buffer.length bb > 0
1995 then
1997 let tmp = !confpath ^ ".tmp" in
1998 let oc = open_out_bin tmp in
1999 Buffer.output_buffer oc bb;
2000 close_out oc;
2001 Unix.rename tmp !confpath;
2002 with exn ->
2003 dolog "error saving configuration: %s" @@ exntos exn
2006 let gc () =
2007 let href = ref @@ Hashtbl.create 0 in
2008 let cref = ref defconf in
2009 let push (h, dc) =
2010 let f path v =
2011 if Sys.file_exists path
2012 then Some v
2013 else (dolog "removing entry for '%s'" path; None) in
2014 Hashtbl.filter_map_inplace f h;
2015 href := h;
2016 cref := dc;
2017 true
2019 ignore (load1 push);
2020 let bb = Buffer.create 32768 in
2021 let save2 (_h, dc) = save1 bb (fun _ _ -> ()) 0 !href dc in
2022 if load1 save2 && Buffer.length bb > 0
2023 then (
2025 let tmp = !confpath ^ ".tmp" in
2026 let oc = open_out_bin tmp in
2027 Buffer.output_buffer oc bb;
2028 close_out oc;
2029 Unix.rename tmp !confpath;
2030 with exn ->
2031 dolog "error saving configuration: %s" @@ exntos exn
2035 let logcurrently = function
2036 | Idle -> dolog "Idle"
2037 | Loading (l, gen) ->
2038 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2039 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2040 dolog
2041 "Tiling %d[%d,%d] page=%s cs=%s angle=%d"
2042 l.pageno col row (~> pageopaque)
2043 (CSTE.to_string colorspace) angle;
2044 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2045 angle gen conf.angle state.gen
2046 tilew tileh
2047 conf.tilew conf.tileh;
2048 | Outlining _ ->
2049 dolog "outlining"