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