Whoops
[llpp.git] / config.ml
blob145eb4294c7ec35d36df1e2e03f93fc07900c026
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
290 ; mutable layouth : int
292 and columns =
293 | Csingle of singlecolumn
294 | Cmulti of multicolumns
295 | Csplit of splitcolumns
296 and outlinekind =
297 | Onone
298 | Oanchor of anchor
299 | Ouri of uri
300 | Olaunch of launchcommand
301 | Oremote of (filename * pageno)
302 | Oremotedest of (filename * destname)
303 | Ohistory of (filename * conf * outline list * x * anchor * filename)
304 and outline = (caption * outlinelevel * outlinekind)
305 and outlinelevel = int
306 and rgb = (float * float * float)
307 and rgba = (float * float * float * float)
310 type page =
311 { pageno : int
312 ; pagedimno : int
313 ; pagew : int
314 ; pageh : int
315 ; pagex : int
316 ; pagey : int
317 ; pagevw : int
318 ; pagevh : int
319 ; pagedispx : int
320 ; pagedispy : int
321 ; pagecol : int
325 type tile = opaque * pixmapsize * elapsed
326 and elapsed = float;;
327 type pagemapkey = pageno * gen;;
328 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
329 and row = int
330 and col = int
331 and currently =
332 | Idle
333 | Loading of (page * gen)
334 | Tiling of (
335 page * opaque * colorspace * angle * gen * col * row * width * height
337 | Outlining of outline list
340 type mpos = int * int
341 and mstate =
342 | Msel of (mpos * mpos)
343 | Mpan of mpos
344 | Mscrolly | Mscrollx
345 | Mzoom of (buttonno * step * mpos)
346 | Mzoomrect of (mpos * mpos)
347 | Mnone
348 and buttonno = int
349 and step = int
352 type mode =
353 | Birdseye of (conf * leftx * pageno * pageno * anchor)
354 | Textentry of (textentry * onleave)
355 | View
356 | LinkNav of linktarget
357 and onleave = leavetextentrystatus -> unit
358 and leavetextentrystatus = | Cancel | Confirm
359 and helpitem = string * int * action
360 and action =
361 | Noaction
362 | Action of (uioh -> uioh)
363 and linktarget =
364 | Ltexact of (pageno * direction)
365 | Ltgendir of direction
366 | Ltnotready of (pageno * direction)
367 and direction = int (* -1, 0, 1 *)
368 and textentry = string * string * onhist option
369 * onkey * ondone * cancelonempty
370 and onkey = string -> Keys.t -> te
371 and ondone = string -> unit
372 and histcancel = unit -> unit
373 and onhist = ((histcmd -> string) * histcancel)
374 and histcmd = HCnext | HCprev | HCfirst | HClast
375 and cancelonempty = bool
376 and te =
377 | TEstop
378 | TEdone of string
379 | TEcont of string
380 | TEswitch of textentry
383 type 'a circbuf =
384 { store : 'a array
385 ; mutable rc : int
386 ; mutable wc : int
387 ; mutable len : int
391 type state =
392 { mutable ss : Unix.file_descr
393 ; mutable wsfd : Unix.file_descr
394 ; mutable stderr : Unix.file_descr
395 ; mutable errmsgs : Buffer.t
396 ; mutable newerrmsgs : bool
397 ; mutable w : int
398 ; mutable x : x
399 ; mutable y : y
400 ; mutable anchor : anchor
401 ; mutable ranchors : (string * string * anchor * string) list
402 ; mutable maxy : int
403 ; mutable layout : page list
404 ; pagemap : (pagemapkey, opaque) Hashtbl.t
405 ; tilemap : (tilemapkey, tile) Hashtbl.t
406 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
407 ; mutable pdims : (pageno * width * height * leftx) list
408 ; mutable pagecount : int
409 ; mutable currently : currently
410 ; mutable mstate : mstate
411 ; mutable searchpattern : string
412 ; mutable rects : (pageno * rectcolor * rect) list
413 ; mutable rects1 : (pageno * rectcolor * rect) list
414 ; prects : (pageno, float array) Hashtbl.t
415 ; mutable text : string
416 ; mutable winstate : Wsi.winstate list
417 ; mutable mode : mode
418 ; mutable uioh : uioh
419 ; mutable outlines : outline array
420 ; mutable bookmarks : outline list
421 ; mutable path : string
422 ; mutable password : string
423 ; mutable nameddest : string
424 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
425 ; mutable memused : memsize
426 ; mutable gen : gen
427 ; mutable throttle : (page list * int * float) option
428 ; mutable autoscroll : int option
429 ; mutable ghyll : (int option -> unit)
430 ; mutable help : helpitem array
431 ; mutable docinfo : (int * string) list
432 ; mutable checkerstexid : GlTex.texture_id option
433 ; hists : hists
434 ; mutable prevzoom : (float * int)
435 ; mutable progress : float
436 ; mutable redisplay : bool
437 ; mutable mpos : mpos
438 ; mutable keystate : keystate
439 ; mutable glinks : bool
440 ; mutable prevcolumns : (columns * float) option
441 ; mutable winw : int
442 ; mutable winh : int
443 ; mutable reprf : (unit -> unit)
444 ; mutable origin : string
445 ; mutable roam : (unit -> unit)
446 ; mutable bzoom : bool
447 ; mutable traw : [`float] Raw.t
448 ; mutable vraw : [`float] Raw.t
449 ; mutable lnava : (pageno * linkno) option
450 ; mutable slideshow : int
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 ; layouth = -1
575 ; keyhashes =
576 let mk n = (n, Hashtbl.create 1) in
577 [ mk "global"
578 ; mk "info"
579 ; mk "help"
580 ; mk "outline"
581 ; mk "listview"
582 ; mk "birdseye"
583 ; mk "textentry"
584 ; mk "links"
585 ; mk "view"
590 let conf = { defconf with angle = defconf.angle };;
592 let gotourl url =
593 let command = Str.global_replace percentsre url conf.urilauncher in
594 try ignore @@ spawn command []
595 with exn -> dolog "failed to execute `%s': %s" command @@ exntos exn
598 let gotouri uri =
599 if emptystr conf.urilauncher
600 then dolog "%s" uri
601 else
602 match geturl uri with
603 | "" -> dolog "obtained empty url from uri %S" uri
604 | url -> gotourl url
607 let makehelp () =
608 let strings =
609 version ()
610 :: "(searching in this text works just by typing (i.e. no initial '/'))"
611 :: E.s :: Help.keys
613 List.map (fun s ->
614 match geturl s with
615 | "" -> (s, 0, Noaction)
616 | url -> (s, 0, Action (fun uioh -> gotourl url; uioh))
617 ) strings;
620 let cbnew n v =
621 { store = Array.make n v
622 ; rc = 0
623 ; wc = 0
624 ; len = 0
628 let cbcap b = Array.length b.store;;
630 let cbput ?(update_rc=true) b v =
631 let cap = cbcap b in
632 b.store.(b.wc) <- v;
633 b.wc <- (b.wc + 1) mod cap;
634 if update_rc
635 then b.rc <- b.wc;
636 b.len <- min (b.len + 1) cap;
639 let cbput_dont_update_rc b v = cbput ~update_rc:false b v;;
641 let cbempty b = b.len = 0;;
643 let cbgetg b circular dir =
644 if cbempty b
645 then b.store.(0)
646 else
647 let rc = b.rc + dir in
648 let rc =
649 if circular
650 then (
651 if rc = -1
652 then b.len-1
653 else (
654 if rc >= b.len
655 then 0
656 else rc
659 else bound rc 0 (b.len-1)
661 b.rc <- rc;
662 b.store.(rc);
665 let cbget b = cbgetg b false;;
666 let cbgetc b = cbgetg b true;;
668 let state =
669 { ss = Unix.stdin
670 ; wsfd = Unix.stdin
671 ; stderr = Unix.stderr
672 ; errmsgs = Buffer.create 0
673 ; newerrmsgs = false
674 ; x = 0
675 ; y = 0
676 ; w = 0
677 ; anchor = emptyanchor
678 ; ranchors = []
679 ; layout = []
680 ; maxy = max_int
681 ; tilelru = Queue.create ()
682 ; pagemap = Hashtbl.create 10
683 ; tilemap = Hashtbl.create 10
684 ; pdims = []
685 ; pagecount = 0
686 ; currently = Idle
687 ; mstate = Mnone
688 ; rects = []
689 ; rects1 = []
690 ; prects = Hashtbl.create 1
691 ; text = E.s
692 ; mode = View
693 ; winstate = []
694 ; searchpattern = E.s
695 ; outlines = E.a
696 ; bookmarks = []
697 ; path = E.s
698 ; password = E.s
699 ; nameddest = E.s
700 ; geomcmds = E.s, []
701 ; hists =
702 { nav = cbnew 10 emptyanchor
703 ; pat = cbnew 10 E.s
704 ; pag = cbnew 10 E.s
705 ; sel = cbnew 10 E.s
707 ; memused = 0
708 ; gen = 0
709 ; throttle = None
710 ; autoscroll = None
711 ; ghyll = noghyll
712 ; help = E.a
713 ; docinfo = []
714 ; checkerstexid = None
715 ; prevzoom = (1.0, 0)
716 ; progress = -1.0
717 ; uioh = nouioh
718 ; redisplay = true
719 ; mpos = (-1, -1)
720 ; keystate = KSnone
721 ; glinks = false
722 ; prevcolumns = None
723 ; winw = -1
724 ; winh = -1
725 ; reprf = noreprf
726 ; origin = E.s
727 ; roam = noroam
728 ; bzoom = false
729 ; traw = Raw.create_static `float ~len:8
730 ; vraw = Raw.create_static `float ~len:8
731 ; lnava = None
732 ; slideshow = 0
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 | "layout-height" -> { c with layouth = int_of_string v }
1115 | _ -> c
1116 with exn ->
1117 dolog "error processing attribute (`%S' = `%S'): %s" k v @@ exntos exn;
1120 let rec fold c = function
1121 | [] -> c
1122 | (k, v) :: rest ->
1123 let c = apply c k v in
1124 fold c rest
1126 fold { c with keyhashes = copykeyhashes c } attrs;
1129 let fromstring f pos n v d =
1130 try f v
1131 with exn ->
1132 dolog "error processing attribute (%S=%S) at %d\n%s" n v pos @@ exntos exn;
1136 let bookmark_of attrs =
1137 let rec fold title page rely visy = function
1138 | ("title", v) :: rest -> fold v page rely visy rest
1139 | ("page", v) :: rest -> fold title v rely visy rest
1140 | ("rely", v) :: rest -> fold title page v visy rest
1141 | ("visy", v) :: rest -> fold title page rely v rest
1142 | _ :: rest -> fold title page rely visy rest
1143 | [] -> title, page, rely, visy
1145 fold "invalid" "0" "0" "0" attrs
1148 let doc_of attrs =
1149 let rec fold path key page rely pan visy origin = function
1150 | ("path", v) :: rest -> fold v key page rely pan visy origin rest
1151 | ("key", v) :: rest -> fold path v page rely pan visy origin rest
1152 | ("page", v) :: rest -> fold path key v rely pan visy origin rest
1153 | ("rely", v) :: rest -> fold path key page v pan visy origin rest
1154 | ("pan", v) :: rest -> fold path key page rely v visy origin rest
1155 | ("visy", v) :: rest -> fold path key page rely pan v origin rest
1156 | ("origin", v) :: rest -> fold path key page rely pan visy v rest
1157 | _ :: rest -> fold path key page rely pan visy origin rest
1158 | [] -> path, key, page, rely, pan, visy, origin
1160 fold E.s E.s "0" "0" "0" "0" E.s attrs
1163 let map_of attrs =
1164 let rec fold rs ls = function
1165 | ("out", v) :: rest -> fold v ls rest
1166 | ("in", v) :: rest -> fold rs v rest
1167 | _ :: rest -> fold ls rs rest
1168 | [] -> ls, rs
1170 fold E.s E.s attrs
1173 let setconf dst src =
1174 dst.scrollbw <- src.scrollbw;
1175 dst.scrollh <- src.scrollh;
1176 dst.icase <- src.icase;
1177 dst.preload <- src.preload;
1178 dst.pagebias <- src.pagebias;
1179 dst.verbose <- src.verbose;
1180 dst.scrollstep <- src.scrollstep;
1181 dst.maxhfit <- src.maxhfit;
1182 dst.crophack <- src.crophack;
1183 dst.autoscrollstep <- src.autoscrollstep;
1184 dst.maxwait <- src.maxwait;
1185 dst.hlinks <- src.hlinks;
1186 dst.underinfo <- src.underinfo;
1187 dst.interpagespace <- src.interpagespace;
1188 dst.zoom <- src.zoom;
1189 dst.presentation <- src.presentation;
1190 dst.angle <- src.angle;
1191 dst.cwinw <- src.cwinw;
1192 dst.cwinh <- src.cwinh;
1193 dst.savebmarks <- src.savebmarks;
1194 dst.memlimit <- src.memlimit;
1195 dst.fitmodel <- src.fitmodel;
1196 dst.texcount <- src.texcount;
1197 dst.sliceheight <- src.sliceheight;
1198 dst.thumbw <- src.thumbw;
1199 dst.jumpback <- src.jumpback;
1200 dst.bgcolor <- src.bgcolor;
1201 dst.tilew <- src.tilew;
1202 dst.tileh <- src.tileh;
1203 dst.mustoresize <- src.mustoresize;
1204 dst.checkers <- src.checkers;
1205 dst.aalevel <- src.aalevel;
1206 dst.trimmargins <- src.trimmargins;
1207 dst.trimfuzz <- src.trimfuzz;
1208 dst.urilauncher <- src.urilauncher;
1209 dst.colorspace <- src.colorspace;
1210 dst.invert <- src.invert;
1211 dst.colorscale <- src.colorscale;
1212 dst.ghyllscroll <- src.ghyllscroll;
1213 dst.columns <- src.columns;
1214 dst.beyecolumns <- src.beyecolumns;
1215 dst.selcmd <- src.selcmd;
1216 dst.updatecurs <- src.updatecurs;
1217 dst.pathlauncher <- src.pathlauncher;
1218 dst.keyhashes <- copykeyhashes src;
1219 dst.hfsize <- src.hfsize;
1220 dst.hscrollstep <- src.hscrollstep;
1221 dst.pgscale <- src.pgscale;
1222 dst.usepbo <- src.usepbo;
1223 dst.wheelbypage <- src.wheelbypage;
1224 dst.stcmd <- src.stcmd;
1225 dst.paxcmd <- src.paxcmd;
1226 dst.passcmd <- src.passcmd;
1227 dst.savecmd <- src.savecmd;
1228 dst.scrollb <- src.scrollb;
1229 dst.riani <- src.riani;
1230 dst.paxmark <- src.paxmark;
1231 dst.leftscroll <- src.leftscroll;
1232 dst.title <- src.title;
1233 dst.annotinline <- src.annotinline;
1234 dst.coarseprespos <- src.coarseprespos;
1235 dst.css <- src.css;
1236 dst.usedoccss <- src.usedoccss;
1237 dst.sbarcolor <- src.sbarcolor;
1238 dst.sbarhndlcolor <- src.sbarhndlcolor;
1239 dst.key <- src.key;
1240 dst.layouth <- src.layouth;
1241 dst.pax <-
1242 if src.pax = None
1243 then None
1244 else Some 0.0;
1247 let findkeyhash c name =
1248 try List.assoc name c.keyhashes
1249 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
1252 let get s =
1253 let open Parser in
1254 let h = Hashtbl.create 10 in
1255 let dc = { defconf with angle = defconf.angle } in
1256 let rec toplevel v t spos _ =
1257 match t with
1258 | Vdata | Vcdata | Vend -> v
1259 | Vopen ("llppconfig", _, closed) ->
1260 if closed
1261 then v
1262 else { v with f = llppconfig }
1263 | Vopen _ -> parse_error "unexpected subelement at top level" s spos
1264 | Vclose _ -> parse_error "unexpected close at top level" s spos
1266 and llppconfig v t spos _ =
1267 match t with
1268 | Vdata | Vcdata -> v
1269 | Vend -> parse_error "unexpected end of input in llppconfig" s spos
1270 | Vopen ("defaults", attrs, closed) ->
1271 let c = config_of dc attrs in
1272 setconf dc c;
1273 if closed
1274 then v
1275 else { v with f = defaults }
1277 | Vopen ("ui-font", attrs, closed) ->
1278 let rec getsize size = function
1279 | [] -> size
1280 | ("size", v) :: rest ->
1281 let size =
1282 fromstring int_of_string spos "size" v fstate.fontsize in
1283 getsize size rest
1284 | l -> getsize size l
1286 fstate.fontsize <- getsize fstate.fontsize attrs;
1287 if closed
1288 then v
1289 else { v with f = uifont (Buffer.create 10) }
1291 | Vopen ("doc", attrs, closed) ->
1292 let pathent, key, spage, srely, span, svisy, origin = doc_of attrs in
1293 let path = unentS pathent
1294 and origin = unentS origin
1295 and pageno = fromstring int_of_string spos "page" spage 0
1296 and rely = fromstring float_of_string spos "rely" srely 0.0
1297 and pan = fromstring int_of_string spos "pan" span 0
1298 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1299 let c = config_of dc attrs in
1300 c.key <- key;
1301 let anchor = (pageno, rely, visy) in
1302 if closed
1303 then (Hashtbl.add h path (c, [], pan, anchor, origin); v)
1304 else { v with f = doc path origin pan anchor c [] }
1306 | Vopen _ ->
1307 parse_error "unexpected subelement in llppconfig" s spos
1309 | Vclose "llppconfig" -> { v with f = toplevel }
1310 | Vclose _ -> parse_error "unexpected close in llppconfig" s spos
1312 and defaults v t spos _ =
1313 match t with
1314 | Vdata | Vcdata -> v
1315 | Vend -> parse_error "unexpected end of input in defaults" s spos
1316 | Vopen ("keymap", attrs, closed) ->
1317 let modename =
1318 try List.assoc "mode" attrs
1319 with Not_found -> "global" in
1320 if closed
1321 then v
1322 else
1323 let ret keymap =
1324 let h = findkeyhash dc modename in
1325 KeyMap.iter (Hashtbl.replace h) keymap;
1326 defaults
1328 { v with f = pkeymap ret KeyMap.empty }
1330 | Vopen (_, _, _) ->
1331 parse_error "unexpected subelement in defaults" s spos
1333 | Vclose "defaults" ->
1334 { v with f = llppconfig }
1336 | Vclose _ -> parse_error "unexpected close in defaults" s spos
1338 and uifont b v t spos epos =
1339 match t with
1340 | Vdata | Vcdata ->
1341 Buffer.add_substring b s spos (epos - spos);
1343 | Vopen (_, _, _) ->
1344 parse_error "unexpected subelement in ui-font" s spos
1345 | Vclose "ui-font" ->
1346 if emptystr !fontpath
1347 then fontpath := Buffer.contents b;
1348 { v with f = llppconfig }
1349 | Vclose _ -> parse_error "unexpected close in ui-font" s spos
1350 | Vend -> parse_error "unexpected end of input in ui-font" s spos
1352 and doc path origin pan anchor c bookmarks v t spos _ =
1353 match t with
1354 | Vdata | Vcdata -> v
1355 | Vend -> parse_error "unexpected end of input in doc" s spos
1356 | Vopen ("bookmarks", _, closed) ->
1357 if closed
1358 then v
1359 else { v with f = pbookmarks path origin pan anchor c bookmarks }
1361 | Vopen ("keymap", attrs, closed) ->
1362 let modename =
1363 try List.assoc "mode" attrs
1364 with Not_found -> "global"
1366 if closed
1367 then v
1368 else
1369 let ret keymap =
1370 let h = findkeyhash c modename in
1371 KeyMap.iter (Hashtbl.replace h) keymap;
1372 doc path origin pan anchor c bookmarks
1374 { v with f = pkeymap ret KeyMap.empty }
1376 | Vopen ("css", [], false) ->
1377 { v with f = pcss path origin pan anchor c bookmarks }
1379 | Vopen (_, _, _) ->
1380 parse_error "unexpected subelement in doc" s spos
1382 | Vclose "doc" ->
1383 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor, origin);
1384 { v with f = llppconfig }
1386 | Vclose _ -> parse_error "unexpected close in doc" s spos
1388 and pcss path origin pan anchor c bookmarks v t spos epos =
1389 match t with
1390 | Vdata | Vcdata ->
1391 let b = Buffer.create 10 in
1392 Buffer.add_substring b s spos (epos - spos);
1393 { v with f = pcss path origin pan anchor
1394 { c with css = Buffer.contents b }
1395 bookmarks }
1396 | Vend -> parse_error "unexpected end of input in css" s spos
1397 | Vopen _ -> parse_error "unexpected subelement in css" s spos
1398 | Vclose "css" -> { v with f = doc path origin pan anchor c bookmarks }
1399 | Vclose _ -> parse_error "unexpected close in css" s spos
1401 and pkeymap ret keymap v t spos _ =
1402 match t with
1403 | Vdata | Vcdata -> v
1404 | Vend -> parse_error "unexpected end of input in keymap" s spos
1405 | Vopen ("map", attrs, closed) ->
1406 let r, l = map_of attrs in
1407 let kss = fromstring keys_of_string spos "in" r [] in
1408 let lss = fromstring keys_of_string spos "out" l [] in
1409 let keymap =
1410 match kss with
1411 | [] -> keymap
1412 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
1413 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
1415 if closed
1416 then { v with f = pkeymap ret keymap }
1417 else
1418 let f () = v in
1419 { v with f = skip "map" f }
1421 | Vopen _ ->
1422 parse_error "unexpected subelement in keymap" s spos
1424 | Vclose "keymap" ->
1425 { v with f = ret keymap }
1427 | Vclose _ -> parse_error "unexpected close in keymap" s spos
1429 and pbookmarks path origin pan anchor c bookmarks v t spos _ =
1430 match t with
1431 | Vdata | Vcdata -> v
1432 | Vend -> parse_error "unexpected end of input in bookmarks" s spos
1433 | Vopen ("item", attrs, closed) ->
1434 let titleent, spage, srely, svisy = bookmark_of attrs in
1435 let page = fromstring int_of_string spos "page" spage 0
1436 and rely = fromstring float_of_string spos "rely" srely 0.0
1437 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1438 let bookmarks =
1439 (unentS titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
1441 if closed
1442 then { v with f = pbookmarks path origin pan anchor c bookmarks }
1443 else
1444 let f () = v in
1445 { v with f = skip "item" f }
1447 | Vopen _ ->
1448 parse_error "unexpected subelement in bookmarks" s spos
1450 | Vclose "bookmarks" ->
1451 { v with f = doc path origin pan anchor c bookmarks }
1453 | Vclose _ -> parse_error "unexpected close in bookmarks" s spos
1455 and skip tag f v t spos _ =
1456 match t with
1457 | Vdata | Vcdata -> v
1458 | Vend ->
1459 parse_error ("unexpected end of input in skipped " ^ tag) s spos
1460 | Vopen (tag', _, closed) ->
1461 if closed
1462 then v
1463 else
1464 let f' () = { v with f = skip tag f } in
1465 { v with f = skip tag' f' }
1466 | Vclose ctag ->
1467 if tag = ctag
1468 then f ()
1469 else parse_error ("unexpected close in skipped " ^ tag) s spos
1472 parse { f = toplevel; accu = () } s;
1473 h, dc;
1476 let do_load f contents =
1477 try f contents
1478 with
1479 | Parser.Parse_error (msg, s, pos) ->
1480 let subs = Parser.subs s pos in
1481 Utils.error "parse error: %s: at %d [..%S..]" msg pos subs
1483 | exn -> Utils.error "parse error: %s" @@ exntos exn
1486 let defconfpath =
1487 let dir =
1488 let xdgconfdir = Utils.getenvwithdef "XDG_CONFIG_HOME" E.s in
1489 if emptystr xdgconfdir
1490 then
1492 let dir = Filename.concat home ".config" in
1493 if Sys.is_directory dir then dir else home
1494 with _ -> home
1495 else xdgconfdir
1497 Filename.concat dir "llpp.conf"
1500 let confpath = ref defconfpath;;
1502 let load2 f default =
1503 match filecontents !confpath with
1504 | contents -> f @@ do_load get contents
1505 | exception Unix.Unix_error (Unix.ENOENT, "open", _) ->
1506 f (Hashtbl.create 0, defconf)
1507 | exception exn ->
1508 dolog "error loading configuration from `%S': %s" !confpath @@ exntos exn;
1509 default
1512 let load1 f = load2 f false;;
1514 let load openlast =
1515 let f (h, dc) =
1516 if openlast
1517 then (
1518 let path, _ =
1519 Hashtbl.fold
1520 (fun path (conf, _, _, _, _) ((_, besttime) as best) ->
1521 if conf.lastvisit > besttime
1522 then (path, conf.lastvisit)
1523 else best)
1525 (state.path, -.infinity)
1527 state.path <- path;
1529 let pc, pb, px, pa, po =
1530 let def = dc, [], 0, emptyanchor, state.origin in
1531 if emptystr state.path
1532 then def
1533 else
1534 let absname = abspath state.path in
1535 match Hashtbl.find h absname with
1536 | (c,b,x,a,o) -> (c,b,x,a,state.origin)
1537 | exception Not_found ->
1538 let exception E of (conf * outline list * int * anchor * string) in
1539 let key = try Digest.file absname |> Digest.to_hex with _ -> E.s in
1540 match (
1541 if emptystr key
1542 then ()
1543 else
1544 Hashtbl.iter (fun p ((c, _, _, _, _) as v) ->
1545 if c.key = key
1546 then (
1547 dolog "will use %s's settings due to matching keys" p;
1548 raise (E v)
1552 with
1553 | _ -> def
1554 | exception E v -> v
1556 setconf defconf dc;
1557 setconf conf pc;
1558 state.bookmarks <- pb;
1559 state.x <- px;
1560 state.origin <- po;
1561 if conf.jumpback
1562 then state.anchor <- pa;
1563 cbput state.hists.nav pa;
1564 true
1566 load1 f
1569 let gethist () =
1570 let f (h, _) =
1571 Hashtbl.fold (fun path (pc, pb, px, pa, po) accu ->
1572 (path, pc, pb, px, pa, po) :: accu)
1573 h [];
1575 load2 f []
1578 let add_attrs bb always dc c time =
1579 let o' fmt s =
1580 Buffer.add_string bb "\n ";
1581 Printf.bprintf bb fmt s
1583 let o c fmt s = if c then o' fmt s else ignore in
1584 let ob s a b = o (always || a != b) "%s='%b'" s a
1585 and op s a b = o (always || a <> b) "%s='%b'" s (a != None)
1586 and oi s a b = o (always || a != b) "%s='%d'" s a
1587 and oI s a b = o (always || a != b) "%s='%s'" s (string_with_suffix_of_int a)
1588 and oz s a b = o (always || a <> b) "%s='%g'" s (a*.100.)
1589 and oF s a b = o (always || a <> b) "%s='%f'" s a
1590 and oL s a b = o (always || a <> b) "%s='%Ld'" s a
1591 and oc s a b = o (always || a <> b) "%s='%s'" s (color_to_string a)
1592 and oA s a b = o (always || a <> b) "%s='%s'" s (rgba_to_string a)
1593 and oC s a b = o (always || a <> b) "%s='%s'" s (CSTE.to_string a)
1594 and oR s a b = o (always || a <> b) "%s='%s'" s (irect_to_string a)
1595 and oFm s a b = o (always || a <> b) "%s='%s'" s (FMTE.to_string a)
1596 and oSv s a b m =
1597 o (always || a land m <> b land m) "%s='%b'" s (a land m != 0)
1598 and oPm s a b = o (always || a <> b) "%s='%s'" s (MTE.to_string a)
1599 and os s a b =
1600 o (always || a <> b) "%s='%s'" s @@ Parser.enent a 0 (String.length a)
1601 and og s a b =
1602 if always || a <> b
1603 then
1604 match a with
1605 | Some (_N, _A, _B) -> o' "%s='%u,%u,%u'" s _N _A _B
1606 | None ->
1607 match b with
1608 | None -> ()
1609 | _ -> o' "%s='none'" s
1610 and oW s a b =
1611 if always || a <> b
1612 then
1613 let v =
1614 match a with
1615 | None -> "false"
1616 | Some f ->
1617 if f = infinity
1618 then "true"
1619 else string_of_float f
1621 o' "%s='%s'" s v
1622 and oco s a b =
1623 if always || a <> b
1624 then
1625 match a with
1626 | Cmulti ((n, a, b), _) when n > 1 -> o' "%s='%d,%d,%d'" s n a b
1627 | Csplit (n, _) when n > 1 -> o' "%s='%d'" s ~-n
1628 | Cmulti _ | Csplit _ | Csingle _ -> ()
1629 and obeco s a b =
1630 if always || a <> b
1631 then
1632 match a with
1633 | Some c when c > 1 -> o' "%s='%d'" s c
1634 | _ -> ()
1636 oi "width" c.cwinw dc.cwinw;
1637 oi "height" c.cwinh dc.cwinh;
1638 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
1639 oi "scroll-handle-height" c.scrollh dc.scrollh;
1640 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
1641 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
1642 ob "case-insensitive-search" c.icase dc.icase;
1643 ob "preload" c.preload dc.preload;
1644 oi "page-bias" c.pagebias dc.pagebias;
1645 oi "scroll-step" c.scrollstep dc.scrollstep;
1646 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
1647 ob "max-height-fit" c.maxhfit dc.maxhfit;
1648 ob "crop-hack" c.crophack dc.crophack;
1649 oW "throttle" c.maxwait dc.maxwait;
1650 ob "highlight-links" c.hlinks dc.hlinks;
1651 ob "under-cursor-info" c.underinfo dc.underinfo;
1652 oi "vertical-margin" c.interpagespace dc.interpagespace;
1653 oz "zoom" c.zoom dc.zoom;
1654 ob "presentation" c.presentation dc.presentation;
1655 oi "rotation-angle" c.angle dc.angle;
1656 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
1657 oFm "fit-model" c.fitmodel dc.fitmodel;
1658 oI "pixmap-cache-size" c.memlimit dc.memlimit;
1659 oi "tex-count" c.texcount dc.texcount;
1660 oi "slice-height" c.sliceheight dc.sliceheight;
1661 oi "thumbnail-width" c.thumbw dc.thumbw;
1662 ob "persistent-location" c.jumpback dc.jumpback;
1663 oc "background-color" c.bgcolor dc.bgcolor;
1664 oA "scrollbar-color" c.sbarcolor dc.sbarcolor;
1665 oA "scrollbar-handle-color" c.sbarhndlcolor dc.sbarhndlcolor;
1666 oi "tile-width" c.tilew dc.tilew;
1667 oi "tile-height" c.tileh dc.tileh;
1668 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
1669 ob "checkers" c.checkers dc.checkers;
1670 oi "aalevel" c.aalevel dc.aalevel;
1671 ob "trim-margins" c.trimmargins dc.trimmargins;
1672 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
1673 os "uri-launcher" c.urilauncher dc.urilauncher;
1674 os "path-launcher" c.pathlauncher dc.pathlauncher;
1675 oC "color-space" c.colorspace dc.colorspace;
1676 ob "invert-colors" c.invert dc.invert;
1677 oF "brightness" c.colorscale dc.colorscale;
1678 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
1679 oco "columns" c.columns dc.columns;
1680 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
1681 os "selection-command" c.selcmd dc.selcmd;
1682 os "synctex-command" c.stcmd dc.stcmd;
1683 os "pax-command" c.paxcmd dc.paxcmd;
1684 os "askpass-command" c.passcmd dc.passcmd;
1685 os "savepath-command" c.savecmd dc.savecmd;
1686 ob "update-cursor" c.updatecurs dc.updatecurs;
1687 oi "hint-font-size" c.hfsize dc.hfsize;
1688 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
1689 oF "page-scroll-scale" c.pgscale dc.pgscale;
1690 ob "use-pbo" c.usepbo dc.usepbo;
1691 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
1692 ob "remote-in-a-new-instance" c.riani dc.riani;
1693 op "point-and-x" c.pax dc.pax;
1694 oPm "point-and-x-mark" c.paxmark dc.paxmark;
1695 ob "scroll-bar-on-the-left" c.leftscroll dc.leftscroll;
1696 if not always
1697 then os "title" c.title dc.title;
1698 oL "last-visit" (Int64.of_float time) 0L;
1699 ob "edit-annotations-inline" c.annotinline dc.annotinline;
1700 ob "coarse-presentation-positioning" c.coarseprespos dc.coarseprespos;
1701 ob "use-document-css" c.usedoccss dc.usedoccss;
1702 oi "layout-height" c.layouth dc.layouth;
1705 let keymapsbuf always dc c =
1706 let open Buffer in
1707 let bb = create 16 in
1708 let rec loop = function
1709 | [] -> ()
1710 | (modename, h) :: rest ->
1711 let dh = findkeyhash dc modename in
1712 if always || h <> dh
1713 then (
1714 if Hashtbl.length h > 0
1715 then (
1716 if length bb > 0 then add_char bb '\n';
1717 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
1718 Hashtbl.iter (fun i o ->
1719 if always || match Hashtbl.find dh i
1720 with | dO -> dO <> o | exception Not_found -> false
1721 then
1722 let addkm (k, m) =
1723 if Wsi.withctrl m then add_string bb "ctrl-";
1724 if Wsi.withalt m then add_string bb "alt-";
1725 if Wsi.withshift m then add_string bb "shift-";
1726 if Wsi.withmeta m then add_string bb "meta-";
1727 add_string bb (Wsi.keyname k);
1729 let addkms l =
1730 let rec loop = function
1731 | [] -> ()
1732 | km :: [] -> addkm km
1733 | km :: rest -> addkm km; add_char bb ' '; loop rest
1735 loop l
1737 add_string bb "<map in='";
1738 addkm i;
1739 match o with
1740 | KMinsrt km ->
1741 add_string bb "' out='"; addkm km; add_string bb "'/>\n"
1743 | KMinsrl kms ->
1744 add_string bb "' out='"; addkms kms; add_string bb "'/>\n"
1746 | KMmulti (ins, kms) ->
1747 add_char bb ' '; addkms ins; add_string bb "' out='";
1748 addkms kms; add_string bb "'/>\n"
1749 ) h;
1750 add_string bb "</keymap>";
1753 loop rest
1755 loop c.keyhashes;
1759 let keystostrlist c =
1760 let rec loop accu = function
1761 | [] -> accu
1762 | (modename, h) :: rest ->
1763 let accu =
1764 if Hashtbl.length h > 0
1765 then (
1766 let accu = Printf.sprintf "\xc2\xb7Keys for %s" modename :: accu in
1767 Hashtbl.fold (fun i o a ->
1768 let bb = Buffer.create 10 in
1769 let addkm (k, m) =
1770 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
1771 if Wsi.withalt m then Buffer.add_string bb "alt-";
1772 if Wsi.withshift m then Buffer.add_string bb "shift-";
1773 if Wsi.withmeta m then Buffer.add_string bb "meta-";
1774 Buffer.add_string bb (Wsi.keyname k);
1776 let addkms l =
1777 let rec loop = function
1778 | [] -> ()
1779 | km :: [] -> addkm km
1780 | km :: rest ->
1781 addkm km; Buffer.add_char bb ' ';
1782 loop rest
1784 loop l
1786 addkm i;
1787 Buffer.add_char bb '\t';
1788 begin match o with
1789 | KMinsrt km ->
1790 addkm km
1792 | KMinsrl kms ->
1793 addkms kms
1795 | KMmulti (ins, kms) ->
1796 Buffer.add_char bb ' ';
1797 addkms ins;
1798 Buffer.add_string bb "\t";
1799 addkms kms
1800 end;
1801 Buffer.contents bb :: a
1802 ) h accu
1804 else accu
1806 loop accu rest
1808 loop [] c.keyhashes
1811 let save1 bb leavebirdseye x h dc =
1812 let uifontsize = fstate.fontsize in
1813 let dc = if conf.bedefault then conf else dc in
1814 Buffer.add_string bb "<llppconfig>\n";
1816 if nonemptystr !fontpath
1817 then
1818 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
1819 uifontsize
1820 !fontpath
1821 else (
1822 if uifontsize <> 14
1823 then
1824 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
1827 Buffer.add_string bb "<defaults";
1828 add_attrs bb true dc dc nan;
1829 let kb = keymapsbuf true dc dc in
1830 if Buffer.length kb > 0
1831 then (
1832 Buffer.add_string bb ">\n";
1833 Buffer.add_buffer bb kb;
1834 Buffer.add_string bb "\n</defaults>\n";
1836 else Buffer.add_string bb "/>\n";
1838 let adddoc path pan anchor c bookmarks time origin =
1839 if bookmarks == [] && c = dc && anchor = emptyanchor
1840 then ()
1841 else (
1842 Printf.bprintf bb "<doc path='%s'"
1843 (Parser.enent path 0 (String.length path));
1845 if nonemptystr c.key
1846 then
1847 Printf.bprintf bb "\n key='%s'" c.key;
1849 if nonemptystr origin
1850 then Printf.bprintf bb "\n origin='%s'"
1851 (Parser.enent origin 0 (String.length origin));
1853 if anchor <> emptyanchor
1854 then (
1855 let n, rely, visy = anchor in
1856 Printf.bprintf bb "\n page='%d'" n;
1858 if rely > 1e-6
1859 then Printf.bprintf bb " rely='%f'" rely;
1861 if abs_float visy > 1e-6
1862 then Printf.bprintf bb " visy='%f'" visy;
1865 if pan != 0
1866 then Printf.bprintf bb " pan='%d'" pan;
1868 add_attrs bb false dc c time;
1869 if nonemptystr c.css
1870 then Printf.bprintf bb ">\n <css><![CDATA[%s]]></css>" c.css;
1871 let kb = keymapsbuf false dc c in
1873 begin match bookmarks with
1874 | [] ->
1875 if Buffer.length kb > 0
1876 then (
1877 Buffer.add_string bb ">\n";
1878 Buffer.add_buffer bb kb;
1879 Buffer.add_string bb "\n</doc>\n";
1881 else
1882 if nonemptystr c.css
1883 then Buffer.add_string bb "\n</doc>\n"
1884 else Buffer.add_string bb "/>\n"
1885 | _ ->
1886 Buffer.add_string bb ">\n<bookmarks>\n";
1887 List.iter (fun (title, _, kind) ->
1888 begin match kind with
1889 | Oanchor (page, rely, visy) ->
1890 Printf.bprintf bb
1891 "<item title='%s' page='%d'"
1892 (Parser.enent title 0 (String.length title))
1893 page
1895 if rely > 1e-6
1896 then
1897 Printf.bprintf bb " rely='%f'" rely
1899 if abs_float visy > 1e-6
1900 then
1901 Printf.bprintf bb " visy='%f'" visy
1903 | Ohistory _ | Onone | Ouri _ | Oremote _
1904 | Oremotedest _ | Olaunch _ ->
1905 failwith "unexpected link in bookmarks"
1906 end;
1907 Buffer.add_string bb "/>\n";
1908 ) bookmarks;
1909 Buffer.add_string bb "</bookmarks>";
1910 if Buffer.length kb > 0
1911 then (
1912 Buffer.add_string bb "\n";
1913 Buffer.add_buffer bb kb;
1915 Buffer.add_string bb "\n</doc>\n";
1916 end;
1920 let pan, conf =
1921 match state.mode with
1922 | Birdseye (c, pan, _, _, _) ->
1923 let beyecolumns =
1924 match conf.columns with
1925 | Cmulti ((c, _, _), _) -> Some c
1926 | Csingle _ -> None
1927 | Csplit _ -> None
1928 and columns =
1929 match c.columns with
1930 | Cmulti (c, _) -> Cmulti (c, E.a)
1931 | Csingle _ -> Csingle E.a
1932 | Csplit _ -> failwith "quit from bird's eye while split"
1934 pan, { c with beyecolumns = beyecolumns; columns = columns }
1935 | Textentry _
1936 | View
1937 | LinkNav _ -> x, conf
1939 let docpath = if nonemptystr state.path then abspath state.path else E.s in
1940 if nonemptystr docpath
1941 then (
1942 adddoc docpath pan (getanchor ())
1944 let autoscrollstep =
1945 match state.autoscroll with
1946 | Some step -> step
1947 | None -> conf.autoscrollstep
1949 begin match state.mode with
1950 | Birdseye beye -> leavebirdseye beye true
1951 | Textentry _
1952 | View
1953 | LinkNav _ -> ()
1954 end;
1955 let key =
1956 try Digest.file docpath |> Digest.to_hex
1957 with _ -> E.s
1959 { conf with autoscrollstep; key }
1961 (if conf.savebmarks then state.bookmarks else [])
1962 (now ())
1963 state.origin
1965 Hashtbl.iter (fun path (c, bookmarks, x, anchor, origin) ->
1966 if docpath <> abspath path
1967 then adddoc path x anchor c bookmarks c.lastvisit origin
1968 ) h;
1969 Buffer.add_string bb "</llppconfig>\n";
1970 true;
1973 let save leavebirdseye =
1974 let relx = float state.x /. float state.winw in
1975 let w, h, x =
1976 let cx w = truncate (relx *. float w) in
1977 List.fold_left
1978 (fun (w, h, x) ws ->
1979 match ws with
1980 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
1981 | Wsi.MaxVert -> (w, conf.cwinh, x)
1982 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
1984 (state.winw, state.winh, state.x) state.winstate
1986 conf.cwinw <- w;
1987 conf.cwinh <- h;
1988 let bb = Buffer.create 32768 in
1989 let save2 (h, dc) =
1990 save1 bb leavebirdseye x h dc
1992 if load1 save2 && Buffer.length bb > 0
1993 then
1995 let tmp = !confpath ^ ".tmp" in
1996 let oc = open_out_bin tmp in
1997 Buffer.output_buffer oc bb;
1998 close_out oc;
1999 Unix.rename tmp !confpath;
2000 with exn ->
2001 dolog "error saving configuration: %s" @@ exntos exn
2004 let gc () =
2005 let href = ref @@ Hashtbl.create 0 in
2006 let cref = ref defconf in
2007 let push (h, dc) =
2008 let f path v =
2009 if Sys.file_exists path
2010 then Some v
2011 else (dolog "removing %s" path; None) in
2012 Hashtbl.filter_map_inplace f h;
2013 href := h;
2014 cref := dc;
2015 true
2017 ignore (load1 push);
2018 let bb = Buffer.create 32768 in
2019 let save2 (_h, dc) = save1 bb (fun _ _ -> ()) 0 !href dc in
2020 if load1 save2 && Buffer.length bb > 0
2021 then (
2023 let tmp = !confpath ^ ".tmp" in
2024 let oc = open_out_bin tmp in
2025 Buffer.output_buffer oc bb;
2026 close_out oc;
2027 Unix.rename tmp !confpath;
2028 with exn ->
2029 dolog "error saving configuration: %s" @@ exntos exn
2033 let logcurrently = function
2034 | Idle -> dolog "Idle"
2035 | Loading (l, gen) ->
2036 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2037 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2038 dolog
2039 "Tiling %d[%d,%d] page=%s cs=%s angle=%d"
2040 l.pageno col row (~> pageopaque)
2041 (CSTE.to_string colorspace) angle;
2042 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2043 angle gen conf.angle state.gen
2044 tilew tileh
2045 conf.tilew conf.tileh;
2046 | Outlining _ ->
2047 dolog "outlining"