Use OCaml 4.14 beta1
[llpp.git] / config.ml
blob6c28f9a28c47d998d866c7a38296a8ccbebe2679
1 open Utils
3 let irect_of_string s =
4 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
6 let irect_to_string (x0,y0,x1,y1) = Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
8 let multicolumns_to_string (n, a, b) =
9 if a = 0 && b = 0
10 then Printf.sprintf "%d" n
11 else Printf.sprintf "%d,%d,%d" n a b
13 let multicolumns_of_string s =
14 try
15 (int_of_string s, 0, 0)
16 with _ ->
17 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
18 if a > 1 || b > 1
19 then error "subtly broken";
20 (n, a, b)
23 include Confstruct
25 type angle = int
26 and opaque = Opaque.t
27 and rectcolor = rgba
28 and pixmapsize = int
29 and gen = int
30 and top = float
31 and dtop = float
32 and fontpath = string
33 and trimmargins = bool
34 and trimparams = (trimmargins * irect)
35 and uri = string
36 and caption = string
37 and tilex = int
38 and tiley = int
39 and tileparams = (x * y * w * h * tilex * tiley)
40 and under =
41 | Unone
42 | Ulinkuri of string
43 | Utext of facename
44 | Utextannot of (opaque * slinkindex)
45 | Ufileannot of (opaque * slinkindex)
46 and slinkindex = int
47 and facename = string
48 and launchcommand = string
49 and filename = string
50 and linkno = int
51 and destname = string
52 and link =
53 | Lnotfound
54 | Lfound of int
55 and linkdir =
56 | LDfirst
57 | LDlast
58 | LDfirstvisible of (int * int * int)
59 | LDleft of int
60 | LDright of int
61 | LDdown of int
62 | LDup of int
63 and pagewithlinks =
64 | Pwlnotfound
65 | Pwl of int
66 and anchor = pageno * top * dtop
67 and rect = float * float * float * float * float * float * float * float
68 and infochange = | Memused | Docinfo | Pdim
69 and redirstderr = bool
70 and fontstate =
71 { mutable fontsize : int
72 ; mutable wwidth : float
73 ; mutable maxrows : int
76 let fstate =
77 { fontsize = Wsi.fontsizescale 20
78 ; wwidth = nan
79 ; maxrows = -1
82 class type uioh =
83 object
84 method display : unit
85 method key : int -> int -> uioh
86 method button : int -> bool -> int -> int -> int -> uioh
87 method multiclick : int -> int -> int -> int -> uioh
88 method motion : int -> int -> uioh
89 method pmotion : int -> int -> uioh
90 method infochanged : infochange -> unit
91 method scrollpw : (int * float * float)
92 method scrollph : (int * float * float)
93 method modehash : keyhash
94 method eformsgs : bool
95 method alwaysscrolly : bool
96 method scroll : int -> int -> uioh
97 method zoom : float -> int -> int -> unit
98 end
100 module type TextEnumType = sig
101 type t
102 val name : string
103 val names : string array
106 module TextEnumMake (Ten : TextEnumType) = struct
107 let names = Ten.names
108 let to_int (t : Ten.t) = Obj.magic t
109 let to_string t = names.(to_int t)
110 let of_int n : Ten.t = Obj.magic n
111 let of_string s =
112 let rec find i =
113 if i = Array.length names
114 then error "invalid %s: %s" Ten.name s
115 else (
116 if Ten.names.(i) = s
117 then of_int i
118 else find (i+1)
120 in find 0
123 module CSTE = TextEnumMake (struct
124 type t = colorspace
125 let name = "colorspace"
126 let names = [|"rgb"; "gray"|]
127 end)
129 module MTE = TextEnumMake (struct
130 type t = mark
131 let name = "mark"
132 let names = [|"page"; "block"; "line"; "word"|]
133 end)
135 module FMTE = TextEnumMake (struct
136 type t = fitmodel
137 let name = "fitmodel"
138 let names = [|"width"; "proportional"; "page"|]
139 end)
141 type outlinekind =
142 | Onone
143 | Oanchor of anchor
144 | Ouri of uri
145 | Olaunch of launchcommand
146 | Oremote of (filename * pageno)
147 | Oremotedest of (filename * destname)
148 | Ohistory of (filename * conf * outline list * x * anchor * filename)
149 and outline = (caption * outlinelevel * outlinekind)
150 and outlinelevel = int
152 type page =
153 { pageno : int
154 ; pagedimno : int
155 ; pagew : int
156 ; pageh : int
157 ; pagex : int
158 ; pagey : int
159 ; pagevw : int
160 ; pagevh : int
161 ; pagedispx : int
162 ; pagedispy : int
163 ; pagecol : int
166 type tile = opaque * pixmapsize * elapsed
167 and elapsed = float
168 and pagemapkey = pageno * gen
169 and tilemapkey = pageno * gen * colorspace * angle * w * h * col * row
170 and row = int
171 and col = int
172 and currently =
173 | Idle
174 | Loading of (page * gen)
175 | Tiling
176 of (page * opaque * colorspace * angle * gen * col * row * w * h)
177 | Outlining of outline list
178 and mpos = int * int
179 and mstate =
180 | Mnone
181 | Msel of (mpos * mpos)
182 | Mpan of mpos
183 | Mscrolly | Mscrollx
184 | Mzoom of (buttonno * step * mpos)
185 | Mzoomrect of (mpos * mpos)
186 and buttonno = int
187 and step = int
188 and mode =
189 | View
190 | Birdseye of (conf * leftx * pageno * pageno * anchor)
191 | Textentry of (textentry * onleave)
192 | LinkNav of linktarget
193 and onleave = leavetextentrystatus -> unit
194 and leavetextentrystatus = | Cancel | Confirm
195 and helpitem = string * int * action
196 and action = (uioh -> uioh) option
197 and linktarget =
198 | Ltexact of (pageno * direction)
199 | Ltgendir of direction
200 | Ltnotready of (pageno * direction)
201 and direction = int (* -1, 0, 1 *)
202 and textentry = string * string * onhist option * onkey * ondone * cancelonempty
203 and onkey = string -> Keys.t -> te
204 and ondone = string -> unit
205 and histcancel = unit -> unit
206 and onhist = ((histcmd -> string) * histcancel)
207 and histcmd = HCnext | HCprev | HCfirst | HClast
208 and cancelonempty = bool
209 and te =
210 | TEstop
211 | TEdone of string
212 | TEcont of string
213 | TEswitch of textentry
214 and 'a circbuf =
215 { store : 'a array
216 ; mutable rc : int
217 ; mutable wc : int
218 ; mutable len : int
220 and 'a nav =
221 { past : 'a list
222 ; future : 'a list
225 let emptykeyhash = Hashtbl.create 0
226 let noreprf () = ()
227 let noroamf () = ()
229 let nouioh : uioh =
230 object (self)
231 method display = ()
232 method key _ _ = self
233 method multiclick _ _ _ _ = self
234 method button _ _ _ _ _ = self
235 method motion _ _ = self
236 method pmotion _ _ = self
237 method infochanged _ = ()
238 method scrollpw = (0, nan, nan)
239 method scrollph = (0, nan, nan)
240 method modehash = emptykeyhash
241 method eformsgs = false
242 method alwaysscrolly = false
243 method scroll _ _ = self
244 method zoom _ _ _ = ()
247 let cbnew n v =
248 { store = Array.make n v
249 ; rc = 0
250 ; wc = 0
251 ; len = 0
254 let cbcap b = Array.length b.store
256 let cbput ?(update_rc=true) b v =
257 let cap = cbcap b in
258 b.store.(b.wc) <- v;
259 b.wc <- (b.wc + 1) mod cap;
260 if update_rc
261 then b.rc <- b.wc;
262 b.len <- min (b.len + 1) cap
264 let cbput_dont_update_rc b v = cbput ~update_rc:false b v
266 let cbempty b = b.len = 0
268 let cbgetg b circular dir =
269 if cbempty b
270 then b.store.(0)
271 else
272 let rc = b.rc + dir in
273 let rc =
274 if circular
275 then (
276 if rc = -1
277 then b.len-1
278 else (
279 if rc >= b.len
280 then 0
281 else rc
284 else bound rc 0 (b.len-1)
286 b.rc <- rc;
287 b.store.(rc)
289 let cbget b = cbgetg b false
290 let cbgetc b = cbgetg b true
292 type hists =
293 { pat : string circbuf
294 ; pag : string circbuf
295 ; sel : string circbuf
298 let home =
299 try Sys.getenv "HOME"
300 with exn ->
301 dolog "cannot determine home directory location: %s" @@ exntos exn;
304 module S = struct
305 let confpath = ref E.s
306 let ss = ref Unix.stdin
307 let wsfd = ref Unix.stdin
308 let stderr = ref Unix.stdin
309 let selfexec = ref E.s
310 let ignoredoctitlte = ref false
311 let errmsgs = Buffer.create 0
312 let newerrmsgs = ref false
313 let w = ref max_int
314 let x = ref max_int
315 let y = ref max_int
316 let xf = ref 0.0
317 let yf = ref 0.0
318 let anchor = ref E.j
319 let ranchors : (string * string * string * anchor * string) list ref = ref []
320 let maxy = ref max_int
321 let layout : page list ref = ref []
322 let pagemap : (pagemapkey, opaque) Hashtbl.t = Hashtbl.create 0
323 let tilemap : (tilemapkey, tile) Hashtbl.t = Hashtbl.create 0
324 let pdims : (pageno * w * h * leftx) list ref = ref []
325 let pagecount = ref max_int
326 let currently = ref Idle
327 let mstate = ref Mnone
328 let searchpattern = ref E.s
329 let rects : (pageno * rectcolor * rect) list ref = ref []
330 let rects1 : (pageno * rectcolor * rect) list ref = ref []
331 let text = ref E.s
332 let path = ref E.s
333 let password = ref E.s
334 let mimetype = ref E.s
335 let nameddest = ref E.s
336 let origin = ref E.s
337 let winstate : Wsi.winstate list ref = ref []
338 let mode : mode ref = ref View
339 let uioh : uioh ref = ref nouioh
340 let outlines : outline array ref = ref [||]
341 let bookmarks : outline list ref = ref []
342 let geomcmds : (string * ((string * (unit -> unit)) list)) ref
343 = ref (E.s, [])
344 let memused : memsize ref = ref 0
345 let gen : gen ref = ref 0
346 let autoscroll : int option ref = ref None
347 let help : helpitem array ref = ref E.a
348 let docinfo : (int * string) list ref = ref []
349 let hists : hists ref
350 = ref { pat = cbnew 10 E.s; pag = cbnew 10 E.s; sel = cbnew 10 E.s; }
351 let prevzoom = ref (1.0, 0)
352 let progress = ref ~-.1.0
353 let mpos = ref (-1, -1)
354 let keystate = ref KSnone
355 let glinks = ref false
356 let prevcolumns : (columns * zoom) option ref = ref None
357 let winw = ref ~-1
358 let winh = ref ~-1
359 let reprf = ref noreprf
360 let roamf = ref noroamf
361 let bzoom = ref false
362 let lnava : (pageno * linkno) option ref = ref None
363 let reload : (x * y * float) option ref = ref None
364 let nav : anchor nav ref = ref { past = []; future = []; }
365 let tilelru : (tilemapkey * opaque * pixmapsize) Queue.t = Queue.create ()
366 let fontpath = ref E.s
367 let redirstderr = ref false
370 let conf = { defconf with keyhashes = copykeyhashes defconf }
372 let calcips h =
373 let d = !S.winh - h in
374 max conf.interpagespace ((d + 1) / 2)
376 let rowyh (c, coverA, coverB) b n =
377 if c = 1 || (n < coverA || n >= !S.pagecount - coverB)
378 then
379 let _, _, vy, (_, _, h, _) = b.(n) in
380 (vy, h)
381 else
382 let n' = n - coverA in
383 let d = n' mod c in
384 let s = n - d in
385 let e = min !S.pagecount (s + c) in
386 let rec findminmax m miny maxh =
387 if m = e
388 then miny, maxh
389 else
390 let _, _, y, (_, _, h, _) = b.(m) in
391 let miny = min miny y in
392 let maxh = max maxh h in
393 findminmax (m+1) miny maxh
395 findminmax s max_int 0
397 let page_of_y y =
398 let ((c, coverA, coverB) as cl), b =
399 match conf.columns with
400 | Csplit (_, b) | Csingle b -> (1, 0, 0), b
401 | Cmulti (c, b) -> c, b
403 if Array.length b = 0
404 then -1
405 else
406 let rec bsearch nmin nmax =
407 if nmin > nmax
408 then bound nmin 0 (!S.pagecount-1)
409 else
410 let n = (nmax + nmin) / 2 in
411 let vy, h = rowyh cl b n in
412 let y0, y1 =
413 if conf.presentation
414 then
415 let ips = calcips h in
416 let y0 = vy - ips in
417 let y1 = vy + h + ips in
418 y0, y1
419 else (
420 if n = 0
421 then 0, vy + h + conf.interpagespace
422 else
423 let y0 = vy - conf.interpagespace in
424 y0, y0 + h + conf.interpagespace
427 if y >= y0 && y < y1
428 then (
429 if c = 1
430 then n
431 else (
432 if n > coverA
433 then
434 if n < !S.pagecount - coverB
435 then ((n-coverA)/c)*c + coverA
436 else n
437 else n
440 else (
441 if y > y0
442 then bsearch (n+1) nmax
443 else bsearch nmin (n-1)
446 bsearch 0 (!S.pagecount-1)
448 let calcheight () =
449 match conf.columns with
450 | Cmulti ((_, _, _) as cl, b) ->
451 if Array.length b > 0
452 then
453 let y, h = rowyh cl b (Array.length b - 1) in
454 y + h + (if conf.presentation then calcips h else 0)
455 else 0
456 | Csingle b ->
457 if Array.length b > 0
458 then
459 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
460 y + h + (if conf.presentation then calcips h else 0)
461 else 0
462 | Csplit (_, b) ->
463 if Array.length b > 0
464 then
465 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
466 y + h
467 else 0
469 let getpageywh pageno =
470 let pageno = bound pageno 0 (!S.pagecount-1) in
471 match conf.columns with
472 | Csingle b ->
473 if Array.length b = 0
474 then 0, 0, 0
475 else
476 let (_, _, y, (_, w, h, _)) = b.(pageno) in
477 let y =
478 if conf.presentation
479 then y - calcips h
480 else y
482 y, w, h
483 | Cmulti (cl, b) ->
484 if Array.length b = 0
485 then 0, 0, 0
486 else
487 let y, h = rowyh cl b pageno in
488 let (_, _, _, (_, w, _, _)) = b.(pageno) in
489 let y =
490 if conf.presentation
491 then y - calcips h
492 else y
494 y, w, h
495 | Csplit (c, b) ->
496 if Array.length b = 0
497 then 0, 0, 0
498 else
499 let n = pageno*c in
500 let (_, _, y, (_, w, h, _)) = b.(n) in
501 y, w / c, h
503 let getpageyh pageno =
504 let y,_,h = getpageywh pageno in
505 y, h
507 let getpagedim pageno =
508 let rec f ppdim l =
509 match l with
510 | (n, _, _, _) as pdim :: rest ->
511 if n >= pageno
512 then (if n = pageno then pdim else ppdim)
513 else f pdim rest
514 | [] -> ppdim
516 f (-1, -1, -1, -1) !S.pdims
518 let getpdimno pageno =
519 let rec f p l =
520 let np = succ p in
521 match l with
522 | (n, _, _, _) :: rest ->
523 if n >= pageno
524 then (if n = pageno then np else p)
525 else f np rest
526 | [] -> p
528 f ~-1 !S.pdims
530 let getpagey pageno = fst (getpageyh pageno)
532 let getanchor1 l =
533 let top =
534 let coloff = l.pagecol * l.pageh in
535 float (l.pagey + coloff) /. float l.pageh
537 let dtop =
538 if l.pagedispy = 0
539 then 0.0
540 else (
541 if conf.presentation
542 then float l.pagedispy /. float (calcips l.pageh)
543 else float l.pagedispy /. float conf.interpagespace
546 (l.pageno, top, dtop)
548 let getanchor () =
549 match !S.layout with
550 | l :: _ -> getanchor1 l
551 | [] ->
552 let n = page_of_y !S.y in
553 if n = -1
554 then !S.anchor
555 else
556 let y, h = getpageyh n in
557 let dy = y - !S.y in
558 let dtop =
559 if conf.presentation
560 then
561 let ips = calcips h in
562 float (dy + ips) /. float ips
563 else float dy /. float conf.interpagespace
565 (n, 0.0, dtop)
567 type historder = [ `lastvisit | `title | `path | `file ]
569 module KeyMap =
570 Map.Make (struct type t = (int * int) let compare = compare end)
572 let unentS s =
573 let l = String.length s in
574 let b = Buffer.create l in
575 Parser.unent b s 0 l;
576 Buffer.contents b
578 let modifier_of_string = function
579 | "alt" -> Wsi.altmask
580 | "shift" -> Wsi.shiftmask
581 | "ctrl" | "control" -> Wsi.ctrlmask
582 | "meta" -> Wsi.metamask
583 | _ -> 0
585 let keys_of_string s =
586 let key_of_string r s =
587 let elems = Str.full_split r s in
588 let f n k m =
589 let g s =
590 let m1 = modifier_of_string s in
591 if m1 = 0
592 then (Wsi.namekey s, m)
593 else (k, m lor m1)
594 in function
595 | Str.Delim s when n land 1 = 0 -> g s
596 | Str.Text s -> g s
597 | Str.Delim _ -> (k, m)
599 let rec loop n k m = function
600 | [] -> (k, m)
601 | x :: xs ->
602 let k, m = f n k m x in
603 loop (n+1) k m xs
605 loop 0 0 0 elems
607 let elems = Str.split Utils.Re.whitespace s in
608 List.map (key_of_string (Str.regexp "-")) elems
610 let validatehcs v =
611 let l = String.length v in
612 if l < 2
613 then error "set must contain more than one char, but has %d" l;
614 let module S = Set.Make (struct type t = char let compare = compare end) in
615 let rec check s i =
616 if i < l
617 then
618 let e = String.get v i in
619 if S.mem e s
620 then error "set has duplicates (at least '%c')" e
621 else check (S.add e s) (i+1)
623 check (S.singleton (String.get v 0)) 1
625 let config_of c attrs =
626 let maxv ?(f=int_of_string) u s = max u @@ f s in
627 let apply c k v =
629 match k with
630 | "scroll-bar-width" -> { c with scrollbw = maxv 0 v }
631 | "scroll-handle-height" -> { c with scrollh = maxv 0 v }
632 | "case-insensitive-search" -> { c with icase = bool_of_string v }
633 | "preload" -> { c with preload = bool_of_string v }
634 | "page-bias" -> { c with pagebias = int_of_string v }
635 | "scroll-step" -> { c with scrollstep = maxv 1 v }
636 | "horizontal-scroll-step" -> { c with hscrollstep = maxv 1 v }
637 | "auto-scroll-step" -> { c with autoscrollstep = maxv 0 v }
638 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
639 | "highlight-links" -> { c with hlinks = bool_of_string v }
640 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
641 | "vertical-margin" -> { c with interpagespace = maxv 0 v }
642 | "zoom" ->
643 let zoom = float_of_string v /. 100. in
644 let zoom = max zoom 0.0 in
645 { c with zoom = zoom }
646 | "presentation" -> { c with presentation = bool_of_string v }
647 | "rotation-angle" -> { c with angle = int_of_string v }
648 | "width" -> { c with cwinw = maxv 20 v }
649 | "height" -> { c with cwinh = maxv 20 v }
650 | "proportional-display" ->
651 { c with fitmodel = if bool_of_string v
652 then FitProportional
653 else FitWidth
655 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
656 | "pixmap-cache-size" ->
657 { c with memlimit = maxv ~f:int_of_string_with_suffix 2 v }
658 | "tex-count" -> { c with texcount = maxv 1 v }
659 | "slice-height" -> { c with sliceheight = maxv 2 v }
660 | "thumbnail-width" -> { c with thumbw = maxv 2 v }
661 | "background-color" -> { c with bgcolor = color_of_string v }
662 | "paper-color" -> { c with papercolor = rgba_of_string v }
663 | "scrollbar-color" -> { c with sbarcolor = rgba_of_string v }
664 | "scrollbar-handle-color" -> { c with sbarhndlcolor = rgba_of_string v }
665 | "texture-color" -> { c with texturecolor = rgba_of_string v }
666 | "tile-width" -> { c with tilew = maxv 2 v }
667 | "tile-height" -> { c with tileh = maxv 2 v }
668 | "mupdf-store-size" ->
669 { c with mustoresize = maxv ~f:int_of_string_with_suffix 1024 v }
670 | "aalevel" -> { c with aalevel = maxv 0 v }
671 | "trim-margins" -> { c with trimmargins = bool_of_string v }
672 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
673 | "uri-launcher" -> { c with urilauncher = unentS v }
674 | "path-launcher" -> { c with pathlauncher = unentS v }
675 | "color-space" -> { c with colorspace = CSTE.of_string v }
676 | "invert-colors" -> { c with invert = bool_of_string v }
677 | "brightness" -> { c with colorscale = float_of_string v }
678 | "columns" ->
679 let (n, _, _) as nab = multicolumns_of_string v in
680 if n < 0
681 then { c with columns = Csplit (-n, E.a) }
682 else { c with columns = Cmulti (nab, E.a) }
683 | "birds-eye-columns" -> { c with beyecolumns = Some (maxv 2 v) }
684 | "selection-command" -> { c with selcmd = unentS v }
685 | "paste-command" -> { c with pastecmd = unentS v }
686 | "synctex-command" -> { c with stcmd = unentS v }
687 | "pax-command" -> { c with paxcmd = unentS v }
688 | "askpass-command" -> { c with passcmd = unentS v }
689 | "savepath-command" -> { c with savecmd = unentS v }
690 | "update-cursor" -> { c with updatecurs = bool_of_string v }
691 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
692 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
693 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
694 | "horizontal-scrollbar-visible" ->
695 { c with scrollb = if bool_of_string v
696 then c.scrollb lor scrollbhv
697 else c.scrollb land (lnot scrollbhv)
699 | "vertical-scrollbar-visible" ->
700 { c with scrollb = if bool_of_string v
701 then c.scrollb lor scrollbvv
702 else c.scrollb land (lnot scrollbvv)
704 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
705 | "point-and-x" ->
706 { c with pax = if bool_of_string v then Some 0.0 else None }
707 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
708 | "scroll-bar-on-the-left" -> { c with leftscroll = bool_of_string v }
709 | "title" -> { c with title = unentS v }
710 | "last-visit" -> { c with lastvisit = float_of_string v }
711 | "edit-annotations-inline" -> { c with annotinline = bool_of_string v }
712 | "coarse-presentation-positioning" ->
713 { c with coarseprespos = bool_of_string v }
714 | "use-document-css" -> { c with usedoccss = bool_of_string v }
715 | "hint-charset" -> validatehcs v; { c with hcs = v }
716 | "rlw" -> { c with rlw = int_of_string v }
717 | "rlh" -> { c with rlh = int_of_string v }
718 | "rlem" -> { c with rlem = int_of_string v }
719 | _ -> c
720 with exn ->
721 dolog "error processing attribute (`%S' = `%S'): %s" k v @@ exntos exn;
724 let rec fold c = function
725 | [] -> c
726 | (k, v) :: rest ->
727 let c = apply c k v in
728 fold c rest
730 fold { c with keyhashes = copykeyhashes c } attrs
732 let fromstring f pos n v d =
733 try f v
734 with exn ->
735 dolog "error processing attribute (%S=%S) at %d\n%s" n v pos @@ exntos exn;
738 let bookmark_of attrs =
739 let rec fold title page rely visy = function
740 | ("title", v) :: rest -> fold v page rely visy rest
741 | ("page", v) :: rest -> fold title v rely visy rest
742 | ("rely", v) :: rest -> fold title page v visy rest
743 | ("visy", v) :: rest -> fold title page rely v rest
744 | _ :: rest -> fold title page rely visy rest
745 | [] -> title, page, rely, visy
747 fold "invalid" "0" "0" "0" attrs
749 let doc_of attrs =
750 let rec fold path key page rely pan visy origin dcf = function
751 | ("path", v) :: rest -> fold v key page rely pan visy origin dcf rest
752 | ("key", v) :: rest -> fold path v page rely pan visy origin dcf rest
753 | ("page", v) :: rest -> fold path key v rely pan visy origin dcf rest
754 | ("rely", v) :: rest -> fold path key page v pan visy origin dcf rest
755 | ("pan", v) :: rest -> fold path key page rely v visy origin dcf rest
756 | ("visy", v) :: rest -> fold path key page rely pan v origin dcf rest
757 | ("origin", v) :: rest -> fold path key page rely pan visy v dcf rest
758 | ("dcf", v) :: rest -> fold path key page rely pan visy origin v rest
759 | _ :: rest -> fold path key page rely pan visy origin dcf rest
760 | [] -> path, key, page, rely, pan, visy, origin, dcf
762 fold E.s E.s "0" "0" "0" "0" E.s E.s attrs
764 let map_of attrs =
765 let rec fold rs ls = function
766 | ("out", v) :: rest -> fold v ls rest
767 | ("in", v) :: rest -> fold rs v rest
768 | _ :: rest -> fold ls rs rest
769 | [] -> ls, rs
771 fold E.s E.s attrs
773 let findkeyhash c name =
774 try List.assoc name c.keyhashes
775 with Not_found -> error "invalid mode name `%s'" name
777 let get s =
778 let open Parser in
779 let h = Hashtbl.create 10 in
780 let dc = { defconf with angle = defconf.angle } in
781 let rec toplevel v t spos _ =
782 match t with
783 | Vdata | Vcdata | Vend -> v
784 | Vopen ("llppconfig", _, closed) ->
785 if closed
786 then v
787 else { v with f = llppconfig }
788 | Vopen _ -> parse_error "unexpected subelement at top level" s spos
789 | Vclose _ -> parse_error "unexpected close at top level" s spos
791 and llppconfig v t spos _ =
792 match t with
793 | Vdata | Vcdata -> v
794 | Vend -> parse_error "unexpected end of input in llppconfig" s spos
795 | Vopen ("defaults", attrs, closed) ->
796 let c = config_of dc attrs in
797 setconf dc c;
798 if closed
799 then v
800 else { v with f = defaults }
802 | Vopen ("ui-font", attrs, closed) ->
803 let rec getsize size = function
804 | [] -> size
805 | ("size", v) :: rest ->
806 let size =
807 fromstring int_of_string spos "size" v fstate.fontsize in
808 getsize size rest
809 | l -> getsize size l
811 fstate.fontsize <- getsize fstate.fontsize attrs;
812 if closed
813 then v
814 else { v with f = uifont (Buffer.create 10) }
816 | Vopen ("doc", attrs, closed) ->
817 let pathent, key, spage, srely, span, svisy, origin, dcf
818 = doc_of attrs in
819 let path = unentS pathent
820 and origin = unentS origin
821 and pageno = fromstring int_of_string spos "page" spage 0
822 and rely = fromstring float_of_string spos "rely" srely 0.0
823 and pan = fromstring int_of_string spos "pan" span 0
824 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
825 let c = config_of dc attrs in
826 c.key <- key;
827 c.dcf <- unentS dcf;
828 let anchor = (pageno, rely, visy) in
829 if closed
830 then (Hashtbl.add h path (c, [], pan, anchor, origin); v)
831 else { v with f = doc path origin pan anchor c [] }
833 | Vopen _ -> parse_error "unexpected subelement in llppconfig" s spos
834 | Vclose "llppconfig" -> { v with f = toplevel }
835 | Vclose _ -> parse_error "unexpected close in llppconfig" s spos
837 and defaults v t spos _ =
838 match t with
839 | Vdata | Vcdata -> v
840 | Vend -> parse_error "unexpected end of input in defaults" s spos
841 | Vopen ("keymap", attrs, closed) ->
842 let modename =
843 try List.assoc "mode" attrs
844 with Not_found -> "global" in
845 if closed
846 then v
847 else
848 let ret keymap =
849 let h = findkeyhash dc modename in
850 KeyMap.iter (Hashtbl.replace h) keymap;
851 defaults
853 { v with f = pkeymap ret KeyMap.empty }
855 | Vopen (_, _, _) -> parse_error "unexpected subelement in defaults" s spos
857 | Vclose "defaults" ->
858 { v with f = llppconfig }
860 | Vclose _ -> parse_error "unexpected close in defaults" s spos
862 and uifont b v t spos epos =
863 match t with
864 | Vdata | Vcdata ->
865 Buffer.add_substring b s spos (epos - spos);
867 | Vopen (_, _, _) -> parse_error "unexpected subelement in ui-font" s spos
868 | Vclose "ui-font" ->
869 if emptystr !S.fontpath
870 then S.fontpath := Buffer.contents b;
871 { v with f = llppconfig }
872 | Vclose _ -> parse_error "unexpected close in ui-font" s spos
873 | Vend -> parse_error "unexpected end of input in ui-font" s spos
875 and doc path origin pan anchor c bookmarks v t spos _ =
876 match t with
877 | Vdata | Vcdata -> v
878 | Vend -> parse_error "unexpected end of input in doc" s spos
879 | Vopen ("bookmarks", _, closed) ->
880 if closed
881 then v
882 else { v with f = pbookmarks path origin pan anchor c bookmarks }
884 | Vopen ("keymap", attrs, closed) ->
885 let modename =
886 try List.assoc "mode" attrs
887 with Not_found -> "global"
889 if closed
890 then v
891 else
892 let ret keymap =
893 let h = findkeyhash c modename in
894 KeyMap.iter (Hashtbl.replace h) keymap;
895 doc path origin pan anchor c bookmarks
897 { v with f = pkeymap ret KeyMap.empty }
899 | Vopen ("css", [], false) ->
900 { v with f = pcss path origin pan anchor c bookmarks }
902 | Vopen (_, _, _) ->
903 parse_error "unexpected subelement in doc" s spos
905 | Vclose "doc" ->
906 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor, origin);
907 { v with f = llppconfig }
909 | Vclose _ -> parse_error "unexpected close in doc" s spos
911 and pcss path origin pan anchor c bookmarks v t spos epos =
912 match t with
913 | Vdata | Vcdata ->
914 let b = Buffer.create 10 in
915 Buffer.add_substring b s spos (epos - spos);
916 { v with f = pcss path origin pan anchor
917 { c with css = Buffer.contents b }
918 bookmarks }
919 | Vend -> parse_error "unexpected end of input in css" s spos
920 | Vopen _ -> parse_error "unexpected subelement in css" s spos
921 | Vclose "css" -> { v with f = doc path origin pan anchor c bookmarks }
922 | Vclose _ -> parse_error "unexpected close in css" s spos
924 and pkeymap ret keymap v t spos _ =
925 match t with
926 | Vdata | Vcdata -> v
927 | Vend -> parse_error "unexpected end of input in keymap" s spos
928 | Vopen ("map", attrs, closed) ->
929 let r, l = map_of attrs in
930 let kss = fromstring keys_of_string spos "in" r [] in
931 let lss = fromstring keys_of_string spos "out" l [] in
932 let keymap =
933 match kss with
934 | [] -> keymap
935 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
936 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
938 if closed
939 then { v with f = pkeymap ret keymap }
940 else
941 let f () = v in
942 { v with f = skip "map" f }
944 | Vopen _ -> parse_error "unexpected subelement in keymap" s spos
945 | Vclose "keymap" ->
946 { v with f = ret keymap }
947 | Vclose _ -> parse_error "unexpected close in keymap" s spos
949 and pbookmarks path origin pan anchor c bookmarks v t spos _ =
950 match t with
951 | Vdata | Vcdata -> v
952 | Vend -> parse_error "unexpected end of input in bookmarks" s spos
953 | Vopen ("item", attrs, closed) ->
954 let titleent, spage, srely, svisy = bookmark_of attrs in
955 let page = fromstring int_of_string spos "page" spage 0
956 and rely = fromstring float_of_string spos "rely" srely 0.0
957 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
958 let bookmarks =
959 (unentS titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
961 if closed
962 then { v with f = pbookmarks path origin pan anchor c bookmarks }
963 else
964 let f () = v in
965 { v with f = skip "item" f }
967 | Vopen _ -> parse_error "unexpected subelement in bookmarks" s spos
968 | Vclose "bookmarks" ->
969 { v with f = doc path origin pan anchor c bookmarks }
970 | Vclose _ -> parse_error "unexpected close in bookmarks" s spos
972 and skip tag f v t spos _ =
973 match t with
974 | Vdata | Vcdata -> v
975 | Vend -> parse_error ("unexpected end of input in skipped " ^ tag) s spos
976 | Vopen (tag', _, closed) ->
977 if closed
978 then v
979 else
980 let f' () = { v with f = skip tag f } in
981 { v with f = skip tag' f' }
982 | Vclose ctag ->
983 if tag = ctag
984 then f ()
985 else parse_error ("unexpected close in skipped " ^ tag) s spos
987 parse { f = toplevel; accu = () } s;
988 h, dc
990 let do_load f contents =
991 try f contents
992 with
993 | Parser.Parse_error (msg, s, pos) ->
994 let subs = Parser.subs s pos in
995 Utils.error "parse error: %s: at %d [..%S..]" msg pos subs
997 | exn -> Utils.error "parse error: %s" @@ exntos exn
999 let load2 f default =
1000 match filecontents !S.confpath with
1001 | contents -> f @@ do_load get contents
1002 | exception Unix.Unix_error (Unix.ENOENT, "open", _) ->
1003 f (Hashtbl.create 0, defconf)
1004 | exception exn ->
1005 dolog "error loading configuration from `%S': %s"
1006 !S.confpath @@ exntos exn;
1007 default
1009 let load1 f = load2 f false
1011 let load openlast =
1012 let f (h, dc) =
1013 if openlast
1014 then (
1015 let path, _ =
1016 Hashtbl.fold
1017 (fun path (conf, _, _, _, _) ((_, besttime) as best) ->
1018 if conf.lastvisit > besttime
1019 then (path, conf.lastvisit)
1020 else best)
1022 (!S.path, -.infinity)
1024 S.path := path;
1026 let pc, pb, px, pa, po =
1027 let def = dc, [], 0, E.j, !S.origin in
1028 if emptystr !S.path
1029 then def
1030 else
1031 let absname = abspath !S.path in
1032 match Hashtbl.find h absname with
1033 | (c,b,x,a,_) -> (c,b,x,a,!S.origin)
1034 | exception Not_found ->
1035 let exception E of (conf * outline list * int * anchor * string) in
1036 let key = try Digest.file absname |> Digest.to_hex with _ -> E.s in
1037 match (
1038 if nonemptystr key
1039 then
1040 Hashtbl.iter (fun p ((c, _, _, _, _) as v) ->
1041 if c.key = key
1042 then (
1043 dolog "will use %s's settings due to matching keys" p;
1044 raise (E v)
1048 with
1049 | _ -> def
1050 | exception E v -> v
1052 setconf defconf dc;
1053 setconf conf pc;
1054 S.bookmarks := pb;
1055 S.x := px;
1056 S.origin := po;
1057 S.anchor := pa;
1058 true
1060 load1 f
1062 let gethist () =
1063 let f (h, _) =
1064 Hashtbl.fold (fun path (pc, pb, px, pa, po) accu ->
1065 (path, pc, pb, px, pa, po) :: accu)
1066 h [];
1068 load2 f []
1070 let add_attrs bb always dc c time =
1071 let o' fmt s =
1072 Buffer.add_string bb "\n ";
1073 Printf.bprintf bb fmt s
1075 let o c fmt s = if c then o' fmt s else ignore in
1076 let ob s a b = o (always || a != b) "%s='%b'" s a
1077 and op s a b = o (always || a <> b) "%s='%b'" s (a != None)
1078 and oi s a b = o (always || a != b) "%s='%d'" s a
1079 and oI s a b = o (always || a != b) "%s='%s'" s (string_with_suffix_of_int a)
1080 and oz s a b = o (always || a <> b) "%s='%g'" s (a*.100.)
1081 and oF s a b = o (always || a <> b) "%s='%f'" s a
1082 and oL s a b = o (always || a <> b) "%s='%Ld'" s a
1083 and oc s a b = o (always || a <> b) "%s='%s'" s (color_to_string a)
1084 and oA s a b = o (always || a <> b) "%s='%s'" s (rgba_to_string a)
1085 and oC s a b = o (always || a <> b) "%s='%s'" s (CSTE.to_string a)
1086 and oR s a b = o (always || a <> b) "%s='%s'" s (irect_to_string a)
1087 and oFm s a b = o (always || a <> b) "%s='%s'" s (FMTE.to_string a)
1088 and oSv s a b m =
1089 o (always || a land m <> b land m) "%s='%b'" s (a land m != 0)
1090 and oPm s a b = o (always || a <> b) "%s='%s'" s (MTE.to_string a)
1091 and os s a b =
1092 o (always || a <> b) "%s='%s'" s @@ Parser.enent a 0 (String.length a)
1093 and oco s a b =
1094 if always || a <> b
1095 then
1096 match a with
1097 | Cmulti ((n, a, b), _) when n > 1 -> o' "%s='%d,%d,%d'" s n a b
1098 | Csplit (n, _) when n > 1 -> o' "%s='%d'" s ~-n
1099 | Cmulti _ | Csplit _ | Csingle _ -> ()
1100 and obeco s a b =
1101 if always || a <> b
1102 then
1103 match a with
1104 | Some c when c > 1 -> o' "%s='%d'" s c
1105 | _ -> ()
1107 oi "width" c.cwinw dc.cwinw;
1108 oi "height" c.cwinh dc.cwinh;
1109 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
1110 oi "scroll-handle-height" c.scrollh dc.scrollh;
1111 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
1112 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
1113 ob "case-insensitive-search" c.icase dc.icase;
1114 ob "preload" c.preload dc.preload;
1115 oi "page-bias" c.pagebias dc.pagebias;
1116 oi "scroll-step" c.scrollstep dc.scrollstep;
1117 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
1118 ob "max-height-fit" c.maxhfit dc.maxhfit;
1119 ob "highlight-links" c.hlinks dc.hlinks;
1120 ob "under-cursor-info" c.underinfo dc.underinfo;
1121 oi "vertical-margin" c.interpagespace dc.interpagespace;
1122 oz "zoom" c.zoom dc.zoom;
1123 ob "presentation" c.presentation dc.presentation;
1124 oi "rotation-angle" c.angle dc.angle;
1125 oFm "fit-model" c.fitmodel dc.fitmodel;
1126 oI "pixmap-cache-size" c.memlimit dc.memlimit;
1127 oi "tex-count" c.texcount dc.texcount;
1128 oi "slice-height" c.sliceheight dc.sliceheight;
1129 oi "thumbnail-width" c.thumbw dc.thumbw;
1130 oc "background-color" c.bgcolor dc.bgcolor;
1131 oA "paper-color" c.papercolor dc.papercolor;
1132 oA "scrollbar-color" c.sbarcolor dc.sbarcolor;
1133 oA "scrollbar-handle-color" c.sbarhndlcolor dc.sbarhndlcolor;
1134 oA "texture-color" c.texturecolor dc.texturecolor;
1135 oi "tile-width" c.tilew dc.tilew;
1136 oi "tile-height" c.tileh dc.tileh;
1137 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
1138 oi "aalevel" c.aalevel dc.aalevel;
1139 ob "trim-margins" c.trimmargins dc.trimmargins;
1140 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
1141 os "uri-launcher" c.urilauncher dc.urilauncher;
1142 os "path-launcher" c.pathlauncher dc.pathlauncher;
1143 oC "color-space" c.colorspace dc.colorspace;
1144 ob "invert-colors" c.invert dc.invert;
1145 oF "brightness" c.colorscale dc.colorscale;
1146 oco "columns" c.columns dc.columns;
1147 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
1148 os "selection-command" c.selcmd dc.selcmd;
1149 os "paste-command" c.pastecmd dc.pastecmd;
1150 os "synctex-command" c.stcmd dc.stcmd;
1151 os "pax-command" c.paxcmd dc.paxcmd;
1152 os "askpass-command" c.passcmd dc.passcmd;
1153 os "savepath-command" c.savecmd dc.savecmd;
1154 ob "update-cursor" c.updatecurs dc.updatecurs;
1155 oi "hint-font-size" c.hfsize dc.hfsize;
1156 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
1157 oF "page-scroll-scale" c.pgscale dc.pgscale;
1158 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
1159 ob "remote-in-a-new-instance" c.riani dc.riani;
1160 op "point-and-x" c.pax dc.pax;
1161 oPm "point-and-x-mark" c.paxmark dc.paxmark;
1162 ob "scroll-bar-on-the-left" c.leftscroll dc.leftscroll;
1163 if not always
1164 then os "title" c.title dc.title;
1165 oL "last-visit" (Int64.of_float time) 0L;
1166 ob "edit-annotations-inline" c.annotinline dc.annotinline;
1167 ob "coarse-presentation-positioning" c.coarseprespos dc.coarseprespos;
1168 ob "use-document-css" c.usedoccss dc.usedoccss;
1169 os "dcf" c.dcf dc.dcf;
1170 os "hint-charset" c.hcs dc.hcs;
1171 oi "rlw" c.rlw dc.rlw;
1172 oi "rlh" c.rlh dc.rlh;
1173 oi "rlem" c.rlem dc.rlem
1175 let keymapsbuf always dc c =
1176 let open Buffer in
1177 let bb = create 16 in
1178 let rec loop = function
1179 | [] -> ()
1180 | (modename, h) :: rest ->
1181 let dh = findkeyhash dc modename in
1182 if always || h <> dh
1183 then (
1184 if Hashtbl.length h > 0
1185 then (
1186 if length bb > 0 then add_char bb '\n';
1187 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
1188 Hashtbl.iter (fun i o ->
1189 if always || match Hashtbl.find dh i
1190 with | dO -> dO <> o | exception Not_found -> false
1191 then
1192 let addkm (k, m) =
1193 if Wsi.withctrl m then add_string bb "ctrl-";
1194 if Wsi.withalt m then add_string bb "alt-";
1195 if Wsi.withshift m then add_string bb "shift-";
1196 if Wsi.withmeta m then add_string bb "meta-";
1197 add_string bb (Wsi.keyname k);
1199 let addkms l =
1200 let rec loop = function
1201 | [] -> ()
1202 | km :: [] -> addkm km
1203 | km :: rest -> addkm km; add_char bb ' '; loop rest
1205 loop l
1207 add_string bb "<map in='";
1208 addkm i;
1209 match o with
1210 | KMinsrt km ->
1211 add_string bb "' out='"; addkm km; add_string bb "'/>\n"
1213 | KMinsrl kms ->
1214 add_string bb "' out='"; addkms kms; add_string bb "'/>\n"
1216 | KMmulti (ins, kms) ->
1217 add_char bb ' '; addkms ins; add_string bb "' out='";
1218 addkms kms; add_string bb "'/>\n"
1219 ) h;
1220 add_string bb "</keymap>";
1223 loop rest
1225 loop c.keyhashes;
1228 let keystostrlist c =
1229 let rec loop accu = function
1230 | [] -> accu
1231 | (modename, h) :: rest ->
1232 let accu =
1233 if Hashtbl.length h > 0
1234 then (
1235 let accu = Printf.sprintf "\xc2\xb7Keys for %s" modename :: accu in
1236 Hashtbl.fold (fun i o a ->
1237 let bb = Buffer.create 10 in
1238 let addkm (k, m) =
1239 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
1240 if Wsi.withalt m then Buffer.add_string bb "alt-";
1241 if Wsi.withshift m then Buffer.add_string bb "shift-";
1242 if Wsi.withmeta m then Buffer.add_string bb "meta-";
1243 Buffer.add_string bb (Wsi.keyname k);
1245 let addkms l =
1246 let rec loop = function
1247 | [] -> ()
1248 | km :: [] -> addkm km
1249 | km :: rest ->
1250 addkm km; Buffer.add_char bb ' ';
1251 loop rest
1253 loop l
1255 addkm i;
1256 Buffer.add_char bb '\t';
1257 begin match o with
1258 | KMinsrt km -> addkm km
1259 | KMinsrl kms -> addkms kms
1260 | KMmulti (ins, kms) ->
1261 Buffer.add_char bb ' ';
1262 addkms ins;
1263 Buffer.add_string bb "\t";
1264 addkms kms
1265 end;
1266 Buffer.contents bb :: a
1267 ) h accu
1269 else accu
1271 loop accu rest
1273 loop [] c.keyhashes
1275 let save1 bb leavebirdseye x h dc =
1276 let uifontsize = fstate.fontsize in
1277 Buffer.add_string bb "<llppconfig>\n";
1278 if nonemptystr !S.fontpath
1279 then (
1280 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
1281 uifontsize !S.fontpath
1283 else (
1284 if uifontsize <> 14
1285 then Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
1288 Buffer.add_string bb "<defaults";
1289 add_attrs bb true dc dc nan;
1290 let kb = keymapsbuf true dc dc in
1291 if Buffer.length kb > 0
1292 then (
1293 Buffer.add_string bb ">\n";
1294 Buffer.add_buffer bb kb;
1295 Buffer.add_string bb "\n</defaults>\n";
1297 else Buffer.add_string bb "/>\n";
1299 let adddoc path pan anchor c bookmarks time origin =
1300 if not (bookmarks == [] && c = dc && anchor = E.j)
1301 then (
1302 Printf.bprintf bb "<doc path='%s'"
1303 (Parser.enent path 0 (String.length path));
1305 if nonemptystr c.key
1306 then (
1307 Printf.bprintf bb "\n key='%s'" c.key;
1310 if nonemptystr origin
1311 then (
1312 Printf.bprintf bb "\n origin='%s'"
1313 (Parser.enent origin 0 (String.length origin));
1316 if anchor <> E.j
1317 then (
1318 let n, rely, visy = anchor in
1319 Printf.bprintf bb "\n page='%d'" n;
1321 if rely > 1e-6
1322 then Printf.bprintf bb " rely='%f'" rely;
1324 if abs_float visy > 1e-6
1325 then Printf.bprintf bb " visy='%f'" visy;
1328 if pan != 0
1329 then Printf.bprintf bb " pan='%d'" pan;
1331 add_attrs bb false dc c time;
1332 if nonemptystr c.css
1333 then Printf.bprintf bb ">\n <css><![CDATA[%s]]></css>" c.css;
1334 let kb = keymapsbuf false dc c in
1336 begin match bookmarks with
1337 | [] ->
1338 if Buffer.length kb > 0
1339 then (
1340 Buffer.add_string bb ">\n";
1341 Buffer.add_buffer bb kb;
1342 Buffer.add_string bb "\n</doc>\n";
1344 else (
1345 if nonemptystr c.css
1346 then Buffer.add_string bb "\n</doc>\n"
1347 else Buffer.add_string bb "/>\n"
1349 | _ ->
1350 Buffer.add_string bb ">\n<bookmarks>\n";
1351 List.iter (fun (title, _, kind) ->
1352 begin match kind with
1353 | Oanchor (page, rely, visy) ->
1354 Printf.bprintf bb
1355 "<item title='%s' page='%d'"
1356 (Parser.enent title 0 (String.length title))
1357 page;
1358 if rely > 1e-6
1359 then Printf.bprintf bb " rely='%f'" rely;
1360 if abs_float visy > 1e-6
1361 then Printf.bprintf bb " visy='%f'" visy;
1363 | Ohistory _ | Onone | Ouri _ | Oremote _
1364 | Oremotedest _ | Olaunch _ -> error "unexpected link in bookmarks"
1365 end;
1366 Buffer.add_string bb "/>\n";
1367 ) bookmarks;
1368 Buffer.add_string bb "</bookmarks>";
1369 if Buffer.length kb > 0
1370 then (
1371 Buffer.add_string bb "\n";
1372 Buffer.add_buffer bb kb;
1374 Buffer.add_string bb "\n</doc>\n"
1379 let pan, conf =
1380 match !S.mode with
1381 | Birdseye (c, pan, _, _, _) ->
1382 let beyecolumns =
1383 match conf.columns with
1384 | Cmulti ((c, _, _), _) -> Some c
1385 | Csingle _
1386 | Csplit _ -> None
1387 and columns =
1388 match c.columns with
1389 | Cmulti (c, _) -> Cmulti (c, E.a)
1390 | Csingle _ -> Csingle E.a
1391 | Csplit _ -> failwith "quit from bird's eye while split"
1393 pan, { c with beyecolumns = beyecolumns; columns = columns }
1394 | Textentry _
1395 | View
1396 | LinkNav _ -> x, conf
1398 let docpath = if nonemptystr !S.path then abspath !S.path else E.s in
1399 if nonemptystr docpath
1400 then (
1401 adddoc docpath pan (getanchor ())
1403 begin match !S.mode with
1404 | Birdseye beye -> leavebirdseye beye true
1405 | Textentry _
1406 | View
1407 | LinkNav _ -> ()
1408 end;
1409 { conf with
1410 autoscrollstep = (match !S.autoscroll with
1411 | Some step -> step
1412 | None -> conf.autoscrollstep)
1413 ; key = (if emptystr conf.key
1414 then (try Digest.file docpath |> Digest.to_hex with _ -> E.s)
1415 else conf.key)
1418 !S.bookmarks
1419 (now ())
1420 !S.origin
1422 Hashtbl.iter (fun path (c, bookmarks, x, anchor, origin) ->
1423 if docpath <> abspath path
1424 then adddoc path x anchor c bookmarks c.lastvisit origin
1425 ) h;
1426 Buffer.add_string bb "</llppconfig>\n";
1427 true
1429 let save leavebirdseye =
1430 let relx = float !S.x /. float !S.winw in
1431 let w, h, x =
1432 let cx w = truncate (relx *. float w) in
1433 List.fold_left
1434 (fun (w, h, x) ws ->
1435 match ws with
1436 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
1437 | Wsi.MaxVert -> (w, conf.cwinh, x)
1438 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
1440 (!S.winw, !S.winh, !S.x) !S.winstate
1442 conf.cwinw <- w;
1443 conf.cwinh <- h;
1444 let bb = Buffer.create 32768 in
1445 let save2 (h, dc) = save1 bb leavebirdseye x h dc in
1446 if load1 save2 && Buffer.length bb > 0
1447 then
1449 let tmp = !S.confpath ^ ".tmp" in
1450 let oc = open_out_bin tmp in
1451 Buffer.output_buffer oc bb;
1452 close_out oc;
1453 Unix.rename tmp !S.confpath;
1454 with exn -> dolog "error saving configuration: %s" @@ exntos exn
1456 let gc () =
1457 let href = ref @@ Hashtbl.create 0 in
1458 let cref = ref defconf in
1459 let push (h, dc) =
1460 let f path v =
1461 if Sys.file_exists path then Some v else (dolog "removing %S" path; None)
1463 Hashtbl.filter_map_inplace f h;
1464 href := h;
1465 cref := dc;
1466 true
1468 ignore (load1 push);
1469 let bb = Buffer.create 32768 in
1470 let save2 (_h, dc) = save1 bb (fun _ _ -> ()) 0 !href dc in
1471 if load1 save2 && Buffer.length bb > 0
1472 then (
1474 let tmp = !S.confpath ^ ".tmp" in
1475 let oc = open_out_bin tmp in
1476 Buffer.output_buffer oc bb;
1477 close_out oc;
1478 Unix.rename tmp !S.confpath;
1479 with exn -> dolog "error saving configuration: %s" @@ exntos exn
1482 let logcurrently = function
1483 | Idle -> dolog "Idle"
1484 | Loading (l, gen) -> dolog "Loading %d gen=%d curgen=%d" l.pageno gen !S.gen
1485 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1486 dolog "Tiling %d[%d,%d] page=%s cs=%s angle=%d"
1487 l.pageno col row (Opaque.to_string pageopaque)
1488 (CSTE.to_string colorspace) angle;
1489 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1490 angle gen conf.angle !S.gen
1491 tilew tileh
1492 conf.tilew conf.tileh
1493 | Outlining _ -> dolog "outlining"