Cosmetics
[llpp.git] / config.ml
blobd4fe3b15826ac376d886bf80e24828a74370f792
1 open Utils;;
3 type fontstate =
4 { mutable fontsize : int
5 ; mutable wwidth : float
6 ; mutable maxrows : int
8 ;;
10 let fstate =
11 { fontsize = 20 * Wsi.fontsizefactor ()
12 ; wwidth = nan
13 ; maxrows = -1
17 let irect_of_string s =
18 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
21 let irect_to_string (x0,y0,x1,y1) = Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1;;
23 let multicolumns_to_string (n, a, b) =
24 if a = 0 && b = 0
25 then Printf.sprintf "%d" n
26 else Printf.sprintf "%d,%d,%d" n a b;
29 let multicolumns_of_string s =
30 try
31 (int_of_string s, 0, 0)
32 with _ ->
33 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
34 if a > 1 || b > 1
35 then error "subtly broken";
36 (n, a, b)
40 include Confstruct;;
42 type angle = int
43 and opaque = Opaque.t
44 and rectcolor = rgba
45 and pixmapsize = int
46 and gen = int
47 and top = float
48 and dtop = float
49 and fontpath = string
50 and aalevel = int
51 and trimmargins = bool
52 and trimparams = (trimmargins * irect)
53 and usedoccss = bool
54 and uri = string
55 and caption = string
56 and tilex = int
57 and tiley = int
58 and tileparams = (x * y * width * height * tilex * tiley)
59 and under =
60 | Unone
61 | Ulinkuri of string
62 | Utext of facename
63 | Uannotation of (opaque * slinkindex)
64 and slinkindex = int
65 and facename = string
66 and launchcommand = string
67 and filename = string
68 and linkno = int
69 and destname = string
70 and link =
71 | Lnotfound
72 | Lfound of int
73 and linkdir =
74 | LDfirst
75 | LDlast
76 | LDfirstvisible of (int * int * int)
77 | LDleft of int
78 | LDright of int
79 | LDdown of int
80 | LDup of int
81 and pagewithlinks =
82 | Pwlnotfound
83 | Pwl of int
84 and scrollb = int
85 and anchor = pageno * top * dtop
86 and rect = float * float * float * float * float * float * float * float
87 and infochange = | Memused | Docinfo | Pdim
90 class type uioh =
91 object
92 method display : unit
93 method key : int -> int -> uioh
94 method button : int -> bool -> int -> int -> int -> uioh
95 method multiclick : int -> int -> int -> int -> uioh
96 method motion : int -> int -> uioh
97 method pmotion : int -> int -> uioh
98 method infochanged : infochange -> unit
99 method scrollpw : (int * float * float)
100 method scrollph : (int * float * float)
101 method modehash : keyhash
102 method eformsgs : bool
103 method alwaysscrolly : bool
104 method scroll : int -> int -> uioh
105 method zoom : float -> int -> int -> unit
106 end;;
108 module type TextEnumType =
110 type t
111 val name : string
112 val names : string array
113 end;;
115 module TextEnumMake (Ten : TextEnumType) =
116 struct
117 let names = Ten.names;;
118 let to_int (t : Ten.t) = Obj.magic t;;
119 let to_string t = names.(to_int t);;
120 let of_int n : Ten.t = Obj.magic n;;
121 let of_string s =
122 let rec find i =
123 if i = Array.length names
124 then error "invalid %s: %s" Ten.name s
125 else (
126 if Ten.names.(i) = s
127 then of_int i
128 else find (i+1)
130 in find 0;;
131 end;;
133 module CSTE = TextEnumMake (
134 struct
135 type t = colorspace;;
136 let name = "colorspace";;
137 let names = [|"rgb"; "gray"|];;
138 end);;
140 module MTE = TextEnumMake (
141 struct
142 type t = mark;;
143 let name = "mark";;
144 let names = [|"page"; "block"; "line"; "word"|];;
145 end);;
147 module FMTE = TextEnumMake (
148 struct
149 type t = fitmodel;;
150 let name = "fitmodel";;
151 let names = [|"width"; "proportional"; "page"|];;
152 end);;
154 type outlinekind =
155 | Onone
156 | Oanchor of anchor
157 | Ouri of uri
158 | Olaunch of launchcommand
159 | Oremote of (filename * pageno)
160 | Oremotedest of (filename * destname)
161 | Ohistory of (filename * conf * outline list * x * anchor * filename)
162 and outline = (caption * outlinelevel * outlinekind)
163 and outlinelevel = int
166 type page =
167 { pageno : int
168 ; pagedimno : int
169 ; pagew : int
170 ; pageh : int
171 ; pagex : int
172 ; pagey : int
173 ; pagevw : int
174 ; pagevh : int
175 ; pagedispx : int
176 ; pagedispy : int
177 ; pagecol : int
181 type tile = opaque * pixmapsize * elapsed
182 and elapsed = float;;
183 type pagemapkey = pageno * gen;;
184 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
185 and row = int
186 and col = int
187 and currently =
188 | Idle
189 | Loading of (page * gen)
190 | Tiling
191 of (page * opaque * colorspace * angle * gen * col * row * width * height)
192 | Outlining of outline list
195 type mpos = int * int
196 and mstate =
197 | Msel of (mpos * mpos)
198 | Mpan of mpos
199 | Mscrolly | Mscrollx
200 | Mzoom of (buttonno * step * mpos)
201 | Mzoomrect of (mpos * mpos)
202 | Mnone
203 and buttonno = int
204 and step = int
207 type mode =
208 | Birdseye of (conf * leftx * pageno * pageno * anchor)
209 | Textentry of (textentry * onleave)
210 | View
211 | LinkNav of linktarget
212 and onleave = leavetextentrystatus -> unit
213 and leavetextentrystatus = | Cancel | Confirm
214 and helpitem = string * int * action
215 and action =
216 | Noaction
217 | Action of (uioh -> uioh)
218 and linktarget =
219 | Ltexact of (pageno * direction)
220 | Ltgendir of direction
221 | Ltnotready of (pageno * direction)
222 and direction = int (* -1, 0, 1 *)
223 and textentry = string * string * onhist option * onkey * ondone * cancelonempty
224 and onkey = string -> Keys.t -> te
225 and ondone = string -> unit
226 and histcancel = unit -> unit
227 and onhist = ((histcmd -> string) * histcancel)
228 and histcmd = HCnext | HCprev | HCfirst | HClast
229 and cancelonempty = bool
230 and te =
231 | TEstop
232 | TEdone of string
233 | TEcont of string
234 | TEswitch of textentry
237 type 'a circbuf =
238 { store : 'a array
239 ; mutable rc : int
240 ; mutable wc : int
241 ; mutable len : int
245 type 'a nav =
246 { past : 'a list
247 ; future : 'a list
251 type state =
252 { mutable ss : Unix.file_descr
253 ; mutable wsfd : Unix.file_descr
254 ; mutable stderr : Unix.file_descr
255 ; mutable errmsgs : Buffer.t
256 ; mutable newerrmsgs : bool
257 ; mutable w : int
258 ; mutable x : x
259 ; mutable y : y
260 ; mutable anchor : anchor
261 ; mutable ranchors : (string * string * anchor * string) list
262 ; mutable maxy : int
263 ; mutable layout : page list
264 ; pagemap : (pagemapkey, opaque) Hashtbl.t
265 ; tilemap : (tilemapkey, tile) Hashtbl.t
266 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
267 ; mutable pdims : (pageno * width * height * leftx) list
268 ; mutable pagecount : int
269 ; mutable currently : currently
270 ; mutable mstate : mstate
271 ; mutable searchpattern : string
272 ; mutable rects : (pageno * rectcolor * rect) list
273 ; mutable rects1 : (pageno * rectcolor * rect) list
274 ; prects : (pageno, float array) Hashtbl.t
275 ; mutable text : string
276 ; mutable winstate : Wsi.winstate list
277 ; mutable mode : mode
278 ; mutable uioh : uioh
279 ; mutable outlines : outline array
280 ; mutable bookmarks : outline list
281 ; mutable path : string
282 ; mutable password : string
283 ; mutable nameddest : string
284 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
285 ; mutable memused : memsize
286 ; mutable gen : gen
287 ; mutable autoscroll : int option
288 ; mutable help : helpitem array
289 ; mutable docinfo : (int * string) list
290 ; hists : hists
291 ; mutable prevzoom : (float * int)
292 ; mutable progress : float
293 ; mutable mpos : mpos
294 ; mutable keystate : keystate
295 ; mutable glinks : bool
296 ; mutable prevcolumns : (columns * float) option
297 ; mutable winw : int
298 ; mutable winh : int
299 ; mutable reprf : (unit -> unit)
300 ; mutable origin : string
301 ; mutable roam : (unit -> unit)
302 ; mutable bzoom : bool
303 ; mutable lnava : (pageno * linkno) option
304 ; mutable slideshow : int
305 ; mutable reload : (x * y * float) option
306 ; mutable nav : anchor nav
308 and hists =
309 { pat : string circbuf
310 ; pag : string circbuf
311 ; sel : string circbuf
315 let emptyanchor = (0, 0.0, 0.0);;
316 let emptykeyhash = Hashtbl.create 0;;
317 let noreprf () = ();;
318 let noroam () = ();;
320 let nouioh : uioh =
321 object (self)
322 method display = ()
323 method key _ _ = self
324 method multiclick _ _ _ _ = self
325 method button _ _ _ _ _ = self
326 method motion _ _ = self
327 method pmotion _ _ = self
328 method infochanged _ = ()
329 method scrollpw = (0, nan, nan)
330 method scrollph = (0, nan, nan)
331 method modehash = emptykeyhash
332 method eformsgs = false
333 method alwaysscrolly = false
334 method scroll _ _ = self
335 method zoom _ _ _ = ()
336 end;;
338 let platform_to_string = function
339 | Punknown -> "unknown"
340 | Plinux -> "Linux"
341 | Pmacos -> "macOS"
342 | Pbsd -> "BSD"
345 let conf = { defconf with keyhashes = copykeyhashes defconf };;
347 let cbnew n v =
348 { store = Array.make n v
349 ; rc = 0
350 ; wc = 0
351 ; len = 0
355 let cbcap b = Array.length b.store;;
357 let cbput ?(update_rc=true) b v =
358 let cap = cbcap b in
359 b.store.(b.wc) <- v;
360 b.wc <- (b.wc + 1) mod cap;
361 if update_rc
362 then b.rc <- b.wc;
363 b.len <- min (b.len + 1) cap;
366 let cbput_dont_update_rc b v = cbput ~update_rc:false b v;;
368 let cbempty b = b.len = 0;;
370 let cbgetg b circular dir =
371 if cbempty b
372 then b.store.(0)
373 else
374 let rc = b.rc + dir in
375 let rc =
376 if circular
377 then (
378 if rc = -1
379 then b.len-1
380 else (
381 if rc >= b.len
382 then 0
383 else rc
386 else bound rc 0 (b.len-1)
388 b.rc <- rc;
389 b.store.(rc);
392 let cbget b = cbgetg b false;;
393 let cbgetc b = cbgetg b true;;
395 let state =
396 { ss = Unix.stdin
397 ; wsfd = Unix.stdin
398 ; stderr = Unix.stderr
399 ; errmsgs = Buffer.create 0
400 ; newerrmsgs = false
401 ; x = 0
402 ; y = 0
403 ; w = 0
404 ; anchor = emptyanchor
405 ; ranchors = []
406 ; layout = []
407 ; maxy = max_int
408 ; tilelru = Queue.create ()
409 ; pagemap = Hashtbl.create 10
410 ; tilemap = Hashtbl.create 10
411 ; pdims = []
412 ; pagecount = 0
413 ; currently = Idle
414 ; mstate = Mnone
415 ; rects = []
416 ; rects1 = []
417 ; prects = Hashtbl.create 1
418 ; text = E.s
419 ; mode = View
420 ; winstate = []
421 ; searchpattern = E.s
422 ; outlines = E.a
423 ; bookmarks = []
424 ; path = E.s
425 ; password = E.s
426 ; nameddest = E.s
427 ; geomcmds = E.s, []
428 ; hists =
429 { pat = cbnew 10 E.s; pag = cbnew 10 E.s; sel = cbnew 10 E.s; }
430 ; memused = 0
431 ; gen = 0
432 ; autoscroll = None
433 ; help = E.a
434 ; docinfo = []
435 ; prevzoom = (1.0, 0)
436 ; progress = -1.0
437 ; uioh = nouioh
438 ; mpos = (-1, -1)
439 ; keystate = KSnone
440 ; glinks = false
441 ; prevcolumns = None
442 ; winw = -1
443 ; winh = -1
444 ; reprf = noreprf
445 ; origin = E.s
446 ; roam = noroam
447 ; bzoom = false
448 ; lnava = None
449 ; slideshow = 0
450 ; reload = None
451 ; nav = { past = []; future = []; }
455 let calcips h =
456 let d = state.winh - h in
457 max conf.interpagespace ((d + 1) / 2)
460 let rowyh (c, coverA, coverB) b n =
461 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
462 then
463 let _, _, vy, (_, _, h, _) = b.(n) in
464 (vy, h)
465 else
466 let n' = n - coverA in
467 let d = n' mod c in
468 let s = n - d in
469 let e = min state.pagecount (s + c) in
470 let rec find m miny maxh = if m = e then miny, maxh else
471 let _, _, y, (_, _, h, _) = b.(m) in
472 let miny = min miny y in
473 let maxh = max maxh h in
474 find (m+1) miny maxh
475 in find s max_int 0
478 let page_of_y y =
479 let ((c, coverA, coverB) as cl), b =
480 match conf.columns with
481 | Csplit (_, b) | Csingle b -> (1, 0, 0), b
482 | Cmulti (c, b) -> c, b
484 if Array.length b = 0
485 then -1
486 else
487 let rec bsearch nmin nmax =
488 if nmin > nmax
489 then bound nmin 0 (state.pagecount-1)
490 else
491 let n = (nmax + nmin) / 2 in
492 let vy, h = rowyh cl b n in
493 let y0, y1 =
494 if conf.presentation
495 then
496 let ips = calcips h in
497 let y0 = vy - ips in
498 let y1 = vy + h + ips in
499 y0, y1
500 else (
501 if n = 0
502 then 0, vy + h + conf.interpagespace
503 else
504 let y0 = vy - conf.interpagespace in
505 y0, y0 + h + conf.interpagespace
508 if y >= y0 && y < y1
509 then (
510 if c = 1
511 then n
512 else (
513 if n > coverA
514 then
515 if n < state.pagecount - coverB
516 then ((n-coverA)/c)*c + coverA
517 else n
518 else n
521 else (
522 if y > y0
523 then bsearch (n+1) nmax
524 else bsearch nmin (n-1)
527 bsearch 0 (state.pagecount-1);
530 let calcheight () =
531 match conf.columns with
532 | Cmulti ((_, _, _) as cl, b) ->
533 if Array.length b > 0
534 then
535 let y, h = rowyh cl b (Array.length b - 1) in
536 y + h + (if conf.presentation then calcips h else 0)
537 else 0
538 | Csingle b ->
539 if Array.length b > 0
540 then
541 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
542 y + h + (if conf.presentation then calcips h else 0)
543 else 0
544 | Csplit (_, b) ->
545 if Array.length b > 0
546 then
547 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
548 y + h
549 else 0
552 let getpageywh pageno =
553 let pageno = bound pageno 0 (state.pagecount-1) in
554 match conf.columns with
555 | Csingle b ->
556 if Array.length b = 0
557 then 0, 0, 0
558 else
559 let (_, _, y, (_, w, h, _)) = b.(pageno) in
560 let y =
561 if conf.presentation
562 then y - calcips h
563 else y
565 y, w, h
566 | Cmulti (cl, b) ->
567 if Array.length b = 0
568 then 0, 0, 0
569 else
570 let y, h = rowyh cl b pageno in
571 let (_, _, _, (_, w, _, _)) = b.(pageno) in
572 let y =
573 if conf.presentation
574 then y - calcips h
575 else y
577 y, w, h
578 | Csplit (c, b) ->
579 if Array.length b = 0
580 then 0, 0, 0
581 else
582 let n = pageno*c in
583 let (_, _, y, (_, w, h, _)) = b.(n) in
584 y, w / c, h
587 let getpageyh pageno =
588 let y,_,h = getpageywh pageno in
589 y, h;
592 let getpagedim pageno =
593 let rec f ppdim l =
594 match l with
595 | (n, _, _, _) as pdim :: rest ->
596 if n >= pageno
597 then (if n = pageno then pdim else ppdim)
598 else f pdim rest
599 | [] -> ppdim
601 f (-1, -1, -1, -1) state.pdims
604 let getpdimno pageno =
605 let rec f p l =
606 let np = succ p in
607 match l with
608 | (n, _, _, _) :: rest ->
609 if n >= pageno
610 then (if n = pageno then np else p)
611 else f np rest
612 | [] -> p
614 f ~-1 state.pdims
617 let getpagey pageno = fst (getpageyh pageno);;
619 let getanchor1 l =
620 let top =
621 let coloff = l.pagecol * l.pageh in
622 float (l.pagey + coloff) /. float l.pageh
624 let dtop =
625 if l.pagedispy = 0
626 then 0.0
627 else (
628 if conf.presentation
629 then float l.pagedispy /. float (calcips l.pageh)
630 else float l.pagedispy /. float conf.interpagespace
633 (l.pageno, top, dtop)
636 let getanchor () =
637 match state.layout with
638 | l :: _ -> getanchor1 l
639 | [] ->
640 let n = page_of_y state.y in
641 if n = -1
642 then state.anchor
643 else
644 let y, h = getpageyh n in
645 let dy = y - state.y in
646 let dtop =
647 if conf.presentation
648 then
649 let ips = calcips h in
650 float (dy + ips) /. float ips
651 else float dy /. float conf.interpagespace
653 (n, 0.0, dtop)
656 let fontpath = ref E.s;;
657 let tcfpath = ref E.s;;
658 let bedefault = ref false;;
660 type historder = [ `lastvisit | `title | `path | `file ];;
662 module KeyMap =
663 Map.Make (struct type t = (int * int) let compare = compare end);;
665 let unentS s =
666 let l = String.length s in
667 let b = Buffer.create l in
668 Parser.unent b s 0 l;
669 Buffer.contents b;
672 let home =
673 try Sys.getenv "HOME"
674 with exn ->
675 dolog "cannot determine home directory location: %s" @@ exntos exn;
679 let modifier_of_string = function
680 | "alt" -> Wsi.altmask
681 | "shift" -> Wsi.shiftmask
682 | "ctrl" | "control" -> Wsi.ctrlmask
683 | "meta" -> Wsi.metamask
684 | _ -> 0
687 let keys_of_string s =
688 let key_of_string r s =
689 let elems = Str.full_split r s in
690 let f n k m =
691 let g s =
692 let m1 = modifier_of_string s in
693 if m1 = 0
694 then (Wsi.namekey s, m)
695 else (k, m lor m1)
696 in function
697 | Str.Delim s when n land 1 = 0 -> g s
698 | Str.Text s -> g s
699 | Str.Delim _ -> (k, m)
701 let rec loop n k m = function
702 | [] -> (k, m)
703 | x :: xs ->
704 let k, m = f n k m x in
705 loop (n+1) k m xs
707 loop 0 0 0 elems
709 let elems = Str.split Utils.Re.whitespace s in
710 List.map (key_of_string (Str.regexp "-")) elems
713 let config_of c attrs =
714 let maxv ?(f=int_of_string) u s = max u @@ f s in
715 let apply c k v =
717 match k with
718 | "scroll-bar-width" -> { c with scrollbw = maxv 0 v }
719 | "scroll-handle-height" -> { c with scrollh = maxv 0 v }
720 | "case-insensitive-search" -> { c with icase = bool_of_string v }
721 | "preload" -> { c with preload = bool_of_string v }
722 | "page-bias" -> { c with pagebias = int_of_string v }
723 | "scroll-step" -> { c with scrollstep = maxv 1 v }
724 | "horizontal-scroll-step" -> { c with hscrollstep = maxv 1 v }
725 | "auto-scroll-step" -> { c with autoscrollstep = maxv 0 v }
726 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
727 | "highlight-links" -> { c with hlinks = bool_of_string v }
728 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
729 | "vertical-margin" -> { c with interpagespace = maxv 0 v }
730 | "zoom" ->
731 let zoom = float_of_string v /. 100. in
732 let zoom = max zoom 0.0 in
733 { c with zoom = zoom }
734 | "presentation" -> { c with presentation = bool_of_string v }
735 | "rotation-angle" -> { c with angle = int_of_string v }
736 | "width" -> { c with cwinw = maxv 20 v }
737 | "height" -> { c with cwinh = maxv 20 v }
738 | "proportional-display" ->
739 let fm =
740 if bool_of_string v
741 then FitProportional
742 else FitWidth
744 { c with fitmodel = fm }
745 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
746 | "pixmap-cache-size" ->
747 { c with memlimit = maxv ~f:int_of_string_with_suffix 2 v }
748 | "tex-count" -> { c with texcount = maxv 1 v }
749 | "slice-height" -> { c with sliceheight = maxv 2 v }
750 | "thumbnail-width" -> { c with thumbw = maxv 2 v }
751 | "background-color" -> { c with bgcolor = color_of_string v }
752 | "paper-color" -> { c with papercolor = rgba_of_string v }
753 | "scrollbar-color" -> { c with sbarcolor = rgba_of_string v }
754 | "scrollbar-handle-color" -> { c with sbarhndlcolor = rgba_of_string v }
755 | "texture-color" -> { c with texturecolor = rgba_of_string v }
756 | "tile-width" -> { c with tilew = maxv 2 v }
757 | "tile-height" -> { c with tileh = maxv 2 v }
758 | "mupdf-store-size" ->
759 { c with mustoresize = maxv ~f:int_of_string_with_suffix 1024 v }
760 | "checkers" -> { c with checkers = bool_of_string v }
761 | "aalevel" -> { c with aalevel = maxv 0 v }
762 | "trim-margins" -> { c with trimmargins = bool_of_string v }
763 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
764 | "trim-cache-path" -> { c with trimcachepath = v }
765 | "uri-launcher" -> { c with urilauncher = unentS v }
766 | "path-launcher" -> { c with pathlauncher = unentS v }
767 | "color-space" -> { c with colorspace = CSTE.of_string v }
768 | "invert-colors" -> { c with invert = bool_of_string v }
769 | "brightness" -> { c with colorscale = float_of_string v }
770 | "columns" ->
771 let (n, _, _) as nab = multicolumns_of_string v in
772 if n < 0
773 then { c with columns = Csplit (-n, E.a) }
774 else { c with columns = Cmulti (nab, E.a) }
775 | "birds-eye-columns" -> { c with beyecolumns = Some (maxv 2 v) }
776 | "selection-command" -> { c with selcmd = unentS v }
777 | "paste-command" -> { c with pastecmd = unentS v }
778 | "synctex-command" -> { c with stcmd = unentS v }
779 | "pax-command" -> { c with paxcmd = unentS v }
780 | "askpass-command" -> { c with passcmd = unentS v }
781 | "savepath-command" -> { c with savecmd = unentS v }
782 | "update-cursor" -> { c with updatecurs = bool_of_string v }
783 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
784 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
785 | "use-pbo" -> { c with usepbo = bool_of_string v }
786 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
787 | "horizontal-scrollbar-visible" ->
788 { c with scrollb = if bool_of_string v
789 then c.scrollb lor scrollbhv
790 else c.scrollb land (lnot scrollbhv)
792 | "vertical-scrollbar-visible" ->
793 { c with scrollb = if bool_of_string v
794 then c.scrollb lor scrollbvv
795 else c.scrollb land (lnot scrollbvv)
797 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
798 | "point-and-x" ->
799 { c with pax = if bool_of_string v then Some 0.0 else None }
800 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
801 | "scroll-bar-on-the-left" -> { c with leftscroll = bool_of_string v }
802 | "title" -> { c with title = unentS v }
803 | "last-visit" -> { c with lastvisit = float_of_string v }
804 | "edit-annotations-inline" -> { c with annotinline = bool_of_string v }
805 | "coarse-presentation-positioning" ->
806 { c with coarseprespos = bool_of_string v }
807 | "use-document-css" -> { c with usedoccss = bool_of_string v }
808 | _ -> c
809 with exn ->
810 dolog "error processing attribute (`%S' = `%S'): %s" k v @@ exntos exn;
813 let rec fold c = function
814 | [] -> c
815 | (k, v) :: rest ->
816 let c = apply c k v in
817 fold c rest
819 fold { c with keyhashes = copykeyhashes c } attrs;
822 let fromstring f pos n v d =
823 try f v
824 with exn ->
825 dolog "error processing attribute (%S=%S) at %d\n%s" n v pos @@ exntos exn;
829 let bookmark_of attrs =
830 let rec fold title page rely visy = function
831 | ("title", v) :: rest -> fold v page rely visy rest
832 | ("page", v) :: rest -> fold title v rely visy rest
833 | ("rely", v) :: rest -> fold title page v visy rest
834 | ("visy", v) :: rest -> fold title page rely v rest
835 | _ :: rest -> fold title page rely visy rest
836 | [] -> title, page, rely, visy
838 fold "invalid" "0" "0" "0" attrs
841 let doc_of attrs =
842 let rec fold path key page rely pan visy origin tcf = function
843 | ("path", v) :: rest -> fold v key page rely pan visy origin tcf rest
844 | ("key", v) :: rest -> fold path v page rely pan visy origin tcf rest
845 | ("page", v) :: rest -> fold path key v rely pan visy origin tcf rest
846 | ("rely", v) :: rest -> fold path key page v pan visy origin tcf rest
847 | ("pan", v) :: rest -> fold path key page rely v visy origin tcf rest
848 | ("visy", v) :: rest -> fold path key page rely pan v origin tcf rest
849 | ("origin", v) :: rest -> fold path key page rely pan visy v tcf rest
850 | ("trim-cache-path", v) :: rest ->
851 fold path key page rely pan visy origin v rest
852 | _ :: rest -> fold path key page rely pan visy origin tcf rest
853 | [] -> path, key, page, rely, pan, visy, origin, tcf
855 fold E.s E.s "0" "0" "0" "0" E.s E.s attrs
858 let map_of attrs =
859 let rec fold rs ls = function
860 | ("out", v) :: rest -> fold v ls rest
861 | ("in", v) :: rest -> fold rs v rest
862 | _ :: rest -> fold ls rs rest
863 | [] -> ls, rs
865 fold E.s E.s attrs
868 let findkeyhash c name =
869 try List.assoc name c.keyhashes
870 with Not_found -> error "invalid mode name `%s'" name
873 let get s =
874 let open Parser in
875 let h = Hashtbl.create 10 in
876 let dc = { defconf with angle = defconf.angle } in
877 let rec toplevel v t spos _ =
878 match t with
879 | Vdata | Vcdata | Vend -> v
880 | Vopen ("llppconfig", _, closed) ->
881 if closed
882 then v
883 else { v with f = llppconfig }
884 | Vopen _ -> parse_error "unexpected subelement at top level" s spos
885 | Vclose _ -> parse_error "unexpected close at top level" s spos
887 and llppconfig v t spos _ =
888 match t with
889 | Vdata | Vcdata -> v
890 | Vend -> parse_error "unexpected end of input in llppconfig" s spos
891 | Vopen ("defaults", attrs, closed) ->
892 let c = config_of dc attrs in
893 setconf dc c;
894 if closed
895 then v
896 else { v with f = defaults }
898 | Vopen ("ui-font", attrs, closed) ->
899 let rec getsize size = function
900 | [] -> size
901 | ("size", v) :: rest ->
902 let size =
903 fromstring int_of_string spos "size" v fstate.fontsize in
904 getsize size rest
905 | l -> getsize size l
907 fstate.fontsize <- getsize fstate.fontsize attrs;
908 if closed
909 then v
910 else { v with f = uifont (Buffer.create 10) }
912 | Vopen ("doc", attrs, closed) ->
913 let pathent, key, spage, srely, span, svisy, origin, tcfE
914 = doc_of attrs in
915 let path = unentS pathent
916 and origin = unentS origin
917 and pageno = fromstring int_of_string spos "page" spage 0
918 and rely = fromstring float_of_string spos "rely" srely 0.0
919 and pan = fromstring int_of_string spos "pan" span 0
920 and tcfS = unentS tcfE
921 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
922 let c = config_of dc attrs in
923 c.key <- key;
924 c.trimcachepath <- tcfS;
925 let anchor = (pageno, rely, visy) in
926 if closed
927 then (Hashtbl.add h path (c, [], pan, anchor, origin); v)
928 else { v with f = doc path origin pan anchor c [] }
930 | Vopen _ -> parse_error "unexpected subelement in llppconfig" s spos
931 | Vclose "llppconfig" -> { v with f = toplevel }
932 | Vclose _ -> parse_error "unexpected close in llppconfig" s spos
934 and defaults v t spos _ =
935 match t with
936 | Vdata | Vcdata -> v
937 | Vend -> parse_error "unexpected end of input in defaults" s spos
938 | Vopen ("keymap", attrs, closed) ->
939 let modename =
940 try List.assoc "mode" attrs
941 with Not_found -> "global" in
942 if closed
943 then v
944 else
945 let ret keymap =
946 let h = findkeyhash dc modename in
947 KeyMap.iter (Hashtbl.replace h) keymap;
948 defaults
950 { v with f = pkeymap ret KeyMap.empty }
952 | Vopen (_, _, _) ->
953 parse_error "unexpected subelement in defaults" s spos
955 | Vclose "defaults" ->
956 { v with f = llppconfig }
958 | Vclose _ -> parse_error "unexpected close in defaults" s spos
960 and uifont b v t spos epos =
961 match t with
962 | Vdata | Vcdata ->
963 Buffer.add_substring b s spos (epos - spos);
965 | Vopen (_, _, _) -> parse_error "unexpected subelement in ui-font" s spos
966 | Vclose "ui-font" ->
967 if emptystr !fontpath
968 then fontpath := Buffer.contents b;
969 { v with f = llppconfig }
970 | Vclose _ -> parse_error "unexpected close in ui-font" s spos
971 | Vend -> parse_error "unexpected end of input in ui-font" s spos
973 and doc path origin pan anchor c bookmarks v t spos _ =
974 match t with
975 | Vdata | Vcdata -> v
976 | Vend -> parse_error "unexpected end of input in doc" s spos
977 | Vopen ("bookmarks", _, closed) ->
978 if closed
979 then v
980 else { v with f = pbookmarks path origin pan anchor c bookmarks }
982 | Vopen ("keymap", attrs, closed) ->
983 let modename =
984 try List.assoc "mode" attrs
985 with Not_found -> "global"
987 if closed
988 then v
989 else
990 let ret keymap =
991 let h = findkeyhash c modename in
992 KeyMap.iter (Hashtbl.replace h) keymap;
993 doc path origin pan anchor c bookmarks
995 { v with f = pkeymap ret KeyMap.empty }
997 | Vopen ("css", [], false) ->
998 { v with f = pcss path origin pan anchor c bookmarks }
1000 | Vopen (_, _, _) ->
1001 parse_error "unexpected subelement in doc" s spos
1003 | Vclose "doc" ->
1004 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor, origin);
1005 { v with f = llppconfig }
1007 | Vclose _ -> parse_error "unexpected close in doc" s spos
1009 and pcss path origin pan anchor c bookmarks v t spos epos =
1010 match t with
1011 | Vdata | Vcdata ->
1012 let b = Buffer.create 10 in
1013 Buffer.add_substring b s spos (epos - spos);
1014 { v with f = pcss path origin pan anchor
1015 { c with css = Buffer.contents b }
1016 bookmarks }
1017 | Vend -> parse_error "unexpected end of input in css" s spos
1018 | Vopen _ -> parse_error "unexpected subelement in css" s spos
1019 | Vclose "css" -> { v with f = doc path origin pan anchor c bookmarks }
1020 | Vclose _ -> parse_error "unexpected close in css" s spos
1022 and pkeymap ret keymap v t spos _ =
1023 match t with
1024 | Vdata | Vcdata -> v
1025 | Vend -> parse_error "unexpected end of input in keymap" s spos
1026 | Vopen ("map", attrs, closed) ->
1027 let r, l = map_of attrs in
1028 let kss = fromstring keys_of_string spos "in" r [] in
1029 let lss = fromstring keys_of_string spos "out" l [] in
1030 let keymap =
1031 match kss with
1032 | [] -> keymap
1033 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
1034 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
1036 if closed
1037 then { v with f = pkeymap ret keymap }
1038 else
1039 let f () = v in
1040 { v with f = skip "map" f }
1042 | Vopen _ -> parse_error "unexpected subelement in keymap" s spos
1043 | Vclose "keymap" ->
1044 { v with f = ret keymap }
1045 | Vclose _ -> parse_error "unexpected close in keymap" s spos
1047 and pbookmarks path origin pan anchor c bookmarks v t spos _ =
1048 match t with
1049 | Vdata | Vcdata -> v
1050 | Vend -> parse_error "unexpected end of input in bookmarks" s spos
1051 | Vopen ("item", attrs, closed) ->
1052 let titleent, spage, srely, svisy = bookmark_of attrs in
1053 let page = fromstring int_of_string spos "page" spage 0
1054 and rely = fromstring float_of_string spos "rely" srely 0.0
1055 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1056 let bookmarks =
1057 (unentS titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
1059 if closed
1060 then { v with f = pbookmarks path origin pan anchor c bookmarks }
1061 else
1062 let f () = v in
1063 { v with f = skip "item" f }
1065 | Vopen _ -> parse_error "unexpected subelement in bookmarks" s spos
1066 | Vclose "bookmarks" ->
1067 { v with f = doc path origin pan anchor c bookmarks }
1068 | Vclose _ -> parse_error "unexpected close in bookmarks" s spos
1070 and skip tag f v t spos _ =
1071 match t with
1072 | Vdata | Vcdata -> v
1073 | Vend ->
1074 parse_error ("unexpected end of input in skipped " ^ tag) s spos
1075 | Vopen (tag', _, closed) ->
1076 if closed
1077 then v
1078 else
1079 let f' () = { v with f = skip tag f } in
1080 { v with f = skip tag' f' }
1081 | Vclose ctag ->
1082 if tag = ctag
1083 then f ()
1084 else parse_error ("unexpected close in skipped " ^ tag) s spos
1086 parse { f = toplevel; accu = () } s;
1087 h, dc;
1090 let do_load f contents =
1091 try f contents
1092 with
1093 | Parser.Parse_error (msg, s, pos) ->
1094 let subs = Parser.subs s pos in
1095 Utils.error "parse error: %s: at %d [..%S..]" msg pos subs
1097 | exn -> Utils.error "parse error: %s" @@ exntos exn
1100 let defconfpath =
1101 let dir =
1102 let xdgconfdir = Utils.getenvdef "XDG_CONFIG_HOME" E.s in
1103 if emptystr xdgconfdir
1104 then
1106 let dir = Filename.concat home ".config" in
1107 if Sys.is_directory dir then dir else home
1108 with _ -> home
1109 else xdgconfdir
1111 Filename.concat dir "llpp.conf"
1114 let confpath = ref defconfpath;;
1116 let load2 f default =
1117 match filecontents !confpath with
1118 | contents -> f @@ do_load get contents
1119 | exception Unix.Unix_error (Unix.ENOENT, "open", _) ->
1120 f (Hashtbl.create 0, defconf)
1121 | exception exn ->
1122 dolog "error loading configuration from `%S': %s" !confpath @@ exntos exn;
1123 default
1126 let load1 f = load2 f false;;
1128 let load openlast =
1129 let f (h, dc) =
1130 if openlast
1131 then (
1132 let path, _ =
1133 Hashtbl.fold
1134 (fun path (conf, _, _, _, _) ((_, besttime) as best) ->
1135 if conf.lastvisit > besttime
1136 then (path, conf.lastvisit)
1137 else best)
1139 (state.path, -.infinity)
1141 state.path <- path;
1143 let pc, pb, px, pa, po =
1144 let def = dc, [], 0, emptyanchor, state.origin in
1145 if emptystr state.path
1146 then def
1147 else
1148 let absname = abspath state.path in
1149 match Hashtbl.find h absname with
1150 | (c,b,x,a,_) -> (c,b,x,a,state.origin)
1151 | exception Not_found ->
1152 let exception E of (conf * outline list * int * anchor * string) in
1153 let key = try Digest.file absname |> Digest.to_hex with _ -> E.s in
1154 match (
1155 if nonemptystr key
1156 then
1157 Hashtbl.iter (fun p ((c, _, _, _, _) as v) ->
1158 if c.key = key
1159 then (
1160 dolog "will use %s's settings due to matching keys" p;
1161 raise (E v)
1165 with
1166 | _ -> def
1167 | exception E v -> v
1169 setconf defconf dc;
1170 setconf conf pc;
1171 state.bookmarks <- pb;
1172 state.x <- px;
1173 state.origin <- po;
1174 state.anchor <- pa;
1175 true
1177 load1 f
1180 let gethist () =
1181 let f (h, _) =
1182 Hashtbl.fold (fun path (pc, pb, px, pa, po) accu ->
1183 (path, pc, pb, px, pa, po) :: accu)
1184 h [];
1186 load2 f []
1189 let add_attrs bb always dc c time =
1190 let o' fmt s =
1191 Buffer.add_string bb "\n ";
1192 Printf.bprintf bb fmt s
1194 let o c fmt s = if c then o' fmt s else ignore in
1195 let ob s a b = o (always || a != b) "%s='%b'" s a
1196 and op s a b = o (always || a <> b) "%s='%b'" s (a != None)
1197 and oi s a b = o (always || a != b) "%s='%d'" s a
1198 and oI s a b = o (always || a != b) "%s='%s'" s (string_with_suffix_of_int a)
1199 and oz s a b = o (always || a <> b) "%s='%g'" s (a*.100.)
1200 and oF s a b = o (always || a <> b) "%s='%f'" s a
1201 and oL s a b = o (always || a <> b) "%s='%Ld'" s a
1202 and oc s a b = o (always || a <> b) "%s='%s'" s (color_to_string a)
1203 and oA s a b = o (always || a <> b) "%s='%s'" s (rgba_to_string a)
1204 and oC s a b = o (always || a <> b) "%s='%s'" s (CSTE.to_string a)
1205 and oR s a b = o (always || a <> b) "%s='%s'" s (irect_to_string a)
1206 and oFm s a b = o (always || a <> b) "%s='%s'" s (FMTE.to_string a)
1207 and oSv s a b m =
1208 o (always || a land m <> b land m) "%s='%b'" s (a land m != 0)
1209 and oPm s a b = o (always || a <> b) "%s='%s'" s (MTE.to_string a)
1210 and os s a b =
1211 o (always || a <> b) "%s='%s'" s @@ Parser.enent a 0 (String.length a)
1212 and oco s a b =
1213 if always || a <> b
1214 then
1215 match a with
1216 | Cmulti ((n, a, b), _) when n > 1 -> o' "%s='%d,%d,%d'" s n a b
1217 | Csplit (n, _) when n > 1 -> o' "%s='%d'" s ~-n
1218 | Cmulti _ | Csplit _ | Csingle _ -> ()
1219 and obeco s a b =
1220 if always || a <> b
1221 then
1222 match a with
1223 | Some c when c > 1 -> o' "%s='%d'" s c
1224 | _ -> ()
1226 oi "width" c.cwinw dc.cwinw;
1227 oi "height" c.cwinh dc.cwinh;
1228 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
1229 oi "scroll-handle-height" c.scrollh dc.scrollh;
1230 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
1231 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
1232 ob "case-insensitive-search" c.icase dc.icase;
1233 ob "preload" c.preload dc.preload;
1234 oi "page-bias" c.pagebias dc.pagebias;
1235 oi "scroll-step" c.scrollstep dc.scrollstep;
1236 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
1237 ob "max-height-fit" c.maxhfit dc.maxhfit;
1238 ob "highlight-links" c.hlinks dc.hlinks;
1239 ob "under-cursor-info" c.underinfo dc.underinfo;
1240 oi "vertical-margin" c.interpagespace dc.interpagespace;
1241 oz "zoom" c.zoom dc.zoom;
1242 ob "presentation" c.presentation dc.presentation;
1243 oi "rotation-angle" c.angle dc.angle;
1244 oFm "fit-model" c.fitmodel dc.fitmodel;
1245 oI "pixmap-cache-size" c.memlimit dc.memlimit;
1246 oi "tex-count" c.texcount dc.texcount;
1247 oi "slice-height" c.sliceheight dc.sliceheight;
1248 oi "thumbnail-width" c.thumbw dc.thumbw;
1249 oc "background-color" c.bgcolor dc.bgcolor;
1250 oA "paper-color" c.papercolor dc.papercolor;
1251 oA "scrollbar-color" c.sbarcolor dc.sbarcolor;
1252 oA "scrollbar-handle-color" c.sbarhndlcolor dc.sbarhndlcolor;
1253 oA "texture-color" c.texturecolor dc.texturecolor;
1254 oi "tile-width" c.tilew dc.tilew;
1255 oi "tile-height" c.tileh dc.tileh;
1256 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
1257 ob "checkers" c.checkers dc.checkers;
1258 oi "aalevel" c.aalevel dc.aalevel;
1259 ob "trim-margins" c.trimmargins dc.trimmargins;
1260 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
1261 os "trim-cache-path" c.trimcachepath dc.trimcachepath;
1262 os "uri-launcher" c.urilauncher dc.urilauncher;
1263 os "path-launcher" c.pathlauncher dc.pathlauncher;
1264 oC "color-space" c.colorspace dc.colorspace;
1265 ob "invert-colors" c.invert dc.invert;
1266 oF "brightness" c.colorscale dc.colorscale;
1267 oco "columns" c.columns dc.columns;
1268 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
1269 os "selection-command" c.selcmd dc.selcmd;
1270 os "paste-command" c.pastecmd dc.pastecmd;
1271 os "synctex-command" c.stcmd dc.stcmd;
1272 os "pax-command" c.paxcmd dc.paxcmd;
1273 os "askpass-command" c.passcmd dc.passcmd;
1274 os "savepath-command" c.savecmd dc.savecmd;
1275 ob "update-cursor" c.updatecurs dc.updatecurs;
1276 oi "hint-font-size" c.hfsize dc.hfsize;
1277 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
1278 oF "page-scroll-scale" c.pgscale dc.pgscale;
1279 ob "use-pbo" c.usepbo dc.usepbo;
1280 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
1281 ob "remote-in-a-new-instance" c.riani dc.riani;
1282 op "point-and-x" c.pax dc.pax;
1283 oPm "point-and-x-mark" c.paxmark dc.paxmark;
1284 ob "scroll-bar-on-the-left" c.leftscroll dc.leftscroll;
1285 if not always
1286 then os "title" c.title dc.title;
1287 oL "last-visit" (Int64.of_float time) 0L;
1288 ob "edit-annotations-inline" c.annotinline dc.annotinline;
1289 ob "coarse-presentation-positioning" c.coarseprespos dc.coarseprespos;
1290 ob "use-document-css" c.usedoccss dc.usedoccss;
1293 let keymapsbuf always dc c =
1294 let open Buffer in
1295 let bb = create 16 in
1296 let rec loop = function
1297 | [] -> ()
1298 | (modename, h) :: rest ->
1299 let dh = findkeyhash dc modename in
1300 if always || h <> dh
1301 then (
1302 if Hashtbl.length h > 0
1303 then (
1304 if length bb > 0 then add_char bb '\n';
1305 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
1306 Hashtbl.iter (fun i o ->
1307 if always || match Hashtbl.find dh i
1308 with | dO -> dO <> o | exception Not_found -> false
1309 then
1310 let addkm (k, m) =
1311 if Wsi.withctrl m then add_string bb "ctrl-";
1312 if Wsi.withalt m then add_string bb "alt-";
1313 if Wsi.withshift m then add_string bb "shift-";
1314 if Wsi.withmeta m then add_string bb "meta-";
1315 add_string bb (Wsi.keyname k);
1317 let addkms l =
1318 let rec loop = function
1319 | [] -> ()
1320 | km :: [] -> addkm km
1321 | km :: rest -> addkm km; add_char bb ' '; loop rest
1323 loop l
1325 add_string bb "<map in='";
1326 addkm i;
1327 match o with
1328 | KMinsrt km ->
1329 add_string bb "' out='"; addkm km; add_string bb "'/>\n"
1331 | KMinsrl kms ->
1332 add_string bb "' out='"; addkms kms; add_string bb "'/>\n"
1334 | KMmulti (ins, kms) ->
1335 add_char bb ' '; addkms ins; add_string bb "' out='";
1336 addkms kms; add_string bb "'/>\n"
1337 ) h;
1338 add_string bb "</keymap>";
1341 loop rest
1343 loop c.keyhashes;
1347 let keystostrlist c =
1348 let rec loop accu = function
1349 | [] -> accu
1350 | (modename, h) :: rest ->
1351 let accu =
1352 if Hashtbl.length h > 0
1353 then (
1354 let accu = Printf.sprintf "\xc2\xb7Keys for %s" modename :: accu in
1355 Hashtbl.fold (fun i o a ->
1356 let bb = Buffer.create 10 in
1357 let addkm (k, m) =
1358 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
1359 if Wsi.withalt m then Buffer.add_string bb "alt-";
1360 if Wsi.withshift m then Buffer.add_string bb "shift-";
1361 if Wsi.withmeta m then Buffer.add_string bb "meta-";
1362 Buffer.add_string bb (Wsi.keyname k);
1364 let addkms l =
1365 let rec loop = function
1366 | [] -> ()
1367 | km :: [] -> addkm km
1368 | km :: rest ->
1369 addkm km; Buffer.add_char bb ' ';
1370 loop rest
1372 loop l
1374 addkm i;
1375 Buffer.add_char bb '\t';
1376 begin match o with
1377 | KMinsrt km -> addkm km
1378 | KMinsrl kms -> addkms kms
1379 | KMmulti (ins, kms) ->
1380 Buffer.add_char bb ' ';
1381 addkms ins;
1382 Buffer.add_string bb "\t";
1383 addkms kms
1384 end;
1385 Buffer.contents bb :: a
1386 ) h accu
1388 else accu
1390 loop accu rest
1392 loop [] c.keyhashes
1395 let save1 bb leavebirdseye x h dc =
1396 let uifontsize = fstate.fontsize in
1397 let dc = if !bedefault then conf else dc in
1398 Buffer.add_string bb "<llppconfig>\n";
1399 if nonemptystr !fontpath
1400 then Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
1401 uifontsize !fontpath
1402 else
1403 if uifontsize <> 14
1404 then Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
1407 Buffer.add_string bb "<defaults";
1408 add_attrs bb true dc dc nan;
1409 let kb = keymapsbuf true dc dc in
1410 if Buffer.length kb > 0
1411 then (
1412 Buffer.add_string bb ">\n";
1413 Buffer.add_buffer bb kb;
1414 Buffer.add_string bb "\n</defaults>\n";
1416 else Buffer.add_string bb "/>\n";
1418 let adddoc path pan anchor c bookmarks time origin =
1419 if not (bookmarks == [] && c = dc && anchor = emptyanchor)
1420 then (
1421 Printf.bprintf bb "<doc path='%s'"
1422 (Parser.enent path 0 (String.length path));
1424 if nonemptystr c.key
1425 then Printf.bprintf bb "\n key='%s'" c.key;
1427 if nonemptystr origin
1428 then Printf.bprintf bb "\n origin='%s'"
1429 (Parser.enent origin 0 (String.length origin));
1431 if anchor <> emptyanchor
1432 then (
1433 let n, rely, visy = anchor in
1434 Printf.bprintf bb "\n page='%d'" n;
1436 if rely > 1e-6
1437 then Printf.bprintf bb " rely='%f'" rely;
1439 if abs_float visy > 1e-6
1440 then Printf.bprintf bb " visy='%f'" visy;
1443 if pan != 0
1444 then Printf.bprintf bb " pan='%d'" pan;
1446 add_attrs bb false dc c time;
1447 if nonemptystr c.css
1448 then Printf.bprintf bb ">\n <css><![CDATA[%s]]></css>" c.css;
1449 let kb = keymapsbuf false dc c in
1451 begin match bookmarks with
1452 | [] ->
1453 if Buffer.length kb > 0
1454 then (
1455 Buffer.add_string bb ">\n";
1456 Buffer.add_buffer bb kb;
1457 Buffer.add_string bb "\n</doc>\n";
1459 else (
1460 if nonemptystr c.css
1461 then Buffer.add_string bb "\n</doc>\n"
1462 else Buffer.add_string bb "/>\n"
1464 | _ ->
1465 Buffer.add_string bb ">\n<bookmarks>\n";
1466 List.iter (fun (title, _, kind) ->
1467 begin match kind with
1468 | Oanchor (page, rely, visy) ->
1469 Printf.bprintf bb
1470 "<item title='%s' page='%d'"
1471 (Parser.enent title 0 (String.length title))
1472 page;
1473 if rely > 1e-6
1474 then Printf.bprintf bb " rely='%f'" rely;
1475 if abs_float visy > 1e-6
1476 then Printf.bprintf bb " visy='%f'" visy;
1478 | Ohistory _ | Onone | Ouri _ | Oremote _
1479 | Oremotedest _ | Olaunch _ -> error "unexpected link in bookmarks"
1480 end;
1481 Buffer.add_string bb "/>\n";
1482 ) bookmarks;
1483 Buffer.add_string bb "</bookmarks>";
1484 if Buffer.length kb > 0
1485 then (
1486 Buffer.add_string bb "\n";
1487 Buffer.add_buffer bb kb;
1489 Buffer.add_string bb "\n</doc>\n";
1490 end;
1494 let pan, conf =
1495 match state.mode with
1496 | Birdseye (c, pan, _, _, _) ->
1497 let beyecolumns =
1498 match conf.columns with
1499 | Cmulti ((c, _, _), _) -> Some c
1500 | Csingle _ -> None
1501 | Csplit _ -> None
1502 and columns =
1503 match c.columns with
1504 | Cmulti (c, _) -> Cmulti (c, E.a)
1505 | Csingle _ -> Csingle E.a
1506 | Csplit _ -> failwith "quit from bird's eye while split"
1508 pan, { c with beyecolumns = beyecolumns; columns = columns }
1509 | Textentry _
1510 | View
1511 | LinkNav _ -> x, conf
1513 let docpath = if nonemptystr state.path then abspath state.path else E.s in
1514 if nonemptystr docpath
1515 then (
1516 adddoc docpath pan (getanchor ())
1518 let autoscrollstep =
1519 match state.autoscroll with
1520 | Some step -> step
1521 | None -> conf.autoscrollstep
1523 begin match state.mode with
1524 | Birdseye beye -> leavebirdseye beye true
1525 | Textentry _
1526 | View
1527 | LinkNav _ -> ()
1528 end;
1529 let key = try Digest.file docpath |> Digest.to_hex
1530 with _ -> E.s in
1531 { conf with autoscrollstep; key }
1533 state.bookmarks
1534 (now ())
1535 state.origin
1537 Hashtbl.iter (fun path (c, bookmarks, x, anchor, origin) ->
1538 if docpath <> abspath path
1539 then adddoc path x anchor c bookmarks c.lastvisit origin
1540 ) h;
1541 Buffer.add_string bb "</llppconfig>\n";
1542 true;
1545 let save leavebirdseye =
1546 let relx = float state.x /. float state.winw in
1547 let w, h, x =
1548 let cx w = truncate (relx *. float w) in
1549 List.fold_left
1550 (fun (w, h, x) ws ->
1551 match ws with
1552 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
1553 | Wsi.MaxVert -> (w, conf.cwinh, x)
1554 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
1556 (state.winw, state.winh, state.x) state.winstate
1558 conf.cwinw <- w;
1559 conf.cwinh <- h;
1560 let bb = Buffer.create 32768 in
1561 let save2 (h, dc) = save1 bb leavebirdseye x h dc in
1562 if load1 save2 && Buffer.length bb > 0
1563 then
1565 let tmp = !confpath ^ ".tmp" in
1566 let oc = open_out_bin tmp in
1567 Buffer.output_buffer oc bb;
1568 close_out oc;
1569 Unix.rename tmp !confpath;
1570 with exn -> dolog "error saving configuration: %s" @@ exntos exn
1573 let gc () =
1574 let href = ref @@ Hashtbl.create 0 in
1575 let cref = ref defconf in
1576 let push (h, dc) =
1577 let f path v =
1578 if Sys.file_exists path
1579 then Some v
1580 else (dolog "removing entry for '%s'" path; None) in
1581 Hashtbl.filter_map_inplace f h;
1582 href := h;
1583 cref := dc;
1584 true
1586 ignore (load1 push);
1587 let bb = Buffer.create 32768 in
1588 let save2 (_h, dc) = save1 bb (fun _ _ -> ()) 0 !href dc in
1589 if load1 save2 && Buffer.length bb > 0
1590 then (
1592 let tmp = !confpath ^ ".tmp" in
1593 let oc = open_out_bin tmp in
1594 Buffer.output_buffer oc bb;
1595 close_out oc;
1596 Unix.rename tmp !confpath;
1597 with exn -> dolog "error saving configuration: %s" @@ exntos exn
1601 let logcurrently = function
1602 | Idle -> dolog "Idle"
1603 | Loading (l, gen) ->
1604 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1605 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1606 dolog "Tiling %d[%d,%d] page=%s cs=%s angle=%d"
1607 l.pageno col row (~> pageopaque)
1608 (CSTE.to_string colorspace) angle;
1609 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1610 angle gen conf.angle state.gen
1611 tilew tileh
1612 conf.tilew conf.tileh
1613 | Outlining _ -> dolog "outlining"
1616 let logpage l =
1617 dolog {|l %d dim=%d {
1618 WxH %dx%d
1619 vWxH %dx%d
1620 pagex,y %d,%d
1621 dispx,y %d,%d
1622 column %d
1624 l.pageno l.pagedimno
1625 l.pagew l.pageh
1626 l.pagevw l.pagevh
1627 l.pagex l.pagey
1628 l.pagedispx l.pagedispy
1629 l.pagecol