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