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