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