Sync with upstream
[llpp.git] / config.ml
blobc8ad3ec619b540b5b4bafa44ccaac5075af68a24
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 params = (angle * fitmodel * trimparams
94 * texcount * sliceheight * memsize
95 * colorspace * fontpath * trimcachepath
96 * haspbo * usefontconfig)
97 and width = int
98 and height = int
99 and leftx = int
100 and opaque = Opaque.t
101 and rectcolor = (float * float * float * float)
102 and pixmapsize = int
103 and gen = int
104 and top = float
105 and dtop = float
106 and fontpath = string
107 and trimcachepath = 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 | Ulinkgoto of (int * int)
124 | Utext of facename
125 | Uunexpected of string
126 | Ulaunch of launchcommand
127 | Unamed of destname
128 | Uremote of (filename * pageno)
129 | Uremotedest of (filename * destname)
130 | Uannotation of (opaque * slinkindex)
131 and slinkindex = int
132 and facename = string
133 and launchcommand = string
134 and filename = string
135 and pageno = int
136 and destname = string
137 and mark =
138 | Mark_page
139 | Mark_block
140 | Mark_line
141 | Mark_word
142 and link =
143 | Lnotfound
144 | Lfound of int
145 and linkdir =
146 | LDfirst
147 | LDlast
148 | LDfirstvisible of (int * int * int)
149 | LDleft of int
150 | LDright of int
151 | LDdown of int
152 | LDup of int
153 and pagewithlinks =
154 | Pwlnotfound
155 | Pwl of int
156 and scrollb = int
157 and anchor = pageno * top * dtop
158 and rect = float * float * float * float * float * float * float * float
159 and infochange = | Memused | Docinfo | Pdim
162 class type uioh = object
163 method display : unit
164 method key : int -> int -> uioh
165 method button : int -> bool -> int -> int -> int -> uioh
166 method multiclick : int -> int -> int -> int -> uioh
167 method motion : int -> int -> uioh
168 method pmotion : int -> int -> uioh
169 method infochanged : infochange -> unit
170 method scrollpw : (int * float * float)
171 method scrollph : (int * float * float)
172 method modehash : keyhash
173 method eformsgs : bool
174 method alwaysscrolly : bool
175 end;;
177 module type TextEnumType =
179 type t
180 val name : string
181 val names : string array
182 end;;
184 module TextEnumMake (Ten : TextEnumType) =
185 struct
186 let names = Ten.names;;
187 let to_int (t : Ten.t) = Obj.magic t;;
188 let to_string t = names.(to_int t);;
189 let of_int n : Ten.t = Obj.magic n;;
190 let of_string s =
191 let rec find i =
192 if i = Array.length names
193 then failwith ("invalid " ^ Ten.name ^ ": " ^ s)
194 else (
195 if Ten.names.(i) = s
196 then of_int i
197 else find (i+1)
199 in find 0;;
200 end;;
202 module CSTE = TextEnumMake (struct
203 type t = colorspace;;
204 let name = "colorspace";;
205 let names = [|"rgb"; "bgr"; "gray"|];;
206 end);;
208 module MTE = TextEnumMake (struct
209 type t = mark;;
210 let name = "mark";;
211 let names = [|"page"; "block"; "line"; "word"|];;
212 end);;
214 module FMTE = TextEnumMake (struct
215 type t = fitmodel;;
216 let name = "fitmodel";;
217 let names = [|"width"; "proportional"; "page"|];;
218 end);;
220 type conf =
221 { mutable scrollbw : int
222 ; mutable scrollh : int
223 ; mutable scrollb : scrollb
224 ; mutable icase : bool
225 ; mutable preload : bool
226 ; mutable pagebias : int
227 ; mutable verbose : bool
228 ; mutable debug : bool
229 ; mutable scrollstep : int
230 ; mutable hscrollstep : int
231 ; mutable maxhfit : bool
232 ; mutable crophack : bool
233 ; mutable autoscrollstep : int
234 ; mutable maxwait : float option
235 ; mutable hlinks : bool
236 ; mutable underinfo : bool
237 ; mutable interpagespace : interpagespace
238 ; mutable zoom : float
239 ; mutable presentation : bool
240 ; mutable angle : angle
241 ; mutable cwinw : int
242 ; mutable cwinh : int
243 ; mutable savebmarks : bool
244 ; mutable fitmodel : fitmodel
245 ; mutable trimmargins : trimmargins
246 ; mutable trimfuzz : irect
247 ; mutable memlimit : memsize
248 ; mutable texcount : texcount
249 ; mutable sliceheight : sliceheight
250 ; mutable thumbw : width
251 ; mutable jumpback : bool
252 ; mutable bgcolor : (float * float * float)
253 ; mutable bedefault : bool
254 ; mutable tilew : int
255 ; mutable tileh : int
256 ; mutable mustoresize : memsize
257 ; mutable checkers : bool
258 ; mutable aalevel : int
259 ; mutable urilauncher : string
260 ; mutable pathlauncher : string
261 ; mutable colorspace : colorspace
262 ; mutable invert : bool
263 ; mutable colorscale : float
264 ; mutable ghyllscroll : (int * int * int) option
265 ; mutable columns : columns
266 ; mutable beyecolumns : columncount option
267 ; mutable selcmd : string
268 ; mutable paxcmd : string
269 ; mutable passcmd : string
270 ; mutable savecmd : string
271 ; mutable updatecurs : bool
272 ; mutable keyhashes : (string * keyhash) list
273 ; mutable hfsize : int
274 ; mutable pgscale : float
275 ; mutable usepbo : bool
276 ; mutable wheelbypage : bool
277 ; mutable stcmd : string
278 ; mutable riani : bool
279 ; mutable pax : (float * int * int) ref option
280 ; mutable paxmark : mark
281 ; mutable leftscroll : bool
282 ; mutable title : string
283 ; mutable lastvisit : float
284 ; mutable annotinline : bool
285 ; mutable coarseprespos : bool
287 and columns =
288 | Csingle of singlecolumn
289 | Cmulti of multicolumns
290 | Csplit of splitcolumns
291 and outlinekind =
292 | Onone
293 | Oanchor of anchor
294 | Ouri of uri
295 | Olaunch of launchcommand
296 | Oremote of (filename * pageno)
297 | Oremotedest of (filename * destname)
298 | Ohistory of (filename * conf * outline list * x * anchor * filename)
299 and outline = (caption * outlinelevel * outlinekind)
300 and outlinelevel = int
303 type page =
304 { pageno : int
305 ; pagedimno : int
306 ; pagew : int
307 ; pageh : int
308 ; pagex : int
309 ; pagey : int
310 ; pagevw : int
311 ; pagevh : int
312 ; pagedispx : int
313 ; pagedispy : int
314 ; pagecol : int
318 type tile = opaque * pixmapsize * elapsed
319 and elapsed = float;;
320 type pagemapkey = pageno * gen;;
321 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
322 and row = int
323 and col = int
324 and currently =
325 | Idle
326 | Loading of (page * gen)
327 | Tiling of (
328 page * opaque * colorspace * angle * gen * col * row * width * height
330 | Outlining of outline list
333 type mpos = int * int
334 and mstate =
335 | Msel of (mpos * mpos)
336 | Mpan of mpos
337 | Mscrolly | Mscrollx
338 | Mzoom of (buttonno * step)
339 | Mzoomrect of (mpos * mpos)
340 | Mnone
341 and buttonno = int
342 and step = int
345 type mode =
346 | Birdseye of (conf * leftx * pageno * pageno * anchor)
347 | Textentry of (textentry * onleave)
348 | View
349 | LinkNav of linktarget
350 and onleave = leavetextentrystatus -> unit
351 and leavetextentrystatus = | Cancel | Confirm
352 and helpitem = string * int * action
353 and action =
354 | Noaction
355 | Action of (uioh -> uioh)
356 and linktarget =
357 | Ltexact of (pageno * direction)
358 | Ltgendir of direction
359 | Ltnotready of (pageno * direction)
360 and direction = int (* -1, 0, 1 *)
361 and textentry = string * string * onhist option * onkey * ondone * cancelonempty
362 and onkey = string -> int -> 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 : int
391 ; mutable y : int
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
442 and hists =
443 { pat : string circbuf
444 ; pag : string circbuf
445 ; nav : anchor circbuf
446 ; sel : string circbuf
450 let emptyanchor = (0, 0.0, 0.0);;
451 let emptykeyhash = Hashtbl.create 0;;
452 let noghyll _ = ();;
453 let noreprf () = ();;
454 let noroam () = ();;
456 let nouioh : uioh = object (self)
457 method display = ()
458 method key _ _ = self
459 method multiclick _ _ _ _ = self
460 method button _ _ _ _ _ = self
461 method motion _ _ = self
462 method pmotion _ _ = self
463 method infochanged _ = ()
464 method scrollpw = (0, nan, nan)
465 method scrollph = (0, nan, nan)
466 method modehash = emptykeyhash
467 method eformsgs = false
468 method alwaysscrolly = false
469 end;;
471 let platform_to_string = function
472 | Punknown -> "unknown"
473 | Plinux -> "Linux"
474 | Posx -> "OSX"
475 | Psun -> "Sun"
476 | Pbsd -> "BSD"
477 | Pcygwin -> "Cygwin"
480 let version () =
481 Printf.sprintf "llpp version %s, fitz %s, ocaml %s/%d bit"
482 Help.version (fz_version ()) Sys.ocaml_version Sys.word_size
485 let defconf =
486 { scrollbw = 7
487 ; scrollh = 12
488 ; scrollb = scrollbhv lor scrollbvv
489 ; icase = true
490 ; preload = true
491 ; pagebias = 0
492 ; verbose = false
493 ; debug = false
494 ; scrollstep = 24
495 ; hscrollstep = 24
496 ; maxhfit = true
497 ; crophack = false
498 ; autoscrollstep = 2
499 ; maxwait = None
500 ; hlinks = false
501 ; underinfo = false
502 ; interpagespace = 2
503 ; zoom = 1.0
504 ; presentation = false
505 ; angle = 0
506 ; cwinw = 900
507 ; cwinh = 900
508 ; savebmarks = true
509 ; fitmodel = FitProportional
510 ; trimmargins = false
511 ; trimfuzz = (0,0,0,0)
512 ; memlimit = 32 lsl 20
513 ; texcount = 256
514 ; sliceheight = 24
515 ; thumbw = 76
516 ; jumpback = true
517 ; bgcolor = (0.5, 0.5, 0.5)
518 ; bedefault = false
519 ; tilew = 2048
520 ; tileh = 2048
521 ; mustoresize = 256 lsl 20
522 ; checkers = true
523 ; aalevel = 8
524 ; urilauncher =
525 (match platform with
526 | Plinux | Psun | Pbsd -> "xdg-open \"%s\""
527 | Posx -> "open \"%s\""
528 | Pcygwin -> "cygstart \"%s\""
529 | Punknown -> "echo %s")
530 ; pathlauncher = "lp \"%s\""
531 ; selcmd =
532 (match platform with
533 | Plinux | Pbsd | Psun -> "xsel -i"
534 | Posx -> "pbcopy"
535 | Pcygwin -> "wsel"
536 | Punknown -> "cat")
537 ; paxcmd = "cat"
538 ; passcmd = E.s
539 ; savecmd = E.s
540 ; colorspace = Rgb
541 ; invert = false
542 ; colorscale = 1.0
543 ; ghyllscroll = None
544 ; columns = Csingle [||]
545 ; beyecolumns = None
546 ; updatecurs = false
547 ; hfsize = 12
548 ; pgscale = 1.0
549 ; usepbo = false
550 ; wheelbypage = false
551 ; stcmd = "echo SyncTex"
552 ; riani = false
553 ; pax = None
554 ; paxmark = Mark_word
555 ; leftscroll = false
556 ; title = E.s
557 ; lastvisit = 0.0
558 ; annotinline = true
559 ; coarseprespos = false
560 ; keyhashes =
561 let mk n = (n, Hashtbl.create 1) in
562 [ mk "global"
563 ; mk "info"
564 ; mk "help"
565 ; mk "outline"
566 ; mk "listview"
567 ; mk "birdseye"
568 ; mk "textentry"
569 ; mk "links"
570 ; mk "view"
575 let conf = { defconf with angle = defconf.angle };;
577 let gotourl url =
578 let command = Str.global_replace percentsre url conf.urilauncher in
579 try ignore @@ spawn command []
580 with exn -> dolog "failed to execute `%s': %s" command @@ exntos exn
583 let gotouri uri =
584 if emptystr conf.urilauncher
585 then dolog "%s" uri
586 else (
587 let url = geturl uri in
588 if emptystr url
589 then dolog "obtained empty url from uri %S" uri
590 else gotourl url
594 let makehelp () =
595 let strings =
596 version ()
597 :: "(searching in this text works just by typing (i.e. no initial '/'))"
598 :: E.s :: Help.keys
600 Array.of_list (
601 List.map (fun s ->
602 let url = geturl s in
603 if nonemptystr url
604 then (s, 0, Action (fun uioh -> gotourl url; uioh))
605 else (s, 0, Noaction)
606 ) strings);
609 let cbnew n v =
610 { store = Array.make n v
611 ; rc = 0
612 ; wc = 0
613 ; len = 0
617 let cbcap b = Array.length b.store;;
619 let cbput b v =
620 let cap = cbcap b in
621 b.store.(b.wc) <- v;
622 b.wc <- (b.wc + 1) mod cap;
623 b.rc <- b.wc;
624 b.len <- min (b.len + 1) cap;
627 let cbempty b = b.len = 0;;
629 let cbgetg b circular dir =
630 if cbempty b
631 then b.store.(0)
632 else
633 let rc = b.rc + dir in
634 let rc =
635 if circular
636 then (
637 if rc = -1
638 then b.len-1
639 else (
640 if rc >= b.len
641 then 0
642 else rc
645 else bound rc 0 (b.len-1)
647 b.rc <- rc;
648 b.store.(rc);
651 let cbget b = cbgetg b false;;
652 let cbgetc b = cbgetg b true;;
654 let state =
655 { ss = Unix.stdin
656 ; wsfd = Unix.stdin
657 ; stderr = Unix.stderr
658 ; errmsgs = Buffer.create 0
659 ; newerrmsgs = false
660 ; x = 0
661 ; y = 0
662 ; w = 0
663 ; anchor = emptyanchor
664 ; ranchors = []
665 ; layout = []
666 ; maxy = max_int
667 ; tilelru = Queue.create ()
668 ; pagemap = Hashtbl.create 10
669 ; tilemap = Hashtbl.create 10
670 ; pdims = []
671 ; pagecount = 0
672 ; currently = Idle
673 ; mstate = Mnone
674 ; rects = []
675 ; rects1 = []
676 ; prects = Hashtbl.create 1
677 ; text = E.s
678 ; mode = View
679 ; winstate = []
680 ; searchpattern = E.s
681 ; outlines = E.a
682 ; bookmarks = []
683 ; path = E.s
684 ; password = E.s
685 ; nameddest = E.s
686 ; geomcmds = E.s, []
687 ; hists =
688 { nav = cbnew 10 emptyanchor
689 ; pat = cbnew 10 E.s
690 ; pag = cbnew 10 E.s
691 ; sel = cbnew 10 E.s
693 ; memused = 0
694 ; gen = 0
695 ; throttle = None
696 ; autoscroll = None
697 ; ghyll = noghyll
698 ; help = makehelp ()
699 ; docinfo = []
700 ; checkerstexid = None
701 ; prevzoom = (1.0, 0)
702 ; progress = -1.0
703 ; uioh = nouioh
704 ; redisplay = true
705 ; mpos = (-1, -1)
706 ; keystate = KSnone
707 ; glinks = false
708 ; prevcolumns = None
709 ; winw = -1
710 ; winh = -1
711 ; reprf = noreprf
712 ; origin = E.s
713 ; roam = noroam
714 ; bzoom = false
715 ; traw = Raw.create_static `float ~len:8
716 ; vraw = Raw.create_static `float ~len:8
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.lowercase 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.pax <-
1215 if src.pax = None
1216 then None
1217 else Some ((ref (0.0, 0, 0)));
1220 let findkeyhash c name =
1221 try List.assoc name c.keyhashes
1222 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
1225 let get s =
1226 let open Parser in
1227 let h = Hashtbl.create 10 in
1228 let dc = { defconf with angle = defconf.angle } in
1229 let rec toplevel v t spos _ =
1230 match t with
1231 | Vdata | Vcdata | Vend -> v
1232 | Vopen ("llppconfig", _, closed) ->
1233 if closed
1234 then v
1235 else { v with f = llppconfig }
1236 | Vopen _ ->
1237 error "unexpected subelement at top level" s spos
1238 | Vclose _ -> error "unexpected close at top level" s spos
1240 and llppconfig v t spos _ =
1241 match t with
1242 | Vdata | Vcdata -> v
1243 | Vend -> 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 error "unexpected subelement in llppconfig" s spos
1282 | Vclose "llppconfig" -> { v with f = toplevel }
1283 | Vclose _ -> error "unexpected close in llppconfig" s spos
1285 and defaults v t spos _ =
1286 match t with
1287 | Vdata | Vcdata -> v
1288 | Vend -> 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 error "unexpected subelement in defaults" s spos
1306 | Vclose "defaults" ->
1307 { v with f = llppconfig }
1309 | Vclose _ -> 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 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 _ -> error "unexpected close in ui-font" s spos
1323 | Vend -> 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 -> 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 (_, _, _) ->
1350 error "unexpected subelement in doc" s spos
1352 | Vclose "doc" ->
1353 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor, origin);
1354 { v with f = llppconfig }
1356 | Vclose _ -> error "unexpected close in doc" s spos
1358 and pkeymap ret keymap v t spos _ =
1359 match t with
1360 | Vdata | Vcdata -> v
1361 | Vend -> error "unexpected end of input in keymap" s spos
1362 | Vopen ("map", attrs, closed) ->
1363 let r, l = map_of attrs in
1364 let kss = fromstring keys_of_string spos "in" r [] in
1365 let lss = fromstring keys_of_string spos "out" l [] in
1366 let keymap =
1367 match kss with
1368 | [] -> keymap
1369 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
1370 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
1372 if closed
1373 then { v with f = pkeymap ret keymap }
1374 else
1375 let f () = v in
1376 { v with f = skip "map" f }
1378 | Vopen _ ->
1379 error "unexpected subelement in keymap" s spos
1381 | Vclose "keymap" ->
1382 { v with f = ret keymap }
1384 | Vclose _ -> error "unexpected close in keymap" s spos
1386 and pbookmarks path origin pan anchor c bookmarks v t spos _ =
1387 match t with
1388 | Vdata | Vcdata -> v
1389 | Vend -> error "unexpected end of input in bookmarks" s spos
1390 | Vopen ("item", attrs, closed) ->
1391 let titleent, spage, srely, svisy = bookmark_of attrs in
1392 let page = fromstring int_of_string spos "page" spage 0
1393 and rely = fromstring float_of_string spos "rely" srely 0.0
1394 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1395 let bookmarks =
1396 (unentS titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
1398 if closed
1399 then { v with f = pbookmarks path origin pan anchor c bookmarks }
1400 else
1401 let f () = v in
1402 { v with f = skip "item" f }
1404 | Vopen _ ->
1405 error "unexpected subelement in bookmarks" s spos
1407 | Vclose "bookmarks" ->
1408 { v with f = doc path origin pan anchor c bookmarks }
1410 | Vclose _ -> Parser.parse_error "unexpected close in bookmarks" s spos
1412 and skip tag f v t spos _ =
1413 match t with
1414 | Vdata | Vcdata -> v
1415 | Vend ->
1416 Parser.parse_error ("unexpected end of input in skipped " ^ tag) s spos
1417 | Vopen (tag', _, closed) ->
1418 if closed
1419 then v
1420 else
1421 let f' () = { v with f = skip tag f } in
1422 { v with f = skip tag' f' }
1423 | Vclose ctag ->
1424 if tag = ctag
1425 then f ()
1426 else Parser.parse_error ("unexpected close in skipped " ^ tag) s spos
1429 parse { f = toplevel; accu = () } s;
1430 h, dc;
1433 let do_load f contents =
1434 try f contents
1435 with
1436 | Parser.Parse_error (msg, s, pos) ->
1437 let subs = Parser.subs s pos in
1438 Utils.error "parse error: %s: at %d [..%S..]" msg pos subs
1440 | exn -> Utils.error "parse error: %s" @@ exntos exn
1443 let defconfpath =
1444 let dir =
1445 let xdgconfdir = Utils.getenvwithdef "XDG_CONFIG_HOME" E.s in
1446 if emptystr xdgconfdir
1447 then
1449 let dir = Filename.concat home ".config" in
1450 if Sys.is_directory dir then dir else home
1451 with _ -> home
1452 else xdgconfdir
1454 Filename.concat dir "llpp.conf"
1457 let confpath = ref defconfpath;;
1459 let load2 f default =
1460 match filecontents !confpath with
1461 | contents -> f @@ do_load get contents
1462 | exception Unix.Unix_error (Unix.ENOENT, "open", _) ->
1463 f (Hashtbl.create 0, defconf)
1464 | exception exn ->
1465 dolog "error loading configuration from `%S': %s" !confpath @@ exntos exn;
1466 default
1469 let load1 f = load2 f false;;
1471 let load openlast =
1472 let f (h, dc) =
1473 if openlast
1474 then (
1475 let path, _ =
1476 Hashtbl.fold
1477 (fun path (conf, _, _, _, _) ((_, besttime) as best) ->
1478 if conf.lastvisit > besttime
1479 then (path, conf.lastvisit)
1480 else best)
1482 (state.path, -.infinity)
1484 state.path <- path;
1486 let pc, pb, px, pa, po =
1488 let absname = abspath state.path in
1489 Hashtbl.find h absname
1490 with Not_found -> dc, [], 0, emptyanchor, state.origin
1492 setconf defconf dc;
1493 setconf conf pc;
1494 state.bookmarks <- pb;
1495 state.x <- px;
1496 state.origin <- po;
1497 if conf.jumpback
1498 then state.anchor <- pa;
1499 cbput state.hists.nav pa;
1500 true
1502 load1 f
1505 let gethist () =
1506 let f (h, _) =
1507 Hashtbl.fold (fun path (pc, pb, px, pa, po) accu ->
1508 (path, pc, pb, px, pa, po) :: accu)
1509 h [];
1511 load2 f []
1514 let add_attrs bb always dc c time =
1515 let o' fmt s =
1516 Buffer.add_string bb "\n ";
1517 Printf.bprintf bb fmt s
1519 let o c fmt s = if c then o' fmt s else ignore in
1520 let ob s a b = o (always || a != b) "%s='%b'" s a
1521 and op s a b = o (always || a <> b) "%s='%b'" s (a != None)
1522 and oi s a b = o (always || a != b) "%s='%d'" s a
1523 and oI s a b = o (always || a != b) "%s='%s'" s (string_with_suffix_of_int a)
1524 and oz s a b = o (always || a <> b) "%s='%g'" s (a*.100.)
1525 and oF s a b = o (always || a <> b) "%s='%f'" s a
1526 and oL s a b = o (always || a <> b) "%s='%Ld'" s a
1527 and oc s a b = o (always || a <> b) "%s='%s'" s (color_to_string a)
1528 and oC s a b = o (always || a <> b) "%s='%s'" s (CSTE.to_string a)
1529 and oR s a b = o (always || a <> b) "%s='%s'" s (irect_to_string a)
1530 and oFm s a b = o (always || a <> b) "%s='%s'" s (FMTE.to_string a)
1531 and oSv s a b m = o (always || a <> b) "%s='%b'" s (a land m != 0)
1532 and oPm s a b = o (always || a <> b) "%s='%s'" s (MTE.to_string a)
1533 and os s a b =
1534 o (always || a <> b) "%s='%s'" s @@ Parser.enent a 0 (String.length a)
1535 and og s a b =
1536 if always || a <> b
1537 then
1538 match a with
1539 | Some (_N, _A, _B) -> o' "%s='%u,%u,%u'" s _N _A _B
1540 | None ->
1541 match b with
1542 | None -> ()
1543 | _ -> o' "%s='none'" s
1544 and oW s a b =
1545 if always || a <> b
1546 then
1547 let v =
1548 match a with
1549 | None -> "false"
1550 | Some f ->
1551 if f = infinity
1552 then "true"
1553 else string_of_float f
1555 o' "%s='%s'" s v
1556 and oco s a b =
1557 if always || a <> b
1558 then
1559 match a with
1560 | Cmulti ((n, a, b), _) when n > 1 -> o' "%s='%d,%d,%d'" s n a b
1561 | Csplit (n, _) when n > 1 -> o' "%s='%d'" s ~-n
1562 | Cmulti _ | Csplit _ | Csingle _ -> ()
1563 and obeco s a b =
1564 if always || a <> b
1565 then
1566 match a with
1567 | Some c when c > 1 -> o' "%s='%d'" s c
1568 | _ -> ()
1570 oi "width" c.cwinw dc.cwinw;
1571 oi "height" c.cwinh dc.cwinh;
1572 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
1573 oi "scroll-handle-height" c.scrollh dc.scrollh;
1574 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
1575 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
1576 ob "case-insensitive-search" c.icase dc.icase;
1577 ob "preload" c.preload dc.preload;
1578 oi "page-bias" c.pagebias dc.pagebias;
1579 oi "scroll-step" c.scrollstep dc.scrollstep;
1580 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
1581 ob "max-height-fit" c.maxhfit dc.maxhfit;
1582 ob "crop-hack" c.crophack dc.crophack;
1583 oW "throttle" c.maxwait dc.maxwait;
1584 ob "highlight-links" c.hlinks dc.hlinks;
1585 ob "under-cursor-info" c.underinfo dc.underinfo;
1586 oi "vertical-margin" c.interpagespace dc.interpagespace;
1587 oz "zoom" c.zoom dc.zoom;
1588 ob "presentation" c.presentation dc.presentation;
1589 oi "rotation-angle" c.angle dc.angle;
1590 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
1591 oFm "fit-model" c.fitmodel dc.fitmodel;
1592 oI "pixmap-cache-size" c.memlimit dc.memlimit;
1593 oi "tex-count" c.texcount dc.texcount;
1594 oi "slice-height" c.sliceheight dc.sliceheight;
1595 oi "thumbnail-width" c.thumbw dc.thumbw;
1596 ob "persistent-location" c.jumpback dc.jumpback;
1597 oc "background-color" c.bgcolor dc.bgcolor;
1598 oi "tile-width" c.tilew dc.tilew;
1599 oi "tile-height" c.tileh dc.tileh;
1600 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
1601 ob "checkers" c.checkers dc.checkers;
1602 oi "aalevel" c.aalevel dc.aalevel;
1603 ob "trim-margins" c.trimmargins dc.trimmargins;
1604 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
1605 os "uri-launcher" c.urilauncher dc.urilauncher;
1606 os "path-launcher" c.pathlauncher dc.pathlauncher;
1607 oC "color-space" c.colorspace dc.colorspace;
1608 ob "invert-colors" c.invert dc.invert;
1609 oF "brightness" c.colorscale dc.colorscale;
1610 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
1611 oco "columns" c.columns dc.columns;
1612 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
1613 os "selection-command" c.selcmd dc.selcmd;
1614 os "synctex-command" c.stcmd dc.stcmd;
1615 os "pax-command" c.paxcmd dc.paxcmd;
1616 os "askpass-command" c.passcmd dc.passcmd;
1617 os "savepath-command" c.savecmd dc.savecmd;
1618 ob "update-cursor" c.updatecurs dc.updatecurs;
1619 oi "hint-font-size" c.hfsize dc.hfsize;
1620 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
1621 oF "page-scroll-scale" c.pgscale dc.pgscale;
1622 ob "use-pbo" c.usepbo dc.usepbo;
1623 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
1624 ob "remote-in-a-new-instance" c.riani dc.riani;
1625 op "point-and-x" c.pax dc.pax;
1626 oPm "point-and-x-mark" c.paxmark dc.paxmark;
1627 ob "scroll-bar-on-the-left" c.leftscroll dc.leftscroll;
1628 if not always
1629 then os "title" c.title dc.title;
1630 oL "last-visit" (Int64.of_float time) 0L;
1631 ob "edit-annotations-inline" c.annotinline dc.annotinline;
1632 ob "coarse-presentation-positioning" c.coarseprespos dc.coarseprespos;
1635 let keymapsbuf always dc c =
1636 let bb = Buffer.create 16 in
1637 let rec loop = function
1638 | [] -> ()
1639 | (modename, h) :: rest ->
1640 let dh = findkeyhash dc modename in
1641 if always || h <> dh
1642 then (
1643 if Hashtbl.length h > 0
1644 then (
1645 if Buffer.length bb > 0
1646 then Buffer.add_char bb '\n';
1647 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
1648 Hashtbl.iter (fun i o ->
1649 let isdifferent = always ||
1651 let dO = Hashtbl.find dh i in
1652 dO <> o
1653 with Not_found -> true
1655 if isdifferent
1656 then
1657 let addkm (k, m) =
1658 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
1659 if Wsi.withalt m then Buffer.add_string bb "alt-";
1660 if Wsi.withshift m then Buffer.add_string bb "shift-";
1661 if Wsi.withmeta m then Buffer.add_string bb "meta-";
1662 Buffer.add_string bb (Wsi.keyname k);
1664 let addkms l =
1665 let rec loop = function
1666 | [] -> ()
1667 | km :: [] -> addkm km
1668 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
1670 loop l
1672 Buffer.add_string bb "<map in='";
1673 addkm i;
1674 match o with
1675 | KMinsrt km ->
1676 Buffer.add_string bb "' out='";
1677 addkm km;
1678 Buffer.add_string bb "'/>\n"
1680 | KMinsrl kms ->
1681 Buffer.add_string bb "' out='";
1682 addkms kms;
1683 Buffer.add_string bb "'/>\n"
1685 | KMmulti (ins, kms) ->
1686 Buffer.add_char bb ' ';
1687 addkms ins;
1688 Buffer.add_string bb "' out='";
1689 addkms kms;
1690 Buffer.add_string bb "'/>\n"
1691 ) h;
1692 Buffer.add_string bb "</keymap>";
1695 loop rest
1697 loop c.keyhashes;
1701 let save1 bb leavebirdseye x h dc =
1702 let uifontsize = fstate.fontsize in
1703 let dc = if conf.bedefault then conf else dc in
1704 Buffer.add_string bb "<llppconfig>\n";
1706 if nonemptystr !fontpath
1707 then
1708 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
1709 uifontsize
1710 !fontpath
1711 else (
1712 if uifontsize <> 14
1713 then
1714 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
1717 Buffer.add_string bb "<defaults";
1718 add_attrs bb true dc dc nan;
1719 let kb = keymapsbuf true dc dc in
1720 if Buffer.length kb > 0
1721 then (
1722 Buffer.add_string bb ">\n";
1723 Buffer.add_buffer bb kb;
1724 Buffer.add_string bb "\n</defaults>\n";
1726 else Buffer.add_string bb "/>\n";
1728 let adddoc path pan anchor c bookmarks time origin =
1729 if bookmarks == [] && c = dc && anchor = emptyanchor
1730 then ()
1731 else (
1732 Printf.bprintf bb "<doc path='%s'"
1733 (Parser.enent path 0 (String.length path));
1735 if nonemptystr origin
1736 then
1737 Printf.bprintf bb "\n origin='%s'"
1738 (Parser.enent origin 0 (String.length origin));
1740 if anchor <> emptyanchor
1741 then (
1742 let n, rely, visy = anchor in
1743 Printf.bprintf bb "\n page='%d'" n;
1744 if rely > 1e-6
1745 then
1746 Printf.bprintf bb " rely='%f'" rely
1748 if abs_float visy > 1e-6
1749 then
1750 Printf.bprintf bb " visy='%f'" visy
1754 if pan != 0
1755 then Printf.bprintf bb " pan='%d'" pan;
1757 add_attrs bb false dc c time;
1758 let kb = keymapsbuf false dc c in
1760 begin match bookmarks with
1761 | [] ->
1762 if Buffer.length kb > 0
1763 then (
1764 Buffer.add_string bb ">\n";
1765 Buffer.add_buffer bb kb;
1766 Buffer.add_string bb "\n</doc>\n";
1768 else Buffer.add_string bb "/>\n"
1769 | _ ->
1770 Buffer.add_string bb ">\n<bookmarks>\n";
1771 List.iter (fun (title, _, kind) ->
1772 begin match kind with
1773 | Oanchor (page, rely, visy) ->
1774 Printf.bprintf bb
1775 "<item title='%s' page='%d'"
1776 (Parser.enent title 0 (String.length title))
1777 page
1779 if rely > 1e-6
1780 then
1781 Printf.bprintf bb " rely='%f'" rely
1783 if abs_float visy > 1e-6
1784 then
1785 Printf.bprintf bb " visy='%f'" visy
1787 | Ohistory _ | Onone | Ouri _ | Oremote _
1788 | Oremotedest _ | Olaunch _ ->
1789 failwith "unexpected link in bookmarks"
1790 end;
1791 Buffer.add_string bb "/>\n";
1792 ) bookmarks;
1793 Buffer.add_string bb "</bookmarks>";
1794 if Buffer.length kb > 0
1795 then (
1796 Buffer.add_string bb "\n";
1797 Buffer.add_buffer bb kb;
1799 Buffer.add_string bb "\n</doc>\n";
1800 end;
1804 let pan, conf =
1805 match state.mode with
1806 | Birdseye (c, pan, _, _, _) ->
1807 let beyecolumns =
1808 match conf.columns with
1809 | Cmulti ((c, _, _), _) -> Some c
1810 | Csingle _ -> None
1811 | Csplit _ -> None
1812 and columns =
1813 match c.columns with
1814 | Cmulti (c, _) -> Cmulti (c, E.a)
1815 | Csingle _ -> Csingle E.a
1816 | Csplit _ -> failwith "quit from bird's eye while split"
1818 pan, { c with beyecolumns = beyecolumns; columns = columns }
1819 | Textentry _
1820 | View
1821 | LinkNav _ -> x, conf
1823 let docpath = if nonemptystr state.path then abspath state.path else E.s in
1824 if nonemptystr docpath
1825 then (
1826 adddoc docpath pan (getanchor ())
1828 let autoscrollstep =
1829 match state.autoscroll with
1830 | Some step -> step
1831 | None -> conf.autoscrollstep
1833 begin match state.mode with
1834 | Birdseye beye -> leavebirdseye beye true
1835 | Textentry _
1836 | View
1837 | LinkNav _ -> ()
1838 end;
1839 { conf with autoscrollstep = autoscrollstep }
1841 (if conf.savebmarks then state.bookmarks else [])
1842 (now ())
1843 state.origin
1845 Hashtbl.iter (fun path (c, bookmarks, x, anchor, origin) ->
1846 if docpath <> abspath path
1847 then adddoc path x anchor c bookmarks c.lastvisit origin
1848 ) h;
1849 Buffer.add_string bb "</llppconfig>\n";
1850 true;
1853 let save leavebirdseye =
1854 let relx = float state.x /. float state.winw in
1855 let w, h, x =
1856 let cx w = truncate (relx *. float w) in
1857 List.fold_left
1858 (fun (w, h, x) ws ->
1859 match ws with
1860 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
1861 | Wsi.MaxVert -> (w, conf.cwinh, x)
1862 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
1864 (state.winw, state.winh, state.x) state.winstate
1866 conf.cwinw <- w;
1867 conf.cwinh <- h;
1868 let bb = Buffer.create 32768 in
1869 let save2 (h, dc) =
1870 save1 bb leavebirdseye x h dc
1872 if load1 save2 && Buffer.length bb > 0
1873 then
1875 let tmp = !confpath ^ ".tmp" in
1876 let oc = open_out_bin tmp in
1877 Buffer.output_buffer oc bb;
1878 close_out oc;
1879 Unix.rename tmp !confpath;
1880 with exn ->
1881 dolog "error saving configuration: %s" @@ exntos exn
1884 let gc fd =
1885 let wr s n =
1886 (* This here has a potential of rising SIGPIPE, silently and
1887 seemingly harmlessly (when -gc was supplied an invalid
1888 executable for instance) probably needs revisiting *)
1889 let n' = Unix.write fd (Bytes.of_string s) 0 n in
1890 if n != n'
1891 then Utils.error "Unix.write %d = %d" n n'
1893 let href = ref (Hashtbl.create 0) in
1894 let cref = ref defconf in
1895 let push (h, dc) =
1896 let f path (pc, _pb, _px, _pa, _po) =
1897 let s =
1898 Printf.sprintf "%s\000%ld\000" path (Int32.of_float pc.lastvisit)
1900 wr s (String.length s)
1902 Hashtbl.iter f h;
1903 href := h;
1904 cref := dc;
1905 true
1907 ignore (load1 push);
1908 Unix.shutdown fd Unix.SHUTDOWN_SEND;
1909 let s = fdcontents fd in
1910 let rec f ppos =
1911 match String.index_from s ppos '\000' with
1912 | exception Not_found -> ()
1913 | zpos1 ->
1914 match String.index_from s (zpos1+1) '\000' with
1915 | exception Not_found -> error "invalid gc input in (%S) at %d" s zpos1
1916 | zpos2 ->
1917 let okey = StringLabels.sub s ~pos:ppos ~len:(zpos1-ppos) in
1918 let nkey = StringLabels.sub s ~pos:(zpos1+1) ~len:(zpos2-zpos1-1) in
1919 if emptystr nkey
1920 then (Hashtbl.remove !href okey; f (zpos2+1))
1921 else
1922 match Hashtbl.find !href okey with
1923 | exception Not_found -> Utils.error "gc: cannot find %S" okey
1924 | v ->
1925 Hashtbl.remove !href okey;
1926 Hashtbl.replace !href nkey v;
1927 f (zpos2+1)
1929 f 0;
1930 let bb = Buffer.create 32768 in
1931 let save2 (_h, dc) = save1 bb (fun _ _ -> ()) 0 !href dc in
1932 if load1 save2 && Buffer.length bb > 0
1933 then (
1935 let tmp = !confpath ^ ".tmp" in
1936 let oc = open_out_bin tmp in
1937 Buffer.output_buffer oc bb;
1938 close_out oc;
1939 Unix.rename tmp !confpath;
1940 with exn ->
1941 dolog "error saving configuration: %s" @@ exntos exn
1945 let logcurrently = function
1946 | Idle -> dolog "Idle"
1947 | Loading (l, gen) ->
1948 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1949 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1950 dolog
1951 "Tiling %d[%d,%d] page=%s cs=%s angle"
1952 l.pageno col row (~> pageopaque)
1953 (CSTE.to_string colorspace)
1955 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1956 angle gen conf.angle state.gen
1957 tilew tileh
1958 conf.tilew conf.tileh
1960 | Outlining _ ->
1961 dolog "outlining"