Whoops
[llpp.git] / config.ml
blob3641a26ca4405faf14d167fe928b769debee53ed
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 initparams =
45 (angle * fitmodel * trimparams * texcount * sliceheight * memsize
46 * colorspace * fontpath * trimcachepath * haspbo)
47 and angle = int
48 and opaque = Opaque.t
49 and rectcolor = rgba
50 and pixmapsize = int
51 and gen = int
52 and top = float
53 and dtop = float
54 and fontpath = string
55 and trimcachepath = string
56 and aalevel = int
57 and trimmargins = bool
58 and trimparams = (trimmargins * irect)
59 and haspbo = bool
60 and usefontconfig = bool
61 and usedoccss = bool
62 and uri = string
63 and caption = string
64 and tilex = int
65 and tiley = int
66 and tileparams = (x * y * width * height * tilex * tiley)
67 and under =
68 | Unone
69 | Ulinkuri of string
70 | Utext of facename
71 | Uannotation of (opaque * slinkindex)
72 and slinkindex = int
73 and facename = string
74 and launchcommand = string
75 and filename = string
76 and linkno = int
77 and destname = string
78 and link =
79 | Lnotfound
80 | Lfound of int
81 and linkdir =
82 | LDfirst
83 | LDlast
84 | LDfirstvisible of (int * int * int)
85 | LDleft of int
86 | LDright of int
87 | LDdown of int
88 | LDup of int
89 and pagewithlinks =
90 | Pwlnotfound
91 | Pwl of int
92 and scrollb = int
93 and anchor = pageno * top * dtop
94 and rect = float * float * float * float * float * float * float * float
95 and infochange = | Memused | Docinfo | Pdim
98 class type uioh =
99 object
100 method display : unit
101 method key : int -> int -> uioh
102 method button : int -> bool -> int -> int -> int -> uioh
103 method multiclick : int -> int -> int -> int -> uioh
104 method motion : int -> int -> uioh
105 method pmotion : int -> int -> uioh
106 method infochanged : infochange -> unit
107 method scrollpw : (int * float * float)
108 method scrollph : (int * float * float)
109 method modehash : keyhash
110 method eformsgs : bool
111 method alwaysscrolly : bool
112 method scroll : int -> int -> uioh
113 method zoom : float -> int -> int -> unit
114 end;;
116 module type TextEnumType = sig
117 type t
118 val name : string
119 val names : string array
120 end;;
122 module TextEnumMake (Ten : TextEnumType) = struct
123 let names = Ten.names;;
124 let to_int (t : Ten.t) = Obj.magic t;;
125 let to_string t = names.(to_int t);;
126 let of_int n : Ten.t = Obj.magic n;;
127 let of_string s =
128 let rec find i =
129 if i = Array.length names
130 then error "invalid %s: %s" Ten.name s
131 else (
132 if Ten.names.(i) = s
133 then of_int i
134 else find (i+1)
136 in find 0;;
137 end;;
139 module CSTE = TextEnumMake (struct
140 type t = colorspace;;
141 let name = "colorspace";;
142 let names = [|"rgb"; "bgr"; "gray"|];;
143 end);;
145 module MTE = TextEnumMake (struct
146 type t = mark;;
147 let name = "mark";;
148 let names = [|"page"; "block"; "line"; "word"|];;
149 end);;
151 module FMTE = TextEnumMake (struct
152 type t = fitmodel;;
153 let name = "fitmodel";;
154 let names = [|"width"; "proportional"; "page"|];;
155 end);;
157 type outlinekind =
158 | Onone
159 | Oanchor of anchor
160 | Ouri of uri
161 | Olaunch of launchcommand
162 | Oremote of (filename * pageno)
163 | Oremotedest of (filename * destname)
164 | Ohistory of (filename * conf * outline list * x * anchor * filename)
165 and outline = (caption * outlinelevel * outlinekind)
166 and outlinelevel = int
169 type page =
170 { pageno : int
171 ; pagedimno : int
172 ; pagew : int
173 ; pageh : int
174 ; pagex : int
175 ; pagey : int
176 ; pagevw : int
177 ; pagevh : int
178 ; pagedispx : int
179 ; pagedispy : int
180 ; pagecol : int
184 type tile = opaque * pixmapsize * elapsed
185 and elapsed = float;;
186 type pagemapkey = pageno * gen;;
187 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
188 and row = int
189 and col = int
190 and currently =
191 | Idle
192 | Loading of (page * gen)
193 | Tiling of (
194 page * opaque * colorspace * angle * gen * col * row * width * height
196 | Outlining of outline list
199 type mpos = int * int
200 and mstate =
201 | Msel of (mpos * mpos)
202 | Mpan of mpos
203 | Mscrolly | Mscrollx
204 | Mzoom of (buttonno * step * mpos)
205 | Mzoomrect of (mpos * mpos)
206 | Mnone
207 and buttonno = int
208 and step = int
211 type mode =
212 | Birdseye of (conf * leftx * pageno * pageno * anchor)
213 | Textentry of (textentry * onleave)
214 | View
215 | LinkNav of linktarget
216 and onleave = leavetextentrystatus -> unit
217 and leavetextentrystatus = | Cancel | Confirm
218 and helpitem = string * int * action
219 and action =
220 | Noaction
221 | Action of (uioh -> uioh)
222 and linktarget =
223 | Ltexact of (pageno * direction)
224 | Ltgendir of direction
225 | Ltnotready of (pageno * direction)
226 and direction = int (* -1, 0, 1 *)
227 and textentry = string * string * onhist option * onkey * ondone * cancelonempty
228 and onkey = string -> Keys.t -> te
229 and ondone = string -> unit
230 and histcancel = unit -> unit
231 and onhist = ((histcmd -> string) * histcancel)
232 and histcmd = HCnext | HCprev | HCfirst | HClast
233 and cancelonempty = bool
234 and te =
235 | TEstop
236 | TEdone of string
237 | TEcont of string
238 | TEswitch of textentry
241 type 'a circbuf =
242 { store : 'a array
243 ; mutable rc : int
244 ; mutable wc : int
245 ; mutable len : int
249 type state =
250 { mutable ss : Unix.file_descr
251 ; mutable wsfd : Unix.file_descr
252 ; mutable stderr : Unix.file_descr
253 ; mutable errmsgs : Buffer.t
254 ; mutable newerrmsgs : bool
255 ; mutable w : int
256 ; mutable x : x
257 ; mutable y : y
258 ; mutable anchor : anchor
259 ; mutable ranchors : (string * string * anchor * string) list
260 ; mutable maxy : int
261 ; mutable layout : page list
262 ; pagemap : (pagemapkey, opaque) Hashtbl.t
263 ; tilemap : (tilemapkey, tile) Hashtbl.t
264 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
265 ; mutable pdims : (pageno * width * height * leftx) list
266 ; mutable pagecount : int
267 ; mutable currently : currently
268 ; mutable mstate : mstate
269 ; mutable searchpattern : string
270 ; mutable rects : (pageno * rectcolor * rect) list
271 ; mutable rects1 : (pageno * rectcolor * rect) list
272 ; prects : (pageno, float array) Hashtbl.t
273 ; mutable text : string
274 ; mutable winstate : Wsi.winstate list
275 ; mutable mode : mode
276 ; mutable uioh : uioh
277 ; mutable outlines : outline array
278 ; mutable bookmarks : outline list
279 ; mutable path : string
280 ; mutable password : string
281 ; mutable nameddest : string
282 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
283 ; mutable memused : memsize
284 ; mutable gen : gen
285 ; mutable autoscroll : int option
286 ; mutable help : helpitem array
287 ; mutable docinfo : (int * string) list
288 ; hists : hists
289 ; mutable prevzoom : (float * int)
290 ; mutable progress : float
291 ; mutable mpos : mpos
292 ; mutable keystate : keystate
293 ; mutable glinks : bool
294 ; mutable prevcolumns : (columns * float) option
295 ; mutable winw : int
296 ; mutable winh : int
297 ; mutable reprf : (unit -> unit)
298 ; mutable origin : string
299 ; mutable roam : (unit -> unit)
300 ; mutable bzoom : bool
301 ; mutable lnava : (pageno * linkno) option
302 ; mutable slideshow : int
304 and hists =
305 { pat : string circbuf
306 ; pag : string circbuf
307 ; nav : anchor circbuf
308 ; sel : string circbuf
312 let emptyanchor = (0, 0.0, 0.0);;
313 let emptykeyhash = Hashtbl.create 0;;
314 let noreprf () = ();;
315 let noroam () = ();;
317 let nouioh : uioh =
318 object (self)
319 method display = ()
320 method key _ _ = self
321 method multiclick _ _ _ _ = self
322 method button _ _ _ _ _ = self
323 method motion _ _ = self
324 method pmotion _ _ = self
325 method infochanged _ = ()
326 method scrollpw = (0, nan, nan)
327 method scrollph = (0, nan, nan)
328 method modehash = emptykeyhash
329 method eformsgs = false
330 method alwaysscrolly = false
331 method scroll _ _ = self
332 method zoom _ _ _ = ()
333 end;;
335 let platform_to_string = function
336 | Punknown -> "unknown"
337 | Plinux -> "Linux"
338 | Pmacos -> "macOS"
339 | Pbsd -> "BSD"
342 let conf = { defconf with keyhashes = copykeyhashes defconf };;
344 let cbnew n v =
345 { store = Array.make n v
346 ; rc = 0
347 ; wc = 0
348 ; len = 0
352 let cbcap b = Array.length b.store;;
354 let cbput ?(update_rc=true) b v =
355 let cap = cbcap b in
356 b.store.(b.wc) <- v;
357 b.wc <- (b.wc + 1) mod cap;
358 if update_rc
359 then b.rc <- b.wc;
360 b.len <- min (b.len + 1) cap;
363 let cbput_dont_update_rc b v = cbput ~update_rc:false b v;;
365 let cbempty b = b.len = 0;;
367 let cbgetg b circular dir =
368 if cbempty b
369 then b.store.(0)
370 else
371 let rc = b.rc + dir in
372 let rc =
373 if circular
374 then (
375 if rc = -1
376 then b.len-1
377 else (
378 if rc >= b.len
379 then 0
380 else rc
383 else bound rc 0 (b.len-1)
385 b.rc <- rc;
386 b.store.(rc);
389 let cbget b = cbgetg b false;;
390 let cbgetc b = cbgetg b true;;
392 let state =
393 { ss = Unix.stdin
394 ; wsfd = Unix.stdin
395 ; stderr = Unix.stderr
396 ; errmsgs = Buffer.create 0
397 ; newerrmsgs = false
398 ; x = 0
399 ; y = 0
400 ; w = 0
401 ; anchor = emptyanchor
402 ; ranchors = []
403 ; layout = []
404 ; maxy = max_int
405 ; tilelru = Queue.create ()
406 ; pagemap = Hashtbl.create 10
407 ; tilemap = Hashtbl.create 10
408 ; pdims = []
409 ; pagecount = 0
410 ; currently = Idle
411 ; mstate = Mnone
412 ; rects = []
413 ; rects1 = []
414 ; prects = Hashtbl.create 1
415 ; text = E.s
416 ; mode = View
417 ; winstate = []
418 ; searchpattern = E.s
419 ; outlines = E.a
420 ; bookmarks = []
421 ; path = E.s
422 ; password = E.s
423 ; nameddest = E.s
424 ; geomcmds = E.s, []
425 ; hists =
426 { nav = cbnew 10 emptyanchor
427 ; pat = cbnew 10 E.s
428 ; pag = cbnew 10 E.s
429 ; sel = cbnew 10 E.s
431 ; memused = 0
432 ; gen = 0
433 ; autoscroll = None
434 ; help = E.a
435 ; docinfo = []
436 ; prevzoom = (1.0, 0)
437 ; progress = -1.0
438 ; uioh = nouioh
439 ; mpos = (-1, -1)
440 ; keystate = KSnone
441 ; glinks = false
442 ; prevcolumns = None
443 ; winw = -1
444 ; winh = -1
445 ; reprf = noreprf
446 ; origin = E.s
447 ; roam = noroam
448 ; bzoom = false
449 ; lnava = None
450 ; slideshow = 0
454 let calcips h =
455 let d = state.winh - h in
456 max conf.interpagespace ((d + 1) / 2)
459 let rowyh (c, coverA, coverB) b n =
460 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
461 then
462 let _, _, vy, (_, _, h, _) = b.(n) in
463 (vy, h)
464 else
465 let n' = n - coverA in
466 let d = n' mod c in
467 let s = n - d in
468 let e = min state.pagecount (s + c) in
469 let rec find m miny maxh = if m = e then miny, maxh else
470 let _, _, y, (_, _, h, _) = b.(m) in
471 let miny = min miny y in
472 let maxh = max maxh h in
473 find (m+1) miny maxh
474 in find s max_int 0
477 let page_of_y y =
478 let ((c, coverA, coverB) as cl), b =
479 match conf.columns with
480 | Csplit (_, b) | Csingle b -> (1, 0, 0), b
481 | Cmulti (c, b) -> c, b
483 if Array.length b = 0
484 then -1
485 else
486 let rec bsearch nmin nmax =
487 if nmin > nmax
488 then bound nmin 0 (state.pagecount-1)
489 else
490 let n = (nmax + nmin) / 2 in
491 let vy, h = rowyh cl b n in
492 let y0, y1 =
493 if conf.presentation
494 then
495 let ips = calcips h in
496 let y0 = vy - ips in
497 let y1 = vy + h + ips in
498 y0, y1
499 else (
500 if n = 0
501 then 0, vy + h + conf.interpagespace
502 else
503 let y0 = vy - conf.interpagespace in
504 y0, y0 + h + conf.interpagespace
507 if y >= y0 && y < y1
508 then (
509 if c = 1
510 then n
511 else (
512 if n > coverA
513 then
514 if n < state.pagecount - coverB
515 then ((n-coverA)/c)*c + coverA
516 else n
517 else n
520 else (
521 if y > y0
522 then bsearch (n+1) nmax
523 else bsearch nmin (n-1)
526 bsearch 0 (state.pagecount-1);
529 let calcheight () =
530 match conf.columns with
531 | Cmulti ((_, _, _) as cl, b) ->
532 if Array.length b > 0
533 then
534 let y, h = rowyh cl b (Array.length b - 1) in
535 y + h + (if conf.presentation then calcips h else 0)
536 else 0
537 | Csingle b ->
538 if Array.length b > 0
539 then
540 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
541 y + h + (if conf.presentation then calcips h else 0)
542 else 0
543 | Csplit (_, b) ->
544 if Array.length b > 0
545 then
546 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
547 y + h
548 else 0
551 let getpageywh pageno =
552 let pageno = bound pageno 0 (state.pagecount-1) in
553 match conf.columns with
554 | Csingle b ->
555 if Array.length b = 0
556 then 0, 0, 0
557 else
558 let (_, _, y, (_, w, h, _)) = b.(pageno) in
559 let y =
560 if conf.presentation
561 then y - calcips h
562 else y
564 y, w, h
565 | Cmulti (cl, b) ->
566 if Array.length b = 0
567 then 0, 0, 0
568 else
569 let y, h = rowyh cl b pageno in
570 let (_, _, _, (_, w, _, _)) = b.(pageno) in
571 let y =
572 if conf.presentation
573 then y - calcips h
574 else y
576 y, w, h
577 | Csplit (c, b) ->
578 if Array.length b = 0
579 then 0, 0, 0
580 else
581 let n = pageno*c in
582 let (_, _, y, (_, w, h, _)) = b.(n) in
583 y, w / c, h
586 let getpageyh pageno =
587 let y,_,h = getpageywh pageno in
588 y, h;
591 let getpagedim pageno =
592 let rec f ppdim l =
593 match l with
594 | (n, _, _, _) as pdim :: rest ->
595 if n >= pageno
596 then (if n = pageno then pdim else ppdim)
597 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
613 | [] -> p
615 f ~-1 state.pdims
618 let getpagey pageno = fst (getpageyh pageno);;
620 let getanchor1 l =
621 let top =
622 let coloff = l.pagecol * l.pageh in
623 float (l.pagey + coloff) /. float l.pageh
625 let dtop =
626 if l.pagedispy = 0
627 then 0.0
628 else (
629 if conf.presentation
630 then float l.pagedispy /. float (calcips l.pageh)
631 else float l.pagedispy /. float conf.interpagespace
634 (l.pageno, top, dtop)
637 let getanchor () =
638 match state.layout with
639 | l :: _ -> getanchor1 l
640 | [] ->
641 let n = page_of_y state.y in
642 if n = -1
643 then state.anchor
644 else
645 let y, h = getpageyh n in
646 let dy = y - state.y in
647 let dtop =
648 if conf.presentation
649 then
650 let ips = calcips h in
651 float (dy + ips) /. float ips
652 else
653 float dy /. float conf.interpagespace
655 (n, 0.0, dtop)
658 let fontpath = ref E.s;;
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 apply c k v =
716 match k with
717 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
718 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
719 | "case-insensitive-search" -> { c with icase = bool_of_string v }
720 | "preload" -> { c with preload = bool_of_string v }
721 | "page-bias" -> { c with pagebias = int_of_string v }
722 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
723 | "horizontal-scroll-step" ->
724 { c with hscrollstep = max (int_of_string v) 1 }
725 | "auto-scroll-step" ->
726 { c with autoscrollstep = max 0 (int_of_string v) }
727 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
728 | "highlight-links" -> { c with hlinks = bool_of_string v }
729 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
730 | "vertical-margin" ->
731 { c with interpagespace = max 0 (int_of_string v) }
732 | "zoom" ->
733 let zoom = float_of_string v /. 100. in
734 let zoom = max zoom 0.0 in
735 { c with zoom = zoom }
736 | "presentation" -> { c with presentation = bool_of_string v }
737 | "rotation-angle" -> { c with angle = int_of_string v }
738 | "width" -> { c with cwinw = max 20 (int_of_string v) }
739 | "height" -> { c with cwinh = max 20 (int_of_string v) }
740 | "proportional-display" ->
741 let fm =
742 if bool_of_string v
743 then FitProportional
744 else FitWidth
746 { c with fitmodel = fm }
747 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
748 | "pixmap-cache-size" ->
749 { c with memlimit = max 2 (int_of_string_with_suffix v) }
750 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
751 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
752 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
753 | "background-color" -> { c with bgcolor = color_of_string v }
754 | "scrollbar-color" -> { c with sbarcolor = rgba_of_string v }
755 | "scrollbar-handle-color" -> { c with sbarhndlcolor = rgba_of_string v }
756 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
757 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
758 | "mupdf-store-size" ->
759 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
760 | "checkers" -> { c with checkers = bool_of_string v }
761 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
762 | "trim-margins" -> { c with trimmargins = bool_of_string v }
763 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
764 | "uri-launcher" -> { c with urilauncher = unentS v }
765 | "path-launcher" -> { c with pathlauncher = unentS v }
766 | "color-space" -> { c with colorspace = CSTE.of_string v }
767 | "invert-colors" -> { c with invert = bool_of_string v }
768 | "brightness" -> { c with colorscale = float_of_string v }
769 | "columns" ->
770 let (n, _, _) as nab = multicolumns_of_string v in
771 if n < 0
772 then { c with columns = Csplit (-n, E.a) }
773 else { c with columns = Cmulti (nab, E.a) }
774 | "birds-eye-columns" ->
775 { c with beyecolumns = Some (max (int_of_string v) 2) }
776 | "selection-command" -> { c with selcmd = unentS v }
777 | "synctex-command" -> { c with stcmd = unentS v }
778 | "pax-command" -> { c with paxcmd = unentS v }
779 | "askpass-command" -> { c with passcmd = unentS v }
780 | "savepath-command" -> { c with savecmd = unentS v }
781 | "update-cursor" -> { c with updatecurs = bool_of_string v }
782 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
783 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
784 | "use-pbo" -> { c with usepbo = bool_of_string v }
785 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
786 | "horizontal-scrollbar-visible" ->
787 let b =
788 if bool_of_string v
789 then c.scrollb lor scrollbhv
790 else c.scrollb land (lnot scrollbhv)
792 { c with scrollb = b }
793 | "vertical-scrollbar-visible" ->
794 let b =
795 if bool_of_string v
796 then c.scrollb lor scrollbvv
797 else c.scrollb land (lnot scrollbvv)
799 { c with scrollb = b }
800 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
801 | "point-and-x" ->
802 { c with pax =
803 if bool_of_string v
804 then Some 0.0
805 else None }
806 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
807 | "scroll-bar-on-the-left" -> { c with leftscroll = bool_of_string v }
808 | "title" -> { c with title = unentS v }
809 | "last-visit" -> { c with lastvisit = float_of_string v }
810 | "edit-annotations-inline" -> { c with annotinline = bool_of_string v }
811 | "coarse-presentation-positioning" ->
812 { c with coarseprespos = bool_of_string v }
813 | "use-document-css" -> { c with usedoccss = bool_of_string v }
814 | _ -> c
815 with exn ->
816 dolog "error processing attribute (`%S' = `%S'): %s" k v @@ exntos exn;
819 let rec fold c = function
820 | [] -> c
821 | (k, v) :: rest ->
822 let c = apply c k v in
823 fold c rest
825 fold { c with keyhashes = copykeyhashes c } attrs;
828 let fromstring f pos n v d =
829 try f v
830 with exn ->
831 dolog "error processing attribute (%S=%S) at %d\n%s" n v pos @@ exntos exn;
835 let bookmark_of attrs =
836 let rec fold title page rely visy = function
837 | ("title", v) :: rest -> fold v page rely visy rest
838 | ("page", v) :: rest -> fold title v rely visy rest
839 | ("rely", v) :: rest -> fold title page v visy rest
840 | ("visy", v) :: rest -> fold title page rely v rest
841 | _ :: rest -> fold title page rely visy rest
842 | [] -> title, page, rely, visy
844 fold "invalid" "0" "0" "0" attrs
847 let doc_of attrs =
848 let rec fold path key page rely pan visy origin = function
849 | ("path", v) :: rest -> fold v key page rely pan visy origin rest
850 | ("key", v) :: rest -> fold path v page rely pan visy origin rest
851 | ("page", v) :: rest -> fold path key v rely pan visy origin rest
852 | ("rely", v) :: rest -> fold path key page v pan visy origin rest
853 | ("pan", v) :: rest -> fold path key page rely v visy origin rest
854 | ("visy", v) :: rest -> fold path key page rely pan v origin rest
855 | ("origin", v) :: rest -> fold path key page rely pan visy v rest
856 | _ :: rest -> fold path key page rely pan visy origin rest
857 | [] -> path, key, page, rely, pan, visy, origin
859 fold E.s E.s "0" "0" "0" "0" E.s attrs
862 let map_of attrs =
863 let rec fold rs ls = function
864 | ("out", v) :: rest -> fold v ls rest
865 | ("in", v) :: rest -> fold rs v rest
866 | _ :: rest -> fold ls rs rest
867 | [] -> ls, rs
869 fold E.s E.s attrs
872 let findkeyhash c name =
873 try List.assoc name c.keyhashes
874 with Not_found -> error "invalid mode name `%s'" name
877 let get s =
878 let open Parser in
879 let h = Hashtbl.create 10 in
880 let dc = { defconf with angle = defconf.angle } in
881 let rec toplevel v t spos _ =
882 match t with
883 | Vdata | Vcdata | Vend -> v
884 | Vopen ("llppconfig", _, closed) ->
885 if closed
886 then v
887 else { v with f = llppconfig }
888 | Vopen _ -> parse_error "unexpected subelement at top level" s spos
889 | Vclose _ -> parse_error "unexpected close at top level" s spos
891 and llppconfig v t spos _ =
892 match t with
893 | Vdata | Vcdata -> v
894 | Vend -> parse_error "unexpected end of input in llppconfig" s spos
895 | Vopen ("defaults", attrs, closed) ->
896 let c = config_of dc attrs in
897 setconf dc c;
898 if closed
899 then v
900 else { v with f = defaults }
902 | Vopen ("ui-font", attrs, closed) ->
903 let rec getsize size = function
904 | [] -> size
905 | ("size", v) :: rest ->
906 let size =
907 fromstring int_of_string spos "size" v fstate.fontsize in
908 getsize size rest
909 | l -> getsize size l
911 fstate.fontsize <- getsize fstate.fontsize attrs;
912 if closed
913 then v
914 else { v with f = uifont (Buffer.create 10) }
916 | Vopen ("doc", attrs, closed) ->
917 let pathent, key, spage, srely, span, svisy, origin = doc_of attrs in
918 let path = unentS pathent
919 and origin = unentS origin
920 and pageno = fromstring int_of_string spos "page" spage 0
921 and rely = fromstring float_of_string spos "rely" srely 0.0
922 and pan = fromstring int_of_string spos "pan" span 0
923 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
924 let c = config_of dc attrs in
925 c.key <- key;
926 let anchor = (pageno, rely, visy) in
927 if closed
928 then (Hashtbl.add h path (c, [], pan, anchor, origin); v)
929 else { v with f = doc path origin pan anchor c [] }
931 | Vopen _ ->
932 parse_error "unexpected subelement in llppconfig" s spos
934 | Vclose "llppconfig" -> { v with f = toplevel }
935 | Vclose _ -> parse_error "unexpected close in llppconfig" s spos
937 and defaults v t spos _ =
938 match t with
939 | Vdata | Vcdata -> v
940 | Vend -> parse_error "unexpected end of input in defaults" s spos
941 | Vopen ("keymap", attrs, closed) ->
942 let modename =
943 try List.assoc "mode" attrs
944 with Not_found -> "global" in
945 if closed
946 then v
947 else
948 let ret keymap =
949 let h = findkeyhash dc modename in
950 KeyMap.iter (Hashtbl.replace h) keymap;
951 defaults
953 { v with f = pkeymap ret KeyMap.empty }
955 | Vopen (_, _, _) ->
956 parse_error "unexpected subelement in defaults" s spos
958 | Vclose "defaults" ->
959 { v with f = llppconfig }
961 | Vclose _ -> parse_error "unexpected close in defaults" s spos
963 and uifont b v t spos epos =
964 match t with
965 | Vdata | Vcdata ->
966 Buffer.add_substring b s spos (epos - spos);
968 | Vopen (_, _, _) ->
969 parse_error "unexpected subelement in ui-font" s spos
970 | Vclose "ui-font" ->
971 if emptystr !fontpath
972 then fontpath := Buffer.contents b;
973 { v with f = llppconfig }
974 | Vclose _ -> parse_error "unexpected close in ui-font" s spos
975 | Vend -> parse_error "unexpected end of input in ui-font" s spos
977 and doc path origin pan anchor c bookmarks v t spos _ =
978 match t with
979 | Vdata | Vcdata -> v
980 | Vend -> parse_error "unexpected end of input in doc" s spos
981 | Vopen ("bookmarks", _, closed) ->
982 if closed
983 then v
984 else { v with f = pbookmarks path origin pan anchor c bookmarks }
986 | Vopen ("keymap", attrs, closed) ->
987 let modename =
988 try List.assoc "mode" attrs
989 with Not_found -> "global"
991 if closed
992 then v
993 else
994 let ret keymap =
995 let h = findkeyhash c modename in
996 KeyMap.iter (Hashtbl.replace h) keymap;
997 doc path origin pan anchor c bookmarks
999 { v with f = pkeymap ret KeyMap.empty }
1001 | Vopen ("css", [], false) ->
1002 { v with f = pcss path origin pan anchor c bookmarks }
1004 | Vopen (_, _, _) ->
1005 parse_error "unexpected subelement in doc" s spos
1007 | Vclose "doc" ->
1008 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor, origin);
1009 { v with f = llppconfig }
1011 | Vclose _ -> parse_error "unexpected close in doc" s spos
1013 and pcss path origin pan anchor c bookmarks v t spos epos =
1014 match t with
1015 | Vdata | Vcdata ->
1016 let b = Buffer.create 10 in
1017 Buffer.add_substring b s spos (epos - spos);
1018 { v with f = pcss path origin pan anchor
1019 { c with css = Buffer.contents b }
1020 bookmarks }
1021 | Vend -> parse_error "unexpected end of input in css" s spos
1022 | Vopen _ -> parse_error "unexpected subelement in css" s spos
1023 | Vclose "css" -> { v with f = doc path origin pan anchor c bookmarks }
1024 | Vclose _ -> parse_error "unexpected close in css" s spos
1026 and pkeymap ret keymap v t spos _ =
1027 match t with
1028 | Vdata | Vcdata -> v
1029 | Vend -> parse_error "unexpected end of input in keymap" s spos
1030 | Vopen ("map", attrs, closed) ->
1031 let r, l = map_of attrs in
1032 let kss = fromstring keys_of_string spos "in" r [] in
1033 let lss = fromstring keys_of_string spos "out" l [] in
1034 let keymap =
1035 match kss with
1036 | [] -> keymap
1037 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
1038 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
1040 if closed
1041 then { v with f = pkeymap ret keymap }
1042 else
1043 let f () = v in
1044 { v with f = skip "map" f }
1046 | Vopen _ ->
1047 parse_error "unexpected subelement in keymap" s spos
1049 | Vclose "keymap" ->
1050 { v with f = ret keymap }
1052 | Vclose _ -> parse_error "unexpected close in keymap" s spos
1054 and pbookmarks path origin pan anchor c bookmarks v t spos _ =
1055 match t with
1056 | Vdata | Vcdata -> v
1057 | Vend -> parse_error "unexpected end of input in bookmarks" s spos
1058 | Vopen ("item", attrs, closed) ->
1059 let titleent, spage, srely, svisy = bookmark_of attrs in
1060 let page = fromstring int_of_string spos "page" spage 0
1061 and rely = fromstring float_of_string spos "rely" srely 0.0
1062 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1063 let bookmarks =
1064 (unentS titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
1066 if closed
1067 then { v with f = pbookmarks path origin pan anchor c bookmarks }
1068 else
1069 let f () = v in
1070 { v with f = skip "item" f }
1072 | Vopen _ ->
1073 parse_error "unexpected subelement in bookmarks" s spos
1075 | Vclose "bookmarks" ->
1076 { v with f = doc path origin pan anchor c bookmarks }
1078 | Vclose _ -> parse_error "unexpected close in bookmarks" s spos
1080 and skip tag f v t spos _ =
1081 match t with
1082 | Vdata | Vcdata -> v
1083 | Vend ->
1084 parse_error ("unexpected end of input in skipped " ^ tag) s spos
1085 | Vopen (tag', _, closed) ->
1086 if closed
1087 then v
1088 else
1089 let f' () = { v with f = skip tag f } in
1090 { v with f = skip tag' f' }
1091 | Vclose ctag ->
1092 if tag = ctag
1093 then f ()
1094 else parse_error ("unexpected close in skipped " ^ tag) s spos
1097 parse { f = toplevel; accu = () } s;
1098 h, dc;
1101 let do_load f contents =
1102 try f contents
1103 with
1104 | Parser.Parse_error (msg, s, pos) ->
1105 let subs = Parser.subs s pos in
1106 Utils.error "parse error: %s: at %d [..%S..]" msg pos subs
1108 | exn -> Utils.error "parse error: %s" @@ exntos exn
1111 let defconfpath =
1112 let dir =
1113 let xdgconfdir = Utils.getenvwithdef "XDG_CONFIG_HOME" E.s in
1114 if emptystr xdgconfdir
1115 then
1117 let dir = Filename.concat home ".config" in
1118 if Sys.is_directory dir then dir else home
1119 with _ -> home
1120 else xdgconfdir
1122 Filename.concat dir "llpp.conf"
1125 let confpath = ref defconfpath;;
1127 let load2 f default =
1128 match filecontents !confpath with
1129 | contents -> f @@ do_load get contents
1130 | exception Unix.Unix_error (Unix.ENOENT, "open", _) ->
1131 f (Hashtbl.create 0, defconf)
1132 | exception exn ->
1133 dolog "error loading configuration from `%S': %s" !confpath @@ exntos exn;
1134 default
1137 let load1 f = load2 f false;;
1139 let load openlast =
1140 let f (h, dc) =
1141 if openlast
1142 then (
1143 let path, _ =
1144 Hashtbl.fold
1145 (fun path (conf, _, _, _, _) ((_, besttime) as best) ->
1146 if conf.lastvisit > besttime
1147 then (path, conf.lastvisit)
1148 else best)
1150 (state.path, -.infinity)
1152 state.path <- path;
1154 let pc, pb, px, pa, po =
1155 let def = dc, [], 0, emptyanchor, state.origin in
1156 if emptystr state.path
1157 then def
1158 else
1159 let absname = abspath state.path in
1160 match Hashtbl.find h absname with
1161 | (c,b,x,a,_) -> (c,b,x,a,state.origin)
1162 | exception Not_found ->
1163 let exception E of (conf * outline list * int * anchor * string) in
1164 let key = try Digest.file absname |> Digest.to_hex with _ -> E.s in
1165 match (
1166 if nonemptystr key
1167 then
1168 Hashtbl.iter (fun p ((c, _, _, _, _) as v) ->
1169 if c.key = key
1170 then (
1171 dolog "will use %s's settings due to matching keys" p;
1172 raise (E v)
1176 with
1177 | _ -> def
1178 | exception E v -> v
1180 setconf defconf dc;
1181 setconf conf pc;
1182 state.bookmarks <- pb;
1183 state.x <- px;
1184 state.origin <- po;
1185 state.anchor <- pa;
1186 cbput state.hists.nav pa;
1187 true
1189 load1 f
1192 let gethist () =
1193 let f (h, _) =
1194 Hashtbl.fold (fun path (pc, pb, px, pa, po) accu ->
1195 (path, pc, pb, px, pa, po) :: accu)
1196 h [];
1198 load2 f []
1201 let add_attrs bb always dc c time =
1202 let o' fmt s =
1203 Buffer.add_string bb "\n ";
1204 Printf.bprintf bb fmt s
1206 let o c fmt s = if c then o' fmt s else ignore in
1207 let ob s a b = o (always || a != b) "%s='%b'" s a
1208 and op s a b = o (always || a <> b) "%s='%b'" s (a != None)
1209 and oi s a b = o (always || a != b) "%s='%d'" s a
1210 and oI s a b = o (always || a != b) "%s='%s'" s (string_with_suffix_of_int a)
1211 and oz s a b = o (always || a <> b) "%s='%g'" s (a*.100.)
1212 and oF s a b = o (always || a <> b) "%s='%f'" s a
1213 and oL s a b = o (always || a <> b) "%s='%Ld'" s a
1214 and oc s a b = o (always || a <> b) "%s='%s'" s (color_to_string a)
1215 and oA s a b = o (always || a <> b) "%s='%s'" s (rgba_to_string a)
1216 and oC s a b = o (always || a <> b) "%s='%s'" s (CSTE.to_string a)
1217 and oR s a b = o (always || a <> b) "%s='%s'" s (irect_to_string a)
1218 and oFm s a b = o (always || a <> b) "%s='%s'" s (FMTE.to_string a)
1219 and oSv s a b m =
1220 o (always || a land m <> b land m) "%s='%b'" s (a land m != 0)
1221 and oPm s a b = o (always || a <> b) "%s='%s'" s (MTE.to_string a)
1222 and os s a b =
1223 o (always || a <> b) "%s='%s'" s @@ Parser.enent a 0 (String.length a)
1224 and oco s a b =
1225 if always || a <> b
1226 then
1227 match a with
1228 | Cmulti ((n, a, b), _) when n > 1 -> o' "%s='%d,%d,%d'" s n a b
1229 | Csplit (n, _) when n > 1 -> o' "%s='%d'" s ~-n
1230 | Cmulti _ | Csplit _ | Csingle _ -> ()
1231 and obeco s a b =
1232 if always || a <> b
1233 then
1234 match a with
1235 | Some c when c > 1 -> o' "%s='%d'" s c
1236 | _ -> ()
1238 oi "width" c.cwinw dc.cwinw;
1239 oi "height" c.cwinh dc.cwinh;
1240 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
1241 oi "scroll-handle-height" c.scrollh dc.scrollh;
1242 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
1243 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
1244 ob "case-insensitive-search" c.icase dc.icase;
1245 ob "preload" c.preload dc.preload;
1246 oi "page-bias" c.pagebias dc.pagebias;
1247 oi "scroll-step" c.scrollstep dc.scrollstep;
1248 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
1249 ob "max-height-fit" c.maxhfit dc.maxhfit;
1250 ob "highlight-links" c.hlinks dc.hlinks;
1251 ob "under-cursor-info" c.underinfo dc.underinfo;
1252 oi "vertical-margin" c.interpagespace dc.interpagespace;
1253 oz "zoom" c.zoom dc.zoom;
1254 ob "presentation" c.presentation dc.presentation;
1255 oi "rotation-angle" c.angle dc.angle;
1256 oFm "fit-model" c.fitmodel dc.fitmodel;
1257 oI "pixmap-cache-size" c.memlimit dc.memlimit;
1258 oi "tex-count" c.texcount dc.texcount;
1259 oi "slice-height" c.sliceheight dc.sliceheight;
1260 oi "thumbnail-width" c.thumbw dc.thumbw;
1261 oc "background-color" c.bgcolor dc.bgcolor;
1262 oA "scrollbar-color" c.sbarcolor dc.sbarcolor;
1263 oA "scrollbar-handle-color" c.sbarhndlcolor dc.sbarhndlcolor;
1264 oi "tile-width" c.tilew dc.tilew;
1265 oi "tile-height" c.tileh dc.tileh;
1266 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
1267 ob "checkers" c.checkers dc.checkers;
1268 oi "aalevel" c.aalevel dc.aalevel;
1269 ob "trim-margins" c.trimmargins dc.trimmargins;
1270 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
1271 os "uri-launcher" c.urilauncher dc.urilauncher;
1272 os "path-launcher" c.pathlauncher dc.pathlauncher;
1273 oC "color-space" c.colorspace dc.colorspace;
1274 ob "invert-colors" c.invert dc.invert;
1275 oF "brightness" c.colorscale dc.colorscale;
1276 oco "columns" c.columns dc.columns;
1277 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
1278 os "selection-command" c.selcmd dc.selcmd;
1279 os "synctex-command" c.stcmd dc.stcmd;
1280 os "pax-command" c.paxcmd dc.paxcmd;
1281 os "askpass-command" c.passcmd dc.passcmd;
1282 os "savepath-command" c.savecmd dc.savecmd;
1283 ob "update-cursor" c.updatecurs dc.updatecurs;
1284 oi "hint-font-size" c.hfsize dc.hfsize;
1285 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
1286 oF "page-scroll-scale" c.pgscale dc.pgscale;
1287 ob "use-pbo" c.usepbo dc.usepbo;
1288 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
1289 ob "remote-in-a-new-instance" c.riani dc.riani;
1290 op "point-and-x" c.pax dc.pax;
1291 oPm "point-and-x-mark" c.paxmark dc.paxmark;
1292 ob "scroll-bar-on-the-left" c.leftscroll dc.leftscroll;
1293 if not always
1294 then os "title" c.title dc.title;
1295 oL "last-visit" (Int64.of_float time) 0L;
1296 ob "edit-annotations-inline" c.annotinline dc.annotinline;
1297 ob "coarse-presentation-positioning" c.coarseprespos dc.coarseprespos;
1298 ob "use-document-css" c.usedoccss dc.usedoccss;
1301 let keymapsbuf always dc c =
1302 let open Buffer in
1303 let bb = create 16 in
1304 let rec loop = function
1305 | [] -> ()
1306 | (modename, h) :: rest ->
1307 let dh = findkeyhash dc modename in
1308 if always || h <> dh
1309 then (
1310 if Hashtbl.length h > 0
1311 then (
1312 if length bb > 0 then add_char bb '\n';
1313 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
1314 Hashtbl.iter (fun i o ->
1315 if always || match Hashtbl.find dh i
1316 with | dO -> dO <> o | exception Not_found -> false
1317 then
1318 let addkm (k, m) =
1319 if Wsi.withctrl m then add_string bb "ctrl-";
1320 if Wsi.withalt m then add_string bb "alt-";
1321 if Wsi.withshift m then add_string bb "shift-";
1322 if Wsi.withmeta m then add_string bb "meta-";
1323 add_string bb (Wsi.keyname k);
1325 let addkms l =
1326 let rec loop = function
1327 | [] -> ()
1328 | km :: [] -> addkm km
1329 | km :: rest -> addkm km; add_char bb ' '; loop rest
1331 loop l
1333 add_string bb "<map in='";
1334 addkm i;
1335 match o with
1336 | KMinsrt km ->
1337 add_string bb "' out='"; addkm km; add_string bb "'/>\n"
1339 | KMinsrl kms ->
1340 add_string bb "' out='"; addkms kms; add_string bb "'/>\n"
1342 | KMmulti (ins, kms) ->
1343 add_char bb ' '; addkms ins; add_string bb "' out='";
1344 addkms kms; add_string bb "'/>\n"
1345 ) h;
1346 add_string bb "</keymap>";
1349 loop rest
1351 loop c.keyhashes;
1355 let keystostrlist c =
1356 let rec loop accu = function
1357 | [] -> accu
1358 | (modename, h) :: rest ->
1359 let accu =
1360 if Hashtbl.length h > 0
1361 then (
1362 let accu = Printf.sprintf "\xc2\xb7Keys for %s" modename :: accu in
1363 Hashtbl.fold (fun i o a ->
1364 let bb = Buffer.create 10 in
1365 let addkm (k, m) =
1366 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
1367 if Wsi.withalt m then Buffer.add_string bb "alt-";
1368 if Wsi.withshift m then Buffer.add_string bb "shift-";
1369 if Wsi.withmeta m then Buffer.add_string bb "meta-";
1370 Buffer.add_string bb (Wsi.keyname k);
1372 let addkms l =
1373 let rec loop = function
1374 | [] -> ()
1375 | km :: [] -> addkm km
1376 | km :: rest ->
1377 addkm km; Buffer.add_char bb ' ';
1378 loop rest
1380 loop l
1382 addkm i;
1383 Buffer.add_char bb '\t';
1384 begin match o with
1385 | KMinsrt km ->
1386 addkm km
1388 | KMinsrl kms ->
1389 addkms kms
1391 | KMmulti (ins, kms) ->
1392 Buffer.add_char bb ' ';
1393 addkms ins;
1394 Buffer.add_string bb "\t";
1395 addkms kms
1396 end;
1397 Buffer.contents bb :: a
1398 ) h accu
1400 else accu
1402 loop accu rest
1404 loop [] c.keyhashes
1407 let save1 bb leavebirdseye x h dc =
1408 let uifontsize = fstate.fontsize in
1409 let dc = if conf.bedefault then conf else dc in
1410 Buffer.add_string bb "<llppconfig>\n";
1412 if nonemptystr !fontpath
1413 then Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
1414 uifontsize
1415 !fontpath
1416 else
1417 if uifontsize <> 14
1418 then Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
1421 Buffer.add_string bb "<defaults";
1422 add_attrs bb true dc dc nan;
1423 let kb = keymapsbuf true dc dc in
1424 if Buffer.length kb > 0
1425 then (
1426 Buffer.add_string bb ">\n";
1427 Buffer.add_buffer bb kb;
1428 Buffer.add_string bb "\n</defaults>\n";
1430 else Buffer.add_string bb "/>\n";
1432 let adddoc path pan anchor c bookmarks time origin =
1433 if not (bookmarks == [] && c = dc && anchor = emptyanchor)
1434 then (
1435 Printf.bprintf bb "<doc path='%s'"
1436 (Parser.enent path 0 (String.length path));
1438 if nonemptystr c.key
1439 then Printf.bprintf bb "\n key='%s'" c.key;
1441 if nonemptystr origin
1442 then Printf.bprintf bb "\n origin='%s'"
1443 (Parser.enent origin 0 (String.length origin));
1445 if anchor <> emptyanchor
1446 then (
1447 let n, rely, visy = anchor in
1448 Printf.bprintf bb "\n page='%d'" n;
1450 if rely > 1e-6
1451 then Printf.bprintf bb " rely='%f'" rely;
1453 if abs_float visy > 1e-6
1454 then Printf.bprintf bb " visy='%f'" visy;
1457 if pan != 0
1458 then Printf.bprintf bb " pan='%d'" pan;
1460 add_attrs bb false dc c time;
1461 if nonemptystr c.css
1462 then Printf.bprintf bb ">\n <css><![CDATA[%s]]></css>" c.css;
1463 let kb = keymapsbuf false dc c in
1465 begin match bookmarks with
1466 | [] ->
1467 if Buffer.length kb > 0
1468 then (
1469 Buffer.add_string bb ">\n";
1470 Buffer.add_buffer bb kb;
1471 Buffer.add_string bb "\n</doc>\n";
1473 else
1474 if nonemptystr c.css
1475 then Buffer.add_string bb "\n</doc>\n"
1476 else Buffer.add_string bb "/>\n"
1477 | _ ->
1478 Buffer.add_string bb ">\n<bookmarks>\n";
1479 List.iter (fun (title, _, kind) ->
1480 begin match kind with
1481 | Oanchor (page, rely, visy) ->
1482 Printf.bprintf bb
1483 "<item title='%s' page='%d'"
1484 (Parser.enent title 0 (String.length title))
1485 page;
1486 if rely > 1e-6
1487 then Printf.bprintf bb " rely='%f'" rely;
1489 if abs_float visy > 1e-6
1490 then Printf.bprintf bb " visy='%f'" visy;
1491 | Ohistory _ | Onone | Ouri _ | Oremote _
1492 | Oremotedest _ | Olaunch _ -> error "unexpected link in bookmarks"
1493 end;
1494 Buffer.add_string bb "/>\n";
1495 ) bookmarks;
1496 Buffer.add_string bb "</bookmarks>";
1497 if Buffer.length kb > 0
1498 then (
1499 Buffer.add_string bb "\n";
1500 Buffer.add_buffer bb kb;
1502 Buffer.add_string bb "\n</doc>\n";
1503 end;
1507 let pan, conf =
1508 match state.mode with
1509 | Birdseye (c, pan, _, _, _) ->
1510 let beyecolumns =
1511 match conf.columns with
1512 | Cmulti ((c, _, _), _) -> Some c
1513 | Csingle _ -> None
1514 | Csplit _ -> None
1515 and columns =
1516 match c.columns with
1517 | Cmulti (c, _) -> Cmulti (c, E.a)
1518 | Csingle _ -> Csingle E.a
1519 | Csplit _ -> failwith "quit from bird's eye while split"
1521 pan, { c with beyecolumns = beyecolumns; columns = columns }
1522 | Textentry _
1523 | View
1524 | LinkNav _ -> x, conf
1526 let docpath = if nonemptystr state.path then abspath state.path else E.s in
1527 if nonemptystr docpath
1528 then (
1529 adddoc docpath pan (getanchor ())
1531 let autoscrollstep =
1532 match state.autoscroll with
1533 | Some step -> step
1534 | None -> conf.autoscrollstep
1536 begin match state.mode with
1537 | Birdseye beye -> leavebirdseye beye true
1538 | Textentry _
1539 | View
1540 | LinkNav _ -> ()
1541 end;
1542 let key = try Digest.file docpath |> Digest.to_hex
1543 with _ -> E.s in
1544 { conf with autoscrollstep; key }
1546 state.bookmarks
1547 (now ())
1548 state.origin
1550 Hashtbl.iter (fun path (c, bookmarks, x, anchor, origin) ->
1551 if docpath <> abspath path
1552 then adddoc path x anchor c bookmarks c.lastvisit origin
1553 ) h;
1554 Buffer.add_string bb "</llppconfig>\n";
1555 true;
1558 let save leavebirdseye =
1559 let relx = float state.x /. float state.winw in
1560 let w, h, x =
1561 let cx w = truncate (relx *. float w) in
1562 List.fold_left
1563 (fun (w, h, x) ws ->
1564 match ws with
1565 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
1566 | Wsi.MaxVert -> (w, conf.cwinh, x)
1567 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
1569 (state.winw, state.winh, state.x) state.winstate
1571 conf.cwinw <- w;
1572 conf.cwinh <- h;
1573 let bb = Buffer.create 32768 in
1574 let save2 (h, dc) =
1575 save1 bb leavebirdseye x h dc
1577 if load1 save2 && Buffer.length bb > 0
1578 then
1580 let tmp = !confpath ^ ".tmp" in
1581 let oc = open_out_bin tmp in
1582 Buffer.output_buffer oc bb;
1583 close_out oc;
1584 Unix.rename tmp !confpath;
1585 with exn ->
1586 dolog "error saving configuration: %s" @@ exntos exn
1589 let gc () =
1590 let href = ref @@ Hashtbl.create 0 in
1591 let cref = ref defconf in
1592 let push (h, dc) =
1593 let f path v =
1594 if Sys.file_exists path
1595 then Some v
1596 else (dolog "removing entry for '%s'" path; None) in
1597 Hashtbl.filter_map_inplace f h;
1598 href := h;
1599 cref := dc;
1600 true
1602 ignore (load1 push);
1603 let bb = Buffer.create 32768 in
1604 let save2 (_h, dc) = save1 bb (fun _ _ -> ()) 0 !href dc in
1605 if load1 save2 && Buffer.length bb > 0
1606 then (
1608 let tmp = !confpath ^ ".tmp" in
1609 let oc = open_out_bin tmp in
1610 Buffer.output_buffer oc bb;
1611 close_out oc;
1612 Unix.rename tmp !confpath;
1613 with exn ->
1614 dolog "error saving configuration: %s" @@ exntos exn
1618 let logcurrently = function
1619 | Idle -> dolog "Idle"
1620 | Loading (l, gen) ->
1621 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1622 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1623 dolog "Tiling %d[%d,%d] page=%s cs=%s angle=%d"
1624 l.pageno col row (~> pageopaque)
1625 (CSTE.to_string colorspace) angle;
1626 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1627 angle gen conf.angle state.gen
1628 tilew tileh
1629 conf.tilew conf.tileh
1630 | Outlining _ -> dolog "outlining"
1633 let logpage l =
1634 dolog {|l %d dim=%d {
1635 WxH %dx%d
1636 vWxH %dx%d
1637 pagex,y %d,%d
1638 dispx,y %d,%d
1639 column %d
1641 l.pageno l.pagedimno
1642 l.pagew l.pageh
1643 l.pagevw l.pagevh
1644 l.pagex l.pagey
1645 l.pagedispx l.pagedispy
1646 l.pagecol