Remove mlis
[llpp.git] / config.ml
blob2daa43fe18ee8d43e8e55dd6c4958497ead53c99
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 ; mutable checkerstexid : GlTex.texture_id option
289 ; hists : hists
290 ; mutable prevzoom : (float * int)
291 ; mutable progress : float
292 ; mutable mpos : mpos
293 ; mutable keystate : keystate
294 ; mutable glinks : bool
295 ; mutable prevcolumns : (columns * float) option
296 ; mutable winw : int
297 ; mutable winh : int
298 ; mutable reprf : (unit -> unit)
299 ; mutable origin : string
300 ; mutable roam : (unit -> unit)
301 ; mutable bzoom : bool
302 ; mutable lnava : (pageno * linkno) option
303 ; mutable slideshow : int
305 and hists =
306 { pat : string circbuf
307 ; pag : string circbuf
308 ; nav : anchor circbuf
309 ; sel : string circbuf
313 let emptyanchor = (0, 0.0, 0.0);;
314 let emptykeyhash = Hashtbl.create 0;;
315 let noreprf () = ();;
316 let noroam () = ();;
318 let nouioh : uioh =
319 object (self)
320 method display = ()
321 method key _ _ = self
322 method multiclick _ _ _ _ = self
323 method button _ _ _ _ _ = self
324 method motion _ _ = self
325 method pmotion _ _ = self
326 method infochanged _ = ()
327 method scrollpw = (0, nan, nan)
328 method scrollph = (0, nan, nan)
329 method modehash = emptykeyhash
330 method eformsgs = false
331 method alwaysscrolly = false
332 method scroll _ _ = self
333 method zoom _ _ _ = ()
334 end;;
336 let platform_to_string = function
337 | Punknown -> "unknown"
338 | Plinux -> "Linux"
339 | Pmacos -> "macOS"
340 | Pbsd -> "BSD"
343 let conf = { defconf with keyhashes = copykeyhashes defconf };;
345 let cbnew n v =
346 { store = Array.make n v
347 ; rc = 0
348 ; wc = 0
349 ; len = 0
353 let cbcap b = Array.length b.store;;
355 let cbput ?(update_rc=true) b v =
356 let cap = cbcap b in
357 b.store.(b.wc) <- v;
358 b.wc <- (b.wc + 1) mod cap;
359 if update_rc
360 then b.rc <- b.wc;
361 b.len <- min (b.len + 1) cap;
364 let cbput_dont_update_rc b v = cbput ~update_rc:false b v;;
366 let cbempty b = b.len = 0;;
368 let cbgetg b circular dir =
369 if cbempty b
370 then b.store.(0)
371 else
372 let rc = b.rc + dir in
373 let rc =
374 if circular
375 then (
376 if rc = -1
377 then b.len-1
378 else (
379 if rc >= b.len
380 then 0
381 else rc
384 else bound rc 0 (b.len-1)
386 b.rc <- rc;
387 b.store.(rc);
390 let cbget b = cbgetg b false;;
391 let cbgetc b = cbgetg b true;;
393 let state =
394 { ss = Unix.stdin
395 ; wsfd = Unix.stdin
396 ; stderr = Unix.stderr
397 ; errmsgs = Buffer.create 0
398 ; newerrmsgs = false
399 ; x = 0
400 ; y = 0
401 ; w = 0
402 ; anchor = emptyanchor
403 ; ranchors = []
404 ; layout = []
405 ; maxy = max_int
406 ; tilelru = Queue.create ()
407 ; pagemap = Hashtbl.create 10
408 ; tilemap = Hashtbl.create 10
409 ; pdims = []
410 ; pagecount = 0
411 ; currently = Idle
412 ; mstate = Mnone
413 ; rects = []
414 ; rects1 = []
415 ; prects = Hashtbl.create 1
416 ; text = E.s
417 ; mode = View
418 ; winstate = []
419 ; searchpattern = E.s
420 ; outlines = E.a
421 ; bookmarks = []
422 ; path = E.s
423 ; password = E.s
424 ; nameddest = E.s
425 ; geomcmds = E.s, []
426 ; hists =
427 { nav = cbnew 10 emptyanchor
428 ; pat = cbnew 10 E.s
429 ; pag = cbnew 10 E.s
430 ; sel = cbnew 10 E.s
432 ; memused = 0
433 ; gen = 0
434 ; autoscroll = None
435 ; help = E.a
436 ; docinfo = []
437 ; checkerstexid = None
438 ; prevzoom = (1.0, 0)
439 ; progress = -1.0
440 ; uioh = nouioh
441 ; mpos = (-1, -1)
442 ; keystate = KSnone
443 ; glinks = false
444 ; prevcolumns = None
445 ; winw = -1
446 ; winh = -1
447 ; reprf = noreprf
448 ; origin = E.s
449 ; roam = noroam
450 ; bzoom = false
451 ; lnava = None
452 ; slideshow = 0
456 let calcips h =
457 let d = state.winh - h in
458 max conf.interpagespace ((d + 1) / 2)
461 let rowyh (c, coverA, coverB) b n =
462 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
463 then
464 let _, _, vy, (_, _, h, _) = b.(n) in
465 (vy, h)
466 else
467 let n' = n - coverA in
468 let d = n' mod c in
469 let s = n - d in
470 let e = min state.pagecount (s + c) in
471 let rec find m miny maxh = if m = e then miny, maxh else
472 let _, _, y, (_, _, h, _) = b.(m) in
473 let miny = min miny y in
474 let maxh = max maxh h in
475 find (m+1) miny maxh
476 in find s max_int 0
479 let page_of_y y =
480 let ((c, coverA, coverB) as cl), b =
481 match conf.columns with
482 | Csplit (_, b) | Csingle b -> (1, 0, 0), b
483 | Cmulti (c, b) -> c, b
485 if Array.length b = 0
486 then -1
487 else
488 let rec bsearch nmin nmax =
489 if nmin > nmax
490 then bound nmin 0 (state.pagecount-1)
491 else
492 let n = (nmax + nmin) / 2 in
493 let vy, h = rowyh cl b n in
494 let y0, y1 =
495 if conf.presentation
496 then
497 let ips = calcips h in
498 let y0 = vy - ips in
499 let y1 = vy + h + ips in
500 y0, y1
501 else (
502 if n = 0
503 then 0, vy + h + conf.interpagespace
504 else
505 let y0 = vy - conf.interpagespace in
506 y0, y0 + h + conf.interpagespace
509 if y >= y0 && y < y1
510 then (
511 if c = 1
512 then n
513 else (
514 if n > coverA
515 then
516 if n < state.pagecount - coverB
517 then ((n-coverA)/c)*c + coverA
518 else n
519 else n
522 else (
523 if y > y0
524 then bsearch (n+1) nmax
525 else bsearch nmin (n-1)
528 bsearch 0 (state.pagecount-1);
531 let calcheight () =
532 match conf.columns with
533 | Cmulti ((_, _, _) as cl, b) ->
534 if Array.length b > 0
535 then
536 let y, h = rowyh cl b (Array.length b - 1) in
537 y + h + (if conf.presentation then calcips h else 0)
538 else 0
539 | Csingle b ->
540 if Array.length b > 0
541 then
542 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
543 y + h + (if conf.presentation then calcips h else 0)
544 else 0
545 | Csplit (_, b) ->
546 if Array.length b > 0
547 then
548 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
549 y + h
550 else 0
553 let getpageywh pageno =
554 let pageno = bound pageno 0 (state.pagecount-1) in
555 match conf.columns with
556 | Csingle b ->
557 if Array.length b = 0
558 then 0, 0, 0
559 else
560 let (_, _, y, (_, w, h, _)) = b.(pageno) in
561 let y =
562 if conf.presentation
563 then y - calcips h
564 else y
566 y, w, h
567 | Cmulti (cl, b) ->
568 if Array.length b = 0
569 then 0, 0, 0
570 else
571 let y, h = rowyh cl b pageno in
572 let (_, _, _, (_, w, _, _)) = b.(pageno) in
573 let y =
574 if conf.presentation
575 then y - calcips h
576 else y
578 y, w, h
579 | Csplit (c, b) ->
580 if Array.length b = 0
581 then 0, 0, 0
582 else
583 let n = pageno*c in
584 let (_, _, y, (_, w, h, _)) = b.(n) in
585 y, w / c, h
588 let getpageyh pageno =
589 let y,_,h = getpageywh pageno in
590 y, h;
593 let getpagedim pageno =
594 let rec f ppdim l =
595 match l with
596 | (n, _, _, _) as pdim :: rest ->
597 if n >= pageno
598 then (if n = pageno then pdim else ppdim)
599 else f pdim rest
601 | [] -> ppdim
603 f (-1, -1, -1, -1) state.pdims
606 let getpdimno pageno =
607 let rec f p l =
608 let np = succ p in
609 match l with
610 | (n, _, _, _) :: rest ->
611 if n >= pageno
612 then (if n = pageno then np else p)
613 else f np rest
615 | [] -> p
617 f ~-1 state.pdims
620 let getpagey pageno = fst (getpageyh pageno);;
622 let getanchor1 l =
623 let top =
624 let coloff = l.pagecol * l.pageh in
625 float (l.pagey + coloff) /. float l.pageh
627 let dtop =
628 if l.pagedispy = 0
629 then 0.0
630 else (
631 if conf.presentation
632 then float l.pagedispy /. float (calcips l.pageh)
633 else float l.pagedispy /. float conf.interpagespace
636 (l.pageno, top, dtop)
639 let getanchor () =
640 match state.layout with
641 | l :: _ -> getanchor1 l
642 | [] ->
643 let n = page_of_y state.y in
644 if n = -1
645 then state.anchor
646 else
647 let y, h = getpageyh n in
648 let dy = y - state.y in
649 let dtop =
650 if conf.presentation
651 then
652 let ips = calcips h in
653 float (dy + ips) /. float ips
654 else
655 float dy /. float conf.interpagespace
657 (n, 0.0, dtop)
660 let fontpath = ref E.s;;
662 type historder = [ `lastvisit | `title | `path | `file ];;
664 module KeyMap =
665 Map.Make (struct type t = (int * int) let compare = compare end);;
667 let unentS s =
668 let l = String.length s in
669 let b = Buffer.create l in
670 Parser.unent b s 0 l;
671 Buffer.contents b;
674 let home =
675 try Sys.getenv "HOME"
676 with exn ->
677 dolog "cannot determine home directory location: %s" @@ exntos exn;
681 let modifier_of_string = function
682 | "alt" -> Wsi.altmask
683 | "shift" -> Wsi.shiftmask
684 | "ctrl" | "control" -> Wsi.ctrlmask
685 | "meta" -> Wsi.metamask
686 | _ -> 0
689 let keys_of_string s =
690 let key_of_string r s =
691 let elems = Str.full_split r s in
692 let f n k m =
693 let g s =
694 let m1 = modifier_of_string s in
695 if m1 = 0
696 then (Wsi.namekey s, m)
697 else (k, m lor m1)
698 in function
699 | Str.Delim s when n land 1 = 0 -> g s
700 | Str.Text s -> g s
701 | Str.Delim _ -> (k, m)
703 let rec loop n k m = function
704 | [] -> (k, m)
705 | x :: xs ->
706 let k, m = f n k m x in
707 loop (n+1) k m xs
709 loop 0 0 0 elems
711 let elems = Str.split Utils.Re.whitespace s in
712 List.map (key_of_string (Str.regexp "-")) elems
715 let config_of c attrs =
716 let apply c k v =
718 match k with
719 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
720 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
721 | "case-insensitive-search" -> { c with icase = bool_of_string v }
722 | "preload" -> { c with preload = bool_of_string v }
723 | "page-bias" -> { c with pagebias = int_of_string v }
724 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
725 | "horizontal-scroll-step" ->
726 { c with hscrollstep = max (int_of_string v) 1 }
727 | "auto-scroll-step" ->
728 { c with autoscrollstep = max 0 (int_of_string v) }
729 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
730 | "highlight-links" -> { c with hlinks = bool_of_string v }
731 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
732 | "vertical-margin" ->
733 { c with interpagespace = max 0 (int_of_string v) }
734 | "zoom" ->
735 let zoom = float_of_string v /. 100. in
736 let zoom = max zoom 0.0 in
737 { c with zoom = zoom }
738 | "presentation" -> { c with presentation = bool_of_string v }
739 | "rotation-angle" -> { c with angle = int_of_string v }
740 | "width" -> { c with cwinw = max 20 (int_of_string v) }
741 | "height" -> { c with cwinh = max 20 (int_of_string v) }
742 | "proportional-display" ->
743 let fm =
744 if bool_of_string v
745 then FitProportional
746 else FitWidth
748 { c with fitmodel = fm }
749 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
750 | "pixmap-cache-size" ->
751 { c with memlimit = max 2 (int_of_string_with_suffix v) }
752 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
753 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
754 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
755 | "background-color" -> { c with bgcolor = color_of_string v }
756 | "scrollbar-color" -> { c with sbarcolor = rgba_of_string v }
757 | "scrollbar-handle-color" -> { c with sbarhndlcolor = rgba_of_string v }
758 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
759 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
760 | "mupdf-store-size" ->
761 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
762 | "checkers" -> { c with checkers = bool_of_string v }
763 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
764 | "trim-margins" -> { c with trimmargins = bool_of_string v }
765 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
766 | "uri-launcher" -> { c with urilauncher = unentS v }
767 | "path-launcher" -> { c with pathlauncher = unentS v }
768 | "color-space" -> { c with colorspace = CSTE.of_string v }
769 | "invert-colors" -> { c with invert = bool_of_string v }
770 | "brightness" -> { c with colorscale = float_of_string v }
771 | "columns" ->
772 let (n, _, _) as nab = multicolumns_of_string v in
773 if n < 0
774 then { c with columns = Csplit (-n, E.a) }
775 else { c with columns = Cmulti (nab, E.a) }
776 | "birds-eye-columns" ->
777 { c with beyecolumns = Some (max (int_of_string v) 2) }
778 | "selection-command" -> { c with selcmd = unentS v }
779 | "synctex-command" -> { c with stcmd = unentS v }
780 | "pax-command" -> { c with paxcmd = unentS v }
781 | "askpass-command" -> { c with passcmd = unentS v }
782 | "savepath-command" -> { c with savecmd = unentS v }
783 | "update-cursor" -> { c with updatecurs = bool_of_string v }
784 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
785 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
786 | "use-pbo" -> { c with usepbo = bool_of_string v }
787 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
788 | "horizontal-scrollbar-visible" ->
789 let b =
790 if bool_of_string v
791 then c.scrollb lor scrollbhv
792 else c.scrollb land (lnot scrollbhv)
794 { c with scrollb = b }
795 | "vertical-scrollbar-visible" ->
796 let b =
797 if bool_of_string v
798 then c.scrollb lor scrollbvv
799 else c.scrollb land (lnot scrollbvv)
801 { c with scrollb = b }
802 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
803 | "point-and-x" ->
804 { c with pax =
805 if bool_of_string v
806 then Some 0.0
807 else None }
808 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
809 | "scroll-bar-on-the-left" -> { c with leftscroll = bool_of_string v }
810 | "title" -> { c with title = unentS v }
811 | "last-visit" -> { c with lastvisit = float_of_string v }
812 | "edit-annotations-inline" -> { c with annotinline = bool_of_string v }
813 | "coarse-presentation-positioning" ->
814 { c with coarseprespos = bool_of_string v }
815 | "use-document-css" -> { c with usedoccss = bool_of_string v }
816 | _ -> c
817 with exn ->
818 dolog "error processing attribute (`%S' = `%S'): %s" k v @@ exntos exn;
821 let rec fold c = function
822 | [] -> c
823 | (k, v) :: rest ->
824 let c = apply c k v in
825 fold c rest
827 fold { c with keyhashes = copykeyhashes c } attrs;
830 let fromstring f pos n v d =
831 try f v
832 with exn ->
833 dolog "error processing attribute (%S=%S) at %d\n%s" n v pos @@ exntos exn;
837 let bookmark_of attrs =
838 let rec fold title page rely visy = function
839 | ("title", v) :: rest -> fold v page rely visy rest
840 | ("page", v) :: rest -> fold title v rely visy rest
841 | ("rely", v) :: rest -> fold title page v visy rest
842 | ("visy", v) :: rest -> fold title page rely v rest
843 | _ :: rest -> fold title page rely visy rest
844 | [] -> title, page, rely, visy
846 fold "invalid" "0" "0" "0" attrs
849 let doc_of attrs =
850 let rec fold path key page rely pan visy origin = function
851 | ("path", v) :: rest -> fold v key page rely pan visy origin rest
852 | ("key", v) :: rest -> fold path v page rely pan visy origin rest
853 | ("page", v) :: rest -> fold path key v rely pan visy origin rest
854 | ("rely", v) :: rest -> fold path key page v pan visy origin rest
855 | ("pan", v) :: rest -> fold path key page rely v visy origin rest
856 | ("visy", v) :: rest -> fold path key page rely pan v origin rest
857 | ("origin", v) :: rest -> fold path key page rely pan visy v rest
858 | _ :: rest -> fold path key page rely pan visy origin rest
859 | [] -> path, key, page, rely, pan, visy, origin
861 fold E.s E.s "0" "0" "0" "0" E.s attrs
864 let map_of attrs =
865 let rec fold rs ls = function
866 | ("out", v) :: rest -> fold v ls rest
867 | ("in", v) :: rest -> fold rs v rest
868 | _ :: rest -> fold ls rs rest
869 | [] -> ls, rs
871 fold E.s E.s attrs
874 let findkeyhash c name =
875 try List.assoc name c.keyhashes
876 with Not_found -> error "invalid mode name `%s'" name
879 let get s =
880 let open Parser in
881 let h = Hashtbl.create 10 in
882 let dc = { defconf with angle = defconf.angle } in
883 let rec toplevel v t spos _ =
884 match t with
885 | Vdata | Vcdata | Vend -> v
886 | Vopen ("llppconfig", _, closed) ->
887 if closed
888 then v
889 else { v with f = llppconfig }
890 | Vopen _ -> parse_error "unexpected subelement at top level" s spos
891 | Vclose _ -> parse_error "unexpected close at top level" s spos
893 and llppconfig v t spos _ =
894 match t with
895 | Vdata | Vcdata -> v
896 | Vend -> parse_error "unexpected end of input in llppconfig" s spos
897 | Vopen ("defaults", attrs, closed) ->
898 let c = config_of dc attrs in
899 setconf dc c;
900 if closed
901 then v
902 else { v with f = defaults }
904 | Vopen ("ui-font", attrs, closed) ->
905 let rec getsize size = function
906 | [] -> size
907 | ("size", v) :: rest ->
908 let size =
909 fromstring int_of_string spos "size" v fstate.fontsize in
910 getsize size rest
911 | l -> getsize size l
913 fstate.fontsize <- getsize fstate.fontsize attrs;
914 if closed
915 then v
916 else { v with f = uifont (Buffer.create 10) }
918 | Vopen ("doc", attrs, closed) ->
919 let pathent, key, spage, srely, span, svisy, origin = doc_of attrs in
920 let path = unentS pathent
921 and origin = unentS origin
922 and pageno = fromstring int_of_string spos "page" spage 0
923 and rely = fromstring float_of_string spos "rely" srely 0.0
924 and pan = fromstring int_of_string spos "pan" span 0
925 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
926 let c = config_of dc attrs in
927 c.key <- key;
928 let anchor = (pageno, rely, visy) in
929 if closed
930 then (Hashtbl.add h path (c, [], pan, anchor, origin); v)
931 else { v with f = doc path origin pan anchor c [] }
933 | Vopen _ ->
934 parse_error "unexpected subelement in llppconfig" s spos
936 | Vclose "llppconfig" -> { v with f = toplevel }
937 | Vclose _ -> parse_error "unexpected close in llppconfig" s spos
939 and defaults v t spos _ =
940 match t with
941 | Vdata | Vcdata -> v
942 | Vend -> parse_error "unexpected end of input in defaults" s spos
943 | Vopen ("keymap", attrs, closed) ->
944 let modename =
945 try List.assoc "mode" attrs
946 with Not_found -> "global" in
947 if closed
948 then v
949 else
950 let ret keymap =
951 let h = findkeyhash dc modename in
952 KeyMap.iter (Hashtbl.replace h) keymap;
953 defaults
955 { v with f = pkeymap ret KeyMap.empty }
957 | Vopen (_, _, _) ->
958 parse_error "unexpected subelement in defaults" s spos
960 | Vclose "defaults" ->
961 { v with f = llppconfig }
963 | Vclose _ -> parse_error "unexpected close in defaults" s spos
965 and uifont b v t spos epos =
966 match t with
967 | Vdata | Vcdata ->
968 Buffer.add_substring b s spos (epos - spos);
970 | Vopen (_, _, _) ->
971 parse_error "unexpected subelement in ui-font" s spos
972 | Vclose "ui-font" ->
973 if emptystr !fontpath
974 then fontpath := Buffer.contents b;
975 { v with f = llppconfig }
976 | Vclose _ -> parse_error "unexpected close in ui-font" s spos
977 | Vend -> parse_error "unexpected end of input in ui-font" s spos
979 and doc path origin pan anchor c bookmarks v t spos _ =
980 match t with
981 | Vdata | Vcdata -> v
982 | Vend -> parse_error "unexpected end of input in doc" s spos
983 | Vopen ("bookmarks", _, closed) ->
984 if closed
985 then v
986 else { v with f = pbookmarks path origin pan anchor c bookmarks }
988 | Vopen ("keymap", attrs, closed) ->
989 let modename =
990 try List.assoc "mode" attrs
991 with Not_found -> "global"
993 if closed
994 then v
995 else
996 let ret keymap =
997 let h = findkeyhash c modename in
998 KeyMap.iter (Hashtbl.replace h) keymap;
999 doc path origin pan anchor c bookmarks
1001 { v with f = pkeymap ret KeyMap.empty }
1003 | Vopen ("css", [], false) ->
1004 { v with f = pcss path origin pan anchor c bookmarks }
1006 | Vopen (_, _, _) ->
1007 parse_error "unexpected subelement in doc" s spos
1009 | Vclose "doc" ->
1010 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor, origin);
1011 { v with f = llppconfig }
1013 | Vclose _ -> parse_error "unexpected close in doc" s spos
1015 and pcss path origin pan anchor c bookmarks v t spos epos =
1016 match t with
1017 | Vdata | Vcdata ->
1018 let b = Buffer.create 10 in
1019 Buffer.add_substring b s spos (epos - spos);
1020 { v with f = pcss path origin pan anchor
1021 { c with css = Buffer.contents b }
1022 bookmarks }
1023 | Vend -> parse_error "unexpected end of input in css" s spos
1024 | Vopen _ -> parse_error "unexpected subelement in css" s spos
1025 | Vclose "css" -> { v with f = doc path origin pan anchor c bookmarks }
1026 | Vclose _ -> parse_error "unexpected close in css" s spos
1028 and pkeymap ret keymap v t spos _ =
1029 match t with
1030 | Vdata | Vcdata -> v
1031 | Vend -> parse_error "unexpected end of input in keymap" s spos
1032 | Vopen ("map", attrs, closed) ->
1033 let r, l = map_of attrs in
1034 let kss = fromstring keys_of_string spos "in" r [] in
1035 let lss = fromstring keys_of_string spos "out" l [] in
1036 let keymap =
1037 match kss with
1038 | [] -> keymap
1039 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
1040 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
1042 if closed
1043 then { v with f = pkeymap ret keymap }
1044 else
1045 let f () = v in
1046 { v with f = skip "map" f }
1048 | Vopen _ ->
1049 parse_error "unexpected subelement in keymap" s spos
1051 | Vclose "keymap" ->
1052 { v with f = ret keymap }
1054 | Vclose _ -> parse_error "unexpected close in keymap" s spos
1056 and pbookmarks path origin pan anchor c bookmarks v t spos _ =
1057 match t with
1058 | Vdata | Vcdata -> v
1059 | Vend -> parse_error "unexpected end of input in bookmarks" s spos
1060 | Vopen ("item", attrs, closed) ->
1061 let titleent, spage, srely, svisy = bookmark_of attrs in
1062 let page = fromstring int_of_string spos "page" spage 0
1063 and rely = fromstring float_of_string spos "rely" srely 0.0
1064 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1065 let bookmarks =
1066 (unentS titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
1068 if closed
1069 then { v with f = pbookmarks path origin pan anchor c bookmarks }
1070 else
1071 let f () = v in
1072 { v with f = skip "item" f }
1074 | Vopen _ ->
1075 parse_error "unexpected subelement in bookmarks" s spos
1077 | Vclose "bookmarks" ->
1078 { v with f = doc path origin pan anchor c bookmarks }
1080 | Vclose _ -> parse_error "unexpected close in bookmarks" s spos
1082 and skip tag f v t spos _ =
1083 match t with
1084 | Vdata | Vcdata -> v
1085 | Vend ->
1086 parse_error ("unexpected end of input in skipped " ^ tag) s spos
1087 | Vopen (tag', _, closed) ->
1088 if closed
1089 then v
1090 else
1091 let f' () = { v with f = skip tag f } in
1092 { v with f = skip tag' f' }
1093 | Vclose ctag ->
1094 if tag = ctag
1095 then f ()
1096 else parse_error ("unexpected close in skipped " ^ tag) s spos
1099 parse { f = toplevel; accu = () } s;
1100 h, dc;
1103 let do_load f contents =
1104 try f contents
1105 with
1106 | Parser.Parse_error (msg, s, pos) ->
1107 let subs = Parser.subs s pos in
1108 Utils.error "parse error: %s: at %d [..%S..]" msg pos subs
1110 | exn -> Utils.error "parse error: %s" @@ exntos exn
1113 let defconfpath =
1114 let dir =
1115 let xdgconfdir = Utils.getenvwithdef "XDG_CONFIG_HOME" E.s in
1116 if emptystr xdgconfdir
1117 then
1119 let dir = Filename.concat home ".config" in
1120 if Sys.is_directory dir then dir else home
1121 with _ -> home
1122 else xdgconfdir
1124 Filename.concat dir "llpp.conf"
1127 let confpath = ref defconfpath;;
1129 let load2 f default =
1130 match filecontents !confpath with
1131 | contents -> f @@ do_load get contents
1132 | exception Unix.Unix_error (Unix.ENOENT, "open", _) ->
1133 f (Hashtbl.create 0, defconf)
1134 | exception exn ->
1135 dolog "error loading configuration from `%S': %s" !confpath @@ exntos exn;
1136 default
1139 let load1 f = load2 f false;;
1141 let load openlast =
1142 let f (h, dc) =
1143 if openlast
1144 then (
1145 let path, _ =
1146 Hashtbl.fold
1147 (fun path (conf, _, _, _, _) ((_, besttime) as best) ->
1148 if conf.lastvisit > besttime
1149 then (path, conf.lastvisit)
1150 else best)
1152 (state.path, -.infinity)
1154 state.path <- path;
1156 let pc, pb, px, pa, po =
1157 let def = dc, [], 0, emptyanchor, state.origin in
1158 if emptystr state.path
1159 then def
1160 else
1161 let absname = abspath state.path in
1162 match Hashtbl.find h absname with
1163 | (c,b,x,a,_) -> (c,b,x,a,state.origin)
1164 | exception Not_found ->
1165 let exception E of (conf * outline list * int * anchor * string) in
1166 let key = try Digest.file absname |> Digest.to_hex with _ -> E.s in
1167 match (
1168 if nonemptystr key
1169 then
1170 Hashtbl.iter (fun p ((c, _, _, _, _) as v) ->
1171 if c.key = key
1172 then (
1173 dolog "will use %s's settings due to matching keys" p;
1174 raise (E v)
1178 with
1179 | _ -> def
1180 | exception E v -> v
1182 setconf defconf dc;
1183 setconf conf pc;
1184 state.bookmarks <- pb;
1185 state.x <- px;
1186 state.origin <- po;
1187 state.anchor <- pa;
1188 cbput state.hists.nav pa;
1189 true
1191 load1 f
1194 let gethist () =
1195 let f (h, _) =
1196 Hashtbl.fold (fun path (pc, pb, px, pa, po) accu ->
1197 (path, pc, pb, px, pa, po) :: accu)
1198 h [];
1200 load2 f []
1203 let add_attrs bb always dc c time =
1204 let o' fmt s =
1205 Buffer.add_string bb "\n ";
1206 Printf.bprintf bb fmt s
1208 let o c fmt s = if c then o' fmt s else ignore in
1209 let ob s a b = o (always || a != b) "%s='%b'" s a
1210 and op s a b = o (always || a <> b) "%s='%b'" s (a != None)
1211 and oi s a b = o (always || a != b) "%s='%d'" s a
1212 and oI s a b = o (always || a != b) "%s='%s'" s (string_with_suffix_of_int a)
1213 and oz s a b = o (always || a <> b) "%s='%g'" s (a*.100.)
1214 and oF s a b = o (always || a <> b) "%s='%f'" s a
1215 and oL s a b = o (always || a <> b) "%s='%Ld'" s a
1216 and oc s a b = o (always || a <> b) "%s='%s'" s (color_to_string a)
1217 and oA s a b = o (always || a <> b) "%s='%s'" s (rgba_to_string a)
1218 and oC s a b = o (always || a <> b) "%s='%s'" s (CSTE.to_string a)
1219 and oR s a b = o (always || a <> b) "%s='%s'" s (irect_to_string a)
1220 and oFm s a b = o (always || a <> b) "%s='%s'" s (FMTE.to_string a)
1221 and oSv s a b m =
1222 o (always || a land m <> b land m) "%s='%b'" s (a land m != 0)
1223 and oPm s a b = o (always || a <> b) "%s='%s'" s (MTE.to_string a)
1224 and os s a b =
1225 o (always || a <> b) "%s='%s'" s @@ Parser.enent a 0 (String.length a)
1226 and oco s a b =
1227 if always || a <> b
1228 then
1229 match a with
1230 | Cmulti ((n, a, b), _) when n > 1 -> o' "%s='%d,%d,%d'" s n a b
1231 | Csplit (n, _) when n > 1 -> o' "%s='%d'" s ~-n
1232 | Cmulti _ | Csplit _ | Csingle _ -> ()
1233 and obeco s a b =
1234 if always || a <> b
1235 then
1236 match a with
1237 | Some c when c > 1 -> o' "%s='%d'" s c
1238 | _ -> ()
1240 oi "width" c.cwinw dc.cwinw;
1241 oi "height" c.cwinh dc.cwinh;
1242 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
1243 oi "scroll-handle-height" c.scrollh dc.scrollh;
1244 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
1245 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
1246 ob "case-insensitive-search" c.icase dc.icase;
1247 ob "preload" c.preload dc.preload;
1248 oi "page-bias" c.pagebias dc.pagebias;
1249 oi "scroll-step" c.scrollstep dc.scrollstep;
1250 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
1251 ob "max-height-fit" c.maxhfit dc.maxhfit;
1252 ob "highlight-links" c.hlinks dc.hlinks;
1253 ob "under-cursor-info" c.underinfo dc.underinfo;
1254 oi "vertical-margin" c.interpagespace dc.interpagespace;
1255 oz "zoom" c.zoom dc.zoom;
1256 ob "presentation" c.presentation dc.presentation;
1257 oi "rotation-angle" c.angle dc.angle;
1258 oFm "fit-model" c.fitmodel dc.fitmodel;
1259 oI "pixmap-cache-size" c.memlimit dc.memlimit;
1260 oi "tex-count" c.texcount dc.texcount;
1261 oi "slice-height" c.sliceheight dc.sliceheight;
1262 oi "thumbnail-width" c.thumbw dc.thumbw;
1263 oc "background-color" c.bgcolor dc.bgcolor;
1264 oA "scrollbar-color" c.sbarcolor dc.sbarcolor;
1265 oA "scrollbar-handle-color" c.sbarhndlcolor dc.sbarhndlcolor;
1266 oi "tile-width" c.tilew dc.tilew;
1267 oi "tile-height" c.tileh dc.tileh;
1268 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
1269 ob "checkers" c.checkers dc.checkers;
1270 oi "aalevel" c.aalevel dc.aalevel;
1271 ob "trim-margins" c.trimmargins dc.trimmargins;
1272 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
1273 os "uri-launcher" c.urilauncher dc.urilauncher;
1274 os "path-launcher" c.pathlauncher dc.pathlauncher;
1275 oC "color-space" c.colorspace dc.colorspace;
1276 ob "invert-colors" c.invert dc.invert;
1277 oF "brightness" c.colorscale dc.colorscale;
1278 oco "columns" c.columns dc.columns;
1279 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
1280 os "selection-command" c.selcmd dc.selcmd;
1281 os "synctex-command" c.stcmd dc.stcmd;
1282 os "pax-command" c.paxcmd dc.paxcmd;
1283 os "askpass-command" c.passcmd dc.passcmd;
1284 os "savepath-command" c.savecmd dc.savecmd;
1285 ob "update-cursor" c.updatecurs dc.updatecurs;
1286 oi "hint-font-size" c.hfsize dc.hfsize;
1287 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
1288 oF "page-scroll-scale" c.pgscale dc.pgscale;
1289 ob "use-pbo" c.usepbo dc.usepbo;
1290 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
1291 ob "remote-in-a-new-instance" c.riani dc.riani;
1292 op "point-and-x" c.pax dc.pax;
1293 oPm "point-and-x-mark" c.paxmark dc.paxmark;
1294 ob "scroll-bar-on-the-left" c.leftscroll dc.leftscroll;
1295 if not always
1296 then os "title" c.title dc.title;
1297 oL "last-visit" (Int64.of_float time) 0L;
1298 ob "edit-annotations-inline" c.annotinline dc.annotinline;
1299 ob "coarse-presentation-positioning" c.coarseprespos dc.coarseprespos;
1300 ob "use-document-css" c.usedoccss dc.usedoccss;
1303 let keymapsbuf always dc c =
1304 let open Buffer in
1305 let bb = create 16 in
1306 let rec loop = function
1307 | [] -> ()
1308 | (modename, h) :: rest ->
1309 let dh = findkeyhash dc modename in
1310 if always || h <> dh
1311 then (
1312 if Hashtbl.length h > 0
1313 then (
1314 if length bb > 0 then add_char bb '\n';
1315 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
1316 Hashtbl.iter (fun i o ->
1317 if always || match Hashtbl.find dh i
1318 with | dO -> dO <> o | exception Not_found -> false
1319 then
1320 let addkm (k, m) =
1321 if Wsi.withctrl m then add_string bb "ctrl-";
1322 if Wsi.withalt m then add_string bb "alt-";
1323 if Wsi.withshift m then add_string bb "shift-";
1324 if Wsi.withmeta m then add_string bb "meta-";
1325 add_string bb (Wsi.keyname k);
1327 let addkms l =
1328 let rec loop = function
1329 | [] -> ()
1330 | km :: [] -> addkm km
1331 | km :: rest -> addkm km; add_char bb ' '; loop rest
1333 loop l
1335 add_string bb "<map in='";
1336 addkm i;
1337 match o with
1338 | KMinsrt km ->
1339 add_string bb "' out='"; addkm km; add_string bb "'/>\n"
1341 | KMinsrl kms ->
1342 add_string bb "' out='"; addkms kms; add_string bb "'/>\n"
1344 | KMmulti (ins, kms) ->
1345 add_char bb ' '; addkms ins; add_string bb "' out='";
1346 addkms kms; add_string bb "'/>\n"
1347 ) h;
1348 add_string bb "</keymap>";
1351 loop rest
1353 loop c.keyhashes;
1357 let keystostrlist c =
1358 let rec loop accu = function
1359 | [] -> accu
1360 | (modename, h) :: rest ->
1361 let accu =
1362 if Hashtbl.length h > 0
1363 then (
1364 let accu = Printf.sprintf "\xc2\xb7Keys for %s" modename :: accu in
1365 Hashtbl.fold (fun i o a ->
1366 let bb = Buffer.create 10 in
1367 let addkm (k, m) =
1368 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
1369 if Wsi.withalt m then Buffer.add_string bb "alt-";
1370 if Wsi.withshift m then Buffer.add_string bb "shift-";
1371 if Wsi.withmeta m then Buffer.add_string bb "meta-";
1372 Buffer.add_string bb (Wsi.keyname k);
1374 let addkms l =
1375 let rec loop = function
1376 | [] -> ()
1377 | km :: [] -> addkm km
1378 | km :: rest ->
1379 addkm km; Buffer.add_char bb ' ';
1380 loop rest
1382 loop l
1384 addkm i;
1385 Buffer.add_char bb '\t';
1386 begin match o with
1387 | KMinsrt km ->
1388 addkm km
1390 | KMinsrl kms ->
1391 addkms kms
1393 | KMmulti (ins, kms) ->
1394 Buffer.add_char bb ' ';
1395 addkms ins;
1396 Buffer.add_string bb "\t";
1397 addkms kms
1398 end;
1399 Buffer.contents bb :: a
1400 ) h accu
1402 else accu
1404 loop accu rest
1406 loop [] c.keyhashes
1409 let save1 bb leavebirdseye x h dc =
1410 let uifontsize = fstate.fontsize in
1411 let dc = if conf.bedefault then conf else dc in
1412 Buffer.add_string bb "<llppconfig>\n";
1414 if nonemptystr !fontpath
1415 then Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
1416 uifontsize
1417 !fontpath
1418 else
1419 if uifontsize <> 14
1420 then Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
1423 Buffer.add_string bb "<defaults";
1424 add_attrs bb true dc dc nan;
1425 let kb = keymapsbuf true dc dc in
1426 if Buffer.length kb > 0
1427 then (
1428 Buffer.add_string bb ">\n";
1429 Buffer.add_buffer bb kb;
1430 Buffer.add_string bb "\n</defaults>\n";
1432 else Buffer.add_string bb "/>\n";
1434 let adddoc path pan anchor c bookmarks time origin =
1435 if not (bookmarks == [] && c = dc && anchor = emptyanchor)
1436 then (
1437 Printf.bprintf bb "<doc path='%s'"
1438 (Parser.enent path 0 (String.length path));
1440 if nonemptystr c.key
1441 then Printf.bprintf bb "\n key='%s'" c.key;
1443 if nonemptystr origin
1444 then Printf.bprintf bb "\n origin='%s'"
1445 (Parser.enent origin 0 (String.length origin));
1447 if anchor <> emptyanchor
1448 then (
1449 let n, rely, visy = anchor in
1450 Printf.bprintf bb "\n page='%d'" n;
1452 if rely > 1e-6
1453 then Printf.bprintf bb " rely='%f'" rely;
1455 if abs_float visy > 1e-6
1456 then Printf.bprintf bb " visy='%f'" visy;
1459 if pan != 0
1460 then Printf.bprintf bb " pan='%d'" pan;
1462 add_attrs bb false dc c time;
1463 if nonemptystr c.css
1464 then Printf.bprintf bb ">\n <css><![CDATA[%s]]></css>" c.css;
1465 let kb = keymapsbuf false dc c in
1467 begin match bookmarks with
1468 | [] ->
1469 if Buffer.length kb > 0
1470 then (
1471 Buffer.add_string bb ">\n";
1472 Buffer.add_buffer bb kb;
1473 Buffer.add_string bb "\n</doc>\n";
1475 else
1476 if nonemptystr c.css
1477 then Buffer.add_string bb "\n</doc>\n"
1478 else Buffer.add_string bb "/>\n"
1479 | _ ->
1480 Buffer.add_string bb ">\n<bookmarks>\n";
1481 List.iter (fun (title, _, kind) ->
1482 begin match kind with
1483 | Oanchor (page, rely, visy) ->
1484 Printf.bprintf bb
1485 "<item title='%s' page='%d'"
1486 (Parser.enent title 0 (String.length title))
1487 page;
1488 if rely > 1e-6
1489 then Printf.bprintf bb " rely='%f'" rely;
1491 if abs_float visy > 1e-6
1492 then Printf.bprintf bb " visy='%f'" visy;
1493 | Ohistory _ | Onone | Ouri _ | Oremote _
1494 | Oremotedest _ | Olaunch _ -> error "unexpected link in bookmarks"
1495 end;
1496 Buffer.add_string bb "/>\n";
1497 ) bookmarks;
1498 Buffer.add_string bb "</bookmarks>";
1499 if Buffer.length kb > 0
1500 then (
1501 Buffer.add_string bb "\n";
1502 Buffer.add_buffer bb kb;
1504 Buffer.add_string bb "\n</doc>\n";
1505 end;
1509 let pan, conf =
1510 match state.mode with
1511 | Birdseye (c, pan, _, _, _) ->
1512 let beyecolumns =
1513 match conf.columns with
1514 | Cmulti ((c, _, _), _) -> Some c
1515 | Csingle _ -> None
1516 | Csplit _ -> None
1517 and columns =
1518 match c.columns with
1519 | Cmulti (c, _) -> Cmulti (c, E.a)
1520 | Csingle _ -> Csingle E.a
1521 | Csplit _ -> failwith "quit from bird's eye while split"
1523 pan, { c with beyecolumns = beyecolumns; columns = columns }
1524 | Textentry _
1525 | View
1526 | LinkNav _ -> x, conf
1528 let docpath = if nonemptystr state.path then abspath state.path else E.s in
1529 if nonemptystr docpath
1530 then (
1531 adddoc docpath pan (getanchor ())
1533 let autoscrollstep =
1534 match state.autoscroll with
1535 | Some step -> step
1536 | None -> conf.autoscrollstep
1538 begin match state.mode with
1539 | Birdseye beye -> leavebirdseye beye true
1540 | Textentry _
1541 | View
1542 | LinkNav _ -> ()
1543 end;
1544 let key = try Digest.file docpath |> Digest.to_hex
1545 with _ -> E.s in
1546 { conf with autoscrollstep; key }
1548 state.bookmarks
1549 (now ())
1550 state.origin
1552 Hashtbl.iter (fun path (c, bookmarks, x, anchor, origin) ->
1553 if docpath <> abspath path
1554 then adddoc path x anchor c bookmarks c.lastvisit origin
1555 ) h;
1556 Buffer.add_string bb "</llppconfig>\n";
1557 true;
1560 let save leavebirdseye =
1561 let relx = float state.x /. float state.winw in
1562 let w, h, x =
1563 let cx w = truncate (relx *. float w) in
1564 List.fold_left
1565 (fun (w, h, x) ws ->
1566 match ws with
1567 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
1568 | Wsi.MaxVert -> (w, conf.cwinh, x)
1569 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
1571 (state.winw, state.winh, state.x) state.winstate
1573 conf.cwinw <- w;
1574 conf.cwinh <- h;
1575 let bb = Buffer.create 32768 in
1576 let save2 (h, dc) =
1577 save1 bb leavebirdseye x h dc
1579 if load1 save2 && Buffer.length bb > 0
1580 then
1582 let tmp = !confpath ^ ".tmp" in
1583 let oc = open_out_bin tmp in
1584 Buffer.output_buffer oc bb;
1585 close_out oc;
1586 Unix.rename tmp !confpath;
1587 with exn ->
1588 dolog "error saving configuration: %s" @@ exntos exn
1591 let gc () =
1592 let href = ref @@ Hashtbl.create 0 in
1593 let cref = ref defconf in
1594 let push (h, dc) =
1595 let f path v =
1596 if Sys.file_exists path
1597 then Some v
1598 else (dolog "removing entry for '%s'" path; None) in
1599 Hashtbl.filter_map_inplace f h;
1600 href := h;
1601 cref := dc;
1602 true
1604 ignore (load1 push);
1605 let bb = Buffer.create 32768 in
1606 let save2 (_h, dc) = save1 bb (fun _ _ -> ()) 0 !href dc in
1607 if load1 save2 && Buffer.length bb > 0
1608 then (
1610 let tmp = !confpath ^ ".tmp" in
1611 let oc = open_out_bin tmp in
1612 Buffer.output_buffer oc bb;
1613 close_out oc;
1614 Unix.rename tmp !confpath;
1615 with exn ->
1616 dolog "error saving configuration: %s" @@ exntos exn
1620 let logcurrently = function
1621 | Idle -> dolog "Idle"
1622 | Loading (l, gen) ->
1623 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1624 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1625 dolog "Tiling %d[%d,%d] page=%s cs=%s angle=%d"
1626 l.pageno col row (~> pageopaque)
1627 (CSTE.to_string colorspace) angle;
1628 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1629 angle gen conf.angle state.gen
1630 tilew tileh
1631 conf.tilew conf.tileh
1632 | Outlining _ -> dolog "outlining"
1635 let logpage l =
1636 dolog {|l %d dim=%d {
1637 WxH %dx%d
1638 vWxH %dx%d
1639 pagex,y %d,%d
1640 dispx,y %d,%d
1641 column %d
1643 l.pageno l.pagedimno
1644 l.pagew l.pageh
1645 l.pagevw l.pagevh
1646 l.pagex l.pagey
1647 l.pagedispx l.pagedispy
1648 l.pagecol