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