Cosmetics
[llpp.git] / config.ml
blob2b4e9cb38035abd1d26fc3d47721382fafbc4ebd
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 uri = string
114 and caption = string
115 and x = int
116 and y = int
117 and tilex = int
118 and tiley = int
119 and tileparams = (x * y * width * height * tilex * tiley)
120 and under =
121 | Unone
122 | Ulinkuri of string
123 | Utext of facename
124 | Uannotation of (opaque * slinkindex)
125 and slinkindex = int
126 and facename = string
127 and launchcommand = string
128 and filename = string
129 and pageno = int
130 and linkno = int
131 and destname = string
132 and mark =
133 | Mark_page
134 | Mark_block
135 | Mark_line
136 | Mark_word
137 and link =
138 | Lnotfound
139 | Lfound of int
140 and linkdir =
141 | LDfirst
142 | LDlast
143 | LDfirstvisible of (int * int * int)
144 | LDleft of int
145 | LDright of int
146 | LDdown of int
147 | LDup of int
148 and pagewithlinks =
149 | Pwlnotfound
150 | Pwl of int
151 and scrollb = int
152 and anchor = pageno * top * dtop
153 and rect = float * float * float * float * float * float * float * float
154 and infochange = | Memused | Docinfo | Pdim
157 class type uioh =
158 object
159 method display : unit
160 method key : int -> int -> uioh
161 method button : int -> bool -> int -> int -> int -> uioh
162 method multiclick : int -> int -> int -> int -> uioh
163 method motion : int -> int -> uioh
164 method pmotion : int -> int -> uioh
165 method infochanged : infochange -> unit
166 method scrollpw : (int * float * float)
167 method scrollph : (int * float * float)
168 method modehash : keyhash
169 method eformsgs : bool
170 method alwaysscrolly : bool
171 end;;
173 module type TextEnumType =
175 type t
176 val name : string
177 val names : string array
178 end;;
180 module TextEnumMake (Ten : TextEnumType) =
181 struct
182 let names = Ten.names;;
183 let to_int (t : Ten.t) = Obj.magic t;;
184 let to_string t = names.(to_int t);;
185 let of_int n : Ten.t = Obj.magic n;;
186 let of_string s =
187 let rec find i =
188 if i = Array.length names
189 then failwith ("invalid " ^ Ten.name ^ ": " ^ s)
190 else (
191 if Ten.names.(i) = s
192 then of_int i
193 else find (i+1)
195 in find 0;;
196 end;;
198 module CSTE = TextEnumMake (struct
199 type t = colorspace;;
200 let name = "colorspace";;
201 let names = [|"rgb"; "bgr"; "gray"|];;
202 end);;
204 module MTE = TextEnumMake (struct
205 type t = mark;;
206 let name = "mark";;
207 let names = [|"page"; "block"; "line"; "word"|];;
208 end);;
210 module FMTE = TextEnumMake (struct
211 type t = fitmodel;;
212 let name = "fitmodel";;
213 let names = [|"width"; "proportional"; "page"|];;
214 end);;
216 type conf =
217 { mutable scrollbw : int
218 ; mutable scrollh : int
219 ; mutable scrollb : scrollb
220 ; mutable icase : bool
221 ; mutable preload : bool
222 ; mutable pagebias : int
223 ; mutable verbose : bool
224 ; mutable debug : bool
225 ; mutable scrollstep : int
226 ; mutable hscrollstep : int
227 ; mutable maxhfit : bool
228 ; mutable crophack : bool
229 ; mutable autoscrollstep : int
230 ; mutable maxwait : float option
231 ; mutable hlinks : bool
232 ; mutable underinfo : bool
233 ; mutable interpagespace : interpagespace
234 ; mutable zoom : float
235 ; mutable presentation : bool
236 ; mutable angle : angle
237 ; mutable cwinw : int
238 ; mutable cwinh : int
239 ; mutable savebmarks : bool
240 ; mutable fitmodel : fitmodel
241 ; mutable trimmargins : trimmargins
242 ; mutable trimfuzz : irect
243 ; mutable memlimit : memsize
244 ; mutable texcount : texcount
245 ; mutable sliceheight : sliceheight
246 ; mutable thumbw : width
247 ; mutable jumpback : bool
248 ; mutable bgcolor : (float * float * float)
249 ; mutable bedefault : bool
250 ; mutable tilew : int
251 ; mutable tileh : int
252 ; mutable mustoresize : memsize
253 ; mutable checkers : bool
254 ; mutable aalevel : int
255 ; mutable urilauncher : string
256 ; mutable pathlauncher : string
257 ; mutable colorspace : colorspace
258 ; mutable invert : bool
259 ; mutable colorscale : float
260 ; mutable ghyllscroll : (int * int * int) option
261 ; mutable columns : columns
262 ; mutable beyecolumns : columncount option
263 ; mutable selcmd : string
264 ; mutable paxcmd : string
265 ; mutable passcmd : string
266 ; mutable savecmd : string
267 ; mutable updatecurs : bool
268 ; mutable keyhashes : (string * keyhash) list
269 ; mutable hfsize : int
270 ; mutable pgscale : float
271 ; mutable usepbo : bool
272 ; mutable wheelbypage : bool
273 ; mutable stcmd : string
274 ; mutable riani : bool
275 ; mutable pax : (float * int * int) ref option
276 ; mutable paxmark : mark
277 ; mutable leftscroll : bool
278 ; mutable title : string
279 ; mutable lastvisit : float
280 ; mutable annotinline : bool
281 ; mutable coarseprespos : bool
282 ; mutable css : css
284 and columns =
285 | Csingle of singlecolumn
286 | Cmulti of multicolumns
287 | Csplit of splitcolumns
288 and outlinekind =
289 | Onone
290 | Oanchor of anchor
291 | Ouri of uri
292 | Olaunch of launchcommand
293 | Oremote of (filename * pageno)
294 | Oremotedest of (filename * destname)
295 | Ohistory of (filename * conf * outline list * x * anchor * filename)
296 and outline = (caption * outlinelevel * outlinekind)
297 and outlinelevel = int
300 type page =
301 { pageno : int
302 ; pagedimno : int
303 ; pagew : int
304 ; pageh : int
305 ; pagex : int
306 ; pagey : int
307 ; pagevw : int
308 ; pagevh : int
309 ; pagedispx : int
310 ; pagedispy : int
311 ; pagecol : int
315 type tile = opaque * pixmapsize * elapsed
316 and elapsed = float;;
317 type pagemapkey = pageno * gen;;
318 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
319 and row = int
320 and col = int
321 and currently =
322 | Idle
323 | Loading of (page * gen)
324 | Tiling of (
325 page * opaque * colorspace * angle * gen * col * row * width * height
327 | Outlining of outline list
330 type mpos = int * int
331 and mstate =
332 | Msel of (mpos * mpos)
333 | Mpan of mpos
334 | Mscrolly | Mscrollx
335 | Mzoom of (buttonno * step * mpos)
336 | Mzoomrect of (mpos * mpos)
337 | Mnone
338 and buttonno = int
339 and step = int
342 type mode =
343 | Birdseye of (conf * leftx * pageno * pageno * anchor)
344 | Textentry of (textentry * onleave)
345 | View
346 | LinkNav of linktarget
347 and onleave = leavetextentrystatus -> unit
348 and leavetextentrystatus = | Cancel | Confirm
349 and helpitem = string * int * action
350 and action =
351 | Noaction
352 | Action of (uioh -> uioh)
353 and linktarget =
354 | Ltexact of (pageno * direction)
355 | Ltgendir of direction
356 | Ltnotready of (pageno * direction)
357 and direction = int (* -1, 0, 1 *)
358 and textentry = string * string * onhist option * onkey * ondone * cancelonempty
359 and onkey = string -> int -> te
360 and ondone = string -> unit
361 and histcancel = unit -> unit
362 and onhist = ((histcmd -> string) * histcancel)
363 and histcmd = HCnext | HCprev | HCfirst | HClast
364 and cancelonempty = bool
365 and te =
366 | TEstop
367 | TEdone of string
368 | TEcont of string
369 | TEswitch of textentry
372 type 'a circbuf =
373 { store : 'a array
374 ; mutable rc : int
375 ; mutable wc : int
376 ; mutable len : int
380 type state =
381 { mutable ss : Unix.file_descr
382 ; mutable wsfd : Unix.file_descr
383 ; mutable stderr : Unix.file_descr
384 ; mutable errmsgs : Buffer.t
385 ; mutable newerrmsgs : bool
386 ; mutable w : int
387 ; mutable x : x
388 ; mutable y : y
389 ; mutable anchor : anchor
390 ; mutable ranchors : (string * string * anchor * string) list
391 ; mutable maxy : int
392 ; mutable layout : page list
393 ; pagemap : (pagemapkey, opaque) Hashtbl.t
394 ; tilemap : (tilemapkey, tile) Hashtbl.t
395 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
396 ; mutable pdims : (pageno * width * height * leftx) list
397 ; mutable pagecount : int
398 ; mutable currently : currently
399 ; mutable mstate : mstate
400 ; mutable searchpattern : string
401 ; mutable rects : (pageno * rectcolor * rect) list
402 ; mutable rects1 : (pageno * rectcolor * rect) list
403 ; prects : (pageno, float array) Hashtbl.t
404 ; mutable text : string
405 ; mutable winstate : Wsi.winstate list
406 ; mutable mode : mode
407 ; mutable uioh : uioh
408 ; mutable outlines : outline array
409 ; mutable bookmarks : outline list
410 ; mutable path : string
411 ; mutable password : string
412 ; mutable nameddest : string
413 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
414 ; mutable memused : memsize
415 ; mutable gen : gen
416 ; mutable throttle : (page list * int * float) option
417 ; mutable autoscroll : int option
418 ; mutable ghyll : (int option -> unit)
419 ; mutable help : helpitem array
420 ; mutable docinfo : (int * string) list
421 ; mutable checkerstexid : GlTex.texture_id option
422 ; hists : hists
423 ; mutable prevzoom : (float * int)
424 ; mutable progress : float
425 ; mutable redisplay : bool
426 ; mutable mpos : mpos
427 ; mutable keystate : keystate
428 ; mutable glinks : bool
429 ; mutable prevcolumns : (columns * float) option
430 ; mutable winw : int
431 ; mutable winh : int
432 ; mutable reprf : (unit -> unit)
433 ; mutable origin : string
434 ; mutable roam : (unit -> unit)
435 ; mutable bzoom : bool
436 ; mutable traw : [`float] Raw.t
437 ; mutable vraw : [`float] Raw.t
438 ; mutable lnava : (pageno * linkno) option
440 and hists =
441 { pat : string circbuf
442 ; pag : string circbuf
443 ; nav : anchor circbuf
444 ; sel : string circbuf
448 let emptyanchor = (0, 0.0, 0.0);;
449 let emptykeyhash = Hashtbl.create 0;;
450 let noghyll _ = ();;
451 let noreprf () = ();;
452 let noroam () = ();;
454 let nouioh : uioh = object (self)
455 method display = ()
456 method key _ _ = self
457 method multiclick _ _ _ _ = self
458 method button _ _ _ _ _ = self
459 method motion _ _ = self
460 method pmotion _ _ = self
461 method infochanged _ = ()
462 method scrollpw = (0, nan, nan)
463 method scrollph = (0, nan, nan)
464 method modehash = emptykeyhash
465 method eformsgs = false
466 method alwaysscrolly = false
467 end;;
469 let platform_to_string = function
470 | Punknown -> "unknown"
471 | Plinux -> "Linux"
472 | Posx -> "OSX"
473 | Psun -> "Sun"
474 | Pbsd -> "BSD"
475 | Pcygwin -> "Cygwin"
478 let version () =
479 Printf.sprintf "llpp version %s, fitz %s, ocaml %s/%d bit"
480 Help.version (fz_version ()) Sys.ocaml_version Sys.word_size
483 let defconf =
484 { scrollbw = 7
485 ; scrollh = 12
486 ; scrollb = scrollbhv lor scrollbvv
487 ; icase = true
488 ; preload = true
489 ; pagebias = 0
490 ; verbose = false
491 ; debug = false
492 ; scrollstep = 24
493 ; hscrollstep = 24
494 ; maxhfit = true
495 ; crophack = false
496 ; autoscrollstep = 2
497 ; maxwait = None
498 ; hlinks = false
499 ; underinfo = false
500 ; interpagespace = 2
501 ; zoom = 1.0
502 ; presentation = false
503 ; angle = 0
504 ; cwinw = 900
505 ; cwinh = 900
506 ; savebmarks = true
507 ; fitmodel = FitProportional
508 ; trimmargins = false
509 ; trimfuzz = (0,0,0,0)
510 ; memlimit = 32 lsl 20
511 ; texcount = 256
512 ; sliceheight = 24
513 ; thumbw = 76
514 ; jumpback = true
515 ; bgcolor = (0.5, 0.5, 0.5)
516 ; bedefault = false
517 ; tilew = 2048
518 ; tileh = 2048
519 ; mustoresize = 256 lsl 20
520 ; checkers = true
521 ; aalevel = 8
522 ; urilauncher =
523 (match platform with
524 | Plinux | Psun | Pbsd -> "xdg-open \"%s\""
525 | Posx -> "open \"%s\""
526 | Pcygwin -> "cygstart \"%s\""
527 | Punknown -> "echo %s")
528 ; pathlauncher = "lp \"%s\""
529 ; selcmd =
530 (match platform with
531 | Plinux | Pbsd | Psun -> "xsel -i"
532 | Posx -> "pbcopy"
533 | Pcygwin -> "wsel"
534 | Punknown -> "cat")
535 ; paxcmd = "cat"
536 ; passcmd = E.s
537 ; savecmd = E.s
538 ; colorspace = Rgb
539 ; invert = false
540 ; colorscale = 1.0
541 ; ghyllscroll = None
542 ; columns = Csingle [||]
543 ; beyecolumns = None
544 ; updatecurs = false
545 ; hfsize = 12
546 ; pgscale = 1.0
547 ; usepbo = false
548 ; wheelbypage = false
549 ; stcmd = "echo SyncTex"
550 ; riani = false
551 ; pax = None
552 ; paxmark = Mark_word
553 ; leftscroll = false
554 ; title = E.s
555 ; lastvisit = 0.0
556 ; annotinline = true
557 ; coarseprespos = false
558 ; css = E.s
559 ; keyhashes =
560 let mk n = (n, Hashtbl.create 1) in
561 [ mk "global"
562 ; mk "info"
563 ; mk "help"
564 ; mk "outline"
565 ; mk "listview"
566 ; mk "birdseye"
567 ; mk "textentry"
568 ; mk "links"
569 ; mk "view"
574 let conf = { defconf with angle = defconf.angle };;
576 let gotourl url =
577 let command = Str.global_replace percentsre url conf.urilauncher in
578 try ignore @@ spawn command []
579 with exn -> dolog "failed to execute `%s': %s" command @@ exntos exn
582 let gotouri uri =
583 if emptystr conf.urilauncher
584 then dolog "%s" uri
585 else (
586 let url = geturl uri in
587 if emptystr url
588 then dolog "obtained empty url from uri %S" uri
589 else gotourl url
593 let makehelp () =
594 let strings =
595 version ()
596 :: "(searching in this text works just by typing (i.e. no initial '/'))"
597 :: E.s :: Help.keys
599 Array.of_list (
600 List.map (fun s ->
601 let url = geturl s in
602 if nonemptystr url
603 then (s, 0, Action (fun uioh -> gotourl url; uioh))
604 else (s, 0, Noaction)
605 ) strings);
608 let cbnew n v =
609 { store = Array.make n v
610 ; rc = 0
611 ; wc = 0
612 ; len = 0
616 let cbcap b = Array.length b.store;;
618 let cbput b v =
619 let cap = cbcap b in
620 b.store.(b.wc) <- v;
621 b.wc <- (b.wc + 1) mod cap;
622 b.rc <- b.wc;
623 b.len <- min (b.len + 1) cap;
626 let cbempty b = b.len = 0;;
628 let cbgetg b circular dir =
629 if cbempty b
630 then b.store.(0)
631 else
632 let rc = b.rc + dir in
633 let rc =
634 if circular
635 then (
636 if rc = -1
637 then b.len-1
638 else (
639 if rc >= b.len
640 then 0
641 else rc
644 else bound rc 0 (b.len-1)
646 b.rc <- rc;
647 b.store.(rc);
650 let cbget b = cbgetg b false;;
651 let cbgetc b = cbgetg b true;;
653 let state =
654 { ss = Unix.stdin
655 ; wsfd = Unix.stdin
656 ; stderr = Unix.stderr
657 ; errmsgs = Buffer.create 0
658 ; newerrmsgs = false
659 ; x = 0
660 ; y = 0
661 ; w = 0
662 ; anchor = emptyanchor
663 ; ranchors = []
664 ; layout = []
665 ; maxy = max_int
666 ; tilelru = Queue.create ()
667 ; pagemap = Hashtbl.create 10
668 ; tilemap = Hashtbl.create 10
669 ; pdims = []
670 ; pagecount = 0
671 ; currently = Idle
672 ; mstate = Mnone
673 ; rects = []
674 ; rects1 = []
675 ; prects = Hashtbl.create 1
676 ; text = E.s
677 ; mode = View
678 ; winstate = []
679 ; searchpattern = E.s
680 ; outlines = E.a
681 ; bookmarks = []
682 ; path = E.s
683 ; password = E.s
684 ; nameddest = E.s
685 ; geomcmds = E.s, []
686 ; hists =
687 { nav = cbnew 10 emptyanchor
688 ; pat = cbnew 10 E.s
689 ; pag = cbnew 10 E.s
690 ; sel = cbnew 10 E.s
692 ; memused = 0
693 ; gen = 0
694 ; throttle = None
695 ; autoscroll = None
696 ; ghyll = noghyll
697 ; help = makehelp ()
698 ; docinfo = []
699 ; checkerstexid = None
700 ; prevzoom = (1.0, 0)
701 ; progress = -1.0
702 ; uioh = nouioh
703 ; redisplay = true
704 ; mpos = (-1, -1)
705 ; keystate = KSnone
706 ; glinks = false
707 ; prevcolumns = None
708 ; winw = -1
709 ; winh = -1
710 ; reprf = noreprf
711 ; origin = E.s
712 ; roam = noroam
713 ; bzoom = false
714 ; traw = Raw.create_static `float ~len:8
715 ; vraw = Raw.create_static `float ~len:8
716 ; lnava = None
720 let copykeyhashes c =
721 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
724 let calcips h =
725 let d = state.winh - h in
726 max conf.interpagespace ((d + 1) / 2)
729 let rowyh (c, coverA, coverB) b n =
730 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
731 then
732 let _, _, vy, (_, _, h, _) = b.(n) in
733 (vy, h)
734 else
735 let n' = n - coverA in
736 let d = n' mod c in
737 let s = n - d in
738 let e = min state.pagecount (s + c) in
739 let rec find m miny maxh = if m = e then miny, maxh else
740 let _, _, y, (_, _, h, _) = b.(m) in
741 let miny = min miny y in
742 let maxh = max maxh h in
743 find (m+1) miny maxh
744 in find s max_int 0
747 let page_of_y y =
748 let ((c, coverA, coverB) as cl), b =
749 match conf.columns with
750 | Csingle b -> (1, 0, 0), b
751 | Cmulti (c, b) -> c, b
752 | Csplit (_, b) -> (1, 0, 0), b
754 if Array.length b = 0
755 then -1
756 else
757 let rec bsearch nmin nmax =
758 if nmin > nmax
759 then bound nmin 0 (state.pagecount-1)
760 else
761 let n = (nmax + nmin) / 2 in
762 let vy, h = rowyh cl b n in
763 let y0, y1 =
764 if conf.presentation
765 then
766 let ips = calcips h in
767 let y0 = vy - ips in
768 let y1 = vy + h + ips in
769 y0, y1
770 else (
771 if n = 0
772 then 0, vy + h + conf.interpagespace
773 else
774 let y0 = vy - conf.interpagespace in
775 y0, y0 + h + conf.interpagespace
778 if y >= y0 && y < y1
779 then (
780 if c = 1
781 then n
782 else (
783 if n > coverA
784 then
785 if n < state.pagecount - coverB
786 then ((n-coverA)/c)*c + coverA
787 else n
788 else n
791 else (
792 if y > y0
793 then bsearch (n+1) nmax
794 else bsearch nmin (n-1)
797 bsearch 0 (state.pagecount-1);
800 let calcheight () =
801 match conf.columns with
802 | Cmulti ((_, _, _) as cl, b) ->
803 if Array.length b > 0
804 then
805 let y, h = rowyh cl b (Array.length b - 1) in
806 y + h + (if conf.presentation then calcips h else 0)
807 else 0
808 | Csingle b ->
809 if Array.length b > 0
810 then
811 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
812 y + h + (if conf.presentation then calcips h else 0)
813 else 0
814 | Csplit (_, b) ->
815 if Array.length b > 0
816 then
817 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
818 y + h
819 else 0
822 let getpageywh pageno =
823 let pageno = bound pageno 0 (state.pagecount-1) in
824 match conf.columns with
825 | Csingle b ->
826 if Array.length b = 0
827 then 0, 0, 0
828 else
829 let (_, _, y, (_, w, h, _)) = b.(pageno) in
830 let y =
831 if conf.presentation
832 then y - calcips h
833 else y
835 y, w, h
836 | Cmulti (cl, b) ->
837 if Array.length b = 0
838 then 0, 0, 0
839 else
840 let y, h = rowyh cl b pageno in
841 let (_, _, _, (_, w, _, _)) = b.(pageno) in
842 let y =
843 if conf.presentation
844 then y - calcips h
845 else y
847 y, w, h
848 | Csplit (c, b) ->
849 if Array.length b = 0
850 then 0, 0, 0
851 else
852 let n = pageno*c in
853 let (_, _, y, (_, w, h, _)) = b.(n) in
854 y, w / c, h
857 let getpageyh pageno =
858 let y,_,h = getpageywh pageno in
859 y, h;
862 let getpagedim pageno =
863 let rec f ppdim l =
864 match l with
865 | (n, _, _, _) as pdim :: rest ->
866 if n >= pageno
867 then (if n = pageno then pdim else ppdim)
868 else f pdim rest
870 | [] -> ppdim
872 f (-1, -1, -1, -1) state.pdims
875 let getpdimno pageno =
876 let rec f p l =
877 let np = succ p in
878 match l with
879 | (n, _, _, _) :: rest ->
880 if n >= pageno
881 then (if n = pageno then np else p)
882 else f np rest
884 | [] -> p
886 f ~-1 state.pdims
889 let getpagey pageno = fst (getpageyh pageno);;
891 let getanchor1 l =
892 let top =
893 let coloff = l.pagecol * l.pageh in
894 float (l.pagey + coloff) /. float l.pageh
896 let dtop =
897 if l.pagedispy = 0
898 then
900 else (
901 if conf.presentation
902 then float l.pagedispy /. float (calcips l.pageh)
903 else float l.pagedispy /. float conf.interpagespace
906 (l.pageno, top, dtop)
909 let getanchor () =
910 match state.layout with
911 | l :: _ -> getanchor1 l
912 | [] ->
913 let n = page_of_y state.y in
914 if n = -1
915 then state.anchor
916 else
917 let y, h = getpageyh n in
918 let dy = y - state.y in
919 let dtop =
920 if conf.presentation
921 then
922 let ips = calcips h in
923 float (dy + ips) /. float ips
924 else
925 float dy /. float conf.interpagespace
927 (n, 0.0, dtop)
930 let fontpath = ref E.s;;
932 type historder = [ `lastvisit | `title | `path | `file ];;
934 module KeyMap =
935 Map.Make (struct type t = (int * int) let compare = compare end);;
937 let unentS s =
938 let l = String.length s in
939 let b = Buffer.create l in
940 Parser.unent b s 0 l;
941 Buffer.contents b;
944 let home =
945 try Sys.getenv "HOME"
946 with exn ->
947 dolog "cannot determine home directory location: %s" @@ exntos exn;
951 let modifier_of_string = function
952 | "alt" -> Wsi.altmask
953 | "shift" -> Wsi.shiftmask
954 | "ctrl" | "control" -> Wsi.ctrlmask
955 | "meta" -> Wsi.metamask
956 | _ -> 0
959 let keys_of_string s =
960 let key_of_string r s =
961 let elems = Str.full_split r s in
962 let f n k m =
963 let g s =
964 let m1 = modifier_of_string s in
965 if m1 = 0
966 then (Wsi.namekey s, m)
967 else (k, m lor m1)
968 in function
969 | Str.Delim s when n land 1 = 0 -> g s
970 | Str.Text s -> g s
971 | Str.Delim _ -> (k, m)
973 let rec loop n k m = function
974 | [] -> (k, m)
975 | x :: xs ->
976 let k, m = f n k m x in
977 loop (n+1) k m xs
979 loop 0 0 0 elems
981 let elems = Str.split whitere s in
982 List.map (key_of_string (Str.regexp "-")) elems
985 let config_of c attrs =
986 let apply c k v =
988 match k with
989 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
990 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
991 | "case-insensitive-search" -> { c with icase = bool_of_string v }
992 | "preload" -> { c with preload = bool_of_string v }
993 | "page-bias" -> { c with pagebias = int_of_string v }
994 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
995 | "horizontal-scroll-step" ->
996 { c with hscrollstep = max (int_of_string v) 1 }
997 | "auto-scroll-step" ->
998 { c with autoscrollstep = max 0 (int_of_string v) }
999 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
1000 | "crop-hack" -> { c with crophack = bool_of_string v }
1001 | "throttle" ->
1002 let mw =
1003 match String.map asciilower v with
1004 | "true" -> Some infinity
1005 | "false" -> None
1006 | f -> Some (float_of_string f)
1008 { c with maxwait = mw }
1009 | "highlight-links" -> { c with hlinks = bool_of_string v }
1010 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
1011 | "vertical-margin" ->
1012 { c with interpagespace = max 0 (int_of_string v) }
1013 | "zoom" ->
1014 let zoom = float_of_string v /. 100. in
1015 let zoom = max zoom 0.0 in
1016 { c with zoom = zoom }
1017 | "presentation" -> { c with presentation = bool_of_string v }
1018 | "rotation-angle" -> { c with angle = int_of_string v }
1019 | "width" -> { c with cwinw = max 20 (int_of_string v) }
1020 | "height" -> { c with cwinh = max 20 (int_of_string v) }
1021 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
1022 | "proportional-display" ->
1023 let fm =
1024 if bool_of_string v
1025 then FitProportional
1026 else FitWidth
1028 { c with fitmodel = fm }
1029 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
1030 | "pixmap-cache-size" ->
1031 { c with memlimit = max 2 (int_of_string_with_suffix v) }
1032 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
1033 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
1034 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
1035 | "persistent-location" -> { c with jumpback = bool_of_string v }
1036 | "background-color" -> { c with bgcolor = color_of_string v }
1037 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
1038 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
1039 | "mupdf-store-size" ->
1040 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
1041 | "checkers" -> { c with checkers = bool_of_string v }
1042 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
1043 | "trim-margins" -> { c with trimmargins = bool_of_string v }
1044 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
1045 | "uri-launcher" -> { c with urilauncher = unentS v }
1046 | "path-launcher" -> { c with pathlauncher = unentS v }
1047 | "color-space" -> { c with colorspace = CSTE.of_string v }
1048 | "invert-colors" -> { c with invert = bool_of_string v }
1049 | "brightness" -> { c with colorscale = float_of_string v }
1050 | "ghyllscroll" -> { c with ghyllscroll = ghyllscroll_of_string v }
1051 | "columns" ->
1052 let (n, _, _) as nab = multicolumns_of_string v in
1053 if n < 0
1054 then { c with columns = Csplit (-n, E.a) }
1055 else { c with columns = Cmulti (nab, E.a) }
1056 | "birds-eye-columns" ->
1057 { c with beyecolumns = Some (max (int_of_string v) 2) }
1058 | "selection-command" -> { c with selcmd = unentS v }
1059 | "synctex-command" -> { c with stcmd = unentS v }
1060 | "pax-command" -> { c with paxcmd = unentS v }
1061 | "askpass-command" -> { c with passcmd = unentS v }
1062 | "savepath-command" -> { c with savecmd = unentS v }
1063 | "update-cursor" -> { c with updatecurs = bool_of_string v }
1064 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
1065 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
1066 | "use-pbo" -> { c with usepbo = bool_of_string v }
1067 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
1068 | "horizontal-scrollbar-visible" ->
1069 let b =
1070 if bool_of_string v
1071 then c.scrollb lor scrollbhv
1072 else c.scrollb land (lnot scrollbhv)
1074 { c with scrollb = b }
1075 | "vertical-scrollbar-visible" ->
1076 let b =
1077 if bool_of_string v
1078 then c.scrollb lor scrollbvv
1079 else c.scrollb land (lnot scrollbvv)
1081 { c with scrollb = b }
1082 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
1083 | "point-and-x" ->
1084 { c with pax =
1085 if bool_of_string v
1086 then Some (ref (0.0, 0, 0))
1087 else None }
1088 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
1089 | "scroll-bar-on-the-left" -> { c with leftscroll = bool_of_string v }
1090 | "title" -> { c with title = unentS v }
1091 | "last-visit" -> { c with lastvisit = float_of_string v }
1092 | "edit-annotations-inline" -> { c with annotinline = bool_of_string v }
1093 | "coarse-presentation-positioning" ->
1094 { c with coarseprespos = bool_of_string v }
1095 | _ -> c
1096 with exn ->
1097 dolog "error processing attribute (`%S' = `%S'): %s" k v @@ exntos exn;
1100 let rec fold c = function
1101 | [] -> c
1102 | (k, v) :: rest ->
1103 let c = apply c k v in
1104 fold c rest
1106 fold { c with keyhashes = copykeyhashes c } attrs;
1109 let fromstring f pos n v d =
1110 try f v
1111 with exn ->
1112 dolog "error processing attribute (%S=%S) at %d\n%s" n v pos @@ exntos exn;
1116 let bookmark_of attrs =
1117 let rec fold title page rely visy = function
1118 | ("title", v) :: rest -> fold v page rely visy rest
1119 | ("page", v) :: rest -> fold title v rely visy rest
1120 | ("rely", v) :: rest -> fold title page v visy rest
1121 | ("visy", v) :: rest -> fold title page rely v rest
1122 | _ :: rest -> fold title page rely visy rest
1123 | [] -> title, page, rely, visy
1125 fold "invalid" "0" "0" "0" attrs
1128 let doc_of attrs =
1129 let rec fold path page rely pan visy origin = function
1130 | ("path", v) :: rest -> fold v page rely pan visy origin rest
1131 | ("page", v) :: rest -> fold path v rely pan visy origin rest
1132 | ("rely", v) :: rest -> fold path page v pan visy origin rest
1133 | ("pan", v) :: rest -> fold path page rely v visy origin rest
1134 | ("visy", v) :: rest -> fold path page rely pan v origin rest
1135 | ("origin", v) :: rest -> fold path page rely pan visy v rest
1136 | _ :: rest -> fold path page rely pan visy origin rest
1137 | [] -> path, page, rely, pan, visy, origin
1139 fold E.s "0" "0" "0" "0" E.s attrs
1142 let map_of attrs =
1143 let rec fold rs ls = function
1144 | ("out", v) :: rest -> fold v ls rest
1145 | ("in", v) :: rest -> fold rs v rest
1146 | _ :: rest -> fold ls rs rest
1147 | [] -> ls, rs
1149 fold E.s E.s attrs
1152 let setconf dst src =
1153 dst.scrollbw <- src.scrollbw;
1154 dst.scrollh <- src.scrollh;
1155 dst.icase <- src.icase;
1156 dst.preload <- src.preload;
1157 dst.pagebias <- src.pagebias;
1158 dst.verbose <- src.verbose;
1159 dst.scrollstep <- src.scrollstep;
1160 dst.maxhfit <- src.maxhfit;
1161 dst.crophack <- src.crophack;
1162 dst.autoscrollstep <- src.autoscrollstep;
1163 dst.maxwait <- src.maxwait;
1164 dst.hlinks <- src.hlinks;
1165 dst.underinfo <- src.underinfo;
1166 dst.interpagespace <- src.interpagespace;
1167 dst.zoom <- src.zoom;
1168 dst.presentation <- src.presentation;
1169 dst.angle <- src.angle;
1170 dst.cwinw <- src.cwinw;
1171 dst.cwinh <- src.cwinh;
1172 dst.savebmarks <- src.savebmarks;
1173 dst.memlimit <- src.memlimit;
1174 dst.fitmodel <- src.fitmodel;
1175 dst.texcount <- src.texcount;
1176 dst.sliceheight <- src.sliceheight;
1177 dst.thumbw <- src.thumbw;
1178 dst.jumpback <- src.jumpback;
1179 dst.bgcolor <- src.bgcolor;
1180 dst.tilew <- src.tilew;
1181 dst.tileh <- src.tileh;
1182 dst.mustoresize <- src.mustoresize;
1183 dst.checkers <- src.checkers;
1184 dst.aalevel <- src.aalevel;
1185 dst.trimmargins <- src.trimmargins;
1186 dst.trimfuzz <- src.trimfuzz;
1187 dst.urilauncher <- src.urilauncher;
1188 dst.colorspace <- src.colorspace;
1189 dst.invert <- src.invert;
1190 dst.colorscale <- src.colorscale;
1191 dst.ghyllscroll <- src.ghyllscroll;
1192 dst.columns <- src.columns;
1193 dst.beyecolumns <- src.beyecolumns;
1194 dst.selcmd <- src.selcmd;
1195 dst.updatecurs <- src.updatecurs;
1196 dst.pathlauncher <- src.pathlauncher;
1197 dst.keyhashes <- copykeyhashes src;
1198 dst.hfsize <- src.hfsize;
1199 dst.hscrollstep <- src.hscrollstep;
1200 dst.pgscale <- src.pgscale;
1201 dst.usepbo <- src.usepbo;
1202 dst.wheelbypage <- src.wheelbypage;
1203 dst.stcmd <- src.stcmd;
1204 dst.paxcmd <- src.paxcmd;
1205 dst.passcmd <- src.passcmd;
1206 dst.savecmd <- src.savecmd;
1207 dst.scrollb <- src.scrollb;
1208 dst.riani <- src.riani;
1209 dst.paxmark <- src.paxmark;
1210 dst.leftscroll <- src.leftscroll;
1211 dst.title <- src.title;
1212 dst.annotinline <- src.annotinline;
1213 dst.coarseprespos <- src.coarseprespos;
1214 dst.css <- src.css;
1215 dst.pax <-
1216 if src.pax = None
1217 then None
1218 else Some ((ref (0.0, 0, 0)));
1221 let findkeyhash c name =
1222 try List.assoc name c.keyhashes
1223 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
1226 let get s =
1227 let open Parser in
1228 let h = Hashtbl.create 10 in
1229 let dc = { defconf with angle = defconf.angle } in
1230 let rec toplevel v t spos _ =
1231 match t with
1232 | Vdata | Vcdata | Vend -> v
1233 | Vopen ("llppconfig", _, closed) ->
1234 if closed
1235 then v
1236 else { v with f = llppconfig }
1237 | Vopen _ -> parse_error "unexpected subelement at top level" s spos
1238 | Vclose _ -> parse_error "unexpected close at top level" s spos
1240 and llppconfig v t spos _ =
1241 match t with
1242 | Vdata | Vcdata -> v
1243 | Vend -> parse_error "unexpected end of input in llppconfig" s spos
1244 | Vopen ("defaults", attrs, closed) ->
1245 let c = config_of dc attrs in
1246 setconf dc c;
1247 if closed
1248 then v
1249 else { v with f = defaults }
1251 | Vopen ("ui-font", attrs, closed) ->
1252 let rec getsize size = function
1253 | [] -> size
1254 | ("size", v) :: rest ->
1255 let size =
1256 fromstring int_of_string spos "size" v fstate.fontsize in
1257 getsize size rest
1258 | l -> getsize size l
1260 fstate.fontsize <- getsize fstate.fontsize attrs;
1261 if closed
1262 then v
1263 else { v with f = uifont (Buffer.create 10) }
1265 | Vopen ("doc", attrs, closed) ->
1266 let pathent, spage, srely, span, svisy, origin = doc_of attrs in
1267 let path = unentS pathent
1268 and origin = unentS origin
1269 and pageno = fromstring int_of_string spos "page" spage 0
1270 and rely = fromstring float_of_string spos "rely" srely 0.0
1271 and pan = fromstring int_of_string spos "pan" span 0
1272 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1273 let c = config_of dc attrs in
1274 let anchor = (pageno, rely, visy) in
1275 if closed
1276 then (Hashtbl.add h path (c, [], pan, anchor, origin); v)
1277 else { v with f = doc path origin pan anchor c [] }
1279 | Vopen _ ->
1280 parse_error "unexpected subelement in llppconfig" s spos
1282 | Vclose "llppconfig" -> { v with f = toplevel }
1283 | Vclose _ -> parse_error "unexpected close in llppconfig" s spos
1285 and defaults v t spos _ =
1286 match t with
1287 | Vdata | Vcdata -> v
1288 | Vend -> parse_error "unexpected end of input in defaults" s spos
1289 | Vopen ("keymap", attrs, closed) ->
1290 let modename =
1291 try List.assoc "mode" attrs
1292 with Not_found -> "global" in
1293 if closed
1294 then v
1295 else
1296 let ret keymap =
1297 let h = findkeyhash dc modename in
1298 KeyMap.iter (Hashtbl.replace h) keymap;
1299 defaults
1301 { v with f = pkeymap ret KeyMap.empty }
1303 | Vopen (_, _, _) ->
1304 parse_error "unexpected subelement in defaults" s spos
1306 | Vclose "defaults" ->
1307 { v with f = llppconfig }
1309 | Vclose _ -> parse_error "unexpected close in defaults" s spos
1311 and uifont b v t spos epos =
1312 match t with
1313 | Vdata | Vcdata ->
1314 Buffer.add_substring b s spos (epos - spos);
1316 | Vopen (_, _, _) ->
1317 parse_error "unexpected subelement in ui-font" s spos
1318 | Vclose "ui-font" ->
1319 if emptystr !fontpath
1320 then fontpath := Buffer.contents b;
1321 { v with f = llppconfig }
1322 | Vclose _ -> parse_error "unexpected close in ui-font" s spos
1323 | Vend -> parse_error "unexpected end of input in ui-font" s spos
1325 and doc path origin pan anchor c bookmarks v t spos _ =
1326 match t with
1327 | Vdata | Vcdata -> v
1328 | Vend -> parse_error "unexpected end of input in doc" s spos
1329 | Vopen ("bookmarks", _, closed) ->
1330 if closed
1331 then v
1332 else { v with f = pbookmarks path origin pan anchor c bookmarks }
1334 | Vopen ("keymap", attrs, closed) ->
1335 let modename =
1336 try List.assoc "mode" attrs
1337 with Not_found -> "global"
1339 if closed
1340 then v
1341 else
1342 let ret keymap =
1343 let h = findkeyhash c modename in
1344 KeyMap.iter (Hashtbl.replace h) keymap;
1345 doc path origin pan anchor c bookmarks
1347 { v with f = pkeymap ret KeyMap.empty }
1349 | Vopen ("css", [], false) ->
1350 { v with f = pcss path origin pan anchor c bookmarks }
1352 | Vopen (_, _, _) ->
1353 parse_error "unexpected subelement in doc" s spos
1355 | Vclose "doc" ->
1356 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor, origin);
1357 { v with f = llppconfig }
1359 | Vclose _ -> parse_error "unexpected close in doc" s spos
1361 and pcss path origin pan anchor c bookmarks v t spos epos =
1362 match t with
1363 | Vdata | Vcdata ->
1364 let b = Buffer.create 10 in
1365 Buffer.add_substring b s spos (epos - spos);
1366 { v with f = pcss path origin pan anchor
1367 { c with css = Buffer.contents b }
1368 bookmarks }
1369 | Vend -> parse_error "unexpected end of input in css" s spos
1370 | Vopen _ -> parse_error "unexpected subelement in css" s spos
1371 | Vclose "css" -> { v with f = doc path origin pan anchor c bookmarks }
1372 | Vclose _ -> parse_error "unexpected close in css" s spos
1374 and pkeymap ret keymap v t spos _ =
1375 match t with
1376 | Vdata | Vcdata -> v
1377 | Vend -> parse_error "unexpected end of input in keymap" s spos
1378 | Vopen ("map", attrs, closed) ->
1379 let r, l = map_of attrs in
1380 let kss = fromstring keys_of_string spos "in" r [] in
1381 let lss = fromstring keys_of_string spos "out" l [] in
1382 let keymap =
1383 match kss with
1384 | [] -> keymap
1385 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
1386 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
1388 if closed
1389 then { v with f = pkeymap ret keymap }
1390 else
1391 let f () = v in
1392 { v with f = skip "map" f }
1394 | Vopen _ ->
1395 parse_error "unexpected subelement in keymap" s spos
1397 | Vclose "keymap" ->
1398 { v with f = ret keymap }
1400 | Vclose _ -> parse_error "unexpected close in keymap" s spos
1402 and pbookmarks path origin pan anchor c bookmarks v t spos _ =
1403 match t with
1404 | Vdata | Vcdata -> v
1405 | Vend -> parse_error "unexpected end of input in bookmarks" s spos
1406 | Vopen ("item", attrs, closed) ->
1407 let titleent, spage, srely, svisy = bookmark_of attrs in
1408 let page = fromstring int_of_string spos "page" spage 0
1409 and rely = fromstring float_of_string spos "rely" srely 0.0
1410 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1411 let bookmarks =
1412 (unentS titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
1414 if closed
1415 then { v with f = pbookmarks path origin pan anchor c bookmarks }
1416 else
1417 let f () = v in
1418 { v with f = skip "item" f }
1420 | Vopen _ ->
1421 parse_error "unexpected subelement in bookmarks" s spos
1423 | Vclose "bookmarks" ->
1424 { v with f = doc path origin pan anchor c bookmarks }
1426 | Vclose _ -> parse_error "unexpected close in bookmarks" s spos
1428 and skip tag f v t spos _ =
1429 match t with
1430 | Vdata | Vcdata -> v
1431 | Vend ->
1432 parse_error ("unexpected end of input in skipped " ^ tag) s spos
1433 | Vopen (tag', _, closed) ->
1434 if closed
1435 then v
1436 else
1437 let f' () = { v with f = skip tag f } in
1438 { v with f = skip tag' f' }
1439 | Vclose ctag ->
1440 if tag = ctag
1441 then f ()
1442 else parse_error ("unexpected close in skipped " ^ tag) s spos
1445 parse { f = toplevel; accu = () } s;
1446 h, dc;
1449 let do_load f contents =
1450 try f contents
1451 with
1452 | Parser.Parse_error (msg, s, pos) ->
1453 let subs = Parser.subs s pos in
1454 Utils.error "parse error: %s: at %d [..%S..]" msg pos subs
1456 | exn -> Utils.error "parse error: %s" @@ exntos exn
1459 let defconfpath =
1460 let dir =
1461 let xdgconfdir = Utils.getenvwithdef "XDG_CONFIG_HOME" E.s in
1462 if emptystr xdgconfdir
1463 then
1465 let dir = Filename.concat home ".config" in
1466 if Sys.is_directory dir then dir else home
1467 with _ -> home
1468 else xdgconfdir
1470 Filename.concat dir "llpp.conf"
1473 let confpath = ref defconfpath;;
1475 let load2 f default =
1476 match filecontents !confpath with
1477 | contents -> f @@ do_load get contents
1478 | exception Unix.Unix_error (Unix.ENOENT, "open", _) ->
1479 f (Hashtbl.create 0, defconf)
1480 | exception exn ->
1481 dolog "error loading configuration from `%S': %s" !confpath @@ exntos exn;
1482 default
1485 let load1 f = load2 f false;;
1487 let load openlast =
1488 let f (h, dc) =
1489 if openlast
1490 then (
1491 let path, _ =
1492 Hashtbl.fold
1493 (fun path (conf, _, _, _, _) ((_, besttime) as best) ->
1494 if conf.lastvisit > besttime
1495 then (path, conf.lastvisit)
1496 else best)
1498 (state.path, -.infinity)
1500 state.path <- path;
1502 let pc, pb, px, pa, po =
1504 let absname = abspath state.path in
1505 Hashtbl.find h absname
1506 with Not_found -> dc, [], 0, emptyanchor, state.origin
1508 setconf defconf dc;
1509 setconf conf pc;
1510 state.bookmarks <- pb;
1511 state.x <- px;
1512 state.origin <- po;
1513 if conf.jumpback
1514 then state.anchor <- pa;
1515 cbput state.hists.nav pa;
1516 true
1518 load1 f
1521 let gethist () =
1522 let f (h, _) =
1523 Hashtbl.fold (fun path (pc, pb, px, pa, po) accu ->
1524 (path, pc, pb, px, pa, po) :: accu)
1525 h [];
1527 load2 f []
1530 let add_attrs bb always dc c time =
1531 let o' fmt s =
1532 Buffer.add_string bb "\n ";
1533 Printf.bprintf bb fmt s
1535 let o c fmt s = if c then o' fmt s else ignore in
1536 let ob s a b = o (always || a != b) "%s='%b'" s a
1537 and op s a b = o (always || a <> b) "%s='%b'" s (a != None)
1538 and oi s a b = o (always || a != b) "%s='%d'" s a
1539 and oI s a b = o (always || a != b) "%s='%s'" s (string_with_suffix_of_int a)
1540 and oz s a b = o (always || a <> b) "%s='%g'" s (a*.100.)
1541 and oF s a b = o (always || a <> b) "%s='%f'" s a
1542 and oL s a b = o (always || a <> b) "%s='%Ld'" s a
1543 and oc s a b = o (always || a <> b) "%s='%s'" s (color_to_string a)
1544 and oC s a b = o (always || a <> b) "%s='%s'" s (CSTE.to_string a)
1545 and oR s a b = o (always || a <> b) "%s='%s'" s (irect_to_string a)
1546 and oFm s a b = o (always || a <> b) "%s='%s'" s (FMTE.to_string a)
1547 and oSv s a b m = o (always || a <> b) "%s='%b'" s (a land m != 0)
1548 and oPm s a b = o (always || a <> b) "%s='%s'" s (MTE.to_string a)
1549 and os s a b =
1550 o (always || a <> b) "%s='%s'" s @@ Parser.enent a 0 (String.length a)
1551 and og s a b =
1552 if always || a <> b
1553 then
1554 match a with
1555 | Some (_N, _A, _B) -> o' "%s='%u,%u,%u'" s _N _A _B
1556 | None ->
1557 match b with
1558 | None -> ()
1559 | _ -> o' "%s='none'" s
1560 and oW s a b =
1561 if always || a <> b
1562 then
1563 let v =
1564 match a with
1565 | None -> "false"
1566 | Some f ->
1567 if f = infinity
1568 then "true"
1569 else string_of_float f
1571 o' "%s='%s'" s v
1572 and oco s a b =
1573 if always || a <> b
1574 then
1575 match a with
1576 | Cmulti ((n, a, b), _) when n > 1 -> o' "%s='%d,%d,%d'" s n a b
1577 | Csplit (n, _) when n > 1 -> o' "%s='%d'" s ~-n
1578 | Cmulti _ | Csplit _ | Csingle _ -> ()
1579 and obeco s a b =
1580 if always || a <> b
1581 then
1582 match a with
1583 | Some c when c > 1 -> o' "%s='%d'" s c
1584 | _ -> ()
1586 oi "width" c.cwinw dc.cwinw;
1587 oi "height" c.cwinh dc.cwinh;
1588 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
1589 oi "scroll-handle-height" c.scrollh dc.scrollh;
1590 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
1591 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
1592 ob "case-insensitive-search" c.icase dc.icase;
1593 ob "preload" c.preload dc.preload;
1594 oi "page-bias" c.pagebias dc.pagebias;
1595 oi "scroll-step" c.scrollstep dc.scrollstep;
1596 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
1597 ob "max-height-fit" c.maxhfit dc.maxhfit;
1598 ob "crop-hack" c.crophack dc.crophack;
1599 oW "throttle" c.maxwait dc.maxwait;
1600 ob "highlight-links" c.hlinks dc.hlinks;
1601 ob "under-cursor-info" c.underinfo dc.underinfo;
1602 oi "vertical-margin" c.interpagespace dc.interpagespace;
1603 oz "zoom" c.zoom dc.zoom;
1604 ob "presentation" c.presentation dc.presentation;
1605 oi "rotation-angle" c.angle dc.angle;
1606 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
1607 oFm "fit-model" c.fitmodel dc.fitmodel;
1608 oI "pixmap-cache-size" c.memlimit dc.memlimit;
1609 oi "tex-count" c.texcount dc.texcount;
1610 oi "slice-height" c.sliceheight dc.sliceheight;
1611 oi "thumbnail-width" c.thumbw dc.thumbw;
1612 ob "persistent-location" c.jumpback dc.jumpback;
1613 oc "background-color" c.bgcolor dc.bgcolor;
1614 oi "tile-width" c.tilew dc.tilew;
1615 oi "tile-height" c.tileh dc.tileh;
1616 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
1617 ob "checkers" c.checkers dc.checkers;
1618 oi "aalevel" c.aalevel dc.aalevel;
1619 ob "trim-margins" c.trimmargins dc.trimmargins;
1620 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
1621 os "uri-launcher" c.urilauncher dc.urilauncher;
1622 os "path-launcher" c.pathlauncher dc.pathlauncher;
1623 oC "color-space" c.colorspace dc.colorspace;
1624 ob "invert-colors" c.invert dc.invert;
1625 oF "brightness" c.colorscale dc.colorscale;
1626 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
1627 oco "columns" c.columns dc.columns;
1628 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
1629 os "selection-command" c.selcmd dc.selcmd;
1630 os "synctex-command" c.stcmd dc.stcmd;
1631 os "pax-command" c.paxcmd dc.paxcmd;
1632 os "askpass-command" c.passcmd dc.passcmd;
1633 os "savepath-command" c.savecmd dc.savecmd;
1634 ob "update-cursor" c.updatecurs dc.updatecurs;
1635 oi "hint-font-size" c.hfsize dc.hfsize;
1636 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
1637 oF "page-scroll-scale" c.pgscale dc.pgscale;
1638 ob "use-pbo" c.usepbo dc.usepbo;
1639 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
1640 ob "remote-in-a-new-instance" c.riani dc.riani;
1641 op "point-and-x" c.pax dc.pax;
1642 oPm "point-and-x-mark" c.paxmark dc.paxmark;
1643 ob "scroll-bar-on-the-left" c.leftscroll dc.leftscroll;
1644 if not always
1645 then os "title" c.title dc.title;
1646 oL "last-visit" (Int64.of_float time) 0L;
1647 ob "edit-annotations-inline" c.annotinline dc.annotinline;
1648 ob "coarse-presentation-positioning" c.coarseprespos dc.coarseprespos;
1651 let keymapsbuf always dc c =
1652 let bb = Buffer.create 16 in
1653 let rec loop = function
1654 | [] -> ()
1655 | (modename, h) :: rest ->
1656 let dh = findkeyhash dc modename in
1657 if always || h <> dh
1658 then (
1659 if Hashtbl.length h > 0
1660 then (
1661 if Buffer.length bb > 0
1662 then Buffer.add_char bb '\n';
1663 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
1664 Hashtbl.iter (fun i o ->
1665 let isdifferent = always ||
1667 let dO = Hashtbl.find dh i in
1668 dO <> o
1669 with Not_found -> true
1671 if isdifferent
1672 then
1673 let addkm (k, m) =
1674 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
1675 if Wsi.withalt m then Buffer.add_string bb "alt-";
1676 if Wsi.withshift m then Buffer.add_string bb "shift-";
1677 if Wsi.withmeta m then Buffer.add_string bb "meta-";
1678 Buffer.add_string bb (Wsi.keyname k);
1680 let addkms l =
1681 let rec loop = function
1682 | [] -> ()
1683 | km :: [] -> addkm km
1684 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
1686 loop l
1688 Buffer.add_string bb "<map in='";
1689 addkm i;
1690 match o with
1691 | KMinsrt km ->
1692 Buffer.add_string bb "' out='";
1693 addkm km;
1694 Buffer.add_string bb "'/>\n"
1696 | KMinsrl kms ->
1697 Buffer.add_string bb "' out='";
1698 addkms kms;
1699 Buffer.add_string bb "'/>\n"
1701 | KMmulti (ins, kms) ->
1702 Buffer.add_char bb ' ';
1703 addkms ins;
1704 Buffer.add_string bb "' out='";
1705 addkms kms;
1706 Buffer.add_string bb "'/>\n"
1707 ) h;
1708 Buffer.add_string bb "</keymap>";
1711 loop rest
1713 loop c.keyhashes;
1717 let save1 bb leavebirdseye x h dc =
1718 let uifontsize = fstate.fontsize in
1719 let dc = if conf.bedefault then conf else dc in
1720 Buffer.add_string bb "<llppconfig>\n";
1722 if nonemptystr !fontpath
1723 then
1724 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
1725 uifontsize
1726 !fontpath
1727 else (
1728 if uifontsize <> 14
1729 then
1730 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
1733 Buffer.add_string bb "<defaults";
1734 add_attrs bb true dc dc nan;
1735 let kb = keymapsbuf true dc dc in
1736 if Buffer.length kb > 0
1737 then (
1738 Buffer.add_string bb ">\n";
1739 Buffer.add_buffer bb kb;
1740 Buffer.add_string bb "\n</defaults>\n";
1742 else Buffer.add_string bb "/>\n";
1744 let adddoc path pan anchor c bookmarks time origin =
1745 if bookmarks == [] && c = dc && anchor = emptyanchor
1746 then ()
1747 else (
1748 Printf.bprintf bb "<doc path='%s'"
1749 (Parser.enent path 0 (String.length path));
1751 if nonemptystr origin
1752 then Printf.bprintf bb "\n origin='%s'"
1753 (Parser.enent origin 0 (String.length origin));
1755 if anchor <> emptyanchor
1756 then (
1757 let n, rely, visy = anchor in
1758 Printf.bprintf bb "\n page='%d'" n;
1760 if rely > 1e-6
1761 then Printf.bprintf bb " rely='%f'" rely;
1763 if abs_float visy > 1e-6
1764 then Printf.bprintf bb " visy='%f'" visy;
1767 if pan != 0
1768 then Printf.bprintf bb " pan='%d'" pan;
1770 add_attrs bb false dc c time;
1771 if nonemptystr c.css
1772 then Printf.bprintf bb ">\n <css><![CDATA[%s]]></css>" c.css;
1773 let kb = keymapsbuf false dc c in
1775 begin match bookmarks with
1776 | [] ->
1777 if Buffer.length kb > 0
1778 then (
1779 Buffer.add_string bb ">\n";
1780 Buffer.add_buffer bb kb;
1781 Buffer.add_string bb "\n</doc>\n";
1783 else
1784 if nonemptystr c.css
1785 then Buffer.add_string bb "\n</doc>\n"
1786 else Buffer.add_string bb "/>\n"
1787 | _ ->
1788 Buffer.add_string bb ">\n<bookmarks>\n";
1789 List.iter (fun (title, _, kind) ->
1790 begin match kind with
1791 | Oanchor (page, rely, visy) ->
1792 Printf.bprintf bb
1793 "<item title='%s' page='%d'"
1794 (Parser.enent title 0 (String.length title))
1795 page
1797 if rely > 1e-6
1798 then
1799 Printf.bprintf bb " rely='%f'" rely
1801 if abs_float visy > 1e-6
1802 then
1803 Printf.bprintf bb " visy='%f'" visy
1805 | Ohistory _ | Onone | Ouri _ | Oremote _
1806 | Oremotedest _ | Olaunch _ ->
1807 failwith "unexpected link in bookmarks"
1808 end;
1809 Buffer.add_string bb "/>\n";
1810 ) bookmarks;
1811 Buffer.add_string bb "</bookmarks>";
1812 if Buffer.length kb > 0
1813 then (
1814 Buffer.add_string bb "\n";
1815 Buffer.add_buffer bb kb;
1817 Buffer.add_string bb "\n</doc>\n";
1818 end;
1822 let pan, conf =
1823 match state.mode with
1824 | Birdseye (c, pan, _, _, _) ->
1825 let beyecolumns =
1826 match conf.columns with
1827 | Cmulti ((c, _, _), _) -> Some c
1828 | Csingle _ -> None
1829 | Csplit _ -> None
1830 and columns =
1831 match c.columns with
1832 | Cmulti (c, _) -> Cmulti (c, E.a)
1833 | Csingle _ -> Csingle E.a
1834 | Csplit _ -> failwith "quit from bird's eye while split"
1836 pan, { c with beyecolumns = beyecolumns; columns = columns }
1837 | Textentry _
1838 | View
1839 | LinkNav _ -> x, conf
1841 let docpath = if nonemptystr state.path then abspath state.path else E.s in
1842 if nonemptystr docpath
1843 then (
1844 adddoc docpath pan (getanchor ())
1846 let autoscrollstep =
1847 match state.autoscroll with
1848 | Some step -> step
1849 | None -> conf.autoscrollstep
1851 begin match state.mode with
1852 | Birdseye beye -> leavebirdseye beye true
1853 | Textentry _
1854 | View
1855 | LinkNav _ -> ()
1856 end;
1857 { conf with autoscrollstep = autoscrollstep }
1859 (if conf.savebmarks then state.bookmarks else [])
1860 (now ())
1861 state.origin
1863 Hashtbl.iter (fun path (c, bookmarks, x, anchor, origin) ->
1864 if docpath <> abspath path
1865 then adddoc path x anchor c bookmarks c.lastvisit origin
1866 ) h;
1867 Buffer.add_string bb "</llppconfig>\n";
1868 true;
1871 let save leavebirdseye =
1872 let relx = float state.x /. float state.winw in
1873 let w, h, x =
1874 let cx w = truncate (relx *. float w) in
1875 List.fold_left
1876 (fun (w, h, x) ws ->
1877 match ws with
1878 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
1879 | Wsi.MaxVert -> (w, conf.cwinh, x)
1880 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
1882 (state.winw, state.winh, state.x) state.winstate
1884 conf.cwinw <- w;
1885 conf.cwinh <- h;
1886 let bb = Buffer.create 32768 in
1887 let save2 (h, dc) =
1888 save1 bb leavebirdseye x h dc
1890 if load1 save2 && Buffer.length bb > 0
1891 then
1893 let tmp = !confpath ^ ".tmp" in
1894 let oc = open_out_bin tmp in
1895 Buffer.output_buffer oc bb;
1896 close_out oc;
1897 Unix.rename tmp !confpath;
1898 with exn ->
1899 dolog "error saving configuration: %s" @@ exntos exn
1902 let gc fd =
1903 let wr s n =
1904 (* This here has a potential of rising SIGPIPE, silently and
1905 seemingly harmlessly (when -gc was supplied an invalid
1906 executable for instance) probably needs revisiting *)
1907 let n' = Unix.write fd (Bytes.of_string s) 0 n in
1908 if n != n'
1909 then Utils.error "Unix.write %d = %d" n n'
1911 let href = ref (Hashtbl.create 0) in
1912 let cref = ref defconf in
1913 let push (h, dc) =
1914 let f path (pc, _pb, _px, _pa, _po) =
1915 let s =
1916 Printf.sprintf "%s\000%ld\000" path (Int32.of_float pc.lastvisit)
1918 wr s (String.length s)
1920 Hashtbl.iter f h;
1921 href := h;
1922 cref := dc;
1923 true
1925 ignore (load1 push);
1926 Unix.shutdown fd Unix.SHUTDOWN_SEND;
1927 let s = fdcontents fd in
1928 let rec f ppos =
1929 match String.index_from s ppos '\000' with
1930 | exception Not_found -> ()
1931 | zpos1 ->
1932 match String.index_from s (zpos1+1) '\000' with
1933 | exception Not_found -> error "invalid gc input in (%S) at %d" s zpos1
1934 | zpos2 ->
1935 let okey = StringLabels.sub s ~pos:ppos ~len:(zpos1-ppos) in
1936 let nkey = StringLabels.sub s ~pos:(zpos1+1) ~len:(zpos2-zpos1-1) in
1937 if emptystr nkey
1938 then (Hashtbl.remove !href okey; f (zpos2+1))
1939 else
1940 match Hashtbl.find !href okey with
1941 | exception Not_found -> Utils.error "gc: cannot find %S" okey
1942 | v ->
1943 Hashtbl.remove !href okey;
1944 Hashtbl.replace !href nkey v;
1945 f (zpos2+1)
1947 f 0;
1948 let bb = Buffer.create 32768 in
1949 let save2 (_h, dc) = save1 bb (fun _ _ -> ()) 0 !href dc in
1950 if load1 save2 && Buffer.length bb > 0
1951 then (
1953 let tmp = !confpath ^ ".tmp" in
1954 let oc = open_out_bin tmp in
1955 Buffer.output_buffer oc bb;
1956 close_out oc;
1957 Unix.rename tmp !confpath;
1958 with exn ->
1959 dolog "error saving configuration: %s" @@ exntos exn
1963 let logcurrently = function
1964 | Idle -> dolog "Idle"
1965 | Loading (l, gen) ->
1966 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1967 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1968 dolog
1969 "Tiling %d[%d,%d] page=%s cs=%s angle=%d"
1970 l.pageno col row (~> pageopaque)
1971 (CSTE.to_string colorspace) angle
1973 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1974 angle gen conf.angle state.gen
1975 tilew tileh
1976 conf.tilew conf.tileh
1978 | Outlining _ ->
1979 dolog "outlining"