Avoid possible confusion
[llpp.git] / config.ml
blob19b92b6686594f832a76e22fab16d662c22e7e26
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 outlinekind =
156 | Onone
157 | Oanchor of anchor
158 | Ouri of uri
159 | Olaunch of launchcommand
160 | Oremote of (filename * pageno)
161 | Oremotedest of (filename * destname)
162 and outline = (caption * outlinelevel * outlinekind)
163 and outlinelevel = int
164 and rect = float * float * float * float * float * float * float * float
165 and infochange = | Memused | Docinfo | Pdim
168 class type uioh = object
169 method display : unit
170 method key : int -> int -> uioh
171 method button : int -> bool -> int -> int -> int -> uioh
172 method multiclick : int -> int -> int -> int -> uioh
173 method motion : int -> int -> uioh
174 method pmotion : int -> int -> uioh
175 method infochanged : infochange -> unit
176 method scrollpw : (int * float * float)
177 method scrollph : (int * float * float)
178 method modehash : keyhash
179 method eformsgs : bool
180 end;;
182 module type TextEnumType =
184 type t
185 val name : string
186 val names : string array
187 end;;
189 module TextEnumMake (Ten : TextEnumType) =
190 struct
191 let names = Ten.names;;
192 let to_int (t : Ten.t) = Obj.magic t;;
193 let to_string t = names.(to_int t);;
194 let of_int n : Ten.t = Obj.magic n;;
195 let of_string s =
196 let rec find i =
197 if i = Array.length names
198 then failwith ("invalid " ^ Ten.name ^ ": " ^ s)
199 else (
200 if Ten.names.(i) = s
201 then of_int i
202 else find (i+1)
204 in find 0;;
205 end;;
207 module CSTE = TextEnumMake (struct
208 type t = colorspace;;
209 let name = "colorspace";;
210 let names = [|"rgb"; "bgr"; "gray"|];;
211 end);;
213 module MTE = TextEnumMake (struct
214 type t = mark;;
215 let name = "mark";;
216 let names = [|"page"; "block"; "line"; "word"|];;
217 end);;
219 module FMTE = TextEnumMake (struct
220 type t = fitmodel;;
221 let name = "fitmodel";;
222 let names = [|"width"; "proportional"; "page"|];;
223 end);;
225 type conf =
226 { mutable scrollbw : int
227 ; mutable scrollh : int
228 ; mutable scrollb : scrollb
229 ; mutable icase : bool
230 ; mutable preload : bool
231 ; mutable pagebias : int
232 ; mutable verbose : bool
233 ; mutable debug : bool
234 ; mutable scrollstep : int
235 ; mutable hscrollstep : int
236 ; mutable maxhfit : bool
237 ; mutable crophack : bool
238 ; mutable autoscrollstep : int
239 ; mutable maxwait : float option
240 ; mutable hlinks : bool
241 ; mutable underinfo : bool
242 ; mutable interpagespace : interpagespace
243 ; mutable zoom : float
244 ; mutable presentation : bool
245 ; mutable angle : angle
246 ; mutable cwinw : int
247 ; mutable cwinh : int
248 ; mutable savebmarks : bool
249 ; mutable fitmodel : fitmodel
250 ; mutable trimmargins : trimmargins
251 ; mutable trimfuzz : irect
252 ; mutable memlimit : memsize
253 ; mutable texcount : texcount
254 ; mutable sliceheight : sliceheight
255 ; mutable thumbw : width
256 ; mutable jumpback : bool
257 ; mutable bgcolor : (float * float * float)
258 ; mutable bedefault : bool
259 ; mutable tilew : int
260 ; mutable tileh : int
261 ; mutable mustoresize : memsize
262 ; mutable checkers : bool
263 ; mutable aalevel : int
264 ; mutable urilauncher : string
265 ; mutable pathlauncher : string
266 ; mutable colorspace : colorspace
267 ; mutable invert : bool
268 ; mutable colorscale : float
269 ; mutable redirectstderr : bool
270 ; mutable ghyllscroll : (int * int * int) option
271 ; mutable columns : columns
272 ; mutable beyecolumns : columncount option
273 ; mutable selcmd : string
274 ; mutable paxcmd : string
275 ; mutable updatecurs : bool
276 ; mutable keyhashes : (string * keyhash) list
277 ; mutable hfsize : int
278 ; mutable pgscale : float
279 ; mutable usepbo : bool
280 ; mutable wheelbypage : bool
281 ; mutable stcmd : string
282 ; mutable riani : bool
283 ; mutable pax : (float * int * int) ref option
284 ; mutable paxmark : mark
285 ; mutable leftscroll : bool
287 and columns =
288 | Csingle of singlecolumn
289 | Cmulti of multicolumns
290 | Csplit of splitcolumns
293 type page =
294 { pageno : int
295 ; pagedimno : int
296 ; pagew : int
297 ; pageh : int
298 ; pagex : int
299 ; pagey : int
300 ; pagevw : int
301 ; pagevh : int
302 ; pagedispx : int
303 ; pagedispy : int
304 ; pagecol : int
308 type tile = opaque * pixmapsize * elapsed
309 and elapsed = float;;
310 type pagemapkey = pageno * gen;;
311 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
312 and row = int
313 and col = int
314 and currently =
315 | Idle
316 | Loading of (page * gen)
317 | Tiling of (
318 page * opaque * colorspace * angle * gen * col * row * width * height
320 | Outlining of outline list
323 type mpos = int * int
324 and mstate =
325 | Msel of (mpos * mpos)
326 | Mpan of mpos
327 | Mscrolly | Mscrollx
328 | Mzoom of (int * int)
329 | Mzoomrect of (mpos * mpos)
330 | Mnone
333 type mode =
334 | Birdseye of (conf * leftx * pageno * pageno * anchor)
335 | Textentry of (textentry * onleave)
336 | View
337 | LinkNav of linktarget
338 and onleave = leavetextentrystatus -> unit
339 and leavetextentrystatus = | Cancel | Confirm
340 and helpitem = string * int * action
341 and action =
342 | Noaction
343 | Action of (uioh -> uioh)
344 and linktarget =
345 | Ltexact of (pageno * int)
346 | Ltgendir of int
347 and textentry = string * string * onhist option * onkey * ondone * cancelonempty
348 and onkey = string -> int -> te
349 and ondone = string -> unit
350 and histcancel = unit -> unit
351 and onhist = ((histcmd -> string) * histcancel)
352 and histcmd = HCnext | HCprev | HCfirst | HClast
353 and cancelonempty = bool
354 and te =
355 | TEstop
356 | TEdone of string
357 | TEcont of string
358 | TEswitch of textentry
361 type 'a circbuf =
362 { store : 'a array
363 ; mutable rc : int
364 ; mutable wc : int
365 ; mutable len : int
369 type state =
370 { mutable sr : Unix.file_descr
371 ; mutable sw : Unix.file_descr
372 ; mutable wsfd : Unix.file_descr
373 ; mutable errfd : Unix.file_descr option
374 ; mutable stderr : Unix.file_descr
375 ; mutable errmsgs : Buffer.t
376 ; mutable newerrmsgs : bool
377 ; mutable w : int
378 ; mutable x : int
379 ; mutable y : int
380 ; mutable anchor : anchor
381 ; mutable ranchors : (string * string * anchor * string) list
382 ; mutable maxy : int
383 ; mutable layout : page list
384 ; pagemap : (pagemapkey, opaque) Hashtbl.t
385 ; tilemap : (tilemapkey, tile) Hashtbl.t
386 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
387 ; mutable pdims : (pageno * width * height * leftx) list
388 ; mutable pagecount : int
389 ; mutable currently : currently
390 ; mutable mstate : mstate
391 ; mutable searchpattern : string
392 ; mutable rects : (pageno * recttype * rect) list
393 ; mutable rects1 : (pageno * recttype * rect) list
394 ; mutable text : string
395 ; mutable winstate : Wsi.winstate list
396 ; mutable mode : mode
397 ; mutable uioh : uioh
398 ; mutable outlines : outline array
399 ; mutable bookmarks : outline list
400 ; mutable path : string
401 ; mutable password : string
402 ; mutable nameddest : string
403 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
404 ; mutable memused : memsize
405 ; mutable gen : gen
406 ; mutable throttle : (page list * int * float) option
407 ; mutable autoscroll : int option
408 ; mutable ghyll : (int option -> unit)
409 ; mutable help : helpitem array
410 ; mutable docinfo : (int * string) list
411 ; mutable checkerstexid : GlTex.texture_id option
412 ; hists : hists
413 ; mutable prevzoom : (float * int)
414 ; mutable progress : float
415 ; mutable redisplay : bool
416 ; mutable mpos : mpos
417 ; mutable keystate : keystate
418 ; mutable glinks : bool
419 ; mutable prevcolumns : (columns * float) option
420 ; mutable winw : int
421 ; mutable winh : int
422 ; mutable reprf : (unit -> unit)
423 ; mutable origin : string
424 ; mutable roam : (unit -> unit)
425 ; mutable bzoom : bool
426 ; mutable traw : [`float] Raw.t
427 ; mutable vraw : [`float] Raw.t
429 and hists =
430 { pat : string circbuf
431 ; pag : string circbuf
432 ; nav : anchor circbuf
433 ; sel : string circbuf
437 let emptyanchor = (0, 0.0, 0.0);;
438 let emptykeyhash = Hashtbl.create 0;;
439 let firstgeomcmds = "", [];;
440 let noghyll _ = ();;
441 let noreprf () = ();;
442 let noroam () = ();;
444 let nouioh : uioh = object (self)
445 method display = ()
446 method key _ _ = self
447 method multiclick _ _ _ _ = self
448 method button _ _ _ _ _ = self
449 method motion _ _ = self
450 method pmotion _ _ = self
451 method infochanged _ = ()
452 method scrollpw = (0, nan, nan)
453 method scrollph = (0, nan, nan)
454 method modehash = emptykeyhash
455 method eformsgs = false
456 end;;
458 let platform_to_string = function
459 | Punknown -> "unknown"
460 | Plinux -> "Linux"
461 | Posx -> "OSX"
462 | Psun -> "Sun"
463 | Pfreebsd -> "FreeBSD"
464 | Pdragonflybsd -> "DragonflyBSD"
465 | Popenbsd -> "OpenBSD"
466 | Pnetbsd -> "NetBSD"
467 | Pcygwin -> "Cygwin"
470 let version () =
471 Printf.sprintf "llpp version %s, fitz %s, ocaml %s (%s/%dbit)"
472 Help.version (fz_version ()) Sys.ocaml_version
473 (platform_to_string platform) 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 | _ -> ""
498 else ""
500 else ""
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 | Pfreebsd | Pdragonflybsd
545 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
546 | Posx -> "open \"%s\""
547 | Pcygwin -> "cygstart \"%s\""
548 | Punknown -> "echo %s")
549 ; pathlauncher = "lp \"%s\""
550 ; selcmd =
551 (match platform with
552 | Plinux | Pfreebsd | Pdragonflybsd
553 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
554 | Posx -> "pbcopy"
555 | Pcygwin -> "wsel"
556 | Punknown -> "cat")
557 ; paxcmd = "cat"
558 ; colorspace = Rgb
559 ; invert = false
560 ; colorscale = 1.0
561 ; redirectstderr = false
562 ; ghyllscroll = None
563 ; columns = Csingle [||]
564 ; beyecolumns = None
565 ; updatecurs = false
566 ; hfsize = 12
567 ; pgscale = 1.0
568 ; usepbo = false
569 ; wheelbypage = false
570 ; stcmd = "echo SyncTex"
571 ; riani = false
572 ; pax = None
573 ; paxmark = Mark_word
574 ; leftscroll = false
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 :: "" :: 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.create 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 { sr = Unix.stdin
672 ; sw = Unix.stdin
673 ; wsfd = Unix.stdin
674 ; errfd = None
675 ; stderr = Unix.stderr
676 ; errmsgs = Buffer.create 0
677 ; newerrmsgs = false
678 ; x = 0
679 ; y = 0
680 ; w = 0
681 ; anchor = emptyanchor
682 ; ranchors = []
683 ; layout = []
684 ; maxy = max_int
685 ; tilelru = Queue.create ()
686 ; pagemap = Hashtbl.create 10
687 ; tilemap = Hashtbl.create 10
688 ; pdims = []
689 ; pagecount = 0
690 ; currently = Idle
691 ; mstate = Mnone
692 ; rects = []
693 ; rects1 = []
694 ; text = ""
695 ; mode = View
696 ; winstate = []
697 ; searchpattern = ""
698 ; outlines = [||]
699 ; bookmarks = []
700 ; path = ""
701 ; password = ""
702 ; nameddest = ""
703 ; geomcmds = firstgeomcmds
704 ; hists =
705 { nav = cbnew 10 emptyanchor
706 ; pat = cbnew 10 ""
707 ; pag = cbnew 10 ""
708 ; sel = cbnew 10 ""
710 ; memused = 0
711 ; gen = 0
712 ; throttle = None
713 ; autoscroll = None
714 ; ghyll = noghyll
715 ; help = makehelp ()
716 ; docinfo = []
717 ; checkerstexid = None
718 ; prevzoom = (1.0, 0)
719 ; progress = -1.0
720 ; uioh = nouioh
721 ; redisplay = true
722 ; mpos = (-1, -1)
723 ; keystate = KSnone
724 ; glinks = false
725 ; prevcolumns = None
726 ; winw = -1
727 ; winh = -1
728 ; reprf = noreprf
729 ; origin = ""
730 ; roam = noroam
731 ; bzoom = false
732 ; traw = Raw.create_static `float 8
733 ; vraw = Raw.create_static `float 8
737 let copykeyhashes c =
738 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
741 let calcips h =
742 let d = state.winh - h in
743 max conf.interpagespace ((d + 1) / 2)
746 let rowyh (c, coverA, coverB) b n =
747 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
748 then
749 let _, _, vy, (_, _, h, _) = b.(n) in
750 (vy, h)
751 else
752 let n' = n - coverA in
753 let d = n' mod c in
754 let s = n - d in
755 let e = min state.pagecount (s + c) in
756 let rec find m miny maxh = if m = e then miny, maxh else
757 let _, _, y, (_, _, h, _) = b.(m) in
758 let miny = min miny y in
759 let maxh = max maxh h in
760 find (m+1) miny maxh
761 in find s max_int 0
764 let page_of_y y =
765 let ((c, coverA, coverB) as cl), b =
766 match conf.columns with
767 | Csingle b -> (1, 0, 0), b
768 | Cmulti (c, b) -> c, b
769 | Csplit (_, b) -> (1, 0, 0), b
771 if Array.length b = 0
772 then -1
773 else
774 let rec bsearch nmin nmax =
775 if nmin > nmax
776 then bound nmin 0 (state.pagecount-1)
777 else
778 let n = (nmax + nmin) / 2 in
779 let vy, h = rowyh cl b n in
780 let y0, y1 =
781 if conf.presentation
782 then
783 let ips = calcips h in
784 let y0 = vy - ips in
785 let y1 = vy + h + ips in
786 y0, y1
787 else (
788 if n = 0
789 then 0, vy + h + conf.interpagespace
790 else
791 let y0 = vy - conf.interpagespace in
792 y0, y0 + h + conf.interpagespace
795 if y >= y0 && y < y1
796 then (
797 if c = 1
798 then n
799 else (
800 if n > coverA
801 then
802 if n < state.pagecount - coverB
803 then ((n-coverA)/c)*c + coverA
804 else n
805 else n
808 else (
809 if y > y0
810 then bsearch (n+1) nmax
811 else bsearch nmin (n-1)
814 bsearch 0 (state.pagecount-1);
817 let calcheight () =
818 match conf.columns with
819 | Cmulti ((_, _, _) as cl, b) ->
820 if Array.length b > 0
821 then
822 let y, h = rowyh cl b (Array.length b - 1) in
823 y + h + (if conf.presentation then calcips h else 0)
824 else 0
825 | Csingle b ->
826 if Array.length b > 0
827 then
828 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
829 y + h + (if conf.presentation then calcips h else 0)
830 else 0
831 | Csplit (_, b) ->
832 if Array.length b > 0
833 then
834 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
835 y + h
836 else 0
839 let getpageywh pageno =
840 let pageno = bound pageno 0 (state.pagecount-1) in
841 match conf.columns with
842 | Csingle b ->
843 if Array.length b = 0
844 then 0, 0, 0
845 else
846 let (_, _, y, (_, w, h, _)) = b.(pageno) in
847 let y =
848 if conf.presentation
849 then y - calcips h
850 else y
852 y, w, h
853 | Cmulti (cl, b) ->
854 if Array.length b = 0
855 then 0, 0, 0
856 else
857 let y, h = rowyh cl b pageno in
858 let (_, _, _, (_, w, _, _)) = b.(pageno) in
859 let y =
860 if conf.presentation
861 then y - calcips h
862 else y
864 y, w, h
865 | Csplit (c, b) ->
866 if Array.length b = 0
867 then 0, 0, 0
868 else
869 let n = pageno*c in
870 let (_, _, y, (_, w, h, _)) = b.(n) in
871 y, w / c, h
874 let getpageyh pageno =
875 let y,_,h = getpageywh pageno in
876 y, h;
879 let getpagedim pageno =
880 let rec f ppdim l =
881 match l with
882 | (n, _, _, _) as pdim :: rest ->
883 if n >= pageno
884 then (if n = pageno then pdim else ppdim)
885 else f pdim rest
887 | [] -> ppdim
889 f (-1, -1, -1, -1) state.pdims
892 let getpagey pageno = fst (getpageyh pageno);;
894 let getanchor1 l =
895 let top =
896 let coloff = l.pagecol * l.pageh in
897 float (l.pagey + coloff) /. float l.pageh
899 let dtop =
900 if l.pagedispy = 0
901 then
903 else (
904 if conf.presentation
905 then float l.pagedispy /. float (calcips l.pageh)
906 else float l.pagedispy /. float conf.interpagespace
909 (l.pageno, top, dtop)
912 let getanchor () =
913 match state.layout with
914 | l :: _ -> getanchor1 l
915 | [] ->
916 let n = page_of_y state.y in
917 if n = -1
918 then state.anchor
919 else
920 let y, h = getpageyh n in
921 let dy = y - state.y in
922 let dtop =
923 if conf.presentation
924 then
925 let ips = calcips h in
926 float (dy + ips) /. float ips
927 else
928 float dy /. float conf.interpagespace
930 (n, 0.0, dtop)
933 let fontpath = ref "";;
935 module KeyMap =
936 Map.Make (struct type t = (int * int) let compare = compare end);;
938 open Parser;;
940 let unent s =
941 let l = String.length s in
942 let b = Buffer.create l in
943 unent b s 0 l;
944 Buffer.contents b;
947 let home =
948 try Sys.getenv "HOME"
949 with exn ->
950 prerr_endline
951 ("Can not determine home directory location: " ^ exntos exn);
955 let modifier_of_string = function
956 | "alt" -> Wsi.altmask
957 | "shift" -> Wsi.shiftmask
958 | "ctrl" | "control" -> Wsi.ctrlmask
959 | "meta" -> Wsi.metamask
960 | _ -> 0
963 let key_of_string =
964 let r = Str.regexp "-" in
965 fun s ->
966 let elems = Str.full_split r s in
967 let f n k m =
968 let g s =
969 let m1 = modifier_of_string s in
970 if m1 = 0
971 then (Wsi.namekey s, m)
972 else (k, m lor m1)
973 in function
974 | Str.Delim s when n land 1 = 0 -> g s
975 | Str.Text s -> g s
976 | Str.Delim _ -> (k, m)
978 let rec loop n k m = function
979 | [] -> (k, m)
980 | x :: xs ->
981 let k, m = f n k m x in
982 loop (n+1) k m xs
984 loop 0 0 0 elems
987 let keys_of_string =
988 let r = Str.regexp "[ \t]" in
989 fun s ->
990 let elems = Str.split r s in
991 List.map key_of_string elems
994 let config_of c attrs =
995 let apply c k v =
997 match k with
998 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
999 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
1000 | "case-insensitive-search" -> { c with icase = bool_of_string v }
1001 | "preload" -> { c with preload = bool_of_string v }
1002 | "page-bias" -> { c with pagebias = int_of_string v }
1003 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
1004 | "horizontal-scroll-step" ->
1005 { c with hscrollstep = max (int_of_string v) 1 }
1006 | "auto-scroll-step" ->
1007 { c with autoscrollstep = max 0 (int_of_string v) }
1008 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
1009 | "crop-hack" -> { c with crophack = bool_of_string v }
1010 | "throttle" ->
1011 let mw =
1012 match String.lowercase v with
1013 | "true" -> Some infinity
1014 | "false" -> None
1015 | f -> Some (float_of_string f)
1017 { c with maxwait = mw}
1018 | "highlight-links" -> { c with hlinks = bool_of_string v }
1019 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
1020 | "vertical-margin" ->
1021 { c with interpagespace = max 0 (int_of_string v) }
1022 | "zoom" ->
1023 let zoom = float_of_string v /. 100. in
1024 let zoom = max zoom 0.0 in
1025 { c with zoom = zoom }
1026 | "presentation" -> { c with presentation = bool_of_string v }
1027 | "rotation-angle" -> { c with angle = int_of_string v }
1028 | "width" -> { c with cwinw = max 20 (int_of_string v) }
1029 | "height" -> { c with cwinh = max 20 (int_of_string v) }
1030 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
1031 | "proportional-display" ->
1032 let fm =
1033 if bool_of_string v
1034 then FitProportional
1035 else FitWidth
1037 { c with fitmodel = fm }
1038 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
1039 | "pixmap-cache-size" ->
1040 { c with memlimit = max 2 (int_of_string_with_suffix v) }
1041 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
1042 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
1043 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
1044 | "persistent-location" -> { c with jumpback = bool_of_string v }
1045 | "background-color" -> { c with bgcolor = color_of_string v }
1046 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
1047 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
1048 | "mupdf-store-size" ->
1049 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
1050 | "checkers" -> { c with checkers = bool_of_string v }
1051 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
1052 | "trim-margins" -> { c with trimmargins = bool_of_string v }
1053 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
1054 | "uri-launcher" -> { c with urilauncher = unent v }
1055 | "path-launcher" -> { c with pathlauncher = unent v }
1056 | "color-space" -> { c with colorspace = CSTE.of_string v }
1057 | "invert-colors" -> { c with invert = bool_of_string v }
1058 | "brightness" -> { c with colorscale = float_of_string v }
1059 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
1060 | "ghyllscroll" -> { c with ghyllscroll = ghyllscroll_of_string v }
1061 | "columns" ->
1062 let (n, _, _) as nab = multicolumns_of_string v in
1063 if n < 0
1064 then { c with columns = Csplit (-n, [||]) }
1065 else { c with columns = Cmulti (nab, [||]) }
1066 | "birds-eye-columns" ->
1067 { c with beyecolumns = Some (max (int_of_string v) 2) }
1068 | "selection-command" -> { c with selcmd = unent v }
1069 | "synctex-command" -> { c with stcmd = unent v }
1070 | "pax-command" -> { c with paxcmd = unent v }
1071 | "update-cursor" -> { c with updatecurs = bool_of_string v }
1072 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
1073 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
1074 | "use-pbo" -> { c with usepbo = bool_of_string v }
1075 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
1076 | "horizontal-scrollbar-visible" ->
1077 let b =
1078 if bool_of_string v
1079 then c.scrollb lor scrollbhv
1080 else c.scrollb land (lnot scrollbhv)
1082 { c with scrollb = b }
1083 | "vertical-scrollbar-visible" ->
1084 let b =
1085 if bool_of_string v
1086 then c.scrollb lor scrollbvv
1087 else c.scrollb land (lnot scrollbvv)
1089 { c with scrollb = b }
1090 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
1091 | "point-and-x" ->
1092 { c with pax =
1093 if bool_of_string v
1094 then Some (ref (0.0, 0, 0))
1095 else None }
1096 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
1097 | "scroll-bar-on-the-left" -> { c with leftscroll = bool_of_string v }
1098 | _ -> c
1099 with exn ->
1100 prerr_endline ("Error processing attribute (`" ^
1101 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
1104 let rec fold c = function
1105 | [] -> c
1106 | (k, v) :: rest ->
1107 let c = apply c k v in
1108 fold c rest
1110 fold { c with keyhashes = copykeyhashes c } attrs;
1113 let fromstring f pos n v d =
1114 try f v
1115 with exn ->
1116 dolog "Error processing attribute (%S=%S) at %d\n%s"
1117 n v pos (exntos exn)
1122 let bookmark_of attrs =
1123 let rec fold title page rely visy = function
1124 | ("title", v) :: rest -> fold v page rely visy rest
1125 | ("page", v) :: rest -> fold title v rely visy rest
1126 | ("rely", v) :: rest -> fold title page v visy rest
1127 | ("visy", v) :: rest -> fold title page rely v rest
1128 | _ :: rest -> fold title page rely visy rest
1129 | [] -> title, page, rely, visy
1131 fold "invalid" "0" "0" "0" attrs
1134 let doc_of attrs =
1135 let rec fold path page rely pan visy = function
1136 | ("path", v) :: rest -> fold v page rely pan visy rest
1137 | ("page", v) :: rest -> fold path v rely pan visy rest
1138 | ("rely", v) :: rest -> fold path page v pan visy rest
1139 | ("pan", v) :: rest -> fold path page rely v visy rest
1140 | ("visy", v) :: rest -> fold path page rely pan v rest
1141 | _ :: rest -> fold path page rely pan visy rest
1142 | [] -> path, page, rely, pan, visy
1144 fold "" "0" "0" "0" "0" attrs
1147 let map_of attrs =
1148 let rec fold rs ls = function
1149 | ("out", v) :: rest -> fold v ls rest
1150 | ("in", v) :: rest -> fold rs v rest
1151 | _ :: rest -> fold ls rs rest
1152 | [] -> ls, rs
1154 fold "" "" attrs
1157 let setconf dst src =
1158 dst.scrollbw <- src.scrollbw;
1159 dst.scrollh <- src.scrollh;
1160 dst.icase <- src.icase;
1161 dst.preload <- src.preload;
1162 dst.pagebias <- src.pagebias;
1163 dst.verbose <- src.verbose;
1164 dst.scrollstep <- src.scrollstep;
1165 dst.maxhfit <- src.maxhfit;
1166 dst.crophack <- src.crophack;
1167 dst.autoscrollstep <- src.autoscrollstep;
1168 dst.maxwait <- src.maxwait;
1169 dst.hlinks <- src.hlinks;
1170 dst.underinfo <- src.underinfo;
1171 dst.interpagespace <- src.interpagespace;
1172 dst.zoom <- src.zoom;
1173 dst.presentation <- src.presentation;
1174 dst.angle <- src.angle;
1175 dst.cwinw <- src.cwinw;
1176 dst.cwinh <- src.cwinh;
1177 dst.savebmarks <- src.savebmarks;
1178 dst.memlimit <- src.memlimit;
1179 dst.fitmodel <- src.fitmodel;
1180 dst.texcount <- src.texcount;
1181 dst.sliceheight <- src.sliceheight;
1182 dst.thumbw <- src.thumbw;
1183 dst.jumpback <- src.jumpback;
1184 dst.bgcolor <- src.bgcolor;
1185 dst.tilew <- src.tilew;
1186 dst.tileh <- src.tileh;
1187 dst.mustoresize <- src.mustoresize;
1188 dst.checkers <- src.checkers;
1189 dst.aalevel <- src.aalevel;
1190 dst.trimmargins <- src.trimmargins;
1191 dst.trimfuzz <- src.trimfuzz;
1192 dst.urilauncher <- src.urilauncher;
1193 dst.colorspace <- src.colorspace;
1194 dst.invert <- src.invert;
1195 dst.colorscale <- src.colorscale;
1196 dst.redirectstderr <- src.redirectstderr;
1197 dst.ghyllscroll <- src.ghyllscroll;
1198 dst.columns <- src.columns;
1199 dst.beyecolumns <- src.beyecolumns;
1200 dst.selcmd <- src.selcmd;
1201 dst.updatecurs <- src.updatecurs;
1202 dst.pathlauncher <- src.pathlauncher;
1203 dst.keyhashes <- copykeyhashes src;
1204 dst.hfsize <- src.hfsize;
1205 dst.hscrollstep <- src.hscrollstep;
1206 dst.pgscale <- src.pgscale;
1207 dst.usepbo <- src.usepbo;
1208 dst.wheelbypage <- src.wheelbypage;
1209 dst.stcmd <- src.stcmd;
1210 dst.paxcmd <- src.paxcmd;
1211 dst.scrollb <- src.scrollb;
1212 dst.riani <- src.riani;
1213 dst.paxmark <- src.paxmark;
1214 dst.leftscroll <- src.leftscroll;
1215 dst.pax <-
1216 if src.pax = None
1217 then None
1218 else Some ((ref (0.0, 0, 0)));
1221 let findkeyhash c name =
1222 try List.assoc name c.keyhashes
1223 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
1226 let get s =
1227 let h = Hashtbl.create 10 in
1228 let dc = { defconf with angle = defconf.angle } in
1229 let rec toplevel v t spos _ =
1230 match t with
1231 | Vdata | Vcdata | Vend -> v
1232 | Vopen ("llppconfig", _, closed) ->
1233 if closed
1234 then v
1235 else { v with f = llppconfig }
1236 | Vopen _ ->
1237 error "unexpected subelement at top level" s spos
1238 | Vclose _ -> error "unexpected close at top level" s spos
1240 and llppconfig v t spos _ =
1241 match t with
1242 | Vdata | Vcdata -> v
1243 | Vend -> error "unexpected end of input in llppconfig" s spos
1244 | Vopen ("defaults", attrs, closed) ->
1245 let c = config_of dc attrs in
1246 setconf dc c;
1247 if closed
1248 then v
1249 else { v with f = defaults }
1251 | Vopen ("ui-font", attrs, closed) ->
1252 let rec getsize size = function
1253 | [] -> size
1254 | ("size", v) :: rest ->
1255 let size =
1256 fromstring int_of_string spos "size" v fstate.fontsize in
1257 getsize size rest
1258 | l -> getsize size l
1260 fstate.fontsize <- getsize fstate.fontsize attrs;
1261 if closed
1262 then v
1263 else { v with f = uifont (Buffer.create 10) }
1265 | Vopen ("doc", attrs, closed) ->
1266 let pathent, spage, srely, span, svisy = doc_of attrs in
1267 let path = unent pathent
1268 and pageno = fromstring int_of_string spos "page" spage 0
1269 and rely = fromstring float_of_string spos "rely" srely 0.0
1270 and pan = fromstring int_of_string spos "pan" span 0
1271 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1272 let c = config_of dc attrs in
1273 let anchor = (pageno, rely, visy) in
1274 if closed
1275 then (Hashtbl.add h path (c, [], pan, anchor); v)
1276 else { v with f = doc path pan anchor c [] }
1278 | Vopen _ ->
1279 error "unexpected subelement in llppconfig" s spos
1281 | Vclose "llppconfig" -> { v with f = toplevel }
1282 | Vclose _ -> error "unexpected close in llppconfig" s spos
1284 and defaults v t spos _ =
1285 match t with
1286 | Vdata | Vcdata -> v
1287 | Vend -> error "unexpected end of input in defaults" s spos
1288 | Vopen ("keymap", attrs, closed) ->
1289 let modename =
1290 try List.assoc "mode" attrs
1291 with Not_found -> "global" in
1292 if closed
1293 then v
1294 else
1295 let ret keymap =
1296 let h = findkeyhash dc modename in
1297 KeyMap.iter (Hashtbl.replace h) keymap;
1298 defaults
1300 { v with f = pkeymap ret KeyMap.empty }
1302 | Vopen (_, _, _) ->
1303 error "unexpected subelement in defaults" s spos
1305 | Vclose "defaults" ->
1306 { v with f = llppconfig }
1308 | Vclose _ -> error "unexpected close in defaults" s spos
1310 and uifont b v t spos epos =
1311 match t with
1312 | Vdata | Vcdata ->
1313 Buffer.add_substring b s spos (epos - spos);
1315 | Vopen (_, _, _) ->
1316 error "unexpected subelement in ui-font" s spos
1317 | Vclose "ui-font" ->
1318 if emptystr !fontpath
1319 then fontpath := Buffer.contents b;
1320 { v with f = llppconfig }
1321 | Vclose _ -> error "unexpected close in ui-font" s spos
1322 | Vend -> error "unexpected end of input in ui-font" s spos
1324 and doc path pan anchor c bookmarks v t spos _ =
1325 match t with
1326 | Vdata | Vcdata -> v
1327 | Vend -> error "unexpected end of input in doc" s spos
1328 | Vopen ("bookmarks", _, closed) ->
1329 if closed
1330 then v
1331 else { v with f = pbookmarks path pan anchor c bookmarks }
1333 | Vopen ("keymap", attrs, closed) ->
1334 let modename =
1335 try List.assoc "mode" attrs
1336 with Not_found -> "global"
1338 if closed
1339 then v
1340 else
1341 let ret keymap =
1342 let h = findkeyhash c modename in
1343 KeyMap.iter (Hashtbl.replace h) keymap;
1344 doc path pan anchor c bookmarks
1346 { v with f = pkeymap ret KeyMap.empty }
1348 | Vopen (_, _, _) ->
1349 error "unexpected subelement in doc" s spos
1351 | Vclose "doc" ->
1352 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
1353 { v with f = llppconfig }
1355 | Vclose _ -> error "unexpected close in doc" s spos
1357 and pkeymap ret keymap v t spos _ =
1358 match t with
1359 | Vdata | Vcdata -> v
1360 | Vend -> error "unexpected end of input in keymap" s spos
1361 | Vopen ("map", attrs, closed) ->
1362 let r, l = map_of attrs in
1363 let kss = fromstring keys_of_string spos "in" r [] in
1364 let lss = fromstring keys_of_string spos "out" l [] in
1365 let keymap =
1366 match kss with
1367 | [] -> keymap
1368 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
1369 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
1371 if closed
1372 then { v with f = pkeymap ret keymap }
1373 else
1374 let f () = v in
1375 { v with f = skip "map" f }
1377 | Vopen _ ->
1378 error "unexpected subelement in keymap" s spos
1380 | Vclose "keymap" ->
1381 { v with f = ret keymap }
1383 | Vclose _ -> error "unexpected close in keymap" s spos
1385 and pbookmarks path pan anchor c bookmarks v t spos _ =
1386 match t with
1387 | Vdata | Vcdata -> v
1388 | Vend -> error "unexpected end of input in bookmarks" s spos
1389 | Vopen ("item", attrs, closed) ->
1390 let titleent, spage, srely, svisy = bookmark_of attrs in
1391 let page = fromstring int_of_string spos "page" spage 0
1392 and rely = fromstring float_of_string spos "rely" srely 0.0
1393 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1394 let bookmarks =
1395 (unent titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
1397 if closed
1398 then { v with f = pbookmarks path pan anchor c bookmarks }
1399 else
1400 let f () = v in
1401 { v with f = skip "item" f }
1403 | Vopen _ ->
1404 error "unexpected subelement in bookmarks" s spos
1406 | Vclose "bookmarks" ->
1407 { v with f = doc path pan anchor c bookmarks }
1409 | Vclose _ -> error "unexpected close in bookmarks" s spos
1411 and skip tag f v t spos _ =
1412 match t with
1413 | Vdata | Vcdata -> v
1414 | Vend ->
1415 error ("unexpected end of input in skipped " ^ tag) s spos
1416 | Vopen (tag', _, closed) ->
1417 if closed
1418 then v
1419 else
1420 let f' () = { v with f = skip tag f } in
1421 { v with f = skip tag' f' }
1422 | Vclose ctag ->
1423 if tag = ctag
1424 then f ()
1425 else error ("unexpected close in skipped " ^ tag) s spos
1428 parse { f = toplevel; accu = () } s;
1429 h, dc;
1432 let do_load f ic =
1434 let len = in_channel_length ic in
1435 let s = String.create len in
1436 really_input ic s 0 len;
1437 f s;
1438 with
1439 | Parse_error (msg, s, pos) ->
1440 let subs = subs s pos in
1441 Utils.error "parse error: %s: at %d [..%s..]" msg pos subs
1443 | exn ->
1444 failwith ("config load error: " ^ exntos exn)
1447 let defconfpath =
1448 let dir =
1450 let dir = Filename.concat home ".config" in
1451 if Sys.is_directory dir then dir else home
1452 with _ -> home
1454 Filename.concat dir "llpp.conf"
1457 let confpath = ref defconfpath;;
1459 let load1 f =
1460 if Sys.file_exists !confpath
1461 then
1462 match
1463 (try Some (open_in_bin !confpath)
1464 with exn ->
1465 prerr_endline
1466 ("Error opening configuration file `" ^ !confpath ^ "': " ^
1467 exntos exn);
1468 None
1470 with
1471 | Some ic ->
1472 let success =
1474 f (do_load get ic)
1475 with exn ->
1476 prerr_endline
1477 ("Error loading configuration from `" ^ !confpath ^ "': " ^
1478 exntos exn);
1479 false
1481 close_in ic;
1482 success
1484 | None -> false
1485 else
1486 f (Hashtbl.create 0, defconf)
1489 let load () =
1490 let f (h, dc) =
1491 let pc, pb, px, pa =
1493 let key =
1494 if emptystr state.origin
1495 then state.path
1496 else state.origin
1498 Hashtbl.find h (Filename.basename key)
1499 with Not_found -> dc, [], 0, emptyanchor
1501 setconf defconf dc;
1502 setconf conf pc;
1503 state.bookmarks <- pb;
1504 state.x <- px;
1505 if conf.jumpback
1506 then state.anchor <- pa;
1507 cbput state.hists.nav pa;
1508 true
1510 load1 f
1513 let add_attrs bb always dc c =
1514 let ob s a b =
1515 if always || a != b
1516 then Printf.bprintf bb "\n %s='%b'" s a
1517 and op s a b =
1518 if always || a <> b
1519 then Printf.bprintf bb "\n %s='%b'" s (a != None)
1520 and oi s a b =
1521 if always || a != b
1522 then Printf.bprintf bb "\n %s='%d'" s a
1523 and oI s a b =
1524 if always || a != b
1525 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
1526 and oz s a b =
1527 if always || a <> b
1528 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
1529 and oF s a b =
1530 if always || a <> b
1531 then Printf.bprintf bb "\n %s='%f'" s a
1532 and oc s a b =
1533 if always || a <> b
1534 then
1535 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
1536 and oC s a b =
1537 if always || a <> b
1538 then
1539 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
1540 and oR s a b =
1541 if always || a <> b
1542 then
1543 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
1544 and os s a b =
1545 if always || a <> b
1546 then
1547 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
1548 and og s a b =
1549 if always || a <> b
1550 then
1551 match a with
1552 | Some (_N, _A, _B) ->
1553 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
1554 | None ->
1555 match b with
1556 | None -> ()
1557 | _ ->
1558 Printf.bprintf bb "\n %s='none'" s
1559 and oW s a b =
1560 if always || a <> b
1561 then
1562 let v =
1563 match a with
1564 | None -> "false"
1565 | Some f ->
1566 if f = infinity
1567 then "true"
1568 else string_of_float f
1570 Printf.bprintf bb "\n %s='%s'" s v
1571 and oco s a b =
1572 if always || a <> b
1573 then
1574 match a with
1575 | Cmulti ((n, a, b), _) when n > 1 ->
1576 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
1577 | Csplit (n, _) when n > 1 ->
1578 Printf.bprintf bb "\n %s='%d'" s ~-n
1579 | _ -> ()
1580 and obeco s a b =
1581 if always || a <> b
1582 then
1583 match a with
1584 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
1585 | _ -> ()
1586 and oFm s a b =
1587 if always || a <> b
1588 then
1589 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
1590 and oSv s a b m =
1591 if always || a <> b
1592 then
1593 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
1594 and oPm s a b =
1595 if always || a <> b
1596 then
1597 Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
1599 oi "width" c.cwinw dc.cwinw;
1600 oi "height" c.cwinh dc.cwinh;
1601 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
1602 oi "scroll-handle-height" c.scrollh dc.scrollh;
1603 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
1604 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
1605 ob "case-insensitive-search" c.icase dc.icase;
1606 ob "preload" c.preload dc.preload;
1607 oi "page-bias" c.pagebias dc.pagebias;
1608 oi "scroll-step" c.scrollstep dc.scrollstep;
1609 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
1610 ob "max-height-fit" c.maxhfit dc.maxhfit;
1611 ob "crop-hack" c.crophack dc.crophack;
1612 oW "throttle" c.maxwait dc.maxwait;
1613 ob "highlight-links" c.hlinks dc.hlinks;
1614 ob "under-cursor-info" c.underinfo dc.underinfo;
1615 oi "vertical-margin" c.interpagespace dc.interpagespace;
1616 oz "zoom" c.zoom dc.zoom;
1617 ob "presentation" c.presentation dc.presentation;
1618 oi "rotation-angle" c.angle dc.angle;
1619 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
1620 oFm "fit-model" c.fitmodel dc.fitmodel;
1621 oI "pixmap-cache-size" c.memlimit dc.memlimit;
1622 oi "tex-count" c.texcount dc.texcount;
1623 oi "slice-height" c.sliceheight dc.sliceheight;
1624 oi "thumbnail-width" c.thumbw dc.thumbw;
1625 ob "persistent-location" c.jumpback dc.jumpback;
1626 oc "background-color" c.bgcolor dc.bgcolor;
1627 oi "tile-width" c.tilew dc.tilew;
1628 oi "tile-height" c.tileh dc.tileh;
1629 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
1630 ob "checkers" c.checkers dc.checkers;
1631 oi "aalevel" c.aalevel dc.aalevel;
1632 ob "trim-margins" c.trimmargins dc.trimmargins;
1633 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
1634 os "uri-launcher" c.urilauncher dc.urilauncher;
1635 os "path-launcher" c.pathlauncher dc.pathlauncher;
1636 oC "color-space" c.colorspace dc.colorspace;
1637 ob "invert-colors" c.invert dc.invert;
1638 oF "brightness" c.colorscale dc.colorscale;
1639 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
1640 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
1641 oco "columns" c.columns dc.columns;
1642 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
1643 os "selection-command" c.selcmd dc.selcmd;
1644 os "synctex-command" c.stcmd dc.stcmd;
1645 os "pax-command" c.paxcmd dc.paxcmd;
1646 ob "update-cursor" c.updatecurs dc.updatecurs;
1647 oi "hint-font-size" c.hfsize dc.hfsize;
1648 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
1649 oF "page-scroll-scale" c.pgscale dc.pgscale;
1650 ob "use-pbo" c.usepbo dc.usepbo;
1651 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
1652 ob "remote-in-a-new-instance" c.riani dc.riani;
1653 op "point-and-x" c.pax dc.pax;
1654 oPm "point-and-x-mark" c.paxmark dc.paxmark;
1655 ob "scroll-bar-on-the-left" c.leftscroll dc.leftscroll;
1658 let keymapsbuf always dc c =
1659 let bb = Buffer.create 16 in
1660 let rec loop = function
1661 | [] -> ()
1662 | (modename, h) :: rest ->
1663 let dh = findkeyhash dc modename in
1664 if always || h <> dh
1665 then (
1666 if Hashtbl.length h > 0
1667 then (
1668 if Buffer.length bb > 0
1669 then Buffer.add_char bb '\n';
1670 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
1671 Hashtbl.iter (fun i o ->
1672 let isdifferent = always ||
1674 let dO = Hashtbl.find dh i in
1675 dO <> o
1676 with Not_found -> true
1678 if isdifferent
1679 then
1680 let addkm (k, m) =
1681 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
1682 if Wsi.withalt m then Buffer.add_string bb "alt-";
1683 if Wsi.withshift m then Buffer.add_string bb "shift-";
1684 if Wsi.withmeta m then Buffer.add_string bb "meta-";
1685 Buffer.add_string bb (Wsi.keyname k);
1687 let addkms l =
1688 let rec loop = function
1689 | [] -> ()
1690 | km :: [] -> addkm km
1691 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
1693 loop l
1695 Buffer.add_string bb "<map in='";
1696 addkm i;
1697 match o with
1698 | KMinsrt km ->
1699 Buffer.add_string bb "' out='";
1700 addkm km;
1701 Buffer.add_string bb "'/>\n"
1703 | KMinsrl kms ->
1704 Buffer.add_string bb "' out='";
1705 addkms kms;
1706 Buffer.add_string bb "'/>\n"
1708 | KMmulti (ins, kms) ->
1709 Buffer.add_char bb ' ';
1710 addkms ins;
1711 Buffer.add_string bb "' out='";
1712 addkms kms;
1713 Buffer.add_string bb "'/>\n"
1714 ) h;
1715 Buffer.add_string bb "</keymap>";
1718 loop rest
1720 loop c.keyhashes;
1724 let save leavebirdseye =
1725 let uifontsize = fstate.fontsize in
1726 let bb = Buffer.create 32768 in
1727 let relx = float state.x /. float state.winw in
1728 let w, h, x =
1729 let cx w = truncate (relx *. float w) in
1730 List.fold_left
1731 (fun (w, h, x) ws ->
1732 match ws with
1733 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
1734 | Wsi.MaxVert -> (w, conf.cwinh, x)
1735 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
1737 (state.winw, state.winh, state.x) state.winstate
1739 conf.cwinw <- w;
1740 conf.cwinh <- h;
1741 let f (h, dc) =
1742 let dc = if conf.bedefault then conf else dc in
1743 Buffer.add_string bb "<llppconfig>\n";
1745 if nonemptystr !fontpath
1746 then
1747 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
1748 uifontsize
1749 !fontpath
1750 else (
1751 if uifontsize <> 14
1752 then
1753 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
1756 Buffer.add_string bb "<defaults";
1757 add_attrs bb true dc dc;
1758 let kb = keymapsbuf true dc dc in
1759 if Buffer.length kb > 0
1760 then (
1761 Buffer.add_string bb ">\n";
1762 Buffer.add_buffer bb kb;
1763 Buffer.add_string bb "\n</defaults>\n";
1765 else Buffer.add_string bb "/>\n";
1767 let adddoc path pan anchor c bookmarks =
1768 if bookmarks == [] && c = dc && anchor = emptyanchor
1769 then ()
1770 else (
1771 Printf.bprintf bb "<doc path='%s'"
1772 (enent path 0 (String.length path));
1774 if anchor <> emptyanchor
1775 then (
1776 let n, rely, visy = anchor in
1777 Printf.bprintf bb " page='%d'" n;
1778 if rely > 1e-6
1779 then
1780 Printf.bprintf bb " rely='%f'" rely
1782 if abs_float visy > 1e-6
1783 then
1784 Printf.bprintf bb " visy='%f'" visy
1788 if pan != 0
1789 then Printf.bprintf bb " pan='%d'" pan;
1791 add_attrs bb false dc c;
1792 let kb = keymapsbuf false dc c in
1794 begin match bookmarks with
1795 | [] ->
1796 if Buffer.length kb > 0
1797 then (
1798 Buffer.add_string bb ">\n";
1799 Buffer.add_buffer bb kb;
1800 Buffer.add_string bb "\n</doc>\n";
1802 else Buffer.add_string bb "/>\n"
1803 | _ ->
1804 Buffer.add_string bb ">\n<bookmarks>\n";
1805 List.iter (fun (title, _, kind) ->
1806 begin match kind with
1807 | Oanchor (page, rely, visy) ->
1808 Printf.bprintf bb
1809 "<item title='%s' page='%d'"
1810 (enent title 0 (String.length title))
1811 page
1813 if rely > 1e-6
1814 then
1815 Printf.bprintf bb " rely='%f'" rely
1817 if abs_float visy > 1e-6
1818 then
1819 Printf.bprintf bb " visy='%f'" visy
1821 | Onone | Ouri _ | Oremote _ | Oremotedest _ | Olaunch _ ->
1822 failwith "unexpected link in bookmarks"
1823 end;
1824 Buffer.add_string bb "/>\n";
1825 ) bookmarks;
1826 Buffer.add_string bb "</bookmarks>";
1827 if Buffer.length kb > 0
1828 then (
1829 Buffer.add_string bb "\n";
1830 Buffer.add_buffer bb kb;
1832 Buffer.add_string bb "\n</doc>\n";
1833 end;
1837 let pan, conf =
1838 match state.mode with
1839 | Birdseye (c, pan, _, _, _) ->
1840 let beyecolumns =
1841 match conf.columns with
1842 | Cmulti ((c, _, _), _) -> Some c
1843 | Csingle _ -> None
1844 | Csplit _ -> None
1845 and columns =
1846 match c.columns with
1847 | Cmulti (c, _) -> Cmulti (c, [||])
1848 | Csingle _ -> Csingle [||]
1849 | Csplit _ -> failwith "quit from bird's eye while split"
1851 pan, { c with beyecolumns = beyecolumns; columns = columns }
1852 | _ -> x, conf
1854 let basename = Filename.basename
1855 (if emptystr state.origin then state.path else state.origin)
1857 adddoc basename pan (getanchor ())
1858 (let autoscrollstep =
1859 match state.autoscroll with
1860 | Some step -> step
1861 | None -> conf.autoscrollstep
1862 in begin match state.mode with
1863 | Birdseye beye -> leavebirdseye beye true
1864 | _ -> ()
1865 end;
1866 { conf with autoscrollstep = autoscrollstep })
1867 (if conf.savebmarks then state.bookmarks else []);
1869 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
1870 if basename <> path
1871 then adddoc path x anchor c bookmarks
1872 ) h;
1873 Buffer.add_string bb "</llppconfig>\n";
1874 true;
1876 if load1 f && Buffer.length bb > 0
1877 then
1879 let tmp = !confpath ^ ".tmp" in
1880 let oc = open_out_bin tmp in
1881 Buffer.output_buffer oc bb;
1882 close_out oc;
1883 Unix.rename tmp !confpath;
1884 with exn ->
1885 prerr_endline
1886 ("error while saving configuration: " ^ exntos exn)