Nomenclature
[llpp.git] / config.ml
blobe8ead147708253ac7c4986a980a90c62d32fa480
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 | Csingle b -> (1, 0, 0), b
486 | Cmulti (c, b) -> c, b
487 | Csplit (_, b) -> (1, 0, 0), b
489 if Array.length b = 0
490 then -1
491 else
492 let rec bsearch nmin nmax =
493 if nmin > nmax
494 then bound nmin 0 (state.pagecount-1)
495 else
496 let n = (nmax + nmin) / 2 in
497 let vy, h = rowyh cl b n in
498 let y0, y1 =
499 if conf.presentation
500 then
501 let ips = calcips h in
502 let y0 = vy - ips in
503 let y1 = vy + h + ips in
504 y0, y1
505 else (
506 if n = 0
507 then 0, vy + h + conf.interpagespace
508 else
509 let y0 = vy - conf.interpagespace in
510 y0, y0 + h + conf.interpagespace
513 if y >= y0 && y < y1
514 then (
515 if c = 1
516 then n
517 else (
518 if n > coverA
519 then
520 if n < state.pagecount - coverB
521 then ((n-coverA)/c)*c + coverA
522 else n
523 else n
526 else (
527 if y > y0
528 then bsearch (n+1) nmax
529 else bsearch nmin (n-1)
532 bsearch 0 (state.pagecount-1);
535 let calcheight () =
536 match conf.columns with
537 | Cmulti ((_, _, _) as cl, b) ->
538 if Array.length b > 0
539 then
540 let y, h = rowyh cl b (Array.length b - 1) in
541 y + h + (if conf.presentation then calcips h else 0)
542 else 0
543 | Csingle b ->
544 if Array.length b > 0
545 then
546 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
547 y + h + (if conf.presentation then calcips h else 0)
548 else 0
549 | Csplit (_, b) ->
550 if Array.length b > 0
551 then
552 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
553 y + h
554 else 0
557 let getpageywh pageno =
558 let pageno = bound pageno 0 (state.pagecount-1) in
559 match conf.columns with
560 | Csingle b ->
561 if Array.length b = 0
562 then 0, 0, 0
563 else
564 let (_, _, y, (_, w, h, _)) = b.(pageno) in
565 let y =
566 if conf.presentation
567 then y - calcips h
568 else y
570 y, w, h
571 | Cmulti (cl, b) ->
572 if Array.length b = 0
573 then 0, 0, 0
574 else
575 let y, h = rowyh cl b pageno in
576 let (_, _, _, (_, w, _, _)) = b.(pageno) in
577 let y =
578 if conf.presentation
579 then y - calcips h
580 else y
582 y, w, h
583 | Csplit (c, b) ->
584 if Array.length b = 0
585 then 0, 0, 0
586 else
587 let n = pageno*c in
588 let (_, _, y, (_, w, h, _)) = b.(n) in
589 y, w / c, h
592 let getpageyh pageno =
593 let y,_,h = getpageywh pageno in
594 y, h;
597 let getpagedim pageno =
598 let rec f ppdim l =
599 match l with
600 | (n, _, _, _) as pdim :: rest ->
601 if n >= pageno
602 then (if n = pageno then pdim else ppdim)
603 else f pdim rest
605 | [] -> ppdim
607 f (-1, -1, -1, -1) state.pdims
610 let getpdimno pageno =
611 let rec f p l =
612 let np = succ p in
613 match l with
614 | (n, _, _, _) :: rest ->
615 if n >= pageno
616 then (if n = pageno then np else p)
617 else f np rest
619 | [] -> p
621 f ~-1 state.pdims
624 let getpagey pageno = fst (getpageyh pageno);;
626 let getanchor1 l =
627 let top =
628 let coloff = l.pagecol * l.pageh in
629 float (l.pagey + coloff) /. float l.pageh
631 let dtop =
632 if l.pagedispy = 0
633 then
635 else (
636 if conf.presentation
637 then float l.pagedispy /. float (calcips l.pageh)
638 else float l.pagedispy /. float conf.interpagespace
641 (l.pageno, top, dtop)
644 let getanchor () =
645 match state.layout with
646 | l :: _ -> getanchor1 l
647 | [] ->
648 let n = page_of_y state.y in
649 if n = -1
650 then state.anchor
651 else
652 let y, h = getpageyh n in
653 let dy = y - state.y in
654 let dtop =
655 if conf.presentation
656 then
657 let ips = calcips h in
658 float (dy + ips) /. float ips
659 else
660 float dy /. float conf.interpagespace
662 (n, 0.0, dtop)
665 let fontpath = ref E.s;;
667 type historder = [ `lastvisit | `title | `path | `file ];;
669 module KeyMap =
670 Map.Make (struct type t = (int * int) let compare = compare end);;
672 let unentS s =
673 let l = String.length s in
674 let b = Buffer.create l in
675 Parser.unent b s 0 l;
676 Buffer.contents b;
679 let home =
680 try Sys.getenv "HOME"
681 with exn ->
682 dolog "cannot determine home directory location: %s" @@ exntos exn;
686 let modifier_of_string = function
687 | "alt" -> Wsi.altmask
688 | "shift" -> Wsi.shiftmask
689 | "ctrl" | "control" -> Wsi.ctrlmask
690 | "meta" -> Wsi.metamask
691 | _ -> 0
694 let keys_of_string s =
695 let key_of_string r s =
696 let elems = Str.full_split r s in
697 let f n k m =
698 let g s =
699 let m1 = modifier_of_string s in
700 if m1 = 0
701 then (Wsi.namekey s, m)
702 else (k, m lor m1)
703 in function
704 | Str.Delim s when n land 1 = 0 -> g s
705 | Str.Text s -> g s
706 | Str.Delim _ -> (k, m)
708 let rec loop n k m = function
709 | [] -> (k, m)
710 | x :: xs ->
711 let k, m = f n k m x in
712 loop (n+1) k m xs
714 loop 0 0 0 elems
716 let elems = Str.split whitere s in
717 List.map (key_of_string (Str.regexp "-")) elems
720 let config_of c attrs =
721 let apply c k v =
723 match k with
724 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
725 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
726 | "case-insensitive-search" -> { c with icase = bool_of_string v }
727 | "preload" -> { c with preload = bool_of_string v }
728 | "page-bias" -> { c with pagebias = int_of_string v }
729 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
730 | "horizontal-scroll-step" ->
731 { c with hscrollstep = max (int_of_string v) 1 }
732 | "auto-scroll-step" ->
733 { c with autoscrollstep = max 0 (int_of_string v) }
734 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
735 | "highlight-links" -> { c with hlinks = bool_of_string v }
736 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
737 | "vertical-margin" ->
738 { c with interpagespace = max 0 (int_of_string v) }
739 | "zoom" ->
740 let zoom = float_of_string v /. 100. in
741 let zoom = max zoom 0.0 in
742 { c with zoom = zoom }
743 | "presentation" -> { c with presentation = bool_of_string v }
744 | "rotation-angle" -> { c with angle = int_of_string v }
745 | "width" -> { c with cwinw = max 20 (int_of_string v) }
746 | "height" -> { c with cwinh = max 20 (int_of_string v) }
747 | "proportional-display" ->
748 let fm =
749 if bool_of_string v
750 then FitProportional
751 else FitWidth
753 { c with fitmodel = fm }
754 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
755 | "pixmap-cache-size" ->
756 { c with memlimit = max 2 (int_of_string_with_suffix v) }
757 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
758 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
759 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
760 | "background-color" -> { c with bgcolor = color_of_string v }
761 | "scrollbar-color" -> { c with sbarcolor = rgba_of_string v }
762 | "scrollbar-handle-color" -> { c with sbarhndlcolor = rgba_of_string v }
763 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
764 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
765 | "mupdf-store-size" ->
766 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
767 | "checkers" -> { c with checkers = bool_of_string v }
768 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
769 | "trim-margins" -> { c with trimmargins = bool_of_string v }
770 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
771 | "uri-launcher" -> { c with urilauncher = unentS v }
772 | "path-launcher" -> { c with pathlauncher = unentS v }
773 | "color-space" -> { c with colorspace = CSTE.of_string v }
774 | "invert-colors" -> { c with invert = bool_of_string v }
775 | "brightness" -> { c with colorscale = float_of_string v }
776 | "columns" ->
777 let (n, _, _) as nab = multicolumns_of_string v in
778 if n < 0
779 then { c with columns = Csplit (-n, E.a) }
780 else { c with columns = Cmulti (nab, E.a) }
781 | "birds-eye-columns" ->
782 { c with beyecolumns = Some (max (int_of_string v) 2) }
783 | "selection-command" -> { c with selcmd = unentS v }
784 | "synctex-command" -> { c with stcmd = unentS v }
785 | "pax-command" -> { c with paxcmd = unentS v }
786 | "askpass-command" -> { c with passcmd = unentS v }
787 | "savepath-command" -> { c with savecmd = unentS v }
788 | "update-cursor" -> { c with updatecurs = bool_of_string v }
789 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
790 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
791 | "use-pbo" -> { c with usepbo = bool_of_string v }
792 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
793 | "horizontal-scrollbar-visible" ->
794 let b =
795 if bool_of_string v
796 then c.scrollb lor scrollbhv
797 else c.scrollb land (lnot scrollbhv)
799 { c with scrollb = b }
800 | "vertical-scrollbar-visible" ->
801 let b =
802 if bool_of_string v
803 then c.scrollb lor scrollbvv
804 else c.scrollb land (lnot scrollbvv)
806 { c with scrollb = b }
807 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
808 | "point-and-x" ->
809 { c with pax =
810 if bool_of_string v
811 then Some 0.0
812 else None }
813 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
814 | "scroll-bar-on-the-left" -> { c with leftscroll = bool_of_string v }
815 | "title" -> { c with title = unentS v }
816 | "last-visit" -> { c with lastvisit = float_of_string v }
817 | "edit-annotations-inline" -> { c with annotinline = bool_of_string v }
818 | "coarse-presentation-positioning" ->
819 { c with coarseprespos = bool_of_string v }
820 | "use-document-css" -> { c with usedoccss = bool_of_string v }
821 | _ -> c
822 with exn ->
823 dolog "error processing attribute (`%S' = `%S'): %s" k v @@ exntos exn;
826 let rec fold c = function
827 | [] -> c
828 | (k, v) :: rest ->
829 let c = apply c k v in
830 fold c rest
832 fold { c with keyhashes = copykeyhashes c } attrs;
835 let fromstring f pos n v d =
836 try f v
837 with exn ->
838 dolog "error processing attribute (%S=%S) at %d\n%s" n v pos @@ exntos exn;
842 let bookmark_of attrs =
843 let rec fold title page rely visy = function
844 | ("title", v) :: rest -> fold v page rely visy rest
845 | ("page", v) :: rest -> fold title v rely visy rest
846 | ("rely", v) :: rest -> fold title page v visy rest
847 | ("visy", v) :: rest -> fold title page rely v rest
848 | _ :: rest -> fold title page rely visy rest
849 | [] -> title, page, rely, visy
851 fold "invalid" "0" "0" "0" attrs
854 let doc_of attrs =
855 let rec fold path key page rely pan visy origin = function
856 | ("path", v) :: rest -> fold v key page rely pan visy origin rest
857 | ("key", v) :: rest -> fold path v page rely pan visy origin rest
858 | ("page", v) :: rest -> fold path key v rely pan visy origin rest
859 | ("rely", v) :: rest -> fold path key page v pan visy origin rest
860 | ("pan", v) :: rest -> fold path key page rely v visy origin rest
861 | ("visy", v) :: rest -> fold path key page rely pan v origin rest
862 | ("origin", v) :: rest -> fold path key page rely pan visy v rest
863 | _ :: rest -> fold path key page rely pan visy origin rest
864 | [] -> path, key, page, rely, pan, visy, origin
866 fold E.s E.s "0" "0" "0" "0" E.s attrs
869 let map_of attrs =
870 let rec fold rs ls = function
871 | ("out", v) :: rest -> fold v ls rest
872 | ("in", v) :: rest -> fold rs v rest
873 | _ :: rest -> fold ls rs rest
874 | [] -> ls, rs
876 fold E.s E.s attrs
879 let findkeyhash c name =
880 try List.assoc name c.keyhashes
881 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
884 let get s =
885 let open Parser in
886 let h = Hashtbl.create 10 in
887 let dc = { defconf with angle = defconf.angle } in
888 let rec toplevel v t spos _ =
889 match t with
890 | Vdata | Vcdata | Vend -> v
891 | Vopen ("llppconfig", _, closed) ->
892 if closed
893 then v
894 else { v with f = llppconfig }
895 | Vopen _ -> parse_error "unexpected subelement at top level" s spos
896 | Vclose _ -> parse_error "unexpected close at top level" s spos
898 and llppconfig v t spos _ =
899 match t with
900 | Vdata | Vcdata -> v
901 | Vend -> parse_error "unexpected end of input in llppconfig" s spos
902 | Vopen ("defaults", attrs, closed) ->
903 let c = config_of dc attrs in
904 setconf dc c;
905 if closed
906 then v
907 else { v with f = defaults }
909 | Vopen ("ui-font", attrs, closed) ->
910 let rec getsize size = function
911 | [] -> size
912 | ("size", v) :: rest ->
913 let size =
914 fromstring int_of_string spos "size" v fstate.fontsize in
915 getsize size rest
916 | l -> getsize size l
918 fstate.fontsize <- getsize fstate.fontsize attrs;
919 if closed
920 then v
921 else { v with f = uifont (Buffer.create 10) }
923 | Vopen ("doc", attrs, closed) ->
924 let pathent, key, spage, srely, span, svisy, origin = doc_of attrs in
925 let path = unentS pathent
926 and origin = unentS origin
927 and pageno = fromstring int_of_string spos "page" spage 0
928 and rely = fromstring float_of_string spos "rely" srely 0.0
929 and pan = fromstring int_of_string spos "pan" span 0
930 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
931 let c = config_of dc attrs in
932 c.key <- key;
933 let anchor = (pageno, rely, visy) in
934 if closed
935 then (Hashtbl.add h path (c, [], pan, anchor, origin); v)
936 else { v with f = doc path origin pan anchor c [] }
938 | Vopen _ ->
939 parse_error "unexpected subelement in llppconfig" s spos
941 | Vclose "llppconfig" -> { v with f = toplevel }
942 | Vclose _ -> parse_error "unexpected close in llppconfig" s spos
944 and defaults v t spos _ =
945 match t with
946 | Vdata | Vcdata -> v
947 | Vend -> parse_error "unexpected end of input in defaults" s spos
948 | Vopen ("keymap", attrs, closed) ->
949 let modename =
950 try List.assoc "mode" attrs
951 with Not_found -> "global" in
952 if closed
953 then v
954 else
955 let ret keymap =
956 let h = findkeyhash dc modename in
957 KeyMap.iter (Hashtbl.replace h) keymap;
958 defaults
960 { v with f = pkeymap ret KeyMap.empty }
962 | Vopen (_, _, _) ->
963 parse_error "unexpected subelement in defaults" s spos
965 | Vclose "defaults" ->
966 { v with f = llppconfig }
968 | Vclose _ -> parse_error "unexpected close in defaults" s spos
970 and uifont b v t spos epos =
971 match t with
972 | Vdata | Vcdata ->
973 Buffer.add_substring b s spos (epos - spos);
975 | Vopen (_, _, _) ->
976 parse_error "unexpected subelement in ui-font" s spos
977 | Vclose "ui-font" ->
978 if emptystr !fontpath
979 then fontpath := Buffer.contents b;
980 { v with f = llppconfig }
981 | Vclose _ -> parse_error "unexpected close in ui-font" s spos
982 | Vend -> parse_error "unexpected end of input in ui-font" s spos
984 and doc path origin pan anchor c bookmarks v t spos _ =
985 match t with
986 | Vdata | Vcdata -> v
987 | Vend -> parse_error "unexpected end of input in doc" s spos
988 | Vopen ("bookmarks", _, closed) ->
989 if closed
990 then v
991 else { v with f = pbookmarks path origin pan anchor c bookmarks }
993 | Vopen ("keymap", attrs, closed) ->
994 let modename =
995 try List.assoc "mode" attrs
996 with Not_found -> "global"
998 if closed
999 then v
1000 else
1001 let ret keymap =
1002 let h = findkeyhash c modename in
1003 KeyMap.iter (Hashtbl.replace h) keymap;
1004 doc path origin pan anchor c bookmarks
1006 { v with f = pkeymap ret KeyMap.empty }
1008 | Vopen ("css", [], false) ->
1009 { v with f = pcss path origin pan anchor c bookmarks }
1011 | Vopen (_, _, _) ->
1012 parse_error "unexpected subelement in doc" s spos
1014 | Vclose "doc" ->
1015 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor, origin);
1016 { v with f = llppconfig }
1018 | Vclose _ -> parse_error "unexpected close in doc" s spos
1020 and pcss path origin pan anchor c bookmarks v t spos epos =
1021 match t with
1022 | Vdata | Vcdata ->
1023 let b = Buffer.create 10 in
1024 Buffer.add_substring b s spos (epos - spos);
1025 { v with f = pcss path origin pan anchor
1026 { c with css = Buffer.contents b }
1027 bookmarks }
1028 | Vend -> parse_error "unexpected end of input in css" s spos
1029 | Vopen _ -> parse_error "unexpected subelement in css" s spos
1030 | Vclose "css" -> { v with f = doc path origin pan anchor c bookmarks }
1031 | Vclose _ -> parse_error "unexpected close in css" s spos
1033 and pkeymap ret keymap v t spos _ =
1034 match t with
1035 | Vdata | Vcdata -> v
1036 | Vend -> parse_error "unexpected end of input in keymap" s spos
1037 | Vopen ("map", attrs, closed) ->
1038 let r, l = map_of attrs in
1039 let kss = fromstring keys_of_string spos "in" r [] in
1040 let lss = fromstring keys_of_string spos "out" l [] in
1041 let keymap =
1042 match kss with
1043 | [] -> keymap
1044 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
1045 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
1047 if closed
1048 then { v with f = pkeymap ret keymap }
1049 else
1050 let f () = v in
1051 { v with f = skip "map" f }
1053 | Vopen _ ->
1054 parse_error "unexpected subelement in keymap" s spos
1056 | Vclose "keymap" ->
1057 { v with f = ret keymap }
1059 | Vclose _ -> parse_error "unexpected close in keymap" s spos
1061 and pbookmarks path origin pan anchor c bookmarks v t spos _ =
1062 match t with
1063 | Vdata | Vcdata -> v
1064 | Vend -> parse_error "unexpected end of input in bookmarks" s spos
1065 | Vopen ("item", attrs, closed) ->
1066 let titleent, spage, srely, svisy = bookmark_of attrs in
1067 let page = fromstring int_of_string spos "page" spage 0
1068 and rely = fromstring float_of_string spos "rely" srely 0.0
1069 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1070 let bookmarks =
1071 (unentS titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
1073 if closed
1074 then { v with f = pbookmarks path origin pan anchor c bookmarks }
1075 else
1076 let f () = v in
1077 { v with f = skip "item" f }
1079 | Vopen _ ->
1080 parse_error "unexpected subelement in bookmarks" s spos
1082 | Vclose "bookmarks" ->
1083 { v with f = doc path origin pan anchor c bookmarks }
1085 | Vclose _ -> parse_error "unexpected close in bookmarks" s spos
1087 and skip tag f v t spos _ =
1088 match t with
1089 | Vdata | Vcdata -> v
1090 | Vend ->
1091 parse_error ("unexpected end of input in skipped " ^ tag) s spos
1092 | Vopen (tag', _, closed) ->
1093 if closed
1094 then v
1095 else
1096 let f' () = { v with f = skip tag f } in
1097 { v with f = skip tag' f' }
1098 | Vclose ctag ->
1099 if tag = ctag
1100 then f ()
1101 else parse_error ("unexpected close in skipped " ^ tag) s spos
1104 parse { f = toplevel; accu = () } s;
1105 h, dc;
1108 let do_load f contents =
1109 try f contents
1110 with
1111 | Parser.Parse_error (msg, s, pos) ->
1112 let subs = Parser.subs s pos in
1113 Utils.error "parse error: %s: at %d [..%S..]" msg pos subs
1115 | exn -> Utils.error "parse error: %s" @@ exntos exn
1118 let defconfpath =
1119 let dir =
1120 let xdgconfdir = Utils.getenvwithdef "XDG_CONFIG_HOME" E.s in
1121 if emptystr xdgconfdir
1122 then
1124 let dir = Filename.concat home ".config" in
1125 if Sys.is_directory dir then dir else home
1126 with _ -> home
1127 else xdgconfdir
1129 Filename.concat dir "llpp.conf"
1132 let confpath = ref defconfpath;;
1134 let load2 f default =
1135 match filecontents !confpath with
1136 | contents -> f @@ do_load get contents
1137 | exception Unix.Unix_error (Unix.ENOENT, "open", _) ->
1138 f (Hashtbl.create 0, defconf)
1139 | exception exn ->
1140 dolog "error loading configuration from `%S': %s" !confpath @@ exntos exn;
1141 default
1144 let load1 f = load2 f false;;
1146 let load openlast =
1147 let f (h, dc) =
1148 if openlast
1149 then (
1150 let path, _ =
1151 Hashtbl.fold
1152 (fun path (conf, _, _, _, _) ((_, besttime) as best) ->
1153 if conf.lastvisit > besttime
1154 then (path, conf.lastvisit)
1155 else best)
1157 (state.path, -.infinity)
1159 state.path <- path;
1161 let pc, pb, px, pa, po =
1162 let def = dc, [], 0, emptyanchor, state.origin in
1163 if emptystr state.path
1164 then def
1165 else
1166 let absname = abspath state.path in
1167 match Hashtbl.find h absname with
1168 | (c,b,x,a,_) -> (c,b,x,a,state.origin)
1169 | exception Not_found ->
1170 let exception E of (conf * outline list * int * anchor * string) in
1171 let key = try Digest.file absname |> Digest.to_hex with _ -> E.s in
1172 match (
1173 if emptystr key
1174 then ()
1175 else
1176 Hashtbl.iter (fun p ((c, _, _, _, _) as v) ->
1177 if c.key = key
1178 then (
1179 dolog "will use %s's settings due to matching keys" p;
1180 raise (E v)
1184 with
1185 | _ -> def
1186 | exception E v -> v
1188 setconf defconf dc;
1189 setconf conf pc;
1190 state.bookmarks <- pb;
1191 state.x <- px;
1192 state.origin <- po;
1193 state.anchor <- pa;
1194 cbput state.hists.nav pa;
1195 true
1197 load1 f
1200 let gethist () =
1201 let f (h, _) =
1202 Hashtbl.fold (fun path (pc, pb, px, pa, po) accu ->
1203 (path, pc, pb, px, pa, po) :: accu)
1204 h [];
1206 load2 f []
1209 let add_attrs bb always dc c time =
1210 let o' fmt s =
1211 Buffer.add_string bb "\n ";
1212 Printf.bprintf bb fmt s
1214 let o c fmt s = if c then o' fmt s else ignore in
1215 let ob s a b = o (always || a != b) "%s='%b'" s a
1216 and op s a b = o (always || a <> b) "%s='%b'" s (a != None)
1217 and oi s a b = o (always || a != b) "%s='%d'" s a
1218 and oI s a b = o (always || a != b) "%s='%s'" s (string_with_suffix_of_int a)
1219 and oz s a b = o (always || a <> b) "%s='%g'" s (a*.100.)
1220 and oF s a b = o (always || a <> b) "%s='%f'" s a
1221 and oL s a b = o (always || a <> b) "%s='%Ld'" s a
1222 and oc s a b = o (always || a <> b) "%s='%s'" s (color_to_string a)
1223 and oA s a b = o (always || a <> b) "%s='%s'" s (rgba_to_string a)
1224 and oC s a b = o (always || a <> b) "%s='%s'" s (CSTE.to_string a)
1225 and oR s a b = o (always || a <> b) "%s='%s'" s (irect_to_string a)
1226 and oFm s a b = o (always || a <> b) "%s='%s'" s (FMTE.to_string a)
1227 and oSv s a b m =
1228 o (always || a land m <> b land m) "%s='%b'" s (a land m != 0)
1229 and oPm s a b = o (always || a <> b) "%s='%s'" s (MTE.to_string a)
1230 and os s a b =
1231 o (always || a <> b) "%s='%s'" s @@ Parser.enent a 0 (String.length a)
1232 and oco s a b =
1233 if always || a <> b
1234 then
1235 match a with
1236 | Cmulti ((n, a, b), _) when n > 1 -> o' "%s='%d,%d,%d'" s n a b
1237 | Csplit (n, _) when n > 1 -> o' "%s='%d'" s ~-n
1238 | Cmulti _ | Csplit _ | Csingle _ -> ()
1239 and obeco s a b =
1240 if always || a <> b
1241 then
1242 match a with
1243 | Some c when c > 1 -> o' "%s='%d'" s c
1244 | _ -> ()
1246 oi "width" c.cwinw dc.cwinw;
1247 oi "height" c.cwinh dc.cwinh;
1248 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
1249 oi "scroll-handle-height" c.scrollh dc.scrollh;
1250 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
1251 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
1252 ob "case-insensitive-search" c.icase dc.icase;
1253 ob "preload" c.preload dc.preload;
1254 oi "page-bias" c.pagebias dc.pagebias;
1255 oi "scroll-step" c.scrollstep dc.scrollstep;
1256 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
1257 ob "max-height-fit" c.maxhfit dc.maxhfit;
1258 ob "highlight-links" c.hlinks dc.hlinks;
1259 ob "under-cursor-info" c.underinfo dc.underinfo;
1260 oi "vertical-margin" c.interpagespace dc.interpagespace;
1261 oz "zoom" c.zoom dc.zoom;
1262 ob "presentation" c.presentation dc.presentation;
1263 oi "rotation-angle" c.angle dc.angle;
1264 oFm "fit-model" c.fitmodel dc.fitmodel;
1265 oI "pixmap-cache-size" c.memlimit dc.memlimit;
1266 oi "tex-count" c.texcount dc.texcount;
1267 oi "slice-height" c.sliceheight dc.sliceheight;
1268 oi "thumbnail-width" c.thumbw dc.thumbw;
1269 oc "background-color" c.bgcolor dc.bgcolor;
1270 oA "scrollbar-color" c.sbarcolor dc.sbarcolor;
1271 oA "scrollbar-handle-color" c.sbarhndlcolor dc.sbarhndlcolor;
1272 oi "tile-width" c.tilew dc.tilew;
1273 oi "tile-height" c.tileh dc.tileh;
1274 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
1275 ob "checkers" c.checkers dc.checkers;
1276 oi "aalevel" c.aalevel dc.aalevel;
1277 ob "trim-margins" c.trimmargins dc.trimmargins;
1278 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
1279 os "uri-launcher" c.urilauncher dc.urilauncher;
1280 os "path-launcher" c.pathlauncher dc.pathlauncher;
1281 oC "color-space" c.colorspace dc.colorspace;
1282 ob "invert-colors" c.invert dc.invert;
1283 oF "brightness" c.colorscale dc.colorscale;
1284 oco "columns" c.columns dc.columns;
1285 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
1286 os "selection-command" c.selcmd dc.selcmd;
1287 os "synctex-command" c.stcmd dc.stcmd;
1288 os "pax-command" c.paxcmd dc.paxcmd;
1289 os "askpass-command" c.passcmd dc.passcmd;
1290 os "savepath-command" c.savecmd dc.savecmd;
1291 ob "update-cursor" c.updatecurs dc.updatecurs;
1292 oi "hint-font-size" c.hfsize dc.hfsize;
1293 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
1294 oF "page-scroll-scale" c.pgscale dc.pgscale;
1295 ob "use-pbo" c.usepbo dc.usepbo;
1296 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
1297 ob "remote-in-a-new-instance" c.riani dc.riani;
1298 op "point-and-x" c.pax dc.pax;
1299 oPm "point-and-x-mark" c.paxmark dc.paxmark;
1300 ob "scroll-bar-on-the-left" c.leftscroll dc.leftscroll;
1301 if not always
1302 then os "title" c.title dc.title;
1303 oL "last-visit" (Int64.of_float time) 0L;
1304 ob "edit-annotations-inline" c.annotinline dc.annotinline;
1305 ob "coarse-presentation-positioning" c.coarseprespos dc.coarseprespos;
1306 ob "use-document-css" c.usedoccss dc.usedoccss;
1309 let keymapsbuf always dc c =
1310 let open Buffer in
1311 let bb = create 16 in
1312 let rec loop = function
1313 | [] -> ()
1314 | (modename, h) :: rest ->
1315 let dh = findkeyhash dc modename in
1316 if always || h <> dh
1317 then (
1318 if Hashtbl.length h > 0
1319 then (
1320 if length bb > 0 then add_char bb '\n';
1321 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
1322 Hashtbl.iter (fun i o ->
1323 if always || match Hashtbl.find dh i
1324 with | dO -> dO <> o | exception Not_found -> false
1325 then
1326 let addkm (k, m) =
1327 if Wsi.withctrl m then add_string bb "ctrl-";
1328 if Wsi.withalt m then add_string bb "alt-";
1329 if Wsi.withshift m then add_string bb "shift-";
1330 if Wsi.withmeta m then add_string bb "meta-";
1331 add_string bb (Wsi.keyname k);
1333 let addkms l =
1334 let rec loop = function
1335 | [] -> ()
1336 | km :: [] -> addkm km
1337 | km :: rest -> addkm km; add_char bb ' '; loop rest
1339 loop l
1341 add_string bb "<map in='";
1342 addkm i;
1343 match o with
1344 | KMinsrt km ->
1345 add_string bb "' out='"; addkm km; add_string bb "'/>\n"
1347 | KMinsrl kms ->
1348 add_string bb "' out='"; addkms kms; add_string bb "'/>\n"
1350 | KMmulti (ins, kms) ->
1351 add_char bb ' '; addkms ins; add_string bb "' out='";
1352 addkms kms; add_string bb "'/>\n"
1353 ) h;
1354 add_string bb "</keymap>";
1357 loop rest
1359 loop c.keyhashes;
1363 let keystostrlist c =
1364 let rec loop accu = function
1365 | [] -> accu
1366 | (modename, h) :: rest ->
1367 let accu =
1368 if Hashtbl.length h > 0
1369 then (
1370 let accu = Printf.sprintf "\xc2\xb7Keys for %s" modename :: accu in
1371 Hashtbl.fold (fun i o a ->
1372 let bb = Buffer.create 10 in
1373 let addkm (k, m) =
1374 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
1375 if Wsi.withalt m then Buffer.add_string bb "alt-";
1376 if Wsi.withshift m then Buffer.add_string bb "shift-";
1377 if Wsi.withmeta m then Buffer.add_string bb "meta-";
1378 Buffer.add_string bb (Wsi.keyname k);
1380 let addkms l =
1381 let rec loop = function
1382 | [] -> ()
1383 | km :: [] -> addkm km
1384 | km :: rest ->
1385 addkm km; Buffer.add_char bb ' ';
1386 loop rest
1388 loop l
1390 addkm i;
1391 Buffer.add_char bb '\t';
1392 begin match o with
1393 | KMinsrt km ->
1394 addkm km
1396 | KMinsrl kms ->
1397 addkms kms
1399 | KMmulti (ins, kms) ->
1400 Buffer.add_char bb ' ';
1401 addkms ins;
1402 Buffer.add_string bb "\t";
1403 addkms kms
1404 end;
1405 Buffer.contents bb :: a
1406 ) h accu
1408 else accu
1410 loop accu rest
1412 loop [] c.keyhashes
1415 let save1 bb leavebirdseye x h dc =
1416 let uifontsize = fstate.fontsize in
1417 let dc = if conf.bedefault then conf else dc in
1418 Buffer.add_string bb "<llppconfig>\n";
1420 if nonemptystr !fontpath
1421 then
1422 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
1423 uifontsize
1424 !fontpath
1425 else (
1426 if uifontsize <> 14
1427 then
1428 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
1431 Buffer.add_string bb "<defaults";
1432 add_attrs bb true dc dc nan;
1433 let kb = keymapsbuf true dc dc in
1434 if Buffer.length kb > 0
1435 then (
1436 Buffer.add_string bb ">\n";
1437 Buffer.add_buffer bb kb;
1438 Buffer.add_string bb "\n</defaults>\n";
1440 else Buffer.add_string bb "/>\n";
1442 let adddoc path pan anchor c bookmarks time origin =
1443 if bookmarks == [] && c = dc && anchor = emptyanchor
1444 then ()
1445 else (
1446 Printf.bprintf bb "<doc path='%s'"
1447 (Parser.enent path 0 (String.length path));
1449 if nonemptystr c.key
1450 then
1451 Printf.bprintf bb "\n key='%s'" c.key;
1453 if nonemptystr origin
1454 then Printf.bprintf bb "\n origin='%s'"
1455 (Parser.enent origin 0 (String.length origin));
1457 if anchor <> emptyanchor
1458 then (
1459 let n, rely, visy = anchor in
1460 Printf.bprintf bb "\n page='%d'" n;
1462 if rely > 1e-6
1463 then Printf.bprintf bb " rely='%f'" rely;
1465 if abs_float visy > 1e-6
1466 then Printf.bprintf bb " visy='%f'" visy;
1469 if pan != 0
1470 then Printf.bprintf bb " pan='%d'" pan;
1472 add_attrs bb false dc c time;
1473 if nonemptystr c.css
1474 then Printf.bprintf bb ">\n <css><![CDATA[%s]]></css>" c.css;
1475 let kb = keymapsbuf false dc c in
1477 begin match bookmarks with
1478 | [] ->
1479 if Buffer.length kb > 0
1480 then (
1481 Buffer.add_string bb ">\n";
1482 Buffer.add_buffer bb kb;
1483 Buffer.add_string bb "\n</doc>\n";
1485 else
1486 if nonemptystr c.css
1487 then Buffer.add_string bb "\n</doc>\n"
1488 else Buffer.add_string bb "/>\n"
1489 | _ ->
1490 Buffer.add_string bb ">\n<bookmarks>\n";
1491 List.iter (fun (title, _, kind) ->
1492 begin match kind with
1493 | Oanchor (page, rely, visy) ->
1494 Printf.bprintf bb
1495 "<item title='%s' page='%d'"
1496 (Parser.enent title 0 (String.length title))
1497 page
1499 if rely > 1e-6
1500 then
1501 Printf.bprintf bb " rely='%f'" rely
1503 if abs_float visy > 1e-6
1504 then
1505 Printf.bprintf bb " visy='%f'" visy
1507 | Ohistory _ | Onone | Ouri _ | Oremote _
1508 | Oremotedest _ | Olaunch _ ->
1509 failwith "unexpected link in bookmarks"
1510 end;
1511 Buffer.add_string bb "/>\n";
1512 ) bookmarks;
1513 Buffer.add_string bb "</bookmarks>";
1514 if Buffer.length kb > 0
1515 then (
1516 Buffer.add_string bb "\n";
1517 Buffer.add_buffer bb kb;
1519 Buffer.add_string bb "\n</doc>\n";
1520 end;
1524 let pan, conf =
1525 match state.mode with
1526 | Birdseye (c, pan, _, _, _) ->
1527 let beyecolumns =
1528 match conf.columns with
1529 | Cmulti ((c, _, _), _) -> Some c
1530 | Csingle _ -> None
1531 | Csplit _ -> None
1532 and columns =
1533 match c.columns with
1534 | Cmulti (c, _) -> Cmulti (c, E.a)
1535 | Csingle _ -> Csingle E.a
1536 | Csplit _ -> failwith "quit from bird's eye while split"
1538 pan, { c with beyecolumns = beyecolumns; columns = columns }
1539 | Textentry _
1540 | View
1541 | LinkNav _ -> x, conf
1543 let docpath = if nonemptystr state.path then abspath state.path else E.s in
1544 if nonemptystr docpath
1545 then (
1546 adddoc docpath pan (getanchor ())
1548 let autoscrollstep =
1549 match state.autoscroll with
1550 | Some step -> step
1551 | None -> conf.autoscrollstep
1553 begin match state.mode with
1554 | Birdseye beye -> leavebirdseye beye true
1555 | Textentry _
1556 | View
1557 | LinkNav _ -> ()
1558 end;
1559 let key = try Digest.file docpath |> Digest.to_hex
1560 with _ -> E.s in
1561 { conf with autoscrollstep; key }
1563 state.bookmarks
1564 (now ())
1565 state.origin
1567 Hashtbl.iter (fun path (c, bookmarks, x, anchor, origin) ->
1568 if docpath <> abspath path
1569 then adddoc path x anchor c bookmarks c.lastvisit origin
1570 ) h;
1571 Buffer.add_string bb "</llppconfig>\n";
1572 true;
1575 let save leavebirdseye =
1576 let relx = float state.x /. float state.winw in
1577 let w, h, x =
1578 let cx w = truncate (relx *. float w) in
1579 List.fold_left
1580 (fun (w, h, x) ws ->
1581 match ws with
1582 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
1583 | Wsi.MaxVert -> (w, conf.cwinh, x)
1584 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
1586 (state.winw, state.winh, state.x) state.winstate
1588 conf.cwinw <- w;
1589 conf.cwinh <- h;
1590 let bb = Buffer.create 32768 in
1591 let save2 (h, dc) =
1592 save1 bb leavebirdseye x h dc
1594 if load1 save2 && Buffer.length bb > 0
1595 then
1597 let tmp = !confpath ^ ".tmp" in
1598 let oc = open_out_bin tmp in
1599 Buffer.output_buffer oc bb;
1600 close_out oc;
1601 Unix.rename tmp !confpath;
1602 with exn ->
1603 dolog "error saving configuration: %s" @@ exntos exn
1606 let gc () =
1607 let href = ref @@ Hashtbl.create 0 in
1608 let cref = ref defconf in
1609 let push (h, dc) =
1610 let f path v =
1611 if Sys.file_exists path
1612 then Some v
1613 else (dolog "removing entry for '%s'" path; None) in
1614 Hashtbl.filter_map_inplace f h;
1615 href := h;
1616 cref := dc;
1617 true
1619 ignore (load1 push);
1620 let bb = Buffer.create 32768 in
1621 let save2 (_h, dc) = save1 bb (fun _ _ -> ()) 0 !href dc in
1622 if load1 save2 && Buffer.length bb > 0
1623 then (
1625 let tmp = !confpath ^ ".tmp" in
1626 let oc = open_out_bin tmp in
1627 Buffer.output_buffer oc bb;
1628 close_out oc;
1629 Unix.rename tmp !confpath;
1630 with exn ->
1631 dolog "error saving configuration: %s" @@ exntos exn
1635 let logcurrently = function
1636 | Idle -> dolog "Idle"
1637 | Loading (l, gen) ->
1638 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1639 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1640 dolog
1641 "Tiling %d[%d,%d] page=%s cs=%s angle=%d"
1642 l.pageno col row (~> pageopaque)
1643 (CSTE.to_string colorspace) angle;
1644 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1645 angle gen conf.angle state.gen
1646 tilew tileh
1647 conf.tilew conf.tileh;
1648 | Outlining _ ->
1649 dolog "outlining"