Robustness
[llpp.git] / main.ml
blob3ac45b178fccd56909da03168cff52de1e657c9b
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 calcheight () =
1029 match conf.columns with
1030 | Cmulti ((c, _, _), b) ->
1031 let rec loop y h n =
1032 if n < 0
1033 then loop y h (n+1)
1034 else (
1035 if n = Array.length b
1036 then y + h
1037 else
1038 let (_, _, y', (_, _, h', _)) = b.(n) in
1039 let y = min y y'
1040 and h = max h h' in
1041 loop y h (n+1)
1044 loop max_int 0 (((Array.length b - 1) / c) * c)
1045 | Csingle b ->
1046 if Array.length b > 0
1047 then
1048 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1049 y + h + (if conf.presentation then calcips h else 0)
1050 else 0
1051 | Csplit (_, b) ->
1052 if Array.length b > 0
1053 then
1054 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1055 y + h
1056 else 0
1059 let getpageyh pageno =
1060 let pageno = bound pageno 0 (state.pagecount-1) in
1061 match conf.columns with
1062 | Csingle b ->
1063 if Array.length b = 0
1064 then 0, 0
1065 else
1066 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1067 let y =
1068 if conf.presentation
1069 then y - calcips h
1070 else y
1072 y, h
1073 | Cmulti (_, b) ->
1074 if Array.length b = 0
1075 then 0, 0
1076 else
1077 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1078 y, h
1079 | Csplit (c, b) ->
1080 if Array.length b = 0
1081 then 0, 0
1082 else
1083 let n = pageno*c in
1084 let (_, _, y, (_, _, h, _)) = b.(n) in
1085 y, h
1088 let getpagedim pageno =
1089 let rec f ppdim l =
1090 match l with
1091 | (n, _, _, _) as pdim :: rest ->
1092 if n >= pageno
1093 then (if n = pageno then pdim else ppdim)
1094 else f pdim rest
1096 | [] -> ppdim
1098 f (-1, -1, -1, -1) state.pdims
1101 let getpagey pageno = fst (getpageyh pageno);;
1103 let nogeomcmds cmds =
1104 match cmds with
1105 | s, [] -> String.length s = 0
1106 | _ -> false
1109 let page_of_y y =
1110 let (c, coverA, coverB), b =
1111 match conf.columns with
1112 | Csingle b -> (1, 0, 0), b
1113 | Cmulti (c, b) -> c, b
1114 | Csplit (_, b) -> (1, 0, 0), b
1116 let rec bsearch nmin nmax =
1117 if nmin > nmax
1118 then bound nmin 0 (state.pagecount-1)
1119 else
1120 let n = (nmax + nmin) / 2 in
1121 let _, _, vy, (_, _, h, _) = b.(n) in
1122 let y0, y1 =
1123 if conf.presentation
1124 then
1125 let ips = calcips h in
1126 let y0 = vy - ips in
1127 let y1 = vy + h + ips in
1128 y0, y1
1129 else (
1130 if n = 0
1131 then 0, vy + h + conf.interpagespace
1132 else
1133 let y0 = vy - conf.interpagespace in
1134 y0, y0 + h + conf.interpagespace
1137 if y >= y0 && y < y1
1138 then (
1139 if c = 1
1140 then n
1141 else (
1142 if n > coverA
1143 then
1144 if n < state.pagecount - coverB
1145 then ((n-coverA)/c)*c + coverA
1146 else n
1147 else n
1150 else (
1151 if y > y0
1152 then bsearch (n+1) nmax
1153 else bsearch nmin (n-1)
1156 let r = bsearch 0 (state.pagecount-1) in
1160 let layoutN ((columns, coverA, coverB), b) y sh =
1161 let sh = sh - state.hscrollh in
1162 let rec fold accu n =
1163 if n = Array.length b
1164 then accu
1165 else
1166 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1167 if (vy - y) > sh &&
1168 (n = coverA - 1
1169 || n = state.pagecount - coverB
1170 || (n - coverA) mod columns = columns - 1)
1171 then accu
1172 else
1173 let accu =
1174 if vy + h > y
1175 then
1176 let pagey = max 0 (y - vy) in
1177 let pagedispy = if pagey > 0 then 0 else vy - y in
1178 let pagedispx, pagex =
1179 let pdx =
1180 if n = coverA - 1 || n = state.pagecount - coverB
1181 then state.x + (conf.winw - state.scrollw - w) / 2
1182 else dx + xoff + state.x
1184 if pdx < 0
1185 then 0, -pdx
1186 else pdx, 0
1188 let pagevw =
1189 let vw = conf.winw - state.scrollw - pagedispx in
1190 let pw = w - pagex in
1191 min vw pw
1193 let pagevh = min (h - pagey) (sh - pagedispy) in
1194 if pagevw > 0 && pagevh > 0
1195 then
1196 let e =
1197 { pageno = n
1198 ; pagedimno = pdimno
1199 ; pagew = w
1200 ; pageh = h
1201 ; pagex = pagex
1202 ; pagey = pagey
1203 ; pagevw = pagevw
1204 ; pagevh = pagevh
1205 ; pagedispx = pagedispx
1206 ; pagedispy = pagedispy
1207 ; pagecol = 0
1210 e :: accu
1211 else
1212 accu
1213 else
1214 accu
1216 fold accu (n+1)
1218 List.rev (fold [] (page_of_y y));
1221 let layoutS (columns, b) y sh =
1222 let sh = sh - state.hscrollh in
1223 let rec fold accu n =
1224 if n = Array.length b
1225 then accu
1226 else
1227 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1228 if (vy - y) > sh
1229 then accu
1230 else
1231 let accu =
1232 if vy + pageh > y
1233 then
1234 let x = xoff + state.x in
1235 let pagey = max 0 (y - vy) in
1236 let pagedispy = if pagey > 0 then 0 else vy - y in
1237 let pagedispx, pagex =
1238 if px = 0
1239 then (
1240 if x < 0
1241 then 0, -x
1242 else x, 0
1244 else (
1245 let px = px - x in
1246 if px < 0
1247 then -px, 0
1248 else 0, px
1251 let pagecolw = pagew/columns in
1252 let pagedispx =
1253 if pagecolw < conf.winw
1254 then pagedispx + ((conf.winw - state.scrollw - pagecolw) / 2)
1255 else pagedispx
1257 let pagevw =
1258 let vw = conf.winw - pagedispx - state.scrollw in
1259 let pw = pagew - pagex in
1260 min vw pw
1262 let pagevw = min pagevw pagecolw in
1263 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1264 if pagevw > 0 && pagevh > 0
1265 then
1266 let e =
1267 { pageno = n/columns
1268 ; pagedimno = pdimno
1269 ; pagew = pagew
1270 ; pageh = pageh
1271 ; pagex = pagex
1272 ; pagey = pagey
1273 ; pagevw = pagevw
1274 ; pagevh = pagevh
1275 ; pagedispx = pagedispx
1276 ; pagedispy = pagedispy
1277 ; pagecol = n mod columns
1280 e :: accu
1281 else
1282 accu
1283 else
1284 accu
1286 fold accu (n+1)
1288 List.rev (fold [] 0)
1291 let layout y sh =
1292 if nogeomcmds state.geomcmds
1293 then
1294 match conf.columns with
1295 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1296 | Cmulti c -> layoutN c y sh
1297 | Csplit s -> layoutS s y sh
1298 else []
1301 let clamp incr =
1302 let y = state.y + incr in
1303 let y = max 0 y in
1304 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1308 let itertiles l f =
1309 let tilex = l.pagex mod conf.tilew in
1310 let tiley = l.pagey mod conf.tileh in
1312 let col = l.pagex / conf.tilew in
1313 let row = l.pagey / conf.tileh in
1315 let rec rowloop row y0 dispy h =
1316 if h = 0
1317 then ()
1318 else (
1319 let dh = conf.tileh - y0 in
1320 let dh = min h dh in
1321 let rec colloop col x0 dispx w =
1322 if w = 0
1323 then ()
1324 else (
1325 let dw = conf.tilew - x0 in
1326 let dw = min w dw in
1328 f col row dispx dispy x0 y0 dw dh;
1329 colloop (col+1) 0 (dispx+dw) (w-dw)
1332 colloop col tilex l.pagedispx l.pagevw;
1333 rowloop (row+1) 0 (dispy+dh) (h-dh)
1336 if l.pagevw > 0 && l.pagevh > 0
1337 then rowloop row tiley l.pagedispy l.pagevh;
1340 let gettileopaque l col row =
1341 let key =
1342 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1344 try Some (Hashtbl.find state.tilemap key)
1345 with Not_found -> None
1348 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1349 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1350 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1353 let drawtiles l color =
1354 GlDraw.color color;
1355 let f col row x y tilex tiley w h =
1356 match gettileopaque l col row with
1357 | Some (opaque, _, t) ->
1358 let params = x, y, w, h, tilex, tiley in
1359 if conf.invert
1360 then (
1361 Gl.enable `blend;
1362 GlFunc.blend_func `zero `one_minus_src_color;
1364 drawtile params opaque;
1365 if conf.invert
1366 then Gl.disable `blend;
1367 if conf.debug
1368 then (
1369 let s = Printf.sprintf
1370 "%d[%d,%d] %f sec"
1371 l.pageno col row t
1373 let w = measurestr fstate.fontsize s in
1374 GlMisc.push_attrib [`current];
1375 GlDraw.color (0.0, 0.0, 0.0);
1376 GlDraw.rect
1377 (float (x-2), float (y-2))
1378 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1379 GlDraw.color (1.0, 1.0, 1.0);
1380 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1381 GlMisc.pop_attrib ();
1384 | _ ->
1385 let w =
1386 let lw = conf.winw - state.scrollw - x in
1387 min lw w
1388 and h =
1389 let lh = conf.winh - y in
1390 min lh h
1392 begin match state.texid with
1393 | Some id ->
1394 Gl.enable `texture_2d;
1395 GlTex.bind_texture `texture_2d id;
1396 let x0 = float x
1397 and y0 = float y
1398 and x1 = float (x+w)
1399 and y1 = float (y+h) in
1401 let tw = float w /. 64.0
1402 and th = float h /. 64.0 in
1403 let tx0 = float tilex /. 64.0
1404 and ty0 = float tiley /. 64.0 in
1405 let tx1 = tx0 +. tw
1406 and ty1 = ty0 +. th in
1407 GlDraw.begins `quads;
1408 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1409 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1410 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1411 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1412 GlDraw.ends ();
1414 Gl.disable `texture_2d;
1415 | None ->
1416 GlDraw.color (1.0, 1.0, 1.0);
1417 GlDraw.rect
1418 (float x, float y)
1419 (float (x+w), float (y+h));
1420 end;
1421 if w > 128 && h > fstate.fontsize + 10
1422 then (
1423 GlDraw.color (0.0, 0.0, 0.0);
1424 let c, r =
1425 if conf.verbose
1426 then (col*conf.tilew, row*conf.tileh)
1427 else col, row
1429 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1431 GlDraw.color color;
1433 itertiles l f
1436 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1438 let tilevisible1 l x y =
1439 let ax0 = l.pagex
1440 and ax1 = l.pagex + l.pagevw
1441 and ay0 = l.pagey
1442 and ay1 = l.pagey + l.pagevh in
1444 let bx0 = x
1445 and by0 = y in
1446 let bx1 = min (bx0 + conf.tilew) l.pagew
1447 and by1 = min (by0 + conf.tileh) l.pageh in
1449 let rx0 = max ax0 bx0
1450 and ry0 = max ay0 by0
1451 and rx1 = min ax1 bx1
1452 and ry1 = min ay1 by1 in
1454 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1455 nonemptyintersection
1458 let tilevisible layout n x y =
1459 let rec findpageinlayout m = function
1460 | l :: rest when l.pageno = n ->
1461 tilevisible1 l x y || (
1462 match conf.columns with
1463 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1464 | _ -> false
1466 | _ :: rest -> findpageinlayout 0 rest
1467 | [] -> false
1469 findpageinlayout 0 layout;
1472 let tileready l x y =
1473 tilevisible1 l x y &&
1474 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1477 let tilepage n p layout =
1478 let rec loop = function
1479 | l :: rest ->
1480 if l.pageno = n
1481 then
1482 let f col row _ _ _ _ _ _ =
1483 if state.currently = Idle
1484 then
1485 match gettileopaque l col row with
1486 | Some _ -> ()
1487 | None ->
1488 let x = col*conf.tilew
1489 and y = row*conf.tileh in
1490 let w =
1491 let w = l.pagew - x in
1492 min w conf.tilew
1494 let h =
1495 let h = l.pageh - y in
1496 min h conf.tileh
1498 wcmd "tile %s %d %d %d %d" p x y w h;
1499 state.currently <-
1500 Tiling (
1501 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1502 conf.tilew, conf.tileh
1505 itertiles l f;
1506 else
1507 loop rest
1509 | [] -> ()
1511 if nogeomcmds state.geomcmds
1512 then loop layout;
1515 let preloadlayout y =
1516 let y = if y < conf.winh then 0 else y - conf.winh in
1517 let h = conf.winh*3 in
1518 layout y h;
1521 let load pages =
1522 let rec loop pages =
1523 if state.currently != Idle
1524 then ()
1525 else
1526 match pages with
1527 | l :: rest ->
1528 begin match getopaque l.pageno with
1529 | None ->
1530 wcmd "page %d %d" l.pageno l.pagedimno;
1531 state.currently <- Loading (l, state.gen);
1532 | Some opaque ->
1533 tilepage l.pageno opaque pages;
1534 loop rest
1535 end;
1536 | _ -> ()
1538 if nogeomcmds state.geomcmds
1539 then loop pages
1542 let preload pages =
1543 load pages;
1544 if conf.preload && state.currently = Idle
1545 then load (preloadlayout state.y);
1548 let layoutready layout =
1549 let rec fold all ls =
1550 all && match ls with
1551 | l :: rest ->
1552 let seen = ref false in
1553 let allvisible = ref true in
1554 let foo col row _ _ _ _ _ _ =
1555 seen := true;
1556 allvisible := !allvisible &&
1557 begin match gettileopaque l col row with
1558 | Some _ -> true
1559 | None -> false
1562 itertiles l foo;
1563 fold (!seen && !allvisible) rest
1564 | [] -> true
1566 let alltilesvisible = fold true layout in
1567 alltilesvisible;
1570 let gotoy y =
1571 let y = bound y 0 state.maxy in
1572 let y, layout, proceed =
1573 match conf.maxwait with
1574 | Some time when state.ghyll == noghyll ->
1575 begin match state.throttle with
1576 | None ->
1577 let layout = layout y conf.winh in
1578 let ready = layoutready layout in
1579 if not ready
1580 then (
1581 load layout;
1582 state.throttle <- Some (layout, y, now ());
1584 else G.postRedisplay "gotoy showall (None)";
1585 y, layout, ready
1586 | Some (_, _, started) ->
1587 let dt = now () -. started in
1588 if dt > time
1589 then (
1590 state.throttle <- None;
1591 let layout = layout y conf.winh in
1592 load layout;
1593 G.postRedisplay "maxwait";
1594 y, layout, true
1596 else -1, [], false
1599 | _ ->
1600 let layout = layout y conf.winh in
1601 if true || layoutready layout
1602 then G.postRedisplay "gotoy ready";
1603 y, layout, true
1605 if proceed
1606 then (
1607 state.y <- y;
1608 state.layout <- layout;
1609 begin match state.mode with
1610 | LinkNav (Ltexact (pageno, linkno)) ->
1611 let rec loop = function
1612 | [] ->
1613 state.mode <- LinkNav (Ltgendir 0)
1614 | l :: _ when l.pageno = pageno ->
1615 begin match getopaque pageno with
1616 | None ->
1617 state.mode <- LinkNav (Ltgendir 0)
1618 | Some opaque ->
1619 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1620 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1621 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1622 then state.mode <- LinkNav (Ltgendir 0)
1624 | _ :: rest -> loop rest
1626 loop layout
1627 | _ -> ()
1628 end;
1629 begin match state.mode with
1630 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1631 if not (pagevisible layout pageno)
1632 then (
1633 match state.layout with
1634 | [] -> ()
1635 | l :: _ ->
1636 state.mode <- Birdseye (
1637 conf, leftx, l.pageno, hooverpageno, anchor
1640 | LinkNav (Ltgendir dir as lt) ->
1641 let linknav =
1642 let rec loop = function
1643 | [] -> lt
1644 | l :: rest ->
1645 match getopaque l.pageno with
1646 | None -> loop rest
1647 | Some opaque ->
1648 let link =
1649 let ld =
1650 if dir = 0
1651 then LDfirstvisible (l.pagex, l.pagey, dir)
1652 else (
1653 if dir > 0 then LDfirst else LDlast
1656 findlink opaque ld
1658 match link with
1659 | Lnotfound -> loop rest
1660 | Lfound n ->
1661 showlinktype (getlink opaque n);
1662 Ltexact (l.pageno, n)
1664 loop state.layout
1666 state.mode <- LinkNav linknav
1667 | _ -> ()
1668 end;
1669 preload layout;
1671 state.ghyll <- noghyll;
1672 if conf.updatecurs
1673 then (
1674 let mx, my = state.mpos in
1675 updateunder mx my;
1679 let conttiling pageno opaque =
1680 tilepage pageno opaque
1681 (if conf.preload then preloadlayout state.y else state.layout)
1684 let gotoy_and_clear_text y =
1685 if not conf.verbose then state.text <- "";
1686 gotoy y;
1689 let getanchor1 l =
1690 let top =
1691 let coloff = l.pagecol * l.pageh in
1692 float (l.pagey + coloff) /. float l.pageh
1694 let dtop =
1695 if l.pagedispy = 0
1696 then
1698 else
1699 if conf.presentation
1700 then float l.pagedispy /. float (calcips l.pageh)
1701 else float l.pagedispy /. float conf.interpagespace
1703 (l.pageno, top, dtop)
1706 let getanchor () =
1707 match state.layout with
1708 | l :: _ -> getanchor1 l
1709 | [] ->
1710 let n = page_of_y state.y in
1711 let y, h = getpageyh n in
1712 let dy = y - state.y in
1713 let dtop =
1714 if conf.presentation
1715 then
1716 let ips = calcips h in
1717 float (dy + ips) /. float ips
1718 else
1719 float dy /. float conf.interpagespace
1721 (n, 0.0, dtop)
1724 let getanchory (n, top, dtop) =
1725 let y, h = getpageyh n in
1726 if conf.presentation
1727 then
1728 let ips = calcips h in
1729 y + truncate (top*.float h -. dtop*.float ips) + ips;
1730 else
1731 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1734 let gotoanchor anchor =
1735 gotoy (getanchory anchor);
1738 let addnav () =
1739 cbput state.hists.nav (getanchor ());
1742 let getnav dir =
1743 let anchor = cbgetc state.hists.nav dir in
1744 getanchory anchor;
1747 let gotoghyll y =
1748 let scroll f n a b =
1749 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1750 let snake f a b =
1751 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1752 if f < a
1753 then s (float f /. float a)
1754 else (
1755 if f > b
1756 then 1.0 -. s ((float (f-b) /. float (n-b)))
1757 else 1.0
1760 snake f a b
1761 and summa f n a b =
1762 (* courtesy:
1763 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1764 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1765 let iv1 = iv f in
1766 let ins = float a *. iv1
1767 and outs = float (n-b) *. iv1 in
1768 let ones = b - a in
1769 ins +. outs +. float ones
1771 let rec set (_N, _A, _B) y sy =
1772 let sum = summa 1.0 _N _A _B in
1773 let dy = float (y - sy) in
1774 state.ghyll <- (
1775 let rec gf n y1 o =
1776 if n >= _N
1777 then state.ghyll <- noghyll
1778 else
1779 let go n =
1780 let s = scroll n _N _A _B in
1781 let y1 = y1 +. ((s *. dy) /. sum) in
1782 gotoy_and_clear_text (truncate y1);
1783 state.ghyll <- gf (n+1) y1;
1785 match o with
1786 | None -> go n
1787 | Some y' -> set (_N/2, 0, 0) y' state.y
1789 gf 0 (float state.y)
1792 match conf.ghyllscroll with
1793 | None ->
1794 gotoy_and_clear_text y
1795 | Some nab ->
1796 if state.ghyll == noghyll
1797 then set nab y state.y
1798 else state.ghyll (Some y)
1801 let gotopage n top =
1802 let y, h = getpageyh n in
1803 let y = y + (truncate (top *. float h)) in
1804 gotoghyll y
1807 let gotopage1 n top =
1808 let y = getpagey n in
1809 let y = y + top in
1810 gotoghyll y
1813 let invalidate s f =
1814 state.layout <- [];
1815 state.pdims <- [];
1816 state.rects <- [];
1817 state.rects1 <- [];
1818 match state.geomcmds with
1819 | ps, [] when String.length ps = 0 ->
1820 f ();
1821 state.geomcmds <- s, [];
1823 | ps, [] ->
1824 state.geomcmds <- ps, [s, f];
1826 | ps, (s', _) :: rest when s' = s ->
1827 state.geomcmds <- ps, ((s, f) :: rest);
1829 | ps, cmds ->
1830 state.geomcmds <- ps, ((s, f) :: cmds);
1833 let opendoc path password =
1834 state.path <- path;
1835 state.password <- password;
1836 state.gen <- state.gen + 1;
1837 state.docinfo <- [];
1839 setaalevel conf.aalevel;
1840 Wsi.settitle ("llpp " ^ Filename.basename path);
1841 wcmd "open %s\000%s\000" path password;
1842 invalidate "reqlayout"
1843 (fun () ->
1844 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1847 let scalecolor c =
1848 let c = c *. conf.colorscale in
1849 (c, c, c);
1852 let scalecolor2 (r, g, b) =
1853 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1856 let docolumns = function
1857 | Csingle _ ->
1858 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1859 let rec loop pageno pdimno pdim y ph pdims =
1860 if pageno = state.pagecount
1861 then ()
1862 else
1863 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1864 match pdims with
1865 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1866 pdimno+1, pdim, rest
1867 | _ ->
1868 pdimno, pdim, pdims
1870 let x = max 0 (((conf.winw - state.scrollw - w) / 2) - xoff) in
1871 let y = y +
1872 (if conf.presentation
1873 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1874 else (if pageno = 0 then 0 else calcips h)
1877 a.(pageno) <- (pdimno, x, y, pdim);
1878 loop (pageno+1) pdimno pdim (y + h) h pdims
1880 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1881 conf.columns <- Csingle a;
1883 | Cmulti ((columns, coverA, coverB), _) ->
1884 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1885 let rec loop pageno pdimno pdim x y rowh pdims =
1886 let rec fixrow m = if m = pageno then () else
1887 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1888 if h < rowh
1889 then (
1890 let y = y + (rowh - h) / 2 in
1891 a.(m) <- (pdimno, x, y, pdim);
1893 fixrow (m+1)
1895 if pageno = state.pagecount
1896 then fixrow (((pageno - 1) / columns) * columns)
1897 else
1898 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1899 match pdims with
1900 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1901 pdimno+1, pdim, rest
1902 | _ ->
1903 pdimno, pdim, pdims
1905 let x, y, rowh' =
1906 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1907 then (
1908 (conf.winw - state.scrollw - w) / 2,
1909 y + rowh + conf.interpagespace, h
1911 else (
1912 if (pageno - coverA) mod columns = 0
1913 then (
1914 (if conf.multicenter then
1915 (conf.winw - state.scrollw - state.w) / 2 else 0),
1916 y + rowh + (if pageno = 0 then 0 else conf.interpagespace), h
1918 else x, y, max rowh h
1921 if pageno > 1 && (pageno - coverA) mod columns = 0
1922 then fixrow (pageno - columns);
1923 a.(pageno) <- (pdimno, x, y, pdim);
1924 let x = x + w + xoff*2 + conf.interpagespace in
1925 loop (pageno+1) pdimno pdim x y rowh' pdims
1927 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1928 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1930 | Csplit (c, _) ->
1931 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1932 let rec loop pageno pdimno pdim y pdims =
1933 if pageno = state.pagecount
1934 then ()
1935 else
1936 let pdimno, ((_, w, h, _) as pdim), pdims =
1937 match pdims with
1938 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1939 pdimno+1, pdim, rest
1940 | _ ->
1941 pdimno, pdim, pdims
1943 let cw = w / c in
1944 let rec loop1 n x y =
1945 if n = c then y else (
1946 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1947 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1950 let y = loop1 0 0 y in
1951 loop (pageno+1) pdimno pdim y pdims
1953 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1954 conf.columns <- Csplit (c, a);
1957 let represent () =
1958 docolumns conf.columns;
1959 state.maxy <- calcheight ();
1960 state.hscrollh <-
1961 if state.w <= conf.winw - state.scrollw
1962 then 0
1963 else state.scrollw
1965 match state.mode with
1966 | Birdseye (_, _, pageno, _, _) ->
1967 let y, h = getpageyh pageno in
1968 let top = (conf.winh - h) / 2 in
1969 gotoy (max 0 (y - top))
1970 | _ -> gotoanchor state.anchor
1973 let reshape w h =
1974 GlDraw.viewport 0 0 w h;
1975 let firsttime = state.geomcmds == firstgeomcmds in
1976 if not firsttime && nogeomcmds state.geomcmds
1977 then state.anchor <- getanchor ();
1979 conf.winw <- w;
1980 let w = truncate (float w *. conf.zoom) - state.scrollw in
1981 let w = max w 2 in
1982 conf.winh <- h;
1983 setfontsize fstate.fontsize;
1984 GlMat.mode `modelview;
1985 GlMat.load_identity ();
1987 GlMat.mode `projection;
1988 GlMat.load_identity ();
1989 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1990 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1991 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1993 let relx =
1994 if conf.zoom <= 1.0
1995 then 0.0
1996 else float state.x /. float state.w
1998 invalidate "geometry"
1999 (fun () ->
2000 state.w <- w;
2001 if not firsttime
2002 then state.x <- truncate (relx *. float w);
2003 let w =
2004 match conf.columns with
2005 | Csingle _ -> w
2006 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2007 | Csplit (c, _) -> w * c
2009 wcmd "geometry %d %d" w h);
2012 let enttext () =
2013 let len = String.length state.text in
2014 let drawstring s =
2015 let hscrollh =
2016 match state.mode with
2017 | Textentry _
2018 | View ->
2019 let h, _, _ = state.uioh#scrollpw in
2021 | _ -> 0
2023 let rect x w =
2024 GlDraw.rect
2025 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
2026 (x+.w, float (conf.winh - hscrollh))
2029 let w = float (conf.winw - state.scrollw - 1) in
2030 if state.progress >= 0.0 && state.progress < 1.0
2031 then (
2032 GlDraw.color (0.3, 0.3, 0.3);
2033 let w1 = w *. state.progress in
2034 rect 0.0 w1;
2035 GlDraw.color (0.0, 0.0, 0.0);
2036 rect w1 (w-.w1)
2038 else (
2039 GlDraw.color (0.0, 0.0, 0.0);
2040 rect 0.0 w;
2043 GlDraw.color (1.0, 1.0, 1.0);
2044 drawstring fstate.fontsize
2045 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
2047 let s =
2048 match state.mode with
2049 | Textentry ((prefix, text, _, _, _, _), _) ->
2050 let s =
2051 if len > 0
2052 then
2053 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2054 else
2055 Printf.sprintf "%s%s_" prefix text
2059 | _ -> state.text
2061 let s =
2062 if state.newerrmsgs
2063 then (
2064 if not (istextentry state.mode)
2065 then
2066 let s1 = "(press 'e' to review error messasges)" in
2067 if String.length s > 0 then s ^ " " ^ s1 else s1
2068 else s
2070 else s
2072 if String.length s > 0
2073 then drawstring s
2076 let gctiles () =
2077 let len = Queue.length state.tilelru in
2078 let layout = lazy (
2079 match state.throttle with
2080 | None ->
2081 if conf.preload
2082 then preloadlayout state.y
2083 else state.layout
2084 | Some (layout, _, _) ->
2085 layout
2086 ) in
2087 let rec loop qpos =
2088 if state.memused <= conf.memlimit
2089 then ()
2090 else (
2091 if qpos < len
2092 then
2093 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2094 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2095 let (_, pw, ph, _) = getpagedim n in
2097 gen = state.gen
2098 && colorspace = conf.colorspace
2099 && angle = conf.angle
2100 && pagew = pw
2101 && pageh = ph
2102 && (
2103 let x = col*conf.tilew
2104 and y = row*conf.tileh in
2105 tilevisible (Lazy.force_val layout) n x y
2107 then Queue.push lruitem state.tilelru
2108 else (
2109 wcmd "freetile %s" p;
2110 state.memused <- state.memused - s;
2111 state.uioh#infochanged Memused;
2112 Hashtbl.remove state.tilemap k;
2114 loop (qpos+1)
2117 loop 0
2120 let flushtiles () =
2121 Queue.iter (fun (k, p, s) ->
2122 wcmd "freetile %s" p;
2123 state.memused <- state.memused - s;
2124 state.uioh#infochanged Memused;
2125 Hashtbl.remove state.tilemap k;
2126 ) state.tilelru;
2127 Queue.clear state.tilelru;
2128 load state.layout;
2131 let logcurrently = function
2132 | Idle -> dolog "Idle"
2133 | Loading (l, gen) ->
2134 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2135 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2136 dolog
2137 "Tiling %d[%d,%d] page=%s cs=%s angle"
2138 l.pageno col row pageopaque
2139 (colorspace_to_string colorspace)
2141 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2142 angle gen conf.angle state.gen
2143 tilew tileh
2144 conf.tilew conf.tileh
2146 | Outlining _ ->
2147 dolog "outlining"
2150 let act cmds =
2151 (* dolog "%S" cmds; *)
2152 let op, args =
2153 let spacepos =
2154 try String.index cmds ' '
2155 with Not_found -> -1
2157 if spacepos = -1
2158 then cmds, ""
2159 else
2160 let l = String.length cmds in
2161 let op = String.sub cmds 0 spacepos in
2162 op, begin
2163 if l - spacepos < 2 then ""
2164 else String.sub cmds (spacepos+1) (l-spacepos-1)
2167 match op with
2168 | "clear" ->
2169 state.uioh#infochanged Pdim;
2170 state.pdims <- [];
2172 | "clearrects" ->
2173 state.rects <- state.rects1;
2174 G.postRedisplay "clearrects";
2176 | "continue" ->
2177 let n =
2178 try Scanf.sscanf args "%u" (fun n -> n)
2179 with exn ->
2180 dolog "error processing 'continue' %S: %s"
2181 cmds (Printexc.to_string exn);
2182 exit 1;
2184 state.pagecount <- n;
2185 begin match state.currently with
2186 | Outlining l ->
2187 state.currently <- Idle;
2188 state.outlines <- Array.of_list (List.rev l)
2189 | _ -> ()
2190 end;
2192 let cur, cmds = state.geomcmds in
2193 if String.length cur = 0
2194 then failwith "umpossible";
2196 begin match List.rev cmds with
2197 | [] ->
2198 state.geomcmds <- "", [];
2199 represent ();
2200 | (s, f) :: rest ->
2201 f ();
2202 state.geomcmds <- s, List.rev rest;
2203 end;
2204 if conf.maxwait = None
2205 then G.postRedisplay "continue";
2207 | "title" ->
2208 Wsi.settitle args
2210 | "msg" ->
2211 showtext ' ' args
2213 | "vmsg" ->
2214 if conf.verbose
2215 then showtext ' ' args
2217 | "progress" ->
2218 let progress, text =
2220 Scanf.sscanf args "%f %n"
2221 (fun f pos ->
2222 f, String.sub args pos (String.length args - pos))
2223 with exn ->
2224 dolog "error processing 'progress' %S: %s"
2225 cmds (Printexc.to_string exn);
2226 exit 1;
2228 state.text <- text;
2229 state.progress <- progress;
2230 G.postRedisplay "progress"
2232 | "firstmatch" ->
2233 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2235 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2236 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2237 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2238 with exn ->
2239 dolog "error processing 'firstmatch' %S: %s"
2240 cmds (Printexc.to_string exn);
2241 exit 1;
2243 let y = (getpagey pageno) + truncate y0 in
2244 addnav ();
2245 gotoy y;
2246 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2248 | "match" ->
2249 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2251 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2252 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2253 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2254 with exn ->
2255 dolog "error processing 'match' %S: %s"
2256 cmds (Printexc.to_string exn);
2257 exit 1;
2259 state.rects1 <-
2260 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2262 | "page" ->
2263 let pageopaque, t =
2265 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2266 with exn ->
2267 dolog "error processing 'page' %S: %s"
2268 cmds (Printexc.to_string exn);
2269 exit 1;
2271 begin match state.currently with
2272 | Loading (l, gen) ->
2273 vlog "page %d took %f sec" l.pageno t;
2274 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2275 begin match state.throttle with
2276 | None ->
2277 let preloadedpages =
2278 if conf.preload
2279 then preloadlayout state.y
2280 else state.layout
2282 let evict () =
2283 let module IntSet =
2284 Set.Make (struct type t = int let compare = (-) end) in
2285 let set =
2286 List.fold_left (fun s l -> IntSet.add l.pageno s)
2287 IntSet.empty preloadedpages
2289 let evictedpages =
2290 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2291 if not (IntSet.mem pageno set)
2292 then (
2293 wcmd "freepage %s" opaque;
2294 key :: accu
2296 else accu
2297 ) state.pagemap []
2299 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2301 evict ();
2302 state.currently <- Idle;
2303 if gen = state.gen
2304 then (
2305 tilepage l.pageno pageopaque state.layout;
2306 load state.layout;
2307 load preloadedpages;
2308 if pagevisible state.layout l.pageno
2309 && layoutready state.layout
2310 then G.postRedisplay "page";
2313 | Some (layout, _, _) ->
2314 state.currently <- Idle;
2315 tilepage l.pageno pageopaque layout;
2316 load state.layout
2317 end;
2319 | _ ->
2320 dolog "Inconsistent loading state";
2321 logcurrently state.currently;
2322 exit 1
2325 | "tile" ->
2326 let (x, y, opaque, size, t) =
2328 Scanf.sscanf args "%u %u %s %u %f"
2329 (fun x y p size t -> (x, y, p, size, t))
2330 with exn ->
2331 dolog "error processing 'tile' %S: %s"
2332 cmds (Printexc.to_string exn);
2333 exit 1;
2335 begin match state.currently with
2336 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2337 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2339 if tilew != conf.tilew || tileh != conf.tileh
2340 then (
2341 wcmd "freetile %s" opaque;
2342 state.currently <- Idle;
2343 load state.layout;
2345 else (
2346 puttileopaque l col row gen cs angle opaque size t;
2347 state.memused <- state.memused + size;
2348 state.uioh#infochanged Memused;
2349 gctiles ();
2350 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2351 opaque, size) state.tilelru;
2353 let layout =
2354 match state.throttle with
2355 | None -> state.layout
2356 | Some (layout, _, _) -> layout
2359 state.currently <- Idle;
2360 if gen = state.gen
2361 && conf.colorspace = cs
2362 && conf.angle = angle
2363 && tilevisible layout l.pageno x y
2364 then conttiling l.pageno pageopaque;
2366 begin match state.throttle with
2367 | None ->
2368 preload state.layout;
2369 if gen = state.gen
2370 && conf.colorspace = cs
2371 && conf.angle = angle
2372 && tilevisible state.layout l.pageno x y
2373 then G.postRedisplay "tile nothrottle";
2375 | Some (layout, y, _) ->
2376 let ready = layoutready layout in
2377 if ready
2378 then (
2379 state.y <- y;
2380 state.layout <- layout;
2381 state.throttle <- None;
2382 G.postRedisplay "throttle";
2384 else load layout;
2385 end;
2388 | _ ->
2389 dolog "Inconsistent tiling state";
2390 logcurrently state.currently;
2391 exit 1
2394 | "pdim" ->
2395 let pdim =
2397 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2398 with exn ->
2399 dolog "error processing 'pdim' %S: %s"
2400 cmds (Printexc.to_string exn);
2401 exit 1;
2403 state.uioh#infochanged Pdim;
2404 state.pdims <- pdim :: state.pdims
2406 | "o" ->
2407 let (l, n, t, h, pos) =
2409 Scanf.sscanf args "%u %u %d %u %n"
2410 (fun l n t h pos -> l, n, t, h, pos)
2411 with exn ->
2412 dolog "error processing 'o' %S: %s"
2413 cmds (Printexc.to_string exn);
2414 exit 1;
2416 let s = String.sub args pos (String.length args - pos) in
2417 let outline = (s, l, (n, float t /. float h, 0.0)) in
2418 begin match state.currently with
2419 | Outlining outlines ->
2420 state.currently <- Outlining (outline :: outlines)
2421 | Idle ->
2422 state.currently <- Outlining [outline]
2423 | currently ->
2424 dolog "invalid outlining state";
2425 logcurrently currently
2428 | "info" ->
2429 state.docinfo <- (1, args) :: state.docinfo
2431 | "infoend" ->
2432 state.uioh#infochanged Docinfo;
2433 state.docinfo <- List.rev state.docinfo
2435 | _ ->
2436 dolog "unknown cmd `%S'" cmds
2439 let onhist cb =
2440 let rc = cb.rc in
2441 let action = function
2442 | HCprev -> cbget cb ~-1
2443 | HCnext -> cbget cb 1
2444 | HCfirst -> cbget cb ~-(cb.rc)
2445 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2446 and cancel () = cb.rc <- rc
2447 in (action, cancel)
2450 let search pattern forward =
2451 if String.length pattern > 0
2452 then
2453 let pn, py =
2454 match state.layout with
2455 | [] -> 0, 0
2456 | l :: _ ->
2457 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2459 wcmd "search %d %d %d %d,%s\000"
2460 (btod conf.icase) pn py (btod forward) pattern;
2463 let intentry text key =
2464 let c =
2465 if key >= 32 && key < 127
2466 then Char.chr key
2467 else '\000'
2469 match c with
2470 | '0' .. '9' ->
2471 let text = addchar text c in
2472 TEcont text
2474 | _ ->
2475 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2476 TEcont text
2479 let linknentry text key =
2480 let c =
2481 if key >= 32 && key < 127
2482 then Char.chr key
2483 else '\000'
2485 match c with
2486 | 'a' .. 'z' ->
2487 let text = addchar text c in
2488 TEcont text
2490 | _ ->
2491 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2492 TEcont text
2495 let linkndone f s =
2496 if String.length s > 0
2497 then (
2498 let n =
2499 let l = String.length s in
2500 let rec loop pos n = if pos = l then n else
2501 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2502 loop (pos+1) (n*26 + m)
2503 in loop 0 0
2505 let rec loop n = function
2506 | [] -> ()
2507 | l :: rest ->
2508 match getopaque l.pageno with
2509 | None -> loop n rest
2510 | Some opaque ->
2511 let m = getlinkcount opaque in
2512 if n < m
2513 then (
2514 let under = getlink opaque n in
2515 f under
2517 else loop (n-m) rest
2519 loop n state.layout;
2523 let textentry text key =
2524 if key land 0xff00 = 0xff00
2525 then TEcont text
2526 else TEcont (text ^ Wsi.toutf8 key)
2529 let reqlayout angle proportional =
2530 match state.throttle with
2531 | None ->
2532 if nogeomcmds state.geomcmds
2533 then state.anchor <- getanchor ();
2534 conf.angle <- angle mod 360;
2535 if conf.angle != 0
2536 then (
2537 match state.mode with
2538 | LinkNav _ -> state.mode <- View
2539 | _ -> ()
2541 conf.proportional <- proportional;
2542 invalidate "reqlayout"
2543 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2544 | _ -> ()
2547 let settrim trimmargins trimfuzz =
2548 if nogeomcmds state.geomcmds
2549 then state.anchor <- getanchor ();
2550 conf.trimmargins <- trimmargins;
2551 conf.trimfuzz <- trimfuzz;
2552 let x0, y0, x1, y1 = trimfuzz in
2553 invalidate "settrim"
2554 (fun () ->
2555 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2556 Hashtbl.iter (fun _ opaque ->
2557 wcmd "freepage %s" opaque;
2558 ) state.pagemap;
2559 Hashtbl.clear state.pagemap;
2562 let setzoom zoom =
2563 match state.throttle with
2564 | None ->
2565 let zoom = max 0.01 zoom in
2566 if zoom <> conf.zoom
2567 then (
2568 state.prevzoom <- conf.zoom;
2569 conf.zoom <- zoom;
2570 reshape conf.winw conf.winh;
2571 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2574 | Some (layout, y, started) ->
2575 let time =
2576 match conf.maxwait with
2577 | None -> 0.0
2578 | Some t -> t
2580 let dt = now () -. started in
2581 if dt > time
2582 then (
2583 state.y <- y;
2584 load layout;
2588 let setcolumns mode columns coverA coverB =
2589 state.prevcolumns <- Some (conf.columns, conf.zoom);
2590 if columns < 0
2591 then (
2592 if isbirdseye mode
2593 then showtext '!' "split mode doesn't work in bird's eye"
2594 else (
2595 conf.columns <- Csplit (-columns, [||]);
2596 state.x <- 0;
2597 conf.zoom <- 1.0;
2600 else (
2601 if columns < 2
2602 then (
2603 conf.columns <- Csingle [||];
2604 state.x <- 0;
2605 setzoom 1.0;
2607 else (
2608 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2609 conf.zoom <- 1.0;
2612 reshape conf.winw conf.winh;
2615 let enterbirdseye () =
2616 let zoom = float conf.thumbw /. float conf.winw in
2617 let birdseyepageno =
2618 let cy = conf.winh / 2 in
2619 let fold = function
2620 | [] -> 0
2621 | l :: rest ->
2622 let rec fold best = function
2623 | [] -> best.pageno
2624 | l :: rest ->
2625 let d = cy - (l.pagedispy + l.pagevh/2)
2626 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2627 if abs d < abs dbest
2628 then fold l rest
2629 else best.pageno
2630 in fold l rest
2632 fold state.layout
2634 state.mode <- Birdseye (
2635 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2637 conf.zoom <- zoom;
2638 conf.presentation <- false;
2639 conf.interpagespace <- 10;
2640 conf.hlinks <- false;
2641 state.x <- 0;
2642 state.mstate <- Mnone;
2643 conf.maxwait <- None;
2644 conf.columns <- (
2645 match conf.beyecolumns with
2646 | Some c ->
2647 conf.zoom <- 1.0;
2648 Cmulti ((c, 0, 0), [||])
2649 | None -> Csingle [||]
2651 Wsi.setcursor Wsi.CURSOR_INHERIT;
2652 if conf.verbose
2653 then
2654 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2655 (100.0*.zoom)
2656 else
2657 state.text <- ""
2659 reshape conf.winw conf.winh;
2662 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2663 state.mode <- View;
2664 conf.zoom <- c.zoom;
2665 conf.presentation <- c.presentation;
2666 conf.interpagespace <- c.interpagespace;
2667 conf.maxwait <- c.maxwait;
2668 conf.hlinks <- c.hlinks;
2669 conf.beyecolumns <- (
2670 match conf.columns with
2671 | Cmulti ((c, _, _), _) -> Some c
2672 | Csingle _ -> None
2673 | Csplit _ -> failwith "leaving bird's eye split mode"
2675 conf.columns <- (
2676 match c.columns with
2677 | Cmulti (c, _) -> Cmulti (c, [||])
2678 | Csingle _ -> Csingle [||]
2679 | Csplit (c, _) -> Csplit (c, [||])
2681 state.x <- leftx;
2682 if conf.verbose
2683 then
2684 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2685 (100.0*.conf.zoom)
2687 reshape conf.winw conf.winh;
2688 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2691 let togglebirdseye () =
2692 match state.mode with
2693 | Birdseye vals -> leavebirdseye vals true
2694 | View -> enterbirdseye ()
2695 | _ -> ()
2698 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2699 let pageno = max 0 (pageno - incr) in
2700 let rec loop = function
2701 | [] -> gotopage1 pageno 0
2702 | l :: _ when l.pageno = pageno ->
2703 if l.pagedispy >= 0 && l.pagey = 0
2704 then G.postRedisplay "upbirdseye"
2705 else gotopage1 pageno 0
2706 | _ :: rest -> loop rest
2708 loop state.layout;
2709 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2712 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2713 let pageno = min (state.pagecount - 1) (pageno + incr) in
2714 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2715 let rec loop = function
2716 | [] ->
2717 let y, h = getpageyh pageno in
2718 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2719 gotoy (clamp dy)
2720 | l :: _ when l.pageno = pageno ->
2721 if l.pagevh != l.pageh
2722 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2723 else G.postRedisplay "downbirdseye"
2724 | _ :: rest -> loop rest
2726 loop state.layout
2729 let optentry mode _ key =
2730 let btos b = if b then "on" else "off" in
2731 if key >= 32 && key < 127
2732 then
2733 let c = Char.chr key in
2734 match c with
2735 | 's' ->
2736 let ondone s =
2737 try conf.scrollstep <- int_of_string s with exc ->
2738 state.text <- Printf.sprintf "bad integer `%s': %s"
2739 s (Printexc.to_string exc)
2741 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2743 | 'A' ->
2744 let ondone s =
2746 conf.autoscrollstep <- int_of_string s;
2747 if state.autoscroll <> None
2748 then state.autoscroll <- Some conf.autoscrollstep
2749 with exc ->
2750 state.text <- Printf.sprintf "bad integer `%s': %s"
2751 s (Printexc.to_string exc)
2753 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2755 | 'C' ->
2756 let ondone s =
2758 let n, a, b = multicolumns_of_string s in
2759 setcolumns mode n a b;
2760 with exc ->
2761 state.text <- Printf.sprintf "bad columns `%s': %s"
2762 s (Printexc.to_string exc)
2764 TEswitch ("columns: ", "", None, textentry, ondone, true)
2766 | 'Z' ->
2767 let ondone s =
2769 let zoom = float (int_of_string s) /. 100.0 in
2770 setzoom zoom
2771 with exc ->
2772 state.text <- Printf.sprintf "bad integer `%s': %s"
2773 s (Printexc.to_string exc)
2775 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2777 | 't' ->
2778 let ondone s =
2780 conf.thumbw <- bound (int_of_string s) 2 4096;
2781 state.text <-
2782 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2783 begin match mode with
2784 | Birdseye beye ->
2785 leavebirdseye beye false;
2786 enterbirdseye ();
2787 | _ -> ();
2789 with exc ->
2790 state.text <- Printf.sprintf "bad integer `%s': %s"
2791 s (Printexc.to_string exc)
2793 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2795 | 'R' ->
2796 let ondone s =
2797 match try
2798 Some (int_of_string s)
2799 with exc ->
2800 state.text <- Printf.sprintf "bad integer `%s': %s"
2801 s (Printexc.to_string exc);
2802 None
2803 with
2804 | Some angle -> reqlayout angle conf.proportional
2805 | None -> ()
2807 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2809 | 'i' ->
2810 conf.icase <- not conf.icase;
2811 TEdone ("case insensitive search " ^ (btos conf.icase))
2813 | 'p' ->
2814 conf.preload <- not conf.preload;
2815 gotoy state.y;
2816 TEdone ("preload " ^ (btos conf.preload))
2818 | 'v' ->
2819 conf.verbose <- not conf.verbose;
2820 TEdone ("verbose " ^ (btos conf.verbose))
2822 | 'd' ->
2823 conf.debug <- not conf.debug;
2824 TEdone ("debug " ^ (btos conf.debug))
2826 | 'h' ->
2827 conf.maxhfit <- not conf.maxhfit;
2828 state.maxy <- calcheight ();
2829 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2831 | 'c' ->
2832 conf.crophack <- not conf.crophack;
2833 TEdone ("crophack " ^ btos conf.crophack)
2835 | 'a' ->
2836 let s =
2837 match conf.maxwait with
2838 | None ->
2839 conf.maxwait <- Some infinity;
2840 "always wait for page to complete"
2841 | Some _ ->
2842 conf.maxwait <- None;
2843 "show placeholder if page is not ready"
2845 TEdone s
2847 | 'f' ->
2848 conf.underinfo <- not conf.underinfo;
2849 TEdone ("underinfo " ^ btos conf.underinfo)
2851 | 'P' ->
2852 conf.savebmarks <- not conf.savebmarks;
2853 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2855 | 'S' ->
2856 let ondone s =
2858 let pageno, py =
2859 match state.layout with
2860 | [] -> 0, 0
2861 | l :: _ ->
2862 l.pageno, l.pagey
2864 conf.interpagespace <- int_of_string s;
2865 docolumns conf.columns;
2866 state.maxy <- calcheight ();
2867 let y = getpagey pageno in
2868 gotoy (y + py)
2869 with exc ->
2870 state.text <- Printf.sprintf "bad integer `%s': %s"
2871 s (Printexc.to_string exc)
2873 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2875 | 'l' ->
2876 reqlayout conf.angle (not conf.proportional);
2877 TEdone ("proportional display " ^ btos conf.proportional)
2879 | 'T' ->
2880 settrim (not conf.trimmargins) conf.trimfuzz;
2881 TEdone ("trim margins " ^ btos conf.trimmargins)
2883 | 'I' ->
2884 conf.invert <- not conf.invert;
2885 TEdone ("invert colors " ^ btos conf.invert)
2887 | 'x' ->
2888 let ondone s =
2889 cbput state.hists.sel s;
2890 conf.selcmd <- s;
2892 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2893 textentry, ondone, true)
2895 | _ ->
2896 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2897 TEstop
2898 else
2899 TEcont state.text
2902 class type lvsource = object
2903 method getitemcount : int
2904 method getitem : int -> (string * int)
2905 method hasaction : int -> bool
2906 method exit :
2907 uioh:uioh ->
2908 cancel:bool ->
2909 active:int ->
2910 first:int ->
2911 pan:int ->
2912 qsearch:string ->
2913 uioh option
2914 method getactive : int
2915 method getfirst : int
2916 method getqsearch : string
2917 method setqsearch : string -> unit
2918 method getpan : int
2919 end;;
2921 class virtual lvsourcebase = object
2922 val mutable m_active = 0
2923 val mutable m_first = 0
2924 val mutable m_qsearch = ""
2925 val mutable m_pan = 0
2926 method getactive = m_active
2927 method getfirst = m_first
2928 method getqsearch = m_qsearch
2929 method getpan = m_pan
2930 method setqsearch s = m_qsearch <- s
2931 end;;
2933 let withoutlastutf8 s =
2934 let len = String.length s in
2935 if len = 0
2936 then s
2937 else
2938 let rec find pos =
2939 if pos = 0
2940 then pos
2941 else
2942 let b = Char.code s.[pos] in
2943 if b land 0b110000 = 0b11000000
2944 then find (pos-1)
2945 else pos-1
2947 let first =
2948 if Char.code s.[len-1] land 0x80 = 0
2949 then len-1
2950 else find (len-1)
2952 String.sub s 0 first;
2955 let textentrykeyboard
2956 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2957 let enttext te =
2958 state.mode <- Textentry (te, onleave);
2959 state.text <- "";
2960 enttext ();
2961 G.postRedisplay "textentrykeyboard enttext";
2963 let histaction cmd =
2964 match opthist with
2965 | None -> ()
2966 | Some (action, _) ->
2967 state.mode <- Textentry (
2968 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2970 G.postRedisplay "textentry histaction"
2972 match key with
2973 | 0xff08 -> (* backspace *)
2974 let s = withoutlastutf8 text in
2975 let len = String.length s in
2976 if cancelonempty && len = 0
2977 then (
2978 onleave Cancel;
2979 G.postRedisplay "textentrykeyboard after cancel";
2981 else (
2982 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2985 | 0xff0d ->
2986 ondone text;
2987 onleave Confirm;
2988 G.postRedisplay "textentrykeyboard after confirm"
2990 | 0xff52 -> histaction HCprev
2991 | 0xff54 -> histaction HCnext
2992 | 0xff50 -> histaction HCfirst
2993 | 0xff57 -> histaction HClast
2995 | 0xff1b -> (* escape*)
2996 if String.length text = 0
2997 then (
2998 begin match opthist with
2999 | None -> ()
3000 | Some (_, onhistcancel) -> onhistcancel ()
3001 end;
3002 onleave Cancel;
3003 state.text <- "";
3004 G.postRedisplay "textentrykeyboard after cancel2"
3006 else (
3007 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3010 | 0xff9f | 0xffff -> () (* delete *)
3012 | _ when key != 0 && key land 0xff00 != 0xff00 ->
3013 begin match onkey text key with
3014 | TEdone text ->
3015 ondone text;
3016 onleave Confirm;
3017 G.postRedisplay "textentrykeyboard after confirm2";
3019 | TEcont text ->
3020 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3022 | TEstop ->
3023 onleave Cancel;
3024 G.postRedisplay "textentrykeyboard after cancel3"
3026 | TEswitch te ->
3027 state.mode <- Textentry (te, onleave);
3028 G.postRedisplay "textentrykeyboard switch";
3029 end;
3031 | _ ->
3032 vlog "unhandled key %s" (Wsi.keyname key)
3035 let firstof first active =
3036 if first > active || abs (first - active) > fstate.maxrows - 1
3037 then max 0 (active - (fstate.maxrows/2))
3038 else first
3041 let calcfirst first active =
3042 if active > first
3043 then
3044 let rows = active - first in
3045 if rows > fstate.maxrows then active - fstate.maxrows else first
3046 else active
3049 let scrollph y maxy =
3050 let sh = (float (maxy + conf.winh) /. float conf.winh) in
3051 let sh = float conf.winh /. sh in
3052 let sh = max sh (float conf.scrollh) in
3054 let percent =
3055 if y = state.maxy
3056 then 1.0
3057 else float y /. float maxy
3059 let position = (float conf.winh -. sh) *. percent in
3061 let position =
3062 if position +. sh > float conf.winh
3063 then float conf.winh -. sh
3064 else position
3066 position, sh;
3069 let coe s = (s :> uioh);;
3071 class listview ~(source:lvsource) ~trusted ~modehash =
3072 object (self)
3073 val m_pan = source#getpan
3074 val m_first = source#getfirst
3075 val m_active = source#getactive
3076 val m_qsearch = source#getqsearch
3077 val m_prev_uioh = state.uioh
3079 method private elemunder y =
3080 let n = y / (fstate.fontsize+1) in
3081 if m_first + n < source#getitemcount
3082 then (
3083 if source#hasaction (m_first + n)
3084 then Some (m_first + n)
3085 else None
3087 else None
3089 method display =
3090 Gl.enable `blend;
3091 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3092 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3093 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
3094 GlDraw.color (1., 1., 1.);
3095 Gl.enable `texture_2d;
3096 let fs = fstate.fontsize in
3097 let nfs = fs + 1 in
3098 let ww = fstate.wwidth in
3099 let tabw = 30.0*.ww in
3100 let itemcount = source#getitemcount in
3101 let rec loop row =
3102 if (row - m_first) * nfs > conf.winh
3103 then ()
3104 else (
3105 if row >= 0 && row < itemcount
3106 then (
3107 let (s, level) = source#getitem row in
3108 let y = (row - m_first) * nfs in
3109 let x = 5.0 +. float (level + m_pan) *. ww in
3110 if row = m_active
3111 then (
3112 Gl.disable `texture_2d;
3113 GlDraw.polygon_mode `both `line;
3114 GlDraw.color (1., 1., 1.) ~alpha:0.9;
3115 GlDraw.rect (1., float (y + 1))
3116 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
3117 GlDraw.polygon_mode `both `fill;
3118 GlDraw.color (1., 1., 1.);
3119 Gl.enable `texture_2d;
3122 let drawtabularstring s =
3123 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3124 if trusted
3125 then
3126 let tabpos = try String.index s '\t' with Not_found -> -1 in
3127 if tabpos > 0
3128 then
3129 let len = String.length s - tabpos - 1 in
3130 let s1 = String.sub s 0 tabpos
3131 and s2 = String.sub s (tabpos + 1) len in
3132 let nx = drawstr x s1 in
3133 let sw = nx -. x in
3134 let x = x +. (max tabw sw) in
3135 drawstr x s2
3136 else
3137 drawstr x s
3138 else
3139 drawstr x s
3141 let _ = drawtabularstring s in
3142 loop (row+1)
3146 loop m_first;
3147 Gl.disable `blend;
3148 Gl.disable `texture_2d;
3150 method updownlevel incr =
3151 let len = source#getitemcount in
3152 let curlevel =
3153 if m_active >= 0 && m_active < len
3154 then snd (source#getitem m_active)
3155 else -1
3157 let rec flow i =
3158 if i = len then i-1 else if i = -1 then 0 else
3159 let _, l = source#getitem i in
3160 if l != curlevel then i else flow (i+incr)
3162 let active = flow m_active in
3163 let first = calcfirst m_first active in
3164 G.postRedisplay "outline updownlevel";
3165 {< m_active = active; m_first = first >}
3167 method private key1 key mask =
3168 let set1 active first qsearch =
3169 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3171 let search active pattern incr =
3172 let dosearch re =
3173 let rec loop n =
3174 if n >= 0 && n < source#getitemcount
3175 then (
3176 let s, _ = source#getitem n in
3178 (try ignore (Str.search_forward re s 0); true
3179 with Not_found -> false)
3180 then Some n
3181 else loop (n + incr)
3183 else None
3185 loop active
3188 let re = Str.regexp_case_fold pattern in
3189 dosearch re
3190 with Failure s ->
3191 state.text <- s;
3192 None
3194 let itemcount = source#getitemcount in
3195 let find start incr =
3196 let rec find i =
3197 if i = -1 || i = itemcount
3198 then -1
3199 else (
3200 if source#hasaction i
3201 then i
3202 else find (i + incr)
3205 find start
3207 let set active first =
3208 let first = bound first 0 (itemcount - fstate.maxrows) in
3209 state.text <- "";
3210 coe {< m_active = active; m_first = first >}
3212 let navigate incr =
3213 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3214 let active, first =
3215 let incr1 = if incr > 0 then 1 else -1 in
3216 if isvisible m_first m_active
3217 then
3218 let next =
3219 let next = m_active + incr in
3220 let next =
3221 if next < 0 || next >= itemcount
3222 then -1
3223 else find next incr1
3225 if next = -1 || abs (m_active - next) > fstate.maxrows
3226 then -1
3227 else next
3229 if next = -1
3230 then
3231 let first = m_first + incr in
3232 let first = bound first 0 (itemcount - 1) in
3233 let next =
3234 let next = m_active + incr in
3235 let next = bound next 0 (itemcount - 1) in
3236 find next ~-incr1
3238 let active = if next = -1 then m_active else next in
3239 active, first
3240 else
3241 let first = min next m_first in
3242 let first =
3243 if abs (next - first) > fstate.maxrows
3244 then first + incr
3245 else first
3247 next, first
3248 else
3249 let first = m_first + incr in
3250 let first = bound first 0 (itemcount - 1) in
3251 let active =
3252 let next = m_active + incr in
3253 let next = bound next 0 (itemcount - 1) in
3254 let next = find next incr1 in
3255 let active =
3256 if next = -1 || abs (m_active - first) > fstate.maxrows
3257 then (
3258 let active = if m_active = -1 then next else m_active in
3259 active
3261 else next
3263 if isvisible first active
3264 then active
3265 else -1
3267 active, first
3269 G.postRedisplay "listview navigate";
3270 set active first;
3272 match key with
3273 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3274 let incr = if key = 0x72 then -1 else 1 in
3275 let active, first =
3276 match search (m_active + incr) m_qsearch incr with
3277 | None ->
3278 state.text <- m_qsearch ^ " [not found]";
3279 m_active, m_first
3280 | Some active ->
3281 state.text <- m_qsearch;
3282 active, firstof m_first active
3284 G.postRedisplay "listview ctrl-r/s";
3285 set1 active first m_qsearch;
3287 | 0xff08 -> (* backspace *)
3288 if String.length m_qsearch = 0
3289 then coe self
3290 else (
3291 let qsearch = withoutlastutf8 m_qsearch in
3292 let len = String.length qsearch in
3293 if len = 0
3294 then (
3295 state.text <- "";
3296 G.postRedisplay "listview empty qsearch";
3297 set1 m_active m_first "";
3299 else
3300 let active, first =
3301 match search m_active qsearch ~-1 with
3302 | None ->
3303 state.text <- qsearch ^ " [not found]";
3304 m_active, m_first
3305 | Some active ->
3306 state.text <- qsearch;
3307 active, firstof m_first active
3309 G.postRedisplay "listview backspace qsearch";
3310 set1 active first qsearch
3313 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3314 let pattern = m_qsearch ^ Wsi.toutf8 key in
3315 let active, first =
3316 match search m_active pattern 1 with
3317 | None ->
3318 state.text <- pattern ^ " [not found]";
3319 m_active, m_first
3320 | Some active ->
3321 state.text <- pattern;
3322 active, firstof m_first active
3324 G.postRedisplay "listview qsearch add";
3325 set1 active first pattern;
3327 | 0xff1b -> (* escape *)
3328 state.text <- "";
3329 if String.length m_qsearch = 0
3330 then (
3331 G.postRedisplay "list view escape";
3332 begin
3333 match
3334 source#exit (coe self) true m_active m_first m_pan m_qsearch
3335 with
3336 | None -> m_prev_uioh
3337 | Some uioh -> uioh
3340 else (
3341 G.postRedisplay "list view kill qsearch";
3342 source#setqsearch "";
3343 coe {< m_qsearch = "" >}
3346 | 0xff0d -> (* return *)
3347 state.text <- "";
3348 let self = {< m_qsearch = "" >} in
3349 source#setqsearch "";
3350 let opt =
3351 G.postRedisplay "listview enter";
3352 if m_active >= 0 && m_active < source#getitemcount
3353 then (
3354 source#exit (coe self) false m_active m_first m_pan "";
3356 else (
3357 source#exit (coe self) true m_active m_first m_pan "";
3360 begin match opt with
3361 | None -> m_prev_uioh
3362 | Some uioh -> uioh
3365 | 0xff9f | 0xffff -> (* delete *)
3366 coe self
3368 | 0xff52 -> navigate ~-1 (* up *)
3369 | 0xff54 -> navigate 1 (* down *)
3370 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3371 | 0xff56 -> navigate fstate.maxrows (* next *)
3373 | 0xff53 -> (* right *)
3374 state.text <- "";
3375 G.postRedisplay "listview right";
3376 coe {< m_pan = m_pan - 1 >}
3378 | 0xff51 -> (* left *)
3379 state.text <- "";
3380 G.postRedisplay "listview left";
3381 coe {< m_pan = m_pan + 1 >}
3383 | 0xff50 -> (* home *)
3384 let active = find 0 1 in
3385 G.postRedisplay "listview home";
3386 set active 0;
3388 | 0xff57 -> (* end *)
3389 let first = max 0 (itemcount - fstate.maxrows) in
3390 let active = find (itemcount - 1) ~-1 in
3391 G.postRedisplay "listview end";
3392 set active first;
3394 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3395 coe self
3397 | _ ->
3398 dolog "listview unknown key %#x" key; coe self
3400 method key key mask =
3401 match state.mode with
3402 | Textentry te -> textentrykeyboard key mask te; coe self
3403 | _ -> self#key1 key mask
3405 method button button down x y _ =
3406 let opt =
3407 match button with
3408 | 1 when x > conf.winw - conf.scrollbw ->
3409 G.postRedisplay "listview scroll";
3410 if down
3411 then
3412 let _, position, sh = self#scrollph in
3413 if y > truncate position && y < truncate (position +. sh)
3414 then (
3415 state.mstate <- Mscrolly;
3416 Some (coe self)
3418 else
3419 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3420 let first = truncate (s *. float source#getitemcount) in
3421 let first = min source#getitemcount first in
3422 Some (coe {< m_first = first; m_active = first >})
3423 else (
3424 state.mstate <- Mnone;
3425 Some (coe self);
3427 | 1 when not down ->
3428 begin match self#elemunder y with
3429 | Some n ->
3430 G.postRedisplay "listview click";
3431 source#exit
3432 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3433 | _ ->
3434 Some (coe self)
3436 | n when (n == 4 || n == 5) && not down ->
3437 let len = source#getitemcount in
3438 let first =
3439 if n = 5 && m_first + fstate.maxrows >= len
3440 then
3441 m_first
3442 else
3443 let first = m_first + (if n == 4 then -1 else 1) in
3444 bound first 0 (len - 1)
3446 G.postRedisplay "listview wheel";
3447 Some (coe {< m_first = first >})
3448 | n when (n = 6 || n = 7) && not down ->
3449 let inc = m_first + (if n = 7 then -1 else 1) in
3450 G.postRedisplay "listview hwheel";
3451 Some (coe {< m_pan = m_pan + inc >})
3452 | _ ->
3453 Some (coe self)
3455 match opt with
3456 | None -> m_prev_uioh
3457 | Some uioh -> uioh
3459 method motion _ y =
3460 match state.mstate with
3461 | Mscrolly ->
3462 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3463 let first = truncate (s *. float source#getitemcount) in
3464 let first = min source#getitemcount first in
3465 G.postRedisplay "listview motion";
3466 coe {< m_first = first; m_active = first >}
3467 | _ -> coe self
3469 method pmotion x y =
3470 if x < conf.winw - conf.scrollbw
3471 then
3472 let n =
3473 match self#elemunder y with
3474 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3475 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3477 let o =
3478 if n != m_active
3479 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3480 else self
3482 coe o
3483 else (
3484 Wsi.setcursor Wsi.CURSOR_INHERIT;
3485 coe self
3488 method infochanged _ = ()
3490 method scrollpw = (0, 0.0, 0.0)
3491 method scrollph =
3492 let nfs = fstate.fontsize + 1 in
3493 let y = m_first * nfs in
3494 let itemcount = source#getitemcount in
3495 let maxi = max 0 (itemcount - fstate.maxrows) in
3496 let maxy = maxi * nfs in
3497 let p, h = scrollph y maxy in
3498 conf.scrollbw, p, h
3500 method modehash = modehash
3501 end;;
3503 class outlinelistview ~source =
3504 object (self)
3505 inherit listview
3506 ~source:(source :> lvsource)
3507 ~trusted:false
3508 ~modehash:(findkeyhash conf "outline")
3509 as super
3511 method key key mask =
3512 let calcfirst first active =
3513 if active > first
3514 then
3515 let rows = active - first in
3516 let maxrows =
3517 if String.length state.text = 0
3518 then fstate.maxrows
3519 else fstate.maxrows - 2
3521 if rows > maxrows then active - maxrows else first
3522 else active
3524 let navigate incr =
3525 let active = m_active + incr in
3526 let active = bound active 0 (source#getitemcount - 1) in
3527 let first = calcfirst m_first active in
3528 G.postRedisplay "outline navigate";
3529 coe {< m_active = active; m_first = first >}
3531 let ctrl = Wsi.withctrl mask in
3532 match key with
3533 | 110 when ctrl -> (* ctrl-n *)
3534 source#narrow m_qsearch;
3535 G.postRedisplay "outline ctrl-n";
3536 coe {< m_first = 0; m_active = 0 >}
3538 | 117 when ctrl -> (* ctrl-u *)
3539 source#denarrow;
3540 G.postRedisplay "outline ctrl-u";
3541 state.text <- "";
3542 coe {< m_first = 0; m_active = 0 >}
3544 | 108 when ctrl -> (* ctrl-l *)
3545 let first = m_active - (fstate.maxrows / 2) in
3546 G.postRedisplay "outline ctrl-l";
3547 coe {< m_first = first >}
3549 | 0xff9f | 0xffff -> (* delete *)
3550 source#remove m_active;
3551 G.postRedisplay "outline delete";
3552 let active = max 0 (m_active-1) in
3553 coe {< m_first = firstof m_first active;
3554 m_active = active >}
3556 | 0xff52 -> navigate ~-1 (* up *)
3557 | 0xff54 -> navigate 1 (* down *)
3558 | 0xff55 -> (* prior *)
3559 navigate ~-(fstate.maxrows)
3560 | 0xff56 -> (* next *)
3561 navigate fstate.maxrows
3563 | 0xff53 -> (* [ctrl-]right *)
3564 let o =
3565 if ctrl
3566 then (
3567 G.postRedisplay "outline ctrl right";
3568 {< m_pan = m_pan + 1 >}
3570 else self#updownlevel 1
3572 coe o
3574 | 0xff51 -> (* [ctrl-]left *)
3575 let o =
3576 if ctrl
3577 then (
3578 G.postRedisplay "outline ctrl left";
3579 {< m_pan = m_pan - 1 >}
3581 else self#updownlevel ~-1
3583 coe o
3585 | 0xff50 -> (* home *)
3586 G.postRedisplay "outline home";
3587 coe {< m_first = 0; m_active = 0 >}
3589 | 0xff57 -> (* end *)
3590 let active = source#getitemcount - 1 in
3591 let first = max 0 (active - fstate.maxrows) in
3592 G.postRedisplay "outline end";
3593 coe {< m_active = active; m_first = first >}
3595 | _ -> super#key key mask
3598 let outlinesource usebookmarks =
3599 let empty = [||] in
3600 (object
3601 inherit lvsourcebase
3602 val mutable m_items = empty
3603 val mutable m_orig_items = empty
3604 val mutable m_prev_items = empty
3605 val mutable m_narrow_pattern = ""
3606 val mutable m_hadremovals = false
3608 method getitemcount =
3609 Array.length m_items + (if m_hadremovals then 1 else 0)
3611 method getitem n =
3612 if n == Array.length m_items && m_hadremovals
3613 then
3614 ("[Confirm removal]", 0)
3615 else
3616 let s, n, _ = m_items.(n) in
3617 (s, n)
3619 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3620 ignore (uioh, first, qsearch);
3621 let confrimremoval = m_hadremovals && active = Array.length m_items in
3622 let items =
3623 if String.length m_narrow_pattern = 0
3624 then m_orig_items
3625 else m_items
3627 if not cancel
3628 then (
3629 if not confrimremoval
3630 then(
3631 let _, _, anchor = m_items.(active) in
3632 gotoanchor anchor;
3633 m_items <- items;
3635 else (
3636 state.bookmarks <- Array.to_list m_items;
3637 m_orig_items <- m_items;
3640 else m_items <- items;
3641 m_pan <- pan;
3642 None
3644 method hasaction _ = true
3646 method greetmsg =
3647 if Array.length m_items != Array.length m_orig_items
3648 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3649 else ""
3651 method narrow pattern =
3652 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3653 match reopt with
3654 | None -> ()
3655 | Some re ->
3656 let rec loop accu n =
3657 if n = -1
3658 then (
3659 m_narrow_pattern <- pattern;
3660 m_items <- Array.of_list accu
3662 else
3663 let (s, _, _) as o = m_items.(n) in
3664 let accu =
3665 if (try ignore (Str.search_forward re s 0); true
3666 with Not_found -> false)
3667 then o :: accu
3668 else accu
3670 loop accu (n-1)
3672 loop [] (Array.length m_items - 1)
3674 method denarrow =
3675 m_orig_items <- (
3676 if usebookmarks
3677 then Array.of_list state.bookmarks
3678 else state.outlines
3680 m_items <- m_orig_items
3682 method remove m =
3683 if usebookmarks
3684 then
3685 if m >= 0 && m < Array.length m_items
3686 then (
3687 m_hadremovals <- true;
3688 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3689 let n = if n >= m then n+1 else n in
3690 m_items.(n)
3694 method reset anchor items =
3695 m_hadremovals <- false;
3696 if m_orig_items == empty || m_prev_items != items
3697 then (
3698 m_orig_items <- items;
3699 if String.length m_narrow_pattern = 0
3700 then m_items <- items;
3702 m_prev_items <- items;
3703 let rely = getanchory anchor in
3704 let active =
3705 let rec loop n best bestd =
3706 if n = Array.length m_items
3707 then best
3708 else
3709 let (_, _, anchor) = m_items.(n) in
3710 let orely = getanchory anchor in
3711 let d = abs (orely - rely) in
3712 if d < bestd
3713 then loop (n+1) n d
3714 else loop (n+1) best bestd
3716 loop 0 ~-1 max_int
3718 m_active <- active;
3719 m_first <- firstof m_first active
3720 end)
3723 let enterselector usebookmarks =
3724 let source = outlinesource usebookmarks in
3725 fun errmsg ->
3726 let outlines =
3727 if usebookmarks
3728 then Array.of_list state.bookmarks
3729 else state.outlines
3731 if Array.length outlines = 0
3732 then (
3733 showtext ' ' errmsg;
3735 else (
3736 state.text <- source#greetmsg;
3737 Wsi.setcursor Wsi.CURSOR_INHERIT;
3738 let anchor = getanchor () in
3739 source#reset anchor outlines;
3740 state.uioh <- coe (new outlinelistview ~source);
3741 G.postRedisplay "enter selector";
3745 let enteroutlinemode =
3746 let f = enterselector false in
3747 fun ()-> f "Document has no outline";
3750 let enterbookmarkmode =
3751 let f = enterselector true in
3752 fun () -> f "Document has no bookmarks (yet)";
3755 let color_of_string s =
3756 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3757 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3761 let color_to_string (r, g, b) =
3762 let r = truncate (r *. 256.0)
3763 and g = truncate (g *. 256.0)
3764 and b = truncate (b *. 256.0) in
3765 Printf.sprintf "%d/%d/%d" r g b
3768 let irect_of_string s =
3769 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3772 let irect_to_string (x0,y0,x1,y1) =
3773 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3776 let makecheckers () =
3777 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3778 following to say:
3779 converted by Issac Trotts. July 25, 2002 *)
3780 let image_height = 64
3781 and image_width = 64 in
3783 let make_image () =
3784 let image =
3785 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3787 for i = 0 to image_width - 1 do
3788 for j = 0 to image_height - 1 do
3789 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3790 (if (i land 8 ) lxor (j land 8) = 0
3791 then [|255;255;255|] else [|200;200;200|])
3792 done
3793 done;
3794 image
3796 let image = make_image () in
3797 let id = GlTex.gen_texture () in
3798 GlTex.bind_texture `texture_2d id;
3799 GlPix.store (`unpack_alignment 1);
3800 GlTex.image2d image;
3801 List.iter (GlTex.parameter ~target:`texture_2d)
3802 [ `wrap_s `repeat;
3803 `wrap_t `repeat;
3804 `mag_filter `nearest;
3805 `min_filter `nearest ];
3809 let setcheckers enabled =
3810 match state.texid with
3811 | None ->
3812 if enabled then state.texid <- Some (makecheckers ())
3814 | Some texid ->
3815 if not enabled
3816 then (
3817 GlTex.delete_texture texid;
3818 state.texid <- None;
3822 let int_of_string_with_suffix s =
3823 let l = String.length s in
3824 let s1, shift =
3825 if l > 1
3826 then
3827 let suffix = Char.lowercase s.[l-1] in
3828 match suffix with
3829 | 'k' -> String.sub s 0 (l-1), 10
3830 | 'm' -> String.sub s 0 (l-1), 20
3831 | 'g' -> String.sub s 0 (l-1), 30
3832 | _ -> s, 0
3833 else s, 0
3835 let n = int_of_string s1 in
3836 let m = n lsl shift in
3837 if m < 0 || m < n
3838 then raise (Failure "value too large")
3839 else m
3842 let string_with_suffix_of_int n =
3843 if n = 0
3844 then "0"
3845 else
3846 let n, s =
3847 if n land ((1 lsl 20) - 1) = 0
3848 then n lsr 20, "M"
3849 else (
3850 if n land ((1 lsl 10) - 1) = 0
3851 then n lsr 10, "K"
3852 else n, ""
3855 let rec loop s n =
3856 let h = n mod 1000 in
3857 let n = n / 1000 in
3858 if n = 0
3859 then string_of_int h ^ s
3860 else (
3861 let s = Printf.sprintf "_%03d%s" h s in
3862 loop s n
3865 loop "" n ^ s;
3868 let defghyllscroll = (40, 8, 32);;
3869 let ghyllscroll_of_string s =
3870 let (n, a, b) as nab =
3871 if s = "default"
3872 then defghyllscroll
3873 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3875 if n <= a || n <= b || a >= b
3876 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3877 nab;
3880 let ghyllscroll_to_string ((n, a, b) as nab) =
3881 if nab = defghyllscroll
3882 then "default"
3883 else Printf.sprintf "%d,%d,%d" n a b;
3886 let describe_location () =
3887 let f (fn, _) l =
3888 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3890 let fn, ln = List.fold_left f (-1, -1) state.layout in
3891 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3892 let percent =
3893 if maxy <= 0
3894 then 100.
3895 else (100. *. (float state.y /. float maxy))
3897 if fn = ln
3898 then
3899 Printf.sprintf "page %d of %d [%.2f%%]"
3900 (fn+1) state.pagecount percent
3901 else
3902 Printf.sprintf
3903 "pages %d-%d of %d [%.2f%%]"
3904 (fn+1) (ln+1) state.pagecount percent
3907 let setpresentationmode v =
3908 let (n, _, _) = getanchor () in
3909 let _, h = getpageyh n in
3910 state.anchor <- (n, 0.0, float (calcips h));
3911 conf.presentation <- v;
3912 if conf.presentation
3913 then (
3914 if not conf.scrollbarinpm
3915 then state.scrollw <- 0;
3917 else state.scrollw <- conf.scrollbw;
3918 represent ();
3921 let enterinfomode =
3922 let btos b = if b then "\xe2\x88\x9a" else "" in
3923 let showextended = ref false in
3924 let leave mode = function
3925 | Confirm -> state.mode <- mode
3926 | Cancel -> state.mode <- mode in
3927 let src =
3928 (object
3929 val mutable m_first_time = true
3930 val mutable m_l = []
3931 val mutable m_a = [||]
3932 val mutable m_prev_uioh = nouioh
3933 val mutable m_prev_mode = View
3935 inherit lvsourcebase
3937 method reset prev_mode prev_uioh =
3938 m_a <- Array.of_list (List.rev m_l);
3939 m_l <- [];
3940 m_prev_mode <- prev_mode;
3941 m_prev_uioh <- prev_uioh;
3942 if m_first_time
3943 then (
3944 let rec loop n =
3945 if n >= Array.length m_a
3946 then ()
3947 else
3948 match m_a.(n) with
3949 | _, _, _, Action _ -> m_active <- n
3950 | _ -> loop (n+1)
3952 loop 0;
3953 m_first_time <- false;
3956 method int name get set =
3957 m_l <-
3958 (name, `int get, 1, Action (
3959 fun u ->
3960 let ondone s =
3961 try set (int_of_string s)
3962 with exn ->
3963 state.text <- Printf.sprintf "bad integer `%s': %s"
3964 s (Printexc.to_string exn)
3966 state.text <- "";
3967 let te = name ^ ": ", "", None, intentry, ondone, true in
3968 state.mode <- Textentry (te, leave m_prev_mode);
3970 )) :: m_l
3972 method int_with_suffix name get set =
3973 m_l <-
3974 (name, `intws get, 1, Action (
3975 fun u ->
3976 let ondone s =
3977 try set (int_of_string_with_suffix s)
3978 with exn ->
3979 state.text <- Printf.sprintf "bad integer `%s': %s"
3980 s (Printexc.to_string exn)
3982 state.text <- "";
3983 let te =
3984 name ^ ": ", "", None, intentry_with_suffix, ondone, true
3986 state.mode <- Textentry (te, leave m_prev_mode);
3988 )) :: m_l
3990 method bool ?(offset=1) ?(btos=btos) name get set =
3991 m_l <-
3992 (name, `bool (btos, get), offset, Action (
3993 fun u ->
3994 let v = get () in
3995 set (not v);
3997 )) :: m_l
3999 method color name get set =
4000 m_l <-
4001 (name, `color get, 1, Action (
4002 fun u ->
4003 let invalid = (nan, nan, nan) in
4004 let ondone s =
4005 let c =
4006 try color_of_string s
4007 with exn ->
4008 state.text <- Printf.sprintf "bad color `%s': %s"
4009 s (Printexc.to_string exn);
4010 invalid
4012 if c <> invalid
4013 then set c;
4015 let te = name ^ ": ", "", None, textentry, ondone, true in
4016 state.text <- color_to_string (get ());
4017 state.mode <- Textentry (te, leave m_prev_mode);
4019 )) :: m_l
4021 method string name get set =
4022 m_l <-
4023 (name, `string get, 1, Action (
4024 fun u ->
4025 let ondone s = set s in
4026 let te = name ^ ": ", "", None, textentry, ondone, true in
4027 state.mode <- Textentry (te, leave m_prev_mode);
4029 )) :: m_l
4031 method colorspace name get set =
4032 m_l <-
4033 (name, `string get, 1, Action (
4034 fun _ ->
4035 let source =
4036 let vals = [| "rgb"; "bgr"; "gray" |] in
4037 (object
4038 inherit lvsourcebase
4040 initializer
4041 m_active <- int_of_colorspace conf.colorspace;
4042 m_first <- 0;
4044 method getitemcount = Array.length vals
4045 method getitem n = (vals.(n), 0)
4046 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4047 ignore (uioh, first, pan, qsearch);
4048 if not cancel then set active;
4049 None
4050 method hasaction _ = true
4051 end)
4053 state.text <- "";
4054 let modehash = findkeyhash conf "info" in
4055 coe (new listview ~source ~trusted:true ~modehash)
4056 )) :: m_l
4058 method caption s offset =
4059 m_l <- (s, `empty, offset, Noaction) :: m_l
4061 method caption2 s f offset =
4062 m_l <- (s, `string f, offset, Noaction) :: m_l
4064 method getitemcount = Array.length m_a
4066 method getitem n =
4067 let tostr = function
4068 | `int f -> string_of_int (f ())
4069 | `intws f -> string_with_suffix_of_int (f ())
4070 | `string f -> f ()
4071 | `color f -> color_to_string (f ())
4072 | `bool (btos, f) -> btos (f ())
4073 | `empty -> ""
4075 let name, t, offset, _ = m_a.(n) in
4076 ((let s = tostr t in
4077 if String.length s > 0
4078 then Printf.sprintf "%s\t%s" name s
4079 else name),
4080 offset)
4082 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4083 let uiohopt =
4084 if not cancel
4085 then (
4086 m_qsearch <- qsearch;
4087 let uioh =
4088 match m_a.(active) with
4089 | _, _, _, Action f -> f uioh
4090 | _ -> uioh
4092 Some uioh
4094 else None
4096 m_active <- active;
4097 m_first <- first;
4098 m_pan <- pan;
4099 uiohopt
4101 method hasaction n =
4102 match m_a.(n) with
4103 | _, _, _, Action _ -> true
4104 | _ -> false
4105 end)
4107 let rec fillsrc prevmode prevuioh =
4108 let sep () = src#caption "" 0 in
4109 let colorp name get set =
4110 src#string name
4111 (fun () -> color_to_string (get ()))
4112 (fun v ->
4114 let c = color_of_string v in
4115 set c
4116 with exn ->
4117 state.text <- Printf.sprintf "bad color `%s': %s"
4118 v (Printexc.to_string exn);
4121 let oldmode = state.mode in
4122 let birdseye = isbirdseye state.mode in
4124 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4126 src#bool "presentation mode"
4127 (fun () -> conf.presentation)
4128 (fun v -> setpresentationmode v);
4130 src#bool "ignore case in searches"
4131 (fun () -> conf.icase)
4132 (fun v -> conf.icase <- v);
4134 src#bool "preload"
4135 (fun () -> conf.preload)
4136 (fun v -> conf.preload <- v);
4138 src#bool "highlight links"
4139 (fun () -> conf.hlinks)
4140 (fun v -> conf.hlinks <- v);
4142 src#bool "under info"
4143 (fun () -> conf.underinfo)
4144 (fun v -> conf.underinfo <- v);
4146 src#bool "persistent bookmarks"
4147 (fun () -> conf.savebmarks)
4148 (fun v -> conf.savebmarks <- v);
4150 src#bool "proportional display"
4151 (fun () -> conf.proportional)
4152 (fun v -> reqlayout conf.angle v);
4154 src#bool "trim margins"
4155 (fun () -> conf.trimmargins)
4156 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4158 src#bool "persistent location"
4159 (fun () -> conf.jumpback)
4160 (fun v -> conf.jumpback <- v);
4162 sep ();
4163 src#int "inter-page space"
4164 (fun () -> conf.interpagespace)
4165 (fun n ->
4166 conf.interpagespace <- n;
4167 docolumns conf.columns;
4168 let pageno, py =
4169 match state.layout with
4170 | [] -> 0, 0
4171 | l :: _ ->
4172 l.pageno, l.pagey
4174 state.maxy <- calcheight ();
4175 let y = getpagey pageno in
4176 gotoy (y + py)
4179 src#int "page bias"
4180 (fun () -> conf.pagebias)
4181 (fun v -> conf.pagebias <- v);
4183 src#int "scroll step"
4184 (fun () -> conf.scrollstep)
4185 (fun n -> conf.scrollstep <- n);
4187 src#int "horizontal scroll step"
4188 (fun () -> conf.hscrollstep)
4189 (fun v -> conf.hscrollstep <- v);
4191 src#int "auto scroll step"
4192 (fun () ->
4193 match state.autoscroll with
4194 | Some step -> step
4195 | _ -> conf.autoscrollstep)
4196 (fun n ->
4197 if state.autoscroll <> None
4198 then state.autoscroll <- Some n;
4199 conf.autoscrollstep <- n);
4201 src#int "zoom"
4202 (fun () -> truncate (conf.zoom *. 100.))
4203 (fun v -> setzoom ((float v) /. 100.));
4205 src#int "rotation"
4206 (fun () -> conf.angle)
4207 (fun v -> reqlayout v conf.proportional);
4209 src#int "scroll bar width"
4210 (fun () -> state.scrollw)
4211 (fun v ->
4212 state.scrollw <- v;
4213 conf.scrollbw <- v;
4214 reshape conf.winw conf.winh;
4217 src#int "scroll handle height"
4218 (fun () -> conf.scrollh)
4219 (fun v -> conf.scrollh <- v;);
4221 src#int "thumbnail width"
4222 (fun () -> conf.thumbw)
4223 (fun v ->
4224 conf.thumbw <- min 4096 v;
4225 match oldmode with
4226 | Birdseye beye ->
4227 leavebirdseye beye false;
4228 enterbirdseye ()
4229 | _ -> ()
4232 let mode = state.mode in
4233 src#string "columns"
4234 (fun () ->
4235 match conf.columns with
4236 | Csingle _ -> "1"
4237 | Cmulti (multi, _) -> multicolumns_to_string multi
4238 | Csplit (count, _) -> "-" ^ string_of_int count
4240 (fun v ->
4241 let n, a, b = multicolumns_of_string v in
4242 setcolumns mode n a b);
4244 sep ();
4245 src#caption "Presentation mode" 0;
4246 src#bool "scrollbar visible"
4247 (fun () -> conf.scrollbarinpm)
4248 (fun v ->
4249 if v != conf.scrollbarinpm
4250 then (
4251 conf.scrollbarinpm <- v;
4252 if conf.presentation
4253 then (
4254 state.scrollw <- if v then conf.scrollbw else 0;
4255 reshape conf.winw conf.winh;
4260 sep ();
4261 src#caption "Pixmap cache" 0;
4262 src#int_with_suffix "size (advisory)"
4263 (fun () -> conf.memlimit)
4264 (fun v -> conf.memlimit <- v);
4266 src#caption2 "used"
4267 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4268 (string_with_suffix_of_int state.memused)
4269 (Hashtbl.length state.tilemap)) 1;
4271 sep ();
4272 src#caption "Layout" 0;
4273 src#caption2 "Dimension"
4274 (fun () ->
4275 Printf.sprintf "%dx%d (virtual %dx%d)"
4276 conf.winw conf.winh
4277 state.w state.maxy)
4279 if conf.debug
4280 then
4281 src#caption2 "Position" (fun () ->
4282 Printf.sprintf "%dx%d" state.x state.y
4284 else
4285 src#caption2 "Visible" (fun () -> describe_location ()) 1
4288 sep ();
4289 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4290 "Save these parameters as global defaults at exit"
4291 (fun () -> conf.bedefault)
4292 (fun v -> conf.bedefault <- v)
4295 sep ();
4296 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4297 src#bool ~offset:0 ~btos "Extended parameters"
4298 (fun () -> !showextended)
4299 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4300 if !showextended
4301 then (
4302 src#bool "checkers"
4303 (fun () -> conf.checkers)
4304 (fun v -> conf.checkers <- v; setcheckers v);
4305 src#bool "update cursor"
4306 (fun () -> conf.updatecurs)
4307 (fun v -> conf.updatecurs <- v);
4308 src#bool "verbose"
4309 (fun () -> conf.verbose)
4310 (fun v -> conf.verbose <- v);
4311 src#bool "invert colors"
4312 (fun () -> conf.invert)
4313 (fun v -> conf.invert <- v);
4314 src#bool "max fit"
4315 (fun () -> conf.maxhfit)
4316 (fun v -> conf.maxhfit <- v);
4317 src#bool "redirect stderr"
4318 (fun () -> conf.redirectstderr)
4319 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4320 src#string "uri launcher"
4321 (fun () -> conf.urilauncher)
4322 (fun v -> conf.urilauncher <- v);
4323 src#string "path launcher"
4324 (fun () -> conf.pathlauncher)
4325 (fun v -> conf.pathlauncher <- v);
4326 src#string "tile size"
4327 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4328 (fun v ->
4330 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4331 conf.tilew <- max 64 w;
4332 conf.tileh <- max 64 h;
4333 flushtiles ();
4334 with exn ->
4335 state.text <- Printf.sprintf "bad tile size `%s': %s"
4336 v (Printexc.to_string exn));
4337 src#int "texture count"
4338 (fun () -> conf.texcount)
4339 (fun v ->
4340 if realloctexts v
4341 then conf.texcount <- v
4342 else showtext '!' " Failed to set texture count please retry later"
4344 src#int "slice height"
4345 (fun () -> conf.sliceheight)
4346 (fun v ->
4347 conf.sliceheight <- v;
4348 wcmd "sliceh %d" conf.sliceheight;
4350 src#int "anti-aliasing level"
4351 (fun () -> conf.aalevel)
4352 (fun v ->
4353 conf.aalevel <- bound v 0 8;
4354 state.anchor <- getanchor ();
4355 opendoc state.path state.password;
4357 src#string "page scroll scaling factor"
4358 (fun () -> string_of_float conf.pgscale)
4359 (fun v ->
4361 let s = float_of_string v in
4362 conf.pgscale <- s
4363 with exn ->
4364 state.text <- Printf.sprintf
4365 "bad page scroll scaling factor `%s': %s"
4366 v (Printexc.to_string exn)
4369 src#int "ui font size"
4370 (fun () -> fstate.fontsize)
4371 (fun v -> setfontsize (bound v 5 100));
4372 src#int "hint font size"
4373 (fun () -> conf.hfsize)
4374 (fun v -> conf.hfsize <- bound v 5 100);
4375 colorp "background color"
4376 (fun () -> conf.bgcolor)
4377 (fun v -> conf.bgcolor <- v);
4378 src#bool "crop hack"
4379 (fun () -> conf.crophack)
4380 (fun v -> conf.crophack <- v);
4381 src#bool "multi column centering"
4382 (fun () -> conf.multicenter)
4383 (fun v -> conf.multicenter <- v; represent ());
4384 src#string "trim fuzz"
4385 (fun () -> irect_to_string conf.trimfuzz)
4386 (fun v ->
4388 conf.trimfuzz <- irect_of_string v;
4389 if conf.trimmargins
4390 then settrim true conf.trimfuzz;
4391 with exn ->
4392 state.text <- Printf.sprintf "bad irect `%s': %s"
4393 v (Printexc.to_string exn)
4395 src#string "throttle"
4396 (fun () ->
4397 match conf.maxwait with
4398 | None -> "show place holder if page is not ready"
4399 | Some time ->
4400 if time = infinity
4401 then "wait for page to fully render"
4402 else
4403 "wait " ^ string_of_float time
4404 ^ " seconds before showing placeholder"
4406 (fun v ->
4408 let f = float_of_string v in
4409 if f <= 0.0
4410 then conf.maxwait <- None
4411 else conf.maxwait <- Some f
4412 with exn ->
4413 state.text <- Printf.sprintf "bad time `%s': %s"
4414 v (Printexc.to_string exn)
4416 src#string "ghyll scroll"
4417 (fun () ->
4418 match conf.ghyllscroll with
4419 | None -> ""
4420 | Some nab -> ghyllscroll_to_string nab
4422 (fun v ->
4424 let gs =
4425 if String.length v = 0
4426 then None
4427 else Some (ghyllscroll_of_string v)
4429 conf.ghyllscroll <- gs
4430 with exn ->
4431 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4432 v (Printexc.to_string exn)
4434 src#string "selection command"
4435 (fun () -> conf.selcmd)
4436 (fun v -> conf.selcmd <- v);
4437 src#colorspace "color space"
4438 (fun () -> colorspace_to_string conf.colorspace)
4439 (fun v ->
4440 conf.colorspace <- colorspace_of_int v;
4441 wcmd "cs %d" v;
4442 load state.layout;
4446 sep ();
4447 src#caption "Document" 0;
4448 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4449 src#caption2 "Pages"
4450 (fun () -> string_of_int state.pagecount) 1;
4451 src#caption2 "Dimensions"
4452 (fun () -> string_of_int (List.length state.pdims)) 1;
4453 if conf.trimmargins
4454 then (
4455 sep ();
4456 src#caption "Trimmed margins" 0;
4457 src#caption2 "Dimensions"
4458 (fun () -> string_of_int (List.length state.pdims)) 1;
4461 sep ();
4462 src#caption "OpenGL" 0;
4463 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4464 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4465 src#reset prevmode prevuioh;
4467 fun () ->
4468 state.text <- "";
4469 let prevmode = state.mode
4470 and prevuioh = state.uioh in
4471 fillsrc prevmode prevuioh;
4472 let source = (src :> lvsource) in
4473 let modehash = findkeyhash conf "info" in
4474 state.uioh <- coe (object (self)
4475 inherit listview ~source ~trusted:true ~modehash as super
4476 val mutable m_prevmemused = 0
4477 method infochanged = function
4478 | Memused ->
4479 if m_prevmemused != state.memused
4480 then (
4481 m_prevmemused <- state.memused;
4482 G.postRedisplay "memusedchanged";
4484 | Pdim -> G.postRedisplay "pdimchanged"
4485 | Docinfo -> fillsrc prevmode prevuioh
4487 method key key mask =
4488 if not (Wsi.withctrl mask)
4489 then
4490 match key with
4491 | 0xff51 -> coe (self#updownlevel ~-1)
4492 | 0xff53 -> coe (self#updownlevel 1)
4493 | _ -> super#key key mask
4494 else super#key key mask
4495 end);
4496 G.postRedisplay "info";
4499 let enterhelpmode =
4500 let source =
4501 (object
4502 inherit lvsourcebase
4503 method getitemcount = Array.length state.help
4504 method getitem n =
4505 let s, n, _ = state.help.(n) in
4506 (s, n)
4508 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4509 let optuioh =
4510 if not cancel
4511 then (
4512 m_qsearch <- qsearch;
4513 match state.help.(active) with
4514 | _, _, Action f -> Some (f uioh)
4515 | _ -> Some (uioh)
4517 else None
4519 m_active <- active;
4520 m_first <- first;
4521 m_pan <- pan;
4522 optuioh
4524 method hasaction n =
4525 match state.help.(n) with
4526 | _, _, Action _ -> true
4527 | _ -> false
4529 initializer
4530 m_active <- -1
4531 end)
4532 in fun () ->
4533 let modehash = findkeyhash conf "help" in
4534 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4535 G.postRedisplay "help";
4538 let entermsgsmode =
4539 let msgsource =
4540 let re = Str.regexp "[\r\n]" in
4541 (object
4542 inherit lvsourcebase
4543 val mutable m_items = [||]
4545 method getitemcount = 1 + Array.length m_items
4547 method getitem n =
4548 if n = 0
4549 then "[Clear]", 0
4550 else m_items.(n-1), 0
4552 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4553 ignore uioh;
4554 if not cancel
4555 then (
4556 if active = 0
4557 then Buffer.clear state.errmsgs;
4558 m_qsearch <- qsearch;
4560 m_active <- active;
4561 m_first <- first;
4562 m_pan <- pan;
4563 None
4565 method hasaction n =
4566 n = 0
4568 method reset =
4569 state.newerrmsgs <- false;
4570 let l = Str.split re (Buffer.contents state.errmsgs) in
4571 m_items <- Array.of_list l
4573 initializer
4574 m_active <- 0
4575 end)
4576 in fun () ->
4577 state.text <- "";
4578 msgsource#reset;
4579 let source = (msgsource :> lvsource) in
4580 let modehash = findkeyhash conf "listview" in
4581 state.uioh <- coe (object
4582 inherit listview ~source ~trusted:false ~modehash as super
4583 method display =
4584 if state.newerrmsgs
4585 then msgsource#reset;
4586 super#display
4587 end);
4588 G.postRedisplay "msgs";
4591 let quickbookmark ?title () =
4592 match state.layout with
4593 | [] -> ()
4594 | l :: _ ->
4595 let title =
4596 match title with
4597 | None ->
4598 let sec = Unix.gettimeofday () in
4599 let tm = Unix.localtime sec in
4600 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4601 (l.pageno+1)
4602 tm.Unix.tm_mday
4603 tm.Unix.tm_mon
4604 (tm.Unix.tm_year + 1900)
4605 tm.Unix.tm_hour
4606 tm.Unix.tm_min
4607 | Some title -> title
4609 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
4612 let doreshape w h =
4613 state.fullscreen <- None;
4614 Wsi.reshape w h;
4617 let setautoscrollspeed step goingdown =
4618 let incr = max 1 ((abs step) / 2) in
4619 let incr = if goingdown then incr else -incr in
4620 let astep = step + incr in
4621 state.autoscroll <- Some astep;
4624 let gotounder = function
4625 | Ulinkgoto (pageno, top) ->
4626 if pageno >= 0
4627 then (
4628 addnav ();
4629 gotopage1 pageno top;
4632 | Ulinkuri s ->
4633 gotouri s
4635 | Uremote (filename, pageno) ->
4636 let path =
4637 if Sys.file_exists filename
4638 then filename
4639 else
4640 let dir = Filename.dirname state.path in
4641 let path = Filename.concat dir filename in
4642 if Sys.file_exists path
4643 then path
4644 else ""
4646 if String.length path > 0
4647 then (
4648 let anchor = getanchor () in
4649 let ranchor = state.path, state.password, anchor in
4650 state.anchor <- (pageno, 0.0, 0.0);
4651 state.ranchors <- ranchor :: state.ranchors;
4652 opendoc path "";
4654 else showtext '!' ("Could not find " ^ filename)
4656 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4659 let canpan () =
4660 match conf.columns with
4661 | Csplit _ -> true
4662 | _ -> conf.zoom > 1.0
4665 let viewkeyboard key mask =
4666 let enttext te =
4667 let mode = state.mode in
4668 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4669 state.text <- "";
4670 enttext ();
4671 G.postRedisplay "view:enttext"
4673 let ctrl = Wsi.withctrl mask in
4674 match key with
4675 | 81 -> (* Q *)
4676 exit 0
4678 | 0xff63 -> (* insert *)
4679 if conf.angle mod 360 = 0
4680 then (
4681 state.mode <- LinkNav (Ltgendir 0);
4682 gotoy state.y;
4684 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4686 | 0xff1b | 113 -> (* escape / q *)
4687 begin match state.mstate with
4688 | Mzoomrect _ ->
4689 state.mstate <- Mnone;
4690 Wsi.setcursor Wsi.CURSOR_INHERIT;
4691 G.postRedisplay "kill zoom rect";
4692 | _ ->
4693 match state.ranchors with
4694 | [] -> raise Quit
4695 | (path, password, anchor) :: rest ->
4696 state.ranchors <- rest;
4697 state.anchor <- anchor;
4698 opendoc path password
4699 end;
4701 | 0xff08 -> (* backspace *)
4702 let y = getnav ~-1 in
4703 gotoy_and_clear_text y
4705 | 111 -> (* o *)
4706 enteroutlinemode ()
4708 | 117 -> (* u *)
4709 state.rects <- [];
4710 state.text <- "";
4711 G.postRedisplay "dehighlight";
4713 | 47 | 63 -> (* / ? *)
4714 let ondone isforw s =
4715 cbput state.hists.pat s;
4716 state.searchpattern <- s;
4717 search s isforw
4719 let s = String.create 1 in
4720 s.[0] <- Char.chr key;
4721 enttext (s, "", Some (onhist state.hists.pat),
4722 textentry, ondone (key = 47), true)
4724 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4725 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4726 setzoom (conf.zoom +. incr)
4728 | 43 | 0xffab -> (* + *)
4729 let ondone s =
4730 let n =
4731 try int_of_string s with exc ->
4732 state.text <- Printf.sprintf "bad integer `%s': %s"
4733 s (Printexc.to_string exc);
4734 max_int
4736 if n != max_int
4737 then (
4738 conf.pagebias <- n;
4739 state.text <- "page bias is now " ^ string_of_int n;
4742 enttext ("page bias: ", "", None, intentry, ondone, true)
4744 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4745 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4746 setzoom (max 0.01 (conf.zoom -. decr))
4748 | 45 | 0xffad -> (* - *)
4749 let ondone msg = state.text <- msg in
4750 enttext (
4751 "option [acfhilpstvxACFPRSZTIS]: ", "", None,
4752 optentry state.mode, ondone, true
4755 | 48 when ctrl -> (* ctrl-0 *)
4756 setzoom 1.0
4758 | 49 when ctrl -> (* ctrl-1 *)
4759 let cols =
4760 match conf.columns with
4761 | Csingle _ | Cmulti _ -> 1
4762 | Csplit (n, _) -> n
4764 let zoom = zoomforh conf.winw conf.winh state.scrollw cols in
4765 if zoom < 1.0
4766 then setzoom zoom
4768 | 0xffc6 -> (* f9 *)
4769 togglebirdseye ()
4771 | 57 when ctrl -> (* ctrl-9 *)
4772 togglebirdseye ()
4774 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4775 when not ctrl -> (* 0..9 *)
4776 let ondone s =
4777 let n =
4778 try int_of_string s with exc ->
4779 state.text <- Printf.sprintf "bad integer `%s': %s"
4780 s (Printexc.to_string exc);
4783 if n >= 0
4784 then (
4785 addnav ();
4786 cbput state.hists.pag (string_of_int n);
4787 gotopage1 (n + conf.pagebias - 1) 0;
4790 let pageentry text key =
4791 match Char.unsafe_chr key with
4792 | 'g' -> TEdone text
4793 | _ -> intentry text key
4795 let text = "x" in text.[0] <- Char.chr key;
4796 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4798 | 98 -> (* b *)
4799 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4800 reshape conf.winw conf.winh;
4802 | 108 -> (* l *)
4803 conf.hlinks <- not conf.hlinks;
4804 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4805 G.postRedisplay "toggle highlightlinks";
4807 | 70 -> (* F *)
4808 state.glinks <- true;
4809 let mode = state.mode in
4810 state.mode <- Textentry (
4811 (":", "", None, linknentry, linkndone (fun under ->
4812 addnav ();
4813 gotounder under
4814 ), false
4815 ), fun _ ->
4816 state.glinks <- false;
4817 state.mode <- mode
4819 state.text <- "";
4820 G.postRedisplay "view:linkent(F)"
4822 | 121 -> (* y *)
4823 state.glinks <- true;
4824 let mode = state.mode in
4825 state.mode <- Textentry (
4826 (":", "", None, linknentry, linkndone (fun under ->
4827 match Ne.pipe () with
4828 | Ne.Exn exn ->
4829 showtext '!' (Printf.sprintf "pipe failed: %s"
4830 (Printexc.to_string exn));
4831 | Ne.Res (r, w) ->
4832 let popened =
4833 try popen conf.selcmd [r, 0; w, -1]; true
4834 with exn ->
4835 showtext '!'
4836 (Printf.sprintf "failed to execute %s: %s"
4837 conf.selcmd (Printexc.to_string exn));
4838 false
4840 let clo cap fd =
4841 Ne.clo fd (fun msg ->
4842 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
4845 let s = undertext under in
4846 if popened
4847 then
4848 (try
4849 let l = String.length s in
4850 let n = Unix.write w s 0 l in
4851 if n != l
4852 then
4853 showtext '!'
4854 (Printf.sprintf
4855 "failed to write %d characters to sel pipe, wrote %d"
4858 with exn ->
4859 showtext '!'
4860 (Printf.sprintf "failed to write to sel pipe: %s"
4861 (Printexc.to_string exn)
4864 else dolog "%s" s;
4865 clo "pipe/r" r;
4866 clo "pipe/w" w;
4867 ), false
4869 fun _ ->
4870 state.glinks <- false;
4871 state.mode <- mode
4873 state.text <- "";
4874 G.postRedisplay "view:linkent"
4876 | 97 -> (* a *)
4877 begin match state.autoscroll with
4878 | Some step ->
4879 conf.autoscrollstep <- step;
4880 state.autoscroll <- None
4881 | None ->
4882 if conf.autoscrollstep = 0
4883 then state.autoscroll <- Some 1
4884 else state.autoscroll <- Some conf.autoscrollstep
4887 | 112 when ctrl -> (* ctrl-p *)
4888 launchpath ()
4890 | 80 -> (* P *)
4891 setpresentationmode (not conf.presentation);
4892 showtext ' ' ("presentation mode " ^
4893 if conf.presentation then "on" else "off");
4895 | 102 -> (* f *)
4896 begin match state.fullscreen with
4897 | None ->
4898 state.fullscreen <- Some (conf.winw, conf.winh);
4899 Wsi.fullscreen ()
4900 | Some (w, h) ->
4901 state.fullscreen <- None;
4902 doreshape w h
4905 | 103 -> (* g *)
4906 gotoy_and_clear_text 0
4908 | 71 -> (* G *)
4909 gotopage1 (state.pagecount - 1) 0
4911 | 112 | 78 -> (* p|N *)
4912 search state.searchpattern false
4914 | 110 | 0xffc0 -> (* n|F3 *)
4915 search state.searchpattern true
4917 | 116 -> (* t *)
4918 begin match state.layout with
4919 | [] -> ()
4920 | l :: _ ->
4921 gotoy_and_clear_text (getpagey l.pageno)
4924 | 32 -> (* space *)
4925 begin match state.layout with
4926 | [] -> ()
4927 | l :: rest ->
4928 match conf.columns with
4929 | Csingle _ | Cmulti _ ->
4930 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4931 then
4932 let y = clamp (pgscale conf.winh) in
4933 gotoy_and_clear_text y
4934 else
4935 let pageno = min (l.pageno+1) (state.pagecount-1) in
4936 gotoy_and_clear_text (getpagey pageno)
4937 | Csplit (n, _) ->
4938 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4939 then
4940 let pagey, pageh = getpageyh l.pageno in
4941 let pagey = pagey + pageh * l.pagecol in
4942 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4943 gotoy_and_clear_text (pagey + pageh + ips)
4946 | 0xff9f | 0xffff -> (* delete *)
4947 begin match state.layout with
4948 | [] -> ()
4949 | l :: _ ->
4950 match conf.columns with
4951 | Csingle _ | Cmulti _ ->
4952 if conf.presentation && l.pagey != 0
4953 then
4954 gotoy_and_clear_text (clamp (pgscale ~-(conf.winh)))
4955 else
4956 let pageno = max 0 (l.pageno-1) in
4957 gotoy_and_clear_text (getpagey pageno)
4958 | Csplit (n, _) ->
4959 let y =
4960 if l.pagecol = 0
4961 then
4962 if l.pageno = 0
4963 then l.pagey
4964 else
4965 let pageno = max 0 (l.pageno-1) in
4966 let pagey, pageh = getpageyh pageno in
4967 pagey + (n-1)*pageh
4968 else
4969 let pagey, pageh = getpageyh l.pageno in
4970 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4972 gotoy_and_clear_text y
4975 | 61 -> (* = *)
4976 showtext ' ' (describe_location ());
4978 | 119 -> (* w *)
4979 begin match state.layout with
4980 | [] -> ()
4981 | l :: _ ->
4982 doreshape (l.pagew + state.scrollw) l.pageh;
4983 G.postRedisplay "w"
4986 | 39 -> (* ' *)
4987 enterbookmarkmode ()
4989 | 104 | 0xffbe -> (* h|F1 *)
4990 enterhelpmode ()
4992 | 105 -> (* i *)
4993 enterinfomode ()
4995 | 101 when conf.redirectstderr -> (* e *)
4996 entermsgsmode ()
4998 | 109 -> (* m *)
4999 let ondone s =
5000 match state.layout with
5001 | l :: _ -> state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5002 | _ -> ()
5004 enttext ("bookmark: ", "", None, textentry, ondone, true)
5006 | 126 -> (* ~ *)
5007 quickbookmark ();
5008 showtext ' ' "Quick bookmark added";
5010 | 122 -> (* z *)
5011 begin match state.layout with
5012 | l :: _ ->
5013 let rect = getpdimrect l.pagedimno in
5014 let w, h =
5015 if conf.crophack
5016 then
5017 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5018 truncate (1.2 *. (rect.(3) -. rect.(0))))
5019 else
5020 (truncate (rect.(1) -. rect.(0)),
5021 truncate (rect.(3) -. rect.(0)))
5023 let w = truncate ((float w)*.conf.zoom)
5024 and h = truncate ((float h)*.conf.zoom) in
5025 if w != 0 && h != 0
5026 then (
5027 state.anchor <- getanchor ();
5028 doreshape (w + state.scrollw) (h + conf.interpagespace)
5030 G.postRedisplay "z";
5032 | [] -> ()
5035 | 50 when ctrl -> (* ctrl-2 *)
5036 let maxw = getmaxw () in
5037 if maxw > 0.0
5038 then setzoom (maxw /. float conf.winw)
5040 | 60 | 62 -> (* < > *)
5041 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
5043 | 91 | 93 -> (* [ ] *)
5044 conf.colorscale <-
5045 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5047 G.postRedisplay "brightness";
5049 | 99 when state.mode = View -> (* c *)
5050 let (c, a, b), z =
5051 match state.prevcolumns with
5052 | None -> (1, 0, 0), 1.0
5053 | Some (columns, z) ->
5054 let cab =
5055 match columns with
5056 | Csplit (c, _) -> -c, 0, 0
5057 | Cmulti ((c, a, b), _) -> c, a, b
5058 | Csingle _ -> 1, 0, 0
5060 cab, z
5062 setcolumns View c a b;
5063 setzoom z;
5065 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
5066 setzoom state.prevzoom
5068 | 107 | 0xff52 -> (* k up *)
5069 begin match state.autoscroll with
5070 | None ->
5071 begin match state.mode with
5072 | Birdseye beye -> upbirdseye 1 beye
5073 | _ ->
5074 if ctrl
5075 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
5076 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5078 | Some n ->
5079 setautoscrollspeed n false
5082 | 106 | 0xff54 -> (* j down *)
5083 begin match state.autoscroll with
5084 | None ->
5085 begin match state.mode with
5086 | Birdseye beye -> downbirdseye 1 beye
5087 | _ ->
5088 if ctrl
5089 then gotoy_and_clear_text (clamp (conf.winh/2))
5090 else gotoy_and_clear_text (clamp conf.scrollstep)
5092 | Some n ->
5093 setautoscrollspeed n true
5096 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
5097 if canpan ()
5098 then
5099 let dx =
5100 if ctrl
5101 then conf.winw / 2
5102 else 10
5104 let dx = if key = 0xff51 then dx else -dx in
5105 state.x <- state.x + dx;
5106 gotoy_and_clear_text state.y
5107 else (
5108 state.text <- "";
5109 G.postRedisplay "lef/right"
5112 | 0xff55 -> (* prior *)
5113 let y =
5114 if ctrl
5115 then
5116 match state.layout with
5117 | [] -> state.y
5118 | l :: _ -> state.y - l.pagey
5119 else
5120 clamp (pgscale (-conf.winh))
5122 gotoghyll y
5124 | 0xff56 -> (* next *)
5125 let y =
5126 if ctrl
5127 then
5128 match List.rev state.layout with
5129 | [] -> state.y
5130 | l :: _ -> getpagey l.pageno
5131 else
5132 clamp (pgscale conf.winh)
5134 gotoghyll y
5136 | 0xff50 -> (* home *)
5137 gotoghyll 0
5138 | 0xff57 -> (* end *)
5139 gotoghyll (clamp state.maxy)
5140 | 0xff53 when Wsi.withalt mask -> (* right *)
5141 gotoghyll (getnav ~-1)
5142 | 0xff51 when Wsi.withalt mask -> (* left *)
5143 gotoghyll (getnav 1)
5145 | 114 -> (* r *)
5146 state.anchor <- getanchor ();
5147 opendoc state.path state.password
5149 | 118 when conf.debug -> (* v *)
5150 state.rects <- [];
5151 List.iter (fun l ->
5152 match getopaque l.pageno with
5153 | None -> ()
5154 | Some opaque ->
5155 let x0, y0, x1, y1 = pagebbox opaque in
5156 let a,b = float x0, float y0 in
5157 let c,d = float x1, float y0 in
5158 let e,f = float x1, float y1 in
5159 let h,j = float x0, float y1 in
5160 let rect = (a,b,c,d,e,f,h,j) in
5161 debugrect rect;
5162 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5163 ) state.layout;
5164 G.postRedisplay "v";
5166 | _ ->
5167 vlog "huh? %s" (Wsi.keyname key)
5170 let linknavkeyboard key mask linknav =
5171 let getpage pageno =
5172 let rec loop = function
5173 | [] -> None
5174 | l :: _ when l.pageno = pageno -> Some l
5175 | _ :: rest -> loop rest
5176 in loop state.layout
5178 let doexact (pageno, n) =
5179 match getopaque pageno, getpage pageno with
5180 | Some opaque, Some l ->
5181 if key = 0xff0d
5182 then
5183 let under = getlink opaque n in
5184 G.postRedisplay "link gotounder";
5185 gotounder under;
5186 state.mode <- View;
5187 else
5188 let opt, dir =
5189 match key with
5190 | 0xff50 -> (* home *)
5191 Some (findlink opaque LDfirst), -1
5193 | 0xff57 -> (* end *)
5194 Some (findlink opaque LDlast), 1
5196 | 0xff51 -> (* left *)
5197 Some (findlink opaque (LDleft n)), -1
5199 | 0xff53 -> (* right *)
5200 Some (findlink opaque (LDright n)), 1
5202 | 0xff52 -> (* up *)
5203 Some (findlink opaque (LDup n)), -1
5205 | 0xff54 -> (* down *)
5206 Some (findlink opaque (LDdown n)), 1
5208 | _ -> None, 0
5210 let pwl l dir =
5211 begin match findpwl l.pageno dir with
5212 | Pwlnotfound -> ()
5213 | Pwl pageno ->
5214 let notfound dir =
5215 state.mode <- LinkNav (Ltgendir dir);
5216 let y, h = getpageyh pageno in
5217 let y =
5218 if dir < 0
5219 then y + h - conf.winh
5220 else y
5222 gotoy y
5224 begin match getopaque pageno, getpage pageno with
5225 | Some opaque, Some _ ->
5226 let link =
5227 let ld = if dir > 0 then LDfirst else LDlast in
5228 findlink opaque ld
5230 begin match link with
5231 | Lfound m ->
5232 showlinktype (getlink opaque m);
5233 state.mode <- LinkNav (Ltexact (pageno, m));
5234 G.postRedisplay "linknav jpage";
5235 | _ -> notfound dir
5236 end;
5237 | _ -> notfound dir
5238 end;
5239 end;
5241 begin match opt with
5242 | Some Lnotfound -> pwl l dir;
5243 | Some (Lfound m) ->
5244 if m = n
5245 then pwl l dir
5246 else (
5247 let _, y0, _, y1 = getlinkrect opaque m in
5248 if y0 < l.pagey
5249 then gotopage1 l.pageno y0
5250 else (
5251 let d = fstate.fontsize + 1 in
5252 if y1 - l.pagey > l.pagevh - d
5253 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
5254 else G.postRedisplay "linknav";
5256 showlinktype (getlink opaque m);
5257 state.mode <- LinkNav (Ltexact (l.pageno, m));
5260 | None -> viewkeyboard key mask
5261 end;
5262 | _ -> viewkeyboard key mask
5264 if key = 0xff63
5265 then (
5266 state.mode <- View;
5267 G.postRedisplay "leave linknav"
5269 else
5270 match linknav with
5271 | Ltgendir _ -> viewkeyboard key mask
5272 | Ltexact exact -> doexact exact
5275 let keyboard key mask =
5276 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5277 then wcmd "interrupt"
5278 else state.uioh <- state.uioh#key key mask
5281 let birdseyekeyboard key mask
5282 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5283 let incr =
5284 match conf.columns with
5285 | Csingle _ -> 1
5286 | Cmulti ((c, _, _), _) -> c
5287 | Csplit _ -> failwith "bird's eye split mode"
5289 let pgh layout = List.fold_left (fun m l -> max l.pageh m) conf.winh layout in
5290 match key with
5291 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5292 let y, h = getpageyh pageno in
5293 let top = (conf.winh - h) / 2 in
5294 gotoy (max 0 (y - top))
5295 | 0xff0d -> leavebirdseye beye false
5296 | 0xff1b -> leavebirdseye beye true (* escape *)
5297 | 0xff52 -> upbirdseye incr beye (* up *)
5298 | 0xff54 -> downbirdseye incr beye (* down *)
5299 | 0xff51 -> upbirdseye 1 beye (* left *)
5300 | 0xff53 -> downbirdseye 1 beye (* right *)
5302 | 0xff55 -> (* prior *)
5303 begin match state.layout with
5304 | l :: _ ->
5305 if l.pagey != 0
5306 then (
5307 state.mode <- Birdseye (
5308 oconf, leftx, l.pageno, hooverpageno, anchor
5310 gotopage1 l.pageno 0;
5312 else (
5313 let layout = layout (state.y-conf.winh) (pgh state.layout) in
5314 match layout with
5315 | [] -> gotoy (clamp (-conf.winh))
5316 | l :: _ ->
5317 state.mode <- Birdseye (
5318 oconf, leftx, l.pageno, hooverpageno, anchor
5320 gotopage1 l.pageno 0
5323 | [] -> gotoy (clamp (-conf.winh))
5324 end;
5326 | 0xff56 -> (* next *)
5327 begin match List.rev state.layout with
5328 | l :: _ ->
5329 let layout = layout (state.y + (pgh state.layout)) conf.winh in
5330 begin match layout with
5331 | [] ->
5332 let incr = l.pageh - l.pagevh in
5333 if incr = 0
5334 then (
5335 state.mode <-
5336 Birdseye (
5337 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5339 G.postRedisplay "birdseye pagedown";
5341 else gotoy (clamp (incr + conf.interpagespace*2));
5343 | l :: _ ->
5344 state.mode <-
5345 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5346 gotopage1 l.pageno 0;
5349 | [] -> gotoy (clamp conf.winh)
5350 end;
5352 | 0xff50 -> (* home *)
5353 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5354 gotopage1 0 0
5356 | 0xff57 -> (* end *)
5357 let pageno = state.pagecount - 1 in
5358 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5359 if not (pagevisible state.layout pageno)
5360 then
5361 let h =
5362 match List.rev state.pdims with
5363 | [] -> conf.winh
5364 | (_, _, h, _) :: _ -> h
5366 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
5367 else G.postRedisplay "birdseye end";
5368 | _ -> viewkeyboard key mask
5371 let drawpage l linkindexbase =
5372 let color =
5373 match state.mode with
5374 | Textentry _ -> scalecolor 0.4
5375 | LinkNav _
5376 | View -> scalecolor 1.0
5377 | Birdseye (_, _, pageno, hooverpageno, _) ->
5378 if l.pageno = hooverpageno
5379 then scalecolor 0.9
5380 else (
5381 if l.pageno = pageno
5382 then scalecolor 1.0
5383 else scalecolor 0.8
5386 drawtiles l color;
5387 begin match getopaque l.pageno with
5388 | Some opaque ->
5389 if tileready l l.pagex l.pagey
5390 then
5391 let x = l.pagedispx - l.pagex
5392 and y = l.pagedispy - l.pagey in
5393 let hlmask =
5394 match conf.columns with
5395 | Csingle _ | Cmulti _ ->
5396 (if conf.hlinks then 1 else 0)
5397 + (if state.glinks
5398 && not (isbirdseye state.mode) then 2 else 0)
5399 | _ -> 0
5401 let s =
5402 match state.mode with
5403 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5404 | _ -> ""
5406 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5407 else 0
5409 | _ -> 0
5410 end;
5413 let scrollindicator () =
5414 let sbw, ph, sh = state.uioh#scrollph in
5415 let sbh, pw, sw = state.uioh#scrollpw in
5417 GlDraw.color (0.64, 0.64, 0.64);
5418 GlDraw.rect
5419 (float (conf.winw - sbw), 0.)
5420 (float conf.winw, float conf.winh)
5422 GlDraw.rect
5423 (0., float (conf.winh - sbh))
5424 (float (conf.winw - state.scrollw - 1), float conf.winh)
5426 GlDraw.color (0.0, 0.0, 0.0);
5428 GlDraw.rect
5429 (float (conf.winw - sbw), ph)
5430 (float conf.winw, ph +. sh)
5432 GlDraw.rect
5433 (pw, float (conf.winh - sbh))
5434 (pw +. sw, float conf.winh)
5438 let showsel () =
5439 match state.mstate with
5440 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5443 | Msel ((x0, y0), (x1, y1)) ->
5444 let rec loop = function
5445 | l :: ls ->
5446 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5447 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5448 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5449 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5450 then
5451 match getopaque l.pageno with
5452 | Some opaque ->
5453 let x0, y0 = pagetranslatepoint l x0 y0 in
5454 let x1, y1 = pagetranslatepoint l x1 y1 in
5455 seltext opaque (x0, y0, x1, y1);
5456 | _ -> ()
5457 else loop ls
5458 | [] -> ()
5460 loop state.layout
5463 let showrects rects =
5464 Gl.enable `blend;
5465 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5466 GlDraw.polygon_mode `both `fill;
5467 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5468 List.iter
5469 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5470 List.iter (fun l ->
5471 if l.pageno = pageno
5472 then (
5473 let dx = float (l.pagedispx - l.pagex) in
5474 let dy = float (l.pagedispy - l.pagey) in
5475 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5476 GlDraw.begins `quads;
5478 GlDraw.vertex2 (x0+.dx, y0+.dy);
5479 GlDraw.vertex2 (x1+.dx, y1+.dy);
5480 GlDraw.vertex2 (x2+.dx, y2+.dy);
5481 GlDraw.vertex2 (x3+.dx, y3+.dy);
5483 GlDraw.ends ();
5485 ) state.layout
5486 ) rects
5488 Gl.disable `blend;
5491 let display () =
5492 GlClear.color (scalecolor2 conf.bgcolor);
5493 GlClear.clear [`color];
5494 let rec loop linkindexbase = function
5495 | l :: rest ->
5496 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5497 loop linkindexbase rest
5498 | [] -> ()
5500 loop 0 state.layout;
5501 let rects =
5502 match state.mode with
5503 | LinkNav (Ltexact (pageno, linkno)) ->
5504 begin match getopaque pageno with
5505 | Some opaque ->
5506 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5507 (pageno, 5, (
5508 float x0, float y0,
5509 float x1, float y0,
5510 float x1, float y1,
5511 float x0, float y1)
5512 ) :: state.rects
5513 | None -> state.rects
5515 | _ -> state.rects
5517 showrects rects;
5518 showsel ();
5519 state.uioh#display;
5520 begin match state.mstate with
5521 | Mzoomrect ((x0, y0), (x1, y1)) ->
5522 Gl.enable `blend;
5523 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5524 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5525 GlDraw.rect (float x0, float y0)
5526 (float x1, float y1);
5527 Gl.disable `blend;
5528 | _ -> ()
5529 end;
5530 enttext ();
5531 scrollindicator ();
5532 Wsi.swapb ();
5535 let zoomrect x y x1 y1 =
5536 let x0 = min x x1
5537 and x1 = max x x1
5538 and y0 = min y y1 in
5539 gotoy (state.y + y0);
5540 state.anchor <- getanchor ();
5541 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5542 let margin =
5543 if state.w < conf.winw - state.scrollw
5544 then (conf.winw - state.scrollw - state.w) / 2
5545 else 0
5547 state.x <- (state.x + margin) - x0;
5548 setzoom zoom;
5549 Wsi.setcursor Wsi.CURSOR_INHERIT;
5550 state.mstate <- Mnone;
5553 let scrollx x =
5554 let winw = conf.winw - state.scrollw - 1 in
5555 let s = float x /. float winw in
5556 let destx = truncate (float (state.w + winw) *. s) in
5557 state.x <- winw - destx;
5558 gotoy_and_clear_text state.y;
5559 state.mstate <- Mscrollx;
5562 let scrolly y =
5563 let s = float y /. float conf.winh in
5564 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5565 gotoy_and_clear_text desty;
5566 state.mstate <- Mscrolly;
5569 let viewmouse button down x y mask =
5570 match button with
5571 | n when (n == 4 || n == 5) && not down ->
5572 if Wsi.withctrl mask
5573 then (
5574 match state.mstate with
5575 | Mzoom (oldn, i) ->
5576 if oldn = n
5577 then (
5578 if i = 2
5579 then
5580 let incr =
5581 match n with
5582 | 5 ->
5583 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5584 | _ ->
5585 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5587 let zoom = conf.zoom -. incr in
5588 setzoom zoom;
5589 state.mstate <- Mzoom (n, 0);
5590 else
5591 state.mstate <- Mzoom (n, i+1);
5593 else state.mstate <- Mzoom (n, 0)
5595 | _ -> state.mstate <- Mzoom (n, 0)
5597 else (
5598 match state.autoscroll with
5599 | Some step -> setautoscrollspeed step (n=4)
5600 | None ->
5601 let incr =
5602 if n = 4
5603 then -conf.scrollstep
5604 else conf.scrollstep
5606 let incr = incr * 2 in
5607 let y = clamp incr in
5608 gotoy_and_clear_text y
5611 | n when (n = 6 || n = 7) && not down && canpan () ->
5612 state.x <- state.x + (if n = 7 then -2 else 2) * conf.hscrollstep;
5613 gotoy_and_clear_text state.y
5615 | 1 when Wsi.withctrl mask ->
5616 if down
5617 then (
5618 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5619 state.mstate <- Mpan (x, y)
5621 else
5622 state.mstate <- Mnone
5624 | 3 ->
5625 if down
5626 then (
5627 Wsi.setcursor Wsi.CURSOR_CYCLE;
5628 let p = (x, y) in
5629 state.mstate <- Mzoomrect (p, p)
5631 else (
5632 match state.mstate with
5633 | Mzoomrect ((x0, y0), _) ->
5634 if abs (x-x0) > 10 && abs (y - y0) > 10
5635 then zoomrect x0 y0 x y
5636 else (
5637 state.mstate <- Mnone;
5638 Wsi.setcursor Wsi.CURSOR_INHERIT;
5639 G.postRedisplay "kill accidental zoom rect";
5641 | _ ->
5642 Wsi.setcursor Wsi.CURSOR_INHERIT;
5643 state.mstate <- Mnone
5646 | 1 when x > conf.winw - state.scrollw ->
5647 if down
5648 then
5649 let _, position, sh = state.uioh#scrollph in
5650 if y > truncate position && y < truncate (position +. sh)
5651 then state.mstate <- Mscrolly
5652 else scrolly y
5653 else
5654 state.mstate <- Mnone
5656 | 1 when y > conf.winh - state.hscrollh ->
5657 if down
5658 then
5659 let _, position, sw = state.uioh#scrollpw in
5660 if x > truncate position && x < truncate (position +. sw)
5661 then state.mstate <- Mscrollx
5662 else scrollx x
5663 else
5664 state.mstate <- Mnone
5666 | 1 ->
5667 let dest = if down then getunder x y else Unone in
5668 begin match dest with
5669 | Ulinkgoto _
5670 | Ulinkuri _
5671 | Uremote _
5672 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5673 gotounder dest
5675 | Unone when down ->
5676 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5677 state.mstate <- Mpan (x, y);
5679 | Unone | Utext _ ->
5680 if down
5681 then (
5682 if conf.angle mod 360 = 0
5683 then (
5684 state.mstate <- Msel ((x, y), (x, y));
5685 G.postRedisplay "mouse select";
5688 else (
5689 match state.mstate with
5690 | Mnone -> ()
5692 | Mzoom _ | Mscrollx | Mscrolly ->
5693 state.mstate <- Mnone
5695 | Mzoomrect ((x0, y0), _) ->
5696 zoomrect x0 y0 x y
5698 | Mpan _ ->
5699 Wsi.setcursor Wsi.CURSOR_INHERIT;
5700 state.mstate <- Mnone
5702 | Msel ((_, y0), (_, y1)) ->
5703 let rec loop = function
5704 | [] -> ()
5705 | l :: rest ->
5706 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5707 || ((y1 >= l.pagedispy
5708 && y1 <= (l.pagedispy + l.pagevh)))
5709 then
5710 match getopaque l.pageno with
5711 | Some opaque ->
5712 begin
5713 match Ne.pipe () with
5714 | Ne.Exn exn ->
5715 showtext '!'
5716 (Printf.sprintf
5717 "can not create sel pipe: %s"
5718 (Printexc.to_string exn));
5719 | Ne.Res (r, w) ->
5720 let doclose what fd =
5721 Ne.clo fd (fun msg ->
5722 dolog "%s close failed: %s" what msg)
5725 popen conf.selcmd [r, 0; w, -1];
5726 copysel w opaque;
5727 doclose "pipe/r" r;
5728 G.postRedisplay "copysel";
5729 with exn ->
5730 dolog "can not execute %S: %s"
5731 conf.selcmd (Printexc.to_string exn);
5732 doclose "pipe/r" r;
5733 doclose "pipe/w" w;
5735 | None -> ()
5736 else loop rest
5738 loop state.layout;
5739 Wsi.setcursor Wsi.CURSOR_INHERIT;
5740 state.mstate <- Mnone;
5744 | _ -> ()
5747 let birdseyemouse button down x y mask
5748 (conf, leftx, _, hooverpageno, anchor) =
5749 match button with
5750 | 1 when down ->
5751 let rec loop = function
5752 | [] -> ()
5753 | l :: rest ->
5754 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5755 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5756 then (
5757 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5759 else loop rest
5761 loop state.layout
5762 | 3 -> ()
5763 | _ -> viewmouse button down x y mask
5766 let mouse button down x y mask =
5767 state.uioh <- state.uioh#button button down x y mask;
5770 let motion ~x ~y =
5771 state.uioh <- state.uioh#motion x y
5774 let pmotion ~x ~y =
5775 state.uioh <- state.uioh#pmotion x y;
5778 let uioh = object
5779 method display = ()
5781 method key key mask =
5782 begin match state.mode with
5783 | Textentry textentry -> textentrykeyboard key mask textentry
5784 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5785 | View -> viewkeyboard key mask
5786 | LinkNav linknav -> linknavkeyboard key mask linknav
5787 end;
5788 state.uioh
5790 method button button bstate x y mask =
5791 begin match state.mode with
5792 | LinkNav _
5793 | View -> viewmouse button bstate x y mask
5794 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5795 | Textentry _ -> ()
5796 end;
5797 state.uioh
5799 method motion x y =
5800 begin match state.mode with
5801 | Textentry _ -> ()
5802 | View | Birdseye _ | LinkNav _ ->
5803 match state.mstate with
5804 | Mzoom _ | Mnone -> ()
5806 | Mpan (x0, y0) ->
5807 let dx = x - x0
5808 and dy = y0 - y in
5809 state.mstate <- Mpan (x, y);
5810 if canpan ()
5811 then state.x <- state.x + dx;
5812 let y = clamp dy in
5813 gotoy_and_clear_text y
5815 | Msel (a, _) ->
5816 state.mstate <- Msel (a, (x, y));
5817 G.postRedisplay "motion select";
5819 | Mscrolly ->
5820 let y = min conf.winh (max 0 y) in
5821 scrolly y
5823 | Mscrollx ->
5824 let x = min conf.winw (max 0 x) in
5825 scrollx x
5827 | Mzoomrect (p0, _) ->
5828 state.mstate <- Mzoomrect (p0, (x, y));
5829 G.postRedisplay "motion zoomrect";
5830 end;
5831 state.uioh
5833 method pmotion x y =
5834 begin match state.mode with
5835 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5836 let rec loop = function
5837 | [] ->
5838 if hooverpageno != -1
5839 then (
5840 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5841 G.postRedisplay "pmotion birdseye no hoover";
5843 | l :: rest ->
5844 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5845 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5846 then (
5847 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5848 G.postRedisplay "pmotion birdseye hoover";
5850 else loop rest
5852 loop state.layout
5854 | Textentry _ -> ()
5856 | LinkNav _
5857 | View ->
5858 match state.mstate with
5859 | Mnone -> updateunder x y
5860 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5862 end;
5863 state.uioh
5865 method infochanged _ = ()
5867 method scrollph =
5868 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5869 let p, h = scrollph state.y maxy in
5870 state.scrollw, p, h
5872 method scrollpw =
5873 let winw = conf.winw - state.scrollw - 1 in
5874 let fwinw = float winw in
5875 let sw =
5876 let sw = fwinw /. float state.w in
5877 let sw = fwinw *. sw in
5878 max sw (float conf.scrollh)
5880 let position, sw =
5881 let f = state.w+winw in
5882 let r = float (winw-state.x) /. float f in
5883 let p = fwinw *. r in
5884 p-.sw/.2., sw
5886 let sw =
5887 if position +. sw > fwinw
5888 then fwinw -. position
5889 else sw
5891 state.hscrollh, position, sw
5893 method modehash =
5894 let modename =
5895 match state.mode with
5896 | LinkNav _ -> "links"
5897 | Textentry _ -> "textentry"
5898 | Birdseye _ -> "birdseye"
5899 | View -> "view"
5901 findkeyhash conf modename
5902 end;;
5904 module Config =
5905 struct
5906 open Parser
5908 let fontpath = ref "";;
5910 module KeyMap =
5911 Map.Make (struct type t = (int * int) let compare = compare end);;
5913 let unent s =
5914 let l = String.length s in
5915 let b = Buffer.create l in
5916 unent b s 0 l;
5917 Buffer.contents b;
5920 let home =
5921 try Sys.getenv "HOME"
5922 with exn ->
5923 prerr_endline
5924 ("Can not determine home directory location: " ^
5925 Printexc.to_string exn);
5929 let modifier_of_string = function
5930 | "alt" -> Wsi.altmask
5931 | "shift" -> Wsi.shiftmask
5932 | "ctrl" | "control" -> Wsi.ctrlmask
5933 | "meta" -> Wsi.metamask
5934 | _ -> 0
5937 let key_of_string =
5938 let r = Str.regexp "-" in
5939 fun s ->
5940 let elems = Str.full_split r s in
5941 let f n k m =
5942 let g s =
5943 let m1 = modifier_of_string s in
5944 if m1 = 0
5945 then (Wsi.namekey s, m)
5946 else (k, m lor m1)
5947 in function
5948 | Str.Delim s when n land 1 = 0 -> g s
5949 | Str.Text s -> g s
5950 | Str.Delim _ -> (k, m)
5952 let rec loop n k m = function
5953 | [] -> (k, m)
5954 | x :: xs ->
5955 let k, m = f n k m x in
5956 loop (n+1) k m xs
5958 loop 0 0 0 elems
5961 let keys_of_string =
5962 let r = Str.regexp "[ \t]" in
5963 fun s ->
5964 let elems = Str.split r s in
5965 List.map key_of_string elems
5968 let copykeyhashes c =
5969 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5972 let config_of c attrs =
5973 let apply c k v =
5975 match k with
5976 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5977 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5978 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5979 | "preload" -> { c with preload = bool_of_string v }
5980 | "page-bias" -> { c with pagebias = int_of_string v }
5981 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5982 | "horizontal-scroll-step" ->
5983 { c with hscrollstep = max (int_of_string v) 1 }
5984 | "auto-scroll-step" ->
5985 { c with autoscrollstep = max 0 (int_of_string v) }
5986 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5987 | "crop-hack" -> { c with crophack = bool_of_string v }
5988 | "throttle" ->
5989 let mw =
5990 match String.lowercase v with
5991 | "true" -> Some infinity
5992 | "false" -> None
5993 | f -> Some (float_of_string f)
5995 { c with maxwait = mw}
5996 | "highlight-links" -> { c with hlinks = bool_of_string v }
5997 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5998 | "vertical-margin" ->
5999 { c with interpagespace = max 0 (int_of_string v) }
6000 | "zoom" ->
6001 let zoom = float_of_string v /. 100. in
6002 let zoom = max zoom 0.0 in
6003 { c with zoom = zoom }
6004 | "presentation" -> { c with presentation = bool_of_string v }
6005 | "rotation-angle" -> { c with angle = int_of_string v }
6006 | "width" -> { c with winw = max 20 (int_of_string v) }
6007 | "height" -> { c with winh = max 20 (int_of_string v) }
6008 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6009 | "proportional-display" -> { c with proportional = bool_of_string v }
6010 | "pixmap-cache-size" ->
6011 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6012 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6013 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6014 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6015 | "persistent-location" -> { c with jumpback = bool_of_string v }
6016 | "background-color" -> { c with bgcolor = color_of_string v }
6017 | "scrollbar-in-presentation" ->
6018 { c with scrollbarinpm = bool_of_string v }
6019 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6020 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6021 | "mupdf-store-size" ->
6022 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6023 | "checkers" -> { c with checkers = bool_of_string v }
6024 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6025 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6026 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6027 | "uri-launcher" -> { c with urilauncher = unent v }
6028 | "path-launcher" -> { c with pathlauncher = unent v }
6029 | "color-space" -> { c with colorspace = colorspace_of_string v }
6030 | "invert-colors" -> { c with invert = bool_of_string v }
6031 | "brightness" -> { c with colorscale = float_of_string v }
6032 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6033 | "ghyllscroll" ->
6034 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6035 | "columns" ->
6036 let (n, _, _) as nab = multicolumns_of_string v in
6037 if n < 0
6038 then { c with columns = Csplit (-n, [||]) }
6039 else { c with columns = Cmulti (nab, [||]) }
6040 | "birds-eye-columns" ->
6041 { c with beyecolumns = Some (max (int_of_string v) 2) }
6042 | "selection-command" -> { c with selcmd = unent v }
6043 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6044 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6045 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6046 | "multi-column-centering" -> { c with multicenter = bool_of_string v }
6047 | _ -> c
6048 with exn ->
6049 prerr_endline ("Error processing attribute (`" ^
6050 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
6053 let rec fold c = function
6054 | [] -> c
6055 | (k, v) :: rest ->
6056 let c = apply c k v in
6057 fold c rest
6059 fold { c with keyhashes = copykeyhashes c } attrs;
6062 let fromstring f pos n v d =
6063 try f v
6064 with exn ->
6065 dolog "Error processing attribute (%S=%S) at %d\n%s"
6066 n v pos (Printexc.to_string exn)
6071 let bookmark_of attrs =
6072 let rec fold title page rely visy = function
6073 | ("title", v) :: rest -> fold v page rely visy rest
6074 | ("page", v) :: rest -> fold title v rely visy rest
6075 | ("rely", v) :: rest -> fold title page v visy rest
6076 | ("visy", v) :: rest -> fold title page rely v rest
6077 | _ :: rest -> fold title page rely visy rest
6078 | [] -> title, page, rely, visy
6080 fold "invalid" "0" "0" "0" attrs
6083 let doc_of attrs =
6084 let rec fold path page rely pan visy = function
6085 | ("path", v) :: rest -> fold v page rely pan visy rest
6086 | ("page", v) :: rest -> fold path v rely pan visy rest
6087 | ("rely", v) :: rest -> fold path page v pan visy rest
6088 | ("pan", v) :: rest -> fold path page rely v visy rest
6089 | ("visy", v) :: rest -> fold path page rely pan v rest
6090 | _ :: rest -> fold path page rely pan visy rest
6091 | [] -> path, page, rely, pan, visy
6093 fold "" "0" "0" "0" "0" attrs
6096 let map_of attrs =
6097 let rec fold rs ls = function
6098 | ("out", v) :: rest -> fold v ls rest
6099 | ("in", v) :: rest -> fold rs v rest
6100 | _ :: rest -> fold ls rs rest
6101 | [] -> ls, rs
6103 fold "" "" attrs
6106 let setconf dst src =
6107 dst.scrollbw <- src.scrollbw;
6108 dst.scrollh <- src.scrollh;
6109 dst.icase <- src.icase;
6110 dst.preload <- src.preload;
6111 dst.pagebias <- src.pagebias;
6112 dst.verbose <- src.verbose;
6113 dst.scrollstep <- src.scrollstep;
6114 dst.maxhfit <- src.maxhfit;
6115 dst.crophack <- src.crophack;
6116 dst.autoscrollstep <- src.autoscrollstep;
6117 dst.maxwait <- src.maxwait;
6118 dst.hlinks <- src.hlinks;
6119 dst.underinfo <- src.underinfo;
6120 dst.interpagespace <- src.interpagespace;
6121 dst.zoom <- src.zoom;
6122 dst.presentation <- src.presentation;
6123 dst.angle <- src.angle;
6124 dst.winw <- src.winw;
6125 dst.winh <- src.winh;
6126 dst.savebmarks <- src.savebmarks;
6127 dst.memlimit <- src.memlimit;
6128 dst.proportional <- src.proportional;
6129 dst.texcount <- src.texcount;
6130 dst.sliceheight <- src.sliceheight;
6131 dst.thumbw <- src.thumbw;
6132 dst.jumpback <- src.jumpback;
6133 dst.bgcolor <- src.bgcolor;
6134 dst.scrollbarinpm <- src.scrollbarinpm;
6135 dst.tilew <- src.tilew;
6136 dst.tileh <- src.tileh;
6137 dst.mustoresize <- src.mustoresize;
6138 dst.checkers <- src.checkers;
6139 dst.aalevel <- src.aalevel;
6140 dst.trimmargins <- src.trimmargins;
6141 dst.trimfuzz <- src.trimfuzz;
6142 dst.urilauncher <- src.urilauncher;
6143 dst.colorspace <- src.colorspace;
6144 dst.invert <- src.invert;
6145 dst.colorscale <- src.colorscale;
6146 dst.redirectstderr <- src.redirectstderr;
6147 dst.ghyllscroll <- src.ghyllscroll;
6148 dst.columns <- src.columns;
6149 dst.beyecolumns <- src.beyecolumns;
6150 dst.selcmd <- src.selcmd;
6151 dst.updatecurs <- src.updatecurs;
6152 dst.pathlauncher <- src.pathlauncher;
6153 dst.keyhashes <- copykeyhashes src;
6154 dst.hfsize <- src.hfsize;
6155 dst.hscrollstep <- src.hscrollstep;
6156 dst.pgscale <- src.pgscale;
6157 dst.multicenter <- src.multicenter;
6160 let get s =
6161 let h = Hashtbl.create 10 in
6162 let dc = { defconf with angle = defconf.angle } in
6163 let rec toplevel v t spos _ =
6164 match t with
6165 | Vdata | Vcdata | Vend -> v
6166 | Vopen ("llppconfig", _, closed) ->
6167 if closed
6168 then v
6169 else { v with f = llppconfig }
6170 | Vopen _ ->
6171 error "unexpected subelement at top level" s spos
6172 | Vclose _ -> error "unexpected close at top level" s spos
6174 and llppconfig v t spos _ =
6175 match t with
6176 | Vdata | Vcdata -> v
6177 | Vend -> error "unexpected end of input in llppconfig" s spos
6178 | Vopen ("defaults", attrs, closed) ->
6179 let c = config_of dc attrs in
6180 setconf dc c;
6181 if closed
6182 then v
6183 else { v with f = defaults }
6185 | Vopen ("ui-font", attrs, closed) ->
6186 let rec getsize size = function
6187 | [] -> size
6188 | ("size", v) :: rest ->
6189 let size =
6190 fromstring int_of_string spos "size" v fstate.fontsize in
6191 getsize size rest
6192 | l -> getsize size l
6194 fstate.fontsize <- getsize fstate.fontsize attrs;
6195 if closed
6196 then v
6197 else { v with f = uifont (Buffer.create 10) }
6199 | Vopen ("doc", attrs, closed) ->
6200 let pathent, spage, srely, span, svisy = doc_of attrs in
6201 let path = unent pathent
6202 and pageno = fromstring int_of_string spos "page" spage 0
6203 and rely = fromstring float_of_string spos "rely" srely 0.0
6204 and pan = fromstring int_of_string spos "pan" span 0
6205 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6206 let c = config_of dc attrs in
6207 let anchor = (pageno, rely, visy) in
6208 if closed
6209 then (Hashtbl.add h path (c, [], pan, anchor); v)
6210 else { v with f = doc path pan anchor c [] }
6212 | Vopen _ ->
6213 error "unexpected subelement in llppconfig" s spos
6215 | Vclose "llppconfig" -> { v with f = toplevel }
6216 | Vclose _ -> error "unexpected close in llppconfig" s spos
6218 and defaults v t spos _ =
6219 match t with
6220 | Vdata | Vcdata -> v
6221 | Vend -> error "unexpected end of input in defaults" s spos
6222 | Vopen ("keymap", attrs, closed) ->
6223 let modename =
6224 try List.assoc "mode" attrs
6225 with Not_found -> "global" in
6226 if closed
6227 then v
6228 else
6229 let ret keymap =
6230 let h = findkeyhash dc modename in
6231 KeyMap.iter (Hashtbl.replace h) keymap;
6232 defaults
6234 { v with f = pkeymap ret KeyMap.empty }
6236 | Vopen (_, _, _) ->
6237 error "unexpected subelement in defaults" s spos
6239 | Vclose "defaults" ->
6240 { v with f = llppconfig }
6242 | Vclose _ -> error "unexpected close in defaults" s spos
6244 and uifont b v t spos epos =
6245 match t with
6246 | Vdata | Vcdata ->
6247 Buffer.add_substring b s spos (epos - spos);
6249 | Vopen (_, _, _) ->
6250 error "unexpected subelement in ui-font" s spos
6251 | Vclose "ui-font" ->
6252 if String.length !fontpath = 0
6253 then fontpath := Buffer.contents b;
6254 { v with f = llppconfig }
6255 | Vclose _ -> error "unexpected close in ui-font" s spos
6256 | Vend -> error "unexpected end of input in ui-font" s spos
6258 and doc path pan anchor c bookmarks v t spos _ =
6259 match t with
6260 | Vdata | Vcdata -> v
6261 | Vend -> error "unexpected end of input in doc" s spos
6262 | Vopen ("bookmarks", _, closed) ->
6263 if closed
6264 then v
6265 else { v with f = pbookmarks path pan anchor c bookmarks }
6267 | Vopen ("keymap", attrs, closed) ->
6268 let modename =
6269 try List.assoc "mode" attrs
6270 with Not_found -> "global"
6272 if closed
6273 then v
6274 else
6275 let ret keymap =
6276 let h = findkeyhash c modename in
6277 KeyMap.iter (Hashtbl.replace h) keymap;
6278 doc path pan anchor c bookmarks
6280 { v with f = pkeymap ret KeyMap.empty }
6282 | Vopen (_, _, _) ->
6283 error "unexpected subelement in doc" s spos
6285 | Vclose "doc" ->
6286 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6287 { v with f = llppconfig }
6289 | Vclose _ -> error "unexpected close in doc" s spos
6291 and pkeymap ret keymap v t spos _ =
6292 match t with
6293 | Vdata | Vcdata -> v
6294 | Vend -> error "unexpected end of input in keymap" s spos
6295 | Vopen ("map", attrs, closed) ->
6296 let r, l = map_of attrs in
6297 let kss = fromstring keys_of_string spos "in" r [] in
6298 let lss = fromstring keys_of_string spos "out" l [] in
6299 let keymap =
6300 match kss with
6301 | [] -> keymap
6302 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6303 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6305 if closed
6306 then { v with f = pkeymap ret keymap }
6307 else
6308 let f () = v in
6309 { v with f = skip "map" f }
6311 | Vopen _ ->
6312 error "unexpected subelement in keymap" s spos
6314 | Vclose "keymap" ->
6315 { v with f = ret keymap }
6317 | Vclose _ -> error "unexpected close in keymap" s spos
6319 and pbookmarks path pan anchor c bookmarks v t spos _ =
6320 match t with
6321 | Vdata | Vcdata -> v
6322 | Vend -> error "unexpected end of input in bookmarks" s spos
6323 | Vopen ("item", attrs, closed) ->
6324 let titleent, spage, srely, svisy = bookmark_of attrs in
6325 let page = fromstring int_of_string spos "page" spage 0
6326 and rely = fromstring float_of_string spos "rely" srely 0.0
6327 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6328 let bookmarks =
6329 (unent titleent, 0, (page, rely, visy)) :: bookmarks
6331 if closed
6332 then { v with f = pbookmarks path pan anchor c bookmarks }
6333 else
6334 let f () = v in
6335 { v with f = skip "item" f }
6337 | Vopen _ ->
6338 error "unexpected subelement in bookmarks" s spos
6340 | Vclose "bookmarks" ->
6341 { v with f = doc path pan anchor c bookmarks }
6343 | Vclose _ -> error "unexpected close in bookmarks" s spos
6345 and skip tag f v t spos _ =
6346 match t with
6347 | Vdata | Vcdata -> v
6348 | Vend ->
6349 error ("unexpected end of input in skipped " ^ tag) s spos
6350 | Vopen (tag', _, closed) ->
6351 if closed
6352 then v
6353 else
6354 let f' () = { v with f = skip tag f } in
6355 { v with f = skip tag' f' }
6356 | Vclose ctag ->
6357 if tag = ctag
6358 then f ()
6359 else error ("unexpected close in skipped " ^ tag) s spos
6362 parse { f = toplevel; accu = () } s;
6363 h, dc;
6366 let do_load f ic =
6368 let len = in_channel_length ic in
6369 let s = String.create len in
6370 really_input ic s 0 len;
6371 f s;
6372 with
6373 | Parse_error (msg, s, pos) ->
6374 let subs = subs s pos in
6375 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
6376 failwith ("parse error: " ^ s)
6378 | exn ->
6379 failwith ("config load error: " ^ Printexc.to_string exn)
6382 let defconfpath =
6383 let dir =
6385 let dir = Filename.concat home ".config" in
6386 if Sys.is_directory dir then dir else home
6387 with _ -> home
6389 Filename.concat dir "llpp.conf"
6392 let confpath = ref defconfpath;;
6394 let load1 f =
6395 if Sys.file_exists !confpath
6396 then
6397 match
6398 (try Some (open_in_bin !confpath)
6399 with exn ->
6400 prerr_endline
6401 ("Error opening configuation file `" ^ !confpath ^ "': " ^
6402 Printexc.to_string exn);
6403 None
6405 with
6406 | Some ic ->
6407 let success =
6409 f (do_load get ic)
6410 with exn ->
6411 prerr_endline
6412 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6413 Printexc.to_string exn);
6414 false
6416 close_in ic;
6417 success
6419 | None -> false
6420 else
6421 f (Hashtbl.create 0, defconf)
6424 let load () =
6425 let f (h, dc) =
6426 let pc, pb, px, pa =
6428 Hashtbl.find h (Filename.basename state.path)
6429 with Not_found -> dc, [], 0, emptyanchor
6431 setconf defconf dc;
6432 setconf conf pc;
6433 state.bookmarks <- pb;
6434 state.x <- px;
6435 state.scrollw <- conf.scrollbw;
6436 if conf.jumpback
6437 then state.anchor <- pa;
6438 cbput state.hists.nav pa;
6439 true
6441 load1 f
6444 let add_attrs bb always dc c =
6445 let ob s a b =
6446 if always || a != b
6447 then Printf.bprintf bb "\n %s='%b'" s a
6448 and oi s a b =
6449 if always || a != b
6450 then Printf.bprintf bb "\n %s='%d'" s a
6451 and oI s a b =
6452 if always || a != b
6453 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6454 and oz s a b =
6455 if always || a <> b
6456 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
6457 and oF s a b =
6458 if always || a <> b
6459 then Printf.bprintf bb "\n %s='%f'" s a
6460 and oc s a b =
6461 if always || a <> b
6462 then
6463 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6464 and oC s a b =
6465 if always || a <> b
6466 then
6467 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6468 and oR s a b =
6469 if always || a <> b
6470 then
6471 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6472 and os s a b =
6473 if always || a <> b
6474 then
6475 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6476 and og s a b =
6477 if always || a <> b
6478 then
6479 match a with
6480 | None -> ()
6481 | Some (_N, _A, _B) ->
6482 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6483 and oW s a b =
6484 if always || a <> b
6485 then
6486 let v =
6487 match a with
6488 | None -> "false"
6489 | Some f ->
6490 if f = infinity
6491 then "true"
6492 else string_of_float f
6494 Printf.bprintf bb "\n %s='%s'" s v
6495 and oco s a b =
6496 if always || a <> b
6497 then
6498 match a with
6499 | Cmulti ((n, a, b), _) when n > 1 ->
6500 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6501 | Csplit (n, _) when n > 1 ->
6502 Printf.bprintf bb "\n %s='%d'" s ~-n
6503 | _ -> ()
6504 and obeco s a b =
6505 if always || a <> b
6506 then
6507 match a with
6508 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6509 | _ -> ()
6511 let w, h =
6512 if always
6513 then dc.winw, dc.winh
6514 else
6515 match state.fullscreen with
6516 | Some wh -> wh
6517 | None -> c.winw, c.winh
6519 oi "width" w dc.winw;
6520 oi "height" h dc.winh;
6521 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6522 oi "scroll-handle-height" c.scrollh dc.scrollh;
6523 ob "case-insensitive-search" c.icase dc.icase;
6524 ob "preload" c.preload dc.preload;
6525 oi "page-bias" c.pagebias dc.pagebias;
6526 oi "scroll-step" c.scrollstep dc.scrollstep;
6527 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6528 ob "max-height-fit" c.maxhfit dc.maxhfit;
6529 ob "crop-hack" c.crophack dc.crophack;
6530 oW "throttle" c.maxwait dc.maxwait;
6531 ob "highlight-links" c.hlinks dc.hlinks;
6532 ob "under-cursor-info" c.underinfo dc.underinfo;
6533 oi "vertical-margin" c.interpagespace dc.interpagespace;
6534 oz "zoom" c.zoom dc.zoom;
6535 ob "presentation" c.presentation dc.presentation;
6536 oi "rotation-angle" c.angle dc.angle;
6537 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6538 ob "proportional-display" c.proportional dc.proportional;
6539 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6540 oi "tex-count" c.texcount dc.texcount;
6541 oi "slice-height" c.sliceheight dc.sliceheight;
6542 oi "thumbnail-width" c.thumbw dc.thumbw;
6543 ob "persistent-location" c.jumpback dc.jumpback;
6544 oc "background-color" c.bgcolor dc.bgcolor;
6545 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6546 oi "tile-width" c.tilew dc.tilew;
6547 oi "tile-height" c.tileh dc.tileh;
6548 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6549 ob "checkers" c.checkers dc.checkers;
6550 oi "aalevel" c.aalevel dc.aalevel;
6551 ob "trim-margins" c.trimmargins dc.trimmargins;
6552 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6553 os "uri-launcher" c.urilauncher dc.urilauncher;
6554 os "path-launcher" c.pathlauncher dc.pathlauncher;
6555 oC "color-space" c.colorspace dc.colorspace;
6556 ob "invert-colors" c.invert dc.invert;
6557 oF "brightness" c.colorscale dc.colorscale;
6558 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6559 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6560 oco "columns" c.columns dc.columns;
6561 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6562 os "selection-command" c.selcmd dc.selcmd;
6563 ob "update-cursor" c.updatecurs dc.updatecurs;
6564 oi "hint-font-size" c.hfsize dc.hfsize;
6565 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
6566 oF "page-scroll-scale" c.pgscale dc.pgscale;
6567 ob "multi-column-centering" c.multicenter dc.multicenter;
6570 let keymapsbuf always dc c =
6571 let bb = Buffer.create 16 in
6572 let rec loop = function
6573 | [] -> ()
6574 | (modename, h) :: rest ->
6575 let dh = findkeyhash dc modename in
6576 if always || h <> dh
6577 then (
6578 if Hashtbl.length h > 0
6579 then (
6580 if Buffer.length bb > 0
6581 then Buffer.add_char bb '\n';
6582 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6583 Hashtbl.iter (fun i o ->
6584 let isdifferent = always ||
6586 let dO = Hashtbl.find dh i in
6587 dO <> o
6588 with Not_found -> true
6590 if isdifferent
6591 then
6592 let addkm (k, m) =
6593 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6594 if Wsi.withalt m then Buffer.add_string bb "alt-";
6595 if Wsi.withshift m then Buffer.add_string bb "shift-";
6596 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6597 Buffer.add_string bb (Wsi.keyname k);
6599 let addkms l =
6600 let rec loop = function
6601 | [] -> ()
6602 | km :: [] -> addkm km
6603 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6605 loop l
6607 Buffer.add_string bb "<map in='";
6608 addkm i;
6609 match o with
6610 | KMinsrt km ->
6611 Buffer.add_string bb "' out='";
6612 addkm km;
6613 Buffer.add_string bb "'/>\n"
6615 | KMinsrl kms ->
6616 Buffer.add_string bb "' out='";
6617 addkms kms;
6618 Buffer.add_string bb "'/>\n"
6620 | KMmulti (ins, kms) ->
6621 Buffer.add_char bb ' ';
6622 addkms ins;
6623 Buffer.add_string bb "' out='";
6624 addkms kms;
6625 Buffer.add_string bb "'/>\n"
6626 ) h;
6627 Buffer.add_string bb "</keymap>";
6630 loop rest
6632 loop c.keyhashes;
6636 let save () =
6637 let uifontsize = fstate.fontsize in
6638 let bb = Buffer.create 32768 in
6639 let f (h, dc) =
6640 let dc = if conf.bedefault then conf else dc in
6641 Buffer.add_string bb "<llppconfig>\n";
6643 if String.length !fontpath > 0
6644 then
6645 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6646 uifontsize
6647 !fontpath
6648 else (
6649 if uifontsize <> 14
6650 then
6651 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6654 Buffer.add_string bb "<defaults ";
6655 add_attrs bb true dc dc;
6656 let kb = keymapsbuf true dc dc in
6657 if Buffer.length kb > 0
6658 then (
6659 Buffer.add_string bb ">\n";
6660 Buffer.add_buffer bb kb;
6661 Buffer.add_string bb "\n</defaults>\n";
6663 else Buffer.add_string bb "/>\n";
6665 let adddoc path pan anchor c bookmarks =
6666 if bookmarks == [] && c = dc && anchor = emptyanchor
6667 then ()
6668 else (
6669 Printf.bprintf bb "<doc path='%s'"
6670 (enent path 0 (String.length path));
6672 if anchor <> emptyanchor
6673 then (
6674 let n, rely, visy = anchor in
6675 Printf.bprintf bb " page='%d'" n;
6676 if rely > 1e-6
6677 then
6678 Printf.bprintf bb " rely='%f'" rely
6680 if abs_float visy > 1e-6
6681 then
6682 Printf.bprintf bb " visy='%f'" visy
6686 if pan != 0
6687 then Printf.bprintf bb " pan='%d'" pan;
6689 add_attrs bb false dc c;
6690 let kb = keymapsbuf false dc c in
6692 begin match bookmarks with
6693 | [] ->
6694 if Buffer.length kb > 0
6695 then (
6696 Buffer.add_string bb ">\n";
6697 Buffer.add_buffer bb kb;
6698 Buffer.add_string bb "\n</doc>\n";
6700 else Buffer.add_string bb "/>\n"
6701 | _ ->
6702 Buffer.add_string bb ">\n<bookmarks>\n";
6703 List.iter (fun (title, _level, (page, rely, visy)) ->
6704 Printf.bprintf bb
6705 "<item title='%s' page='%d'"
6706 (enent title 0 (String.length title))
6707 page
6709 if rely > 1e-6
6710 then
6711 Printf.bprintf bb " rely='%f'" rely
6713 if abs_float visy > 1e-6
6714 then
6715 Printf.bprintf bb " visy='%f'" visy
6717 Buffer.add_string bb "/>\n";
6718 ) bookmarks;
6719 Buffer.add_string bb "</bookmarks>";
6720 if Buffer.length kb > 0
6721 then (
6722 Buffer.add_string bb "\n";
6723 Buffer.add_buffer bb kb;
6725 Buffer.add_string bb "\n</doc>\n";
6726 end;
6730 let pan, conf =
6731 match state.mode with
6732 | Birdseye (c, pan, _, _, _) ->
6733 let beyecolumns =
6734 match conf.columns with
6735 | Cmulti ((c, _, _), _) -> Some c
6736 | Csingle _ -> None
6737 | Csplit _ -> None
6738 and columns =
6739 match c.columns with
6740 | Cmulti (c, _) -> Cmulti (c, [||])
6741 | Csingle _ -> Csingle [||]
6742 | Csplit _ -> failwith "quit from bird's eye while split"
6744 pan, { c with beyecolumns = beyecolumns; columns = columns }
6745 | _ -> state.x, conf
6747 let basename = Filename.basename state.path in
6748 adddoc basename pan (getanchor ())
6749 (let conf =
6750 let autoscrollstep =
6751 match state.autoscroll with
6752 | Some step -> step
6753 | None -> conf.autoscrollstep
6755 match state.mode with
6756 | Birdseye (bc, _, _, _, _) ->
6757 { conf with
6758 zoom = bc.zoom;
6759 presentation = bc.presentation;
6760 interpagespace = bc.interpagespace;
6761 maxwait = bc.maxwait;
6762 autoscrollstep = autoscrollstep }
6763 | _ -> { conf with autoscrollstep = autoscrollstep }
6764 in conf)
6765 (if conf.savebmarks then state.bookmarks else []);
6767 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
6768 if basename <> path
6769 then adddoc path x anchor c bookmarks
6770 ) h;
6771 Buffer.add_string bb "</llppconfig>";
6772 true;
6774 if load1 f && Buffer.length bb > 0
6775 then
6777 let tmp = !confpath ^ ".tmp" in
6778 let oc = open_out_bin tmp in
6779 Buffer.output_buffer oc bb;
6780 close_out oc;
6781 Unix.rename tmp !confpath;
6782 with exn ->
6783 prerr_endline
6784 ("error while saving configuration: " ^ Printexc.to_string exn)
6786 end;;
6788 let () =
6789 let trimcachepath = ref "" in
6790 Arg.parse
6791 (Arg.align
6792 [("-p", Arg.String (fun s -> state.password <- s) ,
6793 "<password> Set password");
6795 ("-f", Arg.String (fun s -> Config.fontpath := s),
6796 "<path> Set path to the user interface font");
6798 ("-c", Arg.String (fun s -> Config.confpath := s),
6799 "<path> Set path to the configuration file");
6801 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6802 "<path> Set path to the trim cache file");
6804 ("-v", Arg.Unit (fun () ->
6805 Printf.printf
6806 "%s\nconfiguration path: %s\n"
6807 (version ())
6808 Config.defconfpath
6810 exit 0), " Print version and exit");
6813 (fun s -> state.path <- s)
6814 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6816 if String.length state.path = 0
6817 then (prerr_endline "file name missing"; exit 1);
6819 if not (Config.load ())
6820 then prerr_endline "failed to load configuration";
6822 let globalkeyhash = findkeyhash conf "global" in
6823 let wsfd, winw, winh = Wsi.init (object
6824 method expose =
6825 if nogeomcmds state.geomcmds || platform == Posx
6826 then display ()
6827 else (
6828 GlClear.color (scalecolor2 conf.bgcolor);
6829 GlClear.clear [`color];
6831 method display = display ()
6832 method reshape w h = reshape w h
6833 method mouse b d x y m = mouse b d x y m
6834 method motion x y = state.mpos <- (x, y); motion x y
6835 method pmotion x y = state.mpos <- (x, y); pmotion x y
6836 method key k m =
6837 let mascm = m land (
6838 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6839 ) in
6840 match state.keystate with
6841 | KSnone ->
6842 let km = k, mascm in
6843 begin
6844 match
6845 let modehash = state.uioh#modehash in
6846 try Hashtbl.find modehash km
6847 with Not_found ->
6848 try Hashtbl.find globalkeyhash km
6849 with Not_found -> KMinsrt (k, m)
6850 with
6851 | KMinsrt (k, m) -> keyboard k m
6852 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6853 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6855 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6856 List.iter (fun (k, m) -> keyboard k m) insrt;
6857 state.keystate <- KSnone
6858 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6859 state.keystate <- KSinto (keys, insrt)
6860 | _ ->
6861 state.keystate <- KSnone
6863 method enter x y = state.mpos <- (x, y); pmotion x y
6864 method leave = state.mpos <- (-1, -1)
6865 method quit = raise Quit
6866 end) conf.winw conf.winh (platform = Posx) in
6868 state.wsfd <- wsfd;
6870 if not (
6871 List.exists GlMisc.check_extension
6872 [ "GL_ARB_texture_rectangle"
6873 ; "GL_EXT_texture_recangle"
6874 ; "GL_NV_texture_rectangle" ]
6876 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6878 let cr, sw =
6879 match Ne.pipe () with
6880 | Ne.Exn exn ->
6881 Printf.eprintf "pipe/crsw failed: %s" (Printexc.to_string exn);
6882 exit 1
6883 | Ne.Res rw -> rw
6884 and sr, cw =
6885 match Ne.pipe () with
6886 | Ne.Exn exn ->
6887 Printf.eprintf "pipe/srcw failed: %s" (Printexc.to_string exn);
6888 exit 1
6889 | Ne.Res rw -> rw
6892 cloexec cr;
6893 cloexec sw;
6894 cloexec sr;
6895 cloexec cw;
6897 setcheckers conf.checkers;
6898 redirectstderr ();
6900 init (cr, cw) (
6901 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6902 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6903 !Config.fontpath, !trimcachepath
6905 state.sr <- sr;
6906 state.sw <- sw;
6907 state.text <- "Opening " ^ state.path;
6908 reshape winw winh;
6909 opendoc state.path state.password;
6910 state.uioh <- uioh;
6912 let rec loop deadline =
6913 let r =
6914 match state.errfd with
6915 | None -> [state.sr; state.wsfd]
6916 | Some fd -> [state.sr; state.wsfd; fd]
6918 if state.redisplay
6919 then (
6920 state.redisplay <- false;
6921 display ();
6923 let timeout =
6924 let now = now () in
6925 if deadline > now
6926 then (
6927 if deadline = infinity
6928 then ~-.1.0
6929 else max 0.0 (deadline -. now)
6931 else 0.0
6933 let r, _, _ =
6934 try Unix.select r [] [] timeout
6935 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6937 begin match r with
6938 | [] ->
6939 state.ghyll None;
6940 let newdeadline =
6941 if state.ghyll == noghyll
6942 then
6943 match state.autoscroll with
6944 | Some step when step != 0 ->
6945 let y = state.y + step in
6946 let y =
6947 if y < 0
6948 then state.maxy
6949 else if y >= state.maxy then 0 else y
6951 gotoy y;
6952 if state.mode = View
6953 then state.text <- "";
6954 deadline +. 0.01
6955 | _ -> infinity
6956 else deadline +. 0.01
6958 loop newdeadline
6960 | l ->
6961 let rec checkfds = function
6962 | [] -> ()
6963 | fd :: rest when fd = state.sr ->
6964 let cmd = readcmd state.sr in
6965 act cmd;
6966 checkfds rest
6968 | fd :: rest when fd = state.wsfd ->
6969 Wsi.readresp fd;
6970 checkfds rest
6972 | fd :: rest ->
6973 let s = String.create 80 in
6974 let n = Unix.read fd s 0 80 in
6975 if conf.redirectstderr
6976 then (
6977 Buffer.add_substring state.errmsgs s 0 n;
6978 state.newerrmsgs <- true;
6979 state.redisplay <- true;
6981 else (
6982 prerr_string (String.sub s 0 n);
6983 flush stderr;
6985 checkfds rest
6987 checkfds l;
6988 let newdeadline =
6989 let deadline1 =
6990 if deadline = infinity
6991 then now () +. 0.01
6992 else deadline
6994 match state.autoscroll with
6995 | Some step when step != 0 -> deadline1
6996 | _ -> if state.ghyll == noghyll then infinity else deadline1
6998 loop newdeadline
6999 end;
7002 loop infinity;
7003 with Quit ->
7004 Config.save ();