Disallow link hints when document was rotated
[llpp.git] / config.ml
blob6ee7e3cb4329f130437c41d719ad977880af9c59
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 "N(%d),A(%d),B(%d) (N <= A, A < B, N <= B)" n a b;
42 Some nab
45 let ghyllscroll_to_string ((n, a, b) as nab) =
46 (**) if nab = fastghyllscroll then "fast"
47 else if nab = neatghyllscroll then "neat"
48 else Printf.sprintf "%d,%d,%d" n a b;
51 let multicolumns_to_string (n, a, b) =
52 if a = 0 && b = 0
53 then Printf.sprintf "%d" n
54 else Printf.sprintf "%d,%d,%d" n a b;
57 let multicolumns_of_string s =
58 try
59 (int_of_string s, 0, 0)
60 with _ ->
61 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
62 if a > 1 || b > 1
63 then failwith "subtly broken";
64 (n, a, b)
68 type keymap =
69 | KMinsrt of key
70 | KMinsrl of key list
71 | KMmulti of key list * key list
72 and key = int * int
73 and keyhash = (key, keymap) Hashtbl.t
74 and keystate =
75 | KSnone
76 | KSinto of (key list * key list)
77 and interpagespace = int
78 and multicolumns = multicol * pagegeom
79 and singlecolumn = pagegeom
80 and splitcolumns = columncount * pagegeom
81 and pagegeom = (pdimno * x * y * (pageno * width * height * leftx)) array
82 and multicol = columncount * covercount * covercount
83 and pdimno = int
84 and columncount = int
85 and covercount = int
86 and fitmodel = | FitWidth | FitProportional | FitPage
87 and trimmargins = bool
88 and irect = (int * int * int * int)
89 and memsize = int
90 and texcount = int
91 and sliceheight = int
92 and angle = int
93 and params = (angle * fitmodel * trimparams
94 * texcount * sliceheight * memsize
95 * colorspace * fontpath * trimcachepath
96 * haspbo * usefontconfig)
97 and width = int
98 and height = int
99 and leftx = int
100 and opaque = Opaque.t
101 and rectcolor = (float * float * float * float)
102 and pixmapsize = int
103 and gen = int
104 and top = float
105 and dtop = float
106 and fontpath = string
107 and trimcachepath = string
108 and aalevel = int
109 and trimparams = (trimmargins * irect)
110 and colorspace = | Rgb | Bgr | Gray
111 and haspbo = bool
112 and usefontconfig = bool
113 and uri = string
114 and caption = string
115 and x = int
116 and y = int
117 and tilex = int
118 and tiley = int
119 and tileparams = (x * y * width * height * tilex * tiley)
120 and under =
121 | Unone
122 | Ulinkuri of string
123 | Ulinkgoto of (int * int)
124 | Utext of facename
125 | Uunexpected of string
126 | Ulaunch of launchcommand
127 | Unamed of destname
128 | Uremote of (filename * pageno)
129 | Uremotedest of (filename * destname)
130 | Uannotation of (opaque * slinkindex)
131 and slinkindex = int
132 and facename = string
133 and launchcommand = string
134 and filename = string
135 and pageno = int
136 and destname = string
137 and mark =
138 | Mark_page
139 | Mark_block
140 | Mark_line
141 | Mark_word
142 and link =
143 | Lnotfound
144 | Lfound of int
145 and linkdir =
146 | LDfirst
147 | LDlast
148 | LDfirstvisible of (int * int * int)
149 | LDleft of int
150 | LDright of int
151 | LDdown of int
152 | LDup of int
153 and pagewithlinks =
154 | Pwlnotfound
155 | Pwl of int
156 and scrollb = int
157 and anchor = pageno * top * dtop
158 and rect = float * float * float * float * float * float * float * float
159 and infochange = | Memused | Docinfo | Pdim
162 class type uioh = object
163 method display : unit
164 method key : int -> int -> uioh
165 method button : int -> bool -> int -> int -> int -> uioh
166 method multiclick : int -> int -> int -> int -> uioh
167 method motion : int -> int -> uioh
168 method pmotion : int -> int -> uioh
169 method infochanged : infochange -> unit
170 method scrollpw : (int * float * float)
171 method scrollph : (int * float * float)
172 method modehash : keyhash
173 method eformsgs : bool
174 method alwaysscrolly : bool
175 end;;
177 module type TextEnumType =
179 type t
180 val name : string
181 val names : string array
182 end;;
184 module TextEnumMake (Ten : TextEnumType) =
185 struct
186 let names = Ten.names;;
187 let to_int (t : Ten.t) = Obj.magic t;;
188 let to_string t = names.(to_int t);;
189 let of_int n : Ten.t = Obj.magic n;;
190 let of_string s =
191 let rec find i =
192 if i = Array.length names
193 then failwith ("invalid " ^ Ten.name ^ ": " ^ s)
194 else (
195 if Ten.names.(i) = s
196 then of_int i
197 else find (i+1)
199 in find 0;;
200 end;;
202 module CSTE = TextEnumMake (struct
203 type t = colorspace;;
204 let name = "colorspace";;
205 let names = [|"rgb"; "bgr"; "gray"|];;
206 end);;
208 module MTE = TextEnumMake (struct
209 type t = mark;;
210 let name = "mark";;
211 let names = [|"page"; "block"; "line"; "word"|];;
212 end);;
214 module FMTE = TextEnumMake (struct
215 type t = fitmodel;;
216 let name = "fitmodel";;
217 let names = [|"width"; "proportional"; "page"|];;
218 end);;
220 type conf =
221 { mutable scrollbw : int
222 ; mutable scrollh : int
223 ; mutable scrollb : scrollb
224 ; mutable icase : bool
225 ; mutable preload : bool
226 ; mutable pagebias : int
227 ; mutable verbose : bool
228 ; mutable debug : bool
229 ; mutable scrollstep : int
230 ; mutable hscrollstep : int
231 ; mutable maxhfit : bool
232 ; mutable crophack : bool
233 ; mutable autoscrollstep : int
234 ; mutable maxwait : float option
235 ; mutable hlinks : bool
236 ; mutable underinfo : bool
237 ; mutable interpagespace : interpagespace
238 ; mutable zoom : float
239 ; mutable presentation : bool
240 ; mutable angle : angle
241 ; mutable cwinw : int
242 ; mutable cwinh : int
243 ; mutable savebmarks : bool
244 ; mutable fitmodel : fitmodel
245 ; mutable trimmargins : trimmargins
246 ; mutable trimfuzz : irect
247 ; mutable memlimit : memsize
248 ; mutable texcount : texcount
249 ; mutable sliceheight : sliceheight
250 ; mutable thumbw : width
251 ; mutable jumpback : bool
252 ; mutable bgcolor : (float * float * float)
253 ; mutable bedefault : bool
254 ; mutable tilew : int
255 ; mutable tileh : int
256 ; mutable mustoresize : memsize
257 ; mutable checkers : bool
258 ; mutable aalevel : int
259 ; mutable urilauncher : string
260 ; mutable pathlauncher : string
261 ; mutable colorspace : colorspace
262 ; mutable invert : bool
263 ; mutable colorscale : float
264 ; mutable ghyllscroll : (int * int * int) option
265 ; mutable columns : columns
266 ; mutable beyecolumns : columncount option
267 ; mutable selcmd : string
268 ; mutable paxcmd : string
269 ; mutable passcmd : string
270 ; mutable savecmd : string
271 ; mutable updatecurs : bool
272 ; mutable keyhashes : (string * keyhash) list
273 ; mutable hfsize : int
274 ; mutable pgscale : float
275 ; mutable usepbo : bool
276 ; mutable wheelbypage : bool
277 ; mutable stcmd : string
278 ; mutable riani : bool
279 ; mutable pax : (float * int * int) ref option
280 ; mutable paxmark : mark
281 ; mutable leftscroll : bool
282 ; mutable title : string
283 ; mutable lastvisit : float
284 ; mutable annotinline : bool
286 and columns =
287 | Csingle of singlecolumn
288 | Cmulti of multicolumns
289 | Csplit of splitcolumns
290 and outlinekind =
291 | Onone
292 | Oanchor of anchor
293 | Ouri of uri
294 | Olaunch of launchcommand
295 | Oremote of (filename * pageno)
296 | Oremotedest of (filename * destname)
297 | Ohistory of (filename * conf * outline list * x * anchor * filename)
298 and outline = (caption * outlinelevel * outlinekind)
299 and outlinelevel = int
302 type page =
303 { pageno : int
304 ; pagedimno : int
305 ; pagew : int
306 ; pageh : int
307 ; pagex : int
308 ; pagey : int
309 ; pagevw : int
310 ; pagevh : int
311 ; pagedispx : int
312 ; pagedispy : int
313 ; pagecol : int
317 type tile = opaque * pixmapsize * elapsed
318 and elapsed = float;;
319 type pagemapkey = pageno * gen;;
320 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
321 and row = int
322 and col = int
323 and currently =
324 | Idle
325 | Loading of (page * gen)
326 | Tiling of (
327 page * opaque * colorspace * angle * gen * col * row * width * height
329 | Outlining of outline list
332 type mpos = int * int
333 and mstate =
334 | Msel of (mpos * mpos)
335 | Mpan of mpos
336 | Mscrolly | Mscrollx
337 | Mzoom of (buttonno * step)
338 | Mzoomrect of (mpos * mpos)
339 | Mnone
340 and buttonno = int
341 and step = int
344 type mode =
345 | Birdseye of (conf * leftx * pageno * pageno * anchor)
346 | Textentry of (textentry * onleave)
347 | View
348 | LinkNav of linktarget
349 and onleave = leavetextentrystatus -> unit
350 and leavetextentrystatus = | Cancel | Confirm
351 and helpitem = string * int * action
352 and action =
353 | Noaction
354 | Action of (uioh -> uioh)
355 and linktarget =
356 | Ltexact of (pageno * direction)
357 | Ltgendir of direction
358 | Ltnotready of (pageno * direction)
359 and direction = int (* -1, 0, 1 *)
360 and textentry = string * string * onhist option * onkey * ondone * cancelonempty
361 and onkey = string -> int -> te
362 and ondone = string -> unit
363 and histcancel = unit -> unit
364 and onhist = ((histcmd -> string) * histcancel)
365 and histcmd = HCnext | HCprev | HCfirst | HClast
366 and cancelonempty = bool
367 and te =
368 | TEstop
369 | TEdone of string
370 | TEcont of string
371 | TEswitch of textentry
374 type 'a circbuf =
375 { store : 'a array
376 ; mutable rc : int
377 ; mutable wc : int
378 ; mutable len : int
382 type state =
383 { mutable ss : Unix.file_descr
384 ; mutable wsfd : Unix.file_descr
385 ; mutable stderr : Unix.file_descr
386 ; mutable errmsgs : Buffer.t
387 ; mutable newerrmsgs : bool
388 ; mutable w : int
389 ; mutable x : int
390 ; mutable y : int
391 ; mutable anchor : anchor
392 ; mutable ranchors : (string * string * anchor * string) list
393 ; mutable maxy : int
394 ; mutable layout : page list
395 ; pagemap : (pagemapkey, opaque) Hashtbl.t
396 ; tilemap : (tilemapkey, tile) Hashtbl.t
397 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
398 ; mutable pdims : (pageno * width * height * leftx) list
399 ; mutable pagecount : int
400 ; mutable currently : currently
401 ; mutable mstate : mstate
402 ; mutable searchpattern : string
403 ; mutable rects : (pageno * rectcolor * rect) list
404 ; mutable rects1 : (pageno * rectcolor * rect) list
405 ; prects : (pageno, float array) Hashtbl.t
406 ; mutable text : string
407 ; mutable winstate : Wsi.winstate list
408 ; mutable mode : mode
409 ; mutable uioh : uioh
410 ; mutable outlines : outline array
411 ; mutable bookmarks : outline list
412 ; mutable path : string
413 ; mutable password : string
414 ; mutable nameddest : string
415 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
416 ; mutable memused : memsize
417 ; mutable gen : gen
418 ; mutable throttle : (page list * int * float) option
419 ; mutable autoscroll : int option
420 ; mutable ghyll : (int option -> unit)
421 ; mutable help : helpitem array
422 ; mutable docinfo : (int * string) list
423 ; mutable checkerstexid : GlTex.texture_id option
424 ; hists : hists
425 ; mutable prevzoom : (float * int)
426 ; mutable progress : float
427 ; mutable redisplay : bool
428 ; mutable mpos : mpos
429 ; mutable keystate : keystate
430 ; mutable glinks : bool
431 ; mutable prevcolumns : (columns * float) option
432 ; mutable winw : int
433 ; mutable winh : int
434 ; mutable reprf : (unit -> unit)
435 ; mutable origin : string
436 ; mutable roam : (unit -> unit)
437 ; mutable bzoom : bool
438 ; mutable traw : [`float] Raw.t
439 ; mutable vraw : [`float] Raw.t
441 and hists =
442 { pat : string circbuf
443 ; pag : string circbuf
444 ; nav : anchor circbuf
445 ; sel : string circbuf
449 let emptyanchor = (0, 0.0, 0.0);;
450 let emptykeyhash = Hashtbl.create 0;;
451 let noghyll _ = ();;
452 let noreprf () = ();;
453 let noroam () = ();;
455 let nouioh : uioh = object (self)
456 method display = ()
457 method key _ _ = self
458 method multiclick _ _ _ _ = self
459 method button _ _ _ _ _ = self
460 method motion _ _ = self
461 method pmotion _ _ = self
462 method infochanged _ = ()
463 method scrollpw = (0, nan, nan)
464 method scrollph = (0, nan, nan)
465 method modehash = emptykeyhash
466 method eformsgs = false
467 method alwaysscrolly = false
468 end;;
470 let platform_to_string = function
471 | Punknown -> "unknown"
472 | Plinux -> "Linux"
473 | Posx -> "OSX"
474 | Psun -> "Sun"
475 | Pbsd -> "BSD"
476 | Pcygwin -> "Cygwin"
479 let version () =
480 Printf.sprintf "llpp version %s, fitz %s, ocaml %s/%d bit"
481 Help.version (fz_version ()) Sys.ocaml_version Sys.word_size
484 let defconf =
485 { scrollbw = 7
486 ; scrollh = 12
487 ; scrollb = scrollbhv lor scrollbvv
488 ; icase = true
489 ; preload = true
490 ; pagebias = 0
491 ; verbose = false
492 ; debug = false
493 ; scrollstep = 24
494 ; hscrollstep = 24
495 ; maxhfit = true
496 ; crophack = false
497 ; autoscrollstep = 2
498 ; maxwait = None
499 ; hlinks = false
500 ; underinfo = false
501 ; interpagespace = 2
502 ; zoom = 1.0
503 ; presentation = false
504 ; angle = 0
505 ; cwinw = 900
506 ; cwinh = 900
507 ; savebmarks = true
508 ; fitmodel = FitProportional
509 ; trimmargins = false
510 ; trimfuzz = (0,0,0,0)
511 ; memlimit = 32 lsl 20
512 ; texcount = 256
513 ; sliceheight = 24
514 ; thumbw = 76
515 ; jumpback = true
516 ; bgcolor = (0.5, 0.5, 0.5)
517 ; bedefault = false
518 ; tilew = 2048
519 ; tileh = 2048
520 ; mustoresize = 256 lsl 20
521 ; checkers = true
522 ; aalevel = 8
523 ; urilauncher =
524 (match platform with
525 | Plinux | Psun | Pbsd -> "xdg-open \"%s\""
526 | Posx -> "open \"%s\""
527 | Pcygwin -> "cygstart \"%s\""
528 | Punknown -> "echo %s")
529 ; pathlauncher = "lp \"%s\""
530 ; selcmd =
531 (match platform with
532 | Plinux | Pbsd | Psun -> "xsel -i"
533 | Posx -> "pbcopy"
534 | Pcygwin -> "wsel"
535 | Punknown -> "cat")
536 ; paxcmd = "cat"
537 ; passcmd = E.s
538 ; savecmd = E.s
539 ; colorspace = Rgb
540 ; invert = false
541 ; colorscale = 1.0
542 ; ghyllscroll = None
543 ; columns = Csingle [||]
544 ; beyecolumns = None
545 ; updatecurs = false
546 ; hfsize = 12
547 ; pgscale = 1.0
548 ; usepbo = false
549 ; wheelbypage = false
550 ; stcmd = "echo SyncTex"
551 ; riani = false
552 ; pax = None
553 ; paxmark = Mark_word
554 ; leftscroll = false
555 ; title = E.s
556 ; lastvisit = 0.0
557 ; annotinline = true
558 ; keyhashes =
559 let mk n = (n, Hashtbl.create 1) in
560 [ mk "global"
561 ; mk "info"
562 ; mk "help"
563 ; mk "outline"
564 ; mk "listview"
565 ; mk "birdseye"
566 ; mk "textentry"
567 ; mk "links"
568 ; mk "view"
573 let conf = { defconf with angle = defconf.angle };;
575 let gotourl url =
576 let command = Str.global_replace percentsre url conf.urilauncher in
577 try ignore @@ spawn command []
578 with exn -> dolog "failed to execute `%s': %s" command @@ exntos exn
581 let gotouri uri =
582 if emptystr conf.urilauncher
583 then dolog "%s" uri
584 else (
585 let url = geturl uri in
586 if emptystr url
587 then dolog "obtained empty url from uri %S\n" uri
588 else gotourl url
592 let makehelp () =
593 let strings =
594 version ()
595 :: "(searching in this text works just by typing (i.e. no initial '/'))"
596 :: E.s :: Help.keys
598 Array.of_list (
599 List.map (fun s ->
600 let url = geturl s in
601 if nonemptystr url
602 then (s, 0, Action (fun uioh -> gotourl url; uioh))
603 else (s, 0, Noaction)
604 ) strings);
607 let cbnew n v =
608 { store = Array.make n v
609 ; rc = 0
610 ; wc = 0
611 ; len = 0
615 let cbcap b = Array.length b.store;;
617 let cbput b v =
618 let cap = cbcap b in
619 b.store.(b.wc) <- v;
620 b.wc <- (b.wc + 1) mod cap;
621 b.rc <- b.wc;
622 b.len <- min (b.len + 1) cap;
625 let cbempty b = b.len = 0;;
627 let cbgetg b circular dir =
628 if cbempty b
629 then b.store.(0)
630 else
631 let rc = b.rc + dir in
632 let rc =
633 if circular
634 then (
635 if rc = -1
636 then b.len-1
637 else (
638 if rc >= b.len
639 then 0
640 else rc
643 else bound rc 0 (b.len-1)
645 b.rc <- rc;
646 b.store.(rc);
649 let cbget b = cbgetg b false;;
650 let cbgetc b = cbgetg b true;;
652 let state =
653 { ss = Unix.stdin
654 ; wsfd = Unix.stdin
655 ; stderr = Unix.stderr
656 ; errmsgs = Buffer.create 0
657 ; newerrmsgs = false
658 ; x = 0
659 ; y = 0
660 ; w = 0
661 ; anchor = emptyanchor
662 ; ranchors = []
663 ; layout = []
664 ; maxy = max_int
665 ; tilelru = Queue.create ()
666 ; pagemap = Hashtbl.create 10
667 ; tilemap = Hashtbl.create 10
668 ; pdims = []
669 ; pagecount = 0
670 ; currently = Idle
671 ; mstate = Mnone
672 ; rects = []
673 ; rects1 = []
674 ; prects = Hashtbl.create 1
675 ; text = E.s
676 ; mode = View
677 ; winstate = []
678 ; searchpattern = E.s
679 ; outlines = E.a
680 ; bookmarks = []
681 ; path = E.s
682 ; password = E.s
683 ; nameddest = E.s
684 ; geomcmds = E.s, []
685 ; hists =
686 { nav = cbnew 10 emptyanchor
687 ; pat = cbnew 10 E.s
688 ; pag = cbnew 10 E.s
689 ; sel = cbnew 10 E.s
691 ; memused = 0
692 ; gen = 0
693 ; throttle = None
694 ; autoscroll = None
695 ; ghyll = noghyll
696 ; help = makehelp ()
697 ; docinfo = []
698 ; checkerstexid = None
699 ; prevzoom = (1.0, 0)
700 ; progress = -1.0
701 ; uioh = nouioh
702 ; redisplay = true
703 ; mpos = (-1, -1)
704 ; keystate = KSnone
705 ; glinks = false
706 ; prevcolumns = None
707 ; winw = -1
708 ; winh = -1
709 ; reprf = noreprf
710 ; origin = E.s
711 ; roam = noroam
712 ; bzoom = false
713 ; traw = Raw.create_static `float ~len:8
714 ; vraw = Raw.create_static `float ~len:8
718 let copykeyhashes c =
719 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
722 let calcips h =
723 let d = state.winh - h in
724 max conf.interpagespace ((d + 1) / 2)
727 let rowyh (c, coverA, coverB) b n =
728 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
729 then
730 let _, _, vy, (_, _, h, _) = b.(n) in
731 (vy, h)
732 else
733 let n' = n - coverA in
734 let d = n' mod c in
735 let s = n - d in
736 let e = min state.pagecount (s + c) in
737 let rec find m miny maxh = if m = e then miny, maxh else
738 let _, _, y, (_, _, h, _) = b.(m) in
739 let miny = min miny y in
740 let maxh = max maxh h in
741 find (m+1) miny maxh
742 in find s max_int 0
745 let page_of_y y =
746 let ((c, coverA, coverB) as cl), b =
747 match conf.columns with
748 | Csingle b -> (1, 0, 0), b
749 | Cmulti (c, b) -> c, b
750 | Csplit (_, b) -> (1, 0, 0), b
752 if Array.length b = 0
753 then -1
754 else
755 let rec bsearch nmin nmax =
756 if nmin > nmax
757 then bound nmin 0 (state.pagecount-1)
758 else
759 let n = (nmax + nmin) / 2 in
760 let vy, h = rowyh cl b n in
761 let y0, y1 =
762 if conf.presentation
763 then
764 let ips = calcips h in
765 let y0 = vy - ips in
766 let y1 = vy + h + ips in
767 y0, y1
768 else (
769 if n = 0
770 then 0, vy + h + conf.interpagespace
771 else
772 let y0 = vy - conf.interpagespace in
773 y0, y0 + h + conf.interpagespace
776 if y >= y0 && y < y1
777 then (
778 if c = 1
779 then n
780 else (
781 if n > coverA
782 then
783 if n < state.pagecount - coverB
784 then ((n-coverA)/c)*c + coverA
785 else n
786 else n
789 else (
790 if y > y0
791 then bsearch (n+1) nmax
792 else bsearch nmin (n-1)
795 bsearch 0 (state.pagecount-1);
798 let calcheight () =
799 match conf.columns with
800 | Cmulti ((_, _, _) as cl, b) ->
801 if Array.length b > 0
802 then
803 let y, h = rowyh cl b (Array.length b - 1) in
804 y + h + (if conf.presentation then calcips h else 0)
805 else 0
806 | Csingle b ->
807 if Array.length b > 0
808 then
809 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
810 y + h + (if conf.presentation then calcips h else 0)
811 else 0
812 | Csplit (_, b) ->
813 if Array.length b > 0
814 then
815 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
816 y + h
817 else 0
820 let getpageywh pageno =
821 let pageno = bound pageno 0 (state.pagecount-1) in
822 match conf.columns with
823 | Csingle b ->
824 if Array.length b = 0
825 then 0, 0, 0
826 else
827 let (_, _, y, (_, w, h, _)) = b.(pageno) in
828 let y =
829 if conf.presentation
830 then y - calcips h
831 else y
833 y, w, h
834 | Cmulti (cl, b) ->
835 if Array.length b = 0
836 then 0, 0, 0
837 else
838 let y, h = rowyh cl b pageno in
839 let (_, _, _, (_, w, _, _)) = b.(pageno) in
840 let y =
841 if conf.presentation
842 then y - calcips h
843 else y
845 y, w, h
846 | Csplit (c, b) ->
847 if Array.length b = 0
848 then 0, 0, 0
849 else
850 let n = pageno*c in
851 let (_, _, y, (_, w, h, _)) = b.(n) in
852 y, w / c, h
855 let getpageyh pageno =
856 let y,_,h = getpageywh pageno in
857 y, h;
860 let getpagedim pageno =
861 let rec f ppdim l =
862 match l with
863 | (n, _, _, _) as pdim :: rest ->
864 if n >= pageno
865 then (if n = pageno then pdim else ppdim)
866 else f pdim rest
868 | [] -> ppdim
870 f (-1, -1, -1, -1) state.pdims
873 let getpagey pageno = fst (getpageyh pageno);;
875 let getanchor1 l =
876 let top =
877 let coloff = l.pagecol * l.pageh in
878 float (l.pagey + coloff) /. float l.pageh
880 let dtop =
881 if l.pagedispy = 0
882 then
884 else (
885 if conf.presentation
886 then float l.pagedispy /. float (calcips l.pageh)
887 else float l.pagedispy /. float conf.interpagespace
890 (l.pageno, top, dtop)
893 let getanchor () =
894 match state.layout with
895 | l :: _ -> getanchor1 l
896 | [] ->
897 let n = page_of_y state.y in
898 if n = -1
899 then state.anchor
900 else
901 let y, h = getpageyh n in
902 let dy = y - state.y in
903 let dtop =
904 if conf.presentation
905 then
906 let ips = calcips h in
907 float (dy + ips) /. float ips
908 else
909 float dy /. float conf.interpagespace
911 (n, 0.0, dtop)
914 let fontpath = ref E.s;;
916 type historder = [ `lastvisit | `title | `path | `file ];;
918 module KeyMap =
919 Map.Make (struct type t = (int * int) let compare = compare end);;
921 let unentS s =
922 let l = String.length s in
923 let b = Buffer.create l in
924 Parser.unent b s 0 l;
925 Buffer.contents b;
928 let home =
929 try Sys.getenv "HOME"
930 with exn ->
931 dolog "cannot determine home directory location: %s" @@ exntos exn;
935 let modifier_of_string = function
936 | "alt" -> Wsi.altmask
937 | "shift" -> Wsi.shiftmask
938 | "ctrl" | "control" -> Wsi.ctrlmask
939 | "meta" -> Wsi.metamask
940 | _ -> 0
943 let keys_of_string s =
944 let key_of_string r s =
945 let elems = Str.full_split r s in
946 let f n k m =
947 let g s =
948 let m1 = modifier_of_string s in
949 if m1 = 0
950 then (Wsi.namekey s, m)
951 else (k, m lor m1)
952 in function
953 | Str.Delim s when n land 1 = 0 -> g s
954 | Str.Text s -> g s
955 | Str.Delim _ -> (k, m)
957 let rec loop n k m = function
958 | [] -> (k, m)
959 | x :: xs ->
960 let k, m = f n k m x in
961 loop (n+1) k m xs
963 loop 0 0 0 elems
965 let elems = Str.split whitere s in
966 List.map (key_of_string (Str.regexp "-")) elems
969 let config_of c attrs =
970 let apply c k v =
972 match k with
973 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
974 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
975 | "case-insensitive-search" -> { c with icase = bool_of_string v }
976 | "preload" -> { c with preload = bool_of_string v }
977 | "page-bias" -> { c with pagebias = int_of_string v }
978 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
979 | "horizontal-scroll-step" ->
980 { c with hscrollstep = max (int_of_string v) 1 }
981 | "auto-scroll-step" ->
982 { c with autoscrollstep = max 0 (int_of_string v) }
983 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
984 | "crop-hack" -> { c with crophack = bool_of_string v }
985 | "throttle" ->
986 let mw =
987 match String.lowercase v with
988 | "true" -> Some infinity
989 | "false" -> None
990 | f -> Some (float_of_string f)
992 { c with maxwait = mw }
993 | "highlight-links" -> { c with hlinks = bool_of_string v }
994 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
995 | "vertical-margin" ->
996 { c with interpagespace = max 0 (int_of_string v) }
997 | "zoom" ->
998 let zoom = float_of_string v /. 100. in
999 let zoom = max zoom 0.0 in
1000 { c with zoom = zoom }
1001 | "presentation" -> { c with presentation = bool_of_string v }
1002 | "rotation-angle" -> { c with angle = int_of_string v }
1003 | "width" -> { c with cwinw = max 20 (int_of_string v) }
1004 | "height" -> { c with cwinh = max 20 (int_of_string v) }
1005 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
1006 | "proportional-display" ->
1007 let fm =
1008 if bool_of_string v
1009 then FitProportional
1010 else FitWidth
1012 { c with fitmodel = fm }
1013 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
1014 | "pixmap-cache-size" ->
1015 { c with memlimit = max 2 (int_of_string_with_suffix v) }
1016 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
1017 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
1018 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
1019 | "persistent-location" -> { c with jumpback = bool_of_string v }
1020 | "background-color" -> { c with bgcolor = color_of_string v }
1021 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
1022 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
1023 | "mupdf-store-size" ->
1024 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
1025 | "checkers" -> { c with checkers = bool_of_string v }
1026 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
1027 | "trim-margins" -> { c with trimmargins = bool_of_string v }
1028 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
1029 | "uri-launcher" -> { c with urilauncher = unentS v }
1030 | "path-launcher" -> { c with pathlauncher = unentS v }
1031 | "color-space" -> { c with colorspace = CSTE.of_string v }
1032 | "invert-colors" -> { c with invert = bool_of_string v }
1033 | "brightness" -> { c with colorscale = float_of_string v }
1034 | "ghyllscroll" -> { c with ghyllscroll = ghyllscroll_of_string v }
1035 | "columns" ->
1036 let (n, _, _) as nab = multicolumns_of_string v in
1037 if n < 0
1038 then { c with columns = Csplit (-n, E.a) }
1039 else { c with columns = Cmulti (nab, E.a) }
1040 | "birds-eye-columns" ->
1041 { c with beyecolumns = Some (max (int_of_string v) 2) }
1042 | "selection-command" -> { c with selcmd = unentS v }
1043 | "synctex-command" -> { c with stcmd = unentS v }
1044 | "pax-command" -> { c with paxcmd = unentS v }
1045 | "askpass-command" -> { c with passcmd = unentS v }
1046 | "savepath-command" -> { c with savecmd = unentS v }
1047 | "update-cursor" -> { c with updatecurs = bool_of_string v }
1048 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
1049 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
1050 | "use-pbo" -> { c with usepbo = bool_of_string v }
1051 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
1052 | "horizontal-scrollbar-visible" ->
1053 let b =
1054 if bool_of_string v
1055 then c.scrollb lor scrollbhv
1056 else c.scrollb land (lnot scrollbhv)
1058 { c with scrollb = b }
1059 | "vertical-scrollbar-visible" ->
1060 let b =
1061 if bool_of_string v
1062 then c.scrollb lor scrollbvv
1063 else c.scrollb land (lnot scrollbvv)
1065 { c with scrollb = b }
1066 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
1067 | "point-and-x" ->
1068 { c with pax =
1069 if bool_of_string v
1070 then Some (ref (0.0, 0, 0))
1071 else None }
1072 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
1073 | "scroll-bar-on-the-left" -> { c with leftscroll = bool_of_string v }
1074 | "title" -> { c with title = unentS v }
1075 | "last-visit" -> { c with lastvisit = float_of_string v }
1076 | "edit-annotations-inline" -> { c with annotinline = bool_of_string v }
1077 | _ -> c
1078 with exn ->
1079 dolog "error processing attribute (`%S' = `%S'): %s" k v @@ exntos exn;
1082 let rec fold c = function
1083 | [] -> c
1084 | (k, v) :: rest ->
1085 let c = apply c k v in
1086 fold c rest
1088 fold { c with keyhashes = copykeyhashes c } attrs;
1091 let fromstring f pos n v d =
1092 try f v
1093 with exn ->
1094 dolog "error processing attribute (%S=%S) at %d\n%s" n v pos @@ exntos exn;
1098 let bookmark_of attrs =
1099 let rec fold title page rely visy = function
1100 | ("title", v) :: rest -> fold v page rely visy rest
1101 | ("page", v) :: rest -> fold title v rely visy rest
1102 | ("rely", v) :: rest -> fold title page v visy rest
1103 | ("visy", v) :: rest -> fold title page rely v rest
1104 | _ :: rest -> fold title page rely visy rest
1105 | [] -> title, page, rely, visy
1107 fold "invalid" "0" "0" "0" attrs
1110 let doc_of attrs =
1111 let rec fold path page rely pan visy origin = function
1112 | ("path", v) :: rest -> fold v page rely pan visy origin rest
1113 | ("page", v) :: rest -> fold path v rely pan visy origin rest
1114 | ("rely", v) :: rest -> fold path page v pan visy origin rest
1115 | ("pan", v) :: rest -> fold path page rely v visy origin rest
1116 | ("visy", v) :: rest -> fold path page rely pan v origin rest
1117 | ("origin", v) :: rest -> fold path page rely pan visy v rest
1118 | _ :: rest -> fold path page rely pan visy origin rest
1119 | [] -> path, page, rely, pan, visy, origin
1121 fold E.s "0" "0" "0" "0" E.s attrs
1124 let map_of attrs =
1125 let rec fold rs ls = function
1126 | ("out", v) :: rest -> fold v ls rest
1127 | ("in", v) :: rest -> fold rs v rest
1128 | _ :: rest -> fold ls rs rest
1129 | [] -> ls, rs
1131 fold E.s E.s attrs
1134 let setconf dst src =
1135 dst.scrollbw <- src.scrollbw;
1136 dst.scrollh <- src.scrollh;
1137 dst.icase <- src.icase;
1138 dst.preload <- src.preload;
1139 dst.pagebias <- src.pagebias;
1140 dst.verbose <- src.verbose;
1141 dst.scrollstep <- src.scrollstep;
1142 dst.maxhfit <- src.maxhfit;
1143 dst.crophack <- src.crophack;
1144 dst.autoscrollstep <- src.autoscrollstep;
1145 dst.maxwait <- src.maxwait;
1146 dst.hlinks <- src.hlinks;
1147 dst.underinfo <- src.underinfo;
1148 dst.interpagespace <- src.interpagespace;
1149 dst.zoom <- src.zoom;
1150 dst.presentation <- src.presentation;
1151 dst.angle <- src.angle;
1152 dst.cwinw <- src.cwinw;
1153 dst.cwinh <- src.cwinh;
1154 dst.savebmarks <- src.savebmarks;
1155 dst.memlimit <- src.memlimit;
1156 dst.fitmodel <- src.fitmodel;
1157 dst.texcount <- src.texcount;
1158 dst.sliceheight <- src.sliceheight;
1159 dst.thumbw <- src.thumbw;
1160 dst.jumpback <- src.jumpback;
1161 dst.bgcolor <- src.bgcolor;
1162 dst.tilew <- src.tilew;
1163 dst.tileh <- src.tileh;
1164 dst.mustoresize <- src.mustoresize;
1165 dst.checkers <- src.checkers;
1166 dst.aalevel <- src.aalevel;
1167 dst.trimmargins <- src.trimmargins;
1168 dst.trimfuzz <- src.trimfuzz;
1169 dst.urilauncher <- src.urilauncher;
1170 dst.colorspace <- src.colorspace;
1171 dst.invert <- src.invert;
1172 dst.colorscale <- src.colorscale;
1173 dst.ghyllscroll <- src.ghyllscroll;
1174 dst.columns <- src.columns;
1175 dst.beyecolumns <- src.beyecolumns;
1176 dst.selcmd <- src.selcmd;
1177 dst.updatecurs <- src.updatecurs;
1178 dst.pathlauncher <- src.pathlauncher;
1179 dst.keyhashes <- copykeyhashes src;
1180 dst.hfsize <- src.hfsize;
1181 dst.hscrollstep <- src.hscrollstep;
1182 dst.pgscale <- src.pgscale;
1183 dst.usepbo <- src.usepbo;
1184 dst.wheelbypage <- src.wheelbypage;
1185 dst.stcmd <- src.stcmd;
1186 dst.paxcmd <- src.paxcmd;
1187 dst.passcmd <- src.passcmd;
1188 dst.savecmd <- src.savecmd;
1189 dst.scrollb <- src.scrollb;
1190 dst.riani <- src.riani;
1191 dst.paxmark <- src.paxmark;
1192 dst.leftscroll <- src.leftscroll;
1193 dst.title <- src.title;
1194 dst.annotinline <- src.annotinline;
1195 dst.pax <-
1196 if src.pax = None
1197 then None
1198 else Some ((ref (0.0, 0, 0)));
1201 let findkeyhash c name =
1202 try List.assoc name c.keyhashes
1203 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
1206 let get s =
1207 let open Parser in
1208 let h = Hashtbl.create 10 in
1209 let dc = { defconf with angle = defconf.angle } in
1210 let rec toplevel v t spos _ =
1211 match t with
1212 | Vdata | Vcdata | Vend -> v
1213 | Vopen ("llppconfig", _, closed) ->
1214 if closed
1215 then v
1216 else { v with f = llppconfig }
1217 | Vopen _ ->
1218 error "unexpected subelement at top level" s spos
1219 | Vclose _ -> error "unexpected close at top level" s spos
1221 and llppconfig v t spos _ =
1222 match t with
1223 | Vdata | Vcdata -> v
1224 | Vend -> error "unexpected end of input in llppconfig" s spos
1225 | Vopen ("defaults", attrs, closed) ->
1226 let c = config_of dc attrs in
1227 setconf dc c;
1228 if closed
1229 then v
1230 else { v with f = defaults }
1232 | Vopen ("ui-font", attrs, closed) ->
1233 let rec getsize size = function
1234 | [] -> size
1235 | ("size", v) :: rest ->
1236 let size =
1237 fromstring int_of_string spos "size" v fstate.fontsize in
1238 getsize size rest
1239 | l -> getsize size l
1241 fstate.fontsize <- getsize fstate.fontsize attrs;
1242 if closed
1243 then v
1244 else { v with f = uifont (Buffer.create 10) }
1246 | Vopen ("doc", attrs, closed) ->
1247 let pathent, spage, srely, span, svisy, origin = doc_of attrs in
1248 let path = unentS pathent
1249 and origin = unentS origin
1250 and pageno = fromstring int_of_string spos "page" spage 0
1251 and rely = fromstring float_of_string spos "rely" srely 0.0
1252 and pan = fromstring int_of_string spos "pan" span 0
1253 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1254 let c = config_of dc attrs in
1255 let anchor = (pageno, rely, visy) in
1256 if closed
1257 then (Hashtbl.add h path (c, [], pan, anchor, origin); v)
1258 else { v with f = doc path origin pan anchor c [] }
1260 | Vopen _ ->
1261 error "unexpected subelement in llppconfig" s spos
1263 | Vclose "llppconfig" -> { v with f = toplevel }
1264 | Vclose _ -> error "unexpected close in llppconfig" s spos
1266 and defaults v t spos _ =
1267 match t with
1268 | Vdata | Vcdata -> v
1269 | Vend -> error "unexpected end of input in defaults" s spos
1270 | Vopen ("keymap", attrs, closed) ->
1271 let modename =
1272 try List.assoc "mode" attrs
1273 with Not_found -> "global" in
1274 if closed
1275 then v
1276 else
1277 let ret keymap =
1278 let h = findkeyhash dc modename in
1279 KeyMap.iter (Hashtbl.replace h) keymap;
1280 defaults
1282 { v with f = pkeymap ret KeyMap.empty }
1284 | Vopen (_, _, _) ->
1285 error "unexpected subelement in defaults" s spos
1287 | Vclose "defaults" ->
1288 { v with f = llppconfig }
1290 | Vclose _ -> error "unexpected close in defaults" s spos
1292 and uifont b v t spos epos =
1293 match t with
1294 | Vdata | Vcdata ->
1295 Buffer.add_substring b s spos (epos - spos);
1297 | Vopen (_, _, _) ->
1298 error "unexpected subelement in ui-font" s spos
1299 | Vclose "ui-font" ->
1300 if emptystr !fontpath
1301 then fontpath := Buffer.contents b;
1302 { v with f = llppconfig }
1303 | Vclose _ -> error "unexpected close in ui-font" s spos
1304 | Vend -> error "unexpected end of input in ui-font" s spos
1306 and doc path origin pan anchor c bookmarks v t spos _ =
1307 match t with
1308 | Vdata | Vcdata -> v
1309 | Vend -> error "unexpected end of input in doc" s spos
1310 | Vopen ("bookmarks", _, closed) ->
1311 if closed
1312 then v
1313 else { v with f = pbookmarks path origin pan anchor c bookmarks }
1315 | Vopen ("keymap", attrs, closed) ->
1316 let modename =
1317 try List.assoc "mode" attrs
1318 with Not_found -> "global"
1320 if closed
1321 then v
1322 else
1323 let ret keymap =
1324 let h = findkeyhash c modename in
1325 KeyMap.iter (Hashtbl.replace h) keymap;
1326 doc path origin pan anchor c bookmarks
1328 { v with f = pkeymap ret KeyMap.empty }
1330 | Vopen (_, _, _) ->
1331 error "unexpected subelement in doc" s spos
1333 | Vclose "doc" ->
1334 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor, origin);
1335 { v with f = llppconfig }
1337 | Vclose _ -> error "unexpected close in doc" s spos
1339 and pkeymap ret keymap v t spos _ =
1340 match t with
1341 | Vdata | Vcdata -> v
1342 | Vend -> error "unexpected end of input in keymap" s spos
1343 | Vopen ("map", attrs, closed) ->
1344 let r, l = map_of attrs in
1345 let kss = fromstring keys_of_string spos "in" r [] in
1346 let lss = fromstring keys_of_string spos "out" l [] in
1347 let keymap =
1348 match kss with
1349 | [] -> keymap
1350 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
1351 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
1353 if closed
1354 then { v with f = pkeymap ret keymap }
1355 else
1356 let f () = v in
1357 { v with f = skip "map" f }
1359 | Vopen _ ->
1360 error "unexpected subelement in keymap" s spos
1362 | Vclose "keymap" ->
1363 { v with f = ret keymap }
1365 | Vclose _ -> error "unexpected close in keymap" s spos
1367 and pbookmarks path origin pan anchor c bookmarks v t spos _ =
1368 match t with
1369 | Vdata | Vcdata -> v
1370 | Vend -> error "unexpected end of input in bookmarks" s spos
1371 | Vopen ("item", attrs, closed) ->
1372 let titleent, spage, srely, svisy = bookmark_of attrs in
1373 let page = fromstring int_of_string spos "page" spage 0
1374 and rely = fromstring float_of_string spos "rely" srely 0.0
1375 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
1376 let bookmarks =
1377 (unentS titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
1379 if closed
1380 then { v with f = pbookmarks path origin pan anchor c bookmarks }
1381 else
1382 let f () = v in
1383 { v with f = skip "item" f }
1385 | Vopen _ ->
1386 error "unexpected subelement in bookmarks" s spos
1388 | Vclose "bookmarks" ->
1389 { v with f = doc path origin pan anchor c bookmarks }
1391 | Vclose _ -> Parser.parse_error "unexpected close in bookmarks" s spos
1393 and skip tag f v t spos _ =
1394 match t with
1395 | Vdata | Vcdata -> v
1396 | Vend ->
1397 Parser.parse_error ("unexpected end of input in skipped " ^ tag) s spos
1398 | Vopen (tag', _, closed) ->
1399 if closed
1400 then v
1401 else
1402 let f' () = { v with f = skip tag f } in
1403 { v with f = skip tag' f' }
1404 | Vclose ctag ->
1405 if tag = ctag
1406 then f ()
1407 else Parser.parse_error ("unexpected close in skipped " ^ tag) s spos
1410 parse { f = toplevel; accu = () } s;
1411 h, dc;
1414 let do_load f contents =
1415 try f contents
1416 with
1417 | Parser.Parse_error (msg, s, pos) ->
1418 let subs = Parser.subs s pos in
1419 Utils.error "parse error: %s: at %d [..%S..]" msg pos subs
1421 | exn -> Utils.error "parse error: %s" @@ exntos exn
1424 let defconfpath =
1425 let dir =
1426 let xdgconfdir = Utils.getenvwithdef "XDG_CONFIG_HOME" E.s in
1427 if emptystr xdgconfdir
1428 then
1430 let dir = Filename.concat home ".config" in
1431 if Sys.is_directory dir then dir else home
1432 with _ -> home
1433 else xdgconfdir
1435 Filename.concat dir "llpp.conf"
1438 let confpath = ref defconfpath;;
1440 let load2 f default =
1441 match filecontents !confpath with
1442 | contents -> f @@ do_load get contents
1443 | exception Unix.Unix_error (Unix.ENOENT, "open", _) ->
1444 f (Hashtbl.create 0, defconf)
1445 | exception exn ->
1446 dolog "error loading configuration from `%S': %s" !confpath @@ exntos exn;
1447 default
1450 let load1 f = load2 f false;;
1452 let load openlast =
1453 let f (h, dc) =
1454 if openlast
1455 then (
1456 let path, _ =
1457 Hashtbl.fold
1458 (fun path (conf, _, _, _, _) ((_, besttime) as best) ->
1459 if conf.lastvisit > besttime
1460 then (path, conf.lastvisit)
1461 else best)
1463 (state.path, -.infinity)
1465 state.path <- path;
1467 let pc, pb, px, pa, po =
1469 let absname = abspath state.path in
1470 Hashtbl.find h absname
1471 with Not_found -> dc, [], 0, emptyanchor, state.origin
1473 setconf defconf dc;
1474 setconf conf pc;
1475 state.bookmarks <- pb;
1476 state.x <- px;
1477 state.origin <- po;
1478 if conf.jumpback
1479 then state.anchor <- pa;
1480 cbput state.hists.nav pa;
1481 true
1483 load1 f
1486 let gethist () =
1487 let f (h, _) =
1488 Hashtbl.fold (fun path (pc, pb, px, pa, po) accu ->
1489 (path, pc, pb, px, pa, po) :: accu)
1490 h [];
1492 load2 f []
1495 let add_attrs bb always dc c time =
1496 let o' fmt s =
1497 Buffer.add_string bb "\n ";
1498 Printf.bprintf bb fmt s
1500 let o c fmt s = if c then o' fmt s else ignore in
1501 let ob s a b = o (always || a != b) "%s='%b'" s a
1502 and op s a b = o (always || a <> b) "%s='%b'" s (a != None)
1503 and oi s a b = o (always || a != b) "%s='%d'" s a
1504 and oI s a b = o (always || a != b) "%s='%s'" s (string_with_suffix_of_int a)
1505 and oz s a b = o (always || a <> b) "%s='%g'" s (a*.100.)
1506 and oF s a b = o (always || a <> b) "%s='%f'" s a
1507 and oL s a b = o (always || a <> b) "%s='%Ld'" s a
1508 and oc s a b = o (always || a <> b) "%s='%s'" s (color_to_string a)
1509 and oC s a b = o (always || a <> b) "%s='%s'" s (CSTE.to_string a)
1510 and oR s a b = o (always || a <> b) "%s='%s'" s (irect_to_string a)
1511 and oFm s a b = o (always || a <> b) "%s='%s'" s (FMTE.to_string a)
1512 and oSv s a b m = o (always || a <> b) "%s='%b'" s (a land m != 0)
1513 and oPm s a b = o (always || a <> b) "%s='%s'" s (MTE.to_string a)
1514 and os s a b =
1515 o (always || a <> b) "%s='%s'" s @@ Parser.enent a 0 (String.length a)
1516 and og s a b =
1517 if always || a <> b
1518 then
1519 match a with
1520 | Some (_N, _A, _B) -> o' "%s='%u,%u,%u'" s _N _A _B
1521 | None ->
1522 match b with
1523 | None -> ()
1524 | _ -> o' "%s='none'" s
1525 and oW s a b =
1526 if always || a <> b
1527 then
1528 let v =
1529 match a with
1530 | None -> "false"
1531 | Some f ->
1532 if f = infinity
1533 then "true"
1534 else string_of_float f
1536 o' "%s='%s'" s v
1537 and oco s a b =
1538 if always || a <> b
1539 then
1540 match a with
1541 | Cmulti ((n, a, b), _) when n > 1 -> o' "%s='%d,%d,%d'" s n a b
1542 | Csplit (n, _) when n > 1 -> o' "%s='%d'" s ~-n
1543 | Cmulti _ | Csplit _ | Csingle _ -> ()
1544 and obeco s a b =
1545 if always || a <> b
1546 then
1547 match a with
1548 | Some c when c > 1 -> o' "%s='%d'" s c
1549 | _ -> ()
1551 oi "width" c.cwinw dc.cwinw;
1552 oi "height" c.cwinh dc.cwinh;
1553 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
1554 oi "scroll-handle-height" c.scrollh dc.scrollh;
1555 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
1556 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
1557 ob "case-insensitive-search" c.icase dc.icase;
1558 ob "preload" c.preload dc.preload;
1559 oi "page-bias" c.pagebias dc.pagebias;
1560 oi "scroll-step" c.scrollstep dc.scrollstep;
1561 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
1562 ob "max-height-fit" c.maxhfit dc.maxhfit;
1563 ob "crop-hack" c.crophack dc.crophack;
1564 oW "throttle" c.maxwait dc.maxwait;
1565 ob "highlight-links" c.hlinks dc.hlinks;
1566 ob "under-cursor-info" c.underinfo dc.underinfo;
1567 oi "vertical-margin" c.interpagespace dc.interpagespace;
1568 oz "zoom" c.zoom dc.zoom;
1569 ob "presentation" c.presentation dc.presentation;
1570 oi "rotation-angle" c.angle dc.angle;
1571 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
1572 oFm "fit-model" c.fitmodel dc.fitmodel;
1573 oI "pixmap-cache-size" c.memlimit dc.memlimit;
1574 oi "tex-count" c.texcount dc.texcount;
1575 oi "slice-height" c.sliceheight dc.sliceheight;
1576 oi "thumbnail-width" c.thumbw dc.thumbw;
1577 ob "persistent-location" c.jumpback dc.jumpback;
1578 oc "background-color" c.bgcolor dc.bgcolor;
1579 oi "tile-width" c.tilew dc.tilew;
1580 oi "tile-height" c.tileh dc.tileh;
1581 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
1582 ob "checkers" c.checkers dc.checkers;
1583 oi "aalevel" c.aalevel dc.aalevel;
1584 ob "trim-margins" c.trimmargins dc.trimmargins;
1585 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
1586 os "uri-launcher" c.urilauncher dc.urilauncher;
1587 os "path-launcher" c.pathlauncher dc.pathlauncher;
1588 oC "color-space" c.colorspace dc.colorspace;
1589 ob "invert-colors" c.invert dc.invert;
1590 oF "brightness" c.colorscale dc.colorscale;
1591 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
1592 oco "columns" c.columns dc.columns;
1593 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
1594 os "selection-command" c.selcmd dc.selcmd;
1595 os "synctex-command" c.stcmd dc.stcmd;
1596 os "pax-command" c.paxcmd dc.paxcmd;
1597 os "askpass-command" c.passcmd dc.passcmd;
1598 os "savepath-command" c.savecmd dc.savecmd;
1599 ob "update-cursor" c.updatecurs dc.updatecurs;
1600 oi "hint-font-size" c.hfsize dc.hfsize;
1601 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
1602 oF "page-scroll-scale" c.pgscale dc.pgscale;
1603 ob "use-pbo" c.usepbo dc.usepbo;
1604 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
1605 ob "remote-in-a-new-instance" c.riani dc.riani;
1606 op "point-and-x" c.pax dc.pax;
1607 oPm "point-and-x-mark" c.paxmark dc.paxmark;
1608 ob "scroll-bar-on-the-left" c.leftscroll dc.leftscroll;
1609 if not always
1610 then os "title" c.title dc.title;
1611 oL "last-visit" (Int64.of_float time) 0L;
1612 ob "edit-annotations-inline" c.annotinline dc.annotinline;
1615 let keymapsbuf always dc c =
1616 let bb = Buffer.create 16 in
1617 let rec loop = function
1618 | [] -> ()
1619 | (modename, h) :: rest ->
1620 let dh = findkeyhash dc modename in
1621 if always || h <> dh
1622 then (
1623 if Hashtbl.length h > 0
1624 then (
1625 if Buffer.length bb > 0
1626 then Buffer.add_char bb '\n';
1627 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
1628 Hashtbl.iter (fun i o ->
1629 let isdifferent = always ||
1631 let dO = Hashtbl.find dh i in
1632 dO <> o
1633 with Not_found -> true
1635 if isdifferent
1636 then
1637 let addkm (k, m) =
1638 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
1639 if Wsi.withalt m then Buffer.add_string bb "alt-";
1640 if Wsi.withshift m then Buffer.add_string bb "shift-";
1641 if Wsi.withmeta m then Buffer.add_string bb "meta-";
1642 Buffer.add_string bb (Wsi.keyname k);
1644 let addkms l =
1645 let rec loop = function
1646 | [] -> ()
1647 | km :: [] -> addkm km
1648 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
1650 loop l
1652 Buffer.add_string bb "<map in='";
1653 addkm i;
1654 match o with
1655 | KMinsrt km ->
1656 Buffer.add_string bb "' out='";
1657 addkm km;
1658 Buffer.add_string bb "'/>\n"
1660 | KMinsrl kms ->
1661 Buffer.add_string bb "' out='";
1662 addkms kms;
1663 Buffer.add_string bb "'/>\n"
1665 | KMmulti (ins, kms) ->
1666 Buffer.add_char bb ' ';
1667 addkms ins;
1668 Buffer.add_string bb "' out='";
1669 addkms kms;
1670 Buffer.add_string bb "'/>\n"
1671 ) h;
1672 Buffer.add_string bb "</keymap>";
1675 loop rest
1677 loop c.keyhashes;
1681 let save1 bb leavebirdseye x h dc =
1682 let uifontsize = fstate.fontsize in
1683 let dc = if conf.bedefault then conf else dc in
1684 Buffer.add_string bb "<llppconfig>\n";
1686 if nonemptystr !fontpath
1687 then
1688 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
1689 uifontsize
1690 !fontpath
1691 else (
1692 if uifontsize <> 14
1693 then
1694 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
1697 Buffer.add_string bb "<defaults";
1698 add_attrs bb true dc dc nan;
1699 let kb = keymapsbuf true dc dc in
1700 if Buffer.length kb > 0
1701 then (
1702 Buffer.add_string bb ">\n";
1703 Buffer.add_buffer bb kb;
1704 Buffer.add_string bb "\n</defaults>\n";
1706 else Buffer.add_string bb "/>\n";
1708 let adddoc path pan anchor c bookmarks time origin =
1709 if bookmarks == [] && c = dc && anchor = emptyanchor
1710 then ()
1711 else (
1712 Printf.bprintf bb "<doc path='%s'"
1713 (Parser.enent path 0 (String.length path));
1715 if nonemptystr origin
1716 then
1717 Printf.bprintf bb "\n origin='%s'"
1718 (Parser.enent origin 0 (String.length origin));
1720 if anchor <> emptyanchor
1721 then (
1722 let n, rely, visy = anchor in
1723 Printf.bprintf bb "\n page='%d'" n;
1724 if rely > 1e-6
1725 then
1726 Printf.bprintf bb " rely='%f'" rely
1728 if abs_float visy > 1e-6
1729 then
1730 Printf.bprintf bb " visy='%f'" visy
1734 if pan != 0
1735 then Printf.bprintf bb " pan='%d'" pan;
1737 add_attrs bb false dc c time;
1738 let kb = keymapsbuf false dc c in
1740 begin match bookmarks with
1741 | [] ->
1742 if Buffer.length kb > 0
1743 then (
1744 Buffer.add_string bb ">\n";
1745 Buffer.add_buffer bb kb;
1746 Buffer.add_string bb "\n</doc>\n";
1748 else Buffer.add_string bb "/>\n"
1749 | _ ->
1750 Buffer.add_string bb ">\n<bookmarks>\n";
1751 List.iter (fun (title, _, kind) ->
1752 begin match kind with
1753 | Oanchor (page, rely, visy) ->
1754 Printf.bprintf bb
1755 "<item title='%s' page='%d'"
1756 (Parser.enent title 0 (String.length title))
1757 page
1759 if rely > 1e-6
1760 then
1761 Printf.bprintf bb " rely='%f'" rely
1763 if abs_float visy > 1e-6
1764 then
1765 Printf.bprintf bb " visy='%f'" visy
1767 | Ohistory _ | Onone | Ouri _ | Oremote _
1768 | Oremotedest _ | Olaunch _ ->
1769 failwith "unexpected link in bookmarks"
1770 end;
1771 Buffer.add_string bb "/>\n";
1772 ) bookmarks;
1773 Buffer.add_string bb "</bookmarks>";
1774 if Buffer.length kb > 0
1775 then (
1776 Buffer.add_string bb "\n";
1777 Buffer.add_buffer bb kb;
1779 Buffer.add_string bb "\n</doc>\n";
1780 end;
1784 let pan, conf =
1785 match state.mode with
1786 | Birdseye (c, pan, _, _, _) ->
1787 let beyecolumns =
1788 match conf.columns with
1789 | Cmulti ((c, _, _), _) -> Some c
1790 | Csingle _ -> None
1791 | Csplit _ -> None
1792 and columns =
1793 match c.columns with
1794 | Cmulti (c, _) -> Cmulti (c, E.a)
1795 | Csingle _ -> Csingle E.a
1796 | Csplit _ -> failwith "quit from bird's eye while split"
1798 pan, { c with beyecolumns = beyecolumns; columns = columns }
1799 | Textentry _
1800 | View
1801 | LinkNav _ -> x, conf
1803 let docpath = if nonemptystr state.path then abspath state.path else E.s in
1804 if nonemptystr docpath
1805 then (
1806 adddoc docpath pan (getanchor ())
1808 let autoscrollstep =
1809 match state.autoscroll with
1810 | Some step -> step
1811 | None -> conf.autoscrollstep
1813 begin match state.mode with
1814 | Birdseye beye -> leavebirdseye beye true
1815 | Textentry _
1816 | View
1817 | LinkNav _ -> ()
1818 end;
1819 { conf with autoscrollstep = autoscrollstep }
1821 (if conf.savebmarks then state.bookmarks else [])
1822 (now ())
1823 state.origin
1825 Hashtbl.iter (fun path (c, bookmarks, x, anchor, origin) ->
1826 if docpath <> abspath path
1827 then adddoc path x anchor c bookmarks c.lastvisit origin
1828 ) h;
1829 Buffer.add_string bb "</llppconfig>\n";
1830 true;
1833 let save leavebirdseye =
1834 let relx = float state.x /. float state.winw in
1835 let w, h, x =
1836 let cx w = truncate (relx *. float w) in
1837 List.fold_left
1838 (fun (w, h, x) ws ->
1839 match ws with
1840 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
1841 | Wsi.MaxVert -> (w, conf.cwinh, x)
1842 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
1844 (state.winw, state.winh, state.x) state.winstate
1846 conf.cwinw <- w;
1847 conf.cwinh <- h;
1848 let bb = Buffer.create 32768 in
1849 let save2 (h, dc) =
1850 save1 bb leavebirdseye x h dc
1852 if load1 save2 && Buffer.length bb > 0
1853 then
1855 let tmp = !confpath ^ ".tmp" in
1856 let oc = open_out_bin tmp in
1857 Buffer.output_buffer oc bb;
1858 close_out oc;
1859 Unix.rename tmp !confpath;
1860 with exn ->
1861 dolog "error saving configuration: %s" @@ exntos exn
1864 let gc fd =
1865 let wr s n =
1866 (* This here has a potential of rising SIGPIPE, silently and
1867 seemingly harmlessly (when -gc was supplied an invalid
1868 executable for instance) probably needs revisiting *)
1869 let n' = Unix.write fd (Bytes.of_string s) 0 n in
1870 if n != n'
1871 then Utils.error "Unix.write %d = %d" n n'
1873 let href = ref (Hashtbl.create 0) in
1874 let cref = ref defconf in
1875 let push (h, dc) =
1876 let f path (pc, _pb, _px, _pa, _po) =
1877 let s =
1878 Printf.sprintf "%s\000%ld\000" path (Int32.of_float pc.lastvisit)
1880 wr s (String.length s)
1882 Hashtbl.iter f h;
1883 href := h;
1884 cref := dc;
1885 true
1887 ignore (load1 push);
1888 Unix.shutdown fd Unix.SHUTDOWN_SEND;
1889 let s = fdcontents fd in
1890 let rec f ppos =
1891 match String.index_from s ppos '\000' with
1892 | exception Not_found -> ()
1893 | zpos1 ->
1894 match String.index_from s (zpos1+1) '\000' with
1895 | exception Not_found -> error "invalid gc input in (%S) at %d" s zpos1
1896 | zpos2 ->
1897 let okey = StringLabels.sub s ~pos:ppos ~len:(zpos1-ppos) in
1898 let nkey = StringLabels.sub s ~pos:(zpos1+1) ~len:(zpos2-zpos1-1) in
1899 if emptystr nkey
1900 then (Hashtbl.remove !href okey; f (zpos2+1))
1901 else
1902 match Hashtbl.find !href okey with
1903 | exception Not_found -> Utils.error "gc: cannot find %S" okey
1904 | v ->
1905 Hashtbl.remove !href okey;
1906 Hashtbl.replace !href nkey v;
1907 f (zpos2+1)
1909 f 0;
1910 let bb = Buffer.create 32768 in
1911 let save2 (_h, dc) = save1 bb (fun _ _ -> ()) 0 !href dc in
1912 if load1 save2 && Buffer.length bb > 0
1913 then (
1915 let tmp = !confpath ^ ".tmp" in
1916 let oc = open_out_bin tmp in
1917 Buffer.output_buffer oc bb;
1918 close_out oc;
1919 Unix.rename tmp !confpath;
1920 with exn ->
1921 dolog "error saving configuration: %s" @@ exntos exn
1925 let logcurrently = function
1926 | Idle -> dolog "Idle"
1927 | Loading (l, gen) ->
1928 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1929 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1930 dolog
1931 "Tiling %d[%d,%d] page=%s cs=%s angle"
1932 l.pageno col row (~> pageopaque)
1933 (CSTE.to_string colorspace)
1935 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1936 angle gen conf.angle state.gen
1937 tilew tileh
1938 conf.tilew conf.tileh
1940 | Outlining _ ->
1941 dolog "outlining"