Move "button" to the bottom
[llpp.git] / config.ml
blobf3a14d43be7ddbd0032137fe3752187df9555a75
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 * usefontconfig)
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 usefontconfig = bool
113 and uri = string
114 and caption = string
115 and x = int
116 and y = int
117 and tilex = int
118 and tiley = int
119 and tileparams = (x * y * width * height * tilex * tiley)
120 and under =
121 | Unone
122 | Ulinkuri of string
123 | Ulinkgoto of (int * int)
124 | Utext of facename
125 | Uunexpected of string
126 | Ulaunch of launchcommand
127 | Unamed of destname
128 | Uremote of (filename * pageno)
129 | Uremotedest of (filename * destname)
130 | Uannotation of string
131 and facename = string
132 and launchcommand = string
133 and filename = string
134 and pageno = int
135 and destname = string
136 and mark =
137 | Mark_page
138 | Mark_block
139 | Mark_line
140 | Mark_word
141 and link =
142 | Lnotfound
143 | Lfound of int
144 and linkdir =
145 | LDfirst
146 | LDlast
147 | LDfirstvisible of (int * int * int)
148 | LDleft of int
149 | LDright of int
150 | LDdown of int
151 | LDup of int
152 and pagewithlinks =
153 | Pwlnotfound
154 | Pwl of int
155 and scrollb = int
156 and anchor = pageno * top * dtop
157 and rect = float * float * float * float * float * float * float * float
158 and infochange = | Memused | Docinfo | Pdim
161 class type uioh = object
162 method display : unit
163 method key : int -> int -> uioh
164 method button : int -> bool -> int -> int -> int -> uioh
165 method multiclick : int -> int -> int -> int -> uioh
166 method motion : int -> int -> uioh
167 method pmotion : int -> int -> uioh
168 method infochanged : infochange -> unit
169 method scrollpw : (int * float * float)
170 method scrollph : (int * float * float)
171 method modehash : keyhash
172 method eformsgs : bool
173 method alwaysscrolly : bool
174 end;;
176 module type TextEnumType =
178 type t
179 val name : string
180 val names : string array
181 end;;
183 module TextEnumMake (Ten : TextEnumType) =
184 struct
185 let names = Ten.names;;
186 let to_int (t : Ten.t) = Obj.magic t;;
187 let to_string t = names.(to_int t);;
188 let of_int n : Ten.t = Obj.magic n;;
189 let of_string s =
190 let rec find i =
191 if i = Array.length names
192 then failwith ("invalid " ^ Ten.name ^ ": " ^ s)
193 else (
194 if Ten.names.(i) = s
195 then of_int i
196 else find (i+1)
198 in find 0;;
199 end;;
201 module CSTE = TextEnumMake (struct
202 type t = colorspace;;
203 let name = "colorspace";;
204 let names = [|"rgb"; "bgr"; "gray"|];;
205 end);;
207 module MTE = TextEnumMake (struct
208 type t = mark;;
209 let name = "mark";;
210 let names = [|"page"; "block"; "line"; "word"|];;
211 end);;
213 module FMTE = TextEnumMake (struct
214 type t = fitmodel;;
215 let name = "fitmodel";;
216 let names = [|"width"; "proportional"; "page"|];;
217 end);;
219 type conf =
220 { mutable scrollbw : int
221 ; mutable scrollh : int
222 ; mutable scrollb : scrollb
223 ; mutable icase : bool
224 ; mutable preload : bool
225 ; mutable pagebias : int
226 ; mutable verbose : bool
227 ; mutable debug : bool
228 ; mutable scrollstep : int
229 ; mutable hscrollstep : int
230 ; mutable maxhfit : bool
231 ; mutable crophack : bool
232 ; mutable autoscrollstep : int
233 ; mutable maxwait : float option
234 ; mutable hlinks : bool
235 ; mutable underinfo : bool
236 ; mutable interpagespace : interpagespace
237 ; mutable zoom : float
238 ; mutable presentation : bool
239 ; mutable angle : angle
240 ; mutable cwinw : int
241 ; mutable cwinh : int
242 ; mutable savebmarks : bool
243 ; mutable fitmodel : fitmodel
244 ; mutable trimmargins : trimmargins
245 ; mutable trimfuzz : irect
246 ; mutable memlimit : memsize
247 ; mutable texcount : texcount
248 ; mutable sliceheight : sliceheight
249 ; mutable thumbw : width
250 ; mutable jumpback : bool
251 ; mutable bgcolor : (float * float * float)
252 ; mutable bedefault : bool
253 ; mutable tilew : int
254 ; mutable tileh : int
255 ; mutable mustoresize : memsize
256 ; mutable checkers : bool
257 ; mutable aalevel : int
258 ; mutable urilauncher : string
259 ; mutable pathlauncher : string
260 ; mutable colorspace : colorspace
261 ; mutable invert : bool
262 ; mutable colorscale : float
263 ; mutable redirectstderr : bool
264 ; mutable ghyllscroll : (int * int * int) option
265 ; mutable columns : columns
266 ; mutable beyecolumns : columncount option
267 ; mutable selcmd : string
268 ; mutable paxcmd : string
269 ; mutable updatecurs : bool
270 ; mutable keyhashes : (string * keyhash) list
271 ; mutable hfsize : int
272 ; mutable pgscale : float
273 ; mutable usepbo : bool
274 ; mutable wheelbypage : bool
275 ; mutable stcmd : string
276 ; mutable riani : bool
277 ; mutable pax : (float * int * int) ref option
278 ; mutable paxmark : mark
279 ; mutable leftscroll : bool
280 ; mutable title : string
281 ; mutable lastvisit : float
283 and columns =
284 | Csingle of singlecolumn
285 | Cmulti of multicolumns
286 | Csplit of splitcolumns
287 and outlinekind =
288 | Onone
289 | Oanchor of anchor
290 | Ouri of uri
291 | Olaunch of launchcommand
292 | Oremote of (filename * pageno)
293 | Oremotedest of (filename * destname)
294 | Ohistory of (filename * (conf * outline list * x * anchor))
295 | Oaction of (unit -> unit)
296 and outline = (caption * outlinelevel * outlinekind)
297 and outlinelevel = int
300 type page =
301 { pageno : int
302 ; pagedimno : int
303 ; pagew : int
304 ; pageh : int
305 ; pagex : int
306 ; pagey : int
307 ; pagevw : int
308 ; pagevh : int
309 ; pagedispx : int
310 ; pagedispy : int
311 ; pagecol : int
315 type tile = opaque * pixmapsize * elapsed
316 and elapsed = float;;
317 type pagemapkey = pageno * gen;;
318 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
319 and row = int
320 and col = int
321 and currently =
322 | Idle
323 | Loading of (page * gen)
324 | Tiling of (
325 page * opaque * colorspace * angle * gen * col * row * width * height
327 | Outlining of outline list
330 type mpos = int * int
331 and mstate =
332 | Msel of (mpos * mpos)
333 | Mpan of mpos
334 | Mscrolly | Mscrollx
335 | Mzoom of (int * int)
336 | Mzoomrect of (mpos * mpos)
337 | Mnone
340 type mode =
341 | Birdseye of (conf * leftx * pageno * pageno * anchor)
342 | Textentry of (textentry * onleave)
343 | View
344 | LinkNav of linktarget
345 and onleave = leavetextentrystatus -> unit
346 and leavetextentrystatus = | Cancel | Confirm
347 and helpitem = string * int * action
348 and action =
349 | Noaction
350 | Action of (uioh -> uioh)
351 and linktarget =
352 | Ltexact of (pageno * int)
353 | Ltgendir of int
354 and textentry = string * string * onhist option * onkey * ondone * cancelonempty
355 and onkey = string -> int -> te
356 and ondone = string -> unit
357 and histcancel = unit -> unit
358 and onhist = ((histcmd -> string) * histcancel)
359 and histcmd = HCnext | HCprev | HCfirst | HClast
360 and cancelonempty = bool
361 and te =
362 | TEstop
363 | TEdone of string
364 | TEcont of string
365 | TEswitch of textentry
368 type 'a circbuf =
369 { store : 'a array
370 ; mutable rc : int
371 ; mutable wc : int
372 ; mutable len : int
376 type state =
377 { mutable ss : Unix.file_descr
378 ; mutable wsfd : Unix.file_descr
379 ; mutable errfd : Unix.file_descr option
380 ; mutable stderr : Unix.file_descr
381 ; mutable errmsgs : Buffer.t
382 ; mutable newerrmsgs : bool
383 ; mutable w : int
384 ; mutable x : int
385 ; mutable y : int
386 ; mutable anchor : anchor
387 ; mutable ranchors : (string * string * anchor * string) list
388 ; mutable maxy : int
389 ; mutable layout : page list
390 ; pagemap : (pagemapkey, opaque) Hashtbl.t
391 ; tilemap : (tilemapkey, tile) Hashtbl.t
392 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
393 ; mutable pdims : (pageno * width * height * leftx) list
394 ; mutable pagecount : int
395 ; mutable currently : currently
396 ; mutable mstate : mstate
397 ; mutable searchpattern : string
398 ; mutable rects : (pageno * recttype * rect) list
399 ; mutable rects1 : (pageno * recttype * rect) list
400 ; mutable text : string
401 ; mutable winstate : Wsi.winstate list
402 ; mutable mode : mode
403 ; mutable uioh : uioh
404 ; mutable outlines : outline array
405 ; mutable bookmarks : outline list
406 ; mutable path : string
407 ; mutable password : string
408 ; mutable nameddest : string
409 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
410 ; mutable memused : memsize
411 ; mutable gen : gen
412 ; mutable throttle : (page list * int * float) option
413 ; mutable autoscroll : int option
414 ; mutable ghyll : (int option -> unit)
415 ; mutable help : helpitem array
416 ; mutable docinfo : (int * string) list
417 ; mutable checkerstexid : GlTex.texture_id option
418 ; hists : hists
419 ; mutable prevzoom : (float * int)
420 ; mutable progress : float
421 ; mutable redisplay : bool
422 ; mutable mpos : mpos
423 ; mutable keystate : keystate
424 ; mutable glinks : bool
425 ; mutable prevcolumns : (columns * float) option
426 ; mutable winw : int
427 ; mutable winh : int
428 ; mutable reprf : (unit -> unit)
429 ; mutable origin : string
430 ; mutable roam : (unit -> unit)
431 ; mutable bzoom : bool
432 ; mutable traw : [`float] Raw.t
433 ; mutable vraw : [`float] Raw.t
435 and hists =
436 { pat : string circbuf
437 ; pag : string circbuf
438 ; nav : anchor circbuf
439 ; sel : string circbuf
443 let emptyanchor = (0, 0.0, 0.0);;
444 let emptykeyhash = Hashtbl.create 0;;
445 let firstgeomcmds = E.s, [];;
446 let noghyll _ = ();;
447 let noreprf () = ();;
448 let noroam () = ();;
450 let nouioh : uioh = object (self)
451 method display = ()
452 method key _ _ = self
453 method multiclick _ _ _ _ = self
454 method button _ _ _ _ _ = self
455 method motion _ _ = self
456 method pmotion _ _ = self
457 method infochanged _ = ()
458 method scrollpw = (0, nan, nan)
459 method scrollph = (0, nan, nan)
460 method modehash = emptykeyhash
461 method eformsgs = false
462 method alwaysscrolly = false
463 end;;
465 let platform_to_string = function
466 | Punknown -> "unknown"
467 | Plinux -> "Linux"
468 | Posx -> "OSX"
469 | Psun -> "Sun"
470 | Pbsd -> "BSD"
471 | Pcygwin -> "Cygwin"
474 let version () =
475 Printf.sprintf "llpp version %s, fitz %s, ocaml %s/%d bit"
476 Help.version (fz_version ()) Sys.ocaml_version 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 | Psun | Pbsd -> "xdg-open \"%s\""
548 | Posx -> "open \"%s\""
549 | Pcygwin -> "cygstart \"%s\""
550 | Punknown -> "echo %s")
551 ; pathlauncher = "lp \"%s\""
552 ; selcmd =
553 (match platform with
554 | Plinux | Pbsd | Psun -> "xsel -i"
555 | Posx -> "pbcopy"
556 | Pcygwin -> "wsel"
557 | Punknown -> "cat")
558 ; paxcmd = "cat"
559 ; colorspace = Rgb
560 ; invert = false
561 ; colorscale = 1.0
562 ; redirectstderr = false
563 ; ghyllscroll = None
564 ; columns = Csingle [||]
565 ; beyecolumns = None
566 ; updatecurs = false
567 ; hfsize = 12
568 ; pgscale = 1.0
569 ; usepbo = false
570 ; wheelbypage = false
571 ; stcmd = "echo SyncTex"
572 ; riani = false
573 ; pax = None
574 ; paxmark = Mark_word
575 ; leftscroll = false
576 ; title = E.s
577 ; lastvisit = 0.0
578 ; keyhashes =
579 let mk n = (n, Hashtbl.create 1) in
580 [ mk "global"
581 ; mk "info"
582 ; mk "help"
583 ; mk "outline"
584 ; mk "listview"
585 ; mk "birdseye"
586 ; mk "textentry"
587 ; mk "links"
588 ; mk "view"
593 let conf = { defconf with angle = defconf.angle };;
595 let gotouri uri =
596 if emptystr conf.urilauncher
597 then print_endline uri
598 else (
599 let url = geturl uri in
600 if emptystr url
601 then Printf.eprintf "obtained empty url from uri %S\n" uri
602 else
603 let re = Str.regexp "%s" in
604 let command = Str.global_replace re url conf.urilauncher in
605 try popen command []
606 with exn ->
607 Printf.eprintf
608 "failed to execute `%s': %s\n" command (exntos exn);
609 flush stderr;
613 let makehelp () =
614 let strings =
615 version ()
616 :: "(searching in this text works just by typing (i.e. no initial '/'))"
617 :: E.s :: Help.keys
619 Array.of_list (
620 List.map (fun s ->
621 let url = geturl s in
622 if nonemptystr url
623 then (s, 0, Action (fun u -> gotouri url; u))
624 else (s, 0, Noaction)
625 ) strings);
628 let cbnew n v =
629 { store = Array.make n v
630 ; rc = 0
631 ; wc = 0
632 ; len = 0
636 let cbcap b = Array.length b.store;;
638 let cbput b v =
639 let cap = cbcap b in
640 b.store.(b.wc) <- v;
641 b.wc <- (b.wc + 1) mod cap;
642 b.rc <- b.wc;
643 b.len <- min (b.len + 1) cap;
646 let cbempty b = b.len = 0;;
648 let cbgetg b circular dir =
649 if cbempty b
650 then b.store.(0)
651 else
652 let rc = b.rc + dir in
653 let rc =
654 if circular
655 then (
656 if rc = -1
657 then b.len-1
658 else (
659 if rc >= b.len
660 then 0
661 else rc
664 else bound rc 0 (b.len-1)
666 b.rc <- rc;
667 b.store.(rc);
670 let cbget b = cbgetg b false;;
671 let cbgetc b = cbgetg b true;;
673 let state =
674 { ss = Unix.stdin
675 ; wsfd = Unix.stdin
676 ; errfd = None
677 ; stderr = Unix.stderr
678 ; errmsgs = Buffer.create 0
679 ; newerrmsgs = false
680 ; x = 0
681 ; y = 0
682 ; w = 0
683 ; anchor = emptyanchor
684 ; ranchors = []
685 ; layout = []
686 ; maxy = max_int
687 ; tilelru = Queue.create ()
688 ; pagemap = Hashtbl.create 10
689 ; tilemap = Hashtbl.create 10
690 ; pdims = []
691 ; pagecount = 0
692 ; currently = Idle
693 ; mstate = Mnone
694 ; rects = []
695 ; rects1 = []
696 ; text = E.s
697 ; mode = View
698 ; winstate = []
699 ; searchpattern = E.s
700 ; outlines = E.a
701 ; bookmarks = []
702 ; path = E.s
703 ; password = E.s
704 ; nameddest = E.s
705 ; geomcmds = firstgeomcmds
706 ; hists =
707 { nav = cbnew 10 emptyanchor
708 ; pat = cbnew 10 E.s
709 ; pag = cbnew 10 E.s
710 ; sel = cbnew 10 E.s
712 ; memused = 0
713 ; gen = 0
714 ; throttle = None
715 ; autoscroll = None
716 ; ghyll = noghyll
717 ; help = makehelp ()
718 ; docinfo = []
719 ; checkerstexid = None
720 ; prevzoom = (1.0, 0)
721 ; progress = -1.0
722 ; uioh = nouioh
723 ; redisplay = true
724 ; mpos = (-1, -1)
725 ; keystate = KSnone
726 ; glinks = false
727 ; prevcolumns = None
728 ; winw = -1
729 ; winh = -1
730 ; reprf = noreprf
731 ; origin = E.s
732 ; roam = noroam
733 ; bzoom = false
734 ; traw = Raw.create_static `float ~len:8
735 ; vraw = Raw.create_static `float ~len:8
739 let copykeyhashes c =
740 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
743 let calcips h =
744 let d = state.winh - h in
745 max conf.interpagespace ((d + 1) / 2)
748 let rowyh (c, coverA, coverB) b n =
749 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
750 then
751 let _, _, vy, (_, _, h, _) = b.(n) in
752 (vy, h)
753 else
754 let n' = n - coverA in
755 let d = n' mod c in
756 let s = n - d in
757 let e = min state.pagecount (s + c) in
758 let rec find m miny maxh = if m = e then miny, maxh else
759 let _, _, y, (_, _, h, _) = b.(m) in
760 let miny = min miny y in
761 let maxh = max maxh h in
762 find (m+1) miny maxh
763 in find s max_int 0
766 let page_of_y y =
767 let ((c, coverA, coverB) as cl), b =
768 match conf.columns with
769 | Csingle b -> (1, 0, 0), b
770 | Cmulti (c, b) -> c, b
771 | Csplit (_, b) -> (1, 0, 0), b
773 if Array.length b = 0
774 then -1
775 else
776 let rec bsearch nmin nmax =
777 if nmin > nmax
778 then bound nmin 0 (state.pagecount-1)
779 else
780 let n = (nmax + nmin) / 2 in
781 let vy, h = rowyh cl b n in
782 let y0, y1 =
783 if conf.presentation
784 then
785 let ips = calcips h in
786 let y0 = vy - ips in
787 let y1 = vy + h + ips in
788 y0, y1
789 else (
790 if n = 0
791 then 0, vy + h + conf.interpagespace
792 else
793 let y0 = vy - conf.interpagespace in
794 y0, y0 + h + conf.interpagespace
797 if y >= y0 && y < y1
798 then (
799 if c = 1
800 then n
801 else (
802 if n > coverA
803 then
804 if n < state.pagecount - coverB
805 then ((n-coverA)/c)*c + coverA
806 else n
807 else n
810 else (
811 if y > y0
812 then bsearch (n+1) nmax
813 else bsearch nmin (n-1)
816 bsearch 0 (state.pagecount-1);
819 let calcheight () =
820 match conf.columns with
821 | Cmulti ((_, _, _) as cl, b) ->
822 if Array.length b > 0
823 then
824 let y, h = rowyh cl b (Array.length b - 1) in
825 y + h + (if conf.presentation then calcips h else 0)
826 else 0
827 | Csingle b ->
828 if Array.length b > 0
829 then
830 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
831 y + h + (if conf.presentation then calcips h else 0)
832 else 0
833 | Csplit (_, b) ->
834 if Array.length b > 0
835 then
836 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
837 y + h
838 else 0
841 let getpageywh pageno =
842 let pageno = bound pageno 0 (state.pagecount-1) in
843 match conf.columns with
844 | Csingle b ->
845 if Array.length b = 0
846 then 0, 0, 0
847 else
848 let (_, _, y, (_, w, h, _)) = b.(pageno) in
849 let y =
850 if conf.presentation
851 then y - calcips h
852 else y
854 y, w, h
855 | Cmulti (cl, b) ->
856 if Array.length b = 0
857 then 0, 0, 0
858 else
859 let y, h = rowyh cl b pageno in
860 let (_, _, _, (_, w, _, _)) = b.(pageno) in
861 let y =
862 if conf.presentation
863 then y - calcips h
864 else y
866 y, w, h
867 | Csplit (c, b) ->
868 if Array.length b = 0
869 then 0, 0, 0
870 else
871 let n = pageno*c in
872 let (_, _, y, (_, w, h, _)) = b.(n) in
873 y, w / c, h
876 let getpageyh pageno =
877 let y,_,h = getpageywh pageno in
878 y, h;
881 let getpagedim pageno =
882 let rec f ppdim l =
883 match l with
884 | (n, _, _, _) as pdim :: rest ->
885 if n >= pageno
886 then (if n = pageno then pdim else ppdim)
887 else f pdim rest
889 | [] -> ppdim
891 f (-1, -1, -1, -1) state.pdims
894 let getpagey pageno = fst (getpageyh pageno);;
896 let getanchor1 l =
897 let top =
898 let coloff = l.pagecol * l.pageh in
899 float (l.pagey + coloff) /. float l.pageh
901 let dtop =
902 if l.pagedispy = 0
903 then
905 else (
906 if conf.presentation
907 then float l.pagedispy /. float (calcips l.pageh)
908 else float l.pagedispy /. float conf.interpagespace
911 (l.pageno, top, dtop)
914 let getanchor () =
915 match state.layout with
916 | l :: _ -> getanchor1 l
917 | [] ->
918 let n = page_of_y state.y in
919 if n = -1
920 then state.anchor
921 else
922 let y, h = getpageyh n in
923 let dy = y - state.y in
924 let dtop =
925 if conf.presentation
926 then
927 let ips = calcips h in
928 float (dy + ips) /. float ips
929 else
930 float dy /. float conf.interpagespace
932 (n, 0.0, dtop)
935 let fontpath = ref E.s;;
937 type historder = [ `lastvisit | `title | `path | `file ];;
938 let historder : historder ref = ref `lastvisit;;
940 module KeyMap =
941 Map.Make (struct type t = (int * int) let compare = compare end);;
943 open Parser;;
945 let unent s =
946 let l = String.length s in
947 let b = Buffer.create l in
948 unent b s 0 l;
949 Buffer.contents b;
952 let home =
953 try Sys.getenv "HOME"
954 with exn ->
955 prerr_endline
956 ("Can not determine home directory location: " ^ exntos exn);
960 let modifier_of_string = function
961 | "alt" -> Wsi.altmask
962 | "shift" -> Wsi.shiftmask
963 | "ctrl" | "control" -> Wsi.ctrlmask
964 | "meta" -> Wsi.metamask
965 | _ -> 0
968 let key_of_string =
969 let r = Str.regexp "-" in
970 fun s ->
971 let elems = Str.full_split r s in
972 let f n k m =
973 let g s =
974 let m1 = modifier_of_string s in
975 if m1 = 0
976 then (Wsi.namekey s, m)
977 else (k, m lor m1)
978 in function
979 | Str.Delim s when n land 1 = 0 -> g s
980 | Str.Text s -> g s
981 | Str.Delim _ -> (k, m)
983 let rec loop n k m = function
984 | [] -> (k, m)
985 | x :: xs ->
986 let k, m = f n k m x in
987 loop (n+1) k m xs
989 loop 0 0 0 elems
992 let keys_of_string =
993 let r = Str.regexp "[ \t]" in
994 fun s ->
995 let elems = Str.split r s in
996 List.map key_of_string elems
999 let config_of c attrs =
1000 let apply c k v =
1002 match k with
1003 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
1004 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
1005 | "case-insensitive-search" -> { c with icase = bool_of_string v }
1006 | "preload" -> { c with preload = bool_of_string v }
1007 | "page-bias" -> { c with pagebias = int_of_string v }
1008 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
1009 | "horizontal-scroll-step" ->
1010 { c with hscrollstep = max (int_of_string v) 1 }
1011 | "auto-scroll-step" ->
1012 { c with autoscrollstep = max 0 (int_of_string v) }
1013 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
1014 | "crop-hack" -> { c with crophack = bool_of_string v }
1015 | "throttle" ->
1016 let mw =
1017 match String.lowercase v with
1018 | "true" -> Some infinity
1019 | "false" -> None
1020 | f -> Some (float_of_string f)
1022 { c with maxwait = mw}
1023 | "highlight-links" -> { c with hlinks = bool_of_string v }
1024 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
1025 | "vertical-margin" ->
1026 { c with interpagespace = max 0 (int_of_string v) }
1027 | "zoom" ->
1028 let zoom = float_of_string v /. 100. in
1029 let zoom = max zoom 0.0 in
1030 { c with zoom = zoom }
1031 | "presentation" -> { c with presentation = bool_of_string v }
1032 | "rotation-angle" -> { c with angle = int_of_string v }
1033 | "width" -> { c with cwinw = max 20 (int_of_string v) }
1034 | "height" -> { c with cwinh = max 20 (int_of_string v) }
1035 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
1036 | "proportional-display" ->
1037 let fm =
1038 if bool_of_string v
1039 then FitProportional
1040 else FitWidth
1042 { c with fitmodel = fm }
1043 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
1044 | "pixmap-cache-size" ->
1045 { c with memlimit = max 2 (int_of_string_with_suffix v) }
1046 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
1047 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
1048 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
1049 | "persistent-location" -> { c with jumpback = bool_of_string v }
1050 | "background-color" -> { c with bgcolor = color_of_string v }
1051 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
1052 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
1053 | "mupdf-store-size" ->
1054 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
1055 | "checkers" -> { c with checkers = bool_of_string v }
1056 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
1057 | "trim-margins" -> { c with trimmargins = bool_of_string v }
1058 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
1059 | "uri-launcher" -> { c with urilauncher = unent v }
1060 | "path-launcher" -> { c with pathlauncher = unent v }
1061 | "color-space" -> { c with colorspace = CSTE.of_string v }
1062 | "invert-colors" -> { c with invert = bool_of_string v }
1063 | "brightness" -> { c with colorscale = float_of_string v }
1064 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
1065 | "ghyllscroll" -> { c with ghyllscroll = ghyllscroll_of_string v }
1066 | "columns" ->
1067 let (n, _, _) as nab = multicolumns_of_string v in
1068 if n < 0
1069 then { c with columns = Csplit (-n, E.a) }
1070 else { c with columns = Cmulti (nab, E.a) }
1071 | "birds-eye-columns" ->
1072 { c with beyecolumns = Some (max (int_of_string v) 2) }
1073 | "selection-command" -> { c with selcmd = unent v }
1074 | "synctex-command" -> { c with stcmd = unent v }
1075 | "pax-command" -> { c with paxcmd = unent v }
1076 | "update-cursor" -> { c with updatecurs = bool_of_string v }
1077 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
1078 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
1079 | "use-pbo" -> { c with usepbo = bool_of_string v }
1080 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
1081 | "horizontal-scrollbar-visible" ->
1082 let b =
1083 if bool_of_string v
1084 then c.scrollb lor scrollbhv
1085 else c.scrollb land (lnot scrollbhv)
1087 { c with scrollb = b }
1088 | "vertical-scrollbar-visible" ->
1089 let b =
1090 if bool_of_string v
1091 then c.scrollb lor scrollbvv
1092 else c.scrollb land (lnot scrollbvv)
1094 { c with scrollb = b }
1095 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
1096 | "point-and-x" ->
1097 { c with pax =
1098 if bool_of_string v
1099 then Some (ref (0.0, 0, 0))
1100 else None }
1101 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
1102 | "scroll-bar-on-the-left" -> { c with leftscroll = bool_of_string v }
1103 | "title" -> { c with title = unent v }
1104 | "last-visit" -> { c with lastvisit = float_of_string v }
1105 | _ -> c
1106 with exn ->
1107 prerr_endline ("Error processing attribute (`" ^
1108 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
1111 let rec fold c = function
1112 | [] -> c
1113 | (k, v) :: rest ->
1114 let c = apply c k v in
1115 fold c rest
1117 fold { c with keyhashes = copykeyhashes c } attrs;
1120 let fromstring f pos n v d =
1121 try f v
1122 with exn ->
1123 dolog "Error processing attribute (%S=%S) at %d\n%s"
1124 n v pos (exntos exn)
1129 let bookmark_of attrs =
1130 let rec fold title page rely visy = function
1131 | ("title", v) :: rest -> fold v page rely visy rest
1132 | ("page", v) :: rest -> fold title v rely visy rest
1133 | ("rely", v) :: rest -> fold title page v visy rest
1134 | ("visy", v) :: rest -> fold title page rely v rest
1135 | _ :: rest -> fold title page rely visy rest
1136 | [] -> title, page, rely, visy
1138 fold "invalid" "0" "0" "0" attrs
1141 let doc_of attrs =
1142 let rec fold path page rely pan visy = function
1143 | ("path", v) :: rest -> fold v page rely pan visy rest
1144 | ("page", v) :: rest -> fold path v rely pan visy rest
1145 | ("rely", v) :: rest -> fold path page v pan visy rest
1146 | ("pan", v) :: rest -> fold path page rely v visy rest
1147 | ("visy", v) :: rest -> fold path page rely pan v rest
1148 | _ :: rest -> fold path page rely pan visy rest
1149 | [] -> path, page, rely, pan, visy
1151 fold E.s "0" "0" "0" "0" attrs
1154 let map_of attrs =
1155 let rec fold rs ls = function
1156 | ("out", v) :: rest -> fold v ls rest
1157 | ("in", v) :: rest -> fold rs v rest
1158 | _ :: rest -> fold ls rs rest
1159 | [] -> ls, rs
1161 fold E.s E.s attrs
1164 let setconf dst src =
1165 dst.scrollbw <- src.scrollbw;
1166 dst.scrollh <- src.scrollh;
1167 dst.icase <- src.icase;
1168 dst.preload <- src.preload;
1169 dst.pagebias <- src.pagebias;
1170 dst.verbose <- src.verbose;
1171 dst.scrollstep <- src.scrollstep;
1172 dst.maxhfit <- src.maxhfit;
1173 dst.crophack <- src.crophack;
1174 dst.autoscrollstep <- src.autoscrollstep;
1175 dst.maxwait <- src.maxwait;
1176 dst.hlinks <- src.hlinks;
1177 dst.underinfo <- src.underinfo;
1178 dst.interpagespace <- src.interpagespace;
1179 dst.zoom <- src.zoom;
1180 dst.presentation <- src.presentation;
1181 dst.angle <- src.angle;
1182 dst.cwinw <- src.cwinw;
1183 dst.cwinh <- src.cwinh;
1184 dst.savebmarks <- src.savebmarks;
1185 dst.memlimit <- src.memlimit;
1186 dst.fitmodel <- src.fitmodel;
1187 dst.texcount <- src.texcount;
1188 dst.sliceheight <- src.sliceheight;
1189 dst.thumbw <- src.thumbw;
1190 dst.jumpback <- src.jumpback;
1191 dst.bgcolor <- src.bgcolor;
1192 dst.tilew <- src.tilew;
1193 dst.tileh <- src.tileh;
1194 dst.mustoresize <- src.mustoresize;
1195 dst.checkers <- src.checkers;
1196 dst.aalevel <- src.aalevel;
1197 dst.trimmargins <- src.trimmargins;
1198 dst.trimfuzz <- src.trimfuzz;
1199 dst.urilauncher <- src.urilauncher;
1200 dst.colorspace <- src.colorspace;
1201 dst.invert <- src.invert;
1202 dst.colorscale <- src.colorscale;
1203 dst.redirectstderr <- src.redirectstderr;
1204 dst.ghyllscroll <- src.ghyllscroll;
1205 dst.columns <- src.columns;
1206 dst.beyecolumns <- src.beyecolumns;
1207 dst.selcmd <- src.selcmd;
1208 dst.updatecurs <- src.updatecurs;
1209 dst.pathlauncher <- src.pathlauncher;
1210 dst.keyhashes <- copykeyhashes src;
1211 dst.hfsize <- src.hfsize;
1212 dst.hscrollstep <- src.hscrollstep;
1213 dst.pgscale <- src.pgscale;
1214 dst.usepbo <- src.usepbo;
1215 dst.wheelbypage <- src.wheelbypage;
1216 dst.stcmd <- src.stcmd;
1217 dst.paxcmd <- src.paxcmd;
1218 dst.scrollb <- src.scrollb;
1219 dst.riani <- src.riani;
1220 dst.paxmark <- src.paxmark;
1221 dst.leftscroll <- src.leftscroll;
1222 dst.title <- src.title;
1223 dst.pax <-
1224 if src.pax = None
1225 then None
1226 else Some ((ref (0.0, 0, 0)));
1229 let findkeyhash c name =
1230 try List.assoc name c.keyhashes
1231 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
1234 let get s =
1235 let h = Hashtbl.create 10 in
1236 let dc = { defconf with angle = defconf.angle } in
1237 let rec toplevel v t spos _ =
1238 match t with
1239 | Vdata | Vcdata | Vend -> v
1240 | Vopen ("llppconfig", _, closed) ->
1241 if closed
1242 then v
1243 else { v with f = llppconfig }
1244 | Vopen _ ->
1245 error "unexpected subelement at top level" s spos
1246 | Vclose _ -> error "unexpected close at top level" s spos
1248 and llppconfig v t spos _ =
1249 match t with
1250 | Vdata | Vcdata -> v
1251 | Vend -> error "unexpected end of input in llppconfig" s spos
1252 | Vopen ("defaults", attrs, closed) ->
1253 let c = config_of dc attrs in
1254 setconf dc c;
1255 if closed
1256 then v
1257 else { v with f = defaults }
1259 | Vopen ("ui-font", attrs, closed) ->
1260 let rec getsize size = function
1261 | [] -> size
1262 | ("size", v) :: rest ->
1263 let size =
1264 fromstring int_of_string spos "size" v fstate.fontsize in
1265 getsize size rest
1266 | l -> getsize size l
1268 fstate.fontsize <- getsize fstate.fontsize attrs;
1269 if closed
1270 then v
1271 else { v with f = uifont (Buffer.create 10) }
1273 | Vopen ("doc", attrs, closed) ->
1274 let pathent, spage, srely, span, svisy = doc_of attrs in
1275 let path = unent pathent
1276 and pageno = fromstring int_of_string spos "page" spage 0
1277 and rely = fromstring float_of_string spos "rely" srely 0.0
1278 and pan = fromstring int_of_string spos "pan" span 0
1279 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1280 let c = config_of dc attrs in
1281 let anchor = (pageno, rely, visy) in
1282 if closed
1283 then (Hashtbl.add h path (c, [], pan, anchor); v)
1284 else { v with f = doc path pan anchor c [] }
1286 | Vopen _ ->
1287 error "unexpected subelement in llppconfig" s spos
1289 | Vclose "llppconfig" -> { v with f = toplevel }
1290 | Vclose _ -> error "unexpected close in llppconfig" s spos
1292 and defaults v t spos _ =
1293 match t with
1294 | Vdata | Vcdata -> v
1295 | Vend -> error "unexpected end of input in defaults" s spos
1296 | Vopen ("keymap", attrs, closed) ->
1297 let modename =
1298 try List.assoc "mode" attrs
1299 with Not_found -> "global" in
1300 if closed
1301 then v
1302 else
1303 let ret keymap =
1304 let h = findkeyhash dc modename in
1305 KeyMap.iter (Hashtbl.replace h) keymap;
1306 defaults
1308 { v with f = pkeymap ret KeyMap.empty }
1310 | Vopen (_, _, _) ->
1311 error "unexpected subelement in defaults" s spos
1313 | Vclose "defaults" ->
1314 { v with f = llppconfig }
1316 | Vclose _ -> error "unexpected close in defaults" s spos
1318 and uifont b v t spos epos =
1319 match t with
1320 | Vdata | Vcdata ->
1321 Buffer.add_substring b s spos (epos - spos);
1323 | Vopen (_, _, _) ->
1324 error "unexpected subelement in ui-font" s spos
1325 | Vclose "ui-font" ->
1326 if emptystr !fontpath
1327 then fontpath := Buffer.contents b;
1328 { v with f = llppconfig }
1329 | Vclose _ -> error "unexpected close in ui-font" s spos
1330 | Vend -> error "unexpected end of input in ui-font" s spos
1332 and doc path pan anchor c bookmarks v t spos _ =
1333 match t with
1334 | Vdata | Vcdata -> v
1335 | Vend -> error "unexpected end of input in doc" s spos
1336 | Vopen ("bookmarks", _, closed) ->
1337 if closed
1338 then v
1339 else { v with f = pbookmarks path pan anchor c bookmarks }
1341 | Vopen ("keymap", attrs, closed) ->
1342 let modename =
1343 try List.assoc "mode" attrs
1344 with Not_found -> "global"
1346 if closed
1347 then v
1348 else
1349 let ret keymap =
1350 let h = findkeyhash c modename in
1351 KeyMap.iter (Hashtbl.replace h) keymap;
1352 doc path pan anchor c bookmarks
1354 { v with f = pkeymap ret KeyMap.empty }
1356 | Vopen (_, _, _) ->
1357 error "unexpected subelement in doc" s spos
1359 | Vclose "doc" ->
1360 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
1361 { v with f = llppconfig }
1363 | Vclose _ -> error "unexpected close in doc" s spos
1365 and pkeymap ret keymap v t spos _ =
1366 match t with
1367 | Vdata | Vcdata -> v
1368 | Vend -> error "unexpected end of input in keymap" s spos
1369 | Vopen ("map", attrs, closed) ->
1370 let r, l = map_of attrs in
1371 let kss = fromstring keys_of_string spos "in" r [] in
1372 let lss = fromstring keys_of_string spos "out" l [] in
1373 let keymap =
1374 match kss with
1375 | [] -> keymap
1376 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
1377 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
1379 if closed
1380 then { v with f = pkeymap ret keymap }
1381 else
1382 let f () = v in
1383 { v with f = skip "map" f }
1385 | Vopen _ ->
1386 error "unexpected subelement in keymap" s spos
1388 | Vclose "keymap" ->
1389 { v with f = ret keymap }
1391 | Vclose _ -> error "unexpected close in keymap" s spos
1393 and pbookmarks path pan anchor c bookmarks v t spos _ =
1394 match t with
1395 | Vdata | Vcdata -> v
1396 | Vend -> error "unexpected end of input in bookmarks" s spos
1397 | Vopen ("item", attrs, closed) ->
1398 let titleent, spage, srely, svisy = bookmark_of attrs in
1399 let page = fromstring int_of_string spos "page" spage 0
1400 and rely = fromstring float_of_string spos "rely" srely 0.0
1401 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1402 let bookmarks =
1403 (unent titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
1405 if closed
1406 then { v with f = pbookmarks path pan anchor c bookmarks }
1407 else
1408 let f () = v in
1409 { v with f = skip "item" f }
1411 | Vopen _ ->
1412 error "unexpected subelement in bookmarks" s spos
1414 | Vclose "bookmarks" ->
1415 { v with f = doc path pan anchor c bookmarks }
1417 | Vclose _ -> Parser.parse_error "unexpected close in bookmarks" s spos
1419 and skip tag f v t spos _ =
1420 match t with
1421 | Vdata | Vcdata -> v
1422 | Vend ->
1423 Parser.parse_error ("unexpected end of input in skipped " ^ tag) s spos
1424 | Vopen (tag', _, closed) ->
1425 if closed
1426 then v
1427 else
1428 let f' () = { v with f = skip tag f } in
1429 { v with f = skip tag' f' }
1430 | Vclose ctag ->
1431 if tag = ctag
1432 then f ()
1433 else Parser.parse_error ("unexpected close in skipped " ^ tag) s spos
1436 parse { f = toplevel; accu = () } s;
1437 h, dc;
1440 let do_load f ic =
1442 let len = in_channel_length ic in
1443 let s = really_input_string ic len in
1444 f s;
1445 with
1446 | Parse_error (msg, s, pos) ->
1447 let subs = subs s pos in
1448 Utils.error "parse error: %s: at %d [..%s..]" msg pos subs
1450 | exn ->
1451 failwith ("config load error: " ^ exntos exn)
1454 let defconfpath =
1455 let dir =
1456 let xdgconfdir = Utils.getenvwithdef "XDG_CONFIG_HOME" E.s in
1457 if xdgconfdir == E.s
1458 then
1460 let dir = Filename.concat home ".config" in
1461 if Sys.is_directory dir then dir else home
1462 with _ -> home
1463 else xdgconfdir
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 openlast =
1501 let f (h, dc) =
1502 if openlast
1503 then (
1504 let path, _ =
1505 Hashtbl.fold
1506 (fun path (conf, _, _, _) ((_, besttime) as best) ->
1507 if conf.lastvisit > besttime
1508 then (path, conf.lastvisit)
1509 else best)
1511 (state.path, -.infinity)
1513 state.path <- path;
1515 let pc, pb, px, pa =
1517 let path =
1518 if emptystr state.origin
1519 then state.path
1520 else state.origin
1522 let absname = abspath path in
1523 Hashtbl.find h absname
1524 with Not_found -> dc, [], 0, emptyanchor
1526 setconf defconf dc;
1527 setconf conf pc;
1528 state.bookmarks <- pb;
1529 state.x <- px;
1530 if conf.jumpback
1531 then state.anchor <- pa;
1532 cbput state.hists.nav pa;
1533 true
1535 load1 f
1538 let gethist listref =
1539 let f (h, _) =
1540 listref :=
1541 Hashtbl.fold (fun path (pc, pb, px, pa) accu ->
1542 (path, pc, pb, px, pa) :: accu)
1543 h [];
1544 true
1546 load1 f
1549 let add_attrs bb always dc c time =
1550 let ob s a b =
1551 if always || a != b
1552 then Printf.bprintf bb "\n %s='%b'" s a
1553 and op s a b =
1554 if always || a <> b
1555 then Printf.bprintf bb "\n %s='%b'" s (a != None)
1556 and oi s a b =
1557 if always || a != b
1558 then Printf.bprintf bb "\n %s='%d'" s a
1559 and oI s a b =
1560 if always || a != b
1561 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
1562 and oz s a b =
1563 if always || a <> b
1564 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
1565 and oF s a b =
1566 if always || a <> b
1567 then Printf.bprintf bb "\n %s='%f'" s a
1568 and oc s a b =
1569 if always || a <> b
1570 then
1571 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
1572 and oC s a b =
1573 if always || a <> b
1574 then
1575 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
1576 and oR s a b =
1577 if always || a <> b
1578 then
1579 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
1580 and os s a b =
1581 if always || a <> b
1582 then
1583 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
1584 and og s a b =
1585 if always || a <> b
1586 then
1587 match a with
1588 | Some (_N, _A, _B) ->
1589 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
1590 | None ->
1591 match b with
1592 | None -> ()
1593 | _ ->
1594 Printf.bprintf bb "\n %s='none'" s
1595 and oW s a b =
1596 if always || a <> b
1597 then
1598 let v =
1599 match a with
1600 | None -> "false"
1601 | Some f ->
1602 if f = infinity
1603 then "true"
1604 else string_of_float f
1606 Printf.bprintf bb "\n %s='%s'" s v
1607 and oco s a b =
1608 if always || a <> b
1609 then
1610 match a with
1611 | Cmulti ((n, a, b), _) when n > 1 ->
1612 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
1613 | Csplit (n, _) when n > 1 ->
1614 Printf.bprintf bb "\n %s='%d'" s ~-n
1615 | Cmulti _ | Csplit _ | Csingle _ -> ()
1616 and obeco s a b =
1617 if always || a <> b
1618 then
1619 match a with
1620 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
1621 | _ -> ()
1622 and oFm s a b =
1623 if always || a <> b
1624 then
1625 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
1626 and oSv s a b m =
1627 if always || a <> b
1628 then
1629 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
1630 and oPm s a b =
1631 if always || a <> b
1632 then
1633 Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
1635 oi "width" c.cwinw dc.cwinw;
1636 oi "height" c.cwinh dc.cwinh;
1637 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
1638 oi "scroll-handle-height" c.scrollh dc.scrollh;
1639 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
1640 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
1641 ob "case-insensitive-search" c.icase dc.icase;
1642 ob "preload" c.preload dc.preload;
1643 oi "page-bias" c.pagebias dc.pagebias;
1644 oi "scroll-step" c.scrollstep dc.scrollstep;
1645 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
1646 ob "max-height-fit" c.maxhfit dc.maxhfit;
1647 ob "crop-hack" c.crophack dc.crophack;
1648 oW "throttle" c.maxwait dc.maxwait;
1649 ob "highlight-links" c.hlinks dc.hlinks;
1650 ob "under-cursor-info" c.underinfo dc.underinfo;
1651 oi "vertical-margin" c.interpagespace dc.interpagespace;
1652 oz "zoom" c.zoom dc.zoom;
1653 ob "presentation" c.presentation dc.presentation;
1654 oi "rotation-angle" c.angle dc.angle;
1655 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
1656 oFm "fit-model" c.fitmodel dc.fitmodel;
1657 oI "pixmap-cache-size" c.memlimit dc.memlimit;
1658 oi "tex-count" c.texcount dc.texcount;
1659 oi "slice-height" c.sliceheight dc.sliceheight;
1660 oi "thumbnail-width" c.thumbw dc.thumbw;
1661 ob "persistent-location" c.jumpback dc.jumpback;
1662 oc "background-color" c.bgcolor dc.bgcolor;
1663 oi "tile-width" c.tilew dc.tilew;
1664 oi "tile-height" c.tileh dc.tileh;
1665 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
1666 ob "checkers" c.checkers dc.checkers;
1667 oi "aalevel" c.aalevel dc.aalevel;
1668 ob "trim-margins" c.trimmargins dc.trimmargins;
1669 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
1670 os "uri-launcher" c.urilauncher dc.urilauncher;
1671 os "path-launcher" c.pathlauncher dc.pathlauncher;
1672 oC "color-space" c.colorspace dc.colorspace;
1673 ob "invert-colors" c.invert dc.invert;
1674 oF "brightness" c.colorscale dc.colorscale;
1675 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
1676 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
1677 oco "columns" c.columns dc.columns;
1678 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
1679 os "selection-command" c.selcmd dc.selcmd;
1680 os "synctex-command" c.stcmd dc.stcmd;
1681 os "pax-command" c.paxcmd dc.paxcmd;
1682 ob "update-cursor" c.updatecurs dc.updatecurs;
1683 oi "hint-font-size" c.hfsize dc.hfsize;
1684 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
1685 oF "page-scroll-scale" c.pgscale dc.pgscale;
1686 ob "use-pbo" c.usepbo dc.usepbo;
1687 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
1688 ob "remote-in-a-new-instance" c.riani dc.riani;
1689 op "point-and-x" c.pax dc.pax;
1690 oPm "point-and-x-mark" c.paxmark dc.paxmark;
1691 ob "scroll-bar-on-the-left" c.leftscroll dc.leftscroll;
1692 if not always
1693 then os "title" c.title dc.title;
1694 oF "last-visit" (snd (modf time)) 0.0;
1697 let keymapsbuf always dc c =
1698 let bb = Buffer.create 16 in
1699 let rec loop = function
1700 | [] -> ()
1701 | (modename, h) :: rest ->
1702 let dh = findkeyhash dc modename in
1703 if always || h <> dh
1704 then (
1705 if Hashtbl.length h > 0
1706 then (
1707 if Buffer.length bb > 0
1708 then Buffer.add_char bb '\n';
1709 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
1710 Hashtbl.iter (fun i o ->
1711 let isdifferent = always ||
1713 let dO = Hashtbl.find dh i in
1714 dO <> o
1715 with Not_found -> true
1717 if isdifferent
1718 then
1719 let addkm (k, m) =
1720 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
1721 if Wsi.withalt m then Buffer.add_string bb "alt-";
1722 if Wsi.withshift m then Buffer.add_string bb "shift-";
1723 if Wsi.withmeta m then Buffer.add_string bb "meta-";
1724 Buffer.add_string bb (Wsi.keyname k);
1726 let addkms l =
1727 let rec loop = function
1728 | [] -> ()
1729 | km :: [] -> addkm km
1730 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
1732 loop l
1734 Buffer.add_string bb "<map in='";
1735 addkm i;
1736 match o with
1737 | KMinsrt km ->
1738 Buffer.add_string bb "' out='";
1739 addkm km;
1740 Buffer.add_string bb "'/>\n"
1742 | KMinsrl kms ->
1743 Buffer.add_string bb "' out='";
1744 addkms kms;
1745 Buffer.add_string bb "'/>\n"
1747 | KMmulti (ins, kms) ->
1748 Buffer.add_char bb ' ';
1749 addkms ins;
1750 Buffer.add_string bb "' out='";
1751 addkms kms;
1752 Buffer.add_string bb "'/>\n"
1753 ) h;
1754 Buffer.add_string bb "</keymap>";
1757 loop rest
1759 loop c.keyhashes;
1763 let save1 bb leavebirdseye x h dc =
1764 let uifontsize = fstate.fontsize in
1765 let dc = if conf.bedefault then conf else dc in
1766 Buffer.add_string bb "<llppconfig>\n";
1768 if nonemptystr !fontpath
1769 then
1770 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
1771 uifontsize
1772 !fontpath
1773 else (
1774 if uifontsize <> 14
1775 then
1776 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
1779 Buffer.add_string bb "<defaults";
1780 add_attrs bb true dc dc nan;
1781 let kb = keymapsbuf true dc dc in
1782 if Buffer.length kb > 0
1783 then (
1784 Buffer.add_string bb ">\n";
1785 Buffer.add_buffer bb kb;
1786 Buffer.add_string bb "\n</defaults>\n";
1788 else Buffer.add_string bb "/>\n";
1790 let adddoc path pan anchor c bookmarks time =
1791 if bookmarks == [] && c = dc && anchor = emptyanchor
1792 then ()
1793 else (
1794 Printf.bprintf bb "<doc path='%s'"
1795 (enent path 0 (String.length path));
1797 if anchor <> emptyanchor
1798 then (
1799 let n, rely, visy = anchor in
1800 Printf.bprintf bb "\n page='%d'" n;
1801 if rely > 1e-6
1802 then
1803 Printf.bprintf bb " rely='%f'" rely
1805 if abs_float visy > 1e-6
1806 then
1807 Printf.bprintf bb " visy='%f'" visy
1811 if pan != 0
1812 then Printf.bprintf bb " pan='%d'" pan;
1814 add_attrs bb false dc c time;
1815 let kb = keymapsbuf false dc c in
1817 begin match bookmarks with
1818 | [] ->
1819 if Buffer.length kb > 0
1820 then (
1821 Buffer.add_string bb ">\n";
1822 Buffer.add_buffer bb kb;
1823 Buffer.add_string bb "\n</doc>\n";
1825 else Buffer.add_string bb "/>\n"
1826 | _ ->
1827 Buffer.add_string bb ">\n<bookmarks>\n";
1828 List.iter (fun (title, _, kind) ->
1829 begin match kind with
1830 | Oanchor (page, rely, visy) ->
1831 Printf.bprintf bb
1832 "<item title='%s' page='%d'"
1833 (enent title 0 (String.length title))
1834 page
1836 if rely > 1e-6
1837 then
1838 Printf.bprintf bb " rely='%f'" rely
1840 if abs_float visy > 1e-6
1841 then
1842 Printf.bprintf bb " visy='%f'" visy
1844 | Ohistory _ | Onone | Ouri _ | Oremote _
1845 | Oremotedest _ | Olaunch _ | Oaction _ ->
1846 failwith "unexpected link in bookmarks"
1847 end;
1848 Buffer.add_string bb "/>\n";
1849 ) bookmarks;
1850 Buffer.add_string bb "</bookmarks>";
1851 if Buffer.length kb > 0
1852 then (
1853 Buffer.add_string bb "\n";
1854 Buffer.add_buffer bb kb;
1856 Buffer.add_string bb "\n</doc>\n";
1857 end;
1861 let pan, conf =
1862 match state.mode with
1863 | Birdseye (c, pan, _, _, _) ->
1864 let beyecolumns =
1865 match conf.columns with
1866 | Cmulti ((c, _, _), _) -> Some c
1867 | Csingle _ -> None
1868 | Csplit _ -> None
1869 and columns =
1870 match c.columns with
1871 | Cmulti (c, _) -> Cmulti (c, E.a)
1872 | Csingle _ -> Csingle E.a
1873 | Csplit _ -> failwith "quit from bird's eye while split"
1875 pan, { c with beyecolumns = beyecolumns; columns = columns }
1876 | Textentry _
1877 | View
1878 | LinkNav _ -> x, conf
1880 let docpath = abspath state.path in
1881 adddoc docpath pan (getanchor ())
1883 let autoscrollstep =
1884 match state.autoscroll with
1885 | Some step -> step
1886 | None -> conf.autoscrollstep
1888 begin match state.mode with
1889 | Birdseye beye -> leavebirdseye beye true
1890 | Textentry _
1891 | View
1892 | LinkNav _ -> ()
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;
1907 let save leavebirdseye =
1908 let relx = float state.x /. float state.winw in
1909 let w, h, x =
1910 let cx w = truncate (relx *. float w) in
1911 List.fold_left
1912 (fun (w, h, x) ws ->
1913 match ws with
1914 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
1915 | Wsi.MaxVert -> (w, conf.cwinh, x)
1916 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
1918 (state.winw, state.winh, state.x) state.winstate
1920 conf.cwinw <- w;
1921 conf.cwinh <- h;
1922 let bb = Buffer.create 32768 in
1923 let save2 (h, dc) =
1924 save1 bb leavebirdseye x h dc
1926 if load1 save2 && Buffer.length bb > 0
1927 then
1929 let tmp = !confpath ^ ".tmp" in
1930 let oc = open_out_bin tmp in
1931 Buffer.output_buffer oc bb;
1932 close_out oc;
1933 Unix.rename tmp !confpath;
1934 with exn ->
1935 prerr_endline
1936 ("error while saving configuration: " ^ exntos exn)
1939 let gc fdi fdo =
1940 let wr s n =
1941 let n' = Unix.write fdo (Bytes.of_string s) 0 n in
1942 if n != n'
1943 then Utils.error "Unix.write %d = %d" n n'
1945 let rd s n =
1946 try Unix.read fdi s 0 n
1947 with exn -> Utils.error "reading gc pipe %s" (exntos exn) in
1948 let href = ref (Hashtbl.create 0) in
1949 let cref = ref defconf in
1950 let push (h, dc) =
1951 let f path (pc, _pb, _px, _pa) =
1952 let s =
1953 Printf.sprintf "%s\000%ld\000" path (Int32.of_float pc.lastvisit)
1955 wr s (String.length s)
1957 Hashtbl.iter f h;
1958 href := h;
1959 cref := dc;
1960 Unix.shutdown fdo Unix.SHUTDOWN_SEND;
1961 true
1963 ignore (load1 push);
1964 let s =
1965 let b = Buffer.create 32768 in
1966 let s = Bytes.create 4096 in
1967 let rec f () =
1968 let n = rd s (Bytes.length s) in
1969 if n = 0
1970 then Buffer.contents b
1971 else (Buffer.add_subbytes b s 0 n; f ())
1973 f ()
1975 let rec f ppos =
1976 match String.index_from s ppos '\000' with
1977 | zpos1 ->
1978 let zpos2 =
1979 try String.index_from s (zpos1+1) '\000' with Not_found -> -1 in
1980 if zpos2 = -1
1981 then error "Incorrect gc input in (%S) at %d" s zpos1
1982 else
1983 let okey = StringLabels.sub s ~pos:ppos ~len:(zpos1-ppos) in
1984 let nkey = StringLabels.sub s ~pos:(zpos1+1) ~len:(zpos2-zpos1-1) in
1985 if emptystr nkey
1986 then (Hashtbl.remove !href okey; f (zpos2+1))
1987 else
1988 let v =
1989 try Hashtbl.find !href okey
1990 with Not_found -> Utils.error "gc: could not find %S" okey
1992 Hashtbl.remove !href okey;
1993 Hashtbl.replace !href nkey v;
1994 f (zpos2+1)
1995 | exception Not_found -> ()
1997 f 0;
1998 let bb = Buffer.create 32768 in
1999 let save2 (_h, dc) = save1 bb (fun _ _ -> ()) 0 !href dc in
2000 if load1 save2 && Buffer.length bb > 0
2001 then (
2003 let tmp = !confpath ^ ".tmp" in
2004 let oc = open_out_bin tmp in
2005 Buffer.output_buffer oc bb;
2006 close_out oc;
2007 Unix.rename tmp !confpath;
2008 with exn ->
2009 prerr_endline
2010 ("error while saving configuration: " ^ exntos exn)