Consistency
[llpp.git] / config.ml
blob8424f12eb7d04ec1b4428b598becfc4ba7f6bd75
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 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 * filename))
300 | Oaction of (unit -> unit)
301 | Oreaction of (unit -> outline array)
302 and outline = (caption * outlinelevel * outlinekind)
303 and outlinelevel = int
306 type page =
307 { pageno : int
308 ; pagedimno : int
309 ; pagew : int
310 ; pageh : int
311 ; pagex : int
312 ; pagey : int
313 ; pagevw : int
314 ; pagevh : int
315 ; pagedispx : int
316 ; pagedispy : int
317 ; pagecol : int
321 type tile = opaque * pixmapsize * elapsed
322 and elapsed = float;;
323 type pagemapkey = pageno * gen;;
324 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
325 and row = int
326 and col = int
327 and currently =
328 | Idle
329 | Loading of (page * gen)
330 | Tiling of (
331 page * opaque * colorspace * angle * gen * col * row * width * height
333 | Outlining of outline list
336 type mpos = int * int
337 and mstate =
338 | Msel of (mpos * mpos)
339 | Mpan of mpos
340 | Mscrolly | Mscrollx
341 | Mzoom of (buttonno * step)
342 | Mzoomrect of (mpos * mpos)
343 | Mnone
344 and buttonno = int
345 and step = int
348 type mode =
349 | Birdseye of (conf * leftx * pageno * pageno * anchor)
350 | Textentry of (textentry * onleave)
351 | View
352 | LinkNav of linktarget
353 and onleave = leavetextentrystatus -> unit
354 and leavetextentrystatus = | Cancel | Confirm
355 and helpitem = string * int * action
356 and action =
357 | Noaction
358 | Action of (uioh -> uioh)
359 and linktarget =
360 | Ltexact of (pageno * direction)
361 | Ltgendir of direction
362 | Ltnotready of (pageno * direction)
363 and direction = int (* -1, 0, 1 *)
364 and textentry = string * string * onhist option * onkey * ondone * cancelonempty
365 and onkey = string -> int -> te
366 and ondone = string -> unit
367 and histcancel = unit -> unit
368 and onhist = ((histcmd -> string) * histcancel)
369 and histcmd = HCnext | HCprev | HCfirst | HClast
370 and cancelonempty = bool
371 and te =
372 | TEstop
373 | TEdone of string
374 | TEcont of string
375 | TEswitch of textentry
378 type 'a circbuf =
379 { store : 'a array
380 ; mutable rc : int
381 ; mutable wc : int
382 ; mutable len : int
386 type state =
387 { mutable ss : Unix.file_descr
388 ; mutable wsfd : Unix.file_descr
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 ; ghyllscroll = None
546 ; columns = Csingle [||]
547 ; beyecolumns = None
548 ; updatecurs = false
549 ; hfsize = 12
550 ; pgscale = 1.0
551 ; usepbo = false
552 ; wheelbypage = false
553 ; stcmd = "echo SyncTex"
554 ; riani = false
555 ; pax = None
556 ; paxmark = Mark_word
557 ; leftscroll = false
558 ; title = E.s
559 ; lastvisit = 0.0
560 ; annotinline = true
561 ; keyhashes =
562 let mk n = (n, Hashtbl.create 1) in
563 [ mk "global"
564 ; mk "info"
565 ; mk "help"
566 ; mk "outline"
567 ; mk "listview"
568 ; mk "birdseye"
569 ; mk "textentry"
570 ; mk "links"
571 ; mk "view"
576 let conf = { defconf with angle = defconf.angle };;
578 let gotouri uri =
579 if emptystr conf.urilauncher
580 then dolog "%s" uri
581 else (
582 let url = geturl uri in
583 if emptystr url
584 then dolog "obtained empty url from uri %S\n" uri
585 else
586 let command = Str.global_replace percentsre url conf.urilauncher in
587 try ignore (popen command [])
588 with exn -> dolog "failed to execute `%s': %s" command @@ exntos exn
592 let makehelp () =
593 let strings =
594 version ()
595 :: "(searching in this text works just by typing (i.e. no initial '/'))"
596 :: E.s :: Help.keys
598 Array.of_list (
599 List.map (fun s ->
600 let url = geturl s in
601 if nonemptystr url
602 then (s, 0, Action (fun uioh -> gotouri url; uioh))
603 else (s, 0, Noaction)
604 ) strings);
607 let cbnew n v =
608 { store = Array.make n v
609 ; rc = 0
610 ; wc = 0
611 ; len = 0
615 let cbcap b = Array.length b.store;;
617 let cbput b v =
618 let cap = cbcap b in
619 b.store.(b.wc) <- v;
620 b.wc <- (b.wc + 1) mod cap;
621 b.rc <- b.wc;
622 b.len <- min (b.len + 1) cap;
625 let cbempty b = b.len = 0;;
627 let cbgetg b circular dir =
628 if cbempty b
629 then b.store.(0)
630 else
631 let rc = b.rc + dir in
632 let rc =
633 if circular
634 then (
635 if rc = -1
636 then b.len-1
637 else (
638 if rc >= b.len
639 then 0
640 else rc
643 else bound rc 0 (b.len-1)
645 b.rc <- rc;
646 b.store.(rc);
649 let cbget b = cbgetg b false;;
650 let cbgetc b = cbgetg b true;;
652 let state =
653 { ss = Unix.stdin
654 ; wsfd = Unix.stdin
655 ; stderr = Unix.stderr
656 ; errmsgs = Buffer.create 0
657 ; newerrmsgs = false
658 ; x = 0
659 ; y = 0
660 ; w = 0
661 ; anchor = emptyanchor
662 ; ranchors = []
663 ; layout = []
664 ; maxy = max_int
665 ; tilelru = Queue.create ()
666 ; pagemap = Hashtbl.create 10
667 ; tilemap = Hashtbl.create 10
668 ; pdims = []
669 ; pagecount = 0
670 ; currently = Idle
671 ; mstate = Mnone
672 ; rects = []
673 ; rects1 = []
674 ; text = E.s
675 ; mode = View
676 ; winstate = []
677 ; searchpattern = E.s
678 ; outlines = E.a
679 ; bookmarks = []
680 ; path = E.s
681 ; password = E.s
682 ; nameddest = E.s
683 ; geomcmds = E.s, []
684 ; hists =
685 { nav = cbnew 10 emptyanchor
686 ; pat = cbnew 10 E.s
687 ; pag = cbnew 10 E.s
688 ; sel = cbnew 10 E.s
690 ; memused = 0
691 ; gen = 0
692 ; throttle = None
693 ; autoscroll = None
694 ; ghyll = noghyll
695 ; help = makehelp ()
696 ; docinfo = []
697 ; checkerstexid = None
698 ; prevzoom = (1.0, 0)
699 ; progress = -1.0
700 ; uioh = nouioh
701 ; redisplay = true
702 ; mpos = (-1, -1)
703 ; keystate = KSnone
704 ; glinks = false
705 ; prevcolumns = None
706 ; winw = -1
707 ; winh = -1
708 ; reprf = noreprf
709 ; origin = E.s
710 ; roam = noroam
711 ; bzoom = false
712 ; traw = Raw.create_static `float ~len:8
713 ; vraw = Raw.create_static `float ~len:8
717 let copykeyhashes c =
718 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
721 let calcips h =
722 let d = state.winh - h in
723 max conf.interpagespace ((d + 1) / 2)
726 let rowyh (c, coverA, coverB) b n =
727 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
728 then
729 let _, _, vy, (_, _, h, _) = b.(n) in
730 (vy, h)
731 else
732 let n' = n - coverA in
733 let d = n' mod c in
734 let s = n - d in
735 let e = min state.pagecount (s + c) in
736 let rec find m miny maxh = if m = e then miny, maxh else
737 let _, _, y, (_, _, h, _) = b.(m) in
738 let miny = min miny y in
739 let maxh = max maxh h in
740 find (m+1) miny maxh
741 in find s max_int 0
744 let page_of_y y =
745 let ((c, coverA, coverB) as cl), b =
746 match conf.columns with
747 | Csingle b -> (1, 0, 0), b
748 | Cmulti (c, b) -> c, b
749 | Csplit (_, b) -> (1, 0, 0), b
751 if Array.length b = 0
752 then -1
753 else
754 let rec bsearch nmin nmax =
755 if nmin > nmax
756 then bound nmin 0 (state.pagecount-1)
757 else
758 let n = (nmax + nmin) / 2 in
759 let vy, h = rowyh cl b n in
760 let y0, y1 =
761 if conf.presentation
762 then
763 let ips = calcips h in
764 let y0 = vy - ips in
765 let y1 = vy + h + ips in
766 y0, y1
767 else (
768 if n = 0
769 then 0, vy + h + conf.interpagespace
770 else
771 let y0 = vy - conf.interpagespace in
772 y0, y0 + h + conf.interpagespace
775 if y >= y0 && y < y1
776 then (
777 if c = 1
778 then n
779 else (
780 if n > coverA
781 then
782 if n < state.pagecount - coverB
783 then ((n-coverA)/c)*c + coverA
784 else n
785 else n
788 else (
789 if y > y0
790 then bsearch (n+1) nmax
791 else bsearch nmin (n-1)
794 bsearch 0 (state.pagecount-1);
797 let calcheight () =
798 match conf.columns with
799 | Cmulti ((_, _, _) as cl, b) ->
800 if Array.length b > 0
801 then
802 let y, h = rowyh cl b (Array.length b - 1) in
803 y + h + (if conf.presentation then calcips h else 0)
804 else 0
805 | Csingle b ->
806 if Array.length b > 0
807 then
808 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
809 y + h + (if conf.presentation then calcips h else 0)
810 else 0
811 | Csplit (_, b) ->
812 if Array.length b > 0
813 then
814 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
815 y + h
816 else 0
819 let getpageywh pageno =
820 let pageno = bound pageno 0 (state.pagecount-1) in
821 match conf.columns with
822 | Csingle b ->
823 if Array.length b = 0
824 then 0, 0, 0
825 else
826 let (_, _, y, (_, w, h, _)) = b.(pageno) in
827 let y =
828 if conf.presentation
829 then y - calcips h
830 else y
832 y, w, h
833 | Cmulti (cl, b) ->
834 if Array.length b = 0
835 then 0, 0, 0
836 else
837 let y, h = rowyh cl b pageno in
838 let (_, _, _, (_, w, _, _)) = b.(pageno) in
839 let y =
840 if conf.presentation
841 then y - calcips h
842 else y
844 y, w, h
845 | Csplit (c, b) ->
846 if Array.length b = 0
847 then 0, 0, 0
848 else
849 let n = pageno*c in
850 let (_, _, y, (_, w, h, _)) = b.(n) in
851 y, w / c, h
854 let getpageyh pageno =
855 let y,_,h = getpageywh pageno in
856 y, h;
859 let getpagedim pageno =
860 let rec f ppdim l =
861 match l with
862 | (n, _, _, _) as pdim :: rest ->
863 if n >= pageno
864 then (if n = pageno then pdim else ppdim)
865 else f pdim rest
867 | [] -> ppdim
869 f (-1, -1, -1, -1) state.pdims
872 let getpagey pageno = fst (getpageyh pageno);;
874 let getanchor1 l =
875 let top =
876 let coloff = l.pagecol * l.pageh in
877 float (l.pagey + coloff) /. float l.pageh
879 let dtop =
880 if l.pagedispy = 0
881 then
883 else (
884 if conf.presentation
885 then float l.pagedispy /. float (calcips l.pageh)
886 else float l.pagedispy /. float conf.interpagespace
889 (l.pageno, top, dtop)
892 let getanchor () =
893 match state.layout with
894 | l :: _ -> getanchor1 l
895 | [] ->
896 let n = page_of_y state.y in
897 if n = -1
898 then state.anchor
899 else
900 let y, h = getpageyh n in
901 let dy = y - state.y in
902 let dtop =
903 if conf.presentation
904 then
905 let ips = calcips h in
906 float (dy + ips) /. float ips
907 else
908 float dy /. float conf.interpagespace
910 (n, 0.0, dtop)
913 let fontpath = ref E.s;;
915 type historder = [ `lastvisit | `title | `path | `file ];;
917 module KeyMap =
918 Map.Make (struct type t = (int * int) let compare = compare end);;
920 let unentS s =
921 let l = String.length s in
922 let b = Buffer.create l in
923 Parser.unent b s 0 l;
924 Buffer.contents b;
927 let home =
928 try Sys.getenv "HOME"
929 with exn ->
930 dolog "cannot determine home directory location: %s" @@ exntos exn;
934 let modifier_of_string = function
935 | "alt" -> Wsi.altmask
936 | "shift" -> Wsi.shiftmask
937 | "ctrl" | "control" -> Wsi.ctrlmask
938 | "meta" -> Wsi.metamask
939 | _ -> 0
942 let keys_of_string s =
943 let key_of_string r s =
944 let elems = Str.full_split r s in
945 let f n k m =
946 let g s =
947 let m1 = modifier_of_string s in
948 if m1 = 0
949 then (Wsi.namekey s, m)
950 else (k, m lor m1)
951 in function
952 | Str.Delim s when n land 1 = 0 -> g s
953 | Str.Text s -> g s
954 | Str.Delim _ -> (k, m)
956 let rec loop n k m = function
957 | [] -> (k, m)
958 | x :: xs ->
959 let k, m = f n k m x in
960 loop (n+1) k m xs
962 loop 0 0 0 elems
964 let elems = Str.split whitere s in
965 List.map (key_of_string (Str.regexp "-")) elems
968 let config_of c attrs =
969 let apply c k v =
971 match k with
972 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
973 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
974 | "case-insensitive-search" -> { c with icase = bool_of_string v }
975 | "preload" -> { c with preload = bool_of_string v }
976 | "page-bias" -> { c with pagebias = int_of_string v }
977 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
978 | "horizontal-scroll-step" ->
979 { c with hscrollstep = max (int_of_string v) 1 }
980 | "auto-scroll-step" ->
981 { c with autoscrollstep = max 0 (int_of_string v) }
982 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
983 | "crop-hack" -> { c with crophack = bool_of_string v }
984 | "throttle" ->
985 let mw =
986 match String.lowercase v with
987 | "true" -> Some infinity
988 | "false" -> None
989 | f -> Some (float_of_string f)
991 { c with maxwait = mw }
992 | "highlight-links" -> { c with hlinks = bool_of_string v }
993 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
994 | "vertical-margin" ->
995 { c with interpagespace = max 0 (int_of_string v) }
996 | "zoom" ->
997 let zoom = float_of_string v /. 100. in
998 let zoom = max zoom 0.0 in
999 { c with zoom = zoom }
1000 | "presentation" -> { c with presentation = bool_of_string v }
1001 | "rotation-angle" -> { c with angle = int_of_string v }
1002 | "width" -> { c with cwinw = max 20 (int_of_string v) }
1003 | "height" -> { c with cwinh = max 20 (int_of_string v) }
1004 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
1005 | "proportional-display" ->
1006 let fm =
1007 if bool_of_string v
1008 then FitProportional
1009 else FitWidth
1011 { c with fitmodel = fm }
1012 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
1013 | "pixmap-cache-size" ->
1014 { c with memlimit = max 2 (int_of_string_with_suffix v) }
1015 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
1016 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
1017 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
1018 | "persistent-location" -> { c with jumpback = bool_of_string v }
1019 | "background-color" -> { c with bgcolor = color_of_string v }
1020 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
1021 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
1022 | "mupdf-store-size" ->
1023 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
1024 | "checkers" -> { c with checkers = bool_of_string v }
1025 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
1026 | "trim-margins" -> { c with trimmargins = bool_of_string v }
1027 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
1028 | "uri-launcher" -> { c with urilauncher = unentS v }
1029 | "path-launcher" -> { c with pathlauncher = unentS v }
1030 | "color-space" -> { c with colorspace = CSTE.of_string v }
1031 | "invert-colors" -> { c with invert = bool_of_string v }
1032 | "brightness" -> { c with colorscale = float_of_string v }
1033 | "ghyllscroll" -> { c with ghyllscroll = ghyllscroll_of_string v }
1034 | "columns" ->
1035 let (n, _, _) as nab = multicolumns_of_string v in
1036 if n < 0
1037 then { c with columns = Csplit (-n, E.a) }
1038 else { c with columns = Cmulti (nab, E.a) }
1039 | "birds-eye-columns" ->
1040 { c with beyecolumns = Some (max (int_of_string v) 2) }
1041 | "selection-command" -> { c with selcmd = unentS v }
1042 | "synctex-command" -> { c with stcmd = unentS v }
1043 | "pax-command" -> { c with paxcmd = unentS v }
1044 | "askpass-command" -> { c with passcmd = unentS v }
1045 | "savepath-command" -> { c with savecmd = unentS v }
1046 | "update-cursor" -> { c with updatecurs = bool_of_string v }
1047 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
1048 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
1049 | "use-pbo" -> { c with usepbo = bool_of_string v }
1050 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
1051 | "horizontal-scrollbar-visible" ->
1052 let b =
1053 if bool_of_string v
1054 then c.scrollb lor scrollbhv
1055 else c.scrollb land (lnot scrollbhv)
1057 { c with scrollb = b }
1058 | "vertical-scrollbar-visible" ->
1059 let b =
1060 if bool_of_string v
1061 then c.scrollb lor scrollbvv
1062 else c.scrollb land (lnot scrollbvv)
1064 { c with scrollb = b }
1065 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
1066 | "point-and-x" ->
1067 { c with pax =
1068 if bool_of_string v
1069 then Some (ref (0.0, 0, 0))
1070 else None }
1071 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
1072 | "scroll-bar-on-the-left" -> { c with leftscroll = bool_of_string v }
1073 | "title" -> { c with title = unentS v }
1074 | "last-visit" -> { c with lastvisit = float_of_string v }
1075 | "edit-annotations-inline" -> { c with annotinline = bool_of_string v }
1076 | _ -> c
1077 with exn ->
1078 dolog "error processing attribute (`%S' = `%S'): %s" k v @@ exntos exn;
1081 let rec fold c = function
1082 | [] -> c
1083 | (k, v) :: rest ->
1084 let c = apply c k v in
1085 fold c rest
1087 fold { c with keyhashes = copykeyhashes c } attrs;
1090 let fromstring f pos n v d =
1091 try f v
1092 with exn ->
1093 dolog "error processing attribute (%S=%S) at %d\n%s" n v pos @@ exntos exn;
1097 let bookmark_of attrs =
1098 let rec fold title page rely visy = function
1099 | ("title", v) :: rest -> fold v page rely visy rest
1100 | ("page", v) :: rest -> fold title v rely visy rest
1101 | ("rely", v) :: rest -> fold title page v visy rest
1102 | ("visy", v) :: rest -> fold title page rely v rest
1103 | _ :: rest -> fold title page rely visy rest
1104 | [] -> title, page, rely, visy
1106 fold "invalid" "0" "0" "0" attrs
1109 let doc_of attrs =
1110 let rec fold path page rely pan visy origin = function
1111 | ("path", v) :: rest -> fold v page rely pan visy origin rest
1112 | ("page", v) :: rest -> fold path v rely pan visy origin rest
1113 | ("rely", v) :: rest -> fold path page v pan visy origin rest
1114 | ("pan", v) :: rest -> fold path page rely v visy origin rest
1115 | ("visy", v) :: rest -> fold path page rely pan v origin rest
1116 | ("origin", v) :: rest -> fold path page rely pan visy v rest
1117 | _ :: rest -> fold path page rely pan visy origin rest
1118 | [] -> path, page, rely, pan, visy, origin
1120 fold E.s "0" "0" "0" "0" E.s attrs
1123 let map_of attrs =
1124 let rec fold rs ls = function
1125 | ("out", v) :: rest -> fold v ls rest
1126 | ("in", v) :: rest -> fold rs v rest
1127 | _ :: rest -> fold ls rs rest
1128 | [] -> ls, rs
1130 fold E.s E.s attrs
1133 let setconf dst src =
1134 dst.scrollbw <- src.scrollbw;
1135 dst.scrollh <- src.scrollh;
1136 dst.icase <- src.icase;
1137 dst.preload <- src.preload;
1138 dst.pagebias <- src.pagebias;
1139 dst.verbose <- src.verbose;
1140 dst.scrollstep <- src.scrollstep;
1141 dst.maxhfit <- src.maxhfit;
1142 dst.crophack <- src.crophack;
1143 dst.autoscrollstep <- src.autoscrollstep;
1144 dst.maxwait <- src.maxwait;
1145 dst.hlinks <- src.hlinks;
1146 dst.underinfo <- src.underinfo;
1147 dst.interpagespace <- src.interpagespace;
1148 dst.zoom <- src.zoom;
1149 dst.presentation <- src.presentation;
1150 dst.angle <- src.angle;
1151 dst.cwinw <- src.cwinw;
1152 dst.cwinh <- src.cwinh;
1153 dst.savebmarks <- src.savebmarks;
1154 dst.memlimit <- src.memlimit;
1155 dst.fitmodel <- src.fitmodel;
1156 dst.texcount <- src.texcount;
1157 dst.sliceheight <- src.sliceheight;
1158 dst.thumbw <- src.thumbw;
1159 dst.jumpback <- src.jumpback;
1160 dst.bgcolor <- src.bgcolor;
1161 dst.tilew <- src.tilew;
1162 dst.tileh <- src.tileh;
1163 dst.mustoresize <- src.mustoresize;
1164 dst.checkers <- src.checkers;
1165 dst.aalevel <- src.aalevel;
1166 dst.trimmargins <- src.trimmargins;
1167 dst.trimfuzz <- src.trimfuzz;
1168 dst.urilauncher <- src.urilauncher;
1169 dst.colorspace <- src.colorspace;
1170 dst.invert <- src.invert;
1171 dst.colorscale <- src.colorscale;
1172 dst.ghyllscroll <- src.ghyllscroll;
1173 dst.columns <- src.columns;
1174 dst.beyecolumns <- src.beyecolumns;
1175 dst.selcmd <- src.selcmd;
1176 dst.updatecurs <- src.updatecurs;
1177 dst.pathlauncher <- src.pathlauncher;
1178 dst.keyhashes <- copykeyhashes src;
1179 dst.hfsize <- src.hfsize;
1180 dst.hscrollstep <- src.hscrollstep;
1181 dst.pgscale <- src.pgscale;
1182 dst.usepbo <- src.usepbo;
1183 dst.wheelbypage <- src.wheelbypage;
1184 dst.stcmd <- src.stcmd;
1185 dst.paxcmd <- src.paxcmd;
1186 dst.passcmd <- src.passcmd;
1187 dst.savecmd <- src.savecmd;
1188 dst.scrollb <- src.scrollb;
1189 dst.riani <- src.riani;
1190 dst.paxmark <- src.paxmark;
1191 dst.leftscroll <- src.leftscroll;
1192 dst.title <- src.title;
1193 dst.annotinline <- src.annotinline;
1194 dst.pax <-
1195 if src.pax = None
1196 then None
1197 else Some ((ref (0.0, 0, 0)));
1200 let findkeyhash c name =
1201 try List.assoc name c.keyhashes
1202 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
1205 let get s =
1206 let open Parser in
1207 let h = Hashtbl.create 10 in
1208 let dc = { defconf with angle = defconf.angle } in
1209 let rec toplevel v t spos _ =
1210 match t with
1211 | Vdata | Vcdata | Vend -> v
1212 | Vopen ("llppconfig", _, closed) ->
1213 if closed
1214 then v
1215 else { v with f = llppconfig }
1216 | Vopen _ ->
1217 error "unexpected subelement at top level" s spos
1218 | Vclose _ -> error "unexpected close at top level" s spos
1220 and llppconfig v t spos _ =
1221 match t with
1222 | Vdata | Vcdata -> v
1223 | Vend -> error "unexpected end of input in llppconfig" s spos
1224 | Vopen ("defaults", attrs, closed) ->
1225 let c = config_of dc attrs in
1226 setconf dc c;
1227 if closed
1228 then v
1229 else { v with f = defaults }
1231 | Vopen ("ui-font", attrs, closed) ->
1232 let rec getsize size = function
1233 | [] -> size
1234 | ("size", v) :: rest ->
1235 let size =
1236 fromstring int_of_string spos "size" v fstate.fontsize in
1237 getsize size rest
1238 | l -> getsize size l
1240 fstate.fontsize <- getsize fstate.fontsize attrs;
1241 if closed
1242 then v
1243 else { v with f = uifont (Buffer.create 10) }
1245 | Vopen ("doc", attrs, closed) ->
1246 let pathent, spage, srely, span, svisy, origin = doc_of attrs in
1247 let path = unentS pathent
1248 and origin = unentS origin
1249 and pageno = fromstring int_of_string spos "page" spage 0
1250 and rely = fromstring float_of_string spos "rely" srely 0.0
1251 and pan = fromstring int_of_string spos "pan" span 0
1252 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1253 let c = config_of dc attrs in
1254 let anchor = (pageno, rely, visy) in
1255 if closed
1256 then (Hashtbl.add h path (c, [], pan, anchor, origin); v)
1257 else { v with f = doc path origin pan anchor c [] }
1259 | Vopen _ ->
1260 error "unexpected subelement in llppconfig" s spos
1262 | Vclose "llppconfig" -> { v with f = toplevel }
1263 | Vclose _ -> error "unexpected close in llppconfig" s spos
1265 and defaults v t spos _ =
1266 match t with
1267 | Vdata | Vcdata -> v
1268 | Vend -> error "unexpected end of input in defaults" s spos
1269 | Vopen ("keymap", attrs, closed) ->
1270 let modename =
1271 try List.assoc "mode" attrs
1272 with Not_found -> "global" in
1273 if closed
1274 then v
1275 else
1276 let ret keymap =
1277 let h = findkeyhash dc modename in
1278 KeyMap.iter (Hashtbl.replace h) keymap;
1279 defaults
1281 { v with f = pkeymap ret KeyMap.empty }
1283 | Vopen (_, _, _) ->
1284 error "unexpected subelement in defaults" s spos
1286 | Vclose "defaults" ->
1287 { v with f = llppconfig }
1289 | Vclose _ -> error "unexpected close in defaults" s spos
1291 and uifont b v t spos epos =
1292 match t with
1293 | Vdata | Vcdata ->
1294 Buffer.add_substring b s spos (epos - spos);
1296 | Vopen (_, _, _) ->
1297 error "unexpected subelement in ui-font" s spos
1298 | Vclose "ui-font" ->
1299 if emptystr !fontpath
1300 then fontpath := Buffer.contents b;
1301 { v with f = llppconfig }
1302 | Vclose _ -> error "unexpected close in ui-font" s spos
1303 | Vend -> error "unexpected end of input in ui-font" s spos
1305 and doc path origin pan anchor c bookmarks v t spos _ =
1306 match t with
1307 | Vdata | Vcdata -> v
1308 | Vend -> error "unexpected end of input in doc" s spos
1309 | Vopen ("bookmarks", _, closed) ->
1310 if closed
1311 then v
1312 else { v with f = pbookmarks path origin pan anchor c bookmarks }
1314 | Vopen ("keymap", attrs, closed) ->
1315 let modename =
1316 try List.assoc "mode" attrs
1317 with Not_found -> "global"
1319 if closed
1320 then v
1321 else
1322 let ret keymap =
1323 let h = findkeyhash c modename in
1324 KeyMap.iter (Hashtbl.replace h) keymap;
1325 doc path origin pan anchor c bookmarks
1327 { v with f = pkeymap ret KeyMap.empty }
1329 | Vopen (_, _, _) ->
1330 error "unexpected subelement in doc" s spos
1332 | Vclose "doc" ->
1333 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor, origin);
1334 { v with f = llppconfig }
1336 | Vclose _ -> error "unexpected close in doc" s spos
1338 and pkeymap ret keymap v t spos _ =
1339 match t with
1340 | Vdata | Vcdata -> v
1341 | Vend -> error "unexpected end of input in keymap" s spos
1342 | Vopen ("map", attrs, closed) ->
1343 let r, l = map_of attrs in
1344 let kss = fromstring keys_of_string spos "in" r [] in
1345 let lss = fromstring keys_of_string spos "out" l [] in
1346 let keymap =
1347 match kss with
1348 | [] -> keymap
1349 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
1350 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
1352 if closed
1353 then { v with f = pkeymap ret keymap }
1354 else
1355 let f () = v in
1356 { v with f = skip "map" f }
1358 | Vopen _ ->
1359 error "unexpected subelement in keymap" s spos
1361 | Vclose "keymap" ->
1362 { v with f = ret keymap }
1364 | Vclose _ -> error "unexpected close in keymap" s spos
1366 and pbookmarks path origin pan anchor c bookmarks v t spos _ =
1367 match t with
1368 | Vdata | Vcdata -> v
1369 | Vend -> error "unexpected end of input in bookmarks" s spos
1370 | Vopen ("item", attrs, closed) ->
1371 let titleent, spage, srely, svisy = bookmark_of attrs in
1372 let page = fromstring int_of_string spos "page" spage 0
1373 and rely = fromstring float_of_string spos "rely" srely 0.0
1374 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1375 let bookmarks =
1376 (unentS titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
1378 if closed
1379 then { v with f = pbookmarks path origin pan anchor c bookmarks }
1380 else
1381 let f () = v in
1382 { v with f = skip "item" f }
1384 | Vopen _ ->
1385 error "unexpected subelement in bookmarks" s spos
1387 | Vclose "bookmarks" ->
1388 { v with f = doc path origin pan anchor c bookmarks }
1390 | Vclose _ -> Parser.parse_error "unexpected close in bookmarks" s spos
1392 and skip tag f v t spos _ =
1393 match t with
1394 | Vdata | Vcdata -> v
1395 | Vend ->
1396 Parser.parse_error ("unexpected end of input in skipped " ^ tag) s spos
1397 | Vopen (tag', _, closed) ->
1398 if closed
1399 then v
1400 else
1401 let f' () = { v with f = skip tag f } in
1402 { v with f = skip tag' f' }
1403 | Vclose ctag ->
1404 if tag = ctag
1405 then f ()
1406 else Parser.parse_error ("unexpected close in skipped " ^ tag) s spos
1409 parse { f = toplevel; accu = () } s;
1410 h, dc;
1413 let do_load f contents =
1414 try f contents
1415 with
1416 | Parser.Parse_error (msg, s, pos) ->
1417 let subs = Parser.subs s pos in
1418 Utils.error "parse error: %s: at %d [..%S..]" msg pos subs
1420 | exn -> Utils.error "parse error: %s" @@ exntos exn
1423 let defconfpath =
1424 let dir =
1425 let xdgconfdir = Utils.getenvwithdef "XDG_CONFIG_HOME" E.s in
1426 if emptystr xdgconfdir
1427 then
1429 let dir = Filename.concat home ".config" in
1430 if Sys.is_directory dir then dir else home
1431 with _ -> home
1432 else xdgconfdir
1434 Filename.concat dir "llpp.conf"
1437 let confpath = ref defconfpath;;
1439 let load2 f default =
1440 match filecontents !confpath with
1441 | contents -> f @@ do_load get contents
1442 | exception Unix.Unix_error (Unix.ENOENT, "open", _) ->
1443 f (Hashtbl.create 0, defconf)
1444 | exception exn ->
1445 dolog "error loading configuration from `%S': %s" !confpath @@ exntos exn;
1446 default
1449 let load1 f = load2 f false;;
1451 let load openlast =
1452 let f (h, dc) =
1453 if openlast
1454 then (
1455 let path, _ =
1456 Hashtbl.fold
1457 (fun path (conf, _, _, _, _) ((_, besttime) as best) ->
1458 if conf.lastvisit > besttime
1459 then (path, conf.lastvisit)
1460 else best)
1462 (state.path, -.infinity)
1464 state.path <- path;
1466 let pc, pb, px, pa, po =
1468 let absname = abspath state.path in
1469 Hashtbl.find h absname
1470 with Not_found -> dc, [], 0, emptyanchor, state.origin
1472 setconf defconf dc;
1473 setconf conf pc;
1474 state.bookmarks <- pb;
1475 state.x <- px;
1476 state.origin <- po;
1477 if conf.jumpback
1478 then state.anchor <- pa;
1479 cbput state.hists.nav pa;
1480 true
1482 load1 f
1485 let gethist () =
1486 let f (h, _) =
1487 Hashtbl.fold (fun path (pc, pb, px, pa, po) accu ->
1488 (path, pc, pb, px, pa, po) :: accu)
1489 h [];
1491 load2 f []
1494 let add_attrs bb always dc c time =
1495 let o' fmt s =
1496 Buffer.add_string bb "\n ";
1497 Printf.bprintf bb fmt s
1499 let o c fmt s = if c then o' fmt s else ignore in
1500 let ob s a b = o (always || a != b) "%s='%b'" s a
1501 and op s a b = o (always || a <> b) "%s='%b'" s (a != None)
1502 and oi s a b = o (always || a != b) "%s='%d'" s a
1503 and oI s a b = o (always || a != b) "%s='%s'" s (string_with_suffix_of_int a)
1504 and oz s a b = o (always || a <> b) "%s='%g'" s (a*.100.)
1505 and oF s a b = o (always || a <> b) "%s='%f'" s a
1506 and oL s a b = o (always || a <> b) "%s='%Ld'" s a
1507 and oc s a b = o (always || a <> b) "%s='%s'" s (color_to_string a)
1508 and oC s a b = o (always || a <> b) "%s='%s'" s (CSTE.to_string a)
1509 and oR s a b = o (always || a <> b) "%s='%s'" s (irect_to_string a)
1510 and oFm s a b = o (always || a <> b) "%s='%s'" s (FMTE.to_string a)
1511 and oSv s a b m = o (always || a <> b) "%s='%b'" s (a land m != 0)
1512 and oPm s a b = o (always || a <> b) "%s='%s'" s (MTE.to_string a)
1513 and os s a b =
1514 o (always || a <> b) "%s='%s'" s @@ Parser.enent a 0 (String.length a)
1515 and og s a b =
1516 if always || a <> b
1517 then
1518 match a with
1519 | Some (_N, _A, _B) -> o' "%s='%u,%u,%u'" s _N _A _B
1520 | None ->
1521 match b with
1522 | None -> ()
1523 | _ -> o' "%s='none'" s
1524 and oW s a b =
1525 if always || a <> b
1526 then
1527 let v =
1528 match a with
1529 | None -> "false"
1530 | Some f ->
1531 if f = infinity
1532 then "true"
1533 else string_of_float f
1535 o' "%s='%s'" s v
1536 and oco s a b =
1537 if always || a <> b
1538 then
1539 match a with
1540 | Cmulti ((n, a, b), _) when n > 1 -> o' "%s='%d,%d,%d'" s n a b
1541 | Csplit (n, _) when n > 1 -> o' "%s='%d'" s ~-n
1542 | Cmulti _ | Csplit _ | Csingle _ -> ()
1543 and obeco s a b =
1544 if always || a <> b
1545 then
1546 match a with
1547 | Some c when c > 1 -> o' "%s='%d'" s c
1548 | _ -> ()
1550 oi "width" c.cwinw dc.cwinw;
1551 oi "height" c.cwinh dc.cwinh;
1552 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
1553 oi "scroll-handle-height" c.scrollh dc.scrollh;
1554 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
1555 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
1556 ob "case-insensitive-search" c.icase dc.icase;
1557 ob "preload" c.preload dc.preload;
1558 oi "page-bias" c.pagebias dc.pagebias;
1559 oi "scroll-step" c.scrollstep dc.scrollstep;
1560 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
1561 ob "max-height-fit" c.maxhfit dc.maxhfit;
1562 ob "crop-hack" c.crophack dc.crophack;
1563 oW "throttle" c.maxwait dc.maxwait;
1564 ob "highlight-links" c.hlinks dc.hlinks;
1565 ob "under-cursor-info" c.underinfo dc.underinfo;
1566 oi "vertical-margin" c.interpagespace dc.interpagespace;
1567 oz "zoom" c.zoom dc.zoom;
1568 ob "presentation" c.presentation dc.presentation;
1569 oi "rotation-angle" c.angle dc.angle;
1570 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
1571 oFm "fit-model" c.fitmodel dc.fitmodel;
1572 oI "pixmap-cache-size" c.memlimit dc.memlimit;
1573 oi "tex-count" c.texcount dc.texcount;
1574 oi "slice-height" c.sliceheight dc.sliceheight;
1575 oi "thumbnail-width" c.thumbw dc.thumbw;
1576 ob "persistent-location" c.jumpback dc.jumpback;
1577 oc "background-color" c.bgcolor dc.bgcolor;
1578 oi "tile-width" c.tilew dc.tilew;
1579 oi "tile-height" c.tileh dc.tileh;
1580 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
1581 ob "checkers" c.checkers dc.checkers;
1582 oi "aalevel" c.aalevel dc.aalevel;
1583 ob "trim-margins" c.trimmargins dc.trimmargins;
1584 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
1585 os "uri-launcher" c.urilauncher dc.urilauncher;
1586 os "path-launcher" c.pathlauncher dc.pathlauncher;
1587 oC "color-space" c.colorspace dc.colorspace;
1588 ob "invert-colors" c.invert dc.invert;
1589 oF "brightness" c.colorscale dc.colorscale;
1590 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
1591 oco "columns" c.columns dc.columns;
1592 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
1593 os "selection-command" c.selcmd dc.selcmd;
1594 os "synctex-command" c.stcmd dc.stcmd;
1595 os "pax-command" c.paxcmd dc.paxcmd;
1596 os "askpass-command" c.passcmd dc.passcmd;
1597 os "savepath-command" c.savecmd dc.savecmd;
1598 ob "update-cursor" c.updatecurs dc.updatecurs;
1599 oi "hint-font-size" c.hfsize dc.hfsize;
1600 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
1601 oF "page-scroll-scale" c.pgscale dc.pgscale;
1602 ob "use-pbo" c.usepbo dc.usepbo;
1603 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
1604 ob "remote-in-a-new-instance" c.riani dc.riani;
1605 op "point-and-x" c.pax dc.pax;
1606 oPm "point-and-x-mark" c.paxmark dc.paxmark;
1607 ob "scroll-bar-on-the-left" c.leftscroll dc.leftscroll;
1608 if not always
1609 then os "title" c.title dc.title;
1610 oL "last-visit" (Int64.of_float time) 0L;
1611 ob "edit-annotations-inline" c.annotinline dc.annotinline;
1614 let keymapsbuf always dc c =
1615 let bb = Buffer.create 16 in
1616 let rec loop = function
1617 | [] -> ()
1618 | (modename, h) :: rest ->
1619 let dh = findkeyhash dc modename in
1620 if always || h <> dh
1621 then (
1622 if Hashtbl.length h > 0
1623 then (
1624 if Buffer.length bb > 0
1625 then Buffer.add_char bb '\n';
1626 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
1627 Hashtbl.iter (fun i o ->
1628 let isdifferent = always ||
1630 let dO = Hashtbl.find dh i in
1631 dO <> o
1632 with Not_found -> true
1634 if isdifferent
1635 then
1636 let addkm (k, m) =
1637 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
1638 if Wsi.withalt m then Buffer.add_string bb "alt-";
1639 if Wsi.withshift m then Buffer.add_string bb "shift-";
1640 if Wsi.withmeta m then Buffer.add_string bb "meta-";
1641 Buffer.add_string bb (Wsi.keyname k);
1643 let addkms l =
1644 let rec loop = function
1645 | [] -> ()
1646 | km :: [] -> addkm km
1647 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
1649 loop l
1651 Buffer.add_string bb "<map in='";
1652 addkm i;
1653 match o with
1654 | KMinsrt km ->
1655 Buffer.add_string bb "' out='";
1656 addkm km;
1657 Buffer.add_string bb "'/>\n"
1659 | KMinsrl kms ->
1660 Buffer.add_string bb "' out='";
1661 addkms kms;
1662 Buffer.add_string bb "'/>\n"
1664 | KMmulti (ins, kms) ->
1665 Buffer.add_char bb ' ';
1666 addkms ins;
1667 Buffer.add_string bb "' out='";
1668 addkms kms;
1669 Buffer.add_string bb "'/>\n"
1670 ) h;
1671 Buffer.add_string bb "</keymap>";
1674 loop rest
1676 loop c.keyhashes;
1680 let save1 bb leavebirdseye x h dc =
1681 let uifontsize = fstate.fontsize in
1682 let dc = if conf.bedefault then conf else dc in
1683 Buffer.add_string bb "<llppconfig>\n";
1685 if nonemptystr !fontpath
1686 then
1687 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
1688 uifontsize
1689 !fontpath
1690 else (
1691 if uifontsize <> 14
1692 then
1693 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
1696 Buffer.add_string bb "<defaults";
1697 add_attrs bb true dc dc nan;
1698 let kb = keymapsbuf true dc dc in
1699 if Buffer.length kb > 0
1700 then (
1701 Buffer.add_string bb ">\n";
1702 Buffer.add_buffer bb kb;
1703 Buffer.add_string bb "\n</defaults>\n";
1705 else Buffer.add_string bb "/>\n";
1707 let adddoc path pan anchor c bookmarks time origin =
1708 if bookmarks == [] && c = dc && anchor = emptyanchor
1709 then ()
1710 else (
1711 Printf.bprintf bb "<doc path='%s'"
1712 (Parser.enent path 0 (String.length path));
1714 if nonemptystr origin
1715 then
1716 Printf.bprintf bb "\n origin='%s'"
1717 (Parser.enent origin 0 (String.length origin));
1719 if anchor <> emptyanchor
1720 then (
1721 let n, rely, visy = anchor in
1722 Printf.bprintf bb "\n page='%d'" n;
1723 if rely > 1e-6
1724 then
1725 Printf.bprintf bb " rely='%f'" rely
1727 if abs_float visy > 1e-6
1728 then
1729 Printf.bprintf bb " visy='%f'" visy
1733 if pan != 0
1734 then Printf.bprintf bb " pan='%d'" pan;
1736 add_attrs bb false dc c time;
1737 let kb = keymapsbuf false dc c in
1739 begin match bookmarks with
1740 | [] ->
1741 if Buffer.length kb > 0
1742 then (
1743 Buffer.add_string bb ">\n";
1744 Buffer.add_buffer bb kb;
1745 Buffer.add_string bb "\n</doc>\n";
1747 else Buffer.add_string bb "/>\n"
1748 | _ ->
1749 Buffer.add_string bb ">\n<bookmarks>\n";
1750 List.iter (fun (title, _, kind) ->
1751 begin match kind with
1752 | Oanchor (page, rely, visy) ->
1753 Printf.bprintf bb
1754 "<item title='%s' page='%d'"
1755 (Parser.enent title 0 (String.length title))
1756 page
1758 if rely > 1e-6
1759 then
1760 Printf.bprintf bb " rely='%f'" rely
1762 if abs_float visy > 1e-6
1763 then
1764 Printf.bprintf bb " visy='%f'" visy
1766 | Ohistory _ | Onone | Ouri _ | Oremote _
1767 | Oremotedest _ | Olaunch _ | Oaction _ | Oreaction _ ->
1768 failwith "unexpected link in bookmarks"
1769 end;
1770 Buffer.add_string bb "/>\n";
1771 ) bookmarks;
1772 Buffer.add_string bb "</bookmarks>";
1773 if Buffer.length kb > 0
1774 then (
1775 Buffer.add_string bb "\n";
1776 Buffer.add_buffer bb kb;
1778 Buffer.add_string bb "\n</doc>\n";
1779 end;
1783 let pan, conf =
1784 match state.mode with
1785 | Birdseye (c, pan, _, _, _) ->
1786 let beyecolumns =
1787 match conf.columns with
1788 | Cmulti ((c, _, _), _) -> Some c
1789 | Csingle _ -> None
1790 | Csplit _ -> None
1791 and columns =
1792 match c.columns with
1793 | Cmulti (c, _) -> Cmulti (c, E.a)
1794 | Csingle _ -> Csingle E.a
1795 | Csplit _ -> failwith "quit from bird's eye while split"
1797 pan, { c with beyecolumns = beyecolumns; columns = columns }
1798 | Textentry _
1799 | View
1800 | LinkNav _ -> x, conf
1802 let docpath = if nonemptystr state.path then abspath state.path else E.s in
1803 if nonemptystr docpath
1804 then (
1805 adddoc docpath pan (getanchor ())
1807 let autoscrollstep =
1808 match state.autoscroll with
1809 | Some step -> step
1810 | None -> conf.autoscrollstep
1812 begin match state.mode with
1813 | Birdseye beye -> leavebirdseye beye true
1814 | Textentry _
1815 | View
1816 | LinkNav _ -> ()
1817 end;
1818 { conf with autoscrollstep = autoscrollstep }
1820 (if conf.savebmarks then state.bookmarks else [])
1821 (now ())
1822 state.origin
1824 Hashtbl.iter (fun path (c, bookmarks, x, anchor, origin) ->
1825 if docpath <> abspath path
1826 then adddoc path x anchor c bookmarks c.lastvisit origin
1827 ) h;
1828 Buffer.add_string bb "</llppconfig>\n";
1829 true;
1832 let save leavebirdseye =
1833 let relx = float state.x /. float state.winw in
1834 let w, h, x =
1835 let cx w = truncate (relx *. float w) in
1836 List.fold_left
1837 (fun (w, h, x) ws ->
1838 match ws with
1839 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
1840 | Wsi.MaxVert -> (w, conf.cwinh, x)
1841 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
1843 (state.winw, state.winh, state.x) state.winstate
1845 conf.cwinw <- w;
1846 conf.cwinh <- h;
1847 let bb = Buffer.create 32768 in
1848 let save2 (h, dc) =
1849 save1 bb leavebirdseye x h dc
1851 if load1 save2 && Buffer.length bb > 0
1852 then
1854 let tmp = !confpath ^ ".tmp" in
1855 let oc = open_out_bin tmp in
1856 Buffer.output_buffer oc bb;
1857 close_out oc;
1858 Unix.rename tmp !confpath;
1859 with exn ->
1860 dolog "error saving configuration: %s" @@ exntos exn
1863 let gc fdi fdo =
1864 let wr s n =
1865 let n' = Unix.write fdo (Bytes.of_string s) 0 n in
1866 if n != n'
1867 then Utils.error "Unix.write %d = %d" n n'
1869 let rd s n =
1870 try Unix.read fdi s 0 n
1871 with exn -> Utils.error "reading gc pipe %s" @@ exntos exn in
1872 let href = ref (Hashtbl.create 0) in
1873 let cref = ref defconf in
1874 let push (h, dc) =
1875 let f path (pc, _pb, _px, _pa, _po) =
1876 let s =
1877 Printf.sprintf "%s\000%ld\000" path (Int32.of_float pc.lastvisit)
1879 wr s (String.length s)
1881 Hashtbl.iter f h;
1882 href := h;
1883 cref := dc;
1884 Unix.shutdown fdo Unix.SHUTDOWN_SEND;
1885 true
1887 ignore (load1 push);
1888 let s =
1889 let b = Buffer.create 32768 in
1890 let s = Bytes.create 4096 in
1891 let rec f () =
1892 let n = rd s (Bytes.length s) in
1893 if n = 0
1894 then Buffer.contents b
1895 else (Buffer.add_subbytes b s 0 n; f ())
1897 f ()
1899 let rec f ppos =
1900 match String.index_from s ppos '\000' with
1901 | zpos1 ->
1902 let zpos2 =
1903 try String.index_from s (zpos1+1) '\000' with Not_found -> -1 in
1904 if zpos2 = -1
1905 then error "invalid gc input in (%S) at %d" s zpos1
1906 else
1907 let okey = StringLabels.sub s ~pos:ppos ~len:(zpos1-ppos) in
1908 let nkey = StringLabels.sub s ~pos:(zpos1+1) ~len:(zpos2-zpos1-1) in
1909 if emptystr nkey
1910 then (Hashtbl.remove !href okey; f (zpos2+1))
1911 else
1912 let v =
1913 try Hashtbl.find !href okey
1914 with Not_found -> Utils.error "gc: cannot find %S" okey
1916 Hashtbl.remove !href okey;
1917 Hashtbl.replace !href nkey v;
1918 f (zpos2+1)
1919 | exception Not_found -> ()
1921 f 0;
1922 let bb = Buffer.create 32768 in
1923 let save2 (_h, dc) = save1 bb (fun _ _ -> ()) 0 !href dc in
1924 if load1 save2 && Buffer.length bb > 0
1925 then (
1927 let tmp = !confpath ^ ".tmp" in
1928 let oc = open_out_bin tmp in
1929 Buffer.output_buffer oc bb;
1930 close_out oc;
1931 Unix.rename tmp !confpath;
1932 with exn ->
1933 dolog "error saving configuration: %s" @@ exntos exn
1937 let logcurrently = function
1938 | Idle -> dolog "Idle"
1939 | Loading (l, gen) ->
1940 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1941 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1942 dolog
1943 "Tiling %d[%d,%d] page=%s cs=%s angle"
1944 l.pageno col row (~> pageopaque)
1945 (CSTE.to_string colorspace)
1947 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1948 angle gen conf.angle state.gen
1949 tilew tileh
1950 conf.tilew conf.tileh
1952 | Outlining _ ->
1953 dolog "outlining"