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