Kill stray space
[llpp.git] / main.ml
blob96c49e0d5014bc645657aaafe3705c78443cd8dc
1 open Utils;;
3 exception Quit;;
5 type under =
6 | Unone
7 | Ulinkuri of string
8 | Ulinkgoto of (int * int)
9 | Utext of facename
10 | Uunexpected of string
11 | Ulaunch of string
12 | Unamed of string
13 | Uremote of (string * int)
14 and facename = string;;
16 type params = (angle * proportional * trimparams
17 * texcount * sliceheight * memsize
18 * colorspace * fontpath * trimcachepath
19 * haspbo)
20 and pageno = int
21 and width = int
22 and height = int
23 and leftx = int
24 and opaque = string
25 and recttype = int
26 and pixmapsize = int
27 and angle = int
28 and proportional = bool
29 and trimmargins = bool
30 and interpagespace = int
31 and texcount = int
32 and sliceheight = int
33 and gen = int
34 and top = float
35 and dtop = float
36 and fontpath = string
37 and trimcachepath = string
38 and memsize = int
39 and aalevel = int
40 and irect = (int * int * int * int)
41 and trimparams = (trimmargins * irect)
42 and colorspace = | Rgb | Bgr | Gray
43 and haspbo = bool
46 type x = int
47 and y = int
48 and tilex = int
49 and tiley = int
50 and tileparams = (x * y * width * height * tilex * tiley)
53 type link =
54 | Lnotfound
55 | Lfound of int
56 and linkdir =
57 | LDfirst
58 | LDlast
59 | LDfirstvisible of (int * int * int)
60 | LDleft of int
61 | LDright of int
62 | LDdown of int
63 | LDup of int
66 type pagewithlinks =
67 | Pwlnotfound
68 | Pwl of int
71 type keymap =
72 | KMinsrt of key
73 | KMinsrl of key list
74 | KMmulti of key list * key list
75 and key = int * int
76 and keyhash = (key, keymap) Hashtbl.t
77 and keystate =
78 | KSnone
79 | KSinto of (key list * key list)
82 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
83 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
85 type pipe = (Unix.file_descr * Unix.file_descr);;
87 external init : pipe -> params -> unit = "ml_init";;
88 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
89 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
90 external getpdimrect : int -> float array = "ml_getpdimrect";;
91 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
92 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
93 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
94 external measurestr : int -> string -> float = "ml_measure_string";;
95 external getmaxw : unit -> float = "ml_getmaxw";;
96 external postprocess :
97 opaque -> int -> int -> int -> (int * string * int) -> int
98 = "ml_postprocess";;
99 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
100 external platform : unit -> platform = "ml_platform";;
101 external setaalevel : int -> unit = "ml_setaalevel";;
102 external realloctexts : int -> bool = "ml_realloctexts";;
103 external findlink : opaque -> linkdir -> link = "ml_findlink";;
104 external getlink : opaque -> int -> under = "ml_getlink";;
105 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
106 external getlinkcount : opaque -> int = "ml_getlinkcount";;
107 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links"
108 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
109 external getpbo : width -> height -> colorspace -> string = "ml_getpbo";;
110 external freepbo : string -> unit = "ml_freepbo";;
111 external unmappbo : string -> unit = "ml_unmappbo";;
112 external pbousable : unit -> bool = "ml_pbo_usable";;
113 external unproject : opaque -> int -> int -> (int * int) option
114 = "ml_unproject";;
115 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
117 let platform_to_string = function
118 | Punknown -> "unknown"
119 | Plinux -> "Linux"
120 | Posx -> "OSX"
121 | Psun -> "Sun"
122 | Pfreebsd -> "FreeBSD"
123 | Pdragonflybsd -> "DragonflyBSD"
124 | Popenbsd -> "OpenBSD"
125 | Pnetbsd -> "NetBSD"
126 | Pcygwin -> "Cygwin"
129 let platform = platform ();;
131 let now = Unix.gettimeofday;;
133 let popen cmd fda =
134 if platform = Pcygwin
135 then (
136 let sh = "/bin/sh" in
137 let args = [|sh; "-c"; cmd|] in
138 let rec std si so se = function
139 | [] -> si, so, se
140 | (fd, 0) :: rest -> std fd so se rest
141 | (fd, -1) :: rest ->
142 Unix.set_close_on_exec fd;
143 std si so se rest
144 | (_, n) :: _ ->
145 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
147 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
148 ignore (Unix.create_process sh args si so se)
150 else popen cmd fda;
153 type mpos = int * int
154 and mstate =
155 | Msel of (mpos * mpos)
156 | Mpan of mpos
157 | Mscrolly | Mscrollx
158 | Mzoom of (int * int)
159 | Mzoomrect of (mpos * mpos)
160 | Mnone
163 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
164 and onkey = string -> int -> te
165 and ondone = string -> unit
166 and histcancel = unit -> unit
167 and onhist = ((histcmd -> string) * histcancel)
168 and histcmd = HCnext | HCprev | HCfirst | HClast
169 and cancelonempty = bool
170 and te =
171 | TEstop
172 | TEdone of string
173 | TEcont of string
174 | TEswitch of textentry
177 type 'a circbuf =
178 { store : 'a array
179 ; mutable rc : int
180 ; mutable wc : int
181 ; mutable len : int
185 let bound v minv maxv =
186 max minv (min maxv v);
189 let cbnew n v =
190 { store = Array.create n v
191 ; rc = 0
192 ; wc = 0
193 ; len = 0
197 let cbcap b = Array.length b.store;;
199 let cbput b v =
200 let cap = cbcap b in
201 b.store.(b.wc) <- v;
202 b.wc <- (b.wc + 1) mod cap;
203 b.rc <- b.wc;
204 b.len <- min (b.len + 1) cap;
207 let cbempty b = b.len = 0;;
209 let cbgetg b circular dir =
210 if cbempty b
211 then b.store.(0)
212 else
213 let rc = b.rc + dir in
214 let rc =
215 if circular
216 then (
217 if rc = -1
218 then b.len-1
219 else (
220 if rc >= b.len
221 then 0
222 else rc
225 else bound rc 0 (b.len-1)
227 b.rc <- rc;
228 b.store.(rc);
231 let cbget b = cbgetg b false;;
232 let cbgetc b = cbgetg b true;;
234 let drawstring size x y s =
235 Gl.enable `blend;
236 Gl.enable `texture_2d;
237 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
238 ignore (drawstr size x y s);
239 Gl.disable `blend;
240 Gl.disable `texture_2d;
243 let drawstring1 size x y s =
244 drawstr size x y s;
247 let drawstring2 size x y fmt =
248 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
251 type page =
252 { pageno : int
253 ; pagedimno : int
254 ; pagew : int
255 ; pageh : int
256 ; pagex : int
257 ; pagey : int
258 ; pagevw : int
259 ; pagevh : int
260 ; pagedispx : int
261 ; pagedispy : int
262 ; pagecol : int
266 let debugl l =
267 dolog "l %d dim=%d {" l.pageno l.pagedimno;
268 dolog " WxH %dx%d" l.pagew l.pageh;
269 dolog " vWxH %dx%d" l.pagevw l.pagevh;
270 dolog " pagex,y %d,%d" l.pagex l.pagey;
271 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
272 dolog " column %d" l.pagecol;
273 dolog "}";
276 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
277 dolog "rect {";
278 dolog " x0,y0=(% f, % f)" x0 y0;
279 dolog " x1,y1=(% f, % f)" x1 y1;
280 dolog " x2,y2=(% f, % f)" x2 y2;
281 dolog " x3,y3=(% f, % f)" x3 y3;
282 dolog "}";
285 type multicolumns = multicol * pagegeom
286 and singlecolumn = pagegeom
287 and splitcolumns = columncount * pagegeom
288 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
289 and multicol = columncount * covercount * covercount
290 and pdimno = int
291 and columncount = int
292 and covercount = int;;
294 type conf =
295 { mutable scrollbw : int
296 ; mutable scrollh : int
297 ; mutable icase : bool
298 ; mutable preload : bool
299 ; mutable pagebias : int
300 ; mutable verbose : bool
301 ; mutable debug : bool
302 ; mutable scrollstep : int
303 ; mutable hscrollstep : int
304 ; mutable maxhfit : bool
305 ; mutable crophack : bool
306 ; mutable autoscrollstep : int
307 ; mutable maxwait : float option
308 ; mutable hlinks : bool
309 ; mutable underinfo : bool
310 ; mutable interpagespace : interpagespace
311 ; mutable zoom : float
312 ; mutable presentation : bool
313 ; mutable angle : angle
314 ; mutable cwinw : int
315 ; mutable cwinh : int
316 ; mutable savebmarks : bool
317 ; mutable proportional : proportional
318 ; mutable trimmargins : trimmargins
319 ; mutable trimfuzz : irect
320 ; mutable memlimit : memsize
321 ; mutable texcount : texcount
322 ; mutable sliceheight : sliceheight
323 ; mutable thumbw : width
324 ; mutable jumpback : bool
325 ; mutable bgcolor : float * float * float
326 ; mutable bedefault : bool
327 ; mutable scrollbarinpm : bool
328 ; mutable tilew : int
329 ; mutable tileh : int
330 ; mutable mustoresize : memsize
331 ; mutable checkers : bool
332 ; mutable aalevel : int
333 ; mutable urilauncher : string
334 ; mutable pathlauncher : string
335 ; mutable colorspace : colorspace
336 ; mutable invert : bool
337 ; mutable colorscale : float
338 ; mutable redirectstderr : bool
339 ; mutable ghyllscroll : (int * int * int) option
340 ; mutable columns : columns
341 ; mutable beyecolumns : columncount option
342 ; mutable selcmd : string
343 ; mutable updatecurs : bool
344 ; mutable keyhashes : (string * keyhash) list
345 ; mutable hfsize : int
346 ; mutable pgscale : float
347 ; mutable usepbo : bool
348 ; mutable wheelbypage : bool
349 ; mutable stcmd : string
351 and columns =
352 | Csingle of singlecolumn
353 | Cmulti of multicolumns
354 | Csplit of splitcolumns
357 type anchor = pageno * top * dtop;;
359 type outline = string * int * anchor;;
361 type rect = float * float * float * float * float * float * float * float;;
363 type tile = opaque * pixmapsize * elapsed
364 and elapsed = float;;
365 type pagemapkey = pageno * gen;;
366 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
367 and row = int
368 and col = int;;
370 let emptyanchor = (0, 0.0, 0.0);;
372 type infochange = | Memused | Docinfo | Pdim;;
374 class type uioh = object
375 method display : unit
376 method key : int -> int -> uioh
377 method button : int -> bool -> int -> int -> int -> uioh
378 method motion : int -> int -> uioh
379 method pmotion : int -> int -> uioh
380 method infochanged : infochange -> unit
381 method scrollpw : (int * float * float)
382 method scrollph : (int * float * float)
383 method modehash : keyhash
384 end;;
386 type mode =
387 | Birdseye of (conf * leftx * pageno * pageno * anchor)
388 | Textentry of (textentry * onleave)
389 | View
390 | LinkNav of linktarget
391 and onleave = leavetextentrystatus -> unit
392 and leavetextentrystatus = | Cancel | Confirm
393 and helpitem = string * int * action
394 and action =
395 | Noaction
396 | Action of (uioh -> uioh)
397 and linktarget =
398 | Ltexact of (pageno * int)
399 | Ltgendir of int
402 let isbirdseye = function Birdseye _ -> true | _ -> false;;
403 let istextentry = function Textentry _ -> true | _ -> false;;
405 type currently =
406 | Idle
407 | Loading of (page * gen)
408 | Tiling of (
409 page * opaque * colorspace * angle * gen * col * row * width * height
411 | Outlining of outline list
414 let emptykeyhash = Hashtbl.create 0;;
415 let nouioh : uioh = object (self)
416 method display = ()
417 method key _ _ = self
418 method button _ _ _ _ _ = self
419 method motion _ _ = self
420 method pmotion _ _ = self
421 method infochanged _ = ()
422 method scrollpw = (0, nan, nan)
423 method scrollph = (0, nan, nan)
424 method modehash = emptykeyhash
425 end;;
427 type state =
428 { mutable sr : Unix.file_descr
429 ; mutable sw : Unix.file_descr
430 ; mutable wsfd : Unix.file_descr
431 ; mutable errfd : Unix.file_descr option
432 ; mutable stderr : Unix.file_descr
433 ; mutable errmsgs : Buffer.t
434 ; mutable newerrmsgs : bool
435 ; mutable w : int
436 ; mutable x : int
437 ; mutable y : int
438 ; mutable scrollw : int
439 ; mutable hscrollh : int
440 ; mutable anchor : anchor
441 ; mutable ranchors : (string * string * anchor) list
442 ; mutable maxy : int
443 ; mutable layout : page list
444 ; pagemap : (pagemapkey, opaque) Hashtbl.t
445 ; tilemap : (tilemapkey, tile) Hashtbl.t
446 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
447 ; mutable pdims : (pageno * width * height * leftx) list
448 ; mutable pagecount : int
449 ; mutable currently : currently
450 ; mutable mstate : mstate
451 ; mutable searchpattern : string
452 ; mutable rects : (pageno * recttype * rect) list
453 ; mutable rects1 : (pageno * recttype * rect) list
454 ; mutable text : string
455 ; mutable winstate : Wsi.winstate list
456 ; mutable mode : mode
457 ; mutable uioh : uioh
458 ; mutable outlines : outline array
459 ; mutable bookmarks : outline list
460 ; mutable path : string
461 ; mutable password : string
462 ; mutable nameddest : string
463 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
464 ; mutable memused : memsize
465 ; mutable gen : gen
466 ; mutable throttle : (page list * int * float) option
467 ; mutable autoscroll : int option
468 ; mutable ghyll : (int option -> unit)
469 ; mutable help : helpitem array
470 ; mutable docinfo : (int * string) list
471 ; mutable texid : GlTex.texture_id option
472 ; hists : hists
473 ; mutable prevzoom : float
474 ; mutable progress : float
475 ; mutable redisplay : bool
476 ; mutable mpos : mpos
477 ; mutable keystate : keystate
478 ; mutable glinks : bool
479 ; mutable prevcolumns : (columns * float) option
480 ; mutable wthack : bool
481 ; mutable winw : int
482 ; mutable winh : int
483 ; mutable reprf : (unit -> unit)
485 and hists =
486 { pat : string circbuf
487 ; pag : string circbuf
488 ; nav : anchor circbuf
489 ; sel : string circbuf
493 let defconf =
494 { scrollbw = 7
495 ; scrollh = 12
496 ; icase = true
497 ; preload = true
498 ; pagebias = 0
499 ; verbose = false
500 ; debug = false
501 ; scrollstep = 24
502 ; hscrollstep = 24
503 ; maxhfit = true
504 ; crophack = false
505 ; autoscrollstep = 2
506 ; maxwait = None
507 ; hlinks = false
508 ; underinfo = false
509 ; interpagespace = 2
510 ; zoom = 1.0
511 ; presentation = false
512 ; angle = 0
513 ; cwinw = 900
514 ; cwinh = 900
515 ; savebmarks = true
516 ; proportional = true
517 ; trimmargins = false
518 ; trimfuzz = (0,0,0,0)
519 ; memlimit = 32 lsl 20
520 ; texcount = 256
521 ; sliceheight = 24
522 ; thumbw = 76
523 ; jumpback = true
524 ; bgcolor = (0.5, 0.5, 0.5)
525 ; bedefault = false
526 ; scrollbarinpm = true
527 ; tilew = 2048
528 ; tileh = 2048
529 ; mustoresize = 256 lsl 20
530 ; checkers = true
531 ; aalevel = 8
532 ; urilauncher =
533 (match platform with
534 | Plinux | Pfreebsd | Pdragonflybsd
535 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
536 | Posx -> "open \"%s\""
537 | Pcygwin -> "cygstart \"%s\""
538 | Punknown -> "echo %s")
539 ; pathlauncher = "lp \"%s\""
540 ; selcmd =
541 (match platform with
542 | Plinux | Pfreebsd | Pdragonflybsd
543 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
544 | Posx -> "pbcopy"
545 | Pcygwin -> "wsel"
546 | Punknown -> "cat")
547 ; colorspace = Rgb
548 ; invert = false
549 ; colorscale = 1.0
550 ; redirectstderr = false
551 ; ghyllscroll = None
552 ; columns = Csingle [||]
553 ; beyecolumns = None
554 ; updatecurs = false
555 ; hfsize = 12
556 ; pgscale = 1.0
557 ; usepbo = false
558 ; wheelbypage = false
559 ; stcmd = "echo SyncTex"
560 ; keyhashes =
561 let mk n = (n, Hashtbl.create 1) in
562 [ mk "global"
563 ; mk "info"
564 ; mk "help"
565 ; mk "outline"
566 ; mk "listview"
567 ; mk "birdseye"
568 ; mk "textentry"
569 ; mk "links"
570 ; mk "view"
575 let wtmode = ref false;;
577 let findkeyhash c name =
578 try List.assoc name c.keyhashes
579 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
582 let conf = { defconf with angle = defconf.angle };;
584 let pgscale h = truncate (float h *. conf.pgscale);;
586 type fontstate =
587 { mutable fontsize : int
588 ; mutable wwidth : float
589 ; mutable maxrows : int
593 let fstate =
594 { fontsize = 14
595 ; wwidth = nan
596 ; maxrows = -1
600 let geturl s =
601 let colonpos = try String.index s ':' with Not_found -> -1 in
602 let len = String.length s in
603 if colonpos >= 0 && colonpos + 3 < len
604 then (
605 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
606 then
607 let schemestartpos =
608 try String.rindex_from s colonpos ' '
609 with Not_found -> -1
611 let scheme =
612 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
614 match scheme with
615 | "http" | "ftp" | "mailto" ->
616 let epos =
617 try String.index_from s colonpos ' '
618 with Not_found -> len
620 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
621 | _ -> ""
622 else ""
624 else ""
627 let gotouri uri =
628 if String.length conf.urilauncher = 0
629 then print_endline uri
630 else (
631 let url = geturl uri in
632 if String.length url = 0
633 then print_endline uri
634 else
635 let re = Str.regexp "%s" in
636 let command = Str.global_replace re url conf.urilauncher in
637 try popen command []
638 with exn ->
639 Printf.eprintf
640 "failed to execute `%s': %s\n" command (exntos exn);
641 flush stderr;
645 let version () =
646 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
647 (platform_to_string platform) Sys.word_size Sys.ocaml_version
650 let makehelp () =
651 let strings = version () :: "" :: Help.keys in
652 Array.of_list (
653 List.map (fun s ->
654 let url = geturl s in
655 if String.length url > 0
656 then (s, 0, Action (fun u -> gotouri url; u))
657 else (s, 0, Noaction)
658 ) strings);
661 let noghyll _ = ();;
662 let firstgeomcmds = "", [];;
663 let noreprf () = ();;
665 let state =
666 { sr = Unix.stdin
667 ; sw = Unix.stdin
668 ; wsfd = Unix.stdin
669 ; errfd = None
670 ; stderr = Unix.stderr
671 ; errmsgs = Buffer.create 0
672 ; newerrmsgs = false
673 ; x = 0
674 ; y = 0
675 ; w = 0
676 ; scrollw = 0
677 ; hscrollh = 0
678 ; anchor = emptyanchor
679 ; ranchors = []
680 ; layout = []
681 ; maxy = max_int
682 ; tilelru = Queue.create ()
683 ; pagemap = Hashtbl.create 10
684 ; tilemap = Hashtbl.create 10
685 ; pdims = []
686 ; pagecount = 0
687 ; currently = Idle
688 ; mstate = Mnone
689 ; rects = []
690 ; rects1 = []
691 ; text = ""
692 ; mode = View
693 ; winstate = []
694 ; searchpattern = ""
695 ; outlines = [||]
696 ; bookmarks = []
697 ; path = ""
698 ; password = ""
699 ; nameddest = ""
700 ; geomcmds = firstgeomcmds
701 ; hists =
702 { nav = cbnew 10 emptyanchor
703 ; pat = cbnew 10 ""
704 ; pag = cbnew 10 ""
705 ; sel = cbnew 10 ""
707 ; memused = 0
708 ; gen = 0
709 ; throttle = None
710 ; autoscroll = None
711 ; ghyll = noghyll
712 ; help = makehelp ()
713 ; docinfo = []
714 ; texid = None
715 ; prevzoom = 1.0
716 ; progress = -1.0
717 ; uioh = nouioh
718 ; redisplay = true
719 ; mpos = (-1, -1)
720 ; keystate = KSnone
721 ; glinks = false
722 ; prevcolumns = None
723 ; wthack = false
724 ; winw = -1
725 ; winh = -1
726 ; reprf = noreprf
730 let setfontsize n =
731 fstate.fontsize <- n;
732 fstate.wwidth <- measurestr fstate.fontsize "w";
733 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
736 let vlog fmt =
737 if conf.verbose
738 then
739 Printf.kprintf prerr_endline fmt
740 else
741 Printf.kprintf ignore fmt
744 let launchpath () =
745 if String.length conf.pathlauncher = 0
746 then print_endline state.path
747 else (
748 let re = Str.regexp "%s" in
749 let command = Str.global_replace re state.path conf.pathlauncher in
750 try popen command []
751 with exn ->
752 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
753 flush stderr;
757 module Ne = struct
758 type 'a t = | Res of 'a | Exn of exn;;
760 let pipe () =
761 try Res (Unix.pipe ())
762 with exn -> Exn exn
765 let clo fd f =
766 try tempfailureretry Unix.close fd
767 with exn -> f (exntos exn)
770 let dup fd =
771 try Res (tempfailureretry Unix.dup fd)
772 with exn -> Exn exn
775 let dup2 fd1 fd2 =
776 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
777 with exn -> Exn exn
779 end;;
781 let redirectstderr () =
782 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
783 if conf.redirectstderr
784 then
785 match Ne.pipe () with
786 | Ne.Exn exn ->
787 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
789 | Ne.Res (r, w) ->
790 begin match Ne.dup Unix.stderr with
791 | Ne.Exn exn ->
792 dolog "failed to dup stderr: %s" (exntos exn);
793 Ne.clo r (clofail "pipe/r");
794 Ne.clo w (clofail "pipe/w");
796 | Ne.Res dupstderr ->
797 begin match Ne.dup2 w Unix.stderr with
798 | Ne.Exn exn ->
799 dolog "failed to dup2 to stderr: %s" (exntos exn);
800 Ne.clo dupstderr (clofail "stderr duplicate");
801 Ne.clo r (clofail "redir pipe/r");
802 Ne.clo w (clofail "redir pipe/w");
804 | Ne.Res () ->
805 state.stderr <- dupstderr;
806 state.errfd <- Some r;
807 end;
809 else (
810 state.newerrmsgs <- false;
811 begin match state.errfd with
812 | Some fd ->
813 begin match Ne.dup2 state.stderr Unix.stderr with
814 | Ne.Exn exn ->
815 dolog "failed to dup2 original stderr: %s" (exntos exn)
816 | Ne.Res () ->
817 Ne.clo fd (clofail "dup of stderr");
818 state.errfd <- None;
819 end;
820 | None -> ()
821 end;
822 prerr_string (Buffer.contents state.errmsgs);
823 flush stderr;
824 Buffer.clear state.errmsgs;
828 module G =
829 struct
830 let postRedisplay who =
831 if conf.verbose
832 then prerr_endline ("redisplay for " ^ who);
833 state.redisplay <- true;
835 end;;
837 let getopaque pageno =
838 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
839 with Not_found -> None
842 let putopaque pageno opaque =
843 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
846 let pagetranslatepoint l x y =
847 let dy = y - l.pagedispy in
848 let y = dy + l.pagey in
849 let dx = x - l.pagedispx in
850 let x = dx + l.pagex in
851 (x, y);
854 let onppundermouse g x y d =
855 let rec f = function
856 | l :: rest ->
857 begin match getopaque l.pageno with
858 | Some opaque ->
859 let x0 = l.pagedispx in
860 let x1 = x0 + l.pagevw in
861 let y0 = l.pagedispy in
862 let y1 = y0 + l.pagevh in
863 if y >= y0 && y <= y1 && x >= x0 && x <= x1
864 then
865 let px, py = pagetranslatepoint l x y in
866 match g opaque l px py with
867 | Some res -> res
868 | None -> f rest
869 else f rest
870 | _ ->
871 f rest
873 | [] -> d
875 f state.layout
878 let getunder x y =
879 let g opaque _ px py =
880 match whatsunder opaque px py with
881 | Unone -> None
882 | under -> Some under
884 onppundermouse g x y Unone
887 let unproject x y =
888 let g opaque l x y =
889 match unproject opaque x y with
890 | Some (x, y) -> Some (Some (l.pageno, x, y))
891 | None -> None
893 onppundermouse g x y None;
896 let showtext c s =
897 state.text <- Printf.sprintf "%c%s" c s;
898 G.postRedisplay "showtext";
901 let undertext = function
902 | Unone -> "none"
903 | Ulinkuri s -> s
904 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
905 | Utext s -> "font: " ^ s
906 | Uunexpected s -> "unexpected: " ^ s
907 | Ulaunch s -> "launch: " ^ s
908 | Unamed s -> "named: " ^ s
909 | Uremote (filename, pageno) ->
910 Printf.sprintf "%s: page %d" filename (pageno+1)
913 let updateunder x y =
914 match getunder x y with
915 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
916 | Ulinkuri uri ->
917 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
918 Wsi.setcursor Wsi.CURSOR_INFO
919 | Ulinkgoto (pageno, _) ->
920 if conf.underinfo
921 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
922 Wsi.setcursor Wsi.CURSOR_INFO
923 | Utext s ->
924 if conf.underinfo then showtext 'f' ("ont: " ^ s);
925 Wsi.setcursor Wsi.CURSOR_TEXT
926 | Uunexpected s ->
927 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
928 Wsi.setcursor Wsi.CURSOR_INHERIT
929 | Ulaunch s ->
930 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
931 Wsi.setcursor Wsi.CURSOR_INHERIT
932 | Unamed s ->
933 if conf.underinfo then showtext 'n' ("amed: " ^ s);
934 Wsi.setcursor Wsi.CURSOR_INHERIT
935 | Uremote (filename, pageno) ->
936 if conf.underinfo then showtext 'r'
937 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
938 Wsi.setcursor Wsi.CURSOR_INFO
941 let showlinktype under =
942 if conf.underinfo
943 then
944 match under with
945 | Unone -> ()
946 | under ->
947 let s = undertext under in
948 showtext ' ' s
951 let addchar s c =
952 let b = Buffer.create (String.length s + 1) in
953 Buffer.add_string b s;
954 Buffer.add_char b c;
955 Buffer.contents b;
958 let colorspace_of_string s =
959 match String.lowercase s with
960 | "rgb" -> Rgb
961 | "bgr" -> Bgr
962 | "gray" -> Gray
963 | _ -> failwith "invalid colorspace"
966 let int_of_colorspace = function
967 | Rgb -> 0
968 | Bgr -> 1
969 | Gray -> 2
972 let colorspace_of_int = function
973 | 0 -> Rgb
974 | 1 -> Bgr
975 | 2 -> Gray
976 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
979 let colorspace_to_string = function
980 | Rgb -> "rgb"
981 | Bgr -> "bgr"
982 | Gray -> "gray"
985 let intentry_with_suffix text key =
986 let c =
987 if key >= 32 && key < 127
988 then Char.chr key
989 else '\000'
991 match Char.lowercase c with
992 | '0' .. '9' ->
993 let text = addchar text c in
994 TEcont text
996 | 'k' | 'm' | 'g' ->
997 let text = addchar text c in
998 TEcont text
1000 | _ ->
1001 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1002 TEcont text
1005 let multicolumns_to_string (n, a, b) =
1006 if a = 0 && b = 0
1007 then Printf.sprintf "%d" n
1008 else Printf.sprintf "%d,%d,%d" n a b;
1011 let multicolumns_of_string s =
1013 (int_of_string s, 0, 0)
1014 with _ ->
1015 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1016 if a > 1 || b > 1
1017 then failwith "subtly broken"; (n, a, b)
1021 let readcmd fd =
1022 let s = "xxxx" in
1023 let n = tempfailureretry (Unix.read fd s 0) 4 in
1024 if n != 4 then failwith "incomplete read(len)";
1025 let len = 0
1026 lor (Char.code s.[0] lsl 24)
1027 lor (Char.code s.[1] lsl 16)
1028 lor (Char.code s.[2] lsl 8)
1029 lor (Char.code s.[3] lsl 0)
1031 let s = String.create len in
1032 let n = tempfailureretry (Unix.read fd s 0) len in
1033 if n != len then failwith "incomplete read(data)";
1037 let btod b = if b then 1 else 0;;
1039 let wcmd fmt =
1040 let b = Buffer.create 16 in
1041 Buffer.add_string b "llll";
1042 Printf.kbprintf
1043 (fun b ->
1044 let s = Buffer.contents b in
1045 let n = String.length s in
1046 let len = n - 4 in
1047 (* dolog "wcmd %S" (String.sub s 4 len); *)
1048 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1049 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1050 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1051 s.[3] <- Char.chr (len land 0xff);
1052 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1053 if n' != n then failwith "write failed";
1054 ) b fmt;
1057 let calcips h =
1058 let d = state.winh - h in
1059 max conf.interpagespace ((d + 1) / 2)
1062 let rowyh (c, coverA, coverB) b n =
1063 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1064 then
1065 let _, _, vy, (_, _, h, _) = b.(n) in
1066 (vy, h)
1067 else
1068 let n' = n - coverA in
1069 let d = n' mod c in
1070 let s = n - d in
1071 let e = min state.pagecount (s + c) in
1072 let rec find m miny maxh = if m = e then miny, maxh else
1073 let _, _, y, (_, _, h, _) = b.(m) in
1074 let miny = min miny y in
1075 let maxh = max maxh h in
1076 find (m+1) miny maxh
1077 in find s max_int 0
1080 let calcheight () =
1081 match conf.columns with
1082 | Cmulti ((_, _, _) as cl, b) ->
1083 if Array.length b > 0
1084 then
1085 let y, h = rowyh cl b (Array.length b - 1) in
1086 y + h + (if conf.presentation then calcips h else 0)
1087 else 0
1088 | Csingle b ->
1089 if Array.length b > 0
1090 then
1091 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1092 y + h + (if conf.presentation then calcips h else 0)
1093 else 0
1094 | Csplit (_, b) ->
1095 if Array.length b > 0
1096 then
1097 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1098 y + h
1099 else 0
1102 let getpageyh pageno =
1103 let pageno = bound pageno 0 (state.pagecount-1) in
1104 match conf.columns with
1105 | Csingle b ->
1106 if Array.length b = 0
1107 then 0, 0
1108 else
1109 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1110 let y =
1111 if conf.presentation
1112 then y - calcips h
1113 else y
1115 y, h
1116 | Cmulti (cl, b) ->
1117 if Array.length b = 0
1118 then 0, 0
1119 else
1120 let y, h = rowyh cl b pageno in
1121 let y =
1122 if conf.presentation
1123 then y - calcips h
1124 else y
1126 y, h
1127 | Csplit (c, b) ->
1128 if Array.length b = 0
1129 then 0, 0
1130 else
1131 let n = pageno*c in
1132 let (_, _, y, (_, _, h, _)) = b.(n) in
1133 y, h
1136 let getpagedim pageno =
1137 let rec f ppdim l =
1138 match l with
1139 | (n, _, _, _) as pdim :: rest ->
1140 if n >= pageno
1141 then (if n = pageno then pdim else ppdim)
1142 else f pdim rest
1144 | [] -> ppdim
1146 f (-1, -1, -1, -1) state.pdims
1149 let getpagey pageno = fst (getpageyh pageno);;
1151 let nogeomcmds cmds =
1152 match cmds with
1153 | s, [] -> String.length s = 0
1154 | _ -> false
1157 let page_of_y y =
1158 let ((c, coverA, coverB) as cl), b =
1159 match conf.columns with
1160 | Csingle b -> (1, 0, 0), b
1161 | Cmulti (c, b) -> c, b
1162 | Csplit (_, b) -> (1, 0, 0), b
1164 if Array.length b = 0
1165 then -1
1166 else
1167 let rec bsearch nmin nmax =
1168 if nmin > nmax
1169 then bound nmin 0 (state.pagecount-1)
1170 else
1171 let n = (nmax + nmin) / 2 in
1172 let vy, h = rowyh cl b n in
1173 let y0, y1 =
1174 if conf.presentation
1175 then
1176 let ips = calcips h in
1177 let y0 = vy - ips in
1178 let y1 = vy + h + ips in
1179 y0, y1
1180 else (
1181 if n = 0
1182 then 0, vy + h + conf.interpagespace
1183 else
1184 let y0 = vy - conf.interpagespace in
1185 y0, y0 + h + conf.interpagespace
1188 if y >= y0 && y < y1
1189 then (
1190 if c = 1
1191 then n
1192 else (
1193 if n > coverA
1194 then
1195 if n < state.pagecount - coverB
1196 then ((n-coverA)/c)*c + coverA
1197 else n
1198 else n
1201 else (
1202 if y > y0
1203 then bsearch (n+1) nmax
1204 else bsearch nmin (n-1)
1207 let r = bsearch 0 (state.pagecount-1) in
1211 let layoutN ((columns, coverA, coverB), b) y sh =
1212 let sh = sh - state.hscrollh in
1213 let rec fold accu n =
1214 if n = Array.length b
1215 then accu
1216 else
1217 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1218 if (vy - y) > sh &&
1219 (n = coverA - 1
1220 || n = state.pagecount - coverB
1221 || (n - coverA) mod columns = columns - 1)
1222 then accu
1223 else
1224 let accu =
1225 if vy + h > y
1226 then
1227 let pagey = max 0 (y - vy) in
1228 let pagedispy = if pagey > 0 then 0 else vy - y in
1229 let pagedispx, pagex =
1230 let pdx =
1231 if n = coverA - 1 || n = state.pagecount - coverB
1232 then state.x + (state.winw - state.scrollw - w) / 2
1233 else dx + xoff + state.x
1235 if pdx < 0
1236 then 0, -pdx
1237 else pdx, 0
1239 let pagevw =
1240 let vw = state.winw - state.scrollw - pagedispx in
1241 let pw = w - pagex in
1242 min vw pw
1244 let pagevh = min (h - pagey) (sh - pagedispy) in
1245 if pagevw > 0 && pagevh > 0
1246 then
1247 let e =
1248 { pageno = n
1249 ; pagedimno = pdimno
1250 ; pagew = w
1251 ; pageh = h
1252 ; pagex = pagex
1253 ; pagey = pagey
1254 ; pagevw = pagevw
1255 ; pagevh = pagevh
1256 ; pagedispx = pagedispx
1257 ; pagedispy = pagedispy
1258 ; pagecol = 0
1261 e :: accu
1262 else
1263 accu
1264 else
1265 accu
1267 fold accu (n+1)
1269 List.rev (fold [] (page_of_y y));
1272 let layoutS (columns, b) y sh =
1273 let sh = sh - state.hscrollh in
1274 let rec fold accu n =
1275 if n = Array.length b
1276 then accu
1277 else
1278 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1279 if (vy - y) > sh
1280 then accu
1281 else
1282 let accu =
1283 if vy + pageh > y
1284 then
1285 let x = xoff + state.x in
1286 let pagey = max 0 (y - vy) in
1287 let pagedispy = if pagey > 0 then 0 else vy - y in
1288 let pagedispx, pagex =
1289 if px = 0
1290 then (
1291 if x < 0
1292 then 0, -x
1293 else x, 0
1295 else (
1296 let px = px - x in
1297 if px < 0
1298 then -px, 0
1299 else 0, px
1302 let pagecolw = pagew/columns in
1303 let pagedispx =
1304 if pagecolw < state.winw
1305 then pagedispx + ((state.winw - state.scrollw - pagecolw) / 2)
1306 else pagedispx
1308 let pagevw =
1309 let vw = state.winw - pagedispx - state.scrollw in
1310 let pw = pagew - pagex in
1311 min vw pw
1313 let pagevw = min pagevw pagecolw in
1314 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1315 if pagevw > 0 && pagevh > 0
1316 then
1317 let e =
1318 { pageno = n/columns
1319 ; pagedimno = pdimno
1320 ; pagew = pagew
1321 ; pageh = pageh
1322 ; pagex = pagex
1323 ; pagey = pagey
1324 ; pagevw = pagevw
1325 ; pagevh = pagevh
1326 ; pagedispx = pagedispx
1327 ; pagedispy = pagedispy
1328 ; pagecol = n mod columns
1331 e :: accu
1332 else
1333 accu
1334 else
1335 accu
1337 fold accu (n+1)
1339 List.rev (fold [] 0)
1342 let layout y sh =
1343 if nogeomcmds state.geomcmds
1344 then
1345 match conf.columns with
1346 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1347 | Cmulti c -> layoutN c y sh
1348 | Csplit s -> layoutS s y sh
1349 else []
1352 let clamp incr =
1353 let y = state.y + incr in
1354 let y = max 0 y in
1355 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1359 let itertiles l f =
1360 let tilex = l.pagex mod conf.tilew in
1361 let tiley = l.pagey mod conf.tileh in
1363 let col = l.pagex / conf.tilew in
1364 let row = l.pagey / conf.tileh in
1366 let rec rowloop row y0 dispy h =
1367 if h = 0
1368 then ()
1369 else (
1370 let dh = conf.tileh - y0 in
1371 let dh = min h dh in
1372 let rec colloop col x0 dispx w =
1373 if w = 0
1374 then ()
1375 else (
1376 let dw = conf.tilew - x0 in
1377 let dw = min w dw in
1379 f col row dispx dispy x0 y0 dw dh;
1380 colloop (col+1) 0 (dispx+dw) (w-dw)
1383 colloop col tilex l.pagedispx l.pagevw;
1384 rowloop (row+1) 0 (dispy+dh) (h-dh)
1387 if l.pagevw > 0 && l.pagevh > 0
1388 then rowloop row tiley l.pagedispy l.pagevh;
1391 let gettileopaque l col row =
1392 let key =
1393 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1395 try Some (Hashtbl.find state.tilemap key)
1396 with Not_found -> None
1399 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1400 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1401 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1404 let drawtiles l color =
1405 GlDraw.color color;
1406 let f col row x y tilex tiley w h =
1407 match gettileopaque l col row with
1408 | Some (opaque, _, t) ->
1409 let params = x, y, w, h, tilex, tiley in
1410 if conf.invert
1411 then (
1412 Gl.enable `blend;
1413 GlFunc.blend_func `zero `one_minus_src_color;
1415 drawtile params opaque;
1416 if conf.invert
1417 then Gl.disable `blend;
1418 if conf.debug
1419 then (
1420 let s = Printf.sprintf
1421 "%d[%d,%d] %f sec"
1422 l.pageno col row t
1424 let w = measurestr fstate.fontsize s in
1425 GlMisc.push_attrib [`current];
1426 GlDraw.color (0.0, 0.0, 0.0);
1427 GlDraw.rect
1428 (float (x-2), float (y-2))
1429 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1430 GlDraw.color (1.0, 1.0, 1.0);
1431 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1432 GlMisc.pop_attrib ();
1435 | _ ->
1436 let w =
1437 let lw = state.winw - state.scrollw - x in
1438 min lw w
1439 and h =
1440 let lh = state.winh - y in
1441 min lh h
1443 begin match state.texid with
1444 | Some id ->
1445 Gl.enable `texture_2d;
1446 GlTex.bind_texture `texture_2d id;
1447 let x0 = float x
1448 and y0 = float y
1449 and x1 = float (x+w)
1450 and y1 = float (y+h) in
1452 let tw = float w /. 16.0
1453 and th = float h /. 16.0 in
1454 let tx0 = float tilex /. 16.0
1455 and ty0 = float tiley /. 16.0 in
1456 let tx1 = tx0 +. tw
1457 and ty1 = ty0 +. th in
1458 GlDraw.begins `quads;
1459 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1460 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1461 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1462 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1463 GlDraw.ends ();
1465 Gl.disable `texture_2d;
1466 | None ->
1467 GlDraw.color (1.0, 1.0, 1.0);
1468 GlDraw.rect
1469 (float x, float y)
1470 (float (x+w), float (y+h));
1471 end;
1472 if w > 128 && h > fstate.fontsize + 10
1473 then (
1474 GlDraw.color (0.0, 0.0, 0.0);
1475 let c, r =
1476 if conf.verbose
1477 then (col*conf.tilew, row*conf.tileh)
1478 else col, row
1480 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1482 GlDraw.color color;
1484 itertiles l f
1487 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1489 let tilevisible1 l x y =
1490 let ax0 = l.pagex
1491 and ax1 = l.pagex + l.pagevw
1492 and ay0 = l.pagey
1493 and ay1 = l.pagey + l.pagevh in
1495 let bx0 = x
1496 and by0 = y in
1497 let bx1 = min (bx0 + conf.tilew) l.pagew
1498 and by1 = min (by0 + conf.tileh) l.pageh in
1500 let rx0 = max ax0 bx0
1501 and ry0 = max ay0 by0
1502 and rx1 = min ax1 bx1
1503 and ry1 = min ay1 by1 in
1505 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1506 nonemptyintersection
1509 let tilevisible layout n x y =
1510 let rec findpageinlayout m = function
1511 | l :: rest when l.pageno = n ->
1512 tilevisible1 l x y || (
1513 match conf.columns with
1514 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1515 | _ -> false
1517 | _ :: rest -> findpageinlayout 0 rest
1518 | [] -> false
1520 findpageinlayout 0 layout;
1523 let tileready l x y =
1524 tilevisible1 l x y &&
1525 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1528 let tilepage n p layout =
1529 let rec loop = function
1530 | l :: rest ->
1531 if l.pageno = n
1532 then
1533 let f col row _ _ _ _ _ _ =
1534 if state.currently = Idle
1535 then
1536 match gettileopaque l col row with
1537 | Some _ -> ()
1538 | None ->
1539 let x = col*conf.tilew
1540 and y = row*conf.tileh in
1541 let w =
1542 let w = l.pagew - x in
1543 min w conf.tilew
1545 let h =
1546 let h = l.pageh - y in
1547 min h conf.tileh
1549 let pbo =
1550 if conf.usepbo
1551 then getpbo w h conf.colorspace
1552 else "0"
1554 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1555 state.currently <-
1556 Tiling (
1557 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1558 conf.tilew, conf.tileh
1561 itertiles l f;
1562 else
1563 loop rest
1565 | [] -> ()
1567 if nogeomcmds state.geomcmds
1568 then loop layout;
1571 let preloadlayout y =
1572 let y = if y < state.winh then 0 else y - state.winh in
1573 let h = state.winh*3 in
1574 layout y h;
1577 let load pages =
1578 let rec loop pages =
1579 if state.currently != Idle
1580 then ()
1581 else
1582 match pages with
1583 | l :: rest ->
1584 begin match getopaque l.pageno with
1585 | None ->
1586 wcmd "page %d %d" l.pageno l.pagedimno;
1587 state.currently <- Loading (l, state.gen);
1588 | Some opaque ->
1589 tilepage l.pageno opaque pages;
1590 loop rest
1591 end;
1592 | _ -> ()
1594 if nogeomcmds state.geomcmds
1595 then loop pages
1598 let preload pages =
1599 load pages;
1600 if conf.preload && state.currently = Idle
1601 then load (preloadlayout state.y);
1604 let layoutready layout =
1605 let rec fold all ls =
1606 all && match ls with
1607 | l :: rest ->
1608 let seen = ref false in
1609 let allvisible = ref true in
1610 let foo col row _ _ _ _ _ _ =
1611 seen := true;
1612 allvisible := !allvisible &&
1613 begin match gettileopaque l col row with
1614 | Some _ -> true
1615 | None -> false
1618 itertiles l foo;
1619 fold (!seen && !allvisible) rest
1620 | [] -> true
1622 let alltilesvisible = fold true layout in
1623 alltilesvisible;
1626 let gotoy y =
1627 state.wthack <- false;
1628 let y = bound y 0 state.maxy in
1629 let y, layout, proceed =
1630 match conf.maxwait with
1631 | Some time when state.ghyll == noghyll ->
1632 begin match state.throttle with
1633 | None ->
1634 let layout = layout y state.winh in
1635 let ready = layoutready layout in
1636 if not ready
1637 then (
1638 load layout;
1639 state.throttle <- Some (layout, y, now ());
1641 else G.postRedisplay "gotoy showall (None)";
1642 y, layout, ready
1643 | Some (_, _, started) ->
1644 let dt = now () -. started in
1645 if dt > time
1646 then (
1647 state.throttle <- None;
1648 let layout = layout y state.winh in
1649 load layout;
1650 G.postRedisplay "maxwait";
1651 y, layout, true
1653 else -1, [], false
1656 | _ ->
1657 let layout = layout y state.winh in
1658 G.postRedisplay "gotoy ready";
1659 y, layout, true
1661 if proceed
1662 then (
1663 state.y <- y;
1664 state.layout <- layout;
1665 begin match state.mode with
1666 | LinkNav (Ltexact (pageno, linkno)) ->
1667 let rec loop = function
1668 | [] ->
1669 state.mode <- LinkNav (Ltgendir 0)
1670 | l :: _ when l.pageno = pageno ->
1671 begin match getopaque pageno with
1672 | None ->
1673 state.mode <- LinkNav (Ltgendir 0)
1674 | Some opaque ->
1675 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1676 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1677 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1678 then state.mode <- LinkNav (Ltgendir 0)
1680 | _ :: rest -> loop rest
1682 loop layout
1683 | _ -> ()
1684 end;
1685 begin match state.mode with
1686 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1687 if not (pagevisible layout pageno)
1688 then (
1689 match state.layout with
1690 | [] -> ()
1691 | l :: _ ->
1692 state.mode <- Birdseye (
1693 conf, leftx, l.pageno, hooverpageno, anchor
1696 | LinkNav (Ltgendir dir as lt) ->
1697 let linknav =
1698 let rec loop = function
1699 | [] -> lt
1700 | l :: rest ->
1701 match getopaque l.pageno with
1702 | None -> loop rest
1703 | Some opaque ->
1704 let link =
1705 let ld =
1706 if dir = 0
1707 then LDfirstvisible (l.pagex, l.pagey, dir)
1708 else (
1709 if dir > 0 then LDfirst else LDlast
1712 findlink opaque ld
1714 match link with
1715 | Lnotfound -> loop rest
1716 | Lfound n ->
1717 showlinktype (getlink opaque n);
1718 Ltexact (l.pageno, n)
1720 loop state.layout
1722 state.mode <- LinkNav linknav
1723 | _ -> ()
1724 end;
1725 preload layout;
1727 state.ghyll <- noghyll;
1728 if conf.updatecurs
1729 then (
1730 let mx, my = state.mpos in
1731 updateunder mx my;
1735 let conttiling pageno opaque =
1736 tilepage pageno opaque
1737 (if conf.preload then preloadlayout state.y else state.layout)
1740 let gotoy_and_clear_text y =
1741 if not conf.verbose then state.text <- "";
1742 gotoy y;
1745 let getanchor1 l =
1746 let top =
1747 let coloff = l.pagecol * l.pageh in
1748 float (l.pagey + coloff) /. float l.pageh
1750 let dtop =
1751 if l.pagedispy = 0
1752 then
1754 else
1755 if conf.presentation
1756 then float l.pagedispy /. float (calcips l.pageh)
1757 else float l.pagedispy /. float conf.interpagespace
1759 (l.pageno, top, dtop)
1762 let getanchor () =
1763 match state.layout with
1764 | l :: _ -> getanchor1 l
1765 | [] ->
1766 let n = page_of_y state.y in
1767 if n = -1
1768 then state.anchor
1769 else
1770 let y, h = getpageyh n in
1771 let dy = y - state.y in
1772 let dtop =
1773 if conf.presentation
1774 then
1775 let ips = calcips h in
1776 float (dy + ips) /. float ips
1777 else
1778 float dy /. float conf.interpagespace
1780 (n, 0.0, dtop)
1783 let getanchory (n, top, dtop) =
1784 let y, h = getpageyh n in
1785 if conf.presentation
1786 then
1787 let ips = calcips h in
1788 y + truncate (top*.float h -. dtop*.float ips) + ips;
1789 else
1790 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1793 let gotoanchor anchor =
1794 gotoy (getanchory anchor);
1797 let addnav () =
1798 cbput state.hists.nav (getanchor ());
1801 let getnav dir =
1802 let anchor = cbgetc state.hists.nav dir in
1803 getanchory anchor;
1806 let gotoghyll y =
1807 let scroll f n a b =
1808 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1809 let snake f a b =
1810 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1811 if f < a
1812 then s (float f /. float a)
1813 else (
1814 if f > b
1815 then 1.0 -. s ((float (f-b) /. float (n-b)))
1816 else 1.0
1819 snake f a b
1820 and summa f n a b =
1821 (* courtesy:
1822 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1823 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1824 let iv1 = iv f in
1825 let ins = float a *. iv1
1826 and outs = float (n-b) *. iv1 in
1827 let ones = b - a in
1828 ins +. outs +. float ones
1830 let rec set (_N, _A, _B) y sy =
1831 let sum = summa 1.0 _N _A _B in
1832 let dy = float (y - sy) in
1833 state.ghyll <- (
1834 let rec gf n y1 o =
1835 if n >= _N
1836 then state.ghyll <- noghyll
1837 else
1838 let go n =
1839 let s = scroll n _N _A _B in
1840 let y1 = y1 +. ((s *. dy) /. sum) in
1841 gotoy_and_clear_text (truncate y1);
1842 state.ghyll <- gf (n+1) y1;
1844 match o with
1845 | None -> go n
1846 | Some y' -> set (_N/2, 1, 1) y' state.y
1848 gf 0 (float state.y)
1851 match conf.ghyllscroll with
1852 | None ->
1853 gotoy_and_clear_text y
1854 | Some nab ->
1855 if state.ghyll == noghyll
1856 then set nab y state.y
1857 else state.ghyll (Some y)
1860 let gotopage n top =
1861 let y, h = getpageyh n in
1862 let y = y + (truncate (top *. float h)) in
1863 gotoghyll y
1866 let gotopage1 n top =
1867 let y = getpagey n in
1868 let y = y + top in
1869 gotoghyll y
1872 let invalidate s f =
1873 state.layout <- [];
1874 state.pdims <- [];
1875 state.rects <- [];
1876 state.rects1 <- [];
1877 match state.geomcmds with
1878 | ps, [] when String.length ps = 0 ->
1879 f ();
1880 state.geomcmds <- s, [];
1882 | ps, [] ->
1883 state.geomcmds <- ps, [s, f];
1885 | ps, (s', _) :: rest when s' = s ->
1886 state.geomcmds <- ps, ((s, f) :: rest);
1888 | ps, cmds ->
1889 state.geomcmds <- ps, ((s, f) :: cmds);
1892 let flushpages () =
1893 Hashtbl.iter (fun _ opaque ->
1894 wcmd "freepage %s" opaque;
1895 ) state.pagemap;
1896 Hashtbl.clear state.pagemap;
1899 let opendoc path password =
1900 state.path <- path;
1901 state.password <- password;
1902 state.gen <- state.gen + 1;
1903 state.docinfo <- [];
1905 flushpages ();
1906 setaalevel conf.aalevel;
1907 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename path)));
1908 wcmd "open %d %s\000%s\000" (btod state.wthack) path password;
1909 invalidate "reqlayout"
1910 (fun () ->
1911 wcmd "reqlayout %d %d %s\000"
1912 conf.angle (btod conf.proportional) state.nameddest;
1916 let reload () =
1917 state.anchor <- getanchor ();
1918 state.wthack <- !wtmode;
1919 opendoc state.path state.password;
1922 let scalecolor c =
1923 let c = c *. conf.colorscale in
1924 (c, c, c);
1927 let scalecolor2 (r, g, b) =
1928 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1931 let docolumns = function
1932 | Csingle _ ->
1933 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1934 let rec loop pageno pdimno pdim y ph pdims =
1935 if pageno = state.pagecount
1936 then ()
1937 else
1938 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1939 match pdims with
1940 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1941 pdimno+1, pdim, rest
1942 | _ ->
1943 pdimno, pdim, pdims
1945 let x = max 0 (((state.winw - state.scrollw - w) / 2) - xoff) in
1946 let y = y +
1947 (if conf.presentation
1948 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1949 else (if pageno = 0 then 0 else conf.interpagespace)
1952 a.(pageno) <- (pdimno, x, y, pdim);
1953 loop (pageno+1) pdimno pdim (y + h) h pdims
1955 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1956 conf.columns <- Csingle a;
1958 | Cmulti ((columns, coverA, coverB), _) ->
1959 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1960 let rec loop pageno pdimno pdim x y rowh pdims =
1961 let rec fixrow m = if m = pageno then () else
1962 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1963 if h < rowh
1964 then (
1965 let y = y + (rowh - h) / 2 in
1966 a.(m) <- (pdimno, x, y, pdim);
1968 fixrow (m+1)
1970 if pageno = state.pagecount
1971 then fixrow (((pageno - 1) / columns) * columns)
1972 else
1973 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1974 match pdims with
1975 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1976 pdimno+1, pdim, rest
1977 | _ ->
1978 pdimno, pdim, pdims
1980 let x, y, rowh' =
1981 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1982 then (
1983 let x = (state.winw - state.scrollw - w) / 2 in
1984 let ips =
1985 if conf.presentation then calcips h else conf.interpagespace in
1986 x, y + ips + rowh, h
1988 else (
1989 if (pageno - coverA) mod columns = 0
1990 then (
1991 let x = max 0 (state.winw - state.scrollw - state.w) / 2 in
1992 let y =
1993 if conf.presentation
1994 then
1995 let ips = calcips h in
1996 y + (if pageno = 0 then 0 else calcips rowh + ips)
1997 else
1998 y + (if pageno = 0 then 0 else conf.interpagespace)
2000 x, y + rowh, h
2002 else x, y, max rowh h
2005 let y =
2006 if pageno > 1 && (pageno - coverA) mod columns = 0
2007 then (
2008 let y =
2009 if pageno = columns && conf.presentation
2010 then (
2011 let ips = calcips rowh in
2012 for i = 0 to pred columns
2014 let (pdimno, x, y, pdim) = a.(i) in
2015 a.(i) <- (pdimno, x, y+ips, pdim)
2016 done;
2017 y+ips;
2019 else y
2021 fixrow (pageno - columns);
2024 else y
2026 a.(pageno) <- (pdimno, x, y, pdim);
2027 let x = x + w + xoff*2 + conf.interpagespace in
2028 loop (pageno+1) pdimno pdim x y rowh' pdims
2030 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2031 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2033 | Csplit (c, _) ->
2034 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2035 let rec loop pageno pdimno pdim y pdims =
2036 if pageno = state.pagecount
2037 then ()
2038 else
2039 let pdimno, ((_, w, h, _) as pdim), pdims =
2040 match pdims with
2041 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2042 pdimno+1, pdim, rest
2043 | _ ->
2044 pdimno, pdim, pdims
2046 let cw = w / c in
2047 let rec loop1 n x y =
2048 if n = c then y else (
2049 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2050 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2053 let y = loop1 0 0 y in
2054 loop (pageno+1) pdimno pdim y pdims
2056 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2057 conf.columns <- Csplit (c, a);
2060 let represent () =
2061 docolumns conf.columns;
2062 state.maxy <- calcheight ();
2063 state.hscrollh <-
2064 if state.w <= state.winw - state.scrollw
2065 then 0
2066 else state.scrollw
2068 if state.reprf == noreprf
2069 then
2070 let wthack = state.wthack in
2071 begin match state.mode with
2072 | Birdseye (_, _, pageno, _, _) ->
2073 let y, h = getpageyh pageno in
2074 let top = (state.winh - h) / 2 in
2075 gotoy (max 0 (y - top))
2076 | _ -> gotoanchor state.anchor
2077 end;
2078 state.wthack <- wthack;
2079 else (
2080 state.reprf ();
2081 state.reprf <- noreprf;
2085 let reshape w h =
2086 state.wthack <- false;
2087 GlDraw.viewport 0 0 w h;
2088 let firsttime = state.geomcmds == firstgeomcmds in
2089 if not firsttime && nogeomcmds state.geomcmds
2090 then state.anchor <- getanchor ();
2092 state.winw <- w;
2093 let w = truncate (float w *. conf.zoom) - state.scrollw in
2094 let w = max w 2 in
2095 state.winh <- h;
2096 setfontsize fstate.fontsize;
2097 GlMat.mode `modelview;
2098 GlMat.load_identity ();
2100 GlMat.mode `projection;
2101 GlMat.load_identity ();
2102 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2103 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2104 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2106 let relx =
2107 if conf.zoom <= 1.0
2108 then 0.0
2109 else float state.x /. float state.w
2111 invalidate "geometry"
2112 (fun () ->
2113 state.w <- w;
2114 if not firsttime
2115 then state.x <- truncate (relx *. float w);
2116 let w =
2117 match conf.columns with
2118 | Csingle _ -> w
2119 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2120 | Csplit (c, _) -> w * c
2122 wcmd "geometry %d %d" w h);
2125 let enttext () =
2126 let len = String.length state.text in
2127 let drawstring s =
2128 let hscrollh =
2129 match state.mode with
2130 | Textentry _
2131 | View ->
2132 let h, _, _ = state.uioh#scrollpw in
2134 | _ -> 0
2136 let rect x w =
2137 GlDraw.rect
2138 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2139 (x+.w, float (state.winh - hscrollh))
2142 let w = float (state.winw - state.scrollw - 1) in
2143 if state.progress >= 0.0 && state.progress < 1.0
2144 then (
2145 GlDraw.color (0.3, 0.3, 0.3);
2146 let w1 = w *. state.progress in
2147 rect 0.0 w1;
2148 GlDraw.color (0.0, 0.0, 0.0);
2149 rect w1 (w-.w1)
2151 else (
2152 GlDraw.color (0.0, 0.0, 0.0);
2153 rect 0.0 w;
2156 GlDraw.color (1.0, 1.0, 1.0);
2157 drawstring fstate.fontsize
2158 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2160 let s =
2161 match state.mode with
2162 | Textentry ((prefix, text, _, _, _, _), _) ->
2163 let s =
2164 if len > 0
2165 then
2166 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2167 else
2168 Printf.sprintf "%s%s_" prefix text
2172 | _ -> state.text
2174 let s =
2175 if state.newerrmsgs
2176 then (
2177 if not (istextentry state.mode)
2178 then
2179 let s1 = "(press 'e' to review error messasges)" in
2180 if String.length s > 0 then s ^ " " ^ s1 else s1
2181 else s
2183 else s
2185 if String.length s > 0
2186 then drawstring s
2189 let gctiles () =
2190 let len = Queue.length state.tilelru in
2191 let layout = lazy (
2192 match state.throttle with
2193 | None ->
2194 if conf.preload
2195 then preloadlayout state.y
2196 else state.layout
2197 | Some (layout, _, _) ->
2198 layout
2199 ) in
2200 let rec loop qpos =
2201 if state.memused <= conf.memlimit
2202 then ()
2203 else (
2204 if qpos < len
2205 then
2206 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2207 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2208 let (_, pw, ph, _) = getpagedim n in
2210 gen = state.gen
2211 && colorspace = conf.colorspace
2212 && angle = conf.angle
2213 && pagew = pw
2214 && pageh = ph
2215 && (
2216 let x = col*conf.tilew
2217 and y = row*conf.tileh in
2218 tilevisible (Lazy.force_val layout) n x y
2220 then Queue.push lruitem state.tilelru
2221 else (
2222 freepbo p;
2223 wcmd "freetile %s" p;
2224 state.memused <- state.memused - s;
2225 state.uioh#infochanged Memused;
2226 Hashtbl.remove state.tilemap k;
2228 loop (qpos+1)
2231 loop 0
2234 let flushtiles () =
2235 Queue.iter (fun (k, p, s) ->
2236 wcmd "freetile %s" p;
2237 state.memused <- state.memused - s;
2238 state.uioh#infochanged Memused;
2239 Hashtbl.remove state.tilemap k;
2240 ) state.tilelru;
2241 Queue.clear state.tilelru;
2242 load state.layout;
2245 let logcurrently = function
2246 | Idle -> dolog "Idle"
2247 | Loading (l, gen) ->
2248 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2249 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2250 dolog
2251 "Tiling %d[%d,%d] page=%s cs=%s angle"
2252 l.pageno col row pageopaque
2253 (colorspace_to_string colorspace)
2255 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2256 angle gen conf.angle state.gen
2257 tilew tileh
2258 conf.tilew conf.tileh
2260 | Outlining _ ->
2261 dolog "outlining"
2264 let splitatspace =
2265 let r = Str.regexp " " in
2266 fun s -> Str.bounded_split r s 2;
2269 let onpagerect pageno f =
2270 let b =
2271 match conf.columns with
2272 | Cmulti (_, b) -> b
2273 | Csingle b -> b
2274 | Csplit (_, b) -> b
2276 if pageno >= 0 && pageno < Array.length b
2277 then
2278 let (pdimno, _, _, (_, _, _, _)) = b.(pageno) in
2279 let r = getpdimrect pdimno in
2280 f (r.(1)-.r.(0)) (r.(3)-.r.(2))
2283 let gotopagexy pageno x y =
2284 onpagerect pageno (fun w h ->
2285 let top = y /. h in
2286 let _,w1,_,leftx = getpagedim pageno in
2287 let wh = state.winh - state.hscrollh in
2288 let sw = float w1 /. w in
2289 let x = sw *. x in
2290 let x = leftx + state.x + truncate x in
2291 let sx =
2292 if x < 0 || x >= state.winw - state.scrollw
2293 then state.x - x
2294 else state.x
2296 let py, h = getpageyh pageno in
2297 let y' = py + truncate (top *. float h) in
2298 let dy = y' - state.y in
2299 let sy =
2300 if x != state.x || not (dy > 0 && dy < wh)
2301 then (
2302 if conf.presentation
2303 then
2304 if abs (py - y') > wh
2305 then y'
2306 else py
2307 else y';
2309 else state.y
2311 if state.x != sx || state.y != sy
2312 then (
2313 let x, y =
2314 if !wtmode
2315 then (
2316 let ww = state.winw - state.scrollw in
2317 let qx = sx / ww
2318 and qy = sy / wh in
2319 let x = qx * ww
2320 and y = qy * wh in
2321 let x = if -x + ww > w1 then -(w1-ww) else x
2322 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2323 let y =
2324 if conf.presentation
2325 then
2326 if abs (py - y') > wh
2327 then y'
2328 else py
2329 else y';
2331 (x, y)
2333 else (sx, sy)
2335 state.x <- x;
2336 gotoy_and_clear_text y;
2337 state.wthack <- !wtmode && not (layoutready state.layout);
2339 else gotoy_and_clear_text state.y;
2343 let act cmds =
2344 (* dolog "%S" cmds; *)
2345 let cl = splitatspace cmds in
2346 let scan s fmt f =
2347 try Scanf.sscanf s fmt f
2348 with exn ->
2349 dolog "error processing '%S': %s" cmds (exntos exn);
2350 exit 1
2352 match cl with
2353 | "clear" :: [] ->
2354 state.uioh#infochanged Pdim;
2355 state.pdims <- [];
2357 | "clearrects" :: [] ->
2358 state.rects <- state.rects1;
2359 G.postRedisplay "clearrects";
2361 | "continue" :: args :: [] ->
2362 let n = scan args "%u" (fun n -> n) in
2363 state.pagecount <- n;
2364 begin match state.currently with
2365 | Outlining l ->
2366 state.currently <- Idle;
2367 state.outlines <- Array.of_list (List.rev l)
2368 | _ -> ()
2369 end;
2371 let cur, cmds = state.geomcmds in
2372 if String.length cur = 0
2373 then failwith "umpossible";
2375 begin match List.rev cmds with
2376 | [] ->
2377 state.geomcmds <- "", [];
2378 represent ();
2379 | (s, f) :: rest ->
2380 f ();
2381 state.geomcmds <- s, List.rev rest;
2382 end;
2383 if conf.maxwait = None
2384 then G.postRedisplay "continue";
2386 | "title" :: args :: [] ->
2387 Wsi.settitle args
2389 | "msg" :: args :: [] ->
2390 showtext ' ' args
2392 | "vmsg" :: args :: [] ->
2393 if conf.verbose
2394 then showtext ' ' args
2396 | "emsg" :: args :: [] ->
2397 Buffer.add_string state.errmsgs args;
2398 state.newerrmsgs <- true;
2399 G.postRedisplay "error message"
2401 | "progress" :: args :: [] ->
2402 let progress, text =
2403 scan args "%f %n"
2404 (fun f pos ->
2405 f, String.sub args pos (String.length args - pos))
2407 state.text <- text;
2408 state.progress <- progress;
2409 G.postRedisplay "progress"
2411 | "firstmatch" :: args :: [] ->
2412 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2413 scan args "%u %d %f %f %f %f %f %f %f %f"
2414 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2415 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2417 let y = (getpagey pageno) + truncate y0 in
2418 addnav ();
2419 gotoy y;
2420 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2422 | "match" :: args :: [] ->
2423 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2424 scan args "%u %d %f %f %f %f %f %f %f %f"
2425 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2426 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2428 state.rects1 <-
2429 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2431 | "page" :: args :: [] ->
2432 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2433 begin match state.currently with
2434 | Loading (l, gen) ->
2435 vlog "page %d took %f sec" l.pageno t;
2436 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2437 begin match state.throttle with
2438 | None ->
2439 let preloadedpages =
2440 if conf.preload
2441 then preloadlayout state.y
2442 else state.layout
2444 let evict () =
2445 let module IntSet =
2446 Set.Make (struct type t = int let compare = (-) end) in
2447 let set =
2448 List.fold_left (fun s l -> IntSet.add l.pageno s)
2449 IntSet.empty preloadedpages
2451 let evictedpages =
2452 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2453 if not (IntSet.mem pageno set)
2454 then (
2455 wcmd "freepage %s" opaque;
2456 key :: accu
2458 else accu
2459 ) state.pagemap []
2461 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2463 evict ();
2464 state.currently <- Idle;
2465 if gen = state.gen
2466 then (
2467 tilepage l.pageno pageopaque state.layout;
2468 load state.layout;
2469 load preloadedpages;
2470 if pagevisible state.layout l.pageno
2471 && layoutready state.layout
2472 then G.postRedisplay "page";
2475 | Some (layout, _, _) ->
2476 state.currently <- Idle;
2477 tilepage l.pageno pageopaque layout;
2478 load state.layout
2479 end;
2481 | _ ->
2482 dolog "Inconsistent loading state";
2483 logcurrently state.currently;
2484 exit 1
2487 | "tile" :: args :: [] ->
2488 let (x, y, opaque, size, t) =
2489 scan args "%u %u %s %u %f"
2490 (fun x y p size t -> (x, y, p, size, t))
2492 begin match state.currently with
2493 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2494 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2496 unmappbo opaque;
2497 if tilew != conf.tilew || tileh != conf.tileh
2498 then (
2499 wcmd "freetile %s" opaque;
2500 state.currently <- Idle;
2501 load state.layout;
2503 else (
2504 puttileopaque l col row gen cs angle opaque size t;
2505 state.memused <- state.memused + size;
2506 state.uioh#infochanged Memused;
2507 gctiles ();
2508 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2509 opaque, size) state.tilelru;
2511 let layout =
2512 match state.throttle with
2513 | None -> state.layout
2514 | Some (layout, _, _) -> layout
2517 state.currently <- Idle;
2518 if gen = state.gen
2519 && conf.colorspace = cs
2520 && conf.angle = angle
2521 && tilevisible layout l.pageno x y
2522 then conttiling l.pageno pageopaque;
2524 begin match state.throttle with
2525 | None ->
2526 if state.wthack
2527 then state.wthack <- not (layoutready state.layout);
2528 preload state.layout;
2529 if gen = state.gen
2530 && conf.colorspace = cs
2531 && conf.angle = angle
2532 && tilevisible state.layout l.pageno x y
2533 then G.postRedisplay "tile nothrottle";
2535 | Some (layout, y, _) ->
2536 let ready = layoutready layout in
2537 if ready
2538 then (
2539 state.wthack <- false;
2540 state.y <- y;
2541 state.layout <- layout;
2542 state.throttle <- None;
2543 G.postRedisplay "throttle";
2545 else load layout;
2546 end;
2549 | _ ->
2550 dolog "Inconsistent tiling state";
2551 logcurrently state.currently;
2552 exit 1
2555 | "pdim" :: args :: [] ->
2556 let pdim =
2557 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2559 state.uioh#infochanged Pdim;
2560 state.pdims <- pdim :: state.pdims
2562 | "o" :: args :: [] ->
2563 let (l, n, t, h, pos) =
2564 scan args "%u %u %d %u %n"
2565 (fun l n t h pos -> l, n, t, h, pos)
2567 let s = String.sub args pos (String.length args - pos) in
2568 let outline = (s, l, (n, float t /. float h, 0.0)) in
2569 begin match state.currently with
2570 | Outlining outlines ->
2571 state.currently <- Outlining (outline :: outlines)
2572 | Idle ->
2573 state.currently <- Outlining [outline]
2574 | currently ->
2575 dolog "invalid outlining state";
2576 logcurrently currently
2579 | "a" :: args :: [] ->
2580 let (n, l, t) =
2581 scan args "%u %d %d" (fun n l t -> n, l, t)
2583 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2585 | "info" :: args :: [] ->
2586 state.docinfo <- (1, args) :: state.docinfo
2588 | "infoend" :: [] ->
2589 state.uioh#infochanged Docinfo;
2590 state.docinfo <- List.rev state.docinfo
2592 | _ ->
2593 failwith (Printf.sprintf "unknown cmd `%S'" cmds)
2596 let onhist cb =
2597 let rc = cb.rc in
2598 let action = function
2599 | HCprev -> cbget cb ~-1
2600 | HCnext -> cbget cb 1
2601 | HCfirst -> cbget cb ~-(cb.rc)
2602 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2603 and cancel () = cb.rc <- rc
2604 in (action, cancel)
2607 let search pattern forward =
2608 if String.length pattern > 0
2609 then
2610 let pn, py =
2611 match state.layout with
2612 | [] -> 0, 0
2613 | l :: _ ->
2614 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2616 wcmd "search %d %d %d %d,%s\000"
2617 (btod conf.icase) pn py (btod forward) pattern;
2620 let intentry text key =
2621 let c =
2622 if key >= 32 && key < 127
2623 then Char.chr key
2624 else '\000'
2626 match c with
2627 | '0' .. '9' ->
2628 let text = addchar text c in
2629 TEcont text
2631 | _ ->
2632 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2633 TEcont text
2636 let linknentry text key =
2637 let c =
2638 if key >= 32 && key < 127
2639 then Char.chr key
2640 else '\000'
2642 match c with
2643 | 'a' .. 'z' ->
2644 let text = addchar text c in
2645 TEcont text
2647 | _ ->
2648 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2649 TEcont text
2652 let linkndone f s =
2653 if String.length s > 0
2654 then (
2655 let n =
2656 let l = String.length s in
2657 let rec loop pos n = if pos = l then n else
2658 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2659 loop (pos+1) (n*26 + m)
2660 in loop 0 0
2662 let rec loop n = function
2663 | [] -> ()
2664 | l :: rest ->
2665 match getopaque l.pageno with
2666 | None -> loop n rest
2667 | Some opaque ->
2668 let m = getlinkcount opaque in
2669 if n < m
2670 then (
2671 let under = getlink opaque n in
2672 f under
2674 else loop (n-m) rest
2676 loop n state.layout;
2680 let textentry text key =
2681 if key land 0xff00 = 0xff00
2682 then TEcont text
2683 else TEcont (text ^ toutf8 key)
2686 let reqlayout angle proportional =
2687 match state.throttle with
2688 | None ->
2689 if nogeomcmds state.geomcmds
2690 then state.anchor <- getanchor ();
2691 conf.angle <- angle mod 360;
2692 if conf.angle != 0
2693 then (
2694 match state.mode with
2695 | LinkNav _ -> state.mode <- View
2696 | _ -> ()
2698 conf.proportional <- proportional;
2699 invalidate "reqlayout"
2700 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2701 | _ -> ()
2704 let settrim trimmargins trimfuzz =
2705 if nogeomcmds state.geomcmds
2706 then state.anchor <- getanchor ();
2707 conf.trimmargins <- trimmargins;
2708 conf.trimfuzz <- trimfuzz;
2709 let x0, y0, x1, y1 = trimfuzz in
2710 invalidate "settrim"
2711 (fun () ->
2712 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2713 flushpages ();
2716 let setzoom zoom =
2717 match state.throttle with
2718 | None ->
2719 let zoom = max 0.01 zoom in
2720 if zoom <> conf.zoom
2721 then (
2722 state.prevzoom <- conf.zoom;
2723 conf.zoom <- zoom;
2724 reshape state.winw state.winh;
2725 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2728 | Some (layout, y, started) ->
2729 let time =
2730 match conf.maxwait with
2731 | None -> 0.0
2732 | Some t -> t
2734 let dt = now () -. started in
2735 if dt > time
2736 then (
2737 state.y <- y;
2738 load layout;
2742 let setcolumns mode columns coverA coverB =
2743 state.prevcolumns <- Some (conf.columns, conf.zoom);
2744 if columns < 0
2745 then (
2746 if isbirdseye mode
2747 then showtext '!' "split mode doesn't work in bird's eye"
2748 else (
2749 conf.columns <- Csplit (-columns, [||]);
2750 state.x <- 0;
2751 conf.zoom <- 1.0;
2754 else (
2755 if columns < 2
2756 then (
2757 conf.columns <- Csingle [||];
2758 state.x <- 0;
2759 setzoom 1.0;
2761 else (
2762 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2763 conf.zoom <- 1.0;
2766 reshape state.winw state.winh;
2769 let enterbirdseye () =
2770 let zoom = float conf.thumbw /. float state.winw in
2771 let birdseyepageno =
2772 let cy = state.winh / 2 in
2773 let fold = function
2774 | [] -> 0
2775 | l :: rest ->
2776 let rec fold best = function
2777 | [] -> best.pageno
2778 | l :: rest ->
2779 let d = cy - (l.pagedispy + l.pagevh/2)
2780 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2781 if abs d < abs dbest
2782 then fold l rest
2783 else best.pageno
2784 in fold l rest
2786 fold state.layout
2788 state.mode <- Birdseye (
2789 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2791 conf.zoom <- zoom;
2792 conf.presentation <- false;
2793 conf.interpagespace <- 10;
2794 conf.hlinks <- false;
2795 state.x <- 0;
2796 state.mstate <- Mnone;
2797 conf.maxwait <- None;
2798 conf.columns <- (
2799 match conf.beyecolumns with
2800 | Some c ->
2801 conf.zoom <- 1.0;
2802 Cmulti ((c, 0, 0), [||])
2803 | None -> Csingle [||]
2805 Wsi.setcursor Wsi.CURSOR_INHERIT;
2806 if conf.verbose
2807 then
2808 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2809 (100.0*.zoom)
2810 else
2811 state.text <- ""
2813 reshape state.winw state.winh;
2816 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2817 state.mode <- View;
2818 conf.zoom <- c.zoom;
2819 conf.presentation <- c.presentation;
2820 conf.interpagespace <- c.interpagespace;
2821 conf.maxwait <- c.maxwait;
2822 conf.hlinks <- c.hlinks;
2823 conf.beyecolumns <- (
2824 match conf.columns with
2825 | Cmulti ((c, _, _), _) -> Some c
2826 | Csingle _ -> None
2827 | Csplit _ -> failwith "leaving bird's eye split mode"
2829 conf.columns <- (
2830 match c.columns with
2831 | Cmulti (c, _) -> Cmulti (c, [||])
2832 | Csingle _ -> Csingle [||]
2833 | Csplit (c, _) -> Csplit (c, [||])
2835 state.x <- leftx;
2836 if conf.verbose
2837 then
2838 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2839 (100.0*.conf.zoom)
2841 reshape state.winw state.winh;
2842 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2845 let togglebirdseye () =
2846 match state.mode with
2847 | Birdseye vals -> leavebirdseye vals true
2848 | View -> enterbirdseye ()
2849 | _ -> ()
2852 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2853 let pageno = max 0 (pageno - incr) in
2854 let rec loop = function
2855 | [] -> gotopage1 pageno 0
2856 | l :: _ when l.pageno = pageno ->
2857 if l.pagedispy >= 0 && l.pagey = 0
2858 then G.postRedisplay "upbirdseye"
2859 else gotopage1 pageno 0
2860 | _ :: rest -> loop rest
2862 loop state.layout;
2863 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2866 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2867 let pageno = min (state.pagecount - 1) (pageno + incr) in
2868 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2869 let rec loop = function
2870 | [] ->
2871 let y, h = getpageyh pageno in
2872 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2873 gotoy (clamp dy)
2874 | l :: _ when l.pageno = pageno ->
2875 if l.pagevh != l.pageh
2876 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2877 else G.postRedisplay "downbirdseye"
2878 | _ :: rest -> loop rest
2880 loop state.layout
2883 let optentry mode _ key =
2884 let btos b = if b then "on" else "off" in
2885 if key >= 32 && key < 127
2886 then
2887 let c = Char.chr key in
2888 match c with
2889 | 's' ->
2890 let ondone s =
2891 try conf.scrollstep <- int_of_string s with exc ->
2892 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2894 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2896 | 'A' ->
2897 let ondone s =
2899 conf.autoscrollstep <- int_of_string s;
2900 if state.autoscroll <> None
2901 then state.autoscroll <- Some conf.autoscrollstep
2902 with exc ->
2903 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2905 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2907 | 'C' ->
2908 let ondone s =
2910 let n, a, b = multicolumns_of_string s in
2911 setcolumns mode n a b;
2912 with exc ->
2913 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
2915 TEswitch ("columns: ", "", None, textentry, ondone, true)
2917 | 'Z' ->
2918 let ondone s =
2920 let zoom = float (int_of_string s) /. 100.0 in
2921 setzoom zoom
2922 with exc ->
2923 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2925 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2927 | 't' ->
2928 let ondone s =
2930 conf.thumbw <- bound (int_of_string s) 2 4096;
2931 state.text <-
2932 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2933 begin match mode with
2934 | Birdseye beye ->
2935 leavebirdseye beye false;
2936 enterbirdseye ();
2937 | _ -> ();
2939 with exc ->
2940 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2942 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2944 | 'R' ->
2945 let ondone s =
2946 match try
2947 Some (int_of_string s)
2948 with exc ->
2949 state.text <- Printf.sprintf "bad integer `%s': %s"
2950 s (exntos exc);
2951 None
2952 with
2953 | Some angle -> reqlayout angle conf.proportional
2954 | None -> ()
2956 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2958 | 'i' ->
2959 conf.icase <- not conf.icase;
2960 TEdone ("case insensitive search " ^ (btos conf.icase))
2962 | 'p' ->
2963 conf.preload <- not conf.preload;
2964 gotoy state.y;
2965 TEdone ("preload " ^ (btos conf.preload))
2967 | 'v' ->
2968 conf.verbose <- not conf.verbose;
2969 TEdone ("verbose " ^ (btos conf.verbose))
2971 | 'd' ->
2972 conf.debug <- not conf.debug;
2973 TEdone ("debug " ^ (btos conf.debug))
2975 | 'h' ->
2976 conf.maxhfit <- not conf.maxhfit;
2977 state.maxy <- calcheight ();
2978 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2980 | 'c' ->
2981 conf.crophack <- not conf.crophack;
2982 TEdone ("crophack " ^ btos conf.crophack)
2984 | 'a' ->
2985 let s =
2986 match conf.maxwait with
2987 | None ->
2988 conf.maxwait <- Some infinity;
2989 "always wait for page to complete"
2990 | Some _ ->
2991 conf.maxwait <- None;
2992 "show placeholder if page is not ready"
2994 TEdone s
2996 | 'f' ->
2997 conf.underinfo <- not conf.underinfo;
2998 TEdone ("underinfo " ^ btos conf.underinfo)
3000 | 'P' ->
3001 conf.savebmarks <- not conf.savebmarks;
3002 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3004 | 'S' ->
3005 let ondone s =
3007 let pageno, py =
3008 match state.layout with
3009 | [] -> 0, 0
3010 | l :: _ ->
3011 l.pageno, l.pagey
3013 conf.interpagespace <- int_of_string s;
3014 docolumns conf.columns;
3015 state.maxy <- calcheight ();
3016 let y = getpagey pageno in
3017 gotoy (y + py)
3018 with exc ->
3019 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3021 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3023 | 'l' ->
3024 reqlayout conf.angle (not conf.proportional);
3025 TEdone ("proportional display " ^ btos conf.proportional)
3027 | 'T' ->
3028 settrim (not conf.trimmargins) conf.trimfuzz;
3029 TEdone ("trim margins " ^ btos conf.trimmargins)
3031 | 'I' ->
3032 conf.invert <- not conf.invert;
3033 TEdone ("invert colors " ^ btos conf.invert)
3035 | 'x' ->
3036 let ondone s =
3037 cbput state.hists.sel s;
3038 conf.selcmd <- s;
3040 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3041 textentry, ondone, true)
3043 | _ ->
3044 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3045 TEstop
3046 else
3047 TEcont state.text
3050 class type lvsource = object
3051 method getitemcount : int
3052 method getitem : int -> (string * int)
3053 method hasaction : int -> bool
3054 method exit :
3055 uioh:uioh ->
3056 cancel:bool ->
3057 active:int ->
3058 first:int ->
3059 pan:int ->
3060 qsearch:string ->
3061 uioh option
3062 method getactive : int
3063 method getfirst : int
3064 method getqsearch : string
3065 method setqsearch : string -> unit
3066 method getpan : int
3067 end;;
3069 class virtual lvsourcebase = object
3070 val mutable m_active = 0
3071 val mutable m_first = 0
3072 val mutable m_qsearch = ""
3073 val mutable m_pan = 0
3074 method getactive = m_active
3075 method getfirst = m_first
3076 method getqsearch = m_qsearch
3077 method getpan = m_pan
3078 method setqsearch s = m_qsearch <- s
3079 end;;
3081 let withoutlastutf8 s =
3082 let len = String.length s in
3083 if len = 0
3084 then s
3085 else
3086 let rec find pos =
3087 if pos = 0
3088 then pos
3089 else
3090 let b = Char.code s.[pos] in
3091 if b land 0b11000000 = 0b11000000
3092 then pos
3093 else find (pos-1)
3095 let first =
3096 if Char.code s.[len-1] land 0x80 = 0
3097 then len-1
3098 else find (len-1)
3100 String.sub s 0 first;
3103 let textentrykeyboard
3104 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3105 let key =
3106 if key >= 0xffb0 && key <= 0xffb9
3107 then key - 0xffb0 + 48 else key
3109 let enttext te =
3110 state.mode <- Textentry (te, onleave);
3111 state.text <- "";
3112 enttext ();
3113 G.postRedisplay "textentrykeyboard enttext";
3115 let histaction cmd =
3116 match opthist with
3117 | None -> ()
3118 | Some (action, _) ->
3119 state.mode <- Textentry (
3120 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3122 G.postRedisplay "textentry histaction"
3124 match key with
3125 | 0xff08 -> (* backspace *)
3126 let s = withoutlastutf8 text in
3127 let len = String.length s in
3128 if cancelonempty && len = 0
3129 then (
3130 onleave Cancel;
3131 G.postRedisplay "textentrykeyboard after cancel";
3133 else (
3134 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3137 | 0xff0d | 0xff8d -> (* (kp) enter *)
3138 ondone text;
3139 onleave Confirm;
3140 G.postRedisplay "textentrykeyboard after confirm"
3142 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3143 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3144 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3145 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3147 | 0xff1b -> (* escape*)
3148 if String.length text = 0
3149 then (
3150 begin match opthist with
3151 | None -> ()
3152 | Some (_, onhistcancel) -> onhistcancel ()
3153 end;
3154 onleave Cancel;
3155 state.text <- "";
3156 G.postRedisplay "textentrykeyboard after cancel2"
3158 else (
3159 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3162 | 0xff9f | 0xffff -> () (* delete *)
3164 | _ when key != 0
3165 && key land 0xff00 != 0xff00 (* keyboard *)
3166 && key land 0xfe00 != 0xfe00 (* xkb *)
3167 && key land 0xfd00 != 0xfd00 (* 3270 *)
3169 begin match onkey text key with
3170 | TEdone text ->
3171 ondone text;
3172 onleave Confirm;
3173 G.postRedisplay "textentrykeyboard after confirm2";
3175 | TEcont text ->
3176 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3178 | TEstop ->
3179 onleave Cancel;
3180 G.postRedisplay "textentrykeyboard after cancel3"
3182 | TEswitch te ->
3183 state.mode <- Textentry (te, onleave);
3184 G.postRedisplay "textentrykeyboard switch";
3185 end;
3187 | _ ->
3188 vlog "unhandled key %s" (Wsi.keyname key)
3191 let firstof first active =
3192 if first > active || abs (first - active) > fstate.maxrows - 1
3193 then max 0 (active - (fstate.maxrows/2))
3194 else first
3197 let calcfirst first active =
3198 if active > first
3199 then
3200 let rows = active - first in
3201 if rows > fstate.maxrows then active - fstate.maxrows else first
3202 else active
3205 let scrollph y maxy =
3206 let sh = (float (maxy + state.winh) /. float state.winh) in
3207 let sh = float state.winh /. sh in
3208 let sh = max sh (float conf.scrollh) in
3210 let percent =
3211 if y = state.maxy
3212 then 1.0
3213 else float y /. float maxy
3215 let position = (float state.winh -. sh) *. percent in
3217 let position =
3218 if position +. sh > float state.winh
3219 then float state.winh -. sh
3220 else position
3222 position, sh;
3225 let coe s = (s :> uioh);;
3227 class listview ~(source:lvsource) ~trusted ~modehash =
3228 object (self)
3229 val m_pan = source#getpan
3230 val m_first = source#getfirst
3231 val m_active = source#getactive
3232 val m_qsearch = source#getqsearch
3233 val m_prev_uioh = state.uioh
3235 method private elemunder y =
3236 let n = y / (fstate.fontsize+1) in
3237 if m_first + n < source#getitemcount
3238 then (
3239 if source#hasaction (m_first + n)
3240 then Some (m_first + n)
3241 else None
3243 else None
3245 method display =
3246 Gl.enable `blend;
3247 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3248 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3249 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3250 GlDraw.color (1., 1., 1.);
3251 Gl.enable `texture_2d;
3252 let fs = fstate.fontsize in
3253 let nfs = fs + 1 in
3254 let ww = fstate.wwidth in
3255 let tabw = 30.0*.ww in
3256 let itemcount = source#getitemcount in
3257 let rec loop row =
3258 if (row - m_first) > fstate.maxrows
3259 then ()
3260 else (
3261 if row >= 0 && row < itemcount
3262 then (
3263 let (s, level) = source#getitem row in
3264 let y = (row - m_first) * nfs in
3265 let x = 5.0 +. float (level + m_pan) *. ww in
3266 if row = m_active
3267 then (
3268 Gl.disable `texture_2d;
3269 GlDraw.polygon_mode `both `line;
3270 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3271 GlDraw.rect (1., float (y + 1))
3272 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3273 GlDraw.polygon_mode `both `fill;
3274 GlDraw.color (1., 1., 1.);
3275 Gl.enable `texture_2d;
3278 let drawtabularstring s =
3279 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3280 if trusted
3281 then
3282 let tabpos = try String.index s '\t' with Not_found -> -1 in
3283 if tabpos > 0
3284 then
3285 let len = String.length s - tabpos - 1 in
3286 let s1 = String.sub s 0 tabpos
3287 and s2 = String.sub s (tabpos + 1) len in
3288 let nx = drawstr x s1 in
3289 let sw = nx -. x in
3290 let x = x +. (max tabw sw) in
3291 drawstr x s2
3292 else
3293 drawstr x s
3294 else
3295 drawstr x s
3297 let _ = drawtabularstring s in
3298 loop (row+1)
3302 loop m_first;
3303 Gl.disable `blend;
3304 Gl.disable `texture_2d;
3306 method updownlevel incr =
3307 let len = source#getitemcount in
3308 let curlevel =
3309 if m_active >= 0 && m_active < len
3310 then snd (source#getitem m_active)
3311 else -1
3313 let rec flow i =
3314 if i = len then i-1 else if i = -1 then 0 else
3315 let _, l = source#getitem i in
3316 if l != curlevel then i else flow (i+incr)
3318 let active = flow m_active in
3319 let first = calcfirst m_first active in
3320 G.postRedisplay "outline updownlevel";
3321 {< m_active = active; m_first = first >}
3323 method private key1 key mask =
3324 let set1 active first qsearch =
3325 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3327 let search active pattern incr =
3328 let dosearch re =
3329 let rec loop n =
3330 if n >= 0 && n < source#getitemcount
3331 then (
3332 let s, _ = source#getitem n in
3334 (try ignore (Str.search_forward re s 0); true
3335 with Not_found -> false)
3336 then Some n
3337 else loop (n + incr)
3339 else None
3341 loop active
3344 let re = Str.regexp_case_fold pattern in
3345 dosearch re
3346 with Failure s ->
3347 state.text <- s;
3348 None
3350 let itemcount = source#getitemcount in
3351 let find start incr =
3352 let rec find i =
3353 if i = -1 || i = itemcount
3354 then -1
3355 else (
3356 if source#hasaction i
3357 then i
3358 else find (i + incr)
3361 find start
3363 let set active first =
3364 let first = bound first 0 (itemcount - fstate.maxrows) in
3365 state.text <- "";
3366 coe {< m_active = active; m_first = first >}
3368 let navigate incr =
3369 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3370 let active, first =
3371 let incr1 = if incr > 0 then 1 else -1 in
3372 if isvisible m_first m_active
3373 then
3374 let next =
3375 let next = m_active + incr in
3376 let next =
3377 if next < 0 || next >= itemcount
3378 then -1
3379 else find next incr1
3381 if next = -1 || abs (m_active - next) > fstate.maxrows
3382 then -1
3383 else next
3385 if next = -1
3386 then
3387 let first = m_first + incr in
3388 let first = bound first 0 (itemcount - 1) in
3389 let next =
3390 let next = m_active + incr in
3391 let next = bound next 0 (itemcount - 1) in
3392 find next ~-incr1
3394 let active = if next = -1 then m_active else next in
3395 active, first
3396 else
3397 let first = min next m_first in
3398 let first =
3399 if abs (next - first) > fstate.maxrows
3400 then first + incr
3401 else first
3403 next, first
3404 else
3405 let first = m_first + incr in
3406 let first = bound first 0 (itemcount - 1) in
3407 let active =
3408 let next = m_active + incr in
3409 let next = bound next 0 (itemcount - 1) in
3410 let next = find next incr1 in
3411 let active =
3412 if next = -1 || abs (m_active - first) > fstate.maxrows
3413 then (
3414 let active = if m_active = -1 then next else m_active in
3415 active
3417 else next
3419 if isvisible first active
3420 then active
3421 else -1
3423 active, first
3425 G.postRedisplay "listview navigate";
3426 set active first;
3428 match key with
3429 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3430 let incr = if key = 0x72 then -1 else 1 in
3431 let active, first =
3432 match search (m_active + incr) m_qsearch incr with
3433 | None ->
3434 state.text <- m_qsearch ^ " [not found]";
3435 m_active, m_first
3436 | Some active ->
3437 state.text <- m_qsearch;
3438 active, firstof m_first active
3440 G.postRedisplay "listview ctrl-r/s";
3441 set1 active first m_qsearch;
3443 | 0xff08 -> (* backspace *)
3444 if String.length m_qsearch = 0
3445 then coe self
3446 else (
3447 let qsearch = withoutlastutf8 m_qsearch in
3448 let len = String.length qsearch in
3449 if len = 0
3450 then (
3451 state.text <- "";
3452 G.postRedisplay "listview empty qsearch";
3453 set1 m_active m_first "";
3455 else
3456 let active, first =
3457 match search m_active qsearch ~-1 with
3458 | None ->
3459 state.text <- qsearch ^ " [not found]";
3460 m_active, m_first
3461 | Some active ->
3462 state.text <- qsearch;
3463 active, firstof m_first active
3465 G.postRedisplay "listview backspace qsearch";
3466 set1 active first qsearch
3469 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3470 let pattern = m_qsearch ^ toutf8 key in
3471 let active, first =
3472 match search m_active pattern 1 with
3473 | None ->
3474 state.text <- pattern ^ " [not found]";
3475 m_active, m_first
3476 | Some active ->
3477 state.text <- pattern;
3478 active, firstof m_first active
3480 G.postRedisplay "listview qsearch add";
3481 set1 active first pattern;
3483 | 0xff1b -> (* escape *)
3484 state.text <- "";
3485 if String.length m_qsearch = 0
3486 then (
3487 G.postRedisplay "list view escape";
3488 begin
3489 match
3490 source#exit (coe self) true m_active m_first m_pan m_qsearch
3491 with
3492 | None -> m_prev_uioh
3493 | Some uioh -> uioh
3496 else (
3497 G.postRedisplay "list view kill qsearch";
3498 source#setqsearch "";
3499 coe {< m_qsearch = "" >}
3502 | 0xff0d | 0xff8d -> (* (kp) enter *)
3503 state.text <- "";
3504 let self = {< m_qsearch = "" >} in
3505 source#setqsearch "";
3506 let opt =
3507 G.postRedisplay "listview enter";
3508 if m_active >= 0 && m_active < source#getitemcount
3509 then (
3510 source#exit (coe self) false m_active m_first m_pan "";
3512 else (
3513 source#exit (coe self) true m_active m_first m_pan "";
3516 begin match opt with
3517 | None -> m_prev_uioh
3518 | Some uioh -> uioh
3521 | 0xff9f | 0xffff -> (* (kp) delete *)
3522 coe self
3524 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3525 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3526 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3527 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3529 | 0xff53 | 0xff98 -> (* (kp) right *)
3530 state.text <- "";
3531 G.postRedisplay "listview right";
3532 coe {< m_pan = m_pan - 1 >}
3534 | 0xff51 | 0xff96 -> (* (kp) left *)
3535 state.text <- "";
3536 G.postRedisplay "listview left";
3537 coe {< m_pan = m_pan + 1 >}
3539 | 0xff50 | 0xff95 -> (* (kp) home *)
3540 let active = find 0 1 in
3541 G.postRedisplay "listview home";
3542 set active 0;
3544 | 0xff57 | 0xff9c -> (* (kp) end *)
3545 let first = max 0 (itemcount - fstate.maxrows) in
3546 let active = find (itemcount - 1) ~-1 in
3547 G.postRedisplay "listview end";
3548 set active first;
3550 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3551 coe self
3553 | _ ->
3554 dolog "listview unknown key %#x" key; coe self
3556 method key key mask =
3557 match state.mode with
3558 | Textentry te -> textentrykeyboard key mask te; coe self
3559 | _ -> self#key1 key mask
3561 method button button down x y _ =
3562 let opt =
3563 match button with
3564 | 1 when x > state.winw - conf.scrollbw ->
3565 G.postRedisplay "listview scroll";
3566 if down
3567 then
3568 let _, position, sh = self#scrollph in
3569 if y > truncate position && y < truncate (position +. sh)
3570 then (
3571 state.mstate <- Mscrolly;
3572 Some (coe self)
3574 else
3575 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3576 let first = truncate (s *. float source#getitemcount) in
3577 let first = min source#getitemcount first in
3578 Some (coe {< m_first = first; m_active = first >})
3579 else (
3580 state.mstate <- Mnone;
3581 Some (coe self);
3583 | 1 when not down ->
3584 begin match self#elemunder y with
3585 | Some n ->
3586 G.postRedisplay "listview click";
3587 source#exit
3588 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3589 | _ ->
3590 Some (coe self)
3592 | n when (n == 4 || n == 5) && not down ->
3593 let len = source#getitemcount in
3594 let first =
3595 if n = 5 && m_first + fstate.maxrows >= len
3596 then
3597 m_first
3598 else
3599 let first = m_first + (if n == 4 then -1 else 1) in
3600 bound first 0 (len - 1)
3602 G.postRedisplay "listview wheel";
3603 Some (coe {< m_first = first >})
3604 | n when (n = 6 || n = 7) && not down ->
3605 let inc = m_first + (if n = 7 then -1 else 1) in
3606 G.postRedisplay "listview hwheel";
3607 Some (coe {< m_pan = m_pan + inc >})
3608 | _ ->
3609 Some (coe self)
3611 match opt with
3612 | None -> m_prev_uioh
3613 | Some uioh -> uioh
3615 method motion _ y =
3616 match state.mstate with
3617 | Mscrolly ->
3618 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3619 let first = truncate (s *. float source#getitemcount) in
3620 let first = min source#getitemcount first in
3621 G.postRedisplay "listview motion";
3622 coe {< m_first = first; m_active = first >}
3623 | _ -> coe self
3625 method pmotion x y =
3626 if x < state.winw - conf.scrollbw
3627 then
3628 let n =
3629 match self#elemunder y with
3630 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3631 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3633 let o =
3634 if n != m_active
3635 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3636 else self
3638 coe o
3639 else (
3640 Wsi.setcursor Wsi.CURSOR_INHERIT;
3641 coe self
3644 method infochanged _ = ()
3646 method scrollpw = (0, 0.0, 0.0)
3647 method scrollph =
3648 let nfs = fstate.fontsize + 1 in
3649 let y = m_first * nfs in
3650 let itemcount = source#getitemcount in
3651 let maxi = max 0 (itemcount - fstate.maxrows) in
3652 let maxy = maxi * nfs in
3653 let p, h = scrollph y maxy in
3654 conf.scrollbw, p, h
3656 method modehash = modehash
3657 end;;
3659 class outlinelistview ~source =
3660 object (self)
3661 inherit listview
3662 ~source:(source :> lvsource)
3663 ~trusted:false
3664 ~modehash:(findkeyhash conf "outline")
3665 as super
3667 method key key mask =
3668 let calcfirst first active =
3669 if active > first
3670 then
3671 let rows = active - first in
3672 let maxrows =
3673 if String.length state.text = 0
3674 then fstate.maxrows
3675 else fstate.maxrows - 2
3677 if rows > maxrows then active - maxrows else first
3678 else active
3680 let navigate incr =
3681 let active = m_active + incr in
3682 let active = bound active 0 (source#getitemcount - 1) in
3683 let first = calcfirst m_first active in
3684 G.postRedisplay "outline navigate";
3685 coe {< m_active = active; m_first = first >}
3687 let ctrl = Wsi.withctrl mask in
3688 match key with
3689 | 110 when ctrl -> (* ctrl-n *)
3690 source#narrow m_qsearch;
3691 G.postRedisplay "outline ctrl-n";
3692 coe {< m_first = 0; m_active = 0 >}
3694 | 117 when ctrl -> (* ctrl-u *)
3695 source#denarrow;
3696 G.postRedisplay "outline ctrl-u";
3697 state.text <- "";
3698 coe {< m_first = 0; m_active = 0 >}
3700 | 108 when ctrl -> (* ctrl-l *)
3701 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3702 G.postRedisplay "outline ctrl-l";
3703 coe {< m_first = first >}
3705 | 0xff9f | 0xffff -> (* (kp) delete *)
3706 source#remove m_active;
3707 G.postRedisplay "outline delete";
3708 let active = max 0 (m_active-1) in
3709 coe {< m_first = firstof m_first active;
3710 m_active = active >}
3712 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3713 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3714 | 0xff55 | 0xff9a -> (* (kp) prior *)
3715 navigate ~-(fstate.maxrows)
3716 | 0xff56 | 0xff9b -> (* (kp) next *)
3717 navigate fstate.maxrows
3719 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3720 let o =
3721 if ctrl
3722 then (
3723 G.postRedisplay "outline ctrl right";
3724 {< m_pan = m_pan + 1 >}
3726 else self#updownlevel 1
3728 coe o
3730 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3731 let o =
3732 if ctrl
3733 then (
3734 G.postRedisplay "outline ctrl left";
3735 {< m_pan = m_pan - 1 >}
3737 else self#updownlevel ~-1
3739 coe o
3741 | 0xff50 | 0xff95 -> (* (kp) home *)
3742 G.postRedisplay "outline home";
3743 coe {< m_first = 0; m_active = 0 >}
3745 | 0xff57 | 0xff9c -> (* (kp) end *)
3746 let active = source#getitemcount - 1 in
3747 let first = max 0 (active - fstate.maxrows) in
3748 G.postRedisplay "outline end";
3749 coe {< m_active = active; m_first = first >}
3751 | _ -> super#key key mask
3754 let outlinesource usebookmarks =
3755 let empty = [||] in
3756 (object
3757 inherit lvsourcebase
3758 val mutable m_items = empty
3759 val mutable m_orig_items = empty
3760 val mutable m_prev_items = empty
3761 val mutable m_narrow_pattern = ""
3762 val mutable m_hadremovals = false
3764 method getitemcount =
3765 Array.length m_items + (if m_hadremovals then 1 else 0)
3767 method getitem n =
3768 if n == Array.length m_items && m_hadremovals
3769 then
3770 ("[Confirm removal]", 0)
3771 else
3772 let s, n, _ = m_items.(n) in
3773 (s, n)
3775 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3776 ignore (uioh, first, qsearch);
3777 let confrimremoval = m_hadremovals && active = Array.length m_items in
3778 let items =
3779 if String.length m_narrow_pattern = 0
3780 then m_orig_items
3781 else m_items
3783 if not cancel
3784 then (
3785 if not confrimremoval
3786 then(
3787 let _, _, anchor = m_items.(active) in
3788 gotoghyll (getanchory anchor);
3789 m_items <- items;
3791 else (
3792 state.bookmarks <- Array.to_list m_items;
3793 m_orig_items <- m_items;
3796 else m_items <- items;
3797 m_pan <- pan;
3798 None
3800 method hasaction _ = true
3802 method greetmsg =
3803 if Array.length m_items != Array.length m_orig_items
3804 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3805 else ""
3807 method narrow pattern =
3808 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3809 match reopt with
3810 | None -> ()
3811 | Some re ->
3812 let rec loop accu n =
3813 if n = -1
3814 then (
3815 m_narrow_pattern <- pattern;
3816 m_items <- Array.of_list accu
3818 else
3819 let (s, _, _) as o = m_items.(n) in
3820 let accu =
3821 if (try ignore (Str.search_forward re s 0); true
3822 with Not_found -> false)
3823 then o :: accu
3824 else accu
3826 loop accu (n-1)
3828 loop [] (Array.length m_items - 1)
3830 method denarrow =
3831 m_orig_items <- (
3832 if usebookmarks
3833 then Array.of_list state.bookmarks
3834 else state.outlines
3836 m_items <- m_orig_items
3838 method remove m =
3839 if usebookmarks
3840 then
3841 if m >= 0 && m < Array.length m_items
3842 then (
3843 m_hadremovals <- true;
3844 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3845 let n = if n >= m then n+1 else n in
3846 m_items.(n)
3850 method reset anchor items =
3851 m_hadremovals <- false;
3852 if m_orig_items == empty || m_prev_items != items
3853 then (
3854 m_orig_items <- items;
3855 if String.length m_narrow_pattern = 0
3856 then m_items <- items;
3858 m_prev_items <- items;
3859 let rely = getanchory anchor in
3860 let active =
3861 let rec loop n best bestd =
3862 if n = Array.length m_items
3863 then best
3864 else
3865 let (_, _, anchor) = m_items.(n) in
3866 let orely = getanchory anchor in
3867 let d = abs (orely - rely) in
3868 if d < bestd
3869 then loop (n+1) n d
3870 else loop (n+1) best bestd
3872 loop 0 ~-1 max_int
3874 m_active <- active;
3875 m_first <- firstof m_first active
3876 end)
3879 let enterselector usebookmarks =
3880 let source = outlinesource usebookmarks in
3881 fun errmsg ->
3882 let outlines =
3883 if usebookmarks
3884 then Array.of_list state.bookmarks
3885 else state.outlines
3887 if Array.length outlines = 0
3888 then (
3889 showtext ' ' errmsg;
3891 else (
3892 state.text <- source#greetmsg;
3893 Wsi.setcursor Wsi.CURSOR_INHERIT;
3894 let anchor = getanchor () in
3895 source#reset anchor outlines;
3896 state.uioh <- coe (new outlinelistview ~source);
3897 G.postRedisplay "enter selector";
3901 let enteroutlinemode =
3902 let f = enterselector false in
3903 fun ()-> f "Document has no outline";
3906 let enterbookmarkmode =
3907 let f = enterselector true in
3908 fun () -> f "Document has no bookmarks (yet)";
3911 let color_of_string s =
3912 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3913 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3917 let color_to_string (r, g, b) =
3918 let r = truncate (r *. 256.0)
3919 and g = truncate (g *. 256.0)
3920 and b = truncate (b *. 256.0) in
3921 Printf.sprintf "%d/%d/%d" r g b
3924 let irect_of_string s =
3925 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3928 let irect_to_string (x0,y0,x1,y1) =
3929 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3932 let makecheckers () =
3933 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3934 following to say:
3935 converted by Issac Trotts. July 25, 2002 *)
3936 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3937 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3938 let id = GlTex.gen_texture () in
3939 GlTex.bind_texture `texture_2d id;
3940 GlPix.store (`unpack_alignment 1);
3941 GlTex.image2d image;
3942 List.iter (GlTex.parameter ~target:`texture_2d)
3943 [ `mag_filter `nearest; `min_filter `nearest ];
3947 let setcheckers enabled =
3948 match state.texid with
3949 | None ->
3950 if enabled then state.texid <- Some (makecheckers ())
3952 | Some texid ->
3953 if not enabled
3954 then (
3955 GlTex.delete_texture texid;
3956 state.texid <- None;
3960 let int_of_string_with_suffix s =
3961 let l = String.length s in
3962 let s1, shift =
3963 if l > 1
3964 then
3965 let suffix = Char.lowercase s.[l-1] in
3966 match suffix with
3967 | 'k' -> String.sub s 0 (l-1), 10
3968 | 'm' -> String.sub s 0 (l-1), 20
3969 | 'g' -> String.sub s 0 (l-1), 30
3970 | _ -> s, 0
3971 else s, 0
3973 let n = int_of_string s1 in
3974 let m = n lsl shift in
3975 if m < 0 || m < n
3976 then raise (Failure "value too large")
3977 else m
3980 let string_with_suffix_of_int n =
3981 if n = 0
3982 then "0"
3983 else
3984 let n, s =
3985 if n land ((1 lsl 30) - 1) = 0
3986 then n lsr 30, "G"
3987 else (
3988 if n land ((1 lsl 20) - 1) = 0
3989 then n lsr 20, "M"
3990 else (
3991 if n land ((1 lsl 10) - 1) = 0
3992 then n lsr 10, "K"
3993 else n, ""
3997 let rec loop s n =
3998 let h = n mod 1000 in
3999 let n = n / 1000 in
4000 if n = 0
4001 then string_of_int h ^ s
4002 else (
4003 let s = Printf.sprintf "_%03d%s" h s in
4004 loop s n
4007 loop "" n ^ s;
4010 let defghyllscroll = (40, 8, 32);;
4011 let ghyllscroll_of_string s =
4012 let (n, a, b) as nab =
4013 if s = "default"
4014 then defghyllscroll
4015 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
4017 if n <= a || n <= b || a >= b
4018 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
4019 nab;
4022 let ghyllscroll_to_string ((n, a, b) as nab) =
4023 if nab = defghyllscroll
4024 then "default"
4025 else Printf.sprintf "%d,%d,%d" n a b;
4028 let describe_location () =
4029 let f (fn, _) l =
4030 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
4032 let fn, ln = List.fold_left f (-1, -1) state.layout in
4033 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4034 let percent =
4035 if maxy <= 0
4036 then 100.
4037 else (100. *. (float state.y /. float maxy))
4039 if fn = ln
4040 then
4041 Printf.sprintf "page %d of %d [%.2f%%]"
4042 (fn+1) state.pagecount percent
4043 else
4044 Printf.sprintf
4045 "pages %d-%d of %d [%.2f%%]"
4046 (fn+1) (ln+1) state.pagecount percent
4049 let setpresentationmode v =
4050 let n = page_of_y state.y in
4051 state.anchor <- (n, 0.0, 1.0);
4052 conf.presentation <- v;
4053 if conf.presentation
4054 then (
4055 if not conf.scrollbarinpm
4056 then state.scrollw <- 0;
4058 else state.scrollw <- conf.scrollbw;
4059 represent ();
4062 let enterinfomode =
4063 let btos b = if b then "\xe2\x88\x9a" else "" in
4064 let showextended = ref false in
4065 let leave mode = function
4066 | Confirm -> state.mode <- mode
4067 | Cancel -> state.mode <- mode in
4068 let src =
4069 (object
4070 val mutable m_first_time = true
4071 val mutable m_l = []
4072 val mutable m_a = [||]
4073 val mutable m_prev_uioh = nouioh
4074 val mutable m_prev_mode = View
4076 inherit lvsourcebase
4078 method reset prev_mode prev_uioh =
4079 m_a <- Array.of_list (List.rev m_l);
4080 m_l <- [];
4081 m_prev_mode <- prev_mode;
4082 m_prev_uioh <- prev_uioh;
4083 if m_first_time
4084 then (
4085 let rec loop n =
4086 if n >= Array.length m_a
4087 then ()
4088 else
4089 match m_a.(n) with
4090 | _, _, _, Action _ -> m_active <- n
4091 | _ -> loop (n+1)
4093 loop 0;
4094 m_first_time <- false;
4097 method int name get set =
4098 m_l <-
4099 (name, `int get, 1, Action (
4100 fun u ->
4101 let ondone s =
4102 try set (int_of_string s)
4103 with exn ->
4104 state.text <- Printf.sprintf "bad integer `%s': %s"
4105 s (exntos exn)
4107 state.text <- "";
4108 let te = name ^ ": ", "", None, intentry, ondone, true in
4109 state.mode <- Textentry (te, leave m_prev_mode);
4111 )) :: m_l
4113 method int_with_suffix name get set =
4114 m_l <-
4115 (name, `intws get, 1, Action (
4116 fun u ->
4117 let ondone s =
4118 try set (int_of_string_with_suffix s)
4119 with exn ->
4120 state.text <- Printf.sprintf "bad integer `%s': %s"
4121 s (exntos exn)
4123 state.text <- "";
4124 let te =
4125 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4127 state.mode <- Textentry (te, leave m_prev_mode);
4129 )) :: m_l
4131 method bool ?(offset=1) ?(btos=btos) name get set =
4132 m_l <-
4133 (name, `bool (btos, get), offset, Action (
4134 fun u ->
4135 let v = get () in
4136 set (not v);
4138 )) :: m_l
4140 method color name get set =
4141 m_l <-
4142 (name, `color get, 1, Action (
4143 fun u ->
4144 let invalid = (nan, nan, nan) in
4145 let ondone s =
4146 let c =
4147 try color_of_string s
4148 with exn ->
4149 state.text <- Printf.sprintf "bad color `%s': %s"
4150 s (exntos exn);
4151 invalid
4153 if c <> invalid
4154 then set c;
4156 let te = name ^ ": ", "", None, textentry, ondone, true in
4157 state.text <- color_to_string (get ());
4158 state.mode <- Textentry (te, leave m_prev_mode);
4160 )) :: m_l
4162 method string name get set =
4163 m_l <-
4164 (name, `string get, 1, Action (
4165 fun u ->
4166 let ondone s = set s in
4167 let te = name ^ ": ", "", None, textentry, ondone, true in
4168 state.mode <- Textentry (te, leave m_prev_mode);
4170 )) :: m_l
4172 method colorspace name get set =
4173 m_l <-
4174 (name, `string get, 1, Action (
4175 fun _ ->
4176 let source =
4177 let vals = [| "rgb"; "bgr"; "gray" |] in
4178 (object
4179 inherit lvsourcebase
4181 initializer
4182 m_active <- int_of_colorspace conf.colorspace;
4183 m_first <- 0;
4185 method getitemcount = Array.length vals
4186 method getitem n = (vals.(n), 0)
4187 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4188 ignore (uioh, first, pan, qsearch);
4189 if not cancel then set active;
4190 None
4191 method hasaction _ = true
4192 end)
4194 state.text <- "";
4195 let modehash = findkeyhash conf "info" in
4196 coe (new listview ~source ~trusted:true ~modehash)
4197 )) :: m_l
4199 method caption s offset =
4200 m_l <- (s, `empty, offset, Noaction) :: m_l
4202 method caption2 s f offset =
4203 m_l <- (s, `string f, offset, Noaction) :: m_l
4205 method getitemcount = Array.length m_a
4207 method getitem n =
4208 let tostr = function
4209 | `int f -> string_of_int (f ())
4210 | `intws f -> string_with_suffix_of_int (f ())
4211 | `string f -> f ()
4212 | `color f -> color_to_string (f ())
4213 | `bool (btos, f) -> btos (f ())
4214 | `empty -> ""
4216 let name, t, offset, _ = m_a.(n) in
4217 ((let s = tostr t in
4218 if String.length s > 0
4219 then Printf.sprintf "%s\t%s" name s
4220 else name),
4221 offset)
4223 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4224 let uiohopt =
4225 if not cancel
4226 then (
4227 m_qsearch <- qsearch;
4228 let uioh =
4229 match m_a.(active) with
4230 | _, _, _, Action f -> f uioh
4231 | _ -> uioh
4233 Some uioh
4235 else None
4237 m_active <- active;
4238 m_first <- first;
4239 m_pan <- pan;
4240 uiohopt
4242 method hasaction n =
4243 match m_a.(n) with
4244 | _, _, _, Action _ -> true
4245 | _ -> false
4246 end)
4248 let rec fillsrc prevmode prevuioh =
4249 let sep () = src#caption "" 0 in
4250 let colorp name get set =
4251 src#string name
4252 (fun () -> color_to_string (get ()))
4253 (fun v ->
4255 let c = color_of_string v in
4256 set c
4257 with exn ->
4258 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4261 let oldmode = state.mode in
4262 let birdseye = isbirdseye state.mode in
4264 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4266 src#bool "presentation mode"
4267 (fun () -> conf.presentation)
4268 (fun v -> setpresentationmode v);
4270 src#bool "ignore case in searches"
4271 (fun () -> conf.icase)
4272 (fun v -> conf.icase <- v);
4274 src#bool "preload"
4275 (fun () -> conf.preload)
4276 (fun v -> conf.preload <- v);
4278 src#bool "highlight links"
4279 (fun () -> conf.hlinks)
4280 (fun v -> conf.hlinks <- v);
4282 src#bool "under info"
4283 (fun () -> conf.underinfo)
4284 (fun v -> conf.underinfo <- v);
4286 src#bool "persistent bookmarks"
4287 (fun () -> conf.savebmarks)
4288 (fun v -> conf.savebmarks <- v);
4290 src#bool "proportional display"
4291 (fun () -> conf.proportional)
4292 (fun v -> reqlayout conf.angle v);
4294 src#bool "trim margins"
4295 (fun () -> conf.trimmargins)
4296 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4298 src#bool "persistent location"
4299 (fun () -> conf.jumpback)
4300 (fun v -> conf.jumpback <- v);
4302 sep ();
4303 src#int "inter-page space"
4304 (fun () -> conf.interpagespace)
4305 (fun n ->
4306 conf.interpagespace <- n;
4307 docolumns conf.columns;
4308 let pageno, py =
4309 match state.layout with
4310 | [] -> 0, 0
4311 | l :: _ ->
4312 l.pageno, l.pagey
4314 state.maxy <- calcheight ();
4315 let y = getpagey pageno in
4316 gotoy (y + py)
4319 src#int "page bias"
4320 (fun () -> conf.pagebias)
4321 (fun v -> conf.pagebias <- v);
4323 src#int "scroll step"
4324 (fun () -> conf.scrollstep)
4325 (fun n -> conf.scrollstep <- n);
4327 src#int "horizontal scroll step"
4328 (fun () -> conf.hscrollstep)
4329 (fun v -> conf.hscrollstep <- v);
4331 src#int "auto scroll step"
4332 (fun () ->
4333 match state.autoscroll with
4334 | Some step -> step
4335 | _ -> conf.autoscrollstep)
4336 (fun n ->
4337 if state.autoscroll <> None
4338 then state.autoscroll <- Some n;
4339 conf.autoscrollstep <- n);
4341 src#int "zoom"
4342 (fun () -> truncate (conf.zoom *. 100.))
4343 (fun v -> setzoom ((float v) /. 100.));
4345 src#int "rotation"
4346 (fun () -> conf.angle)
4347 (fun v -> reqlayout v conf.proportional);
4349 src#int "scroll bar width"
4350 (fun () -> state.scrollw)
4351 (fun v ->
4352 state.scrollw <- v;
4353 conf.scrollbw <- v;
4354 reshape state.winw state.winh;
4357 src#int "scroll handle height"
4358 (fun () -> conf.scrollh)
4359 (fun v -> conf.scrollh <- v;);
4361 src#int "thumbnail width"
4362 (fun () -> conf.thumbw)
4363 (fun v ->
4364 conf.thumbw <- min 4096 v;
4365 match oldmode with
4366 | Birdseye beye ->
4367 leavebirdseye beye false;
4368 enterbirdseye ()
4369 | _ -> ()
4372 let mode = state.mode in
4373 src#string "columns"
4374 (fun () ->
4375 match conf.columns with
4376 | Csingle _ -> "1"
4377 | Cmulti (multi, _) -> multicolumns_to_string multi
4378 | Csplit (count, _) -> "-" ^ string_of_int count
4380 (fun v ->
4381 let n, a, b = multicolumns_of_string v in
4382 setcolumns mode n a b);
4384 sep ();
4385 src#caption "Presentation mode" 0;
4386 src#bool "scrollbar visible"
4387 (fun () -> conf.scrollbarinpm)
4388 (fun v ->
4389 if v != conf.scrollbarinpm
4390 then (
4391 conf.scrollbarinpm <- v;
4392 if conf.presentation
4393 then (
4394 state.scrollw <- if v then conf.scrollbw else 0;
4395 reshape state.winw state.winh;
4400 sep ();
4401 src#caption "Pixmap cache" 0;
4402 src#int_with_suffix "size (advisory)"
4403 (fun () -> conf.memlimit)
4404 (fun v -> conf.memlimit <- v);
4406 src#caption2 "used"
4407 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4408 (string_with_suffix_of_int state.memused)
4409 (Hashtbl.length state.tilemap)) 1;
4411 sep ();
4412 src#caption "Layout" 0;
4413 src#caption2 "Dimension"
4414 (fun () ->
4415 Printf.sprintf "%dx%d (virtual %dx%d)"
4416 state.winw state.winh
4417 state.w state.maxy)
4419 if conf.debug
4420 then
4421 src#caption2 "Position" (fun () ->
4422 Printf.sprintf "%dx%d" state.x state.y
4424 else
4425 src#caption2 "Visible" (fun () -> describe_location ()) 1
4428 sep ();
4429 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4430 "Save these parameters as global defaults at exit"
4431 (fun () -> conf.bedefault)
4432 (fun v -> conf.bedefault <- v)
4435 sep ();
4436 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4437 src#bool ~offset:0 ~btos "Extended parameters"
4438 (fun () -> !showextended)
4439 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4440 if !showextended
4441 then (
4442 src#bool "checkers"
4443 (fun () -> conf.checkers)
4444 (fun v -> conf.checkers <- v; setcheckers v);
4445 src#bool "update cursor"
4446 (fun () -> conf.updatecurs)
4447 (fun v -> conf.updatecurs <- v);
4448 src#bool "verbose"
4449 (fun () -> conf.verbose)
4450 (fun v -> conf.verbose <- v);
4451 src#bool "invert colors"
4452 (fun () -> conf.invert)
4453 (fun v -> conf.invert <- v);
4454 src#bool "max fit"
4455 (fun () -> conf.maxhfit)
4456 (fun v -> conf.maxhfit <- v);
4457 src#bool "redirect stderr"
4458 (fun () -> conf.redirectstderr)
4459 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4460 src#string "uri launcher"
4461 (fun () -> conf.urilauncher)
4462 (fun v -> conf.urilauncher <- v);
4463 src#string "path launcher"
4464 (fun () -> conf.pathlauncher)
4465 (fun v -> conf.pathlauncher <- v);
4466 src#string "tile size"
4467 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4468 (fun v ->
4470 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4471 conf.tilew <- max 64 w;
4472 conf.tileh <- max 64 h;
4473 flushtiles ();
4474 with exn ->
4475 state.text <- Printf.sprintf "bad tile size `%s': %s"
4476 v (exntos exn)
4478 src#int "texture count"
4479 (fun () -> conf.texcount)
4480 (fun v ->
4481 if realloctexts v
4482 then conf.texcount <- v
4483 else showtext '!' " Failed to set texture count please retry later"
4485 src#int "slice height"
4486 (fun () -> conf.sliceheight)
4487 (fun v ->
4488 conf.sliceheight <- v;
4489 wcmd "sliceh %d" conf.sliceheight;
4491 src#int "anti-aliasing level"
4492 (fun () -> conf.aalevel)
4493 (fun v ->
4494 conf.aalevel <- bound v 0 8;
4495 state.anchor <- getanchor ();
4496 opendoc state.path state.password;
4498 src#string "page scroll scaling factor"
4499 (fun () -> string_of_float conf.pgscale)
4500 (fun v ->
4502 let s = float_of_string v in
4503 conf.pgscale <- s
4504 with exn ->
4505 state.text <- Printf.sprintf
4506 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4509 src#int "ui font size"
4510 (fun () -> fstate.fontsize)
4511 (fun v -> setfontsize (bound v 5 100));
4512 src#int "hint font size"
4513 (fun () -> conf.hfsize)
4514 (fun v -> conf.hfsize <- bound v 5 100);
4515 colorp "background color"
4516 (fun () -> conf.bgcolor)
4517 (fun v -> conf.bgcolor <- v);
4518 src#bool "crop hack"
4519 (fun () -> conf.crophack)
4520 (fun v -> conf.crophack <- v);
4521 src#string "trim fuzz"
4522 (fun () -> irect_to_string conf.trimfuzz)
4523 (fun v ->
4525 conf.trimfuzz <- irect_of_string v;
4526 if conf.trimmargins
4527 then settrim true conf.trimfuzz;
4528 with exn ->
4529 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4531 src#string "throttle"
4532 (fun () ->
4533 match conf.maxwait with
4534 | None -> "show place holder if page is not ready"
4535 | Some time ->
4536 if time = infinity
4537 then "wait for page to fully render"
4538 else
4539 "wait " ^ string_of_float time
4540 ^ " seconds before showing placeholder"
4542 (fun v ->
4544 let f = float_of_string v in
4545 if f <= 0.0
4546 then conf.maxwait <- None
4547 else conf.maxwait <- Some f
4548 with exn ->
4549 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4551 src#string "ghyll scroll"
4552 (fun () ->
4553 match conf.ghyllscroll with
4554 | None -> ""
4555 | Some nab -> ghyllscroll_to_string nab
4557 (fun v ->
4559 let gs =
4560 if String.length v = 0
4561 then None
4562 else Some (ghyllscroll_of_string v)
4564 conf.ghyllscroll <- gs
4565 with exn ->
4566 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4568 src#string "selection command"
4569 (fun () -> conf.selcmd)
4570 (fun v -> conf.selcmd <- v);
4571 src#string "synctex command"
4572 (fun () -> conf.stcmd)
4573 (fun v -> conf.stcmd <- v);
4574 src#colorspace "color space"
4575 (fun () -> colorspace_to_string conf.colorspace)
4576 (fun v ->
4577 conf.colorspace <- colorspace_of_int v;
4578 wcmd "cs %d" v;
4579 load state.layout;
4581 if pbousable ()
4582 then
4583 src#bool "use PBO"
4584 (fun () -> conf.usepbo)
4585 (fun v -> conf.usepbo <- v);
4586 src#bool "mouse wheel scrolls pages"
4587 (fun () -> conf.wheelbypage)
4588 (fun v -> conf.wheelbypage <- v);
4591 sep ();
4592 src#caption "Document" 0;
4593 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4594 src#caption2 "Pages"
4595 (fun () -> string_of_int state.pagecount) 1;
4596 src#caption2 "Dimensions"
4597 (fun () -> string_of_int (List.length state.pdims)) 1;
4598 if conf.trimmargins
4599 then (
4600 sep ();
4601 src#caption "Trimmed margins" 0;
4602 src#caption2 "Dimensions"
4603 (fun () -> string_of_int (List.length state.pdims)) 1;
4606 sep ();
4607 src#caption "OpenGL" 0;
4608 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4609 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4610 src#reset prevmode prevuioh;
4612 fun () ->
4613 state.text <- "";
4614 let prevmode = state.mode
4615 and prevuioh = state.uioh in
4616 fillsrc prevmode prevuioh;
4617 let source = (src :> lvsource) in
4618 let modehash = findkeyhash conf "info" in
4619 state.uioh <- coe (object (self)
4620 inherit listview ~source ~trusted:true ~modehash as super
4621 val mutable m_prevmemused = 0
4622 method infochanged = function
4623 | Memused ->
4624 if m_prevmemused != state.memused
4625 then (
4626 m_prevmemused <- state.memused;
4627 G.postRedisplay "memusedchanged";
4629 | Pdim -> G.postRedisplay "pdimchanged"
4630 | Docinfo -> fillsrc prevmode prevuioh
4632 method key key mask =
4633 if not (Wsi.withctrl mask)
4634 then
4635 match key with
4636 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4637 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4638 | _ -> super#key key mask
4639 else super#key key mask
4640 end);
4641 G.postRedisplay "info";
4644 let enterhelpmode =
4645 let source =
4646 (object
4647 inherit lvsourcebase
4648 method getitemcount = Array.length state.help
4649 method getitem n =
4650 let s, l, _ = state.help.(n) in
4651 (s, l)
4653 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4654 let optuioh =
4655 if not cancel
4656 then (
4657 m_qsearch <- qsearch;
4658 match state.help.(active) with
4659 | _, _, Action f -> Some (f uioh)
4660 | _ -> Some (uioh)
4662 else None
4664 m_active <- active;
4665 m_first <- first;
4666 m_pan <- pan;
4667 optuioh
4669 method hasaction n =
4670 match state.help.(n) with
4671 | _, _, Action _ -> true
4672 | _ -> false
4674 initializer
4675 m_active <- -1
4676 end)
4677 in fun () ->
4678 let modehash = findkeyhash conf "help" in
4679 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4680 G.postRedisplay "help";
4683 let entermsgsmode =
4684 let msgsource =
4685 let re = Str.regexp "[\r\n]" in
4686 (object
4687 inherit lvsourcebase
4688 val mutable m_items = [||]
4690 method getitemcount = 1 + Array.length m_items
4692 method getitem n =
4693 if n = 0
4694 then "[Clear]", 0
4695 else m_items.(n-1), 0
4697 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4698 ignore uioh;
4699 if not cancel
4700 then (
4701 if active = 0
4702 then Buffer.clear state.errmsgs;
4703 m_qsearch <- qsearch;
4705 m_active <- active;
4706 m_first <- first;
4707 m_pan <- pan;
4708 None
4710 method hasaction n =
4711 n = 0
4713 method reset =
4714 state.newerrmsgs <- false;
4715 let l = Str.split re (Buffer.contents state.errmsgs) in
4716 m_items <- Array.of_list l
4718 initializer
4719 m_active <- 0
4720 end)
4721 in fun () ->
4722 state.text <- "";
4723 msgsource#reset;
4724 let source = (msgsource :> lvsource) in
4725 let modehash = findkeyhash conf "listview" in
4726 state.uioh <- coe (object
4727 inherit listview ~source ~trusted:false ~modehash as super
4728 method display =
4729 if state.newerrmsgs
4730 then msgsource#reset;
4731 super#display
4732 end);
4733 G.postRedisplay "msgs";
4736 let quickbookmark ?title () =
4737 match state.layout with
4738 | [] -> ()
4739 | l :: _ ->
4740 let title =
4741 match title with
4742 | None ->
4743 let sec = Unix.gettimeofday () in
4744 let tm = Unix.localtime sec in
4745 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4746 (l.pageno+1)
4747 tm.Unix.tm_mday
4748 tm.Unix.tm_mon
4749 (tm.Unix.tm_year + 1900)
4750 tm.Unix.tm_hour
4751 tm.Unix.tm_min
4752 | Some title -> title
4754 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4757 let doreshape w h =
4758 Wsi.reshape w h;
4761 let setautoscrollspeed step goingdown =
4762 let incr = max 1 ((abs step) / 2) in
4763 let incr = if goingdown then incr else -incr in
4764 let astep = step + incr in
4765 state.autoscroll <- Some astep;
4768 let gotounder = function
4769 | Ulinkgoto (pageno, top) ->
4770 if pageno >= 0
4771 then (
4772 addnav ();
4773 gotopage1 pageno top;
4776 | Ulinkuri s ->
4777 gotouri s
4779 | Uremote (filename, pageno) ->
4780 let path =
4781 if Sys.file_exists filename
4782 then filename
4783 else
4784 let dir = Filename.dirname state.path in
4785 let path = Filename.concat dir filename in
4786 if Sys.file_exists path
4787 then path
4788 else ""
4790 if String.length path > 0
4791 then (
4792 let anchor = getanchor () in
4793 let ranchor = state.path, state.password, anchor in
4794 state.anchor <- (pageno, 0.0, 0.0);
4795 state.ranchors <- ranchor :: state.ranchors;
4796 opendoc path "";
4798 else showtext '!' ("Could not find " ^ filename)
4800 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4803 let canpan () =
4804 match conf.columns with
4805 | Csplit _ -> true
4806 | _ -> conf.zoom > 1.0
4809 let existsinrow pageno (columns, coverA, coverB) p =
4810 let last = ((pageno - coverA) mod columns) + columns in
4811 let rec any = function
4812 | [] -> false
4813 | l :: rest ->
4814 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4815 then p l
4816 else (
4817 if not (p l)
4818 then (if l.pageno = last then false else any rest)
4819 else true
4822 any state.layout
4825 let nextpage () =
4826 match state.layout with
4827 | [] ->
4828 let pageno = page_of_y state.y in
4829 gotoghyll (getpagey (pageno+1))
4830 | l :: rest ->
4831 match conf.columns with
4832 | Csingle _ ->
4833 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4834 then
4835 let y = clamp (pgscale state.winh) in
4836 gotoghyll y
4837 else
4838 let pageno = min (l.pageno+1) (state.pagecount-1) in
4839 gotoghyll (getpagey pageno)
4840 | Cmulti ((c, _, _) as cl, _) ->
4841 if conf.presentation
4842 && (existsinrow l.pageno cl
4843 (fun l -> l.pageh > l.pagey + l.pagevh))
4844 then
4845 let y = clamp (pgscale state.winh) in
4846 gotoghyll y
4847 else
4848 let pageno = min (l.pageno+c) (state.pagecount-1) in
4849 gotoghyll (getpagey pageno)
4850 | Csplit (n, _) ->
4851 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4852 then
4853 let pagey, pageh = getpageyh l.pageno in
4854 let pagey = pagey + pageh * l.pagecol in
4855 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4856 gotoghyll (pagey + pageh + ips)
4859 let prevpage () =
4860 match state.layout with
4861 | [] ->
4862 let pageno = page_of_y state.y in
4863 gotoghyll (getpagey (pageno-1))
4864 | l :: _ ->
4865 match conf.columns with
4866 | Csingle _ ->
4867 if conf.presentation && l.pagey != 0
4868 then
4869 gotoghyll (clamp (pgscale ~-(state.winh)))
4870 else
4871 let pageno = max 0 (l.pageno-1) in
4872 gotoghyll (getpagey pageno)
4873 | Cmulti ((c, _, coverB) as cl, _) ->
4874 if conf.presentation &&
4875 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4876 then
4877 gotoghyll (clamp (pgscale ~-(state.winh)))
4878 else
4879 let decr =
4880 if l.pageno = state.pagecount - coverB
4881 then 1
4882 else c
4884 let pageno = max 0 (l.pageno-decr) in
4885 gotoghyll (getpagey pageno)
4886 | Csplit (n, _) ->
4887 let y =
4888 if l.pagecol = 0
4889 then
4890 if l.pageno = 0
4891 then l.pagey
4892 else
4893 let pageno = max 0 (l.pageno-1) in
4894 let pagey, pageh = getpageyh pageno in
4895 pagey + (n-1)*pageh
4896 else
4897 let pagey, pageh = getpageyh l.pageno in
4898 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4900 gotoghyll y
4903 let viewkeyboard key mask =
4904 let enttext te =
4905 let mode = state.mode in
4906 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4907 state.text <- "";
4908 enttext ();
4909 G.postRedisplay "view:enttext"
4911 let ctrl = Wsi.withctrl mask in
4912 let key =
4913 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4915 match key with
4916 | 81 -> (* Q *)
4917 exit 0
4919 | 0xff63 -> (* insert *)
4920 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4921 then (
4922 state.mode <- LinkNav (Ltgendir 0);
4923 gotoy state.y;
4925 else showtext '!' "Keyboard link navigation does not work under rotation"
4927 | 0xff1b | 113 -> (* escape / q *)
4928 begin match state.mstate with
4929 | Mzoomrect _ ->
4930 state.mstate <- Mnone;
4931 Wsi.setcursor Wsi.CURSOR_INHERIT;
4932 G.postRedisplay "kill zoom rect";
4933 | _ ->
4934 begin match state.mode with
4935 | LinkNav _ ->
4936 state.mode <- View;
4937 G.postRedisplay "esc leave linknav"
4938 | _ ->
4939 match state.ranchors with
4940 | [] -> raise Quit
4941 | (path, password, anchor) :: rest ->
4942 state.ranchors <- rest;
4943 state.anchor <- anchor;
4944 opendoc path password
4945 end;
4946 end;
4948 | 0xff08 -> (* backspace *)
4949 gotoghyll (getnav ~-1)
4951 | 111 -> (* o *)
4952 enteroutlinemode ()
4954 | 117 -> (* u *)
4955 state.rects <- [];
4956 state.text <- "";
4957 G.postRedisplay "dehighlight";
4959 | 47 | 63 -> (* / ? *)
4960 let ondone isforw s =
4961 cbput state.hists.pat s;
4962 state.searchpattern <- s;
4963 search s isforw
4965 let s = String.create 1 in
4966 s.[0] <- Char.chr key;
4967 enttext (s, "", Some (onhist state.hists.pat),
4968 textentry, ondone (key = 47), true)
4970 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4971 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4972 setzoom (conf.zoom +. incr)
4974 | 43 | 0xffab -> (* + *)
4975 let ondone s =
4976 let n =
4977 try int_of_string s with exc ->
4978 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4979 max_int
4981 if n != max_int
4982 then (
4983 conf.pagebias <- n;
4984 state.text <- "page bias is now " ^ string_of_int n;
4987 enttext ("page bias: ", "", None, intentry, ondone, true)
4989 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4990 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4991 setzoom (max 0.01 (conf.zoom -. decr))
4993 | 45 | 0xffad -> (* - *)
4994 let ondone msg = state.text <- msg in
4995 enttext (
4996 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4997 optentry state.mode, ondone, true
5000 | 48 when ctrl -> (* ctrl-0 *)
5001 setzoom 1.0
5003 | 49 when ctrl -> (* ctrl-1 *)
5004 let cols =
5005 match conf.columns with
5006 | Csingle _ | Cmulti _ -> 1
5007 | Csplit (n, _) -> n
5009 let zoom = zoomforh state.winw state.winh state.scrollw cols in
5010 if zoom < 1.0
5011 then setzoom zoom
5013 | 0xffc6 -> (* f9 *)
5014 togglebirdseye ()
5016 | 57 when ctrl -> (* ctrl-9 *)
5017 togglebirdseye ()
5019 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5020 when not ctrl -> (* 0..9 *)
5021 let ondone s =
5022 let n =
5023 try int_of_string s with exc ->
5024 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5027 if n >= 0
5028 then (
5029 addnav ();
5030 cbput state.hists.pag (string_of_int n);
5031 gotopage1 (n + conf.pagebias - 1) 0;
5034 let pageentry text key =
5035 match Char.unsafe_chr key with
5036 | 'g' -> TEdone text
5037 | _ -> intentry text key
5039 let text = "x" in text.[0] <- Char.chr key;
5040 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5042 | 98 -> (* b *)
5043 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
5044 reshape state.winw state.winh;
5046 | 108 -> (* l *)
5047 conf.hlinks <- not conf.hlinks;
5048 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5049 G.postRedisplay "toggle highlightlinks";
5051 | 70 -> (* F *)
5052 state.glinks <- true;
5053 let mode = state.mode in
5054 state.mode <- Textentry (
5055 (":", "", None, linknentry, linkndone gotounder, false),
5056 (fun _ ->
5057 state.glinks <- false;
5058 state.mode <- mode)
5060 state.text <- "";
5061 G.postRedisplay "view:linkent(F)"
5063 | 121 -> (* y *)
5064 state.glinks <- true;
5065 let mode = state.mode in
5066 state.mode <- Textentry (
5067 (":", "", None, linknentry, linkndone (fun under ->
5068 match Ne.pipe () with
5069 | Ne.Exn exn ->
5070 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
5071 | Ne.Res (r, w) ->
5072 let popened =
5073 try popen conf.selcmd [r, 0; w, -1]; true
5074 with exn ->
5075 showtext '!'
5076 (Printf.sprintf "failed to execute %s: %s"
5077 conf.selcmd (exntos exn));
5078 false
5080 let clo cap fd =
5081 Ne.clo fd (fun msg ->
5082 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
5085 let s = undertext under in
5086 if popened
5087 then
5088 (try
5089 let l = String.length s in
5090 let n = tempfailureretry (Unix.write w s 0) l in
5091 if n != l
5092 then
5093 showtext '!'
5094 (Printf.sprintf
5095 "failed to write %d characters to sel pipe, wrote %d"
5098 with exn ->
5099 showtext '!'
5100 (Printf.sprintf "failed to write to sel pipe: %s"
5101 (exntos exn)
5104 else dolog "%s" s;
5105 clo "pipe/r" r;
5106 clo "pipe/w" w;
5107 ), false
5109 fun _ ->
5110 state.glinks <- false;
5111 state.mode <- mode
5113 state.text <- "";
5114 G.postRedisplay "view:linkent"
5116 | 97 -> (* a *)
5117 begin match state.autoscroll with
5118 | Some step ->
5119 conf.autoscrollstep <- step;
5120 state.autoscroll <- None
5121 | None ->
5122 if conf.autoscrollstep = 0
5123 then state.autoscroll <- Some 1
5124 else state.autoscroll <- Some conf.autoscrollstep
5127 | 112 when ctrl -> (* ctrl-p *)
5128 launchpath ()
5130 | 80 -> (* P *)
5131 setpresentationmode (not conf.presentation);
5132 showtext ' ' ("presentation mode " ^
5133 if conf.presentation then "on" else "off");
5135 | 102 -> (* f *)
5136 if List.mem Wsi.Fullscreen state.winstate
5137 then doreshape conf.cwinw conf.cwinh
5138 else Wsi.fullscreen ()
5140 | 112 | 78 -> (* p|N *)
5141 search state.searchpattern false
5143 | 110 | 0xffc0 -> (* n|F3 *)
5144 search state.searchpattern true
5146 | 116 -> (* t *)
5147 begin match state.layout with
5148 | [] -> ()
5149 | l :: _ ->
5150 gotoghyll (getpagey l.pageno)
5153 | 32 -> (* space *)
5154 nextpage ()
5156 | 0xff9f | 0xffff -> (* delete *)
5157 prevpage ()
5159 | 61 -> (* = *)
5160 showtext ' ' (describe_location ());
5162 | 119 -> (* w *)
5163 begin match state.layout with
5164 | [] -> ()
5165 | l :: _ ->
5166 doreshape (l.pagew + state.scrollw) l.pageh;
5167 G.postRedisplay "w"
5170 | 39 -> (* ' *)
5171 enterbookmarkmode ()
5173 | 104 | 0xffbe -> (* h|F1 *)
5174 enterhelpmode ()
5176 | 105 -> (* i *)
5177 enterinfomode ()
5179 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5180 entermsgsmode ()
5182 | 109 -> (* m *)
5183 let ondone s =
5184 match state.layout with
5185 | l :: _ ->
5186 if String.length s > 0
5187 then
5188 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5189 | _ -> ()
5191 enttext ("bookmark: ", "", None, textentry, ondone, true)
5193 | 126 -> (* ~ *)
5194 quickbookmark ();
5195 showtext ' ' "Quick bookmark added";
5197 | 122 -> (* z *)
5198 begin match state.layout with
5199 | l :: _ ->
5200 let rect = getpdimrect l.pagedimno in
5201 let w, h =
5202 if conf.crophack
5203 then
5204 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5205 truncate (1.2 *. (rect.(3) -. rect.(0))))
5206 else
5207 (truncate (rect.(1) -. rect.(0)),
5208 truncate (rect.(3) -. rect.(0)))
5210 let w = truncate ((float w)*.conf.zoom)
5211 and h = truncate ((float h)*.conf.zoom) in
5212 if w != 0 && h != 0
5213 then (
5214 state.anchor <- getanchor ();
5215 doreshape (w + state.scrollw) (h + conf.interpagespace)
5217 G.postRedisplay "z";
5219 | [] -> ()
5222 | 50 when ctrl -> (* ctrl-2 *)
5223 let maxw = getmaxw () in
5224 if maxw > 0.0
5225 then setzoom (maxw /. float state.winw)
5227 | 60 | 62 -> (* < > *)
5228 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5230 | 91 | 93 -> (* [ ] *)
5231 conf.colorscale <-
5232 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5234 G.postRedisplay "brightness";
5236 | 99 when state.mode = View -> (* c *)
5237 let (c, a, b), z =
5238 match state.prevcolumns with
5239 | None -> (1, 0, 0), 1.0
5240 | Some (columns, z) ->
5241 let cab =
5242 match columns with
5243 | Csplit (c, _) -> -c, 0, 0
5244 | Cmulti ((c, a, b), _) -> c, a, b
5245 | Csingle _ -> 1, 0, 0
5247 cab, z
5249 setcolumns View c a b;
5250 setzoom z;
5252 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5253 setzoom state.prevzoom
5255 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5256 begin match state.autoscroll with
5257 | None ->
5258 begin match state.mode with
5259 | Birdseye beye -> upbirdseye 1 beye
5260 | _ ->
5261 if ctrl
5262 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5263 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5265 | Some n ->
5266 setautoscrollspeed n false
5269 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5270 begin match state.autoscroll with
5271 | None ->
5272 begin match state.mode with
5273 | Birdseye beye -> downbirdseye 1 beye
5274 | _ ->
5275 if ctrl
5276 then gotoy_and_clear_text (clamp (state.winh/2))
5277 else gotoy_and_clear_text (clamp conf.scrollstep)
5279 | Some n ->
5280 setautoscrollspeed n true
5283 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5284 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5285 if canpan ()
5286 then
5287 let dx =
5288 if ctrl
5289 then state.winw / 2
5290 else conf.hscrollstep
5292 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5293 state.x <- state.x + dx;
5294 gotoy_and_clear_text state.y
5295 else (
5296 state.text <- "";
5297 G.postRedisplay "lef/right"
5300 | 0xff55 | 0xff9a -> (* (kp) prior *)
5301 let y =
5302 if ctrl
5303 then
5304 match state.layout with
5305 | [] -> state.y
5306 | l :: _ -> state.y - l.pagey
5307 else
5308 clamp (pgscale (-state.winh))
5310 gotoghyll y
5312 | 0xff56 | 0xff9b -> (* (kp) next *)
5313 let y =
5314 if ctrl
5315 then
5316 match List.rev state.layout with
5317 | [] -> state.y
5318 | l :: _ -> getpagey l.pageno
5319 else
5320 clamp (pgscale state.winh)
5322 gotoghyll y
5324 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5325 gotoghyll 0
5326 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5327 gotoghyll (clamp state.maxy)
5329 | 0xff53 | 0xff98
5330 when Wsi.withalt mask -> (* alt-(kp) right *)
5331 gotoghyll (getnav 1)
5332 | 0xff51 | 0xff96
5333 when Wsi.withalt mask -> (* alt-(kp) left *)
5334 gotoghyll (getnav ~-1)
5336 | 114 -> (* r *)
5337 reload ()
5339 | 118 when conf.debug -> (* v *)
5340 state.rects <- [];
5341 List.iter (fun l ->
5342 match getopaque l.pageno with
5343 | None -> ()
5344 | Some opaque ->
5345 let x0, y0, x1, y1 = pagebbox opaque in
5346 let a,b = float x0, float y0 in
5347 let c,d = float x1, float y0 in
5348 let e,f = float x1, float y1 in
5349 let h,j = float x0, float y1 in
5350 let rect = (a,b,c,d,e,f,h,j) in
5351 debugrect rect;
5352 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5353 ) state.layout;
5354 G.postRedisplay "v";
5356 | _ ->
5357 vlog "huh? %s" (Wsi.keyname key)
5360 let linknavkeyboard key mask linknav =
5361 let getpage pageno =
5362 let rec loop = function
5363 | [] -> None
5364 | l :: _ when l.pageno = pageno -> Some l
5365 | _ :: rest -> loop rest
5366 in loop state.layout
5368 let doexact (pageno, n) =
5369 match getopaque pageno, getpage pageno with
5370 | Some opaque, Some l ->
5371 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5372 then
5373 let under = getlink opaque n in
5374 G.postRedisplay "link gotounder";
5375 gotounder under;
5376 state.mode <- View;
5377 else
5378 let opt, dir =
5379 match key with
5380 | 0xff50 -> (* home *)
5381 Some (findlink opaque LDfirst), -1
5383 | 0xff57 -> (* end *)
5384 Some (findlink opaque LDlast), 1
5386 | 0xff51 -> (* left *)
5387 Some (findlink opaque (LDleft n)), -1
5389 | 0xff53 -> (* right *)
5390 Some (findlink opaque (LDright n)), 1
5392 | 0xff52 -> (* up *)
5393 Some (findlink opaque (LDup n)), -1
5395 | 0xff54 -> (* down *)
5396 Some (findlink opaque (LDdown n)), 1
5398 | _ -> None, 0
5400 let pwl l dir =
5401 begin match findpwl l.pageno dir with
5402 | Pwlnotfound -> ()
5403 | Pwl pageno ->
5404 let notfound dir =
5405 state.mode <- LinkNav (Ltgendir dir);
5406 let y, h = getpageyh pageno in
5407 let y =
5408 if dir < 0
5409 then y + h - state.winh
5410 else y
5412 gotoy y
5414 begin match getopaque pageno, getpage pageno with
5415 | Some opaque, Some _ ->
5416 let link =
5417 let ld = if dir > 0 then LDfirst else LDlast in
5418 findlink opaque ld
5420 begin match link with
5421 | Lfound m ->
5422 showlinktype (getlink opaque m);
5423 state.mode <- LinkNav (Ltexact (pageno, m));
5424 G.postRedisplay "linknav jpage";
5425 | _ -> notfound dir
5426 end;
5427 | _ -> notfound dir
5428 end;
5429 end;
5431 begin match opt with
5432 | Some Lnotfound -> pwl l dir;
5433 | Some (Lfound m) ->
5434 if m = n
5435 then pwl l dir
5436 else (
5437 let _, y0, _, y1 = getlinkrect opaque m in
5438 if y0 < l.pagey
5439 then gotopage1 l.pageno y0
5440 else (
5441 let d = fstate.fontsize + 1 in
5442 if y1 - l.pagey > l.pagevh - d
5443 then gotopage1 l.pageno (y1 - state.winh - state.hscrollh + d)
5444 else G.postRedisplay "linknav";
5446 showlinktype (getlink opaque m);
5447 state.mode <- LinkNav (Ltexact (l.pageno, m));
5450 | None -> viewkeyboard key mask
5451 end;
5452 | _ -> viewkeyboard key mask
5454 if key = 0xff63
5455 then (
5456 state.mode <- View;
5457 G.postRedisplay "leave linknav"
5459 else
5460 match linknav with
5461 | Ltgendir _ -> viewkeyboard key mask
5462 | Ltexact exact -> doexact exact
5465 let keyboard key mask =
5466 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5467 then wcmd "interrupt"
5468 else state.uioh <- state.uioh#key key mask
5471 let birdseyekeyboard key mask
5472 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5473 let incr =
5474 match conf.columns with
5475 | Csingle _ -> 1
5476 | Cmulti ((c, _, _), _) -> c
5477 | Csplit _ -> failwith "bird's eye split mode"
5479 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5480 match key with
5481 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5482 let y, h = getpageyh pageno in
5483 let top = (state.winh - h) / 2 in
5484 gotoy (max 0 (y - top))
5485 | 0xff0d (* enter *)
5486 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5487 | 0xff1b -> leavebirdseye beye true (* escape *)
5488 | 0xff52 -> upbirdseye incr beye (* up *)
5489 | 0xff54 -> downbirdseye incr beye (* down *)
5490 | 0xff51 -> upbirdseye 1 beye (* left *)
5491 | 0xff53 -> downbirdseye 1 beye (* right *)
5493 | 0xff55 -> (* prior *)
5494 begin match state.layout with
5495 | l :: _ ->
5496 if l.pagey != 0
5497 then (
5498 state.mode <- Birdseye (
5499 oconf, leftx, l.pageno, hooverpageno, anchor
5501 gotopage1 l.pageno 0;
5503 else (
5504 let layout = layout (state.y-state.winh) (pgh state.layout) in
5505 match layout with
5506 | [] -> gotoy (clamp (-state.winh))
5507 | l :: _ ->
5508 state.mode <- Birdseye (
5509 oconf, leftx, l.pageno, hooverpageno, anchor
5511 gotopage1 l.pageno 0
5514 | [] -> gotoy (clamp (-state.winh))
5515 end;
5517 | 0xff56 -> (* next *)
5518 begin match List.rev state.layout with
5519 | l :: _ ->
5520 let layout = layout (state.y + (pgh state.layout)) state.winh in
5521 begin match layout with
5522 | [] ->
5523 let incr = l.pageh - l.pagevh in
5524 if incr = 0
5525 then (
5526 state.mode <-
5527 Birdseye (
5528 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5530 G.postRedisplay "birdseye pagedown";
5532 else gotoy (clamp (incr + conf.interpagespace*2));
5534 | l :: _ ->
5535 state.mode <-
5536 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5537 gotopage1 l.pageno 0;
5540 | [] -> gotoy (clamp state.winh)
5541 end;
5543 | 0xff50 -> (* home *)
5544 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5545 gotopage1 0 0
5547 | 0xff57 -> (* end *)
5548 let pageno = state.pagecount - 1 in
5549 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5550 if not (pagevisible state.layout pageno)
5551 then
5552 let h =
5553 match List.rev state.pdims with
5554 | [] -> state.winh
5555 | (_, _, h, _) :: _ -> h
5557 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5558 else G.postRedisplay "birdseye end";
5559 | _ -> viewkeyboard key mask
5562 let drawpage l linkindexbase =
5563 let color =
5564 match state.mode with
5565 | Textentry _ -> scalecolor 0.4
5566 | LinkNav _
5567 | View -> scalecolor 1.0
5568 | Birdseye (_, _, pageno, hooverpageno, _) ->
5569 if l.pageno = hooverpageno
5570 then scalecolor 0.9
5571 else (
5572 if l.pageno = pageno
5573 then scalecolor 1.0
5574 else scalecolor 0.8
5577 drawtiles l color;
5578 begin match getopaque l.pageno with
5579 | Some opaque ->
5580 if tileready l l.pagex l.pagey
5581 then
5582 let x = l.pagedispx - l.pagex
5583 and y = l.pagedispy - l.pagey in
5584 let hlmask =
5585 match conf.columns with
5586 | Csingle _ | Cmulti _ ->
5587 (if conf.hlinks then 1 else 0)
5588 + (if state.glinks
5589 && not (isbirdseye state.mode) then 2 else 0)
5590 | _ -> 0
5592 let s =
5593 match state.mode with
5594 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5595 | _ -> ""
5597 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5598 else 0
5600 | _ -> 0
5601 end;
5604 let scrollindicator () =
5605 let sbw, ph, sh = state.uioh#scrollph in
5606 let sbh, pw, sw = state.uioh#scrollpw in
5608 GlDraw.color (0.64, 0.64, 0.64);
5609 GlDraw.rect
5610 (float (state.winw - sbw), 0.)
5611 (float state.winw, float state.winh)
5613 GlDraw.rect
5614 (0., float (state.winh - sbh))
5615 (float (state.winw - state.scrollw - 1), float state.winh)
5617 GlDraw.color (0.0, 0.0, 0.0);
5619 GlDraw.rect
5620 (float (state.winw - sbw), ph)
5621 (float state.winw, ph +. sh)
5623 GlDraw.rect
5624 (pw, float (state.winh - sbh))
5625 (pw +. sw, float state.winh)
5629 let showsel () =
5630 match state.mstate with
5631 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5634 | Msel ((x0, y0), (x1, y1)) ->
5635 let rec loop = function
5636 | l :: ls ->
5637 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5638 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5639 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5640 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5641 then
5642 match getopaque l.pageno with
5643 | Some opaque ->
5644 let x0, y0 = pagetranslatepoint l x0 y0 in
5645 let x1, y1 = pagetranslatepoint l x1 y1 in
5646 seltext opaque (x0, y0, x1, y1);
5647 | _ -> ()
5648 else loop ls
5649 | [] -> ()
5651 loop state.layout
5654 let showrects rects =
5655 Gl.enable `blend;
5656 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5657 GlDraw.polygon_mode `both `fill;
5658 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5659 List.iter
5660 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5661 List.iter (fun l ->
5662 if l.pageno = pageno
5663 then (
5664 let dx = float (l.pagedispx - l.pagex) in
5665 let dy = float (l.pagedispy - l.pagey) in
5666 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5667 GlDraw.begins `quads;
5669 GlDraw.vertex2 (x0+.dx, y0+.dy);
5670 GlDraw.vertex2 (x1+.dx, y1+.dy);
5671 GlDraw.vertex2 (x2+.dx, y2+.dy);
5672 GlDraw.vertex2 (x3+.dx, y3+.dy);
5674 GlDraw.ends ();
5676 ) state.layout
5677 ) rects
5679 Gl.disable `blend;
5682 let display () =
5683 GlClear.color (scalecolor2 conf.bgcolor);
5684 GlClear.clear [`color];
5685 let rec loop linkindexbase = function
5686 | l :: rest ->
5687 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5688 loop linkindexbase rest
5689 | [] -> ()
5691 loop 0 state.layout;
5692 let rects =
5693 match state.mode with
5694 | LinkNav (Ltexact (pageno, linkno)) ->
5695 begin match getopaque pageno with
5696 | Some opaque ->
5697 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5698 (pageno, 5, (
5699 float x0, float y0,
5700 float x1, float y0,
5701 float x1, float y1,
5702 float x0, float y1)
5703 ) :: state.rects
5704 | None -> state.rects
5706 | _ -> state.rects
5708 showrects rects;
5709 showsel ();
5710 state.uioh#display;
5711 begin match state.mstate with
5712 | Mzoomrect ((x0, y0), (x1, y1)) ->
5713 Gl.enable `blend;
5714 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5715 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5716 GlDraw.rect (float x0, float y0)
5717 (float x1, float y1);
5718 Gl.disable `blend;
5719 | _ -> ()
5720 end;
5721 enttext ();
5722 scrollindicator ();
5723 Wsi.swapb ();
5726 let zoomrect x y x1 y1 =
5727 let x0 = min x x1
5728 and x1 = max x x1
5729 and y0 = min y y1 in
5730 gotoy (state.y + y0);
5731 state.anchor <- getanchor ();
5732 let zoom = (float state.winw *. conf.zoom) /. float (x1 - x0) in
5733 let margin =
5734 if state.w < state.winw - state.scrollw
5735 then (state.winw - state.scrollw - state.w) / 2
5736 else 0
5738 state.x <- (state.x + margin) - x0;
5739 setzoom zoom;
5740 Wsi.setcursor Wsi.CURSOR_INHERIT;
5741 state.mstate <- Mnone;
5744 let scrollx x =
5745 let winw = state.winw - state.scrollw - 1 in
5746 let s = float x /. float winw in
5747 let destx = truncate (float (state.w + winw) *. s) in
5748 state.x <- winw - destx;
5749 gotoy_and_clear_text state.y;
5750 state.mstate <- Mscrollx;
5753 let scrolly y =
5754 let s = float y /. float state.winh in
5755 let desty = truncate (float (state.maxy - state.winh) *. s) in
5756 gotoy_and_clear_text desty;
5757 state.mstate <- Mscrolly;
5760 let viewmouse button down x y mask =
5761 match button with
5762 | n when (n == 4 || n == 5) && not down ->
5763 if Wsi.withctrl mask
5764 then (
5765 match state.mstate with
5766 | Mzoom (oldn, i) ->
5767 if oldn = n
5768 then (
5769 if i = 2
5770 then
5771 let incr =
5772 match n with
5773 | 5 ->
5774 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5775 | _ ->
5776 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5778 let zoom = conf.zoom -. incr in
5779 setzoom zoom;
5780 state.mstate <- Mzoom (n, 0);
5781 else
5782 state.mstate <- Mzoom (n, i+1);
5784 else state.mstate <- Mzoom (n, 0)
5786 | _ -> state.mstate <- Mzoom (n, 0)
5788 else (
5789 match state.autoscroll with
5790 | Some step -> setautoscrollspeed step (n=4)
5791 | None ->
5792 if conf.wheelbypage
5793 then (
5794 if n = 4
5795 then prevpage ()
5796 else nextpage ()
5798 else
5799 let incr =
5800 if n = 4
5801 then -conf.scrollstep
5802 else conf.scrollstep
5804 let incr = incr * 2 in
5805 let y = clamp incr in
5806 gotoy_and_clear_text y
5809 | n when (n = 6 || n = 7) && not down && canpan () ->
5810 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5811 gotoy_and_clear_text state.y
5813 | 1 when Wsi.withshift mask ->
5814 state.mstate <- Mnone;
5815 if not down then (
5816 match unproject x y with
5817 | Some (pageno, ux, uy) ->
5818 let cmd = Printf.sprintf
5819 "%s %s %d %d %d"
5820 conf.stcmd state.path pageno ux uy
5822 popen cmd []
5823 | None -> ()
5826 | 1 when Wsi.withctrl mask ->
5827 if down
5828 then (
5829 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5830 state.mstate <- Mpan (x, y)
5832 else
5833 state.mstate <- Mnone
5835 | 3 ->
5836 if down
5837 then (
5838 Wsi.setcursor Wsi.CURSOR_CYCLE;
5839 let p = (x, y) in
5840 state.mstate <- Mzoomrect (p, p)
5842 else (
5843 match state.mstate with
5844 | Mzoomrect ((x0, y0), _) ->
5845 if abs (x-x0) > 10 && abs (y - y0) > 10
5846 then zoomrect x0 y0 x y
5847 else (
5848 state.mstate <- Mnone;
5849 Wsi.setcursor Wsi.CURSOR_INHERIT;
5850 G.postRedisplay "kill accidental zoom rect";
5852 | _ ->
5853 Wsi.setcursor Wsi.CURSOR_INHERIT;
5854 state.mstate <- Mnone
5857 | 1 when x > state.winw - state.scrollw ->
5858 if down
5859 then
5860 let _, position, sh = state.uioh#scrollph in
5861 if y > truncate position && y < truncate (position +. sh)
5862 then state.mstate <- Mscrolly
5863 else scrolly y
5864 else
5865 state.mstate <- Mnone
5867 | 1 when y > state.winh - state.hscrollh ->
5868 if down
5869 then
5870 let _, position, sw = state.uioh#scrollpw in
5871 if x > truncate position && x < truncate (position +. sw)
5872 then state.mstate <- Mscrollx
5873 else scrollx x
5874 else
5875 state.mstate <- Mnone
5877 | 1 ->
5878 let dest = if down then getunder x y else Unone in
5879 begin match dest with
5880 | Ulinkgoto _
5881 | Ulinkuri _
5882 | Uremote _
5883 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5884 gotounder dest
5886 | Unone when down ->
5887 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5888 state.mstate <- Mpan (x, y);
5890 | Unone | Utext _ ->
5891 if down
5892 then (
5893 if conf.angle mod 360 = 0
5894 then (
5895 state.mstate <- Msel ((x, y), (x, y));
5896 G.postRedisplay "mouse select";
5899 else (
5900 match state.mstate with
5901 | Mnone -> ()
5903 | Mzoom _ | Mscrollx | Mscrolly ->
5904 state.mstate <- Mnone
5906 | Mzoomrect ((x0, y0), _) ->
5907 zoomrect x0 y0 x y
5909 | Mpan _ ->
5910 Wsi.setcursor Wsi.CURSOR_INHERIT;
5911 state.mstate <- Mnone
5913 | Msel ((x0, y0), (x1, y1)) ->
5914 let rec loop = function
5915 | [] -> ()
5916 | l :: rest ->
5917 let inside =
5918 let a0 = l.pagedispy in
5919 let a1 = a0 + l.pagevh in
5920 let b0 = l.pagedispx in
5921 let b1 = b0 + l.pagevw in
5922 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5923 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5925 if inside
5926 then
5927 match getopaque l.pageno with
5928 | Some opaque ->
5929 begin
5930 match Ne.pipe () with
5931 | Ne.Exn exn ->
5932 showtext '!'
5933 (Printf.sprintf
5934 "can not create sel pipe: %s"
5935 (exntos exn));
5936 | Ne.Res (r, w) ->
5937 let doclose what fd =
5938 Ne.clo fd (fun msg ->
5939 dolog "%s close failed: %s" what msg)
5942 popen conf.selcmd [r, 0; w, -1];
5943 copysel w opaque;
5944 doclose "pipe/r" r;
5945 G.postRedisplay "copysel";
5946 with exn ->
5947 dolog "can not execute %S: %s"
5948 conf.selcmd (exntos exn);
5949 doclose "pipe/r" r;
5950 doclose "pipe/w" w;
5952 | None -> ()
5953 else loop rest
5955 loop state.layout;
5956 Wsi.setcursor Wsi.CURSOR_INHERIT;
5957 state.mstate <- Mnone;
5961 | _ -> ()
5964 let birdseyemouse button down x y mask
5965 (conf, leftx, _, hooverpageno, anchor) =
5966 match button with
5967 | 1 when down ->
5968 let rec loop = function
5969 | [] -> ()
5970 | l :: rest ->
5971 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5972 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5973 then (
5974 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5976 else loop rest
5978 loop state.layout
5979 | 3 -> ()
5980 | _ -> viewmouse button down x y mask
5983 let mouse button down x y mask =
5984 state.uioh <- state.uioh#button button down x y mask;
5987 let motion ~x ~y =
5988 state.uioh <- state.uioh#motion x y
5991 let pmotion ~x ~y =
5992 state.uioh <- state.uioh#pmotion x y;
5995 let uioh = object
5996 method display = ()
5998 method key key mask =
5999 begin match state.mode with
6000 | Textentry textentry -> textentrykeyboard key mask textentry
6001 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6002 | View -> viewkeyboard key mask
6003 | LinkNav linknav -> linknavkeyboard key mask linknav
6004 end;
6005 state.uioh
6007 method button button bstate x y mask =
6008 begin match state.mode with
6009 | LinkNav _
6010 | View -> viewmouse button bstate x y mask
6011 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6012 | Textentry _ -> ()
6013 end;
6014 state.uioh
6016 method motion x y =
6017 begin match state.mode with
6018 | Textentry _ -> ()
6019 | View | Birdseye _ | LinkNav _ ->
6020 match state.mstate with
6021 | Mzoom _ | Mnone -> ()
6023 | Mpan (x0, y0) ->
6024 let dx = x - x0
6025 and dy = y0 - y in
6026 state.mstate <- Mpan (x, y);
6027 if canpan ()
6028 then state.x <- state.x + dx;
6029 let y = clamp dy in
6030 gotoy_and_clear_text y
6032 | Msel (a, _) ->
6033 state.mstate <- Msel (a, (x, y));
6034 G.postRedisplay "motion select";
6036 | Mscrolly ->
6037 let y = min state.winh (max 0 y) in
6038 scrolly y
6040 | Mscrollx ->
6041 let x = min state.winw (max 0 x) in
6042 scrollx x
6044 | Mzoomrect (p0, _) ->
6045 state.mstate <- Mzoomrect (p0, (x, y));
6046 G.postRedisplay "motion zoomrect";
6047 end;
6048 state.uioh
6050 method pmotion x y =
6051 begin match state.mode with
6052 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6053 let rec loop = function
6054 | [] ->
6055 if hooverpageno != -1
6056 then (
6057 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6058 G.postRedisplay "pmotion birdseye no hoover";
6060 | l :: rest ->
6061 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6062 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6063 then (
6064 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6065 G.postRedisplay "pmotion birdseye hoover";
6067 else loop rest
6069 loop state.layout
6071 | Textentry _ -> ()
6073 | LinkNav _
6074 | View ->
6075 match state.mstate with
6076 | Mnone -> updateunder x y
6077 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6079 end;
6080 state.uioh
6082 method infochanged _ = ()
6084 method scrollph =
6085 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6086 let p, h = scrollph state.y maxy in
6087 state.scrollw, p, h
6089 method scrollpw =
6090 let winw = state.winw - state.scrollw - 1 in
6091 let fwinw = float winw in
6092 let sw =
6093 let sw = fwinw /. float state.w in
6094 let sw = fwinw *. sw in
6095 max sw (float conf.scrollh)
6097 let position, sw =
6098 let f = state.w+winw in
6099 let r = float (winw-state.x) /. float f in
6100 let p = fwinw *. r in
6101 p-.sw/.2., sw
6103 let sw =
6104 if position +. sw > fwinw
6105 then fwinw -. position
6106 else sw
6108 state.hscrollh, position, sw
6110 method modehash =
6111 let modename =
6112 match state.mode with
6113 | LinkNav _ -> "links"
6114 | Textentry _ -> "textentry"
6115 | Birdseye _ -> "birdseye"
6116 | View -> "view"
6118 findkeyhash conf modename
6119 end;;
6121 module Config =
6122 struct
6123 open Parser
6125 let fontpath = ref "";;
6127 module KeyMap =
6128 Map.Make (struct type t = (int * int) let compare = compare end);;
6130 let unent s =
6131 let l = String.length s in
6132 let b = Buffer.create l in
6133 unent b s 0 l;
6134 Buffer.contents b;
6137 let home =
6138 try Sys.getenv "HOME"
6139 with exn ->
6140 prerr_endline
6141 ("Can not determine home directory location: " ^ exntos exn);
6145 let modifier_of_string = function
6146 | "alt" -> Wsi.altmask
6147 | "shift" -> Wsi.shiftmask
6148 | "ctrl" | "control" -> Wsi.ctrlmask
6149 | "meta" -> Wsi.metamask
6150 | _ -> 0
6153 let key_of_string =
6154 let r = Str.regexp "-" in
6155 fun s ->
6156 let elems = Str.full_split r s in
6157 let f n k m =
6158 let g s =
6159 let m1 = modifier_of_string s in
6160 if m1 = 0
6161 then (Wsi.namekey s, m)
6162 else (k, m lor m1)
6163 in function
6164 | Str.Delim s when n land 1 = 0 -> g s
6165 | Str.Text s -> g s
6166 | Str.Delim _ -> (k, m)
6168 let rec loop n k m = function
6169 | [] -> (k, m)
6170 | x :: xs ->
6171 let k, m = f n k m x in
6172 loop (n+1) k m xs
6174 loop 0 0 0 elems
6177 let keys_of_string =
6178 let r = Str.regexp "[ \t]" in
6179 fun s ->
6180 let elems = Str.split r s in
6181 List.map key_of_string elems
6184 let copykeyhashes c =
6185 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6188 let config_of c attrs =
6189 let apply c k v =
6191 match k with
6192 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6193 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6194 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6195 | "preload" -> { c with preload = bool_of_string v }
6196 | "page-bias" -> { c with pagebias = int_of_string v }
6197 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6198 | "horizontal-scroll-step" ->
6199 { c with hscrollstep = max (int_of_string v) 1 }
6200 | "auto-scroll-step" ->
6201 { c with autoscrollstep = max 0 (int_of_string v) }
6202 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6203 | "crop-hack" -> { c with crophack = bool_of_string v }
6204 | "throttle" ->
6205 let mw =
6206 match String.lowercase v with
6207 | "true" -> Some infinity
6208 | "false" -> None
6209 | f -> Some (float_of_string f)
6211 { c with maxwait = mw}
6212 | "highlight-links" -> { c with hlinks = bool_of_string v }
6213 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6214 | "vertical-margin" ->
6215 { c with interpagespace = max 0 (int_of_string v) }
6216 | "zoom" ->
6217 let zoom = float_of_string v /. 100. in
6218 let zoom = max zoom 0.0 in
6219 { c with zoom = zoom }
6220 | "presentation" -> { c with presentation = bool_of_string v }
6221 | "rotation-angle" -> { c with angle = int_of_string v }
6222 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6223 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6224 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6225 | "proportional-display" -> { c with proportional = bool_of_string v }
6226 | "pixmap-cache-size" ->
6227 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6228 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6229 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6230 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6231 | "persistent-location" -> { c with jumpback = bool_of_string v }
6232 | "background-color" -> { c with bgcolor = color_of_string v }
6233 | "scrollbar-in-presentation" ->
6234 { c with scrollbarinpm = bool_of_string v }
6235 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6236 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6237 | "mupdf-store-size" ->
6238 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6239 | "checkers" -> { c with checkers = bool_of_string v }
6240 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6241 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6242 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6243 | "uri-launcher" -> { c with urilauncher = unent v }
6244 | "path-launcher" -> { c with pathlauncher = unent v }
6245 | "color-space" -> { c with colorspace = colorspace_of_string v }
6246 | "invert-colors" -> { c with invert = bool_of_string v }
6247 | "brightness" -> { c with colorscale = float_of_string v }
6248 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6249 | "ghyllscroll" ->
6250 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6251 | "columns" ->
6252 let (n, _, _) as nab = multicolumns_of_string v in
6253 if n < 0
6254 then { c with columns = Csplit (-n, [||]) }
6255 else { c with columns = Cmulti (nab, [||]) }
6256 | "birds-eye-columns" ->
6257 { c with beyecolumns = Some (max (int_of_string v) 2) }
6258 | "selection-command" -> { c with selcmd = unent v }
6259 | "synctex-command" -> { c with stcmd = unent v }
6260 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6261 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6262 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6263 | "use-pbo" -> { c with usepbo = bool_of_string v }
6264 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6265 | _ -> c
6266 with exn ->
6267 prerr_endline ("Error processing attribute (`" ^
6268 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6271 let rec fold c = function
6272 | [] -> c
6273 | (k, v) :: rest ->
6274 let c = apply c k v in
6275 fold c rest
6277 fold { c with keyhashes = copykeyhashes c } attrs;
6280 let fromstring f pos n v d =
6281 try f v
6282 with exn ->
6283 dolog "Error processing attribute (%S=%S) at %d\n%s"
6284 n v pos (exntos exn)
6289 let bookmark_of attrs =
6290 let rec fold title page rely visy = function
6291 | ("title", v) :: rest -> fold v page rely visy rest
6292 | ("page", v) :: rest -> fold title v rely visy rest
6293 | ("rely", v) :: rest -> fold title page v visy rest
6294 | ("visy", v) :: rest -> fold title page rely v rest
6295 | _ :: rest -> fold title page rely visy rest
6296 | [] -> title, page, rely, visy
6298 fold "invalid" "0" "0" "0" attrs
6301 let doc_of attrs =
6302 let rec fold path page rely pan visy = function
6303 | ("path", v) :: rest -> fold v page rely pan visy rest
6304 | ("page", v) :: rest -> fold path v rely pan visy rest
6305 | ("rely", v) :: rest -> fold path page v pan visy rest
6306 | ("pan", v) :: rest -> fold path page rely v visy rest
6307 | ("visy", v) :: rest -> fold path page rely pan v rest
6308 | _ :: rest -> fold path page rely pan visy rest
6309 | [] -> path, page, rely, pan, visy
6311 fold "" "0" "0" "0" "0" attrs
6314 let map_of attrs =
6315 let rec fold rs ls = function
6316 | ("out", v) :: rest -> fold v ls rest
6317 | ("in", v) :: rest -> fold rs v rest
6318 | _ :: rest -> fold ls rs rest
6319 | [] -> ls, rs
6321 fold "" "" attrs
6324 let setconf dst src =
6325 dst.scrollbw <- src.scrollbw;
6326 dst.scrollh <- src.scrollh;
6327 dst.icase <- src.icase;
6328 dst.preload <- src.preload;
6329 dst.pagebias <- src.pagebias;
6330 dst.verbose <- src.verbose;
6331 dst.scrollstep <- src.scrollstep;
6332 dst.maxhfit <- src.maxhfit;
6333 dst.crophack <- src.crophack;
6334 dst.autoscrollstep <- src.autoscrollstep;
6335 dst.maxwait <- src.maxwait;
6336 dst.hlinks <- src.hlinks;
6337 dst.underinfo <- src.underinfo;
6338 dst.interpagespace <- src.interpagespace;
6339 dst.zoom <- src.zoom;
6340 dst.presentation <- src.presentation;
6341 dst.angle <- src.angle;
6342 dst.cwinw <- src.cwinw;
6343 dst.cwinh <- src.cwinh;
6344 dst.savebmarks <- src.savebmarks;
6345 dst.memlimit <- src.memlimit;
6346 dst.proportional <- src.proportional;
6347 dst.texcount <- src.texcount;
6348 dst.sliceheight <- src.sliceheight;
6349 dst.thumbw <- src.thumbw;
6350 dst.jumpback <- src.jumpback;
6351 dst.bgcolor <- src.bgcolor;
6352 dst.scrollbarinpm <- src.scrollbarinpm;
6353 dst.tilew <- src.tilew;
6354 dst.tileh <- src.tileh;
6355 dst.mustoresize <- src.mustoresize;
6356 dst.checkers <- src.checkers;
6357 dst.aalevel <- src.aalevel;
6358 dst.trimmargins <- src.trimmargins;
6359 dst.trimfuzz <- src.trimfuzz;
6360 dst.urilauncher <- src.urilauncher;
6361 dst.colorspace <- src.colorspace;
6362 dst.invert <- src.invert;
6363 dst.colorscale <- src.colorscale;
6364 dst.redirectstderr <- src.redirectstderr;
6365 dst.ghyllscroll <- src.ghyllscroll;
6366 dst.columns <- src.columns;
6367 dst.beyecolumns <- src.beyecolumns;
6368 dst.selcmd <- src.selcmd;
6369 dst.updatecurs <- src.updatecurs;
6370 dst.pathlauncher <- src.pathlauncher;
6371 dst.keyhashes <- copykeyhashes src;
6372 dst.hfsize <- src.hfsize;
6373 dst.hscrollstep <- src.hscrollstep;
6374 dst.pgscale <- src.pgscale;
6375 dst.usepbo <- src.usepbo;
6376 dst.wheelbypage <- src.wheelbypage;
6377 dst.stcmd <- src.stcmd;
6380 let get s =
6381 let h = Hashtbl.create 10 in
6382 let dc = { defconf with angle = defconf.angle } in
6383 let rec toplevel v t spos _ =
6384 match t with
6385 | Vdata | Vcdata | Vend -> v
6386 | Vopen ("llppconfig", _, closed) ->
6387 if closed
6388 then v
6389 else { v with f = llppconfig }
6390 | Vopen _ ->
6391 error "unexpected subelement at top level" s spos
6392 | Vclose _ -> error "unexpected close at top level" s spos
6394 and llppconfig v t spos _ =
6395 match t with
6396 | Vdata | Vcdata -> v
6397 | Vend -> error "unexpected end of input in llppconfig" s spos
6398 | Vopen ("defaults", attrs, closed) ->
6399 let c = config_of dc attrs in
6400 setconf dc c;
6401 if closed
6402 then v
6403 else { v with f = defaults }
6405 | Vopen ("ui-font", attrs, closed) ->
6406 let rec getsize size = function
6407 | [] -> size
6408 | ("size", v) :: rest ->
6409 let size =
6410 fromstring int_of_string spos "size" v fstate.fontsize in
6411 getsize size rest
6412 | l -> getsize size l
6414 fstate.fontsize <- getsize fstate.fontsize attrs;
6415 if closed
6416 then v
6417 else { v with f = uifont (Buffer.create 10) }
6419 | Vopen ("doc", attrs, closed) ->
6420 let pathent, spage, srely, span, svisy = doc_of attrs in
6421 let path = unent pathent
6422 and pageno = fromstring int_of_string spos "page" spage 0
6423 and rely = fromstring float_of_string spos "rely" srely 0.0
6424 and pan = fromstring int_of_string spos "pan" span 0
6425 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6426 let c = config_of dc attrs in
6427 let anchor = (pageno, rely, visy) in
6428 if closed
6429 then (Hashtbl.add h path (c, [], pan, anchor); v)
6430 else { v with f = doc path pan anchor c [] }
6432 | Vopen _ ->
6433 error "unexpected subelement in llppconfig" s spos
6435 | Vclose "llppconfig" -> { v with f = toplevel }
6436 | Vclose _ -> error "unexpected close in llppconfig" s spos
6438 and defaults v t spos _ =
6439 match t with
6440 | Vdata | Vcdata -> v
6441 | Vend -> error "unexpected end of input in defaults" s spos
6442 | Vopen ("keymap", attrs, closed) ->
6443 let modename =
6444 try List.assoc "mode" attrs
6445 with Not_found -> "global" in
6446 if closed
6447 then v
6448 else
6449 let ret keymap =
6450 let h = findkeyhash dc modename in
6451 KeyMap.iter (Hashtbl.replace h) keymap;
6452 defaults
6454 { v with f = pkeymap ret KeyMap.empty }
6456 | Vopen (_, _, _) ->
6457 error "unexpected subelement in defaults" s spos
6459 | Vclose "defaults" ->
6460 { v with f = llppconfig }
6462 | Vclose _ -> error "unexpected close in defaults" s spos
6464 and uifont b v t spos epos =
6465 match t with
6466 | Vdata | Vcdata ->
6467 Buffer.add_substring b s spos (epos - spos);
6469 | Vopen (_, _, _) ->
6470 error "unexpected subelement in ui-font" s spos
6471 | Vclose "ui-font" ->
6472 if String.length !fontpath = 0
6473 then fontpath := Buffer.contents b;
6474 { v with f = llppconfig }
6475 | Vclose _ -> error "unexpected close in ui-font" s spos
6476 | Vend -> error "unexpected end of input in ui-font" s spos
6478 and doc path pan anchor c bookmarks v t spos _ =
6479 match t with
6480 | Vdata | Vcdata -> v
6481 | Vend -> error "unexpected end of input in doc" s spos
6482 | Vopen ("bookmarks", _, closed) ->
6483 if closed
6484 then v
6485 else { v with f = pbookmarks path pan anchor c bookmarks }
6487 | Vopen ("keymap", attrs, closed) ->
6488 let modename =
6489 try List.assoc "mode" attrs
6490 with Not_found -> "global"
6492 if closed
6493 then v
6494 else
6495 let ret keymap =
6496 let h = findkeyhash c modename in
6497 KeyMap.iter (Hashtbl.replace h) keymap;
6498 doc path pan anchor c bookmarks
6500 { v with f = pkeymap ret KeyMap.empty }
6502 | Vopen (_, _, _) ->
6503 error "unexpected subelement in doc" s spos
6505 | Vclose "doc" ->
6506 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6507 { v with f = llppconfig }
6509 | Vclose _ -> error "unexpected close in doc" s spos
6511 and pkeymap ret keymap v t spos _ =
6512 match t with
6513 | Vdata | Vcdata -> v
6514 | Vend -> error "unexpected end of input in keymap" s spos
6515 | Vopen ("map", attrs, closed) ->
6516 let r, l = map_of attrs in
6517 let kss = fromstring keys_of_string spos "in" r [] in
6518 let lss = fromstring keys_of_string spos "out" l [] in
6519 let keymap =
6520 match kss with
6521 | [] -> keymap
6522 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6523 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6525 if closed
6526 then { v with f = pkeymap ret keymap }
6527 else
6528 let f () = v in
6529 { v with f = skip "map" f }
6531 | Vopen _ ->
6532 error "unexpected subelement in keymap" s spos
6534 | Vclose "keymap" ->
6535 { v with f = ret keymap }
6537 | Vclose _ -> error "unexpected close in keymap" s spos
6539 and pbookmarks path pan anchor c bookmarks v t spos _ =
6540 match t with
6541 | Vdata | Vcdata -> v
6542 | Vend -> error "unexpected end of input in bookmarks" s spos
6543 | Vopen ("item", attrs, closed) ->
6544 let titleent, spage, srely, svisy = bookmark_of attrs in
6545 let page = fromstring int_of_string spos "page" spage 0
6546 and rely = fromstring float_of_string spos "rely" srely 0.0
6547 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6548 let bookmarks =
6549 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6551 if closed
6552 then { v with f = pbookmarks path pan anchor c bookmarks }
6553 else
6554 let f () = v in
6555 { v with f = skip "item" f }
6557 | Vopen _ ->
6558 error "unexpected subelement in bookmarks" s spos
6560 | Vclose "bookmarks" ->
6561 { v with f = doc path pan anchor c bookmarks }
6563 | Vclose _ -> error "unexpected close in bookmarks" s spos
6565 and skip tag f v t spos _ =
6566 match t with
6567 | Vdata | Vcdata -> v
6568 | Vend ->
6569 error ("unexpected end of input in skipped " ^ tag) s spos
6570 | Vopen (tag', _, closed) ->
6571 if closed
6572 then v
6573 else
6574 let f' () = { v with f = skip tag f } in
6575 { v with f = skip tag' f' }
6576 | Vclose ctag ->
6577 if tag = ctag
6578 then f ()
6579 else error ("unexpected close in skipped " ^ tag) s spos
6582 parse { f = toplevel; accu = () } s;
6583 h, dc;
6586 let do_load f ic =
6588 let len = in_channel_length ic in
6589 let s = String.create len in
6590 really_input ic s 0 len;
6591 f s;
6592 with
6593 | Parse_error (msg, s, pos) ->
6594 let subs = subs s pos in
6595 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6596 failwith ("parse error: " ^ s)
6598 | exn ->
6599 failwith ("config load error: " ^ exntos exn)
6602 let defconfpath =
6603 let dir =
6605 let dir = Filename.concat home ".config" in
6606 if Sys.is_directory dir then dir else home
6607 with _ -> home
6609 Filename.concat dir "llpp.conf"
6612 let confpath = ref defconfpath;;
6614 let load1 f =
6615 if Sys.file_exists !confpath
6616 then
6617 match
6618 (try Some (open_in_bin !confpath)
6619 with exn ->
6620 prerr_endline
6621 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6622 exntos exn);
6623 None
6625 with
6626 | Some ic ->
6627 let success =
6629 f (do_load get ic)
6630 with exn ->
6631 prerr_endline
6632 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6633 exntos exn);
6634 false
6636 close_in ic;
6637 success
6639 | None -> false
6640 else
6641 f (Hashtbl.create 0, defconf)
6644 let load () =
6645 let f (h, dc) =
6646 let pc, pb, px, pa =
6648 Hashtbl.find h (Filename.basename state.path)
6649 with Not_found -> dc, [], 0, emptyanchor
6651 setconf defconf dc;
6652 setconf conf pc;
6653 state.bookmarks <- pb;
6654 state.x <- px;
6655 state.scrollw <- conf.scrollbw;
6656 if conf.jumpback
6657 then state.anchor <- pa;
6658 cbput state.hists.nav pa;
6659 true
6661 load1 f
6664 let add_attrs bb always dc c =
6665 let ob s a b =
6666 if always || a != b
6667 then Printf.bprintf bb "\n %s='%b'" s a
6668 and oi s a b =
6669 if always || a != b
6670 then Printf.bprintf bb "\n %s='%d'" s a
6671 and oI s a b =
6672 if always || a != b
6673 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6674 and oz s a b =
6675 if always || a <> b
6676 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6677 and oF s a b =
6678 if always || a <> b
6679 then Printf.bprintf bb "\n %s='%f'" s a
6680 and oc s a b =
6681 if always || a <> b
6682 then
6683 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6684 and oC s a b =
6685 if always || a <> b
6686 then
6687 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6688 and oR s a b =
6689 if always || a <> b
6690 then
6691 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6692 and os s a b =
6693 if always || a <> b
6694 then
6695 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6696 and og s a b =
6697 if always || a <> b
6698 then
6699 match a with
6700 | None -> ()
6701 | Some (_N, _A, _B) ->
6702 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6703 and oW s a b =
6704 if always || a <> b
6705 then
6706 let v =
6707 match a with
6708 | None -> "false"
6709 | Some f ->
6710 if f = infinity
6711 then "true"
6712 else string_of_float f
6714 Printf.bprintf bb "\n %s='%s'" s v
6715 and oco s a b =
6716 if always || a <> b
6717 then
6718 match a with
6719 | Cmulti ((n, a, b), _) when n > 1 ->
6720 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6721 | Csplit (n, _) when n > 1 ->
6722 Printf.bprintf bb "\n %s='%d'" s ~-n
6723 | _ -> ()
6724 and obeco s a b =
6725 if always || a <> b
6726 then
6727 match a with
6728 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6729 | _ -> ()
6731 oi "width" c.cwinw dc.cwinw;
6732 oi "height" c.cwinh dc.cwinh;
6733 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6734 oi "scroll-handle-height" c.scrollh dc.scrollh;
6735 ob "case-insensitive-search" c.icase dc.icase;
6736 ob "preload" c.preload dc.preload;
6737 oi "page-bias" c.pagebias dc.pagebias;
6738 oi "scroll-step" c.scrollstep dc.scrollstep;
6739 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6740 ob "max-height-fit" c.maxhfit dc.maxhfit;
6741 ob "crop-hack" c.crophack dc.crophack;
6742 oW "throttle" c.maxwait dc.maxwait;
6743 ob "highlight-links" c.hlinks dc.hlinks;
6744 ob "under-cursor-info" c.underinfo dc.underinfo;
6745 oi "vertical-margin" c.interpagespace dc.interpagespace;
6746 oz "zoom" c.zoom dc.zoom;
6747 ob "presentation" c.presentation dc.presentation;
6748 oi "rotation-angle" c.angle dc.angle;
6749 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6750 ob "proportional-display" c.proportional dc.proportional;
6751 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6752 oi "tex-count" c.texcount dc.texcount;
6753 oi "slice-height" c.sliceheight dc.sliceheight;
6754 oi "thumbnail-width" c.thumbw dc.thumbw;
6755 ob "persistent-location" c.jumpback dc.jumpback;
6756 oc "background-color" c.bgcolor dc.bgcolor;
6757 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6758 oi "tile-width" c.tilew dc.tilew;
6759 oi "tile-height" c.tileh dc.tileh;
6760 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6761 ob "checkers" c.checkers dc.checkers;
6762 oi "aalevel" c.aalevel dc.aalevel;
6763 ob "trim-margins" c.trimmargins dc.trimmargins;
6764 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6765 os "uri-launcher" c.urilauncher dc.urilauncher;
6766 os "path-launcher" c.pathlauncher dc.pathlauncher;
6767 oC "color-space" c.colorspace dc.colorspace;
6768 ob "invert-colors" c.invert dc.invert;
6769 oF "brightness" c.colorscale dc.colorscale;
6770 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6771 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6772 oco "columns" c.columns dc.columns;
6773 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6774 os "selection-command" c.selcmd dc.selcmd;
6775 os "synctex-command" c.stcmd dc.stcmd;
6776 ob "update-cursor" c.updatecurs dc.updatecurs;
6777 oi "hint-font-size" c.hfsize dc.hfsize;
6778 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6779 oF "page-scroll-scale" c.pgscale dc.pgscale;
6780 ob "use-pbo" c.usepbo dc.usepbo;
6781 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
6784 let keymapsbuf always dc c =
6785 let bb = Buffer.create 16 in
6786 let rec loop = function
6787 | [] -> ()
6788 | (modename, h) :: rest ->
6789 let dh = findkeyhash dc modename in
6790 if always || h <> dh
6791 then (
6792 if Hashtbl.length h > 0
6793 then (
6794 if Buffer.length bb > 0
6795 then Buffer.add_char bb '\n';
6796 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6797 Hashtbl.iter (fun i o ->
6798 let isdifferent = always ||
6800 let dO = Hashtbl.find dh i in
6801 dO <> o
6802 with Not_found -> true
6804 if isdifferent
6805 then
6806 let addkm (k, m) =
6807 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6808 if Wsi.withalt m then Buffer.add_string bb "alt-";
6809 if Wsi.withshift m then Buffer.add_string bb "shift-";
6810 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6811 Buffer.add_string bb (Wsi.keyname k);
6813 let addkms l =
6814 let rec loop = function
6815 | [] -> ()
6816 | km :: [] -> addkm km
6817 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6819 loop l
6821 Buffer.add_string bb "<map in='";
6822 addkm i;
6823 match o with
6824 | KMinsrt km ->
6825 Buffer.add_string bb "' out='";
6826 addkm km;
6827 Buffer.add_string bb "'/>\n"
6829 | KMinsrl kms ->
6830 Buffer.add_string bb "' out='";
6831 addkms kms;
6832 Buffer.add_string bb "'/>\n"
6834 | KMmulti (ins, kms) ->
6835 Buffer.add_char bb ' ';
6836 addkms ins;
6837 Buffer.add_string bb "' out='";
6838 addkms kms;
6839 Buffer.add_string bb "'/>\n"
6840 ) h;
6841 Buffer.add_string bb "</keymap>";
6844 loop rest
6846 loop c.keyhashes;
6850 let save () =
6851 let uifontsize = fstate.fontsize in
6852 let bb = Buffer.create 32768 in
6853 let w, h =
6854 List.fold_left
6855 (fun (w, h) ws ->
6856 match ws with
6857 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh)
6858 | Wsi.MaxVert -> (w, conf.cwinh)
6859 | Wsi.MaxHorz -> (conf.cwinw, h)
6861 (state.winw, state.winh) state.winstate
6863 conf.cwinw <- w;
6864 conf.cwinh <- h;
6865 let f (h, dc) =
6866 let dc = if conf.bedefault then conf else dc in
6867 Buffer.add_string bb "<llppconfig>\n";
6869 if String.length !fontpath > 0
6870 then
6871 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6872 uifontsize
6873 !fontpath
6874 else (
6875 if uifontsize <> 14
6876 then
6877 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6880 Buffer.add_string bb "<defaults ";
6881 add_attrs bb true dc dc;
6882 let kb = keymapsbuf true dc dc in
6883 if Buffer.length kb > 0
6884 then (
6885 Buffer.add_string bb ">\n";
6886 Buffer.add_buffer bb kb;
6887 Buffer.add_string bb "\n</defaults>\n";
6889 else Buffer.add_string bb "/>\n";
6891 let adddoc path pan anchor c bookmarks =
6892 if bookmarks == [] && c = dc && anchor = emptyanchor
6893 then ()
6894 else (
6895 Printf.bprintf bb "<doc path='%s'"
6896 (enent path 0 (String.length path));
6898 if anchor <> emptyanchor
6899 then (
6900 let n, rely, visy = anchor in
6901 Printf.bprintf bb " page='%d'" n;
6902 if rely > 1e-6
6903 then
6904 Printf.bprintf bb " rely='%f'" rely
6906 if abs_float visy > 1e-6
6907 then
6908 Printf.bprintf bb " visy='%f'" visy
6912 if pan != 0
6913 then Printf.bprintf bb " pan='%d'" pan;
6915 add_attrs bb false dc c;
6916 let kb = keymapsbuf false dc c in
6918 begin match bookmarks with
6919 | [] ->
6920 if Buffer.length kb > 0
6921 then (
6922 Buffer.add_string bb ">\n";
6923 Buffer.add_buffer bb kb;
6924 Buffer.add_string bb "\n</doc>\n";
6926 else Buffer.add_string bb "/>\n"
6927 | _ ->
6928 Buffer.add_string bb ">\n<bookmarks>\n";
6929 List.iter (fun (title, _level, (page, rely, visy)) ->
6930 Printf.bprintf bb
6931 "<item title='%s' page='%d'"
6932 (enent title 0 (String.length title))
6933 page
6935 if rely > 1e-6
6936 then
6937 Printf.bprintf bb " rely='%f'" rely
6939 if abs_float visy > 1e-6
6940 then
6941 Printf.bprintf bb " visy='%f'" visy
6943 Buffer.add_string bb "/>\n";
6944 ) bookmarks;
6945 Buffer.add_string bb "</bookmarks>";
6946 if Buffer.length kb > 0
6947 then (
6948 Buffer.add_string bb "\n";
6949 Buffer.add_buffer bb kb;
6951 Buffer.add_string bb "\n</doc>\n";
6952 end;
6956 let pan, conf =
6957 match state.mode with
6958 | Birdseye (c, pan, _, _, _) ->
6959 let beyecolumns =
6960 match conf.columns with
6961 | Cmulti ((c, _, _), _) -> Some c
6962 | Csingle _ -> None
6963 | Csplit _ -> None
6964 and columns =
6965 match c.columns with
6966 | Cmulti (c, _) -> Cmulti (c, [||])
6967 | Csingle _ -> Csingle [||]
6968 | Csplit _ -> failwith "quit from bird's eye while split"
6970 pan, { c with beyecolumns = beyecolumns; columns = columns }
6971 | _ -> state.x, conf
6973 let basename = Filename.basename state.path in
6974 adddoc basename pan (getanchor ())
6975 (let conf =
6976 let autoscrollstep =
6977 match state.autoscroll with
6978 | Some step -> step
6979 | None -> conf.autoscrollstep
6981 match state.mode with
6982 | Birdseye (bc, _, _, _, _) ->
6983 { conf with
6984 zoom = bc.zoom;
6985 presentation = bc.presentation;
6986 interpagespace = bc.interpagespace;
6987 maxwait = bc.maxwait;
6988 autoscrollstep = autoscrollstep }
6989 | _ -> { conf with autoscrollstep = autoscrollstep }
6990 in conf)
6991 (if conf.savebmarks then state.bookmarks else []);
6993 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
6994 if basename <> path
6995 then adddoc path x anchor c bookmarks
6996 ) h;
6997 Buffer.add_string bb "</llppconfig>\n";
6998 true;
7000 if load1 f && Buffer.length bb > 0
7001 then
7003 let tmp = !confpath ^ ".tmp" in
7004 let oc = open_out_bin tmp in
7005 Buffer.output_buffer oc bb;
7006 close_out oc;
7007 Unix.rename tmp !confpath;
7008 with exn ->
7009 prerr_endline
7010 ("error while saving configuration: " ^ exntos exn)
7012 end;;
7014 let adderrmsg src msg =
7015 Buffer.add_string state.errmsgs msg;
7016 state.newerrmsgs <- true;
7017 G.postRedisplay src
7020 let adderrfmt src fmt =
7021 Format.kprintf (fun s -> adderrmsg src s) fmt;
7024 let ract cmds =
7025 let cl = splitatspace cmds in
7026 let scan s fmt f =
7027 try Scanf.sscanf s fmt f
7028 with exn ->
7029 adderrfmt "remote exec"
7030 "error processing '%S': %s\n" cmds (exntos exn)
7032 match cl with
7033 | "reload" :: [] -> reload ()
7034 | "goto" :: args :: [] ->
7035 scan args "%u %f %f"
7036 (fun pageno x y ->
7037 let cmd, _ = state.geomcmds in
7038 if String.length cmd = 0
7039 then gotopagexy pageno x y
7040 else
7041 let f prevf () =
7042 gotopagexy pageno x y;
7043 prevf ()
7045 state.reprf <- f state.reprf
7047 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7048 | "rect" :: args :: [] ->
7049 scan args "%u %u %f %f %f %f"
7050 (fun pageno color x0 y0 x1 y1 ->
7051 onpagerect pageno (fun w h ->
7052 let _,w1,h1,_ = getpagedim pageno in
7053 let sw = float w1 /. w
7054 and sh = float h1 /. h in
7055 let x0s = x0 *. sw
7056 and x1s = x1 *. sw
7057 and y0s = y0 *. sh
7058 and y1s = y1 *. sh in
7059 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7060 debugrect rect;
7061 state.rects <- (pageno, color, rect) :: state.rects;
7062 G.postRedisplay "rect";
7065 | "activatewin" :: [] -> Wsi.activatewin ()
7066 | "quit" :: [] -> raise Quit
7067 | _ ->
7068 adderrfmt "remote command"
7069 "error processing remote command: %S\n" cmds;
7072 let remote =
7073 let scratch = String.create 80 in
7074 let buf = Buffer.create 80 in
7075 fun fd ->
7076 let rec tempfr () =
7077 try Some (Unix.read fd scratch 0 80)
7078 with
7079 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7080 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7081 | exn -> raise exn
7083 match tempfr () with
7084 | None -> Some fd
7085 | Some n ->
7086 if n = 0
7087 then (
7088 Unix.close fd;
7089 if Buffer.length buf > 0
7090 then (
7091 let s = Buffer.contents buf in
7092 Buffer.clear buf;
7093 ract s;
7095 None
7097 else
7098 let rec eat ppos =
7099 let nlpos =
7101 let pos = String.index_from scratch ppos '\n' in
7102 if pos >= n then -1 else pos
7103 with Not_found -> -1
7105 if nlpos >= 0
7106 then (
7107 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7108 let s = Buffer.contents buf in
7109 Buffer.clear buf;
7110 ract s;
7111 eat (nlpos+1);
7113 else (
7114 Buffer.add_substring buf scratch ppos (n-ppos);
7115 Some fd
7117 in eat 0
7120 let remoteopen path =
7121 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7122 with exn ->
7123 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7124 None
7127 let () =
7128 let trimcachepath = ref "" in
7129 let rcmdpath = ref "" in
7130 Arg.parse
7131 (Arg.align
7132 [("-p", Arg.String (fun s -> state.password <- s),
7133 "<password> Set password");
7135 ("-f", Arg.String (fun s -> Config.fontpath := s),
7136 "<path> Set path to the user interface font");
7138 ("-c", Arg.String (fun s -> Config.confpath := s),
7139 "<path> Set path to the configuration file");
7141 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7142 "<path> Set path to the trim cache file");
7144 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7145 "<named-destination> Set named destination");
7147 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7149 ("-remote", Arg.String (fun s -> rcmdpath := s),
7150 "<path> Set path to the remote commands source");
7152 ("-v", Arg.Unit (fun () ->
7153 Printf.printf
7154 "%s\nconfiguration path: %s\n"
7155 (version ())
7156 Config.defconfpath
7158 exit 0), " Print version and exit");
7161 (fun s -> state.path <- s)
7162 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7164 if String.length state.path = 0
7165 then (prerr_endline "file name missing"; exit 1);
7167 if not (Config.load ())
7168 then prerr_endline "failed to load configuration";
7170 let globalkeyhash = findkeyhash conf "global" in
7171 let wsfd, winw, winh = Wsi.init (object
7172 method expose =
7173 state.wthack <- false;
7174 if nogeomcmds state.geomcmds || platform == Posx
7175 then display ()
7176 else (
7177 GlClear.color (scalecolor2 conf.bgcolor);
7178 GlClear.clear [`color];
7180 method display = display ()
7181 method reshape w h = reshape w h
7182 method mouse b d x y m = mouse b d x y m
7183 method motion x y = state.mpos <- (x, y); motion x y
7184 method pmotion x y = state.mpos <- (x, y); pmotion x y
7185 method key k m =
7186 let mascm = m land (
7187 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7188 ) in
7189 match state.keystate with
7190 | KSnone ->
7191 let km = k, mascm in
7192 begin
7193 match
7194 let modehash = state.uioh#modehash in
7195 try Hashtbl.find modehash km
7196 with Not_found ->
7197 try Hashtbl.find globalkeyhash km
7198 with Not_found -> KMinsrt (k, m)
7199 with
7200 | KMinsrt (k, m) -> keyboard k m
7201 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7202 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7204 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7205 List.iter (fun (k, m) -> keyboard k m) insrt;
7206 state.keystate <- KSnone
7207 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7208 state.keystate <- KSinto (keys, insrt)
7209 | _ ->
7210 state.keystate <- KSnone
7212 method enter x y = state.mpos <- (x, y); pmotion x y
7213 method leave = state.mpos <- (-1, -1)
7214 method winstate wsl = state.winstate <- wsl
7215 method quit = raise Quit
7216 end) conf.cwinw conf.cwinh (platform = Posx) in
7218 state.wsfd <- wsfd;
7220 if not (
7221 List.exists GlMisc.check_extension
7222 [ "GL_ARB_texture_rectangle"
7223 ; "GL_EXT_texture_recangle"
7224 ; "GL_NV_texture_rectangle" ]
7226 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7228 let cr, sw =
7229 match Ne.pipe () with
7230 | Ne.Exn exn ->
7231 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7232 exit 1
7233 | Ne.Res rw -> rw
7234 and sr, cw =
7235 match Ne.pipe () with
7236 | Ne.Exn exn ->
7237 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7238 exit 1
7239 | Ne.Res rw -> rw
7242 cloexec cr;
7243 cloexec sw;
7244 cloexec sr;
7245 cloexec cw;
7247 setcheckers conf.checkers;
7248 redirectstderr ();
7250 init (cr, cw) (
7251 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
7252 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7253 !Config.fontpath, !trimcachepath,
7254 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7256 state.sr <- sr;
7257 state.sw <- sw;
7258 state.text <- "Opening " ^ (mbtoutf8 state.path);
7259 reshape winw winh;
7260 opendoc state.path state.password;
7261 state.uioh <- uioh;
7263 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7264 let optrfd =
7265 ref (
7266 if String.length !rcmdpath > 0
7267 then remoteopen !rcmdpath
7268 else None
7272 let rec loop deadline =
7273 let r =
7274 match state.errfd with
7275 | None -> [state.sr; state.wsfd]
7276 | Some fd -> [state.sr; state.wsfd; fd]
7278 let r =
7279 match !optrfd with
7280 | None -> r
7281 | Some fd -> fd :: r
7283 if state.redisplay && not state.wthack
7284 then (
7285 state.redisplay <- false;
7286 display ();
7288 let timeout =
7289 let now = now () in
7290 if deadline > now
7291 then (
7292 if deadline = infinity
7293 then ~-.1.0
7294 else max 0.0 (deadline -. now)
7296 else 0.0
7298 let r, _, _ =
7299 try Unix.select r [] [] timeout
7300 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7302 begin match r with
7303 | [] ->
7304 state.ghyll None;
7305 let newdeadline =
7306 if state.ghyll == noghyll
7307 then
7308 match state.autoscroll with
7309 | Some step when step != 0 ->
7310 let y = state.y + step in
7311 let y =
7312 if y < 0
7313 then state.maxy
7314 else if y >= state.maxy then 0 else y
7316 gotoy y;
7317 if state.mode = View
7318 then state.text <- "";
7319 deadline +. 0.01
7320 | _ -> infinity
7321 else deadline +. 0.01
7323 loop newdeadline
7325 | l ->
7326 let rec checkfds = function
7327 | [] -> ()
7328 | fd :: rest when fd = state.sr ->
7329 let cmd = readcmd state.sr in
7330 act cmd;
7331 checkfds rest
7333 | fd :: rest when fd = state.wsfd ->
7334 Wsi.readresp fd;
7335 checkfds rest
7337 | fd :: rest when Some fd = !optrfd ->
7338 begin match remote fd with
7339 | None -> optrfd := remoteopen !rcmdpath;
7340 | opt -> optrfd := opt
7341 end;
7342 checkfds rest
7344 | fd :: rest ->
7345 let s = String.create 80 in
7346 let n = tempfailureretry (Unix.read fd s 0) 80 in
7347 if conf.redirectstderr
7348 then (
7349 Buffer.add_substring state.errmsgs s 0 n;
7350 state.newerrmsgs <- true;
7351 state.redisplay <- true;
7353 else (
7354 prerr_string (String.sub s 0 n);
7355 flush stderr;
7357 checkfds rest
7359 checkfds l;
7360 let newdeadline =
7361 let deadline1 =
7362 if deadline = infinity
7363 then now () +. 0.01
7364 else deadline
7366 match state.autoscroll with
7367 | Some step when step != 0 -> deadline1
7368 | _ -> if state.ghyll == noghyll then infinity else deadline1
7370 loop newdeadline
7371 end;
7374 loop infinity;
7375 with Quit ->
7376 Config.save ();