Whoops
[llpp.git] / config.ml
blob4b5b2014cd17442ba819a7f1b1e86e4df61e3b6c
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) =
22 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
25 let multicolumns_to_string (n, a, b) =
26 if a = 0 && b = 0
27 then Printf.sprintf "%d" n
28 else Printf.sprintf "%d,%d,%d" n a b;
31 let multicolumns_of_string s =
32 try
33 (int_of_string s, 0, 0)
34 with _ ->
35 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
36 if a > 1 || b > 1
37 then error "subtly broken";
38 (n, a, b)
42 include Confstruct;;
44 type angle = int
45 and opaque = Opaque.t
46 and rectcolor = rgba
47 and pixmapsize = int
48 and gen = int
49 and top = float
50 and dtop = float
51 and fontpath = string
52 and trimcachepath = string
53 and aalevel = int
54 and trimmargins = bool
55 and trimparams = (trimmargins * irect)
56 and usefontconfig = bool
57 and usedoccss = bool
58 and uri = string
59 and caption = string
60 and tilex = int
61 and tiley = int
62 and tileparams = (x * y * width * height * tilex * tiley)
63 and under =
64 | Unone
65 | Ulinkuri of string
66 | Utext of facename
67 | Uannotation of (opaque * slinkindex)
68 and slinkindex = int
69 and facename = string
70 and launchcommand = string
71 and filename = string
72 and linkno = int
73 and destname = string
74 and link =
75 | Lnotfound
76 | Lfound of int
77 and linkdir =
78 | LDfirst
79 | LDlast
80 | LDfirstvisible of (int * int * int)
81 | LDleft of int
82 | LDright of int
83 | LDdown of int
84 | LDup of int
85 and pagewithlinks =
86 | Pwlnotfound
87 | Pwl of int
88 and scrollb = int
89 and anchor = pageno * top * dtop
90 and rect = float * float * float * float * float * float * float * float
91 and infochange = | Memused | Docinfo | Pdim
94 class type uioh =
95 object
96 method display : unit
97 method key : int -> int -> uioh
98 method button : int -> bool -> int -> int -> int -> uioh
99 method multiclick : int -> int -> int -> int -> uioh
100 method motion : int -> int -> uioh
101 method pmotion : int -> int -> uioh
102 method infochanged : infochange -> unit
103 method scrollpw : (int * float * float)
104 method scrollph : (int * float * float)
105 method modehash : keyhash
106 method eformsgs : bool
107 method alwaysscrolly : bool
108 method scroll : int -> int -> uioh
109 method zoom : float -> int -> int -> unit
110 end;;
112 module type TextEnumType = sig
113 type t
114 val name : string
115 val names : string array
116 end;;
118 module TextEnumMake (Ten : TextEnumType) = struct
119 let names = Ten.names;;
120 let to_int (t : Ten.t) = Obj.magic t;;
121 let to_string t = names.(to_int t);;
122 let of_int n : Ten.t = Obj.magic n;;
123 let of_string s =
124 let rec find i =
125 if i = Array.length names
126 then error "invalid %s: %s" Ten.name s
127 else (
128 if Ten.names.(i) = s
129 then of_int i
130 else find (i+1)
132 in find 0;;
133 end;;
135 module CSTE = TextEnumMake (struct
136 type t = colorspace;;
137 let name = "colorspace";;
138 let names = [|"rgb"; "bgr"; "gray"|];;
139 end);;
141 module MTE = TextEnumMake (struct
142 type t = mark;;
143 let name = "mark";;
144 let names = [|"page"; "block"; "line"; "word"|];;
145 end);;
147 module FMTE = TextEnumMake (struct
148 type t = fitmodel;;
149 let name = "fitmodel";;
150 let names = [|"width"; "proportional"; "page"|];;
151 end);;
153 type outlinekind =
154 | Onone
155 | Oanchor of anchor
156 | Ouri of uri
157 | Olaunch of launchcommand
158 | Oremote of (filename * pageno)
159 | Oremotedest of (filename * destname)
160 | Ohistory of (filename * conf * outline list * x * anchor * filename)
161 and outline = (caption * outlinelevel * outlinekind)
162 and outlinelevel = int
165 type page =
166 { pageno : int
167 ; pagedimno : int
168 ; pagew : int
169 ; pageh : int
170 ; pagex : int
171 ; pagey : int
172 ; pagevw : int
173 ; pagevh : int
174 ; pagedispx : int
175 ; pagedispy : int
176 ; pagecol : int
180 type tile = opaque * pixmapsize * elapsed
181 and elapsed = float;;
182 type pagemapkey = pageno * gen;;
183 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
184 and row = int
185 and col = int
186 and currently =
187 | Idle
188 | Loading of (page * gen)
189 | Tiling of (
190 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 state =
246 { mutable ss : Unix.file_descr
247 ; mutable wsfd : Unix.file_descr
248 ; mutable stderr : Unix.file_descr
249 ; mutable errmsgs : Buffer.t
250 ; mutable newerrmsgs : bool
251 ; mutable w : int
252 ; mutable x : x
253 ; mutable y : y
254 ; mutable anchor : anchor
255 ; mutable ranchors : (string * string * anchor * string) list
256 ; mutable maxy : int
257 ; mutable layout : page list
258 ; pagemap : (pagemapkey, opaque) Hashtbl.t
259 ; tilemap : (tilemapkey, tile) Hashtbl.t
260 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
261 ; mutable pdims : (pageno * width * height * leftx) list
262 ; mutable pagecount : int
263 ; mutable currently : currently
264 ; mutable mstate : mstate
265 ; mutable searchpattern : string
266 ; mutable rects : (pageno * rectcolor * rect) list
267 ; mutable rects1 : (pageno * rectcolor * rect) list
268 ; prects : (pageno, float array) Hashtbl.t
269 ; mutable text : string
270 ; mutable winstate : Wsi.winstate list
271 ; mutable mode : mode
272 ; mutable uioh : uioh
273 ; mutable outlines : outline array
274 ; mutable bookmarks : outline list
275 ; mutable path : string
276 ; mutable password : string
277 ; mutable nameddest : string
278 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
279 ; mutable memused : memsize
280 ; mutable gen : gen
281 ; mutable autoscroll : int option
282 ; mutable help : helpitem array
283 ; mutable docinfo : (int * string) list
284 ; hists : hists
285 ; mutable prevzoom : (float * int)
286 ; mutable progress : float
287 ; mutable mpos : mpos
288 ; mutable keystate : keystate
289 ; mutable glinks : bool
290 ; mutable prevcolumns : (columns * float) option
291 ; mutable winw : int
292 ; mutable winh : int
293 ; mutable reprf : (unit -> unit)
294 ; mutable origin : string
295 ; mutable roam : (unit -> unit)
296 ; mutable bzoom : bool
297 ; mutable lnava : (pageno * linkno) option
298 ; mutable slideshow : int
300 and hists =
301 { pat : string circbuf
302 ; pag : string circbuf
303 ; nav : anchor circbuf
304 ; sel : string circbuf
308 let emptyanchor = (0, 0.0, 0.0);;
309 let emptykeyhash = Hashtbl.create 0;;
310 let noreprf () = ();;
311 let noroam () = ();;
313 let nouioh : uioh =
314 object (self)
315 method display = ()
316 method key _ _ = self
317 method multiclick _ _ _ _ = self
318 method button _ _ _ _ _ = self
319 method motion _ _ = self
320 method pmotion _ _ = self
321 method infochanged _ = ()
322 method scrollpw = (0, nan, nan)
323 method scrollph = (0, nan, nan)
324 method modehash = emptykeyhash
325 method eformsgs = false
326 method alwaysscrolly = false
327 method scroll _ _ = self
328 method zoom _ _ _ = ()
329 end;;
331 let platform_to_string = function
332 | Punknown -> "unknown"
333 | Plinux -> "Linux"
334 | Pmacos -> "macOS"
335 | Pbsd -> "BSD"
338 let conf = { defconf with keyhashes = copykeyhashes defconf };;
340 let cbnew n v =
341 { store = Array.make n v
342 ; rc = 0
343 ; wc = 0
344 ; len = 0
348 let cbcap b = Array.length b.store;;
350 let cbput ?(update_rc=true) b v =
351 let cap = cbcap b in
352 b.store.(b.wc) <- v;
353 b.wc <- (b.wc + 1) mod cap;
354 if update_rc
355 then b.rc <- b.wc;
356 b.len <- min (b.len + 1) cap;
359 let cbput_dont_update_rc b v = cbput ~update_rc:false b v;;
361 let cbempty b = b.len = 0;;
363 let cbgetg b circular dir =
364 if cbempty b
365 then b.store.(0)
366 else
367 let rc = b.rc + dir in
368 let rc =
369 if circular
370 then (
371 if rc = -1
372 then b.len-1
373 else (
374 if rc >= b.len
375 then 0
376 else rc
379 else bound rc 0 (b.len-1)
381 b.rc <- rc;
382 b.store.(rc);
385 let cbget b = cbgetg b false;;
386 let cbgetc b = cbgetg b true;;
388 let state =
389 { ss = Unix.stdin
390 ; wsfd = Unix.stdin
391 ; stderr = Unix.stderr
392 ; errmsgs = Buffer.create 0
393 ; newerrmsgs = false
394 ; x = 0
395 ; y = 0
396 ; w = 0
397 ; anchor = emptyanchor
398 ; ranchors = []
399 ; layout = []
400 ; maxy = max_int
401 ; tilelru = Queue.create ()
402 ; pagemap = Hashtbl.create 10
403 ; tilemap = Hashtbl.create 10
404 ; pdims = []
405 ; pagecount = 0
406 ; currently = Idle
407 ; mstate = Mnone
408 ; rects = []
409 ; rects1 = []
410 ; prects = Hashtbl.create 1
411 ; text = E.s
412 ; mode = View
413 ; winstate = []
414 ; searchpattern = E.s
415 ; outlines = E.a
416 ; bookmarks = []
417 ; path = E.s
418 ; password = E.s
419 ; nameddest = E.s
420 ; geomcmds = E.s, []
421 ; hists =
422 { nav = cbnew 10 emptyanchor
423 ; pat = cbnew 10 E.s
424 ; pag = cbnew 10 E.s
425 ; sel = cbnew 10 E.s
427 ; memused = 0
428 ; gen = 0
429 ; autoscroll = None
430 ; help = E.a
431 ; docinfo = []
432 ; prevzoom = (1.0, 0)
433 ; progress = -1.0
434 ; uioh = nouioh
435 ; mpos = (-1, -1)
436 ; keystate = KSnone
437 ; glinks = false
438 ; prevcolumns = None
439 ; winw = -1
440 ; winh = -1
441 ; reprf = noreprf
442 ; origin = E.s
443 ; roam = noroam
444 ; bzoom = false
445 ; lnava = None
446 ; slideshow = 0
450 let calcips h =
451 let d = state.winh - h in
452 max conf.interpagespace ((d + 1) / 2)
455 let rowyh (c, coverA, coverB) b n =
456 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
457 then
458 let _, _, vy, (_, _, h, _) = b.(n) in
459 (vy, h)
460 else
461 let n' = n - coverA in
462 let d = n' mod c in
463 let s = n - d in
464 let e = min state.pagecount (s + c) in
465 let rec find m miny maxh = if m = e then miny, maxh else
466 let _, _, y, (_, _, h, _) = b.(m) in
467 let miny = min miny y in
468 let maxh = max maxh h in
469 find (m+1) miny maxh
470 in find s max_int 0
473 let page_of_y y =
474 let ((c, coverA, coverB) as cl), b =
475 match conf.columns with
476 | Csplit (_, b) | Csingle b -> (1, 0, 0), b
477 | Cmulti (c, b) -> c, b
479 if Array.length b = 0
480 then -1
481 else
482 let rec bsearch nmin nmax =
483 if nmin > nmax
484 then bound nmin 0 (state.pagecount-1)
485 else
486 let n = (nmax + nmin) / 2 in
487 let vy, h = rowyh cl b n in
488 let y0, y1 =
489 if conf.presentation
490 then
491 let ips = calcips h in
492 let y0 = vy - ips in
493 let y1 = vy + h + ips in
494 y0, y1
495 else (
496 if n = 0
497 then 0, vy + h + conf.interpagespace
498 else
499 let y0 = vy - conf.interpagespace in
500 y0, y0 + h + conf.interpagespace
503 if y >= y0 && y < y1
504 then (
505 if c = 1
506 then n
507 else (
508 if n > coverA
509 then
510 if n < state.pagecount - coverB
511 then ((n-coverA)/c)*c + coverA
512 else n
513 else n
516 else (
517 if y > y0
518 then bsearch (n+1) nmax
519 else bsearch nmin (n-1)
522 bsearch 0 (state.pagecount-1);
525 let calcheight () =
526 match conf.columns with
527 | Cmulti ((_, _, _) as cl, b) ->
528 if Array.length b > 0
529 then
530 let y, h = rowyh cl b (Array.length b - 1) in
531 y + h + (if conf.presentation then calcips h else 0)
532 else 0
533 | Csingle b ->
534 if Array.length b > 0
535 then
536 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
537 y + h + (if conf.presentation then calcips h else 0)
538 else 0
539 | Csplit (_, b) ->
540 if Array.length b > 0
541 then
542 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
543 y + h
544 else 0
547 let getpageywh pageno =
548 let pageno = bound pageno 0 (state.pagecount-1) in
549 match conf.columns with
550 | Csingle b ->
551 if Array.length b = 0
552 then 0, 0, 0
553 else
554 let (_, _, y, (_, w, h, _)) = b.(pageno) in
555 let y =
556 if conf.presentation
557 then y - calcips h
558 else y
560 y, w, h
561 | Cmulti (cl, b) ->
562 if Array.length b = 0
563 then 0, 0, 0
564 else
565 let y, h = rowyh cl b pageno in
566 let (_, _, _, (_, w, _, _)) = b.(pageno) in
567 let y =
568 if conf.presentation
569 then y - calcips h
570 else y
572 y, w, h
573 | Csplit (c, b) ->
574 if Array.length b = 0
575 then 0, 0, 0
576 else
577 let n = pageno*c in
578 let (_, _, y, (_, w, h, _)) = b.(n) in
579 y, w / c, h
582 let getpageyh pageno =
583 let y,_,h = getpageywh pageno in
584 y, h;
587 let getpagedim pageno =
588 let rec f ppdim l =
589 match l with
590 | (n, _, _, _) as pdim :: rest ->
591 if n >= pageno
592 then (if n = pageno then pdim else ppdim)
593 else f pdim rest
595 | [] -> ppdim
597 f (-1, -1, -1, -1) state.pdims
600 let getpdimno pageno =
601 let rec f p l =
602 let np = succ p in
603 match l with
604 | (n, _, _, _) :: rest ->
605 if n >= pageno
606 then (if n = pageno then np else p)
607 else f np rest
609 | [] -> p
611 f ~-1 state.pdims
614 let getpagey pageno = fst (getpageyh pageno);;
616 let getanchor1 l =
617 let top =
618 let coloff = l.pagecol * l.pageh in
619 float (l.pagey + coloff) /. float l.pageh
621 let dtop =
622 if l.pagedispy = 0
623 then 0.0
624 else (
625 if conf.presentation
626 then float l.pagedispy /. float (calcips l.pageh)
627 else float l.pagedispy /. float conf.interpagespace
630 (l.pageno, top, dtop)
633 let getanchor () =
634 match state.layout with
635 | l :: _ -> getanchor1 l
636 | [] ->
637 let n = page_of_y state.y in
638 if n = -1
639 then state.anchor
640 else
641 let y, h = getpageyh n in
642 let dy = y - state.y in
643 let dtop =
644 if conf.presentation
645 then
646 let ips = calcips h in
647 float (dy + ips) /. float ips
648 else
649 float dy /. float conf.interpagespace
651 (n, 0.0, dtop)
654 let fontpath = ref E.s;;
656 type historder = [ `lastvisit | `title | `path | `file ];;
658 module KeyMap =
659 Map.Make (struct type t = (int * int) let compare = compare end);;
661 let unentS s =
662 let l = String.length s in
663 let b = Buffer.create l in
664 Parser.unent b s 0 l;
665 Buffer.contents b;
668 let home =
669 try Sys.getenv "HOME"
670 with exn ->
671 dolog "cannot determine home directory location: %s" @@ exntos exn;
675 let modifier_of_string = function
676 | "alt" -> Wsi.altmask
677 | "shift" -> Wsi.shiftmask
678 | "ctrl" | "control" -> Wsi.ctrlmask
679 | "meta" -> Wsi.metamask
680 | _ -> 0
683 let keys_of_string s =
684 let key_of_string r s =
685 let elems = Str.full_split r s in
686 let f n k m =
687 let g s =
688 let m1 = modifier_of_string s in
689 if m1 = 0
690 then (Wsi.namekey s, m)
691 else (k, m lor m1)
692 in function
693 | Str.Delim s when n land 1 = 0 -> g s
694 | Str.Text s -> g s
695 | Str.Delim _ -> (k, m)
697 let rec loop n k m = function
698 | [] -> (k, m)
699 | x :: xs ->
700 let k, m = f n k m x in
701 loop (n+1) k m xs
703 loop 0 0 0 elems
705 let elems = Str.split Utils.Re.whitespace s in
706 List.map (key_of_string (Str.regexp "-")) elems
709 let config_of c attrs =
710 let apply c k v =
712 match k with
713 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
714 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
715 | "case-insensitive-search" -> { c with icase = bool_of_string v }
716 | "preload" -> { c with preload = bool_of_string v }
717 | "page-bias" -> { c with pagebias = int_of_string v }
718 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
719 | "horizontal-scroll-step" ->
720 { c with hscrollstep = max (int_of_string v) 1 }
721 | "auto-scroll-step" ->
722 { c with autoscrollstep = max 0 (int_of_string v) }
723 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
724 | "highlight-links" -> { c with hlinks = bool_of_string v }
725 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
726 | "vertical-margin" ->
727 { c with interpagespace = max 0 (int_of_string v) }
728 | "zoom" ->
729 let zoom = float_of_string v /. 100. in
730 let zoom = max zoom 0.0 in
731 { c with zoom = zoom }
732 | "presentation" -> { c with presentation = bool_of_string v }
733 | "rotation-angle" -> { c with angle = int_of_string v }
734 | "width" -> { c with cwinw = max 20 (int_of_string v) }
735 | "height" -> { c with cwinh = max 20 (int_of_string v) }
736 | "proportional-display" ->
737 let fm =
738 if bool_of_string v
739 then FitProportional
740 else FitWidth
742 { c with fitmodel = fm }
743 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
744 | "pixmap-cache-size" ->
745 { c with memlimit = max 2 (int_of_string_with_suffix v) }
746 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
747 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
748 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
749 | "background-color" -> { c with bgcolor = color_of_string v }
750 | "scrollbar-color" -> { c with sbarcolor = rgba_of_string v }
751 | "scrollbar-handle-color" -> { c with sbarhndlcolor = rgba_of_string v }
752 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
753 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
754 | "mupdf-store-size" ->
755 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
756 | "checkers" -> { c with checkers = bool_of_string v }
757 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
758 | "trim-margins" -> { c with trimmargins = bool_of_string v }
759 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
760 | "uri-launcher" -> { c with urilauncher = unentS v }
761 | "path-launcher" -> { c with pathlauncher = unentS v }
762 | "color-space" -> { c with colorspace = CSTE.of_string v }
763 | "invert-colors" -> { c with invert = bool_of_string v }
764 | "brightness" -> { c with colorscale = float_of_string v }
765 | "columns" ->
766 let (n, _, _) as nab = multicolumns_of_string v in
767 if n < 0
768 then { c with columns = Csplit (-n, E.a) }
769 else { c with columns = Cmulti (nab, E.a) }
770 | "birds-eye-columns" ->
771 { c with beyecolumns = Some (max (int_of_string v) 2) }
772 | "selection-command" -> { c with selcmd = unentS v }
773 | "synctex-command" -> { c with stcmd = unentS v }
774 | "pax-command" -> { c with paxcmd = unentS v }
775 | "askpass-command" -> { c with passcmd = unentS v }
776 | "savepath-command" -> { c with savecmd = unentS v }
777 | "update-cursor" -> { c with updatecurs = bool_of_string v }
778 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
779 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
780 | "use-pbo" -> { c with usepbo = bool_of_string v }
781 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
782 | "horizontal-scrollbar-visible" ->
783 let b =
784 if bool_of_string v
785 then c.scrollb lor scrollbhv
786 else c.scrollb land (lnot scrollbhv)
788 { c with scrollb = b }
789 | "vertical-scrollbar-visible" ->
790 let b =
791 if bool_of_string v
792 then c.scrollb lor scrollbvv
793 else c.scrollb land (lnot scrollbvv)
795 { c with scrollb = b }
796 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
797 | "point-and-x" ->
798 { c with pax =
799 if bool_of_string v
800 then Some 0.0
801 else None }
802 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
803 | "scroll-bar-on-the-left" -> { c with leftscroll = bool_of_string v }
804 | "title" -> { c with title = unentS v }
805 | "last-visit" -> { c with lastvisit = float_of_string v }
806 | "edit-annotations-inline" -> { c with annotinline = bool_of_string v }
807 | "coarse-presentation-positioning" ->
808 { c with coarseprespos = bool_of_string v }
809 | "use-document-css" -> { c with usedoccss = bool_of_string v }
810 | _ -> c
811 with exn ->
812 dolog "error processing attribute (`%S' = `%S'): %s" k v @@ exntos exn;
815 let rec fold c = function
816 | [] -> c
817 | (k, v) :: rest ->
818 let c = apply c k v in
819 fold c rest
821 fold { c with keyhashes = copykeyhashes c } attrs;
824 let fromstring f pos n v d =
825 try f v
826 with exn ->
827 dolog "error processing attribute (%S=%S) at %d\n%s" n v pos @@ exntos exn;
831 let bookmark_of attrs =
832 let rec fold title page rely visy = function
833 | ("title", v) :: rest -> fold v page rely visy rest
834 | ("page", v) :: rest -> fold title v rely visy rest
835 | ("rely", v) :: rest -> fold title page v visy rest
836 | ("visy", v) :: rest -> fold title page rely v rest
837 | _ :: rest -> fold title page rely visy rest
838 | [] -> title, page, rely, visy
840 fold "invalid" "0" "0" "0" attrs
843 let doc_of attrs =
844 let rec fold path key page rely pan visy origin = function
845 | ("path", v) :: rest -> fold v key page rely pan visy origin rest
846 | ("key", v) :: rest -> fold path v page rely pan visy origin rest
847 | ("page", v) :: rest -> fold path key v rely pan visy origin rest
848 | ("rely", v) :: rest -> fold path key page v pan visy origin rest
849 | ("pan", v) :: rest -> fold path key page rely v visy origin rest
850 | ("visy", v) :: rest -> fold path key page rely pan v origin rest
851 | ("origin", v) :: rest -> fold path key page rely pan visy v rest
852 | _ :: rest -> fold path key page rely pan visy origin rest
853 | [] -> path, key, page, rely, pan, visy, origin
855 fold E.s E.s "0" "0" "0" "0" 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 = doc_of attrs in
914 let path = unentS pathent
915 and origin = unentS origin
916 and pageno = fromstring int_of_string spos "page" spage 0
917 and rely = fromstring float_of_string spos "rely" srely 0.0
918 and pan = fromstring int_of_string spos "pan" span 0
919 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
920 let c = config_of dc attrs in
921 c.key <- key;
922 let anchor = (pageno, rely, visy) in
923 if closed
924 then (Hashtbl.add h path (c, [], pan, anchor, origin); v)
925 else { v with f = doc path origin pan anchor c [] }
927 | Vopen _ ->
928 parse_error "unexpected subelement in llppconfig" s spos
930 | Vclose "llppconfig" -> { v with f = toplevel }
931 | Vclose _ -> parse_error "unexpected close in llppconfig" s spos
933 and defaults v t spos _ =
934 match t with
935 | Vdata | Vcdata -> v
936 | Vend -> parse_error "unexpected end of input in defaults" s spos
937 | Vopen ("keymap", attrs, closed) ->
938 let modename =
939 try List.assoc "mode" attrs
940 with Not_found -> "global" in
941 if closed
942 then v
943 else
944 let ret keymap =
945 let h = findkeyhash dc modename in
946 KeyMap.iter (Hashtbl.replace h) keymap;
947 defaults
949 { v with f = pkeymap ret KeyMap.empty }
951 | Vopen (_, _, _) ->
952 parse_error "unexpected subelement in defaults" s spos
954 | Vclose "defaults" ->
955 { v with f = llppconfig }
957 | Vclose _ -> parse_error "unexpected close in defaults" s spos
959 and uifont b v t spos epos =
960 match t with
961 | Vdata | Vcdata ->
962 Buffer.add_substring b s spos (epos - spos);
964 | Vopen (_, _, _) ->
965 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 _ ->
1043 parse_error "unexpected subelement in keymap" s spos
1045 | Vclose "keymap" ->
1046 { v with f = ret keymap }
1048 | Vclose _ -> parse_error "unexpected close in keymap" s spos
1050 and pbookmarks path origin pan anchor c bookmarks v t spos _ =
1051 match t with
1052 | Vdata | Vcdata -> v
1053 | Vend -> parse_error "unexpected end of input in bookmarks" s spos
1054 | Vopen ("item", attrs, closed) ->
1055 let titleent, spage, srely, svisy = bookmark_of attrs in
1056 let page = fromstring int_of_string spos "page" spage 0
1057 and rely = fromstring float_of_string spos "rely" srely 0.0
1058 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1059 let bookmarks =
1060 (unentS titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
1062 if closed
1063 then { v with f = pbookmarks path origin pan anchor c bookmarks }
1064 else
1065 let f () = v in
1066 { v with f = skip "item" f }
1068 | Vopen _ ->
1069 parse_error "unexpected subelement in bookmarks" s spos
1071 | Vclose "bookmarks" ->
1072 { v with f = doc path origin pan anchor c bookmarks }
1074 | Vclose _ -> parse_error "unexpected close in bookmarks" s spos
1076 and skip tag f v t spos _ =
1077 match t with
1078 | Vdata | Vcdata -> v
1079 | Vend ->
1080 parse_error ("unexpected end of input in skipped " ^ tag) s spos
1081 | Vopen (tag', _, closed) ->
1082 if closed
1083 then v
1084 else
1085 let f' () = { v with f = skip tag f } in
1086 { v with f = skip tag' f' }
1087 | Vclose ctag ->
1088 if tag = ctag
1089 then f ()
1090 else parse_error ("unexpected close in skipped " ^ tag) s spos
1093 parse { f = toplevel; accu = () } s;
1094 h, dc;
1097 let do_load f contents =
1098 try f contents
1099 with
1100 | Parser.Parse_error (msg, s, pos) ->
1101 let subs = Parser.subs s pos in
1102 Utils.error "parse error: %s: at %d [..%S..]" msg pos subs
1104 | exn -> Utils.error "parse error: %s" @@ exntos exn
1107 let defconfpath =
1108 let dir =
1109 let xdgconfdir = Utils.getenvwithdef "XDG_CONFIG_HOME" E.s in
1110 if emptystr xdgconfdir
1111 then
1113 let dir = Filename.concat home ".config" in
1114 if Sys.is_directory dir then dir else home
1115 with _ -> home
1116 else xdgconfdir
1118 Filename.concat dir "llpp.conf"
1121 let confpath = ref defconfpath;;
1123 let load2 f default =
1124 match filecontents !confpath with
1125 | contents -> f @@ do_load get contents
1126 | exception Unix.Unix_error (Unix.ENOENT, "open", _) ->
1127 f (Hashtbl.create 0, defconf)
1128 | exception exn ->
1129 dolog "error loading configuration from `%S': %s" !confpath @@ exntos exn;
1130 default
1133 let load1 f = load2 f false;;
1135 let load openlast =
1136 let f (h, dc) =
1137 if openlast
1138 then (
1139 let path, _ =
1140 Hashtbl.fold
1141 (fun path (conf, _, _, _, _) ((_, besttime) as best) ->
1142 if conf.lastvisit > besttime
1143 then (path, conf.lastvisit)
1144 else best)
1146 (state.path, -.infinity)
1148 state.path <- path;
1150 let pc, pb, px, pa, po =
1151 let def = dc, [], 0, emptyanchor, state.origin in
1152 if emptystr state.path
1153 then def
1154 else
1155 let absname = abspath state.path in
1156 match Hashtbl.find h absname with
1157 | (c,b,x,a,_) -> (c,b,x,a,state.origin)
1158 | exception Not_found ->
1159 let exception E of (conf * outline list * int * anchor * string) in
1160 let key = try Digest.file absname |> Digest.to_hex with _ -> E.s in
1161 match (
1162 if nonemptystr key
1163 then
1164 Hashtbl.iter (fun p ((c, _, _, _, _) as v) ->
1165 if c.key = key
1166 then (
1167 dolog "will use %s's settings due to matching keys" p;
1168 raise (E v)
1172 with
1173 | _ -> def
1174 | exception E v -> v
1176 setconf defconf dc;
1177 setconf conf pc;
1178 state.bookmarks <- pb;
1179 state.x <- px;
1180 state.origin <- po;
1181 state.anchor <- pa;
1182 cbput state.hists.nav pa;
1183 true
1185 load1 f
1188 let gethist () =
1189 let f (h, _) =
1190 Hashtbl.fold (fun path (pc, pb, px, pa, po) accu ->
1191 (path, pc, pb, px, pa, po) :: accu)
1192 h [];
1194 load2 f []
1197 let add_attrs bb always dc c time =
1198 let o' fmt s =
1199 Buffer.add_string bb "\n ";
1200 Printf.bprintf bb fmt s
1202 let o c fmt s = if c then o' fmt s else ignore in
1203 let ob s a b = o (always || a != b) "%s='%b'" s a
1204 and op s a b = o (always || a <> b) "%s='%b'" s (a != None)
1205 and oi s a b = o (always || a != b) "%s='%d'" s a
1206 and oI s a b = o (always || a != b) "%s='%s'" s (string_with_suffix_of_int a)
1207 and oz s a b = o (always || a <> b) "%s='%g'" s (a*.100.)
1208 and oF s a b = o (always || a <> b) "%s='%f'" s a
1209 and oL s a b = o (always || a <> b) "%s='%Ld'" s a
1210 and oc s a b = o (always || a <> b) "%s='%s'" s (color_to_string a)
1211 and oA s a b = o (always || a <> b) "%s='%s'" s (rgba_to_string a)
1212 and oC s a b = o (always || a <> b) "%s='%s'" s (CSTE.to_string a)
1213 and oR s a b = o (always || a <> b) "%s='%s'" s (irect_to_string a)
1214 and oFm s a b = o (always || a <> b) "%s='%s'" s (FMTE.to_string a)
1215 and oSv s a b m =
1216 o (always || a land m <> b land m) "%s='%b'" s (a land m != 0)
1217 and oPm s a b = o (always || a <> b) "%s='%s'" s (MTE.to_string a)
1218 and os s a b =
1219 o (always || a <> b) "%s='%s'" s @@ Parser.enent a 0 (String.length a)
1220 and oco s a b =
1221 if always || a <> b
1222 then
1223 match a with
1224 | Cmulti ((n, a, b), _) when n > 1 -> o' "%s='%d,%d,%d'" s n a b
1225 | Csplit (n, _) when n > 1 -> o' "%s='%d'" s ~-n
1226 | Cmulti _ | Csplit _ | Csingle _ -> ()
1227 and obeco s a b =
1228 if always || a <> b
1229 then
1230 match a with
1231 | Some c when c > 1 -> o' "%s='%d'" s c
1232 | _ -> ()
1234 oi "width" c.cwinw dc.cwinw;
1235 oi "height" c.cwinh dc.cwinh;
1236 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
1237 oi "scroll-handle-height" c.scrollh dc.scrollh;
1238 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
1239 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
1240 ob "case-insensitive-search" c.icase dc.icase;
1241 ob "preload" c.preload dc.preload;
1242 oi "page-bias" c.pagebias dc.pagebias;
1243 oi "scroll-step" c.scrollstep dc.scrollstep;
1244 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
1245 ob "max-height-fit" c.maxhfit dc.maxhfit;
1246 ob "highlight-links" c.hlinks dc.hlinks;
1247 ob "under-cursor-info" c.underinfo dc.underinfo;
1248 oi "vertical-margin" c.interpagespace dc.interpagespace;
1249 oz "zoom" c.zoom dc.zoom;
1250 ob "presentation" c.presentation dc.presentation;
1251 oi "rotation-angle" c.angle dc.angle;
1252 oFm "fit-model" c.fitmodel dc.fitmodel;
1253 oI "pixmap-cache-size" c.memlimit dc.memlimit;
1254 oi "tex-count" c.texcount dc.texcount;
1255 oi "slice-height" c.sliceheight dc.sliceheight;
1256 oi "thumbnail-width" c.thumbw dc.thumbw;
1257 oc "background-color" c.bgcolor dc.bgcolor;
1258 oA "scrollbar-color" c.sbarcolor dc.sbarcolor;
1259 oA "scrollbar-handle-color" c.sbarhndlcolor dc.sbarhndlcolor;
1260 oi "tile-width" c.tilew dc.tilew;
1261 oi "tile-height" c.tileh dc.tileh;
1262 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
1263 ob "checkers" c.checkers dc.checkers;
1264 oi "aalevel" c.aalevel dc.aalevel;
1265 ob "trim-margins" c.trimmargins dc.trimmargins;
1266 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
1267 os "uri-launcher" c.urilauncher dc.urilauncher;
1268 os "path-launcher" c.pathlauncher dc.pathlauncher;
1269 oC "color-space" c.colorspace dc.colorspace;
1270 ob "invert-colors" c.invert dc.invert;
1271 oF "brightness" c.colorscale dc.colorscale;
1272 oco "columns" c.columns dc.columns;
1273 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
1274 os "selection-command" c.selcmd dc.selcmd;
1275 os "synctex-command" c.stcmd dc.stcmd;
1276 os "pax-command" c.paxcmd dc.paxcmd;
1277 os "askpass-command" c.passcmd dc.passcmd;
1278 os "savepath-command" c.savecmd dc.savecmd;
1279 ob "update-cursor" c.updatecurs dc.updatecurs;
1280 oi "hint-font-size" c.hfsize dc.hfsize;
1281 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
1282 oF "page-scroll-scale" c.pgscale dc.pgscale;
1283 ob "use-pbo" c.usepbo dc.usepbo;
1284 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
1285 ob "remote-in-a-new-instance" c.riani dc.riani;
1286 op "point-and-x" c.pax dc.pax;
1287 oPm "point-and-x-mark" c.paxmark dc.paxmark;
1288 ob "scroll-bar-on-the-left" c.leftscroll dc.leftscroll;
1289 if not always
1290 then os "title" c.title dc.title;
1291 oL "last-visit" (Int64.of_float time) 0L;
1292 ob "edit-annotations-inline" c.annotinline dc.annotinline;
1293 ob "coarse-presentation-positioning" c.coarseprespos dc.coarseprespos;
1294 ob "use-document-css" c.usedoccss dc.usedoccss;
1297 let keymapsbuf always dc c =
1298 let open Buffer in
1299 let bb = create 16 in
1300 let rec loop = function
1301 | [] -> ()
1302 | (modename, h) :: rest ->
1303 let dh = findkeyhash dc modename in
1304 if always || h <> dh
1305 then (
1306 if Hashtbl.length h > 0
1307 then (
1308 if length bb > 0 then add_char bb '\n';
1309 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
1310 Hashtbl.iter (fun i o ->
1311 if always || match Hashtbl.find dh i
1312 with | dO -> dO <> o | exception Not_found -> false
1313 then
1314 let addkm (k, m) =
1315 if Wsi.withctrl m then add_string bb "ctrl-";
1316 if Wsi.withalt m then add_string bb "alt-";
1317 if Wsi.withshift m then add_string bb "shift-";
1318 if Wsi.withmeta m then add_string bb "meta-";
1319 add_string bb (Wsi.keyname k);
1321 let addkms l =
1322 let rec loop = function
1323 | [] -> ()
1324 | km :: [] -> addkm km
1325 | km :: rest -> addkm km; add_char bb ' '; loop rest
1327 loop l
1329 add_string bb "<map in='";
1330 addkm i;
1331 match o with
1332 | KMinsrt km ->
1333 add_string bb "' out='"; addkm km; add_string bb "'/>\n"
1335 | KMinsrl kms ->
1336 add_string bb "' out='"; addkms kms; add_string bb "'/>\n"
1338 | KMmulti (ins, kms) ->
1339 add_char bb ' '; addkms ins; add_string bb "' out='";
1340 addkms kms; add_string bb "'/>\n"
1341 ) h;
1342 add_string bb "</keymap>";
1345 loop rest
1347 loop c.keyhashes;
1351 let keystostrlist c =
1352 let rec loop accu = function
1353 | [] -> accu
1354 | (modename, h) :: rest ->
1355 let accu =
1356 if Hashtbl.length h > 0
1357 then (
1358 let accu = Printf.sprintf "\xc2\xb7Keys for %s" modename :: accu in
1359 Hashtbl.fold (fun i o a ->
1360 let bb = Buffer.create 10 in
1361 let addkm (k, m) =
1362 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
1363 if Wsi.withalt m then Buffer.add_string bb "alt-";
1364 if Wsi.withshift m then Buffer.add_string bb "shift-";
1365 if Wsi.withmeta m then Buffer.add_string bb "meta-";
1366 Buffer.add_string bb (Wsi.keyname k);
1368 let addkms l =
1369 let rec loop = function
1370 | [] -> ()
1371 | km :: [] -> addkm km
1372 | km :: rest ->
1373 addkm km; Buffer.add_char bb ' ';
1374 loop rest
1376 loop l
1378 addkm i;
1379 Buffer.add_char bb '\t';
1380 begin match o with
1381 | KMinsrt km ->
1382 addkm km
1384 | KMinsrl kms ->
1385 addkms kms
1387 | KMmulti (ins, kms) ->
1388 Buffer.add_char bb ' ';
1389 addkms ins;
1390 Buffer.add_string bb "\t";
1391 addkms kms
1392 end;
1393 Buffer.contents bb :: a
1394 ) h accu
1396 else accu
1398 loop accu rest
1400 loop [] c.keyhashes
1403 let save1 bb leavebirdseye x h dc =
1404 let uifontsize = fstate.fontsize in
1405 let dc = if conf.bedefault then conf else dc in
1406 Buffer.add_string bb "<llppconfig>\n";
1408 if nonemptystr !fontpath
1409 then Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
1410 uifontsize
1411 !fontpath
1412 else
1413 if uifontsize <> 14
1414 then Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
1417 Buffer.add_string bb "<defaults";
1418 add_attrs bb true dc dc nan;
1419 let kb = keymapsbuf true dc dc in
1420 if Buffer.length kb > 0
1421 then (
1422 Buffer.add_string bb ">\n";
1423 Buffer.add_buffer bb kb;
1424 Buffer.add_string bb "\n</defaults>\n";
1426 else Buffer.add_string bb "/>\n";
1428 let adddoc path pan anchor c bookmarks time origin =
1429 if not (bookmarks == [] && c = dc && anchor = emptyanchor)
1430 then (
1431 Printf.bprintf bb "<doc path='%s'"
1432 (Parser.enent path 0 (String.length path));
1434 if nonemptystr c.key
1435 then Printf.bprintf bb "\n key='%s'" c.key;
1437 if nonemptystr origin
1438 then Printf.bprintf bb "\n origin='%s'"
1439 (Parser.enent origin 0 (String.length origin));
1441 if anchor <> emptyanchor
1442 then (
1443 let n, rely, visy = anchor in
1444 Printf.bprintf bb "\n page='%d'" n;
1446 if rely > 1e-6
1447 then Printf.bprintf bb " rely='%f'" rely;
1449 if abs_float visy > 1e-6
1450 then Printf.bprintf bb " visy='%f'" visy;
1453 if pan != 0
1454 then Printf.bprintf bb " pan='%d'" pan;
1456 add_attrs bb false dc c time;
1457 if nonemptystr c.css
1458 then Printf.bprintf bb ">\n <css><![CDATA[%s]]></css>" c.css;
1459 let kb = keymapsbuf false dc c in
1461 begin match bookmarks with
1462 | [] ->
1463 if Buffer.length kb > 0
1464 then (
1465 Buffer.add_string bb ">\n";
1466 Buffer.add_buffer bb kb;
1467 Buffer.add_string bb "\n</doc>\n";
1469 else
1470 if nonemptystr c.css
1471 then Buffer.add_string bb "\n</doc>\n"
1472 else Buffer.add_string bb "/>\n"
1473 | _ ->
1474 Buffer.add_string bb ">\n<bookmarks>\n";
1475 List.iter (fun (title, _, kind) ->
1476 begin match kind with
1477 | Oanchor (page, rely, visy) ->
1478 Printf.bprintf bb
1479 "<item title='%s' page='%d'"
1480 (Parser.enent title 0 (String.length title))
1481 page;
1482 if rely > 1e-6
1483 then Printf.bprintf bb " rely='%f'" rely;
1485 if abs_float visy > 1e-6
1486 then Printf.bprintf bb " visy='%f'" visy;
1487 | Ohistory _ | Onone | Ouri _ | Oremote _
1488 | Oremotedest _ | Olaunch _ -> error "unexpected link in bookmarks"
1489 end;
1490 Buffer.add_string bb "/>\n";
1491 ) bookmarks;
1492 Buffer.add_string bb "</bookmarks>";
1493 if Buffer.length kb > 0
1494 then (
1495 Buffer.add_string bb "\n";
1496 Buffer.add_buffer bb kb;
1498 Buffer.add_string bb "\n</doc>\n";
1499 end;
1503 let pan, conf =
1504 match state.mode with
1505 | Birdseye (c, pan, _, _, _) ->
1506 let beyecolumns =
1507 match conf.columns with
1508 | Cmulti ((c, _, _), _) -> Some c
1509 | Csingle _ -> None
1510 | Csplit _ -> None
1511 and columns =
1512 match c.columns with
1513 | Cmulti (c, _) -> Cmulti (c, E.a)
1514 | Csingle _ -> Csingle E.a
1515 | Csplit _ -> failwith "quit from bird's eye while split"
1517 pan, { c with beyecolumns = beyecolumns; columns = columns }
1518 | Textentry _
1519 | View
1520 | LinkNav _ -> x, conf
1522 let docpath = if nonemptystr state.path then abspath state.path else E.s in
1523 if nonemptystr docpath
1524 then (
1525 adddoc docpath pan (getanchor ())
1527 let autoscrollstep =
1528 match state.autoscroll with
1529 | Some step -> step
1530 | None -> conf.autoscrollstep
1532 begin match state.mode with
1533 | Birdseye beye -> leavebirdseye beye true
1534 | Textentry _
1535 | View
1536 | LinkNav _ -> ()
1537 end;
1538 let key = try Digest.file docpath |> Digest.to_hex
1539 with _ -> E.s in
1540 { conf with autoscrollstep; key }
1542 state.bookmarks
1543 (now ())
1544 state.origin
1546 Hashtbl.iter (fun path (c, bookmarks, x, anchor, origin) ->
1547 if docpath <> abspath path
1548 then adddoc path x anchor c bookmarks c.lastvisit origin
1549 ) h;
1550 Buffer.add_string bb "</llppconfig>\n";
1551 true;
1554 let save leavebirdseye =
1555 let relx = float state.x /. float state.winw in
1556 let w, h, x =
1557 let cx w = truncate (relx *. float w) in
1558 List.fold_left
1559 (fun (w, h, x) ws ->
1560 match ws with
1561 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
1562 | Wsi.MaxVert -> (w, conf.cwinh, x)
1563 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
1565 (state.winw, state.winh, state.x) state.winstate
1567 conf.cwinw <- w;
1568 conf.cwinh <- h;
1569 let bb = Buffer.create 32768 in
1570 let save2 (h, dc) =
1571 save1 bb leavebirdseye x h dc
1573 if load1 save2 && Buffer.length bb > 0
1574 then
1576 let tmp = !confpath ^ ".tmp" in
1577 let oc = open_out_bin tmp in
1578 Buffer.output_buffer oc bb;
1579 close_out oc;
1580 Unix.rename tmp !confpath;
1581 with exn ->
1582 dolog "error saving configuration: %s" @@ exntos exn
1585 let gc () =
1586 let href = ref @@ Hashtbl.create 0 in
1587 let cref = ref defconf in
1588 let push (h, dc) =
1589 let f path v =
1590 if Sys.file_exists path
1591 then Some v
1592 else (dolog "removing entry for '%s'" path; None) in
1593 Hashtbl.filter_map_inplace f h;
1594 href := h;
1595 cref := dc;
1596 true
1598 ignore (load1 push);
1599 let bb = Buffer.create 32768 in
1600 let save2 (_h, dc) = save1 bb (fun _ _ -> ()) 0 !href dc in
1601 if load1 save2 && Buffer.length bb > 0
1602 then (
1604 let tmp = !confpath ^ ".tmp" in
1605 let oc = open_out_bin tmp in
1606 Buffer.output_buffer oc bb;
1607 close_out oc;
1608 Unix.rename tmp !confpath;
1609 with exn ->
1610 dolog "error saving configuration: %s" @@ exntos exn
1614 let logcurrently = function
1615 | Idle -> dolog "Idle"
1616 | Loading (l, gen) ->
1617 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1618 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1619 dolog "Tiling %d[%d,%d] page=%s cs=%s angle=%d"
1620 l.pageno col row (~> pageopaque)
1621 (CSTE.to_string colorspace) angle;
1622 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1623 angle gen conf.angle state.gen
1624 tilew tileh
1625 conf.tilew conf.tileh
1626 | Outlining _ -> dolog "outlining"
1629 let logpage l =
1630 dolog {|l %d dim=%d {
1631 WxH %dx%d
1632 vWxH %dx%d
1633 pagex,y %d,%d
1634 dispx,y %d,%d
1635 column %d
1637 l.pageno l.pagedimno
1638 l.pagew l.pageh
1639 l.pagevw l.pagevh
1640 l.pagex l.pagey
1641 l.pagedispx l.pagedispy
1642 l.pagecol