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