Use rowyh in calcheight and fix the former while at it
[llpp.git] / main.ml
blobac76667e3e83e2b0527da8b27e57d7028dc0e2d0
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 ->
978 if a > 1 || b > 1
979 then failwith "subtly broken"; (n, a, b)
983 let readcmd fd =
984 let s = "xxxx" in
985 let n = Unix.read fd s 0 4 in
986 if n != 4 then failwith "incomplete read(len)";
987 let len = 0
988 lor (Char.code s.[0] lsl 24)
989 lor (Char.code s.[1] lsl 16)
990 lor (Char.code s.[2] lsl 8)
991 lor (Char.code s.[3] lsl 0)
993 let s = String.create len in
994 let n = Unix.read fd s 0 len in
995 if n != len then failwith "incomplete read(data)";
999 let btod b = if b then 1 else 0;;
1001 let wcmd fmt =
1002 let b = Buffer.create 16 in
1003 Buffer.add_string b "llll";
1004 Printf.kbprintf
1005 (fun b ->
1006 let s = Buffer.contents b in
1007 let n = String.length s in
1008 let len = n - 4 in
1009 (* dolog "wcmd %S" (String.sub s 4 len); *)
1010 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1011 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1012 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1013 s.[3] <- Char.chr (len land 0xff);
1014 let n' = Unix.write state.sw s 0 n in
1015 if n' != n then failwith "write failed";
1016 ) b fmt;
1019 let calcips h =
1020 if conf.presentation
1021 then
1022 let d = conf.winh - h in
1023 max conf.interpagespace ((d + 1) / 2)
1024 else
1025 conf.interpagespace
1028 let rowyh (c, coverA, coverB) b n =
1029 if c = 1 || (n < coverA || n > state.pagecount - coverB)
1030 then
1031 let _, _, vy, (_, _, h, _) = b.(n) in
1032 (vy, h)
1033 else
1034 let n' = n - coverA in
1035 let d = n' mod c in
1036 let s = n - d in
1037 let e = min state.pagecount (s + c) in
1038 let rec find m miny maxh = if m = e then miny, maxh else
1039 let _, _, y, (_, _, h, _) = b.(m) in
1040 let miny = min miny y in
1041 let maxh = max maxh h in
1042 find (m+1) miny maxh
1043 in find s max_int 0
1046 let calcheight () =
1047 match conf.columns with
1048 | Cmulti ((_, _, _) as cl, b) ->
1049 if Array.length b > 0
1050 then
1051 let y, h = rowyh cl b (Array.length b - 1) in
1052 y + h
1053 else 0
1054 | Csingle b ->
1055 if Array.length b > 0
1056 then
1057 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1058 y + h + (if conf.presentation then calcips h else 0)
1059 else 0
1060 | Csplit (_, b) ->
1061 if Array.length b > 0
1062 then
1063 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1064 y + h
1065 else 0
1068 let getpageyh pageno =
1069 let pageno = bound pageno 0 (state.pagecount-1) in
1070 match conf.columns with
1071 | Csingle b ->
1072 if Array.length b = 0
1073 then 0, 0
1074 else
1075 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1076 let y =
1077 if conf.presentation
1078 then y - calcips h
1079 else y
1081 y, h
1082 | Cmulti (cl, b) ->
1083 if Array.length b = 0
1084 then 0, 0
1085 else rowyh cl b pageno
1086 | Csplit (c, b) ->
1087 if Array.length b = 0
1088 then 0, 0
1089 else
1090 let n = pageno*c in
1091 let (_, _, y, (_, _, h, _)) = b.(n) in
1092 y, h
1095 let getpagedim pageno =
1096 let rec f ppdim l =
1097 match l with
1098 | (n, _, _, _) as pdim :: rest ->
1099 if n >= pageno
1100 then (if n = pageno then pdim else ppdim)
1101 else f pdim rest
1103 | [] -> ppdim
1105 f (-1, -1, -1, -1) state.pdims
1108 let getpagey pageno = fst (getpageyh pageno);;
1110 let nogeomcmds cmds =
1111 match cmds with
1112 | s, [] -> String.length s = 0
1113 | _ -> false
1116 let page_of_y y =
1117 let ((c, coverA, coverB) as cl), b =
1118 match conf.columns with
1119 | Csingle b -> (1, 0, 0), b
1120 | Cmulti (c, b) -> c, b
1121 | Csplit (_, b) -> (1, 0, 0), b
1123 let rec bsearch nmin nmax =
1124 if nmin > nmax
1125 then bound nmin 0 (state.pagecount-1)
1126 else
1127 let n = (nmax + nmin) / 2 in
1128 let vy, h = rowyh cl b n in
1129 let y0, y1 =
1130 if c = 1 && conf.presentation
1131 then
1132 let ips = calcips h in
1133 let y0 = vy - ips in
1134 let y1 = vy + h + ips in
1135 y0, y1
1136 else (
1137 if n = 0
1138 then 0, vy + h + conf.interpagespace
1139 else
1140 let y0 = vy - conf.interpagespace in
1141 y0, y0 + h + conf.interpagespace
1144 if y >= y0 && y < y1
1145 then (
1146 if c = 1
1147 then n
1148 else (
1149 if n > coverA
1150 then
1151 if n < state.pagecount - coverB
1152 then ((n-coverA)/c)*c + coverA
1153 else n
1154 else n
1157 else (
1158 if y > y0
1159 then bsearch (n+1) nmax
1160 else bsearch nmin (n-1)
1163 let r = bsearch 0 (state.pagecount-1) in
1167 let layoutN ((columns, coverA, coverB), b) y sh =
1168 let sh = sh - state.hscrollh in
1169 let rec fold accu n =
1170 if n = Array.length b
1171 then accu
1172 else
1173 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1174 if (vy - y) > sh &&
1175 (n = coverA - 1
1176 || n = state.pagecount - coverB
1177 || (n - coverA) mod columns = columns - 1)
1178 then accu
1179 else
1180 let accu =
1181 if vy + h > y
1182 then
1183 let pagey = max 0 (y - vy) in
1184 let pagedispy = if pagey > 0 then 0 else vy - y in
1185 let pagedispx, pagex =
1186 let pdx =
1187 if n = coverA - 1 || n = state.pagecount - coverB
1188 then state.x + (conf.winw - state.scrollw - w) / 2
1189 else dx + xoff + state.x
1191 if pdx < 0
1192 then 0, -pdx
1193 else pdx, 0
1195 let pagevw =
1196 let vw = conf.winw - state.scrollw - pagedispx in
1197 let pw = w - pagex in
1198 min vw pw
1200 let pagevh = min (h - pagey) (sh - pagedispy) in
1201 if pagevw > 0 && pagevh > 0
1202 then
1203 let e =
1204 { pageno = n
1205 ; pagedimno = pdimno
1206 ; pagew = w
1207 ; pageh = h
1208 ; pagex = pagex
1209 ; pagey = pagey
1210 ; pagevw = pagevw
1211 ; pagevh = pagevh
1212 ; pagedispx = pagedispx
1213 ; pagedispy = pagedispy
1214 ; pagecol = 0
1217 e :: accu
1218 else
1219 accu
1220 else
1221 accu
1223 fold accu (n+1)
1225 List.rev (fold [] (page_of_y y));
1228 let layoutS (columns, b) y sh =
1229 let sh = sh - state.hscrollh in
1230 let rec fold accu n =
1231 if n = Array.length b
1232 then accu
1233 else
1234 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1235 if (vy - y) > sh
1236 then accu
1237 else
1238 let accu =
1239 if vy + pageh > y
1240 then
1241 let x = xoff + state.x in
1242 let pagey = max 0 (y - vy) in
1243 let pagedispy = if pagey > 0 then 0 else vy - y in
1244 let pagedispx, pagex =
1245 if px = 0
1246 then (
1247 if x < 0
1248 then 0, -x
1249 else x, 0
1251 else (
1252 let px = px - x in
1253 if px < 0
1254 then -px, 0
1255 else 0, px
1258 let pagecolw = pagew/columns in
1259 let pagedispx =
1260 if pagecolw < conf.winw
1261 then pagedispx + ((conf.winw - state.scrollw - pagecolw) / 2)
1262 else pagedispx
1264 let pagevw =
1265 let vw = conf.winw - pagedispx - state.scrollw in
1266 let pw = pagew - pagex in
1267 min vw pw
1269 let pagevw = min pagevw pagecolw in
1270 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1271 if pagevw > 0 && pagevh > 0
1272 then
1273 let e =
1274 { pageno = n/columns
1275 ; pagedimno = pdimno
1276 ; pagew = pagew
1277 ; pageh = pageh
1278 ; pagex = pagex
1279 ; pagey = pagey
1280 ; pagevw = pagevw
1281 ; pagevh = pagevh
1282 ; pagedispx = pagedispx
1283 ; pagedispy = pagedispy
1284 ; pagecol = n mod columns
1287 e :: accu
1288 else
1289 accu
1290 else
1291 accu
1293 fold accu (n+1)
1295 List.rev (fold [] 0)
1298 let layout y sh =
1299 if nogeomcmds state.geomcmds
1300 then
1301 match conf.columns with
1302 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1303 | Cmulti c -> layoutN c y sh
1304 | Csplit s -> layoutS s y sh
1305 else []
1308 let clamp incr =
1309 let y = state.y + incr in
1310 let y = max 0 y in
1311 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1315 let itertiles l f =
1316 let tilex = l.pagex mod conf.tilew in
1317 let tiley = l.pagey mod conf.tileh in
1319 let col = l.pagex / conf.tilew in
1320 let row = l.pagey / conf.tileh in
1322 let rec rowloop row y0 dispy h =
1323 if h = 0
1324 then ()
1325 else (
1326 let dh = conf.tileh - y0 in
1327 let dh = min h dh in
1328 let rec colloop col x0 dispx w =
1329 if w = 0
1330 then ()
1331 else (
1332 let dw = conf.tilew - x0 in
1333 let dw = min w dw in
1335 f col row dispx dispy x0 y0 dw dh;
1336 colloop (col+1) 0 (dispx+dw) (w-dw)
1339 colloop col tilex l.pagedispx l.pagevw;
1340 rowloop (row+1) 0 (dispy+dh) (h-dh)
1343 if l.pagevw > 0 && l.pagevh > 0
1344 then rowloop row tiley l.pagedispy l.pagevh;
1347 let gettileopaque l col row =
1348 let key =
1349 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1351 try Some (Hashtbl.find state.tilemap key)
1352 with Not_found -> None
1355 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1356 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1357 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1360 let drawtiles l color =
1361 GlDraw.color color;
1362 let f col row x y tilex tiley w h =
1363 match gettileopaque l col row with
1364 | Some (opaque, _, t) ->
1365 let params = x, y, w, h, tilex, tiley in
1366 if conf.invert
1367 then (
1368 Gl.enable `blend;
1369 GlFunc.blend_func `zero `one_minus_src_color;
1371 drawtile params opaque;
1372 if conf.invert
1373 then Gl.disable `blend;
1374 if conf.debug
1375 then (
1376 let s = Printf.sprintf
1377 "%d[%d,%d] %f sec"
1378 l.pageno col row t
1380 let w = measurestr fstate.fontsize s in
1381 GlMisc.push_attrib [`current];
1382 GlDraw.color (0.0, 0.0, 0.0);
1383 GlDraw.rect
1384 (float (x-2), float (y-2))
1385 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1386 GlDraw.color (1.0, 1.0, 1.0);
1387 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1388 GlMisc.pop_attrib ();
1391 | _ ->
1392 let w =
1393 let lw = conf.winw - state.scrollw - x in
1394 min lw w
1395 and h =
1396 let lh = conf.winh - y in
1397 min lh h
1399 begin match state.texid with
1400 | Some id ->
1401 Gl.enable `texture_2d;
1402 GlTex.bind_texture `texture_2d id;
1403 let x0 = float x
1404 and y0 = float y
1405 and x1 = float (x+w)
1406 and y1 = float (y+h) in
1408 let tw = float w /. 64.0
1409 and th = float h /. 64.0 in
1410 let tx0 = float tilex /. 64.0
1411 and ty0 = float tiley /. 64.0 in
1412 let tx1 = tx0 +. tw
1413 and ty1 = ty0 +. th in
1414 GlDraw.begins `quads;
1415 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1416 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1417 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1418 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1419 GlDraw.ends ();
1421 Gl.disable `texture_2d;
1422 | None ->
1423 GlDraw.color (1.0, 1.0, 1.0);
1424 GlDraw.rect
1425 (float x, float y)
1426 (float (x+w), float (y+h));
1427 end;
1428 if w > 128 && h > fstate.fontsize + 10
1429 then (
1430 GlDraw.color (0.0, 0.0, 0.0);
1431 let c, r =
1432 if conf.verbose
1433 then (col*conf.tilew, row*conf.tileh)
1434 else col, row
1436 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1438 GlDraw.color color;
1440 itertiles l f
1443 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1445 let tilevisible1 l x y =
1446 let ax0 = l.pagex
1447 and ax1 = l.pagex + l.pagevw
1448 and ay0 = l.pagey
1449 and ay1 = l.pagey + l.pagevh in
1451 let bx0 = x
1452 and by0 = y in
1453 let bx1 = min (bx0 + conf.tilew) l.pagew
1454 and by1 = min (by0 + conf.tileh) l.pageh in
1456 let rx0 = max ax0 bx0
1457 and ry0 = max ay0 by0
1458 and rx1 = min ax1 bx1
1459 and ry1 = min ay1 by1 in
1461 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1462 nonemptyintersection
1465 let tilevisible layout n x y =
1466 let rec findpageinlayout m = function
1467 | l :: rest when l.pageno = n ->
1468 tilevisible1 l x y || (
1469 match conf.columns with
1470 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1471 | _ -> false
1473 | _ :: rest -> findpageinlayout 0 rest
1474 | [] -> false
1476 findpageinlayout 0 layout;
1479 let tileready l x y =
1480 tilevisible1 l x y &&
1481 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1484 let tilepage n p layout =
1485 let rec loop = function
1486 | l :: rest ->
1487 if l.pageno = n
1488 then
1489 let f col row _ _ _ _ _ _ =
1490 if state.currently = Idle
1491 then
1492 match gettileopaque l col row with
1493 | Some _ -> ()
1494 | None ->
1495 let x = col*conf.tilew
1496 and y = row*conf.tileh in
1497 let w =
1498 let w = l.pagew - x in
1499 min w conf.tilew
1501 let h =
1502 let h = l.pageh - y in
1503 min h conf.tileh
1505 wcmd "tile %s %d %d %d %d" p x y w h;
1506 state.currently <-
1507 Tiling (
1508 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1509 conf.tilew, conf.tileh
1512 itertiles l f;
1513 else
1514 loop rest
1516 | [] -> ()
1518 if nogeomcmds state.geomcmds
1519 then loop layout;
1522 let preloadlayout y =
1523 let y = if y < conf.winh then 0 else y - conf.winh in
1524 let h = conf.winh*3 in
1525 layout y h;
1528 let load pages =
1529 let rec loop pages =
1530 if state.currently != Idle
1531 then ()
1532 else
1533 match pages with
1534 | l :: rest ->
1535 begin match getopaque l.pageno with
1536 | None ->
1537 wcmd "page %d %d" l.pageno l.pagedimno;
1538 state.currently <- Loading (l, state.gen);
1539 | Some opaque ->
1540 tilepage l.pageno opaque pages;
1541 loop rest
1542 end;
1543 | _ -> ()
1545 if nogeomcmds state.geomcmds
1546 then loop pages
1549 let preload pages =
1550 load pages;
1551 if conf.preload && state.currently = Idle
1552 then load (preloadlayout state.y);
1555 let layoutready layout =
1556 let rec fold all ls =
1557 all && match ls with
1558 | l :: rest ->
1559 let seen = ref false in
1560 let allvisible = ref true in
1561 let foo col row _ _ _ _ _ _ =
1562 seen := true;
1563 allvisible := !allvisible &&
1564 begin match gettileopaque l col row with
1565 | Some _ -> true
1566 | None -> false
1569 itertiles l foo;
1570 fold (!seen && !allvisible) rest
1571 | [] -> true
1573 let alltilesvisible = fold true layout in
1574 alltilesvisible;
1577 let gotoy y =
1578 let y = bound y 0 state.maxy in
1579 let y, layout, proceed =
1580 match conf.maxwait with
1581 | Some time when state.ghyll == noghyll ->
1582 begin match state.throttle with
1583 | None ->
1584 let layout = layout y conf.winh in
1585 let ready = layoutready layout in
1586 if not ready
1587 then (
1588 load layout;
1589 state.throttle <- Some (layout, y, now ());
1591 else G.postRedisplay "gotoy showall (None)";
1592 y, layout, ready
1593 | Some (_, _, started) ->
1594 let dt = now () -. started in
1595 if dt > time
1596 then (
1597 state.throttle <- None;
1598 let layout = layout y conf.winh in
1599 load layout;
1600 G.postRedisplay "maxwait";
1601 y, layout, true
1603 else -1, [], false
1606 | _ ->
1607 let layout = layout y conf.winh in
1608 if true || layoutready layout
1609 then G.postRedisplay "gotoy ready";
1610 y, layout, true
1612 if proceed
1613 then (
1614 state.y <- y;
1615 state.layout <- layout;
1616 begin match state.mode with
1617 | LinkNav (Ltexact (pageno, linkno)) ->
1618 let rec loop = function
1619 | [] ->
1620 state.mode <- LinkNav (Ltgendir 0)
1621 | l :: _ when l.pageno = pageno ->
1622 begin match getopaque pageno with
1623 | None ->
1624 state.mode <- LinkNav (Ltgendir 0)
1625 | Some opaque ->
1626 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1627 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1628 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1629 then state.mode <- LinkNav (Ltgendir 0)
1631 | _ :: rest -> loop rest
1633 loop layout
1634 | _ -> ()
1635 end;
1636 begin match state.mode with
1637 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1638 if not (pagevisible layout pageno)
1639 then (
1640 match state.layout with
1641 | [] -> ()
1642 | l :: _ ->
1643 state.mode <- Birdseye (
1644 conf, leftx, l.pageno, hooverpageno, anchor
1647 | LinkNav (Ltgendir dir as lt) ->
1648 let linknav =
1649 let rec loop = function
1650 | [] -> lt
1651 | l :: rest ->
1652 match getopaque l.pageno with
1653 | None -> loop rest
1654 | Some opaque ->
1655 let link =
1656 let ld =
1657 if dir = 0
1658 then LDfirstvisible (l.pagex, l.pagey, dir)
1659 else (
1660 if dir > 0 then LDfirst else LDlast
1663 findlink opaque ld
1665 match link with
1666 | Lnotfound -> loop rest
1667 | Lfound n ->
1668 showlinktype (getlink opaque n);
1669 Ltexact (l.pageno, n)
1671 loop state.layout
1673 state.mode <- LinkNav linknav
1674 | _ -> ()
1675 end;
1676 preload layout;
1678 state.ghyll <- noghyll;
1679 if conf.updatecurs
1680 then (
1681 let mx, my = state.mpos in
1682 updateunder mx my;
1686 let conttiling pageno opaque =
1687 tilepage pageno opaque
1688 (if conf.preload then preloadlayout state.y else state.layout)
1691 let gotoy_and_clear_text y =
1692 if not conf.verbose then state.text <- "";
1693 gotoy y;
1696 let getanchor1 l =
1697 let top =
1698 let coloff = l.pagecol * l.pageh in
1699 float (l.pagey + coloff) /. float l.pageh
1701 let dtop =
1702 if l.pagedispy = 0
1703 then
1705 else
1706 if conf.presentation
1707 then float l.pagedispy /. float (calcips l.pageh)
1708 else float l.pagedispy /. float conf.interpagespace
1710 (l.pageno, top, dtop)
1713 let getanchor () =
1714 match state.layout with
1715 | l :: _ -> getanchor1 l
1716 | [] ->
1717 let n = page_of_y state.y in
1718 let y, h = getpageyh n in
1719 let dy = y - state.y in
1720 let dtop =
1721 if conf.presentation
1722 then
1723 let ips = calcips h in
1724 float (dy + ips) /. float ips
1725 else
1726 float dy /. float conf.interpagespace
1728 (n, 0.0, dtop)
1731 let getanchory (n, top, dtop) =
1732 let y, h = getpageyh n in
1733 if conf.presentation
1734 then
1735 let ips = calcips h in
1736 y + truncate (top*.float h -. dtop*.float ips) + ips;
1737 else
1738 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1741 let gotoanchor anchor =
1742 gotoy (getanchory anchor);
1745 let addnav () =
1746 cbput state.hists.nav (getanchor ());
1749 let getnav dir =
1750 let anchor = cbgetc state.hists.nav dir in
1751 getanchory anchor;
1754 let gotoghyll y =
1755 let scroll f n a b =
1756 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1757 let snake f a b =
1758 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1759 if f < a
1760 then s (float f /. float a)
1761 else (
1762 if f > b
1763 then 1.0 -. s ((float (f-b) /. float (n-b)))
1764 else 1.0
1767 snake f a b
1768 and summa f n a b =
1769 (* courtesy:
1770 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1771 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1772 let iv1 = iv f in
1773 let ins = float a *. iv1
1774 and outs = float (n-b) *. iv1 in
1775 let ones = b - a in
1776 ins +. outs +. float ones
1778 let rec set (_N, _A, _B) y sy =
1779 let sum = summa 1.0 _N _A _B in
1780 let dy = float (y - sy) in
1781 state.ghyll <- (
1782 let rec gf n y1 o =
1783 if n >= _N
1784 then state.ghyll <- noghyll
1785 else
1786 let go n =
1787 let s = scroll n _N _A _B in
1788 let y1 = y1 +. ((s *. dy) /. sum) in
1789 gotoy_and_clear_text (truncate y1);
1790 state.ghyll <- gf (n+1) y1;
1792 match o with
1793 | None -> go n
1794 | Some y' -> set (_N/2, 0, 0) y' state.y
1796 gf 0 (float state.y)
1799 match conf.ghyllscroll with
1800 | None ->
1801 gotoy_and_clear_text y
1802 | Some nab ->
1803 if state.ghyll == noghyll
1804 then set nab y state.y
1805 else state.ghyll (Some y)
1808 let gotopage n top =
1809 let y, h = getpageyh n in
1810 let y = y + (truncate (top *. float h)) in
1811 gotoghyll y
1814 let gotopage1 n top =
1815 let y = getpagey n in
1816 let y = y + top in
1817 gotoghyll y
1820 let invalidate s f =
1821 state.layout <- [];
1822 state.pdims <- [];
1823 state.rects <- [];
1824 state.rects1 <- [];
1825 match state.geomcmds with
1826 | ps, [] when String.length ps = 0 ->
1827 f ();
1828 state.geomcmds <- s, [];
1830 | ps, [] ->
1831 state.geomcmds <- ps, [s, f];
1833 | ps, (s', _) :: rest when s' = s ->
1834 state.geomcmds <- ps, ((s, f) :: rest);
1836 | ps, cmds ->
1837 state.geomcmds <- ps, ((s, f) :: cmds);
1840 let opendoc path password =
1841 state.path <- path;
1842 state.password <- password;
1843 state.gen <- state.gen + 1;
1844 state.docinfo <- [];
1846 setaalevel conf.aalevel;
1847 Wsi.settitle ("llpp " ^ Filename.basename path);
1848 wcmd "open %s\000%s\000" path password;
1849 invalidate "reqlayout"
1850 (fun () ->
1851 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1854 let scalecolor c =
1855 let c = c *. conf.colorscale in
1856 (c, c, c);
1859 let scalecolor2 (r, g, b) =
1860 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1863 let docolumns = function
1864 | Csingle _ ->
1865 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1866 let rec loop pageno pdimno pdim y ph pdims =
1867 if pageno = state.pagecount
1868 then ()
1869 else
1870 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1871 match pdims with
1872 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1873 pdimno+1, pdim, rest
1874 | _ ->
1875 pdimno, pdim, pdims
1877 let x = max 0 (((conf.winw - state.scrollw - w) / 2) - xoff) in
1878 let y = y +
1879 (if conf.presentation
1880 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1881 else (if pageno = 0 then 0 else calcips h)
1884 a.(pageno) <- (pdimno, x, y, pdim);
1885 loop (pageno+1) pdimno pdim (y + h) h pdims
1887 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1888 conf.columns <- Csingle a;
1890 | Cmulti ((columns, coverA, coverB), _) ->
1891 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1892 let rec loop pageno pdimno pdim x y rowh pdims =
1893 let rec fixrow m = if m = pageno then () else
1894 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1895 if h < rowh
1896 then (
1897 let y = y + (rowh - h) / 2 in
1898 a.(m) <- (pdimno, x, y, pdim);
1900 fixrow (m+1)
1902 if pageno = state.pagecount
1903 then fixrow (((pageno - 1) / columns) * columns)
1904 else
1905 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1906 match pdims with
1907 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1908 pdimno+1, pdim, rest
1909 | _ ->
1910 pdimno, pdim, pdims
1912 let x, y, rowh' =
1913 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1914 then (
1915 (conf.winw - state.scrollw - w) / 2,
1916 y + rowh + conf.interpagespace, h
1918 else (
1919 if (pageno - coverA) mod columns = 0
1920 then (
1921 (if conf.multicenter then
1922 (conf.winw - state.scrollw - state.w) / 2 else 0),
1923 y + rowh + (if pageno = 0 then 0 else conf.interpagespace), h
1925 else x, y, max rowh h
1928 if pageno > 1 && (pageno - coverA) mod columns = 0
1929 then fixrow (pageno - columns);
1930 a.(pageno) <- (pdimno, x, y, pdim);
1931 let x = x + w + xoff*2 + conf.interpagespace in
1932 loop (pageno+1) pdimno pdim x y rowh' pdims
1934 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1935 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1937 | Csplit (c, _) ->
1938 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1939 let rec loop pageno pdimno pdim y pdims =
1940 if pageno = state.pagecount
1941 then ()
1942 else
1943 let pdimno, ((_, w, h, _) as pdim), pdims =
1944 match pdims with
1945 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1946 pdimno+1, pdim, rest
1947 | _ ->
1948 pdimno, pdim, pdims
1950 let cw = w / c in
1951 let rec loop1 n x y =
1952 if n = c then y else (
1953 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1954 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1957 let y = loop1 0 0 y in
1958 loop (pageno+1) pdimno pdim y pdims
1960 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1961 conf.columns <- Csplit (c, a);
1964 let represent () =
1965 docolumns conf.columns;
1966 state.maxy <- calcheight ();
1967 state.hscrollh <-
1968 if state.w <= conf.winw - state.scrollw
1969 then 0
1970 else state.scrollw
1972 match state.mode with
1973 | Birdseye (_, _, pageno, _, _) ->
1974 let y, h = getpageyh pageno in
1975 let top = (conf.winh - h) / 2 in
1976 gotoy (max 0 (y - top))
1977 | _ -> gotoanchor state.anchor
1980 let reshape w h =
1981 GlDraw.viewport 0 0 w h;
1982 let firsttime = state.geomcmds == firstgeomcmds in
1983 if not firsttime && nogeomcmds state.geomcmds
1984 then state.anchor <- getanchor ();
1986 conf.winw <- w;
1987 let w = truncate (float w *. conf.zoom) - state.scrollw in
1988 let w = max w 2 in
1989 conf.winh <- h;
1990 setfontsize fstate.fontsize;
1991 GlMat.mode `modelview;
1992 GlMat.load_identity ();
1994 GlMat.mode `projection;
1995 GlMat.load_identity ();
1996 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1997 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1998 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
2000 let relx =
2001 if conf.zoom <= 1.0
2002 then 0.0
2003 else float state.x /. float state.w
2005 invalidate "geometry"
2006 (fun () ->
2007 state.w <- w;
2008 if not firsttime
2009 then state.x <- truncate (relx *. float w);
2010 let w =
2011 match conf.columns with
2012 | Csingle _ -> w
2013 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2014 | Csplit (c, _) -> w * c
2016 wcmd "geometry %d %d" w h);
2019 let enttext () =
2020 let len = String.length state.text in
2021 let drawstring s =
2022 let hscrollh =
2023 match state.mode with
2024 | Textentry _
2025 | View ->
2026 let h, _, _ = state.uioh#scrollpw in
2028 | _ -> 0
2030 let rect x w =
2031 GlDraw.rect
2032 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2033 (x+.w, float (conf.winh - hscrollh))
2036 let w = float (conf.winw - state.scrollw - 1) in
2037 if state.progress >= 0.0 && state.progress < 1.0
2038 then (
2039 GlDraw.color (0.3, 0.3, 0.3);
2040 let w1 = w *. state.progress in
2041 rect 0.0 w1;
2042 GlDraw.color (0.0, 0.0, 0.0);
2043 rect w1 (w-.w1)
2045 else (
2046 GlDraw.color (0.0, 0.0, 0.0);
2047 rect 0.0 w;
2050 GlDraw.color (1.0, 1.0, 1.0);
2051 drawstring fstate.fontsize
2052 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2054 let s =
2055 match state.mode with
2056 | Textentry ((prefix, text, _, _, _, _), _) ->
2057 let s =
2058 if len > 0
2059 then
2060 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2061 else
2062 Printf.sprintf "%s%s_" prefix text
2066 | _ -> state.text
2068 let s =
2069 if state.newerrmsgs
2070 then (
2071 if not (istextentry state.mode)
2072 then
2073 let s1 = "(press 'e' to review error messasges)" in
2074 if String.length s > 0 then s ^ " " ^ s1 else s1
2075 else s
2077 else s
2079 if String.length s > 0
2080 then drawstring s
2083 let gctiles () =
2084 let len = Queue.length state.tilelru in
2085 let layout = lazy (
2086 match state.throttle with
2087 | None ->
2088 if conf.preload
2089 then preloadlayout state.y
2090 else state.layout
2091 | Some (layout, _, _) ->
2092 layout
2093 ) in
2094 let rec loop qpos =
2095 if state.memused <= conf.memlimit
2096 then ()
2097 else (
2098 if qpos < len
2099 then
2100 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2101 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2102 let (_, pw, ph, _) = getpagedim n in
2104 gen = state.gen
2105 && colorspace = conf.colorspace
2106 && angle = conf.angle
2107 && pagew = pw
2108 && pageh = ph
2109 && (
2110 let x = col*conf.tilew
2111 and y = row*conf.tileh in
2112 tilevisible (Lazy.force_val layout) n x y
2114 then Queue.push lruitem state.tilelru
2115 else (
2116 wcmd "freetile %s" p;
2117 state.memused <- state.memused - s;
2118 state.uioh#infochanged Memused;
2119 Hashtbl.remove state.tilemap k;
2121 loop (qpos+1)
2124 loop 0
2127 let flushtiles () =
2128 Queue.iter (fun (k, p, s) ->
2129 wcmd "freetile %s" p;
2130 state.memused <- state.memused - s;
2131 state.uioh#infochanged Memused;
2132 Hashtbl.remove state.tilemap k;
2133 ) state.tilelru;
2134 Queue.clear state.tilelru;
2135 load state.layout;
2138 let logcurrently = function
2139 | Idle -> dolog "Idle"
2140 | Loading (l, gen) ->
2141 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2142 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2143 dolog
2144 "Tiling %d[%d,%d] page=%s cs=%s angle"
2145 l.pageno col row pageopaque
2146 (colorspace_to_string colorspace)
2148 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2149 angle gen conf.angle state.gen
2150 tilew tileh
2151 conf.tilew conf.tileh
2153 | Outlining _ ->
2154 dolog "outlining"
2157 let act cmds =
2158 (* dolog "%S" cmds; *)
2159 let op, args =
2160 let spacepos =
2161 try String.index cmds ' '
2162 with Not_found -> -1
2164 if spacepos = -1
2165 then cmds, ""
2166 else
2167 let l = String.length cmds in
2168 let op = String.sub cmds 0 spacepos in
2169 op, begin
2170 if l - spacepos < 2 then ""
2171 else String.sub cmds (spacepos+1) (l-spacepos-1)
2174 match op with
2175 | "clear" ->
2176 state.uioh#infochanged Pdim;
2177 state.pdims <- [];
2179 | "clearrects" ->
2180 state.rects <- state.rects1;
2181 G.postRedisplay "clearrects";
2183 | "continue" ->
2184 let n =
2185 try Scanf.sscanf args "%u" (fun n -> n)
2186 with exn ->
2187 dolog "error processing 'continue' %S: %s"
2188 cmds (Printexc.to_string exn);
2189 exit 1;
2191 state.pagecount <- n;
2192 begin match state.currently with
2193 | Outlining l ->
2194 state.currently <- Idle;
2195 state.outlines <- Array.of_list (List.rev l)
2196 | _ -> ()
2197 end;
2199 let cur, cmds = state.geomcmds in
2200 if String.length cur = 0
2201 then failwith "umpossible";
2203 begin match List.rev cmds with
2204 | [] ->
2205 state.geomcmds <- "", [];
2206 represent ();
2207 | (s, f) :: rest ->
2208 f ();
2209 state.geomcmds <- s, List.rev rest;
2210 end;
2211 if conf.maxwait = None
2212 then G.postRedisplay "continue";
2214 | "title" ->
2215 Wsi.settitle args
2217 | "msg" ->
2218 showtext ' ' args
2220 | "vmsg" ->
2221 if conf.verbose
2222 then showtext ' ' args
2224 | "progress" ->
2225 let progress, text =
2227 Scanf.sscanf args "%f %n"
2228 (fun f pos ->
2229 f, String.sub args pos (String.length args - pos))
2230 with exn ->
2231 dolog "error processing 'progress' %S: %s"
2232 cmds (Printexc.to_string exn);
2233 exit 1;
2235 state.text <- text;
2236 state.progress <- progress;
2237 G.postRedisplay "progress"
2239 | "firstmatch" ->
2240 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2242 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2243 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2244 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2245 with exn ->
2246 dolog "error processing 'firstmatch' %S: %s"
2247 cmds (Printexc.to_string exn);
2248 exit 1;
2250 let y = (getpagey pageno) + truncate y0 in
2251 addnav ();
2252 gotoy y;
2253 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2255 | "match" ->
2256 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2258 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2259 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2260 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2261 with exn ->
2262 dolog "error processing 'match' %S: %s"
2263 cmds (Printexc.to_string exn);
2264 exit 1;
2266 state.rects1 <-
2267 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2269 | "page" ->
2270 let pageopaque, t =
2272 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2273 with exn ->
2274 dolog "error processing 'page' %S: %s"
2275 cmds (Printexc.to_string exn);
2276 exit 1;
2278 begin match state.currently with
2279 | Loading (l, gen) ->
2280 vlog "page %d took %f sec" l.pageno t;
2281 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2282 begin match state.throttle with
2283 | None ->
2284 let preloadedpages =
2285 if conf.preload
2286 then preloadlayout state.y
2287 else state.layout
2289 let evict () =
2290 let module IntSet =
2291 Set.Make (struct type t = int let compare = (-) end) in
2292 let set =
2293 List.fold_left (fun s l -> IntSet.add l.pageno s)
2294 IntSet.empty preloadedpages
2296 let evictedpages =
2297 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2298 if not (IntSet.mem pageno set)
2299 then (
2300 wcmd "freepage %s" opaque;
2301 key :: accu
2303 else accu
2304 ) state.pagemap []
2306 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2308 evict ();
2309 state.currently <- Idle;
2310 if gen = state.gen
2311 then (
2312 tilepage l.pageno pageopaque state.layout;
2313 load state.layout;
2314 load preloadedpages;
2315 if pagevisible state.layout l.pageno
2316 && layoutready state.layout
2317 then G.postRedisplay "page";
2320 | Some (layout, _, _) ->
2321 state.currently <- Idle;
2322 tilepage l.pageno pageopaque layout;
2323 load state.layout
2324 end;
2326 | _ ->
2327 dolog "Inconsistent loading state";
2328 logcurrently state.currently;
2329 exit 1
2332 | "tile" ->
2333 let (x, y, opaque, size, t) =
2335 Scanf.sscanf args "%u %u %s %u %f"
2336 (fun x y p size t -> (x, y, p, size, t))
2337 with exn ->
2338 dolog "error processing 'tile' %S: %s"
2339 cmds (Printexc.to_string exn);
2340 exit 1;
2342 begin match state.currently with
2343 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2344 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2346 if tilew != conf.tilew || tileh != conf.tileh
2347 then (
2348 wcmd "freetile %s" opaque;
2349 state.currently <- Idle;
2350 load state.layout;
2352 else (
2353 puttileopaque l col row gen cs angle opaque size t;
2354 state.memused <- state.memused + size;
2355 state.uioh#infochanged Memused;
2356 gctiles ();
2357 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2358 opaque, size) state.tilelru;
2360 let layout =
2361 match state.throttle with
2362 | None -> state.layout
2363 | Some (layout, _, _) -> layout
2366 state.currently <- Idle;
2367 if gen = state.gen
2368 && conf.colorspace = cs
2369 && conf.angle = angle
2370 && tilevisible layout l.pageno x y
2371 then conttiling l.pageno pageopaque;
2373 begin match state.throttle with
2374 | None ->
2375 preload state.layout;
2376 if gen = state.gen
2377 && conf.colorspace = cs
2378 && conf.angle = angle
2379 && tilevisible state.layout l.pageno x y
2380 then G.postRedisplay "tile nothrottle";
2382 | Some (layout, y, _) ->
2383 let ready = layoutready layout in
2384 if ready
2385 then (
2386 state.y <- y;
2387 state.layout <- layout;
2388 state.throttle <- None;
2389 G.postRedisplay "throttle";
2391 else load layout;
2392 end;
2395 | _ ->
2396 dolog "Inconsistent tiling state";
2397 logcurrently state.currently;
2398 exit 1
2401 | "pdim" ->
2402 let pdim =
2404 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2405 with exn ->
2406 dolog "error processing 'pdim' %S: %s"
2407 cmds (Printexc.to_string exn);
2408 exit 1;
2410 state.uioh#infochanged Pdim;
2411 state.pdims <- pdim :: state.pdims
2413 | "o" ->
2414 let (l, n, t, h, pos) =
2416 Scanf.sscanf args "%u %u %d %u %n"
2417 (fun l n t h pos -> l, n, t, h, pos)
2418 with exn ->
2419 dolog "error processing 'o' %S: %s"
2420 cmds (Printexc.to_string exn);
2421 exit 1;
2423 let s = String.sub args pos (String.length args - pos) in
2424 let outline = (s, l, (n, float t /. float h, 0.0)) in
2425 begin match state.currently with
2426 | Outlining outlines ->
2427 state.currently <- Outlining (outline :: outlines)
2428 | Idle ->
2429 state.currently <- Outlining [outline]
2430 | currently ->
2431 dolog "invalid outlining state";
2432 logcurrently currently
2435 | "info" ->
2436 state.docinfo <- (1, args) :: state.docinfo
2438 | "infoend" ->
2439 state.uioh#infochanged Docinfo;
2440 state.docinfo <- List.rev state.docinfo
2442 | _ ->
2443 dolog "unknown cmd `%S'" cmds
2446 let onhist cb =
2447 let rc = cb.rc in
2448 let action = function
2449 | HCprev -> cbget cb ~-1
2450 | HCnext -> cbget cb 1
2451 | HCfirst -> cbget cb ~-(cb.rc)
2452 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2453 and cancel () = cb.rc <- rc
2454 in (action, cancel)
2457 let search pattern forward =
2458 if String.length pattern > 0
2459 then
2460 let pn, py =
2461 match state.layout with
2462 | [] -> 0, 0
2463 | l :: _ ->
2464 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2466 wcmd "search %d %d %d %d,%s\000"
2467 (btod conf.icase) pn py (btod forward) pattern;
2470 let intentry text key =
2471 let c =
2472 if key >= 32 && key < 127
2473 then Char.chr key
2474 else '\000'
2476 match c with
2477 | '0' .. '9' ->
2478 let text = addchar text c in
2479 TEcont text
2481 | _ ->
2482 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2483 TEcont text
2486 let linknentry text key =
2487 let c =
2488 if key >= 32 && key < 127
2489 then Char.chr key
2490 else '\000'
2492 match c with
2493 | 'a' .. 'z' ->
2494 let text = addchar text c in
2495 TEcont text
2497 | _ ->
2498 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2499 TEcont text
2502 let linkndone f s =
2503 if String.length s > 0
2504 then (
2505 let n =
2506 let l = String.length s in
2507 let rec loop pos n = if pos = l then n else
2508 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2509 loop (pos+1) (n*26 + m)
2510 in loop 0 0
2512 let rec loop n = function
2513 | [] -> ()
2514 | l :: rest ->
2515 match getopaque l.pageno with
2516 | None -> loop n rest
2517 | Some opaque ->
2518 let m = getlinkcount opaque in
2519 if n < m
2520 then (
2521 let under = getlink opaque n in
2522 f under
2524 else loop (n-m) rest
2526 loop n state.layout;
2530 let textentry text key =
2531 if key land 0xff00 = 0xff00
2532 then TEcont text
2533 else TEcont (text ^ Wsi.toutf8 key)
2536 let reqlayout angle proportional =
2537 match state.throttle with
2538 | None ->
2539 if nogeomcmds state.geomcmds
2540 then state.anchor <- getanchor ();
2541 conf.angle <- angle mod 360;
2542 if conf.angle != 0
2543 then (
2544 match state.mode with
2545 | LinkNav _ -> state.mode <- View
2546 | _ -> ()
2548 conf.proportional <- proportional;
2549 invalidate "reqlayout"
2550 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2551 | _ -> ()
2554 let settrim trimmargins trimfuzz =
2555 if nogeomcmds state.geomcmds
2556 then state.anchor <- getanchor ();
2557 conf.trimmargins <- trimmargins;
2558 conf.trimfuzz <- trimfuzz;
2559 let x0, y0, x1, y1 = trimfuzz in
2560 invalidate "settrim"
2561 (fun () ->
2562 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2563 Hashtbl.iter (fun _ opaque ->
2564 wcmd "freepage %s" opaque;
2565 ) state.pagemap;
2566 Hashtbl.clear state.pagemap;
2569 let setzoom zoom =
2570 match state.throttle with
2571 | None ->
2572 let zoom = max 0.01 zoom in
2573 if zoom <> conf.zoom
2574 then (
2575 state.prevzoom <- conf.zoom;
2576 conf.zoom <- zoom;
2577 reshape conf.winw conf.winh;
2578 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2581 | Some (layout, y, started) ->
2582 let time =
2583 match conf.maxwait with
2584 | None -> 0.0
2585 | Some t -> t
2587 let dt = now () -. started in
2588 if dt > time
2589 then (
2590 state.y <- y;
2591 load layout;
2595 let setcolumns mode columns coverA coverB =
2596 state.prevcolumns <- Some (conf.columns, conf.zoom);
2597 if columns < 0
2598 then (
2599 if isbirdseye mode
2600 then showtext '!' "split mode doesn't work in bird's eye"
2601 else (
2602 conf.columns <- Csplit (-columns, [||]);
2603 state.x <- 0;
2604 conf.zoom <- 1.0;
2607 else (
2608 if columns < 2
2609 then (
2610 conf.columns <- Csingle [||];
2611 state.x <- 0;
2612 setzoom 1.0;
2614 else (
2615 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2616 conf.zoom <- 1.0;
2619 reshape conf.winw conf.winh;
2622 let enterbirdseye () =
2623 let zoom = float conf.thumbw /. float conf.winw in
2624 let birdseyepageno =
2625 let cy = conf.winh / 2 in
2626 let fold = function
2627 | [] -> 0
2628 | l :: rest ->
2629 let rec fold best = function
2630 | [] -> best.pageno
2631 | l :: rest ->
2632 let d = cy - (l.pagedispy + l.pagevh/2)
2633 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2634 if abs d < abs dbest
2635 then fold l rest
2636 else best.pageno
2637 in fold l rest
2639 fold state.layout
2641 state.mode <- Birdseye (
2642 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2644 conf.zoom <- zoom;
2645 conf.presentation <- false;
2646 conf.interpagespace <- 10;
2647 conf.hlinks <- false;
2648 state.x <- 0;
2649 state.mstate <- Mnone;
2650 conf.maxwait <- None;
2651 conf.columns <- (
2652 match conf.beyecolumns with
2653 | Some c ->
2654 conf.zoom <- 1.0;
2655 Cmulti ((c, 0, 0), [||])
2656 | None -> Csingle [||]
2658 Wsi.setcursor Wsi.CURSOR_INHERIT;
2659 if conf.verbose
2660 then
2661 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2662 (100.0*.zoom)
2663 else
2664 state.text <- ""
2666 reshape conf.winw conf.winh;
2669 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2670 state.mode <- View;
2671 conf.zoom <- c.zoom;
2672 conf.presentation <- c.presentation;
2673 conf.interpagespace <- c.interpagespace;
2674 conf.maxwait <- c.maxwait;
2675 conf.hlinks <- c.hlinks;
2676 conf.beyecolumns <- (
2677 match conf.columns with
2678 | Cmulti ((c, _, _), _) -> Some c
2679 | Csingle _ -> None
2680 | Csplit _ -> failwith "leaving bird's eye split mode"
2682 conf.columns <- (
2683 match c.columns with
2684 | Cmulti (c, _) -> Cmulti (c, [||])
2685 | Csingle _ -> Csingle [||]
2686 | Csplit (c, _) -> Csplit (c, [||])
2688 state.x <- leftx;
2689 if conf.verbose
2690 then
2691 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2692 (100.0*.conf.zoom)
2694 reshape conf.winw conf.winh;
2695 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2698 let togglebirdseye () =
2699 match state.mode with
2700 | Birdseye vals -> leavebirdseye vals true
2701 | View -> enterbirdseye ()
2702 | _ -> ()
2705 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2706 let pageno = max 0 (pageno - incr) in
2707 let rec loop = function
2708 | [] -> gotopage1 pageno 0
2709 | l :: _ when l.pageno = pageno ->
2710 if l.pagedispy >= 0 && l.pagey = 0
2711 then G.postRedisplay "upbirdseye"
2712 else gotopage1 pageno 0
2713 | _ :: rest -> loop rest
2715 loop state.layout;
2716 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2719 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2720 let pageno = min (state.pagecount - 1) (pageno + incr) in
2721 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2722 let rec loop = function
2723 | [] ->
2724 let y, h = getpageyh pageno in
2725 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2726 gotoy (clamp dy)
2727 | l :: _ when l.pageno = pageno ->
2728 if l.pagevh != l.pageh
2729 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2730 else G.postRedisplay "downbirdseye"
2731 | _ :: rest -> loop rest
2733 loop state.layout
2736 let optentry mode _ key =
2737 let btos b = if b then "on" else "off" in
2738 if key >= 32 && key < 127
2739 then
2740 let c = Char.chr key in
2741 match c with
2742 | 's' ->
2743 let ondone s =
2744 try conf.scrollstep <- int_of_string s with exc ->
2745 state.text <- Printf.sprintf "bad integer `%s': %s"
2746 s (Printexc.to_string exc)
2748 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2750 | 'A' ->
2751 let ondone s =
2753 conf.autoscrollstep <- int_of_string s;
2754 if state.autoscroll <> None
2755 then state.autoscroll <- Some conf.autoscrollstep
2756 with exc ->
2757 state.text <- Printf.sprintf "bad integer `%s': %s"
2758 s (Printexc.to_string exc)
2760 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2762 | 'C' ->
2763 let ondone s =
2765 let n, a, b = multicolumns_of_string s in
2766 setcolumns mode n a b;
2767 with exc ->
2768 state.text <- Printf.sprintf "bad columns `%s': %s"
2769 s (Printexc.to_string exc)
2771 TEswitch ("columns: ", "", None, textentry, ondone, true)
2773 | 'Z' ->
2774 let ondone s =
2776 let zoom = float (int_of_string s) /. 100.0 in
2777 setzoom zoom
2778 with exc ->
2779 state.text <- Printf.sprintf "bad integer `%s': %s"
2780 s (Printexc.to_string exc)
2782 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2784 | 't' ->
2785 let ondone s =
2787 conf.thumbw <- bound (int_of_string s) 2 4096;
2788 state.text <-
2789 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2790 begin match mode with
2791 | Birdseye beye ->
2792 leavebirdseye beye false;
2793 enterbirdseye ();
2794 | _ -> ();
2796 with exc ->
2797 state.text <- Printf.sprintf "bad integer `%s': %s"
2798 s (Printexc.to_string exc)
2800 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2802 | 'R' ->
2803 let ondone s =
2804 match try
2805 Some (int_of_string s)
2806 with exc ->
2807 state.text <- Printf.sprintf "bad integer `%s': %s"
2808 s (Printexc.to_string exc);
2809 None
2810 with
2811 | Some angle -> reqlayout angle conf.proportional
2812 | None -> ()
2814 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2816 | 'i' ->
2817 conf.icase <- not conf.icase;
2818 TEdone ("case insensitive search " ^ (btos conf.icase))
2820 | 'p' ->
2821 conf.preload <- not conf.preload;
2822 gotoy state.y;
2823 TEdone ("preload " ^ (btos conf.preload))
2825 | 'v' ->
2826 conf.verbose <- not conf.verbose;
2827 TEdone ("verbose " ^ (btos conf.verbose))
2829 | 'd' ->
2830 conf.debug <- not conf.debug;
2831 TEdone ("debug " ^ (btos conf.debug))
2833 | 'h' ->
2834 conf.maxhfit <- not conf.maxhfit;
2835 state.maxy <- calcheight ();
2836 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2838 | 'c' ->
2839 conf.crophack <- not conf.crophack;
2840 TEdone ("crophack " ^ btos conf.crophack)
2842 | 'a' ->
2843 let s =
2844 match conf.maxwait with
2845 | None ->
2846 conf.maxwait <- Some infinity;
2847 "always wait for page to complete"
2848 | Some _ ->
2849 conf.maxwait <- None;
2850 "show placeholder if page is not ready"
2852 TEdone s
2854 | 'f' ->
2855 conf.underinfo <- not conf.underinfo;
2856 TEdone ("underinfo " ^ btos conf.underinfo)
2858 | 'P' ->
2859 conf.savebmarks <- not conf.savebmarks;
2860 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2862 | 'S' ->
2863 let ondone s =
2865 let pageno, py =
2866 match state.layout with
2867 | [] -> 0, 0
2868 | l :: _ ->
2869 l.pageno, l.pagey
2871 conf.interpagespace <- int_of_string s;
2872 docolumns conf.columns;
2873 state.maxy <- calcheight ();
2874 let y = getpagey pageno in
2875 gotoy (y + py)
2876 with exc ->
2877 state.text <- Printf.sprintf "bad integer `%s': %s"
2878 s (Printexc.to_string exc)
2880 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2882 | 'l' ->
2883 reqlayout conf.angle (not conf.proportional);
2884 TEdone ("proportional display " ^ btos conf.proportional)
2886 | 'T' ->
2887 settrim (not conf.trimmargins) conf.trimfuzz;
2888 TEdone ("trim margins " ^ btos conf.trimmargins)
2890 | 'I' ->
2891 conf.invert <- not conf.invert;
2892 TEdone ("invert colors " ^ btos conf.invert)
2894 | 'x' ->
2895 let ondone s =
2896 cbput state.hists.sel s;
2897 conf.selcmd <- s;
2899 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2900 textentry, ondone, true)
2902 | _ ->
2903 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2904 TEstop
2905 else
2906 TEcont state.text
2909 class type lvsource = object
2910 method getitemcount : int
2911 method getitem : int -> (string * int)
2912 method hasaction : int -> bool
2913 method exit :
2914 uioh:uioh ->
2915 cancel:bool ->
2916 active:int ->
2917 first:int ->
2918 pan:int ->
2919 qsearch:string ->
2920 uioh option
2921 method getactive : int
2922 method getfirst : int
2923 method getqsearch : string
2924 method setqsearch : string -> unit
2925 method getpan : int
2926 end;;
2928 class virtual lvsourcebase = object
2929 val mutable m_active = 0
2930 val mutable m_first = 0
2931 val mutable m_qsearch = ""
2932 val mutable m_pan = 0
2933 method getactive = m_active
2934 method getfirst = m_first
2935 method getqsearch = m_qsearch
2936 method getpan = m_pan
2937 method setqsearch s = m_qsearch <- s
2938 end;;
2940 let withoutlastutf8 s =
2941 let len = String.length s in
2942 if len = 0
2943 then s
2944 else
2945 let rec find pos =
2946 if pos = 0
2947 then pos
2948 else
2949 let b = Char.code s.[pos] in
2950 if b land 0b110000 = 0b11000000
2951 then find (pos-1)
2952 else pos-1
2954 let first =
2955 if Char.code s.[len-1] land 0x80 = 0
2956 then len-1
2957 else find (len-1)
2959 String.sub s 0 first;
2962 let textentrykeyboard
2963 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2964 let enttext te =
2965 state.mode <- Textentry (te, onleave);
2966 state.text <- "";
2967 enttext ();
2968 G.postRedisplay "textentrykeyboard enttext";
2970 let histaction cmd =
2971 match opthist with
2972 | None -> ()
2973 | Some (action, _) ->
2974 state.mode <- Textentry (
2975 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2977 G.postRedisplay "textentry histaction"
2979 match key with
2980 | 0xff08 -> (* backspace *)
2981 let s = withoutlastutf8 text in
2982 let len = String.length s in
2983 if cancelonempty && len = 0
2984 then (
2985 onleave Cancel;
2986 G.postRedisplay "textentrykeyboard after cancel";
2988 else (
2989 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2992 | 0xff0d ->
2993 ondone text;
2994 onleave Confirm;
2995 G.postRedisplay "textentrykeyboard after confirm"
2997 | 0xff52 -> histaction HCprev
2998 | 0xff54 -> histaction HCnext
2999 | 0xff50 -> histaction HCfirst
3000 | 0xff57 -> histaction HClast
3002 | 0xff1b -> (* escape*)
3003 if String.length text = 0
3004 then (
3005 begin match opthist with
3006 | None -> ()
3007 | Some (_, onhistcancel) -> onhistcancel ()
3008 end;
3009 onleave Cancel;
3010 state.text <- "";
3011 G.postRedisplay "textentrykeyboard after cancel2"
3013 else (
3014 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3017 | 0xff9f | 0xffff -> () (* delete *)
3019 | _ when key != 0 && key land 0xff00 != 0xff00 ->
3020 begin match onkey text key with
3021 | TEdone text ->
3022 ondone text;
3023 onleave Confirm;
3024 G.postRedisplay "textentrykeyboard after confirm2";
3026 | TEcont text ->
3027 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3029 | TEstop ->
3030 onleave Cancel;
3031 G.postRedisplay "textentrykeyboard after cancel3"
3033 | TEswitch te ->
3034 state.mode <- Textentry (te, onleave);
3035 G.postRedisplay "textentrykeyboard switch";
3036 end;
3038 | _ ->
3039 vlog "unhandled key %s" (Wsi.keyname key)
3042 let firstof first active =
3043 if first > active || abs (first - active) > fstate.maxrows - 1
3044 then max 0 (active - (fstate.maxrows/2))
3045 else first
3048 let calcfirst first active =
3049 if active > first
3050 then
3051 let rows = active - first in
3052 if rows > fstate.maxrows then active - fstate.maxrows else first
3053 else active
3056 let scrollph y maxy =
3057 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3058 let sh = float conf.winh /. sh in
3059 let sh = max sh (float conf.scrollh) in
3061 let percent =
3062 if y = state.maxy
3063 then 1.0
3064 else float y /. float maxy
3066 let position = (float conf.winh -. sh) *. percent in
3068 let position =
3069 if position +. sh > float conf.winh
3070 then float conf.winh -. sh
3071 else position
3073 position, sh;
3076 let coe s = (s :> uioh);;
3078 class listview ~(source:lvsource) ~trusted ~modehash =
3079 object (self)
3080 val m_pan = source#getpan
3081 val m_first = source#getfirst
3082 val m_active = source#getactive
3083 val m_qsearch = source#getqsearch
3084 val m_prev_uioh = state.uioh
3086 method private elemunder y =
3087 let n = y / (fstate.fontsize+1) in
3088 if m_first + n < source#getitemcount
3089 then (
3090 if source#hasaction (m_first + n)
3091 then Some (m_first + n)
3092 else None
3094 else None
3096 method display =
3097 Gl.enable `blend;
3098 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3099 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3100 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3101 GlDraw.color (1., 1., 1.);
3102 Gl.enable `texture_2d;
3103 let fs = fstate.fontsize in
3104 let nfs = fs + 1 in
3105 let ww = fstate.wwidth in
3106 let tabw = 30.0*.ww in
3107 let itemcount = source#getitemcount in
3108 let rec loop row =
3109 if (row - m_first) * nfs > conf.winh
3110 then ()
3111 else (
3112 if row >= 0 && row < itemcount
3113 then (
3114 let (s, level) = source#getitem row in
3115 let y = (row - m_first) * nfs in
3116 let x = 5.0 +. float (level + m_pan) *. ww in
3117 if row = m_active
3118 then (
3119 Gl.disable `texture_2d;
3120 GlDraw.polygon_mode `both `line;
3121 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3122 GlDraw.rect (1., float (y + 1))
3123 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3124 GlDraw.polygon_mode `both `fill;
3125 GlDraw.color (1., 1., 1.);
3126 Gl.enable `texture_2d;
3129 let drawtabularstring s =
3130 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3131 if trusted
3132 then
3133 let tabpos = try String.index s '\t' with Not_found -> -1 in
3134 if tabpos > 0
3135 then
3136 let len = String.length s - tabpos - 1 in
3137 let s1 = String.sub s 0 tabpos
3138 and s2 = String.sub s (tabpos + 1) len in
3139 let nx = drawstr x s1 in
3140 let sw = nx -. x in
3141 let x = x +. (max tabw sw) in
3142 drawstr x s2
3143 else
3144 drawstr x s
3145 else
3146 drawstr x s
3148 let _ = drawtabularstring s in
3149 loop (row+1)
3153 loop m_first;
3154 Gl.disable `blend;
3155 Gl.disable `texture_2d;
3157 method updownlevel incr =
3158 let len = source#getitemcount in
3159 let curlevel =
3160 if m_active >= 0 && m_active < len
3161 then snd (source#getitem m_active)
3162 else -1
3164 let rec flow i =
3165 if i = len then i-1 else if i = -1 then 0 else
3166 let _, l = source#getitem i in
3167 if l != curlevel then i else flow (i+incr)
3169 let active = flow m_active in
3170 let first = calcfirst m_first active in
3171 G.postRedisplay "outline updownlevel";
3172 {< m_active = active; m_first = first >}
3174 method private key1 key mask =
3175 let set1 active first qsearch =
3176 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3178 let search active pattern incr =
3179 let dosearch re =
3180 let rec loop n =
3181 if n >= 0 && n < source#getitemcount
3182 then (
3183 let s, _ = source#getitem n in
3185 (try ignore (Str.search_forward re s 0); true
3186 with Not_found -> false)
3187 then Some n
3188 else loop (n + incr)
3190 else None
3192 loop active
3195 let re = Str.regexp_case_fold pattern in
3196 dosearch re
3197 with Failure s ->
3198 state.text <- s;
3199 None
3201 let itemcount = source#getitemcount in
3202 let find start incr =
3203 let rec find i =
3204 if i = -1 || i = itemcount
3205 then -1
3206 else (
3207 if source#hasaction i
3208 then i
3209 else find (i + incr)
3212 find start
3214 let set active first =
3215 let first = bound first 0 (itemcount - fstate.maxrows) in
3216 state.text <- "";
3217 coe {< m_active = active; m_first = first >}
3219 let navigate incr =
3220 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3221 let active, first =
3222 let incr1 = if incr > 0 then 1 else -1 in
3223 if isvisible m_first m_active
3224 then
3225 let next =
3226 let next = m_active + incr in
3227 let next =
3228 if next < 0 || next >= itemcount
3229 then -1
3230 else find next incr1
3232 if next = -1 || abs (m_active - next) > fstate.maxrows
3233 then -1
3234 else next
3236 if next = -1
3237 then
3238 let first = m_first + incr in
3239 let first = bound first 0 (itemcount - 1) in
3240 let next =
3241 let next = m_active + incr in
3242 let next = bound next 0 (itemcount - 1) in
3243 find next ~-incr1
3245 let active = if next = -1 then m_active else next in
3246 active, first
3247 else
3248 let first = min next m_first in
3249 let first =
3250 if abs (next - first) > fstate.maxrows
3251 then first + incr
3252 else first
3254 next, first
3255 else
3256 let first = m_first + incr in
3257 let first = bound first 0 (itemcount - 1) in
3258 let active =
3259 let next = m_active + incr in
3260 let next = bound next 0 (itemcount - 1) in
3261 let next = find next incr1 in
3262 let active =
3263 if next = -1 || abs (m_active - first) > fstate.maxrows
3264 then (
3265 let active = if m_active = -1 then next else m_active in
3266 active
3268 else next
3270 if isvisible first active
3271 then active
3272 else -1
3274 active, first
3276 G.postRedisplay "listview navigate";
3277 set active first;
3279 match key with
3280 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3281 let incr = if key = 0x72 then -1 else 1 in
3282 let active, first =
3283 match search (m_active + incr) m_qsearch incr with
3284 | None ->
3285 state.text <- m_qsearch ^ " [not found]";
3286 m_active, m_first
3287 | Some active ->
3288 state.text <- m_qsearch;
3289 active, firstof m_first active
3291 G.postRedisplay "listview ctrl-r/s";
3292 set1 active first m_qsearch;
3294 | 0xff08 -> (* backspace *)
3295 if String.length m_qsearch = 0
3296 then coe self
3297 else (
3298 let qsearch = withoutlastutf8 m_qsearch in
3299 let len = String.length qsearch in
3300 if len = 0
3301 then (
3302 state.text <- "";
3303 G.postRedisplay "listview empty qsearch";
3304 set1 m_active m_first "";
3306 else
3307 let active, first =
3308 match search m_active qsearch ~-1 with
3309 | None ->
3310 state.text <- qsearch ^ " [not found]";
3311 m_active, m_first
3312 | Some active ->
3313 state.text <- qsearch;
3314 active, firstof m_first active
3316 G.postRedisplay "listview backspace qsearch";
3317 set1 active first qsearch
3320 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3321 let pattern = m_qsearch ^ Wsi.toutf8 key in
3322 let active, first =
3323 match search m_active pattern 1 with
3324 | None ->
3325 state.text <- pattern ^ " [not found]";
3326 m_active, m_first
3327 | Some active ->
3328 state.text <- pattern;
3329 active, firstof m_first active
3331 G.postRedisplay "listview qsearch add";
3332 set1 active first pattern;
3334 | 0xff1b -> (* escape *)
3335 state.text <- "";
3336 if String.length m_qsearch = 0
3337 then (
3338 G.postRedisplay "list view escape";
3339 begin
3340 match
3341 source#exit (coe self) true m_active m_first m_pan m_qsearch
3342 with
3343 | None -> m_prev_uioh
3344 | Some uioh -> uioh
3347 else (
3348 G.postRedisplay "list view kill qsearch";
3349 source#setqsearch "";
3350 coe {< m_qsearch = "" >}
3353 | 0xff0d -> (* return *)
3354 state.text <- "";
3355 let self = {< m_qsearch = "" >} in
3356 source#setqsearch "";
3357 let opt =
3358 G.postRedisplay "listview enter";
3359 if m_active >= 0 && m_active < source#getitemcount
3360 then (
3361 source#exit (coe self) false m_active m_first m_pan "";
3363 else (
3364 source#exit (coe self) true m_active m_first m_pan "";
3367 begin match opt with
3368 | None -> m_prev_uioh
3369 | Some uioh -> uioh
3372 | 0xff9f | 0xffff -> (* delete *)
3373 coe self
3375 | 0xff52 -> navigate ~-1 (* up *)
3376 | 0xff54 -> navigate 1 (* down *)
3377 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3378 | 0xff56 -> navigate fstate.maxrows (* next *)
3380 | 0xff53 -> (* right *)
3381 state.text <- "";
3382 G.postRedisplay "listview right";
3383 coe {< m_pan = m_pan - 1 >}
3385 | 0xff51 -> (* left *)
3386 state.text <- "";
3387 G.postRedisplay "listview left";
3388 coe {< m_pan = m_pan + 1 >}
3390 | 0xff50 -> (* home *)
3391 let active = find 0 1 in
3392 G.postRedisplay "listview home";
3393 set active 0;
3395 | 0xff57 -> (* end *)
3396 let first = max 0 (itemcount - fstate.maxrows) in
3397 let active = find (itemcount - 1) ~-1 in
3398 G.postRedisplay "listview end";
3399 set active first;
3401 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3402 coe self
3404 | _ ->
3405 dolog "listview unknown key %#x" key; coe self
3407 method key key mask =
3408 match state.mode with
3409 | Textentry te -> textentrykeyboard key mask te; coe self
3410 | _ -> self#key1 key mask
3412 method button button down x y _ =
3413 let opt =
3414 match button with
3415 | 1 when x > conf.winw - conf.scrollbw ->
3416 G.postRedisplay "listview scroll";
3417 if down
3418 then
3419 let _, position, sh = self#scrollph in
3420 if y > truncate position && y < truncate (position +. sh)
3421 then (
3422 state.mstate <- Mscrolly;
3423 Some (coe self)
3425 else
3426 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3427 let first = truncate (s *. float source#getitemcount) in
3428 let first = min source#getitemcount first in
3429 Some (coe {< m_first = first; m_active = first >})
3430 else (
3431 state.mstate <- Mnone;
3432 Some (coe self);
3434 | 1 when not down ->
3435 begin match self#elemunder y with
3436 | Some n ->
3437 G.postRedisplay "listview click";
3438 source#exit
3439 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3440 | _ ->
3441 Some (coe self)
3443 | n when (n == 4 || n == 5) && not down ->
3444 let len = source#getitemcount in
3445 let first =
3446 if n = 5 && m_first + fstate.maxrows >= len
3447 then
3448 m_first
3449 else
3450 let first = m_first + (if n == 4 then -1 else 1) in
3451 bound first 0 (len - 1)
3453 G.postRedisplay "listview wheel";
3454 Some (coe {< m_first = first >})
3455 | n when (n = 6 || n = 7) && not down ->
3456 let inc = m_first + (if n = 7 then -1 else 1) in
3457 G.postRedisplay "listview hwheel";
3458 Some (coe {< m_pan = m_pan + inc >})
3459 | _ ->
3460 Some (coe self)
3462 match opt with
3463 | None -> m_prev_uioh
3464 | Some uioh -> uioh
3466 method motion _ y =
3467 match state.mstate with
3468 | Mscrolly ->
3469 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3470 let first = truncate (s *. float source#getitemcount) in
3471 let first = min source#getitemcount first in
3472 G.postRedisplay "listview motion";
3473 coe {< m_first = first; m_active = first >}
3474 | _ -> coe self
3476 method pmotion x y =
3477 if x < conf.winw - conf.scrollbw
3478 then
3479 let n =
3480 match self#elemunder y with
3481 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3482 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3484 let o =
3485 if n != m_active
3486 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3487 else self
3489 coe o
3490 else (
3491 Wsi.setcursor Wsi.CURSOR_INHERIT;
3492 coe self
3495 method infochanged _ = ()
3497 method scrollpw = (0, 0.0, 0.0)
3498 method scrollph =
3499 let nfs = fstate.fontsize + 1 in
3500 let y = m_first * nfs in
3501 let itemcount = source#getitemcount in
3502 let maxi = max 0 (itemcount - fstate.maxrows) in
3503 let maxy = maxi * nfs in
3504 let p, h = scrollph y maxy in
3505 conf.scrollbw, p, h
3507 method modehash = modehash
3508 end;;
3510 class outlinelistview ~source =
3511 object (self)
3512 inherit listview
3513 ~source:(source :> lvsource)
3514 ~trusted:false
3515 ~modehash:(findkeyhash conf "outline")
3516 as super
3518 method key key mask =
3519 let calcfirst first active =
3520 if active > first
3521 then
3522 let rows = active - first in
3523 let maxrows =
3524 if String.length state.text = 0
3525 then fstate.maxrows
3526 else fstate.maxrows - 2
3528 if rows > maxrows then active - maxrows else first
3529 else active
3531 let navigate incr =
3532 let active = m_active + incr in
3533 let active = bound active 0 (source#getitemcount - 1) in
3534 let first = calcfirst m_first active in
3535 G.postRedisplay "outline navigate";
3536 coe {< m_active = active; m_first = first >}
3538 let ctrl = Wsi.withctrl mask in
3539 match key with
3540 | 110 when ctrl -> (* ctrl-n *)
3541 source#narrow m_qsearch;
3542 G.postRedisplay "outline ctrl-n";
3543 coe {< m_first = 0; m_active = 0 >}
3545 | 117 when ctrl -> (* ctrl-u *)
3546 source#denarrow;
3547 G.postRedisplay "outline ctrl-u";
3548 state.text <- "";
3549 coe {< m_first = 0; m_active = 0 >}
3551 | 108 when ctrl -> (* ctrl-l *)
3552 let first = m_active - (fstate.maxrows / 2) in
3553 G.postRedisplay "outline ctrl-l";
3554 coe {< m_first = first >}
3556 | 0xff9f | 0xffff -> (* delete *)
3557 source#remove m_active;
3558 G.postRedisplay "outline delete";
3559 let active = max 0 (m_active-1) in
3560 coe {< m_first = firstof m_first active;
3561 m_active = active >}
3563 | 0xff52 -> navigate ~-1 (* up *)
3564 | 0xff54 -> navigate 1 (* down *)
3565 | 0xff55 -> (* prior *)
3566 navigate ~-(fstate.maxrows)
3567 | 0xff56 -> (* next *)
3568 navigate fstate.maxrows
3570 | 0xff53 -> (* [ctrl-]right *)
3571 let o =
3572 if ctrl
3573 then (
3574 G.postRedisplay "outline ctrl right";
3575 {< m_pan = m_pan + 1 >}
3577 else self#updownlevel 1
3579 coe o
3581 | 0xff51 -> (* [ctrl-]left *)
3582 let o =
3583 if ctrl
3584 then (
3585 G.postRedisplay "outline ctrl left";
3586 {< m_pan = m_pan - 1 >}
3588 else self#updownlevel ~-1
3590 coe o
3592 | 0xff50 -> (* home *)
3593 G.postRedisplay "outline home";
3594 coe {< m_first = 0; m_active = 0 >}
3596 | 0xff57 -> (* end *)
3597 let active = source#getitemcount - 1 in
3598 let first = max 0 (active - fstate.maxrows) in
3599 G.postRedisplay "outline end";
3600 coe {< m_active = active; m_first = first >}
3602 | _ -> super#key key mask
3605 let outlinesource usebookmarks =
3606 let empty = [||] in
3607 (object
3608 inherit lvsourcebase
3609 val mutable m_items = empty
3610 val mutable m_orig_items = empty
3611 val mutable m_prev_items = empty
3612 val mutable m_narrow_pattern = ""
3613 val mutable m_hadremovals = false
3615 method getitemcount =
3616 Array.length m_items + (if m_hadremovals then 1 else 0)
3618 method getitem n =
3619 if n == Array.length m_items && m_hadremovals
3620 then
3621 ("[Confirm removal]", 0)
3622 else
3623 let s, n, _ = m_items.(n) in
3624 (s, n)
3626 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3627 ignore (uioh, first, qsearch);
3628 let confrimremoval = m_hadremovals && active = Array.length m_items in
3629 let items =
3630 if String.length m_narrow_pattern = 0
3631 then m_orig_items
3632 else m_items
3634 if not cancel
3635 then (
3636 if not confrimremoval
3637 then(
3638 let _, _, anchor = m_items.(active) in
3639 gotoanchor anchor;
3640 m_items <- items;
3642 else (
3643 state.bookmarks <- Array.to_list m_items;
3644 m_orig_items <- m_items;
3647 else m_items <- items;
3648 m_pan <- pan;
3649 None
3651 method hasaction _ = true
3653 method greetmsg =
3654 if Array.length m_items != Array.length m_orig_items
3655 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3656 else ""
3658 method narrow pattern =
3659 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3660 match reopt with
3661 | None -> ()
3662 | Some re ->
3663 let rec loop accu n =
3664 if n = -1
3665 then (
3666 m_narrow_pattern <- pattern;
3667 m_items <- Array.of_list accu
3669 else
3670 let (s, _, _) as o = m_items.(n) in
3671 let accu =
3672 if (try ignore (Str.search_forward re s 0); true
3673 with Not_found -> false)
3674 then o :: accu
3675 else accu
3677 loop accu (n-1)
3679 loop [] (Array.length m_items - 1)
3681 method denarrow =
3682 m_orig_items <- (
3683 if usebookmarks
3684 then Array.of_list state.bookmarks
3685 else state.outlines
3687 m_items <- m_orig_items
3689 method remove m =
3690 if usebookmarks
3691 then
3692 if m >= 0 && m < Array.length m_items
3693 then (
3694 m_hadremovals <- true;
3695 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3696 let n = if n >= m then n+1 else n in
3697 m_items.(n)
3701 method reset anchor items =
3702 m_hadremovals <- false;
3703 if m_orig_items == empty || m_prev_items != items
3704 then (
3705 m_orig_items <- items;
3706 if String.length m_narrow_pattern = 0
3707 then m_items <- items;
3709 m_prev_items <- items;
3710 let rely = getanchory anchor in
3711 let active =
3712 let rec loop n best bestd =
3713 if n = Array.length m_items
3714 then best
3715 else
3716 let (_, _, anchor) = m_items.(n) in
3717 let orely = getanchory anchor in
3718 let d = abs (orely - rely) in
3719 if d < bestd
3720 then loop (n+1) n d
3721 else loop (n+1) best bestd
3723 loop 0 ~-1 max_int
3725 m_active <- active;
3726 m_first <- firstof m_first active
3727 end)
3730 let enterselector usebookmarks =
3731 let source = outlinesource usebookmarks in
3732 fun errmsg ->
3733 let outlines =
3734 if usebookmarks
3735 then Array.of_list state.bookmarks
3736 else state.outlines
3738 if Array.length outlines = 0
3739 then (
3740 showtext ' ' errmsg;
3742 else (
3743 state.text <- source#greetmsg;
3744 Wsi.setcursor Wsi.CURSOR_INHERIT;
3745 let anchor = getanchor () in
3746 source#reset anchor outlines;
3747 state.uioh <- coe (new outlinelistview ~source);
3748 G.postRedisplay "enter selector";
3752 let enteroutlinemode =
3753 let f = enterselector false in
3754 fun ()-> f "Document has no outline";
3757 let enterbookmarkmode =
3758 let f = enterselector true in
3759 fun () -> f "Document has no bookmarks (yet)";
3762 let color_of_string s =
3763 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3764 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3768 let color_to_string (r, g, b) =
3769 let r = truncate (r *. 256.0)
3770 and g = truncate (g *. 256.0)
3771 and b = truncate (b *. 256.0) in
3772 Printf.sprintf "%d/%d/%d" r g b
3775 let irect_of_string s =
3776 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3779 let irect_to_string (x0,y0,x1,y1) =
3780 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3783 let makecheckers () =
3784 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3785 following to say:
3786 converted by Issac Trotts. July 25, 2002 *)
3787 let image_height = 64
3788 and image_width = 64 in
3790 let make_image () =
3791 let image =
3792 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3794 for i = 0 to image_width - 1 do
3795 for j = 0 to image_height - 1 do
3796 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3797 (if (i land 8 ) lxor (j land 8) = 0
3798 then [|255;255;255|] else [|200;200;200|])
3799 done
3800 done;
3801 image
3803 let image = make_image () in
3804 let id = GlTex.gen_texture () in
3805 GlTex.bind_texture `texture_2d id;
3806 GlPix.store (`unpack_alignment 1);
3807 GlTex.image2d image;
3808 List.iter (GlTex.parameter ~target:`texture_2d)
3809 [ `wrap_s `repeat;
3810 `wrap_t `repeat;
3811 `mag_filter `nearest;
3812 `min_filter `nearest ];
3816 let setcheckers enabled =
3817 match state.texid with
3818 | None ->
3819 if enabled then state.texid <- Some (makecheckers ())
3821 | Some texid ->
3822 if not enabled
3823 then (
3824 GlTex.delete_texture texid;
3825 state.texid <- None;
3829 let int_of_string_with_suffix s =
3830 let l = String.length s in
3831 let s1, shift =
3832 if l > 1
3833 then
3834 let suffix = Char.lowercase s.[l-1] in
3835 match suffix with
3836 | 'k' -> String.sub s 0 (l-1), 10
3837 | 'm' -> String.sub s 0 (l-1), 20
3838 | 'g' -> String.sub s 0 (l-1), 30
3839 | _ -> s, 0
3840 else s, 0
3842 let n = int_of_string s1 in
3843 let m = n lsl shift in
3844 if m < 0 || m < n
3845 then raise (Failure "value too large")
3846 else m
3849 let string_with_suffix_of_int n =
3850 if n = 0
3851 then "0"
3852 else
3853 let n, s =
3854 if n land ((1 lsl 20) - 1) = 0
3855 then n lsr 20, "M"
3856 else (
3857 if n land ((1 lsl 10) - 1) = 0
3858 then n lsr 10, "K"
3859 else n, ""
3862 let rec loop s n =
3863 let h = n mod 1000 in
3864 let n = n / 1000 in
3865 if n = 0
3866 then string_of_int h ^ s
3867 else (
3868 let s = Printf.sprintf "_%03d%s" h s in
3869 loop s n
3872 loop "" n ^ s;
3875 let defghyllscroll = (40, 8, 32);;
3876 let ghyllscroll_of_string s =
3877 let (n, a, b) as nab =
3878 if s = "default"
3879 then defghyllscroll
3880 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3882 if n <= a || n <= b || a >= b
3883 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3884 nab;
3887 let ghyllscroll_to_string ((n, a, b) as nab) =
3888 if nab = defghyllscroll
3889 then "default"
3890 else Printf.sprintf "%d,%d,%d" n a b;
3893 let describe_location () =
3894 let f (fn, _) l =
3895 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3897 let fn, ln = List.fold_left f (-1, -1) state.layout in
3898 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3899 let percent =
3900 if maxy <= 0
3901 then 100.
3902 else (100. *. (float state.y /. float maxy))
3904 if fn = ln
3905 then
3906 Printf.sprintf "page %d of %d [%.2f%%]"
3907 (fn+1) state.pagecount percent
3908 else
3909 Printf.sprintf
3910 "pages %d-%d of %d [%.2f%%]"
3911 (fn+1) (ln+1) state.pagecount percent
3914 let setpresentationmode v =
3915 let (n, _, _) = getanchor () in
3916 let _, h = getpageyh n in
3917 state.anchor <- (n, 0.0, float (calcips h));
3918 conf.presentation <- v;
3919 if conf.presentation
3920 then (
3921 if not conf.scrollbarinpm
3922 then state.scrollw <- 0;
3924 else state.scrollw <- conf.scrollbw;
3925 represent ();
3928 let enterinfomode =
3929 let btos b = if b then "\xe2\x88\x9a" else "" in
3930 let showextended = ref false in
3931 let leave mode = function
3932 | Confirm -> state.mode <- mode
3933 | Cancel -> state.mode <- mode in
3934 let src =
3935 (object
3936 val mutable m_first_time = true
3937 val mutable m_l = []
3938 val mutable m_a = [||]
3939 val mutable m_prev_uioh = nouioh
3940 val mutable m_prev_mode = View
3942 inherit lvsourcebase
3944 method reset prev_mode prev_uioh =
3945 m_a <- Array.of_list (List.rev m_l);
3946 m_l <- [];
3947 m_prev_mode <- prev_mode;
3948 m_prev_uioh <- prev_uioh;
3949 if m_first_time
3950 then (
3951 let rec loop n =
3952 if n >= Array.length m_a
3953 then ()
3954 else
3955 match m_a.(n) with
3956 | _, _, _, Action _ -> m_active <- n
3957 | _ -> loop (n+1)
3959 loop 0;
3960 m_first_time <- false;
3963 method int name get set =
3964 m_l <-
3965 (name, `int get, 1, Action (
3966 fun u ->
3967 let ondone s =
3968 try set (int_of_string s)
3969 with exn ->
3970 state.text <- Printf.sprintf "bad integer `%s': %s"
3971 s (Printexc.to_string exn)
3973 state.text <- "";
3974 let te = name ^ ": ", "", None, intentry, ondone, true in
3975 state.mode <- Textentry (te, leave m_prev_mode);
3977 )) :: m_l
3979 method int_with_suffix name get set =
3980 m_l <-
3981 (name, `intws get, 1, Action (
3982 fun u ->
3983 let ondone s =
3984 try set (int_of_string_with_suffix s)
3985 with exn ->
3986 state.text <- Printf.sprintf "bad integer `%s': %s"
3987 s (Printexc.to_string exn)
3989 state.text <- "";
3990 let te =
3991 name ^ ": ", "", None, intentry_with_suffix, ondone, true
3993 state.mode <- Textentry (te, leave m_prev_mode);
3995 )) :: m_l
3997 method bool ?(offset=1) ?(btos=btos) name get set =
3998 m_l <-
3999 (name, `bool (btos, get), offset, Action (
4000 fun u ->
4001 let v = get () in
4002 set (not v);
4004 )) :: m_l
4006 method color name get set =
4007 m_l <-
4008 (name, `color get, 1, Action (
4009 fun u ->
4010 let invalid = (nan, nan, nan) in
4011 let ondone s =
4012 let c =
4013 try color_of_string s
4014 with exn ->
4015 state.text <- Printf.sprintf "bad color `%s': %s"
4016 s (Printexc.to_string exn);
4017 invalid
4019 if c <> invalid
4020 then set c;
4022 let te = name ^ ": ", "", None, textentry, ondone, true in
4023 state.text <- color_to_string (get ());
4024 state.mode <- Textentry (te, leave m_prev_mode);
4026 )) :: m_l
4028 method string name get set =
4029 m_l <-
4030 (name, `string get, 1, Action (
4031 fun u ->
4032 let ondone s = set s in
4033 let te = name ^ ": ", "", None, textentry, ondone, true in
4034 state.mode <- Textentry (te, leave m_prev_mode);
4036 )) :: m_l
4038 method colorspace name get set =
4039 m_l <-
4040 (name, `string get, 1, Action (
4041 fun _ ->
4042 let source =
4043 let vals = [| "rgb"; "bgr"; "gray" |] in
4044 (object
4045 inherit lvsourcebase
4047 initializer
4048 m_active <- int_of_colorspace conf.colorspace;
4049 m_first <- 0;
4051 method getitemcount = Array.length vals
4052 method getitem n = (vals.(n), 0)
4053 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4054 ignore (uioh, first, pan, qsearch);
4055 if not cancel then set active;
4056 None
4057 method hasaction _ = true
4058 end)
4060 state.text <- "";
4061 let modehash = findkeyhash conf "info" in
4062 coe (new listview ~source ~trusted:true ~modehash)
4063 )) :: m_l
4065 method caption s offset =
4066 m_l <- (s, `empty, offset, Noaction) :: m_l
4068 method caption2 s f offset =
4069 m_l <- (s, `string f, offset, Noaction) :: m_l
4071 method getitemcount = Array.length m_a
4073 method getitem n =
4074 let tostr = function
4075 | `int f -> string_of_int (f ())
4076 | `intws f -> string_with_suffix_of_int (f ())
4077 | `string f -> f ()
4078 | `color f -> color_to_string (f ())
4079 | `bool (btos, f) -> btos (f ())
4080 | `empty -> ""
4082 let name, t, offset, _ = m_a.(n) in
4083 ((let s = tostr t in
4084 if String.length s > 0
4085 then Printf.sprintf "%s\t%s" name s
4086 else name),
4087 offset)
4089 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4090 let uiohopt =
4091 if not cancel
4092 then (
4093 m_qsearch <- qsearch;
4094 let uioh =
4095 match m_a.(active) with
4096 | _, _, _, Action f -> f uioh
4097 | _ -> uioh
4099 Some uioh
4101 else None
4103 m_active <- active;
4104 m_first <- first;
4105 m_pan <- pan;
4106 uiohopt
4108 method hasaction n =
4109 match m_a.(n) with
4110 | _, _, _, Action _ -> true
4111 | _ -> false
4112 end)
4114 let rec fillsrc prevmode prevuioh =
4115 let sep () = src#caption "" 0 in
4116 let colorp name get set =
4117 src#string name
4118 (fun () -> color_to_string (get ()))
4119 (fun v ->
4121 let c = color_of_string v in
4122 set c
4123 with exn ->
4124 state.text <- Printf.sprintf "bad color `%s': %s"
4125 v (Printexc.to_string exn);
4128 let oldmode = state.mode in
4129 let birdseye = isbirdseye state.mode in
4131 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4133 src#bool "presentation mode"
4134 (fun () -> conf.presentation)
4135 (fun v -> setpresentationmode v);
4137 src#bool "ignore case in searches"
4138 (fun () -> conf.icase)
4139 (fun v -> conf.icase <- v);
4141 src#bool "preload"
4142 (fun () -> conf.preload)
4143 (fun v -> conf.preload <- v);
4145 src#bool "highlight links"
4146 (fun () -> conf.hlinks)
4147 (fun v -> conf.hlinks <- v);
4149 src#bool "under info"
4150 (fun () -> conf.underinfo)
4151 (fun v -> conf.underinfo <- v);
4153 src#bool "persistent bookmarks"
4154 (fun () -> conf.savebmarks)
4155 (fun v -> conf.savebmarks <- v);
4157 src#bool "proportional display"
4158 (fun () -> conf.proportional)
4159 (fun v -> reqlayout conf.angle v);
4161 src#bool "trim margins"
4162 (fun () -> conf.trimmargins)
4163 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4165 src#bool "persistent location"
4166 (fun () -> conf.jumpback)
4167 (fun v -> conf.jumpback <- v);
4169 sep ();
4170 src#int "inter-page space"
4171 (fun () -> conf.interpagespace)
4172 (fun n ->
4173 conf.interpagespace <- n;
4174 docolumns conf.columns;
4175 let pageno, py =
4176 match state.layout with
4177 | [] -> 0, 0
4178 | l :: _ ->
4179 l.pageno, l.pagey
4181 state.maxy <- calcheight ();
4182 let y = getpagey pageno in
4183 gotoy (y + py)
4186 src#int "page bias"
4187 (fun () -> conf.pagebias)
4188 (fun v -> conf.pagebias <- v);
4190 src#int "scroll step"
4191 (fun () -> conf.scrollstep)
4192 (fun n -> conf.scrollstep <- n);
4194 src#int "horizontal scroll step"
4195 (fun () -> conf.hscrollstep)
4196 (fun v -> conf.hscrollstep <- v);
4198 src#int "auto scroll step"
4199 (fun () ->
4200 match state.autoscroll with
4201 | Some step -> step
4202 | _ -> conf.autoscrollstep)
4203 (fun n ->
4204 if state.autoscroll <> None
4205 then state.autoscroll <- Some n;
4206 conf.autoscrollstep <- n);
4208 src#int "zoom"
4209 (fun () -> truncate (conf.zoom *. 100.))
4210 (fun v -> setzoom ((float v) /. 100.));
4212 src#int "rotation"
4213 (fun () -> conf.angle)
4214 (fun v -> reqlayout v conf.proportional);
4216 src#int "scroll bar width"
4217 (fun () -> state.scrollw)
4218 (fun v ->
4219 state.scrollw <- v;
4220 conf.scrollbw <- v;
4221 reshape conf.winw conf.winh;
4224 src#int "scroll handle height"
4225 (fun () -> conf.scrollh)
4226 (fun v -> conf.scrollh <- v;);
4228 src#int "thumbnail width"
4229 (fun () -> conf.thumbw)
4230 (fun v ->
4231 conf.thumbw <- min 4096 v;
4232 match oldmode with
4233 | Birdseye beye ->
4234 leavebirdseye beye false;
4235 enterbirdseye ()
4236 | _ -> ()
4239 let mode = state.mode in
4240 src#string "columns"
4241 (fun () ->
4242 match conf.columns with
4243 | Csingle _ -> "1"
4244 | Cmulti (multi, _) -> multicolumns_to_string multi
4245 | Csplit (count, _) -> "-" ^ string_of_int count
4247 (fun v ->
4248 let n, a, b = multicolumns_of_string v in
4249 setcolumns mode n a b);
4251 sep ();
4252 src#caption "Presentation mode" 0;
4253 src#bool "scrollbar visible"
4254 (fun () -> conf.scrollbarinpm)
4255 (fun v ->
4256 if v != conf.scrollbarinpm
4257 then (
4258 conf.scrollbarinpm <- v;
4259 if conf.presentation
4260 then (
4261 state.scrollw <- if v then conf.scrollbw else 0;
4262 reshape conf.winw conf.winh;
4267 sep ();
4268 src#caption "Pixmap cache" 0;
4269 src#int_with_suffix "size (advisory)"
4270 (fun () -> conf.memlimit)
4271 (fun v -> conf.memlimit <- v);
4273 src#caption2 "used"
4274 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4275 (string_with_suffix_of_int state.memused)
4276 (Hashtbl.length state.tilemap)) 1;
4278 sep ();
4279 src#caption "Layout" 0;
4280 src#caption2 "Dimension"
4281 (fun () ->
4282 Printf.sprintf "%dx%d (virtual %dx%d)"
4283 conf.winw conf.winh
4284 state.w state.maxy)
4286 if conf.debug
4287 then
4288 src#caption2 "Position" (fun () ->
4289 Printf.sprintf "%dx%d" state.x state.y
4291 else
4292 src#caption2 "Visible" (fun () -> describe_location ()) 1
4295 sep ();
4296 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4297 "Save these parameters as global defaults at exit"
4298 (fun () -> conf.bedefault)
4299 (fun v -> conf.bedefault <- v)
4302 sep ();
4303 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4304 src#bool ~offset:0 ~btos "Extended parameters"
4305 (fun () -> !showextended)
4306 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4307 if !showextended
4308 then (
4309 src#bool "checkers"
4310 (fun () -> conf.checkers)
4311 (fun v -> conf.checkers <- v; setcheckers v);
4312 src#bool "update cursor"
4313 (fun () -> conf.updatecurs)
4314 (fun v -> conf.updatecurs <- v);
4315 src#bool "verbose"
4316 (fun () -> conf.verbose)
4317 (fun v -> conf.verbose <- v);
4318 src#bool "invert colors"
4319 (fun () -> conf.invert)
4320 (fun v -> conf.invert <- v);
4321 src#bool "max fit"
4322 (fun () -> conf.maxhfit)
4323 (fun v -> conf.maxhfit <- v);
4324 src#bool "redirect stderr"
4325 (fun () -> conf.redirectstderr)
4326 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4327 src#string "uri launcher"
4328 (fun () -> conf.urilauncher)
4329 (fun v -> conf.urilauncher <- v);
4330 src#string "path launcher"
4331 (fun () -> conf.pathlauncher)
4332 (fun v -> conf.pathlauncher <- v);
4333 src#string "tile size"
4334 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4335 (fun v ->
4337 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4338 conf.tilew <- max 64 w;
4339 conf.tileh <- max 64 h;
4340 flushtiles ();
4341 with exn ->
4342 state.text <- Printf.sprintf "bad tile size `%s': %s"
4343 v (Printexc.to_string exn));
4344 src#int "texture count"
4345 (fun () -> conf.texcount)
4346 (fun v ->
4347 if realloctexts v
4348 then conf.texcount <- v
4349 else showtext '!' " Failed to set texture count please retry later"
4351 src#int "slice height"
4352 (fun () -> conf.sliceheight)
4353 (fun v ->
4354 conf.sliceheight <- v;
4355 wcmd "sliceh %d" conf.sliceheight;
4357 src#int "anti-aliasing level"
4358 (fun () -> conf.aalevel)
4359 (fun v ->
4360 conf.aalevel <- bound v 0 8;
4361 state.anchor <- getanchor ();
4362 opendoc state.path state.password;
4364 src#string "page scroll scaling factor"
4365 (fun () -> string_of_float conf.pgscale)
4366 (fun v ->
4368 let s = float_of_string v in
4369 conf.pgscale <- s
4370 with exn ->
4371 state.text <- Printf.sprintf
4372 "bad page scroll scaling factor `%s': %s"
4373 v (Printexc.to_string exn)
4376 src#int "ui font size"
4377 (fun () -> fstate.fontsize)
4378 (fun v -> setfontsize (bound v 5 100));
4379 src#int "hint font size"
4380 (fun () -> conf.hfsize)
4381 (fun v -> conf.hfsize <- bound v 5 100);
4382 colorp "background color"
4383 (fun () -> conf.bgcolor)
4384 (fun v -> conf.bgcolor <- v);
4385 src#bool "crop hack"
4386 (fun () -> conf.crophack)
4387 (fun v -> conf.crophack <- v);
4388 src#bool "multi column centering"
4389 (fun () -> conf.multicenter)
4390 (fun v -> conf.multicenter <- v; represent ());
4391 src#string "trim fuzz"
4392 (fun () -> irect_to_string conf.trimfuzz)
4393 (fun v ->
4395 conf.trimfuzz <- irect_of_string v;
4396 if conf.trimmargins
4397 then settrim true conf.trimfuzz;
4398 with exn ->
4399 state.text <- Printf.sprintf "bad irect `%s': %s"
4400 v (Printexc.to_string exn)
4402 src#string "throttle"
4403 (fun () ->
4404 match conf.maxwait with
4405 | None -> "show place holder if page is not ready"
4406 | Some time ->
4407 if time = infinity
4408 then "wait for page to fully render"
4409 else
4410 "wait " ^ string_of_float time
4411 ^ " seconds before showing placeholder"
4413 (fun v ->
4415 let f = float_of_string v in
4416 if f <= 0.0
4417 then conf.maxwait <- None
4418 else conf.maxwait <- Some f
4419 with exn ->
4420 state.text <- Printf.sprintf "bad time `%s': %s"
4421 v (Printexc.to_string exn)
4423 src#string "ghyll scroll"
4424 (fun () ->
4425 match conf.ghyllscroll with
4426 | None -> ""
4427 | Some nab -> ghyllscroll_to_string nab
4429 (fun v ->
4431 let gs =
4432 if String.length v = 0
4433 then None
4434 else Some (ghyllscroll_of_string v)
4436 conf.ghyllscroll <- gs
4437 with exn ->
4438 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4439 v (Printexc.to_string exn)
4441 src#string "selection command"
4442 (fun () -> conf.selcmd)
4443 (fun v -> conf.selcmd <- v);
4444 src#colorspace "color space"
4445 (fun () -> colorspace_to_string conf.colorspace)
4446 (fun v ->
4447 conf.colorspace <- colorspace_of_int v;
4448 wcmd "cs %d" v;
4449 load state.layout;
4453 sep ();
4454 src#caption "Document" 0;
4455 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4456 src#caption2 "Pages"
4457 (fun () -> string_of_int state.pagecount) 1;
4458 src#caption2 "Dimensions"
4459 (fun () -> string_of_int (List.length state.pdims)) 1;
4460 if conf.trimmargins
4461 then (
4462 sep ();
4463 src#caption "Trimmed margins" 0;
4464 src#caption2 "Dimensions"
4465 (fun () -> string_of_int (List.length state.pdims)) 1;
4468 sep ();
4469 src#caption "OpenGL" 0;
4470 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4471 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4472 src#reset prevmode prevuioh;
4474 fun () ->
4475 state.text <- "";
4476 let prevmode = state.mode
4477 and prevuioh = state.uioh in
4478 fillsrc prevmode prevuioh;
4479 let source = (src :> lvsource) in
4480 let modehash = findkeyhash conf "info" in
4481 state.uioh <- coe (object (self)
4482 inherit listview ~source ~trusted:true ~modehash as super
4483 val mutable m_prevmemused = 0
4484 method infochanged = function
4485 | Memused ->
4486 if m_prevmemused != state.memused
4487 then (
4488 m_prevmemused <- state.memused;
4489 G.postRedisplay "memusedchanged";
4491 | Pdim -> G.postRedisplay "pdimchanged"
4492 | Docinfo -> fillsrc prevmode prevuioh
4494 method key key mask =
4495 if not (Wsi.withctrl mask)
4496 then
4497 match key with
4498 | 0xff51 -> coe (self#updownlevel ~-1)
4499 | 0xff53 -> coe (self#updownlevel 1)
4500 | _ -> super#key key mask
4501 else super#key key mask
4502 end);
4503 G.postRedisplay "info";
4506 let enterhelpmode =
4507 let source =
4508 (object
4509 inherit lvsourcebase
4510 method getitemcount = Array.length state.help
4511 method getitem n =
4512 let s, n, _ = state.help.(n) in
4513 (s, n)
4515 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4516 let optuioh =
4517 if not cancel
4518 then (
4519 m_qsearch <- qsearch;
4520 match state.help.(active) with
4521 | _, _, Action f -> Some (f uioh)
4522 | _ -> Some (uioh)
4524 else None
4526 m_active <- active;
4527 m_first <- first;
4528 m_pan <- pan;
4529 optuioh
4531 method hasaction n =
4532 match state.help.(n) with
4533 | _, _, Action _ -> true
4534 | _ -> false
4536 initializer
4537 m_active <- -1
4538 end)
4539 in fun () ->
4540 let modehash = findkeyhash conf "help" in
4541 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4542 G.postRedisplay "help";
4545 let entermsgsmode =
4546 let msgsource =
4547 let re = Str.regexp "[\r\n]" in
4548 (object
4549 inherit lvsourcebase
4550 val mutable m_items = [||]
4552 method getitemcount = 1 + Array.length m_items
4554 method getitem n =
4555 if n = 0
4556 then "[Clear]", 0
4557 else m_items.(n-1), 0
4559 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4560 ignore uioh;
4561 if not cancel
4562 then (
4563 if active = 0
4564 then Buffer.clear state.errmsgs;
4565 m_qsearch <- qsearch;
4567 m_active <- active;
4568 m_first <- first;
4569 m_pan <- pan;
4570 None
4572 method hasaction n =
4573 n = 0
4575 method reset =
4576 state.newerrmsgs <- false;
4577 let l = Str.split re (Buffer.contents state.errmsgs) in
4578 m_items <- Array.of_list l
4580 initializer
4581 m_active <- 0
4582 end)
4583 in fun () ->
4584 state.text <- "";
4585 msgsource#reset;
4586 let source = (msgsource :> lvsource) in
4587 let modehash = findkeyhash conf "listview" in
4588 state.uioh <- coe (object
4589 inherit listview ~source ~trusted:false ~modehash as super
4590 method display =
4591 if state.newerrmsgs
4592 then msgsource#reset;
4593 super#display
4594 end);
4595 G.postRedisplay "msgs";
4598 let quickbookmark ?title () =
4599 match state.layout with
4600 | [] -> ()
4601 | l :: _ ->
4602 let title =
4603 match title with
4604 | None ->
4605 let sec = Unix.gettimeofday () in
4606 let tm = Unix.localtime sec in
4607 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4608 (l.pageno+1)
4609 tm.Unix.tm_mday
4610 tm.Unix.tm_mon
4611 (tm.Unix.tm_year + 1900)
4612 tm.Unix.tm_hour
4613 tm.Unix.tm_min
4614 | Some title -> title
4616 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4619 let doreshape w h =
4620 state.fullscreen <- None;
4621 Wsi.reshape w h;
4624 let setautoscrollspeed step goingdown =
4625 let incr = max 1 ((abs step) / 2) in
4626 let incr = if goingdown then incr else -incr in
4627 let astep = step + incr in
4628 state.autoscroll <- Some astep;
4631 let gotounder = function
4632 | Ulinkgoto (pageno, top) ->
4633 if pageno >= 0
4634 then (
4635 addnav ();
4636 gotopage1 pageno top;
4639 | Ulinkuri s ->
4640 gotouri s
4642 | Uremote (filename, pageno) ->
4643 let path =
4644 if Sys.file_exists filename
4645 then filename
4646 else
4647 let dir = Filename.dirname state.path in
4648 let path = Filename.concat dir filename in
4649 if Sys.file_exists path
4650 then path
4651 else ""
4653 if String.length path > 0
4654 then (
4655 let anchor = getanchor () in
4656 let ranchor = state.path, state.password, anchor in
4657 state.anchor <- (pageno, 0.0, 0.0);
4658 state.ranchors <- ranchor :: state.ranchors;
4659 opendoc path "";
4661 else showtext '!' ("Could not find " ^ filename)
4663 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4666 let canpan () =
4667 match conf.columns with
4668 | Csplit _ -> true
4669 | _ -> conf.zoom > 1.0
4672 let viewkeyboard key mask =
4673 let enttext te =
4674 let mode = state.mode in
4675 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4676 state.text <- "";
4677 enttext ();
4678 G.postRedisplay "view:enttext"
4680 let ctrl = Wsi.withctrl mask in
4681 match key with
4682 | 81 -> (* Q *)
4683 exit 0
4685 | 0xff63 -> (* insert *)
4686 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4687 then (
4688 state.mode <- LinkNav (Ltgendir 0);
4689 gotoy state.y;
4691 else showtext '!' "Keyboard link navigation does not work under rotation"
4693 | 0xff1b | 113 -> (* escape / q *)
4694 begin match state.mstate with
4695 | Mzoomrect _ ->
4696 state.mstate <- Mnone;
4697 Wsi.setcursor Wsi.CURSOR_INHERIT;
4698 G.postRedisplay "kill zoom rect";
4699 | _ ->
4700 match state.ranchors with
4701 | [] -> raise Quit
4702 | (path, password, anchor) :: rest ->
4703 state.ranchors <- rest;
4704 state.anchor <- anchor;
4705 opendoc path password
4706 end;
4708 | 0xff08 -> (* backspace *)
4709 let y = getnav ~-1 in
4710 gotoy_and_clear_text y
4712 | 111 -> (* o *)
4713 enteroutlinemode ()
4715 | 117 -> (* u *)
4716 state.rects <- [];
4717 state.text <- "";
4718 G.postRedisplay "dehighlight";
4720 | 47 | 63 -> (* / ? *)
4721 let ondone isforw s =
4722 cbput state.hists.pat s;
4723 state.searchpattern <- s;
4724 search s isforw
4726 let s = String.create 1 in
4727 s.[0] <- Char.chr key;
4728 enttext (s, "", Some (onhist state.hists.pat),
4729 textentry, ondone (key = 47), true)
4731 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4732 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4733 setzoom (conf.zoom +. incr)
4735 | 43 | 0xffab -> (* + *)
4736 let ondone s =
4737 let n =
4738 try int_of_string s with exc ->
4739 state.text <- Printf.sprintf "bad integer `%s': %s"
4740 s (Printexc.to_string exc);
4741 max_int
4743 if n != max_int
4744 then (
4745 conf.pagebias <- n;
4746 state.text <- "page bias is now " ^ string_of_int n;
4749 enttext ("page bias: ", "", None, intentry, ondone, true)
4751 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4752 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4753 setzoom (max 0.01 (conf.zoom -. decr))
4755 | 45 | 0xffad -> (* - *)
4756 let ondone msg = state.text <- msg in
4757 enttext (
4758 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4759 optentry state.mode, ondone, true
4762 | 48 when ctrl -> (* ctrl-0 *)
4763 setzoom 1.0
4765 | 49 when ctrl -> (* ctrl-1 *)
4766 let cols =
4767 match conf.columns with
4768 | Csingle _ | Cmulti _ -> 1
4769 | Csplit (n, _) -> n
4771 let zoom = zoomforh conf.winw conf.winh state.scrollw cols in
4772 if zoom < 1.0
4773 then setzoom zoom
4775 | 0xffc6 -> (* f9 *)
4776 togglebirdseye ()
4778 | 57 when ctrl -> (* ctrl-9 *)
4779 togglebirdseye ()
4781 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4782 when not ctrl -> (* 0..9 *)
4783 let ondone s =
4784 let n =
4785 try int_of_string s with exc ->
4786 state.text <- Printf.sprintf "bad integer `%s': %s"
4787 s (Printexc.to_string exc);
4790 if n >= 0
4791 then (
4792 addnav ();
4793 cbput state.hists.pag (string_of_int n);
4794 gotopage1 (n + conf.pagebias - 1) 0;
4797 let pageentry text key =
4798 match Char.unsafe_chr key with
4799 | 'g' -> TEdone text
4800 | _ -> intentry text key
4802 let text = "x" in text.[0] <- Char.chr key;
4803 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4805 | 98 -> (* b *)
4806 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4807 reshape conf.winw conf.winh;
4809 | 108 -> (* l *)
4810 conf.hlinks <- not conf.hlinks;
4811 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4812 G.postRedisplay "toggle highlightlinks";
4814 | 70 -> (* F *)
4815 state.glinks <- true;
4816 let mode = state.mode in
4817 state.mode <- Textentry (
4818 (":", "", None, linknentry, linkndone (fun under ->
4819 addnav ();
4820 gotounder under
4821 ), false
4822 ), fun _ ->
4823 state.glinks <- false;
4824 state.mode <- mode
4826 state.text <- "";
4827 G.postRedisplay "view:linkent(F)"
4829 | 121 -> (* y *)
4830 state.glinks <- true;
4831 let mode = state.mode in
4832 state.mode <- Textentry (
4833 (":", "", None, linknentry, linkndone (fun under ->
4834 match Ne.pipe () with
4835 | Ne.Exn exn ->
4836 showtext '!' (Printf.sprintf "pipe failed: %s"
4837 (Printexc.to_string exn));
4838 | Ne.Res (r, w) ->
4839 let popened =
4840 try popen conf.selcmd [r, 0; w, -1]; true
4841 with exn ->
4842 showtext '!'
4843 (Printf.sprintf "failed to execute %s: %s"
4844 conf.selcmd (Printexc.to_string exn));
4845 false
4847 let clo cap fd =
4848 Ne.clo fd (fun msg ->
4849 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
4852 let s = undertext under in
4853 if popened
4854 then
4855 (try
4856 let l = String.length s in
4857 let n = Unix.write w s 0 l in
4858 if n != l
4859 then
4860 showtext '!'
4861 (Printf.sprintf
4862 "failed to write %d characters to sel pipe, wrote %d"
4865 with exn ->
4866 showtext '!'
4867 (Printf.sprintf "failed to write to sel pipe: %s"
4868 (Printexc.to_string exn)
4871 else dolog "%s" s;
4872 clo "pipe/r" r;
4873 clo "pipe/w" w;
4874 ), false
4876 fun _ ->
4877 state.glinks <- false;
4878 state.mode <- mode
4880 state.text <- "";
4881 G.postRedisplay "view:linkent"
4883 | 97 -> (* a *)
4884 begin match state.autoscroll with
4885 | Some step ->
4886 conf.autoscrollstep <- step;
4887 state.autoscroll <- None
4888 | None ->
4889 if conf.autoscrollstep = 0
4890 then state.autoscroll <- Some 1
4891 else state.autoscroll <- Some conf.autoscrollstep
4894 | 112 when ctrl -> (* ctrl-p *)
4895 launchpath ()
4897 | 80 -> (* P *)
4898 setpresentationmode (not conf.presentation);
4899 showtext ' ' ("presentation mode " ^
4900 if conf.presentation then "on" else "off");
4902 | 102 -> (* f *)
4903 begin match state.fullscreen with
4904 | None ->
4905 state.fullscreen <- Some (conf.winw, conf.winh);
4906 Wsi.fullscreen ()
4907 | Some (w, h) ->
4908 state.fullscreen <- None;
4909 doreshape w h
4912 | 103 -> (* g *)
4913 gotoy_and_clear_text 0
4915 | 71 -> (* G *)
4916 gotopage1 (state.pagecount - 1) 0
4918 | 112 | 78 -> (* p|N *)
4919 search state.searchpattern false
4921 | 110 | 0xffc0 -> (* n|F3 *)
4922 search state.searchpattern true
4924 | 116 -> (* t *)
4925 begin match state.layout with
4926 | [] -> ()
4927 | l :: _ ->
4928 gotoy_and_clear_text (getpagey l.pageno)
4931 | 32 -> (* space *)
4932 begin match state.layout with
4933 | [] -> ()
4934 | l :: rest ->
4935 match conf.columns with
4936 | Csingle _ ->
4937 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4938 then
4939 let y = clamp (pgscale conf.winh) in
4940 gotoy_and_clear_text y
4941 else
4942 let pageno = min (l.pageno+1) (state.pagecount-1) in
4943 gotoy_and_clear_text (getpagey pageno)
4944 | Cmulti ((c, _, _), _) ->
4945 let pageno = min (l.pageno+c) (state.pagecount-1) in
4946 gotoy_and_clear_text (getpagey pageno)
4947 | Csplit (n, _) ->
4948 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4949 then
4950 let pagey, pageh = getpageyh l.pageno in
4951 let pagey = pagey + pageh * l.pagecol in
4952 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4953 gotoy_and_clear_text (pagey + pageh + ips)
4956 | 0xff9f | 0xffff -> (* delete *)
4957 begin match state.layout with
4958 | [] -> ()
4959 | l :: _ ->
4960 match conf.columns with
4961 | Csingle _ ->
4962 if conf.presentation && l.pagey != 0
4963 then
4964 gotoy_and_clear_text (clamp (pgscale ~-(conf.winh)))
4965 else
4966 let pageno = max 0 (l.pageno-1) in
4967 gotoy_and_clear_text (getpagey pageno)
4968 | Cmulti ((c, _, _), _) ->
4969 let pageno = max 0 (l.pageno-c) in
4970 gotoy_and_clear_text (getpagey pageno)
4971 | Csplit (n, _) ->
4972 let y =
4973 if l.pagecol = 0
4974 then
4975 if l.pageno = 0
4976 then l.pagey
4977 else
4978 let pageno = max 0 (l.pageno-1) in
4979 let pagey, pageh = getpageyh pageno in
4980 pagey + (n-1)*pageh
4981 else
4982 let pagey, pageh = getpageyh l.pageno in
4983 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4985 gotoy_and_clear_text y
4988 | 61 -> (* = *)
4989 showtext ' ' (describe_location ());
4991 | 119 -> (* w *)
4992 begin match state.layout with
4993 | [] -> ()
4994 | l :: _ ->
4995 doreshape (l.pagew + state.scrollw) l.pageh;
4996 G.postRedisplay "w"
4999 | 39 -> (* ' *)
5000 enterbookmarkmode ()
5002 | 104 | 0xffbe -> (* h|F1 *)
5003 enterhelpmode ()
5005 | 105 -> (* i *)
5006 enterinfomode ()
5008 | 101 when conf.redirectstderr -> (* e *)
5009 entermsgsmode ()
5011 | 109 -> (* m *)
5012 let ondone s =
5013 match state.layout with
5014 | l :: _ -> state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5015 | _ -> ()
5017 enttext ("bookmark: ", "", None, textentry, ondone, true)
5019 | 126 -> (* ~ *)
5020 quickbookmark ();
5021 showtext ' ' "Quick bookmark added";
5023 | 122 -> (* z *)
5024 begin match state.layout with
5025 | l :: _ ->
5026 let rect = getpdimrect l.pagedimno in
5027 let w, h =
5028 if conf.crophack
5029 then
5030 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5031 truncate (1.2 *. (rect.(3) -. rect.(0))))
5032 else
5033 (truncate (rect.(1) -. rect.(0)),
5034 truncate (rect.(3) -. rect.(0)))
5036 let w = truncate ((float w)*.conf.zoom)
5037 and h = truncate ((float h)*.conf.zoom) in
5038 if w != 0 && h != 0
5039 then (
5040 state.anchor <- getanchor ();
5041 doreshape (w + state.scrollw) (h + conf.interpagespace)
5043 G.postRedisplay "z";
5045 | [] -> ()
5048 | 50 when ctrl -> (* ctrl-2 *)
5049 let maxw = getmaxw () in
5050 if maxw > 0.0
5051 then setzoom (maxw /. float conf.winw)
5053 | 60 | 62 -> (* < > *)
5054 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5056 | 91 | 93 -> (* [ ] *)
5057 conf.colorscale <-
5058 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5060 G.postRedisplay "brightness";
5062 | 99 when state.mode = View -> (* c *)
5063 let (c, a, b), z =
5064 match state.prevcolumns with
5065 | None -> (1, 0, 0), 1.0
5066 | Some (columns, z) ->
5067 let cab =
5068 match columns with
5069 | Csplit (c, _) -> -c, 0, 0
5070 | Cmulti ((c, a, b), _) -> c, a, b
5071 | Csingle _ -> 1, 0, 0
5073 cab, z
5075 setcolumns View c a b;
5076 setzoom z;
5078 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5079 setzoom state.prevzoom
5081 | 107 | 0xff52 -> (* k up *)
5082 begin match state.autoscroll with
5083 | None ->
5084 begin match state.mode with
5085 | Birdseye beye -> upbirdseye 1 beye
5086 | _ ->
5087 if ctrl
5088 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5089 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5091 | Some n ->
5092 setautoscrollspeed n false
5095 | 106 | 0xff54 -> (* j down *)
5096 begin match state.autoscroll with
5097 | None ->
5098 begin match state.mode with
5099 | Birdseye beye -> downbirdseye 1 beye
5100 | _ ->
5101 if ctrl
5102 then gotoy_and_clear_text (clamp (conf.winh/2))
5103 else gotoy_and_clear_text (clamp conf.scrollstep)
5105 | Some n ->
5106 setautoscrollspeed n true
5109 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5110 if canpan ()
5111 then
5112 let dx =
5113 if ctrl
5114 then conf.winw / 2
5115 else 10
5117 let dx = if key = 0xff51 then dx else -dx in
5118 state.x <- state.x + dx;
5119 gotoy_and_clear_text state.y
5120 else (
5121 state.text <- "";
5122 G.postRedisplay "lef/right"
5125 | 0xff55 -> (* prior *)
5126 let y =
5127 if ctrl
5128 then
5129 match state.layout with
5130 | [] -> state.y
5131 | l :: _ -> state.y - l.pagey
5132 else
5133 clamp (pgscale (-conf.winh))
5135 gotoghyll y
5137 | 0xff56 -> (* next *)
5138 let y =
5139 if ctrl
5140 then
5141 match List.rev state.layout with
5142 | [] -> state.y
5143 | l :: _ -> getpagey l.pageno
5144 else
5145 clamp (pgscale conf.winh)
5147 gotoghyll y
5149 | 0xff50 -> (* home *)
5150 gotoghyll 0
5151 | 0xff57 -> (* end *)
5152 gotoghyll (clamp state.maxy)
5153 | 0xff53 when Wsi.withalt mask -> (* right *)
5154 gotoghyll (getnav ~-1)
5155 | 0xff51 when Wsi.withalt mask -> (* left *)
5156 gotoghyll (getnav 1)
5158 | 114 -> (* r *)
5159 state.anchor <- getanchor ();
5160 opendoc state.path state.password
5162 | 118 when conf.debug -> (* v *)
5163 state.rects <- [];
5164 List.iter (fun l ->
5165 match getopaque l.pageno with
5166 | None -> ()
5167 | Some opaque ->
5168 let x0, y0, x1, y1 = pagebbox opaque in
5169 let a,b = float x0, float y0 in
5170 let c,d = float x1, float y0 in
5171 let e,f = float x1, float y1 in
5172 let h,j = float x0, float y1 in
5173 let rect = (a,b,c,d,e,f,h,j) in
5174 debugrect rect;
5175 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5176 ) state.layout;
5177 G.postRedisplay "v";
5179 | _ ->
5180 vlog "huh? %s" (Wsi.keyname key)
5183 let linknavkeyboard key mask linknav =
5184 let getpage pageno =
5185 let rec loop = function
5186 | [] -> None
5187 | l :: _ when l.pageno = pageno -> Some l
5188 | _ :: rest -> loop rest
5189 in loop state.layout
5191 let doexact (pageno, n) =
5192 match getopaque pageno, getpage pageno with
5193 | Some opaque, Some l ->
5194 if key = 0xff0d
5195 then
5196 let under = getlink opaque n in
5197 G.postRedisplay "link gotounder";
5198 gotounder under;
5199 state.mode <- View;
5200 else
5201 let opt, dir =
5202 match key with
5203 | 0xff50 -> (* home *)
5204 Some (findlink opaque LDfirst), -1
5206 | 0xff57 -> (* end *)
5207 Some (findlink opaque LDlast), 1
5209 | 0xff51 -> (* left *)
5210 Some (findlink opaque (LDleft n)), -1
5212 | 0xff53 -> (* right *)
5213 Some (findlink opaque (LDright n)), 1
5215 | 0xff52 -> (* up *)
5216 Some (findlink opaque (LDup n)), -1
5218 | 0xff54 -> (* down *)
5219 Some (findlink opaque (LDdown n)), 1
5221 | _ -> None, 0
5223 let pwl l dir =
5224 begin match findpwl l.pageno dir with
5225 | Pwlnotfound -> ()
5226 | Pwl pageno ->
5227 let notfound dir =
5228 state.mode <- LinkNav (Ltgendir dir);
5229 let y, h = getpageyh pageno in
5230 let y =
5231 if dir < 0
5232 then y + h - conf.winh
5233 else y
5235 gotoy y
5237 begin match getopaque pageno, getpage pageno with
5238 | Some opaque, Some _ ->
5239 let link =
5240 let ld = if dir > 0 then LDfirst else LDlast in
5241 findlink opaque ld
5243 begin match link with
5244 | Lfound m ->
5245 showlinktype (getlink opaque m);
5246 state.mode <- LinkNav (Ltexact (pageno, m));
5247 G.postRedisplay "linknav jpage";
5248 | _ -> notfound dir
5249 end;
5250 | _ -> notfound dir
5251 end;
5252 end;
5254 begin match opt with
5255 | Some Lnotfound -> pwl l dir;
5256 | Some (Lfound m) ->
5257 if m = n
5258 then pwl l dir
5259 else (
5260 let _, y0, _, y1 = getlinkrect opaque m in
5261 if y0 < l.pagey
5262 then gotopage1 l.pageno y0
5263 else (
5264 let d = fstate.fontsize + 1 in
5265 if y1 - l.pagey > l.pagevh - d
5266 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5267 else G.postRedisplay "linknav";
5269 showlinktype (getlink opaque m);
5270 state.mode <- LinkNav (Ltexact (l.pageno, m));
5273 | None -> viewkeyboard key mask
5274 end;
5275 | _ -> viewkeyboard key mask
5277 if key = 0xff63
5278 then (
5279 state.mode <- View;
5280 G.postRedisplay "leave linknav"
5282 else
5283 match linknav with
5284 | Ltgendir _ -> viewkeyboard key mask
5285 | Ltexact exact -> doexact exact
5288 let keyboard key mask =
5289 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5290 then wcmd "interrupt"
5291 else state.uioh <- state.uioh#key key mask
5294 let birdseyekeyboard key mask
5295 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5296 let incr =
5297 match conf.columns with
5298 | Csingle _ -> 1
5299 | Cmulti ((c, _, _), _) -> c
5300 | Csplit _ -> failwith "bird's eye split mode"
5302 let pgh layout = List.fold_left (fun m l -> max l.pageh m) conf.winh layout in
5303 match key with
5304 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5305 let y, h = getpageyh pageno in
5306 let top = (conf.winh - h) / 2 in
5307 gotoy (max 0 (y - top))
5308 | 0xff0d -> leavebirdseye beye false
5309 | 0xff1b -> leavebirdseye beye true (* escape *)
5310 | 0xff52 -> upbirdseye incr beye (* up *)
5311 | 0xff54 -> downbirdseye incr beye (* down *)
5312 | 0xff51 -> upbirdseye 1 beye (* left *)
5313 | 0xff53 -> downbirdseye 1 beye (* right *)
5315 | 0xff55 -> (* prior *)
5316 begin match state.layout with
5317 | l :: _ ->
5318 if l.pagey != 0
5319 then (
5320 state.mode <- Birdseye (
5321 oconf, leftx, l.pageno, hooverpageno, anchor
5323 gotopage1 l.pageno 0;
5325 else (
5326 let layout = layout (state.y-conf.winh) (pgh state.layout) in
5327 match layout with
5328 | [] -> gotoy (clamp (-conf.winh))
5329 | l :: _ ->
5330 state.mode <- Birdseye (
5331 oconf, leftx, l.pageno, hooverpageno, anchor
5333 gotopage1 l.pageno 0
5336 | [] -> gotoy (clamp (-conf.winh))
5337 end;
5339 | 0xff56 -> (* next *)
5340 begin match List.rev state.layout with
5341 | l :: _ ->
5342 let layout = layout (state.y + (pgh state.layout)) conf.winh in
5343 begin match layout with
5344 | [] ->
5345 let incr = l.pageh - l.pagevh in
5346 if incr = 0
5347 then (
5348 state.mode <-
5349 Birdseye (
5350 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5352 G.postRedisplay "birdseye pagedown";
5354 else gotoy (clamp (incr + conf.interpagespace*2));
5356 | l :: _ ->
5357 state.mode <-
5358 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5359 gotopage1 l.pageno 0;
5362 | [] -> gotoy (clamp conf.winh)
5363 end;
5365 | 0xff50 -> (* home *)
5366 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5367 gotopage1 0 0
5369 | 0xff57 -> (* end *)
5370 let pageno = state.pagecount - 1 in
5371 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5372 if not (pagevisible state.layout pageno)
5373 then
5374 let h =
5375 match List.rev state.pdims with
5376 | [] -> conf.winh
5377 | (_, _, h, _) :: _ -> h
5379 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5380 else G.postRedisplay "birdseye end";
5381 | _ -> viewkeyboard key mask
5384 let drawpage l linkindexbase =
5385 let color =
5386 match state.mode with
5387 | Textentry _ -> scalecolor 0.4
5388 | LinkNav _
5389 | View -> scalecolor 1.0
5390 | Birdseye (_, _, pageno, hooverpageno, _) ->
5391 if l.pageno = hooverpageno
5392 then scalecolor 0.9
5393 else (
5394 if l.pageno = pageno
5395 then scalecolor 1.0
5396 else scalecolor 0.8
5399 drawtiles l color;
5400 begin match getopaque l.pageno with
5401 | Some opaque ->
5402 if tileready l l.pagex l.pagey
5403 then
5404 let x = l.pagedispx - l.pagex
5405 and y = l.pagedispy - l.pagey in
5406 let hlmask =
5407 match conf.columns with
5408 | Csingle _ | Cmulti _ ->
5409 (if conf.hlinks then 1 else 0)
5410 + (if state.glinks
5411 && not (isbirdseye state.mode) then 2 else 0)
5412 | _ -> 0
5414 let s =
5415 match state.mode with
5416 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5417 | _ -> ""
5419 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5420 else 0
5422 | _ -> 0
5423 end;
5426 let scrollindicator () =
5427 let sbw, ph, sh = state.uioh#scrollph in
5428 let sbh, pw, sw = state.uioh#scrollpw in
5430 GlDraw.color (0.64, 0.64, 0.64);
5431 GlDraw.rect
5432 (float (conf.winw - sbw), 0.)
5433 (float conf.winw, float conf.winh)
5435 GlDraw.rect
5436 (0., float (conf.winh - sbh))
5437 (float (conf.winw - state.scrollw - 1), float conf.winh)
5439 GlDraw.color (0.0, 0.0, 0.0);
5441 GlDraw.rect
5442 (float (conf.winw - sbw), ph)
5443 (float conf.winw, ph +. sh)
5445 GlDraw.rect
5446 (pw, float (conf.winh - sbh))
5447 (pw +. sw, float conf.winh)
5451 let showsel () =
5452 match state.mstate with
5453 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5456 | Msel ((x0, y0), (x1, y1)) ->
5457 let rec loop = function
5458 | l :: ls ->
5459 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5460 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5461 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5462 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5463 then
5464 match getopaque l.pageno with
5465 | Some opaque ->
5466 let x0, y0 = pagetranslatepoint l x0 y0 in
5467 let x1, y1 = pagetranslatepoint l x1 y1 in
5468 seltext opaque (x0, y0, x1, y1);
5469 | _ -> ()
5470 else loop ls
5471 | [] -> ()
5473 loop state.layout
5476 let showrects rects =
5477 Gl.enable `blend;
5478 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5479 GlDraw.polygon_mode `both `fill;
5480 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5481 List.iter
5482 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5483 List.iter (fun l ->
5484 if l.pageno = pageno
5485 then (
5486 let dx = float (l.pagedispx - l.pagex) in
5487 let dy = float (l.pagedispy - l.pagey) in
5488 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5489 GlDraw.begins `quads;
5491 GlDraw.vertex2 (x0+.dx, y0+.dy);
5492 GlDraw.vertex2 (x1+.dx, y1+.dy);
5493 GlDraw.vertex2 (x2+.dx, y2+.dy);
5494 GlDraw.vertex2 (x3+.dx, y3+.dy);
5496 GlDraw.ends ();
5498 ) state.layout
5499 ) rects
5501 Gl.disable `blend;
5504 let display () =
5505 GlClear.color (scalecolor2 conf.bgcolor);
5506 GlClear.clear [`color];
5507 let rec loop linkindexbase = function
5508 | l :: rest ->
5509 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5510 loop linkindexbase rest
5511 | [] -> ()
5513 loop 0 state.layout;
5514 let rects =
5515 match state.mode with
5516 | LinkNav (Ltexact (pageno, linkno)) ->
5517 begin match getopaque pageno with
5518 | Some opaque ->
5519 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5520 (pageno, 5, (
5521 float x0, float y0,
5522 float x1, float y0,
5523 float x1, float y1,
5524 float x0, float y1)
5525 ) :: state.rects
5526 | None -> state.rects
5528 | _ -> state.rects
5530 showrects rects;
5531 showsel ();
5532 state.uioh#display;
5533 begin match state.mstate with
5534 | Mzoomrect ((x0, y0), (x1, y1)) ->
5535 Gl.enable `blend;
5536 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5537 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5538 GlDraw.rect (float x0, float y0)
5539 (float x1, float y1);
5540 Gl.disable `blend;
5541 | _ -> ()
5542 end;
5543 enttext ();
5544 scrollindicator ();
5545 Wsi.swapb ();
5548 let zoomrect x y x1 y1 =
5549 let x0 = min x x1
5550 and x1 = max x x1
5551 and y0 = min y y1 in
5552 gotoy (state.y + y0);
5553 state.anchor <- getanchor ();
5554 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5555 let margin =
5556 if state.w < conf.winw - state.scrollw
5557 then (conf.winw - state.scrollw - state.w) / 2
5558 else 0
5560 state.x <- (state.x + margin) - x0;
5561 setzoom zoom;
5562 Wsi.setcursor Wsi.CURSOR_INHERIT;
5563 state.mstate <- Mnone;
5566 let scrollx x =
5567 let winw = conf.winw - state.scrollw - 1 in
5568 let s = float x /. float winw in
5569 let destx = truncate (float (state.w + winw) *. s) in
5570 state.x <- winw - destx;
5571 gotoy_and_clear_text state.y;
5572 state.mstate <- Mscrollx;
5575 let scrolly y =
5576 let s = float y /. float conf.winh in
5577 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5578 gotoy_and_clear_text desty;
5579 state.mstate <- Mscrolly;
5582 let viewmouse button down x y mask =
5583 match button with
5584 | n when (n == 4 || n == 5) && not down ->
5585 if Wsi.withctrl mask
5586 then (
5587 match state.mstate with
5588 | Mzoom (oldn, i) ->
5589 if oldn = n
5590 then (
5591 if i = 2
5592 then
5593 let incr =
5594 match n with
5595 | 5 ->
5596 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5597 | _ ->
5598 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5600 let zoom = conf.zoom -. incr in
5601 setzoom zoom;
5602 state.mstate <- Mzoom (n, 0);
5603 else
5604 state.mstate <- Mzoom (n, i+1);
5606 else state.mstate <- Mzoom (n, 0)
5608 | _ -> state.mstate <- Mzoom (n, 0)
5610 else (
5611 match state.autoscroll with
5612 | Some step -> setautoscrollspeed step (n=4)
5613 | None ->
5614 let incr =
5615 if n = 4
5616 then -conf.scrollstep
5617 else conf.scrollstep
5619 let incr = incr * 2 in
5620 let y = clamp incr in
5621 gotoy_and_clear_text y
5624 | n when (n = 6 || n = 7) && not down && canpan () ->
5625 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5626 gotoy_and_clear_text state.y
5628 | 1 when Wsi.withctrl mask ->
5629 if down
5630 then (
5631 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5632 state.mstate <- Mpan (x, y)
5634 else
5635 state.mstate <- Mnone
5637 | 3 ->
5638 if down
5639 then (
5640 Wsi.setcursor Wsi.CURSOR_CYCLE;
5641 let p = (x, y) in
5642 state.mstate <- Mzoomrect (p, p)
5644 else (
5645 match state.mstate with
5646 | Mzoomrect ((x0, y0), _) ->
5647 if abs (x-x0) > 10 && abs (y - y0) > 10
5648 then zoomrect x0 y0 x y
5649 else (
5650 state.mstate <- Mnone;
5651 Wsi.setcursor Wsi.CURSOR_INHERIT;
5652 G.postRedisplay "kill accidental zoom rect";
5654 | _ ->
5655 Wsi.setcursor Wsi.CURSOR_INHERIT;
5656 state.mstate <- Mnone
5659 | 1 when x > conf.winw - state.scrollw ->
5660 if down
5661 then
5662 let _, position, sh = state.uioh#scrollph in
5663 if y > truncate position && y < truncate (position +. sh)
5664 then state.mstate <- Mscrolly
5665 else scrolly y
5666 else
5667 state.mstate <- Mnone
5669 | 1 when y > conf.winh - state.hscrollh ->
5670 if down
5671 then
5672 let _, position, sw = state.uioh#scrollpw in
5673 if x > truncate position && x < truncate (position +. sw)
5674 then state.mstate <- Mscrollx
5675 else scrollx x
5676 else
5677 state.mstate <- Mnone
5679 | 1 ->
5680 let dest = if down then getunder x y else Unone in
5681 begin match dest with
5682 | Ulinkgoto _
5683 | Ulinkuri _
5684 | Uremote _
5685 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5686 gotounder dest
5688 | Unone when down ->
5689 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5690 state.mstate <- Mpan (x, y);
5692 | Unone | Utext _ ->
5693 if down
5694 then (
5695 if conf.angle mod 360 = 0
5696 then (
5697 state.mstate <- Msel ((x, y), (x, y));
5698 G.postRedisplay "mouse select";
5701 else (
5702 match state.mstate with
5703 | Mnone -> ()
5705 | Mzoom _ | Mscrollx | Mscrolly ->
5706 state.mstate <- Mnone
5708 | Mzoomrect ((x0, y0), _) ->
5709 zoomrect x0 y0 x y
5711 | Mpan _ ->
5712 Wsi.setcursor Wsi.CURSOR_INHERIT;
5713 state.mstate <- Mnone
5715 | Msel ((_, y0), (_, y1)) ->
5716 let rec loop = function
5717 | [] -> ()
5718 | l :: rest ->
5719 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5720 || ((y1 >= l.pagedispy
5721 && y1 <= (l.pagedispy + l.pagevh)))
5722 then
5723 match getopaque l.pageno with
5724 | Some opaque ->
5725 begin
5726 match Ne.pipe () with
5727 | Ne.Exn exn ->
5728 showtext '!'
5729 (Printf.sprintf
5730 "can not create sel pipe: %s"
5731 (Printexc.to_string exn));
5732 | Ne.Res (r, w) ->
5733 let doclose what fd =
5734 Ne.clo fd (fun msg ->
5735 dolog "%s close failed: %s" what msg)
5738 popen conf.selcmd [r, 0; w, -1];
5739 copysel w opaque;
5740 doclose "pipe/r" r;
5741 G.postRedisplay "copysel";
5742 with exn ->
5743 dolog "can not execute %S: %s"
5744 conf.selcmd (Printexc.to_string exn);
5745 doclose "pipe/r" r;
5746 doclose "pipe/w" w;
5748 | None -> ()
5749 else loop rest
5751 loop state.layout;
5752 Wsi.setcursor Wsi.CURSOR_INHERIT;
5753 state.mstate <- Mnone;
5757 | _ -> ()
5760 let birdseyemouse button down x y mask
5761 (conf, leftx, _, hooverpageno, anchor) =
5762 match button with
5763 | 1 when down ->
5764 let rec loop = function
5765 | [] -> ()
5766 | l :: rest ->
5767 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5768 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5769 then (
5770 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5772 else loop rest
5774 loop state.layout
5775 | 3 -> ()
5776 | _ -> viewmouse button down x y mask
5779 let mouse button down x y mask =
5780 state.uioh <- state.uioh#button button down x y mask;
5783 let motion ~x ~y =
5784 state.uioh <- state.uioh#motion x y
5787 let pmotion ~x ~y =
5788 state.uioh <- state.uioh#pmotion x y;
5791 let uioh = object
5792 method display = ()
5794 method key key mask =
5795 begin match state.mode with
5796 | Textentry textentry -> textentrykeyboard key mask textentry
5797 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5798 | View -> viewkeyboard key mask
5799 | LinkNav linknav -> linknavkeyboard key mask linknav
5800 end;
5801 state.uioh
5803 method button button bstate x y mask =
5804 begin match state.mode with
5805 | LinkNav _
5806 | View -> viewmouse button bstate x y mask
5807 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5808 | Textentry _ -> ()
5809 end;
5810 state.uioh
5812 method motion x y =
5813 begin match state.mode with
5814 | Textentry _ -> ()
5815 | View | Birdseye _ | LinkNav _ ->
5816 match state.mstate with
5817 | Mzoom _ | Mnone -> ()
5819 | Mpan (x0, y0) ->
5820 let dx = x - x0
5821 and dy = y0 - y in
5822 state.mstate <- Mpan (x, y);
5823 if canpan ()
5824 then state.x <- state.x + dx;
5825 let y = clamp dy in
5826 gotoy_and_clear_text y
5828 | Msel (a, _) ->
5829 state.mstate <- Msel (a, (x, y));
5830 G.postRedisplay "motion select";
5832 | Mscrolly ->
5833 let y = min conf.winh (max 0 y) in
5834 scrolly y
5836 | Mscrollx ->
5837 let x = min conf.winw (max 0 x) in
5838 scrollx x
5840 | Mzoomrect (p0, _) ->
5841 state.mstate <- Mzoomrect (p0, (x, y));
5842 G.postRedisplay "motion zoomrect";
5843 end;
5844 state.uioh
5846 method pmotion x y =
5847 begin match state.mode with
5848 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5849 let rec loop = function
5850 | [] ->
5851 if hooverpageno != -1
5852 then (
5853 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5854 G.postRedisplay "pmotion birdseye no hoover";
5856 | l :: rest ->
5857 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5858 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5859 then (
5860 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5861 G.postRedisplay "pmotion birdseye hoover";
5863 else loop rest
5865 loop state.layout
5867 | Textentry _ -> ()
5869 | LinkNav _
5870 | View ->
5871 match state.mstate with
5872 | Mnone -> updateunder x y
5873 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5875 end;
5876 state.uioh
5878 method infochanged _ = ()
5880 method scrollph =
5881 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5882 let p, h = scrollph state.y maxy in
5883 state.scrollw, p, h
5885 method scrollpw =
5886 let winw = conf.winw - state.scrollw - 1 in
5887 let fwinw = float winw in
5888 let sw =
5889 let sw = fwinw /. float state.w in
5890 let sw = fwinw *. sw in
5891 max sw (float conf.scrollh)
5893 let position, sw =
5894 let f = state.w+winw in
5895 let r = float (winw-state.x) /. float f in
5896 let p = fwinw *. r in
5897 p-.sw/.2., sw
5899 let sw =
5900 if position +. sw > fwinw
5901 then fwinw -. position
5902 else sw
5904 state.hscrollh, position, sw
5906 method modehash =
5907 let modename =
5908 match state.mode with
5909 | LinkNav _ -> "links"
5910 | Textentry _ -> "textentry"
5911 | Birdseye _ -> "birdseye"
5912 | View -> "view"
5914 findkeyhash conf modename
5915 end;;
5917 module Config =
5918 struct
5919 open Parser
5921 let fontpath = ref "";;
5923 module KeyMap =
5924 Map.Make (struct type t = (int * int) let compare = compare end);;
5926 let unent s =
5927 let l = String.length s in
5928 let b = Buffer.create l in
5929 unent b s 0 l;
5930 Buffer.contents b;
5933 let home =
5934 try Sys.getenv "HOME"
5935 with exn ->
5936 prerr_endline
5937 ("Can not determine home directory location: " ^
5938 Printexc.to_string exn);
5942 let modifier_of_string = function
5943 | "alt" -> Wsi.altmask
5944 | "shift" -> Wsi.shiftmask
5945 | "ctrl" | "control" -> Wsi.ctrlmask
5946 | "meta" -> Wsi.metamask
5947 | _ -> 0
5950 let key_of_string =
5951 let r = Str.regexp "-" in
5952 fun s ->
5953 let elems = Str.full_split r s in
5954 let f n k m =
5955 let g s =
5956 let m1 = modifier_of_string s in
5957 if m1 = 0
5958 then (Wsi.namekey s, m)
5959 else (k, m lor m1)
5960 in function
5961 | Str.Delim s when n land 1 = 0 -> g s
5962 | Str.Text s -> g s
5963 | Str.Delim _ -> (k, m)
5965 let rec loop n k m = function
5966 | [] -> (k, m)
5967 | x :: xs ->
5968 let k, m = f n k m x in
5969 loop (n+1) k m xs
5971 loop 0 0 0 elems
5974 let keys_of_string =
5975 let r = Str.regexp "[ \t]" in
5976 fun s ->
5977 let elems = Str.split r s in
5978 List.map key_of_string elems
5981 let copykeyhashes c =
5982 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5985 let config_of c attrs =
5986 let apply c k v =
5988 match k with
5989 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5990 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5991 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5992 | "preload" -> { c with preload = bool_of_string v }
5993 | "page-bias" -> { c with pagebias = int_of_string v }
5994 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5995 | "horizontal-scroll-step" ->
5996 { c with hscrollstep = max (int_of_string v) 1 }
5997 | "auto-scroll-step" ->
5998 { c with autoscrollstep = max 0 (int_of_string v) }
5999 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6000 | "crop-hack" -> { c with crophack = bool_of_string v }
6001 | "throttle" ->
6002 let mw =
6003 match String.lowercase v with
6004 | "true" -> Some infinity
6005 | "false" -> None
6006 | f -> Some (float_of_string f)
6008 { c with maxwait = mw}
6009 | "highlight-links" -> { c with hlinks = bool_of_string v }
6010 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6011 | "vertical-margin" ->
6012 { c with interpagespace = max 0 (int_of_string v) }
6013 | "zoom" ->
6014 let zoom = float_of_string v /. 100. in
6015 let zoom = max zoom 0.0 in
6016 { c with zoom = zoom }
6017 | "presentation" -> { c with presentation = bool_of_string v }
6018 | "rotation-angle" -> { c with angle = int_of_string v }
6019 | "width" -> { c with winw = max 20 (int_of_string v) }
6020 | "height" -> { c with winh = max 20 (int_of_string v) }
6021 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6022 | "proportional-display" -> { c with proportional = bool_of_string v }
6023 | "pixmap-cache-size" ->
6024 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6025 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6026 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6027 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6028 | "persistent-location" -> { c with jumpback = bool_of_string v }
6029 | "background-color" -> { c with bgcolor = color_of_string v }
6030 | "scrollbar-in-presentation" ->
6031 { c with scrollbarinpm = bool_of_string v }
6032 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6033 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6034 | "mupdf-store-size" ->
6035 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6036 | "checkers" -> { c with checkers = bool_of_string v }
6037 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6038 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6039 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6040 | "uri-launcher" -> { c with urilauncher = unent v }
6041 | "path-launcher" -> { c with pathlauncher = unent v }
6042 | "color-space" -> { c with colorspace = colorspace_of_string v }
6043 | "invert-colors" -> { c with invert = bool_of_string v }
6044 | "brightness" -> { c with colorscale = float_of_string v }
6045 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6046 | "ghyllscroll" ->
6047 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6048 | "columns" ->
6049 let (n, _, _) as nab = multicolumns_of_string v in
6050 if n < 0
6051 then { c with columns = Csplit (-n, [||]) }
6052 else { c with columns = Cmulti (nab, [||]) }
6053 | "birds-eye-columns" ->
6054 { c with beyecolumns = Some (max (int_of_string v) 2) }
6055 | "selection-command" -> { c with selcmd = unent v }
6056 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6057 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6058 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6059 | "multi-column-centering" -> { c with multicenter = bool_of_string v }
6060 | _ -> c
6061 with exn ->
6062 prerr_endline ("Error processing attribute (`" ^
6063 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
6066 let rec fold c = function
6067 | [] -> c
6068 | (k, v) :: rest ->
6069 let c = apply c k v in
6070 fold c rest
6072 fold { c with keyhashes = copykeyhashes c } attrs;
6075 let fromstring f pos n v d =
6076 try f v
6077 with exn ->
6078 dolog "Error processing attribute (%S=%S) at %d\n%s"
6079 n v pos (Printexc.to_string exn)
6084 let bookmark_of attrs =
6085 let rec fold title page rely visy = function
6086 | ("title", v) :: rest -> fold v page rely visy rest
6087 | ("page", v) :: rest -> fold title v rely visy rest
6088 | ("rely", v) :: rest -> fold title page v visy rest
6089 | ("visy", v) :: rest -> fold title page rely v rest
6090 | _ :: rest -> fold title page rely visy rest
6091 | [] -> title, page, rely, visy
6093 fold "invalid" "0" "0" "0" attrs
6096 let doc_of attrs =
6097 let rec fold path page rely pan visy = function
6098 | ("path", v) :: rest -> fold v page rely pan visy rest
6099 | ("page", v) :: rest -> fold path v rely pan visy rest
6100 | ("rely", v) :: rest -> fold path page v pan visy rest
6101 | ("pan", v) :: rest -> fold path page rely v visy rest
6102 | ("visy", v) :: rest -> fold path page rely pan v rest
6103 | _ :: rest -> fold path page rely pan visy rest
6104 | [] -> path, page, rely, pan, visy
6106 fold "" "0" "0" "0" "0" attrs
6109 let map_of attrs =
6110 let rec fold rs ls = function
6111 | ("out", v) :: rest -> fold v ls rest
6112 | ("in", v) :: rest -> fold rs v rest
6113 | _ :: rest -> fold ls rs rest
6114 | [] -> ls, rs
6116 fold "" "" attrs
6119 let setconf dst src =
6120 dst.scrollbw <- src.scrollbw;
6121 dst.scrollh <- src.scrollh;
6122 dst.icase <- src.icase;
6123 dst.preload <- src.preload;
6124 dst.pagebias <- src.pagebias;
6125 dst.verbose <- src.verbose;
6126 dst.scrollstep <- src.scrollstep;
6127 dst.maxhfit <- src.maxhfit;
6128 dst.crophack <- src.crophack;
6129 dst.autoscrollstep <- src.autoscrollstep;
6130 dst.maxwait <- src.maxwait;
6131 dst.hlinks <- src.hlinks;
6132 dst.underinfo <- src.underinfo;
6133 dst.interpagespace <- src.interpagespace;
6134 dst.zoom <- src.zoom;
6135 dst.presentation <- src.presentation;
6136 dst.angle <- src.angle;
6137 dst.winw <- src.winw;
6138 dst.winh <- src.winh;
6139 dst.savebmarks <- src.savebmarks;
6140 dst.memlimit <- src.memlimit;
6141 dst.proportional <- src.proportional;
6142 dst.texcount <- src.texcount;
6143 dst.sliceheight <- src.sliceheight;
6144 dst.thumbw <- src.thumbw;
6145 dst.jumpback <- src.jumpback;
6146 dst.bgcolor <- src.bgcolor;
6147 dst.scrollbarinpm <- src.scrollbarinpm;
6148 dst.tilew <- src.tilew;
6149 dst.tileh <- src.tileh;
6150 dst.mustoresize <- src.mustoresize;
6151 dst.checkers <- src.checkers;
6152 dst.aalevel <- src.aalevel;
6153 dst.trimmargins <- src.trimmargins;
6154 dst.trimfuzz <- src.trimfuzz;
6155 dst.urilauncher <- src.urilauncher;
6156 dst.colorspace <- src.colorspace;
6157 dst.invert <- src.invert;
6158 dst.colorscale <- src.colorscale;
6159 dst.redirectstderr <- src.redirectstderr;
6160 dst.ghyllscroll <- src.ghyllscroll;
6161 dst.columns <- src.columns;
6162 dst.beyecolumns <- src.beyecolumns;
6163 dst.selcmd <- src.selcmd;
6164 dst.updatecurs <- src.updatecurs;
6165 dst.pathlauncher <- src.pathlauncher;
6166 dst.keyhashes <- copykeyhashes src;
6167 dst.hfsize <- src.hfsize;
6168 dst.hscrollstep <- src.hscrollstep;
6169 dst.pgscale <- src.pgscale;
6170 dst.multicenter <- src.multicenter;
6173 let get s =
6174 let h = Hashtbl.create 10 in
6175 let dc = { defconf with angle = defconf.angle } in
6176 let rec toplevel v t spos _ =
6177 match t with
6178 | Vdata | Vcdata | Vend -> v
6179 | Vopen ("llppconfig", _, closed) ->
6180 if closed
6181 then v
6182 else { v with f = llppconfig }
6183 | Vopen _ ->
6184 error "unexpected subelement at top level" s spos
6185 | Vclose _ -> error "unexpected close at top level" s spos
6187 and llppconfig v t spos _ =
6188 match t with
6189 | Vdata | Vcdata -> v
6190 | Vend -> error "unexpected end of input in llppconfig" s spos
6191 | Vopen ("defaults", attrs, closed) ->
6192 let c = config_of dc attrs in
6193 setconf dc c;
6194 if closed
6195 then v
6196 else { v with f = defaults }
6198 | Vopen ("ui-font", attrs, closed) ->
6199 let rec getsize size = function
6200 | [] -> size
6201 | ("size", v) :: rest ->
6202 let size =
6203 fromstring int_of_string spos "size" v fstate.fontsize in
6204 getsize size rest
6205 | l -> getsize size l
6207 fstate.fontsize <- getsize fstate.fontsize attrs;
6208 if closed
6209 then v
6210 else { v with f = uifont (Buffer.create 10) }
6212 | Vopen ("doc", attrs, closed) ->
6213 let pathent, spage, srely, span, svisy = doc_of attrs in
6214 let path = unent pathent
6215 and pageno = fromstring int_of_string spos "page" spage 0
6216 and rely = fromstring float_of_string spos "rely" srely 0.0
6217 and pan = fromstring int_of_string spos "pan" span 0
6218 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6219 let c = config_of dc attrs in
6220 let anchor = (pageno, rely, visy) in
6221 if closed
6222 then (Hashtbl.add h path (c, [], pan, anchor); v)
6223 else { v with f = doc path pan anchor c [] }
6225 | Vopen _ ->
6226 error "unexpected subelement in llppconfig" s spos
6228 | Vclose "llppconfig" -> { v with f = toplevel }
6229 | Vclose _ -> error "unexpected close in llppconfig" s spos
6231 and defaults v t spos _ =
6232 match t with
6233 | Vdata | Vcdata -> v
6234 | Vend -> error "unexpected end of input in defaults" s spos
6235 | Vopen ("keymap", attrs, closed) ->
6236 let modename =
6237 try List.assoc "mode" attrs
6238 with Not_found -> "global" in
6239 if closed
6240 then v
6241 else
6242 let ret keymap =
6243 let h = findkeyhash dc modename in
6244 KeyMap.iter (Hashtbl.replace h) keymap;
6245 defaults
6247 { v with f = pkeymap ret KeyMap.empty }
6249 | Vopen (_, _, _) ->
6250 error "unexpected subelement in defaults" s spos
6252 | Vclose "defaults" ->
6253 { v with f = llppconfig }
6255 | Vclose _ -> error "unexpected close in defaults" s spos
6257 and uifont b v t spos epos =
6258 match t with
6259 | Vdata | Vcdata ->
6260 Buffer.add_substring b s spos (epos - spos);
6262 | Vopen (_, _, _) ->
6263 error "unexpected subelement in ui-font" s spos
6264 | Vclose "ui-font" ->
6265 if String.length !fontpath = 0
6266 then fontpath := Buffer.contents b;
6267 { v with f = llppconfig }
6268 | Vclose _ -> error "unexpected close in ui-font" s spos
6269 | Vend -> error "unexpected end of input in ui-font" s spos
6271 and doc path pan anchor c bookmarks v t spos _ =
6272 match t with
6273 | Vdata | Vcdata -> v
6274 | Vend -> error "unexpected end of input in doc" s spos
6275 | Vopen ("bookmarks", _, closed) ->
6276 if closed
6277 then v
6278 else { v with f = pbookmarks path pan anchor c bookmarks }
6280 | Vopen ("keymap", attrs, closed) ->
6281 let modename =
6282 try List.assoc "mode" attrs
6283 with Not_found -> "global"
6285 if closed
6286 then v
6287 else
6288 let ret keymap =
6289 let h = findkeyhash c modename in
6290 KeyMap.iter (Hashtbl.replace h) keymap;
6291 doc path pan anchor c bookmarks
6293 { v with f = pkeymap ret KeyMap.empty }
6295 | Vopen (_, _, _) ->
6296 error "unexpected subelement in doc" s spos
6298 | Vclose "doc" ->
6299 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6300 { v with f = llppconfig }
6302 | Vclose _ -> error "unexpected close in doc" s spos
6304 and pkeymap ret keymap v t spos _ =
6305 match t with
6306 | Vdata | Vcdata -> v
6307 | Vend -> error "unexpected end of input in keymap" s spos
6308 | Vopen ("map", attrs, closed) ->
6309 let r, l = map_of attrs in
6310 let kss = fromstring keys_of_string spos "in" r [] in
6311 let lss = fromstring keys_of_string spos "out" l [] in
6312 let keymap =
6313 match kss with
6314 | [] -> keymap
6315 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6316 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6318 if closed
6319 then { v with f = pkeymap ret keymap }
6320 else
6321 let f () = v in
6322 { v with f = skip "map" f }
6324 | Vopen _ ->
6325 error "unexpected subelement in keymap" s spos
6327 | Vclose "keymap" ->
6328 { v with f = ret keymap }
6330 | Vclose _ -> error "unexpected close in keymap" s spos
6332 and pbookmarks path pan anchor c bookmarks v t spos _ =
6333 match t with
6334 | Vdata | Vcdata -> v
6335 | Vend -> error "unexpected end of input in bookmarks" s spos
6336 | Vopen ("item", attrs, closed) ->
6337 let titleent, spage, srely, svisy = bookmark_of attrs in
6338 let page = fromstring int_of_string spos "page" spage 0
6339 and rely = fromstring float_of_string spos "rely" srely 0.0
6340 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6341 let bookmarks =
6342 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6344 if closed
6345 then { v with f = pbookmarks path pan anchor c bookmarks }
6346 else
6347 let f () = v in
6348 { v with f = skip "item" f }
6350 | Vopen _ ->
6351 error "unexpected subelement in bookmarks" s spos
6353 | Vclose "bookmarks" ->
6354 { v with f = doc path pan anchor c bookmarks }
6356 | Vclose _ -> error "unexpected close in bookmarks" s spos
6358 and skip tag f v t spos _ =
6359 match t with
6360 | Vdata | Vcdata -> v
6361 | Vend ->
6362 error ("unexpected end of input in skipped " ^ tag) s spos
6363 | Vopen (tag', _, closed) ->
6364 if closed
6365 then v
6366 else
6367 let f' () = { v with f = skip tag f } in
6368 { v with f = skip tag' f' }
6369 | Vclose ctag ->
6370 if tag = ctag
6371 then f ()
6372 else error ("unexpected close in skipped " ^ tag) s spos
6375 parse { f = toplevel; accu = () } s;
6376 h, dc;
6379 let do_load f ic =
6381 let len = in_channel_length ic in
6382 let s = String.create len in
6383 really_input ic s 0 len;
6384 f s;
6385 with
6386 | Parse_error (msg, s, pos) ->
6387 let subs = subs s pos in
6388 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6389 failwith ("parse error: " ^ s)
6391 | exn ->
6392 failwith ("config load error: " ^ Printexc.to_string exn)
6395 let defconfpath =
6396 let dir =
6398 let dir = Filename.concat home ".config" in
6399 if Sys.is_directory dir then dir else home
6400 with _ -> home
6402 Filename.concat dir "llpp.conf"
6405 let confpath = ref defconfpath;;
6407 let load1 f =
6408 if Sys.file_exists !confpath
6409 then
6410 match
6411 (try Some (open_in_bin !confpath)
6412 with exn ->
6413 prerr_endline
6414 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6415 Printexc.to_string exn);
6416 None
6418 with
6419 | Some ic ->
6420 let success =
6422 f (do_load get ic)
6423 with exn ->
6424 prerr_endline
6425 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6426 Printexc.to_string exn);
6427 false
6429 close_in ic;
6430 success
6432 | None -> false
6433 else
6434 f (Hashtbl.create 0, defconf)
6437 let load () =
6438 let f (h, dc) =
6439 let pc, pb, px, pa =
6441 Hashtbl.find h (Filename.basename state.path)
6442 with Not_found -> dc, [], 0, emptyanchor
6444 setconf defconf dc;
6445 setconf conf pc;
6446 state.bookmarks <- pb;
6447 state.x <- px;
6448 state.scrollw <- conf.scrollbw;
6449 if conf.jumpback
6450 then state.anchor <- pa;
6451 cbput state.hists.nav pa;
6452 true
6454 load1 f
6457 let add_attrs bb always dc c =
6458 let ob s a b =
6459 if always || a != b
6460 then Printf.bprintf bb "\n %s='%b'" s a
6461 and oi s a b =
6462 if always || a != b
6463 then Printf.bprintf bb "\n %s='%d'" s a
6464 and oI s a b =
6465 if always || a != b
6466 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6467 and oz s a b =
6468 if always || a <> b
6469 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6470 and oF s a b =
6471 if always || a <> b
6472 then Printf.bprintf bb "\n %s='%f'" s a
6473 and oc s a b =
6474 if always || a <> b
6475 then
6476 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6477 and oC s a b =
6478 if always || a <> b
6479 then
6480 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6481 and oR s a b =
6482 if always || a <> b
6483 then
6484 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6485 and os s a b =
6486 if always || a <> b
6487 then
6488 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6489 and og s a b =
6490 if always || a <> b
6491 then
6492 match a with
6493 | None -> ()
6494 | Some (_N, _A, _B) ->
6495 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6496 and oW s a b =
6497 if always || a <> b
6498 then
6499 let v =
6500 match a with
6501 | None -> "false"
6502 | Some f ->
6503 if f = infinity
6504 then "true"
6505 else string_of_float f
6507 Printf.bprintf bb "\n %s='%s'" s v
6508 and oco s a b =
6509 if always || a <> b
6510 then
6511 match a with
6512 | Cmulti ((n, a, b), _) when n > 1 ->
6513 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6514 | Csplit (n, _) when n > 1 ->
6515 Printf.bprintf bb "\n %s='%d'" s ~-n
6516 | _ -> ()
6517 and obeco s a b =
6518 if always || a <> b
6519 then
6520 match a with
6521 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6522 | _ -> ()
6524 let w, h =
6525 if always
6526 then dc.winw, dc.winh
6527 else
6528 match state.fullscreen with
6529 | Some wh -> wh
6530 | None -> c.winw, c.winh
6532 oi "width" w dc.winw;
6533 oi "height" h dc.winh;
6534 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6535 oi "scroll-handle-height" c.scrollh dc.scrollh;
6536 ob "case-insensitive-search" c.icase dc.icase;
6537 ob "preload" c.preload dc.preload;
6538 oi "page-bias" c.pagebias dc.pagebias;
6539 oi "scroll-step" c.scrollstep dc.scrollstep;
6540 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6541 ob "max-height-fit" c.maxhfit dc.maxhfit;
6542 ob "crop-hack" c.crophack dc.crophack;
6543 oW "throttle" c.maxwait dc.maxwait;
6544 ob "highlight-links" c.hlinks dc.hlinks;
6545 ob "under-cursor-info" c.underinfo dc.underinfo;
6546 oi "vertical-margin" c.interpagespace dc.interpagespace;
6547 oz "zoom" c.zoom dc.zoom;
6548 ob "presentation" c.presentation dc.presentation;
6549 oi "rotation-angle" c.angle dc.angle;
6550 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6551 ob "proportional-display" c.proportional dc.proportional;
6552 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6553 oi "tex-count" c.texcount dc.texcount;
6554 oi "slice-height" c.sliceheight dc.sliceheight;
6555 oi "thumbnail-width" c.thumbw dc.thumbw;
6556 ob "persistent-location" c.jumpback dc.jumpback;
6557 oc "background-color" c.bgcolor dc.bgcolor;
6558 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6559 oi "tile-width" c.tilew dc.tilew;
6560 oi "tile-height" c.tileh dc.tileh;
6561 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6562 ob "checkers" c.checkers dc.checkers;
6563 oi "aalevel" c.aalevel dc.aalevel;
6564 ob "trim-margins" c.trimmargins dc.trimmargins;
6565 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6566 os "uri-launcher" c.urilauncher dc.urilauncher;
6567 os "path-launcher" c.pathlauncher dc.pathlauncher;
6568 oC "color-space" c.colorspace dc.colorspace;
6569 ob "invert-colors" c.invert dc.invert;
6570 oF "brightness" c.colorscale dc.colorscale;
6571 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6572 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6573 oco "columns" c.columns dc.columns;
6574 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6575 os "selection-command" c.selcmd dc.selcmd;
6576 ob "update-cursor" c.updatecurs dc.updatecurs;
6577 oi "hint-font-size" c.hfsize dc.hfsize;
6578 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6579 oF "page-scroll-scale" c.pgscale dc.pgscale;
6580 ob "multi-column-centering" c.multicenter dc.multicenter;
6583 let keymapsbuf always dc c =
6584 let bb = Buffer.create 16 in
6585 let rec loop = function
6586 | [] -> ()
6587 | (modename, h) :: rest ->
6588 let dh = findkeyhash dc modename in
6589 if always || h <> dh
6590 then (
6591 if Hashtbl.length h > 0
6592 then (
6593 if Buffer.length bb > 0
6594 then Buffer.add_char bb '\n';
6595 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6596 Hashtbl.iter (fun i o ->
6597 let isdifferent = always ||
6599 let dO = Hashtbl.find dh i in
6600 dO <> o
6601 with Not_found -> true
6603 if isdifferent
6604 then
6605 let addkm (k, m) =
6606 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6607 if Wsi.withalt m then Buffer.add_string bb "alt-";
6608 if Wsi.withshift m then Buffer.add_string bb "shift-";
6609 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6610 Buffer.add_string bb (Wsi.keyname k);
6612 let addkms l =
6613 let rec loop = function
6614 | [] -> ()
6615 | km :: [] -> addkm km
6616 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6618 loop l
6620 Buffer.add_string bb "<map in='";
6621 addkm i;
6622 match o with
6623 | KMinsrt km ->
6624 Buffer.add_string bb "' out='";
6625 addkm km;
6626 Buffer.add_string bb "'/>\n"
6628 | KMinsrl kms ->
6629 Buffer.add_string bb "' out='";
6630 addkms kms;
6631 Buffer.add_string bb "'/>\n"
6633 | KMmulti (ins, kms) ->
6634 Buffer.add_char bb ' ';
6635 addkms ins;
6636 Buffer.add_string bb "' out='";
6637 addkms kms;
6638 Buffer.add_string bb "'/>\n"
6639 ) h;
6640 Buffer.add_string bb "</keymap>";
6643 loop rest
6645 loop c.keyhashes;
6649 let save () =
6650 let uifontsize = fstate.fontsize in
6651 let bb = Buffer.create 32768 in
6652 let f (h, dc) =
6653 let dc = if conf.bedefault then conf else dc in
6654 Buffer.add_string bb "<llppconfig>\n";
6656 if String.length !fontpath > 0
6657 then
6658 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6659 uifontsize
6660 !fontpath
6661 else (
6662 if uifontsize <> 14
6663 then
6664 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6667 Buffer.add_string bb "<defaults ";
6668 add_attrs bb true dc dc;
6669 let kb = keymapsbuf true dc dc in
6670 if Buffer.length kb > 0
6671 then (
6672 Buffer.add_string bb ">\n";
6673 Buffer.add_buffer bb kb;
6674 Buffer.add_string bb "\n</defaults>\n";
6676 else Buffer.add_string bb "/>\n";
6678 let adddoc path pan anchor c bookmarks =
6679 if bookmarks == [] && c = dc && anchor = emptyanchor
6680 then ()
6681 else (
6682 Printf.bprintf bb "<doc path='%s'"
6683 (enent path 0 (String.length path));
6685 if anchor <> emptyanchor
6686 then (
6687 let n, rely, visy = anchor in
6688 Printf.bprintf bb " page='%d'" n;
6689 if rely > 1e-6
6690 then
6691 Printf.bprintf bb " rely='%f'" rely
6693 if abs_float visy > 1e-6
6694 then
6695 Printf.bprintf bb " visy='%f'" visy
6699 if pan != 0
6700 then Printf.bprintf bb " pan='%d'" pan;
6702 add_attrs bb false dc c;
6703 let kb = keymapsbuf false dc c in
6705 begin match bookmarks with
6706 | [] ->
6707 if Buffer.length kb > 0
6708 then (
6709 Buffer.add_string bb ">\n";
6710 Buffer.add_buffer bb kb;
6711 Buffer.add_string bb "\n</doc>\n";
6713 else Buffer.add_string bb "/>\n"
6714 | _ ->
6715 Buffer.add_string bb ">\n<bookmarks>\n";
6716 List.iter (fun (title, _level, (page, rely, visy)) ->
6717 Printf.bprintf bb
6718 "<item title='%s' page='%d'"
6719 (enent title 0 (String.length title))
6720 page
6722 if rely > 1e-6
6723 then
6724 Printf.bprintf bb " rely='%f'" rely
6726 if abs_float visy > 1e-6
6727 then
6728 Printf.bprintf bb " visy='%f'" visy
6730 Buffer.add_string bb "/>\n";
6731 ) bookmarks;
6732 Buffer.add_string bb "</bookmarks>";
6733 if Buffer.length kb > 0
6734 then (
6735 Buffer.add_string bb "\n";
6736 Buffer.add_buffer bb kb;
6738 Buffer.add_string bb "\n</doc>\n";
6739 end;
6743 let pan, conf =
6744 match state.mode with
6745 | Birdseye (c, pan, _, _, _) ->
6746 let beyecolumns =
6747 match conf.columns with
6748 | Cmulti ((c, _, _), _) -> Some c
6749 | Csingle _ -> None
6750 | Csplit _ -> None
6751 and columns =
6752 match c.columns with
6753 | Cmulti (c, _) -> Cmulti (c, [||])
6754 | Csingle _ -> Csingle [||]
6755 | Csplit _ -> failwith "quit from bird's eye while split"
6757 pan, { c with beyecolumns = beyecolumns; columns = columns }
6758 | _ -> state.x, conf
6760 let basename = Filename.basename state.path in
6761 adddoc basename pan (getanchor ())
6762 (let conf =
6763 let autoscrollstep =
6764 match state.autoscroll with
6765 | Some step -> step
6766 | None -> conf.autoscrollstep
6768 match state.mode with
6769 | Birdseye (bc, _, _, _, _) ->
6770 { conf with
6771 zoom = bc.zoom;
6772 presentation = bc.presentation;
6773 interpagespace = bc.interpagespace;
6774 maxwait = bc.maxwait;
6775 autoscrollstep = autoscrollstep }
6776 | _ -> { conf with autoscrollstep = autoscrollstep }
6777 in conf)
6778 (if conf.savebmarks then state.bookmarks else []);
6780 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
6781 if basename <> path
6782 then adddoc path x anchor c bookmarks
6783 ) h;
6784 Buffer.add_string bb "</llppconfig>\n";
6785 true;
6787 if load1 f && Buffer.length bb > 0
6788 then
6790 let tmp = !confpath ^ ".tmp" in
6791 let oc = open_out_bin tmp in
6792 Buffer.output_buffer oc bb;
6793 close_out oc;
6794 Unix.rename tmp !confpath;
6795 with exn ->
6796 prerr_endline
6797 ("error while saving configuration: " ^ Printexc.to_string exn)
6799 end;;
6801 let () =
6802 let trimcachepath = ref "" in
6803 Arg.parse
6804 (Arg.align
6805 [("-p", Arg.String (fun s -> state.password <- s) ,
6806 "<password> Set password");
6808 ("-f", Arg.String (fun s -> Config.fontpath := s),
6809 "<path> Set path to the user interface font");
6811 ("-c", Arg.String (fun s -> Config.confpath := s),
6812 "<path> Set path to the configuration file");
6814 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6815 "<path> Set path to the trim cache file");
6817 ("-v", Arg.Unit (fun () ->
6818 Printf.printf
6819 "%s\nconfiguration path: %s\n"
6820 (version ())
6821 Config.defconfpath
6823 exit 0), " Print version and exit");
6826 (fun s -> state.path <- s)
6827 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6829 if String.length state.path = 0
6830 then (prerr_endline "file name missing"; exit 1);
6832 if not (Config.load ())
6833 then prerr_endline "failed to load configuration";
6835 let globalkeyhash = findkeyhash conf "global" in
6836 let wsfd, winw, winh = Wsi.init (object
6837 method expose =
6838 if nogeomcmds state.geomcmds || platform == Posx
6839 then display ()
6840 else (
6841 GlClear.color (scalecolor2 conf.bgcolor);
6842 GlClear.clear [`color];
6844 method display = display ()
6845 method reshape w h = reshape w h
6846 method mouse b d x y m = mouse b d x y m
6847 method motion x y = state.mpos <- (x, y); motion x y
6848 method pmotion x y = state.mpos <- (x, y); pmotion x y
6849 method key k m =
6850 let mascm = m land (
6851 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6852 ) in
6853 match state.keystate with
6854 | KSnone ->
6855 let km = k, mascm in
6856 begin
6857 match
6858 let modehash = state.uioh#modehash in
6859 try Hashtbl.find modehash km
6860 with Not_found ->
6861 try Hashtbl.find globalkeyhash km
6862 with Not_found -> KMinsrt (k, m)
6863 with
6864 | KMinsrt (k, m) -> keyboard k m
6865 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6866 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6868 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6869 List.iter (fun (k, m) -> keyboard k m) insrt;
6870 state.keystate <- KSnone
6871 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6872 state.keystate <- KSinto (keys, insrt)
6873 | _ ->
6874 state.keystate <- KSnone
6876 method enter x y = state.mpos <- (x, y); pmotion x y
6877 method leave = state.mpos <- (-1, -1)
6878 method quit = raise Quit
6879 end) conf.winw conf.winh (platform = Posx) in
6881 state.wsfd <- wsfd;
6883 if not (
6884 List.exists GlMisc.check_extension
6885 [ "GL_ARB_texture_rectangle"
6886 ; "GL_EXT_texture_recangle"
6887 ; "GL_NV_texture_rectangle" ]
6889 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6891 let cr, sw =
6892 match Ne.pipe () with
6893 | Ne.Exn exn ->
6894 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
6895 exit 1
6896 | Ne.Res rw -> rw
6897 and sr, cw =
6898 match Ne.pipe () with
6899 | Ne.Exn exn ->
6900 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
6901 exit 1
6902 | Ne.Res rw -> rw
6905 cloexec cr;
6906 cloexec sw;
6907 cloexec sr;
6908 cloexec cw;
6910 setcheckers conf.checkers;
6911 redirectstderr ();
6913 init (cr, cw) (
6914 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6915 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6916 !Config.fontpath, !trimcachepath
6918 state.sr <- sr;
6919 state.sw <- sw;
6920 state.text <- "Opening " ^ state.path;
6921 reshape winw winh;
6922 opendoc state.path state.password;
6923 state.uioh <- uioh;
6925 let rec loop deadline =
6926 let r =
6927 match state.errfd with
6928 | None -> [state.sr; state.wsfd]
6929 | Some fd -> [state.sr; state.wsfd; fd]
6931 if state.redisplay
6932 then (
6933 state.redisplay <- false;
6934 display ();
6936 let timeout =
6937 let now = now () in
6938 if deadline > now
6939 then (
6940 if deadline = infinity
6941 then ~-.1.0
6942 else max 0.0 (deadline -. now)
6944 else 0.0
6946 let r, _, _ =
6947 try Unix.select r [] [] timeout
6948 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6950 begin match r with
6951 | [] ->
6952 state.ghyll None;
6953 let newdeadline =
6954 if state.ghyll == noghyll
6955 then
6956 match state.autoscroll with
6957 | Some step when step != 0 ->
6958 let y = state.y + step in
6959 let y =
6960 if y < 0
6961 then state.maxy
6962 else if y >= state.maxy then 0 else y
6964 gotoy y;
6965 if state.mode = View
6966 then state.text <- "";
6967 deadline +. 0.01
6968 | _ -> infinity
6969 else deadline +. 0.01
6971 loop newdeadline
6973 | l ->
6974 let rec checkfds = function
6975 | [] -> ()
6976 | fd :: rest when fd = state.sr ->
6977 let cmd = readcmd state.sr in
6978 act cmd;
6979 checkfds rest
6981 | fd :: rest when fd = state.wsfd ->
6982 Wsi.readresp fd;
6983 checkfds rest
6985 | fd :: rest ->
6986 let s = String.create 80 in
6987 let n = Unix.read fd s 0 80 in
6988 if conf.redirectstderr
6989 then (
6990 Buffer.add_substring state.errmsgs s 0 n;
6991 state.newerrmsgs <- true;
6992 state.redisplay <- true;
6994 else (
6995 prerr_string (String.sub s 0 n);
6996 flush stderr;
6998 checkfds rest
7000 checkfds l;
7001 let newdeadline =
7002 let deadline1 =
7003 if deadline = infinity
7004 then now () +. 0.01
7005 else deadline
7007 match state.autoscroll with
7008 | Some step when step != 0 -> deadline1
7009 | _ -> if state.ghyll == noghyll then infinity else deadline1
7011 loop newdeadline
7012 end;
7015 loop infinity;
7016 with Quit ->
7017 Config.save ();