Hard to explain...
[llpp.git] / main.ml
blob691583b65df5cb84637036a91937eaff184b1ed5
1 exception Quit;;
3 type under =
4 | Unone
5 | Ulinkuri of string
6 | Ulinkgoto of (int * int)
7 | Utext of facename
8 | Uunexpected of string
9 | Ulaunch of string
10 | Unamed of string
11 | Uremote of (string * int)
12 and facename = string;;
14 let dolog fmt = Printf.kprintf prerr_endline fmt;;
15 let now = Unix.gettimeofday;;
17 type params = (angle * proportional * trimparams
18 * texcount * sliceheight * memsize
19 * colorspace * fontpath * trimcachepath)
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
45 type link =
46 | Lnotfound
47 | Lfound of int
48 and linkdir =
49 | LDfirst
50 | LDlast
51 | LDfirstvisible of (int * int * int)
52 | LDleft of int
53 | LDright of int
54 | LDdown of int
55 | LDup of int
58 type pagewithlinks =
59 | Pwlnotfound
60 | Pwl of int
63 type keymap =
64 | KMinsrt of key
65 | KMinsrl of key list
66 | KMmulti of key list * key list
67 and key = int * int
68 and keyhash = (key, keymap) Hashtbl.t
69 and keystate =
70 | KSnone
71 | KSinto of (key list * key list)
74 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
75 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
77 type pipe = (Unix.file_descr * Unix.file_descr);;
79 external init : pipe -> params -> unit = "ml_init";;
80 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
81 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
82 external getpdimrect : int -> float array = "ml_getpdimrect";;
83 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
84 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
85 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
86 external measurestr : int -> string -> float = "ml_measure_string";;
87 external getmaxw : unit -> float = "ml_getmaxw";;
88 external postprocess :
89 opaque -> int -> int -> int -> (int * string * int) -> int = "ml_postprocess";;
90 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
91 external platform : unit -> platform = "ml_platform";;
92 external setaalevel : int -> unit = "ml_setaalevel";;
93 external realloctexts : int -> bool = "ml_realloctexts";;
94 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
95 external findlink : opaque -> linkdir -> link = "ml_findlink";;
96 external getlink : opaque -> int -> under = "ml_getlink";;
97 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
98 external getlinkcount : opaque -> int = "ml_getlinkcount";;
99 external findpwl: int -> int -> pagewithlinks = "ml_find_page_with_links"
100 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
102 let platform_to_string = function
103 | Punknown -> "unknown"
104 | Plinux -> "Linux"
105 | Posx -> "OSX"
106 | Psun -> "Sun"
107 | Pfreebsd -> "FreeBSD"
108 | Pdragonflybsd -> "DragonflyBSD"
109 | Popenbsd -> "OpenBSD"
110 | Pnetbsd -> "NetBSD"
111 | Pcygwin -> "Cygwin"
114 let platform = platform ();;
116 let popen cmd fda =
117 if platform = Pcygwin
118 then (
119 let sh = "/bin/sh" in
120 let args = [|sh; "-c"; cmd|] in
121 let rec std si so se = function
122 | [] -> si, so, se
123 | (fd, 0) :: rest -> std fd so se rest
124 | (fd, -1) :: rest ->
125 Unix.set_close_on_exec fd;
126 std si so se rest
127 | (_, n) :: _ ->
128 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
130 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
131 ignore (Unix.create_process sh args si so se)
133 else popen cmd fda;
136 type x = int
137 and y = int
138 and tilex = int
139 and tiley = int
140 and tileparams = (x * y * width * height * tilex * tiley)
143 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
145 type mpos = int * int
146 and mstate =
147 | Msel of (mpos * mpos)
148 | Mpan of mpos
149 | Mscrolly | Mscrollx
150 | Mzoom of (int * int)
151 | Mzoomrect of (mpos * mpos)
152 | Mnone
155 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
156 and onkey = string -> int -> te
157 and ondone = string -> unit
158 and histcancel = unit -> unit
159 and onhist = ((histcmd -> string) * histcancel)
160 and histcmd = HCnext | HCprev | HCfirst | HClast
161 and cancelonempty = bool
162 and te =
163 | TEstop
164 | TEdone of string
165 | TEcont of string
166 | TEswitch of textentry
169 type 'a circbuf =
170 { store : 'a array
171 ; mutable rc : int
172 ; mutable wc : int
173 ; mutable len : int
177 let bound v minv maxv =
178 max minv (min maxv v);
181 let cbnew n v =
182 { store = Array.create n v
183 ; rc = 0
184 ; wc = 0
185 ; len = 0
189 let drawstring size x y s =
190 Gl.enable `blend;
191 Gl.enable `texture_2d;
192 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
193 ignore (drawstr size x y s);
194 Gl.disable `blend;
195 Gl.disable `texture_2d;
198 let drawstring1 size x y s =
199 drawstr size x y s;
202 let drawstring2 size x y fmt =
203 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
206 let cbcap b = Array.length b.store;;
208 let cbput b v =
209 let cap = cbcap b in
210 b.store.(b.wc) <- v;
211 b.wc <- (b.wc + 1) mod cap;
212 b.rc <- b.wc;
213 b.len <- min (b.len + 1) cap;
216 let cbempty b = b.len = 0;;
218 let cbgetg b circular dir =
219 if cbempty b
220 then b.store.(0)
221 else
222 let rc = b.rc + dir in
223 let rc =
224 if circular
225 then (
226 if rc = -1
227 then b.len-1
228 else (
229 if rc = b.len
230 then 0
231 else rc
234 else max 0 (min rc (b.len-1))
236 b.rc <- rc;
237 b.store.(rc);
240 let cbget b = cbgetg b false;;
241 let cbgetc b = cbgetg b true;;
243 type page =
244 { pageno : int
245 ; pagedimno : int
246 ; pagew : int
247 ; pageh : int
248 ; pagex : int
249 ; pagey : int
250 ; pagevw : int
251 ; pagevh : int
252 ; pagedispx : int
253 ; pagedispy : int
254 ; pagecol : int
258 let debugl l =
259 dolog "l %d dim=%d {" l.pageno l.pagedimno;
260 dolog " WxH %dx%d" l.pagew l.pageh;
261 dolog " vWxH %dx%d" l.pagevw l.pagevh;
262 dolog " pagex,y %d,%d" l.pagex l.pagey;
263 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
264 dolog " column %d" l.pagecol;
265 dolog "}";
268 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
269 dolog "rect {";
270 dolog " x0,y0=(% f, % f)" x0 y0;
271 dolog " x1,y1=(% f, % f)" x1 y1;
272 dolog " x2,y2=(% f, % f)" x2 y2;
273 dolog " x3,y3=(% f, % f)" x3 y3;
274 dolog "}";
277 type multicolumns = multicol * pagegeom
278 and singlecolumn = pagegeom
279 and splitcolumns = columncount * pagegeom
280 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
281 and multicol = columncount * covercount * covercount
282 and pdimno = int
283 and columncount = int
284 and covercount = int;;
286 type conf =
287 { mutable scrollbw : int
288 ; mutable scrollh : int
289 ; mutable icase : bool
290 ; mutable preload : bool
291 ; mutable pagebias : int
292 ; mutable verbose : bool
293 ; mutable debug : bool
294 ; mutable scrollstep : int
295 ; mutable hscrollstep : int
296 ; mutable maxhfit : bool
297 ; mutable crophack : bool
298 ; mutable autoscrollstep : int
299 ; mutable maxwait : float option
300 ; mutable hlinks : bool
301 ; mutable underinfo : bool
302 ; mutable interpagespace : interpagespace
303 ; mutable zoom : float
304 ; mutable presentation : bool
305 ; mutable angle : angle
306 ; mutable winw : int
307 ; mutable winh : int
308 ; mutable savebmarks : bool
309 ; mutable proportional : proportional
310 ; mutable trimmargins : trimmargins
311 ; mutable trimfuzz : irect
312 ; mutable memlimit : memsize
313 ; mutable texcount : texcount
314 ; mutable sliceheight : sliceheight
315 ; mutable thumbw : width
316 ; mutable jumpback : bool
317 ; mutable bgcolor : float * float * float
318 ; mutable bedefault : bool
319 ; mutable scrollbarinpm : bool
320 ; mutable tilew : int
321 ; mutable tileh : int
322 ; mutable mustoresize : memsize
323 ; mutable checkers : bool
324 ; mutable aalevel : int
325 ; mutable urilauncher : string
326 ; mutable pathlauncher : string
327 ; mutable colorspace : colorspace
328 ; mutable invert : bool
329 ; mutable colorscale : float
330 ; mutable redirectstderr : bool
331 ; mutable ghyllscroll : (int * int * int) option
332 ; mutable columns : columns
333 ; mutable beyecolumns : columncount option
334 ; mutable selcmd : string
335 ; mutable updatecurs : bool
336 ; mutable keyhashes : (string * keyhash) list
337 ; mutable hfsize : int
338 ; mutable pgscale : float
339 ; mutable multicenter : bool
341 and columns =
342 | Csingle of singlecolumn
343 | Cmulti of multicolumns
344 | Csplit of splitcolumns
347 type anchor = pageno * top * dtop;;
349 type outline = string * int * anchor;;
351 type rect = float * float * float * float * float * float * float * float;;
353 type tile = opaque * pixmapsize * elapsed
354 and elapsed = float;;
355 type pagemapkey = pageno * gen;;
356 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
357 and row = int
358 and col = int;;
360 let emptyanchor = (0, 0.0, 0.0);;
362 type infochange = | Memused | Docinfo | Pdim;;
364 class type uioh = object
365 method display : unit
366 method key : int -> int -> uioh
367 method button : int -> bool -> int -> int -> int -> uioh
368 method motion : int -> int -> uioh
369 method pmotion : int -> int -> uioh
370 method infochanged : infochange -> unit
371 method scrollpw : (int * float * float)
372 method scrollph : (int * float * float)
373 method modehash : keyhash
374 end;;
376 type mode =
377 | Birdseye of (conf * leftx * pageno * pageno * anchor)
378 | Textentry of (textentry * onleave)
379 | View
380 | LinkNav of linktarget
381 and onleave = leavetextentrystatus -> unit
382 and leavetextentrystatus = | Cancel | Confirm
383 and helpitem = string * int * action
384 and action =
385 | Noaction
386 | Action of (uioh -> uioh)
387 and linktarget =
388 | Ltexact of (pageno * int)
389 | Ltgendir of int
392 let isbirdseye = function Birdseye _ -> true | _ -> false;;
393 let istextentry = function Textentry _ -> true | _ -> false;;
395 type currently =
396 | Idle
397 | Loading of (page * gen)
398 | Tiling of (
399 page * opaque * colorspace * angle * gen * col * row * width * height
401 | Outlining of outline list
404 let emptykeyhash = Hashtbl.create 0;;
405 let nouioh : uioh = object (self)
406 method display = ()
407 method key _ _ = self
408 method button _ _ _ _ _ = self
409 method motion _ _ = self
410 method pmotion _ _ = self
411 method infochanged _ = ()
412 method scrollpw = (0, nan, nan)
413 method scrollph = (0, nan, nan)
414 method modehash = emptykeyhash
415 end;;
417 type state =
418 { mutable sr : Unix.file_descr
419 ; mutable sw : Unix.file_descr
420 ; mutable wsfd : Unix.file_descr
421 ; mutable errfd : Unix.file_descr option
422 ; mutable stderr : Unix.file_descr
423 ; mutable errmsgs : Buffer.t
424 ; mutable newerrmsgs : bool
425 ; mutable w : int
426 ; mutable x : int
427 ; mutable y : int
428 ; mutable scrollw : int
429 ; mutable hscrollh : int
430 ; mutable anchor : anchor
431 ; mutable ranchors : (string * string * anchor) list
432 ; mutable maxy : int
433 ; mutable layout : page list
434 ; pagemap : (pagemapkey, opaque) Hashtbl.t
435 ; tilemap : (tilemapkey, tile) Hashtbl.t
436 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
437 ; mutable pdims : (pageno * width * height * leftx) list
438 ; mutable pagecount : int
439 ; mutable currently : currently
440 ; mutable mstate : mstate
441 ; mutable searchpattern : string
442 ; mutable rects : (pageno * recttype * rect) list
443 ; mutable rects1 : (pageno * recttype * rect) list
444 ; mutable text : string
445 ; mutable fullscreen : (width * height) option
446 ; mutable mode : mode
447 ; mutable uioh : uioh
448 ; mutable outlines : outline array
449 ; mutable bookmarks : outline list
450 ; mutable path : string
451 ; mutable password : string
452 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
453 ; mutable memused : memsize
454 ; mutable gen : gen
455 ; mutable throttle : (page list * int * float) option
456 ; mutable autoscroll : int option
457 ; mutable ghyll : (int option -> unit)
458 ; mutable help : helpitem array
459 ; mutable docinfo : (int * string) list
460 ; mutable texid : GlTex.texture_id option
461 ; hists : hists
462 ; mutable prevzoom : float
463 ; mutable progress : float
464 ; mutable redisplay : bool
465 ; mutable mpos : mpos
466 ; mutable keystate : keystate
467 ; mutable glinks : bool
468 ; mutable prevcolumns : (columns * float) option
470 and hists =
471 { pat : string circbuf
472 ; pag : string circbuf
473 ; nav : anchor circbuf
474 ; sel : string circbuf
478 let defconf =
479 { scrollbw = 7
480 ; scrollh = 12
481 ; icase = true
482 ; preload = true
483 ; pagebias = 0
484 ; verbose = false
485 ; debug = false
486 ; scrollstep = 24
487 ; hscrollstep = 24
488 ; maxhfit = true
489 ; crophack = false
490 ; autoscrollstep = 2
491 ; maxwait = None
492 ; hlinks = false
493 ; underinfo = false
494 ; interpagespace = 2
495 ; zoom = 1.0
496 ; presentation = false
497 ; angle = 0
498 ; winw = 900
499 ; winh = 900
500 ; savebmarks = true
501 ; proportional = true
502 ; trimmargins = false
503 ; trimfuzz = (0,0,0,0)
504 ; memlimit = 32 lsl 20
505 ; texcount = 256
506 ; sliceheight = 24
507 ; thumbw = 76
508 ; jumpback = true
509 ; bgcolor = (0.5, 0.5, 0.5)
510 ; bedefault = false
511 ; scrollbarinpm = true
512 ; tilew = 2048
513 ; tileh = 2048
514 ; mustoresize = 256 lsl 20
515 ; checkers = true
516 ; aalevel = 8
517 ; urilauncher =
518 (match platform with
519 | Plinux | Pfreebsd | Pdragonflybsd
520 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
521 | Posx -> "open \"%s\""
522 | Pcygwin -> "cygstart \"%s\""
523 | Punknown -> "echo %s")
524 ; pathlauncher = "lp \"%s\""
525 ; selcmd =
526 (match platform with
527 | Plinux | Pfreebsd | Pdragonflybsd
528 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
529 | Posx -> "pbcopy"
530 | Pcygwin -> "wsel"
531 | Punknown -> "cat")
532 ; colorspace = Rgb
533 ; invert = false
534 ; colorscale = 1.0
535 ; redirectstderr = false
536 ; ghyllscroll = None
537 ; columns = Csingle [||]
538 ; beyecolumns = None
539 ; updatecurs = false
540 ; hfsize = 12
541 ; pgscale = 1.0
542 ; multicenter = false
543 ; keyhashes =
544 let mk n = (n, Hashtbl.create 1) in
545 [ mk "global"
546 ; mk "info"
547 ; mk "help"
548 ; mk "outline"
549 ; mk "listview"
550 ; mk "birdseye"
551 ; mk "textentry"
552 ; mk "links"
553 ; mk "view"
558 let findkeyhash c name =
559 try List.assoc name c.keyhashes
560 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
563 let conf = { defconf with angle = defconf.angle };;
565 let pgscale h = truncate (float h *. conf.pgscale);;
567 type fontstate =
568 { mutable fontsize : int
569 ; mutable wwidth : float
570 ; mutable maxrows : int
574 let fstate =
575 { fontsize = 14
576 ; wwidth = nan
577 ; maxrows = -1
581 let setfontsize n =
582 fstate.fontsize <- n;
583 fstate.wwidth <- measurestr fstate.fontsize "w";
584 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
587 let geturl s =
588 let colonpos = try String.index s ':' with Not_found -> -1 in
589 let len = String.length s in
590 if colonpos >= 0 && colonpos + 3 < len
591 then (
592 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
593 then
594 let schemestartpos =
595 try String.rindex_from s colonpos ' '
596 with Not_found -> -1
598 let scheme =
599 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
601 match scheme with
602 | "http" | "ftp" | "mailto" ->
603 let epos =
604 try String.index_from s colonpos ' '
605 with Not_found -> len
607 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
608 | _ -> ""
609 else ""
611 else ""
614 let gotouri uri =
615 if String.length conf.urilauncher = 0
616 then print_endline uri
617 else (
618 let url = geturl uri in
619 if String.length url = 0
620 then print_endline uri
621 else
622 let re = Str.regexp "%s" in
623 let command = Str.global_replace re url conf.urilauncher in
624 try popen command []
625 with exn ->
626 Printf.eprintf
627 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
628 flush stderr;
632 let version () =
633 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
634 (platform_to_string platform) Sys.word_size Sys.ocaml_version
637 let makehelp () =
638 let strings = version () :: "" :: Help.keys in
639 Array.of_list (
640 List.map (fun s ->
641 let url = geturl s in
642 if String.length url > 0
643 then (s, 0, Action (fun u -> gotouri url; u))
644 else (s, 0, Noaction)
645 ) strings);
648 let noghyll _ = ();;
649 let firstgeomcmds = "", [];;
651 let state =
652 { sr = Unix.stdin
653 ; sw = Unix.stdin
654 ; wsfd = Unix.stdin
655 ; errfd = None
656 ; stderr = Unix.stderr
657 ; errmsgs = Buffer.create 0
658 ; newerrmsgs = false
659 ; x = 0
660 ; y = 0
661 ; w = 0
662 ; scrollw = 0
663 ; hscrollh = 0
664 ; anchor = emptyanchor
665 ; ranchors = []
666 ; layout = []
667 ; maxy = max_int
668 ; tilelru = Queue.create ()
669 ; pagemap = Hashtbl.create 10
670 ; tilemap = Hashtbl.create 10
671 ; pdims = []
672 ; pagecount = 0
673 ; currently = Idle
674 ; mstate = Mnone
675 ; rects = []
676 ; rects1 = []
677 ; text = ""
678 ; mode = View
679 ; fullscreen = None
680 ; searchpattern = ""
681 ; outlines = [||]
682 ; bookmarks = []
683 ; path = ""
684 ; password = ""
685 ; geomcmds = firstgeomcmds
686 ; hists =
687 { nav = cbnew 10 emptyanchor
688 ; pat = cbnew 10 ""
689 ; pag = cbnew 10 ""
690 ; sel = cbnew 10 ""
692 ; memused = 0
693 ; gen = 0
694 ; throttle = None
695 ; autoscroll = None
696 ; ghyll = noghyll
697 ; help = makehelp ()
698 ; docinfo = []
699 ; texid = None
700 ; prevzoom = 1.0
701 ; progress = -1.0
702 ; uioh = nouioh
703 ; redisplay = true
704 ; mpos = (-1, -1)
705 ; keystate = KSnone
706 ; glinks = false
707 ; prevcolumns = None
711 let vlog fmt =
712 if conf.verbose
713 then
714 Printf.kprintf prerr_endline fmt
715 else
716 Printf.kprintf ignore fmt
719 let launchpath () =
720 if String.length conf.pathlauncher = 0
721 then print_endline state.path
722 else (
723 let re = Str.regexp "%s" in
724 let command = Str.global_replace re state.path conf.pathlauncher in
725 try popen command []
726 with exn ->
727 Printf.eprintf
728 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
729 flush stderr;
733 module Ne = struct
734 type 'a t = | Res of 'a | Exn of exn;;
736 let pipe () =
737 try Res (Unix.pipe ())
738 with exn -> Exn exn
741 let clo fd f =
742 try Unix.close fd
743 with exn -> f (Printexc.to_string exn)
746 let dup fd =
747 try Res (Unix.dup fd)
748 with exn -> Exn exn
751 let dup2 fd1 fd2 =
752 try Res (Unix.dup2 fd1 fd2)
753 with exn -> Exn exn
755 end;;
757 let redirectstderr () =
758 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
759 if conf.redirectstderr
760 then
761 match Ne.pipe () with
762 | Ne.Exn exn ->
763 dolog "failed to create stderr redirection pipes: %s"
764 (Printexc.to_string exn)
766 | Ne.Res (r, w) ->
767 begin match Ne.dup Unix.stderr with
768 | Ne.Exn exn ->
769 dolog "failed to dup stderr: %s" (Printexc.to_string exn);
770 Ne.clo r (clofail "pipe/r");
771 Ne.clo w (clofail "pipe/w");
773 | Ne.Res dupstderr ->
774 begin match Ne.dup2 w Unix.stderr with
775 | Ne.Exn exn ->
776 dolog "failed to dup2 to stderr: %s"
777 (Printexc.to_string exn);
778 Ne.clo dupstderr (clofail "stderr duplicate");
779 Ne.clo r (clofail "redir pipe/r");
780 Ne.clo w (clofail "redir pipe/w");
782 | Ne.Res () ->
783 state.stderr <- dupstderr;
784 state.errfd <- Some r;
785 end;
787 else (
788 state.newerrmsgs <- false;
789 begin match state.errfd with
790 | Some fd ->
791 begin match Ne.dup2 state.stderr Unix.stderr with
792 | Ne.Exn exn ->
793 dolog "failed to dup2 original stderr: %s"
794 (Printexc.to_string exn)
795 | Ne.Res () ->
796 Ne.clo fd (clofail "dup of stderr");
797 Unix.dup2 state.stderr Unix.stderr;
798 state.errfd <- None;
799 end;
800 | None -> ()
801 end;
802 prerr_string (Buffer.contents state.errmsgs);
803 flush stderr;
804 Buffer.clear state.errmsgs;
808 module G =
809 struct
810 let postRedisplay who =
811 if conf.verbose
812 then prerr_endline ("redisplay for " ^ who);
813 state.redisplay <- true;
815 end;;
817 let getopaque pageno =
818 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
819 with Not_found -> None
822 let putopaque pageno opaque =
823 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
826 let pagetranslatepoint l x y =
827 let dy = y - l.pagedispy in
828 let y = dy + l.pagey in
829 let dx = x - l.pagedispx in
830 let x = dx + l.pagex in
831 (x, y);
834 let getunder x y =
835 let rec f = function
836 | l :: rest ->
837 begin match getopaque l.pageno with
838 | Some opaque ->
839 let x0 = l.pagedispx in
840 let x1 = x0 + l.pagevw in
841 let y0 = l.pagedispy in
842 let y1 = y0 + l.pagevh in
843 if y >= y0 && y <= y1 && x >= x0 && x <= x1
844 then
845 let px, py = pagetranslatepoint l x y in
846 match whatsunder opaque px py with
847 | Unone -> f rest
848 | under -> under
849 else f rest
850 | _ ->
851 f rest
853 | [] -> Unone
855 f state.layout
858 let showtext c s =
859 state.text <- Printf.sprintf "%c%s" c s;
860 G.postRedisplay "showtext";
863 let undertext = function
864 | Unone -> "none"
865 | Ulinkuri s -> s
866 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
867 | Utext s -> "font: " ^ s
868 | Uunexpected s -> "unexpected: " ^ s
869 | Ulaunch s -> "launch: " ^ s
870 | Unamed s -> "named: " ^ s
871 | Uremote (filename, pageno) ->
872 Printf.sprintf "%s: page %d" filename (pageno+1)
875 let updateunder x y =
876 match getunder x y with
877 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
878 | Ulinkuri uri ->
879 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
880 Wsi.setcursor Wsi.CURSOR_INFO
881 | Ulinkgoto (pageno, _) ->
882 if conf.underinfo
883 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
884 Wsi.setcursor Wsi.CURSOR_INFO
885 | Utext s ->
886 if conf.underinfo then showtext 'f' ("ont: " ^ s);
887 Wsi.setcursor Wsi.CURSOR_TEXT
888 | Uunexpected s ->
889 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
890 Wsi.setcursor Wsi.CURSOR_INHERIT
891 | Ulaunch s ->
892 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
893 Wsi.setcursor Wsi.CURSOR_INHERIT
894 | Unamed s ->
895 if conf.underinfo then showtext 'n' ("amed: " ^ s);
896 Wsi.setcursor Wsi.CURSOR_INHERIT
897 | Uremote (filename, pageno) ->
898 if conf.underinfo then showtext 'r'
899 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
900 Wsi.setcursor Wsi.CURSOR_INFO
903 let showlinktype under =
904 if conf.underinfo
905 then
906 match under with
907 | Unone -> ()
908 | under ->
909 let s = undertext under in
910 showtext ' ' s
913 let addchar s c =
914 let b = Buffer.create (String.length s + 1) in
915 Buffer.add_string b s;
916 Buffer.add_char b c;
917 Buffer.contents b;
920 let colorspace_of_string s =
921 match String.lowercase s with
922 | "rgb" -> Rgb
923 | "bgr" -> Bgr
924 | "gray" -> Gray
925 | _ -> failwith "invalid colorspace"
928 let int_of_colorspace = function
929 | Rgb -> 0
930 | Bgr -> 1
931 | Gray -> 2
934 let colorspace_of_int = function
935 | 0 -> Rgb
936 | 1 -> Bgr
937 | 2 -> Gray
938 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
941 let colorspace_to_string = function
942 | Rgb -> "rgb"
943 | Bgr -> "bgr"
944 | Gray -> "gray"
947 let intentry_with_suffix text key =
948 let c =
949 if key >= 32 && key < 127
950 then Char.chr key
951 else '\000'
953 match Char.lowercase c with
954 | '0' .. '9' ->
955 let text = addchar text c in
956 TEcont text
958 | 'k' | 'm' | 'g' ->
959 let text = addchar text c in
960 TEcont text
962 | _ ->
963 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
964 TEcont text
967 let multicolumns_to_string (n, a, b) =
968 if a = 0 && b = 0
969 then Printf.sprintf "%d" n
970 else Printf.sprintf "%d,%d,%d" n a b;
973 let multicolumns_of_string s =
975 (int_of_string s, 0, 0)
976 with _ ->
977 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
980 let readcmd fd =
981 let s = "xxxx" in
982 let n = Unix.read fd s 0 4 in
983 if n != 4 then failwith "incomplete read(len)";
984 let len = 0
985 lor (Char.code s.[0] lsl 24)
986 lor (Char.code s.[1] lsl 16)
987 lor (Char.code s.[2] lsl 8)
988 lor (Char.code s.[3] lsl 0)
990 let s = String.create len in
991 let n = Unix.read fd s 0 len in
992 if n != len then failwith "incomplete read(data)";
996 let btod b = if b then 1 else 0;;
998 let wcmd fmt =
999 let b = Buffer.create 16 in
1000 Buffer.add_string b "llll";
1001 Printf.kbprintf
1002 (fun b ->
1003 let s = Buffer.contents b in
1004 let n = String.length s in
1005 let len = n - 4 in
1006 (* dolog "wcmd %S" (String.sub s 4 len); *)
1007 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1008 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1009 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1010 s.[3] <- Char.chr (len land 0xff);
1011 let n' = Unix.write state.sw s 0 n in
1012 if n' != n then failwith "write failed";
1013 ) b fmt;
1016 let calcips h =
1017 if conf.presentation
1018 then
1019 let d = conf.winh - h in
1020 max conf.interpagespace ((d + 1) / 2)
1021 else
1022 conf.interpagespace
1025 let calcheight () =
1026 match conf.columns with
1027 | Cmulti ((c, _, _), b) ->
1028 let rec loop y h n =
1029 if n < 0
1030 then loop y h (n+1)
1031 else (
1032 if n = Array.length b
1033 then y + h
1034 else
1035 let (_, _, y', (_, _, h', _)) = b.(n) in
1036 let y = min y y'
1037 and h = max h h' in
1038 loop y h (n+1)
1041 loop max_int 0 (((Array.length b - 1) / c) * c)
1042 | Csingle b ->
1043 if Array.length b > 0
1044 then
1045 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1046 y + h + (if conf.presentation then calcips h else 0)
1047 else 0
1048 | Csplit (_, b) ->
1049 if Array.length b > 0
1050 then
1051 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1052 y + h
1053 else 0
1056 let getpageyh pageno =
1057 let pageno = bound pageno 0 (state.pagecount-1) in
1058 match conf.columns with
1059 | Csingle b ->
1060 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1061 let y =
1062 if conf.presentation
1063 then y - calcips h
1064 else y
1066 y, h
1067 | Cmulti (_, b) ->
1068 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1069 y, h
1070 | Csplit (c, b) ->
1071 let n = pageno*c in
1072 let (_, _, y, (_, _, h, _)) = b.(n) in
1073 y, h
1076 let getpagedim pageno =
1077 let rec f ppdim l =
1078 match l with
1079 | (n, _, _, _) as pdim :: rest ->
1080 if n >= pageno
1081 then (if n = pageno then pdim else ppdim)
1082 else f pdim rest
1084 | [] -> ppdim
1086 f (-1, -1, -1, -1) state.pdims
1089 let getpagey pageno = fst (getpageyh pageno);;
1091 let nogeomcmds cmds =
1092 match cmds with
1093 | s, [] -> String.length s = 0
1094 | _ -> false
1097 let layoutN ((columns, coverA, coverB), b) y sh =
1098 let sh = sh - state.hscrollh in
1099 let rec fold accu n =
1100 if n = Array.length b
1101 then accu
1102 else
1103 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1104 if (vy - y) > sh &&
1105 (n = coverA - 1
1106 || n = state.pagecount - coverB
1107 || (n - coverA) mod columns = columns - 1)
1108 then accu
1109 else
1110 let accu =
1111 if vy + h > y
1112 then
1113 let pagey = max 0 (y - vy) in
1114 let pagedispy = if pagey > 0 then 0 else vy - y in
1115 let pagedispx, pagex =
1116 let pdx =
1117 if n = coverA - 1 || n = state.pagecount - coverB
1118 then state.x + (conf.winw - state.scrollw - w) / 2
1119 else dx + xoff + state.x
1121 if pdx < 0
1122 then 0, -pdx
1123 else pdx, 0
1125 let pagevw =
1126 let vw = conf.winw - state.scrollw - pagedispx in
1127 let pw = w - pagex in
1128 min vw pw
1130 let pagevh = min (h - pagey) (sh - pagedispy) in
1131 if pagevw > 0 && pagevh > 0
1132 then
1133 let e =
1134 { pageno = n
1135 ; pagedimno = pdimno
1136 ; pagew = w
1137 ; pageh = h
1138 ; pagex = pagex
1139 ; pagey = pagey
1140 ; pagevw = pagevw
1141 ; pagevh = pagevh
1142 ; pagedispx = pagedispx
1143 ; pagedispy = pagedispy
1144 ; pagecol = 0
1147 e :: accu
1148 else
1149 accu
1150 else
1151 accu
1153 fold accu (n+1)
1155 List.rev (fold [] 0)
1158 let layoutS (columns, b) y sh =
1159 let sh = sh - state.hscrollh in
1160 let rec fold accu n =
1161 if n = Array.length b
1162 then accu
1163 else
1164 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1165 if (vy - y) > sh
1166 then accu
1167 else
1168 let accu =
1169 if vy + pageh > y
1170 then
1171 let x = xoff + state.x in
1172 let pagey = max 0 (y - vy) in
1173 let pagedispy = if pagey > 0 then 0 else vy - y in
1174 let pagedispx, pagex =
1175 if px = 0
1176 then (
1177 if x < 0
1178 then 0, -x
1179 else x, 0
1181 else (
1182 let px = px - x in
1183 if px < 0
1184 then -px, 0
1185 else 0, px
1188 let pagecolw = pagew/columns in
1189 let pagedispx =
1190 if pagecolw < conf.winw
1191 then pagedispx + ((conf.winw - state.scrollw - pagecolw) / 2)
1192 else pagedispx
1194 let pagevw =
1195 let vw = conf.winw - pagedispx - state.scrollw in
1196 let pw = pagew - pagex in
1197 min vw pw
1199 let pagevw = min pagevw pagecolw in
1200 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1201 if pagevw > 0 && pagevh > 0
1202 then
1203 let e =
1204 { pageno = n/columns
1205 ; pagedimno = pdimno
1206 ; pagew = pagew
1207 ; pageh = pageh
1208 ; pagex = pagex
1209 ; pagey = pagey
1210 ; pagevw = pagevw
1211 ; pagevh = pagevh
1212 ; pagedispx = pagedispx
1213 ; pagedispy = pagedispy
1214 ; pagecol = n mod columns
1217 e :: accu
1218 else
1219 accu
1220 else
1221 accu
1223 fold accu (n+1)
1225 List.rev (fold [] 0)
1228 let layout y sh =
1229 if nogeomcmds state.geomcmds
1230 then
1231 match conf.columns with
1232 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1233 | Cmulti c -> layoutN c y sh
1234 | Csplit s -> layoutS s y sh
1235 else []
1238 let clamp incr =
1239 let y = state.y + incr in
1240 let y = max 0 y in
1241 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1245 let itertiles l f =
1246 let tilex = l.pagex mod conf.tilew in
1247 let tiley = l.pagey mod conf.tileh in
1249 let col = l.pagex / conf.tilew in
1250 let row = l.pagey / conf.tileh in
1252 let rec rowloop row y0 dispy h =
1253 if h = 0
1254 then ()
1255 else (
1256 let dh = conf.tileh - y0 in
1257 let dh = min h dh in
1258 let rec colloop col x0 dispx w =
1259 if w = 0
1260 then ()
1261 else (
1262 let dw = conf.tilew - x0 in
1263 let dw = min w dw in
1265 f col row dispx dispy x0 y0 dw dh;
1266 colloop (col+1) 0 (dispx+dw) (w-dw)
1269 colloop col tilex l.pagedispx l.pagevw;
1270 rowloop (row+1) 0 (dispy+dh) (h-dh)
1273 if l.pagevw > 0 && l.pagevh > 0
1274 then rowloop row tiley l.pagedispy l.pagevh;
1277 let gettileopaque l col row =
1278 let key =
1279 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1281 try Some (Hashtbl.find state.tilemap key)
1282 with Not_found -> None
1285 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1286 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1287 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1290 let drawtiles l color =
1291 GlDraw.color color;
1292 let f col row x y tilex tiley w h =
1293 match gettileopaque l col row with
1294 | Some (opaque, _, t) ->
1295 let params = x, y, w, h, tilex, tiley in
1296 if conf.invert
1297 then (
1298 Gl.enable `blend;
1299 GlFunc.blend_func `zero `one_minus_src_color;
1301 drawtile params opaque;
1302 if conf.invert
1303 then Gl.disable `blend;
1304 if conf.debug
1305 then (
1306 let s = Printf.sprintf
1307 "%d[%d,%d] %f sec"
1308 l.pageno col row t
1310 let w = measurestr fstate.fontsize s in
1311 GlMisc.push_attrib [`current];
1312 GlDraw.color (0.0, 0.0, 0.0);
1313 GlDraw.rect
1314 (float (x-2), float (y-2))
1315 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1316 GlDraw.color (1.0, 1.0, 1.0);
1317 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1318 GlMisc.pop_attrib ();
1321 | _ ->
1322 let w =
1323 let lw = conf.winw - state.scrollw - x in
1324 min lw w
1325 and h =
1326 let lh = conf.winh - y in
1327 min lh h
1329 begin match state.texid with
1330 | Some id ->
1331 Gl.enable `texture_2d;
1332 GlTex.bind_texture `texture_2d id;
1333 let x0 = float x
1334 and y0 = float y
1335 and x1 = float (x+w)
1336 and y1 = float (y+h) in
1338 let tw = float w /. 64.0
1339 and th = float h /. 64.0 in
1340 let tx0 = float tilex /. 64.0
1341 and ty0 = float tiley /. 64.0 in
1342 let tx1 = tx0 +. tw
1343 and ty1 = ty0 +. th in
1344 GlDraw.begins `quads;
1345 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1346 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1347 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1348 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1349 GlDraw.ends ();
1351 Gl.disable `texture_2d;
1352 | None ->
1353 GlDraw.color (1.0, 1.0, 1.0);
1354 GlDraw.rect
1355 (float x, float y)
1356 (float (x+w), float (y+h));
1357 end;
1358 if w > 128 && h > fstate.fontsize + 10
1359 then (
1360 GlDraw.color (0.0, 0.0, 0.0);
1361 let c, r =
1362 if conf.verbose
1363 then (col*conf.tilew, row*conf.tileh)
1364 else col, row
1366 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1368 GlDraw.color color;
1370 itertiles l f
1373 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1375 let tilevisible1 l x y =
1376 let ax0 = l.pagex
1377 and ax1 = l.pagex + l.pagevw
1378 and ay0 = l.pagey
1379 and ay1 = l.pagey + l.pagevh in
1381 let bx0 = x
1382 and by0 = y in
1383 let bx1 = min (bx0 + conf.tilew) l.pagew
1384 and by1 = min (by0 + conf.tileh) l.pageh in
1386 let rx0 = max ax0 bx0
1387 and ry0 = max ay0 by0
1388 and rx1 = min ax1 bx1
1389 and ry1 = min ay1 by1 in
1391 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1392 nonemptyintersection
1395 let tilevisible layout n x y =
1396 let rec findpageinlayout m = function
1397 | l :: rest when l.pageno = n ->
1398 tilevisible1 l x y || (
1399 match conf.columns with
1400 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1401 | _ -> false
1403 | _ :: rest -> findpageinlayout 0 rest
1404 | [] -> false
1406 findpageinlayout 0 layout;
1409 let tileready l x y =
1410 tilevisible1 l x y &&
1411 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1414 let tilepage n p layout =
1415 let rec loop = function
1416 | l :: rest ->
1417 if l.pageno = n
1418 then
1419 let f col row _ _ _ _ _ _ =
1420 if state.currently = Idle
1421 then
1422 match gettileopaque l col row with
1423 | Some _ -> ()
1424 | None ->
1425 let x = col*conf.tilew
1426 and y = row*conf.tileh in
1427 let w =
1428 let w = l.pagew - x in
1429 min w conf.tilew
1431 let h =
1432 let h = l.pageh - y in
1433 min h conf.tileh
1435 wcmd "tile %s %d %d %d %d" p x y w h;
1436 state.currently <-
1437 Tiling (
1438 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1439 conf.tilew, conf.tileh
1442 itertiles l f;
1443 else
1444 loop rest
1446 | [] -> ()
1448 if nogeomcmds state.geomcmds
1449 then loop layout;
1452 let page_of_y y =
1453 let b =
1454 match conf.columns with
1455 | Csingle b -> b
1456 | Cmulti (_, b) -> b
1457 | Csplit (_, b) -> b
1459 let rec bsearch nmin nmax =
1460 if nmin > nmax
1461 then -1
1462 else
1463 let n = (nmax + nmin) / 2 in
1464 let _, _, vy, (_, h, _, _) = b.(n) in
1465 let y0, y1 =
1466 if conf.presentation
1467 then
1468 let ips = calcips h in
1469 let y0 = vy - ips in
1470 let y1 = vy + h + ips in
1471 y0, y1
1472 else (
1473 if n = 0
1474 then 0, vy + h + conf.interpagespace
1475 else
1476 let y0 = vy - conf.interpagespace in
1477 y0, y0 + h
1480 if y >= y0 && y < y1
1481 then n
1482 else (
1483 if y > y0
1484 then bsearch (n+1) nmax
1485 else bsearch nmin (n-1)
1488 let r = bsearch 0 (state.pagecount-1) in
1492 let preloadlayout y =
1493 let y = if y < conf.winh then 0 else y - conf.winh in
1494 let h = conf.winh*3 in
1495 layout y h;
1498 let load pages =
1499 let rec loop pages =
1500 if state.currently != Idle
1501 then ()
1502 else
1503 match pages with
1504 | l :: rest ->
1505 begin match getopaque l.pageno with
1506 | None ->
1507 wcmd "page %d %d" l.pageno l.pagedimno;
1508 state.currently <- Loading (l, state.gen);
1509 | Some opaque ->
1510 tilepage l.pageno opaque pages;
1511 loop rest
1512 end;
1513 | _ -> ()
1515 if nogeomcmds state.geomcmds
1516 then loop pages
1519 let preload pages =
1520 load pages;
1521 if conf.preload && state.currently = Idle
1522 then load (preloadlayout state.y);
1525 let layoutready layout =
1526 let rec fold all ls =
1527 all && match ls with
1528 | l :: rest ->
1529 let seen = ref false in
1530 let allvisible = ref true in
1531 let foo col row _ _ _ _ _ _ =
1532 seen := true;
1533 allvisible := !allvisible &&
1534 begin match gettileopaque l col row with
1535 | Some _ -> true
1536 | None -> false
1539 itertiles l foo;
1540 fold (!seen && !allvisible) rest
1541 | [] -> true
1543 let alltilesvisible = fold true layout in
1544 alltilesvisible;
1547 let gotoy y =
1548 let y = bound y 0 state.maxy in
1549 let y, layout, proceed =
1550 match conf.maxwait with
1551 | Some time when state.ghyll == noghyll ->
1552 begin match state.throttle with
1553 | None ->
1554 let layout = layout y conf.winh in
1555 let ready = layoutready layout in
1556 if not ready
1557 then (
1558 load layout;
1559 state.throttle <- Some (layout, y, now ());
1561 else G.postRedisplay "gotoy showall (None)";
1562 y, layout, ready
1563 | Some (_, _, started) ->
1564 let dt = now () -. started in
1565 if dt > time
1566 then (
1567 state.throttle <- None;
1568 let layout = layout y conf.winh in
1569 load layout;
1570 G.postRedisplay "maxwait";
1571 y, layout, true
1573 else -1, [], false
1576 | _ ->
1577 let layout = layout y conf.winh in
1578 if true || layoutready layout
1579 then G.postRedisplay "gotoy ready";
1580 y, layout, true
1582 if proceed
1583 then (
1584 state.y <- y;
1585 state.layout <- layout;
1586 begin match state.mode with
1587 | LinkNav (Ltexact (pageno, linkno)) ->
1588 let rec loop = function
1589 | [] ->
1590 state.mode <- LinkNav (Ltgendir 0)
1591 | l :: _ when l.pageno = pageno ->
1592 begin match getopaque pageno with
1593 | None ->
1594 state.mode <- LinkNav (Ltgendir 0)
1595 | Some opaque ->
1596 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1597 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1598 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1599 then state.mode <- LinkNav (Ltgendir 0)
1601 | _ :: rest -> loop rest
1603 loop layout
1604 | _ -> ()
1605 end;
1606 begin match state.mode with
1607 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1608 if not (pagevisible layout pageno)
1609 then (
1610 match state.layout with
1611 | [] -> ()
1612 | l :: _ ->
1613 state.mode <- Birdseye (
1614 conf, leftx, l.pageno, hooverpageno, anchor
1617 | LinkNav (Ltgendir dir as lt) ->
1618 let linknav =
1619 let rec loop = function
1620 | [] -> lt
1621 | l :: rest ->
1622 match getopaque l.pageno with
1623 | None -> loop rest
1624 | Some opaque ->
1625 let link =
1626 let ld =
1627 if dir = 0
1628 then LDfirstvisible (l.pagex, l.pagey, dir)
1629 else (
1630 if dir > 0 then LDfirst else LDlast
1633 findlink opaque ld
1635 match link with
1636 | Lnotfound -> loop rest
1637 | Lfound n ->
1638 showlinktype (getlink opaque n);
1639 Ltexact (l.pageno, n)
1641 loop state.layout
1643 state.mode <- LinkNav linknav
1644 | _ -> ()
1645 end;
1646 preload layout;
1648 state.ghyll <- noghyll;
1649 if conf.updatecurs
1650 then (
1651 let mx, my = state.mpos in
1652 updateunder mx my;
1656 let conttiling pageno opaque =
1657 tilepage pageno opaque
1658 (if conf.preload then preloadlayout state.y else state.layout)
1661 let gotoy_and_clear_text y =
1662 if not conf.verbose then state.text <- "";
1663 gotoy y;
1666 let getanchor1 l =
1667 let top =
1668 let coloff = l.pagecol * l.pageh in
1669 float (l.pagey + coloff) /. float l.pageh
1671 let dtop =
1672 if l.pagedispy = 0
1673 then
1675 else
1676 if conf.presentation
1677 then float l.pagedispy /. float (calcips l.pageh)
1678 else float l.pagedispy /. float conf.interpagespace
1680 (l.pageno, top, dtop)
1683 let getanchor () =
1684 match state.layout with
1685 | l :: _ -> getanchor1 l
1686 | [] ->
1687 let n = page_of_y state.y in
1688 let y, h = getpageyh n in
1689 let dy = y - state.y in
1690 let dtop =
1691 if conf.presentation
1692 then
1693 let ips = calcips h in
1694 float (dy + ips) /. float ips
1695 else
1696 float dy /. float conf.interpagespace
1698 (n, 0.0, dtop)
1701 let getanchory (n, top, dtop) =
1702 let y, h = getpageyh n in
1703 if conf.presentation
1704 then
1705 let ips = calcips h in
1706 y + truncate (top*.float h -. dtop*.float ips) + ips;
1707 else
1708 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1711 let gotoanchor anchor =
1712 gotoy (getanchory anchor);
1715 let addnav () =
1716 cbput state.hists.nav (getanchor ());
1719 let getnav dir =
1720 let anchor = cbgetc state.hists.nav dir in
1721 getanchory anchor;
1724 let gotoghyll y =
1725 let scroll f n a b =
1726 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1727 let snake f a b =
1728 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1729 if f < a
1730 then s (float f /. float a)
1731 else (
1732 if f > b
1733 then 1.0 -. s ((float (f-b) /. float (n-b)))
1734 else 1.0
1737 snake f a b
1738 and summa f n a b =
1739 (* courtesy:
1740 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1741 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1742 let iv1 = iv f in
1743 let ins = float a *. iv1
1744 and outs = float (n-b) *. iv1 in
1745 let ones = b - a in
1746 ins +. outs +. float ones
1748 let rec set (_N, _A, _B) y sy =
1749 let sum = summa 1.0 _N _A _B in
1750 let dy = float (y - sy) in
1751 state.ghyll <- (
1752 let rec gf n y1 o =
1753 if n >= _N
1754 then state.ghyll <- noghyll
1755 else
1756 let go n =
1757 let s = scroll n _N _A _B in
1758 let y1 = y1 +. ((s *. dy) /. sum) in
1759 gotoy_and_clear_text (truncate y1);
1760 state.ghyll <- gf (n+1) y1;
1762 match o with
1763 | None -> go n
1764 | Some y' -> set (_N/2, 0, 0) y' state.y
1766 gf 0 (float state.y)
1769 match conf.ghyllscroll with
1770 | None ->
1771 gotoy_and_clear_text y
1772 | Some nab ->
1773 if state.ghyll == noghyll
1774 then set nab y state.y
1775 else state.ghyll (Some y)
1778 let gotopage n top =
1779 let y, h = getpageyh n in
1780 let y = y + (truncate (top *. float h)) in
1781 gotoghyll y
1784 let gotopage1 n top =
1785 let y = getpagey n in
1786 let y = y + top in
1787 gotoghyll y
1790 let invalidate s f =
1791 state.layout <- [];
1792 state.pdims <- [];
1793 state.rects <- [];
1794 state.rects1 <- [];
1795 match state.geomcmds with
1796 | ps, [] when String.length ps = 0 ->
1797 f ();
1798 state.geomcmds <- s, [];
1800 | ps, [] ->
1801 state.geomcmds <- ps, [s, f];
1803 | ps, (s', _) :: rest when s' = s ->
1804 state.geomcmds <- ps, ((s, f) :: rest);
1806 | ps, cmds ->
1807 state.geomcmds <- ps, ((s, f) :: cmds);
1810 let opendoc path password =
1811 state.path <- path;
1812 state.password <- password;
1813 state.gen <- state.gen + 1;
1814 state.docinfo <- [];
1816 setaalevel conf.aalevel;
1817 Wsi.settitle ("llpp " ^ Filename.basename path);
1818 wcmd "open %s\000%s\000" path password;
1819 invalidate "reqlayout"
1820 (fun () ->
1821 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1824 let scalecolor c =
1825 let c = c *. conf.colorscale in
1826 (c, c, c);
1829 let scalecolor2 (r, g, b) =
1830 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1833 let docolumns = function
1834 | Csingle _ ->
1835 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1836 let rec loop pageno pdimno pdim y ph pdims =
1837 if pageno = state.pagecount
1838 then ()
1839 else
1840 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1841 match pdims with
1842 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1843 pdimno+1, pdim, rest
1844 | _ ->
1845 pdimno, pdim, pdims
1847 let x = max 0 (((conf.winw - state.scrollw - w) / 2) - xoff) in
1848 let y = y +
1849 (if conf.presentation
1850 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1851 else (if pageno = 0 then 0 else calcips h)
1854 a.(pageno) <- (pdimno, x, y, pdim);
1855 loop (pageno+1) pdimno pdim (y + h) h pdims
1857 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1858 conf.columns <- Csingle a;
1860 | Cmulti ((columns, coverA, coverB), _) ->
1861 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1862 let rec loop pageno pdimno pdim x y rowh pdims =
1863 let rec fixrow m = if m = pageno then () else
1864 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1865 if h < rowh
1866 then (
1867 let y = y + (rowh - h) / 2 in
1868 a.(m) <- (pdimno, x, y, pdim);
1870 fixrow (m+1)
1872 if pageno = state.pagecount
1873 then fixrow (((pageno - 1) / columns) * columns)
1874 else
1875 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1876 match pdims with
1877 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1878 pdimno+1, pdim, rest
1879 | _ ->
1880 pdimno, pdim, pdims
1882 let x, y, rowh' =
1883 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1884 then (
1885 (conf.winw - state.scrollw - w) / 2,
1886 y + rowh + conf.interpagespace, h
1888 else (
1889 if (pageno - coverA) mod columns = 0
1890 then (
1891 (if conf.multicenter then
1892 (conf.winw - state.scrollw - state.w) / 2 else 0),
1893 y + rowh + (if pageno = 0 then 0 else conf.interpagespace), h
1895 else x, y, max rowh h
1898 if pageno > 1 && (pageno - coverA) mod columns = 0
1899 then fixrow (pageno - columns);
1900 a.(pageno) <- (pdimno, x, y, pdim);
1901 let x = x + w + xoff*2 + conf.interpagespace in
1902 loop (pageno+1) pdimno pdim x y rowh' pdims
1904 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1905 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1907 | Csplit (c, _) ->
1908 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1909 let rec loop pageno pdimno pdim y pdims =
1910 if pageno = state.pagecount
1911 then ()
1912 else
1913 let pdimno, ((_, w, h, _) as pdim), pdims =
1914 match pdims with
1915 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1916 pdimno+1, pdim, rest
1917 | _ ->
1918 pdimno, pdim, pdims
1920 let cw = w / c in
1921 let rec loop1 n x y =
1922 if n = c then y else (
1923 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1924 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1927 let y = loop1 0 0 y in
1928 loop (pageno+1) pdimno pdim y pdims
1930 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1931 conf.columns <- Csplit (c, a);
1934 let represent () =
1935 docolumns conf.columns;
1936 state.maxy <- calcheight ();
1937 state.hscrollh <-
1938 if state.w <= conf.winw - state.scrollw
1939 then 0
1940 else state.scrollw
1942 match state.mode with
1943 | Birdseye (_, _, pageno, _, _) ->
1944 let y, h = getpageyh pageno in
1945 let top = (conf.winh - h) / 2 in
1946 gotoy (max 0 (y - top))
1947 | _ -> gotoanchor state.anchor
1950 let reshape w h =
1951 GlDraw.viewport 0 0 w h;
1952 let firsttime = state.geomcmds == firstgeomcmds in
1953 if not firsttime && nogeomcmds state.geomcmds
1954 then state.anchor <- getanchor ();
1956 conf.winw <- w;
1957 let w = truncate (float w *. conf.zoom) - state.scrollw in
1958 let w = max w 2 in
1959 conf.winh <- h;
1960 setfontsize fstate.fontsize;
1961 GlMat.mode `modelview;
1962 GlMat.load_identity ();
1964 GlMat.mode `projection;
1965 GlMat.load_identity ();
1966 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1967 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1968 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1970 let relx =
1971 if conf.zoom <= 1.0
1972 then 0.0
1973 else float state.x /. float state.w
1975 invalidate "geometry"
1976 (fun () ->
1977 state.w <- w;
1978 if not firsttime
1979 then state.x <- truncate (relx *. float w);
1980 let w =
1981 match conf.columns with
1982 | Csingle _ -> w
1983 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1984 | Csplit (c, _) -> w * c
1986 wcmd "geometry %d %d" w h);
1989 let enttext () =
1990 let len = String.length state.text in
1991 let drawstring s =
1992 let hscrollh =
1993 match state.mode with
1994 | Textentry _
1995 | View ->
1996 let h, _, _ = state.uioh#scrollpw in
1998 | _ -> 0
2000 let rect x w =
2001 GlDraw.rect
2002 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2003 (x+.w, float (conf.winh - hscrollh))
2006 let w = float (conf.winw - state.scrollw - 1) in
2007 if state.progress >= 0.0 && state.progress < 1.0
2008 then (
2009 GlDraw.color (0.3, 0.3, 0.3);
2010 let w1 = w *. state.progress in
2011 rect 0.0 w1;
2012 GlDraw.color (0.0, 0.0, 0.0);
2013 rect w1 (w-.w1)
2015 else (
2016 GlDraw.color (0.0, 0.0, 0.0);
2017 rect 0.0 w;
2020 GlDraw.color (1.0, 1.0, 1.0);
2021 drawstring fstate.fontsize
2022 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2024 let s =
2025 match state.mode with
2026 | Textentry ((prefix, text, _, _, _, _), _) ->
2027 let s =
2028 if len > 0
2029 then
2030 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2031 else
2032 Printf.sprintf "%s%s_" prefix text
2036 | _ -> state.text
2038 let s =
2039 if state.newerrmsgs
2040 then (
2041 if not (istextentry state.mode)
2042 then
2043 let s1 = "(press 'e' to review error messasges)" in
2044 if String.length s > 0 then s ^ " " ^ s1 else s1
2045 else s
2047 else s
2049 if String.length s > 0
2050 then drawstring s
2053 let gctiles () =
2054 let len = Queue.length state.tilelru in
2055 let layout = lazy (
2056 match state.throttle with
2057 | None ->
2058 if conf.preload
2059 then preloadlayout state.y
2060 else state.layout
2061 | Some (layout, _, _) ->
2062 layout
2063 ) in
2064 let rec loop qpos =
2065 if state.memused <= conf.memlimit
2066 then ()
2067 else (
2068 if qpos < len
2069 then
2070 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2071 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2072 let (_, pw, ph, _) = getpagedim n in
2074 gen = state.gen
2075 && colorspace = conf.colorspace
2076 && angle = conf.angle
2077 && pagew = pw
2078 && pageh = ph
2079 && (
2080 let x = col*conf.tilew
2081 and y = row*conf.tileh in
2082 tilevisible (Lazy.force_val layout) n x y
2084 then Queue.push lruitem state.tilelru
2085 else (
2086 wcmd "freetile %s" p;
2087 state.memused <- state.memused - s;
2088 state.uioh#infochanged Memused;
2089 Hashtbl.remove state.tilemap k;
2091 loop (qpos+1)
2094 loop 0
2097 let flushtiles () =
2098 Queue.iter (fun (k, p, s) ->
2099 wcmd "freetile %s" p;
2100 state.memused <- state.memused - s;
2101 state.uioh#infochanged Memused;
2102 Hashtbl.remove state.tilemap k;
2103 ) state.tilelru;
2104 Queue.clear state.tilelru;
2105 load state.layout;
2108 let logcurrently = function
2109 | Idle -> dolog "Idle"
2110 | Loading (l, gen) ->
2111 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2112 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2113 dolog
2114 "Tiling %d[%d,%d] page=%s cs=%s angle"
2115 l.pageno col row pageopaque
2116 (colorspace_to_string colorspace)
2118 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2119 angle gen conf.angle state.gen
2120 tilew tileh
2121 conf.tilew conf.tileh
2123 | Outlining _ ->
2124 dolog "outlining"
2127 let act cmds =
2128 (* dolog "%S" cmds; *)
2129 let op, args =
2130 let spacepos =
2131 try String.index cmds ' '
2132 with Not_found -> -1
2134 if spacepos = -1
2135 then cmds, ""
2136 else
2137 let l = String.length cmds in
2138 let op = String.sub cmds 0 spacepos in
2139 op, begin
2140 if l - spacepos < 2 then ""
2141 else String.sub cmds (spacepos+1) (l-spacepos-1)
2144 match op with
2145 | "clear" ->
2146 state.uioh#infochanged Pdim;
2147 state.pdims <- [];
2149 | "clearrects" ->
2150 state.rects <- state.rects1;
2151 G.postRedisplay "clearrects";
2153 | "continue" ->
2154 let n =
2155 try Scanf.sscanf args "%u" (fun n -> n)
2156 with exn ->
2157 dolog "error processing 'continue' %S: %s"
2158 cmds (Printexc.to_string exn);
2159 exit 1;
2161 state.pagecount <- n;
2162 begin match state.currently with
2163 | Outlining l ->
2164 state.currently <- Idle;
2165 state.outlines <- Array.of_list (List.rev l)
2166 | _ -> ()
2167 end;
2169 let cur, cmds = state.geomcmds in
2170 if String.length cur = 0
2171 then failwith "umpossible";
2173 begin match List.rev cmds with
2174 | [] ->
2175 state.geomcmds <- "", [];
2176 represent ();
2177 | (s, f) :: rest ->
2178 f ();
2179 state.geomcmds <- s, List.rev rest;
2180 end;
2181 if conf.maxwait = None
2182 then G.postRedisplay "continue";
2184 | "title" ->
2185 Wsi.settitle args
2187 | "msg" ->
2188 showtext ' ' args
2190 | "vmsg" ->
2191 if conf.verbose
2192 then showtext ' ' args
2194 | "progress" ->
2195 let progress, text =
2197 Scanf.sscanf args "%f %n"
2198 (fun f pos ->
2199 f, String.sub args pos (String.length args - pos))
2200 with exn ->
2201 dolog "error processing 'progress' %S: %s"
2202 cmds (Printexc.to_string exn);
2203 exit 1;
2205 state.text <- text;
2206 state.progress <- progress;
2207 G.postRedisplay "progress"
2209 | "firstmatch" ->
2210 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2212 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2213 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2214 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2215 with exn ->
2216 dolog "error processing 'firstmatch' %S: %s"
2217 cmds (Printexc.to_string exn);
2218 exit 1;
2220 let y = (getpagey pageno) + truncate y0 in
2221 addnav ();
2222 gotoy y;
2223 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2225 | "match" ->
2226 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2228 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2229 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2230 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2231 with exn ->
2232 dolog "error processing 'match' %S: %s"
2233 cmds (Printexc.to_string exn);
2234 exit 1;
2236 state.rects1 <-
2237 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2239 | "page" ->
2240 let pageopaque, t =
2242 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2243 with exn ->
2244 dolog "error processing 'page' %S: %s"
2245 cmds (Printexc.to_string exn);
2246 exit 1;
2248 begin match state.currently with
2249 | Loading (l, gen) ->
2250 vlog "page %d took %f sec" l.pageno t;
2251 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2252 begin match state.throttle with
2253 | None ->
2254 let preloadedpages =
2255 if conf.preload
2256 then preloadlayout state.y
2257 else state.layout
2259 let evict () =
2260 let module IntSet =
2261 Set.Make (struct type t = int let compare = (-) end) in
2262 let set =
2263 List.fold_left (fun s l -> IntSet.add l.pageno s)
2264 IntSet.empty preloadedpages
2266 let evictedpages =
2267 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2268 if not (IntSet.mem pageno set)
2269 then (
2270 wcmd "freepage %s" opaque;
2271 key :: accu
2273 else accu
2274 ) state.pagemap []
2276 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2278 evict ();
2279 state.currently <- Idle;
2280 if gen = state.gen
2281 then (
2282 tilepage l.pageno pageopaque state.layout;
2283 load state.layout;
2284 load preloadedpages;
2285 if pagevisible state.layout l.pageno
2286 && layoutready state.layout
2287 then G.postRedisplay "page";
2290 | Some (layout, _, _) ->
2291 state.currently <- Idle;
2292 tilepage l.pageno pageopaque layout;
2293 load state.layout
2294 end;
2296 | _ ->
2297 dolog "Inconsistent loading state";
2298 logcurrently state.currently;
2299 exit 1
2302 | "tile" ->
2303 let (x, y, opaque, size, t) =
2305 Scanf.sscanf args "%u %u %s %u %f"
2306 (fun x y p size t -> (x, y, p, size, t))
2307 with exn ->
2308 dolog "error processing 'tile' %S: %s"
2309 cmds (Printexc.to_string exn);
2310 exit 1;
2312 begin match state.currently with
2313 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2314 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2316 if tilew != conf.tilew || tileh != conf.tileh
2317 then (
2318 wcmd "freetile %s" opaque;
2319 state.currently <- Idle;
2320 load state.layout;
2322 else (
2323 puttileopaque l col row gen cs angle opaque size t;
2324 state.memused <- state.memused + size;
2325 state.uioh#infochanged Memused;
2326 gctiles ();
2327 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2328 opaque, size) state.tilelru;
2330 let layout =
2331 match state.throttle with
2332 | None -> state.layout
2333 | Some (layout, _, _) -> layout
2336 state.currently <- Idle;
2337 if gen = state.gen
2338 && conf.colorspace = cs
2339 && conf.angle = angle
2340 && tilevisible layout l.pageno x y
2341 then conttiling l.pageno pageopaque;
2343 begin match state.throttle with
2344 | None ->
2345 preload state.layout;
2346 if gen = state.gen
2347 && conf.colorspace = cs
2348 && conf.angle = angle
2349 && tilevisible state.layout l.pageno x y
2350 then G.postRedisplay "tile nothrottle";
2352 | Some (layout, y, _) ->
2353 let ready = layoutready layout in
2354 if ready
2355 then (
2356 state.y <- y;
2357 state.layout <- layout;
2358 state.throttle <- None;
2359 G.postRedisplay "throttle";
2361 else load layout;
2362 end;
2365 | _ ->
2366 dolog "Inconsistent tiling state";
2367 logcurrently state.currently;
2368 exit 1
2371 | "pdim" ->
2372 let pdim =
2374 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2375 with exn ->
2376 dolog "error processing 'pdim' %S: %s"
2377 cmds (Printexc.to_string exn);
2378 exit 1;
2380 state.uioh#infochanged Pdim;
2381 state.pdims <- pdim :: state.pdims
2383 | "o" ->
2384 let (l, n, t, h, pos) =
2386 Scanf.sscanf args "%u %u %d %u %n"
2387 (fun l n t h pos -> l, n, t, h, pos)
2388 with exn ->
2389 dolog "error processing 'o' %S: %s"
2390 cmds (Printexc.to_string exn);
2391 exit 1;
2393 let s = String.sub args pos (String.length args - pos) in
2394 let outline = (s, l, (n, float t /. float h, 0.0)) in
2395 begin match state.currently with
2396 | Outlining outlines ->
2397 state.currently <- Outlining (outline :: outlines)
2398 | Idle ->
2399 state.currently <- Outlining [outline]
2400 | currently ->
2401 dolog "invalid outlining state";
2402 logcurrently currently
2405 | "info" ->
2406 state.docinfo <- (1, args) :: state.docinfo
2408 | "infoend" ->
2409 state.uioh#infochanged Docinfo;
2410 state.docinfo <- List.rev state.docinfo
2412 | _ ->
2413 dolog "unknown cmd `%S'" cmds
2416 let onhist cb =
2417 let rc = cb.rc in
2418 let action = function
2419 | HCprev -> cbget cb ~-1
2420 | HCnext -> cbget cb 1
2421 | HCfirst -> cbget cb ~-(cb.rc)
2422 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2423 and cancel () = cb.rc <- rc
2424 in (action, cancel)
2427 let search pattern forward =
2428 if String.length pattern > 0
2429 then
2430 let pn, py =
2431 match state.layout with
2432 | [] -> 0, 0
2433 | l :: _ ->
2434 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2436 wcmd "search %d %d %d %d,%s\000"
2437 (btod conf.icase) pn py (btod forward) pattern;
2440 let intentry text key =
2441 let c =
2442 if key >= 32 && key < 127
2443 then Char.chr key
2444 else '\000'
2446 match c with
2447 | '0' .. '9' ->
2448 let text = addchar text c in
2449 TEcont text
2451 | _ ->
2452 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2453 TEcont text
2456 let linknentry text key =
2457 let c =
2458 if key >= 32 && key < 127
2459 then Char.chr key
2460 else '\000'
2462 match c with
2463 | 'a' .. 'z' ->
2464 let text = addchar text c in
2465 TEcont text
2467 | _ ->
2468 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2469 TEcont text
2472 let linkndone f s =
2473 if String.length s > 0
2474 then (
2475 let n =
2476 let l = String.length s in
2477 let rec loop pos n = if pos = l then n else
2478 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2479 loop (pos+1) (n*26 + m)
2480 in loop 0 0
2482 let rec loop n = function
2483 | [] -> ()
2484 | l :: rest ->
2485 match getopaque l.pageno with
2486 | None -> loop n rest
2487 | Some opaque ->
2488 let m = getlinkcount opaque in
2489 if n < m
2490 then (
2491 let under = getlink opaque n in
2492 f under
2494 else loop (n-m) rest
2496 loop n state.layout;
2500 let textentry text key =
2501 if key land 0xff00 = 0xff00
2502 then TEcont text
2503 else TEcont (text ^ Wsi.toutf8 key)
2506 let reqlayout angle proportional =
2507 match state.throttle with
2508 | None ->
2509 if nogeomcmds state.geomcmds
2510 then state.anchor <- getanchor ();
2511 conf.angle <- angle mod 360;
2512 if conf.angle != 0
2513 then (
2514 match state.mode with
2515 | LinkNav _ -> state.mode <- View
2516 | _ -> ()
2518 conf.proportional <- proportional;
2519 invalidate "reqlayout"
2520 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2521 | _ -> ()
2524 let settrim trimmargins trimfuzz =
2525 if nogeomcmds state.geomcmds
2526 then state.anchor <- getanchor ();
2527 conf.trimmargins <- trimmargins;
2528 conf.trimfuzz <- trimfuzz;
2529 let x0, y0, x1, y1 = trimfuzz in
2530 invalidate "settrim"
2531 (fun () ->
2532 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2533 Hashtbl.iter (fun _ opaque ->
2534 wcmd "freepage %s" opaque;
2535 ) state.pagemap;
2536 Hashtbl.clear state.pagemap;
2539 let setzoom zoom =
2540 match state.throttle with
2541 | None ->
2542 let zoom = max 0.01 zoom in
2543 if zoom <> conf.zoom
2544 then (
2545 state.prevzoom <- conf.zoom;
2546 conf.zoom <- zoom;
2547 reshape conf.winw conf.winh;
2548 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2551 | Some (layout, y, started) ->
2552 let time =
2553 match conf.maxwait with
2554 | None -> 0.0
2555 | Some t -> t
2557 let dt = now () -. started in
2558 if dt > time
2559 then (
2560 state.y <- y;
2561 load layout;
2565 let setcolumns mode columns coverA coverB =
2566 state.prevcolumns <- Some (conf.columns, conf.zoom);
2567 if columns < 0
2568 then (
2569 if isbirdseye mode
2570 then showtext '!' "split mode doesn't work in bird's eye"
2571 else (
2572 conf.columns <- Csplit (-columns, [||]);
2573 state.x <- 0;
2574 conf.zoom <- 1.0;
2577 else (
2578 if columns < 2
2579 then (
2580 conf.columns <- Csingle [||];
2581 state.x <- 0;
2582 setzoom 1.0;
2584 else (
2585 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2586 conf.zoom <- 1.0;
2589 reshape conf.winw conf.winh;
2592 let enterbirdseye () =
2593 let zoom = float conf.thumbw /. float conf.winw in
2594 let birdseyepageno =
2595 let cy = conf.winh / 2 in
2596 let fold = function
2597 | [] -> 0
2598 | l :: rest ->
2599 let rec fold best = function
2600 | [] -> best.pageno
2601 | l :: rest ->
2602 let d = cy - (l.pagedispy + l.pagevh/2)
2603 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2604 if abs d < abs dbest
2605 then fold l rest
2606 else best.pageno
2607 in fold l rest
2609 fold state.layout
2611 state.mode <- Birdseye (
2612 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2614 conf.zoom <- zoom;
2615 conf.presentation <- false;
2616 conf.interpagespace <- 10;
2617 conf.hlinks <- false;
2618 state.x <- 0;
2619 state.mstate <- Mnone;
2620 conf.maxwait <- None;
2621 conf.columns <- (
2622 match conf.beyecolumns with
2623 | Some c ->
2624 conf.zoom <- 1.0;
2625 Cmulti ((c, 0, 0), [||])
2626 | None -> Csingle [||]
2628 Wsi.setcursor Wsi.CURSOR_INHERIT;
2629 if conf.verbose
2630 then
2631 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2632 (100.0*.zoom)
2633 else
2634 state.text <- ""
2636 reshape conf.winw conf.winh;
2639 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2640 state.mode <- View;
2641 conf.zoom <- c.zoom;
2642 conf.presentation <- c.presentation;
2643 conf.interpagespace <- c.interpagespace;
2644 conf.maxwait <- c.maxwait;
2645 conf.hlinks <- c.hlinks;
2646 conf.beyecolumns <- (
2647 match conf.columns with
2648 | Cmulti ((c, _, _), _) -> Some c
2649 | Csingle _ -> None
2650 | Csplit _ -> failwith "leaving bird's eye split mode"
2652 conf.columns <- (
2653 match c.columns with
2654 | Cmulti (c, _) -> Cmulti (c, [||])
2655 | Csingle _ -> Csingle [||]
2656 | Csplit (c, _) -> Csplit (c, [||])
2658 state.x <- leftx;
2659 if conf.verbose
2660 then
2661 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2662 (100.0*.conf.zoom)
2664 reshape conf.winw conf.winh;
2665 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2668 let togglebirdseye () =
2669 match state.mode with
2670 | Birdseye vals -> leavebirdseye vals true
2671 | View -> enterbirdseye ()
2672 | _ -> ()
2675 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2676 let pageno = max 0 (pageno - incr) in
2677 let rec loop = function
2678 | [] -> gotopage1 pageno 0
2679 | l :: _ when l.pageno = pageno ->
2680 if l.pagedispy >= 0 && l.pagey = 0
2681 then G.postRedisplay "upbirdseye"
2682 else gotopage1 pageno 0
2683 | _ :: rest -> loop rest
2685 loop state.layout;
2686 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2689 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2690 let pageno = min (state.pagecount - 1) (pageno + incr) in
2691 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2692 let rec loop = function
2693 | [] ->
2694 let y, h = getpageyh pageno in
2695 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2696 gotoy (clamp dy)
2697 | l :: _ when l.pageno = pageno ->
2698 if l.pagevh != l.pageh
2699 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2700 else G.postRedisplay "downbirdseye"
2701 | _ :: rest -> loop rest
2703 loop state.layout
2706 let optentry mode _ key =
2707 let btos b = if b then "on" else "off" in
2708 if key >= 32 && key < 127
2709 then
2710 let c = Char.chr key in
2711 match c with
2712 | 's' ->
2713 let ondone s =
2714 try conf.scrollstep <- int_of_string s with exc ->
2715 state.text <- Printf.sprintf "bad integer `%s': %s"
2716 s (Printexc.to_string exc)
2718 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2720 | 'A' ->
2721 let ondone s =
2723 conf.autoscrollstep <- int_of_string s;
2724 if state.autoscroll <> None
2725 then state.autoscroll <- Some conf.autoscrollstep
2726 with exc ->
2727 state.text <- Printf.sprintf "bad integer `%s': %s"
2728 s (Printexc.to_string exc)
2730 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2732 | 'C' ->
2733 let ondone s =
2735 let n, a, b = multicolumns_of_string s in
2736 setcolumns mode n a b;
2737 with exc ->
2738 state.text <- Printf.sprintf "bad columns `%s': %s"
2739 s (Printexc.to_string exc)
2741 TEswitch ("columns: ", "", None, textentry, ondone, true)
2743 | 'Z' ->
2744 let ondone s =
2746 let zoom = float (int_of_string s) /. 100.0 in
2747 setzoom zoom
2748 with exc ->
2749 state.text <- Printf.sprintf "bad integer `%s': %s"
2750 s (Printexc.to_string exc)
2752 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2754 | 't' ->
2755 let ondone s =
2757 conf.thumbw <- bound (int_of_string s) 2 4096;
2758 state.text <-
2759 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2760 begin match mode with
2761 | Birdseye beye ->
2762 leavebirdseye beye false;
2763 enterbirdseye ();
2764 | _ -> ();
2766 with exc ->
2767 state.text <- Printf.sprintf "bad integer `%s': %s"
2768 s (Printexc.to_string exc)
2770 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2772 | 'R' ->
2773 let ondone s =
2774 match try
2775 Some (int_of_string s)
2776 with exc ->
2777 state.text <- Printf.sprintf "bad integer `%s': %s"
2778 s (Printexc.to_string exc);
2779 None
2780 with
2781 | Some angle -> reqlayout angle conf.proportional
2782 | None -> ()
2784 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2786 | 'i' ->
2787 conf.icase <- not conf.icase;
2788 TEdone ("case insensitive search " ^ (btos conf.icase))
2790 | 'p' ->
2791 conf.preload <- not conf.preload;
2792 gotoy state.y;
2793 TEdone ("preload " ^ (btos conf.preload))
2795 | 'v' ->
2796 conf.verbose <- not conf.verbose;
2797 TEdone ("verbose " ^ (btos conf.verbose))
2799 | 'd' ->
2800 conf.debug <- not conf.debug;
2801 TEdone ("debug " ^ (btos conf.debug))
2803 | 'h' ->
2804 conf.maxhfit <- not conf.maxhfit;
2805 state.maxy <- calcheight ();
2806 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2808 | 'c' ->
2809 conf.crophack <- not conf.crophack;
2810 TEdone ("crophack " ^ btos conf.crophack)
2812 | 'a' ->
2813 let s =
2814 match conf.maxwait with
2815 | None ->
2816 conf.maxwait <- Some infinity;
2817 "always wait for page to complete"
2818 | Some _ ->
2819 conf.maxwait <- None;
2820 "show placeholder if page is not ready"
2822 TEdone s
2824 | 'f' ->
2825 conf.underinfo <- not conf.underinfo;
2826 TEdone ("underinfo " ^ btos conf.underinfo)
2828 | 'P' ->
2829 conf.savebmarks <- not conf.savebmarks;
2830 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2832 | 'S' ->
2833 let ondone s =
2835 let pageno, py =
2836 match state.layout with
2837 | [] -> 0, 0
2838 | l :: _ ->
2839 l.pageno, l.pagey
2841 conf.interpagespace <- int_of_string s;
2842 docolumns conf.columns;
2843 state.maxy <- calcheight ();
2844 let y = getpagey pageno in
2845 gotoy (y + py)
2846 with exc ->
2847 state.text <- Printf.sprintf "bad integer `%s': %s"
2848 s (Printexc.to_string exc)
2850 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2852 | 'l' ->
2853 reqlayout conf.angle (not conf.proportional);
2854 TEdone ("proportional display " ^ btos conf.proportional)
2856 | 'T' ->
2857 settrim (not conf.trimmargins) conf.trimfuzz;
2858 TEdone ("trim margins " ^ btos conf.trimmargins)
2860 | 'I' ->
2861 conf.invert <- not conf.invert;
2862 TEdone ("invert colors " ^ btos conf.invert)
2864 | 'x' ->
2865 let ondone s =
2866 cbput state.hists.sel s;
2867 conf.selcmd <- s;
2869 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2870 textentry, ondone, true)
2872 | _ ->
2873 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2874 TEstop
2875 else
2876 TEcont state.text
2879 class type lvsource = object
2880 method getitemcount : int
2881 method getitem : int -> (string * int)
2882 method hasaction : int -> bool
2883 method exit :
2884 uioh:uioh ->
2885 cancel:bool ->
2886 active:int ->
2887 first:int ->
2888 pan:int ->
2889 qsearch:string ->
2890 uioh option
2891 method getactive : int
2892 method getfirst : int
2893 method getqsearch : string
2894 method setqsearch : string -> unit
2895 method getpan : int
2896 end;;
2898 class virtual lvsourcebase = object
2899 val mutable m_active = 0
2900 val mutable m_first = 0
2901 val mutable m_qsearch = ""
2902 val mutable m_pan = 0
2903 method getactive = m_active
2904 method getfirst = m_first
2905 method getqsearch = m_qsearch
2906 method getpan = m_pan
2907 method setqsearch s = m_qsearch <- s
2908 end;;
2910 let withoutlastutf8 s =
2911 let len = String.length s in
2912 if len = 0
2913 then s
2914 else
2915 let rec find pos =
2916 if pos = 0
2917 then pos
2918 else
2919 let b = Char.code s.[pos] in
2920 if b land 0b110000 = 0b11000000
2921 then find (pos-1)
2922 else pos-1
2924 let first =
2925 if Char.code s.[len-1] land 0x80 = 0
2926 then len-1
2927 else find (len-1)
2929 String.sub s 0 first;
2932 let textentrykeyboard
2933 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2934 let enttext te =
2935 state.mode <- Textentry (te, onleave);
2936 state.text <- "";
2937 enttext ();
2938 G.postRedisplay "textentrykeyboard enttext";
2940 let histaction cmd =
2941 match opthist with
2942 | None -> ()
2943 | Some (action, _) ->
2944 state.mode <- Textentry (
2945 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2947 G.postRedisplay "textentry histaction"
2949 match key with
2950 | 0xff08 -> (* backspace *)
2951 let s = withoutlastutf8 text in
2952 let len = String.length s in
2953 if cancelonempty && len = 0
2954 then (
2955 onleave Cancel;
2956 G.postRedisplay "textentrykeyboard after cancel";
2958 else (
2959 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2962 | 0xff0d ->
2963 ondone text;
2964 onleave Confirm;
2965 G.postRedisplay "textentrykeyboard after confirm"
2967 | 0xff52 -> histaction HCprev
2968 | 0xff54 -> histaction HCnext
2969 | 0xff50 -> histaction HCfirst
2970 | 0xff57 -> histaction HClast
2972 | 0xff1b -> (* escape*)
2973 if String.length text = 0
2974 then (
2975 begin match opthist with
2976 | None -> ()
2977 | Some (_, onhistcancel) -> onhistcancel ()
2978 end;
2979 onleave Cancel;
2980 state.text <- "";
2981 G.postRedisplay "textentrykeyboard after cancel2"
2983 else (
2984 enttext (c, "", opthist, onkey, ondone, cancelonempty)
2987 | 0xff9f | 0xffff -> () (* delete *)
2989 | _ when key != 0 && key land 0xff00 != 0xff00 ->
2990 begin match onkey text key with
2991 | TEdone text ->
2992 ondone text;
2993 onleave Confirm;
2994 G.postRedisplay "textentrykeyboard after confirm2";
2996 | TEcont text ->
2997 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2999 | TEstop ->
3000 onleave Cancel;
3001 G.postRedisplay "textentrykeyboard after cancel3"
3003 | TEswitch te ->
3004 state.mode <- Textentry (te, onleave);
3005 G.postRedisplay "textentrykeyboard switch";
3006 end;
3008 | _ ->
3009 vlog "unhandled key %s" (Wsi.keyname key)
3012 let firstof first active =
3013 if first > active || abs (first - active) > fstate.maxrows - 1
3014 then max 0 (active - (fstate.maxrows/2))
3015 else first
3018 let calcfirst first active =
3019 if active > first
3020 then
3021 let rows = active - first in
3022 if rows > fstate.maxrows then active - fstate.maxrows else first
3023 else active
3026 let scrollph y maxy =
3027 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3028 let sh = float conf.winh /. sh in
3029 let sh = max sh (float conf.scrollh) in
3031 let percent =
3032 if y = state.maxy
3033 then 1.0
3034 else float y /. float maxy
3036 let position = (float conf.winh -. sh) *. percent in
3038 let position =
3039 if position +. sh > float conf.winh
3040 then float conf.winh -. sh
3041 else position
3043 position, sh;
3046 let coe s = (s :> uioh);;
3048 class listview ~(source:lvsource) ~trusted ~modehash =
3049 object (self)
3050 val m_pan = source#getpan
3051 val m_first = source#getfirst
3052 val m_active = source#getactive
3053 val m_qsearch = source#getqsearch
3054 val m_prev_uioh = state.uioh
3056 method private elemunder y =
3057 let n = y / (fstate.fontsize+1) in
3058 if m_first + n < source#getitemcount
3059 then (
3060 if source#hasaction (m_first + n)
3061 then Some (m_first + n)
3062 else None
3064 else None
3066 method display =
3067 Gl.enable `blend;
3068 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3069 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3070 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3071 GlDraw.color (1., 1., 1.);
3072 Gl.enable `texture_2d;
3073 let fs = fstate.fontsize in
3074 let nfs = fs + 1 in
3075 let ww = fstate.wwidth in
3076 let tabw = 30.0*.ww in
3077 let itemcount = source#getitemcount in
3078 let rec loop row =
3079 if (row - m_first) * nfs > conf.winh
3080 then ()
3081 else (
3082 if row >= 0 && row < itemcount
3083 then (
3084 let (s, level) = source#getitem row in
3085 let y = (row - m_first) * nfs in
3086 let x = 5.0 +. float (level + m_pan) *. ww in
3087 if row = m_active
3088 then (
3089 Gl.disable `texture_2d;
3090 GlDraw.polygon_mode `both `line;
3091 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3092 GlDraw.rect (1., float (y + 1))
3093 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3094 GlDraw.polygon_mode `both `fill;
3095 GlDraw.color (1., 1., 1.);
3096 Gl.enable `texture_2d;
3099 let drawtabularstring s =
3100 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3101 if trusted
3102 then
3103 let tabpos = try String.index s '\t' with Not_found -> -1 in
3104 if tabpos > 0
3105 then
3106 let len = String.length s - tabpos - 1 in
3107 let s1 = String.sub s 0 tabpos
3108 and s2 = String.sub s (tabpos + 1) len in
3109 let nx = drawstr x s1 in
3110 let sw = nx -. x in
3111 let x = x +. (max tabw sw) in
3112 drawstr x s2
3113 else
3114 drawstr x s
3115 else
3116 drawstr x s
3118 let _ = drawtabularstring s in
3119 loop (row+1)
3123 loop m_first;
3124 Gl.disable `blend;
3125 Gl.disable `texture_2d;
3127 method updownlevel incr =
3128 let len = source#getitemcount in
3129 let curlevel =
3130 if m_active >= 0 && m_active < len
3131 then snd (source#getitem m_active)
3132 else -1
3134 let rec flow i =
3135 if i = len then i-1 else if i = -1 then 0 else
3136 let _, l = source#getitem i in
3137 if l != curlevel then i else flow (i+incr)
3139 let active = flow m_active in
3140 let first = calcfirst m_first active in
3141 G.postRedisplay "outline updownlevel";
3142 {< m_active = active; m_first = first >}
3144 method private key1 key mask =
3145 let set1 active first qsearch =
3146 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3148 let search active pattern incr =
3149 let dosearch re =
3150 let rec loop n =
3151 if n >= 0 && n < source#getitemcount
3152 then (
3153 let s, _ = source#getitem n in
3155 (try ignore (Str.search_forward re s 0); true
3156 with Not_found -> false)
3157 then Some n
3158 else loop (n + incr)
3160 else None
3162 loop active
3165 let re = Str.regexp_case_fold pattern in
3166 dosearch re
3167 with Failure s ->
3168 state.text <- s;
3169 None
3171 let itemcount = source#getitemcount in
3172 let find start incr =
3173 let rec find i =
3174 if i = -1 || i = itemcount
3175 then -1
3176 else (
3177 if source#hasaction i
3178 then i
3179 else find (i + incr)
3182 find start
3184 let set active first =
3185 let first = bound first 0 (itemcount - fstate.maxrows) in
3186 state.text <- "";
3187 coe {< m_active = active; m_first = first >}
3189 let navigate incr =
3190 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3191 let active, first =
3192 let incr1 = if incr > 0 then 1 else -1 in
3193 if isvisible m_first m_active
3194 then
3195 let next =
3196 let next = m_active + incr in
3197 let next =
3198 if next < 0 || next >= itemcount
3199 then -1
3200 else find next incr1
3202 if next = -1 || abs (m_active - next) > fstate.maxrows
3203 then -1
3204 else next
3206 if next = -1
3207 then
3208 let first = m_first + incr in
3209 let first = bound first 0 (itemcount - 1) in
3210 let next =
3211 let next = m_active + incr in
3212 let next = bound next 0 (itemcount - 1) in
3213 find next ~-incr1
3215 let active = if next = -1 then m_active else next in
3216 active, first
3217 else
3218 let first = min next m_first in
3219 let first =
3220 if abs (next - first) > fstate.maxrows
3221 then first + incr
3222 else first
3224 next, first
3225 else
3226 let first = m_first + incr in
3227 let first = bound first 0 (itemcount - 1) in
3228 let active =
3229 let next = m_active + incr in
3230 let next = bound next 0 (itemcount - 1) in
3231 let next = find next incr1 in
3232 let active =
3233 if next = -1 || abs (m_active - first) > fstate.maxrows
3234 then (
3235 let active = if m_active = -1 then next else m_active in
3236 active
3238 else next
3240 if isvisible first active
3241 then active
3242 else -1
3244 active, first
3246 G.postRedisplay "listview navigate";
3247 set active first;
3249 match key with
3250 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3251 let incr = if key = 0x72 then -1 else 1 in
3252 let active, first =
3253 match search (m_active + incr) m_qsearch incr with
3254 | None ->
3255 state.text <- m_qsearch ^ " [not found]";
3256 m_active, m_first
3257 | Some active ->
3258 state.text <- m_qsearch;
3259 active, firstof m_first active
3261 G.postRedisplay "listview ctrl-r/s";
3262 set1 active first m_qsearch;
3264 | 0xff08 -> (* backspace *)
3265 if String.length m_qsearch = 0
3266 then coe self
3267 else (
3268 let qsearch = withoutlastutf8 m_qsearch in
3269 let len = String.length qsearch in
3270 if len = 0
3271 then (
3272 state.text <- "";
3273 G.postRedisplay "listview empty qsearch";
3274 set1 m_active m_first "";
3276 else
3277 let active, first =
3278 match search m_active qsearch ~-1 with
3279 | None ->
3280 state.text <- qsearch ^ " [not found]";
3281 m_active, m_first
3282 | Some active ->
3283 state.text <- qsearch;
3284 active, firstof m_first active
3286 G.postRedisplay "listview backspace qsearch";
3287 set1 active first qsearch
3290 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3291 let pattern = m_qsearch ^ Wsi.toutf8 key in
3292 let active, first =
3293 match search m_active pattern 1 with
3294 | None ->
3295 state.text <- pattern ^ " [not found]";
3296 m_active, m_first
3297 | Some active ->
3298 state.text <- pattern;
3299 active, firstof m_first active
3301 G.postRedisplay "listview qsearch add";
3302 set1 active first pattern;
3304 | 0xff1b -> (* escape *)
3305 state.text <- "";
3306 if String.length m_qsearch = 0
3307 then (
3308 G.postRedisplay "list view escape";
3309 begin
3310 match
3311 source#exit (coe self) true m_active m_first m_pan m_qsearch
3312 with
3313 | None -> m_prev_uioh
3314 | Some uioh -> uioh
3317 else (
3318 G.postRedisplay "list view kill qsearch";
3319 source#setqsearch "";
3320 coe {< m_qsearch = "" >}
3323 | 0xff0d -> (* return *)
3324 state.text <- "";
3325 let self = {< m_qsearch = "" >} in
3326 source#setqsearch "";
3327 let opt =
3328 G.postRedisplay "listview enter";
3329 if m_active >= 0 && m_active < source#getitemcount
3330 then (
3331 source#exit (coe self) false m_active m_first m_pan "";
3333 else (
3334 source#exit (coe self) true m_active m_first m_pan "";
3337 begin match opt with
3338 | None -> m_prev_uioh
3339 | Some uioh -> uioh
3342 | 0xff9f | 0xffff -> (* delete *)
3343 coe self
3345 | 0xff52 -> navigate ~-1 (* up *)
3346 | 0xff54 -> navigate 1 (* down *)
3347 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3348 | 0xff56 -> navigate fstate.maxrows (* next *)
3350 | 0xff53 -> (* right *)
3351 state.text <- "";
3352 G.postRedisplay "listview right";
3353 coe {< m_pan = m_pan - 1 >}
3355 | 0xff51 -> (* left *)
3356 state.text <- "";
3357 G.postRedisplay "listview left";
3358 coe {< m_pan = m_pan + 1 >}
3360 | 0xff50 -> (* home *)
3361 let active = find 0 1 in
3362 G.postRedisplay "listview home";
3363 set active 0;
3365 | 0xff57 -> (* end *)
3366 let first = max 0 (itemcount - fstate.maxrows) in
3367 let active = find (itemcount - 1) ~-1 in
3368 G.postRedisplay "listview end";
3369 set active first;
3371 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3372 coe self
3374 | _ ->
3375 dolog "listview unknown key %#x" key; coe self
3377 method key key mask =
3378 match state.mode with
3379 | Textentry te -> textentrykeyboard key mask te; coe self
3380 | _ -> self#key1 key mask
3382 method button button down x y _ =
3383 let opt =
3384 match button with
3385 | 1 when x > conf.winw - conf.scrollbw ->
3386 G.postRedisplay "listview scroll";
3387 if down
3388 then
3389 let _, position, sh = self#scrollph in
3390 if y > truncate position && y < truncate (position +. sh)
3391 then (
3392 state.mstate <- Mscrolly;
3393 Some (coe self)
3395 else
3396 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3397 let first = truncate (s *. float source#getitemcount) in
3398 let first = min source#getitemcount first in
3399 Some (coe {< m_first = first; m_active = first >})
3400 else (
3401 state.mstate <- Mnone;
3402 Some (coe self);
3404 | 1 when not down ->
3405 begin match self#elemunder y with
3406 | Some n ->
3407 G.postRedisplay "listview click";
3408 source#exit
3409 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3410 | _ ->
3411 Some (coe self)
3413 | n when (n == 4 || n == 5) && not down ->
3414 let len = source#getitemcount in
3415 let first =
3416 if n = 5 && m_first + fstate.maxrows >= len
3417 then
3418 m_first
3419 else
3420 let first = m_first + (if n == 4 then -1 else 1) in
3421 bound first 0 (len - 1)
3423 G.postRedisplay "listview wheel";
3424 Some (coe {< m_first = first >})
3425 | n when (n = 6 || n = 7) && not down ->
3426 let inc = m_first + (if n = 7 then -1 else 1) in
3427 G.postRedisplay "listview hwheel";
3428 Some (coe {< m_pan = m_pan + inc >})
3429 | _ ->
3430 Some (coe self)
3432 match opt with
3433 | None -> m_prev_uioh
3434 | Some uioh -> uioh
3436 method motion _ y =
3437 match state.mstate with
3438 | Mscrolly ->
3439 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3440 let first = truncate (s *. float source#getitemcount) in
3441 let first = min source#getitemcount first in
3442 G.postRedisplay "listview motion";
3443 coe {< m_first = first; m_active = first >}
3444 | _ -> coe self
3446 method pmotion x y =
3447 if x < conf.winw - conf.scrollbw
3448 then
3449 let n =
3450 match self#elemunder y with
3451 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3452 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3454 let o =
3455 if n != m_active
3456 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3457 else self
3459 coe o
3460 else (
3461 Wsi.setcursor Wsi.CURSOR_INHERIT;
3462 coe self
3465 method infochanged _ = ()
3467 method scrollpw = (0, 0.0, 0.0)
3468 method scrollph =
3469 let nfs = fstate.fontsize + 1 in
3470 let y = m_first * nfs in
3471 let itemcount = source#getitemcount in
3472 let maxi = max 0 (itemcount - fstate.maxrows) in
3473 let maxy = maxi * nfs in
3474 let p, h = scrollph y maxy in
3475 conf.scrollbw, p, h
3477 method modehash = modehash
3478 end;;
3480 class outlinelistview ~source =
3481 object (self)
3482 inherit listview
3483 ~source:(source :> lvsource)
3484 ~trusted:false
3485 ~modehash:(findkeyhash conf "outline")
3486 as super
3488 method key key mask =
3489 let calcfirst first active =
3490 if active > first
3491 then
3492 let rows = active - first in
3493 let maxrows =
3494 if String.length state.text = 0
3495 then fstate.maxrows
3496 else fstate.maxrows - 2
3498 if rows > maxrows then active - maxrows else first
3499 else active
3501 let navigate incr =
3502 let active = m_active + incr in
3503 let active = bound active 0 (source#getitemcount - 1) in
3504 let first = calcfirst m_first active in
3505 G.postRedisplay "outline navigate";
3506 coe {< m_active = active; m_first = first >}
3508 let ctrl = Wsi.withctrl mask in
3509 match key with
3510 | 110 when ctrl -> (* ctrl-n *)
3511 source#narrow m_qsearch;
3512 G.postRedisplay "outline ctrl-n";
3513 coe {< m_first = 0; m_active = 0 >}
3515 | 117 when ctrl -> (* ctrl-u *)
3516 source#denarrow;
3517 G.postRedisplay "outline ctrl-u";
3518 state.text <- "";
3519 coe {< m_first = 0; m_active = 0 >}
3521 | 108 when ctrl -> (* ctrl-l *)
3522 let first = m_active - (fstate.maxrows / 2) in
3523 G.postRedisplay "outline ctrl-l";
3524 coe {< m_first = first >}
3526 | 0xff9f | 0xffff -> (* delete *)
3527 source#remove m_active;
3528 G.postRedisplay "outline delete";
3529 let active = max 0 (m_active-1) in
3530 coe {< m_first = firstof m_first active;
3531 m_active = active >}
3533 | 0xff52 -> navigate ~-1 (* up *)
3534 | 0xff54 -> navigate 1 (* down *)
3535 | 0xff55 -> (* prior *)
3536 navigate ~-(fstate.maxrows)
3537 | 0xff56 -> (* next *)
3538 navigate fstate.maxrows
3540 | 0xff53 -> (* [ctrl-]right *)
3541 let o =
3542 if ctrl
3543 then (
3544 G.postRedisplay "outline ctrl right";
3545 {< m_pan = m_pan + 1 >}
3547 else self#updownlevel 1
3549 coe o
3551 | 0xff51 -> (* [ctrl-]left *)
3552 let o =
3553 if ctrl
3554 then (
3555 G.postRedisplay "outline ctrl left";
3556 {< m_pan = m_pan - 1 >}
3558 else self#updownlevel ~-1
3560 coe o
3562 | 0xff50 -> (* home *)
3563 G.postRedisplay "outline home";
3564 coe {< m_first = 0; m_active = 0 >}
3566 | 0xff57 -> (* end *)
3567 let active = source#getitemcount - 1 in
3568 let first = max 0 (active - fstate.maxrows) in
3569 G.postRedisplay "outline end";
3570 coe {< m_active = active; m_first = first >}
3572 | _ -> super#key key mask
3575 let outlinesource usebookmarks =
3576 let empty = [||] in
3577 (object
3578 inherit lvsourcebase
3579 val mutable m_items = empty
3580 val mutable m_orig_items = empty
3581 val mutable m_prev_items = empty
3582 val mutable m_narrow_pattern = ""
3583 val mutable m_hadremovals = false
3585 method getitemcount =
3586 Array.length m_items + (if m_hadremovals then 1 else 0)
3588 method getitem n =
3589 if n == Array.length m_items && m_hadremovals
3590 then
3591 ("[Confirm removal]", 0)
3592 else
3593 let s, n, _ = m_items.(n) in
3594 (s, n)
3596 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3597 ignore (uioh, first, qsearch);
3598 let confrimremoval = m_hadremovals && active = Array.length m_items in
3599 let items =
3600 if String.length m_narrow_pattern = 0
3601 then m_orig_items
3602 else m_items
3604 if not cancel
3605 then (
3606 if not confrimremoval
3607 then(
3608 let _, _, anchor = m_items.(active) in
3609 gotoanchor anchor;
3610 m_items <- items;
3612 else (
3613 state.bookmarks <- Array.to_list m_items;
3614 m_orig_items <- m_items;
3617 else m_items <- items;
3618 m_pan <- pan;
3619 None
3621 method hasaction _ = true
3623 method greetmsg =
3624 if Array.length m_items != Array.length m_orig_items
3625 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3626 else ""
3628 method narrow pattern =
3629 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3630 match reopt with
3631 | None -> ()
3632 | Some re ->
3633 let rec loop accu n =
3634 if n = -1
3635 then (
3636 m_narrow_pattern <- pattern;
3637 m_items <- Array.of_list accu
3639 else
3640 let (s, _, _) as o = m_items.(n) in
3641 let accu =
3642 if (try ignore (Str.search_forward re s 0); true
3643 with Not_found -> false)
3644 then o :: accu
3645 else accu
3647 loop accu (n-1)
3649 loop [] (Array.length m_items - 1)
3651 method denarrow =
3652 m_orig_items <- (
3653 if usebookmarks
3654 then Array.of_list state.bookmarks
3655 else state.outlines
3657 m_items <- m_orig_items
3659 method remove m =
3660 if usebookmarks
3661 then
3662 if m >= 0 && m < Array.length m_items
3663 then (
3664 m_hadremovals <- true;
3665 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3666 let n = if n >= m then n+1 else n in
3667 m_items.(n)
3671 method reset anchor items =
3672 m_hadremovals <- false;
3673 if m_orig_items == empty || m_prev_items != items
3674 then (
3675 m_orig_items <- items;
3676 if String.length m_narrow_pattern = 0
3677 then m_items <- items;
3679 m_prev_items <- items;
3680 let rely = getanchory anchor in
3681 let active =
3682 let rec loop n best bestd =
3683 if n = Array.length m_items
3684 then best
3685 else
3686 let (_, _, anchor) = m_items.(n) in
3687 let orely = getanchory anchor in
3688 let d = abs (orely - rely) in
3689 if d < bestd
3690 then loop (n+1) n d
3691 else loop (n+1) best bestd
3693 loop 0 ~-1 max_int
3695 m_active <- active;
3696 m_first <- firstof m_first active
3697 end)
3700 let enterselector usebookmarks =
3701 let source = outlinesource usebookmarks in
3702 fun errmsg ->
3703 let outlines =
3704 if usebookmarks
3705 then Array.of_list state.bookmarks
3706 else state.outlines
3708 if Array.length outlines = 0
3709 then (
3710 showtext ' ' errmsg;
3712 else (
3713 state.text <- source#greetmsg;
3714 Wsi.setcursor Wsi.CURSOR_INHERIT;
3715 let anchor = getanchor () in
3716 source#reset anchor outlines;
3717 state.uioh <- coe (new outlinelistview ~source);
3718 G.postRedisplay "enter selector";
3722 let enteroutlinemode =
3723 let f = enterselector false in
3724 fun ()-> f "Document has no outline";
3727 let enterbookmarkmode =
3728 let f = enterselector true in
3729 fun () -> f "Document has no bookmarks (yet)";
3732 let color_of_string s =
3733 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3734 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3738 let color_to_string (r, g, b) =
3739 let r = truncate (r *. 256.0)
3740 and g = truncate (g *. 256.0)
3741 and b = truncate (b *. 256.0) in
3742 Printf.sprintf "%d/%d/%d" r g b
3745 let irect_of_string s =
3746 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3749 let irect_to_string (x0,y0,x1,y1) =
3750 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3753 let makecheckers () =
3754 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3755 following to say:
3756 converted by Issac Trotts. July 25, 2002 *)
3757 let image_height = 64
3758 and image_width = 64 in
3760 let make_image () =
3761 let image =
3762 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3764 for i = 0 to image_width - 1 do
3765 for j = 0 to image_height - 1 do
3766 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3767 (if (i land 8 ) lxor (j land 8) = 0
3768 then [|255;255;255|] else [|200;200;200|])
3769 done
3770 done;
3771 image
3773 let image = make_image () in
3774 let id = GlTex.gen_texture () in
3775 GlTex.bind_texture `texture_2d id;
3776 GlPix.store (`unpack_alignment 1);
3777 GlTex.image2d image;
3778 List.iter (GlTex.parameter ~target:`texture_2d)
3779 [ `wrap_s `repeat;
3780 `wrap_t `repeat;
3781 `mag_filter `nearest;
3782 `min_filter `nearest ];
3786 let setcheckers enabled =
3787 match state.texid with
3788 | None ->
3789 if enabled then state.texid <- Some (makecheckers ())
3791 | Some texid ->
3792 if not enabled
3793 then (
3794 GlTex.delete_texture texid;
3795 state.texid <- None;
3799 let int_of_string_with_suffix s =
3800 let l = String.length s in
3801 let s1, shift =
3802 if l > 1
3803 then
3804 let suffix = Char.lowercase s.[l-1] in
3805 match suffix with
3806 | 'k' -> String.sub s 0 (l-1), 10
3807 | 'm' -> String.sub s 0 (l-1), 20
3808 | 'g' -> String.sub s 0 (l-1), 30
3809 | _ -> s, 0
3810 else s, 0
3812 let n = int_of_string s1 in
3813 let m = n lsl shift in
3814 if m < 0 || m < n
3815 then raise (Failure "value too large")
3816 else m
3819 let string_with_suffix_of_int n =
3820 if n = 0
3821 then "0"
3822 else
3823 let n, s =
3824 if n land ((1 lsl 20) - 1) = 0
3825 then n lsr 20, "M"
3826 else (
3827 if n land ((1 lsl 10) - 1) = 0
3828 then n lsr 10, "K"
3829 else n, ""
3832 let rec loop s n =
3833 let h = n mod 1000 in
3834 let n = n / 1000 in
3835 if n = 0
3836 then string_of_int h ^ s
3837 else (
3838 let s = Printf.sprintf "_%03d%s" h s in
3839 loop s n
3842 loop "" n ^ s;
3845 let defghyllscroll = (40, 8, 32);;
3846 let ghyllscroll_of_string s =
3847 let (n, a, b) as nab =
3848 if s = "default"
3849 then defghyllscroll
3850 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3852 if n <= a || n <= b || a >= b
3853 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3854 nab;
3857 let ghyllscroll_to_string ((n, a, b) as nab) =
3858 if nab = defghyllscroll
3859 then "default"
3860 else Printf.sprintf "%d,%d,%d" n a b;
3863 let describe_location () =
3864 let f (fn, _) l =
3865 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3867 let fn, ln = List.fold_left f (-1, -1) state.layout in
3868 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3869 let percent =
3870 if maxy <= 0
3871 then 100.
3872 else (100. *. (float state.y /. float maxy))
3874 if fn = ln
3875 then
3876 Printf.sprintf "page %d of %d [%.2f%%]"
3877 (fn+1) state.pagecount percent
3878 else
3879 Printf.sprintf
3880 "pages %d-%d of %d [%.2f%%]"
3881 (fn+1) (ln+1) state.pagecount percent
3884 let enterinfomode =
3885 let btos b = if b then "\xe2\x88\x9a" else "" in
3886 let showextended = ref false in
3887 let leave mode = function
3888 | Confirm -> state.mode <- mode
3889 | Cancel -> state.mode <- mode in
3890 let src =
3891 (object
3892 val mutable m_first_time = true
3893 val mutable m_l = []
3894 val mutable m_a = [||]
3895 val mutable m_prev_uioh = nouioh
3896 val mutable m_prev_mode = View
3898 inherit lvsourcebase
3900 method reset prev_mode prev_uioh =
3901 m_a <- Array.of_list (List.rev m_l);
3902 m_l <- [];
3903 m_prev_mode <- prev_mode;
3904 m_prev_uioh <- prev_uioh;
3905 if m_first_time
3906 then (
3907 let rec loop n =
3908 if n >= Array.length m_a
3909 then ()
3910 else
3911 match m_a.(n) with
3912 | _, _, _, Action _ -> m_active <- n
3913 | _ -> loop (n+1)
3915 loop 0;
3916 m_first_time <- false;
3919 method int name get set =
3920 m_l <-
3921 (name, `int get, 1, Action (
3922 fun u ->
3923 let ondone s =
3924 try set (int_of_string s)
3925 with exn ->
3926 state.text <- Printf.sprintf "bad integer `%s': %s"
3927 s (Printexc.to_string exn)
3929 state.text <- "";
3930 let te = name ^ ": ", "", None, intentry, ondone, true in
3931 state.mode <- Textentry (te, leave m_prev_mode);
3933 )) :: m_l
3935 method int_with_suffix name get set =
3936 m_l <-
3937 (name, `intws get, 1, Action (
3938 fun u ->
3939 let ondone s =
3940 try set (int_of_string_with_suffix s)
3941 with exn ->
3942 state.text <- Printf.sprintf "bad integer `%s': %s"
3943 s (Printexc.to_string exn)
3945 state.text <- "";
3946 let te =
3947 name ^ ": ", "", None, intentry_with_suffix, ondone, true
3949 state.mode <- Textentry (te, leave m_prev_mode);
3951 )) :: m_l
3953 method bool ?(offset=1) ?(btos=btos) name get set =
3954 m_l <-
3955 (name, `bool (btos, get), offset, Action (
3956 fun u ->
3957 let v = get () in
3958 set (not v);
3960 )) :: m_l
3962 method color name get set =
3963 m_l <-
3964 (name, `color get, 1, Action (
3965 fun u ->
3966 let invalid = (nan, nan, nan) in
3967 let ondone s =
3968 let c =
3969 try color_of_string s
3970 with exn ->
3971 state.text <- Printf.sprintf "bad color `%s': %s"
3972 s (Printexc.to_string exn);
3973 invalid
3975 if c <> invalid
3976 then set c;
3978 let te = name ^ ": ", "", None, textentry, ondone, true in
3979 state.text <- color_to_string (get ());
3980 state.mode <- Textentry (te, leave m_prev_mode);
3982 )) :: m_l
3984 method string name get set =
3985 m_l <-
3986 (name, `string get, 1, Action (
3987 fun u ->
3988 let ondone s = set s in
3989 let te = name ^ ": ", "", None, textentry, ondone, true in
3990 state.mode <- Textentry (te, leave m_prev_mode);
3992 )) :: m_l
3994 method colorspace name get set =
3995 m_l <-
3996 (name, `string get, 1, Action (
3997 fun _ ->
3998 let source =
3999 let vals = [| "rgb"; "bgr"; "gray" |] in
4000 (object
4001 inherit lvsourcebase
4003 initializer
4004 m_active <- int_of_colorspace conf.colorspace;
4005 m_first <- 0;
4007 method getitemcount = Array.length vals
4008 method getitem n = (vals.(n), 0)
4009 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4010 ignore (uioh, first, pan, qsearch);
4011 if not cancel then set active;
4012 None
4013 method hasaction _ = true
4014 end)
4016 state.text <- "";
4017 let modehash = findkeyhash conf "info" in
4018 coe (new listview ~source ~trusted:true ~modehash)
4019 )) :: m_l
4021 method caption s offset =
4022 m_l <- (s, `empty, offset, Noaction) :: m_l
4024 method caption2 s f offset =
4025 m_l <- (s, `string f, offset, Noaction) :: m_l
4027 method getitemcount = Array.length m_a
4029 method getitem n =
4030 let tostr = function
4031 | `int f -> string_of_int (f ())
4032 | `intws f -> string_with_suffix_of_int (f ())
4033 | `string f -> f ()
4034 | `color f -> color_to_string (f ())
4035 | `bool (btos, f) -> btos (f ())
4036 | `empty -> ""
4038 let name, t, offset, _ = m_a.(n) in
4039 ((let s = tostr t in
4040 if String.length s > 0
4041 then Printf.sprintf "%s\t%s" name s
4042 else name),
4043 offset)
4045 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4046 let uiohopt =
4047 if not cancel
4048 then (
4049 m_qsearch <- qsearch;
4050 let uioh =
4051 match m_a.(active) with
4052 | _, _, _, Action f -> f uioh
4053 | _ -> uioh
4055 Some uioh
4057 else None
4059 m_active <- active;
4060 m_first <- first;
4061 m_pan <- pan;
4062 uiohopt
4064 method hasaction n =
4065 match m_a.(n) with
4066 | _, _, _, Action _ -> true
4067 | _ -> false
4068 end)
4070 let rec fillsrc prevmode prevuioh =
4071 let sep () = src#caption "" 0 in
4072 let colorp name get set =
4073 src#string name
4074 (fun () -> color_to_string (get ()))
4075 (fun v ->
4077 let c = color_of_string v in
4078 set c
4079 with exn ->
4080 state.text <- Printf.sprintf "bad color `%s': %s"
4081 v (Printexc.to_string exn);
4084 let oldmode = state.mode in
4085 let birdseye = isbirdseye state.mode in
4087 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4089 src#bool "presentation mode"
4090 (fun () -> conf.presentation)
4091 (fun v ->
4092 conf.presentation <- v;
4093 state.anchor <- getanchor ();
4094 represent ());
4096 src#bool "ignore case in searches"
4097 (fun () -> conf.icase)
4098 (fun v -> conf.icase <- v);
4100 src#bool "preload"
4101 (fun () -> conf.preload)
4102 (fun v -> conf.preload <- v);
4104 src#bool "highlight links"
4105 (fun () -> conf.hlinks)
4106 (fun v -> conf.hlinks <- v);
4108 src#bool "under info"
4109 (fun () -> conf.underinfo)
4110 (fun v -> conf.underinfo <- v);
4112 src#bool "persistent bookmarks"
4113 (fun () -> conf.savebmarks)
4114 (fun v -> conf.savebmarks <- v);
4116 src#bool "proportional display"
4117 (fun () -> conf.proportional)
4118 (fun v -> reqlayout conf.angle v);
4120 src#bool "trim margins"
4121 (fun () -> conf.trimmargins)
4122 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4124 src#bool "persistent location"
4125 (fun () -> conf.jumpback)
4126 (fun v -> conf.jumpback <- v);
4128 sep ();
4129 src#int "inter-page space"
4130 (fun () -> conf.interpagespace)
4131 (fun n ->
4132 conf.interpagespace <- n;
4133 docolumns conf.columns;
4134 let pageno, py =
4135 match state.layout with
4136 | [] -> 0, 0
4137 | l :: _ ->
4138 l.pageno, l.pagey
4140 state.maxy <- calcheight ();
4141 let y = getpagey pageno in
4142 gotoy (y + py)
4145 src#int "page bias"
4146 (fun () -> conf.pagebias)
4147 (fun v -> conf.pagebias <- v);
4149 src#int "scroll step"
4150 (fun () -> conf.scrollstep)
4151 (fun n -> conf.scrollstep <- n);
4153 src#int "horizontal scroll step"
4154 (fun () -> conf.hscrollstep)
4155 (fun v -> conf.hscrollstep <- v);
4157 src#int "auto scroll step"
4158 (fun () ->
4159 match state.autoscroll with
4160 | Some step -> step
4161 | _ -> conf.autoscrollstep)
4162 (fun n ->
4163 if state.autoscroll <> None
4164 then state.autoscroll <- Some n;
4165 conf.autoscrollstep <- n);
4167 src#int "zoom"
4168 (fun () -> truncate (conf.zoom *. 100.))
4169 (fun v -> setzoom ((float v) /. 100.));
4171 src#int "rotation"
4172 (fun () -> conf.angle)
4173 (fun v -> reqlayout v conf.proportional);
4175 src#int "scroll bar width"
4176 (fun () -> state.scrollw)
4177 (fun v ->
4178 state.scrollw <- v;
4179 conf.scrollbw <- v;
4180 reshape conf.winw conf.winh;
4183 src#int "scroll handle height"
4184 (fun () -> conf.scrollh)
4185 (fun v -> conf.scrollh <- v;);
4187 src#int "thumbnail width"
4188 (fun () -> conf.thumbw)
4189 (fun v ->
4190 conf.thumbw <- min 4096 v;
4191 match oldmode with
4192 | Birdseye beye ->
4193 leavebirdseye beye false;
4194 enterbirdseye ()
4195 | _ -> ()
4198 let mode = state.mode in
4199 src#string "columns"
4200 (fun () ->
4201 match conf.columns with
4202 | Csingle _ -> "1"
4203 | Cmulti (multi, _) -> multicolumns_to_string multi
4204 | Csplit (count, _) -> "-" ^ string_of_int count
4206 (fun v ->
4207 let n, a, b = multicolumns_of_string v in
4208 setcolumns mode n a b);
4210 sep ();
4211 src#caption "Presentation mode" 0;
4212 src#bool "scrollbar visible"
4213 (fun () -> conf.scrollbarinpm)
4214 (fun v ->
4215 if v != conf.scrollbarinpm
4216 then (
4217 conf.scrollbarinpm <- v;
4218 if conf.presentation
4219 then (
4220 state.scrollw <- if v then conf.scrollbw else 0;
4221 reshape conf.winw conf.winh;
4226 sep ();
4227 src#caption "Pixmap cache" 0;
4228 src#int_with_suffix "size (advisory)"
4229 (fun () -> conf.memlimit)
4230 (fun v -> conf.memlimit <- v);
4232 src#caption2 "used"
4233 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4234 (string_with_suffix_of_int state.memused)
4235 (Hashtbl.length state.tilemap)) 1;
4237 sep ();
4238 src#caption "Layout" 0;
4239 src#caption2 "Dimension"
4240 (fun () ->
4241 Printf.sprintf "%dx%d (virtual %dx%d)"
4242 conf.winw conf.winh
4243 state.w state.maxy)
4245 if conf.debug
4246 then
4247 src#caption2 "Position" (fun () ->
4248 Printf.sprintf "%dx%d" state.x state.y
4250 else
4251 src#caption2 "Visible" (fun () -> describe_location ()) 1
4254 sep ();
4255 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4256 "Save these parameters as global defaults at exit"
4257 (fun () -> conf.bedefault)
4258 (fun v -> conf.bedefault <- v)
4261 sep ();
4262 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4263 src#bool ~offset:0 ~btos "Extended parameters"
4264 (fun () -> !showextended)
4265 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4266 if !showextended
4267 then (
4268 src#bool "checkers"
4269 (fun () -> conf.checkers)
4270 (fun v -> conf.checkers <- v; setcheckers v);
4271 src#bool "update cursor"
4272 (fun () -> conf.updatecurs)
4273 (fun v -> conf.updatecurs <- v);
4274 src#bool "verbose"
4275 (fun () -> conf.verbose)
4276 (fun v -> conf.verbose <- v);
4277 src#bool "invert colors"
4278 (fun () -> conf.invert)
4279 (fun v -> conf.invert <- v);
4280 src#bool "max fit"
4281 (fun () -> conf.maxhfit)
4282 (fun v -> conf.maxhfit <- v);
4283 src#bool "redirect stderr"
4284 (fun () -> conf.redirectstderr)
4285 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4286 src#string "uri launcher"
4287 (fun () -> conf.urilauncher)
4288 (fun v -> conf.urilauncher <- v);
4289 src#string "path launcher"
4290 (fun () -> conf.pathlauncher)
4291 (fun v -> conf.pathlauncher <- v);
4292 src#string "tile size"
4293 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4294 (fun v ->
4296 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4297 conf.tilew <- max 64 w;
4298 conf.tileh <- max 64 h;
4299 flushtiles ();
4300 with exn ->
4301 state.text <- Printf.sprintf "bad tile size `%s': %s"
4302 v (Printexc.to_string exn));
4303 src#int "texture count"
4304 (fun () -> conf.texcount)
4305 (fun v ->
4306 if realloctexts v
4307 then conf.texcount <- v
4308 else showtext '!' " Failed to set texture count please retry later"
4310 src#int "slice height"
4311 (fun () -> conf.sliceheight)
4312 (fun v ->
4313 conf.sliceheight <- v;
4314 wcmd "sliceh %d" conf.sliceheight;
4316 src#int "anti-aliasing level"
4317 (fun () -> conf.aalevel)
4318 (fun v ->
4319 conf.aalevel <- bound v 0 8;
4320 state.anchor <- getanchor ();
4321 opendoc state.path state.password;
4323 src#string "page scroll scaling factor"
4324 (fun () -> string_of_float conf.pgscale)
4325 (fun v ->
4327 let s = float_of_string v in
4328 conf.pgscale <- s
4329 with exn ->
4330 state.text <- Printf.sprintf
4331 "bad page scroll scaling factor `%s': %s"
4332 v (Printexc.to_string exn)
4335 src#int "ui font size"
4336 (fun () -> fstate.fontsize)
4337 (fun v -> setfontsize (bound v 5 100));
4338 src#int "hint font size"
4339 (fun () -> conf.hfsize)
4340 (fun v -> conf.hfsize <- bound v 5 100);
4341 colorp "background color"
4342 (fun () -> conf.bgcolor)
4343 (fun v -> conf.bgcolor <- v);
4344 src#bool "crop hack"
4345 (fun () -> conf.crophack)
4346 (fun v -> conf.crophack <- v);
4347 src#bool "multi column centering"
4348 (fun () -> conf.multicenter)
4349 (fun v -> conf.multicenter <- v; represent ());
4350 src#string "trim fuzz"
4351 (fun () -> irect_to_string conf.trimfuzz)
4352 (fun v ->
4354 conf.trimfuzz <- irect_of_string v;
4355 if conf.trimmargins
4356 then settrim true conf.trimfuzz;
4357 with exn ->
4358 state.text <- Printf.sprintf "bad irect `%s': %s"
4359 v (Printexc.to_string exn)
4361 src#string "throttle"
4362 (fun () ->
4363 match conf.maxwait with
4364 | None -> "show place holder if page is not ready"
4365 | Some time ->
4366 if time = infinity
4367 then "wait for page to fully render"
4368 else
4369 "wait " ^ string_of_float time
4370 ^ " seconds before showing placeholder"
4372 (fun v ->
4374 let f = float_of_string v in
4375 if f <= 0.0
4376 then conf.maxwait <- None
4377 else conf.maxwait <- Some f
4378 with exn ->
4379 state.text <- Printf.sprintf "bad time `%s': %s"
4380 v (Printexc.to_string exn)
4382 src#string "ghyll scroll"
4383 (fun () ->
4384 match conf.ghyllscroll with
4385 | None -> ""
4386 | Some nab -> ghyllscroll_to_string nab
4388 (fun v ->
4390 let gs =
4391 if String.length v = 0
4392 then None
4393 else Some (ghyllscroll_of_string v)
4395 conf.ghyllscroll <- gs
4396 with exn ->
4397 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4398 v (Printexc.to_string exn)
4400 src#string "selection command"
4401 (fun () -> conf.selcmd)
4402 (fun v -> conf.selcmd <- v);
4403 src#colorspace "color space"
4404 (fun () -> colorspace_to_string conf.colorspace)
4405 (fun v ->
4406 conf.colorspace <- colorspace_of_int v;
4407 wcmd "cs %d" v;
4408 load state.layout;
4412 sep ();
4413 src#caption "Document" 0;
4414 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4415 src#caption2 "Pages"
4416 (fun () -> string_of_int state.pagecount) 1;
4417 src#caption2 "Dimensions"
4418 (fun () -> string_of_int (List.length state.pdims)) 1;
4419 if conf.trimmargins
4420 then (
4421 sep ();
4422 src#caption "Trimmed margins" 0;
4423 src#caption2 "Dimensions"
4424 (fun () -> string_of_int (List.length state.pdims)) 1;
4427 sep ();
4428 src#caption "OpenGL" 0;
4429 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4430 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4431 src#reset prevmode prevuioh;
4433 fun () ->
4434 state.text <- "";
4435 let prevmode = state.mode
4436 and prevuioh = state.uioh in
4437 fillsrc prevmode prevuioh;
4438 let source = (src :> lvsource) in
4439 let modehash = findkeyhash conf "info" in
4440 state.uioh <- coe (object (self)
4441 inherit listview ~source ~trusted:true ~modehash as super
4442 val mutable m_prevmemused = 0
4443 method infochanged = function
4444 | Memused ->
4445 if m_prevmemused != state.memused
4446 then (
4447 m_prevmemused <- state.memused;
4448 G.postRedisplay "memusedchanged";
4450 | Pdim -> G.postRedisplay "pdimchanged"
4451 | Docinfo -> fillsrc prevmode prevuioh
4453 method key key mask =
4454 if not (Wsi.withctrl mask)
4455 then
4456 match key with
4457 | 0xff51 -> coe (self#updownlevel ~-1)
4458 | 0xff53 -> coe (self#updownlevel 1)
4459 | _ -> super#key key mask
4460 else super#key key mask
4461 end);
4462 G.postRedisplay "info";
4465 let enterhelpmode =
4466 let source =
4467 (object
4468 inherit lvsourcebase
4469 method getitemcount = Array.length state.help
4470 method getitem n =
4471 let s, n, _ = state.help.(n) in
4472 (s, n)
4474 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4475 let optuioh =
4476 if not cancel
4477 then (
4478 m_qsearch <- qsearch;
4479 match state.help.(active) with
4480 | _, _, Action f -> Some (f uioh)
4481 | _ -> Some (uioh)
4483 else None
4485 m_active <- active;
4486 m_first <- first;
4487 m_pan <- pan;
4488 optuioh
4490 method hasaction n =
4491 match state.help.(n) with
4492 | _, _, Action _ -> true
4493 | _ -> false
4495 initializer
4496 m_active <- -1
4497 end)
4498 in fun () ->
4499 let modehash = findkeyhash conf "help" in
4500 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4501 G.postRedisplay "help";
4504 let entermsgsmode =
4505 let msgsource =
4506 let re = Str.regexp "[\r\n]" in
4507 (object
4508 inherit lvsourcebase
4509 val mutable m_items = [||]
4511 method getitemcount = 1 + Array.length m_items
4513 method getitem n =
4514 if n = 0
4515 then "[Clear]", 0
4516 else m_items.(n-1), 0
4518 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4519 ignore uioh;
4520 if not cancel
4521 then (
4522 if active = 0
4523 then Buffer.clear state.errmsgs;
4524 m_qsearch <- qsearch;
4526 m_active <- active;
4527 m_first <- first;
4528 m_pan <- pan;
4529 None
4531 method hasaction n =
4532 n = 0
4534 method reset =
4535 state.newerrmsgs <- false;
4536 let l = Str.split re (Buffer.contents state.errmsgs) in
4537 m_items <- Array.of_list l
4539 initializer
4540 m_active <- 0
4541 end)
4542 in fun () ->
4543 state.text <- "";
4544 msgsource#reset;
4545 let source = (msgsource :> lvsource) in
4546 let modehash = findkeyhash conf "listview" in
4547 state.uioh <- coe (object
4548 inherit listview ~source ~trusted:false ~modehash as super
4549 method display =
4550 if state.newerrmsgs
4551 then msgsource#reset;
4552 super#display
4553 end);
4554 G.postRedisplay "msgs";
4557 let quickbookmark ?title () =
4558 match state.layout with
4559 | [] -> ()
4560 | l :: _ ->
4561 let title =
4562 match title with
4563 | None ->
4564 let sec = Unix.gettimeofday () in
4565 let tm = Unix.localtime sec in
4566 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4567 (l.pageno+1)
4568 tm.Unix.tm_mday
4569 tm.Unix.tm_mon
4570 (tm.Unix.tm_year + 1900)
4571 tm.Unix.tm_hour
4572 tm.Unix.tm_min
4573 | Some title -> title
4575 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4578 let doreshape w h =
4579 state.fullscreen <- None;
4580 Wsi.reshape w h;
4583 let setautoscrollspeed step goingdown =
4584 let incr = max 1 ((abs step) / 2) in
4585 let incr = if goingdown then incr else -incr in
4586 let astep = step + incr in
4587 state.autoscroll <- Some astep;
4590 let gotounder = function
4591 | Ulinkgoto (pageno, top) ->
4592 if pageno >= 0
4593 then (
4594 addnav ();
4595 gotopage1 pageno top;
4598 | Ulinkuri s ->
4599 gotouri s
4601 | Uremote (filename, pageno) ->
4602 let path =
4603 if Sys.file_exists filename
4604 then filename
4605 else
4606 let dir = Filename.dirname state.path in
4607 let path = Filename.concat dir filename in
4608 if Sys.file_exists path
4609 then path
4610 else ""
4612 if String.length path > 0
4613 then (
4614 let anchor = getanchor () in
4615 let ranchor = state.path, state.password, anchor in
4616 state.anchor <- (pageno, 0.0, 0.0);
4617 state.ranchors <- ranchor :: state.ranchors;
4618 opendoc path "";
4620 else showtext '!' ("Could not find " ^ filename)
4622 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4625 let canpan () =
4626 match conf.columns with
4627 | Csplit _ -> true
4628 | _ -> conf.zoom > 1.0
4631 let viewkeyboard key mask =
4632 let enttext te =
4633 let mode = state.mode in
4634 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4635 state.text <- "";
4636 enttext ();
4637 G.postRedisplay "view:enttext"
4639 let ctrl = Wsi.withctrl mask in
4640 match key with
4641 | 81 -> (* Q *)
4642 exit 0
4644 | 0xff63 -> (* insert *)
4645 if conf.angle mod 360 = 0
4646 then (
4647 state.mode <- LinkNav (Ltgendir 0);
4648 gotoy state.y;
4650 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4652 | 0xff1b | 113 -> (* escape / q *)
4653 begin match state.mstate with
4654 | Mzoomrect _ ->
4655 state.mstate <- Mnone;
4656 Wsi.setcursor Wsi.CURSOR_INHERIT;
4657 G.postRedisplay "kill zoom rect";
4658 | _ ->
4659 match state.ranchors with
4660 | [] -> raise Quit
4661 | (path, password, anchor) :: rest ->
4662 state.ranchors <- rest;
4663 state.anchor <- anchor;
4664 opendoc path password
4665 end;
4667 | 0xff08 -> (* backspace *)
4668 let y = getnav ~-1 in
4669 gotoy_and_clear_text y
4671 | 111 -> (* o *)
4672 enteroutlinemode ()
4674 | 117 -> (* u *)
4675 state.rects <- [];
4676 state.text <- "";
4677 G.postRedisplay "dehighlight";
4679 | 47 | 63 -> (* / ? *)
4680 let ondone isforw s =
4681 cbput state.hists.pat s;
4682 state.searchpattern <- s;
4683 search s isforw
4685 let s = String.create 1 in
4686 s.[0] <- Char.chr key;
4687 enttext (s, "", Some (onhist state.hists.pat),
4688 textentry, ondone (key = 47), true)
4690 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4691 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4692 setzoom (conf.zoom +. incr)
4694 | 43 | 0xffab -> (* + *)
4695 let ondone s =
4696 let n =
4697 try int_of_string s with exc ->
4698 state.text <- Printf.sprintf "bad integer `%s': %s"
4699 s (Printexc.to_string exc);
4700 max_int
4702 if n != max_int
4703 then (
4704 conf.pagebias <- n;
4705 state.text <- "page bias is now " ^ string_of_int n;
4708 enttext ("page bias: ", "", None, intentry, ondone, true)
4710 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4711 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4712 setzoom (max 0.01 (conf.zoom -. decr))
4714 | 45 | 0xffad -> (* - *)
4715 let ondone msg = state.text <- msg in
4716 enttext (
4717 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4718 optentry state.mode, ondone, true
4721 | 48 when ctrl -> (* ctrl-0 *)
4722 setzoom 1.0
4724 | 49 when ctrl -> (* ctrl-1 *)
4725 let cols =
4726 match conf.columns with
4727 | Csingle _ | Cmulti _ -> 1
4728 | Csplit (n, _) -> n
4730 let zoom = zoomforh conf.winw conf.winh state.scrollw cols in
4731 if zoom < 1.0
4732 then setzoom zoom
4734 | 0xffc6 -> (* f9 *)
4735 togglebirdseye ()
4737 | 57 when ctrl -> (* ctrl-9 *)
4738 togglebirdseye ()
4740 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4741 when not ctrl -> (* 0..9 *)
4742 let ondone s =
4743 let n =
4744 try int_of_string s with exc ->
4745 state.text <- Printf.sprintf "bad integer `%s': %s"
4746 s (Printexc.to_string exc);
4749 if n >= 0
4750 then (
4751 addnav ();
4752 cbput state.hists.pag (string_of_int n);
4753 gotopage1 (n + conf.pagebias - 1) 0;
4756 let pageentry text key =
4757 match Char.unsafe_chr key with
4758 | 'g' -> TEdone text
4759 | _ -> intentry text key
4761 let text = "x" in text.[0] <- Char.chr key;
4762 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4764 | 98 -> (* b *)
4765 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4766 reshape conf.winw conf.winh;
4768 | 108 -> (* l *)
4769 conf.hlinks <- not conf.hlinks;
4770 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4771 G.postRedisplay "toggle highlightlinks";
4773 | 70 -> (* F *)
4774 state.glinks <- true;
4775 let mode = state.mode in
4776 state.mode <- Textentry (
4777 (":", "", None, linknentry, linkndone (fun under ->
4778 addnav ();
4779 gotounder under
4780 ), false
4781 ), fun _ ->
4782 state.glinks <- false;
4783 state.mode <- mode
4785 state.text <- "";
4786 G.postRedisplay "view:linkent(F)"
4788 | 121 -> (* y *)
4789 state.glinks <- true;
4790 let mode = state.mode in
4791 state.mode <- Textentry (
4792 (":", "", None, linknentry, linkndone (fun under ->
4793 match Ne.pipe () with
4794 | Ne.Exn exn ->
4795 showtext '!' (Printf.sprintf "pipe failed: %s"
4796 (Printexc.to_string exn));
4797 | Ne.Res (r, w) ->
4798 let popened =
4799 try popen conf.selcmd [r, 0; w, -1]; true
4800 with exn ->
4801 showtext '!'
4802 (Printf.sprintf "failed to execute %s: %s"
4803 conf.selcmd (Printexc.to_string exn));
4804 false
4806 let clo cap fd =
4807 Ne.clo fd (fun msg ->
4808 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
4811 let s = undertext under in
4812 if popened
4813 then
4814 (try
4815 let l = String.length s in
4816 let n = Unix.write w s 0 l in
4817 if n != l
4818 then
4819 showtext '!'
4820 (Printf.sprintf
4821 "failed to write %d characters to sel pipe, wrote %d"
4824 with exn ->
4825 showtext '!'
4826 (Printf.sprintf "failed to write to sel pipe: %s"
4827 (Printexc.to_string exn)
4830 else dolog "%s" s;
4831 clo "pipe/r" r;
4832 clo "pipe/w" w;
4833 ), false
4835 fun _ ->
4836 state.glinks <- false;
4837 state.mode <- mode
4839 state.text <- "";
4840 G.postRedisplay "view:linkent"
4842 | 97 -> (* a *)
4843 begin match state.autoscroll with
4844 | Some step ->
4845 conf.autoscrollstep <- step;
4846 state.autoscroll <- None
4847 | None ->
4848 if conf.autoscrollstep = 0
4849 then state.autoscroll <- Some 1
4850 else state.autoscroll <- Some conf.autoscrollstep
4853 | 112 when ctrl -> (* ctrl-p *)
4854 launchpath ()
4856 | 80 -> (* P *)
4857 conf.presentation <- not conf.presentation;
4858 if conf.presentation
4859 then (
4860 if not conf.scrollbarinpm
4861 then state.scrollw <- 0;
4863 else
4864 state.scrollw <- conf.scrollbw;
4866 showtext ' ' ("presentation mode " ^
4867 if conf.presentation then "on" else "off");
4868 state.anchor <- getanchor ();
4869 represent ()
4871 | 102 -> (* f *)
4872 begin match state.fullscreen with
4873 | None ->
4874 state.fullscreen <- Some (conf.winw, conf.winh);
4875 Wsi.fullscreen ()
4876 | Some (w, h) ->
4877 state.fullscreen <- None;
4878 doreshape w h
4881 | 103 -> (* g *)
4882 gotoy_and_clear_text 0
4884 | 71 -> (* G *)
4885 gotopage1 (state.pagecount - 1) 0
4887 | 112 | 78 -> (* p|N *)
4888 search state.searchpattern false
4890 | 110 | 0xffc0 -> (* n|F3 *)
4891 search state.searchpattern true
4893 | 116 -> (* t *)
4894 begin match state.layout with
4895 | [] -> ()
4896 | l :: _ ->
4897 gotoy_and_clear_text (getpagey l.pageno)
4900 | 32 -> (* space *)
4901 begin match state.layout with
4902 | [] -> ()
4903 | l :: rest ->
4904 match conf.columns with
4905 | Csingle _ | Cmulti _ ->
4906 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4907 then
4908 let y = clamp (pgscale conf.winh) in
4909 gotoy_and_clear_text y
4910 else
4911 let pageno = min (l.pageno+1) (state.pagecount-1) in
4912 gotoy_and_clear_text (getpagey pageno)
4913 | Csplit (n, _) ->
4914 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4915 then
4916 let pagey, pageh = getpageyh l.pageno in
4917 let pagey = pagey + pageh * l.pagecol in
4918 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4919 gotoy_and_clear_text (pagey + pageh + ips)
4922 | 0xff9f | 0xffff -> (* delete *)
4923 begin match state.layout with
4924 | [] -> ()
4925 | l :: _ ->
4926 match conf.columns with
4927 | Csingle _ | Cmulti _ ->
4928 if conf.presentation && l.pagey != 0
4929 then
4930 gotoy_and_clear_text (clamp (pgscale ~-(conf.winh)))
4931 else
4932 let pageno = max 0 (l.pageno-1) in
4933 gotoy_and_clear_text (getpagey pageno)
4934 | Csplit (n, _) ->
4935 let y =
4936 if l.pagecol = 0
4937 then
4938 if l.pageno = 0
4939 then l.pagey
4940 else
4941 let pageno = max 0 (l.pageno-1) in
4942 let pagey, pageh = getpageyh pageno in
4943 pagey + (n-1)*pageh
4944 else
4945 let pagey, pageh = getpageyh l.pageno in
4946 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4948 gotoy_and_clear_text y
4951 | 61 -> (* = *)
4952 showtext ' ' (describe_location ());
4954 | 119 -> (* w *)
4955 begin match state.layout with
4956 | [] -> ()
4957 | l :: _ ->
4958 doreshape (l.pagew + state.scrollw) l.pageh;
4959 G.postRedisplay "w"
4962 | 39 -> (* ' *)
4963 enterbookmarkmode ()
4965 | 104 | 0xffbe -> (* h|F1 *)
4966 enterhelpmode ()
4968 | 105 -> (* i *)
4969 enterinfomode ()
4971 | 101 when conf.redirectstderr -> (* e *)
4972 entermsgsmode ()
4974 | 109 -> (* m *)
4975 let ondone s =
4976 match state.layout with
4977 | l :: _ -> state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
4978 | _ -> ()
4980 enttext ("bookmark: ", "", None, textentry, ondone, true)
4982 | 126 -> (* ~ *)
4983 quickbookmark ();
4984 showtext ' ' "Quick bookmark added";
4986 | 122 -> (* z *)
4987 begin match state.layout with
4988 | l :: _ ->
4989 let rect = getpdimrect l.pagedimno in
4990 let w, h =
4991 if conf.crophack
4992 then
4993 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4994 truncate (1.2 *. (rect.(3) -. rect.(0))))
4995 else
4996 (truncate (rect.(1) -. rect.(0)),
4997 truncate (rect.(3) -. rect.(0)))
4999 let w = truncate ((float w)*.conf.zoom)
5000 and h = truncate ((float h)*.conf.zoom) in
5001 if w != 0 && h != 0
5002 then (
5003 state.anchor <- getanchor ();
5004 doreshape (w + state.scrollw) (h + conf.interpagespace)
5006 G.postRedisplay "z";
5008 | [] -> ()
5011 | 50 when ctrl -> (* ctrl-2 *)
5012 let maxw = getmaxw () in
5013 if maxw > 0.0
5014 then setzoom (maxw /. float conf.winw)
5016 | 60 | 62 -> (* < > *)
5017 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5019 | 91 | 93 -> (* [ ] *)
5020 conf.colorscale <-
5021 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5023 G.postRedisplay "brightness";
5025 | 99 when state.mode = View -> (* c *)
5026 let (c, a, b), z =
5027 match state.prevcolumns with
5028 | None -> (1, 0, 0), 1.0
5029 | Some (columns, z) ->
5030 let cab =
5031 match columns with
5032 | Csplit (c, _) -> -c, 0, 0
5033 | Cmulti ((c, a, b), _) -> c, a, b
5034 | Csingle _ -> 1, 0, 0
5036 cab, z
5038 setcolumns View c a b;
5039 setzoom z;
5041 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5042 setzoom state.prevzoom
5044 | 107 | 0xff52 -> (* k up *)
5045 begin match state.autoscroll with
5046 | None ->
5047 begin match state.mode with
5048 | Birdseye beye -> upbirdseye 1 beye
5049 | _ ->
5050 if ctrl
5051 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5052 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5054 | Some n ->
5055 setautoscrollspeed n false
5058 | 106 | 0xff54 -> (* j down *)
5059 begin match state.autoscroll with
5060 | None ->
5061 begin match state.mode with
5062 | Birdseye beye -> downbirdseye 1 beye
5063 | _ ->
5064 if ctrl
5065 then gotoy_and_clear_text (clamp (conf.winh/2))
5066 else gotoy_and_clear_text (clamp conf.scrollstep)
5068 | Some n ->
5069 setautoscrollspeed n true
5072 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5073 if canpan ()
5074 then
5075 let dx =
5076 if ctrl
5077 then conf.winw / 2
5078 else 10
5080 let dx = if key = 0xff51 then dx else -dx in
5081 state.x <- state.x + dx;
5082 gotoy_and_clear_text state.y
5083 else (
5084 state.text <- "";
5085 G.postRedisplay "lef/right"
5088 | 0xff55 -> (* prior *)
5089 let y =
5090 if ctrl
5091 then
5092 match state.layout with
5093 | [] -> state.y
5094 | l :: _ -> state.y - l.pagey
5095 else
5096 clamp (pgscale (-conf.winh))
5098 gotoghyll y
5100 | 0xff56 -> (* next *)
5101 let y =
5102 if ctrl
5103 then
5104 match List.rev state.layout with
5105 | [] -> state.y
5106 | l :: _ -> getpagey l.pageno
5107 else
5108 clamp (pgscale conf.winh)
5110 gotoghyll y
5112 | 0xff50 -> (* home *)
5113 gotoghyll 0
5114 | 0xff57 -> (* end *)
5115 gotoghyll (clamp state.maxy)
5116 | 0xff53 when Wsi.withalt mask -> (* right *)
5117 gotoghyll (getnav ~-1)
5118 | 0xff51 when Wsi.withalt mask -> (* left *)
5119 gotoghyll (getnav 1)
5121 | 114 -> (* r *)
5122 state.anchor <- getanchor ();
5123 opendoc state.path state.password
5125 | 118 when conf.debug -> (* v *)
5126 state.rects <- [];
5127 List.iter (fun l ->
5128 match getopaque l.pageno with
5129 | None -> ()
5130 | Some opaque ->
5131 let x0, y0, x1, y1 = pagebbox opaque in
5132 let a,b = float x0, float y0 in
5133 let c,d = float x1, float y0 in
5134 let e,f = float x1, float y1 in
5135 let h,j = float x0, float y1 in
5136 let rect = (a,b,c,d,e,f,h,j) in
5137 debugrect rect;
5138 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5139 ) state.layout;
5140 G.postRedisplay "v";
5142 | _ ->
5143 vlog "huh? %s" (Wsi.keyname key)
5146 let linknavkeyboard key mask linknav =
5147 let getpage pageno =
5148 let rec loop = function
5149 | [] -> None
5150 | l :: _ when l.pageno = pageno -> Some l
5151 | _ :: rest -> loop rest
5152 in loop state.layout
5154 let doexact (pageno, n) =
5155 match getopaque pageno, getpage pageno with
5156 | Some opaque, Some l ->
5157 if key = 0xff0d
5158 then
5159 let under = getlink opaque n in
5160 G.postRedisplay "link gotounder";
5161 gotounder under;
5162 state.mode <- View;
5163 else
5164 let opt, dir =
5165 match key with
5166 | 0xff50 -> (* home *)
5167 Some (findlink opaque LDfirst), -1
5169 | 0xff57 -> (* end *)
5170 Some (findlink opaque LDlast), 1
5172 | 0xff51 -> (* left *)
5173 Some (findlink opaque (LDleft n)), -1
5175 | 0xff53 -> (* right *)
5176 Some (findlink opaque (LDright n)), 1
5178 | 0xff52 -> (* up *)
5179 Some (findlink opaque (LDup n)), -1
5181 | 0xff54 -> (* down *)
5182 Some (findlink opaque (LDdown n)), 1
5184 | _ -> None, 0
5186 let pwl l dir =
5187 begin match findpwl l.pageno dir with
5188 | Pwlnotfound -> ()
5189 | Pwl pageno ->
5190 let notfound dir =
5191 state.mode <- LinkNav (Ltgendir dir);
5192 let y, h = getpageyh pageno in
5193 let y =
5194 if dir < 0
5195 then y + h - conf.winh
5196 else y
5198 gotoy y
5200 begin match getopaque pageno, getpage pageno with
5201 | Some opaque, Some _ ->
5202 let link =
5203 let ld = if dir > 0 then LDfirst else LDlast in
5204 findlink opaque ld
5206 begin match link with
5207 | Lfound m ->
5208 showlinktype (getlink opaque m);
5209 state.mode <- LinkNav (Ltexact (pageno, m));
5210 G.postRedisplay "linknav jpage";
5211 | _ -> notfound dir
5212 end;
5213 | _ -> notfound dir
5214 end;
5215 end;
5217 begin match opt with
5218 | Some Lnotfound -> pwl l dir;
5219 | Some (Lfound m) ->
5220 if m = n
5221 then pwl l dir
5222 else (
5223 let _, y0, _, y1 = getlinkrect opaque m in
5224 if y0 < l.pagey
5225 then gotopage1 l.pageno y0
5226 else (
5227 let d = fstate.fontsize + 1 in
5228 if y1 - l.pagey > l.pagevh - d
5229 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5230 else G.postRedisplay "linknav";
5232 showlinktype (getlink opaque m);
5233 state.mode <- LinkNav (Ltexact (l.pageno, m));
5236 | None -> viewkeyboard key mask
5237 end;
5238 | _ -> viewkeyboard key mask
5240 if key = 0xff63
5241 then (
5242 state.mode <- View;
5243 G.postRedisplay "leave linknav"
5245 else
5246 match linknav with
5247 | Ltgendir _ -> viewkeyboard key mask
5248 | Ltexact exact -> doexact exact
5251 let keyboard key mask =
5252 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5253 then wcmd "interrupt"
5254 else state.uioh <- state.uioh#key key mask
5257 let birdseyekeyboard key mask
5258 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5259 let incr =
5260 match conf.columns with
5261 | Csingle _ -> 1
5262 | Cmulti ((c, _, _), _) -> c
5263 | Csplit _ -> failwith "bird's eye split mode"
5265 let pgh layout = List.fold_left (fun m l -> max l.pageh m) conf.winh layout in
5266 match key with
5267 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5268 let y, h = getpageyh pageno in
5269 let top = (conf.winh - h) / 2 in
5270 gotoy (max 0 (y - top))
5271 | 0xff0d -> leavebirdseye beye false
5272 | 0xff1b -> leavebirdseye beye true (* escape *)
5273 | 0xff52 -> upbirdseye incr beye (* up *)
5274 | 0xff54 -> downbirdseye incr beye (* down *)
5275 | 0xff51 -> upbirdseye 1 beye (* left *)
5276 | 0xff53 -> downbirdseye 1 beye (* right *)
5278 | 0xff55 -> (* prior *)
5279 begin match state.layout with
5280 | l :: _ ->
5281 if l.pagey != 0
5282 then (
5283 state.mode <- Birdseye (
5284 oconf, leftx, l.pageno, hooverpageno, anchor
5286 gotopage1 l.pageno 0;
5288 else (
5289 let layout = layout (state.y-conf.winh) (pgh state.layout) in
5290 match layout with
5291 | [] -> gotoy (clamp (-conf.winh))
5292 | l :: _ ->
5293 state.mode <- Birdseye (
5294 oconf, leftx, l.pageno, hooverpageno, anchor
5296 gotopage1 l.pageno 0
5299 | [] -> gotoy (clamp (-conf.winh))
5300 end;
5302 | 0xff56 -> (* next *)
5303 begin match List.rev state.layout with
5304 | l :: _ ->
5305 let layout = layout (state.y + (pgh state.layout)) conf.winh in
5306 begin match layout with
5307 | [] ->
5308 let incr = l.pageh - l.pagevh in
5309 if incr = 0
5310 then (
5311 state.mode <-
5312 Birdseye (
5313 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5315 G.postRedisplay "birdseye pagedown";
5317 else gotoy (clamp (incr + conf.interpagespace*2));
5319 | l :: _ ->
5320 state.mode <-
5321 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5322 gotopage1 l.pageno 0;
5325 | [] -> gotoy (clamp conf.winh)
5326 end;
5328 | 0xff50 -> (* home *)
5329 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5330 gotopage1 0 0
5332 | 0xff57 -> (* end *)
5333 let pageno = state.pagecount - 1 in
5334 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5335 if not (pagevisible state.layout pageno)
5336 then
5337 let h =
5338 match List.rev state.pdims with
5339 | [] -> conf.winh
5340 | (_, _, h, _) :: _ -> h
5342 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5343 else G.postRedisplay "birdseye end";
5344 | _ -> viewkeyboard key mask
5347 let drawpage l linkindexbase =
5348 let color =
5349 match state.mode with
5350 | Textentry _ -> scalecolor 0.4
5351 | LinkNav _
5352 | View -> scalecolor 1.0
5353 | Birdseye (_, _, pageno, hooverpageno, _) ->
5354 if l.pageno = hooverpageno
5355 then scalecolor 0.9
5356 else (
5357 if l.pageno = pageno
5358 then scalecolor 1.0
5359 else scalecolor 0.8
5362 drawtiles l color;
5363 begin match getopaque l.pageno with
5364 | Some opaque ->
5365 if tileready l l.pagex l.pagey
5366 then
5367 let x = l.pagedispx - l.pagex
5368 and y = l.pagedispy - l.pagey in
5369 let hlmask =
5370 match conf.columns with
5371 | Csingle _ | Cmulti _ ->
5372 (if conf.hlinks then 1 else 0)
5373 + (if state.glinks
5374 && not (isbirdseye state.mode) then 2 else 0)
5375 | _ -> 0
5377 let s =
5378 match state.mode with
5379 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5380 | _ -> ""
5382 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5383 else 0
5385 | _ -> 0
5386 end;
5389 let scrollindicator () =
5390 let sbw, ph, sh = state.uioh#scrollph in
5391 let sbh, pw, sw = state.uioh#scrollpw in
5393 GlDraw.color (0.64, 0.64, 0.64);
5394 GlDraw.rect
5395 (float (conf.winw - sbw), 0.)
5396 (float conf.winw, float conf.winh)
5398 GlDraw.rect
5399 (0., float (conf.winh - sbh))
5400 (float (conf.winw - state.scrollw - 1), float conf.winh)
5402 GlDraw.color (0.0, 0.0, 0.0);
5404 GlDraw.rect
5405 (float (conf.winw - sbw), ph)
5406 (float conf.winw, ph +. sh)
5408 GlDraw.rect
5409 (pw, float (conf.winh - sbh))
5410 (pw +. sw, float conf.winh)
5414 let showsel () =
5415 match state.mstate with
5416 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5419 | Msel ((x0, y0), (x1, y1)) ->
5420 let rec loop = function
5421 | l :: ls ->
5422 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5423 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5424 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5425 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5426 then
5427 match getopaque l.pageno with
5428 | Some opaque ->
5429 let x0, y0 = pagetranslatepoint l x0 y0 in
5430 let x1, y1 = pagetranslatepoint l x1 y1 in
5431 seltext opaque (x0, y0, x1, y1);
5432 | _ -> ()
5433 else loop ls
5434 | [] -> ()
5436 loop state.layout
5439 let showrects rects =
5440 Gl.enable `blend;
5441 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5442 GlDraw.polygon_mode `both `fill;
5443 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5444 List.iter
5445 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5446 List.iter (fun l ->
5447 if l.pageno = pageno
5448 then (
5449 let dx = float (l.pagedispx - l.pagex) in
5450 let dy = float (l.pagedispy - l.pagey) in
5451 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5452 GlDraw.begins `quads;
5454 GlDraw.vertex2 (x0+.dx, y0+.dy);
5455 GlDraw.vertex2 (x1+.dx, y1+.dy);
5456 GlDraw.vertex2 (x2+.dx, y2+.dy);
5457 GlDraw.vertex2 (x3+.dx, y3+.dy);
5459 GlDraw.ends ();
5461 ) state.layout
5462 ) rects
5464 Gl.disable `blend;
5467 let display () =
5468 GlClear.color (scalecolor2 conf.bgcolor);
5469 GlClear.clear [`color];
5470 let rec loop linkindexbase = function
5471 | l :: rest ->
5472 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5473 loop linkindexbase rest
5474 | [] -> ()
5476 loop 0 state.layout;
5477 let rects =
5478 match state.mode with
5479 | LinkNav (Ltexact (pageno, linkno)) ->
5480 begin match getopaque pageno with
5481 | Some opaque ->
5482 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5483 (pageno, 5, (
5484 float x0, float y0,
5485 float x1, float y0,
5486 float x1, float y1,
5487 float x0, float y1)
5488 ) :: state.rects
5489 | None -> state.rects
5491 | _ -> state.rects
5493 showrects rects;
5494 showsel ();
5495 state.uioh#display;
5496 begin match state.mstate with
5497 | Mzoomrect ((x0, y0), (x1, y1)) ->
5498 Gl.enable `blend;
5499 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5500 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5501 GlDraw.rect (float x0, float y0)
5502 (float x1, float y1);
5503 Gl.disable `blend;
5504 | _ -> ()
5505 end;
5506 enttext ();
5507 scrollindicator ();
5508 Wsi.swapb ();
5511 let zoomrect x y x1 y1 =
5512 let x0 = min x x1
5513 and x1 = max x x1
5514 and y0 = min y y1 in
5515 gotoy (state.y + y0);
5516 state.anchor <- getanchor ();
5517 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5518 let margin =
5519 if state.w < conf.winw - state.scrollw
5520 then (conf.winw - state.scrollw - state.w) / 2
5521 else 0
5523 state.x <- (state.x + margin) - x0;
5524 setzoom zoom;
5525 Wsi.setcursor Wsi.CURSOR_INHERIT;
5526 state.mstate <- Mnone;
5529 let scrollx x =
5530 let winw = conf.winw - state.scrollw - 1 in
5531 let s = float x /. float winw in
5532 let destx = truncate (float (state.w + winw) *. s) in
5533 state.x <- winw - destx;
5534 gotoy_and_clear_text state.y;
5535 state.mstate <- Mscrollx;
5538 let scrolly y =
5539 let s = float y /. float conf.winh in
5540 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5541 gotoy_and_clear_text desty;
5542 state.mstate <- Mscrolly;
5545 let viewmouse button down x y mask =
5546 match button with
5547 | n when (n == 4 || n == 5) && not down ->
5548 if Wsi.withctrl mask
5549 then (
5550 match state.mstate with
5551 | Mzoom (oldn, i) ->
5552 if oldn = n
5553 then (
5554 if i = 2
5555 then
5556 let incr =
5557 match n with
5558 | 5 ->
5559 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5560 | _ ->
5561 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5563 let zoom = conf.zoom -. incr in
5564 setzoom zoom;
5565 state.mstate <- Mzoom (n, 0);
5566 else
5567 state.mstate <- Mzoom (n, i+1);
5569 else state.mstate <- Mzoom (n, 0)
5571 | _ -> state.mstate <- Mzoom (n, 0)
5573 else (
5574 match state.autoscroll with
5575 | Some step -> setautoscrollspeed step (n=4)
5576 | None ->
5577 let incr =
5578 if n = 4
5579 then -conf.scrollstep
5580 else conf.scrollstep
5582 let incr = incr * 2 in
5583 let y = clamp incr in
5584 gotoy_and_clear_text y
5587 | n when (n = 6 || n = 7) && not down && canpan () ->
5588 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5589 gotoy_and_clear_text state.y
5591 | 1 when Wsi.withctrl mask ->
5592 if down
5593 then (
5594 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5595 state.mstate <- Mpan (x, y)
5597 else
5598 state.mstate <- Mnone
5600 | 3 ->
5601 if down
5602 then (
5603 Wsi.setcursor Wsi.CURSOR_CYCLE;
5604 let p = (x, y) in
5605 state.mstate <- Mzoomrect (p, p)
5607 else (
5608 match state.mstate with
5609 | Mzoomrect ((x0, y0), _) ->
5610 if abs (x-x0) > 10 && abs (y - y0) > 10
5611 then zoomrect x0 y0 x y
5612 else (
5613 state.mstate <- Mnone;
5614 Wsi.setcursor Wsi.CURSOR_INHERIT;
5615 G.postRedisplay "kill accidental zoom rect";
5617 | _ ->
5618 Wsi.setcursor Wsi.CURSOR_INHERIT;
5619 state.mstate <- Mnone
5622 | 1 when x > conf.winw - state.scrollw ->
5623 if down
5624 then
5625 let _, position, sh = state.uioh#scrollph in
5626 if y > truncate position && y < truncate (position +. sh)
5627 then state.mstate <- Mscrolly
5628 else scrolly y
5629 else
5630 state.mstate <- Mnone
5632 | 1 when y > conf.winh - state.hscrollh ->
5633 if down
5634 then
5635 let _, position, sw = state.uioh#scrollpw in
5636 if x > truncate position && x < truncate (position +. sw)
5637 then state.mstate <- Mscrollx
5638 else scrollx x
5639 else
5640 state.mstate <- Mnone
5642 | 1 ->
5643 let dest = if down then getunder x y else Unone in
5644 begin match dest with
5645 | Ulinkgoto _
5646 | Ulinkuri _
5647 | Uremote _
5648 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5649 gotounder dest
5651 | Unone when down ->
5652 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5653 state.mstate <- Mpan (x, y);
5655 | Unone | Utext _ ->
5656 if down
5657 then (
5658 if conf.angle mod 360 = 0
5659 then (
5660 state.mstate <- Msel ((x, y), (x, y));
5661 G.postRedisplay "mouse select";
5664 else (
5665 match state.mstate with
5666 | Mnone -> ()
5668 | Mzoom _ | Mscrollx | Mscrolly ->
5669 state.mstate <- Mnone
5671 | Mzoomrect ((x0, y0), _) ->
5672 zoomrect x0 y0 x y
5674 | Mpan _ ->
5675 Wsi.setcursor Wsi.CURSOR_INHERIT;
5676 state.mstate <- Mnone
5678 | Msel ((_, y0), (_, y1)) ->
5679 let rec loop = function
5680 | [] -> ()
5681 | l :: rest ->
5682 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5683 || ((y1 >= l.pagedispy
5684 && y1 <= (l.pagedispy + l.pagevh)))
5685 then
5686 match getopaque l.pageno with
5687 | Some opaque ->
5688 begin
5689 match Ne.pipe () with
5690 | Ne.Exn exn ->
5691 showtext '!'
5692 (Printf.sprintf
5693 "can not create sel pipe: %s"
5694 (Printexc.to_string exn));
5695 | Ne.Res (r, w) ->
5696 let doclose what fd =
5697 Ne.clo fd (fun msg ->
5698 dolog "%s close failed: %s" what msg)
5701 popen conf.selcmd [r, 0; w, -1];
5702 copysel w opaque;
5703 doclose "pipe/r" r;
5704 G.postRedisplay "copysel";
5705 with exn ->
5706 dolog "can not execute %S: %s"
5707 conf.selcmd (Printexc.to_string exn);
5708 doclose "pipe/r" r;
5709 doclose "pipe/w" w;
5711 | None -> ()
5712 else loop rest
5714 loop state.layout;
5715 Wsi.setcursor Wsi.CURSOR_INHERIT;
5716 state.mstate <- Mnone;
5720 | _ -> ()
5723 let birdseyemouse button down x y mask
5724 (conf, leftx, _, hooverpageno, anchor) =
5725 match button with
5726 | 1 when down ->
5727 let rec loop = function
5728 | [] -> ()
5729 | l :: rest ->
5730 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5731 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5732 then (
5733 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5735 else loop rest
5737 loop state.layout
5738 | 3 -> ()
5739 | _ -> viewmouse button down x y mask
5742 let mouse button down x y mask =
5743 state.uioh <- state.uioh#button button down x y mask;
5746 let motion ~x ~y =
5747 state.uioh <- state.uioh#motion x y
5750 let pmotion ~x ~y =
5751 state.uioh <- state.uioh#pmotion x y;
5754 let uioh = object
5755 method display = ()
5757 method key key mask =
5758 begin match state.mode with
5759 | Textentry textentry -> textentrykeyboard key mask textentry
5760 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5761 | View -> viewkeyboard key mask
5762 | LinkNav linknav -> linknavkeyboard key mask linknav
5763 end;
5764 state.uioh
5766 method button button bstate x y mask =
5767 begin match state.mode with
5768 | LinkNav _
5769 | View -> viewmouse button bstate x y mask
5770 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5771 | Textentry _ -> ()
5772 end;
5773 state.uioh
5775 method motion x y =
5776 begin match state.mode with
5777 | Textentry _ -> ()
5778 | View | Birdseye _ | LinkNav _ ->
5779 match state.mstate with
5780 | Mzoom _ | Mnone -> ()
5782 | Mpan (x0, y0) ->
5783 let dx = x - x0
5784 and dy = y0 - y in
5785 state.mstate <- Mpan (x, y);
5786 if canpan ()
5787 then state.x <- state.x + dx;
5788 let y = clamp dy in
5789 gotoy_and_clear_text y
5791 | Msel (a, _) ->
5792 state.mstate <- Msel (a, (x, y));
5793 G.postRedisplay "motion select";
5795 | Mscrolly ->
5796 let y = min conf.winh (max 0 y) in
5797 scrolly y
5799 | Mscrollx ->
5800 let x = min conf.winw (max 0 x) in
5801 scrollx x
5803 | Mzoomrect (p0, _) ->
5804 state.mstate <- Mzoomrect (p0, (x, y));
5805 G.postRedisplay "motion zoomrect";
5806 end;
5807 state.uioh
5809 method pmotion x y =
5810 begin match state.mode with
5811 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5812 let rec loop = function
5813 | [] ->
5814 if hooverpageno != -1
5815 then (
5816 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5817 G.postRedisplay "pmotion birdseye no hoover";
5819 | l :: rest ->
5820 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5821 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5822 then (
5823 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5824 G.postRedisplay "pmotion birdseye hoover";
5826 else loop rest
5828 loop state.layout
5830 | Textentry _ -> ()
5832 | LinkNav _
5833 | View ->
5834 match state.mstate with
5835 | Mnone -> updateunder x y
5836 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5838 end;
5839 state.uioh
5841 method infochanged _ = ()
5843 method scrollph =
5844 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5845 let p, h = scrollph state.y maxy in
5846 state.scrollw, p, h
5848 method scrollpw =
5849 let winw = conf.winw - state.scrollw - 1 in
5850 let fwinw = float winw in
5851 let sw =
5852 let sw = fwinw /. float state.w in
5853 let sw = fwinw *. sw in
5854 max sw (float conf.scrollh)
5856 let position, sw =
5857 let f = state.w+winw in
5858 let r = float (winw-state.x) /. float f in
5859 let p = fwinw *. r in
5860 p-.sw/.2., sw
5862 let sw =
5863 if position +. sw > fwinw
5864 then fwinw -. position
5865 else sw
5867 state.hscrollh, position, sw
5869 method modehash =
5870 let modename =
5871 match state.mode with
5872 | LinkNav _ -> "links"
5873 | Textentry _ -> "textentry"
5874 | Birdseye _ -> "birdseye"
5875 | View -> "view"
5877 findkeyhash conf modename
5878 end;;
5880 module Config =
5881 struct
5882 open Parser
5884 let fontpath = ref "";;
5886 module KeyMap =
5887 Map.Make (struct type t = (int * int) let compare = compare end);;
5889 let unent s =
5890 let l = String.length s in
5891 let b = Buffer.create l in
5892 unent b s 0 l;
5893 Buffer.contents b;
5896 let home =
5897 try Sys.getenv "HOME"
5898 with exn ->
5899 prerr_endline
5900 ("Can not determine home directory location: " ^
5901 Printexc.to_string exn);
5905 let modifier_of_string = function
5906 | "alt" -> Wsi.altmask
5907 | "shift" -> Wsi.shiftmask
5908 | "ctrl" | "control" -> Wsi.ctrlmask
5909 | "meta" -> Wsi.metamask
5910 | _ -> 0
5913 let key_of_string =
5914 let r = Str.regexp "-" in
5915 fun s ->
5916 let elems = Str.full_split r s in
5917 let f n k m =
5918 let g s =
5919 let m1 = modifier_of_string s in
5920 if m1 = 0
5921 then (Wsi.namekey s, m)
5922 else (k, m lor m1)
5923 in function
5924 | Str.Delim s when n land 1 = 0 -> g s
5925 | Str.Text s -> g s
5926 | Str.Delim _ -> (k, m)
5928 let rec loop n k m = function
5929 | [] -> (k, m)
5930 | x :: xs ->
5931 let k, m = f n k m x in
5932 loop (n+1) k m xs
5934 loop 0 0 0 elems
5937 let keys_of_string =
5938 let r = Str.regexp "[ \t]" in
5939 fun s ->
5940 let elems = Str.split r s in
5941 List.map key_of_string elems
5944 let copykeyhashes c =
5945 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5948 let config_of c attrs =
5949 let apply c k v =
5951 match k with
5952 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5953 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5954 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5955 | "preload" -> { c with preload = bool_of_string v }
5956 | "page-bias" -> { c with pagebias = int_of_string v }
5957 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5958 | "horizontal-scroll-step" ->
5959 { c with hscrollstep = max (int_of_string v) 1 }
5960 | "auto-scroll-step" ->
5961 { c with autoscrollstep = max 0 (int_of_string v) }
5962 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5963 | "crop-hack" -> { c with crophack = bool_of_string v }
5964 | "throttle" ->
5965 let mw =
5966 match String.lowercase v with
5967 | "true" -> Some infinity
5968 | "false" -> None
5969 | f -> Some (float_of_string f)
5971 { c with maxwait = mw}
5972 | "highlight-links" -> { c with hlinks = bool_of_string v }
5973 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5974 | "vertical-margin" ->
5975 { c with interpagespace = max 0 (int_of_string v) }
5976 | "zoom" ->
5977 let zoom = float_of_string v /. 100. in
5978 let zoom = max zoom 0.0 in
5979 { c with zoom = zoom }
5980 | "presentation" -> { c with presentation = bool_of_string v }
5981 | "rotation-angle" -> { c with angle = int_of_string v }
5982 | "width" -> { c with winw = max 20 (int_of_string v) }
5983 | "height" -> { c with winh = max 20 (int_of_string v) }
5984 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5985 | "proportional-display" -> { c with proportional = bool_of_string v }
5986 | "pixmap-cache-size" ->
5987 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5988 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5989 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5990 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5991 | "persistent-location" -> { c with jumpback = bool_of_string v }
5992 | "background-color" -> { c with bgcolor = color_of_string v }
5993 | "scrollbar-in-presentation" ->
5994 { c with scrollbarinpm = bool_of_string v }
5995 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5996 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5997 | "mupdf-store-size" ->
5998 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5999 | "checkers" -> { c with checkers = bool_of_string v }
6000 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6001 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6002 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6003 | "uri-launcher" -> { c with urilauncher = unent v }
6004 | "path-launcher" -> { c with pathlauncher = unent v }
6005 | "color-space" -> { c with colorspace = colorspace_of_string v }
6006 | "invert-colors" -> { c with invert = bool_of_string v }
6007 | "brightness" -> { c with colorscale = float_of_string v }
6008 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6009 | "ghyllscroll" ->
6010 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6011 | "columns" ->
6012 let (n, _, _) as nab = multicolumns_of_string v in
6013 if n < 0
6014 then { c with columns = Csplit (-n, [||]) }
6015 else { c with columns = Cmulti (nab, [||]) }
6016 | "birds-eye-columns" ->
6017 { c with beyecolumns = Some (max (int_of_string v) 2) }
6018 | "selection-command" -> { c with selcmd = unent v }
6019 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6020 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6021 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6022 | "multi-column-centering" -> { c with multicenter = bool_of_string v }
6023 | _ -> c
6024 with exn ->
6025 prerr_endline ("Error processing attribute (`" ^
6026 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
6029 let rec fold c = function
6030 | [] -> c
6031 | (k, v) :: rest ->
6032 let c = apply c k v in
6033 fold c rest
6035 fold { c with keyhashes = copykeyhashes c } attrs;
6038 let fromstring f pos n v d =
6039 try f v
6040 with exn ->
6041 dolog "Error processing attribute (%S=%S) at %d\n%s"
6042 n v pos (Printexc.to_string exn)
6047 let bookmark_of attrs =
6048 let rec fold title page rely visy = function
6049 | ("title", v) :: rest -> fold v page rely visy rest
6050 | ("page", v) :: rest -> fold title v rely visy rest
6051 | ("rely", v) :: rest -> fold title page v visy rest
6052 | ("visy", v) :: rest -> fold title page rely v rest
6053 | _ :: rest -> fold title page rely visy rest
6054 | [] -> title, page, rely, visy
6056 fold "invalid" "0" "0" "0" attrs
6059 let doc_of attrs =
6060 let rec fold path page rely pan visy = function
6061 | ("path", v) :: rest -> fold v page rely pan visy rest
6062 | ("page", v) :: rest -> fold path v rely pan visy rest
6063 | ("rely", v) :: rest -> fold path page v pan visy rest
6064 | ("pan", v) :: rest -> fold path page rely v visy rest
6065 | ("visy", v) :: rest -> fold path page rely pan v rest
6066 | _ :: rest -> fold path page rely pan visy rest
6067 | [] -> path, page, rely, pan, visy
6069 fold "" "0" "0" "0" "0" attrs
6072 let map_of attrs =
6073 let rec fold rs ls = function
6074 | ("out", v) :: rest -> fold v ls rest
6075 | ("in", v) :: rest -> fold rs v rest
6076 | _ :: rest -> fold ls rs rest
6077 | [] -> ls, rs
6079 fold "" "" attrs
6082 let setconf dst src =
6083 dst.scrollbw <- src.scrollbw;
6084 dst.scrollh <- src.scrollh;
6085 dst.icase <- src.icase;
6086 dst.preload <- src.preload;
6087 dst.pagebias <- src.pagebias;
6088 dst.verbose <- src.verbose;
6089 dst.scrollstep <- src.scrollstep;
6090 dst.maxhfit <- src.maxhfit;
6091 dst.crophack <- src.crophack;
6092 dst.autoscrollstep <- src.autoscrollstep;
6093 dst.maxwait <- src.maxwait;
6094 dst.hlinks <- src.hlinks;
6095 dst.underinfo <- src.underinfo;
6096 dst.interpagespace <- src.interpagespace;
6097 dst.zoom <- src.zoom;
6098 dst.presentation <- src.presentation;
6099 dst.angle <- src.angle;
6100 dst.winw <- src.winw;
6101 dst.winh <- src.winh;
6102 dst.savebmarks <- src.savebmarks;
6103 dst.memlimit <- src.memlimit;
6104 dst.proportional <- src.proportional;
6105 dst.texcount <- src.texcount;
6106 dst.sliceheight <- src.sliceheight;
6107 dst.thumbw <- src.thumbw;
6108 dst.jumpback <- src.jumpback;
6109 dst.bgcolor <- src.bgcolor;
6110 dst.scrollbarinpm <- src.scrollbarinpm;
6111 dst.tilew <- src.tilew;
6112 dst.tileh <- src.tileh;
6113 dst.mustoresize <- src.mustoresize;
6114 dst.checkers <- src.checkers;
6115 dst.aalevel <- src.aalevel;
6116 dst.trimmargins <- src.trimmargins;
6117 dst.trimfuzz <- src.trimfuzz;
6118 dst.urilauncher <- src.urilauncher;
6119 dst.colorspace <- src.colorspace;
6120 dst.invert <- src.invert;
6121 dst.colorscale <- src.colorscale;
6122 dst.redirectstderr <- src.redirectstderr;
6123 dst.ghyllscroll <- src.ghyllscroll;
6124 dst.columns <- src.columns;
6125 dst.beyecolumns <- src.beyecolumns;
6126 dst.selcmd <- src.selcmd;
6127 dst.updatecurs <- src.updatecurs;
6128 dst.pathlauncher <- src.pathlauncher;
6129 dst.keyhashes <- copykeyhashes src;
6130 dst.hfsize <- src.hfsize;
6131 dst.hscrollstep <- src.hscrollstep;
6132 dst.pgscale <- src.pgscale;
6133 dst.multicenter <- src.multicenter;
6136 let get s =
6137 let h = Hashtbl.create 10 in
6138 let dc = { defconf with angle = defconf.angle } in
6139 let rec toplevel v t spos _ =
6140 match t with
6141 | Vdata | Vcdata | Vend -> v
6142 | Vopen ("llppconfig", _, closed) ->
6143 if closed
6144 then v
6145 else { v with f = llppconfig }
6146 | Vopen _ ->
6147 error "unexpected subelement at top level" s spos
6148 | Vclose _ -> error "unexpected close at top level" s spos
6150 and llppconfig v t spos _ =
6151 match t with
6152 | Vdata | Vcdata -> v
6153 | Vend -> error "unexpected end of input in llppconfig" s spos
6154 | Vopen ("defaults", attrs, closed) ->
6155 let c = config_of dc attrs in
6156 setconf dc c;
6157 if closed
6158 then v
6159 else { v with f = defaults }
6161 | Vopen ("ui-font", attrs, closed) ->
6162 let rec getsize size = function
6163 | [] -> size
6164 | ("size", v) :: rest ->
6165 let size =
6166 fromstring int_of_string spos "size" v fstate.fontsize in
6167 getsize size rest
6168 | l -> getsize size l
6170 fstate.fontsize <- getsize fstate.fontsize attrs;
6171 if closed
6172 then v
6173 else { v with f = uifont (Buffer.create 10) }
6175 | Vopen ("doc", attrs, closed) ->
6176 let pathent, spage, srely, span, svisy = doc_of attrs in
6177 let path = unent pathent
6178 and pageno = fromstring int_of_string spos "page" spage 0
6179 and rely = fromstring float_of_string spos "rely" srely 0.0
6180 and pan = fromstring int_of_string spos "pan" span 0
6181 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6182 let c = config_of dc attrs in
6183 let anchor = (pageno, rely, visy) in
6184 if closed
6185 then (Hashtbl.add h path (c, [], pan, anchor); v)
6186 else { v with f = doc path pan anchor c [] }
6188 | Vopen _ ->
6189 error "unexpected subelement in llppconfig" s spos
6191 | Vclose "llppconfig" -> { v with f = toplevel }
6192 | Vclose _ -> error "unexpected close in llppconfig" s spos
6194 and defaults v t spos _ =
6195 match t with
6196 | Vdata | Vcdata -> v
6197 | Vend -> error "unexpected end of input in defaults" s spos
6198 | Vopen ("keymap", attrs, closed) ->
6199 let modename =
6200 try List.assoc "mode" attrs
6201 with Not_found -> "global" in
6202 if closed
6203 then v
6204 else
6205 let ret keymap =
6206 let h = findkeyhash dc modename in
6207 KeyMap.iter (Hashtbl.replace h) keymap;
6208 defaults
6210 { v with f = pkeymap ret KeyMap.empty }
6212 | Vopen (_, _, _) ->
6213 error "unexpected subelement in defaults" s spos
6215 | Vclose "defaults" ->
6216 { v with f = llppconfig }
6218 | Vclose _ -> error "unexpected close in defaults" s spos
6220 and uifont b v t spos epos =
6221 match t with
6222 | Vdata | Vcdata ->
6223 Buffer.add_substring b s spos (epos - spos);
6225 | Vopen (_, _, _) ->
6226 error "unexpected subelement in ui-font" s spos
6227 | Vclose "ui-font" ->
6228 if String.length !fontpath = 0
6229 then fontpath := Buffer.contents b;
6230 { v with f = llppconfig }
6231 | Vclose _ -> error "unexpected close in ui-font" s spos
6232 | Vend -> error "unexpected end of input in ui-font" s spos
6234 and doc path pan anchor c bookmarks v t spos _ =
6235 match t with
6236 | Vdata | Vcdata -> v
6237 | Vend -> error "unexpected end of input in doc" s spos
6238 | Vopen ("bookmarks", _, closed) ->
6239 if closed
6240 then v
6241 else { v with f = pbookmarks path pan anchor c bookmarks }
6243 | Vopen ("keymap", attrs, closed) ->
6244 let modename =
6245 try List.assoc "mode" attrs
6246 with Not_found -> "global"
6248 if closed
6249 then v
6250 else
6251 let ret keymap =
6252 let h = findkeyhash c modename in
6253 KeyMap.iter (Hashtbl.replace h) keymap;
6254 doc path pan anchor c bookmarks
6256 { v with f = pkeymap ret KeyMap.empty }
6258 | Vopen (_, _, _) ->
6259 error "unexpected subelement in doc" s spos
6261 | Vclose "doc" ->
6262 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6263 { v with f = llppconfig }
6265 | Vclose _ -> error "unexpected close in doc" s spos
6267 and pkeymap ret keymap v t spos _ =
6268 match t with
6269 | Vdata | Vcdata -> v
6270 | Vend -> error "unexpected end of input in keymap" s spos
6271 | Vopen ("map", attrs, closed) ->
6272 let r, l = map_of attrs in
6273 let kss = fromstring keys_of_string spos "in" r [] in
6274 let lss = fromstring keys_of_string spos "out" l [] in
6275 let keymap =
6276 match kss with
6277 | [] -> keymap
6278 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6279 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6281 if closed
6282 then { v with f = pkeymap ret keymap }
6283 else
6284 let f () = v in
6285 { v with f = skip "map" f }
6287 | Vopen _ ->
6288 error "unexpected subelement in keymap" s spos
6290 | Vclose "keymap" ->
6291 { v with f = ret keymap }
6293 | Vclose _ -> error "unexpected close in keymap" s spos
6295 and pbookmarks path pan anchor c bookmarks v t spos _ =
6296 match t with
6297 | Vdata | Vcdata -> v
6298 | Vend -> error "unexpected end of input in bookmarks" s spos
6299 | Vopen ("item", attrs, closed) ->
6300 let titleent, spage, srely, svisy = bookmark_of attrs in
6301 let page = fromstring int_of_string spos "page" spage 0
6302 and rely = fromstring float_of_string spos "rely" srely 0.0
6303 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6304 let bookmarks =
6305 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6307 if closed
6308 then { v with f = pbookmarks path pan anchor c bookmarks }
6309 else
6310 let f () = v in
6311 { v with f = skip "item" f }
6313 | Vopen _ ->
6314 error "unexpected subelement in bookmarks" s spos
6316 | Vclose "bookmarks" ->
6317 { v with f = doc path pan anchor c bookmarks }
6319 | Vclose _ -> error "unexpected close in bookmarks" s spos
6321 and skip tag f v t spos _ =
6322 match t with
6323 | Vdata | Vcdata -> v
6324 | Vend ->
6325 error ("unexpected end of input in skipped " ^ tag) s spos
6326 | Vopen (tag', _, closed) ->
6327 if closed
6328 then v
6329 else
6330 let f' () = { v with f = skip tag f } in
6331 { v with f = skip tag' f' }
6332 | Vclose ctag ->
6333 if tag = ctag
6334 then f ()
6335 else error ("unexpected close in skipped " ^ tag) s spos
6338 parse { f = toplevel; accu = () } s;
6339 h, dc;
6342 let do_load f ic =
6344 let len = in_channel_length ic in
6345 let s = String.create len in
6346 really_input ic s 0 len;
6347 f s;
6348 with
6349 | Parse_error (msg, s, pos) ->
6350 let subs = subs s pos in
6351 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6352 failwith ("parse error: " ^ s)
6354 | exn ->
6355 failwith ("config load error: " ^ Printexc.to_string exn)
6358 let defconfpath =
6359 let dir =
6361 let dir = Filename.concat home ".config" in
6362 if Sys.is_directory dir then dir else home
6363 with _ -> home
6365 Filename.concat dir "llpp.conf"
6368 let confpath = ref defconfpath;;
6370 let load1 f =
6371 if Sys.file_exists !confpath
6372 then
6373 match
6374 (try Some (open_in_bin !confpath)
6375 with exn ->
6376 prerr_endline
6377 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6378 Printexc.to_string exn);
6379 None
6381 with
6382 | Some ic ->
6383 begin try
6384 f (do_load get ic)
6385 with exn ->
6386 prerr_endline
6387 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6388 Printexc.to_string exn);
6389 end;
6390 close_in ic;
6392 | None -> ()
6393 else
6394 f (Hashtbl.create 0, defconf)
6397 let load () =
6398 let f (h, dc) =
6399 let pc, pb, px, pa =
6401 Hashtbl.find h (Filename.basename state.path)
6402 with Not_found -> dc, [], 0, emptyanchor
6404 setconf defconf dc;
6405 setconf conf pc;
6406 state.bookmarks <- pb;
6407 state.x <- px;
6408 state.scrollw <- conf.scrollbw;
6409 if conf.jumpback
6410 then state.anchor <- pa;
6411 cbput state.hists.nav pa;
6413 load1 f
6416 let add_attrs bb always dc c =
6417 let ob s a b =
6418 if always || a != b
6419 then Printf.bprintf bb "\n %s='%b'" s a
6420 and oi s a b =
6421 if always || a != b
6422 then Printf.bprintf bb "\n %s='%d'" s a
6423 and oI s a b =
6424 if always || a != b
6425 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6426 and oz s a b =
6427 if always || a <> b
6428 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6429 and oF s a b =
6430 if always || a <> b
6431 then Printf.bprintf bb "\n %s='%f'" s a
6432 and oc s a b =
6433 if always || a <> b
6434 then
6435 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6436 and oC s a b =
6437 if always || a <> b
6438 then
6439 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6440 and oR s a b =
6441 if always || a <> b
6442 then
6443 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6444 and os s a b =
6445 if always || a <> b
6446 then
6447 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6448 and og s a b =
6449 if always || a <> b
6450 then
6451 match a with
6452 | None -> ()
6453 | Some (_N, _A, _B) ->
6454 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6455 and oW s a b =
6456 if always || a <> b
6457 then
6458 let v =
6459 match a with
6460 | None -> "false"
6461 | Some f ->
6462 if f = infinity
6463 then "true"
6464 else string_of_float f
6466 Printf.bprintf bb "\n %s='%s'" s v
6467 and oco s a b =
6468 if always || a <> b
6469 then
6470 match a with
6471 | Cmulti ((n, a, b), _) when n > 1 ->
6472 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6473 | Csplit (n, _) when n > 1 ->
6474 Printf.bprintf bb "\n %s='%d'" s ~-n
6475 | _ -> ()
6476 and obeco s a b =
6477 if always || a <> b
6478 then
6479 match a with
6480 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6481 | _ -> ()
6483 let w, h =
6484 if always
6485 then dc.winw, dc.winh
6486 else
6487 match state.fullscreen with
6488 | Some wh -> wh
6489 | None -> c.winw, c.winh
6491 oi "width" w dc.winw;
6492 oi "height" h dc.winh;
6493 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6494 oi "scroll-handle-height" c.scrollh dc.scrollh;
6495 ob "case-insensitive-search" c.icase dc.icase;
6496 ob "preload" c.preload dc.preload;
6497 oi "page-bias" c.pagebias dc.pagebias;
6498 oi "scroll-step" c.scrollstep dc.scrollstep;
6499 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6500 ob "max-height-fit" c.maxhfit dc.maxhfit;
6501 ob "crop-hack" c.crophack dc.crophack;
6502 oW "throttle" c.maxwait dc.maxwait;
6503 ob "highlight-links" c.hlinks dc.hlinks;
6504 ob "under-cursor-info" c.underinfo dc.underinfo;
6505 oi "vertical-margin" c.interpagespace dc.interpagespace;
6506 oz "zoom" c.zoom dc.zoom;
6507 ob "presentation" c.presentation dc.presentation;
6508 oi "rotation-angle" c.angle dc.angle;
6509 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6510 ob "proportional-display" c.proportional dc.proportional;
6511 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6512 oi "tex-count" c.texcount dc.texcount;
6513 oi "slice-height" c.sliceheight dc.sliceheight;
6514 oi "thumbnail-width" c.thumbw dc.thumbw;
6515 ob "persistent-location" c.jumpback dc.jumpback;
6516 oc "background-color" c.bgcolor dc.bgcolor;
6517 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6518 oi "tile-width" c.tilew dc.tilew;
6519 oi "tile-height" c.tileh dc.tileh;
6520 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6521 ob "checkers" c.checkers dc.checkers;
6522 oi "aalevel" c.aalevel dc.aalevel;
6523 ob "trim-margins" c.trimmargins dc.trimmargins;
6524 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6525 os "uri-launcher" c.urilauncher dc.urilauncher;
6526 os "path-launcher" c.pathlauncher dc.pathlauncher;
6527 oC "color-space" c.colorspace dc.colorspace;
6528 ob "invert-colors" c.invert dc.invert;
6529 oF "brightness" c.colorscale dc.colorscale;
6530 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6531 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6532 oco "columns" c.columns dc.columns;
6533 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6534 os "selection-command" c.selcmd dc.selcmd;
6535 ob "update-cursor" c.updatecurs dc.updatecurs;
6536 oi "hint-font-size" c.hfsize dc.hfsize;
6537 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6538 oF "page-scroll-scale" c.pgscale dc.pgscale;
6539 ob "multi-column-centering" c.multicenter dc.multicenter;
6542 let keymapsbuf always dc c =
6543 let bb = Buffer.create 16 in
6544 let rec loop = function
6545 | [] -> ()
6546 | (modename, h) :: rest ->
6547 let dh = findkeyhash dc modename in
6548 if always || h <> dh
6549 then (
6550 if Hashtbl.length h > 0
6551 then (
6552 if Buffer.length bb > 0
6553 then Buffer.add_char bb '\n';
6554 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6555 Hashtbl.iter (fun i o ->
6556 let isdifferent = always ||
6558 let dO = Hashtbl.find dh i in
6559 dO <> o
6560 with Not_found -> true
6562 if isdifferent
6563 then
6564 let addkm (k, m) =
6565 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6566 if Wsi.withalt m then Buffer.add_string bb "alt-";
6567 if Wsi.withshift m then Buffer.add_string bb "shift-";
6568 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6569 Buffer.add_string bb (Wsi.keyname k);
6571 let addkms l =
6572 let rec loop = function
6573 | [] -> ()
6574 | km :: [] -> addkm km
6575 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6577 loop l
6579 Buffer.add_string bb "<map in='";
6580 addkm i;
6581 match o with
6582 | KMinsrt km ->
6583 Buffer.add_string bb "' out='";
6584 addkm km;
6585 Buffer.add_string bb "'/>\n"
6587 | KMinsrl kms ->
6588 Buffer.add_string bb "' out='";
6589 addkms kms;
6590 Buffer.add_string bb "'/>\n"
6592 | KMmulti (ins, kms) ->
6593 Buffer.add_char bb ' ';
6594 addkms ins;
6595 Buffer.add_string bb "' out='";
6596 addkms kms;
6597 Buffer.add_string bb "'/>\n"
6598 ) h;
6599 Buffer.add_string bb "</keymap>";
6602 loop rest
6604 loop c.keyhashes;
6608 let save () =
6609 let uifontsize = fstate.fontsize in
6610 let bb = Buffer.create 32768 in
6611 let f (h, dc) =
6612 let dc = if conf.bedefault then conf else dc in
6613 Buffer.add_string bb "<llppconfig>\n";
6615 if String.length !fontpath > 0
6616 then
6617 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6618 uifontsize
6619 !fontpath
6620 else (
6621 if uifontsize <> 14
6622 then
6623 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6626 Buffer.add_string bb "<defaults ";
6627 add_attrs bb true dc dc;
6628 let kb = keymapsbuf true dc dc in
6629 if Buffer.length kb > 0
6630 then (
6631 Buffer.add_string bb ">\n";
6632 Buffer.add_buffer bb kb;
6633 Buffer.add_string bb "\n</defaults>\n";
6635 else Buffer.add_string bb "/>\n";
6637 let adddoc path pan anchor c bookmarks =
6638 if bookmarks == [] && c = dc && anchor = emptyanchor
6639 then ()
6640 else (
6641 Printf.bprintf bb "<doc path='%s'"
6642 (enent path 0 (String.length path));
6644 if anchor <> emptyanchor
6645 then (
6646 let n, rely, visy = anchor in
6647 Printf.bprintf bb " page='%d'" n;
6648 if rely > 1e-6
6649 then
6650 Printf.bprintf bb " rely='%f'" rely
6652 if abs_float visy > 1e-6
6653 then
6654 Printf.bprintf bb " visy='%f'" visy
6658 if pan != 0
6659 then Printf.bprintf bb " pan='%d'" pan;
6661 add_attrs bb false dc c;
6662 let kb = keymapsbuf false dc c in
6664 begin match bookmarks with
6665 | [] ->
6666 if Buffer.length kb > 0
6667 then (
6668 Buffer.add_string bb ">\n";
6669 Buffer.add_buffer bb kb;
6670 Buffer.add_string bb "\n</doc>\n";
6672 else Buffer.add_string bb "/>\n"
6673 | _ ->
6674 Buffer.add_string bb ">\n<bookmarks>\n";
6675 List.iter (fun (title, _level, (page, rely, visy)) ->
6676 Printf.bprintf bb
6677 "<item title='%s' page='%d'"
6678 (enent title 0 (String.length title))
6679 page
6681 if rely > 1e-6
6682 then
6683 Printf.bprintf bb " rely='%f'" rely
6685 if abs_float visy > 1e-6
6686 then
6687 Printf.bprintf bb " visy='%f'" visy
6689 Buffer.add_string bb "/>\n";
6690 ) bookmarks;
6691 Buffer.add_string bb "</bookmarks>";
6692 if Buffer.length kb > 0
6693 then (
6694 Buffer.add_string bb "\n";
6695 Buffer.add_buffer bb kb;
6697 Buffer.add_string bb "\n</doc>\n";
6698 end;
6702 let pan, conf =
6703 match state.mode with
6704 | Birdseye (c, pan, _, _, _) ->
6705 let beyecolumns =
6706 match conf.columns with
6707 | Cmulti ((c, _, _), _) -> Some c
6708 | Csingle _ -> None
6709 | Csplit _ -> None
6710 and columns =
6711 match c.columns with
6712 | Cmulti (c, _) -> Cmulti (c, [||])
6713 | Csingle _ -> Csingle [||]
6714 | Csplit _ -> failwith "quit from bird's eye while split"
6716 pan, { c with beyecolumns = beyecolumns; columns = columns }
6717 | _ -> state.x, conf
6719 let basename = Filename.basename state.path in
6720 adddoc basename pan (getanchor ())
6721 (let conf =
6722 let autoscrollstep =
6723 match state.autoscroll with
6724 | Some step -> step
6725 | None -> conf.autoscrollstep
6727 match state.mode with
6728 | Birdseye (bc, _, _, _, _) ->
6729 { conf with
6730 zoom = bc.zoom;
6731 presentation = bc.presentation;
6732 interpagespace = bc.interpagespace;
6733 maxwait = bc.maxwait;
6734 autoscrollstep = autoscrollstep }
6735 | _ -> { conf with autoscrollstep = autoscrollstep }
6736 in conf)
6737 (if conf.savebmarks then state.bookmarks else []);
6739 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
6740 if basename <> path
6741 then adddoc path x anchor c bookmarks
6742 ) h;
6743 Buffer.add_string bb "</llppconfig>";
6745 load1 f;
6746 if Buffer.length bb > 0
6747 then
6749 let tmp = !confpath ^ ".tmp" in
6750 let oc = open_out_bin tmp in
6751 Buffer.output_buffer oc bb;
6752 close_out oc;
6753 Unix.rename tmp !confpath;
6754 with exn ->
6755 prerr_endline
6756 ("error while saving configuration: " ^ Printexc.to_string exn)
6758 end;;
6760 let () =
6761 let trimcachepath = ref "" in
6762 Arg.parse
6763 (Arg.align
6764 [("-p", Arg.String (fun s -> state.password <- s) ,
6765 "<password> Set password");
6767 ("-f", Arg.String (fun s -> Config.fontpath := s),
6768 "<path> Set path to the user interface font");
6770 ("-c", Arg.String (fun s -> Config.confpath := s),
6771 "<path> Set path to the configuration file");
6773 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6774 "<path> Set path to the trim cache file");
6776 ("-v", Arg.Unit (fun () ->
6777 Printf.printf
6778 "%s\nconfiguration path: %s\n"
6779 (version ())
6780 Config.defconfpath
6782 exit 0), " Print version and exit");
6785 (fun s -> state.path <- s)
6786 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6788 if String.length state.path = 0
6789 then (prerr_endline "file name missing"; exit 1);
6791 Config.load ();
6793 let globalkeyhash = findkeyhash conf "global" in
6794 let wsfd, winw, winh = Wsi.init (object
6795 method expose =
6796 if nogeomcmds state.geomcmds || platform == Posx
6797 then display ()
6798 else (
6799 GlClear.color (scalecolor2 conf.bgcolor);
6800 GlClear.clear [`color];
6802 method display = display ()
6803 method reshape w h = reshape w h
6804 method mouse b d x y m = mouse b d x y m
6805 method motion x y = state.mpos <- (x, y); motion x y
6806 method pmotion x y = state.mpos <- (x, y); pmotion x y
6807 method key k m =
6808 let mascm = m land (
6809 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6810 ) in
6811 match state.keystate with
6812 | KSnone ->
6813 let km = k, mascm in
6814 begin
6815 match
6816 let modehash = state.uioh#modehash in
6817 try Hashtbl.find modehash km
6818 with Not_found ->
6819 try Hashtbl.find globalkeyhash km
6820 with Not_found -> KMinsrt (k, m)
6821 with
6822 | KMinsrt (k, m) -> keyboard k m
6823 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6824 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6826 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6827 List.iter (fun (k, m) -> keyboard k m) insrt;
6828 state.keystate <- KSnone
6829 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6830 state.keystate <- KSinto (keys, insrt)
6831 | _ ->
6832 state.keystate <- KSnone
6834 method enter x y = state.mpos <- (x, y); pmotion x y
6835 method leave = state.mpos <- (-1, -1)
6836 method quit = raise Quit
6837 end) conf.winw conf.winh (platform = Posx) in
6839 state.wsfd <- wsfd;
6841 if not (
6842 List.exists GlMisc.check_extension
6843 [ "GL_ARB_texture_rectangle"
6844 ; "GL_EXT_texture_recangle"
6845 ; "GL_NV_texture_rectangle" ]
6847 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6849 let cr, sw =
6850 match Ne.pipe () with
6851 | Ne.Exn exn ->
6852 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
6853 exit 1
6854 | Ne.Res rw -> rw
6855 and sr, cw =
6856 match Ne.pipe () with
6857 | Ne.Exn exn ->
6858 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
6859 exit 1
6860 | Ne.Res rw -> rw
6863 cloexec cr;
6864 cloexec sw;
6865 cloexec sr;
6866 cloexec cw;
6868 setcheckers conf.checkers;
6869 redirectstderr ();
6871 init (cr, cw) (
6872 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6873 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6874 !Config.fontpath, !trimcachepath
6876 state.sr <- sr;
6877 state.sw <- sw;
6878 state.text <- "Opening " ^ state.path;
6879 reshape winw winh;
6880 opendoc state.path state.password;
6881 state.uioh <- uioh;
6883 let rec loop deadline =
6884 let r =
6885 match state.errfd with
6886 | None -> [state.sr; state.wsfd]
6887 | Some fd -> [state.sr; state.wsfd; fd]
6889 if state.redisplay
6890 then (
6891 state.redisplay <- false;
6892 display ();
6894 let timeout =
6895 let now = now () in
6896 if deadline > now
6897 then (
6898 if deadline = infinity
6899 then ~-.1.0
6900 else max 0.0 (deadline -. now)
6902 else 0.0
6904 let r, _, _ =
6905 try Unix.select r [] [] timeout
6906 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6908 begin match r with
6909 | [] ->
6910 state.ghyll None;
6911 let newdeadline =
6912 if state.ghyll == noghyll
6913 then
6914 match state.autoscroll with
6915 | Some step when step != 0 ->
6916 let y = state.y + step in
6917 let y =
6918 if y < 0
6919 then state.maxy
6920 else if y >= state.maxy then 0 else y
6922 gotoy y;
6923 if state.mode = View
6924 then state.text <- "";
6925 deadline +. 0.01
6926 | _ -> infinity
6927 else deadline +. 0.01
6929 loop newdeadline
6931 | l ->
6932 let rec checkfds = function
6933 | [] -> ()
6934 | fd :: rest when fd = state.sr ->
6935 let cmd = readcmd state.sr in
6936 act cmd;
6937 checkfds rest
6939 | fd :: rest when fd = state.wsfd ->
6940 Wsi.readresp fd;
6941 checkfds rest
6943 | fd :: rest ->
6944 let s = String.create 80 in
6945 let n = Unix.read fd s 0 80 in
6946 if conf.redirectstderr
6947 then (
6948 Buffer.add_substring state.errmsgs s 0 n;
6949 state.newerrmsgs <- true;
6950 state.redisplay <- true;
6952 else (
6953 prerr_string (String.sub s 0 n);
6954 flush stderr;
6956 checkfds rest
6958 checkfds l;
6959 let newdeadline =
6960 let deadline1 =
6961 if deadline = infinity
6962 then now () +. 0.01
6963 else deadline
6965 match state.autoscroll with
6966 | Some step when step != 0 -> deadline1
6967 | _ -> if state.ghyll == noghyll then infinity else deadline1
6969 loop newdeadline
6970 end;
6973 loop infinity;
6974 with Quit ->
6975 Config.save ();