Quoting
[llpp.git] / config.ml
blob9de3be35e9f913a2484cab6dc795375e1d7faa3d
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 failwith "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 =
118 type t
119 val name : string
120 val names : string array
121 end;;
123 module TextEnumMake (Ten : TextEnumType) =
124 struct
125 let names = Ten.names;;
126 let to_int (t : Ten.t) = Obj.magic t;;
127 let to_string t = names.(to_int t);;
128 let of_int n : Ten.t = Obj.magic n;;
129 let of_string s =
130 let rec find i =
131 if i = Array.length names
132 then failwith ("invalid " ^ Ten.name ^ ": " ^ s)
133 else (
134 if Ten.names.(i) = s
135 then of_int i
136 else find (i+1)
138 in find 0;;
139 end;;
141 module CSTE = TextEnumMake (struct
142 type t = colorspace;;
143 let name = "colorspace";;
144 let names = [|"rgb"; "bgr"; "gray"|];;
145 end);;
147 module MTE = TextEnumMake (struct
148 type t = mark;;
149 let name = "mark";;
150 let names = [|"page"; "block"; "line"; "word"|];;
151 end);;
153 module FMTE = TextEnumMake (struct
154 type t = fitmodel;;
155 let name = "fitmodel";;
156 let names = [|"width"; "proportional"; "page"|];;
157 end);;
159 type outlinekind =
160 | Onone
161 | Oanchor of anchor
162 | Ouri of uri
163 | Olaunch of launchcommand
164 | Oremote of (filename * pageno)
165 | Oremotedest of (filename * destname)
166 | Ohistory of (filename * conf * outline list * x * anchor * filename)
167 and outline = (caption * outlinelevel * outlinekind)
168 and outlinelevel = int
171 type page =
172 { pageno : int
173 ; pagedimno : int
174 ; pagew : int
175 ; pageh : int
176 ; pagex : int
177 ; pagey : int
178 ; pagevw : int
179 ; pagevh : int
180 ; pagedispx : int
181 ; pagedispy : int
182 ; pagecol : int
186 type tile = opaque * pixmapsize * elapsed
187 and elapsed = float;;
188 type pagemapkey = pageno * gen;;
189 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
190 and row = int
191 and col = int
192 and currently =
193 | Idle
194 | Loading of (page * gen)
195 | Tiling of (
196 page * opaque * colorspace * angle * gen * col * row * width * height
198 | Outlining of outline list
201 type mpos = int * int
202 and mstate =
203 | Msel of (mpos * mpos)
204 | Mpan of mpos
205 | Mscrolly | Mscrollx
206 | Mzoom of (buttonno * step * mpos)
207 | Mzoomrect of (mpos * mpos)
208 | Mnone
209 and buttonno = int
210 and step = int
213 type mode =
214 | Birdseye of (conf * leftx * pageno * pageno * anchor)
215 | Textentry of (textentry * onleave)
216 | View
217 | LinkNav of linktarget
218 and onleave = leavetextentrystatus -> unit
219 and leavetextentrystatus = | Cancel | Confirm
220 and helpitem = string * int * action
221 and action =
222 | Noaction
223 | Action of (uioh -> uioh)
224 and linktarget =
225 | Ltexact of (pageno * direction)
226 | Ltgendir of direction
227 | Ltnotready of (pageno * direction)
228 and direction = int (* -1, 0, 1 *)
229 and textentry = string * string * onhist option
230 * onkey * ondone * cancelonempty
231 and onkey = string -> Keys.t -> te
232 and ondone = string -> unit
233 and histcancel = unit -> unit
234 and onhist = ((histcmd -> string) * histcancel)
235 and histcmd = HCnext | HCprev | HCfirst | HClast
236 and cancelonempty = bool
237 and te =
238 | TEstop
239 | TEdone of string
240 | TEcont of string
241 | TEswitch of textentry
244 type 'a circbuf =
245 { store : 'a array
246 ; mutable rc : int
247 ; mutable wc : int
248 ; mutable len : int
252 type state =
253 { mutable ss : Unix.file_descr
254 ; mutable wsfd : Unix.file_descr
255 ; mutable stderr : Unix.file_descr
256 ; mutable errmsgs : Buffer.t
257 ; mutable newerrmsgs : bool
258 ; mutable w : int
259 ; mutable x : x
260 ; mutable y : y
261 ; mutable anchor : anchor
262 ; mutable ranchors : (string * string * anchor * string) list
263 ; mutable maxy : int
264 ; mutable layout : page list
265 ; pagemap : (pagemapkey, opaque) Hashtbl.t
266 ; tilemap : (tilemapkey, tile) Hashtbl.t
267 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
268 ; mutable pdims : (pageno * width * height * leftx) list
269 ; mutable pagecount : int
270 ; mutable currently : currently
271 ; mutable mstate : mstate
272 ; mutable searchpattern : string
273 ; mutable rects : (pageno * rectcolor * rect) list
274 ; mutable rects1 : (pageno * rectcolor * rect) list
275 ; prects : (pageno, float array) Hashtbl.t
276 ; mutable text : string
277 ; mutable winstate : Wsi.winstate list
278 ; mutable mode : mode
279 ; mutable uioh : uioh
280 ; mutable outlines : outline array
281 ; mutable bookmarks : outline list
282 ; mutable path : string
283 ; mutable password : string
284 ; mutable nameddest : string
285 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
286 ; mutable memused : memsize
287 ; mutable gen : gen
288 ; mutable autoscroll : int option
289 ; mutable help : helpitem array
290 ; mutable docinfo : (int * string) list
291 ; mutable checkerstexid : GlTex.texture_id option
292 ; hists : hists
293 ; mutable prevzoom : (float * int)
294 ; mutable progress : float
295 ; mutable mpos : mpos
296 ; mutable keystate : keystate
297 ; mutable glinks : bool
298 ; mutable prevcolumns : (columns * float) option
299 ; mutable winw : int
300 ; mutable winh : int
301 ; mutable reprf : (unit -> unit)
302 ; mutable origin : string
303 ; mutable roam : (unit -> unit)
304 ; mutable bzoom : bool
305 ; mutable lnava : (pageno * linkno) option
306 ; mutable slideshow : int
308 and hists =
309 { pat : string circbuf
310 ; pag : string circbuf
311 ; nav : anchor circbuf
312 ; sel : string circbuf
316 let emptyanchor = (0, 0.0, 0.0);;
317 let emptykeyhash = Hashtbl.create 0;;
318 let noreprf () = ();;
319 let noroam () = ();;
321 let nouioh : uioh = object (self)
322 method display = ()
323 method key _ _ = self
324 method multiclick _ _ _ _ = self
325 method button _ _ _ _ _ = self
326 method motion _ _ = self
327 method pmotion _ _ = self
328 method infochanged _ = ()
329 method scrollpw = (0, nan, nan)
330 method scrollph = (0, nan, nan)
331 method modehash = emptykeyhash
332 method eformsgs = false
333 method alwaysscrolly = false
334 method scroll _ _ = self
335 method zoom _ _ _ = ()
336 end;;
338 let platform_to_string = function
339 | Punknown -> "unknown"
340 | Plinux -> "Linux"
341 | Posx -> "macOS"
342 | Psun -> "Sun"
343 | Pbsd -> "BSD"
346 let conf = { defconf with keyhashes = copykeyhashes defconf };;
348 let cbnew n v =
349 { store = Array.make n v
350 ; rc = 0
351 ; wc = 0
352 ; len = 0
356 let cbcap b = Array.length b.store;;
358 let cbput ?(update_rc=true) b v =
359 let cap = cbcap b in
360 b.store.(b.wc) <- v;
361 b.wc <- (b.wc + 1) mod cap;
362 if update_rc
363 then b.rc <- b.wc;
364 b.len <- min (b.len + 1) cap;
367 let cbput_dont_update_rc b v = cbput ~update_rc:false b v;;
369 let cbempty b = b.len = 0;;
371 let cbgetg b circular dir =
372 if cbempty b
373 then b.store.(0)
374 else
375 let rc = b.rc + dir in
376 let rc =
377 if circular
378 then (
379 if rc = -1
380 then b.len-1
381 else (
382 if rc >= b.len
383 then 0
384 else rc
387 else bound rc 0 (b.len-1)
389 b.rc <- rc;
390 b.store.(rc);
393 let cbget b = cbgetg b false;;
394 let cbgetc b = cbgetg b true;;
396 let state =
397 { ss = Unix.stdin
398 ; wsfd = Unix.stdin
399 ; stderr = Unix.stderr
400 ; errmsgs = Buffer.create 0
401 ; newerrmsgs = false
402 ; x = 0
403 ; y = 0
404 ; w = 0
405 ; anchor = emptyanchor
406 ; ranchors = []
407 ; layout = []
408 ; maxy = max_int
409 ; tilelru = Queue.create ()
410 ; pagemap = Hashtbl.create 10
411 ; tilemap = Hashtbl.create 10
412 ; pdims = []
413 ; pagecount = 0
414 ; currently = Idle
415 ; mstate = Mnone
416 ; rects = []
417 ; rects1 = []
418 ; prects = Hashtbl.create 1
419 ; text = E.s
420 ; mode = View
421 ; winstate = []
422 ; searchpattern = E.s
423 ; outlines = E.a
424 ; bookmarks = []
425 ; path = E.s
426 ; password = E.s
427 ; nameddest = E.s
428 ; geomcmds = E.s, []
429 ; hists =
430 { nav = cbnew 10 emptyanchor
431 ; pat = cbnew 10 E.s
432 ; pag = cbnew 10 E.s
433 ; sel = cbnew 10 E.s
435 ; memused = 0
436 ; gen = 0
437 ; autoscroll = None
438 ; help = E.a
439 ; docinfo = []
440 ; checkerstexid = None
441 ; prevzoom = (1.0, 0)
442 ; progress = -1.0
443 ; uioh = nouioh
444 ; mpos = (-1, -1)
445 ; keystate = KSnone
446 ; glinks = false
447 ; prevcolumns = None
448 ; winw = -1
449 ; winh = -1
450 ; reprf = noreprf
451 ; origin = E.s
452 ; roam = noroam
453 ; bzoom = false
454 ; lnava = None
455 ; slideshow = 0
459 let calcips h =
460 let d = state.winh - h in
461 max conf.interpagespace ((d + 1) / 2)
464 let rowyh (c, coverA, coverB) b n =
465 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
466 then
467 let _, _, vy, (_, _, h, _) = b.(n) in
468 (vy, h)
469 else
470 let n' = n - coverA in
471 let d = n' mod c in
472 let s = n - d in
473 let e = min state.pagecount (s + c) in
474 let rec find m miny maxh = if m = e then miny, maxh else
475 let _, _, y, (_, _, h, _) = b.(m) in
476 let miny = min miny y in
477 let maxh = max maxh h in
478 find (m+1) miny maxh
479 in find s max_int 0
482 let page_of_y y =
483 let ((c, coverA, coverB) as cl), b =
484 match conf.columns with
485 | Csplit (_, b) | Csingle b -> (1, 0, 0), b
486 | Cmulti (c, b) -> c, b
488 if Array.length b = 0
489 then -1
490 else
491 let rec bsearch nmin nmax =
492 if nmin > nmax
493 then bound nmin 0 (state.pagecount-1)
494 else
495 let n = (nmax + nmin) / 2 in
496 let vy, h = rowyh cl b n in
497 let y0, y1 =
498 if conf.presentation
499 then
500 let ips = calcips h in
501 let y0 = vy - ips in
502 let y1 = vy + h + ips in
503 y0, y1
504 else (
505 if n = 0
506 then 0, vy + h + conf.interpagespace
507 else
508 let y0 = vy - conf.interpagespace in
509 y0, y0 + h + conf.interpagespace
512 if y >= y0 && y < y1
513 then (
514 if c = 1
515 then n
516 else (
517 if n > coverA
518 then
519 if n < state.pagecount - coverB
520 then ((n-coverA)/c)*c + coverA
521 else n
522 else n
525 else (
526 if y > y0
527 then bsearch (n+1) nmax
528 else bsearch nmin (n-1)
531 bsearch 0 (state.pagecount-1);
534 let calcheight () =
535 match conf.columns with
536 | Cmulti ((_, _, _) as cl, b) ->
537 if Array.length b > 0
538 then
539 let y, h = rowyh cl b (Array.length b - 1) in
540 y + h + (if conf.presentation then calcips h else 0)
541 else 0
542 | Csingle b ->
543 if Array.length b > 0
544 then
545 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
546 y + h + (if conf.presentation then calcips h else 0)
547 else 0
548 | Csplit (_, b) ->
549 if Array.length b > 0
550 then
551 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
552 y + h
553 else 0
556 let getpageywh pageno =
557 let pageno = bound pageno 0 (state.pagecount-1) in
558 match conf.columns with
559 | Csingle b ->
560 if Array.length b = 0
561 then 0, 0, 0
562 else
563 let (_, _, y, (_, w, h, _)) = b.(pageno) in
564 let y =
565 if conf.presentation
566 then y - calcips h
567 else y
569 y, w, h
570 | Cmulti (cl, b) ->
571 if Array.length b = 0
572 then 0, 0, 0
573 else
574 let y, h = rowyh cl b pageno in
575 let (_, _, _, (_, w, _, _)) = b.(pageno) in
576 let y =
577 if conf.presentation
578 then y - calcips h
579 else y
581 y, w, h
582 | Csplit (c, b) ->
583 if Array.length b = 0
584 then 0, 0, 0
585 else
586 let n = pageno*c in
587 let (_, _, y, (_, w, h, _)) = b.(n) in
588 y, w / c, h
591 let getpageyh pageno =
592 let y,_,h = getpageywh pageno in
593 y, h;
596 let getpagedim pageno =
597 let rec f ppdim l =
598 match l with
599 | (n, _, _, _) as pdim :: rest ->
600 if n >= pageno
601 then (if n = pageno then pdim else ppdim)
602 else f pdim rest
604 | [] -> ppdim
606 f (-1, -1, -1, -1) state.pdims
609 let getpdimno pageno =
610 let rec f p l =
611 let np = succ p in
612 match l with
613 | (n, _, _, _) :: rest ->
614 if n >= pageno
615 then (if n = pageno then np else p)
616 else f np rest
618 | [] -> p
620 f ~-1 state.pdims
623 let getpagey pageno = fst (getpageyh pageno);;
625 let getanchor1 l =
626 let top =
627 let coloff = l.pagecol * l.pageh in
628 float (l.pagey + coloff) /. float l.pageh
630 let dtop =
631 if l.pagedispy = 0
632 then
634 else (
635 if conf.presentation
636 then float l.pagedispy /. float (calcips l.pageh)
637 else float l.pagedispy /. float conf.interpagespace
640 (l.pageno, top, dtop)
643 let getanchor () =
644 match state.layout with
645 | l :: _ -> getanchor1 l
646 | [] ->
647 let n = page_of_y state.y in
648 if n = -1
649 then state.anchor
650 else
651 let y, h = getpageyh n in
652 let dy = y - state.y in
653 let dtop =
654 if conf.presentation
655 then
656 let ips = calcips h in
657 float (dy + ips) /. float ips
658 else
659 float dy /. float conf.interpagespace
661 (n, 0.0, dtop)
664 let fontpath = ref E.s;;
666 type historder = [ `lastvisit | `title | `path | `file ];;
668 module KeyMap =
669 Map.Make (struct type t = (int * int) let compare = compare end);;
671 let unentS s =
672 let l = String.length s in
673 let b = Buffer.create l in
674 Parser.unent b s 0 l;
675 Buffer.contents b;
678 let home =
679 try Sys.getenv "HOME"
680 with exn ->
681 dolog "cannot determine home directory location: %s" @@ exntos exn;
685 let modifier_of_string = function
686 | "alt" -> Wsi.altmask
687 | "shift" -> Wsi.shiftmask
688 | "ctrl" | "control" -> Wsi.ctrlmask
689 | "meta" -> Wsi.metamask
690 | _ -> 0
693 let keys_of_string s =
694 let key_of_string r s =
695 let elems = Str.full_split r s in
696 let f n k m =
697 let g s =
698 let m1 = modifier_of_string s in
699 if m1 = 0
700 then (Wsi.namekey s, m)
701 else (k, m lor m1)
702 in function
703 | Str.Delim s when n land 1 = 0 -> g s
704 | Str.Text s -> g s
705 | Str.Delim _ -> (k, m)
707 let rec loop n k m = function
708 | [] -> (k, m)
709 | x :: xs ->
710 let k, m = f n k m x in
711 loop (n+1) k m xs
713 loop 0 0 0 elems
715 let elems = Str.split whitere s in
716 List.map (key_of_string (Str.regexp "-")) elems
719 let config_of c attrs =
720 let apply c k v =
722 match k with
723 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
724 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
725 | "case-insensitive-search" -> { c with icase = bool_of_string v }
726 | "preload" -> { c with preload = bool_of_string v }
727 | "page-bias" -> { c with pagebias = int_of_string v }
728 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
729 | "horizontal-scroll-step" ->
730 { c with hscrollstep = max (int_of_string v) 1 }
731 | "auto-scroll-step" ->
732 { c with autoscrollstep = max 0 (int_of_string v) }
733 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
734 | "highlight-links" -> { c with hlinks = bool_of_string v }
735 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
736 | "vertical-margin" ->
737 { c with interpagespace = max 0 (int_of_string v) }
738 | "zoom" ->
739 let zoom = float_of_string v /. 100. in
740 let zoom = max zoom 0.0 in
741 { c with zoom = zoom }
742 | "presentation" -> { c with presentation = bool_of_string v }
743 | "rotation-angle" -> { c with angle = int_of_string v }
744 | "width" -> { c with cwinw = max 20 (int_of_string v) }
745 | "height" -> { c with cwinh = max 20 (int_of_string v) }
746 | "proportional-display" ->
747 let fm =
748 if bool_of_string v
749 then FitProportional
750 else FitWidth
752 { c with fitmodel = fm }
753 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
754 | "pixmap-cache-size" ->
755 { c with memlimit = max 2 (int_of_string_with_suffix v) }
756 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
757 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
758 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
759 | "background-color" -> { c with bgcolor = color_of_string v }
760 | "scrollbar-color" -> { c with sbarcolor = rgba_of_string v }
761 | "scrollbar-handle-color" -> { c with sbarhndlcolor = rgba_of_string v }
762 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
763 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
764 | "mupdf-store-size" ->
765 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
766 | "checkers" -> { c with checkers = bool_of_string v }
767 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
768 | "trim-margins" -> { c with trimmargins = bool_of_string v }
769 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
770 | "uri-launcher" -> { c with urilauncher = unentS v }
771 | "path-launcher" -> { c with pathlauncher = unentS v }
772 | "color-space" -> { c with colorspace = CSTE.of_string v }
773 | "invert-colors" -> { c with invert = bool_of_string v }
774 | "brightness" -> { c with colorscale = float_of_string v }
775 | "columns" ->
776 let (n, _, _) as nab = multicolumns_of_string v in
777 if n < 0
778 then { c with columns = Csplit (-n, E.a) }
779 else { c with columns = Cmulti (nab, E.a) }
780 | "birds-eye-columns" ->
781 { c with beyecolumns = Some (max (int_of_string v) 2) }
782 | "selection-command" -> { c with selcmd = unentS v }
783 | "synctex-command" -> { c with stcmd = unentS v }
784 | "pax-command" -> { c with paxcmd = unentS v }
785 | "askpass-command" -> { c with passcmd = unentS v }
786 | "savepath-command" -> { c with savecmd = unentS v }
787 | "update-cursor" -> { c with updatecurs = bool_of_string v }
788 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
789 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
790 | "use-pbo" -> { c with usepbo = bool_of_string v }
791 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
792 | "horizontal-scrollbar-visible" ->
793 let b =
794 if bool_of_string v
795 then c.scrollb lor scrollbhv
796 else c.scrollb land (lnot scrollbhv)
798 { c with scrollb = b }
799 | "vertical-scrollbar-visible" ->
800 let b =
801 if bool_of_string v
802 then c.scrollb lor scrollbvv
803 else c.scrollb land (lnot scrollbvv)
805 { c with scrollb = b }
806 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
807 | "point-and-x" ->
808 { c with pax =
809 if bool_of_string v
810 then Some 0.0
811 else None }
812 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
813 | "scroll-bar-on-the-left" -> { c with leftscroll = bool_of_string v }
814 | "title" -> { c with title = unentS v }
815 | "last-visit" -> { c with lastvisit = float_of_string v }
816 | "edit-annotations-inline" -> { c with annotinline = bool_of_string v }
817 | "coarse-presentation-positioning" ->
818 { c with coarseprespos = bool_of_string v }
819 | "use-document-css" -> { c with usedoccss = bool_of_string v }
820 | _ -> c
821 with exn ->
822 dolog "error processing attribute (`%S' = `%S'): %s" k v @@ exntos exn;
825 let rec fold c = function
826 | [] -> c
827 | (k, v) :: rest ->
828 let c = apply c k v in
829 fold c rest
831 fold { c with keyhashes = copykeyhashes c } attrs;
834 let fromstring f pos n v d =
835 try f v
836 with exn ->
837 dolog "error processing attribute (%S=%S) at %d\n%s" n v pos @@ exntos exn;
841 let bookmark_of attrs =
842 let rec fold title page rely visy = function
843 | ("title", v) :: rest -> fold v page rely visy rest
844 | ("page", v) :: rest -> fold title v rely visy rest
845 | ("rely", v) :: rest -> fold title page v visy rest
846 | ("visy", v) :: rest -> fold title page rely v rest
847 | _ :: rest -> fold title page rely visy rest
848 | [] -> title, page, rely, visy
850 fold "invalid" "0" "0" "0" attrs
853 let doc_of attrs =
854 let rec fold path key page rely pan visy origin = function
855 | ("path", v) :: rest -> fold v key page rely pan visy origin rest
856 | ("key", v) :: rest -> fold path v page rely pan visy origin rest
857 | ("page", v) :: rest -> fold path key v rely pan visy origin rest
858 | ("rely", v) :: rest -> fold path key page v pan visy origin rest
859 | ("pan", v) :: rest -> fold path key page rely v visy origin rest
860 | ("visy", v) :: rest -> fold path key page rely pan v origin rest
861 | ("origin", v) :: rest -> fold path key page rely pan visy v rest
862 | _ :: rest -> fold path key page rely pan visy origin rest
863 | [] -> path, key, page, rely, pan, visy, origin
865 fold E.s E.s "0" "0" "0" "0" E.s attrs
868 let map_of attrs =
869 let rec fold rs ls = function
870 | ("out", v) :: rest -> fold v ls rest
871 | ("in", v) :: rest -> fold rs v rest
872 | _ :: rest -> fold ls rs rest
873 | [] -> ls, rs
875 fold E.s E.s attrs
878 let findkeyhash c name =
879 try List.assoc name c.keyhashes
880 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
883 let get s =
884 let open Parser in
885 let h = Hashtbl.create 10 in
886 let dc = { defconf with angle = defconf.angle } in
887 let rec toplevel v t spos _ =
888 match t with
889 | Vdata | Vcdata | Vend -> v
890 | Vopen ("llppconfig", _, closed) ->
891 if closed
892 then v
893 else { v with f = llppconfig }
894 | Vopen _ -> parse_error "unexpected subelement at top level" s spos
895 | Vclose _ -> parse_error "unexpected close at top level" s spos
897 and llppconfig v t spos _ =
898 match t with
899 | Vdata | Vcdata -> v
900 | Vend -> parse_error "unexpected end of input in llppconfig" s spos
901 | Vopen ("defaults", attrs, closed) ->
902 let c = config_of dc attrs in
903 setconf dc c;
904 if closed
905 then v
906 else { v with f = defaults }
908 | Vopen ("ui-font", attrs, closed) ->
909 let rec getsize size = function
910 | [] -> size
911 | ("size", v) :: rest ->
912 let size =
913 fromstring int_of_string spos "size" v fstate.fontsize in
914 getsize size rest
915 | l -> getsize size l
917 fstate.fontsize <- getsize fstate.fontsize attrs;
918 if closed
919 then v
920 else { v with f = uifont (Buffer.create 10) }
922 | Vopen ("doc", attrs, closed) ->
923 let pathent, key, spage, srely, span, svisy, origin = doc_of attrs in
924 let path = unentS pathent
925 and origin = unentS origin
926 and pageno = fromstring int_of_string spos "page" spage 0
927 and rely = fromstring float_of_string spos "rely" srely 0.0
928 and pan = fromstring int_of_string spos "pan" span 0
929 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
930 let c = config_of dc attrs in
931 c.key <- key;
932 let anchor = (pageno, rely, visy) in
933 if closed
934 then (Hashtbl.add h path (c, [], pan, anchor, origin); v)
935 else { v with f = doc path origin pan anchor c [] }
937 | Vopen _ ->
938 parse_error "unexpected subelement in llppconfig" s spos
940 | Vclose "llppconfig" -> { v with f = toplevel }
941 | Vclose _ -> parse_error "unexpected close in llppconfig" s spos
943 and defaults v t spos _ =
944 match t with
945 | Vdata | Vcdata -> v
946 | Vend -> parse_error "unexpected end of input in defaults" s spos
947 | Vopen ("keymap", attrs, closed) ->
948 let modename =
949 try List.assoc "mode" attrs
950 with Not_found -> "global" in
951 if closed
952 then v
953 else
954 let ret keymap =
955 let h = findkeyhash dc modename in
956 KeyMap.iter (Hashtbl.replace h) keymap;
957 defaults
959 { v with f = pkeymap ret KeyMap.empty }
961 | Vopen (_, _, _) ->
962 parse_error "unexpected subelement in defaults" s spos
964 | Vclose "defaults" ->
965 { v with f = llppconfig }
967 | Vclose _ -> parse_error "unexpected close in defaults" s spos
969 and uifont b v t spos epos =
970 match t with
971 | Vdata | Vcdata ->
972 Buffer.add_substring b s spos (epos - spos);
974 | Vopen (_, _, _) ->
975 parse_error "unexpected subelement in ui-font" s spos
976 | Vclose "ui-font" ->
977 if emptystr !fontpath
978 then fontpath := Buffer.contents b;
979 { v with f = llppconfig }
980 | Vclose _ -> parse_error "unexpected close in ui-font" s spos
981 | Vend -> parse_error "unexpected end of input in ui-font" s spos
983 and doc path origin pan anchor c bookmarks v t spos _ =
984 match t with
985 | Vdata | Vcdata -> v
986 | Vend -> parse_error "unexpected end of input in doc" s spos
987 | Vopen ("bookmarks", _, closed) ->
988 if closed
989 then v
990 else { v with f = pbookmarks path origin pan anchor c bookmarks }
992 | Vopen ("keymap", attrs, closed) ->
993 let modename =
994 try List.assoc "mode" attrs
995 with Not_found -> "global"
997 if closed
998 then v
999 else
1000 let ret keymap =
1001 let h = findkeyhash c modename in
1002 KeyMap.iter (Hashtbl.replace h) keymap;
1003 doc path origin pan anchor c bookmarks
1005 { v with f = pkeymap ret KeyMap.empty }
1007 | Vopen ("css", [], false) ->
1008 { v with f = pcss path origin pan anchor c bookmarks }
1010 | Vopen (_, _, _) ->
1011 parse_error "unexpected subelement in doc" s spos
1013 | Vclose "doc" ->
1014 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor, origin);
1015 { v with f = llppconfig }
1017 | Vclose _ -> parse_error "unexpected close in doc" s spos
1019 and pcss path origin pan anchor c bookmarks v t spos epos =
1020 match t with
1021 | Vdata | Vcdata ->
1022 let b = Buffer.create 10 in
1023 Buffer.add_substring b s spos (epos - spos);
1024 { v with f = pcss path origin pan anchor
1025 { c with css = Buffer.contents b }
1026 bookmarks }
1027 | Vend -> parse_error "unexpected end of input in css" s spos
1028 | Vopen _ -> parse_error "unexpected subelement in css" s spos
1029 | Vclose "css" -> { v with f = doc path origin pan anchor c bookmarks }
1030 | Vclose _ -> parse_error "unexpected close in css" s spos
1032 and pkeymap ret keymap v t spos _ =
1033 match t with
1034 | Vdata | Vcdata -> v
1035 | Vend -> parse_error "unexpected end of input in keymap" s spos
1036 | Vopen ("map", attrs, closed) ->
1037 let r, l = map_of attrs in
1038 let kss = fromstring keys_of_string spos "in" r [] in
1039 let lss = fromstring keys_of_string spos "out" l [] in
1040 let keymap =
1041 match kss with
1042 | [] -> keymap
1043 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
1044 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
1046 if closed
1047 then { v with f = pkeymap ret keymap }
1048 else
1049 let f () = v in
1050 { v with f = skip "map" f }
1052 | Vopen _ ->
1053 parse_error "unexpected subelement in keymap" s spos
1055 | Vclose "keymap" ->
1056 { v with f = ret keymap }
1058 | Vclose _ -> parse_error "unexpected close in keymap" s spos
1060 and pbookmarks path origin pan anchor c bookmarks v t spos _ =
1061 match t with
1062 | Vdata | Vcdata -> v
1063 | Vend -> parse_error "unexpected end of input in bookmarks" s spos
1064 | Vopen ("item", attrs, closed) ->
1065 let titleent, spage, srely, svisy = bookmark_of attrs in
1066 let page = fromstring int_of_string spos "page" spage 0
1067 and rely = fromstring float_of_string spos "rely" srely 0.0
1068 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1069 let bookmarks =
1070 (unentS titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
1072 if closed
1073 then { v with f = pbookmarks path origin pan anchor c bookmarks }
1074 else
1075 let f () = v in
1076 { v with f = skip "item" f }
1078 | Vopen _ ->
1079 parse_error "unexpected subelement in bookmarks" s spos
1081 | Vclose "bookmarks" ->
1082 { v with f = doc path origin pan anchor c bookmarks }
1084 | Vclose _ -> parse_error "unexpected close in bookmarks" s spos
1086 and skip tag f v t spos _ =
1087 match t with
1088 | Vdata | Vcdata -> v
1089 | Vend ->
1090 parse_error ("unexpected end of input in skipped " ^ tag) s spos
1091 | Vopen (tag', _, closed) ->
1092 if closed
1093 then v
1094 else
1095 let f' () = { v with f = skip tag f } in
1096 { v with f = skip tag' f' }
1097 | Vclose ctag ->
1098 if tag = ctag
1099 then f ()
1100 else parse_error ("unexpected close in skipped " ^ tag) s spos
1103 parse { f = toplevel; accu = () } s;
1104 h, dc;
1107 let do_load f contents =
1108 try f contents
1109 with
1110 | Parser.Parse_error (msg, s, pos) ->
1111 let subs = Parser.subs s pos in
1112 Utils.error "parse error: %s: at %d [..%S..]" msg pos subs
1114 | exn -> Utils.error "parse error: %s" @@ exntos exn
1117 let defconfpath =
1118 let dir =
1119 let xdgconfdir = Utils.getenvwithdef "XDG_CONFIG_HOME" E.s in
1120 if emptystr xdgconfdir
1121 then
1123 let dir = Filename.concat home ".config" in
1124 if Sys.is_directory dir then dir else home
1125 with _ -> home
1126 else xdgconfdir
1128 Filename.concat dir "llpp.conf"
1131 let confpath = ref defconfpath;;
1133 let load2 f default =
1134 match filecontents !confpath with
1135 | contents -> f @@ do_load get contents
1136 | exception Unix.Unix_error (Unix.ENOENT, "open", _) ->
1137 f (Hashtbl.create 0, defconf)
1138 | exception exn ->
1139 dolog "error loading configuration from `%S': %s" !confpath @@ exntos exn;
1140 default
1143 let load1 f = load2 f false;;
1145 let load openlast =
1146 let f (h, dc) =
1147 if openlast
1148 then (
1149 let path, _ =
1150 Hashtbl.fold
1151 (fun path (conf, _, _, _, _) ((_, besttime) as best) ->
1152 if conf.lastvisit > besttime
1153 then (path, conf.lastvisit)
1154 else best)
1156 (state.path, -.infinity)
1158 state.path <- path;
1160 let pc, pb, px, pa, po =
1161 let def = dc, [], 0, emptyanchor, state.origin in
1162 if emptystr state.path
1163 then def
1164 else
1165 let absname = abspath state.path in
1166 match Hashtbl.find h absname with
1167 | (c,b,x,a,_) -> (c,b,x,a,state.origin)
1168 | exception Not_found ->
1169 let exception E of (conf * outline list * int * anchor * string) in
1170 let key = try Digest.file absname |> Digest.to_hex with _ -> E.s in
1171 match (
1172 if emptystr key
1173 then ()
1174 else
1175 Hashtbl.iter (fun p ((c, _, _, _, _) as v) ->
1176 if c.key = key
1177 then (
1178 dolog "will use %s's settings due to matching keys" p;
1179 raise (E v)
1183 with
1184 | _ -> def
1185 | exception E v -> v
1187 setconf defconf dc;
1188 setconf conf pc;
1189 state.bookmarks <- pb;
1190 state.x <- px;
1191 state.origin <- po;
1192 state.anchor <- pa;
1193 cbput state.hists.nav pa;
1194 true
1196 load1 f
1199 let gethist () =
1200 let f (h, _) =
1201 Hashtbl.fold (fun path (pc, pb, px, pa, po) accu ->
1202 (path, pc, pb, px, pa, po) :: accu)
1203 h [];
1205 load2 f []
1208 let add_attrs bb always dc c time =
1209 let o' fmt s =
1210 Buffer.add_string bb "\n ";
1211 Printf.bprintf bb fmt s
1213 let o c fmt s = if c then o' fmt s else ignore in
1214 let ob s a b = o (always || a != b) "%s='%b'" s a
1215 and op s a b = o (always || a <> b) "%s='%b'" s (a != None)
1216 and oi s a b = o (always || a != b) "%s='%d'" s a
1217 and oI s a b = o (always || a != b) "%s='%s'" s (string_with_suffix_of_int a)
1218 and oz s a b = o (always || a <> b) "%s='%g'" s (a*.100.)
1219 and oF s a b = o (always || a <> b) "%s='%f'" s a
1220 and oL s a b = o (always || a <> b) "%s='%Ld'" s a
1221 and oc s a b = o (always || a <> b) "%s='%s'" s (color_to_string a)
1222 and oA s a b = o (always || a <> b) "%s='%s'" s (rgba_to_string a)
1223 and oC s a b = o (always || a <> b) "%s='%s'" s (CSTE.to_string a)
1224 and oR s a b = o (always || a <> b) "%s='%s'" s (irect_to_string a)
1225 and oFm s a b = o (always || a <> b) "%s='%s'" s (FMTE.to_string a)
1226 and oSv s a b m =
1227 o (always || a land m <> b land m) "%s='%b'" s (a land m != 0)
1228 and oPm s a b = o (always || a <> b) "%s='%s'" s (MTE.to_string a)
1229 and os s a b =
1230 o (always || a <> b) "%s='%s'" s @@ Parser.enent a 0 (String.length a)
1231 and oco s a b =
1232 if always || a <> b
1233 then
1234 match a with
1235 | Cmulti ((n, a, b), _) when n > 1 -> o' "%s='%d,%d,%d'" s n a b
1236 | Csplit (n, _) when n > 1 -> o' "%s='%d'" s ~-n
1237 | Cmulti _ | Csplit _ | Csingle _ -> ()
1238 and obeco s a b =
1239 if always || a <> b
1240 then
1241 match a with
1242 | Some c when c > 1 -> o' "%s='%d'" s c
1243 | _ -> ()
1245 oi "width" c.cwinw dc.cwinw;
1246 oi "height" c.cwinh dc.cwinh;
1247 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
1248 oi "scroll-handle-height" c.scrollh dc.scrollh;
1249 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
1250 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
1251 ob "case-insensitive-search" c.icase dc.icase;
1252 ob "preload" c.preload dc.preload;
1253 oi "page-bias" c.pagebias dc.pagebias;
1254 oi "scroll-step" c.scrollstep dc.scrollstep;
1255 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
1256 ob "max-height-fit" c.maxhfit dc.maxhfit;
1257 ob "highlight-links" c.hlinks dc.hlinks;
1258 ob "under-cursor-info" c.underinfo dc.underinfo;
1259 oi "vertical-margin" c.interpagespace dc.interpagespace;
1260 oz "zoom" c.zoom dc.zoom;
1261 ob "presentation" c.presentation dc.presentation;
1262 oi "rotation-angle" c.angle dc.angle;
1263 oFm "fit-model" c.fitmodel dc.fitmodel;
1264 oI "pixmap-cache-size" c.memlimit dc.memlimit;
1265 oi "tex-count" c.texcount dc.texcount;
1266 oi "slice-height" c.sliceheight dc.sliceheight;
1267 oi "thumbnail-width" c.thumbw dc.thumbw;
1268 oc "background-color" c.bgcolor dc.bgcolor;
1269 oA "scrollbar-color" c.sbarcolor dc.sbarcolor;
1270 oA "scrollbar-handle-color" c.sbarhndlcolor dc.sbarhndlcolor;
1271 oi "tile-width" c.tilew dc.tilew;
1272 oi "tile-height" c.tileh dc.tileh;
1273 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
1274 ob "checkers" c.checkers dc.checkers;
1275 oi "aalevel" c.aalevel dc.aalevel;
1276 ob "trim-margins" c.trimmargins dc.trimmargins;
1277 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
1278 os "uri-launcher" c.urilauncher dc.urilauncher;
1279 os "path-launcher" c.pathlauncher dc.pathlauncher;
1280 oC "color-space" c.colorspace dc.colorspace;
1281 ob "invert-colors" c.invert dc.invert;
1282 oF "brightness" c.colorscale dc.colorscale;
1283 oco "columns" c.columns dc.columns;
1284 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
1285 os "selection-command" c.selcmd dc.selcmd;
1286 os "synctex-command" c.stcmd dc.stcmd;
1287 os "pax-command" c.paxcmd dc.paxcmd;
1288 os "askpass-command" c.passcmd dc.passcmd;
1289 os "savepath-command" c.savecmd dc.savecmd;
1290 ob "update-cursor" c.updatecurs dc.updatecurs;
1291 oi "hint-font-size" c.hfsize dc.hfsize;
1292 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
1293 oF "page-scroll-scale" c.pgscale dc.pgscale;
1294 ob "use-pbo" c.usepbo dc.usepbo;
1295 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
1296 ob "remote-in-a-new-instance" c.riani dc.riani;
1297 op "point-and-x" c.pax dc.pax;
1298 oPm "point-and-x-mark" c.paxmark dc.paxmark;
1299 ob "scroll-bar-on-the-left" c.leftscroll dc.leftscroll;
1300 if not always
1301 then os "title" c.title dc.title;
1302 oL "last-visit" (Int64.of_float time) 0L;
1303 ob "edit-annotations-inline" c.annotinline dc.annotinline;
1304 ob "coarse-presentation-positioning" c.coarseprespos dc.coarseprespos;
1305 ob "use-document-css" c.usedoccss dc.usedoccss;
1308 let keymapsbuf always dc c =
1309 let open Buffer in
1310 let bb = create 16 in
1311 let rec loop = function
1312 | [] -> ()
1313 | (modename, h) :: rest ->
1314 let dh = findkeyhash dc modename in
1315 if always || h <> dh
1316 then (
1317 if Hashtbl.length h > 0
1318 then (
1319 if length bb > 0 then add_char bb '\n';
1320 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
1321 Hashtbl.iter (fun i o ->
1322 if always || match Hashtbl.find dh i
1323 with | dO -> dO <> o | exception Not_found -> false
1324 then
1325 let addkm (k, m) =
1326 if Wsi.withctrl m then add_string bb "ctrl-";
1327 if Wsi.withalt m then add_string bb "alt-";
1328 if Wsi.withshift m then add_string bb "shift-";
1329 if Wsi.withmeta m then add_string bb "meta-";
1330 add_string bb (Wsi.keyname k);
1332 let addkms l =
1333 let rec loop = function
1334 | [] -> ()
1335 | km :: [] -> addkm km
1336 | km :: rest -> addkm km; add_char bb ' '; loop rest
1338 loop l
1340 add_string bb "<map in='";
1341 addkm i;
1342 match o with
1343 | KMinsrt km ->
1344 add_string bb "' out='"; addkm km; add_string bb "'/>\n"
1346 | KMinsrl kms ->
1347 add_string bb "' out='"; addkms kms; add_string bb "'/>\n"
1349 | KMmulti (ins, kms) ->
1350 add_char bb ' '; addkms ins; add_string bb "' out='";
1351 addkms kms; add_string bb "'/>\n"
1352 ) h;
1353 add_string bb "</keymap>";
1356 loop rest
1358 loop c.keyhashes;
1362 let keystostrlist c =
1363 let rec loop accu = function
1364 | [] -> accu
1365 | (modename, h) :: rest ->
1366 let accu =
1367 if Hashtbl.length h > 0
1368 then (
1369 let accu = Printf.sprintf "\xc2\xb7Keys for %s" modename :: accu in
1370 Hashtbl.fold (fun i o a ->
1371 let bb = Buffer.create 10 in
1372 let addkm (k, m) =
1373 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
1374 if Wsi.withalt m then Buffer.add_string bb "alt-";
1375 if Wsi.withshift m then Buffer.add_string bb "shift-";
1376 if Wsi.withmeta m then Buffer.add_string bb "meta-";
1377 Buffer.add_string bb (Wsi.keyname k);
1379 let addkms l =
1380 let rec loop = function
1381 | [] -> ()
1382 | km :: [] -> addkm km
1383 | km :: rest ->
1384 addkm km; Buffer.add_char bb ' ';
1385 loop rest
1387 loop l
1389 addkm i;
1390 Buffer.add_char bb '\t';
1391 begin match o with
1392 | KMinsrt km ->
1393 addkm km
1395 | KMinsrl kms ->
1396 addkms kms
1398 | KMmulti (ins, kms) ->
1399 Buffer.add_char bb ' ';
1400 addkms ins;
1401 Buffer.add_string bb "\t";
1402 addkms kms
1403 end;
1404 Buffer.contents bb :: a
1405 ) h accu
1407 else accu
1409 loop accu rest
1411 loop [] c.keyhashes
1414 let save1 bb leavebirdseye x h dc =
1415 let uifontsize = fstate.fontsize in
1416 let dc = if conf.bedefault then conf else dc in
1417 Buffer.add_string bb "<llppconfig>\n";
1419 if nonemptystr !fontpath
1420 then
1421 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
1422 uifontsize
1423 !fontpath
1424 else (
1425 if uifontsize <> 14
1426 then
1427 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
1430 Buffer.add_string bb "<defaults";
1431 add_attrs bb true dc dc nan;
1432 let kb = keymapsbuf true dc dc in
1433 if Buffer.length kb > 0
1434 then (
1435 Buffer.add_string bb ">\n";
1436 Buffer.add_buffer bb kb;
1437 Buffer.add_string bb "\n</defaults>\n";
1439 else Buffer.add_string bb "/>\n";
1441 let adddoc path pan anchor c bookmarks time origin =
1442 if bookmarks == [] && c = dc && anchor = emptyanchor
1443 then ()
1444 else (
1445 Printf.bprintf bb "<doc path='%s'"
1446 (Parser.enent path 0 (String.length path));
1448 if nonemptystr c.key
1449 then
1450 Printf.bprintf bb "\n key='%s'" c.key;
1452 if nonemptystr origin
1453 then Printf.bprintf bb "\n origin='%s'"
1454 (Parser.enent origin 0 (String.length origin));
1456 if anchor <> emptyanchor
1457 then (
1458 let n, rely, visy = anchor in
1459 Printf.bprintf bb "\n page='%d'" n;
1461 if rely > 1e-6
1462 then Printf.bprintf bb " rely='%f'" rely;
1464 if abs_float visy > 1e-6
1465 then Printf.bprintf bb " visy='%f'" visy;
1468 if pan != 0
1469 then Printf.bprintf bb " pan='%d'" pan;
1471 add_attrs bb false dc c time;
1472 if nonemptystr c.css
1473 then Printf.bprintf bb ">\n <css><![CDATA[%s]]></css>" c.css;
1474 let kb = keymapsbuf false dc c in
1476 begin match bookmarks with
1477 | [] ->
1478 if Buffer.length kb > 0
1479 then (
1480 Buffer.add_string bb ">\n";
1481 Buffer.add_buffer bb kb;
1482 Buffer.add_string bb "\n</doc>\n";
1484 else
1485 if nonemptystr c.css
1486 then Buffer.add_string bb "\n</doc>\n"
1487 else Buffer.add_string bb "/>\n"
1488 | _ ->
1489 Buffer.add_string bb ">\n<bookmarks>\n";
1490 List.iter (fun (title, _, kind) ->
1491 begin match kind with
1492 | Oanchor (page, rely, visy) ->
1493 Printf.bprintf bb
1494 "<item title='%s' page='%d'"
1495 (Parser.enent title 0 (String.length title))
1496 page
1498 if rely > 1e-6
1499 then
1500 Printf.bprintf bb " rely='%f'" rely
1502 if abs_float visy > 1e-6
1503 then
1504 Printf.bprintf bb " visy='%f'" visy
1506 | Ohistory _ | Onone | Ouri _ | Oremote _
1507 | Oremotedest _ | Olaunch _ ->
1508 failwith "unexpected link in bookmarks"
1509 end;
1510 Buffer.add_string bb "/>\n";
1511 ) bookmarks;
1512 Buffer.add_string bb "</bookmarks>";
1513 if Buffer.length kb > 0
1514 then (
1515 Buffer.add_string bb "\n";
1516 Buffer.add_buffer bb kb;
1518 Buffer.add_string bb "\n</doc>\n";
1519 end;
1523 let pan, conf =
1524 match state.mode with
1525 | Birdseye (c, pan, _, _, _) ->
1526 let beyecolumns =
1527 match conf.columns with
1528 | Cmulti ((c, _, _), _) -> Some c
1529 | Csingle _ -> None
1530 | Csplit _ -> None
1531 and columns =
1532 match c.columns with
1533 | Cmulti (c, _) -> Cmulti (c, E.a)
1534 | Csingle _ -> Csingle E.a
1535 | Csplit _ -> failwith "quit from bird's eye while split"
1537 pan, { c with beyecolumns = beyecolumns; columns = columns }
1538 | Textentry _
1539 | View
1540 | LinkNav _ -> x, conf
1542 let docpath = if nonemptystr state.path then abspath state.path else E.s in
1543 if nonemptystr docpath
1544 then (
1545 adddoc docpath pan (getanchor ())
1547 let autoscrollstep =
1548 match state.autoscroll with
1549 | Some step -> step
1550 | None -> conf.autoscrollstep
1552 begin match state.mode with
1553 | Birdseye beye -> leavebirdseye beye true
1554 | Textentry _
1555 | View
1556 | LinkNav _ -> ()
1557 end;
1558 let key = try Digest.file docpath |> Digest.to_hex
1559 with _ -> E.s in
1560 { conf with autoscrollstep; key }
1562 state.bookmarks
1563 (now ())
1564 state.origin
1566 Hashtbl.iter (fun path (c, bookmarks, x, anchor, origin) ->
1567 if docpath <> abspath path
1568 then adddoc path x anchor c bookmarks c.lastvisit origin
1569 ) h;
1570 Buffer.add_string bb "</llppconfig>\n";
1571 true;
1574 let save leavebirdseye =
1575 let relx = float state.x /. float state.winw in
1576 let w, h, x =
1577 let cx w = truncate (relx *. float w) in
1578 List.fold_left
1579 (fun (w, h, x) ws ->
1580 match ws with
1581 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
1582 | Wsi.MaxVert -> (w, conf.cwinh, x)
1583 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
1585 (state.winw, state.winh, state.x) state.winstate
1587 conf.cwinw <- w;
1588 conf.cwinh <- h;
1589 let bb = Buffer.create 32768 in
1590 let save2 (h, dc) =
1591 save1 bb leavebirdseye x h dc
1593 if load1 save2 && Buffer.length bb > 0
1594 then
1596 let tmp = !confpath ^ ".tmp" in
1597 let oc = open_out_bin tmp in
1598 Buffer.output_buffer oc bb;
1599 close_out oc;
1600 Unix.rename tmp !confpath;
1601 with exn ->
1602 dolog "error saving configuration: %s" @@ exntos exn
1605 let gc () =
1606 let href = ref @@ Hashtbl.create 0 in
1607 let cref = ref defconf in
1608 let push (h, dc) =
1609 let f path v =
1610 if Sys.file_exists path
1611 then Some v
1612 else (dolog "removing entry for '%s'" path; None) in
1613 Hashtbl.filter_map_inplace f h;
1614 href := h;
1615 cref := dc;
1616 true
1618 ignore (load1 push);
1619 let bb = Buffer.create 32768 in
1620 let save2 (_h, dc) = save1 bb (fun _ _ -> ()) 0 !href dc in
1621 if load1 save2 && Buffer.length bb > 0
1622 then (
1624 let tmp = !confpath ^ ".tmp" in
1625 let oc = open_out_bin tmp in
1626 Buffer.output_buffer oc bb;
1627 close_out oc;
1628 Unix.rename tmp !confpath;
1629 with exn ->
1630 dolog "error saving configuration: %s" @@ exntos exn
1634 let logcurrently = function
1635 | Idle -> dolog "Idle"
1636 | Loading (l, gen) ->
1637 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1638 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1639 dolog "Tiling %d[%d,%d] page=%s cs=%s angle=%d"
1640 l.pageno col row (~> pageopaque)
1641 (CSTE.to_string colorspace) angle;
1642 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1643 angle gen conf.angle state.gen
1644 tilew tileh
1645 conf.tilew conf.tileh
1646 | Outlining _ -> dolog "outlining"