Less incorrect patch
[llpp.git] / config.ml
blob2a9efb286727cda5fa58c3dbd5666363bbc9eb56
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 =
1459 let xdgconfdir = Utils.getenvwithdef "XDG_CONFIG_HOME" E.s in
1460 if xdgconfdir == E.s
1461 then
1463 let dir = Filename.concat home ".config" in
1464 if Sys.is_directory dir then dir else home
1465 with _ -> home
1466 else xdgconfdir
1468 Filename.concat dir "llpp.conf"
1471 let confpath = ref defconfpath;;
1473 let load1 f =
1474 if Sys.file_exists !confpath
1475 then
1476 match
1477 (try Some (open_in_bin !confpath)
1478 with exn ->
1479 prerr_endline
1480 ("Error opening configuration file `" ^ !confpath ^ "': " ^
1481 exntos exn);
1482 None
1484 with
1485 | Some ic ->
1486 let success =
1488 f (do_load get ic)
1489 with exn ->
1490 prerr_endline
1491 ("Error loading configuration from `" ^ !confpath ^ "': " ^
1492 exntos exn);
1493 false
1495 close_in ic;
1496 success
1498 | None -> false
1499 else
1500 f (Hashtbl.create 0, defconf)
1503 let load openlast =
1504 let f (h, dc) =
1505 if openlast
1506 then (
1507 let path, _ =
1508 Hashtbl.fold
1509 (fun path (conf, _, _, _) ((_, besttime) as best) ->
1510 if conf.lastvisit > besttime
1511 then (path, conf.lastvisit)
1512 else best)
1514 (state.path, -.infinity)
1516 state.path <- path;
1518 let pc, pb, px, pa =
1520 let path =
1521 if emptystr state.origin
1522 then state.path
1523 else state.origin
1525 let absname = abspath path in
1526 Hashtbl.find h absname
1527 with Not_found -> dc, [], 0, emptyanchor
1529 setconf defconf dc;
1530 setconf conf pc;
1531 state.bookmarks <- pb;
1532 state.x <- px;
1533 if conf.jumpback
1534 then state.anchor <- pa;
1535 cbput state.hists.nav pa;
1536 true
1538 load1 f
1541 let gethist listref =
1542 let f (h, _) =
1543 listref :=
1544 Hashtbl.fold (fun path (pc, pb, px, pa) accu ->
1545 (path, pc, pb, px, pa) :: accu)
1546 h [];
1547 true
1549 load1 f
1552 let add_attrs bb always dc c time =
1553 let ob s a b =
1554 if always || a != b
1555 then Printf.bprintf bb "\n %s='%b'" s a
1556 and op s a b =
1557 if always || a <> b
1558 then Printf.bprintf bb "\n %s='%b'" s (a != None)
1559 and oi s a b =
1560 if always || a != b
1561 then Printf.bprintf bb "\n %s='%d'" s a
1562 and oI s a b =
1563 if always || a != b
1564 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
1565 and oz s a b =
1566 if always || a <> b
1567 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
1568 and oF s a b =
1569 if always || a <> b
1570 then Printf.bprintf bb "\n %s='%F'" s a
1571 and oc s a b =
1572 if always || a <> b
1573 then
1574 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
1575 and oC s a b =
1576 if always || a <> b
1577 then
1578 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
1579 and oR s a b =
1580 if always || a <> b
1581 then
1582 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
1583 and os s a b =
1584 if always || a <> b
1585 then
1586 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
1587 and og s a b =
1588 if always || a <> b
1589 then
1590 match a with
1591 | Some (_N, _A, _B) ->
1592 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
1593 | None ->
1594 match b with
1595 | None -> ()
1596 | _ ->
1597 Printf.bprintf bb "\n %s='none'" s
1598 and oW s a b =
1599 if always || a <> b
1600 then
1601 let v =
1602 match a with
1603 | None -> "false"
1604 | Some f ->
1605 if f = infinity
1606 then "true"
1607 else string_of_float f
1609 Printf.bprintf bb "\n %s='%s'" s v
1610 and oco s a b =
1611 if always || a <> b
1612 then
1613 match a with
1614 | Cmulti ((n, a, b), _) when n > 1 ->
1615 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
1616 | Csplit (n, _) when n > 1 ->
1617 Printf.bprintf bb "\n %s='%d'" s ~-n
1618 | Cmulti _ | Csplit _ | Csingle _ -> ()
1619 and obeco s a b =
1620 if always || a <> b
1621 then
1622 match a with
1623 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
1624 | _ -> ()
1625 and oFm s a b =
1626 if always || a <> b
1627 then
1628 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
1629 and oSv s a b m =
1630 if always || a <> b
1631 then
1632 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
1633 and oPm s a b =
1634 if always || a <> b
1635 then
1636 Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
1638 oi "width" c.cwinw dc.cwinw;
1639 oi "height" c.cwinh dc.cwinh;
1640 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
1641 oi "scroll-handle-height" c.scrollh dc.scrollh;
1642 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
1643 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
1644 ob "case-insensitive-search" c.icase dc.icase;
1645 ob "preload" c.preload dc.preload;
1646 oi "page-bias" c.pagebias dc.pagebias;
1647 oi "scroll-step" c.scrollstep dc.scrollstep;
1648 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
1649 ob "max-height-fit" c.maxhfit dc.maxhfit;
1650 ob "crop-hack" c.crophack dc.crophack;
1651 oW "throttle" c.maxwait dc.maxwait;
1652 ob "highlight-links" c.hlinks dc.hlinks;
1653 ob "under-cursor-info" c.underinfo dc.underinfo;
1654 oi "vertical-margin" c.interpagespace dc.interpagespace;
1655 oz "zoom" c.zoom dc.zoom;
1656 ob "presentation" c.presentation dc.presentation;
1657 oi "rotation-angle" c.angle dc.angle;
1658 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
1659 oFm "fit-model" c.fitmodel dc.fitmodel;
1660 oI "pixmap-cache-size" c.memlimit dc.memlimit;
1661 oi "tex-count" c.texcount dc.texcount;
1662 oi "slice-height" c.sliceheight dc.sliceheight;
1663 oi "thumbnail-width" c.thumbw dc.thumbw;
1664 ob "persistent-location" c.jumpback dc.jumpback;
1665 oc "background-color" c.bgcolor dc.bgcolor;
1666 oi "tile-width" c.tilew dc.tilew;
1667 oi "tile-height" c.tileh dc.tileh;
1668 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
1669 ob "checkers" c.checkers dc.checkers;
1670 oi "aalevel" c.aalevel dc.aalevel;
1671 ob "trim-margins" c.trimmargins dc.trimmargins;
1672 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
1673 os "uri-launcher" c.urilauncher dc.urilauncher;
1674 os "path-launcher" c.pathlauncher dc.pathlauncher;
1675 oC "color-space" c.colorspace dc.colorspace;
1676 ob "invert-colors" c.invert dc.invert;
1677 oF "brightness" c.colorscale dc.colorscale;
1678 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
1679 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
1680 oco "columns" c.columns dc.columns;
1681 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
1682 os "selection-command" c.selcmd dc.selcmd;
1683 os "synctex-command" c.stcmd dc.stcmd;
1684 os "pax-command" c.paxcmd dc.paxcmd;
1685 ob "update-cursor" c.updatecurs dc.updatecurs;
1686 oi "hint-font-size" c.hfsize dc.hfsize;
1687 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
1688 oF "page-scroll-scale" c.pgscale dc.pgscale;
1689 ob "use-pbo" c.usepbo dc.usepbo;
1690 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
1691 ob "remote-in-a-new-instance" c.riani dc.riani;
1692 op "point-and-x" c.pax dc.pax;
1693 oPm "point-and-x-mark" c.paxmark dc.paxmark;
1694 ob "scroll-bar-on-the-left" c.leftscroll dc.leftscroll;
1695 if not always
1696 then os "title" c.title dc.title;
1697 oF "last-visit" (snd (modf time)) 0.0;
1700 let keymapsbuf always dc c =
1701 let bb = Buffer.create 16 in
1702 let rec loop = function
1703 | [] -> ()
1704 | (modename, h) :: rest ->
1705 let dh = findkeyhash dc modename in
1706 if always || h <> dh
1707 then (
1708 if Hashtbl.length h > 0
1709 then (
1710 if Buffer.length bb > 0
1711 then Buffer.add_char bb '\n';
1712 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
1713 Hashtbl.iter (fun i o ->
1714 let isdifferent = always ||
1716 let dO = Hashtbl.find dh i in
1717 dO <> o
1718 with Not_found -> true
1720 if isdifferent
1721 then
1722 let addkm (k, m) =
1723 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
1724 if Wsi.withalt m then Buffer.add_string bb "alt-";
1725 if Wsi.withshift m then Buffer.add_string bb "shift-";
1726 if Wsi.withmeta m then Buffer.add_string bb "meta-";
1727 Buffer.add_string bb (Wsi.keyname k);
1729 let addkms l =
1730 let rec loop = function
1731 | [] -> ()
1732 | km :: [] -> addkm km
1733 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
1735 loop l
1737 Buffer.add_string bb "<map in='";
1738 addkm i;
1739 match o with
1740 | KMinsrt km ->
1741 Buffer.add_string bb "' out='";
1742 addkm km;
1743 Buffer.add_string bb "'/>\n"
1745 | KMinsrl kms ->
1746 Buffer.add_string bb "' out='";
1747 addkms kms;
1748 Buffer.add_string bb "'/>\n"
1750 | KMmulti (ins, kms) ->
1751 Buffer.add_char bb ' ';
1752 addkms ins;
1753 Buffer.add_string bb "' out='";
1754 addkms kms;
1755 Buffer.add_string bb "'/>\n"
1756 ) h;
1757 Buffer.add_string bb "</keymap>";
1760 loop rest
1762 loop c.keyhashes;
1766 let save1 bb leavebirdseye x h dc =
1767 let uifontsize = fstate.fontsize in
1768 let dc = if conf.bedefault then conf else dc in
1769 Buffer.add_string bb "<llppconfig>\n";
1771 if nonemptystr !fontpath
1772 then
1773 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
1774 uifontsize
1775 !fontpath
1776 else (
1777 if uifontsize <> 14
1778 then
1779 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
1782 Buffer.add_string bb "<defaults";
1783 add_attrs bb true dc dc nan;
1784 let kb = keymapsbuf true dc dc in
1785 if Buffer.length kb > 0
1786 then (
1787 Buffer.add_string bb ">\n";
1788 Buffer.add_buffer bb kb;
1789 Buffer.add_string bb "\n</defaults>\n";
1791 else Buffer.add_string bb "/>\n";
1793 let adddoc path pan anchor c bookmarks time =
1794 if bookmarks == [] && c = dc && anchor = emptyanchor
1795 then ()
1796 else (
1797 Printf.bprintf bb "<doc path='%s'"
1798 (enent path 0 (String.length path));
1800 if anchor <> emptyanchor
1801 then (
1802 let n, rely, visy = anchor in
1803 Printf.bprintf bb "\n page='%d'" n;
1804 if rely > 1e-6
1805 then
1806 Printf.bprintf bb " rely='%f'" rely
1808 if abs_float visy > 1e-6
1809 then
1810 Printf.bprintf bb " visy='%f'" visy
1814 if pan != 0
1815 then Printf.bprintf bb " pan='%d'" pan;
1817 add_attrs bb false dc c time;
1818 let kb = keymapsbuf false dc c in
1820 begin match bookmarks with
1821 | [] ->
1822 if Buffer.length kb > 0
1823 then (
1824 Buffer.add_string bb ">\n";
1825 Buffer.add_buffer bb kb;
1826 Buffer.add_string bb "\n</doc>\n";
1828 else Buffer.add_string bb "/>\n"
1829 | _ ->
1830 Buffer.add_string bb ">\n<bookmarks>\n";
1831 List.iter (fun (title, _, kind) ->
1832 begin match kind with
1833 | Oanchor (page, rely, visy) ->
1834 Printf.bprintf bb
1835 "<item title='%s' page='%d'"
1836 (enent title 0 (String.length title))
1837 page
1839 if rely > 1e-6
1840 then
1841 Printf.bprintf bb " rely='%f'" rely
1843 if abs_float visy > 1e-6
1844 then
1845 Printf.bprintf bb " visy='%f'" visy
1847 | Ohistory _ | Onone | Ouri _ | Oremote _
1848 | Oremotedest _ | Olaunch _ | Oaction _ ->
1849 failwith "unexpected link in bookmarks"
1850 end;
1851 Buffer.add_string bb "/>\n";
1852 ) bookmarks;
1853 Buffer.add_string bb "</bookmarks>";
1854 if Buffer.length kb > 0
1855 then (
1856 Buffer.add_string bb "\n";
1857 Buffer.add_buffer bb kb;
1859 Buffer.add_string bb "\n</doc>\n";
1860 end;
1864 let pan, conf =
1865 match state.mode with
1866 | Birdseye (c, pan, _, _, _) ->
1867 let beyecolumns =
1868 match conf.columns with
1869 | Cmulti ((c, _, _), _) -> Some c
1870 | Csingle _ -> None
1871 | Csplit _ -> None
1872 and columns =
1873 match c.columns with
1874 | Cmulti (c, _) -> Cmulti (c, E.a)
1875 | Csingle _ -> Csingle E.a
1876 | Csplit _ -> failwith "quit from bird's eye while split"
1878 pan, { c with beyecolumns = beyecolumns; columns = columns }
1879 | Textentry _
1880 | View
1881 | LinkNav _ -> x, conf
1883 let docpath = abspath state.path in
1884 adddoc docpath pan (getanchor ())
1886 let autoscrollstep =
1887 match state.autoscroll with
1888 | Some step -> step
1889 | None -> conf.autoscrollstep
1891 begin match state.mode with
1892 | Birdseye beye -> leavebirdseye beye true
1893 | Textentry _
1894 | View
1895 | LinkNav _ -> ()
1896 end;
1897 { conf with autoscrollstep = autoscrollstep }
1899 (if conf.savebmarks then state.bookmarks else [])
1900 (now ());
1902 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
1903 if docpath <> abspath path
1904 then adddoc path x anchor c bookmarks c.lastvisit
1905 ) h;
1906 Buffer.add_string bb "</llppconfig>\n";
1907 true;
1910 let save leavebirdseye =
1911 let relx = float state.x /. float state.winw in
1912 let w, h, x =
1913 let cx w = truncate (relx *. float w) in
1914 List.fold_left
1915 (fun (w, h, x) ws ->
1916 match ws with
1917 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
1918 | Wsi.MaxVert -> (w, conf.cwinh, x)
1919 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
1921 (state.winw, state.winh, state.x) state.winstate
1923 conf.cwinw <- w;
1924 conf.cwinh <- h;
1925 let bb = Buffer.create 32768 in
1926 let save2 (h, dc) =
1927 save1 bb leavebirdseye x h dc
1929 if load1 save2 && Buffer.length bb > 0
1930 then
1932 let tmp = !confpath ^ ".tmp" in
1933 let oc = open_out_bin tmp in
1934 Buffer.output_buffer oc bb;
1935 close_out oc;
1936 Unix.rename tmp !confpath;
1937 with exn ->
1938 prerr_endline
1939 ("error while saving configuration: " ^ exntos exn)
1942 let gc fdi fdo =
1943 let wr s n =
1944 let n' = Unix.write fdo s 0 n in
1945 if n != n'
1946 then Utils.error "Unix.write %d = %d" n n'
1948 let rd s n =
1949 try Unix.read fdi s 0 n
1950 with exn -> Utils.error "reading gc pipe %s" (exntos exn) in
1951 let href = ref (Hashtbl.create 0) in
1952 let cref = ref defconf in
1953 let push (h, dc) =
1954 let f path (pc, _pb, _px, _pa) =
1955 let s =
1956 Printf.sprintf "%s\000%ld\000" path (Int32.of_float pc.lastvisit)
1958 wr s (String.length s)
1960 Hashtbl.iter f h;
1961 href := h;
1962 cref := dc;
1963 Unix.shutdown fdo Unix.SHUTDOWN_SEND;
1964 true
1966 ignore (load1 push);
1967 let s =
1968 let b = Buffer.create 32768 in
1969 let s = String.create 4096 in
1970 let rec f () =
1971 let n = rd s (String.length s) in
1972 if n = 0
1973 then Buffer.contents b
1974 else (Buffer.add_substring b s 0 n; f ())
1976 f ()
1978 let rec f ppos =
1979 let zpos1 = try String.index_from s ppos '\000' with Not_found -> -1 in
1980 if zpos1 = -1 then ()
1981 else (
1982 let zpos2 =
1983 try String.index_from s (zpos1+1) '\000' with Not_found -> -1 in
1984 if zpos2 = -1 then failwith "moo2"
1985 else (
1986 let okey = StringLabels.sub s ~pos:ppos ~len:(zpos1-ppos) in
1987 let nkey = StringLabels.sub s ~pos:(zpos1+1) ~len:(zpos2-zpos1-1) in
1988 if emptystr nkey
1989 then (Hashtbl.remove !href okey; f (zpos2+1))
1990 else
1991 let v =
1992 try Hashtbl.find !href okey
1993 with Not_found -> Utils.error "gc: could not find %S" okey
1995 Hashtbl.remove !href okey;
1996 Hashtbl.replace !href nkey v;
1997 f (zpos2+1)
2001 f 0;
2002 let bb = Buffer.create 32768 in
2003 let save2 (_h, dc) = save1 bb (fun _ _ -> ()) 0 !href dc in
2004 if load1 save2 && Buffer.length bb > 0
2005 then (
2007 let tmp = !confpath ^ ".tmp" in
2008 let oc = open_out_bin tmp in
2009 Buffer.output_buffer oc bb;
2010 close_out oc;
2011 Unix.rename tmp !confpath;
2012 with exn ->
2013 prerr_endline
2014 ("error while saving configuration: " ^ exntos exn)