Diacritics and apostrophes
[llpp.git] / config.ml
blob0764f976f228e18fb68e128155b634f842f7f28c
1 open Utils;;
3 external fz_version : unit -> string = "ml_fz_version";;
5 type fontstate =
6 { mutable fontsize : int
7 ; mutable wwidth : float
8 ; mutable maxrows : int
12 let fstate =
13 { fontsize = 14
14 ; wwidth = nan
15 ; maxrows = -1
19 let scrollbvv = 1;;
20 let scrollbhv = 2;;
21 let fastghyllscroll = (5,1,2);;
22 let neatghyllscroll = (10,1,9);;
24 let irect_of_string s =
25 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
28 let irect_to_string (x0,y0,x1,y1) =
29 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
32 let ghyllscroll_of_string s =
33 match s with
34 | "fast" -> Some fastghyllscroll
35 | "neat" -> Some (10,1,9)
36 | "" | "none" -> None
37 | _ ->
38 let (n,a,b) as nab =
39 Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b) in
40 if n <= a || n <= b || a >= b
41 then error "N(%d),A(%d),B(%d) (N <= A, A < B, N <= B)" n a b;
42 Some nab
45 let ghyllscroll_to_string ((n, a, b) as nab) =
46 if nab = fastghyllscroll then "fast"
47 else if nab = neatghyllscroll then "neat"
48 else Printf.sprintf "%d,%d,%d" n a b;
51 let multicolumns_to_string (n, a, b) =
52 if a = 0 && b = 0
53 then Printf.sprintf "%d" n
54 else Printf.sprintf "%d,%d,%d" n a b;
57 let multicolumns_of_string s =
58 try
59 (int_of_string s, 0, 0)
60 with _ ->
61 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
62 if a > 1 || b > 1
63 then failwith "subtly broken";
64 (n, a, b)
68 type keymap =
69 | KMinsrt of key
70 | KMinsrl of key list
71 | KMmulti of key list * key list
72 and key = int * int
73 and keyhash = (key, keymap) Hashtbl.t
74 and keystate =
75 | KSnone
76 | KSinto of (key list * key list)
77 and interpagespace = int
78 and multicolumns = multicol * pagegeom
79 and singlecolumn = pagegeom
80 and splitcolumns = columncount * pagegeom
81 and pagegeom = (pdimno * x * y * (pageno * width * height * leftx)) array
82 and multicol = columncount * covercount * covercount
83 and pdimno = int
84 and columncount = int
85 and covercount = int
86 and fitmodel = | FitWidth | FitProportional | FitPage
87 and trimmargins = bool
88 and irect = (int * int * int * int)
89 and memsize = int
90 and texcount = int
91 and sliceheight = int
92 and angle = int
93 and params = (angle * fitmodel * trimparams
94 * texcount * sliceheight * memsize
95 * colorspace * fontpath * trimcachepath
96 * haspbo * usefontconfig)
97 and width = int
98 and height = int
99 and leftx = int
100 and opaque = Opaque.t
101 and rectcolor = (float * float * float * float)
102 and pixmapsize = int
103 and gen = int
104 and top = float
105 and dtop = float
106 and fontpath = string
107 and trimcachepath = string
108 and aalevel = int
109 and trimparams = (trimmargins * irect)
110 and colorspace = | Rgb | Bgr | Gray
111 and haspbo = bool
112 and usefontconfig = bool
113 and uri = string
114 and caption = string
115 and x = int
116 and y = int
117 and tilex = int
118 and tiley = int
119 and tileparams = (x * y * width * height * tilex * tiley)
120 and under =
121 | Unone
122 | Ulinkuri of string
123 | Utext of facename
124 | Uannotation of (opaque * slinkindex)
125 and slinkindex = int
126 and facename = string
127 and launchcommand = string
128 and filename = string
129 and pageno = int
130 and destname = string
131 and mark =
132 | Mark_page
133 | Mark_block
134 | Mark_line
135 | Mark_word
136 and link =
137 | Lnotfound
138 | Lfound of int
139 and linkdir =
140 | LDfirst
141 | LDlast
142 | LDfirstvisible of (int * int * int)
143 | LDleft of int
144 | LDright of int
145 | LDdown of int
146 | LDup of int
147 and pagewithlinks =
148 | Pwlnotfound
149 | Pwl of int
150 and scrollb = int
151 and anchor = pageno * top * dtop
152 and rect = float * float * float * float * float * float * float * float
153 and infochange = | Memused | Docinfo | Pdim
156 class type uioh = object
157 method display : unit
158 method key : int -> int -> uioh
159 method button : int -> bool -> int -> int -> int -> uioh
160 method multiclick : int -> int -> int -> int -> uioh
161 method motion : int -> int -> uioh
162 method pmotion : int -> int -> uioh
163 method infochanged : infochange -> unit
164 method scrollpw : (int * float * float)
165 method scrollph : (int * float * float)
166 method modehash : keyhash
167 method eformsgs : bool
168 method alwaysscrolly : bool
169 end;;
171 module type TextEnumType =
173 type t
174 val name : string
175 val names : string array
176 end;;
178 module TextEnumMake (Ten : TextEnumType) =
179 struct
180 let names = Ten.names;;
181 let to_int (t : Ten.t) = Obj.magic t;;
182 let to_string t = names.(to_int t);;
183 let of_int n : Ten.t = Obj.magic n;;
184 let of_string s =
185 let rec find i =
186 if i = Array.length names
187 then failwith ("invalid " ^ Ten.name ^ ": " ^ s)
188 else (
189 if Ten.names.(i) = s
190 then of_int i
191 else find (i+1)
193 in find 0;;
194 end;;
196 module CSTE = TextEnumMake (struct
197 type t = colorspace;;
198 let name = "colorspace";;
199 let names = [|"rgb"; "bgr"; "gray"|];;
200 end);;
202 module MTE = TextEnumMake (struct
203 type t = mark;;
204 let name = "mark";;
205 let names = [|"page"; "block"; "line"; "word"|];;
206 end);;
208 module FMTE = TextEnumMake (struct
209 type t = fitmodel;;
210 let name = "fitmodel";;
211 let names = [|"width"; "proportional"; "page"|];;
212 end);;
214 type conf =
215 { mutable scrollbw : int
216 ; mutable scrollh : int
217 ; mutable scrollb : scrollb
218 ; mutable icase : bool
219 ; mutable preload : bool
220 ; mutable pagebias : int
221 ; mutable verbose : bool
222 ; mutable debug : bool
223 ; mutable scrollstep : int
224 ; mutable hscrollstep : int
225 ; mutable maxhfit : bool
226 ; mutable crophack : bool
227 ; mutable autoscrollstep : int
228 ; mutable maxwait : float option
229 ; mutable hlinks : bool
230 ; mutable underinfo : bool
231 ; mutable interpagespace : interpagespace
232 ; mutable zoom : float
233 ; mutable presentation : bool
234 ; mutable angle : angle
235 ; mutable cwinw : int
236 ; mutable cwinh : int
237 ; mutable savebmarks : bool
238 ; mutable fitmodel : fitmodel
239 ; mutable trimmargins : trimmargins
240 ; mutable trimfuzz : irect
241 ; mutable memlimit : memsize
242 ; mutable texcount : texcount
243 ; mutable sliceheight : sliceheight
244 ; mutable thumbw : width
245 ; mutable jumpback : bool
246 ; mutable bgcolor : (float * float * float)
247 ; mutable bedefault : bool
248 ; mutable tilew : int
249 ; mutable tileh : int
250 ; mutable mustoresize : memsize
251 ; mutable checkers : bool
252 ; mutable aalevel : int
253 ; mutable urilauncher : string
254 ; mutable pathlauncher : string
255 ; mutable colorspace : colorspace
256 ; mutable invert : bool
257 ; mutable colorscale : float
258 ; mutable ghyllscroll : (int * int * int) option
259 ; mutable columns : columns
260 ; mutable beyecolumns : columncount option
261 ; mutable selcmd : string
262 ; mutable paxcmd : string
263 ; mutable passcmd : string
264 ; mutable savecmd : string
265 ; mutable updatecurs : bool
266 ; mutable keyhashes : (string * keyhash) list
267 ; mutable hfsize : int
268 ; mutable pgscale : float
269 ; mutable usepbo : bool
270 ; mutable wheelbypage : bool
271 ; mutable stcmd : string
272 ; mutable riani : bool
273 ; mutable pax : (float * int * int) ref option
274 ; mutable paxmark : mark
275 ; mutable leftscroll : bool
276 ; mutable title : string
277 ; mutable lastvisit : float
278 ; mutable annotinline : bool
279 ; mutable coarseprespos : bool
281 and columns =
282 | Csingle of singlecolumn
283 | Cmulti of multicolumns
284 | Csplit of splitcolumns
285 and outlinekind =
286 | Onone
287 | Oanchor of anchor
288 | Ouri of uri
289 | Olaunch of launchcommand
290 | Oremote of (filename * pageno)
291 | Oremotedest of (filename * destname)
292 | Ohistory of (filename * conf * outline list * x * anchor * filename)
293 and outline = (caption * outlinelevel * outlinekind)
294 and outlinelevel = int
297 type page =
298 { pageno : int
299 ; pagedimno : int
300 ; pagew : int
301 ; pageh : int
302 ; pagex : int
303 ; pagey : int
304 ; pagevw : int
305 ; pagevh : int
306 ; pagedispx : int
307 ; pagedispy : int
308 ; pagecol : int
312 type tile = opaque * pixmapsize * elapsed
313 and elapsed = float;;
314 type pagemapkey = pageno * gen;;
315 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
316 and row = int
317 and col = int
318 and currently =
319 | Idle
320 | Loading of (page * gen)
321 | Tiling of (
322 page * opaque * colorspace * angle * gen * col * row * width * height
324 | Outlining of outline list
327 type mpos = int * int
328 and mstate =
329 | Msel of (mpos * mpos)
330 | Mpan of mpos
331 | Mscrolly | Mscrollx
332 | Mzoom of (buttonno * step * mpos)
333 | Mzoomrect of (mpos * mpos)
334 | Mnone
335 and buttonno = int
336 and step = int
339 type mode =
340 | Birdseye of (conf * leftx * pageno * pageno * anchor)
341 | Textentry of (textentry * onleave)
342 | View
343 | LinkNav of linktarget
344 and onleave = leavetextentrystatus -> unit
345 and leavetextentrystatus = | Cancel | Confirm
346 and helpitem = string * int * action
347 and action =
348 | Noaction
349 | Action of (uioh -> uioh)
350 and linktarget =
351 | Ltexact of (pageno * direction)
352 | Ltgendir of direction
353 | Ltnotready of (pageno * direction)
354 and direction = int (* -1, 0, 1 *)
355 and textentry = string * string * onhist option * onkey * ondone * cancelonempty
356 and onkey = string -> int -> te
357 and ondone = string -> unit
358 and histcancel = unit -> unit
359 and onhist = ((histcmd -> string) * histcancel)
360 and histcmd = HCnext | HCprev | HCfirst | HClast
361 and cancelonempty = bool
362 and te =
363 | TEstop
364 | TEdone of string
365 | TEcont of string
366 | TEswitch of textentry
369 type 'a circbuf =
370 { store : 'a array
371 ; mutable rc : int
372 ; mutable wc : int
373 ; mutable len : int
377 type state =
378 { mutable ss : Unix.file_descr
379 ; mutable wsfd : Unix.file_descr
380 ; mutable stderr : Unix.file_descr
381 ; mutable errmsgs : Buffer.t
382 ; mutable newerrmsgs : bool
383 ; mutable w : int
384 ; mutable x : x
385 ; mutable y : y
386 ; mutable anchor : anchor
387 ; mutable ranchors : (string * string * anchor * string) list
388 ; mutable maxy : int
389 ; mutable layout : page list
390 ; pagemap : (pagemapkey, opaque) Hashtbl.t
391 ; tilemap : (tilemapkey, tile) Hashtbl.t
392 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
393 ; mutable pdims : (pageno * width * height * leftx) list
394 ; mutable pagecount : int
395 ; mutable currently : currently
396 ; mutable mstate : mstate
397 ; mutable searchpattern : string
398 ; mutable rects : (pageno * rectcolor * rect) list
399 ; mutable rects1 : (pageno * rectcolor * rect) list
400 ; prects : (pageno, float array) Hashtbl.t
401 ; mutable text : string
402 ; mutable winstate : Wsi.winstate list
403 ; mutable mode : mode
404 ; mutable uioh : uioh
405 ; mutable outlines : outline array
406 ; mutable bookmarks : outline list
407 ; mutable path : string
408 ; mutable password : string
409 ; mutable nameddest : string
410 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
411 ; mutable memused : memsize
412 ; mutable gen : gen
413 ; mutable throttle : (page list * int * float) option
414 ; mutable autoscroll : int option
415 ; mutable ghyll : (int option -> unit)
416 ; mutable help : helpitem array
417 ; mutable docinfo : (int * string) list
418 ; mutable checkerstexid : GlTex.texture_id option
419 ; hists : hists
420 ; mutable prevzoom : (float * int)
421 ; mutable progress : float
422 ; mutable redisplay : bool
423 ; mutable mpos : mpos
424 ; mutable keystate : keystate
425 ; mutable glinks : bool
426 ; mutable prevcolumns : (columns * float) option
427 ; mutable winw : int
428 ; mutable winh : int
429 ; mutable reprf : (unit -> unit)
430 ; mutable origin : string
431 ; mutable roam : (unit -> unit)
432 ; mutable bzoom : bool
433 ; mutable traw : [`float] Raw.t
434 ; mutable vraw : [`float] Raw.t
436 and hists =
437 { pat : string circbuf
438 ; pag : string circbuf
439 ; nav : anchor circbuf
440 ; sel : string circbuf
444 let emptyanchor = (0, 0.0, 0.0);;
445 let emptykeyhash = Hashtbl.create 0;;
446 let noghyll _ = ();;
447 let noreprf () = ();;
448 let noroam () = ();;
450 let nouioh : uioh = object (self)
451 method display = ()
452 method key _ _ = self
453 method multiclick _ _ _ _ = self
454 method button _ _ _ _ _ = self
455 method motion _ _ = self
456 method pmotion _ _ = self
457 method infochanged _ = ()
458 method scrollpw = (0, nan, nan)
459 method scrollph = (0, nan, nan)
460 method modehash = emptykeyhash
461 method eformsgs = false
462 method alwaysscrolly = false
463 end;;
465 let platform_to_string = function
466 | Punknown -> "unknown"
467 | Plinux -> "Linux"
468 | Posx -> "OSX"
469 | Psun -> "Sun"
470 | Pbsd -> "BSD"
471 | Pcygwin -> "Cygwin"
474 let version () =
475 Printf.sprintf "llpp version %s, fitz %s, ocaml %s/%d bit"
476 Help.version (fz_version ()) Sys.ocaml_version Sys.word_size
479 let defconf =
480 { scrollbw = 7
481 ; scrollh = 12
482 ; scrollb = scrollbhv lor scrollbvv
483 ; icase = true
484 ; preload = true
485 ; pagebias = 0
486 ; verbose = false
487 ; debug = false
488 ; scrollstep = 24
489 ; hscrollstep = 24
490 ; maxhfit = true
491 ; crophack = false
492 ; autoscrollstep = 2
493 ; maxwait = None
494 ; hlinks = false
495 ; underinfo = false
496 ; interpagespace = 2
497 ; zoom = 1.0
498 ; presentation = false
499 ; angle = 0
500 ; cwinw = 900
501 ; cwinh = 900
502 ; savebmarks = true
503 ; fitmodel = FitProportional
504 ; trimmargins = false
505 ; trimfuzz = (0,0,0,0)
506 ; memlimit = 32 lsl 20
507 ; texcount = 256
508 ; sliceheight = 24
509 ; thumbw = 76
510 ; jumpback = true
511 ; bgcolor = (0.5, 0.5, 0.5)
512 ; bedefault = false
513 ; tilew = 2048
514 ; tileh = 2048
515 ; mustoresize = 256 lsl 20
516 ; checkers = true
517 ; aalevel = 8
518 ; urilauncher =
519 (match platform with
520 | Plinux | Psun | Pbsd -> "xdg-open \"%s\""
521 | Posx -> "open \"%s\""
522 | Pcygwin -> "cygstart \"%s\""
523 | Punknown -> "echo %s")
524 ; pathlauncher = "lp \"%s\""
525 ; selcmd =
526 (match platform with
527 | Plinux | Pbsd | Psun -> "xsel -i"
528 | Posx -> "pbcopy"
529 | Pcygwin -> "wsel"
530 | Punknown -> "cat")
531 ; paxcmd = "cat"
532 ; passcmd = E.s
533 ; savecmd = E.s
534 ; colorspace = Rgb
535 ; invert = false
536 ; colorscale = 1.0
537 ; ghyllscroll = None
538 ; columns = Csingle [||]
539 ; beyecolumns = None
540 ; updatecurs = false
541 ; hfsize = 12
542 ; pgscale = 1.0
543 ; usepbo = false
544 ; wheelbypage = false
545 ; stcmd = "echo SyncTex"
546 ; riani = false
547 ; pax = None
548 ; paxmark = Mark_word
549 ; leftscroll = false
550 ; title = E.s
551 ; lastvisit = 0.0
552 ; annotinline = true
553 ; coarseprespos = false
554 ; keyhashes =
555 let mk n = (n, Hashtbl.create 1) in
556 [ mk "global"
557 ; mk "info"
558 ; mk "help"
559 ; mk "outline"
560 ; mk "listview"
561 ; mk "birdseye"
562 ; mk "textentry"
563 ; mk "links"
564 ; mk "view"
569 let conf = { defconf with angle = defconf.angle };;
571 let gotourl url =
572 let command = Str.global_replace percentsre url conf.urilauncher in
573 try ignore @@ spawn command []
574 with exn -> dolog "failed to execute `%s': %s" command @@ exntos exn
577 let gotouri uri =
578 if emptystr conf.urilauncher
579 then dolog "%s" uri
580 else (
581 let url = geturl uri in
582 if emptystr url
583 then dolog "obtained empty url from uri %S" uri
584 else gotourl url
588 let makehelp () =
589 let strings =
590 version ()
591 :: "(searching in this text works just by typing (i.e. no initial '/'))"
592 :: E.s :: Help.keys
594 Array.of_list (
595 List.map (fun s ->
596 let url = geturl s in
597 if nonemptystr url
598 then (s, 0, Action (fun uioh -> gotourl url; uioh))
599 else (s, 0, Noaction)
600 ) strings);
603 let cbnew n v =
604 { store = Array.make n v
605 ; rc = 0
606 ; wc = 0
607 ; len = 0
611 let cbcap b = Array.length b.store;;
613 let cbput b v =
614 let cap = cbcap b in
615 b.store.(b.wc) <- v;
616 b.wc <- (b.wc + 1) mod cap;
617 b.rc <- b.wc;
618 b.len <- min (b.len + 1) cap;
621 let cbempty b = b.len = 0;;
623 let cbgetg b circular dir =
624 if cbempty b
625 then b.store.(0)
626 else
627 let rc = b.rc + dir in
628 let rc =
629 if circular
630 then (
631 if rc = -1
632 then b.len-1
633 else (
634 if rc >= b.len
635 then 0
636 else rc
639 else bound rc 0 (b.len-1)
641 b.rc <- rc;
642 b.store.(rc);
645 let cbget b = cbgetg b false;;
646 let cbgetc b = cbgetg b true;;
648 let state =
649 { ss = Unix.stdin
650 ; wsfd = Unix.stdin
651 ; stderr = Unix.stderr
652 ; errmsgs = Buffer.create 0
653 ; newerrmsgs = false
654 ; x = 0
655 ; y = 0
656 ; w = 0
657 ; anchor = emptyanchor
658 ; ranchors = []
659 ; layout = []
660 ; maxy = max_int
661 ; tilelru = Queue.create ()
662 ; pagemap = Hashtbl.create 10
663 ; tilemap = Hashtbl.create 10
664 ; pdims = []
665 ; pagecount = 0
666 ; currently = Idle
667 ; mstate = Mnone
668 ; rects = []
669 ; rects1 = []
670 ; prects = Hashtbl.create 1
671 ; text = E.s
672 ; mode = View
673 ; winstate = []
674 ; searchpattern = E.s
675 ; outlines = E.a
676 ; bookmarks = []
677 ; path = E.s
678 ; password = E.s
679 ; nameddest = E.s
680 ; geomcmds = E.s, []
681 ; hists =
682 { nav = cbnew 10 emptyanchor
683 ; pat = cbnew 10 E.s
684 ; pag = cbnew 10 E.s
685 ; sel = cbnew 10 E.s
687 ; memused = 0
688 ; gen = 0
689 ; throttle = None
690 ; autoscroll = None
691 ; ghyll = noghyll
692 ; help = makehelp ()
693 ; docinfo = []
694 ; checkerstexid = None
695 ; prevzoom = (1.0, 0)
696 ; progress = -1.0
697 ; uioh = nouioh
698 ; redisplay = true
699 ; mpos = (-1, -1)
700 ; keystate = KSnone
701 ; glinks = false
702 ; prevcolumns = None
703 ; winw = -1
704 ; winh = -1
705 ; reprf = noreprf
706 ; origin = E.s
707 ; roam = noroam
708 ; bzoom = false
709 ; traw = Raw.create_static `float ~len:8
710 ; vraw = Raw.create_static `float ~len:8
714 let copykeyhashes c =
715 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
718 let calcips h =
719 let d = state.winh - h in
720 max conf.interpagespace ((d + 1) / 2)
723 let rowyh (c, coverA, coverB) b n =
724 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
725 then
726 let _, _, vy, (_, _, h, _) = b.(n) in
727 (vy, h)
728 else
729 let n' = n - coverA in
730 let d = n' mod c in
731 let s = n - d in
732 let e = min state.pagecount (s + c) in
733 let rec find m miny maxh = if m = e then miny, maxh else
734 let _, _, y, (_, _, h, _) = b.(m) in
735 let miny = min miny y in
736 let maxh = max maxh h in
737 find (m+1) miny maxh
738 in find s max_int 0
741 let page_of_y y =
742 let ((c, coverA, coverB) as cl), b =
743 match conf.columns with
744 | Csingle b -> (1, 0, 0), b
745 | Cmulti (c, b) -> c, b
746 | Csplit (_, b) -> (1, 0, 0), b
748 if Array.length b = 0
749 then -1
750 else
751 let rec bsearch nmin nmax =
752 if nmin > nmax
753 then bound nmin 0 (state.pagecount-1)
754 else
755 let n = (nmax + nmin) / 2 in
756 let vy, h = rowyh cl b n in
757 let y0, y1 =
758 if conf.presentation
759 then
760 let ips = calcips h in
761 let y0 = vy - ips in
762 let y1 = vy + h + ips in
763 y0, y1
764 else (
765 if n = 0
766 then 0, vy + h + conf.interpagespace
767 else
768 let y0 = vy - conf.interpagespace in
769 y0, y0 + h + conf.interpagespace
772 if y >= y0 && y < y1
773 then (
774 if c = 1
775 then n
776 else (
777 if n > coverA
778 then
779 if n < state.pagecount - coverB
780 then ((n-coverA)/c)*c + coverA
781 else n
782 else n
785 else (
786 if y > y0
787 then bsearch (n+1) nmax
788 else bsearch nmin (n-1)
791 bsearch 0 (state.pagecount-1);
794 let calcheight () =
795 match conf.columns with
796 | Cmulti ((_, _, _) as cl, b) ->
797 if Array.length b > 0
798 then
799 let y, h = rowyh cl b (Array.length b - 1) in
800 y + h + (if conf.presentation then calcips h else 0)
801 else 0
802 | Csingle b ->
803 if Array.length b > 0
804 then
805 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
806 y + h + (if conf.presentation then calcips h else 0)
807 else 0
808 | Csplit (_, b) ->
809 if Array.length b > 0
810 then
811 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
812 y + h
813 else 0
816 let getpageywh pageno =
817 let pageno = bound pageno 0 (state.pagecount-1) in
818 match conf.columns with
819 | Csingle b ->
820 if Array.length b = 0
821 then 0, 0, 0
822 else
823 let (_, _, y, (_, w, h, _)) = b.(pageno) in
824 let y =
825 if conf.presentation
826 then y - calcips h
827 else y
829 y, w, h
830 | Cmulti (cl, b) ->
831 if Array.length b = 0
832 then 0, 0, 0
833 else
834 let y, h = rowyh cl b pageno in
835 let (_, _, _, (_, w, _, _)) = b.(pageno) in
836 let y =
837 if conf.presentation
838 then y - calcips h
839 else y
841 y, w, h
842 | Csplit (c, b) ->
843 if Array.length b = 0
844 then 0, 0, 0
845 else
846 let n = pageno*c in
847 let (_, _, y, (_, w, h, _)) = b.(n) in
848 y, w / c, h
851 let getpageyh pageno =
852 let y,_,h = getpageywh pageno in
853 y, h;
856 let getpagedim pageno =
857 let rec f ppdim l =
858 match l with
859 | (n, _, _, _) as pdim :: rest ->
860 if n >= pageno
861 then (if n = pageno then pdim else ppdim)
862 else f pdim rest
864 | [] -> ppdim
866 f (-1, -1, -1, -1) state.pdims
869 let getpdimno pageno =
870 let rec f p l =
871 let np = succ p in
872 match l with
873 | (n, _, _, _) :: rest ->
874 if n >= pageno
875 then (if n = pageno then np else p)
876 else f np rest
878 | [] -> p
880 f ~-1 state.pdims
883 let getpagey pageno = fst (getpageyh pageno);;
885 let getanchor1 l =
886 let top =
887 let coloff = l.pagecol * l.pageh in
888 float (l.pagey + coloff) /. float l.pageh
890 let dtop =
891 if l.pagedispy = 0
892 then
894 else (
895 if conf.presentation
896 then float l.pagedispy /. float (calcips l.pageh)
897 else float l.pagedispy /. float conf.interpagespace
900 (l.pageno, top, dtop)
903 let getanchor () =
904 match state.layout with
905 | l :: _ -> getanchor1 l
906 | [] ->
907 let n = page_of_y state.y in
908 if n = -1
909 then state.anchor
910 else
911 let y, h = getpageyh n in
912 let dy = y - state.y in
913 let dtop =
914 if conf.presentation
915 then
916 let ips = calcips h in
917 float (dy + ips) /. float ips
918 else
919 float dy /. float conf.interpagespace
921 (n, 0.0, dtop)
924 let fontpath = ref E.s;;
926 type historder = [ `lastvisit | `title | `path | `file ];;
928 module KeyMap =
929 Map.Make (struct type t = (int * int) let compare = compare end);;
931 let unentS s =
932 let l = String.length s in
933 let b = Buffer.create l in
934 Parser.unent b s 0 l;
935 Buffer.contents b;
938 let home =
939 try Sys.getenv "HOME"
940 with exn ->
941 dolog "cannot determine home directory location: %s" @@ exntos exn;
945 let modifier_of_string = function
946 | "alt" -> Wsi.altmask
947 | "shift" -> Wsi.shiftmask
948 | "ctrl" | "control" -> Wsi.ctrlmask
949 | "meta" -> Wsi.metamask
950 | _ -> 0
953 let keys_of_string s =
954 let key_of_string r s =
955 let elems = Str.full_split r s in
956 let f n k m =
957 let g s =
958 let m1 = modifier_of_string s in
959 if m1 = 0
960 then (Wsi.namekey s, m)
961 else (k, m lor m1)
962 in function
963 | Str.Delim s when n land 1 = 0 -> g s
964 | Str.Text s -> g s
965 | Str.Delim _ -> (k, m)
967 let rec loop n k m = function
968 | [] -> (k, m)
969 | x :: xs ->
970 let k, m = f n k m x in
971 loop (n+1) k m xs
973 loop 0 0 0 elems
975 let elems = Str.split whitere s in
976 List.map (key_of_string (Str.regexp "-")) elems
979 let config_of c attrs =
980 let apply c k v =
982 match k with
983 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
984 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
985 | "case-insensitive-search" -> { c with icase = bool_of_string v }
986 | "preload" -> { c with preload = bool_of_string v }
987 | "page-bias" -> { c with pagebias = int_of_string v }
988 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
989 | "horizontal-scroll-step" ->
990 { c with hscrollstep = max (int_of_string v) 1 }
991 | "auto-scroll-step" ->
992 { c with autoscrollstep = max 0 (int_of_string v) }
993 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
994 | "crop-hack" -> { c with crophack = bool_of_string v }
995 | "throttle" ->
996 let mw =
997 match String.map asciilower v with
998 | "true" -> Some infinity
999 | "false" -> None
1000 | f -> Some (float_of_string f)
1002 { c with maxwait = mw }
1003 | "highlight-links" -> { c with hlinks = bool_of_string v }
1004 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
1005 | "vertical-margin" ->
1006 { c with interpagespace = max 0 (int_of_string v) }
1007 | "zoom" ->
1008 let zoom = float_of_string v /. 100. in
1009 let zoom = max zoom 0.0 in
1010 { c with zoom = zoom }
1011 | "presentation" -> { c with presentation = bool_of_string v }
1012 | "rotation-angle" -> { c with angle = int_of_string v }
1013 | "width" -> { c with cwinw = max 20 (int_of_string v) }
1014 | "height" -> { c with cwinh = max 20 (int_of_string v) }
1015 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
1016 | "proportional-display" ->
1017 let fm =
1018 if bool_of_string v
1019 then FitProportional
1020 else FitWidth
1022 { c with fitmodel = fm }
1023 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
1024 | "pixmap-cache-size" ->
1025 { c with memlimit = max 2 (int_of_string_with_suffix v) }
1026 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
1027 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
1028 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
1029 | "persistent-location" -> { c with jumpback = bool_of_string v }
1030 | "background-color" -> { c with bgcolor = color_of_string v }
1031 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
1032 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
1033 | "mupdf-store-size" ->
1034 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
1035 | "checkers" -> { c with checkers = bool_of_string v }
1036 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
1037 | "trim-margins" -> { c with trimmargins = bool_of_string v }
1038 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
1039 | "uri-launcher" -> { c with urilauncher = unentS v }
1040 | "path-launcher" -> { c with pathlauncher = unentS v }
1041 | "color-space" -> { c with colorspace = CSTE.of_string v }
1042 | "invert-colors" -> { c with invert = bool_of_string v }
1043 | "brightness" -> { c with colorscale = float_of_string v }
1044 | "ghyllscroll" -> { c with ghyllscroll = ghyllscroll_of_string v }
1045 | "columns" ->
1046 let (n, _, _) as nab = multicolumns_of_string v in
1047 if n < 0
1048 then { c with columns = Csplit (-n, E.a) }
1049 else { c with columns = Cmulti (nab, E.a) }
1050 | "birds-eye-columns" ->
1051 { c with beyecolumns = Some (max (int_of_string v) 2) }
1052 | "selection-command" -> { c with selcmd = unentS v }
1053 | "synctex-command" -> { c with stcmd = unentS v }
1054 | "pax-command" -> { c with paxcmd = unentS v }
1055 | "askpass-command" -> { c with passcmd = unentS v }
1056 | "savepath-command" -> { c with savecmd = unentS v }
1057 | "update-cursor" -> { c with updatecurs = bool_of_string v }
1058 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
1059 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
1060 | "use-pbo" -> { c with usepbo = bool_of_string v }
1061 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
1062 | "horizontal-scrollbar-visible" ->
1063 let b =
1064 if bool_of_string v
1065 then c.scrollb lor scrollbhv
1066 else c.scrollb land (lnot scrollbhv)
1068 { c with scrollb = b }
1069 | "vertical-scrollbar-visible" ->
1070 let b =
1071 if bool_of_string v
1072 then c.scrollb lor scrollbvv
1073 else c.scrollb land (lnot scrollbvv)
1075 { c with scrollb = b }
1076 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
1077 | "point-and-x" ->
1078 { c with pax =
1079 if bool_of_string v
1080 then Some (ref (0.0, 0, 0))
1081 else None }
1082 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
1083 | "scroll-bar-on-the-left" -> { c with leftscroll = bool_of_string v }
1084 | "title" -> { c with title = unentS v }
1085 | "last-visit" -> { c with lastvisit = float_of_string v }
1086 | "edit-annotations-inline" -> { c with annotinline = bool_of_string v }
1087 | "coarse-presentation-positioning" ->
1088 { c with coarseprespos = bool_of_string v }
1089 | _ -> c
1090 with exn ->
1091 dolog "error processing attribute (`%S' = `%S'): %s" k v @@ exntos exn;
1094 let rec fold c = function
1095 | [] -> c
1096 | (k, v) :: rest ->
1097 let c = apply c k v in
1098 fold c rest
1100 fold { c with keyhashes = copykeyhashes c } attrs;
1103 let fromstring f pos n v d =
1104 try f v
1105 with exn ->
1106 dolog "error processing attribute (%S=%S) at %d\n%s" n v pos @@ exntos exn;
1110 let bookmark_of attrs =
1111 let rec fold title page rely visy = function
1112 | ("title", v) :: rest -> fold v page rely visy rest
1113 | ("page", v) :: rest -> fold title v rely visy rest
1114 | ("rely", v) :: rest -> fold title page v visy rest
1115 | ("visy", v) :: rest -> fold title page rely v rest
1116 | _ :: rest -> fold title page rely visy rest
1117 | [] -> title, page, rely, visy
1119 fold "invalid" "0" "0" "0" attrs
1122 let doc_of attrs =
1123 let rec fold path page rely pan visy origin = function
1124 | ("path", v) :: rest -> fold v page rely pan visy origin rest
1125 | ("page", v) :: rest -> fold path v rely pan visy origin rest
1126 | ("rely", v) :: rest -> fold path page v pan visy origin rest
1127 | ("pan", v) :: rest -> fold path page rely v visy origin rest
1128 | ("visy", v) :: rest -> fold path page rely pan v origin rest
1129 | ("origin", v) :: rest -> fold path page rely pan visy v rest
1130 | _ :: rest -> fold path page rely pan visy origin rest
1131 | [] -> path, page, rely, pan, visy, origin
1133 fold E.s "0" "0" "0" "0" E.s attrs
1136 let map_of attrs =
1137 let rec fold rs ls = function
1138 | ("out", v) :: rest -> fold v ls rest
1139 | ("in", v) :: rest -> fold rs v rest
1140 | _ :: rest -> fold ls rs rest
1141 | [] -> ls, rs
1143 fold E.s E.s attrs
1146 let setconf dst src =
1147 dst.scrollbw <- src.scrollbw;
1148 dst.scrollh <- src.scrollh;
1149 dst.icase <- src.icase;
1150 dst.preload <- src.preload;
1151 dst.pagebias <- src.pagebias;
1152 dst.verbose <- src.verbose;
1153 dst.scrollstep <- src.scrollstep;
1154 dst.maxhfit <- src.maxhfit;
1155 dst.crophack <- src.crophack;
1156 dst.autoscrollstep <- src.autoscrollstep;
1157 dst.maxwait <- src.maxwait;
1158 dst.hlinks <- src.hlinks;
1159 dst.underinfo <- src.underinfo;
1160 dst.interpagespace <- src.interpagespace;
1161 dst.zoom <- src.zoom;
1162 dst.presentation <- src.presentation;
1163 dst.angle <- src.angle;
1164 dst.cwinw <- src.cwinw;
1165 dst.cwinh <- src.cwinh;
1166 dst.savebmarks <- src.savebmarks;
1167 dst.memlimit <- src.memlimit;
1168 dst.fitmodel <- src.fitmodel;
1169 dst.texcount <- src.texcount;
1170 dst.sliceheight <- src.sliceheight;
1171 dst.thumbw <- src.thumbw;
1172 dst.jumpback <- src.jumpback;
1173 dst.bgcolor <- src.bgcolor;
1174 dst.tilew <- src.tilew;
1175 dst.tileh <- src.tileh;
1176 dst.mustoresize <- src.mustoresize;
1177 dst.checkers <- src.checkers;
1178 dst.aalevel <- src.aalevel;
1179 dst.trimmargins <- src.trimmargins;
1180 dst.trimfuzz <- src.trimfuzz;
1181 dst.urilauncher <- src.urilauncher;
1182 dst.colorspace <- src.colorspace;
1183 dst.invert <- src.invert;
1184 dst.colorscale <- src.colorscale;
1185 dst.ghyllscroll <- src.ghyllscroll;
1186 dst.columns <- src.columns;
1187 dst.beyecolumns <- src.beyecolumns;
1188 dst.selcmd <- src.selcmd;
1189 dst.updatecurs <- src.updatecurs;
1190 dst.pathlauncher <- src.pathlauncher;
1191 dst.keyhashes <- copykeyhashes src;
1192 dst.hfsize <- src.hfsize;
1193 dst.hscrollstep <- src.hscrollstep;
1194 dst.pgscale <- src.pgscale;
1195 dst.usepbo <- src.usepbo;
1196 dst.wheelbypage <- src.wheelbypage;
1197 dst.stcmd <- src.stcmd;
1198 dst.paxcmd <- src.paxcmd;
1199 dst.passcmd <- src.passcmd;
1200 dst.savecmd <- src.savecmd;
1201 dst.scrollb <- src.scrollb;
1202 dst.riani <- src.riani;
1203 dst.paxmark <- src.paxmark;
1204 dst.leftscroll <- src.leftscroll;
1205 dst.title <- src.title;
1206 dst.annotinline <- src.annotinline;
1207 dst.coarseprespos <- src.coarseprespos;
1208 dst.pax <-
1209 if src.pax = None
1210 then None
1211 else Some ((ref (0.0, 0, 0)));
1214 let findkeyhash c name =
1215 try List.assoc name c.keyhashes
1216 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
1219 let get s =
1220 let open Parser in
1221 let h = Hashtbl.create 10 in
1222 let dc = { defconf with angle = defconf.angle } in
1223 let rec toplevel v t spos _ =
1224 match t with
1225 | Vdata | Vcdata | Vend -> v
1226 | Vopen ("llppconfig", _, closed) ->
1227 if closed
1228 then v
1229 else { v with f = llppconfig }
1230 | Vopen _ ->
1231 error "unexpected subelement at top level" s spos
1232 | Vclose _ -> error "unexpected close at top level" s spos
1234 and llppconfig v t spos _ =
1235 match t with
1236 | Vdata | Vcdata -> v
1237 | Vend -> error "unexpected end of input in llppconfig" s spos
1238 | Vopen ("defaults", attrs, closed) ->
1239 let c = config_of dc attrs in
1240 setconf dc c;
1241 if closed
1242 then v
1243 else { v with f = defaults }
1245 | Vopen ("ui-font", attrs, closed) ->
1246 let rec getsize size = function
1247 | [] -> size
1248 | ("size", v) :: rest ->
1249 let size =
1250 fromstring int_of_string spos "size" v fstate.fontsize in
1251 getsize size rest
1252 | l -> getsize size l
1254 fstate.fontsize <- getsize fstate.fontsize attrs;
1255 if closed
1256 then v
1257 else { v with f = uifont (Buffer.create 10) }
1259 | Vopen ("doc", attrs, closed) ->
1260 let pathent, spage, srely, span, svisy, origin = doc_of attrs in
1261 let path = unentS pathent
1262 and origin = unentS origin
1263 and pageno = fromstring int_of_string spos "page" spage 0
1264 and rely = fromstring float_of_string spos "rely" srely 0.0
1265 and pan = fromstring int_of_string spos "pan" span 0
1266 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1267 let c = config_of dc attrs in
1268 let anchor = (pageno, rely, visy) in
1269 if closed
1270 then (Hashtbl.add h path (c, [], pan, anchor, origin); v)
1271 else { v with f = doc path origin pan anchor c [] }
1273 | Vopen _ ->
1274 error "unexpected subelement in llppconfig" s spos
1276 | Vclose "llppconfig" -> { v with f = toplevel }
1277 | Vclose _ -> error "unexpected close in llppconfig" s spos
1279 and defaults v t spos _ =
1280 match t with
1281 | Vdata | Vcdata -> v
1282 | Vend -> error "unexpected end of input in defaults" s spos
1283 | Vopen ("keymap", attrs, closed) ->
1284 let modename =
1285 try List.assoc "mode" attrs
1286 with Not_found -> "global" in
1287 if closed
1288 then v
1289 else
1290 let ret keymap =
1291 let h = findkeyhash dc modename in
1292 KeyMap.iter (Hashtbl.replace h) keymap;
1293 defaults
1295 { v with f = pkeymap ret KeyMap.empty }
1297 | Vopen (_, _, _) ->
1298 error "unexpected subelement in defaults" s spos
1300 | Vclose "defaults" ->
1301 { v with f = llppconfig }
1303 | Vclose _ -> error "unexpected close in defaults" s spos
1305 and uifont b v t spos epos =
1306 match t with
1307 | Vdata | Vcdata ->
1308 Buffer.add_substring b s spos (epos - spos);
1310 | Vopen (_, _, _) ->
1311 error "unexpected subelement in ui-font" s spos
1312 | Vclose "ui-font" ->
1313 if emptystr !fontpath
1314 then fontpath := Buffer.contents b;
1315 { v with f = llppconfig }
1316 | Vclose _ -> error "unexpected close in ui-font" s spos
1317 | Vend -> error "unexpected end of input in ui-font" s spos
1319 and doc path origin pan anchor c bookmarks v t spos _ =
1320 match t with
1321 | Vdata | Vcdata -> v
1322 | Vend -> error "unexpected end of input in doc" s spos
1323 | Vopen ("bookmarks", _, closed) ->
1324 if closed
1325 then v
1326 else { v with f = pbookmarks path origin pan anchor c bookmarks }
1328 | Vopen ("keymap", attrs, closed) ->
1329 let modename =
1330 try List.assoc "mode" attrs
1331 with Not_found -> "global"
1333 if closed
1334 then v
1335 else
1336 let ret keymap =
1337 let h = findkeyhash c modename in
1338 KeyMap.iter (Hashtbl.replace h) keymap;
1339 doc path origin pan anchor c bookmarks
1341 { v with f = pkeymap ret KeyMap.empty }
1343 | Vopen (_, _, _) ->
1344 error "unexpected subelement in doc" s spos
1346 | Vclose "doc" ->
1347 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor, origin);
1348 { v with f = llppconfig }
1350 | Vclose _ -> error "unexpected close in doc" s spos
1352 and pkeymap ret keymap v t spos _ =
1353 match t with
1354 | Vdata | Vcdata -> v
1355 | Vend -> error "unexpected end of input in keymap" s spos
1356 | Vopen ("map", attrs, closed) ->
1357 let r, l = map_of attrs in
1358 let kss = fromstring keys_of_string spos "in" r [] in
1359 let lss = fromstring keys_of_string spos "out" l [] in
1360 let keymap =
1361 match kss with
1362 | [] -> keymap
1363 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
1364 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
1366 if closed
1367 then { v with f = pkeymap ret keymap }
1368 else
1369 let f () = v in
1370 { v with f = skip "map" f }
1372 | Vopen _ ->
1373 error "unexpected subelement in keymap" s spos
1375 | Vclose "keymap" ->
1376 { v with f = ret keymap }
1378 | Vclose _ -> error "unexpected close in keymap" s spos
1380 and pbookmarks path origin pan anchor c bookmarks v t spos _ =
1381 match t with
1382 | Vdata | Vcdata -> v
1383 | Vend -> error "unexpected end of input in bookmarks" s spos
1384 | Vopen ("item", attrs, closed) ->
1385 let titleent, spage, srely, svisy = bookmark_of attrs in
1386 let page = fromstring int_of_string spos "page" spage 0
1387 and rely = fromstring float_of_string spos "rely" srely 0.0
1388 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1389 let bookmarks =
1390 (unentS titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
1392 if closed
1393 then { v with f = pbookmarks path origin pan anchor c bookmarks }
1394 else
1395 let f () = v in
1396 { v with f = skip "item" f }
1398 | Vopen _ ->
1399 error "unexpected subelement in bookmarks" s spos
1401 | Vclose "bookmarks" ->
1402 { v with f = doc path origin pan anchor c bookmarks }
1404 | Vclose _ -> Parser.parse_error "unexpected close in bookmarks" s spos
1406 and skip tag f v t spos _ =
1407 match t with
1408 | Vdata | Vcdata -> v
1409 | Vend ->
1410 Parser.parse_error ("unexpected end of input in skipped " ^ tag) s spos
1411 | Vopen (tag', _, closed) ->
1412 if closed
1413 then v
1414 else
1415 let f' () = { v with f = skip tag f } in
1416 { v with f = skip tag' f' }
1417 | Vclose ctag ->
1418 if tag = ctag
1419 then f ()
1420 else Parser.parse_error ("unexpected close in skipped " ^ tag) s spos
1423 parse { f = toplevel; accu = () } s;
1424 h, dc;
1427 let do_load f contents =
1428 try f contents
1429 with
1430 | Parser.Parse_error (msg, s, pos) ->
1431 let subs = Parser.subs s pos in
1432 Utils.error "parse error: %s: at %d [..%S..]" msg pos subs
1434 | exn -> Utils.error "parse error: %s" @@ exntos exn
1437 let defconfpath =
1438 let dir =
1439 let xdgconfdir = Utils.getenvwithdef "XDG_CONFIG_HOME" E.s in
1440 if emptystr xdgconfdir
1441 then
1443 let dir = Filename.concat home ".config" in
1444 if Sys.is_directory dir then dir else home
1445 with _ -> home
1446 else xdgconfdir
1448 Filename.concat dir "llpp.conf"
1451 let confpath = ref defconfpath;;
1453 let load2 f default =
1454 match filecontents !confpath with
1455 | contents -> f @@ do_load get contents
1456 | exception Unix.Unix_error (Unix.ENOENT, "open", _) ->
1457 f (Hashtbl.create 0, defconf)
1458 | exception exn ->
1459 dolog "error loading configuration from `%S': %s" !confpath @@ exntos exn;
1460 default
1463 let load1 f = load2 f false;;
1465 let load openlast =
1466 let f (h, dc) =
1467 if openlast
1468 then (
1469 let path, _ =
1470 Hashtbl.fold
1471 (fun path (conf, _, _, _, _) ((_, besttime) as best) ->
1472 if conf.lastvisit > besttime
1473 then (path, conf.lastvisit)
1474 else best)
1476 (state.path, -.infinity)
1478 state.path <- path;
1480 let pc, pb, px, pa, po =
1482 let absname = abspath state.path in
1483 Hashtbl.find h absname
1484 with Not_found -> dc, [], 0, emptyanchor, state.origin
1486 setconf defconf dc;
1487 setconf conf pc;
1488 state.bookmarks <- pb;
1489 state.x <- px;
1490 state.origin <- po;
1491 if conf.jumpback
1492 then state.anchor <- pa;
1493 cbput state.hists.nav pa;
1494 true
1496 load1 f
1499 let gethist () =
1500 let f (h, _) =
1501 Hashtbl.fold (fun path (pc, pb, px, pa, po) accu ->
1502 (path, pc, pb, px, pa, po) :: accu)
1503 h [];
1505 load2 f []
1508 let add_attrs bb always dc c time =
1509 let o' fmt s =
1510 Buffer.add_string bb "\n ";
1511 Printf.bprintf bb fmt s
1513 let o c fmt s = if c then o' fmt s else ignore in
1514 let ob s a b = o (always || a != b) "%s='%b'" s a
1515 and op s a b = o (always || a <> b) "%s='%b'" s (a != None)
1516 and oi s a b = o (always || a != b) "%s='%d'" s a
1517 and oI s a b = o (always || a != b) "%s='%s'" s (string_with_suffix_of_int a)
1518 and oz s a b = o (always || a <> b) "%s='%g'" s (a*.100.)
1519 and oF s a b = o (always || a <> b) "%s='%f'" s a
1520 and oL s a b = o (always || a <> b) "%s='%Ld'" s a
1521 and oc s a b = o (always || a <> b) "%s='%s'" s (color_to_string a)
1522 and oC s a b = o (always || a <> b) "%s='%s'" s (CSTE.to_string a)
1523 and oR s a b = o (always || a <> b) "%s='%s'" s (irect_to_string a)
1524 and oFm s a b = o (always || a <> b) "%s='%s'" s (FMTE.to_string a)
1525 and oSv s a b m = o (always || a <> b) "%s='%b'" s (a land m != 0)
1526 and oPm s a b = o (always || a <> b) "%s='%s'" s (MTE.to_string a)
1527 and os s a b =
1528 o (always || a <> b) "%s='%s'" s @@ Parser.enent a 0 (String.length a)
1529 and og s a b =
1530 if always || a <> b
1531 then
1532 match a with
1533 | Some (_N, _A, _B) -> o' "%s='%u,%u,%u'" s _N _A _B
1534 | None ->
1535 match b with
1536 | None -> ()
1537 | _ -> o' "%s='none'" s
1538 and oW s a b =
1539 if always || a <> b
1540 then
1541 let v =
1542 match a with
1543 | None -> "false"
1544 | Some f ->
1545 if f = infinity
1546 then "true"
1547 else string_of_float f
1549 o' "%s='%s'" s v
1550 and oco s a b =
1551 if always || a <> b
1552 then
1553 match a with
1554 | Cmulti ((n, a, b), _) when n > 1 -> o' "%s='%d,%d,%d'" s n a b
1555 | Csplit (n, _) when n > 1 -> o' "%s='%d'" s ~-n
1556 | Cmulti _ | Csplit _ | Csingle _ -> ()
1557 and obeco s a b =
1558 if always || a <> b
1559 then
1560 match a with
1561 | Some c when c > 1 -> o' "%s='%d'" s c
1562 | _ -> ()
1564 oi "width" c.cwinw dc.cwinw;
1565 oi "height" c.cwinh dc.cwinh;
1566 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
1567 oi "scroll-handle-height" c.scrollh dc.scrollh;
1568 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
1569 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
1570 ob "case-insensitive-search" c.icase dc.icase;
1571 ob "preload" c.preload dc.preload;
1572 oi "page-bias" c.pagebias dc.pagebias;
1573 oi "scroll-step" c.scrollstep dc.scrollstep;
1574 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
1575 ob "max-height-fit" c.maxhfit dc.maxhfit;
1576 ob "crop-hack" c.crophack dc.crophack;
1577 oW "throttle" c.maxwait dc.maxwait;
1578 ob "highlight-links" c.hlinks dc.hlinks;
1579 ob "under-cursor-info" c.underinfo dc.underinfo;
1580 oi "vertical-margin" c.interpagespace dc.interpagespace;
1581 oz "zoom" c.zoom dc.zoom;
1582 ob "presentation" c.presentation dc.presentation;
1583 oi "rotation-angle" c.angle dc.angle;
1584 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
1585 oFm "fit-model" c.fitmodel dc.fitmodel;
1586 oI "pixmap-cache-size" c.memlimit dc.memlimit;
1587 oi "tex-count" c.texcount dc.texcount;
1588 oi "slice-height" c.sliceheight dc.sliceheight;
1589 oi "thumbnail-width" c.thumbw dc.thumbw;
1590 ob "persistent-location" c.jumpback dc.jumpback;
1591 oc "background-color" c.bgcolor dc.bgcolor;
1592 oi "tile-width" c.tilew dc.tilew;
1593 oi "tile-height" c.tileh dc.tileh;
1594 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
1595 ob "checkers" c.checkers dc.checkers;
1596 oi "aalevel" c.aalevel dc.aalevel;
1597 ob "trim-margins" c.trimmargins dc.trimmargins;
1598 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
1599 os "uri-launcher" c.urilauncher dc.urilauncher;
1600 os "path-launcher" c.pathlauncher dc.pathlauncher;
1601 oC "color-space" c.colorspace dc.colorspace;
1602 ob "invert-colors" c.invert dc.invert;
1603 oF "brightness" c.colorscale dc.colorscale;
1604 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
1605 oco "columns" c.columns dc.columns;
1606 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
1607 os "selection-command" c.selcmd dc.selcmd;
1608 os "synctex-command" c.stcmd dc.stcmd;
1609 os "pax-command" c.paxcmd dc.paxcmd;
1610 os "askpass-command" c.passcmd dc.passcmd;
1611 os "savepath-command" c.savecmd dc.savecmd;
1612 ob "update-cursor" c.updatecurs dc.updatecurs;
1613 oi "hint-font-size" c.hfsize dc.hfsize;
1614 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
1615 oF "page-scroll-scale" c.pgscale dc.pgscale;
1616 ob "use-pbo" c.usepbo dc.usepbo;
1617 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
1618 ob "remote-in-a-new-instance" c.riani dc.riani;
1619 op "point-and-x" c.pax dc.pax;
1620 oPm "point-and-x-mark" c.paxmark dc.paxmark;
1621 ob "scroll-bar-on-the-left" c.leftscroll dc.leftscroll;
1622 if not always
1623 then os "title" c.title dc.title;
1624 oL "last-visit" (Int64.of_float time) 0L;
1625 ob "edit-annotations-inline" c.annotinline dc.annotinline;
1626 ob "coarse-presentation-positioning" c.coarseprespos dc.coarseprespos;
1629 let keymapsbuf always dc c =
1630 let bb = Buffer.create 16 in
1631 let rec loop = function
1632 | [] -> ()
1633 | (modename, h) :: rest ->
1634 let dh = findkeyhash dc modename in
1635 if always || h <> dh
1636 then (
1637 if Hashtbl.length h > 0
1638 then (
1639 if Buffer.length bb > 0
1640 then Buffer.add_char bb '\n';
1641 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
1642 Hashtbl.iter (fun i o ->
1643 let isdifferent = always ||
1645 let dO = Hashtbl.find dh i in
1646 dO <> o
1647 with Not_found -> true
1649 if isdifferent
1650 then
1651 let addkm (k, m) =
1652 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
1653 if Wsi.withalt m then Buffer.add_string bb "alt-";
1654 if Wsi.withshift m then Buffer.add_string bb "shift-";
1655 if Wsi.withmeta m then Buffer.add_string bb "meta-";
1656 Buffer.add_string bb (Wsi.keyname k);
1658 let addkms l =
1659 let rec loop = function
1660 | [] -> ()
1661 | km :: [] -> addkm km
1662 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
1664 loop l
1666 Buffer.add_string bb "<map in='";
1667 addkm i;
1668 match o with
1669 | KMinsrt km ->
1670 Buffer.add_string bb "' out='";
1671 addkm km;
1672 Buffer.add_string bb "'/>\n"
1674 | KMinsrl kms ->
1675 Buffer.add_string bb "' out='";
1676 addkms kms;
1677 Buffer.add_string bb "'/>\n"
1679 | KMmulti (ins, kms) ->
1680 Buffer.add_char bb ' ';
1681 addkms ins;
1682 Buffer.add_string bb "' out='";
1683 addkms kms;
1684 Buffer.add_string bb "'/>\n"
1685 ) h;
1686 Buffer.add_string bb "</keymap>";
1689 loop rest
1691 loop c.keyhashes;
1695 let save1 bb leavebirdseye x h dc =
1696 let uifontsize = fstate.fontsize in
1697 let dc = if conf.bedefault then conf else dc in
1698 Buffer.add_string bb "<llppconfig>\n";
1700 if nonemptystr !fontpath
1701 then
1702 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
1703 uifontsize
1704 !fontpath
1705 else (
1706 if uifontsize <> 14
1707 then
1708 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
1711 Buffer.add_string bb "<defaults";
1712 add_attrs bb true dc dc nan;
1713 let kb = keymapsbuf true dc dc in
1714 if Buffer.length kb > 0
1715 then (
1716 Buffer.add_string bb ">\n";
1717 Buffer.add_buffer bb kb;
1718 Buffer.add_string bb "\n</defaults>\n";
1720 else Buffer.add_string bb "/>\n";
1722 let adddoc path pan anchor c bookmarks time origin =
1723 if bookmarks == [] && c = dc && anchor = emptyanchor
1724 then ()
1725 else (
1726 Printf.bprintf bb "<doc path='%s'"
1727 (Parser.enent path 0 (String.length path));
1729 if nonemptystr origin
1730 then
1731 Printf.bprintf bb "\n origin='%s'"
1732 (Parser.enent origin 0 (String.length origin));
1734 if anchor <> emptyanchor
1735 then (
1736 let n, rely, visy = anchor in
1737 Printf.bprintf bb "\n page='%d'" n;
1738 if rely > 1e-6
1739 then
1740 Printf.bprintf bb " rely='%f'" rely
1742 if abs_float visy > 1e-6
1743 then
1744 Printf.bprintf bb " visy='%f'" visy
1748 if pan != 0
1749 then Printf.bprintf bb " pan='%d'" pan;
1751 add_attrs bb false dc c time;
1752 let kb = keymapsbuf false dc c in
1754 begin match bookmarks with
1755 | [] ->
1756 if Buffer.length kb > 0
1757 then (
1758 Buffer.add_string bb ">\n";
1759 Buffer.add_buffer bb kb;
1760 Buffer.add_string bb "\n</doc>\n";
1762 else Buffer.add_string bb "/>\n"
1763 | _ ->
1764 Buffer.add_string bb ">\n<bookmarks>\n";
1765 List.iter (fun (title, _, kind) ->
1766 begin match kind with
1767 | Oanchor (page, rely, visy) ->
1768 Printf.bprintf bb
1769 "<item title='%s' page='%d'"
1770 (Parser.enent title 0 (String.length title))
1771 page
1773 if rely > 1e-6
1774 then
1775 Printf.bprintf bb " rely='%f'" rely
1777 if abs_float visy > 1e-6
1778 then
1779 Printf.bprintf bb " visy='%f'" visy
1781 | Ohistory _ | Onone | Ouri _ | Oremote _
1782 | Oremotedest _ | Olaunch _ ->
1783 failwith "unexpected link in bookmarks"
1784 end;
1785 Buffer.add_string bb "/>\n";
1786 ) bookmarks;
1787 Buffer.add_string bb "</bookmarks>";
1788 if Buffer.length kb > 0
1789 then (
1790 Buffer.add_string bb "\n";
1791 Buffer.add_buffer bb kb;
1793 Buffer.add_string bb "\n</doc>\n";
1794 end;
1798 let pan, conf =
1799 match state.mode with
1800 | Birdseye (c, pan, _, _, _) ->
1801 let beyecolumns =
1802 match conf.columns with
1803 | Cmulti ((c, _, _), _) -> Some c
1804 | Csingle _ -> None
1805 | Csplit _ -> None
1806 and columns =
1807 match c.columns with
1808 | Cmulti (c, _) -> Cmulti (c, E.a)
1809 | Csingle _ -> Csingle E.a
1810 | Csplit _ -> failwith "quit from bird's eye while split"
1812 pan, { c with beyecolumns = beyecolumns; columns = columns }
1813 | Textentry _
1814 | View
1815 | LinkNav _ -> x, conf
1817 let docpath = if nonemptystr state.path then abspath state.path else E.s in
1818 if nonemptystr docpath
1819 then (
1820 adddoc docpath pan (getanchor ())
1822 let autoscrollstep =
1823 match state.autoscroll with
1824 | Some step -> step
1825 | None -> conf.autoscrollstep
1827 begin match state.mode with
1828 | Birdseye beye -> leavebirdseye beye true
1829 | Textentry _
1830 | View
1831 | LinkNav _ -> ()
1832 end;
1833 { conf with autoscrollstep = autoscrollstep }
1835 (if conf.savebmarks then state.bookmarks else [])
1836 (now ())
1837 state.origin
1839 Hashtbl.iter (fun path (c, bookmarks, x, anchor, origin) ->
1840 if docpath <> abspath path
1841 then adddoc path x anchor c bookmarks c.lastvisit origin
1842 ) h;
1843 Buffer.add_string bb "</llppconfig>\n";
1844 true;
1847 let save leavebirdseye =
1848 let relx = float state.x /. float state.winw in
1849 let w, h, x =
1850 let cx w = truncate (relx *. float w) in
1851 List.fold_left
1852 (fun (w, h, x) ws ->
1853 match ws with
1854 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
1855 | Wsi.MaxVert -> (w, conf.cwinh, x)
1856 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
1858 (state.winw, state.winh, state.x) state.winstate
1860 conf.cwinw <- w;
1861 conf.cwinh <- h;
1862 let bb = Buffer.create 32768 in
1863 let save2 (h, dc) =
1864 save1 bb leavebirdseye x h dc
1866 if load1 save2 && Buffer.length bb > 0
1867 then
1869 let tmp = !confpath ^ ".tmp" in
1870 let oc = open_out_bin tmp in
1871 Buffer.output_buffer oc bb;
1872 close_out oc;
1873 Unix.rename tmp !confpath;
1874 with exn ->
1875 dolog "error saving configuration: %s" @@ exntos exn
1878 let gc fd =
1879 let wr s n =
1880 (* This here has a potential of rising SIGPIPE, silently and
1881 seemingly harmlessly (when -gc was supplied an invalid
1882 executable for instance) probably needs revisiting *)
1883 let n' = Unix.write fd (Bytes.of_string s) 0 n in
1884 if n != n'
1885 then Utils.error "Unix.write %d = %d" n n'
1887 let href = ref (Hashtbl.create 0) in
1888 let cref = ref defconf in
1889 let push (h, dc) =
1890 let f path (pc, _pb, _px, _pa, _po) =
1891 let s =
1892 Printf.sprintf "%s\000%ld\000" path (Int32.of_float pc.lastvisit)
1894 wr s (String.length s)
1896 Hashtbl.iter f h;
1897 href := h;
1898 cref := dc;
1899 true
1901 ignore (load1 push);
1902 Unix.shutdown fd Unix.SHUTDOWN_SEND;
1903 let s = fdcontents fd in
1904 let rec f ppos =
1905 match String.index_from s ppos '\000' with
1906 | exception Not_found -> ()
1907 | zpos1 ->
1908 match String.index_from s (zpos1+1) '\000' with
1909 | exception Not_found -> error "invalid gc input in (%S) at %d" s zpos1
1910 | zpos2 ->
1911 let okey = StringLabels.sub s ~pos:ppos ~len:(zpos1-ppos) in
1912 let nkey = StringLabels.sub s ~pos:(zpos1+1) ~len:(zpos2-zpos1-1) in
1913 if emptystr nkey
1914 then (Hashtbl.remove !href okey; f (zpos2+1))
1915 else
1916 match Hashtbl.find !href okey with
1917 | exception Not_found -> Utils.error "gc: cannot find %S" okey
1918 | v ->
1919 Hashtbl.remove !href okey;
1920 Hashtbl.replace !href nkey v;
1921 f (zpos2+1)
1923 f 0;
1924 let bb = Buffer.create 32768 in
1925 let save2 (_h, dc) = save1 bb (fun _ _ -> ()) 0 !href dc in
1926 if load1 save2 && Buffer.length bb > 0
1927 then (
1929 let tmp = !confpath ^ ".tmp" in
1930 let oc = open_out_bin tmp in
1931 Buffer.output_buffer oc bb;
1932 close_out oc;
1933 Unix.rename tmp !confpath;
1934 with exn ->
1935 dolog "error saving configuration: %s" @@ exntos exn
1939 let logcurrently = function
1940 | Idle -> dolog "Idle"
1941 | Loading (l, gen) ->
1942 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1943 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1944 dolog
1945 "Tiling %d[%d,%d] page=%s cs=%s angle=%d"
1946 l.pageno col row (~> pageopaque)
1947 (CSTE.to_string colorspace) angle
1949 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1950 angle gen conf.angle state.gen
1951 tilew tileh
1952 conf.tilew conf.tileh
1954 | Outlining _ ->
1955 dolog "outlining"