Just use a copy
[llpp.git] / config.ml
blobf1466c156bc2f4e30e702289f681d260cb522d7f
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 : (float * float * float)
252 ; mutable bedefault : bool
253 ; mutable tilew : int
254 ; mutable tileh : int
255 ; mutable mustoresize : memsize
256 ; mutable checkers : bool
257 ; mutable aalevel : int
258 ; mutable urilauncher : string
259 ; mutable pathlauncher : string
260 ; mutable colorspace : colorspace
261 ; mutable invert : bool
262 ; mutable colorscale : float
263 ; mutable ghyllscroll : (int * int * int) option
264 ; mutable columns : columns
265 ; mutable beyecolumns : columncount option
266 ; mutable selcmd : string
267 ; mutable paxcmd : string
268 ; mutable passcmd : string
269 ; mutable savecmd : string
270 ; mutable updatecurs : bool
271 ; mutable keyhashes : (string * keyhash) list
272 ; mutable hfsize : int
273 ; mutable pgscale : float
274 ; mutable usepbo : bool
275 ; mutable wheelbypage : bool
276 ; mutable stcmd : string
277 ; mutable riani : bool
278 ; mutable pax : (float * int * int) ref option
279 ; mutable paxmark : mark
280 ; mutable leftscroll : bool
281 ; mutable title : string
282 ; mutable lastvisit : float
283 ; mutable annotinline : bool
284 ; mutable coarseprespos : bool
285 ; mutable css : css
286 ; mutable usedoccss : usedoccss
288 and columns =
289 | Csingle of singlecolumn
290 | Cmulti of multicolumns
291 | Csplit of splitcolumns
292 and outlinekind =
293 | Onone
294 | Oanchor of anchor
295 | Ouri of uri
296 | Olaunch of launchcommand
297 | Oremote of (filename * pageno)
298 | Oremotedest of (filename * destname)
299 | Ohistory of (filename * conf * outline list * x * anchor * filename)
300 and outline = (caption * outlinelevel * outlinekind)
301 and outlinelevel = int
304 type page =
305 { pageno : int
306 ; pagedimno : int
307 ; pagew : int
308 ; pageh : int
309 ; pagex : int
310 ; pagey : int
311 ; pagevw : int
312 ; pagevh : int
313 ; pagedispx : int
314 ; pagedispy : int
315 ; pagecol : int
319 type tile = opaque * pixmapsize * elapsed
320 and elapsed = float;;
321 type pagemapkey = pageno * gen;;
322 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
323 and row = int
324 and col = int
325 and currently =
326 | Idle
327 | Loading of (page * gen)
328 | Tiling of (
329 page * opaque * colorspace * angle * gen * col * row * width * height
331 | Outlining of outline list
334 type mpos = int * int
335 and mstate =
336 | Msel of (mpos * mpos)
337 | Mpan of mpos
338 | Mscrolly | Mscrollx
339 | Mzoom of (buttonno * step * mpos)
340 | Mzoomrect of (mpos * mpos)
341 | Mnone
342 and buttonno = int
343 and step = int
346 type mode =
347 | Birdseye of (conf * leftx * pageno * pageno * anchor)
348 | Textentry of (textentry * onleave)
349 | View
350 | LinkNav of linktarget
351 and onleave = leavetextentrystatus -> unit
352 and leavetextentrystatus = | Cancel | Confirm
353 and helpitem = string * int * action
354 and action =
355 | Noaction
356 | Action of (uioh -> uioh)
357 and linktarget =
358 | Ltexact of (pageno * direction)
359 | Ltgendir of direction
360 | Ltnotready of (pageno * direction)
361 and direction = int (* -1, 0, 1 *)
362 and textentry = string * string * onhist option
363 * onkey * ondone * cancelonempty
364 and onkey = string -> Keys.t -> te
365 and ondone = string -> unit
366 and histcancel = unit -> unit
367 and onhist = ((histcmd -> string) * histcancel)
368 and histcmd = HCnext | HCprev | HCfirst | HClast
369 and cancelonempty = bool
370 and te =
371 | TEstop
372 | TEdone of string
373 | TEcont of string
374 | TEswitch of textentry
377 type 'a circbuf =
378 { store : 'a array
379 ; mutable rc : int
380 ; mutable wc : int
381 ; mutable len : int
385 type state =
386 { mutable ss : Unix.file_descr
387 ; mutable wsfd : Unix.file_descr
388 ; mutable stderr : Unix.file_descr
389 ; mutable errmsgs : Buffer.t
390 ; mutable newerrmsgs : bool
391 ; mutable w : int
392 ; mutable x : x
393 ; mutable y : y
394 ; mutable anchor : anchor
395 ; mutable ranchors : (string * string * anchor * string) list
396 ; mutable maxy : int
397 ; mutable layout : page list
398 ; pagemap : (pagemapkey, opaque) Hashtbl.t
399 ; tilemap : (tilemapkey, tile) Hashtbl.t
400 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
401 ; mutable pdims : (pageno * width * height * leftx) list
402 ; mutable pagecount : int
403 ; mutable currently : currently
404 ; mutable mstate : mstate
405 ; mutable searchpattern : string
406 ; mutable rects : (pageno * rectcolor * rect) list
407 ; mutable rects1 : (pageno * rectcolor * rect) list
408 ; prects : (pageno, float array) Hashtbl.t
409 ; mutable text : string
410 ; mutable winstate : Wsi.winstate list
411 ; mutable mode : mode
412 ; mutable uioh : uioh
413 ; mutable outlines : outline array
414 ; mutable bookmarks : outline list
415 ; mutable path : string
416 ; mutable password : string
417 ; mutable nameddest : string
418 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
419 ; mutable memused : memsize
420 ; mutable gen : gen
421 ; mutable throttle : (page list * int * float) option
422 ; mutable autoscroll : int option
423 ; mutable ghyll : (int option -> unit)
424 ; mutable help : helpitem array
425 ; mutable docinfo : (int * string) list
426 ; mutable checkerstexid : GlTex.texture_id option
427 ; hists : hists
428 ; mutable prevzoom : (float * int)
429 ; mutable progress : float
430 ; mutable redisplay : bool
431 ; mutable mpos : mpos
432 ; mutable keystate : keystate
433 ; mutable glinks : bool
434 ; mutable prevcolumns : (columns * float) option
435 ; mutable winw : int
436 ; mutable winh : int
437 ; mutable reprf : (unit -> unit)
438 ; mutable origin : string
439 ; mutable roam : (unit -> unit)
440 ; mutable bzoom : bool
441 ; mutable traw : [`float] Raw.t
442 ; mutable vraw : [`float] Raw.t
443 ; mutable lnava : (pageno * linkno) option
445 and hists =
446 { pat : string circbuf
447 ; pag : string circbuf
448 ; nav : anchor circbuf
449 ; sel : string circbuf
453 let emptyanchor = (0, 0.0, 0.0);;
454 let emptykeyhash = Hashtbl.create 0;;
455 let noghyll _ = ();;
456 let noreprf () = ();;
457 let noroam () = ();;
459 let nouioh : uioh = object (self)
460 method display = ()
461 method key _ _ = self
462 method multiclick _ _ _ _ = self
463 method button _ _ _ _ _ = self
464 method motion _ _ = self
465 method pmotion _ _ = self
466 method infochanged _ = ()
467 method scrollpw = (0, nan, nan)
468 method scrollph = (0, nan, nan)
469 method modehash = emptykeyhash
470 method eformsgs = false
471 method alwaysscrolly = false
472 method scroll _ _ = self
473 method zoom _ _ _ = ()
474 end;;
476 let platform_to_string = function
477 | Punknown -> "unknown"
478 | Plinux -> "Linux"
479 | Posx -> "OSX"
480 | Psun -> "Sun"
481 | Pbsd -> "BSD"
482 | Pcygwin -> "Cygwin"
485 let version () =
486 Printf.sprintf "llpp version %s, fitz %s, ocaml %s/%d bit"
487 Help.version (fz_version ()) Sys.ocaml_version Sys.word_size
490 let defconf =
491 { scrollbw = 7
492 ; scrollh = 12
493 ; scrollb = scrollbhv lor scrollbvv
494 ; icase = true
495 ; preload = true
496 ; pagebias = 0
497 ; verbose = false
498 ; debug = false
499 ; scrollstep = 24
500 ; hscrollstep = 24
501 ; maxhfit = true
502 ; crophack = false
503 ; autoscrollstep = 2
504 ; maxwait = None
505 ; hlinks = false
506 ; underinfo = false
507 ; interpagespace = 2
508 ; zoom = 1.0
509 ; presentation = false
510 ; angle = 0
511 ; cwinw = 1200
512 ; cwinh = 1000
513 ; savebmarks = true
514 ; fitmodel = FitProportional
515 ; trimmargins = false
516 ; trimfuzz = (0,0,0,0)
517 ; memlimit = 32 lsl 20
518 ; texcount = 256
519 ; sliceheight = 24
520 ; thumbw = 76
521 ; jumpback = true
522 ; bgcolor = (0.5, 0.5, 0.5)
523 ; bedefault = false
524 ; tilew = 2048
525 ; tileh = 2048
526 ; mustoresize = 256 lsl 20
527 ; checkers = true
528 ; aalevel = 8
529 ; urilauncher =
530 (match platform with
531 | Plinux | Psun | Pbsd -> "xdg-open \"%s\""
532 | Posx -> "open \"%s\""
533 | Pcygwin -> "cygstart \"%s\""
534 | Punknown -> "echo %s")
535 ; pathlauncher = "lp \"%s\""
536 ; selcmd =
537 (match platform with
538 | Plinux | Pbsd | Psun -> "xsel -i"
539 | Posx -> "pbcopy"
540 | Pcygwin -> "wsel"
541 | Punknown -> "cat")
542 ; paxcmd = "cat"
543 ; passcmd = E.s
544 ; savecmd = E.s
545 ; colorspace = Rgb
546 ; invert = false
547 ; colorscale = 1.0
548 ; ghyllscroll = None
549 ; columns = Csingle [||]
550 ; beyecolumns = None
551 ; updatecurs = true
552 ; hfsize = 12 * Wsi.fontsizefactor ()
553 ; pgscale = 1.0
554 ; usepbo = false
555 ; wheelbypage = false
556 ; stcmd = "echo SyncTex"
557 ; riani = false
558 ; pax = None
559 ; paxmark = Mark_word
560 ; leftscroll = false
561 ; title = E.s
562 ; lastvisit = 0.0
563 ; annotinline = true
564 ; coarseprespos = false
565 ; css = E.s
566 ; usedoccss = true
567 ; keyhashes =
568 let mk n = (n, Hashtbl.create 1) in
569 [ mk "global"
570 ; mk "info"
571 ; mk "help"
572 ; mk "outline"
573 ; mk "listview"
574 ; mk "birdseye"
575 ; mk "textentry"
576 ; mk "links"
577 ; mk "view"
582 let conf = { defconf with angle = defconf.angle };;
584 let gotourl url =
585 let command = Str.global_replace percentsre url conf.urilauncher in
586 try ignore @@ spawn command []
587 with exn -> dolog "failed to execute `%s': %s" command @@ exntos exn
590 let gotouri uri =
591 if emptystr conf.urilauncher
592 then dolog "%s" uri
593 else
594 match geturl uri with
595 | "" -> dolog "obtained empty url from uri %S" uri
596 | url -> gotourl url
599 let makehelp () =
600 let strings =
601 version ()
602 :: "(searching in this text works just by typing (i.e. no initial '/'))"
603 :: E.s :: Help.keys
605 List.map (fun s ->
606 match geturl s with
607 | "" -> (s, 0, Noaction)
608 | url -> (s, 0, Action (fun uioh -> gotourl url; uioh))
609 ) strings;
612 let cbnew n v =
613 { store = Array.make n v
614 ; rc = 0
615 ; wc = 0
616 ; len = 0
620 let cbcap b = Array.length b.store;;
622 let cbput b v =
623 let cap = cbcap b in
624 b.store.(b.wc) <- v;
625 b.wc <- (b.wc + 1) mod cap;
626 b.rc <- b.wc;
627 b.len <- min (b.len + 1) cap;
630 let cbempty b = b.len = 0;;
632 let cbgetg b circular dir =
633 if cbempty b
634 then b.store.(0)
635 else
636 let rc = b.rc + dir in
637 let rc =
638 if circular
639 then (
640 if rc = -1
641 then b.len-1
642 else (
643 if rc >= b.len
644 then 0
645 else rc
648 else bound rc 0 (b.len-1)
650 b.rc <- rc;
651 b.store.(rc);
654 let cbget b = cbgetg b false;;
655 let cbgetc b = cbgetg b true;;
657 let state =
658 { ss = Unix.stdin
659 ; wsfd = Unix.stdin
660 ; stderr = Unix.stderr
661 ; errmsgs = Buffer.create 0
662 ; newerrmsgs = false
663 ; x = 0
664 ; y = 0
665 ; w = 0
666 ; anchor = emptyanchor
667 ; ranchors = []
668 ; layout = []
669 ; maxy = max_int
670 ; tilelru = Queue.create ()
671 ; pagemap = Hashtbl.create 10
672 ; tilemap = Hashtbl.create 10
673 ; pdims = []
674 ; pagecount = 0
675 ; currently = Idle
676 ; mstate = Mnone
677 ; rects = []
678 ; rects1 = []
679 ; prects = Hashtbl.create 1
680 ; text = E.s
681 ; mode = View
682 ; winstate = []
683 ; searchpattern = E.s
684 ; outlines = E.a
685 ; bookmarks = []
686 ; path = E.s
687 ; password = E.s
688 ; nameddest = E.s
689 ; geomcmds = E.s, []
690 ; hists =
691 { nav = cbnew 10 emptyanchor
692 ; pat = cbnew 10 E.s
693 ; pag = cbnew 10 E.s
694 ; sel = cbnew 10 E.s
696 ; memused = 0
697 ; gen = 0
698 ; throttle = None
699 ; autoscroll = None
700 ; ghyll = noghyll
701 ; help = E.a
702 ; docinfo = []
703 ; checkerstexid = None
704 ; prevzoom = (1.0, 0)
705 ; progress = -1.0
706 ; uioh = nouioh
707 ; redisplay = true
708 ; mpos = (-1, -1)
709 ; keystate = KSnone
710 ; glinks = false
711 ; prevcolumns = None
712 ; winw = -1
713 ; winh = -1
714 ; reprf = noreprf
715 ; origin = E.s
716 ; roam = noroam
717 ; bzoom = false
718 ; traw = Raw.create_static `float ~len:8
719 ; vraw = Raw.create_static `float ~len:8
720 ; lnava = None
724 let copykeyhashes c =
725 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
728 let calcips h =
729 let d = state.winh - h in
730 max conf.interpagespace ((d + 1) / 2)
733 let rowyh (c, coverA, coverB) b n =
734 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
735 then
736 let _, _, vy, (_, _, h, _) = b.(n) in
737 (vy, h)
738 else
739 let n' = n - coverA in
740 let d = n' mod c in
741 let s = n - d in
742 let e = min state.pagecount (s + c) in
743 let rec find m miny maxh = if m = e then miny, maxh else
744 let _, _, y, (_, _, h, _) = b.(m) in
745 let miny = min miny y in
746 let maxh = max maxh h in
747 find (m+1) miny maxh
748 in find s max_int 0
751 let page_of_y y =
752 let ((c, coverA, coverB) as cl), b =
753 match conf.columns with
754 | Csingle b -> (1, 0, 0), b
755 | Cmulti (c, b) -> c, b
756 | Csplit (_, b) -> (1, 0, 0), b
758 if Array.length b = 0
759 then -1
760 else
761 let rec bsearch nmin nmax =
762 if nmin > nmax
763 then bound nmin 0 (state.pagecount-1)
764 else
765 let n = (nmax + nmin) / 2 in
766 let vy, h = rowyh cl b n in
767 let y0, y1 =
768 if conf.presentation
769 then
770 let ips = calcips h in
771 let y0 = vy - ips in
772 let y1 = vy + h + ips in
773 y0, y1
774 else (
775 if n = 0
776 then 0, vy + h + conf.interpagespace
777 else
778 let y0 = vy - conf.interpagespace in
779 y0, y0 + h + conf.interpagespace
782 if y >= y0 && y < y1
783 then (
784 if c = 1
785 then n
786 else (
787 if n > coverA
788 then
789 if n < state.pagecount - coverB
790 then ((n-coverA)/c)*c + coverA
791 else n
792 else n
795 else (
796 if y > y0
797 then bsearch (n+1) nmax
798 else bsearch nmin (n-1)
801 bsearch 0 (state.pagecount-1);
804 let calcheight () =
805 match conf.columns with
806 | Cmulti ((_, _, _) as cl, b) ->
807 if Array.length b > 0
808 then
809 let y, h = rowyh cl b (Array.length b - 1) in
810 y + h + (if conf.presentation then calcips h else 0)
811 else 0
812 | Csingle b ->
813 if Array.length b > 0
814 then
815 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
816 y + h + (if conf.presentation then calcips h else 0)
817 else 0
818 | Csplit (_, b) ->
819 if Array.length b > 0
820 then
821 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
822 y + h
823 else 0
826 let getpageywh pageno =
827 let pageno = bound pageno 0 (state.pagecount-1) in
828 match conf.columns with
829 | Csingle b ->
830 if Array.length b = 0
831 then 0, 0, 0
832 else
833 let (_, _, y, (_, w, h, _)) = b.(pageno) in
834 let y =
835 if conf.presentation
836 then y - calcips h
837 else y
839 y, w, h
840 | Cmulti (cl, b) ->
841 if Array.length b = 0
842 then 0, 0, 0
843 else
844 let y, h = rowyh cl b pageno in
845 let (_, _, _, (_, w, _, _)) = b.(pageno) in
846 let y =
847 if conf.presentation
848 then y - calcips h
849 else y
851 y, w, h
852 | Csplit (c, b) ->
853 if Array.length b = 0
854 then 0, 0, 0
855 else
856 let n = pageno*c in
857 let (_, _, y, (_, w, h, _)) = b.(n) in
858 y, w / c, h
861 let getpageyh pageno =
862 let y,_,h = getpageywh pageno in
863 y, h;
866 let getpagedim pageno =
867 let rec f ppdim l =
868 match l with
869 | (n, _, _, _) as pdim :: rest ->
870 if n >= pageno
871 then (if n = pageno then pdim else ppdim)
872 else f pdim rest
874 | [] -> ppdim
876 f (-1, -1, -1, -1) state.pdims
879 let getpdimno pageno =
880 let rec f p l =
881 let np = succ p in
882 match l with
883 | (n, _, _, _) :: rest ->
884 if n >= pageno
885 then (if n = pageno then np else p)
886 else f np rest
888 | [] -> p
890 f ~-1 state.pdims
893 let getpagey pageno = fst (getpageyh pageno);;
895 let getanchor1 l =
896 let top =
897 let coloff = l.pagecol * l.pageh in
898 float (l.pagey + coloff) /. float l.pageh
900 let dtop =
901 if l.pagedispy = 0
902 then
904 else (
905 if conf.presentation
906 then float l.pagedispy /. float (calcips l.pageh)
907 else float l.pagedispy /. float conf.interpagespace
910 (l.pageno, top, dtop)
913 let getanchor () =
914 match state.layout with
915 | l :: _ -> getanchor1 l
916 | [] ->
917 let n = page_of_y state.y in
918 if n = -1
919 then state.anchor
920 else
921 let y, h = getpageyh n in
922 let dy = y - state.y in
923 let dtop =
924 if conf.presentation
925 then
926 let ips = calcips h in
927 float (dy + ips) /. float ips
928 else
929 float dy /. float conf.interpagespace
931 (n, 0.0, dtop)
934 let fontpath = ref E.s;;
936 type historder = [ `lastvisit | `title | `path | `file ];;
938 module KeyMap =
939 Map.Make (struct type t = (int * int) let compare = compare end);;
941 let unentS s =
942 let l = String.length s in
943 let b = Buffer.create l in
944 Parser.unent b s 0 l;
945 Buffer.contents b;
948 let home =
949 try Sys.getenv "HOME"
950 with exn ->
951 dolog "cannot determine home directory location: %s" @@ exntos exn;
955 let modifier_of_string = function
956 | "alt" -> Wsi.altmask
957 | "shift" -> Wsi.shiftmask
958 | "ctrl" | "control" -> Wsi.ctrlmask
959 | "meta" -> Wsi.metamask
960 | _ -> 0
963 let keys_of_string s =
964 let key_of_string r s =
965 let elems = Str.full_split r s in
966 let f n k m =
967 let g s =
968 let m1 = modifier_of_string s in
969 if m1 = 0
970 then (Wsi.namekey s, m)
971 else (k, m lor m1)
972 in function
973 | Str.Delim s when n land 1 = 0 -> g s
974 | Str.Text s -> g s
975 | Str.Delim _ -> (k, m)
977 let rec loop n k m = function
978 | [] -> (k, m)
979 | x :: xs ->
980 let k, m = f n k m x in
981 loop (n+1) k m xs
983 loop 0 0 0 elems
985 let elems = Str.split whitere s in
986 List.map (key_of_string (Str.regexp "-")) elems
989 let config_of c attrs =
990 let apply c k v =
992 match k with
993 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
994 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
995 | "case-insensitive-search" -> { c with icase = bool_of_string v }
996 | "preload" -> { c with preload = bool_of_string v }
997 | "page-bias" -> { c with pagebias = int_of_string v }
998 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
999 | "horizontal-scroll-step" ->
1000 { c with hscrollstep = max (int_of_string v) 1 }
1001 | "auto-scroll-step" ->
1002 { c with autoscrollstep = max 0 (int_of_string v) }
1003 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
1004 | "crop-hack" -> { c with crophack = bool_of_string v }
1005 | "throttle" ->
1006 let mw =
1007 match String.map asciilower v with
1008 | "true" -> Some infinity
1009 | "false" -> None
1010 | f -> Some (float_of_string f)
1012 { c with maxwait = mw }
1013 | "highlight-links" -> { c with hlinks = bool_of_string v }
1014 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
1015 | "vertical-margin" ->
1016 { c with interpagespace = max 0 (int_of_string v) }
1017 | "zoom" ->
1018 let zoom = float_of_string v /. 100. in
1019 let zoom = max zoom 0.0 in
1020 { c with zoom = zoom }
1021 | "presentation" -> { c with presentation = bool_of_string v }
1022 | "rotation-angle" -> { c with angle = int_of_string v }
1023 | "width" -> { c with cwinw = max 20 (int_of_string v) }
1024 | "height" -> { c with cwinh = max 20 (int_of_string v) }
1025 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
1026 | "proportional-display" ->
1027 let fm =
1028 if bool_of_string v
1029 then FitProportional
1030 else FitWidth
1032 { c with fitmodel = fm }
1033 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
1034 | "pixmap-cache-size" ->
1035 { c with memlimit = max 2 (int_of_string_with_suffix v) }
1036 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
1037 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
1038 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
1039 | "persistent-location" -> { c with jumpback = bool_of_string v }
1040 | "background-color" -> { c with bgcolor = color_of_string v }
1041 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
1042 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
1043 | "mupdf-store-size" ->
1044 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
1045 | "checkers" -> { c with checkers = bool_of_string v }
1046 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
1047 | "trim-margins" -> { c with trimmargins = bool_of_string v }
1048 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
1049 | "uri-launcher" -> { c with urilauncher = unentS v }
1050 | "path-launcher" -> { c with pathlauncher = unentS v }
1051 | "color-space" -> { c with colorspace = CSTE.of_string v }
1052 | "invert-colors" -> { c with invert = bool_of_string v }
1053 | "brightness" -> { c with colorscale = float_of_string v }
1054 | "ghyllscroll" -> { c with ghyllscroll = ghyllscroll_of_string v }
1055 | "columns" ->
1056 let (n, _, _) as nab = multicolumns_of_string v in
1057 if n < 0
1058 then { c with columns = Csplit (-n, E.a) }
1059 else { c with columns = Cmulti (nab, E.a) }
1060 | "birds-eye-columns" ->
1061 { c with beyecolumns = Some (max (int_of_string v) 2) }
1062 | "selection-command" -> { c with selcmd = unentS v }
1063 | "synctex-command" -> { c with stcmd = unentS v }
1064 | "pax-command" -> { c with paxcmd = unentS v }
1065 | "askpass-command" -> { c with passcmd = unentS v }
1066 | "savepath-command" -> { c with savecmd = unentS v }
1067 | "update-cursor" -> { c with updatecurs = bool_of_string v }
1068 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
1069 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
1070 | "use-pbo" -> { c with usepbo = bool_of_string v }
1071 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
1072 | "horizontal-scrollbar-visible" ->
1073 let b =
1074 if bool_of_string v
1075 then c.scrollb lor scrollbhv
1076 else c.scrollb land (lnot scrollbhv)
1078 { c with scrollb = b }
1079 | "vertical-scrollbar-visible" ->
1080 let b =
1081 if bool_of_string v
1082 then c.scrollb lor scrollbvv
1083 else c.scrollb land (lnot scrollbvv)
1085 { c with scrollb = b }
1086 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
1087 | "point-and-x" ->
1088 { c with pax =
1089 if bool_of_string v
1090 then Some (ref (0.0, 0, 0))
1091 else None }
1092 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
1093 | "scroll-bar-on-the-left" -> { c with leftscroll = bool_of_string v }
1094 | "title" -> { c with title = unentS v }
1095 | "last-visit" -> { c with lastvisit = float_of_string v }
1096 | "edit-annotations-inline" -> { c with annotinline = bool_of_string v }
1097 | "coarse-presentation-positioning" ->
1098 { c with coarseprespos = bool_of_string v }
1099 | "use-document-css" -> { c with usedoccss = bool_of_string v }
1100 | _ -> c
1101 with exn ->
1102 dolog "error processing attribute (`%S' = `%S'): %s" k v @@ exntos exn;
1105 let rec fold c = function
1106 | [] -> c
1107 | (k, v) :: rest ->
1108 let c = apply c k v in
1109 fold c rest
1111 fold { c with keyhashes = copykeyhashes c } attrs;
1114 let fromstring f pos n v d =
1115 try f v
1116 with exn ->
1117 dolog "error processing attribute (%S=%S) at %d\n%s" n v pos @@ exntos exn;
1121 let bookmark_of attrs =
1122 let rec fold title page rely visy = function
1123 | ("title", v) :: rest -> fold v page rely visy rest
1124 | ("page", v) :: rest -> fold title v rely visy rest
1125 | ("rely", v) :: rest -> fold title page v visy rest
1126 | ("visy", v) :: rest -> fold title page rely v rest
1127 | _ :: rest -> fold title page rely visy rest
1128 | [] -> title, page, rely, visy
1130 fold "invalid" "0" "0" "0" attrs
1133 let doc_of attrs =
1134 let rec fold path page rely pan visy origin = function
1135 | ("path", v) :: rest -> fold v page rely pan visy origin rest
1136 | ("page", v) :: rest -> fold path v rely pan visy origin rest
1137 | ("rely", v) :: rest -> fold path page v pan visy origin rest
1138 | ("pan", v) :: rest -> fold path page rely v visy origin rest
1139 | ("visy", v) :: rest -> fold path page rely pan v origin rest
1140 | ("origin", v) :: rest -> fold path page rely pan visy v rest
1141 | _ :: rest -> fold path page rely pan visy origin rest
1142 | [] -> path, page, rely, pan, visy, origin
1144 fold E.s "0" "0" "0" "0" E.s attrs
1147 let map_of attrs =
1148 let rec fold rs ls = function
1149 | ("out", v) :: rest -> fold v ls rest
1150 | ("in", v) :: rest -> fold rs v rest
1151 | _ :: rest -> fold ls rs rest
1152 | [] -> ls, rs
1154 fold E.s E.s attrs
1157 let setconf dst src =
1158 dst.scrollbw <- src.scrollbw;
1159 dst.scrollh <- src.scrollh;
1160 dst.icase <- src.icase;
1161 dst.preload <- src.preload;
1162 dst.pagebias <- src.pagebias;
1163 dst.verbose <- src.verbose;
1164 dst.scrollstep <- src.scrollstep;
1165 dst.maxhfit <- src.maxhfit;
1166 dst.crophack <- src.crophack;
1167 dst.autoscrollstep <- src.autoscrollstep;
1168 dst.maxwait <- src.maxwait;
1169 dst.hlinks <- src.hlinks;
1170 dst.underinfo <- src.underinfo;
1171 dst.interpagespace <- src.interpagespace;
1172 dst.zoom <- src.zoom;
1173 dst.presentation <- src.presentation;
1174 dst.angle <- src.angle;
1175 dst.cwinw <- src.cwinw;
1176 dst.cwinh <- src.cwinh;
1177 dst.savebmarks <- src.savebmarks;
1178 dst.memlimit <- src.memlimit;
1179 dst.fitmodel <- src.fitmodel;
1180 dst.texcount <- src.texcount;
1181 dst.sliceheight <- src.sliceheight;
1182 dst.thumbw <- src.thumbw;
1183 dst.jumpback <- src.jumpback;
1184 dst.bgcolor <- src.bgcolor;
1185 dst.tilew <- src.tilew;
1186 dst.tileh <- src.tileh;
1187 dst.mustoresize <- src.mustoresize;
1188 dst.checkers <- src.checkers;
1189 dst.aalevel <- src.aalevel;
1190 dst.trimmargins <- src.trimmargins;
1191 dst.trimfuzz <- src.trimfuzz;
1192 dst.urilauncher <- src.urilauncher;
1193 dst.colorspace <- src.colorspace;
1194 dst.invert <- src.invert;
1195 dst.colorscale <- src.colorscale;
1196 dst.ghyllscroll <- src.ghyllscroll;
1197 dst.columns <- src.columns;
1198 dst.beyecolumns <- src.beyecolumns;
1199 dst.selcmd <- src.selcmd;
1200 dst.updatecurs <- src.updatecurs;
1201 dst.pathlauncher <- src.pathlauncher;
1202 dst.keyhashes <- copykeyhashes src;
1203 dst.hfsize <- src.hfsize;
1204 dst.hscrollstep <- src.hscrollstep;
1205 dst.pgscale <- src.pgscale;
1206 dst.usepbo <- src.usepbo;
1207 dst.wheelbypage <- src.wheelbypage;
1208 dst.stcmd <- src.stcmd;
1209 dst.paxcmd <- src.paxcmd;
1210 dst.passcmd <- src.passcmd;
1211 dst.savecmd <- src.savecmd;
1212 dst.scrollb <- src.scrollb;
1213 dst.riani <- src.riani;
1214 dst.paxmark <- src.paxmark;
1215 dst.leftscroll <- src.leftscroll;
1216 dst.title <- src.title;
1217 dst.annotinline <- src.annotinline;
1218 dst.coarseprespos <- src.coarseprespos;
1219 dst.css <- src.css;
1220 dst.usedoccss <- src.usedoccss;
1221 dst.pax <-
1222 if src.pax = None
1223 then None
1224 else Some ((ref (0.0, 0, 0)));
1227 let findkeyhash c name =
1228 try List.assoc name c.keyhashes
1229 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
1232 let get s =
1233 let open Parser in
1234 let h = Hashtbl.create 10 in
1235 let dc = { defconf with angle = defconf.angle } in
1236 let rec toplevel v t spos _ =
1237 match t with
1238 | Vdata | Vcdata | Vend -> v
1239 | Vopen ("llppconfig", _, closed) ->
1240 if closed
1241 then v
1242 else { v with f = llppconfig }
1243 | Vopen _ -> parse_error "unexpected subelement at top level" s spos
1244 | Vclose _ -> parse_error "unexpected close at top level" s spos
1246 and llppconfig v t spos _ =
1247 match t with
1248 | Vdata | Vcdata -> v
1249 | Vend -> parse_error "unexpected end of input in llppconfig" s spos
1250 | Vopen ("defaults", attrs, closed) ->
1251 let c = config_of dc attrs in
1252 setconf dc c;
1253 if closed
1254 then v
1255 else { v with f = defaults }
1257 | Vopen ("ui-font", attrs, closed) ->
1258 let rec getsize size = function
1259 | [] -> size
1260 | ("size", v) :: rest ->
1261 let size =
1262 fromstring int_of_string spos "size" v fstate.fontsize in
1263 getsize size rest
1264 | l -> getsize size l
1266 fstate.fontsize <- getsize fstate.fontsize attrs;
1267 if closed
1268 then v
1269 else { v with f = uifont (Buffer.create 10) }
1271 | Vopen ("doc", attrs, closed) ->
1272 let pathent, spage, srely, span, svisy, origin = doc_of attrs in
1273 let path = unentS pathent
1274 and origin = unentS origin
1275 and pageno = fromstring int_of_string spos "page" spage 0
1276 and rely = fromstring float_of_string spos "rely" srely 0.0
1277 and pan = fromstring int_of_string spos "pan" span 0
1278 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1279 let c = config_of dc attrs in
1280 let anchor = (pageno, rely, visy) in
1281 if closed
1282 then (Hashtbl.add h path (c, [], pan, anchor, origin); v)
1283 else { v with f = doc path origin pan anchor c [] }
1285 | Vopen _ ->
1286 parse_error "unexpected subelement in llppconfig" s spos
1288 | Vclose "llppconfig" -> { v with f = toplevel }
1289 | Vclose _ -> parse_error "unexpected close in llppconfig" s spos
1291 and defaults v t spos _ =
1292 match t with
1293 | Vdata | Vcdata -> v
1294 | Vend -> parse_error "unexpected end of input in defaults" s spos
1295 | Vopen ("keymap", attrs, closed) ->
1296 let modename =
1297 try List.assoc "mode" attrs
1298 with Not_found -> "global" in
1299 if closed
1300 then v
1301 else
1302 let ret keymap =
1303 let h = findkeyhash dc modename in
1304 KeyMap.iter (Hashtbl.replace h) keymap;
1305 defaults
1307 { v with f = pkeymap ret KeyMap.empty }
1309 | Vopen (_, _, _) ->
1310 parse_error "unexpected subelement in defaults" s spos
1312 | Vclose "defaults" ->
1313 { v with f = llppconfig }
1315 | Vclose _ -> parse_error "unexpected close in defaults" s spos
1317 and uifont b v t spos epos =
1318 match t with
1319 | Vdata | Vcdata ->
1320 Buffer.add_substring b s spos (epos - spos);
1322 | Vopen (_, _, _) ->
1323 parse_error "unexpected subelement in ui-font" s spos
1324 | Vclose "ui-font" ->
1325 if emptystr !fontpath
1326 then fontpath := Buffer.contents b;
1327 { v with f = llppconfig }
1328 | Vclose _ -> parse_error "unexpected close in ui-font" s spos
1329 | Vend -> parse_error "unexpected end of input in ui-font" s spos
1331 and doc path origin pan anchor c bookmarks v t spos _ =
1332 match t with
1333 | Vdata | Vcdata -> v
1334 | Vend -> parse_error "unexpected end of input in doc" s spos
1335 | Vopen ("bookmarks", _, closed) ->
1336 if closed
1337 then v
1338 else { v with f = pbookmarks path origin pan anchor c bookmarks }
1340 | Vopen ("keymap", attrs, closed) ->
1341 let modename =
1342 try List.assoc "mode" attrs
1343 with Not_found -> "global"
1345 if closed
1346 then v
1347 else
1348 let ret keymap =
1349 let h = findkeyhash c modename in
1350 KeyMap.iter (Hashtbl.replace h) keymap;
1351 doc path origin pan anchor c bookmarks
1353 { v with f = pkeymap ret KeyMap.empty }
1355 | Vopen ("css", [], false) ->
1356 { v with f = pcss path origin pan anchor c bookmarks }
1358 | Vopen (_, _, _) ->
1359 parse_error "unexpected subelement in doc" s spos
1361 | Vclose "doc" ->
1362 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor, origin);
1363 { v with f = llppconfig }
1365 | Vclose _ -> parse_error "unexpected close in doc" s spos
1367 and pcss path origin pan anchor c bookmarks v t spos epos =
1368 match t with
1369 | Vdata | Vcdata ->
1370 let b = Buffer.create 10 in
1371 Buffer.add_substring b s spos (epos - spos);
1372 { v with f = pcss path origin pan anchor
1373 { c with css = Buffer.contents b }
1374 bookmarks }
1375 | Vend -> parse_error "unexpected end of input in css" s spos
1376 | Vopen _ -> parse_error "unexpected subelement in css" s spos
1377 | Vclose "css" -> { v with f = doc path origin pan anchor c bookmarks }
1378 | Vclose _ -> parse_error "unexpected close in css" s spos
1380 and pkeymap ret keymap v t spos _ =
1381 match t with
1382 | Vdata | Vcdata -> v
1383 | Vend -> parse_error "unexpected end of input in keymap" s spos
1384 | Vopen ("map", attrs, closed) ->
1385 let r, l = map_of attrs in
1386 let kss = fromstring keys_of_string spos "in" r [] in
1387 let lss = fromstring keys_of_string spos "out" l [] in
1388 let keymap =
1389 match kss with
1390 | [] -> keymap
1391 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
1392 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
1394 if closed
1395 then { v with f = pkeymap ret keymap }
1396 else
1397 let f () = v in
1398 { v with f = skip "map" f }
1400 | Vopen _ ->
1401 parse_error "unexpected subelement in keymap" s spos
1403 | Vclose "keymap" ->
1404 { v with f = ret keymap }
1406 | Vclose _ -> parse_error "unexpected close in keymap" s spos
1408 and pbookmarks path origin pan anchor c bookmarks v t spos _ =
1409 match t with
1410 | Vdata | Vcdata -> v
1411 | Vend -> parse_error "unexpected end of input in bookmarks" s spos
1412 | Vopen ("item", attrs, closed) ->
1413 let titleent, spage, srely, svisy = bookmark_of attrs in
1414 let page = fromstring int_of_string spos "page" spage 0
1415 and rely = fromstring float_of_string spos "rely" srely 0.0
1416 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1417 let bookmarks =
1418 (unentS titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
1420 if closed
1421 then { v with f = pbookmarks path origin pan anchor c bookmarks }
1422 else
1423 let f () = v in
1424 { v with f = skip "item" f }
1426 | Vopen _ ->
1427 parse_error "unexpected subelement in bookmarks" s spos
1429 | Vclose "bookmarks" ->
1430 { v with f = doc path origin pan anchor c bookmarks }
1432 | Vclose _ -> parse_error "unexpected close in bookmarks" s spos
1434 and skip tag f v t spos _ =
1435 match t with
1436 | Vdata | Vcdata -> v
1437 | Vend ->
1438 parse_error ("unexpected end of input in skipped " ^ tag) s spos
1439 | Vopen (tag', _, closed) ->
1440 if closed
1441 then v
1442 else
1443 let f' () = { v with f = skip tag f } in
1444 { v with f = skip tag' f' }
1445 | Vclose ctag ->
1446 if tag = ctag
1447 then f ()
1448 else parse_error ("unexpected close in skipped " ^ tag) s spos
1451 parse { f = toplevel; accu = () } s;
1452 h, dc;
1455 let do_load f contents =
1456 try f contents
1457 with
1458 | Parser.Parse_error (msg, s, pos) ->
1459 let subs = Parser.subs s pos in
1460 Utils.error "parse error: %s: at %d [..%S..]" msg pos subs
1462 | exn -> Utils.error "parse error: %s" @@ exntos exn
1465 let defconfpath =
1466 let dir =
1467 let xdgconfdir = Utils.getenvwithdef "XDG_CONFIG_HOME" E.s in
1468 if emptystr xdgconfdir
1469 then
1471 let dir = Filename.concat home ".config" in
1472 if Sys.is_directory dir then dir else home
1473 with _ -> home
1474 else xdgconfdir
1476 Filename.concat dir "llpp.conf"
1479 let confpath = ref defconfpath;;
1481 let load2 f default =
1482 match filecontents !confpath with
1483 | contents -> f @@ do_load get contents
1484 | exception Unix.Unix_error (Unix.ENOENT, "open", _) ->
1485 f (Hashtbl.create 0, defconf)
1486 | exception exn ->
1487 dolog "error loading configuration from `%S': %s" !confpath @@ exntos exn;
1488 default
1491 let load1 f = load2 f false;;
1493 let load openlast =
1494 let f (h, dc) =
1495 if openlast
1496 then (
1497 let path, _ =
1498 Hashtbl.fold
1499 (fun path (conf, _, _, _, _) ((_, besttime) as best) ->
1500 if conf.lastvisit > besttime
1501 then (path, conf.lastvisit)
1502 else best)
1504 (state.path, -.infinity)
1506 state.path <- path;
1508 let pc, pb, px, pa, po =
1510 let absname = abspath state.path in
1511 Hashtbl.find h absname
1512 with Not_found -> dc, [], 0, emptyanchor, state.origin
1514 setconf defconf dc;
1515 setconf conf pc;
1516 state.bookmarks <- pb;
1517 state.x <- px;
1518 state.origin <- po;
1519 if conf.jumpback
1520 then state.anchor <- pa;
1521 cbput state.hists.nav pa;
1522 true
1524 load1 f
1527 let gethist () =
1528 let f (h, _) =
1529 Hashtbl.fold (fun path (pc, pb, px, pa, po) accu ->
1530 (path, pc, pb, px, pa, po) :: accu)
1531 h [];
1533 load2 f []
1536 let add_attrs bb always dc c time =
1537 let o' fmt s =
1538 Buffer.add_string bb "\n ";
1539 Printf.bprintf bb fmt s
1541 let o c fmt s = if c then o' fmt s else ignore in
1542 let ob s a b = o (always || a != b) "%s='%b'" s a
1543 and op s a b = o (always || a <> b) "%s='%b'" s (a != None)
1544 and oi s a b = o (always || a != b) "%s='%d'" s a
1545 and oI s a b = o (always || a != b) "%s='%s'" s (string_with_suffix_of_int a)
1546 and oz s a b = o (always || a <> b) "%s='%g'" s (a*.100.)
1547 and oF s a b = o (always || a <> b) "%s='%f'" s a
1548 and oL s a b = o (always || a <> b) "%s='%Ld'" s a
1549 and oc s a b = o (always || a <> b) "%s='%s'" s (color_to_string a)
1550 and oC s a b = o (always || a <> b) "%s='%s'" s (CSTE.to_string a)
1551 and oR s a b = o (always || a <> b) "%s='%s'" s (irect_to_string a)
1552 and oFm s a b = o (always || a <> b) "%s='%s'" s (FMTE.to_string a)
1553 and oSv s a b m = o (always || a <> b) "%s='%b'" s (a land m != 0)
1554 and oPm s a b = o (always || a <> b) "%s='%s'" s (MTE.to_string a)
1555 and os s a b =
1556 o (always || a <> b) "%s='%s'" s @@ Parser.enent a 0 (String.length a)
1557 and og s a b =
1558 if always || a <> b
1559 then
1560 match a with
1561 | Some (_N, _A, _B) -> o' "%s='%u,%u,%u'" s _N _A _B
1562 | None ->
1563 match b with
1564 | None -> ()
1565 | _ -> o' "%s='none'" s
1566 and oW s a b =
1567 if always || a <> b
1568 then
1569 let v =
1570 match a with
1571 | None -> "false"
1572 | Some f ->
1573 if f = infinity
1574 then "true"
1575 else string_of_float f
1577 o' "%s='%s'" s v
1578 and oco s a b =
1579 if always || a <> b
1580 then
1581 match a with
1582 | Cmulti ((n, a, b), _) when n > 1 -> o' "%s='%d,%d,%d'" s n a b
1583 | Csplit (n, _) when n > 1 -> o' "%s='%d'" s ~-n
1584 | Cmulti _ | Csplit _ | Csingle _ -> ()
1585 and obeco s a b =
1586 if always || a <> b
1587 then
1588 match a with
1589 | Some c when c > 1 -> o' "%s='%d'" s c
1590 | _ -> ()
1592 oi "width" c.cwinw dc.cwinw;
1593 oi "height" c.cwinh dc.cwinh;
1594 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
1595 oi "scroll-handle-height" c.scrollh dc.scrollh;
1596 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
1597 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
1598 ob "case-insensitive-search" c.icase dc.icase;
1599 ob "preload" c.preload dc.preload;
1600 oi "page-bias" c.pagebias dc.pagebias;
1601 oi "scroll-step" c.scrollstep dc.scrollstep;
1602 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
1603 ob "max-height-fit" c.maxhfit dc.maxhfit;
1604 ob "crop-hack" c.crophack dc.crophack;
1605 oW "throttle" c.maxwait dc.maxwait;
1606 ob "highlight-links" c.hlinks dc.hlinks;
1607 ob "under-cursor-info" c.underinfo dc.underinfo;
1608 oi "vertical-margin" c.interpagespace dc.interpagespace;
1609 oz "zoom" c.zoom dc.zoom;
1610 ob "presentation" c.presentation dc.presentation;
1611 oi "rotation-angle" c.angle dc.angle;
1612 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
1613 oFm "fit-model" c.fitmodel dc.fitmodel;
1614 oI "pixmap-cache-size" c.memlimit dc.memlimit;
1615 oi "tex-count" c.texcount dc.texcount;
1616 oi "slice-height" c.sliceheight dc.sliceheight;
1617 oi "thumbnail-width" c.thumbw dc.thumbw;
1618 ob "persistent-location" c.jumpback dc.jumpback;
1619 oc "background-color" c.bgcolor dc.bgcolor;
1620 oi "tile-width" c.tilew dc.tilew;
1621 oi "tile-height" c.tileh dc.tileh;
1622 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
1623 ob "checkers" c.checkers dc.checkers;
1624 oi "aalevel" c.aalevel dc.aalevel;
1625 ob "trim-margins" c.trimmargins dc.trimmargins;
1626 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
1627 os "uri-launcher" c.urilauncher dc.urilauncher;
1628 os "path-launcher" c.pathlauncher dc.pathlauncher;
1629 oC "color-space" c.colorspace dc.colorspace;
1630 ob "invert-colors" c.invert dc.invert;
1631 oF "brightness" c.colorscale dc.colorscale;
1632 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
1633 oco "columns" c.columns dc.columns;
1634 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
1635 os "selection-command" c.selcmd dc.selcmd;
1636 os "synctex-command" c.stcmd dc.stcmd;
1637 os "pax-command" c.paxcmd dc.paxcmd;
1638 os "askpass-command" c.passcmd dc.passcmd;
1639 os "savepath-command" c.savecmd dc.savecmd;
1640 ob "update-cursor" c.updatecurs dc.updatecurs;
1641 oi "hint-font-size" c.hfsize dc.hfsize;
1642 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
1643 oF "page-scroll-scale" c.pgscale dc.pgscale;
1644 ob "use-pbo" c.usepbo dc.usepbo;
1645 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
1646 ob "remote-in-a-new-instance" c.riani dc.riani;
1647 op "point-and-x" c.pax dc.pax;
1648 oPm "point-and-x-mark" c.paxmark dc.paxmark;
1649 ob "scroll-bar-on-the-left" c.leftscroll dc.leftscroll;
1650 if not always
1651 then os "title" c.title dc.title;
1652 oL "last-visit" (Int64.of_float time) 0L;
1653 ob "edit-annotations-inline" c.annotinline dc.annotinline;
1654 ob "coarse-presentation-positioning" c.coarseprespos dc.coarseprespos;
1655 ob "use-document-css" c.usedoccss dc.usedoccss;
1658 let keymapsbuf always dc c =
1659 let open Buffer in
1660 let bb = create 16 in
1661 let rec loop = function
1662 | [] -> ()
1663 | (modename, h) :: rest ->
1664 let dh = findkeyhash dc modename in
1665 if always || h <> dh
1666 then (
1667 if Hashtbl.length h > 0
1668 then (
1669 if length bb > 0 then add_char bb '\n';
1670 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
1671 Hashtbl.iter (fun i o ->
1672 if always || match Hashtbl.find dh i
1673 with | dO -> dO <> o | exception Not_found -> false
1674 then
1675 let addkm (k, m) =
1676 if Wsi.withctrl m then add_string bb "ctrl-";
1677 if Wsi.withalt m then add_string bb "alt-";
1678 if Wsi.withshift m then add_string bb "shift-";
1679 if Wsi.withmeta m then add_string bb "meta-";
1680 add_string bb (Wsi.keyname k);
1682 let addkms l =
1683 let rec loop = function
1684 | [] -> ()
1685 | km :: [] -> addkm km
1686 | km :: rest -> addkm km; add_char bb ' '; loop rest
1688 loop l
1690 add_string bb "<map in='";
1691 addkm i;
1692 match o with
1693 | KMinsrt km ->
1694 add_string bb "' out='"; addkm km; add_string bb "'/>\n"
1696 | KMinsrl kms ->
1697 add_string bb "' out='"; addkms kms; add_string bb "'/>\n"
1699 | KMmulti (ins, kms) ->
1700 add_char bb ' '; addkms ins; add_string bb "' out='";
1701 addkms kms; add_string bb "'/>\n"
1702 ) h;
1703 add_string bb "</keymap>";
1706 loop rest
1708 loop c.keyhashes;
1712 let keystostrlist c =
1713 let rec loop accu = function
1714 | [] -> accu
1715 | (modename, h) :: rest ->
1716 let accu =
1717 if Hashtbl.length h > 0
1718 then (
1719 let accu = Printf.sprintf "\xc2\xb7Keys for %s" modename :: accu in
1720 Hashtbl.fold (fun i o a ->
1721 let bb = Buffer.create 10 in
1722 let addkm (k, m) =
1723 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
1724 if Wsi.withalt m then Buffer.add_string bb "alt-";
1725 if Wsi.withshift m then Buffer.add_string bb "shift-";
1726 if Wsi.withmeta m then Buffer.add_string bb "meta-";
1727 Buffer.add_string bb (Wsi.keyname k);
1729 let addkms l =
1730 let rec loop = function
1731 | [] -> ()
1732 | km :: [] -> addkm km
1733 | km :: rest ->
1734 addkm km; Buffer.add_char bb ' ';
1735 loop rest
1737 loop l
1739 addkm i;
1740 Buffer.add_char bb '\t';
1741 begin match o with
1742 | KMinsrt km ->
1743 addkm km
1745 | KMinsrl kms ->
1746 addkms kms
1748 | KMmulti (ins, kms) ->
1749 Buffer.add_char bb ' ';
1750 addkms ins;
1751 Buffer.add_string bb "\t";
1752 addkms kms
1753 end;
1754 Buffer.contents bb :: a
1755 ) h accu
1757 else accu
1759 loop accu rest
1761 loop [] c.keyhashes
1764 let save1 bb leavebirdseye x h dc =
1765 let uifontsize = fstate.fontsize in
1766 let dc = if conf.bedefault then conf else dc in
1767 Buffer.add_string bb "<llppconfig>\n";
1769 if nonemptystr !fontpath
1770 then
1771 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
1772 uifontsize
1773 !fontpath
1774 else (
1775 if uifontsize <> 14
1776 then
1777 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
1780 Buffer.add_string bb "<defaults";
1781 add_attrs bb true dc dc nan;
1782 let kb = keymapsbuf true dc dc in
1783 if Buffer.length kb > 0
1784 then (
1785 Buffer.add_string bb ">\n";
1786 Buffer.add_buffer bb kb;
1787 Buffer.add_string bb "\n</defaults>\n";
1789 else Buffer.add_string bb "/>\n";
1791 let adddoc path pan anchor c bookmarks time origin =
1792 if bookmarks == [] && c = dc && anchor = emptyanchor
1793 then ()
1794 else (
1795 Printf.bprintf bb "<doc path='%s'"
1796 (Parser.enent path 0 (String.length path));
1798 if nonemptystr origin
1799 then Printf.bprintf bb "\n origin='%s'"
1800 (Parser.enent origin 0 (String.length origin));
1802 if anchor <> emptyanchor
1803 then (
1804 let n, rely, visy = anchor in
1805 Printf.bprintf bb "\n page='%d'" n;
1807 if rely > 1e-6
1808 then Printf.bprintf bb " rely='%f'" rely;
1810 if abs_float visy > 1e-6
1811 then Printf.bprintf bb " visy='%f'" visy;
1814 if pan != 0
1815 then Printf.bprintf bb " pan='%d'" pan;
1817 add_attrs bb false dc c time;
1818 if nonemptystr c.css
1819 then Printf.bprintf bb ">\n <css><![CDATA[%s]]></css>" c.css;
1820 let kb = keymapsbuf false dc c in
1822 begin match bookmarks with
1823 | [] ->
1824 if Buffer.length kb > 0
1825 then (
1826 Buffer.add_string bb ">\n";
1827 Buffer.add_buffer bb kb;
1828 Buffer.add_string bb "\n</doc>\n";
1830 else
1831 if nonemptystr c.css
1832 then Buffer.add_string bb "\n</doc>\n"
1833 else Buffer.add_string bb "/>\n"
1834 | _ ->
1835 Buffer.add_string bb ">\n<bookmarks>\n";
1836 List.iter (fun (title, _, kind) ->
1837 begin match kind with
1838 | Oanchor (page, rely, visy) ->
1839 Printf.bprintf bb
1840 "<item title='%s' page='%d'"
1841 (Parser.enent title 0 (String.length title))
1842 page
1844 if rely > 1e-6
1845 then
1846 Printf.bprintf bb " rely='%f'" rely
1848 if abs_float visy > 1e-6
1849 then
1850 Printf.bprintf bb " visy='%f'" visy
1852 | Ohistory _ | Onone | Ouri _ | Oremote _
1853 | Oremotedest _ | Olaunch _ ->
1854 failwith "unexpected link in bookmarks"
1855 end;
1856 Buffer.add_string bb "/>\n";
1857 ) bookmarks;
1858 Buffer.add_string bb "</bookmarks>";
1859 if Buffer.length kb > 0
1860 then (
1861 Buffer.add_string bb "\n";
1862 Buffer.add_buffer bb kb;
1864 Buffer.add_string bb "\n</doc>\n";
1865 end;
1869 let pan, conf =
1870 match state.mode with
1871 | Birdseye (c, pan, _, _, _) ->
1872 let beyecolumns =
1873 match conf.columns with
1874 | Cmulti ((c, _, _), _) -> Some c
1875 | Csingle _ -> None
1876 | Csplit _ -> None
1877 and columns =
1878 match c.columns with
1879 | Cmulti (c, _) -> Cmulti (c, E.a)
1880 | Csingle _ -> Csingle E.a
1881 | Csplit _ -> failwith "quit from bird's eye while split"
1883 pan, { c with beyecolumns = beyecolumns; columns = columns }
1884 | Textentry _
1885 | View
1886 | LinkNav _ -> x, conf
1888 let docpath = if nonemptystr state.path then abspath state.path else E.s in
1889 if nonemptystr docpath
1890 then (
1891 adddoc docpath pan (getanchor ())
1893 let autoscrollstep =
1894 match state.autoscroll with
1895 | Some step -> step
1896 | None -> conf.autoscrollstep
1898 begin match state.mode with
1899 | Birdseye beye -> leavebirdseye beye true
1900 | Textentry _
1901 | View
1902 | LinkNav _ -> ()
1903 end;
1904 { conf with autoscrollstep = autoscrollstep }
1906 (if conf.savebmarks then state.bookmarks else [])
1907 (now ())
1908 state.origin
1910 Hashtbl.iter (fun path (c, bookmarks, x, anchor, origin) ->
1911 if docpath <> abspath path
1912 then adddoc path x anchor c bookmarks c.lastvisit origin
1913 ) h;
1914 Buffer.add_string bb "</llppconfig>\n";
1915 true;
1918 let save leavebirdseye =
1919 let relx = float state.x /. float state.winw in
1920 let w, h, x =
1921 let cx w = truncate (relx *. float w) in
1922 List.fold_left
1923 (fun (w, h, x) ws ->
1924 match ws with
1925 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
1926 | Wsi.MaxVert -> (w, conf.cwinh, x)
1927 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
1929 (state.winw, state.winh, state.x) state.winstate
1931 conf.cwinw <- w;
1932 conf.cwinh <- h;
1933 let bb = Buffer.create 32768 in
1934 let save2 (h, dc) =
1935 save1 bb leavebirdseye x h dc
1937 if load1 save2 && Buffer.length bb > 0
1938 then
1940 let tmp = !confpath ^ ".tmp" in
1941 let oc = open_out_bin tmp in
1942 Buffer.output_buffer oc bb;
1943 close_out oc;
1944 Unix.rename tmp !confpath;
1945 with exn ->
1946 dolog "error saving configuration: %s" @@ exntos exn
1949 let gc () =
1950 let href = ref @@ Hashtbl.create 0 in
1951 let cref = ref defconf in
1952 let push (h, dc) =
1953 let f path v =
1954 if Sys.file_exists path
1955 then Some v
1956 else (dolog "removing %s" path; None) in
1957 Hashtbl.filter_map_inplace f h;
1958 href := h;
1959 cref := dc;
1960 true
1962 ignore (load1 push);
1963 let bb = Buffer.create 32768 in
1964 let save2 (_h, dc) = save1 bb (fun _ _ -> ()) 0 !href dc in
1965 if load1 save2 && Buffer.length bb > 0
1966 then (
1968 let tmp = !confpath ^ ".tmp" in
1969 let oc = open_out_bin tmp in
1970 Buffer.output_buffer oc bb;
1971 close_out oc;
1972 Unix.rename tmp !confpath;
1973 with exn ->
1974 dolog "error saving configuration: %s" @@ exntos exn
1978 let logcurrently = function
1979 | Idle -> dolog "Idle"
1980 | Loading (l, gen) ->
1981 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1982 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1983 dolog
1984 "Tiling %d[%d,%d] page=%s cs=%s angle=%d"
1985 l.pageno col row (~> pageopaque)
1986 (CSTE.to_string colorspace) angle;
1987 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1988 angle gen conf.angle state.gen
1989 tilew tileh
1990 conf.tilew conf.tileh;
1991 | Outlining _ ->
1992 dolog "outlining"