Ellipsis
[llpp.git] / config.ml
blobb114c46c2126e5a8794bef21fe33157de7710829
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 and outline = (caption * outlinelevel * outlinekind)
293 and outlinelevel = int
296 type page =
297 { pageno : int
298 ; pagedimno : int
299 ; pagew : int
300 ; pageh : int
301 ; pagex : int
302 ; pagey : int
303 ; pagevw : int
304 ; pagevh : int
305 ; pagedispx : int
306 ; pagedispy : int
307 ; pagecol : int
311 type tile = opaque * pixmapsize * elapsed
312 and elapsed = float;;
313 type pagemapkey = pageno * gen;;
314 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
315 and row = int
316 and col = int
317 and currently =
318 | Idle
319 | Loading of (page * gen)
320 | Tiling of (
321 page * opaque * colorspace * angle * gen * col * row * width * height
323 | Outlining of outline list
326 type mpos = int * int
327 and mstate =
328 | Msel of (mpos * mpos)
329 | Mpan of mpos
330 | Mscrolly | Mscrollx
331 | Mzoom of (int * int)
332 | Mzoomrect of (mpos * mpos)
333 | Mnone
336 type mode =
337 | Birdseye of (conf * leftx * pageno * pageno * anchor)
338 | Textentry of (textentry * onleave)
339 | View
340 | LinkNav of linktarget
341 and onleave = leavetextentrystatus -> unit
342 and leavetextentrystatus = | Cancel | Confirm
343 and helpitem = string * int * action
344 and action =
345 | Noaction
346 | Action of (uioh -> uioh)
347 and linktarget =
348 | Ltexact of (pageno * int)
349 | Ltgendir of int
350 and textentry = string * string * onhist option * onkey * ondone * cancelonempty
351 and onkey = string -> int -> te
352 and ondone = string -> unit
353 and histcancel = unit -> unit
354 and onhist = ((histcmd -> string) * histcancel)
355 and histcmd = HCnext | HCprev | HCfirst | HClast
356 and cancelonempty = bool
357 and te =
358 | TEstop
359 | TEdone of string
360 | TEcont of string
361 | TEswitch of textentry
364 type 'a circbuf =
365 { store : 'a array
366 ; mutable rc : int
367 ; mutable wc : int
368 ; mutable len : int
372 type state =
373 { mutable sr : Unix.file_descr
374 ; mutable sw : 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 { sr = Unix.stdin
677 ; sw = Unix.stdin
678 ; wsfd = Unix.stdin
679 ; errfd = None
680 ; stderr = Unix.stderr
681 ; errmsgs = Buffer.create 0
682 ; newerrmsgs = false
683 ; x = 0
684 ; y = 0
685 ; w = 0
686 ; anchor = emptyanchor
687 ; ranchors = []
688 ; layout = []
689 ; maxy = max_int
690 ; tilelru = Queue.create ()
691 ; pagemap = Hashtbl.create 10
692 ; tilemap = Hashtbl.create 10
693 ; pdims = []
694 ; pagecount = 0
695 ; currently = Idle
696 ; mstate = Mnone
697 ; rects = []
698 ; rects1 = []
699 ; text = E.s
700 ; mode = View
701 ; winstate = []
702 ; searchpattern = E.s
703 ; outlines = E.a
704 ; bookmarks = []
705 ; path = E.s
706 ; password = E.s
707 ; nameddest = E.s
708 ; geomcmds = firstgeomcmds
709 ; hists =
710 { nav = cbnew 10 emptyanchor
711 ; pat = cbnew 10 E.s
712 ; pag = cbnew 10 E.s
713 ; sel = cbnew 10 E.s
715 ; memused = 0
716 ; gen = 0
717 ; throttle = None
718 ; autoscroll = None
719 ; ghyll = noghyll
720 ; help = makehelp ()
721 ; docinfo = []
722 ; checkerstexid = None
723 ; prevzoom = (1.0, 0)
724 ; progress = -1.0
725 ; uioh = nouioh
726 ; redisplay = true
727 ; mpos = (-1, -1)
728 ; keystate = KSnone
729 ; glinks = false
730 ; prevcolumns = None
731 ; winw = -1
732 ; winh = -1
733 ; reprf = noreprf
734 ; origin = E.s
735 ; roam = noroam
736 ; bzoom = false
737 ; traw = Raw.create_static `float 8
738 ; vraw = Raw.create_static `float 8
742 let copykeyhashes c =
743 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
746 let calcips h =
747 let d = state.winh - h in
748 max conf.interpagespace ((d + 1) / 2)
751 let rowyh (c, coverA, coverB) b n =
752 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
753 then
754 let _, _, vy, (_, _, h, _) = b.(n) in
755 (vy, h)
756 else
757 let n' = n - coverA in
758 let d = n' mod c in
759 let s = n - d in
760 let e = min state.pagecount (s + c) in
761 let rec find m miny maxh = if m = e then miny, maxh else
762 let _, _, y, (_, _, h, _) = b.(m) in
763 let miny = min miny y in
764 let maxh = max maxh h in
765 find (m+1) miny maxh
766 in find s max_int 0
769 let page_of_y y =
770 let ((c, coverA, coverB) as cl), b =
771 match conf.columns with
772 | Csingle b -> (1, 0, 0), b
773 | Cmulti (c, b) -> c, b
774 | Csplit (_, b) -> (1, 0, 0), b
776 if Array.length b = 0
777 then -1
778 else
779 let rec bsearch nmin nmax =
780 if nmin > nmax
781 then bound nmin 0 (state.pagecount-1)
782 else
783 let n = (nmax + nmin) / 2 in
784 let vy, h = rowyh cl b n in
785 let y0, y1 =
786 if conf.presentation
787 then
788 let ips = calcips h in
789 let y0 = vy - ips in
790 let y1 = vy + h + ips in
791 y0, y1
792 else (
793 if n = 0
794 then 0, vy + h + conf.interpagespace
795 else
796 let y0 = vy - conf.interpagespace in
797 y0, y0 + h + conf.interpagespace
800 if y >= y0 && y < y1
801 then (
802 if c = 1
803 then n
804 else (
805 if n > coverA
806 then
807 if n < state.pagecount - coverB
808 then ((n-coverA)/c)*c + coverA
809 else n
810 else n
813 else (
814 if y > y0
815 then bsearch (n+1) nmax
816 else bsearch nmin (n-1)
819 bsearch 0 (state.pagecount-1);
822 let calcheight () =
823 match conf.columns with
824 | Cmulti ((_, _, _) as cl, b) ->
825 if Array.length b > 0
826 then
827 let y, h = rowyh cl b (Array.length b - 1) in
828 y + h + (if conf.presentation then calcips h else 0)
829 else 0
830 | Csingle b ->
831 if Array.length b > 0
832 then
833 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
834 y + h + (if conf.presentation then calcips h else 0)
835 else 0
836 | Csplit (_, b) ->
837 if Array.length b > 0
838 then
839 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
840 y + h
841 else 0
844 let getpageywh pageno =
845 let pageno = bound pageno 0 (state.pagecount-1) in
846 match conf.columns with
847 | Csingle b ->
848 if Array.length b = 0
849 then 0, 0, 0
850 else
851 let (_, _, y, (_, w, h, _)) = b.(pageno) in
852 let y =
853 if conf.presentation
854 then y - calcips h
855 else y
857 y, w, h
858 | Cmulti (cl, b) ->
859 if Array.length b = 0
860 then 0, 0, 0
861 else
862 let y, h = rowyh cl b pageno in
863 let (_, _, _, (_, w, _, _)) = b.(pageno) in
864 let y =
865 if conf.presentation
866 then y - calcips h
867 else y
869 y, w, h
870 | Csplit (c, b) ->
871 if Array.length b = 0
872 then 0, 0, 0
873 else
874 let n = pageno*c in
875 let (_, _, y, (_, w, h, _)) = b.(n) in
876 y, w / c, h
879 let getpageyh pageno =
880 let y,_,h = getpageywh pageno in
881 y, h;
884 let getpagedim pageno =
885 let rec f ppdim l =
886 match l with
887 | (n, _, _, _) as pdim :: rest ->
888 if n >= pageno
889 then (if n = pageno then pdim else ppdim)
890 else f pdim rest
892 | [] -> ppdim
894 f (-1, -1, -1, -1) state.pdims
897 let getpagey pageno = fst (getpageyh pageno);;
899 let getanchor1 l =
900 let top =
901 let coloff = l.pagecol * l.pageh in
902 float (l.pagey + coloff) /. float l.pageh
904 let dtop =
905 if l.pagedispy = 0
906 then
908 else (
909 if conf.presentation
910 then float l.pagedispy /. float (calcips l.pageh)
911 else float l.pagedispy /. float conf.interpagespace
914 (l.pageno, top, dtop)
917 let getanchor () =
918 match state.layout with
919 | l :: _ -> getanchor1 l
920 | [] ->
921 let n = page_of_y state.y in
922 if n = -1
923 then state.anchor
924 else
925 let y, h = getpageyh n in
926 let dy = y - state.y in
927 let dtop =
928 if conf.presentation
929 then
930 let ips = calcips h in
931 float (dy + ips) /. float ips
932 else
933 float dy /. float conf.interpagespace
935 (n, 0.0, dtop)
938 let fontpath = ref E.s;;
940 type historder = [ `lastvisit | `title | `path | `file ];;
941 let historder : historder ref = ref `lastvisit;;
943 module KeyMap =
944 Map.Make (struct type t = (int * int) let compare = compare end);;
946 open Parser;;
948 let unent s =
949 let l = String.length s in
950 let b = Buffer.create l in
951 unent b s 0 l;
952 Buffer.contents b;
955 let home =
956 try Sys.getenv "HOME"
957 with exn ->
958 prerr_endline
959 ("Can not determine home directory location: " ^ exntos exn);
963 let modifier_of_string = function
964 | "alt" -> Wsi.altmask
965 | "shift" -> Wsi.shiftmask
966 | "ctrl" | "control" -> Wsi.ctrlmask
967 | "meta" -> Wsi.metamask
968 | _ -> 0
971 let key_of_string =
972 let r = Str.regexp "-" in
973 fun s ->
974 let elems = Str.full_split r s in
975 let f n k m =
976 let g s =
977 let m1 = modifier_of_string s in
978 if m1 = 0
979 then (Wsi.namekey s, m)
980 else (k, m lor m1)
981 in function
982 | Str.Delim s when n land 1 = 0 -> g s
983 | Str.Text s -> g s
984 | Str.Delim _ -> (k, m)
986 let rec loop n k m = function
987 | [] -> (k, m)
988 | x :: xs ->
989 let k, m = f n k m x in
990 loop (n+1) k m xs
992 loop 0 0 0 elems
995 let keys_of_string =
996 let r = Str.regexp "[ \t]" in
997 fun s ->
998 let elems = Str.split r s in
999 List.map key_of_string elems
1002 let config_of c attrs =
1003 let apply c k v =
1005 match k with
1006 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
1007 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
1008 | "case-insensitive-search" -> { c with icase = bool_of_string v }
1009 | "preload" -> { c with preload = bool_of_string v }
1010 | "page-bias" -> { c with pagebias = int_of_string v }
1011 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
1012 | "horizontal-scroll-step" ->
1013 { c with hscrollstep = max (int_of_string v) 1 }
1014 | "auto-scroll-step" ->
1015 { c with autoscrollstep = max 0 (int_of_string v) }
1016 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
1017 | "crop-hack" -> { c with crophack = bool_of_string v }
1018 | "throttle" ->
1019 let mw =
1020 match String.lowercase v with
1021 | "true" -> Some infinity
1022 | "false" -> None
1023 | f -> Some (float_of_string f)
1025 { c with maxwait = mw}
1026 | "highlight-links" -> { c with hlinks = bool_of_string v }
1027 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
1028 | "vertical-margin" ->
1029 { c with interpagespace = max 0 (int_of_string v) }
1030 | "zoom" ->
1031 let zoom = float_of_string v /. 100. in
1032 let zoom = max zoom 0.0 in
1033 { c with zoom = zoom }
1034 | "presentation" -> { c with presentation = bool_of_string v }
1035 | "rotation-angle" -> { c with angle = int_of_string v }
1036 | "width" -> { c with cwinw = max 20 (int_of_string v) }
1037 | "height" -> { c with cwinh = max 20 (int_of_string v) }
1038 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
1039 | "proportional-display" ->
1040 let fm =
1041 if bool_of_string v
1042 then FitProportional
1043 else FitWidth
1045 { c with fitmodel = fm }
1046 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
1047 | "pixmap-cache-size" ->
1048 { c with memlimit = max 2 (int_of_string_with_suffix v) }
1049 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
1050 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
1051 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
1052 | "persistent-location" -> { c with jumpback = bool_of_string v }
1053 | "background-color" -> { c with bgcolor = color_of_string v }
1054 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
1055 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
1056 | "mupdf-store-size" ->
1057 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
1058 | "checkers" -> { c with checkers = bool_of_string v }
1059 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
1060 | "trim-margins" -> { c with trimmargins = bool_of_string v }
1061 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
1062 | "uri-launcher" -> { c with urilauncher = unent v }
1063 | "path-launcher" -> { c with pathlauncher = unent v }
1064 | "color-space" -> { c with colorspace = CSTE.of_string v }
1065 | "invert-colors" -> { c with invert = bool_of_string v }
1066 | "brightness" -> { c with colorscale = float_of_string v }
1067 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
1068 | "ghyllscroll" -> { c with ghyllscroll = ghyllscroll_of_string v }
1069 | "columns" ->
1070 let (n, _, _) as nab = multicolumns_of_string v in
1071 if n < 0
1072 then { c with columns = Csplit (-n, E.a) }
1073 else { c with columns = Cmulti (nab, E.a) }
1074 | "birds-eye-columns" ->
1075 { c with beyecolumns = Some (max (int_of_string v) 2) }
1076 | "selection-command" -> { c with selcmd = unent v }
1077 | "synctex-command" -> { c with stcmd = unent v }
1078 | "pax-command" -> { c with paxcmd = unent v }
1079 | "update-cursor" -> { c with updatecurs = bool_of_string v }
1080 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
1081 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
1082 | "use-pbo" -> { c with usepbo = bool_of_string v }
1083 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
1084 | "horizontal-scrollbar-visible" ->
1085 let b =
1086 if bool_of_string v
1087 then c.scrollb lor scrollbhv
1088 else c.scrollb land (lnot scrollbhv)
1090 { c with scrollb = b }
1091 | "vertical-scrollbar-visible" ->
1092 let b =
1093 if bool_of_string v
1094 then c.scrollb lor scrollbvv
1095 else c.scrollb land (lnot scrollbvv)
1097 { c with scrollb = b }
1098 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
1099 | "point-and-x" ->
1100 { c with pax =
1101 if bool_of_string v
1102 then Some (ref (0.0, 0, 0))
1103 else None }
1104 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
1105 | "scroll-bar-on-the-left" -> { c with leftscroll = bool_of_string v }
1106 | "title" -> { c with title = unent v }
1107 | "last-visit" -> { c with lastvisit = float_of_string v }
1108 | _ -> c
1109 with exn ->
1110 prerr_endline ("Error processing attribute (`" ^
1111 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
1114 let rec fold c = function
1115 | [] -> c
1116 | (k, v) :: rest ->
1117 let c = apply c k v in
1118 fold c rest
1120 fold { c with keyhashes = copykeyhashes c } attrs;
1123 let fromstring f pos n v d =
1124 try f v
1125 with exn ->
1126 dolog "Error processing attribute (%S=%S) at %d\n%s"
1127 n v pos (exntos exn)
1132 let bookmark_of attrs =
1133 let rec fold title page rely visy = function
1134 | ("title", v) :: rest -> fold v page rely visy rest
1135 | ("page", v) :: rest -> fold title v rely visy rest
1136 | ("rely", v) :: rest -> fold title page v visy rest
1137 | ("visy", v) :: rest -> fold title page rely v rest
1138 | _ :: rest -> fold title page rely visy rest
1139 | [] -> title, page, rely, visy
1141 fold "invalid" "0" "0" "0" attrs
1144 let doc_of attrs =
1145 let rec fold path page rely pan visy = function
1146 | ("path", v) :: rest -> fold v page rely pan visy rest
1147 | ("page", v) :: rest -> fold path v rely pan visy rest
1148 | ("rely", v) :: rest -> fold path page v pan visy rest
1149 | ("pan", v) :: rest -> fold path page rely v visy rest
1150 | ("visy", v) :: rest -> fold path page rely pan v rest
1151 | _ :: rest -> fold path page rely pan visy rest
1152 | [] -> path, page, rely, pan, visy
1154 fold E.s "0" "0" "0" "0" attrs
1157 let map_of attrs =
1158 let rec fold rs ls = function
1159 | ("out", v) :: rest -> fold v ls rest
1160 | ("in", v) :: rest -> fold rs v rest
1161 | _ :: rest -> fold ls rs rest
1162 | [] -> ls, rs
1164 fold E.s "" attrs
1167 let setconf dst src =
1168 dst.scrollbw <- src.scrollbw;
1169 dst.scrollh <- src.scrollh;
1170 dst.icase <- src.icase;
1171 dst.preload <- src.preload;
1172 dst.pagebias <- src.pagebias;
1173 dst.verbose <- src.verbose;
1174 dst.scrollstep <- src.scrollstep;
1175 dst.maxhfit <- src.maxhfit;
1176 dst.crophack <- src.crophack;
1177 dst.autoscrollstep <- src.autoscrollstep;
1178 dst.maxwait <- src.maxwait;
1179 dst.hlinks <- src.hlinks;
1180 dst.underinfo <- src.underinfo;
1181 dst.interpagespace <- src.interpagespace;
1182 dst.zoom <- src.zoom;
1183 dst.presentation <- src.presentation;
1184 dst.angle <- src.angle;
1185 dst.cwinw <- src.cwinw;
1186 dst.cwinh <- src.cwinh;
1187 dst.savebmarks <- src.savebmarks;
1188 dst.memlimit <- src.memlimit;
1189 dst.fitmodel <- src.fitmodel;
1190 dst.texcount <- src.texcount;
1191 dst.sliceheight <- src.sliceheight;
1192 dst.thumbw <- src.thumbw;
1193 dst.jumpback <- src.jumpback;
1194 dst.bgcolor <- src.bgcolor;
1195 dst.tilew <- src.tilew;
1196 dst.tileh <- src.tileh;
1197 dst.mustoresize <- src.mustoresize;
1198 dst.checkers <- src.checkers;
1199 dst.aalevel <- src.aalevel;
1200 dst.trimmargins <- src.trimmargins;
1201 dst.trimfuzz <- src.trimfuzz;
1202 dst.urilauncher <- src.urilauncher;
1203 dst.colorspace <- src.colorspace;
1204 dst.invert <- src.invert;
1205 dst.colorscale <- src.colorscale;
1206 dst.redirectstderr <- src.redirectstderr;
1207 dst.ghyllscroll <- src.ghyllscroll;
1208 dst.columns <- src.columns;
1209 dst.beyecolumns <- src.beyecolumns;
1210 dst.selcmd <- src.selcmd;
1211 dst.updatecurs <- src.updatecurs;
1212 dst.pathlauncher <- src.pathlauncher;
1213 dst.keyhashes <- copykeyhashes src;
1214 dst.hfsize <- src.hfsize;
1215 dst.hscrollstep <- src.hscrollstep;
1216 dst.pgscale <- src.pgscale;
1217 dst.usepbo <- src.usepbo;
1218 dst.wheelbypage <- src.wheelbypage;
1219 dst.stcmd <- src.stcmd;
1220 dst.paxcmd <- src.paxcmd;
1221 dst.scrollb <- src.scrollb;
1222 dst.riani <- src.riani;
1223 dst.paxmark <- src.paxmark;
1224 dst.leftscroll <- src.leftscroll;
1225 dst.title <- src.title;
1226 dst.pax <-
1227 if src.pax = None
1228 then None
1229 else Some ((ref (0.0, 0, 0)));
1232 let findkeyhash c name =
1233 try List.assoc name c.keyhashes
1234 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
1237 let get s =
1238 let h = Hashtbl.create 10 in
1239 let dc = { defconf with angle = defconf.angle } in
1240 let rec toplevel v t spos _ =
1241 match t with
1242 | Vdata | Vcdata | Vend -> v
1243 | Vopen ("llppconfig", _, closed) ->
1244 if closed
1245 then v
1246 else { v with f = llppconfig }
1247 | Vopen _ ->
1248 error "unexpected subelement at top level" s spos
1249 | Vclose _ -> error "unexpected close at top level" s spos
1251 and llppconfig v t spos _ =
1252 match t with
1253 | Vdata | Vcdata -> v
1254 | Vend -> error "unexpected end of input in llppconfig" s spos
1255 | Vopen ("defaults", attrs, closed) ->
1256 let c = config_of dc attrs in
1257 setconf dc c;
1258 if closed
1259 then v
1260 else { v with f = defaults }
1262 | Vopen ("ui-font", attrs, closed) ->
1263 let rec getsize size = function
1264 | [] -> size
1265 | ("size", v) :: rest ->
1266 let size =
1267 fromstring int_of_string spos "size" v fstate.fontsize in
1268 getsize size rest
1269 | l -> getsize size l
1271 fstate.fontsize <- getsize fstate.fontsize attrs;
1272 if closed
1273 then v
1274 else { v with f = uifont (Buffer.create 10) }
1276 | Vopen ("doc", attrs, closed) ->
1277 let pathent, spage, srely, span, svisy = doc_of attrs in
1278 let path = unent pathent
1279 and pageno = fromstring int_of_string spos "page" spage 0
1280 and rely = fromstring float_of_string spos "rely" srely 0.0
1281 and pan = fromstring int_of_string spos "pan" span 0
1282 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1283 let c = config_of dc attrs in
1284 let anchor = (pageno, rely, visy) in
1285 if closed
1286 then (Hashtbl.add h path (c, [], pan, anchor); v)
1287 else { v with f = doc path pan anchor c [] }
1289 | Vopen _ ->
1290 error "unexpected subelement in llppconfig" s spos
1292 | Vclose "llppconfig" -> { v with f = toplevel }
1293 | Vclose _ -> error "unexpected close in llppconfig" s spos
1295 and defaults v t spos _ =
1296 match t with
1297 | Vdata | Vcdata -> v
1298 | Vend -> error "unexpected end of input in defaults" s spos
1299 | Vopen ("keymap", attrs, closed) ->
1300 let modename =
1301 try List.assoc "mode" attrs
1302 with Not_found -> "global" in
1303 if closed
1304 then v
1305 else
1306 let ret keymap =
1307 let h = findkeyhash dc modename in
1308 KeyMap.iter (Hashtbl.replace h) keymap;
1309 defaults
1311 { v with f = pkeymap ret KeyMap.empty }
1313 | Vopen (_, _, _) ->
1314 error "unexpected subelement in defaults" s spos
1316 | Vclose "defaults" ->
1317 { v with f = llppconfig }
1319 | Vclose _ -> error "unexpected close in defaults" s spos
1321 and uifont b v t spos epos =
1322 match t with
1323 | Vdata | Vcdata ->
1324 Buffer.add_substring b s spos (epos - spos);
1326 | Vopen (_, _, _) ->
1327 error "unexpected subelement in ui-font" s spos
1328 | Vclose "ui-font" ->
1329 if emptystr !fontpath
1330 then fontpath := Buffer.contents b;
1331 { v with f = llppconfig }
1332 | Vclose _ -> error "unexpected close in ui-font" s spos
1333 | Vend -> error "unexpected end of input in ui-font" s spos
1335 and doc path pan anchor c bookmarks v t spos _ =
1336 match t with
1337 | Vdata | Vcdata -> v
1338 | Vend -> error "unexpected end of input in doc" s spos
1339 | Vopen ("bookmarks", _, closed) ->
1340 if closed
1341 then v
1342 else { v with f = pbookmarks path pan anchor c bookmarks }
1344 | Vopen ("keymap", attrs, closed) ->
1345 let modename =
1346 try List.assoc "mode" attrs
1347 with Not_found -> "global"
1349 if closed
1350 then v
1351 else
1352 let ret keymap =
1353 let h = findkeyhash c modename in
1354 KeyMap.iter (Hashtbl.replace h) keymap;
1355 doc path pan anchor c bookmarks
1357 { v with f = pkeymap ret KeyMap.empty }
1359 | Vopen (_, _, _) ->
1360 error "unexpected subelement in doc" s spos
1362 | Vclose "doc" ->
1363 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
1364 { v with f = llppconfig }
1366 | Vclose _ -> error "unexpected close in doc" s spos
1368 and pkeymap ret keymap v t spos _ =
1369 match t with
1370 | Vdata | Vcdata -> v
1371 | Vend -> error "unexpected end of input in keymap" s spos
1372 | Vopen ("map", attrs, closed) ->
1373 let r, l = map_of attrs in
1374 let kss = fromstring keys_of_string spos "in" r [] in
1375 let lss = fromstring keys_of_string spos "out" l [] in
1376 let keymap =
1377 match kss with
1378 | [] -> keymap
1379 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
1380 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
1382 if closed
1383 then { v with f = pkeymap ret keymap }
1384 else
1385 let f () = v in
1386 { v with f = skip "map" f }
1388 | Vopen _ ->
1389 error "unexpected subelement in keymap" s spos
1391 | Vclose "keymap" ->
1392 { v with f = ret keymap }
1394 | Vclose _ -> error "unexpected close in keymap" s spos
1396 and pbookmarks path pan anchor c bookmarks v t spos _ =
1397 match t with
1398 | Vdata | Vcdata -> v
1399 | Vend -> error "unexpected end of input in bookmarks" s spos
1400 | Vopen ("item", attrs, closed) ->
1401 let titleent, spage, srely, svisy = bookmark_of attrs in
1402 let page = fromstring int_of_string spos "page" spage 0
1403 and rely = fromstring float_of_string spos "rely" srely 0.0
1404 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1405 let bookmarks =
1406 (unent titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
1408 if closed
1409 then { v with f = pbookmarks path pan anchor c bookmarks }
1410 else
1411 let f () = v in
1412 { v with f = skip "item" f }
1414 | Vopen _ ->
1415 error "unexpected subelement in bookmarks" s spos
1417 | Vclose "bookmarks" ->
1418 { v with f = doc path pan anchor c bookmarks }
1420 | Vclose _ -> error "unexpected close in bookmarks" s spos
1422 and skip tag f v t spos _ =
1423 match t with
1424 | Vdata | Vcdata -> v
1425 | Vend ->
1426 error ("unexpected end of input in skipped " ^ tag) s spos
1427 | Vopen (tag', _, closed) ->
1428 if closed
1429 then v
1430 else
1431 let f' () = { v with f = skip tag f } in
1432 { v with f = skip tag' f' }
1433 | Vclose ctag ->
1434 if tag = ctag
1435 then f ()
1436 else error ("unexpected close in skipped " ^ tag) s spos
1439 parse { f = toplevel; accu = () } s;
1440 h, dc;
1443 let do_load f ic =
1445 let len = in_channel_length ic in
1446 let s = String.create len in
1447 really_input ic s 0 len;
1448 f s;
1449 with
1450 | Parse_error (msg, s, pos) ->
1451 let subs = subs s pos in
1452 Utils.error "parse error: %s: at %d [..%s..]" msg pos subs
1454 | exn ->
1455 failwith ("config load error: " ^ exntos exn)
1458 let defconfpath =
1459 let dir =
1461 let dir = Filename.concat home ".config" in
1462 if Sys.is_directory dir then dir else home
1463 with _ -> home
1465 Filename.concat dir "llpp.conf"
1468 let confpath = ref defconfpath;;
1470 let load1 f =
1471 if Sys.file_exists !confpath
1472 then
1473 match
1474 (try Some (open_in_bin !confpath)
1475 with exn ->
1476 prerr_endline
1477 ("Error opening configuration file `" ^ !confpath ^ "': " ^
1478 exntos exn);
1479 None
1481 with
1482 | Some ic ->
1483 let success =
1485 f (do_load get ic)
1486 with exn ->
1487 prerr_endline
1488 ("Error loading configuration from `" ^ !confpath ^ "': " ^
1489 exntos exn);
1490 false
1492 close_in ic;
1493 success
1495 | None -> false
1496 else
1497 f (Hashtbl.create 0, defconf)
1500 let load () =
1501 let f (h, dc) =
1502 let pc, pb, px, pa =
1504 let path =
1505 if emptystr state.origin
1506 then state.path
1507 else state.origin
1509 let absname = abspath path in
1510 Hashtbl.find h absname
1511 with Not_found -> dc, [], 0, emptyanchor
1513 setconf defconf dc;
1514 setconf conf pc;
1515 state.bookmarks <- pb;
1516 state.x <- px;
1517 if conf.jumpback
1518 then state.anchor <- pa;
1519 cbput state.hists.nav pa;
1520 true
1522 load1 f
1525 let gethist listref =
1526 let f (h, _) =
1527 listref :=
1528 Hashtbl.fold (fun path (pc, pb, px, pa) accu ->
1529 (path, pc, pb, px, pa) :: accu)
1530 h [];
1531 true
1533 load1 f
1536 let add_attrs bb always dc c time =
1537 let ob s a b =
1538 if always || a != b
1539 then Printf.bprintf bb "\n %s='%b'" s a
1540 and op s a b =
1541 if always || a <> b
1542 then Printf.bprintf bb "\n %s='%b'" s (a != None)
1543 and oi s a b =
1544 if always || a != b
1545 then Printf.bprintf bb "\n %s='%d'" s a
1546 and oI s a b =
1547 if always || a != b
1548 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
1549 and oz s a b =
1550 if always || a <> b
1551 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
1552 and oF s a b =
1553 if always || a <> b
1554 then Printf.bprintf bb "\n %s='%f'" s a
1555 and oc s a b =
1556 if always || a <> b
1557 then
1558 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
1559 and oC s a b =
1560 if always || a <> b
1561 then
1562 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
1563 and oR s a b =
1564 if always || a <> b
1565 then
1566 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
1567 and os s a b =
1568 if always || a <> b
1569 then
1570 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
1571 and og s a b =
1572 if always || a <> b
1573 then
1574 match a with
1575 | Some (_N, _A, _B) ->
1576 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
1577 | None ->
1578 match b with
1579 | None -> ()
1580 | _ ->
1581 Printf.bprintf bb "\n %s='none'" s
1582 and oW s a b =
1583 if always || a <> b
1584 then
1585 let v =
1586 match a with
1587 | None -> "false"
1588 | Some f ->
1589 if f = infinity
1590 then "true"
1591 else string_of_float f
1593 Printf.bprintf bb "\n %s='%s'" s v
1594 and oco s a b =
1595 if always || a <> b
1596 then
1597 match a with
1598 | Cmulti ((n, a, b), _) when n > 1 ->
1599 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
1600 | Csplit (n, _) when n > 1 ->
1601 Printf.bprintf bb "\n %s='%d'" s ~-n
1602 | _ -> ()
1603 and obeco s a b =
1604 if always || a <> b
1605 then
1606 match a with
1607 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
1608 | _ -> ()
1609 and oFm s a b =
1610 if always || a <> b
1611 then
1612 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
1613 and oSv s a b m =
1614 if always || a <> b
1615 then
1616 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
1617 and oPm s a b =
1618 if always || a <> b
1619 then
1620 Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
1622 oi "width" c.cwinw dc.cwinw;
1623 oi "height" c.cwinh dc.cwinh;
1624 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
1625 oi "scroll-handle-height" c.scrollh dc.scrollh;
1626 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
1627 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
1628 ob "case-insensitive-search" c.icase dc.icase;
1629 ob "preload" c.preload dc.preload;
1630 oi "page-bias" c.pagebias dc.pagebias;
1631 oi "scroll-step" c.scrollstep dc.scrollstep;
1632 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
1633 ob "max-height-fit" c.maxhfit dc.maxhfit;
1634 ob "crop-hack" c.crophack dc.crophack;
1635 oW "throttle" c.maxwait dc.maxwait;
1636 ob "highlight-links" c.hlinks dc.hlinks;
1637 ob "under-cursor-info" c.underinfo dc.underinfo;
1638 oi "vertical-margin" c.interpagespace dc.interpagespace;
1639 oz "zoom" c.zoom dc.zoom;
1640 ob "presentation" c.presentation dc.presentation;
1641 oi "rotation-angle" c.angle dc.angle;
1642 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
1643 oFm "fit-model" c.fitmodel dc.fitmodel;
1644 oI "pixmap-cache-size" c.memlimit dc.memlimit;
1645 oi "tex-count" c.texcount dc.texcount;
1646 oi "slice-height" c.sliceheight dc.sliceheight;
1647 oi "thumbnail-width" c.thumbw dc.thumbw;
1648 ob "persistent-location" c.jumpback dc.jumpback;
1649 oc "background-color" c.bgcolor dc.bgcolor;
1650 oi "tile-width" c.tilew dc.tilew;
1651 oi "tile-height" c.tileh dc.tileh;
1652 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
1653 ob "checkers" c.checkers dc.checkers;
1654 oi "aalevel" c.aalevel dc.aalevel;
1655 ob "trim-margins" c.trimmargins dc.trimmargins;
1656 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
1657 os "uri-launcher" c.urilauncher dc.urilauncher;
1658 os "path-launcher" c.pathlauncher dc.pathlauncher;
1659 oC "color-space" c.colorspace dc.colorspace;
1660 ob "invert-colors" c.invert dc.invert;
1661 oF "brightness" c.colorscale dc.colorscale;
1662 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
1663 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
1664 oco "columns" c.columns dc.columns;
1665 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
1666 os "selection-command" c.selcmd dc.selcmd;
1667 os "synctex-command" c.stcmd dc.stcmd;
1668 os "pax-command" c.paxcmd dc.paxcmd;
1669 ob "update-cursor" c.updatecurs dc.updatecurs;
1670 oi "hint-font-size" c.hfsize dc.hfsize;
1671 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
1672 oF "page-scroll-scale" c.pgscale dc.pgscale;
1673 ob "use-pbo" c.usepbo dc.usepbo;
1674 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
1675 ob "remote-in-a-new-instance" c.riani dc.riani;
1676 op "point-and-x" c.pax dc.pax;
1677 oPm "point-and-x-mark" c.paxmark dc.paxmark;
1678 ob "scroll-bar-on-the-left" c.leftscroll dc.leftscroll;
1679 if not always
1680 then os "title" c.title dc.title;
1681 oz "last-visit" time 0.0;
1684 let keymapsbuf always dc c =
1685 let bb = Buffer.create 16 in
1686 let rec loop = function
1687 | [] -> ()
1688 | (modename, h) :: rest ->
1689 let dh = findkeyhash dc modename in
1690 if always || h <> dh
1691 then (
1692 if Hashtbl.length h > 0
1693 then (
1694 if Buffer.length bb > 0
1695 then Buffer.add_char bb '\n';
1696 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
1697 Hashtbl.iter (fun i o ->
1698 let isdifferent = always ||
1700 let dO = Hashtbl.find dh i in
1701 dO <> o
1702 with Not_found -> true
1704 if isdifferent
1705 then
1706 let addkm (k, m) =
1707 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
1708 if Wsi.withalt m then Buffer.add_string bb "alt-";
1709 if Wsi.withshift m then Buffer.add_string bb "shift-";
1710 if Wsi.withmeta m then Buffer.add_string bb "meta-";
1711 Buffer.add_string bb (Wsi.keyname k);
1713 let addkms l =
1714 let rec loop = function
1715 | [] -> ()
1716 | km :: [] -> addkm km
1717 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
1719 loop l
1721 Buffer.add_string bb "<map in='";
1722 addkm i;
1723 match o with
1724 | KMinsrt km ->
1725 Buffer.add_string bb "' out='";
1726 addkm km;
1727 Buffer.add_string bb "'/>\n"
1729 | KMinsrl kms ->
1730 Buffer.add_string bb "' out='";
1731 addkms kms;
1732 Buffer.add_string bb "'/>\n"
1734 | KMmulti (ins, kms) ->
1735 Buffer.add_char bb ' ';
1736 addkms ins;
1737 Buffer.add_string bb "' out='";
1738 addkms kms;
1739 Buffer.add_string bb "'/>\n"
1740 ) h;
1741 Buffer.add_string bb "</keymap>";
1744 loop rest
1746 loop c.keyhashes;
1750 let save leavebirdseye =
1751 let uifontsize = fstate.fontsize in
1752 let bb = Buffer.create 32768 in
1753 let relx = float state.x /. float state.winw in
1754 let w, h, x =
1755 let cx w = truncate (relx *. float w) in
1756 List.fold_left
1757 (fun (w, h, x) ws ->
1758 match ws with
1759 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
1760 | Wsi.MaxVert -> (w, conf.cwinh, x)
1761 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
1763 (state.winw, state.winh, state.x) state.winstate
1765 conf.cwinw <- w;
1766 conf.cwinh <- h;
1767 let f (h, dc) =
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 _ ->
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 | _ -> x, conf
1881 let docpath = abspath state.path in
1882 adddoc docpath pan (getanchor ())
1884 let autoscrollstep =
1885 match state.autoscroll with
1886 | Some step -> step
1887 | None -> conf.autoscrollstep
1889 begin match state.mode with
1890 | Birdseye beye -> leavebirdseye beye true
1891 | _ -> ()
1892 end;
1893 { conf with autoscrollstep = autoscrollstep }
1895 (if conf.savebmarks then state.bookmarks else [])
1896 (now ());
1898 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
1899 if docpath <> abspath path
1900 then adddoc path x anchor c bookmarks c.lastvisit
1901 ) h;
1902 Buffer.add_string bb "</llppconfig>\n";
1903 true;
1905 if load1 f && Buffer.length bb > 0
1906 then
1908 let tmp = !confpath ^ ".tmp" in
1909 let oc = open_out_bin tmp in
1910 Buffer.output_buffer oc bb;
1911 close_out oc;
1912 Unix.rename tmp !confpath;
1913 with exn ->
1914 prerr_endline
1915 ("error while saving configuration: " ^ exntos exn)