Kill redundant method
[llpp.git] / config.ml
blob96da7e019410b4e15826e231941bfaf378e6e66c
1 open Utils;;
3 external fz_version : unit -> string = "ml_fz_version";;
5 type fontstate =
6 { mutable fontsize : int
7 ; mutable wwidth : float
8 ; mutable maxrows : int
12 let fstate =
13 { fontsize = 14
14 ; wwidth = nan
15 ; maxrows = -1
19 let scrollbvv = 1;;
20 let scrollbhv = 2;;
21 let fastghyllscroll = (5,1,2);;
22 let neatghyllscroll = (10,1,9);;
24 let irect_of_string s =
25 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
28 let irect_to_string (x0,y0,x1,y1) =
29 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
32 let ghyllscroll_of_string s =
33 match s with
34 | "fast" -> Some fastghyllscroll
35 | "neat" -> Some (10,1,9)
36 | "" | "none" -> None
37 | _ ->
38 let (n,a,b) as nab =
39 Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b) in
40 if n <= a || n <= b || a >= b
41 then error "invalid ghyll N(%d),A(%d),B(%d) (N <= A, A < B, N <= B)"
42 n a b;
43 Some nab
46 let ghyllscroll_to_string ((n, a, b) as nab) =
47 (**) if nab = fastghyllscroll then "fast"
48 else if nab = neatghyllscroll then "neat"
49 else Printf.sprintf "%d,%d,%d" n a b;
52 let multicolumns_to_string (n, a, b) =
53 if a = 0 && b = 0
54 then Printf.sprintf "%d" n
55 else Printf.sprintf "%d,%d,%d" n a b;
58 let multicolumns_of_string s =
59 try
60 (int_of_string s, 0, 0)
61 with _ ->
62 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
63 if a > 1 || b > 1
64 then failwith "subtly broken"; (n, a, b)
68 type keymap =
69 | KMinsrt of key
70 | KMinsrl of key list
71 | KMmulti of key list * key list
72 and key = int * int
73 and keyhash = (key, keymap) Hashtbl.t
74 and keystate =
75 | KSnone
76 | KSinto of (key list * key list)
77 and interpagespace = int
78 and multicolumns = multicol * pagegeom
79 and singlecolumn = pagegeom
80 and splitcolumns = columncount * pagegeom
81 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
82 and multicol = columncount * covercount * covercount
83 and pdimno = int
84 and columncount = int
85 and covercount = int
86 and fitmodel = | FitWidth | FitProportional | FitPage
87 and trimmargins = bool
88 and irect = (int * int * int * int)
89 and memsize = int
90 and texcount = int
91 and sliceheight = int
92 and angle = int
93 and params = (angle * fitmodel * trimparams
94 * texcount * sliceheight * memsize
95 * colorspace * fontpath * trimcachepath
96 * haspbo)
97 and width = int
98 and height = int
99 and leftx = int
100 and opaque = Opaque.t
101 and recttype = int
102 and pixmapsize = int
103 and gen = int
104 and top = float
105 and dtop = float
106 and fontpath = string
107 and trimcachepath = string
108 and aalevel = int
109 and trimparams = (trimmargins * irect)
110 and colorspace = | Rgb | Bgr | Gray
111 and haspbo = bool
112 and uri = string
113 and caption = string
114 and x = int
115 and y = int
116 and tilex = int
117 and tiley = int
118 and tileparams = (x * y * width * height * tilex * tiley)
119 and under =
120 | Unone
121 | Ulinkuri of string
122 | Ulinkgoto of (int * int)
123 | Utext of facename
124 | Uunexpected of string
125 | Ulaunch of launchcommand
126 | Unamed of destname
127 | Uremote of (filename * pageno)
128 | Uremotedest of (filename * destname)
129 and facename = string
130 and launchcommand = string
131 and filename = string
132 and pageno = int
133 and destname = string
134 and mark =
135 | Mark_page
136 | Mark_block
137 | Mark_line
138 | Mark_word
139 and link =
140 | Lnotfound
141 | Lfound of int
142 and linkdir =
143 | LDfirst
144 | LDlast
145 | LDfirstvisible of (int * int * int)
146 | LDleft of int
147 | LDright of int
148 | LDdown of int
149 | LDup of int
150 and pagewithlinks =
151 | Pwlnotfound
152 | Pwl of int
153 and scrollb = int
154 and anchor = pageno * top * dtop
155 and rect = float * float * float * float * float * float * float * float
156 and infochange = | Memused | Docinfo | Pdim
159 class type uioh = object
160 method display : unit
161 method key : int -> int -> uioh
162 method button : int -> bool -> int -> int -> int -> uioh
163 method multiclick : int -> int -> int -> int -> uioh
164 method motion : int -> int -> uioh
165 method pmotion : int -> int -> uioh
166 method infochanged : infochange -> unit
167 method scrollpw : (int * float * float)
168 method scrollph : (int * float * float)
169 method modehash : keyhash
170 method eformsgs : bool
171 end;;
173 module type TextEnumType =
175 type t
176 val name : string
177 val names : string array
178 end;;
180 module TextEnumMake (Ten : TextEnumType) =
181 struct
182 let names = Ten.names;;
183 let to_int (t : Ten.t) = Obj.magic t;;
184 let to_string t = names.(to_int t);;
185 let of_int n : Ten.t = Obj.magic n;;
186 let of_string s =
187 let rec find i =
188 if i = Array.length names
189 then failwith ("invalid " ^ Ten.name ^ ": " ^ s)
190 else (
191 if Ten.names.(i) = s
192 then of_int i
193 else find (i+1)
195 in find 0;;
196 end;;
198 module CSTE = TextEnumMake (struct
199 type t = colorspace;;
200 let name = "colorspace";;
201 let names = [|"rgb"; "bgr"; "gray"|];;
202 end);;
204 module MTE = TextEnumMake (struct
205 type t = mark;;
206 let name = "mark";;
207 let names = [|"page"; "block"; "line"; "word"|];;
208 end);;
210 module FMTE = TextEnumMake (struct
211 type t = fitmodel;;
212 let name = "fitmodel";;
213 let names = [|"width"; "proportional"; "page"|];;
214 end);;
216 type conf =
217 { mutable scrollbw : int
218 ; mutable scrollh : int
219 ; mutable scrollb : scrollb
220 ; mutable icase : bool
221 ; mutable preload : bool
222 ; mutable pagebias : int
223 ; mutable verbose : bool
224 ; mutable debug : bool
225 ; mutable scrollstep : int
226 ; mutable hscrollstep : int
227 ; mutable maxhfit : bool
228 ; mutable crophack : bool
229 ; mutable autoscrollstep : int
230 ; mutable maxwait : float option
231 ; mutable hlinks : bool
232 ; mutable underinfo : bool
233 ; mutable interpagespace : interpagespace
234 ; mutable zoom : float
235 ; mutable presentation : bool
236 ; mutable angle : angle
237 ; mutable cwinw : int
238 ; mutable cwinh : int
239 ; mutable savebmarks : bool
240 ; mutable fitmodel : fitmodel
241 ; mutable trimmargins : trimmargins
242 ; mutable trimfuzz : irect
243 ; mutable memlimit : memsize
244 ; mutable texcount : texcount
245 ; mutable sliceheight : sliceheight
246 ; mutable thumbw : width
247 ; mutable jumpback : bool
248 ; mutable bgcolor : (float * float * float)
249 ; mutable bedefault : bool
250 ; mutable tilew : int
251 ; mutable tileh : int
252 ; mutable mustoresize : memsize
253 ; mutable checkers : bool
254 ; mutable aalevel : int
255 ; mutable urilauncher : string
256 ; mutable pathlauncher : string
257 ; mutable colorspace : colorspace
258 ; mutable invert : bool
259 ; mutable colorscale : float
260 ; mutable redirectstderr : bool
261 ; mutable ghyllscroll : (int * int * int) option
262 ; mutable columns : columns
263 ; mutable beyecolumns : columncount option
264 ; mutable selcmd : string
265 ; mutable paxcmd : string
266 ; mutable updatecurs : bool
267 ; mutable keyhashes : (string * keyhash) list
268 ; mutable hfsize : int
269 ; mutable pgscale : float
270 ; mutable usepbo : bool
271 ; mutable wheelbypage : bool
272 ; mutable stcmd : string
273 ; mutable riani : bool
274 ; mutable pax : (float * int * int) ref option
275 ; mutable paxmark : mark
276 ; mutable leftscroll : bool
277 ; mutable title : string
279 and columns =
280 | Csingle of singlecolumn
281 | Cmulti of multicolumns
282 | Csplit of splitcolumns
283 and outlinekind =
284 | Onone
285 | Oanchor of anchor
286 | Ouri of uri
287 | Olaunch of launchcommand
288 | Oremote of (filename * pageno)
289 | Oremotedest of (filename * destname)
290 | Ohistory of (filename * (conf * outline list * x * anchor))
291 and outline = (caption * outlinelevel * outlinekind)
292 and outlinelevel = int
295 type page =
296 { pageno : int
297 ; pagedimno : int
298 ; pagew : int
299 ; pageh : int
300 ; pagex : int
301 ; pagey : int
302 ; pagevw : int
303 ; pagevh : int
304 ; pagedispx : int
305 ; pagedispy : int
306 ; pagecol : int
310 type tile = opaque * pixmapsize * elapsed
311 and elapsed = float;;
312 type pagemapkey = pageno * gen;;
313 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
314 and row = int
315 and col = int
316 and currently =
317 | Idle
318 | Loading of (page * gen)
319 | Tiling of (
320 page * opaque * colorspace * angle * gen * col * row * width * height
322 | Outlining of outline list
325 type mpos = int * int
326 and mstate =
327 | Msel of (mpos * mpos)
328 | Mpan of mpos
329 | Mscrolly | Mscrollx
330 | Mzoom of (int * int)
331 | Mzoomrect of (mpos * mpos)
332 | Mnone
335 type mode =
336 | Birdseye of (conf * leftx * pageno * pageno * anchor)
337 | Textentry of (textentry * onleave)
338 | View
339 | LinkNav of linktarget
340 and onleave = leavetextentrystatus -> unit
341 and leavetextentrystatus = | Cancel | Confirm
342 and helpitem = string * int * action
343 and action =
344 | Noaction
345 | Action of (uioh -> uioh)
346 and linktarget =
347 | Ltexact of (pageno * int)
348 | Ltgendir of int
349 and textentry = string * string * onhist option * onkey * ondone * cancelonempty
350 and onkey = string -> int -> te
351 and ondone = string -> unit
352 and histcancel = unit -> unit
353 and onhist = ((histcmd -> string) * histcancel)
354 and histcmd = HCnext | HCprev | HCfirst | HClast
355 and cancelonempty = bool
356 and te =
357 | TEstop
358 | TEdone of string
359 | TEcont of string
360 | TEswitch of textentry
363 type 'a circbuf =
364 { store : 'a array
365 ; mutable rc : int
366 ; mutable wc : int
367 ; mutable len : int
371 type state =
372 { mutable sr : Unix.file_descr
373 ; mutable sw : Unix.file_descr
374 ; mutable wsfd : Unix.file_descr
375 ; mutable errfd : Unix.file_descr option
376 ; mutable stderr : Unix.file_descr
377 ; mutable errmsgs : Buffer.t
378 ; mutable newerrmsgs : bool
379 ; mutable w : int
380 ; mutable x : int
381 ; mutable y : int
382 ; mutable anchor : anchor
383 ; mutable ranchors : (string * string * anchor * string) list
384 ; mutable maxy : int
385 ; mutable layout : page list
386 ; pagemap : (pagemapkey, opaque) Hashtbl.t
387 ; tilemap : (tilemapkey, tile) Hashtbl.t
388 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
389 ; mutable pdims : (pageno * width * height * leftx) list
390 ; mutable pagecount : int
391 ; mutable currently : currently
392 ; mutable mstate : mstate
393 ; mutable searchpattern : string
394 ; mutable rects : (pageno * recttype * rect) list
395 ; mutable rects1 : (pageno * recttype * rect) list
396 ; mutable text : string
397 ; mutable winstate : Wsi.winstate list
398 ; mutable mode : mode
399 ; mutable uioh : uioh
400 ; mutable outlines : outline array
401 ; mutable bookmarks : outline list
402 ; mutable path : string
403 ; mutable password : string
404 ; mutable nameddest : string
405 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
406 ; mutable memused : memsize
407 ; mutable gen : gen
408 ; mutable throttle : (page list * int * float) option
409 ; mutable autoscroll : int option
410 ; mutable ghyll : (int option -> unit)
411 ; mutable help : helpitem array
412 ; mutable docinfo : (int * string) list
413 ; mutable checkerstexid : GlTex.texture_id option
414 ; hists : hists
415 ; mutable prevzoom : (float * int)
416 ; mutable progress : float
417 ; mutable redisplay : bool
418 ; mutable mpos : mpos
419 ; mutable keystate : keystate
420 ; mutable glinks : bool
421 ; mutable prevcolumns : (columns * float) option
422 ; mutable winw : int
423 ; mutable winh : int
424 ; mutable reprf : (unit -> unit)
425 ; mutable origin : string
426 ; mutable roam : (unit -> unit)
427 ; mutable bzoom : bool
428 ; mutable traw : [`float] Raw.t
429 ; mutable vraw : [`float] Raw.t
431 and hists =
432 { pat : string circbuf
433 ; pag : string circbuf
434 ; nav : anchor circbuf
435 ; sel : string circbuf
439 let emptyanchor = (0, 0.0, 0.0);;
440 let emptykeyhash = Hashtbl.create 0;;
441 let firstgeomcmds = E.s, [];;
442 let noghyll _ = ();;
443 let noreprf () = ();;
444 let noroam () = ();;
446 let nouioh : uioh = object (self)
447 method display = ()
448 method key _ _ = self
449 method multiclick _ _ _ _ = self
450 method button _ _ _ _ _ = self
451 method motion _ _ = self
452 method pmotion _ _ = self
453 method infochanged _ = ()
454 method scrollpw = (0, nan, nan)
455 method scrollph = (0, nan, nan)
456 method modehash = emptykeyhash
457 method eformsgs = false
458 end;;
460 let platform_to_string = function
461 | Punknown -> "unknown"
462 | Plinux -> "Linux"
463 | Posx -> "OSX"
464 | Psun -> "Sun"
465 | Pfreebsd -> "FreeBSD"
466 | Pdragonflybsd -> "DragonflyBSD"
467 | Popenbsd -> "OpenBSD"
468 | Pnetbsd -> "NetBSD"
469 | Pcygwin -> "Cygwin"
472 let version () =
473 Printf.sprintf "llpp version %s, fitz %s, ocaml %s (%s/%dbit)"
474 Help.version (fz_version ()) Sys.ocaml_version
475 (platform_to_string platform) Sys.word_size
478 let geturl s =
479 let colonpos = try String.index s ':' with Not_found -> -1 in
480 let len = String.length s in
481 if colonpos >= 0 && colonpos + 3 < len
482 then (
483 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
484 then
485 let schemestartpos =
486 try String.rindex_from s colonpos ' '
487 with Not_found -> -1
489 let scheme =
490 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
492 match scheme with
493 | "http" | "ftp" | "mailto" ->
494 let epos =
495 try String.index_from s colonpos ' '
496 with Not_found -> len
498 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
499 | _ -> E.s
500 else E.s
502 else E.s
505 let defconf =
506 { scrollbw = 7
507 ; scrollh = 12
508 ; scrollb = scrollbhv lor scrollbvv
509 ; icase = true
510 ; preload = true
511 ; pagebias = 0
512 ; verbose = false
513 ; debug = false
514 ; scrollstep = 24
515 ; hscrollstep = 24
516 ; maxhfit = true
517 ; crophack = false
518 ; autoscrollstep = 2
519 ; maxwait = None
520 ; hlinks = false
521 ; underinfo = false
522 ; interpagespace = 2
523 ; zoom = 1.0
524 ; presentation = false
525 ; angle = 0
526 ; cwinw = 900
527 ; cwinh = 900
528 ; savebmarks = true
529 ; fitmodel = FitProportional
530 ; trimmargins = false
531 ; trimfuzz = (0,0,0,0)
532 ; memlimit = 32 lsl 20
533 ; texcount = 256
534 ; sliceheight = 24
535 ; thumbw = 76
536 ; jumpback = true
537 ; bgcolor = (0.5, 0.5, 0.5)
538 ; bedefault = false
539 ; tilew = 2048
540 ; tileh = 2048
541 ; mustoresize = 256 lsl 20
542 ; checkers = true
543 ; aalevel = 8
544 ; urilauncher =
545 (match platform with
546 | Plinux | Pfreebsd | Pdragonflybsd
547 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
548 | Posx -> "open \"%s\""
549 | Pcygwin -> "cygstart \"%s\""
550 | Punknown -> "echo %s")
551 ; pathlauncher = "lp \"%s\""
552 ; selcmd =
553 (match platform with
554 | Plinux | Pfreebsd | Pdragonflybsd
555 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
556 | Posx -> "pbcopy"
557 | Pcygwin -> "wsel"
558 | Punknown -> "cat")
559 ; paxcmd = "cat"
560 ; colorspace = Rgb
561 ; invert = false
562 ; colorscale = 1.0
563 ; redirectstderr = false
564 ; ghyllscroll = None
565 ; columns = Csingle [||]
566 ; beyecolumns = None
567 ; updatecurs = false
568 ; hfsize = 12
569 ; pgscale = 1.0
570 ; usepbo = false
571 ; wheelbypage = false
572 ; stcmd = "echo SyncTex"
573 ; riani = false
574 ; pax = None
575 ; paxmark = Mark_word
576 ; leftscroll = false
577 ; title = E.s
578 ; keyhashes =
579 let mk n = (n, Hashtbl.create 1) in
580 [ mk "global"
581 ; mk "info"
582 ; mk "help"
583 ; mk "outline"
584 ; mk "listview"
585 ; mk "birdseye"
586 ; mk "textentry"
587 ; mk "links"
588 ; mk "view"
593 let conf = { defconf with angle = defconf.angle };;
595 let gotouri uri =
596 if emptystr conf.urilauncher
597 then print_endline uri
598 else (
599 let url = geturl uri in
600 if emptystr url
601 then Printf.eprintf "obtained empty url from uri %S\n" uri
602 else
603 let re = Str.regexp "%s" in
604 let command = Str.global_replace re url conf.urilauncher in
605 try popen command []
606 with exn ->
607 Printf.eprintf
608 "failed to execute `%s': %s\n" command (exntos exn);
609 flush stderr;
613 let makehelp () =
614 let strings =
615 version ()
616 :: "(searching in this text works just by typing (i.e. no initial '/'))"
617 :: E.s :: Help.keys
619 Array.of_list (
620 List.map (fun s ->
621 let url = geturl s in
622 if nonemptystr url
623 then (s, 0, Action (fun u -> gotouri url; u))
624 else (s, 0, Noaction)
625 ) strings);
628 let cbnew n v =
629 { store = Array.create n v
630 ; rc = 0
631 ; wc = 0
632 ; len = 0
636 let cbcap b = Array.length b.store;;
638 let cbput b v =
639 let cap = cbcap b in
640 b.store.(b.wc) <- v;
641 b.wc <- (b.wc + 1) mod cap;
642 b.rc <- b.wc;
643 b.len <- min (b.len + 1) cap;
646 let cbempty b = b.len = 0;;
648 let cbgetg b circular dir =
649 if cbempty b
650 then b.store.(0)
651 else
652 let rc = b.rc + dir in
653 let rc =
654 if circular
655 then (
656 if rc = -1
657 then b.len-1
658 else (
659 if rc >= b.len
660 then 0
661 else rc
664 else bound rc 0 (b.len-1)
666 b.rc <- rc;
667 b.store.(rc);
670 let cbget b = cbgetg b false;;
671 let cbgetc b = cbgetg b true;;
673 let state =
674 { sr = Unix.stdin
675 ; sw = Unix.stdin
676 ; wsfd = Unix.stdin
677 ; errfd = None
678 ; stderr = Unix.stderr
679 ; errmsgs = Buffer.create 0
680 ; newerrmsgs = false
681 ; x = 0
682 ; y = 0
683 ; w = 0
684 ; anchor = emptyanchor
685 ; ranchors = []
686 ; layout = []
687 ; maxy = max_int
688 ; tilelru = Queue.create ()
689 ; pagemap = Hashtbl.create 10
690 ; tilemap = Hashtbl.create 10
691 ; pdims = []
692 ; pagecount = 0
693 ; currently = Idle
694 ; mstate = Mnone
695 ; rects = []
696 ; rects1 = []
697 ; text = E.s
698 ; mode = View
699 ; winstate = []
700 ; searchpattern = E.s
701 ; outlines = E.a
702 ; bookmarks = []
703 ; path = E.s
704 ; password = E.s
705 ; nameddest = E.s
706 ; geomcmds = firstgeomcmds
707 ; hists =
708 { nav = cbnew 10 emptyanchor
709 ; pat = cbnew 10 E.s
710 ; pag = cbnew 10 E.s
711 ; sel = cbnew 10 E.s
713 ; memused = 0
714 ; gen = 0
715 ; throttle = None
716 ; autoscroll = None
717 ; ghyll = noghyll
718 ; help = makehelp ()
719 ; docinfo = []
720 ; checkerstexid = None
721 ; prevzoom = (1.0, 0)
722 ; progress = -1.0
723 ; uioh = nouioh
724 ; redisplay = true
725 ; mpos = (-1, -1)
726 ; keystate = KSnone
727 ; glinks = false
728 ; prevcolumns = None
729 ; winw = -1
730 ; winh = -1
731 ; reprf = noreprf
732 ; origin = E.s
733 ; roam = noroam
734 ; bzoom = false
735 ; traw = Raw.create_static `float 8
736 ; vraw = Raw.create_static `float 8
740 let copykeyhashes c =
741 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
744 let calcips h =
745 let d = state.winh - h in
746 max conf.interpagespace ((d + 1) / 2)
749 let rowyh (c, coverA, coverB) b n =
750 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
751 then
752 let _, _, vy, (_, _, h, _) = b.(n) in
753 (vy, h)
754 else
755 let n' = n - coverA in
756 let d = n' mod c in
757 let s = n - d in
758 let e = min state.pagecount (s + c) in
759 let rec find m miny maxh = if m = e then miny, maxh else
760 let _, _, y, (_, _, h, _) = b.(m) in
761 let miny = min miny y in
762 let maxh = max maxh h in
763 find (m+1) miny maxh
764 in find s max_int 0
767 let page_of_y y =
768 let ((c, coverA, coverB) as cl), b =
769 match conf.columns with
770 | Csingle b -> (1, 0, 0), b
771 | Cmulti (c, b) -> c, b
772 | Csplit (_, b) -> (1, 0, 0), b
774 if Array.length b = 0
775 then -1
776 else
777 let rec bsearch nmin nmax =
778 if nmin > nmax
779 then bound nmin 0 (state.pagecount-1)
780 else
781 let n = (nmax + nmin) / 2 in
782 let vy, h = rowyh cl b n in
783 let y0, y1 =
784 if conf.presentation
785 then
786 let ips = calcips h in
787 let y0 = vy - ips in
788 let y1 = vy + h + ips in
789 y0, y1
790 else (
791 if n = 0
792 then 0, vy + h + conf.interpagespace
793 else
794 let y0 = vy - conf.interpagespace in
795 y0, y0 + h + conf.interpagespace
798 if y >= y0 && y < y1
799 then (
800 if c = 1
801 then n
802 else (
803 if n > coverA
804 then
805 if n < state.pagecount - coverB
806 then ((n-coverA)/c)*c + coverA
807 else n
808 else n
811 else (
812 if y > y0
813 then bsearch (n+1) nmax
814 else bsearch nmin (n-1)
817 bsearch 0 (state.pagecount-1);
820 let calcheight () =
821 match conf.columns with
822 | Cmulti ((_, _, _) as cl, b) ->
823 if Array.length b > 0
824 then
825 let y, h = rowyh cl b (Array.length b - 1) in
826 y + h + (if conf.presentation then calcips h else 0)
827 else 0
828 | Csingle b ->
829 if Array.length b > 0
830 then
831 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
832 y + h + (if conf.presentation then calcips h else 0)
833 else 0
834 | Csplit (_, b) ->
835 if Array.length b > 0
836 then
837 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
838 y + h
839 else 0
842 let getpageywh pageno =
843 let pageno = bound pageno 0 (state.pagecount-1) in
844 match conf.columns with
845 | Csingle b ->
846 if Array.length b = 0
847 then 0, 0, 0
848 else
849 let (_, _, y, (_, w, h, _)) = b.(pageno) in
850 let y =
851 if conf.presentation
852 then y - calcips h
853 else y
855 y, w, h
856 | Cmulti (cl, b) ->
857 if Array.length b = 0
858 then 0, 0, 0
859 else
860 let y, h = rowyh cl b pageno in
861 let (_, _, _, (_, w, _, _)) = b.(pageno) in
862 let y =
863 if conf.presentation
864 then y - calcips h
865 else y
867 y, w, h
868 | Csplit (c, b) ->
869 if Array.length b = 0
870 then 0, 0, 0
871 else
872 let n = pageno*c in
873 let (_, _, y, (_, w, h, _)) = b.(n) in
874 y, w / c, h
877 let getpageyh pageno =
878 let y,_,h = getpageywh pageno in
879 y, h;
882 let getpagedim pageno =
883 let rec f ppdim l =
884 match l with
885 | (n, _, _, _) as pdim :: rest ->
886 if n >= pageno
887 then (if n = pageno then pdim else ppdim)
888 else f pdim rest
890 | [] -> ppdim
892 f (-1, -1, -1, -1) state.pdims
895 let getpagey pageno = fst (getpageyh pageno);;
897 let getanchor1 l =
898 let top =
899 let coloff = l.pagecol * l.pageh in
900 float (l.pagey + coloff) /. float l.pageh
902 let dtop =
903 if l.pagedispy = 0
904 then
906 else (
907 if conf.presentation
908 then float l.pagedispy /. float (calcips l.pageh)
909 else float l.pagedispy /. float conf.interpagespace
912 (l.pageno, top, dtop)
915 let getanchor () =
916 match state.layout with
917 | l :: _ -> getanchor1 l
918 | [] ->
919 let n = page_of_y state.y in
920 if n = -1
921 then state.anchor
922 else
923 let y, h = getpageyh n in
924 let dy = y - state.y in
925 let dtop =
926 if conf.presentation
927 then
928 let ips = calcips h in
929 float (dy + ips) /. float ips
930 else
931 float dy /. float conf.interpagespace
933 (n, 0.0, dtop)
936 let fontpath = ref E.s;;
938 module KeyMap =
939 Map.Make (struct type t = (int * int) let compare = compare end);;
941 open Parser;;
943 let unent s =
944 let l = String.length s in
945 let b = Buffer.create l in
946 unent b s 0 l;
947 Buffer.contents b;
950 let home =
951 try Sys.getenv "HOME"
952 with exn ->
953 prerr_endline
954 ("Can not determine home directory location: " ^ exntos exn);
958 let modifier_of_string = function
959 | "alt" -> Wsi.altmask
960 | "shift" -> Wsi.shiftmask
961 | "ctrl" | "control" -> Wsi.ctrlmask
962 | "meta" -> Wsi.metamask
963 | _ -> 0
966 let key_of_string =
967 let r = Str.regexp "-" in
968 fun s ->
969 let elems = Str.full_split r s in
970 let f n k m =
971 let g s =
972 let m1 = modifier_of_string s in
973 if m1 = 0
974 then (Wsi.namekey s, m)
975 else (k, m lor m1)
976 in function
977 | Str.Delim s when n land 1 = 0 -> g s
978 | Str.Text s -> g s
979 | Str.Delim _ -> (k, m)
981 let rec loop n k m = function
982 | [] -> (k, m)
983 | x :: xs ->
984 let k, m = f n k m x in
985 loop (n+1) k m xs
987 loop 0 0 0 elems
990 let keys_of_string =
991 let r = Str.regexp "[ \t]" in
992 fun s ->
993 let elems = Str.split r s in
994 List.map key_of_string elems
997 let config_of c attrs =
998 let apply c k v =
1000 match k with
1001 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
1002 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
1003 | "case-insensitive-search" -> { c with icase = bool_of_string v }
1004 | "preload" -> { c with preload = bool_of_string v }
1005 | "page-bias" -> { c with pagebias = int_of_string v }
1006 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
1007 | "horizontal-scroll-step" ->
1008 { c with hscrollstep = max (int_of_string v) 1 }
1009 | "auto-scroll-step" ->
1010 { c with autoscrollstep = max 0 (int_of_string v) }
1011 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
1012 | "crop-hack" -> { c with crophack = bool_of_string v }
1013 | "throttle" ->
1014 let mw =
1015 match String.lowercase v with
1016 | "true" -> Some infinity
1017 | "false" -> None
1018 | f -> Some (float_of_string f)
1020 { c with maxwait = mw}
1021 | "highlight-links" -> { c with hlinks = bool_of_string v }
1022 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
1023 | "vertical-margin" ->
1024 { c with interpagespace = max 0 (int_of_string v) }
1025 | "zoom" ->
1026 let zoom = float_of_string v /. 100. in
1027 let zoom = max zoom 0.0 in
1028 { c with zoom = zoom }
1029 | "presentation" -> { c with presentation = bool_of_string v }
1030 | "rotation-angle" -> { c with angle = int_of_string v }
1031 | "width" -> { c with cwinw = max 20 (int_of_string v) }
1032 | "height" -> { c with cwinh = max 20 (int_of_string v) }
1033 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
1034 | "proportional-display" ->
1035 let fm =
1036 if bool_of_string v
1037 then FitProportional
1038 else FitWidth
1040 { c with fitmodel = fm }
1041 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
1042 | "pixmap-cache-size" ->
1043 { c with memlimit = max 2 (int_of_string_with_suffix v) }
1044 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
1045 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
1046 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
1047 | "persistent-location" -> { c with jumpback = bool_of_string v }
1048 | "background-color" -> { c with bgcolor = color_of_string v }
1049 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
1050 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
1051 | "mupdf-store-size" ->
1052 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
1053 | "checkers" -> { c with checkers = bool_of_string v }
1054 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
1055 | "trim-margins" -> { c with trimmargins = bool_of_string v }
1056 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
1057 | "uri-launcher" -> { c with urilauncher = unent v }
1058 | "path-launcher" -> { c with pathlauncher = unent v }
1059 | "color-space" -> { c with colorspace = CSTE.of_string v }
1060 | "invert-colors" -> { c with invert = bool_of_string v }
1061 | "brightness" -> { c with colorscale = float_of_string v }
1062 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
1063 | "ghyllscroll" -> { c with ghyllscroll = ghyllscroll_of_string v }
1064 | "columns" ->
1065 let (n, _, _) as nab = multicolumns_of_string v in
1066 if n < 0
1067 then { c with columns = Csplit (-n, E.a) }
1068 else { c with columns = Cmulti (nab, E.a) }
1069 | "birds-eye-columns" ->
1070 { c with beyecolumns = Some (max (int_of_string v) 2) }
1071 | "selection-command" -> { c with selcmd = unent v }
1072 | "synctex-command" -> { c with stcmd = unent v }
1073 | "pax-command" -> { c with paxcmd = unent v }
1074 | "update-cursor" -> { c with updatecurs = bool_of_string v }
1075 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
1076 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
1077 | "use-pbo" -> { c with usepbo = bool_of_string v }
1078 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
1079 | "horizontal-scrollbar-visible" ->
1080 let b =
1081 if bool_of_string v
1082 then c.scrollb lor scrollbhv
1083 else c.scrollb land (lnot scrollbhv)
1085 { c with scrollb = b }
1086 | "vertical-scrollbar-visible" ->
1087 let b =
1088 if bool_of_string v
1089 then c.scrollb lor scrollbvv
1090 else c.scrollb land (lnot scrollbvv)
1092 { c with scrollb = b }
1093 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
1094 | "point-and-x" ->
1095 { c with pax =
1096 if bool_of_string v
1097 then Some (ref (0.0, 0, 0))
1098 else None }
1099 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
1100 | "scroll-bar-on-the-left" -> { c with leftscroll = bool_of_string v }
1101 | "title" -> { c with title = unent 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 "" 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 _ -> 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 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 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 = String.create len in
1441 really_input ic s 0 len;
1442 f s;
1443 with
1444 | Parse_error (msg, s, pos) ->
1445 let subs = subs s pos in
1446 Utils.error "parse error: %s: at %d [..%s..]" msg pos subs
1448 | exn ->
1449 failwith ("config load error: " ^ exntos exn)
1452 let defconfpath =
1453 let dir =
1455 let dir = Filename.concat home ".config" in
1456 if Sys.is_directory dir then dir else home
1457 with _ -> home
1459 Filename.concat dir "llpp.conf"
1462 let confpath = ref defconfpath;;
1464 let load1 f =
1465 if Sys.file_exists !confpath
1466 then
1467 match
1468 (try Some (open_in_bin !confpath)
1469 with exn ->
1470 prerr_endline
1471 ("Error opening configuration file `" ^ !confpath ^ "': " ^
1472 exntos exn);
1473 None
1475 with
1476 | Some ic ->
1477 let success =
1479 f (do_load get ic)
1480 with exn ->
1481 prerr_endline
1482 ("Error loading configuration from `" ^ !confpath ^ "': " ^
1483 exntos exn);
1484 false
1486 close_in ic;
1487 success
1489 | None -> false
1490 else
1491 f (Hashtbl.create 0, defconf)
1494 let load () =
1495 let f (h, dc) =
1496 let pc, pb, px, pa =
1498 let path =
1499 if emptystr state.origin
1500 then state.path
1501 else state.origin
1503 let absname = abspath path in
1504 Hashtbl.find h absname
1505 with Not_found -> dc, [], 0, emptyanchor
1507 setconf defconf dc;
1508 setconf conf pc;
1509 state.bookmarks <- pb;
1510 state.x <- px;
1511 if conf.jumpback
1512 then state.anchor <- pa;
1513 cbput state.hists.nav pa;
1514 true
1516 load1 f
1519 let gethist listref =
1520 let f (h, _) =
1521 listref :=
1522 Hashtbl.fold (fun path (pc, pb, px, pa) accu ->
1523 (path, pc, pb, px, pa) :: accu)
1524 h [];
1525 true
1527 load1 f
1530 let add_attrs bb always dc c =
1531 let ob s a b =
1532 if always || a != b
1533 then Printf.bprintf bb "\n %s='%b'" s a
1534 and op s a b =
1535 if always || a <> b
1536 then Printf.bprintf bb "\n %s='%b'" s (a != None)
1537 and oi s a b =
1538 if always || a != b
1539 then Printf.bprintf bb "\n %s='%d'" s a
1540 and oI s a b =
1541 if always || a != b
1542 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
1543 and oz s a b =
1544 if always || a <> b
1545 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
1546 and oF s a b =
1547 if always || a <> b
1548 then Printf.bprintf bb "\n %s='%f'" s a
1549 and oc s a b =
1550 if always || a <> b
1551 then
1552 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
1553 and oC s a b =
1554 if always || a <> b
1555 then
1556 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
1557 and oR s a b =
1558 if always || a <> b
1559 then
1560 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
1561 and os s a b =
1562 if always || a <> b
1563 then
1564 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
1565 and og s a b =
1566 if always || a <> b
1567 then
1568 match a with
1569 | Some (_N, _A, _B) ->
1570 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
1571 | None ->
1572 match b with
1573 | None -> ()
1574 | _ ->
1575 Printf.bprintf bb "\n %s='none'" s
1576 and oW s a b =
1577 if always || a <> b
1578 then
1579 let v =
1580 match a with
1581 | None -> "false"
1582 | Some f ->
1583 if f = infinity
1584 then "true"
1585 else string_of_float f
1587 Printf.bprintf bb "\n %s='%s'" s v
1588 and oco s a b =
1589 if always || a <> b
1590 then
1591 match a with
1592 | Cmulti ((n, a, b), _) when n > 1 ->
1593 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
1594 | Csplit (n, _) when n > 1 ->
1595 Printf.bprintf bb "\n %s='%d'" s ~-n
1596 | _ -> ()
1597 and obeco s a b =
1598 if always || a <> b
1599 then
1600 match a with
1601 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
1602 | _ -> ()
1603 and oFm s a b =
1604 if always || a <> b
1605 then
1606 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
1607 and oSv s a b m =
1608 if always || a <> b
1609 then
1610 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
1611 and oPm s a b =
1612 if always || a <> b
1613 then
1614 Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
1616 oi "width" c.cwinw dc.cwinw;
1617 oi "height" c.cwinh dc.cwinh;
1618 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
1619 oi "scroll-handle-height" c.scrollh dc.scrollh;
1620 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
1621 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
1622 ob "case-insensitive-search" c.icase dc.icase;
1623 ob "preload" c.preload dc.preload;
1624 oi "page-bias" c.pagebias dc.pagebias;
1625 oi "scroll-step" c.scrollstep dc.scrollstep;
1626 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
1627 ob "max-height-fit" c.maxhfit dc.maxhfit;
1628 ob "crop-hack" c.crophack dc.crophack;
1629 oW "throttle" c.maxwait dc.maxwait;
1630 ob "highlight-links" c.hlinks dc.hlinks;
1631 ob "under-cursor-info" c.underinfo dc.underinfo;
1632 oi "vertical-margin" c.interpagespace dc.interpagespace;
1633 oz "zoom" c.zoom dc.zoom;
1634 ob "presentation" c.presentation dc.presentation;
1635 oi "rotation-angle" c.angle dc.angle;
1636 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
1637 oFm "fit-model" c.fitmodel dc.fitmodel;
1638 oI "pixmap-cache-size" c.memlimit dc.memlimit;
1639 oi "tex-count" c.texcount dc.texcount;
1640 oi "slice-height" c.sliceheight dc.sliceheight;
1641 oi "thumbnail-width" c.thumbw dc.thumbw;
1642 ob "persistent-location" c.jumpback dc.jumpback;
1643 oc "background-color" c.bgcolor dc.bgcolor;
1644 oi "tile-width" c.tilew dc.tilew;
1645 oi "tile-height" c.tileh dc.tileh;
1646 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
1647 ob "checkers" c.checkers dc.checkers;
1648 oi "aalevel" c.aalevel dc.aalevel;
1649 ob "trim-margins" c.trimmargins dc.trimmargins;
1650 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
1651 os "uri-launcher" c.urilauncher dc.urilauncher;
1652 os "path-launcher" c.pathlauncher dc.pathlauncher;
1653 oC "color-space" c.colorspace dc.colorspace;
1654 ob "invert-colors" c.invert dc.invert;
1655 oF "brightness" c.colorscale dc.colorscale;
1656 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
1657 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
1658 oco "columns" c.columns dc.columns;
1659 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
1660 os "selection-command" c.selcmd dc.selcmd;
1661 os "synctex-command" c.stcmd dc.stcmd;
1662 os "pax-command" c.paxcmd dc.paxcmd;
1663 ob "update-cursor" c.updatecurs dc.updatecurs;
1664 oi "hint-font-size" c.hfsize dc.hfsize;
1665 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
1666 oF "page-scroll-scale" c.pgscale dc.pgscale;
1667 ob "use-pbo" c.usepbo dc.usepbo;
1668 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
1669 ob "remote-in-a-new-instance" c.riani dc.riani;
1670 op "point-and-x" c.pax dc.pax;
1671 oPm "point-and-x-mark" c.paxmark dc.paxmark;
1672 ob "scroll-bar-on-the-left" c.leftscroll dc.leftscroll;
1673 if not always
1674 then os "title" c.title dc.title;
1677 let keymapsbuf always dc c =
1678 let bb = Buffer.create 16 in
1679 let rec loop = function
1680 | [] -> ()
1681 | (modename, h) :: rest ->
1682 let dh = findkeyhash dc modename in
1683 if always || h <> dh
1684 then (
1685 if Hashtbl.length h > 0
1686 then (
1687 if Buffer.length bb > 0
1688 then Buffer.add_char bb '\n';
1689 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
1690 Hashtbl.iter (fun i o ->
1691 let isdifferent = always ||
1693 let dO = Hashtbl.find dh i in
1694 dO <> o
1695 with Not_found -> true
1697 if isdifferent
1698 then
1699 let addkm (k, m) =
1700 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
1701 if Wsi.withalt m then Buffer.add_string bb "alt-";
1702 if Wsi.withshift m then Buffer.add_string bb "shift-";
1703 if Wsi.withmeta m then Buffer.add_string bb "meta-";
1704 Buffer.add_string bb (Wsi.keyname k);
1706 let addkms l =
1707 let rec loop = function
1708 | [] -> ()
1709 | km :: [] -> addkm km
1710 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
1712 loop l
1714 Buffer.add_string bb "<map in='";
1715 addkm i;
1716 match o with
1717 | KMinsrt km ->
1718 Buffer.add_string bb "' out='";
1719 addkm km;
1720 Buffer.add_string bb "'/>\n"
1722 | KMinsrl kms ->
1723 Buffer.add_string bb "' out='";
1724 addkms kms;
1725 Buffer.add_string bb "'/>\n"
1727 | KMmulti (ins, kms) ->
1728 Buffer.add_char bb ' ';
1729 addkms ins;
1730 Buffer.add_string bb "' out='";
1731 addkms kms;
1732 Buffer.add_string bb "'/>\n"
1733 ) h;
1734 Buffer.add_string bb "</keymap>";
1737 loop rest
1739 loop c.keyhashes;
1743 let save leavebirdseye =
1744 let uifontsize = fstate.fontsize in
1745 let bb = Buffer.create 32768 in
1746 let relx = float state.x /. float state.winw in
1747 let w, h, x =
1748 let cx w = truncate (relx *. float w) in
1749 List.fold_left
1750 (fun (w, h, x) ws ->
1751 match ws with
1752 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
1753 | Wsi.MaxVert -> (w, conf.cwinh, x)
1754 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
1756 (state.winw, state.winh, state.x) state.winstate
1758 conf.cwinw <- w;
1759 conf.cwinh <- h;
1760 let f (h, dc) =
1761 let dc = if conf.bedefault then conf else dc in
1762 Buffer.add_string bb "<llppconfig>\n";
1764 if nonemptystr !fontpath
1765 then
1766 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
1767 uifontsize
1768 !fontpath
1769 else (
1770 if uifontsize <> 14
1771 then
1772 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
1775 Buffer.add_string bb "<defaults";
1776 add_attrs bb true dc dc;
1777 let kb = keymapsbuf true dc dc in
1778 if Buffer.length kb > 0
1779 then (
1780 Buffer.add_string bb ">\n";
1781 Buffer.add_buffer bb kb;
1782 Buffer.add_string bb "\n</defaults>\n";
1784 else Buffer.add_string bb "/>\n";
1786 let adddoc path pan anchor c bookmarks =
1787 if bookmarks == [] && c = dc && anchor = emptyanchor
1788 then ()
1789 else (
1790 Printf.bprintf bb "<doc path='%s'"
1791 (enent path 0 (String.length path));
1793 if anchor <> emptyanchor
1794 then (
1795 let n, rely, visy = anchor in
1796 Printf.bprintf bb "\n page='%d'" n;
1797 if rely > 1e-6
1798 then
1799 Printf.bprintf bb " rely='%f'" rely
1801 if abs_float visy > 1e-6
1802 then
1803 Printf.bprintf bb " visy='%f'" visy
1807 if pan != 0
1808 then Printf.bprintf bb " pan='%d'" pan;
1810 add_attrs bb false dc c;
1811 let kb = keymapsbuf false dc c in
1813 begin match bookmarks with
1814 | [] ->
1815 if Buffer.length kb > 0
1816 then (
1817 Buffer.add_string bb ">\n";
1818 Buffer.add_buffer bb kb;
1819 Buffer.add_string bb "\n</doc>\n";
1821 else Buffer.add_string bb "/>\n"
1822 | _ ->
1823 Buffer.add_string bb ">\n<bookmarks>\n";
1824 List.iter (fun (title, _, kind) ->
1825 begin match kind with
1826 | Oanchor (page, rely, visy) ->
1827 Printf.bprintf bb
1828 "<item title='%s' page='%d'"
1829 (enent title 0 (String.length title))
1830 page
1832 if rely > 1e-6
1833 then
1834 Printf.bprintf bb " rely='%f'" rely
1836 if abs_float visy > 1e-6
1837 then
1838 Printf.bprintf bb " visy='%f'" visy
1840 | Ohistory _ | Onone | Ouri _ | Oremote _
1841 | Oremotedest _ | Olaunch _ ->
1842 failwith "unexpected link in bookmarks"
1843 end;
1844 Buffer.add_string bb "/>\n";
1845 ) bookmarks;
1846 Buffer.add_string bb "</bookmarks>";
1847 if Buffer.length kb > 0
1848 then (
1849 Buffer.add_string bb "\n";
1850 Buffer.add_buffer bb kb;
1852 Buffer.add_string bb "\n</doc>\n";
1853 end;
1857 let pan, conf =
1858 match state.mode with
1859 | Birdseye (c, pan, _, _, _) ->
1860 let beyecolumns =
1861 match conf.columns with
1862 | Cmulti ((c, _, _), _) -> Some c
1863 | Csingle _ -> None
1864 | Csplit _ -> None
1865 and columns =
1866 match c.columns with
1867 | Cmulti (c, _) -> Cmulti (c, E.a)
1868 | Csingle _ -> Csingle E.a
1869 | Csplit _ -> failwith "quit from bird's eye while split"
1871 pan, { c with beyecolumns = beyecolumns; columns = columns }
1872 | _ -> x, conf
1874 let docpath = abspath state.path in
1875 adddoc docpath pan (getanchor ())
1877 let autoscrollstep =
1878 match state.autoscroll with
1879 | Some step -> step
1880 | None -> conf.autoscrollstep
1882 begin match state.mode with
1883 | Birdseye beye -> leavebirdseye beye true
1884 | _ -> ()
1885 end;
1886 { conf with autoscrollstep = autoscrollstep }
1888 (if conf.savebmarks then state.bookmarks else []);
1890 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
1891 if docpath <> abspath path
1892 then adddoc path x anchor c bookmarks
1893 ) h;
1894 Buffer.add_string bb "</llppconfig>\n";
1895 true;
1897 if load1 f && Buffer.length bb > 0
1898 then
1900 let tmp = !confpath ^ ".tmp" in
1901 let oc = open_out_bin tmp in
1902 Buffer.output_buffer oc bb;
1903 close_out oc;
1904 Unix.rename tmp !confpath;
1905 with exn ->
1906 prerr_endline
1907 ("error while saving configuration: " ^ exntos exn)