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