Use filt
[llpp.git] / config.ml
blobda3f9f1555e06d14adebc04192fdc7704414a387
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 sr : Unix.file_descr
375 ; mutable sw : Unix.file_descr
376 ; mutable wsfd : Unix.file_descr
377 ; mutable errfd : Unix.file_descr option
378 ; mutable stderr : Unix.file_descr
379 ; mutable errmsgs : Buffer.t
380 ; mutable newerrmsgs : bool
381 ; mutable w : int
382 ; mutable x : int
383 ; mutable y : int
384 ; mutable anchor : anchor
385 ; mutable ranchors : (string * string * anchor * string) list
386 ; mutable maxy : int
387 ; mutable layout : page list
388 ; pagemap : (pagemapkey, opaque) Hashtbl.t
389 ; tilemap : (tilemapkey, tile) Hashtbl.t
390 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
391 ; mutable pdims : (pageno * width * height * leftx) list
392 ; mutable pagecount : int
393 ; mutable currently : currently
394 ; mutable mstate : mstate
395 ; mutable searchpattern : string
396 ; mutable rects : (pageno * recttype * rect) list
397 ; mutable rects1 : (pageno * recttype * rect) list
398 ; mutable text : string
399 ; mutable winstate : Wsi.winstate list
400 ; mutable mode : mode
401 ; mutable uioh : uioh
402 ; mutable outlines : outline array
403 ; mutable bookmarks : outline list
404 ; mutable path : string
405 ; mutable password : string
406 ; mutable nameddest : string
407 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
408 ; mutable memused : memsize
409 ; mutable gen : gen
410 ; mutable throttle : (page list * int * float) option
411 ; mutable autoscroll : int option
412 ; mutable ghyll : (int option -> unit)
413 ; mutable help : helpitem array
414 ; mutable docinfo : (int * string) list
415 ; mutable checkerstexid : GlTex.texture_id option
416 ; hists : hists
417 ; mutable prevzoom : (float * int)
418 ; mutable progress : float
419 ; mutable redisplay : bool
420 ; mutable mpos : mpos
421 ; mutable keystate : keystate
422 ; mutable glinks : bool
423 ; mutable prevcolumns : (columns * float) option
424 ; mutable winw : int
425 ; mutable winh : int
426 ; mutable reprf : (unit -> unit)
427 ; mutable origin : string
428 ; mutable roam : (unit -> unit)
429 ; mutable bzoom : bool
430 ; mutable traw : [`float] Raw.t
431 ; mutable vraw : [`float] Raw.t
433 and hists =
434 { pat : string circbuf
435 ; pag : string circbuf
436 ; nav : anchor circbuf
437 ; sel : string circbuf
441 let emptyanchor = (0, 0.0, 0.0);;
442 let emptykeyhash = Hashtbl.create 0;;
443 let firstgeomcmds = E.s, [];;
444 let noghyll _ = ();;
445 let noreprf () = ();;
446 let noroam () = ();;
448 let nouioh : uioh = object (self)
449 method display = ()
450 method key _ _ = self
451 method multiclick _ _ _ _ = self
452 method button _ _ _ _ _ = self
453 method motion _ _ = self
454 method pmotion _ _ = self
455 method infochanged _ = ()
456 method scrollpw = (0, nan, nan)
457 method scrollph = (0, nan, nan)
458 method modehash = emptykeyhash
459 method eformsgs = false
460 end;;
462 let platform_to_string = function
463 | Punknown -> "unknown"
464 | Plinux -> "Linux"
465 | Posx -> "OSX"
466 | Psun -> "Sun"
467 | Pfreebsd -> "FreeBSD"
468 | Pdragonflybsd -> "DragonflyBSD"
469 | Popenbsd -> "OpenBSD"
470 | Pnetbsd -> "NetBSD"
471 | Pcygwin -> "Cygwin"
474 let version () =
475 Printf.sprintf "llpp version %s, fitz %s, ocaml %s (%s/%dbit)"
476 Help.version (fz_version ()) Sys.ocaml_version
477 (platform_to_string platform) Sys.word_size
480 let geturl s =
481 let colonpos = try String.index s ':' with Not_found -> -1 in
482 let len = String.length s in
483 if colonpos >= 0 && colonpos + 3 < len
484 then (
485 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
486 then
487 let schemestartpos =
488 try String.rindex_from s colonpos ' '
489 with Not_found -> -1
491 let scheme =
492 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
494 match scheme with
495 | "http" | "ftp" | "mailto" ->
496 let epos =
497 try String.index_from s colonpos ' '
498 with Not_found -> len
500 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
501 | _ -> E.s
502 else E.s
504 else E.s
507 let defconf =
508 { scrollbw = 7
509 ; scrollh = 12
510 ; scrollb = scrollbhv lor scrollbvv
511 ; icase = true
512 ; preload = true
513 ; pagebias = 0
514 ; verbose = false
515 ; debug = false
516 ; scrollstep = 24
517 ; hscrollstep = 24
518 ; maxhfit = true
519 ; crophack = false
520 ; autoscrollstep = 2
521 ; maxwait = None
522 ; hlinks = false
523 ; underinfo = false
524 ; interpagespace = 2
525 ; zoom = 1.0
526 ; presentation = false
527 ; angle = 0
528 ; cwinw = 900
529 ; cwinh = 900
530 ; savebmarks = true
531 ; fitmodel = FitProportional
532 ; trimmargins = false
533 ; trimfuzz = (0,0,0,0)
534 ; memlimit = 32 lsl 20
535 ; texcount = 256
536 ; sliceheight = 24
537 ; thumbw = 76
538 ; jumpback = true
539 ; bgcolor = (0.5, 0.5, 0.5)
540 ; bedefault = false
541 ; tilew = 2048
542 ; tileh = 2048
543 ; mustoresize = 256 lsl 20
544 ; checkers = true
545 ; aalevel = 8
546 ; urilauncher =
547 (match platform with
548 | Plinux | Pfreebsd | Pdragonflybsd
549 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
550 | Posx -> "open \"%s\""
551 | Pcygwin -> "cygstart \"%s\""
552 | Punknown -> "echo %s")
553 ; pathlauncher = "lp \"%s\""
554 ; selcmd =
555 (match platform with
556 | Plinux | Pfreebsd | Pdragonflybsd
557 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
558 | Posx -> "pbcopy"
559 | Pcygwin -> "wsel"
560 | Punknown -> "cat")
561 ; paxcmd = "cat"
562 ; colorspace = Rgb
563 ; invert = false
564 ; colorscale = 1.0
565 ; redirectstderr = false
566 ; ghyllscroll = None
567 ; columns = Csingle [||]
568 ; beyecolumns = None
569 ; updatecurs = false
570 ; hfsize = 12
571 ; pgscale = 1.0
572 ; usepbo = false
573 ; wheelbypage = false
574 ; stcmd = "echo SyncTex"
575 ; riani = false
576 ; pax = None
577 ; paxmark = Mark_word
578 ; leftscroll = false
579 ; title = E.s
580 ; lastvisit = 0.0
581 ; keyhashes =
582 let mk n = (n, Hashtbl.create 1) in
583 [ mk "global"
584 ; mk "info"
585 ; mk "help"
586 ; mk "outline"
587 ; mk "listview"
588 ; mk "birdseye"
589 ; mk "textentry"
590 ; mk "links"
591 ; mk "view"
596 let conf = { defconf with angle = defconf.angle };;
598 let gotouri uri =
599 if emptystr conf.urilauncher
600 then print_endline uri
601 else (
602 let url = geturl uri in
603 if emptystr url
604 then Printf.eprintf "obtained empty url from uri %S\n" uri
605 else
606 let re = Str.regexp "%s" in
607 let command = Str.global_replace re url conf.urilauncher in
608 try popen command []
609 with exn ->
610 Printf.eprintf
611 "failed to execute `%s': %s\n" command (exntos exn);
612 flush stderr;
616 let makehelp () =
617 let strings =
618 version ()
619 :: "(searching in this text works just by typing (i.e. no initial '/'))"
620 :: E.s :: Help.keys
622 Array.of_list (
623 List.map (fun s ->
624 let url = geturl s in
625 if nonemptystr url
626 then (s, 0, Action (fun u -> gotouri url; u))
627 else (s, 0, Noaction)
628 ) strings);
631 let cbnew n v =
632 { store = Array.create n v
633 ; rc = 0
634 ; wc = 0
635 ; len = 0
639 let cbcap b = Array.length b.store;;
641 let cbput b v =
642 let cap = cbcap b in
643 b.store.(b.wc) <- v;
644 b.wc <- (b.wc + 1) mod cap;
645 b.rc <- b.wc;
646 b.len <- min (b.len + 1) cap;
649 let cbempty b = b.len = 0;;
651 let cbgetg b circular dir =
652 if cbempty b
653 then b.store.(0)
654 else
655 let rc = b.rc + dir in
656 let rc =
657 if circular
658 then (
659 if rc = -1
660 then b.len-1
661 else (
662 if rc >= b.len
663 then 0
664 else rc
667 else bound rc 0 (b.len-1)
669 b.rc <- rc;
670 b.store.(rc);
673 let cbget b = cbgetg b false;;
674 let cbgetc b = cbgetg b true;;
676 let state =
677 { sr = Unix.stdin
678 ; sw = Unix.stdin
679 ; wsfd = Unix.stdin
680 ; errfd = None
681 ; stderr = Unix.stderr
682 ; errmsgs = Buffer.create 0
683 ; newerrmsgs = false
684 ; x = 0
685 ; y = 0
686 ; w = 0
687 ; anchor = emptyanchor
688 ; ranchors = []
689 ; layout = []
690 ; maxy = max_int
691 ; tilelru = Queue.create ()
692 ; pagemap = Hashtbl.create 10
693 ; tilemap = Hashtbl.create 10
694 ; pdims = []
695 ; pagecount = 0
696 ; currently = Idle
697 ; mstate = Mnone
698 ; rects = []
699 ; rects1 = []
700 ; text = E.s
701 ; mode = View
702 ; winstate = []
703 ; searchpattern = E.s
704 ; outlines = E.a
705 ; bookmarks = []
706 ; path = E.s
707 ; password = E.s
708 ; nameddest = E.s
709 ; geomcmds = firstgeomcmds
710 ; hists =
711 { nav = cbnew 10 emptyanchor
712 ; pat = cbnew 10 E.s
713 ; pag = cbnew 10 E.s
714 ; sel = cbnew 10 E.s
716 ; memused = 0
717 ; gen = 0
718 ; throttle = None
719 ; autoscroll = None
720 ; ghyll = noghyll
721 ; help = makehelp ()
722 ; docinfo = []
723 ; checkerstexid = None
724 ; prevzoom = (1.0, 0)
725 ; progress = -1.0
726 ; uioh = nouioh
727 ; redisplay = true
728 ; mpos = (-1, -1)
729 ; keystate = KSnone
730 ; glinks = false
731 ; prevcolumns = None
732 ; winw = -1
733 ; winh = -1
734 ; reprf = noreprf
735 ; origin = E.s
736 ; roam = noroam
737 ; bzoom = false
738 ; traw = Raw.create_static `float 8
739 ; vraw = Raw.create_static `float 8
743 let copykeyhashes c =
744 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
747 let calcips h =
748 let d = state.winh - h in
749 max conf.interpagespace ((d + 1) / 2)
752 let rowyh (c, coverA, coverB) b n =
753 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
754 then
755 let _, _, vy, (_, _, h, _) = b.(n) in
756 (vy, h)
757 else
758 let n' = n - coverA in
759 let d = n' mod c in
760 let s = n - d in
761 let e = min state.pagecount (s + c) in
762 let rec find m miny maxh = if m = e then miny, maxh else
763 let _, _, y, (_, _, h, _) = b.(m) in
764 let miny = min miny y in
765 let maxh = max maxh h in
766 find (m+1) miny maxh
767 in find s max_int 0
770 let page_of_y y =
771 let ((c, coverA, coverB) as cl), b =
772 match conf.columns with
773 | Csingle b -> (1, 0, 0), b
774 | Cmulti (c, b) -> c, b
775 | Csplit (_, b) -> (1, 0, 0), b
777 if Array.length b = 0
778 then -1
779 else
780 let rec bsearch nmin nmax =
781 if nmin > nmax
782 then bound nmin 0 (state.pagecount-1)
783 else
784 let n = (nmax + nmin) / 2 in
785 let vy, h = rowyh cl b n in
786 let y0, y1 =
787 if conf.presentation
788 then
789 let ips = calcips h in
790 let y0 = vy - ips in
791 let y1 = vy + h + ips in
792 y0, y1
793 else (
794 if n = 0
795 then 0, vy + h + conf.interpagespace
796 else
797 let y0 = vy - conf.interpagespace in
798 y0, y0 + h + conf.interpagespace
801 if y >= y0 && y < y1
802 then (
803 if c = 1
804 then n
805 else (
806 if n > coverA
807 then
808 if n < state.pagecount - coverB
809 then ((n-coverA)/c)*c + coverA
810 else n
811 else n
814 else (
815 if y > y0
816 then bsearch (n+1) nmax
817 else bsearch nmin (n-1)
820 bsearch 0 (state.pagecount-1);
823 let calcheight () =
824 match conf.columns with
825 | Cmulti ((_, _, _) as cl, b) ->
826 if Array.length b > 0
827 then
828 let y, h = rowyh cl b (Array.length b - 1) in
829 y + h + (if conf.presentation then calcips h else 0)
830 else 0
831 | Csingle b ->
832 if Array.length b > 0
833 then
834 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
835 y + h + (if conf.presentation then calcips h else 0)
836 else 0
837 | Csplit (_, b) ->
838 if Array.length b > 0
839 then
840 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
841 y + h
842 else 0
845 let getpageywh pageno =
846 let pageno = bound pageno 0 (state.pagecount-1) in
847 match conf.columns with
848 | Csingle b ->
849 if Array.length b = 0
850 then 0, 0, 0
851 else
852 let (_, _, y, (_, w, h, _)) = b.(pageno) in
853 let y =
854 if conf.presentation
855 then y - calcips h
856 else y
858 y, w, h
859 | Cmulti (cl, b) ->
860 if Array.length b = 0
861 then 0, 0, 0
862 else
863 let y, h = rowyh cl b pageno in
864 let (_, _, _, (_, w, _, _)) = b.(pageno) in
865 let y =
866 if conf.presentation
867 then y - calcips h
868 else y
870 y, w, h
871 | Csplit (c, b) ->
872 if Array.length b = 0
873 then 0, 0, 0
874 else
875 let n = pageno*c in
876 let (_, _, y, (_, w, h, _)) = b.(n) in
877 y, w / c, h
880 let getpageyh pageno =
881 let y,_,h = getpageywh pageno in
882 y, h;
885 let getpagedim pageno =
886 let rec f ppdim l =
887 match l with
888 | (n, _, _, _) as pdim :: rest ->
889 if n >= pageno
890 then (if n = pageno then pdim else ppdim)
891 else f pdim rest
893 | [] -> ppdim
895 f (-1, -1, -1, -1) state.pdims
898 let getpagey pageno = fst (getpageyh pageno);;
900 let getanchor1 l =
901 let top =
902 let coloff = l.pagecol * l.pageh in
903 float (l.pagey + coloff) /. float l.pageh
905 let dtop =
906 if l.pagedispy = 0
907 then
909 else (
910 if conf.presentation
911 then float l.pagedispy /. float (calcips l.pageh)
912 else float l.pagedispy /. float conf.interpagespace
915 (l.pageno, top, dtop)
918 let getanchor () =
919 match state.layout with
920 | l :: _ -> getanchor1 l
921 | [] ->
922 let n = page_of_y state.y in
923 if n = -1
924 then state.anchor
925 else
926 let y, h = getpageyh n in
927 let dy = y - state.y in
928 let dtop =
929 if conf.presentation
930 then
931 let ips = calcips h in
932 float (dy + ips) /. float ips
933 else
934 float dy /. float conf.interpagespace
936 (n, 0.0, dtop)
939 let fontpath = ref E.s;;
941 type historder = [ `lastvisit | `title | `path | `file ];;
942 let historder : historder ref = ref `lastvisit;;
944 module KeyMap =
945 Map.Make (struct type t = (int * int) let compare = compare end);;
947 open Parser;;
949 let unent s =
950 let l = String.length s in
951 let b = Buffer.create l in
952 unent b s 0 l;
953 Buffer.contents b;
956 let home =
957 try Sys.getenv "HOME"
958 with exn ->
959 prerr_endline
960 ("Can not determine home directory location: " ^ exntos exn);
964 let modifier_of_string = function
965 | "alt" -> Wsi.altmask
966 | "shift" -> Wsi.shiftmask
967 | "ctrl" | "control" -> Wsi.ctrlmask
968 | "meta" -> Wsi.metamask
969 | _ -> 0
972 let key_of_string =
973 let r = Str.regexp "-" in
974 fun s ->
975 let elems = Str.full_split r s in
976 let f n k m =
977 let g s =
978 let m1 = modifier_of_string s in
979 if m1 = 0
980 then (Wsi.namekey s, m)
981 else (k, m lor m1)
982 in function
983 | Str.Delim s when n land 1 = 0 -> g s
984 | Str.Text s -> g s
985 | Str.Delim _ -> (k, m)
987 let rec loop n k m = function
988 | [] -> (k, m)
989 | x :: xs ->
990 let k, m = f n k m x in
991 loop (n+1) k m xs
993 loop 0 0 0 elems
996 let keys_of_string =
997 let r = Str.regexp "[ \t]" in
998 fun s ->
999 let elems = Str.split r s in
1000 List.map key_of_string elems
1003 let config_of c attrs =
1004 let apply c k v =
1006 match k with
1007 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
1008 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
1009 | "case-insensitive-search" -> { c with icase = bool_of_string v }
1010 | "preload" -> { c with preload = bool_of_string v }
1011 | "page-bias" -> { c with pagebias = int_of_string v }
1012 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
1013 | "horizontal-scroll-step" ->
1014 { c with hscrollstep = max (int_of_string v) 1 }
1015 | "auto-scroll-step" ->
1016 { c with autoscrollstep = max 0 (int_of_string v) }
1017 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
1018 | "crop-hack" -> { c with crophack = bool_of_string v }
1019 | "throttle" ->
1020 let mw =
1021 match String.lowercase v with
1022 | "true" -> Some infinity
1023 | "false" -> None
1024 | f -> Some (float_of_string f)
1026 { c with maxwait = mw}
1027 | "highlight-links" -> { c with hlinks = bool_of_string v }
1028 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
1029 | "vertical-margin" ->
1030 { c with interpagespace = max 0 (int_of_string v) }
1031 | "zoom" ->
1032 let zoom = float_of_string v /. 100. in
1033 let zoom = max zoom 0.0 in
1034 { c with zoom = zoom }
1035 | "presentation" -> { c with presentation = bool_of_string v }
1036 | "rotation-angle" -> { c with angle = int_of_string v }
1037 | "width" -> { c with cwinw = max 20 (int_of_string v) }
1038 | "height" -> { c with cwinh = max 20 (int_of_string v) }
1039 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
1040 | "proportional-display" ->
1041 let fm =
1042 if bool_of_string v
1043 then FitProportional
1044 else FitWidth
1046 { c with fitmodel = fm }
1047 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
1048 | "pixmap-cache-size" ->
1049 { c with memlimit = max 2 (int_of_string_with_suffix v) }
1050 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
1051 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
1052 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
1053 | "persistent-location" -> { c with jumpback = bool_of_string v }
1054 | "background-color" -> { c with bgcolor = color_of_string v }
1055 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
1056 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
1057 | "mupdf-store-size" ->
1058 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
1059 | "checkers" -> { c with checkers = bool_of_string v }
1060 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
1061 | "trim-margins" -> { c with trimmargins = bool_of_string v }
1062 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
1063 | "uri-launcher" -> { c with urilauncher = unent v }
1064 | "path-launcher" -> { c with pathlauncher = unent v }
1065 | "color-space" -> { c with colorspace = CSTE.of_string v }
1066 | "invert-colors" -> { c with invert = bool_of_string v }
1067 | "brightness" -> { c with colorscale = float_of_string v }
1068 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
1069 | "ghyllscroll" -> { c with ghyllscroll = ghyllscroll_of_string v }
1070 | "columns" ->
1071 let (n, _, _) as nab = multicolumns_of_string v in
1072 if n < 0
1073 then { c with columns = Csplit (-n, E.a) }
1074 else { c with columns = Cmulti (nab, E.a) }
1075 | "birds-eye-columns" ->
1076 { c with beyecolumns = Some (max (int_of_string v) 2) }
1077 | "selection-command" -> { c with selcmd = unent v }
1078 | "synctex-command" -> { c with stcmd = unent v }
1079 | "pax-command" -> { c with paxcmd = unent v }
1080 | "update-cursor" -> { c with updatecurs = bool_of_string v }
1081 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
1082 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
1083 | "use-pbo" -> { c with usepbo = bool_of_string v }
1084 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
1085 | "horizontal-scrollbar-visible" ->
1086 let b =
1087 if bool_of_string v
1088 then c.scrollb lor scrollbhv
1089 else c.scrollb land (lnot scrollbhv)
1091 { c with scrollb = b }
1092 | "vertical-scrollbar-visible" ->
1093 let b =
1094 if bool_of_string v
1095 then c.scrollb lor scrollbvv
1096 else c.scrollb land (lnot scrollbvv)
1098 { c with scrollb = b }
1099 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
1100 | "point-and-x" ->
1101 { c with pax =
1102 if bool_of_string v
1103 then Some (ref (0.0, 0, 0))
1104 else None }
1105 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
1106 | "scroll-bar-on-the-left" -> { c with leftscroll = bool_of_string v }
1107 | "title" -> { c with title = unent v }
1108 | "last-visit" -> { c with lastvisit = float_of_string v }
1109 | _ -> c
1110 with exn ->
1111 prerr_endline ("Error processing attribute (`" ^
1112 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
1115 let rec fold c = function
1116 | [] -> c
1117 | (k, v) :: rest ->
1118 let c = apply c k v in
1119 fold c rest
1121 fold { c with keyhashes = copykeyhashes c } attrs;
1124 let fromstring f pos n v d =
1125 try f v
1126 with exn ->
1127 dolog "Error processing attribute (%S=%S) at %d\n%s"
1128 n v pos (exntos exn)
1133 let bookmark_of attrs =
1134 let rec fold title page rely visy = function
1135 | ("title", v) :: rest -> fold v page rely visy rest
1136 | ("page", v) :: rest -> fold title v rely visy rest
1137 | ("rely", v) :: rest -> fold title page v visy rest
1138 | ("visy", v) :: rest -> fold title page rely v rest
1139 | _ :: rest -> fold title page rely visy rest
1140 | [] -> title, page, rely, visy
1142 fold "invalid" "0" "0" "0" attrs
1145 let doc_of attrs =
1146 let rec fold path page rely pan visy = function
1147 | ("path", v) :: rest -> fold v page rely pan visy rest
1148 | ("page", v) :: rest -> fold path v rely pan visy rest
1149 | ("rely", v) :: rest -> fold path page v pan visy rest
1150 | ("pan", v) :: rest -> fold path page rely v visy rest
1151 | ("visy", v) :: rest -> fold path page rely pan v rest
1152 | _ :: rest -> fold path page rely pan visy rest
1153 | [] -> path, page, rely, pan, visy
1155 fold E.s "0" "0" "0" "0" attrs
1158 let map_of attrs =
1159 let rec fold rs ls = function
1160 | ("out", v) :: rest -> fold v ls rest
1161 | ("in", v) :: rest -> fold rs v rest
1162 | _ :: rest -> fold ls rs rest
1163 | [] -> ls, rs
1165 fold E.s "" attrs
1168 let setconf dst src =
1169 dst.scrollbw <- src.scrollbw;
1170 dst.scrollh <- src.scrollh;
1171 dst.icase <- src.icase;
1172 dst.preload <- src.preload;
1173 dst.pagebias <- src.pagebias;
1174 dst.verbose <- src.verbose;
1175 dst.scrollstep <- src.scrollstep;
1176 dst.maxhfit <- src.maxhfit;
1177 dst.crophack <- src.crophack;
1178 dst.autoscrollstep <- src.autoscrollstep;
1179 dst.maxwait <- src.maxwait;
1180 dst.hlinks <- src.hlinks;
1181 dst.underinfo <- src.underinfo;
1182 dst.interpagespace <- src.interpagespace;
1183 dst.zoom <- src.zoom;
1184 dst.presentation <- src.presentation;
1185 dst.angle <- src.angle;
1186 dst.cwinw <- src.cwinw;
1187 dst.cwinh <- src.cwinh;
1188 dst.savebmarks <- src.savebmarks;
1189 dst.memlimit <- src.memlimit;
1190 dst.fitmodel <- src.fitmodel;
1191 dst.texcount <- src.texcount;
1192 dst.sliceheight <- src.sliceheight;
1193 dst.thumbw <- src.thumbw;
1194 dst.jumpback <- src.jumpback;
1195 dst.bgcolor <- src.bgcolor;
1196 dst.tilew <- src.tilew;
1197 dst.tileh <- src.tileh;
1198 dst.mustoresize <- src.mustoresize;
1199 dst.checkers <- src.checkers;
1200 dst.aalevel <- src.aalevel;
1201 dst.trimmargins <- src.trimmargins;
1202 dst.trimfuzz <- src.trimfuzz;
1203 dst.urilauncher <- src.urilauncher;
1204 dst.colorspace <- src.colorspace;
1205 dst.invert <- src.invert;
1206 dst.colorscale <- src.colorscale;
1207 dst.redirectstderr <- src.redirectstderr;
1208 dst.ghyllscroll <- src.ghyllscroll;
1209 dst.columns <- src.columns;
1210 dst.beyecolumns <- src.beyecolumns;
1211 dst.selcmd <- src.selcmd;
1212 dst.updatecurs <- src.updatecurs;
1213 dst.pathlauncher <- src.pathlauncher;
1214 dst.keyhashes <- copykeyhashes src;
1215 dst.hfsize <- src.hfsize;
1216 dst.hscrollstep <- src.hscrollstep;
1217 dst.pgscale <- src.pgscale;
1218 dst.usepbo <- src.usepbo;
1219 dst.wheelbypage <- src.wheelbypage;
1220 dst.stcmd <- src.stcmd;
1221 dst.paxcmd <- src.paxcmd;
1222 dst.scrollb <- src.scrollb;
1223 dst.riani <- src.riani;
1224 dst.paxmark <- src.paxmark;
1225 dst.leftscroll <- src.leftscroll;
1226 dst.title <- src.title;
1227 dst.pax <-
1228 if src.pax = None
1229 then None
1230 else Some ((ref (0.0, 0, 0)));
1233 let findkeyhash c name =
1234 try List.assoc name c.keyhashes
1235 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
1238 let get s =
1239 let h = Hashtbl.create 10 in
1240 let dc = { defconf with angle = defconf.angle } in
1241 let rec toplevel v t spos _ =
1242 match t with
1243 | Vdata | Vcdata | Vend -> v
1244 | Vopen ("llppconfig", _, closed) ->
1245 if closed
1246 then v
1247 else { v with f = llppconfig }
1248 | Vopen _ ->
1249 error "unexpected subelement at top level" s spos
1250 | Vclose _ -> error "unexpected close at top level" s spos
1252 and llppconfig v t spos _ =
1253 match t with
1254 | Vdata | Vcdata -> v
1255 | Vend -> error "unexpected end of input in llppconfig" s spos
1256 | Vopen ("defaults", attrs, closed) ->
1257 let c = config_of dc attrs in
1258 setconf dc c;
1259 if closed
1260 then v
1261 else { v with f = defaults }
1263 | Vopen ("ui-font", attrs, closed) ->
1264 let rec getsize size = function
1265 | [] -> size
1266 | ("size", v) :: rest ->
1267 let size =
1268 fromstring int_of_string spos "size" v fstate.fontsize in
1269 getsize size rest
1270 | l -> getsize size l
1272 fstate.fontsize <- getsize fstate.fontsize attrs;
1273 if closed
1274 then v
1275 else { v with f = uifont (Buffer.create 10) }
1277 | Vopen ("doc", attrs, closed) ->
1278 let pathent, spage, srely, span, svisy = doc_of attrs in
1279 let path = unent pathent
1280 and pageno = fromstring int_of_string spos "page" spage 0
1281 and rely = fromstring float_of_string spos "rely" srely 0.0
1282 and pan = fromstring int_of_string spos "pan" span 0
1283 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1284 let c = config_of dc attrs in
1285 let anchor = (pageno, rely, visy) in
1286 if closed
1287 then (Hashtbl.add h path (c, [], pan, anchor); v)
1288 else { v with f = doc path pan anchor c [] }
1290 | Vopen _ ->
1291 error "unexpected subelement in llppconfig" s spos
1293 | Vclose "llppconfig" -> { v with f = toplevel }
1294 | Vclose _ -> error "unexpected close in llppconfig" s spos
1296 and defaults v t spos _ =
1297 match t with
1298 | Vdata | Vcdata -> v
1299 | Vend -> error "unexpected end of input in defaults" s spos
1300 | Vopen ("keymap", attrs, closed) ->
1301 let modename =
1302 try List.assoc "mode" attrs
1303 with Not_found -> "global" in
1304 if closed
1305 then v
1306 else
1307 let ret keymap =
1308 let h = findkeyhash dc modename in
1309 KeyMap.iter (Hashtbl.replace h) keymap;
1310 defaults
1312 { v with f = pkeymap ret KeyMap.empty }
1314 | Vopen (_, _, _) ->
1315 error "unexpected subelement in defaults" s spos
1317 | Vclose "defaults" ->
1318 { v with f = llppconfig }
1320 | Vclose _ -> error "unexpected close in defaults" s spos
1322 and uifont b v t spos epos =
1323 match t with
1324 | Vdata | Vcdata ->
1325 Buffer.add_substring b s spos (epos - spos);
1327 | Vopen (_, _, _) ->
1328 error "unexpected subelement in ui-font" s spos
1329 | Vclose "ui-font" ->
1330 if emptystr !fontpath
1331 then fontpath := Buffer.contents b;
1332 { v with f = llppconfig }
1333 | Vclose _ -> error "unexpected close in ui-font" s spos
1334 | Vend -> error "unexpected end of input in ui-font" s spos
1336 and doc path pan anchor c bookmarks v t spos _ =
1337 match t with
1338 | Vdata | Vcdata -> v
1339 | Vend -> error "unexpected end of input in doc" s spos
1340 | Vopen ("bookmarks", _, closed) ->
1341 if closed
1342 then v
1343 else { v with f = pbookmarks path pan anchor c bookmarks }
1345 | Vopen ("keymap", attrs, closed) ->
1346 let modename =
1347 try List.assoc "mode" attrs
1348 with Not_found -> "global"
1350 if closed
1351 then v
1352 else
1353 let ret keymap =
1354 let h = findkeyhash c modename in
1355 KeyMap.iter (Hashtbl.replace h) keymap;
1356 doc path pan anchor c bookmarks
1358 { v with f = pkeymap ret KeyMap.empty }
1360 | Vopen (_, _, _) ->
1361 error "unexpected subelement in doc" s spos
1363 | Vclose "doc" ->
1364 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
1365 { v with f = llppconfig }
1367 | Vclose _ -> error "unexpected close in doc" s spos
1369 and pkeymap ret keymap v t spos _ =
1370 match t with
1371 | Vdata | Vcdata -> v
1372 | Vend -> error "unexpected end of input in keymap" s spos
1373 | Vopen ("map", attrs, closed) ->
1374 let r, l = map_of attrs in
1375 let kss = fromstring keys_of_string spos "in" r [] in
1376 let lss = fromstring keys_of_string spos "out" l [] in
1377 let keymap =
1378 match kss with
1379 | [] -> keymap
1380 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
1381 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
1383 if closed
1384 then { v with f = pkeymap ret keymap }
1385 else
1386 let f () = v in
1387 { v with f = skip "map" f }
1389 | Vopen _ ->
1390 error "unexpected subelement in keymap" s spos
1392 | Vclose "keymap" ->
1393 { v with f = ret keymap }
1395 | Vclose _ -> error "unexpected close in keymap" s spos
1397 and pbookmarks path pan anchor c bookmarks v t spos _ =
1398 match t with
1399 | Vdata | Vcdata -> v
1400 | Vend -> error "unexpected end of input in bookmarks" s spos
1401 | Vopen ("item", attrs, closed) ->
1402 let titleent, spage, srely, svisy = bookmark_of attrs in
1403 let page = fromstring int_of_string spos "page" spage 0
1404 and rely = fromstring float_of_string spos "rely" srely 0.0
1405 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1406 let bookmarks =
1407 (unent titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
1409 if closed
1410 then { v with f = pbookmarks path pan anchor c bookmarks }
1411 else
1412 let f () = v in
1413 { v with f = skip "item" f }
1415 | Vopen _ ->
1416 error "unexpected subelement in bookmarks" s spos
1418 | Vclose "bookmarks" ->
1419 { v with f = doc path pan anchor c bookmarks }
1421 | Vclose _ -> error "unexpected close in bookmarks" s spos
1423 and skip tag f v t spos _ =
1424 match t with
1425 | Vdata | Vcdata -> v
1426 | Vend ->
1427 error ("unexpected end of input in skipped " ^ tag) s spos
1428 | Vopen (tag', _, closed) ->
1429 if closed
1430 then v
1431 else
1432 let f' () = { v with f = skip tag f } in
1433 { v with f = skip tag' f' }
1434 | Vclose ctag ->
1435 if tag = ctag
1436 then f ()
1437 else error ("unexpected close in skipped " ^ tag) s spos
1440 parse { f = toplevel; accu = () } s;
1441 h, dc;
1444 let do_load f ic =
1446 let len = in_channel_length ic in
1447 let s = String.create len in
1448 really_input ic s 0 len;
1449 f s;
1450 with
1451 | Parse_error (msg, s, pos) ->
1452 let subs = subs s pos in
1453 Utils.error "parse error: %s: at %d [..%s..]" msg pos subs
1455 | exn ->
1456 failwith ("config load error: " ^ exntos exn)
1459 let defconfpath =
1460 let dir =
1462 let dir = Filename.concat home ".config" in
1463 if Sys.is_directory dir then dir else home
1464 with _ -> home
1466 Filename.concat dir "llpp.conf"
1469 let confpath = ref defconfpath;;
1471 let load1 f =
1472 if Sys.file_exists !confpath
1473 then
1474 match
1475 (try Some (open_in_bin !confpath)
1476 with exn ->
1477 prerr_endline
1478 ("Error opening configuration file `" ^ !confpath ^ "': " ^
1479 exntos exn);
1480 None
1482 with
1483 | Some ic ->
1484 let success =
1486 f (do_load get ic)
1487 with exn ->
1488 prerr_endline
1489 ("Error loading configuration from `" ^ !confpath ^ "': " ^
1490 exntos exn);
1491 false
1493 close_in ic;
1494 success
1496 | None -> false
1497 else
1498 f (Hashtbl.create 0, defconf)
1501 let load () =
1502 let f (h, dc) =
1503 let pc, pb, px, pa =
1505 let path =
1506 if emptystr state.origin
1507 then state.path
1508 else state.origin
1510 let absname = abspath path in
1511 Hashtbl.find h absname
1512 with Not_found -> dc, [], 0, emptyanchor
1514 setconf defconf dc;
1515 setconf conf pc;
1516 state.bookmarks <- pb;
1517 state.x <- px;
1518 if conf.jumpback
1519 then state.anchor <- pa;
1520 cbput state.hists.nav pa;
1521 true
1523 load1 f
1526 let gethist listref =
1527 let f (h, _) =
1528 listref :=
1529 Hashtbl.fold (fun path (pc, pb, px, pa) accu ->
1530 (path, pc, pb, px, pa) :: accu)
1531 h [];
1532 true
1534 load1 f
1537 let add_attrs bb always dc c time =
1538 let ob s a b =
1539 if always || a != b
1540 then Printf.bprintf bb "\n %s='%b'" s a
1541 and op s a b =
1542 if always || a <> b
1543 then Printf.bprintf bb "\n %s='%b'" s (a != None)
1544 and oi s a b =
1545 if always || a != b
1546 then Printf.bprintf bb "\n %s='%d'" s a
1547 and oI s a b =
1548 if always || a != b
1549 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
1550 and oz s a b =
1551 if always || a <> b
1552 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
1553 and oF s a b =
1554 if always || a <> b
1555 then Printf.bprintf bb "\n %s='%F'" s a
1556 and oc s a b =
1557 if always || a <> b
1558 then
1559 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
1560 and oC s a b =
1561 if always || a <> b
1562 then
1563 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
1564 and oR s a b =
1565 if always || a <> b
1566 then
1567 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
1568 and os s a b =
1569 if always || a <> b
1570 then
1571 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
1572 and og s a b =
1573 if always || a <> b
1574 then
1575 match a with
1576 | Some (_N, _A, _B) ->
1577 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
1578 | None ->
1579 match b with
1580 | None -> ()
1581 | _ ->
1582 Printf.bprintf bb "\n %s='none'" s
1583 and oW s a b =
1584 if always || a <> b
1585 then
1586 let v =
1587 match a with
1588 | None -> "false"
1589 | Some f ->
1590 if f = infinity
1591 then "true"
1592 else string_of_float f
1594 Printf.bprintf bb "\n %s='%s'" s v
1595 and oco s a b =
1596 if always || a <> b
1597 then
1598 match a with
1599 | Cmulti ((n, a, b), _) when n > 1 ->
1600 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
1601 | Csplit (n, _) when n > 1 ->
1602 Printf.bprintf bb "\n %s='%d'" s ~-n
1603 | _ -> ()
1604 and obeco s a b =
1605 if always || a <> b
1606 then
1607 match a with
1608 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
1609 | _ -> ()
1610 and oFm s a b =
1611 if always || a <> b
1612 then
1613 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
1614 and oSv s a b m =
1615 if always || a <> b
1616 then
1617 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
1618 and oPm s a b =
1619 if always || a <> b
1620 then
1621 Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
1623 oi "width" c.cwinw dc.cwinw;
1624 oi "height" c.cwinh dc.cwinh;
1625 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
1626 oi "scroll-handle-height" c.scrollh dc.scrollh;
1627 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
1628 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
1629 ob "case-insensitive-search" c.icase dc.icase;
1630 ob "preload" c.preload dc.preload;
1631 oi "page-bias" c.pagebias dc.pagebias;
1632 oi "scroll-step" c.scrollstep dc.scrollstep;
1633 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
1634 ob "max-height-fit" c.maxhfit dc.maxhfit;
1635 ob "crop-hack" c.crophack dc.crophack;
1636 oW "throttle" c.maxwait dc.maxwait;
1637 ob "highlight-links" c.hlinks dc.hlinks;
1638 ob "under-cursor-info" c.underinfo dc.underinfo;
1639 oi "vertical-margin" c.interpagespace dc.interpagespace;
1640 oz "zoom" c.zoom dc.zoom;
1641 ob "presentation" c.presentation dc.presentation;
1642 oi "rotation-angle" c.angle dc.angle;
1643 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
1644 oFm "fit-model" c.fitmodel dc.fitmodel;
1645 oI "pixmap-cache-size" c.memlimit dc.memlimit;
1646 oi "tex-count" c.texcount dc.texcount;
1647 oi "slice-height" c.sliceheight dc.sliceheight;
1648 oi "thumbnail-width" c.thumbw dc.thumbw;
1649 ob "persistent-location" c.jumpback dc.jumpback;
1650 oc "background-color" c.bgcolor dc.bgcolor;
1651 oi "tile-width" c.tilew dc.tilew;
1652 oi "tile-height" c.tileh dc.tileh;
1653 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
1654 ob "checkers" c.checkers dc.checkers;
1655 oi "aalevel" c.aalevel dc.aalevel;
1656 ob "trim-margins" c.trimmargins dc.trimmargins;
1657 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
1658 os "uri-launcher" c.urilauncher dc.urilauncher;
1659 os "path-launcher" c.pathlauncher dc.pathlauncher;
1660 oC "color-space" c.colorspace dc.colorspace;
1661 ob "invert-colors" c.invert dc.invert;
1662 oF "brightness" c.colorscale dc.colorscale;
1663 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
1664 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
1665 oco "columns" c.columns dc.columns;
1666 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
1667 os "selection-command" c.selcmd dc.selcmd;
1668 os "synctex-command" c.stcmd dc.stcmd;
1669 os "pax-command" c.paxcmd dc.paxcmd;
1670 ob "update-cursor" c.updatecurs dc.updatecurs;
1671 oi "hint-font-size" c.hfsize dc.hfsize;
1672 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
1673 oF "page-scroll-scale" c.pgscale dc.pgscale;
1674 ob "use-pbo" c.usepbo dc.usepbo;
1675 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
1676 ob "remote-in-a-new-instance" c.riani dc.riani;
1677 op "point-and-x" c.pax dc.pax;
1678 oPm "point-and-x-mark" c.paxmark dc.paxmark;
1679 ob "scroll-bar-on-the-left" c.leftscroll dc.leftscroll;
1680 if not always
1681 then os "title" c.title dc.title;
1682 oF "last-visit" (snd (modf time)) 0.0;
1685 let keymapsbuf always dc c =
1686 let bb = Buffer.create 16 in
1687 let rec loop = function
1688 | [] -> ()
1689 | (modename, h) :: rest ->
1690 let dh = findkeyhash dc modename in
1691 if always || h <> dh
1692 then (
1693 if Hashtbl.length h > 0
1694 then (
1695 if Buffer.length bb > 0
1696 then Buffer.add_char bb '\n';
1697 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
1698 Hashtbl.iter (fun i o ->
1699 let isdifferent = always ||
1701 let dO = Hashtbl.find dh i in
1702 dO <> o
1703 with Not_found -> true
1705 if isdifferent
1706 then
1707 let addkm (k, m) =
1708 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
1709 if Wsi.withalt m then Buffer.add_string bb "alt-";
1710 if Wsi.withshift m then Buffer.add_string bb "shift-";
1711 if Wsi.withmeta m then Buffer.add_string bb "meta-";
1712 Buffer.add_string bb (Wsi.keyname k);
1714 let addkms l =
1715 let rec loop = function
1716 | [] -> ()
1717 | km :: [] -> addkm km
1718 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
1720 loop l
1722 Buffer.add_string bb "<map in='";
1723 addkm i;
1724 match o with
1725 | KMinsrt km ->
1726 Buffer.add_string bb "' out='";
1727 addkm km;
1728 Buffer.add_string bb "'/>\n"
1730 | KMinsrl kms ->
1731 Buffer.add_string bb "' out='";
1732 addkms kms;
1733 Buffer.add_string bb "'/>\n"
1735 | KMmulti (ins, kms) ->
1736 Buffer.add_char bb ' ';
1737 addkms ins;
1738 Buffer.add_string bb "' out='";
1739 addkms kms;
1740 Buffer.add_string bb "'/>\n"
1741 ) h;
1742 Buffer.add_string bb "</keymap>";
1745 loop rest
1747 loop c.keyhashes;
1751 let save leavebirdseye =
1752 let uifontsize = fstate.fontsize in
1753 let bb = Buffer.create 32768 in
1754 let relx = float state.x /. float state.winw in
1755 let w, h, x =
1756 let cx w = truncate (relx *. float w) in
1757 List.fold_left
1758 (fun (w, h, x) ws ->
1759 match ws with
1760 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
1761 | Wsi.MaxVert -> (w, conf.cwinh, x)
1762 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
1764 (state.winw, state.winh, state.x) state.winstate
1766 conf.cwinw <- w;
1767 conf.cwinh <- h;
1768 let f (h, dc) =
1769 let dc = if conf.bedefault then conf else dc in
1770 Buffer.add_string bb "<llppconfig>\n";
1772 if nonemptystr !fontpath
1773 then
1774 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
1775 uifontsize
1776 !fontpath
1777 else (
1778 if uifontsize <> 14
1779 then
1780 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
1783 Buffer.add_string bb "<defaults";
1784 add_attrs bb true dc dc nan;
1785 let kb = keymapsbuf true dc dc in
1786 if Buffer.length kb > 0
1787 then (
1788 Buffer.add_string bb ">\n";
1789 Buffer.add_buffer bb kb;
1790 Buffer.add_string bb "\n</defaults>\n";
1792 else Buffer.add_string bb "/>\n";
1794 let adddoc path pan anchor c bookmarks time =
1795 if bookmarks == [] && c = dc && anchor = emptyanchor
1796 then ()
1797 else (
1798 Printf.bprintf bb "<doc path='%s'"
1799 (enent path 0 (String.length path));
1801 if anchor <> emptyanchor
1802 then (
1803 let n, rely, visy = anchor in
1804 Printf.bprintf bb "\n page='%d'" n;
1805 if rely > 1e-6
1806 then
1807 Printf.bprintf bb " rely='%f'" rely
1809 if abs_float visy > 1e-6
1810 then
1811 Printf.bprintf bb " visy='%f'" visy
1815 if pan != 0
1816 then Printf.bprintf bb " pan='%d'" pan;
1818 add_attrs bb false dc c time;
1819 let kb = keymapsbuf false dc c in
1821 begin match bookmarks with
1822 | [] ->
1823 if Buffer.length kb > 0
1824 then (
1825 Buffer.add_string bb ">\n";
1826 Buffer.add_buffer bb kb;
1827 Buffer.add_string bb "\n</doc>\n";
1829 else Buffer.add_string bb "/>\n"
1830 | _ ->
1831 Buffer.add_string bb ">\n<bookmarks>\n";
1832 List.iter (fun (title, _, kind) ->
1833 begin match kind with
1834 | Oanchor (page, rely, visy) ->
1835 Printf.bprintf bb
1836 "<item title='%s' page='%d'"
1837 (enent title 0 (String.length title))
1838 page
1840 if rely > 1e-6
1841 then
1842 Printf.bprintf bb " rely='%f'" rely
1844 if abs_float visy > 1e-6
1845 then
1846 Printf.bprintf bb " visy='%f'" visy
1848 | Ohistory _ | Onone | Ouri _ | Oremote _
1849 | Oremotedest _ | Olaunch _ | Oaction _ ->
1850 failwith "unexpected link in bookmarks"
1851 end;
1852 Buffer.add_string bb "/>\n";
1853 ) bookmarks;
1854 Buffer.add_string bb "</bookmarks>";
1855 if Buffer.length kb > 0
1856 then (
1857 Buffer.add_string bb "\n";
1858 Buffer.add_buffer bb kb;
1860 Buffer.add_string bb "\n</doc>\n";
1861 end;
1865 let pan, conf =
1866 match state.mode with
1867 | Birdseye (c, pan, _, _, _) ->
1868 let beyecolumns =
1869 match conf.columns with
1870 | Cmulti ((c, _, _), _) -> Some c
1871 | Csingle _ -> None
1872 | Csplit _ -> None
1873 and columns =
1874 match c.columns with
1875 | Cmulti (c, _) -> Cmulti (c, E.a)
1876 | Csingle _ -> Csingle E.a
1877 | Csplit _ -> failwith "quit from bird's eye while split"
1879 pan, { c with beyecolumns = beyecolumns; columns = columns }
1880 | _ -> x, conf
1882 let docpath = abspath state.path in
1883 adddoc docpath pan (getanchor ())
1885 let autoscrollstep =
1886 match state.autoscroll with
1887 | Some step -> step
1888 | None -> conf.autoscrollstep
1890 begin match state.mode with
1891 | Birdseye beye -> leavebirdseye beye true
1892 | _ -> ()
1893 end;
1894 { conf with autoscrollstep = autoscrollstep }
1896 (if conf.savebmarks then state.bookmarks else [])
1897 (now ());
1899 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
1900 if docpath <> abspath path
1901 then adddoc path x anchor c bookmarks c.lastvisit
1902 ) h;
1903 Buffer.add_string bb "</llppconfig>\n";
1904 true;
1906 if load1 f && Buffer.length bb > 0
1907 then
1909 let tmp = !confpath ^ ".tmp" in
1910 let oc = open_out_bin tmp in
1911 Buffer.output_buffer oc bb;
1912 close_out oc;
1913 Unix.rename tmp !confpath;
1914 with exn ->
1915 prerr_endline
1916 ("error while saving configuration: " ^ exntos exn)