Do not hardcode xclip
[llpp.git] / config.ml
blob4886508d4bfd8d19d1d6e0d84ee7dad63f3e98fa
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 trimcachepath = string
51 and aalevel = int
52 and trimmargins = bool
53 and trimparams = (trimmargins * irect)
54 and usefontconfig = bool
55 and usedoccss = bool
56 and uri = string
57 and caption = string
58 and tilex = int
59 and tiley = int
60 and tileparams = (x * y * width * height * tilex * tiley)
61 and under =
62 | Unone
63 | Ulinkuri of string
64 | Utext of facename
65 | Uannotation of (opaque * slinkindex)
66 and slinkindex = int
67 and facename = string
68 and launchcommand = string
69 and filename = string
70 and linkno = int
71 and destname = string
72 and link =
73 | Lnotfound
74 | Lfound of int
75 and linkdir =
76 | LDfirst
77 | LDlast
78 | LDfirstvisible of (int * int * int)
79 | LDleft of int
80 | LDright of int
81 | LDdown of int
82 | LDup of int
83 and pagewithlinks =
84 | Pwlnotfound
85 | Pwl of int
86 and scrollb = int
87 and anchor = pageno * top * dtop
88 and rect = float * float * float * float * float * float * float * float
89 and infochange = | Memused | Docinfo | Pdim
92 class type uioh =
93 object
94 method display : unit
95 method key : int -> int -> uioh
96 method button : int -> bool -> int -> int -> int -> uioh
97 method multiclick : int -> int -> int -> int -> uioh
98 method motion : int -> int -> uioh
99 method pmotion : int -> int -> uioh
100 method infochanged : infochange -> unit
101 method scrollpw : (int * float * float)
102 method scrollph : (int * float * float)
103 method modehash : keyhash
104 method eformsgs : bool
105 method alwaysscrolly : bool
106 method scroll : int -> int -> uioh
107 method zoom : float -> int -> int -> unit
108 end;;
110 module type TextEnumType =
112 type t
113 val name : string
114 val names : string array
115 end;;
117 module TextEnumMake (Ten : TextEnumType) =
118 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 (
136 struct
137 type t = colorspace;;
138 let name = "colorspace";;
139 let names = [|"rgb"; "gray"|];;
140 end);;
142 module MTE = TextEnumMake (
143 struct
144 type t = mark;;
145 let name = "mark";;
146 let names = [|"page"; "block"; "line"; "word"|];;
147 end);;
149 module FMTE = TextEnumMake (
150 struct
151 type t = fitmodel;;
152 let name = "fitmodel";;
153 let names = [|"width"; "proportional"; "page"|];;
154 end);;
156 type outlinekind =
157 | Onone
158 | Oanchor of anchor
159 | Ouri of uri
160 | Olaunch of launchcommand
161 | Oremote of (filename * pageno)
162 | Oremotedest of (filename * destname)
163 | Ohistory of (filename * conf * outline list * x * anchor * filename)
164 and outline = (caption * outlinelevel * outlinekind)
165 and outlinelevel = int
168 type page =
169 { pageno : int
170 ; pagedimno : int
171 ; pagew : int
172 ; pageh : int
173 ; pagex : int
174 ; pagey : int
175 ; pagevw : int
176 ; pagevh : int
177 ; pagedispx : int
178 ; pagedispy : int
179 ; pagecol : int
183 type tile = opaque * pixmapsize * elapsed
184 and elapsed = float;;
185 type pagemapkey = pageno * gen;;
186 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
187 and row = int
188 and col = int
189 and currently =
190 | Idle
191 | Loading of (page * gen)
192 | Tiling
193 of (page * opaque * colorspace * angle * gen * col * row * width * height)
194 | Outlining of outline list
197 type mpos = int * int
198 and mstate =
199 | Msel of (mpos * mpos)
200 | Mpan of mpos
201 | Mscrolly | Mscrollx
202 | Mzoom of (buttonno * step * mpos)
203 | Mzoomrect of (mpos * mpos)
204 | Mnone
205 and buttonno = int
206 and step = int
209 type mode =
210 | Birdseye of (conf * leftx * pageno * pageno * anchor)
211 | Textentry of (textentry * onleave)
212 | View
213 | LinkNav of linktarget
214 and onleave = leavetextentrystatus -> unit
215 and leavetextentrystatus = | Cancel | Confirm
216 and helpitem = string * int * action
217 and action =
218 | Noaction
219 | Action of (uioh -> uioh)
220 and linktarget =
221 | Ltexact of (pageno * direction)
222 | Ltgendir of direction
223 | Ltnotready of (pageno * direction)
224 and direction = int (* -1, 0, 1 *)
225 and textentry = string * string * onhist option * onkey * ondone * cancelonempty
226 and onkey = string -> Keys.t -> te
227 and ondone = string -> unit
228 and histcancel = unit -> unit
229 and onhist = ((histcmd -> string) * histcancel)
230 and histcmd = HCnext | HCprev | HCfirst | HClast
231 and cancelonempty = bool
232 and te =
233 | TEstop
234 | TEdone of string
235 | TEcont of string
236 | TEswitch of textentry
239 type 'a circbuf =
240 { store : 'a array
241 ; mutable rc : int
242 ; mutable wc : int
243 ; mutable len : int
247 type state =
248 { mutable ss : Unix.file_descr
249 ; mutable wsfd : Unix.file_descr
250 ; mutable stderr : Unix.file_descr
251 ; mutable errmsgs : Buffer.t
252 ; mutable newerrmsgs : bool
253 ; mutable w : int
254 ; mutable x : x
255 ; mutable y : y
256 ; mutable anchor : anchor
257 ; mutable ranchors : (string * string * anchor * string) list
258 ; mutable maxy : int
259 ; mutable layout : page list
260 ; pagemap : (pagemapkey, opaque) Hashtbl.t
261 ; tilemap : (tilemapkey, tile) Hashtbl.t
262 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
263 ; mutable pdims : (pageno * width * height * leftx) list
264 ; mutable pagecount : int
265 ; mutable currently : currently
266 ; mutable mstate : mstate
267 ; mutable searchpattern : string
268 ; mutable rects : (pageno * rectcolor * rect) list
269 ; mutable rects1 : (pageno * rectcolor * rect) list
270 ; prects : (pageno, float array) Hashtbl.t
271 ; mutable text : string
272 ; mutable winstate : Wsi.winstate list
273 ; mutable mode : mode
274 ; mutable uioh : uioh
275 ; mutable outlines : outline array
276 ; mutable bookmarks : outline list
277 ; mutable path : string
278 ; mutable password : string
279 ; mutable nameddest : string
280 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
281 ; mutable memused : memsize
282 ; mutable gen : gen
283 ; mutable autoscroll : int option
284 ; mutable help : helpitem array
285 ; mutable docinfo : (int * string) list
286 ; hists : hists
287 ; mutable prevzoom : (float * int)
288 ; mutable progress : float
289 ; mutable mpos : mpos
290 ; mutable keystate : keystate
291 ; mutable glinks : bool
292 ; mutable prevcolumns : (columns * float) option
293 ; mutable winw : int
294 ; mutable winh : int
295 ; mutable reprf : (unit -> unit)
296 ; mutable origin : string
297 ; mutable roam : (unit -> unit)
298 ; mutable bzoom : bool
299 ; mutable lnava : (pageno * linkno) option
300 ; mutable slideshow : int
302 and hists =
303 { pat : string circbuf
304 ; pag : string circbuf
305 ; nav : anchor circbuf
306 ; sel : string circbuf
310 let emptyanchor = (0, 0.0, 0.0);;
311 let emptykeyhash = Hashtbl.create 0;;
312 let noreprf () = ();;
313 let noroam () = ();;
315 let nouioh : uioh =
316 object (self)
317 method display = ()
318 method key _ _ = self
319 method multiclick _ _ _ _ = self
320 method button _ _ _ _ _ = self
321 method motion _ _ = self
322 method pmotion _ _ = self
323 method infochanged _ = ()
324 method scrollpw = (0, nan, nan)
325 method scrollph = (0, nan, nan)
326 method modehash = emptykeyhash
327 method eformsgs = false
328 method alwaysscrolly = false
329 method scroll _ _ = self
330 method zoom _ _ _ = ()
331 end;;
333 let platform_to_string = function
334 | Punknown -> "unknown"
335 | Plinux -> "Linux"
336 | Pmacos -> "macOS"
337 | Pbsd -> "BSD"
340 let conf = { defconf with keyhashes = copykeyhashes defconf };;
342 let cbnew n v =
343 { store = Array.make n v
344 ; rc = 0
345 ; wc = 0
346 ; len = 0
350 let cbcap b = Array.length b.store;;
352 let cbput ?(update_rc=true) b v =
353 let cap = cbcap b in
354 b.store.(b.wc) <- v;
355 b.wc <- (b.wc + 1) mod cap;
356 if update_rc
357 then b.rc <- b.wc;
358 b.len <- min (b.len + 1) cap;
361 let cbput_dont_update_rc b v = cbput ~update_rc:false b v;;
363 let cbempty b = b.len = 0;;
365 let cbgetg b circular dir =
366 if cbempty b
367 then b.store.(0)
368 else
369 let rc = b.rc + dir in
370 let rc =
371 if circular
372 then (
373 if rc = -1
374 then b.len-1
375 else (
376 if rc >= b.len
377 then 0
378 else rc
381 else bound rc 0 (b.len-1)
383 b.rc <- rc;
384 b.store.(rc);
387 let cbget b = cbgetg b false;;
388 let cbgetc b = cbgetg b true;;
390 let state =
391 { ss = Unix.stdin
392 ; wsfd = Unix.stdin
393 ; stderr = Unix.stderr
394 ; errmsgs = Buffer.create 0
395 ; newerrmsgs = false
396 ; x = 0
397 ; y = 0
398 ; w = 0
399 ; anchor = emptyanchor
400 ; ranchors = []
401 ; layout = []
402 ; maxy = max_int
403 ; tilelru = Queue.create ()
404 ; pagemap = Hashtbl.create 10
405 ; tilemap = Hashtbl.create 10
406 ; pdims = []
407 ; pagecount = 0
408 ; currently = Idle
409 ; mstate = Mnone
410 ; rects = []
411 ; rects1 = []
412 ; prects = Hashtbl.create 1
413 ; text = E.s
414 ; mode = View
415 ; winstate = []
416 ; searchpattern = E.s
417 ; outlines = E.a
418 ; bookmarks = []
419 ; path = E.s
420 ; password = E.s
421 ; nameddest = E.s
422 ; geomcmds = E.s, []
423 ; hists =
424 { nav = cbnew 10 emptyanchor
425 ; pat = cbnew 10 E.s
426 ; pag = cbnew 10 E.s
427 ; sel = cbnew 10 E.s
429 ; memused = 0
430 ; gen = 0
431 ; autoscroll = None
432 ; help = E.a
433 ; docinfo = []
434 ; prevzoom = (1.0, 0)
435 ; progress = -1.0
436 ; uioh = nouioh
437 ; mpos = (-1, -1)
438 ; keystate = KSnone
439 ; glinks = false
440 ; prevcolumns = None
441 ; winw = -1
442 ; winh = -1
443 ; reprf = noreprf
444 ; origin = E.s
445 ; roam = noroam
446 ; bzoom = false
447 ; lnava = None
448 ; slideshow = 0
452 let calcips h =
453 let d = state.winh - h in
454 max conf.interpagespace ((d + 1) / 2)
457 let rowyh (c, coverA, coverB) b n =
458 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
459 then
460 let _, _, vy, (_, _, h, _) = b.(n) in
461 (vy, h)
462 else
463 let n' = n - coverA in
464 let d = n' mod c in
465 let s = n - d in
466 let e = min state.pagecount (s + c) in
467 let rec find m miny maxh = if m = e then miny, maxh else
468 let _, _, y, (_, _, h, _) = b.(m) in
469 let miny = min miny y in
470 let maxh = max maxh h in
471 find (m+1) miny maxh
472 in find s max_int 0
475 let page_of_y y =
476 let ((c, coverA, coverB) as cl), b =
477 match conf.columns with
478 | Csplit (_, b) | Csingle b -> (1, 0, 0), b
479 | Cmulti (c, b) -> c, b
481 if Array.length b = 0
482 then -1
483 else
484 let rec bsearch nmin nmax =
485 if nmin > nmax
486 then bound nmin 0 (state.pagecount-1)
487 else
488 let n = (nmax + nmin) / 2 in
489 let vy, h = rowyh cl b n in
490 let y0, y1 =
491 if conf.presentation
492 then
493 let ips = calcips h in
494 let y0 = vy - ips in
495 let y1 = vy + h + ips in
496 y0, y1
497 else (
498 if n = 0
499 then 0, vy + h + conf.interpagespace
500 else
501 let y0 = vy - conf.interpagespace in
502 y0, y0 + h + conf.interpagespace
505 if y >= y0 && y < y1
506 then (
507 if c = 1
508 then n
509 else (
510 if n > coverA
511 then
512 if n < state.pagecount - coverB
513 then ((n-coverA)/c)*c + coverA
514 else n
515 else n
518 else (
519 if y > y0
520 then bsearch (n+1) nmax
521 else bsearch nmin (n-1)
524 bsearch 0 (state.pagecount-1);
527 let calcheight () =
528 match conf.columns with
529 | Cmulti ((_, _, _) as cl, b) ->
530 if Array.length b > 0
531 then
532 let y, h = rowyh cl b (Array.length b - 1) in
533 y + h + (if conf.presentation then calcips h else 0)
534 else 0
535 | Csingle b ->
536 if Array.length b > 0
537 then
538 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
539 y + h + (if conf.presentation then calcips h else 0)
540 else 0
541 | Csplit (_, b) ->
542 if Array.length b > 0
543 then
544 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
545 y + h
546 else 0
549 let getpageywh pageno =
550 let pageno = bound pageno 0 (state.pagecount-1) in
551 match conf.columns with
552 | Csingle b ->
553 if Array.length b = 0
554 then 0, 0, 0
555 else
556 let (_, _, y, (_, w, h, _)) = b.(pageno) in
557 let y =
558 if conf.presentation
559 then y - calcips h
560 else y
562 y, w, h
563 | Cmulti (cl, b) ->
564 if Array.length b = 0
565 then 0, 0, 0
566 else
567 let y, h = rowyh cl b pageno in
568 let (_, _, _, (_, w, _, _)) = b.(pageno) in
569 let y =
570 if conf.presentation
571 then y - calcips h
572 else y
574 y, w, h
575 | Csplit (c, b) ->
576 if Array.length b = 0
577 then 0, 0, 0
578 else
579 let n = pageno*c in
580 let (_, _, y, (_, w, h, _)) = b.(n) in
581 y, w / c, h
584 let getpageyh pageno =
585 let y,_,h = getpageywh pageno in
586 y, h;
589 let getpagedim pageno =
590 let rec f ppdim l =
591 match l with
592 | (n, _, _, _) as pdim :: rest ->
593 if n >= pageno
594 then (if n = pageno then pdim else ppdim)
595 else f pdim rest
596 | [] -> ppdim
598 f (-1, -1, -1, -1) state.pdims
601 let getpdimno pageno =
602 let rec f p l =
603 let np = succ p in
604 match l with
605 | (n, _, _, _) :: rest ->
606 if n >= pageno
607 then (if n = pageno then np else p)
608 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 float dy /. float conf.interpagespace
650 (n, 0.0, dtop)
653 let fontpath = ref E.s;;
655 type historder = [ `lastvisit | `title | `path | `file ];;
657 module KeyMap =
658 Map.Make (struct type t = (int * int) let compare = compare end);;
660 let unentS s =
661 let l = String.length s in
662 let b = Buffer.create l in
663 Parser.unent b s 0 l;
664 Buffer.contents b;
667 let home =
668 try Sys.getenv "HOME"
669 with exn ->
670 dolog "cannot determine home directory location: %s" @@ exntos exn;
674 let modifier_of_string = function
675 | "alt" -> Wsi.altmask
676 | "shift" -> Wsi.shiftmask
677 | "ctrl" | "control" -> Wsi.ctrlmask
678 | "meta" -> Wsi.metamask
679 | _ -> 0
682 let keys_of_string s =
683 let key_of_string r s =
684 let elems = Str.full_split r s in
685 let f n k m =
686 let g s =
687 let m1 = modifier_of_string s in
688 if m1 = 0
689 then (Wsi.namekey s, m)
690 else (k, m lor m1)
691 in function
692 | Str.Delim s when n land 1 = 0 -> g s
693 | Str.Text s -> g s
694 | Str.Delim _ -> (k, m)
696 let rec loop n k m = function
697 | [] -> (k, m)
698 | x :: xs ->
699 let k, m = f n k m x in
700 loop (n+1) k m xs
702 loop 0 0 0 elems
704 let elems = Str.split Utils.Re.whitespace s in
705 List.map (key_of_string (Str.regexp "-")) elems
708 let config_of c attrs =
709 let maxv ?(f=int_of_string) u s = max u @@ f s in
710 let apply c k v =
712 match k with
713 | "scroll-bar-width" -> { c with scrollbw = maxv 0 v }
714 | "scroll-handle-height" -> { c with scrollh = maxv 0 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 = maxv 1 v }
719 | "horizontal-scroll-step" -> { c with hscrollstep = maxv 1 v }
720 | "auto-scroll-step" -> { c with autoscrollstep = maxv 0 v }
721 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
722 | "highlight-links" -> { c with hlinks = bool_of_string v }
723 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
724 | "vertical-margin" -> { c with interpagespace = maxv 0 v }
725 | "zoom" ->
726 let zoom = float_of_string v /. 100. in
727 let zoom = max zoom 0.0 in
728 { c with zoom = zoom }
729 | "presentation" -> { c with presentation = bool_of_string v }
730 | "rotation-angle" -> { c with angle = int_of_string v }
731 | "width" -> { c with cwinw = maxv 20 v }
732 | "height" -> { c with cwinh = maxv 20 v }
733 | "proportional-display" ->
734 let fm =
735 if bool_of_string v
736 then FitProportional
737 else FitWidth
739 { c with fitmodel = fm }
740 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
741 | "pixmap-cache-size" ->
742 { c with memlimit = maxv ~f:int_of_string_with_suffix 2 v }
743 | "tex-count" -> { c with texcount = maxv 1 v }
744 | "slice-height" -> { c with sliceheight = maxv 2 v }
745 | "thumbnail-width" -> { c with thumbw = maxv 2 v }
746 | "background-color" -> { c with bgcolor = color_of_string v }
747 | "scrollbar-color" -> { c with sbarcolor = rgba_of_string v }
748 | "scrollbar-handle-color" -> { c with sbarhndlcolor = rgba_of_string v }
749 | "tile-width" -> { c with tilew = maxv 2 v }
750 | "tile-height" -> { c with tileh = maxv 2 v }
751 | "mupdf-store-size" ->
752 { c with mustoresize = maxv ~f:int_of_string_with_suffix 1024 v }
753 | "checkers" -> { c with checkers = bool_of_string v }
754 | "aalevel" -> { c with aalevel = maxv 0 v }
755 | "trim-margins" -> { c with trimmargins = bool_of_string v }
756 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
757 | "uri-launcher" -> { c with urilauncher = unentS v }
758 | "path-launcher" -> { c with pathlauncher = unentS v }
759 | "color-space" -> { c with colorspace = CSTE.of_string v }
760 | "invert-colors" -> { c with invert = bool_of_string v }
761 | "brightness" -> { c with colorscale = float_of_string v }
762 | "columns" ->
763 let (n, _, _) as nab = multicolumns_of_string v in
764 if n < 0
765 then { c with columns = Csplit (-n, E.a) }
766 else { c with columns = Cmulti (nab, E.a) }
767 | "birds-eye-columns" -> { c with beyecolumns = Some (maxv 2 v) }
768 | "selection-command" -> { c with selcmd = unentS v }
769 | "paste-command" -> { c with pastecmd = unentS v }
770 | "synctex-command" -> { c with stcmd = unentS v }
771 | "pax-command" -> { c with paxcmd = unentS v }
772 | "askpass-command" -> { c with passcmd = unentS v }
773 | "savepath-command" -> { c with savecmd = unentS v }
774 | "update-cursor" -> { c with updatecurs = bool_of_string v }
775 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
776 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
777 | "use-pbo" -> { c with usepbo = bool_of_string v }
778 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
779 | "horizontal-scrollbar-visible" ->
780 { c with scrollb = if bool_of_string v
781 then c.scrollb lor scrollbhv
782 else c.scrollb land (lnot scrollbhv)
784 | "vertical-scrollbar-visible" ->
785 { c with scrollb = if bool_of_string v
786 then c.scrollb lor scrollbvv
787 else c.scrollb land (lnot scrollbvv)
789 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
790 | "point-and-x" ->
791 { c with pax = if bool_of_string v then Some 0.0 else None }
792 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
793 | "scroll-bar-on-the-left" -> { c with leftscroll = bool_of_string v }
794 | "title" -> { c with title = unentS v }
795 | "last-visit" -> { c with lastvisit = float_of_string v }
796 | "edit-annotations-inline" -> { c with annotinline = bool_of_string v }
797 | "coarse-presentation-positioning" ->
798 { c with coarseprespos = bool_of_string v }
799 | "use-document-css" -> { c with usedoccss = bool_of_string v }
800 | _ -> c
801 with exn ->
802 dolog "error processing attribute (`%S' = `%S'): %s" k v @@ exntos exn;
805 let rec fold c = function
806 | [] -> c
807 | (k, v) :: rest ->
808 let c = apply c k v in
809 fold c rest
811 fold { c with keyhashes = copykeyhashes c } attrs;
814 let fromstring f pos n v d =
815 try f v
816 with exn ->
817 dolog "error processing attribute (%S=%S) at %d\n%s" n v pos @@ exntos exn;
821 let bookmark_of attrs =
822 let rec fold title page rely visy = function
823 | ("title", v) :: rest -> fold v page rely visy rest
824 | ("page", v) :: rest -> fold title v rely visy rest
825 | ("rely", v) :: rest -> fold title page v visy rest
826 | ("visy", v) :: rest -> fold title page rely v rest
827 | _ :: rest -> fold title page rely visy rest
828 | [] -> title, page, rely, visy
830 fold "invalid" "0" "0" "0" attrs
833 let doc_of attrs =
834 let rec fold path key page rely pan visy origin = function
835 | ("path", v) :: rest -> fold v key page rely pan visy origin rest
836 | ("key", v) :: rest -> fold path v page rely pan visy origin rest
837 | ("page", v) :: rest -> fold path key v rely pan visy origin rest
838 | ("rely", v) :: rest -> fold path key page v pan visy origin rest
839 | ("pan", v) :: rest -> fold path key page rely v visy origin rest
840 | ("visy", v) :: rest -> fold path key page rely pan v origin rest
841 | ("origin", v) :: rest -> fold path key page rely pan visy v rest
842 | _ :: rest -> fold path key page rely pan visy origin rest
843 | [] -> path, key, page, rely, pan, visy, origin
845 fold E.s E.s "0" "0" "0" "0" E.s attrs
848 let map_of attrs =
849 let rec fold rs ls = function
850 | ("out", v) :: rest -> fold v ls rest
851 | ("in", v) :: rest -> fold rs v rest
852 | _ :: rest -> fold ls rs rest
853 | [] -> ls, rs
855 fold E.s E.s attrs
858 let findkeyhash c name =
859 try List.assoc name c.keyhashes
860 with Not_found -> error "invalid mode name `%s'" name
863 let get s =
864 let open Parser in
865 let h = Hashtbl.create 10 in
866 let dc = { defconf with angle = defconf.angle } in
867 let rec toplevel v t spos _ =
868 match t with
869 | Vdata | Vcdata | Vend -> v
870 | Vopen ("llppconfig", _, closed) ->
871 if closed
872 then v
873 else { v with f = llppconfig }
874 | Vopen _ -> parse_error "unexpected subelement at top level" s spos
875 | Vclose _ -> parse_error "unexpected close at top level" s spos
877 and llppconfig v t spos _ =
878 match t with
879 | Vdata | Vcdata -> v
880 | Vend -> parse_error "unexpected end of input in llppconfig" s spos
881 | Vopen ("defaults", attrs, closed) ->
882 let c = config_of dc attrs in
883 setconf dc c;
884 if closed
885 then v
886 else { v with f = defaults }
888 | Vopen ("ui-font", attrs, closed) ->
889 let rec getsize size = function
890 | [] -> size
891 | ("size", v) :: rest ->
892 let size =
893 fromstring int_of_string spos "size" v fstate.fontsize in
894 getsize size rest
895 | l -> getsize size l
897 fstate.fontsize <- getsize fstate.fontsize attrs;
898 if closed
899 then v
900 else { v with f = uifont (Buffer.create 10) }
902 | Vopen ("doc", attrs, closed) ->
903 let pathent, key, spage, srely, span, svisy, origin = doc_of attrs in
904 let path = unentS pathent
905 and origin = unentS origin
906 and pageno = fromstring int_of_string spos "page" spage 0
907 and rely = fromstring float_of_string spos "rely" srely 0.0
908 and pan = fromstring int_of_string spos "pan" span 0
909 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
910 let c = config_of dc attrs in
911 c.key <- key;
912 let anchor = (pageno, rely, visy) in
913 if closed
914 then (Hashtbl.add h path (c, [], pan, anchor, origin); v)
915 else { v with f = doc path origin pan anchor c [] }
917 | Vopen _ -> parse_error "unexpected subelement in llppconfig" s spos
918 | Vclose "llppconfig" -> { v with f = toplevel }
919 | Vclose _ -> parse_error "unexpected close in llppconfig" s spos
921 and defaults v t spos _ =
922 match t with
923 | Vdata | Vcdata -> v
924 | Vend -> parse_error "unexpected end of input in defaults" s spos
925 | Vopen ("keymap", attrs, closed) ->
926 let modename =
927 try List.assoc "mode" attrs
928 with Not_found -> "global" in
929 if closed
930 then v
931 else
932 let ret keymap =
933 let h = findkeyhash dc modename in
934 KeyMap.iter (Hashtbl.replace h) keymap;
935 defaults
937 { v with f = pkeymap ret KeyMap.empty }
939 | Vopen (_, _, _) ->
940 parse_error "unexpected subelement in defaults" s spos
942 | Vclose "defaults" ->
943 { v with f = llppconfig }
945 | Vclose _ -> parse_error "unexpected close in defaults" s spos
947 and uifont b v t spos epos =
948 match t with
949 | Vdata | Vcdata ->
950 Buffer.add_substring b s spos (epos - spos);
952 | Vopen (_, _, _) -> parse_error "unexpected subelement in ui-font" s spos
953 | Vclose "ui-font" ->
954 if emptystr !fontpath
955 then fontpath := Buffer.contents b;
956 { v with f = llppconfig }
957 | Vclose _ -> parse_error "unexpected close in ui-font" s spos
958 | Vend -> parse_error "unexpected end of input in ui-font" s spos
960 and doc path origin pan anchor c bookmarks v t spos _ =
961 match t with
962 | Vdata | Vcdata -> v
963 | Vend -> parse_error "unexpected end of input in doc" s spos
964 | Vopen ("bookmarks", _, closed) ->
965 if closed
966 then v
967 else { v with f = pbookmarks path origin pan anchor c bookmarks }
969 | Vopen ("keymap", attrs, closed) ->
970 let modename =
971 try List.assoc "mode" attrs
972 with Not_found -> "global"
974 if closed
975 then v
976 else
977 let ret keymap =
978 let h = findkeyhash c modename in
979 KeyMap.iter (Hashtbl.replace h) keymap;
980 doc path origin pan anchor c bookmarks
982 { v with f = pkeymap ret KeyMap.empty }
984 | Vopen ("css", [], false) ->
985 { v with f = pcss path origin pan anchor c bookmarks }
987 | Vopen (_, _, _) ->
988 parse_error "unexpected subelement in doc" s spos
990 | Vclose "doc" ->
991 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor, origin);
992 { v with f = llppconfig }
994 | Vclose _ -> parse_error "unexpected close in doc" s spos
996 and pcss path origin pan anchor c bookmarks v t spos epos =
997 match t with
998 | Vdata | Vcdata ->
999 let b = Buffer.create 10 in
1000 Buffer.add_substring b s spos (epos - spos);
1001 { v with f = pcss path origin pan anchor
1002 { c with css = Buffer.contents b }
1003 bookmarks }
1004 | Vend -> parse_error "unexpected end of input in css" s spos
1005 | Vopen _ -> parse_error "unexpected subelement in css" s spos
1006 | Vclose "css" -> { v with f = doc path origin pan anchor c bookmarks }
1007 | Vclose _ -> parse_error "unexpected close in css" s spos
1009 and pkeymap ret keymap v t spos _ =
1010 match t with
1011 | Vdata | Vcdata -> v
1012 | Vend -> parse_error "unexpected end of input in keymap" s spos
1013 | Vopen ("map", attrs, closed) ->
1014 let r, l = map_of attrs in
1015 let kss = fromstring keys_of_string spos "in" r [] in
1016 let lss = fromstring keys_of_string spos "out" l [] in
1017 let keymap =
1018 match kss with
1019 | [] -> keymap
1020 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
1021 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
1023 if closed
1024 then { v with f = pkeymap ret keymap }
1025 else
1026 let f () = v in
1027 { v with f = skip "map" f }
1029 | Vopen _ -> parse_error "unexpected subelement in keymap" s spos
1030 | Vclose "keymap" ->
1031 { v with f = ret keymap }
1032 | Vclose _ -> parse_error "unexpected close in keymap" s spos
1034 and pbookmarks path origin pan anchor c bookmarks v t spos _ =
1035 match t with
1036 | Vdata | Vcdata -> v
1037 | Vend -> parse_error "unexpected end of input in bookmarks" s spos
1038 | Vopen ("item", attrs, closed) ->
1039 let titleent, spage, srely, svisy = bookmark_of attrs in
1040 let page = fromstring int_of_string spos "page" spage 0
1041 and rely = fromstring float_of_string spos "rely" srely 0.0
1042 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1043 let bookmarks =
1044 (unentS titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
1046 if closed
1047 then { v with f = pbookmarks path origin pan anchor c bookmarks }
1048 else
1049 let f () = v in
1050 { v with f = skip "item" f }
1052 | Vopen _ -> parse_error "unexpected subelement in bookmarks" s spos
1053 | Vclose "bookmarks" ->
1054 { v with f = doc path origin pan anchor c bookmarks }
1055 | Vclose _ -> parse_error "unexpected close in bookmarks" s spos
1057 and skip tag f v t spos _ =
1058 match t with
1059 | Vdata | Vcdata -> v
1060 | Vend ->
1061 parse_error ("unexpected end of input in skipped " ^ tag) s spos
1062 | Vopen (tag', _, closed) ->
1063 if closed
1064 then v
1065 else
1066 let f' () = { v with f = skip tag f } in
1067 { v with f = skip tag' f' }
1068 | Vclose ctag ->
1069 if tag = ctag
1070 then f ()
1071 else parse_error ("unexpected close in skipped " ^ tag) s spos
1073 parse { f = toplevel; accu = () } s;
1074 h, dc;
1077 let do_load f contents =
1078 try f contents
1079 with
1080 | Parser.Parse_error (msg, s, pos) ->
1081 let subs = Parser.subs s pos in
1082 Utils.error "parse error: %s: at %d [..%S..]" msg pos subs
1084 | exn -> Utils.error "parse error: %s" @@ exntos exn
1087 let defconfpath =
1088 let dir =
1089 let xdgconfdir = Utils.getenvdef "XDG_CONFIG_HOME" E.s in
1090 if emptystr xdgconfdir
1091 then
1093 let dir = Filename.concat home ".config" in
1094 if Sys.is_directory dir then dir else home
1095 with _ -> home
1096 else xdgconfdir
1098 Filename.concat dir "llpp.conf"
1101 let confpath = ref defconfpath;;
1103 let load2 f default =
1104 match filecontents !confpath with
1105 | contents -> f @@ do_load get contents
1106 | exception Unix.Unix_error (Unix.ENOENT, "open", _) ->
1107 f (Hashtbl.create 0, defconf)
1108 | exception exn ->
1109 dolog "error loading configuration from `%S': %s" !confpath @@ exntos exn;
1110 default
1113 let load1 f = load2 f false;;
1115 let load openlast =
1116 let f (h, dc) =
1117 if openlast
1118 then (
1119 let path, _ =
1120 Hashtbl.fold
1121 (fun path (conf, _, _, _, _) ((_, besttime) as best) ->
1122 if conf.lastvisit > besttime
1123 then (path, conf.lastvisit)
1124 else best)
1126 (state.path, -.infinity)
1128 state.path <- path;
1130 let pc, pb, px, pa, po =
1131 let def = dc, [], 0, emptyanchor, state.origin in
1132 if emptystr state.path
1133 then def
1134 else
1135 let absname = abspath state.path in
1136 match Hashtbl.find h absname with
1137 | (c,b,x,a,_) -> (c,b,x,a,state.origin)
1138 | exception Not_found ->
1139 let exception E of (conf * outline list * int * anchor * string) in
1140 let key = try Digest.file absname |> Digest.to_hex with _ -> E.s in
1141 match (
1142 if nonemptystr key
1143 then
1144 Hashtbl.iter (fun p ((c, _, _, _, _) as v) ->
1145 if c.key = key
1146 then (
1147 dolog "will use %s's settings due to matching keys" p;
1148 raise (E v)
1152 with
1153 | _ -> def
1154 | exception E v -> v
1156 setconf defconf dc;
1157 setconf conf pc;
1158 state.bookmarks <- pb;
1159 state.x <- px;
1160 state.origin <- po;
1161 state.anchor <- pa;
1162 cbput state.hists.nav pa;
1163 true
1165 load1 f
1168 let gethist () =
1169 let f (h, _) =
1170 Hashtbl.fold (fun path (pc, pb, px, pa, po) accu ->
1171 (path, pc, pb, px, pa, po) :: accu)
1172 h [];
1174 load2 f []
1177 let add_attrs bb always dc c time =
1178 let o' fmt s =
1179 Buffer.add_string bb "\n ";
1180 Printf.bprintf bb fmt s
1182 let o c fmt s = if c then o' fmt s else ignore in
1183 let ob s a b = o (always || a != b) "%s='%b'" s a
1184 and op s a b = o (always || a <> b) "%s='%b'" s (a != None)
1185 and oi s a b = o (always || a != b) "%s='%d'" s a
1186 and oI s a b = o (always || a != b) "%s='%s'" s (string_with_suffix_of_int a)
1187 and oz s a b = o (always || a <> b) "%s='%g'" s (a*.100.)
1188 and oF s a b = o (always || a <> b) "%s='%f'" s a
1189 and oL s a b = o (always || a <> b) "%s='%Ld'" s a
1190 and oc s a b = o (always || a <> b) "%s='%s'" s (color_to_string a)
1191 and oA s a b = o (always || a <> b) "%s='%s'" s (rgba_to_string a)
1192 and oC s a b = o (always || a <> b) "%s='%s'" s (CSTE.to_string a)
1193 and oR s a b = o (always || a <> b) "%s='%s'" s (irect_to_string a)
1194 and oFm s a b = o (always || a <> b) "%s='%s'" s (FMTE.to_string a)
1195 and oSv s a b m =
1196 o (always || a land m <> b land m) "%s='%b'" s (a land m != 0)
1197 and oPm s a b = o (always || a <> b) "%s='%s'" s (MTE.to_string a)
1198 and os s a b =
1199 o (always || a <> b) "%s='%s'" s @@ Parser.enent a 0 (String.length a)
1200 and oco s a b =
1201 if always || a <> b
1202 then
1203 match a with
1204 | Cmulti ((n, a, b), _) when n > 1 -> o' "%s='%d,%d,%d'" s n a b
1205 | Csplit (n, _) when n > 1 -> o' "%s='%d'" s ~-n
1206 | Cmulti _ | Csplit _ | Csingle _ -> ()
1207 and obeco s a b =
1208 if always || a <> b
1209 then
1210 match a with
1211 | Some c when c > 1 -> o' "%s='%d'" s c
1212 | _ -> ()
1214 oi "width" c.cwinw dc.cwinw;
1215 oi "height" c.cwinh dc.cwinh;
1216 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
1217 oi "scroll-handle-height" c.scrollh dc.scrollh;
1218 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
1219 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
1220 ob "case-insensitive-search" c.icase dc.icase;
1221 ob "preload" c.preload dc.preload;
1222 oi "page-bias" c.pagebias dc.pagebias;
1223 oi "scroll-step" c.scrollstep dc.scrollstep;
1224 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
1225 ob "max-height-fit" c.maxhfit dc.maxhfit;
1226 ob "highlight-links" c.hlinks dc.hlinks;
1227 ob "under-cursor-info" c.underinfo dc.underinfo;
1228 oi "vertical-margin" c.interpagespace dc.interpagespace;
1229 oz "zoom" c.zoom dc.zoom;
1230 ob "presentation" c.presentation dc.presentation;
1231 oi "rotation-angle" c.angle dc.angle;
1232 oFm "fit-model" c.fitmodel dc.fitmodel;
1233 oI "pixmap-cache-size" c.memlimit dc.memlimit;
1234 oi "tex-count" c.texcount dc.texcount;
1235 oi "slice-height" c.sliceheight dc.sliceheight;
1236 oi "thumbnail-width" c.thumbw dc.thumbw;
1237 oc "background-color" c.bgcolor dc.bgcolor;
1238 oA "scrollbar-color" c.sbarcolor dc.sbarcolor;
1239 oA "scrollbar-handle-color" c.sbarhndlcolor dc.sbarhndlcolor;
1240 oi "tile-width" c.tilew dc.tilew;
1241 oi "tile-height" c.tileh dc.tileh;
1242 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
1243 ob "checkers" c.checkers dc.checkers;
1244 oi "aalevel" c.aalevel dc.aalevel;
1245 ob "trim-margins" c.trimmargins dc.trimmargins;
1246 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
1247 os "uri-launcher" c.urilauncher dc.urilauncher;
1248 os "path-launcher" c.pathlauncher dc.pathlauncher;
1249 oC "color-space" c.colorspace dc.colorspace;
1250 ob "invert-colors" c.invert dc.invert;
1251 oF "brightness" c.colorscale dc.colorscale;
1252 oco "columns" c.columns dc.columns;
1253 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
1254 os "selection-command" c.selcmd dc.selcmd;
1255 os "paste-command" c.pastecmd dc.pastecmd;
1256 os "synctex-command" c.stcmd dc.stcmd;
1257 os "pax-command" c.paxcmd dc.paxcmd;
1258 os "askpass-command" c.passcmd dc.passcmd;
1259 os "savepath-command" c.savecmd dc.savecmd;
1260 ob "update-cursor" c.updatecurs dc.updatecurs;
1261 oi "hint-font-size" c.hfsize dc.hfsize;
1262 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
1263 oF "page-scroll-scale" c.pgscale dc.pgscale;
1264 ob "use-pbo" c.usepbo dc.usepbo;
1265 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
1266 ob "remote-in-a-new-instance" c.riani dc.riani;
1267 op "point-and-x" c.pax dc.pax;
1268 oPm "point-and-x-mark" c.paxmark dc.paxmark;
1269 ob "scroll-bar-on-the-left" c.leftscroll dc.leftscroll;
1270 if not always
1271 then os "title" c.title dc.title;
1272 oL "last-visit" (Int64.of_float time) 0L;
1273 ob "edit-annotations-inline" c.annotinline dc.annotinline;
1274 ob "coarse-presentation-positioning" c.coarseprespos dc.coarseprespos;
1275 ob "use-document-css" c.usedoccss dc.usedoccss;
1278 let keymapsbuf always dc c =
1279 let open Buffer in
1280 let bb = create 16 in
1281 let rec loop = function
1282 | [] -> ()
1283 | (modename, h) :: rest ->
1284 let dh = findkeyhash dc modename in
1285 if always || h <> dh
1286 then (
1287 if Hashtbl.length h > 0
1288 then (
1289 if length bb > 0 then add_char bb '\n';
1290 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
1291 Hashtbl.iter (fun i o ->
1292 if always || match Hashtbl.find dh i
1293 with | dO -> dO <> o | exception Not_found -> false
1294 then
1295 let addkm (k, m) =
1296 if Wsi.withctrl m then add_string bb "ctrl-";
1297 if Wsi.withalt m then add_string bb "alt-";
1298 if Wsi.withshift m then add_string bb "shift-";
1299 if Wsi.withmeta m then add_string bb "meta-";
1300 add_string bb (Wsi.keyname k);
1302 let addkms l =
1303 let rec loop = function
1304 | [] -> ()
1305 | km :: [] -> addkm km
1306 | km :: rest -> addkm km; add_char bb ' '; loop rest
1308 loop l
1310 add_string bb "<map in='";
1311 addkm i;
1312 match o with
1313 | KMinsrt km ->
1314 add_string bb "' out='"; addkm km; add_string bb "'/>\n"
1316 | KMinsrl kms ->
1317 add_string bb "' out='"; addkms kms; add_string bb "'/>\n"
1319 | KMmulti (ins, kms) ->
1320 add_char bb ' '; addkms ins; add_string bb "' out='";
1321 addkms kms; add_string bb "'/>\n"
1322 ) h;
1323 add_string bb "</keymap>";
1326 loop rest
1328 loop c.keyhashes;
1332 let keystostrlist c =
1333 let rec loop accu = function
1334 | [] -> accu
1335 | (modename, h) :: rest ->
1336 let accu =
1337 if Hashtbl.length h > 0
1338 then (
1339 let accu = Printf.sprintf "\xc2\xb7Keys for %s" modename :: accu in
1340 Hashtbl.fold (fun i o a ->
1341 let bb = Buffer.create 10 in
1342 let addkm (k, m) =
1343 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
1344 if Wsi.withalt m then Buffer.add_string bb "alt-";
1345 if Wsi.withshift m then Buffer.add_string bb "shift-";
1346 if Wsi.withmeta m then Buffer.add_string bb "meta-";
1347 Buffer.add_string bb (Wsi.keyname k);
1349 let addkms l =
1350 let rec loop = function
1351 | [] -> ()
1352 | km :: [] -> addkm km
1353 | km :: rest ->
1354 addkm km; Buffer.add_char bb ' ';
1355 loop rest
1357 loop l
1359 addkm i;
1360 Buffer.add_char bb '\t';
1361 begin match o with
1362 | KMinsrt km -> addkm km
1363 | KMinsrl kms -> addkms kms
1364 | KMmulti (ins, kms) ->
1365 Buffer.add_char bb ' ';
1366 addkms ins;
1367 Buffer.add_string bb "\t";
1368 addkms kms
1369 end;
1370 Buffer.contents bb :: a
1371 ) h accu
1373 else accu
1375 loop accu rest
1377 loop [] c.keyhashes
1380 let save1 bb leavebirdseye x h dc =
1381 let uifontsize = fstate.fontsize in
1382 let dc = if conf.bedefault then conf else dc in
1383 Buffer.add_string bb "<llppconfig>\n";
1385 if nonemptystr !fontpath
1386 then Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
1387 uifontsize
1388 !fontpath
1389 else
1390 if uifontsize <> 14
1391 then Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
1394 Buffer.add_string bb "<defaults";
1395 add_attrs bb true dc dc nan;
1396 let kb = keymapsbuf true dc dc in
1397 if Buffer.length kb > 0
1398 then (
1399 Buffer.add_string bb ">\n";
1400 Buffer.add_buffer bb kb;
1401 Buffer.add_string bb "\n</defaults>\n";
1403 else Buffer.add_string bb "/>\n";
1405 let adddoc path pan anchor c bookmarks time origin =
1406 if not (bookmarks == [] && c = dc && anchor = emptyanchor)
1407 then (
1408 Printf.bprintf bb "<doc path='%s'"
1409 (Parser.enent path 0 (String.length path));
1411 if nonemptystr c.key
1412 then Printf.bprintf bb "\n key='%s'" c.key;
1414 if nonemptystr origin
1415 then Printf.bprintf bb "\n origin='%s'"
1416 (Parser.enent origin 0 (String.length origin));
1418 if anchor <> emptyanchor
1419 then (
1420 let n, rely, visy = anchor in
1421 Printf.bprintf bb "\n page='%d'" n;
1423 if rely > 1e-6
1424 then Printf.bprintf bb " rely='%f'" rely;
1426 if abs_float visy > 1e-6
1427 then Printf.bprintf bb " visy='%f'" visy;
1430 if pan != 0
1431 then Printf.bprintf bb " pan='%d'" pan;
1433 add_attrs bb false dc c time;
1434 if nonemptystr c.css
1435 then Printf.bprintf bb ">\n <css><![CDATA[%s]]></css>" c.css;
1436 let kb = keymapsbuf false dc c in
1438 begin match bookmarks with
1439 | [] ->
1440 if Buffer.length kb > 0
1441 then (
1442 Buffer.add_string bb ">\n";
1443 Buffer.add_buffer bb kb;
1444 Buffer.add_string bb "\n</doc>\n";
1446 else (
1447 if nonemptystr c.css
1448 then Buffer.add_string bb "\n</doc>\n"
1449 else Buffer.add_string bb "/>\n"
1451 | _ ->
1452 Buffer.add_string bb ">\n<bookmarks>\n";
1453 List.iter (fun (title, _, kind) ->
1454 begin match kind with
1455 | Oanchor (page, rely, visy) ->
1456 Printf.bprintf bb
1457 "<item title='%s' page='%d'"
1458 (Parser.enent title 0 (String.length title))
1459 page;
1460 if rely > 1e-6
1461 then Printf.bprintf bb " rely='%f'" rely;
1462 if abs_float visy > 1e-6
1463 then Printf.bprintf bb " visy='%f'" visy;
1465 | Ohistory _ | Onone | Ouri _ | Oremote _
1466 | Oremotedest _ | Olaunch _ -> error "unexpected link in bookmarks"
1467 end;
1468 Buffer.add_string bb "/>\n";
1469 ) bookmarks;
1470 Buffer.add_string bb "</bookmarks>";
1471 if Buffer.length kb > 0
1472 then (
1473 Buffer.add_string bb "\n";
1474 Buffer.add_buffer bb kb;
1476 Buffer.add_string bb "\n</doc>\n";
1477 end;
1481 let pan, conf =
1482 match state.mode with
1483 | Birdseye (c, pan, _, _, _) ->
1484 let beyecolumns =
1485 match conf.columns with
1486 | Cmulti ((c, _, _), _) -> Some c
1487 | Csingle _ -> None
1488 | Csplit _ -> None
1489 and columns =
1490 match c.columns with
1491 | Cmulti (c, _) -> Cmulti (c, E.a)
1492 | Csingle _ -> Csingle E.a
1493 | Csplit _ -> failwith "quit from bird's eye while split"
1495 pan, { c with beyecolumns = beyecolumns; columns = columns }
1496 | Textentry _
1497 | View
1498 | LinkNav _ -> x, conf
1500 let docpath = if nonemptystr state.path then abspath state.path else E.s in
1501 if nonemptystr docpath
1502 then (
1503 adddoc docpath pan (getanchor ())
1505 let autoscrollstep =
1506 match state.autoscroll with
1507 | Some step -> step
1508 | None -> conf.autoscrollstep
1510 begin match state.mode with
1511 | Birdseye beye -> leavebirdseye beye true
1512 | Textentry _
1513 | View
1514 | LinkNav _ -> ()
1515 end;
1516 let key = try Digest.file docpath |> Digest.to_hex
1517 with _ -> E.s in
1518 { conf with autoscrollstep; key }
1520 state.bookmarks
1521 (now ())
1522 state.origin
1524 Hashtbl.iter (fun path (c, bookmarks, x, anchor, origin) ->
1525 if docpath <> abspath path
1526 then adddoc path x anchor c bookmarks c.lastvisit origin
1527 ) h;
1528 Buffer.add_string bb "</llppconfig>\n";
1529 true;
1532 let save leavebirdseye =
1533 let relx = float state.x /. float state.winw in
1534 let w, h, x =
1535 let cx w = truncate (relx *. float w) in
1536 List.fold_left
1537 (fun (w, h, x) ws ->
1538 match ws with
1539 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
1540 | Wsi.MaxVert -> (w, conf.cwinh, x)
1541 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
1543 (state.winw, state.winh, state.x) state.winstate
1545 conf.cwinw <- w;
1546 conf.cwinh <- h;
1547 let bb = Buffer.create 32768 in
1548 let save2 (h, dc) = save1 bb leavebirdseye x h dc in
1549 if load1 save2 && Buffer.length bb > 0
1550 then
1552 let tmp = !confpath ^ ".tmp" in
1553 let oc = open_out_bin tmp in
1554 Buffer.output_buffer oc bb;
1555 close_out oc;
1556 Unix.rename tmp !confpath;
1557 with exn -> dolog "error saving configuration: %s" @@ exntos exn
1560 let gc () =
1561 let href = ref @@ Hashtbl.create 0 in
1562 let cref = ref defconf in
1563 let push (h, dc) =
1564 let f path v =
1565 if Sys.file_exists path
1566 then Some v
1567 else (dolog "removing entry for '%s'" path; None) in
1568 Hashtbl.filter_map_inplace f h;
1569 href := h;
1570 cref := dc;
1571 true
1573 ignore (load1 push);
1574 let bb = Buffer.create 32768 in
1575 let save2 (_h, dc) = save1 bb (fun _ _ -> ()) 0 !href dc in
1576 if load1 save2 && Buffer.length bb > 0
1577 then (
1579 let tmp = !confpath ^ ".tmp" in
1580 let oc = open_out_bin tmp in
1581 Buffer.output_buffer oc bb;
1582 close_out oc;
1583 Unix.rename tmp !confpath;
1584 with exn -> dolog "error saving configuration: %s" @@ exntos exn
1588 let logcurrently = function
1589 | Idle -> dolog "Idle"
1590 | Loading (l, gen) ->
1591 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1592 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1593 dolog "Tiling %d[%d,%d] page=%s cs=%s angle=%d"
1594 l.pageno col row (~> pageopaque)
1595 (CSTE.to_string colorspace) angle;
1596 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1597 angle gen conf.angle state.gen
1598 tilew tileh
1599 conf.tilew conf.tileh
1600 | Outlining _ -> dolog "outlining"
1603 let logpage l =
1604 dolog {|l %d dim=%d {
1605 WxH %dx%d
1606 vWxH %dx%d
1607 pagex,y %d,%d
1608 dispx,y %d,%d
1609 column %d
1611 l.pageno l.pagedimno
1612 l.pagew l.pageh
1613 l.pagevw l.pagevh
1614 l.pagex l.pagey
1615 l.pagedispx l.pagedispy
1616 l.pagecol