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