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