More reaping
[llpp.git] / config.ml
bloba7561255801839a17a387263c56bd735511febd3
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"; (n, a, b)
69 type keymap =
70 | KMinsrt of key
71 | KMinsrl of key list
72 | KMmulti of key list * key list
73 and key = int * int
74 and keyhash = (key, keymap) Hashtbl.t
75 and keystate =
76 | KSnone
77 | KSinto of (key list * key list)
78 and interpagespace = int
79 and multicolumns = multicol * pagegeom
80 and singlecolumn = pagegeom
81 and splitcolumns = columncount * pagegeom
82 and pagegeom = (pdimno * x * y * (pageno * width * height * leftx)) array
83 and multicol = columncount * covercount * covercount
84 and pdimno = int
85 and columncount = int
86 and covercount = int
87 and fitmodel = | FitWidth | FitProportional | FitPage
88 and trimmargins = bool
89 and irect = (int * int * int * int)
90 and memsize = int
91 and texcount = int
92 and sliceheight = int
93 and angle = int
94 and params = (angle * fitmodel * trimparams
95 * texcount * sliceheight * memsize
96 * colorspace * fontpath * trimcachepath
97 * haspbo * usefontconfig)
98 and width = int
99 and height = int
100 and leftx = int
101 and opaque = Opaque.t
102 and recttype = int
103 and pixmapsize = int
104 and gen = int
105 and top = float
106 and dtop = float
107 and fontpath = string
108 and trimcachepath = string
109 and aalevel = int
110 and trimparams = (trimmargins * irect)
111 and colorspace = | Rgb | Bgr | Gray
112 and haspbo = bool
113 and usefontconfig = bool
114 and uri = string
115 and caption = string
116 and x = int
117 and y = int
118 and tilex = int
119 and tiley = int
120 and tileparams = (x * y * width * height * tilex * tiley)
121 and under =
122 | Unone
123 | Ulinkuri of string
124 | Ulinkgoto of (int * int)
125 | Utext of facename
126 | Uunexpected of string
127 | Ulaunch of launchcommand
128 | Unamed of destname
129 | Uremote of (filename * pageno)
130 | Uremotedest of (filename * destname)
131 | Uannotation of (opaque * slinkindex)
132 and slinkindex = int
133 and facename = string
134 and launchcommand = string
135 and filename = string
136 and pageno = int
137 and destname = string
138 and mark =
139 | Mark_page
140 | Mark_block
141 | Mark_line
142 | Mark_word
143 and link =
144 | Lnotfound
145 | Lfound of int
146 and linkdir =
147 | LDfirst
148 | LDlast
149 | LDfirstvisible of (int * int * int)
150 | LDleft of int
151 | LDright of int
152 | LDdown of int
153 | LDup of int
154 and pagewithlinks =
155 | Pwlnotfound
156 | Pwl of int
157 and scrollb = int
158 and anchor = pageno * top * dtop
159 and rect = float * float * float * float * float * float * float * float
160 and infochange = | Memused | Docinfo | Pdim
163 class type uioh = object
164 method display : unit
165 method key : int -> int -> uioh
166 method button : int -> bool -> int -> int -> int -> uioh
167 method multiclick : int -> int -> int -> int -> uioh
168 method motion : int -> int -> uioh
169 method pmotion : int -> int -> uioh
170 method infochanged : infochange -> unit
171 method scrollpw : (int * float * float)
172 method scrollph : (int * float * float)
173 method modehash : keyhash
174 method eformsgs : bool
175 method alwaysscrolly : bool
176 end;;
178 module type TextEnumType =
180 type t
181 val name : string
182 val names : string array
183 end;;
185 module TextEnumMake (Ten : TextEnumType) =
186 struct
187 let names = Ten.names;;
188 let to_int (t : Ten.t) = Obj.magic t;;
189 let to_string t = names.(to_int t);;
190 let of_int n : Ten.t = Obj.magic n;;
191 let of_string s =
192 let rec find i =
193 if i = Array.length names
194 then failwith ("invalid " ^ Ten.name ^ ": " ^ s)
195 else (
196 if Ten.names.(i) = s
197 then of_int i
198 else find (i+1)
200 in find 0;;
201 end;;
203 module CSTE = TextEnumMake (struct
204 type t = colorspace;;
205 let name = "colorspace";;
206 let names = [|"rgb"; "bgr"; "gray"|];;
207 end);;
209 module MTE = TextEnumMake (struct
210 type t = mark;;
211 let name = "mark";;
212 let names = [|"page"; "block"; "line"; "word"|];;
213 end);;
215 module FMTE = TextEnumMake (struct
216 type t = fitmodel;;
217 let name = "fitmodel";;
218 let names = [|"width"; "proportional"; "page"|];;
219 end);;
221 type conf =
222 { mutable scrollbw : int
223 ; mutable scrollh : int
224 ; mutable scrollb : scrollb
225 ; mutable icase : bool
226 ; mutable preload : bool
227 ; mutable pagebias : int
228 ; mutable verbose : bool
229 ; mutable debug : bool
230 ; mutable scrollstep : int
231 ; mutable hscrollstep : int
232 ; mutable maxhfit : bool
233 ; mutable crophack : bool
234 ; mutable autoscrollstep : int
235 ; mutable maxwait : float option
236 ; mutable hlinks : bool
237 ; mutable underinfo : bool
238 ; mutable interpagespace : interpagespace
239 ; mutable zoom : float
240 ; mutable presentation : bool
241 ; mutable angle : angle
242 ; mutable cwinw : int
243 ; mutable cwinh : int
244 ; mutable savebmarks : bool
245 ; mutable fitmodel : fitmodel
246 ; mutable trimmargins : trimmargins
247 ; mutable trimfuzz : irect
248 ; mutable memlimit : memsize
249 ; mutable texcount : texcount
250 ; mutable sliceheight : sliceheight
251 ; mutable thumbw : width
252 ; mutable jumpback : bool
253 ; mutable bgcolor : (float * float * float)
254 ; mutable bedefault : bool
255 ; mutable tilew : int
256 ; mutable tileh : int
257 ; mutable mustoresize : memsize
258 ; mutable checkers : bool
259 ; mutable aalevel : int
260 ; mutable urilauncher : string
261 ; mutable pathlauncher : string
262 ; mutable colorspace : colorspace
263 ; mutable invert : bool
264 ; mutable colorscale : float
265 ; mutable redirectstderr : bool
266 ; mutable ghyllscroll : (int * int * int) option
267 ; mutable columns : columns
268 ; mutable beyecolumns : columncount option
269 ; mutable selcmd : string
270 ; mutable paxcmd : string
271 ; mutable passcmd : string
272 ; mutable savecmd : string
273 ; mutable updatecurs : bool
274 ; mutable keyhashes : (string * keyhash) list
275 ; mutable hfsize : int
276 ; mutable pgscale : float
277 ; mutable usepbo : bool
278 ; mutable wheelbypage : bool
279 ; mutable stcmd : string
280 ; mutable riani : bool
281 ; mutable pax : (float * int * int) ref option
282 ; mutable paxmark : mark
283 ; mutable leftscroll : bool
284 ; mutable title : string
285 ; mutable lastvisit : float
287 and columns =
288 | Csingle of singlecolumn
289 | Cmulti of multicolumns
290 | Csplit of splitcolumns
291 and outlinekind =
292 | Onone
293 | Oanchor of anchor
294 | Ouri of uri
295 | Olaunch of launchcommand
296 | Oremote of (filename * pageno)
297 | Oremotedest of (filename * destname)
298 | Ohistory of (filename * (conf * outline list * x * anchor))
299 | Oaction of (unit -> unit)
300 and outline = (caption * outlinelevel * outlinekind)
301 and outlinelevel = int
304 type page =
305 { pageno : int
306 ; pagedimno : int
307 ; pagew : int
308 ; pageh : int
309 ; pagex : int
310 ; pagey : int
311 ; pagevw : int
312 ; pagevh : int
313 ; pagedispx : int
314 ; pagedispy : int
315 ; pagecol : int
319 type tile = opaque * pixmapsize * elapsed
320 and elapsed = float;;
321 type pagemapkey = pageno * gen;;
322 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
323 and row = int
324 and col = int
325 and currently =
326 | Idle
327 | Loading of (page * gen)
328 | Tiling of (
329 page * opaque * colorspace * angle * gen * col * row * width * height
331 | Outlining of outline list
334 type mpos = int * int
335 and mstate =
336 | Msel of (mpos * mpos)
337 | Mpan of mpos
338 | Mscrolly | Mscrollx
339 | Mzoom of (buttonno * step)
340 | Mzoomrect of (mpos * mpos)
341 | Mnone
342 and buttonno = int
343 and step = int
346 type mode =
347 | Birdseye of (conf * leftx * pageno * pageno * anchor)
348 | Textentry of (textentry * onleave)
349 | View
350 | LinkNav of linktarget
351 and onleave = leavetextentrystatus -> unit
352 and leavetextentrystatus = | Cancel | Confirm
353 and helpitem = string * int * action
354 and action =
355 | Noaction
356 | Action of (uioh -> uioh)
357 and linktarget =
358 | Ltexact of (pageno * direction)
359 | Ltgendir of direction
360 | Ltnotready of (pageno * direction)
361 and direction = int (* -1, 0, 1 *)
362 and textentry = string * string * onhist option * onkey * ondone * cancelonempty
363 and onkey = string -> int -> te
364 and ondone = string -> unit
365 and histcancel = unit -> unit
366 and onhist = ((histcmd -> string) * histcancel)
367 and histcmd = HCnext | HCprev | HCfirst | HClast
368 and cancelonempty = bool
369 and te =
370 | TEstop
371 | TEdone of string
372 | TEcont of string
373 | TEswitch of textentry
376 type 'a circbuf =
377 { store : 'a array
378 ; mutable rc : int
379 ; mutable wc : int
380 ; mutable len : int
384 type state =
385 { mutable ss : Unix.file_descr
386 ; mutable wsfd : Unix.file_descr
387 ; mutable errfd : Unix.file_descr option
388 ; mutable stderr : Unix.file_descr
389 ; mutable errmsgs : Buffer.t
390 ; mutable newerrmsgs : bool
391 ; mutable w : int
392 ; mutable x : int
393 ; mutable y : int
394 ; mutable anchor : anchor
395 ; mutable ranchors : (string * string * anchor * string) list
396 ; mutable maxy : int
397 ; mutable layout : page list
398 ; pagemap : (pagemapkey, opaque) Hashtbl.t
399 ; tilemap : (tilemapkey, tile) Hashtbl.t
400 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
401 ; mutable pdims : (pageno * width * height * leftx) list
402 ; mutable pagecount : int
403 ; mutable currently : currently
404 ; mutable mstate : mstate
405 ; mutable searchpattern : string
406 ; mutable rects : (pageno * recttype * rect) list
407 ; mutable rects1 : (pageno * recttype * rect) list
408 ; mutable text : string
409 ; mutable winstate : Wsi.winstate list
410 ; mutable mode : mode
411 ; mutable uioh : uioh
412 ; mutable outlines : outline array
413 ; mutable bookmarks : outline list
414 ; mutable path : string
415 ; mutable password : string
416 ; mutable nameddest : string
417 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
418 ; mutable memused : memsize
419 ; mutable gen : gen
420 ; mutable throttle : (page list * int * float) option
421 ; mutable autoscroll : int option
422 ; mutable ghyll : (int option -> unit)
423 ; mutable help : helpitem array
424 ; mutable docinfo : (int * string) list
425 ; mutable checkerstexid : GlTex.texture_id option
426 ; hists : hists
427 ; mutable prevzoom : (float * int)
428 ; mutable progress : float
429 ; mutable redisplay : bool
430 ; mutable mpos : mpos
431 ; mutable keystate : keystate
432 ; mutable glinks : bool
433 ; mutable prevcolumns : (columns * float) option
434 ; mutable winw : int
435 ; mutable winh : int
436 ; mutable reprf : (unit -> unit)
437 ; mutable origin : string
438 ; mutable roam : (unit -> unit)
439 ; mutable bzoom : bool
440 ; mutable traw : [`float] Raw.t
441 ; mutable vraw : [`float] Raw.t
443 and hists =
444 { pat : string circbuf
445 ; pag : string circbuf
446 ; nav : anchor circbuf
447 ; sel : string circbuf
451 let emptyanchor = (0, 0.0, 0.0);;
452 let emptykeyhash = Hashtbl.create 0;;
453 let firstgeomcmds = E.s, [];;
454 let noghyll _ = ();;
455 let noreprf () = ();;
456 let noroam () = ();;
458 let nouioh : uioh = object (self)
459 method display = ()
460 method key _ _ = self
461 method multiclick _ _ _ _ = self
462 method button _ _ _ _ _ = self
463 method motion _ _ = self
464 method pmotion _ _ = self
465 method infochanged _ = ()
466 method scrollpw = (0, nan, nan)
467 method scrollph = (0, nan, nan)
468 method modehash = emptykeyhash
469 method eformsgs = false
470 method alwaysscrolly = false
471 end;;
473 let platform_to_string = function
474 | Punknown -> "unknown"
475 | Plinux -> "Linux"
476 | Posx -> "OSX"
477 | Psun -> "Sun"
478 | Pbsd -> "BSD"
479 | Pcygwin -> "Cygwin"
482 let version () =
483 Printf.sprintf "llpp version %s, fitz %s, ocaml %s/%d bit"
484 Help.version (fz_version ()) Sys.ocaml_version Sys.word_size
487 let geturl s =
488 let colonpos = try String.index s ':' with Not_found -> -1 in
489 let len = String.length s in
490 if colonpos >= 0 && colonpos + 3 < len
491 then (
492 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
493 then
494 let schemestartpos =
495 try String.rindex_from s colonpos ' '
496 with Not_found -> -1
498 let scheme =
499 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
501 match scheme with
502 | "http" | "ftp" | "mailto" ->
503 let epos =
504 try String.index_from s colonpos ' '
505 with Not_found -> len
507 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
508 | _ -> E.s
509 else E.s
511 else E.s
514 let defconf =
515 { scrollbw = 7
516 ; scrollh = 12
517 ; scrollb = scrollbhv lor scrollbvv
518 ; icase = true
519 ; preload = true
520 ; pagebias = 0
521 ; verbose = false
522 ; debug = false
523 ; scrollstep = 24
524 ; hscrollstep = 24
525 ; maxhfit = true
526 ; crophack = false
527 ; autoscrollstep = 2
528 ; maxwait = None
529 ; hlinks = false
530 ; underinfo = false
531 ; interpagespace = 2
532 ; zoom = 1.0
533 ; presentation = false
534 ; angle = 0
535 ; cwinw = 900
536 ; cwinh = 900
537 ; savebmarks = true
538 ; fitmodel = FitProportional
539 ; trimmargins = false
540 ; trimfuzz = (0,0,0,0)
541 ; memlimit = 32 lsl 20
542 ; texcount = 256
543 ; sliceheight = 24
544 ; thumbw = 76
545 ; jumpback = true
546 ; bgcolor = (0.5, 0.5, 0.5)
547 ; bedefault = false
548 ; tilew = 2048
549 ; tileh = 2048
550 ; mustoresize = 256 lsl 20
551 ; checkers = true
552 ; aalevel = 8
553 ; urilauncher =
554 (match platform with
555 | Plinux | Psun | Pbsd -> "xdg-open \"%s\""
556 | Posx -> "open \"%s\""
557 | Pcygwin -> "cygstart \"%s\""
558 | Punknown -> "echo %s")
559 ; pathlauncher = "lp \"%s\""
560 ; selcmd =
561 (match platform with
562 | Plinux | Pbsd | Psun -> "xsel -i"
563 | Posx -> "pbcopy"
564 | Pcygwin -> "wsel"
565 | Punknown -> "cat")
566 ; paxcmd = "cat"
567 ; passcmd = E.s
568 ; savecmd = E.s
569 ; colorspace = Rgb
570 ; invert = false
571 ; colorscale = 1.0
572 ; redirectstderr = false
573 ; ghyllscroll = None
574 ; columns = Csingle [||]
575 ; beyecolumns = None
576 ; updatecurs = false
577 ; hfsize = 12
578 ; pgscale = 1.0
579 ; usepbo = false
580 ; wheelbypage = false
581 ; stcmd = "echo SyncTex"
582 ; riani = false
583 ; pax = None
584 ; paxmark = Mark_word
585 ; leftscroll = false
586 ; title = E.s
587 ; lastvisit = 0.0
588 ; keyhashes =
589 let mk n = (n, Hashtbl.create 1) in
590 [ mk "global"
591 ; mk "info"
592 ; mk "help"
593 ; mk "outline"
594 ; mk "listview"
595 ; mk "birdseye"
596 ; mk "textentry"
597 ; mk "links"
598 ; mk "view"
603 let conf = { defconf with angle = defconf.angle };;
605 let gotouri uri =
606 if emptystr conf.urilauncher
607 then print_endline uri
608 else (
609 let url = geturl uri in
610 if emptystr url
611 then (Printf.eprintf "obtained empty url from uri %S\n" uri)
612 else
613 let re = Str.regexp "%s" in
614 let command = Str.global_replace re url conf.urilauncher in
615 try ignore (popen command [])
616 with exn ->
617 Printf.eprintf
618 "failed to execute `%s': %s\n" command (exntos exn);
619 flush stderr;
623 let makehelp () =
624 let strings =
625 version ()
626 :: "(searching in this text works just by typing (i.e. no initial '/'))"
627 :: E.s :: Help.keys
629 Array.of_list (
630 List.map (fun s ->
631 let url = geturl s in
632 if nonemptystr url
633 then (s, 0, Action (fun u -> gotouri url; u))
634 else (s, 0, Noaction)
635 ) strings);
638 let cbnew n v =
639 { store = Array.make n v
640 ; rc = 0
641 ; wc = 0
642 ; len = 0
646 let cbcap b = Array.length b.store;;
648 let cbput b v =
649 let cap = cbcap b in
650 b.store.(b.wc) <- v;
651 b.wc <- (b.wc + 1) mod cap;
652 b.rc <- b.wc;
653 b.len <- min (b.len + 1) cap;
656 let cbempty b = b.len = 0;;
658 let cbgetg b circular dir =
659 if cbempty b
660 then b.store.(0)
661 else
662 let rc = b.rc + dir in
663 let rc =
664 if circular
665 then (
666 if rc = -1
667 then b.len-1
668 else (
669 if rc >= b.len
670 then 0
671 else rc
674 else bound rc 0 (b.len-1)
676 b.rc <- rc;
677 b.store.(rc);
680 let cbget b = cbgetg b false;;
681 let cbgetc b = cbgetg b true;;
683 let state =
684 { ss = Unix.stdin
685 ; wsfd = Unix.stdin
686 ; errfd = None
687 ; stderr = Unix.stderr
688 ; errmsgs = Buffer.create 0
689 ; newerrmsgs = false
690 ; x = 0
691 ; y = 0
692 ; w = 0
693 ; anchor = emptyanchor
694 ; ranchors = []
695 ; layout = []
696 ; maxy = max_int
697 ; tilelru = Queue.create ()
698 ; pagemap = Hashtbl.create 10
699 ; tilemap = Hashtbl.create 10
700 ; pdims = []
701 ; pagecount = 0
702 ; currently = Idle
703 ; mstate = Mnone
704 ; rects = []
705 ; rects1 = []
706 ; text = E.s
707 ; mode = View
708 ; winstate = []
709 ; searchpattern = E.s
710 ; outlines = E.a
711 ; bookmarks = []
712 ; path = E.s
713 ; password = E.s
714 ; nameddest = E.s
715 ; geomcmds = firstgeomcmds
716 ; hists =
717 { nav = cbnew 10 emptyanchor
718 ; pat = cbnew 10 E.s
719 ; pag = cbnew 10 E.s
720 ; sel = cbnew 10 E.s
722 ; memused = 0
723 ; gen = 0
724 ; throttle = None
725 ; autoscroll = None
726 ; ghyll = noghyll
727 ; help = makehelp ()
728 ; docinfo = []
729 ; checkerstexid = None
730 ; prevzoom = (1.0, 0)
731 ; progress = -1.0
732 ; uioh = nouioh
733 ; redisplay = true
734 ; mpos = (-1, -1)
735 ; keystate = KSnone
736 ; glinks = false
737 ; prevcolumns = None
738 ; winw = -1
739 ; winh = -1
740 ; reprf = noreprf
741 ; origin = E.s
742 ; roam = noroam
743 ; bzoom = false
744 ; traw = Raw.create_static `float ~len:8
745 ; vraw = Raw.create_static `float ~len:8
749 let copykeyhashes c =
750 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
753 let calcips h =
754 let d = state.winh - h in
755 max conf.interpagespace ((d + 1) / 2)
758 let rowyh (c, coverA, coverB) b n =
759 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
760 then
761 let _, _, vy, (_, _, h, _) = b.(n) in
762 (vy, h)
763 else
764 let n' = n - coverA in
765 let d = n' mod c in
766 let s = n - d in
767 let e = min state.pagecount (s + c) in
768 let rec find m miny maxh = if m = e then miny, maxh else
769 let _, _, y, (_, _, h, _) = b.(m) in
770 let miny = min miny y in
771 let maxh = max maxh h in
772 find (m+1) miny maxh
773 in find s max_int 0
776 let page_of_y y =
777 let ((c, coverA, coverB) as cl), b =
778 match conf.columns with
779 | Csingle b -> (1, 0, 0), b
780 | Cmulti (c, b) -> c, b
781 | Csplit (_, b) -> (1, 0, 0), b
783 if Array.length b = 0
784 then -1
785 else
786 let rec bsearch nmin nmax =
787 if nmin > nmax
788 then bound nmin 0 (state.pagecount-1)
789 else
790 let n = (nmax + nmin) / 2 in
791 let vy, h = rowyh cl b n in
792 let y0, y1 =
793 if conf.presentation
794 then
795 let ips = calcips h in
796 let y0 = vy - ips in
797 let y1 = vy + h + ips in
798 y0, y1
799 else (
800 if n = 0
801 then 0, vy + h + conf.interpagespace
802 else
803 let y0 = vy - conf.interpagespace in
804 y0, y0 + h + conf.interpagespace
807 if y >= y0 && y < y1
808 then (
809 if c = 1
810 then n
811 else (
812 if n > coverA
813 then
814 if n < state.pagecount - coverB
815 then ((n-coverA)/c)*c + coverA
816 else n
817 else n
820 else (
821 if y > y0
822 then bsearch (n+1) nmax
823 else bsearch nmin (n-1)
826 bsearch 0 (state.pagecount-1);
829 let calcheight () =
830 match conf.columns with
831 | Cmulti ((_, _, _) as cl, b) ->
832 if Array.length b > 0
833 then
834 let y, h = rowyh cl b (Array.length b - 1) in
835 y + h + (if conf.presentation then calcips h else 0)
836 else 0
837 | Csingle b ->
838 if Array.length b > 0
839 then
840 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
841 y + h + (if conf.presentation then calcips h else 0)
842 else 0
843 | Csplit (_, b) ->
844 if Array.length b > 0
845 then
846 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
847 y + h
848 else 0
851 let getpageywh pageno =
852 let pageno = bound pageno 0 (state.pagecount-1) in
853 match conf.columns with
854 | Csingle b ->
855 if Array.length b = 0
856 then 0, 0, 0
857 else
858 let (_, _, y, (_, w, h, _)) = b.(pageno) in
859 let y =
860 if conf.presentation
861 then y - calcips h
862 else y
864 y, w, h
865 | Cmulti (cl, b) ->
866 if Array.length b = 0
867 then 0, 0, 0
868 else
869 let y, h = rowyh cl b pageno in
870 let (_, _, _, (_, w, _, _)) = b.(pageno) in
871 let y =
872 if conf.presentation
873 then y - calcips h
874 else y
876 y, w, h
877 | Csplit (c, b) ->
878 if Array.length b = 0
879 then 0, 0, 0
880 else
881 let n = pageno*c in
882 let (_, _, y, (_, w, h, _)) = b.(n) in
883 y, w / c, h
886 let getpageyh pageno =
887 let y,_,h = getpageywh pageno in
888 y, h;
891 let getpagedim pageno =
892 let rec f ppdim l =
893 match l with
894 | (n, _, _, _) as pdim :: rest ->
895 if n >= pageno
896 then (if n = pageno then pdim else ppdim)
897 else f pdim rest
899 | [] -> ppdim
901 f (-1, -1, -1, -1) state.pdims
904 let getpagey pageno = fst (getpageyh pageno);;
906 let getanchor1 l =
907 let top =
908 let coloff = l.pagecol * l.pageh in
909 float (l.pagey + coloff) /. float l.pageh
911 let dtop =
912 if l.pagedispy = 0
913 then
915 else (
916 if conf.presentation
917 then float l.pagedispy /. float (calcips l.pageh)
918 else float l.pagedispy /. float conf.interpagespace
921 (l.pageno, top, dtop)
924 let getanchor () =
925 match state.layout with
926 | l :: _ -> getanchor1 l
927 | [] ->
928 let n = page_of_y state.y in
929 if n = -1
930 then state.anchor
931 else
932 let y, h = getpageyh n in
933 let dy = y - state.y in
934 let dtop =
935 if conf.presentation
936 then
937 let ips = calcips h in
938 float (dy + ips) /. float ips
939 else
940 float dy /. float conf.interpagespace
942 (n, 0.0, dtop)
945 let fontpath = ref E.s;;
947 type historder = [ `lastvisit | `title | `path | `file ];;
948 let historder : historder ref = ref `lastvisit;;
950 module KeyMap =
951 Map.Make (struct type t = (int * int) let compare = compare end);;
953 open Parser;;
955 let unent s =
956 let l = String.length s in
957 let b = Buffer.create l in
958 unent b s 0 l;
959 Buffer.contents b;
962 let home =
963 try Sys.getenv "HOME"
964 with exn ->
965 prerr_endline
966 ("Can not determine home directory location: " ^ exntos exn);
970 let modifier_of_string = function
971 | "alt" -> Wsi.altmask
972 | "shift" -> Wsi.shiftmask
973 | "ctrl" | "control" -> Wsi.ctrlmask
974 | "meta" -> Wsi.metamask
975 | _ -> 0
978 let key_of_string =
979 let r = Str.regexp "-" in
980 fun s ->
981 let elems = Str.full_split r s in
982 let f n k m =
983 let g s =
984 let m1 = modifier_of_string s in
985 if m1 = 0
986 then (Wsi.namekey s, m)
987 else (k, m lor m1)
988 in function
989 | Str.Delim s when n land 1 = 0 -> g s
990 | Str.Text s -> g s
991 | Str.Delim _ -> (k, m)
993 let rec loop n k m = function
994 | [] -> (k, m)
995 | x :: xs ->
996 let k, m = f n k m x in
997 loop (n+1) k m xs
999 loop 0 0 0 elems
1002 let keys_of_string =
1003 let r = Str.regexp "[ \t]" in
1004 fun s ->
1005 let elems = Str.split r s in
1006 List.map key_of_string elems
1009 let config_of c attrs =
1010 let apply c k v =
1012 match k with
1013 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
1014 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
1015 | "case-insensitive-search" -> { c with icase = bool_of_string v }
1016 | "preload" -> { c with preload = bool_of_string v }
1017 | "page-bias" -> { c with pagebias = int_of_string v }
1018 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
1019 | "horizontal-scroll-step" ->
1020 { c with hscrollstep = max (int_of_string v) 1 }
1021 | "auto-scroll-step" ->
1022 { c with autoscrollstep = max 0 (int_of_string v) }
1023 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
1024 | "crop-hack" -> { c with crophack = bool_of_string v }
1025 | "throttle" ->
1026 let mw =
1027 match String.lowercase v with
1028 | "true" -> Some infinity
1029 | "false" -> None
1030 | f -> Some (float_of_string f)
1032 { c with maxwait = mw}
1033 | "highlight-links" -> { c with hlinks = bool_of_string v }
1034 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
1035 | "vertical-margin" ->
1036 { c with interpagespace = max 0 (int_of_string v) }
1037 | "zoom" ->
1038 let zoom = float_of_string v /. 100. in
1039 let zoom = max zoom 0.0 in
1040 { c with zoom = zoom }
1041 | "presentation" -> { c with presentation = bool_of_string v }
1042 | "rotation-angle" -> { c with angle = int_of_string v }
1043 | "width" -> { c with cwinw = max 20 (int_of_string v) }
1044 | "height" -> { c with cwinh = max 20 (int_of_string v) }
1045 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
1046 | "proportional-display" ->
1047 let fm =
1048 if bool_of_string v
1049 then FitProportional
1050 else FitWidth
1052 { c with fitmodel = fm }
1053 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
1054 | "pixmap-cache-size" ->
1055 { c with memlimit = max 2 (int_of_string_with_suffix v) }
1056 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
1057 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
1058 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
1059 | "persistent-location" -> { c with jumpback = bool_of_string v }
1060 | "background-color" -> { c with bgcolor = color_of_string v }
1061 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
1062 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
1063 | "mupdf-store-size" ->
1064 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
1065 | "checkers" -> { c with checkers = bool_of_string v }
1066 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
1067 | "trim-margins" -> { c with trimmargins = bool_of_string v }
1068 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
1069 | "uri-launcher" -> { c with urilauncher = unent v }
1070 | "path-launcher" -> { c with pathlauncher = unent v }
1071 | "color-space" -> { c with colorspace = CSTE.of_string v }
1072 | "invert-colors" -> { c with invert = bool_of_string v }
1073 | "brightness" -> { c with colorscale = float_of_string v }
1074 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
1075 | "ghyllscroll" -> { c with ghyllscroll = ghyllscroll_of_string v }
1076 | "columns" ->
1077 let (n, _, _) as nab = multicolumns_of_string v in
1078 if n < 0
1079 then { c with columns = Csplit (-n, E.a) }
1080 else { c with columns = Cmulti (nab, E.a) }
1081 | "birds-eye-columns" ->
1082 { c with beyecolumns = Some (max (int_of_string v) 2) }
1083 | "selection-command" -> { c with selcmd = unent v }
1084 | "synctex-command" -> { c with stcmd = unent v }
1085 | "pax-command" -> { c with paxcmd = unent v }
1086 | "askpass-command" -> { c with passcmd = unent v }
1087 | "savepath-command" -> { c with savecmd = unent v }
1088 | "update-cursor" -> { c with updatecurs = bool_of_string v }
1089 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
1090 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
1091 | "use-pbo" -> { c with usepbo = bool_of_string v }
1092 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
1093 | "horizontal-scrollbar-visible" ->
1094 let b =
1095 if bool_of_string v
1096 then c.scrollb lor scrollbhv
1097 else c.scrollb land (lnot scrollbhv)
1099 { c with scrollb = b }
1100 | "vertical-scrollbar-visible" ->
1101 let b =
1102 if bool_of_string v
1103 then c.scrollb lor scrollbvv
1104 else c.scrollb land (lnot scrollbvv)
1106 { c with scrollb = b }
1107 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
1108 | "point-and-x" ->
1109 { c with pax =
1110 if bool_of_string v
1111 then Some (ref (0.0, 0, 0))
1112 else None }
1113 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
1114 | "scroll-bar-on-the-left" -> { c with leftscroll = bool_of_string v }
1115 | "title" -> { c with title = unent v }
1116 | "last-visit" -> { c with lastvisit = float_of_string v }
1117 | _ -> c
1118 with exn ->
1119 prerr_endline ("Error processing attribute (`" ^
1120 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
1123 let rec fold c = function
1124 | [] -> c
1125 | (k, v) :: rest ->
1126 let c = apply c k v in
1127 fold c rest
1129 fold { c with keyhashes = copykeyhashes c } attrs;
1132 let fromstring f pos n v d =
1133 try f v
1134 with exn ->
1135 dolog "Error processing attribute (%S=%S) at %d\n%s"
1136 n v pos (exntos exn)
1141 let bookmark_of attrs =
1142 let rec fold title page rely visy = function
1143 | ("title", v) :: rest -> fold v page rely visy rest
1144 | ("page", v) :: rest -> fold title v rely visy rest
1145 | ("rely", v) :: rest -> fold title page v visy rest
1146 | ("visy", v) :: rest -> fold title page rely v rest
1147 | _ :: rest -> fold title page rely visy rest
1148 | [] -> title, page, rely, visy
1150 fold "invalid" "0" "0" "0" attrs
1153 let doc_of attrs =
1154 let rec fold path page rely pan visy = function
1155 | ("path", v) :: rest -> fold v page rely pan visy rest
1156 | ("page", v) :: rest -> fold path v rely pan visy rest
1157 | ("rely", v) :: rest -> fold path page v pan visy rest
1158 | ("pan", v) :: rest -> fold path page rely v visy rest
1159 | ("visy", v) :: rest -> fold path page rely pan v rest
1160 | _ :: rest -> fold path page rely pan visy rest
1161 | [] -> path, page, rely, pan, visy
1163 fold E.s "0" "0" "0" "0" attrs
1166 let map_of attrs =
1167 let rec fold rs ls = function
1168 | ("out", v) :: rest -> fold v ls rest
1169 | ("in", v) :: rest -> fold rs v rest
1170 | _ :: rest -> fold ls rs rest
1171 | [] -> ls, rs
1173 fold E.s E.s attrs
1176 let setconf dst src =
1177 dst.scrollbw <- src.scrollbw;
1178 dst.scrollh <- src.scrollh;
1179 dst.icase <- src.icase;
1180 dst.preload <- src.preload;
1181 dst.pagebias <- src.pagebias;
1182 dst.verbose <- src.verbose;
1183 dst.scrollstep <- src.scrollstep;
1184 dst.maxhfit <- src.maxhfit;
1185 dst.crophack <- src.crophack;
1186 dst.autoscrollstep <- src.autoscrollstep;
1187 dst.maxwait <- src.maxwait;
1188 dst.hlinks <- src.hlinks;
1189 dst.underinfo <- src.underinfo;
1190 dst.interpagespace <- src.interpagespace;
1191 dst.zoom <- src.zoom;
1192 dst.presentation <- src.presentation;
1193 dst.angle <- src.angle;
1194 dst.cwinw <- src.cwinw;
1195 dst.cwinh <- src.cwinh;
1196 dst.savebmarks <- src.savebmarks;
1197 dst.memlimit <- src.memlimit;
1198 dst.fitmodel <- src.fitmodel;
1199 dst.texcount <- src.texcount;
1200 dst.sliceheight <- src.sliceheight;
1201 dst.thumbw <- src.thumbw;
1202 dst.jumpback <- src.jumpback;
1203 dst.bgcolor <- src.bgcolor;
1204 dst.tilew <- src.tilew;
1205 dst.tileh <- src.tileh;
1206 dst.mustoresize <- src.mustoresize;
1207 dst.checkers <- src.checkers;
1208 dst.aalevel <- src.aalevel;
1209 dst.trimmargins <- src.trimmargins;
1210 dst.trimfuzz <- src.trimfuzz;
1211 dst.urilauncher <- src.urilauncher;
1212 dst.colorspace <- src.colorspace;
1213 dst.invert <- src.invert;
1214 dst.colorscale <- src.colorscale;
1215 dst.redirectstderr <- src.redirectstderr;
1216 dst.ghyllscroll <- src.ghyllscroll;
1217 dst.columns <- src.columns;
1218 dst.beyecolumns <- src.beyecolumns;
1219 dst.selcmd <- src.selcmd;
1220 dst.updatecurs <- src.updatecurs;
1221 dst.pathlauncher <- src.pathlauncher;
1222 dst.keyhashes <- copykeyhashes src;
1223 dst.hfsize <- src.hfsize;
1224 dst.hscrollstep <- src.hscrollstep;
1225 dst.pgscale <- src.pgscale;
1226 dst.usepbo <- src.usepbo;
1227 dst.wheelbypage <- src.wheelbypage;
1228 dst.stcmd <- src.stcmd;
1229 dst.paxcmd <- src.paxcmd;
1230 dst.passcmd <- src.passcmd;
1231 dst.savecmd <- src.savecmd;
1232 dst.scrollb <- src.scrollb;
1233 dst.riani <- src.riani;
1234 dst.paxmark <- src.paxmark;
1235 dst.leftscroll <- src.leftscroll;
1236 dst.title <- src.title;
1237 dst.pax <-
1238 if src.pax = None
1239 then None
1240 else Some ((ref (0.0, 0, 0)));
1243 let findkeyhash c name =
1244 try List.assoc name c.keyhashes
1245 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
1248 let get s =
1249 let h = Hashtbl.create 10 in
1250 let dc = { defconf with angle = defconf.angle } in
1251 let rec toplevel v t spos _ =
1252 match t with
1253 | Vdata | Vcdata | Vend -> v
1254 | Vopen ("llppconfig", _, closed) ->
1255 if closed
1256 then v
1257 else { v with f = llppconfig }
1258 | Vopen _ ->
1259 error "unexpected subelement at top level" s spos
1260 | Vclose _ -> error "unexpected close at top level" s spos
1262 and llppconfig v t spos _ =
1263 match t with
1264 | Vdata | Vcdata -> v
1265 | Vend -> error "unexpected end of input in llppconfig" s spos
1266 | Vopen ("defaults", attrs, closed) ->
1267 let c = config_of dc attrs in
1268 setconf dc c;
1269 if closed
1270 then v
1271 else { v with f = defaults }
1273 | Vopen ("ui-font", attrs, closed) ->
1274 let rec getsize size = function
1275 | [] -> size
1276 | ("size", v) :: rest ->
1277 let size =
1278 fromstring int_of_string spos "size" v fstate.fontsize in
1279 getsize size rest
1280 | l -> getsize size l
1282 fstate.fontsize <- getsize fstate.fontsize attrs;
1283 if closed
1284 then v
1285 else { v with f = uifont (Buffer.create 10) }
1287 | Vopen ("doc", attrs, closed) ->
1288 let pathent, spage, srely, span, svisy = doc_of attrs in
1289 let path = unent pathent
1290 and pageno = fromstring int_of_string spos "page" spage 0
1291 and rely = fromstring float_of_string spos "rely" srely 0.0
1292 and pan = fromstring int_of_string spos "pan" span 0
1293 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1294 let c = config_of dc attrs in
1295 let anchor = (pageno, rely, visy) in
1296 if closed
1297 then (Hashtbl.add h path (c, [], pan, anchor); v)
1298 else { v with f = doc path pan anchor c [] }
1300 | Vopen _ ->
1301 error "unexpected subelement in llppconfig" s spos
1303 | Vclose "llppconfig" -> { v with f = toplevel }
1304 | Vclose _ -> error "unexpected close in llppconfig" s spos
1306 and defaults v t spos _ =
1307 match t with
1308 | Vdata | Vcdata -> v
1309 | Vend -> error "unexpected end of input in defaults" s spos
1310 | Vopen ("keymap", attrs, closed) ->
1311 let modename =
1312 try List.assoc "mode" attrs
1313 with Not_found -> "global" in
1314 if closed
1315 then v
1316 else
1317 let ret keymap =
1318 let h = findkeyhash dc modename in
1319 KeyMap.iter (Hashtbl.replace h) keymap;
1320 defaults
1322 { v with f = pkeymap ret KeyMap.empty }
1324 | Vopen (_, _, _) ->
1325 error "unexpected subelement in defaults" s spos
1327 | Vclose "defaults" ->
1328 { v with f = llppconfig }
1330 | Vclose _ -> error "unexpected close in defaults" s spos
1332 and uifont b v t spos epos =
1333 match t with
1334 | Vdata | Vcdata ->
1335 Buffer.add_substring b s spos (epos - spos);
1337 | Vopen (_, _, _) ->
1338 error "unexpected subelement in ui-font" s spos
1339 | Vclose "ui-font" ->
1340 if emptystr !fontpath
1341 then fontpath := Buffer.contents b;
1342 { v with f = llppconfig }
1343 | Vclose _ -> error "unexpected close in ui-font" s spos
1344 | Vend -> error "unexpected end of input in ui-font" s spos
1346 and doc path pan anchor c bookmarks v t spos _ =
1347 match t with
1348 | Vdata | Vcdata -> v
1349 | Vend -> error "unexpected end of input in doc" s spos
1350 | Vopen ("bookmarks", _, closed) ->
1351 if closed
1352 then v
1353 else { v with f = pbookmarks path pan anchor c bookmarks }
1355 | Vopen ("keymap", attrs, closed) ->
1356 let modename =
1357 try List.assoc "mode" attrs
1358 with Not_found -> "global"
1360 if closed
1361 then v
1362 else
1363 let ret keymap =
1364 let h = findkeyhash c modename in
1365 KeyMap.iter (Hashtbl.replace h) keymap;
1366 doc path pan anchor c bookmarks
1368 { v with f = pkeymap ret KeyMap.empty }
1370 | Vopen (_, _, _) ->
1371 error "unexpected subelement in doc" s spos
1373 | Vclose "doc" ->
1374 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
1375 { v with f = llppconfig }
1377 | Vclose _ -> error "unexpected close in doc" s spos
1379 and pkeymap ret keymap v t spos _ =
1380 match t with
1381 | Vdata | Vcdata -> v
1382 | Vend -> error "unexpected end of input in keymap" s spos
1383 | Vopen ("map", attrs, closed) ->
1384 let r, l = map_of attrs in
1385 let kss = fromstring keys_of_string spos "in" r [] in
1386 let lss = fromstring keys_of_string spos "out" l [] in
1387 let keymap =
1388 match kss with
1389 | [] -> keymap
1390 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
1391 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
1393 if closed
1394 then { v with f = pkeymap ret keymap }
1395 else
1396 let f () = v in
1397 { v with f = skip "map" f }
1399 | Vopen _ ->
1400 error "unexpected subelement in keymap" s spos
1402 | Vclose "keymap" ->
1403 { v with f = ret keymap }
1405 | Vclose _ -> error "unexpected close in keymap" s spos
1407 and pbookmarks path pan anchor c bookmarks v t spos _ =
1408 match t with
1409 | Vdata | Vcdata -> v
1410 | Vend -> error "unexpected end of input in bookmarks" s spos
1411 | Vopen ("item", attrs, closed) ->
1412 let titleent, spage, srely, svisy = bookmark_of attrs in
1413 let page = fromstring int_of_string spos "page" spage 0
1414 and rely = fromstring float_of_string spos "rely" srely 0.0
1415 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1416 let bookmarks =
1417 (unent titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
1419 if closed
1420 then { v with f = pbookmarks path pan anchor c bookmarks }
1421 else
1422 let f () = v in
1423 { v with f = skip "item" f }
1425 | Vopen _ ->
1426 error "unexpected subelement in bookmarks" s spos
1428 | Vclose "bookmarks" ->
1429 { v with f = doc path pan anchor c bookmarks }
1431 | Vclose _ -> Parser.parse_error "unexpected close in bookmarks" s spos
1433 and skip tag f v t spos _ =
1434 match t with
1435 | Vdata | Vcdata -> v
1436 | Vend ->
1437 Parser.parse_error ("unexpected end of input in skipped " ^ tag) s spos
1438 | Vopen (tag', _, closed) ->
1439 if closed
1440 then v
1441 else
1442 let f' () = { v with f = skip tag f } in
1443 { v with f = skip tag' f' }
1444 | Vclose ctag ->
1445 if tag = ctag
1446 then f ()
1447 else Parser.parse_error ("unexpected close in skipped " ^ tag) s spos
1450 parse { f = toplevel; accu = () } s;
1451 h, dc;
1454 let do_load f ic =
1456 let len = in_channel_length ic in
1457 let s = really_input_string ic len in
1458 f s;
1459 with
1460 | Parse_error (msg, s, pos) ->
1461 let subs = subs s pos in
1462 Utils.error "parse error: %s: at %d [..%s..]" msg pos subs
1464 | exn ->
1465 failwith ("config load error: " ^ exntos exn)
1468 let defconfpath =
1469 let dir =
1470 let xdgconfdir = Utils.getenvwithdef "XDG_CONFIG_HOME" E.s in
1471 if xdgconfdir == E.s
1472 then
1474 let dir = Filename.concat home ".config" in
1475 if Sys.is_directory dir then dir else home
1476 with _ -> home
1477 else xdgconfdir
1479 Filename.concat dir "llpp.conf"
1482 let confpath = ref defconfpath;;
1484 let load1 f =
1485 if Sys.file_exists !confpath
1486 then
1487 match
1488 (try Some (open_in_bin !confpath)
1489 with exn ->
1490 prerr_endline
1491 ("Error opening configuration file `" ^ !confpath ^ "': " ^
1492 exntos exn);
1493 None
1495 with
1496 | Some ic ->
1497 let success =
1499 f (do_load get ic)
1500 with exn ->
1501 prerr_endline
1502 ("Error loading configuration from `" ^ !confpath ^ "': " ^
1503 exntos exn);
1504 false
1506 close_in ic;
1507 success
1509 | None -> false
1510 else
1511 f (Hashtbl.create 0, defconf)
1514 let load openlast =
1515 let f (h, dc) =
1516 if openlast
1517 then (
1518 let path, _ =
1519 Hashtbl.fold
1520 (fun path (conf, _, _, _) ((_, besttime) as best) ->
1521 if conf.lastvisit > besttime
1522 then (path, conf.lastvisit)
1523 else best)
1525 (state.path, -.infinity)
1527 state.path <- path;
1529 let pc, pb, px, pa =
1531 let path =
1532 if emptystr state.origin
1533 then state.path
1534 else state.origin
1536 let absname = abspath path in
1537 Hashtbl.find h absname
1538 with Not_found -> dc, [], 0, emptyanchor
1540 setconf defconf dc;
1541 setconf conf pc;
1542 state.bookmarks <- pb;
1543 state.x <- px;
1544 if conf.jumpback
1545 then state.anchor <- pa;
1546 cbput state.hists.nav pa;
1547 true
1549 load1 f
1552 let gethist listref =
1553 let f (h, _) =
1554 listref :=
1555 Hashtbl.fold (fun path (pc, pb, px, pa) accu ->
1556 (path, pc, pb, px, pa) :: accu)
1557 h [];
1558 true
1560 load1 f
1563 let add_attrs bb always dc c time =
1564 let ob s a b =
1565 if always || a != b
1566 then Printf.bprintf bb "\n %s='%b'" s a
1567 and op s a b =
1568 if always || a <> b
1569 then Printf.bprintf bb "\n %s='%b'" s (a != None)
1570 and oi s a b =
1571 if always || a != b
1572 then Printf.bprintf bb "\n %s='%d'" s a
1573 and oI s a b =
1574 if always || a != b
1575 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
1576 and oz s a b =
1577 if always || a <> b
1578 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
1579 and oF s a b =
1580 if always || a <> b
1581 then Printf.bprintf bb "\n %s='%f'" s a
1582 and oc s a b =
1583 if always || a <> b
1584 then
1585 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
1586 and oC s a b =
1587 if always || a <> b
1588 then
1589 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
1590 and oR s a b =
1591 if always || a <> b
1592 then
1593 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
1594 and os s a b =
1595 if always || a <> b
1596 then
1597 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
1598 and og s a b =
1599 if always || a <> b
1600 then
1601 match a with
1602 | Some (_N, _A, _B) ->
1603 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
1604 | None ->
1605 match b with
1606 | None -> ()
1607 | _ ->
1608 Printf.bprintf bb "\n %s='none'" s
1609 and oW s a b =
1610 if always || a <> b
1611 then
1612 let v =
1613 match a with
1614 | None -> "false"
1615 | Some f ->
1616 if f = infinity
1617 then "true"
1618 else string_of_float f
1620 Printf.bprintf bb "\n %s='%s'" s v
1621 and oco s a b =
1622 if always || a <> b
1623 then
1624 match a with
1625 | Cmulti ((n, a, b), _) when n > 1 ->
1626 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
1627 | Csplit (n, _) when n > 1 ->
1628 Printf.bprintf bb "\n %s='%d'" s ~-n
1629 | Cmulti _ | Csplit _ | Csingle _ -> ()
1630 and obeco s a b =
1631 if always || a <> b
1632 then
1633 match a with
1634 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
1635 | _ -> ()
1636 and oFm s a b =
1637 if always || a <> b
1638 then
1639 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
1640 and oSv s a b m =
1641 if always || a <> b
1642 then
1643 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
1644 and oPm s a b =
1645 if always || a <> b
1646 then
1647 Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
1649 oi "width" c.cwinw dc.cwinw;
1650 oi "height" c.cwinh dc.cwinh;
1651 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
1652 oi "scroll-handle-height" c.scrollh dc.scrollh;
1653 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
1654 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
1655 ob "case-insensitive-search" c.icase dc.icase;
1656 ob "preload" c.preload dc.preload;
1657 oi "page-bias" c.pagebias dc.pagebias;
1658 oi "scroll-step" c.scrollstep dc.scrollstep;
1659 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
1660 ob "max-height-fit" c.maxhfit dc.maxhfit;
1661 ob "crop-hack" c.crophack dc.crophack;
1662 oW "throttle" c.maxwait dc.maxwait;
1663 ob "highlight-links" c.hlinks dc.hlinks;
1664 ob "under-cursor-info" c.underinfo dc.underinfo;
1665 oi "vertical-margin" c.interpagespace dc.interpagespace;
1666 oz "zoom" c.zoom dc.zoom;
1667 ob "presentation" c.presentation dc.presentation;
1668 oi "rotation-angle" c.angle dc.angle;
1669 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
1670 oFm "fit-model" c.fitmodel dc.fitmodel;
1671 oI "pixmap-cache-size" c.memlimit dc.memlimit;
1672 oi "tex-count" c.texcount dc.texcount;
1673 oi "slice-height" c.sliceheight dc.sliceheight;
1674 oi "thumbnail-width" c.thumbw dc.thumbw;
1675 ob "persistent-location" c.jumpback dc.jumpback;
1676 oc "background-color" c.bgcolor dc.bgcolor;
1677 oi "tile-width" c.tilew dc.tilew;
1678 oi "tile-height" c.tileh dc.tileh;
1679 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
1680 ob "checkers" c.checkers dc.checkers;
1681 oi "aalevel" c.aalevel dc.aalevel;
1682 ob "trim-margins" c.trimmargins dc.trimmargins;
1683 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
1684 os "uri-launcher" c.urilauncher dc.urilauncher;
1685 os "path-launcher" c.pathlauncher dc.pathlauncher;
1686 oC "color-space" c.colorspace dc.colorspace;
1687 ob "invert-colors" c.invert dc.invert;
1688 oF "brightness" c.colorscale dc.colorscale;
1689 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
1690 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
1691 oco "columns" c.columns dc.columns;
1692 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
1693 os "selection-command" c.selcmd dc.selcmd;
1694 os "synctex-command" c.stcmd dc.stcmd;
1695 os "pax-command" c.paxcmd dc.paxcmd;
1696 os "askpass-command" c.passcmd dc.passcmd;
1697 os "savepath-command" c.savecmd dc.savecmd;
1698 ob "update-cursor" c.updatecurs dc.updatecurs;
1699 oi "hint-font-size" c.hfsize dc.hfsize;
1700 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
1701 oF "page-scroll-scale" c.pgscale dc.pgscale;
1702 ob "use-pbo" c.usepbo dc.usepbo;
1703 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
1704 ob "remote-in-a-new-instance" c.riani dc.riani;
1705 op "point-and-x" c.pax dc.pax;
1706 oPm "point-and-x-mark" c.paxmark dc.paxmark;
1707 ob "scroll-bar-on-the-left" c.leftscroll dc.leftscroll;
1708 if not always
1709 then os "title" c.title dc.title;
1710 oF "last-visit" (snd (modf time)) 0.0;
1713 let keymapsbuf always dc c =
1714 let bb = Buffer.create 16 in
1715 let rec loop = function
1716 | [] -> ()
1717 | (modename, h) :: rest ->
1718 let dh = findkeyhash dc modename in
1719 if always || h <> dh
1720 then (
1721 if Hashtbl.length h > 0
1722 then (
1723 if Buffer.length bb > 0
1724 then Buffer.add_char bb '\n';
1725 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
1726 Hashtbl.iter (fun i o ->
1727 let isdifferent = always ||
1729 let dO = Hashtbl.find dh i in
1730 dO <> o
1731 with Not_found -> true
1733 if isdifferent
1734 then
1735 let addkm (k, m) =
1736 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
1737 if Wsi.withalt m then Buffer.add_string bb "alt-";
1738 if Wsi.withshift m then Buffer.add_string bb "shift-";
1739 if Wsi.withmeta m then Buffer.add_string bb "meta-";
1740 Buffer.add_string bb (Wsi.keyname k);
1742 let addkms l =
1743 let rec loop = function
1744 | [] -> ()
1745 | km :: [] -> addkm km
1746 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
1748 loop l
1750 Buffer.add_string bb "<map in='";
1751 addkm i;
1752 match o with
1753 | KMinsrt km ->
1754 Buffer.add_string bb "' out='";
1755 addkm km;
1756 Buffer.add_string bb "'/>\n"
1758 | KMinsrl kms ->
1759 Buffer.add_string bb "' out='";
1760 addkms kms;
1761 Buffer.add_string bb "'/>\n"
1763 | KMmulti (ins, kms) ->
1764 Buffer.add_char bb ' ';
1765 addkms ins;
1766 Buffer.add_string bb "' out='";
1767 addkms kms;
1768 Buffer.add_string bb "'/>\n"
1769 ) h;
1770 Buffer.add_string bb "</keymap>";
1773 loop rest
1775 loop c.keyhashes;
1779 let save1 bb leavebirdseye x h dc =
1780 let uifontsize = fstate.fontsize in
1781 let dc = if conf.bedefault then conf else dc in
1782 Buffer.add_string bb "<llppconfig>\n";
1784 if nonemptystr !fontpath
1785 then
1786 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
1787 uifontsize
1788 !fontpath
1789 else (
1790 if uifontsize <> 14
1791 then
1792 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
1795 Buffer.add_string bb "<defaults";
1796 add_attrs bb true dc dc nan;
1797 let kb = keymapsbuf true dc dc in
1798 if Buffer.length kb > 0
1799 then (
1800 Buffer.add_string bb ">\n";
1801 Buffer.add_buffer bb kb;
1802 Buffer.add_string bb "\n</defaults>\n";
1804 else Buffer.add_string bb "/>\n";
1806 let adddoc path pan anchor c bookmarks time =
1807 if bookmarks == [] && c = dc && anchor = emptyanchor
1808 then ()
1809 else (
1810 Printf.bprintf bb "<doc path='%s'"
1811 (enent path 0 (String.length path));
1813 if anchor <> emptyanchor
1814 then (
1815 let n, rely, visy = anchor in
1816 Printf.bprintf bb "\n page='%d'" n;
1817 if rely > 1e-6
1818 then
1819 Printf.bprintf bb " rely='%f'" rely
1821 if abs_float visy > 1e-6
1822 then
1823 Printf.bprintf bb " visy='%f'" visy
1827 if pan != 0
1828 then Printf.bprintf bb " pan='%d'" pan;
1830 add_attrs bb false dc c time;
1831 let kb = keymapsbuf false dc c in
1833 begin match bookmarks with
1834 | [] ->
1835 if Buffer.length kb > 0
1836 then (
1837 Buffer.add_string bb ">\n";
1838 Buffer.add_buffer bb kb;
1839 Buffer.add_string bb "\n</doc>\n";
1841 else Buffer.add_string bb "/>\n"
1842 | _ ->
1843 Buffer.add_string bb ">\n<bookmarks>\n";
1844 List.iter (fun (title, _, kind) ->
1845 begin match kind with
1846 | Oanchor (page, rely, visy) ->
1847 Printf.bprintf bb
1848 "<item title='%s' page='%d'"
1849 (enent title 0 (String.length title))
1850 page
1852 if rely > 1e-6
1853 then
1854 Printf.bprintf bb " rely='%f'" rely
1856 if abs_float visy > 1e-6
1857 then
1858 Printf.bprintf bb " visy='%f'" visy
1860 | Ohistory _ | Onone | Ouri _ | Oremote _
1861 | Oremotedest _ | Olaunch _ | Oaction _ ->
1862 failwith "unexpected link in bookmarks"
1863 end;
1864 Buffer.add_string bb "/>\n";
1865 ) bookmarks;
1866 Buffer.add_string bb "</bookmarks>";
1867 if Buffer.length kb > 0
1868 then (
1869 Buffer.add_string bb "\n";
1870 Buffer.add_buffer bb kb;
1872 Buffer.add_string bb "\n</doc>\n";
1873 end;
1877 let pan, conf =
1878 match state.mode with
1879 | Birdseye (c, pan, _, _, _) ->
1880 let beyecolumns =
1881 match conf.columns with
1882 | Cmulti ((c, _, _), _) -> Some c
1883 | Csingle _ -> None
1884 | Csplit _ -> None
1885 and columns =
1886 match c.columns with
1887 | Cmulti (c, _) -> Cmulti (c, E.a)
1888 | Csingle _ -> Csingle E.a
1889 | Csplit _ -> failwith "quit from bird's eye while split"
1891 pan, { c with beyecolumns = beyecolumns; columns = columns }
1892 | Textentry _
1893 | View
1894 | LinkNav _ -> x, conf
1896 let docpath = abspath state.path in
1897 adddoc docpath pan (getanchor ())
1899 let autoscrollstep =
1900 match state.autoscroll with
1901 | Some step -> step
1902 | None -> conf.autoscrollstep
1904 begin match state.mode with
1905 | Birdseye beye -> leavebirdseye beye true
1906 | Textentry _
1907 | View
1908 | LinkNav _ -> ()
1909 end;
1910 { conf with autoscrollstep = autoscrollstep }
1912 (if conf.savebmarks then state.bookmarks else [])
1913 (now ());
1915 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
1916 if docpath <> abspath path
1917 then adddoc path x anchor c bookmarks c.lastvisit
1918 ) h;
1919 Buffer.add_string bb "</llppconfig>\n";
1920 true;
1923 let save leavebirdseye =
1924 let relx = float state.x /. float state.winw in
1925 let w, h, x =
1926 let cx w = truncate (relx *. float w) in
1927 List.fold_left
1928 (fun (w, h, x) ws ->
1929 match ws with
1930 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
1931 | Wsi.MaxVert -> (w, conf.cwinh, x)
1932 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
1934 (state.winw, state.winh, state.x) state.winstate
1936 conf.cwinw <- w;
1937 conf.cwinh <- h;
1938 let bb = Buffer.create 32768 in
1939 let save2 (h, dc) =
1940 save1 bb leavebirdseye x h dc
1942 if load1 save2 && Buffer.length bb > 0
1943 then
1945 let tmp = !confpath ^ ".tmp" in
1946 let oc = open_out_bin tmp in
1947 Buffer.output_buffer oc bb;
1948 close_out oc;
1949 Unix.rename tmp !confpath;
1950 with exn ->
1951 prerr_endline
1952 ("error while saving configuration: " ^ exntos exn)
1955 let gc fdi fdo =
1956 let wr s n =
1957 let n' = Unix.write fdo (Bytes.of_string s) 0 n in
1958 if n != n'
1959 then Utils.error "Unix.write %d = %d" n n'
1961 let rd s n =
1962 try Unix.read fdi s 0 n
1963 with exn -> Utils.error "reading gc pipe %s" (exntos exn) in
1964 let href = ref (Hashtbl.create 0) in
1965 let cref = ref defconf in
1966 let push (h, dc) =
1967 let f path (pc, _pb, _px, _pa) =
1968 let s =
1969 Printf.sprintf "%s\000%ld\000" path (Int32.of_float pc.lastvisit)
1971 wr s (String.length s)
1973 Hashtbl.iter f h;
1974 href := h;
1975 cref := dc;
1976 Unix.shutdown fdo Unix.SHUTDOWN_SEND;
1977 true
1979 ignore (load1 push);
1980 let s =
1981 let b = Buffer.create 32768 in
1982 let s = Bytes.create 4096 in
1983 let rec f () =
1984 let n = rd s (Bytes.length s) in
1985 if n = 0
1986 then Buffer.contents b
1987 else (Buffer.add_subbytes b s 0 n; f ())
1989 f ()
1991 let rec f ppos =
1992 match String.index_from s ppos '\000' with
1993 | zpos1 ->
1994 let zpos2 =
1995 try String.index_from s (zpos1+1) '\000' with Not_found -> -1 in
1996 if zpos2 = -1
1997 then error "Incorrect gc input in (%S) at %d" s zpos1
1998 else
1999 let okey = StringLabels.sub s ~pos:ppos ~len:(zpos1-ppos) in
2000 let nkey = StringLabels.sub s ~pos:(zpos1+1) ~len:(zpos2-zpos1-1) in
2001 if emptystr nkey
2002 then (Hashtbl.remove !href okey; f (zpos2+1))
2003 else
2004 let v =
2005 try Hashtbl.find !href okey
2006 with Not_found -> Utils.error "gc: could not find %S" okey
2008 Hashtbl.remove !href okey;
2009 Hashtbl.replace !href nkey v;
2010 f (zpos2+1)
2011 | exception Not_found -> ()
2013 f 0;
2014 let bb = Buffer.create 32768 in
2015 let save2 (_h, dc) = save1 bb (fun _ _ -> ()) 0 !href dc in
2016 if load1 save2 && Buffer.length bb > 0
2017 then (
2019 let tmp = !confpath ^ ".tmp" in
2020 let oc = open_out_bin tmp in
2021 Buffer.output_buffer oc bb;
2022 close_out oc;
2023 Unix.rename tmp !confpath;
2024 with exn ->
2025 prerr_endline
2026 ("error while saving configuration: " ^ exntos exn)
2030 let logcurrently = function
2031 | Idle -> dolog "Idle"
2032 | Loading (l, gen) ->
2033 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2034 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2035 dolog
2036 "Tiling %d[%d,%d] page=%s cs=%s angle"
2037 l.pageno col row (~> pageopaque)
2038 (CSTE.to_string colorspace)
2040 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2041 angle gen conf.angle state.gen
2042 tilew tileh
2043 conf.tilew conf.tileh
2045 | Outlining _ ->
2046 dolog "outlining"