Avoid possible confusion
[llpp.git] / main.ml
blobdf2c0d9f2a175af3c74cf717213705c7b83c489e
1 open Utils;;
2 open Config;;
4 exception Quit;;
6 type pipe = (Unix.file_descr * Unix.file_descr);;
8 external init : pipe -> params -> unit = "ml_init";;
9 external seltext : opaque -> (int * int * int * int) -> unit = "ml_seltext";;
10 external hassel : opaque -> bool = "ml_hassel";;
11 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
12 external getpdimrect : int -> float array = "ml_getpdimrect";;
13 external whatsunder : opaque -> int -> int -> under = "ml_whatsunder";;
14 external markunder : opaque -> int -> int -> mark -> bool = "ml_markunder";;
15 external clearmark : opaque -> unit = "ml_clearmark";;
16 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
17 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
18 external measurestr : int -> string -> float = "ml_measure_string";;
19 external postprocess :
20 opaque -> int -> int -> int -> (int * string * int) -> int
21 = "ml_postprocess";;
22 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
23 external setaalevel : int -> unit = "ml_setaalevel";;
24 external realloctexts : int -> bool = "ml_realloctexts";;
25 external findlink : opaque -> linkdir -> link = "ml_findlink";;
26 external getlink : opaque -> int -> under = "ml_getlink";;
27 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
28 external getlinkcount : opaque -> int = "ml_getlinkcount";;
29 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links"
30 external getpbo : width -> height -> colorspace -> opaque = "ml_getpbo";;
31 external freepbo : opaque -> unit = "ml_freepbo";;
32 external unmappbo : opaque -> unit = "ml_unmappbo";;
33 external pbousable : unit -> bool = "ml_pbo_usable";;
34 external unproject : opaque -> int -> int -> (int * int) option
35 = "ml_unproject";;
36 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
37 external rectofblock : opaque -> int -> int -> float array option
38 = "ml_rectofblock";;
39 external begintiles : unit -> unit = "ml_begintiles";;
40 external endtiles : unit -> unit = "ml_endtiles";;
42 let selfexec = ref "";;
44 let drawstring size x y s =
45 Gl.enable `blend;
46 Gl.enable `texture_2d;
47 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
48 ignore (drawstr size x y s);
49 Gl.disable `blend;
50 Gl.disable `texture_2d;
53 let drawstring1 size x y s =
54 drawstr size x y s;
57 let drawstring2 size x y fmt =
58 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
61 let debugl l =
62 dolog "l %d dim=%d {" l.pageno l.pagedimno;
63 dolog " WxH %dx%d" l.pagew l.pageh;
64 dolog " vWxH %dx%d" l.pagevw l.pagevh;
65 dolog " pagex,y %d,%d" l.pagex l.pagey;
66 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
67 dolog " column %d" l.pagecol;
68 dolog "}";
71 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
72 dolog "rect {";
73 dolog " x0,y0=(% f, % f)" x0 y0;
74 dolog " x1,y1=(% f, % f)" x1 y1;
75 dolog " x2,y2=(% f, % f)" x2 y2;
76 dolog " x3,y3=(% f, % f)" x3 y3;
77 dolog "}";
80 let isbirdseye = function Birdseye _ -> true | _ -> false;;
81 let istextentry = function Textentry _ -> true | _ -> false;;
83 let wtmode = ref false;;
84 let cxack = ref false;;
86 let pgscale h = truncate (float h *. conf.pgscale);;
88 let hscrollh () =
89 if (conf.scrollb land scrollbhv = 0)
90 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
91 then 0
92 else conf.scrollbw
95 let vscrollw () =
96 if (conf.scrollb land scrollbvv = 0)
97 then 0
98 else conf.scrollbw
101 let wadjsb w = w - vscrollw ();;
103 let setfontsize n =
104 fstate.fontsize <- n;
105 fstate.wwidth <- measurestr fstate.fontsize "w";
106 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
109 let vlog fmt =
110 if conf.verbose
111 then
112 Printf.kprintf prerr_endline fmt
113 else
114 Printf.kprintf ignore fmt
117 let launchpath () =
118 if emptystr conf.pathlauncher
119 then print_endline state.path
120 else (
121 let re = Str.regexp "%s" in
122 let command = Str.global_replace re state.path conf.pathlauncher in
123 try popen command []
124 with exn ->
125 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
126 flush stderr;
130 module Ne = struct
131 type 'a t = | Res of 'a | Exn of exn;;
133 let res f =
134 try Res (f ())
135 with exn -> Exn exn
138 let clo fd f =
139 try tempfailureretry Unix.close fd
140 with exn -> f (exntos exn)
143 let dup fd =
144 try Res (tempfailureretry Unix.dup fd)
145 with exn -> Exn exn
148 let dup2 fd1 fd2 =
149 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
150 with exn -> Exn exn
152 end;;
154 let redirectstderr () =
155 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
156 if conf.redirectstderr
157 then
158 match Ne.res Unix.pipe with
159 | Ne.Exn exn ->
160 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
162 | Ne.Res (r, w) ->
163 begin match Ne.dup Unix.stderr with
164 | Ne.Exn exn ->
165 dolog "failed to dup stderr: %s" (exntos exn);
166 Ne.clo r (clofail "pipe/r");
167 Ne.clo w (clofail "pipe/w");
169 | Ne.Res dupstderr ->
170 begin match Ne.dup2 w Unix.stderr with
171 | Ne.Exn exn ->
172 dolog "failed to dup2 to stderr: %s" (exntos exn);
173 Ne.clo dupstderr (clofail "stderr duplicate");
174 Ne.clo r (clofail "redir pipe/r");
175 Ne.clo w (clofail "redir pipe/w");
177 | Ne.Res () ->
178 state.stderr <- dupstderr;
179 state.errfd <- Some r;
180 end;
182 else (
183 state.newerrmsgs <- false;
184 begin match state.errfd with
185 | Some fd ->
186 begin match Ne.dup2 state.stderr Unix.stderr with
187 | Ne.Exn exn ->
188 dolog "failed to dup2 original stderr: %s" (exntos exn)
189 | Ne.Res () ->
190 Ne.clo fd (clofail "dup of stderr");
191 state.errfd <- None;
192 end;
193 | None -> ()
194 end;
195 prerr_string (Buffer.contents state.errmsgs);
196 flush stderr;
197 Buffer.clear state.errmsgs;
201 module G =
202 struct
203 let postRedisplay who =
204 if conf.verbose
205 then prerr_endline ("redisplay for " ^ who);
206 state.redisplay <- true;
208 end;;
210 let getopaque pageno =
211 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
212 with Not_found -> None
215 let putopaque pageno opaque =
216 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
219 let pagetranslatepoint l x y =
220 let dy = y - l.pagedispy in
221 let y = dy + l.pagey in
222 let dx = x - l.pagedispx in
223 let x = dx + l.pagex in
224 (x, y);
227 let onppundermouse g x y d =
228 let rec f = function
229 | l :: rest ->
230 begin match getopaque l.pageno with
231 | Some opaque ->
232 let x0 = l.pagedispx in
233 let x1 = x0 + l.pagevw in
234 let y0 = l.pagedispy in
235 let y1 = y0 + l.pagevh in
236 if y >= y0 && y <= y1 && x >= x0 && x <= x1
237 then
238 let px, py = pagetranslatepoint l x y in
239 match g opaque l px py with
240 | Some res -> res
241 | None -> f rest
242 else f rest
243 | _ ->
244 f rest
246 | [] -> d
248 f state.layout
251 let getunder x y =
252 let g opaque l px py =
253 if state.bzoom
254 then (
255 match rectofblock opaque px py with
256 | Some a ->
257 let rect = (a.(0),a.(2),a.(1),a.(2),a.(1),a.(3),a.(0),a.(3)) in
258 state.rects <- [l.pageno, l.pageno mod 3, rect];
259 G.postRedisplay "getunder";
260 | None -> ()
262 match whatsunder opaque px py with
263 | Unone -> None
264 | under -> Some under
266 onppundermouse g x y Unone
269 let unproject x y =
270 let g opaque l x y =
271 match unproject opaque x y with
272 | Some (x, y) -> Some (Some (l.pageno, x, y))
273 | None -> None
275 onppundermouse g x y None;
278 let showtext c s =
279 state.text <- Printf.sprintf "%c%s" c s;
280 G.postRedisplay "showtext";
283 let pipesel opaque cmd =
284 if hassel opaque
285 then
286 match Ne.res Unix.pipe with
287 | Ne.Exn exn ->
288 showtext '!'
289 (Printf.sprintf "pipesel can not create pipe: %s" (exntos exn));
290 | Ne.Res (r, w) ->
291 let doclose what fd =
292 Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
294 let popened =
295 try popen cmd [r, 0; w, -1]; true
296 with exn ->
297 dolog "can not execute %S: %s" cmd (exntos exn);
298 false
300 if popened
301 then (
302 copysel w opaque;
303 G.postRedisplay "pipesel";
305 else doclose "pipesel pipe/w" w;
306 doclose "pipesel pipe/r" r;
309 let paxunder x y =
310 let g opaque l px py =
311 if markunder opaque px py conf.paxmark
312 then (
313 Some (fun () ->
314 match getopaque l.pageno with
315 | None -> ()
316 | Some opaque -> pipesel opaque conf.paxcmd
319 else None
321 G.postRedisplay "paxunder";
322 if conf.paxmark = Mark_page
323 then
324 List.iter (fun l ->
325 match getopaque l.pageno with
326 | None -> ()
327 | Some opaque -> clearmark opaque) state.layout;
328 state.roam <-
329 onppundermouse g x y (fun () -> showtext '!' "Whoopsie daisy");
332 let selstring s =
333 match Ne.res Unix.pipe with
334 | Ne.Exn exn ->
335 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
336 | Ne.Res (r, w) ->
337 let clo cap fd =
338 Ne.clo fd (fun msg ->
339 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
342 let popened =
343 try popen conf.selcmd [r, 0; w, -1]; true
344 with exn ->
345 showtext '!'
346 (Printf.sprintf "failed to execute %s: %s"
347 conf.selcmd (exntos exn));
348 false
350 if popened
351 then (
353 let l = String.length s in
354 let n = tempfailureretry (Unix.write w s 0) l in
355 if n != l
356 then
357 showtext '!'
358 (Printf.sprintf
359 "failed to write %d characters to sel pipe, wrote %d"
362 with exn ->
363 showtext '!'
364 (Printf.sprintf "failed to write to sel pipe: %s"
365 (exntos exn)
368 else dolog "%s" s;
369 clo "selstring pipe/r" r;
370 clo "selstring pipe/w" w;
373 let undertext = function
374 | Unone -> "none"
375 | Ulinkuri s -> s
376 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
377 | Utext s -> "font: " ^ s
378 | Uunexpected s -> "unexpected: " ^ s
379 | Ulaunch s -> "launch: " ^ s
380 | Unamed s -> "named: " ^ s
381 | Uremote (filename, pageno) ->
382 Printf.sprintf "%s: page %d" filename (pageno+1)
383 | Uremotedest (filename, destname) ->
384 Printf.sprintf "%s: destination %S" filename destname
387 let updateunder x y =
388 match getunder x y with
389 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
390 | Ulinkuri uri ->
391 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
392 Wsi.setcursor Wsi.CURSOR_INFO
393 | Ulinkgoto (pageno, _) ->
394 if conf.underinfo
395 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
396 Wsi.setcursor Wsi.CURSOR_INFO
397 | Utext s ->
398 if conf.underinfo then showtext 'f' ("ont: " ^ s);
399 Wsi.setcursor Wsi.CURSOR_TEXT
400 | Uunexpected s ->
401 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
402 Wsi.setcursor Wsi.CURSOR_INHERIT
403 | Ulaunch s ->
404 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
405 Wsi.setcursor Wsi.CURSOR_INHERIT
406 | Unamed s ->
407 if conf.underinfo then showtext 'n' ("amed: " ^ s);
408 Wsi.setcursor Wsi.CURSOR_INHERIT
409 | Uremote (filename, pageno) ->
410 if conf.underinfo then showtext 'r'
411 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
412 Wsi.setcursor Wsi.CURSOR_INFO
413 | Uremotedest (filename, destname) ->
414 if conf.underinfo then showtext 'r'
415 (Printf.sprintf "emote destination: %s (%S)" filename destname);
416 Wsi.setcursor Wsi.CURSOR_INFO
419 let showlinktype under =
420 if conf.underinfo
421 then
422 match under with
423 | Unone -> ()
424 | under ->
425 let s = undertext under in
426 showtext ' ' s
429 let addchar s c =
430 let b = Buffer.create (String.length s + 1) in
431 Buffer.add_string b s;
432 Buffer.add_char b c;
433 Buffer.contents b;
436 let intentry_with_suffix text key =
437 let c =
438 if key >= 32 && key < 127
439 then Char.chr key
440 else '\000'
442 match Char.lowercase c with
443 | '0' .. '9' ->
444 let text = addchar text c in
445 TEcont text
447 | 'k' | 'm' | 'g' ->
448 let text = addchar text c in
449 TEcont text
451 | _ ->
452 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
453 TEcont text
456 let readcmd fd =
457 let s = "xxxx" in
458 let n = tempfailureretry (Unix.read fd s 0) 4 in
459 if n != 4 then error "incomplete read(len) = %d" n;
460 let len = 0
461 lor (Char.code s.[0] lsl 24)
462 lor (Char.code s.[1] lsl 16)
463 lor (Char.code s.[2] lsl 8)
464 lor (Char.code s.[3] lsl 0)
466 let s = String.create len in
467 let n = tempfailureretry (Unix.read fd s 0) len in
468 if n != len then error "incomplete read(data) %d vs %d" n len;
472 let btod b = if b then 1 else 0;;
474 let wcmd fmt =
475 let b = Buffer.create 16 in
476 Buffer.add_string b "llll";
477 Printf.kbprintf
478 (fun b ->
479 let s = Buffer.contents b in
480 let n = String.length s in
481 let len = n - 4 in
482 (* dolog "wcmd %S" (String.sub s 4 len); *)
483 s.[0] <- Char.chr ((len lsr 24) land 0xff);
484 s.[1] <- Char.chr ((len lsr 16) land 0xff);
485 s.[2] <- Char.chr ((len lsr 8) land 0xff);
486 s.[3] <- Char.chr (len land 0xff);
487 let n' = tempfailureretry (Unix.write state.sw s 0) n in
488 if n' != n then error "write failed %d vs %d" n' n;
489 ) b fmt;
492 let nogeomcmds cmds =
493 match cmds with
494 | s, [] -> emptystr s
495 | _ -> false
498 let layoutN ((columns, coverA, coverB), b) y sh =
499 let sh = sh - (hscrollh ()) in
500 let rec fold accu n =
501 if n = Array.length b
502 then accu
503 else
504 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
505 if (vy - y) > sh &&
506 (n = coverA - 1
507 || n = state.pagecount - coverB
508 || (n - coverA) mod columns = columns - 1)
509 then accu
510 else
511 let accu =
512 if vy + h > y
513 then
514 let pagey = max 0 (y - vy) in
515 let pagedispy = if pagey > 0 then 0 else vy - y in
516 let pagedispx, pagex =
517 let pdx =
518 if n = coverA - 1 || n = state.pagecount - coverB
519 then state.x + (wadjsb state.winw - w) / 2
520 else dx + xoff + state.x
522 if pdx < 0
523 then 0, -pdx
524 else pdx, 0
526 let pagevw =
527 let vw = wadjsb state.winw - pagedispx in
528 let pw = w - pagex in
529 min vw pw
531 let pagevh = min (h - pagey) (sh - pagedispy) in
532 if pagevw > 0 && pagevh > 0
533 then
534 let e =
535 { pageno = n
536 ; pagedimno = pdimno
537 ; pagew = w
538 ; pageh = h
539 ; pagex = pagex
540 ; pagey = pagey
541 ; pagevw = pagevw
542 ; pagevh = pagevh
543 ; pagedispx = pagedispx
544 ; pagedispy = pagedispy
545 ; pagecol = 0
548 e :: accu
549 else
550 accu
551 else
552 accu
554 fold accu (n+1)
556 if Array.length b = 0
557 then []
558 else List.rev (fold [] (page_of_y y))
561 let layoutS (columns, b) y sh =
562 let sh = sh - hscrollh () in
563 let rec fold accu n =
564 if n = Array.length b
565 then accu
566 else
567 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
568 if (vy - y) > sh
569 then accu
570 else
571 let accu =
572 if vy + pageh > y
573 then
574 let x = xoff + state.x in
575 let pagey = max 0 (y - vy) in
576 let pagedispy = if pagey > 0 then 0 else vy - y in
577 let pagedispx, pagex =
578 if px = 0
579 then (
580 if x < 0
581 then 0, -x
582 else x, 0
584 else (
585 let px = px - x in
586 if px < 0
587 then -px, 0
588 else 0, px
591 let pagecolw = pagew/columns in
592 let pagedispx =
593 if pagecolw < state.winw
594 then pagedispx + ((wadjsb state.winw - pagecolw) / 2)
595 else pagedispx
597 let pagevw =
598 let vw = wadjsb state.winw - pagedispx in
599 let pw = pagew - pagex in
600 min vw pw
602 let pagevw = min pagevw pagecolw in
603 let pagevh = min (pageh - pagey) (sh - pagedispy) in
604 if pagevw > 0 && pagevh > 0
605 then
606 let e =
607 { pageno = n/columns
608 ; pagedimno = pdimno
609 ; pagew = pagew
610 ; pageh = pageh
611 ; pagex = pagex
612 ; pagey = pagey
613 ; pagevw = pagevw
614 ; pagevh = pagevh
615 ; pagedispx = pagedispx
616 ; pagedispy = pagedispy
617 ; pagecol = n mod columns
620 e :: accu
621 else
622 accu
623 else
624 accu
626 fold accu (n+1)
628 List.rev (fold [] 0)
631 let layout y sh =
632 if nogeomcmds state.geomcmds
633 then
634 match conf.columns with
635 | Csingle b -> layoutN ((1, 0, 0), b) y sh
636 | Cmulti c -> layoutN c y sh
637 | Csplit s -> layoutS s y sh
638 else []
641 let clamp incr =
642 let y = state.y + incr in
643 let y = max 0 y in
644 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
648 let itertiles l f =
649 let tilex = l.pagex mod conf.tilew in
650 let tiley = l.pagey mod conf.tileh in
652 let col = l.pagex / conf.tilew in
653 let row = l.pagey / conf.tileh in
655 let rec rowloop row y0 dispy h =
656 if h = 0
657 then ()
658 else (
659 let dh = conf.tileh - y0 in
660 let dh = min h dh in
661 let rec colloop col x0 dispx w =
662 if w = 0
663 then ()
664 else (
665 let dw = conf.tilew - x0 in
666 let dw = min w dw in
667 let dispx' =
668 if conf.leftscroll
669 then dispx+conf.scrollbw
670 else dispx
672 f col row dispx' dispy x0 y0 dw dh;
673 colloop (col+1) 0 (dispx+dw) (w-dw)
676 colloop col tilex l.pagedispx l.pagevw;
677 rowloop (row+1) 0 (dispy+dh) (h-dh)
680 if l.pagevw > 0 && l.pagevh > 0
681 then rowloop row tiley l.pagedispy l.pagevh;
684 let gettileopaque l col row =
685 let key =
686 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
688 try Some (Hashtbl.find state.tilemap key)
689 with Not_found -> None
692 let puttileopaque l col row gen colorspace angle opaque size elapsed =
693 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
694 Hashtbl.add state.tilemap key (opaque, size, elapsed)
697 let filledrect x0 y0 x1 y1 =
698 GlArray.disable `texture_coord;
699 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
700 GlArray.vertex `two state.vraw;
701 GlArray.draw_arrays `triangle_strip 0 4;
702 GlArray.enable `texture_coord;
705 let linerect x0 y0 x1 y1 =
706 GlArray.disable `texture_coord;
707 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
708 GlArray.vertex `two state.vraw;
709 GlArray.draw_arrays `line_loop 0 4;
710 GlArray.enable `texture_coord;
713 let drawtiles l color =
714 GlDraw.color color;
715 begintiles ();
716 let f col row x y tilex tiley w h =
717 match gettileopaque l col row with
718 | Some (opaque, _, t) ->
719 let params = x, y, w, h, tilex, tiley in
720 if conf.invert
721 then GlTex.env (`mode `blend);
722 drawtile params opaque;
723 if conf.invert
724 then GlTex.env (`mode `modulate);
725 if conf.debug
726 then (
727 endtiles ();
728 let s = Printf.sprintf
729 "%d[%d,%d] %f sec"
730 l.pageno col row t
732 let w = measurestr fstate.fontsize s in
733 GlDraw.color (0.0, 0.0, 0.0);
734 filledrect (float (x-2))
735 (float (y-2))
736 (float (x+2) +. w)
737 (float (y + fstate.fontsize + 2));
738 GlDraw.color (1.0, 1.0, 1.0);
739 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
740 begintiles ();
743 | None ->
744 endtiles ();
745 let w =
746 if conf.leftscroll
747 then w
748 else
749 let lw = wadjsb state.winw - x in
750 min lw w
751 and h =
752 let lh = state.winh - y in
753 min lh h
755 if conf.invert
756 then GlTex.env (`mode `blend);
757 begin match state.checkerstexid with
758 | Some id ->
759 Gl.enable `texture_2d;
760 GlTex.bind_texture `texture_2d id;
761 let x0 = float x
762 and y0 = float y
763 and x1 = float (x+w)
764 and y1 = float (y+h) in
766 let tw = float w /. 16.0
767 and th = float h /. 16.0 in
768 let tx0 = float tilex /. 16.0
769 and ty0 = float tiley /. 16.0 in
770 let tx1 = tx0 +. tw
771 and ty1 = ty0 +. th in
772 Raw.sets_float state.vraw ~pos:0
773 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
774 Raw.sets_float state.traw ~pos:0
775 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
776 GlArray.vertex `two state.vraw;
777 GlArray.tex_coord `two state.traw;
778 GlArray.draw_arrays `triangle_strip 0 4;
779 Gl.disable `texture_2d;
781 | None ->
782 GlDraw.color (1.0, 1.0, 1.0);
783 filledrect (float x) (float y) (float (x+w)) (float (y+h));
784 end;
785 if conf.invert
786 then GlTex.env (`mode `modulate);
787 if w > 128 && h > fstate.fontsize + 10
788 then (
789 let c = if conf.invert then 1.0 else 0.0 in
790 GlDraw.color (c, c, c);
791 let c, r =
792 if conf.verbose
793 then (col*conf.tilew, row*conf.tileh)
794 else col, row
796 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
798 GlDraw.color color;
799 begintiles ();
801 itertiles l f;
802 endtiles ();
805 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
807 let tilevisible1 l x y =
808 let ax0 = l.pagex
809 and ax1 = l.pagex + l.pagevw
810 and ay0 = l.pagey
811 and ay1 = l.pagey + l.pagevh in
813 let bx0 = x
814 and by0 = y in
815 let bx1 = min (bx0 + conf.tilew) l.pagew
816 and by1 = min (by0 + conf.tileh) l.pageh in
818 let rx0 = max ax0 bx0
819 and ry0 = max ay0 by0
820 and rx1 = min ax1 bx1
821 and ry1 = min ay1 by1 in
823 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
824 nonemptyintersection
827 let tilevisible layout n x y =
828 let rec findpageinlayout m = function
829 | l :: rest when l.pageno = n ->
830 tilevisible1 l x y || (
831 match conf.columns with
832 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
833 | _ -> false
835 | _ :: rest -> findpageinlayout 0 rest
836 | [] -> false
838 findpageinlayout 0 layout;
841 let tileready l x y =
842 tilevisible1 l x y &&
843 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
846 let tilepage n p layout =
847 let rec loop = function
848 | l :: rest ->
849 if l.pageno = n
850 then
851 let f col row _ _ _ _ _ _ =
852 if state.currently = Idle
853 then
854 match gettileopaque l col row with
855 | Some _ -> ()
856 | None ->
857 let x = col*conf.tilew
858 and y = row*conf.tileh in
859 let w =
860 let w = l.pagew - x in
861 min w conf.tilew
863 let h =
864 let h = l.pageh - y in
865 min h conf.tileh
867 let pbo =
868 if conf.usepbo
869 then getpbo w h conf.colorspace
870 else ~< "0"
872 wcmd "tile %s %d %d %d %d %s"
873 (~> p) x y w h (~> pbo);
874 state.currently <-
875 Tiling (
876 l, p, conf.colorspace, conf.angle,
877 state.gen, col, row, conf.tilew, conf.tileh
880 itertiles l f;
881 else
882 loop rest
884 | [] -> ()
886 if nogeomcmds state.geomcmds
887 then loop layout;
890 let preloadlayout y =
891 let y = if y < state.winh then 0 else y - state.winh in
892 let h = state.winh*3 in
893 layout y h;
896 let load pages =
897 let rec loop pages =
898 if state.currently != Idle
899 then ()
900 else
901 match pages with
902 | l :: rest ->
903 begin match getopaque l.pageno with
904 | None ->
905 wcmd "page %d %d" l.pageno l.pagedimno;
906 state.currently <- Loading (l, state.gen);
907 | Some opaque ->
908 tilepage l.pageno opaque pages;
909 loop rest
910 end;
911 | _ -> ()
913 if nogeomcmds state.geomcmds
914 then loop pages
917 let preload pages =
918 load pages;
919 if conf.preload && state.currently = Idle
920 then load (preloadlayout state.y);
923 let layoutready layout =
924 let rec fold all ls =
925 all && match ls with
926 | l :: rest ->
927 let seen = ref false in
928 let allvisible = ref true in
929 let foo col row _ _ _ _ _ _ =
930 seen := true;
931 allvisible := !allvisible &&
932 begin match gettileopaque l col row with
933 | Some _ -> true
934 | None -> false
937 itertiles l foo;
938 fold (!seen && !allvisible) rest
939 | [] -> true
941 let alltilesvisible = fold true layout in
942 alltilesvisible;
945 let gotoy y =
946 let y = bound y 0 state.maxy in
947 let y, layout, proceed =
948 match conf.maxwait with
949 | Some time when state.ghyll == noghyll ->
950 begin match state.throttle with
951 | None ->
952 let layout = layout y state.winh in
953 let ready = layoutready layout in
954 if not ready
955 then (
956 load layout;
957 state.throttle <- Some (layout, y, now ());
959 else G.postRedisplay "gotoy showall (None)";
960 y, layout, ready
961 | Some (_, _, started) ->
962 let dt = now () -. started in
963 if dt > time
964 then (
965 state.throttle <- None;
966 let layout = layout y state.winh in
967 load layout;
968 G.postRedisplay "maxwait";
969 y, layout, true
971 else -1, [], false
974 | _ ->
975 let layout = layout y state.winh in
976 if not !wtmode || layoutready layout
977 then G.postRedisplay "gotoy ready";
978 y, layout, true
980 if proceed
981 then (
982 state.y <- y;
983 state.layout <- layout;
984 begin match state.mode with
985 | LinkNav (Ltexact (pageno, linkno)) ->
986 let rec loop = function
987 | [] ->
988 state.mode <- LinkNav (Ltgendir 0)
989 | l :: _ when l.pageno = pageno ->
990 begin match getopaque pageno with
991 | None ->
992 state.mode <- LinkNav (Ltgendir 0)
993 | Some opaque ->
994 let x0, y0, x1, y1 = getlinkrect opaque linkno in
995 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
996 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
997 then state.mode <- LinkNav (Ltgendir 0)
999 | _ :: rest -> loop rest
1001 loop layout
1002 | _ -> ()
1003 end;
1004 begin match state.mode with
1005 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1006 if not (pagevisible layout pageno)
1007 then (
1008 match state.layout with
1009 | [] -> ()
1010 | l :: _ ->
1011 state.mode <- Birdseye (
1012 conf, leftx, l.pageno, hooverpageno, anchor
1015 | LinkNav (Ltgendir dir as lt) ->
1016 let linknav =
1017 let rec loop = function
1018 | [] -> lt
1019 | l :: rest ->
1020 match getopaque l.pageno with
1021 | None -> loop rest
1022 | Some opaque ->
1023 let link =
1024 let ld =
1025 if dir = 0
1026 then LDfirstvisible (l.pagex, l.pagey, dir)
1027 else (
1028 if dir > 0 then LDfirst else LDlast
1031 findlink opaque ld
1033 match link with
1034 | Lnotfound -> loop rest
1035 | Lfound n ->
1036 showlinktype (getlink opaque n);
1037 Ltexact (l.pageno, n)
1039 loop state.layout
1041 state.mode <- LinkNav linknav
1042 | _ -> ()
1043 end;
1044 preload layout;
1046 state.ghyll <- noghyll;
1047 if conf.updatecurs
1048 then (
1049 let mx, my = state.mpos in
1050 updateunder mx my;
1054 let conttiling pageno opaque =
1055 tilepage pageno opaque
1056 (if conf.preload then preloadlayout state.y else state.layout)
1059 let gotoy_and_clear_text y =
1060 if not conf.verbose then state.text <- "";
1061 gotoy y;
1064 let getanchory (n, top, dtop) =
1065 let y, h = getpageyh n in
1066 if conf.presentation
1067 then
1068 let ips = calcips h in
1069 y + truncate (top*.float h -. dtop*.float ips) + ips;
1070 else
1071 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1074 let gotoanchor anchor =
1075 gotoy (getanchory anchor);
1078 let addnav () =
1079 cbput state.hists.nav (getanchor ());
1082 let getnav dir =
1083 let anchor = cbgetc state.hists.nav dir in
1084 getanchory anchor;
1087 let gotoghyll1 single y =
1088 let scroll f n a b =
1089 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1090 let snake f a b =
1091 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1092 if f < a
1093 then s (float f /. float a)
1094 else (
1095 if f > b
1096 then 1.0 -. s ((float (f-b) /. float (n-b)))
1097 else 1.0
1100 snake f a b
1101 and summa n a b =
1102 let ins = float a *. 0.5
1103 and outs = float (n-b) *. 0.5 in
1104 let ones = b - a in
1105 ins +. outs +. float ones
1107 let rec set nab y sy =
1108 let (_N, _A, _B), y =
1109 if single
1110 then
1111 let scl = if y > sy then 2 else -2 in
1112 let _N, _, _ = nab in
1113 (_N,0,_N), y+conf.scrollstep*scl
1114 else nab,y in
1115 let sum = summa _N _A _B in
1116 let dy = float (y - sy) in
1117 state.ghyll <- (
1118 let rec gf n y1 o =
1119 if n >= _N
1120 then state.ghyll <- noghyll
1121 else
1122 let go n =
1123 let s = scroll n _N _A _B in
1124 let y1 = y1 +. ((s *. dy) /. sum) in
1125 gotoy_and_clear_text (truncate y1);
1126 state.ghyll <- gf (n+1) y1;
1128 match o with
1129 | None -> go n
1130 | Some y' when single -> set nab y' state.y
1131 | Some y' -> set (_N/2, 1, 1) y' state.y
1133 gf 0 (float state.y)
1136 match conf.ghyllscroll with
1137 | Some nab when not conf.presentation ->
1138 if state.ghyll == noghyll
1139 then set nab y state.y
1140 else state.ghyll (Some y)
1141 | _ ->
1142 gotoy_and_clear_text y
1145 let gotoghyll = gotoghyll1 false;;
1147 let gotopage n top =
1148 let y, h = getpageyh n in
1149 let y = y + (truncate (top *. float h)) in
1150 gotoghyll y
1153 let gotopage1 n top =
1154 let y = getpagey n in
1155 let y = y + top in
1156 gotoghyll y
1159 let invalidate s f =
1160 state.layout <- [];
1161 state.pdims <- [];
1162 state.rects <- [];
1163 state.rects1 <- [];
1164 match state.geomcmds with
1165 | ps, [] when emptystr ps ->
1166 f ();
1167 state.geomcmds <- s, [];
1169 | ps, [] ->
1170 state.geomcmds <- ps, [s, f];
1172 | ps, (s', _) :: rest when s' = s ->
1173 state.geomcmds <- ps, ((s, f) :: rest);
1175 | ps, cmds ->
1176 state.geomcmds <- ps, ((s, f) :: cmds);
1179 let flushpages () =
1180 Hashtbl.iter (fun _ opaque ->
1181 wcmd "freepage %s" (~> opaque);
1182 ) state.pagemap;
1183 Hashtbl.clear state.pagemap;
1186 let flushtiles () =
1187 if not (Queue.is_empty state.tilelru)
1188 then (
1189 Queue.iter (fun (k, p, s) ->
1190 wcmd "freetile %s" (~> p);
1191 state.memused <- state.memused - s;
1192 Hashtbl.remove state.tilemap k;
1193 ) state.tilelru;
1194 state.uioh#infochanged Memused;
1195 Queue.clear state.tilelru;
1197 load state.layout;
1200 let stateh h =
1201 let h = truncate (float h*.conf.zoom) in
1202 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1203 h - d
1206 let opendoc path password =
1207 state.path <- path;
1208 state.password <- password;
1209 state.gen <- state.gen + 1;
1210 state.docinfo <- [];
1212 flushpages ();
1213 setaalevel conf.aalevel;
1214 let titlepath =
1215 if emptystr state.origin
1216 then path
1217 else state.origin
1219 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1220 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
1221 invalidate "reqlayout"
1222 (fun () ->
1223 wcmd "reqlayout %d %d %d %s\000"
1224 conf.angle (FMTE.to_int conf.fitmodel)
1225 (stateh state.winh) state.nameddest
1229 let reload () =
1230 state.anchor <- getanchor ();
1231 opendoc state.path state.password;
1234 let scalecolor c =
1235 let c = c *. conf.colorscale in
1236 (c, c, c);
1239 let scalecolor2 (r, g, b) =
1240 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1243 let docolumns = function
1244 | Csingle _ ->
1245 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1246 let rec loop pageno pdimno pdim y ph pdims =
1247 if pageno = state.pagecount
1248 then ()
1249 else
1250 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1251 match pdims with
1252 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1253 pdimno+1, pdim, rest
1254 | _ ->
1255 pdimno, pdim, pdims
1257 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
1258 let y = y +
1259 (if conf.presentation
1260 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1261 else (if pageno = 0 then 0 else conf.interpagespace)
1264 a.(pageno) <- (pdimno, x, y, pdim);
1265 loop (pageno+1) pdimno pdim (y + h) h pdims
1267 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1268 conf.columns <- Csingle a;
1270 | Cmulti ((columns, coverA, coverB), _) ->
1271 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1272 let rec loop pageno pdimno pdim x y rowh pdims =
1273 let rec fixrow m = if m = pageno then () else
1274 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1275 if h < rowh
1276 then (
1277 let y = y + (rowh - h) / 2 in
1278 a.(m) <- (pdimno, x, y, pdim);
1280 fixrow (m+1)
1282 if pageno = state.pagecount
1283 then fixrow (((pageno - 1) / columns) * columns)
1284 else
1285 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1286 match pdims with
1287 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1288 pdimno+1, pdim, rest
1289 | _ ->
1290 pdimno, pdim, pdims
1292 let x, y, rowh' =
1293 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1294 then (
1295 let x = (wadjsb state.winw - w) / 2 in
1296 let ips =
1297 if conf.presentation then calcips h else conf.interpagespace in
1298 x, y + ips + rowh, h
1300 else (
1301 if (pageno - coverA) mod columns = 0
1302 then (
1303 let x = max 0 (wadjsb state.winw - state.w) / 2 in
1304 let y =
1305 if conf.presentation
1306 then
1307 let ips = calcips h in
1308 y + (if pageno = 0 then 0 else calcips rowh + ips)
1309 else
1310 y + (if pageno = 0 then 0 else conf.interpagespace)
1312 x, y + rowh, h
1314 else x, y, max rowh h
1317 let y =
1318 if pageno > 1 && (pageno - coverA) mod columns = 0
1319 then (
1320 let y =
1321 if pageno = columns && conf.presentation
1322 then (
1323 let ips = calcips rowh in
1324 for i = 0 to pred columns
1326 let (pdimno, x, y, pdim) = a.(i) in
1327 a.(i) <- (pdimno, x, y+ips, pdim)
1328 done;
1329 y+ips;
1331 else y
1333 fixrow (pageno - columns);
1336 else y
1338 a.(pageno) <- (pdimno, x, y, pdim);
1339 let x = x + w + xoff*2 + conf.interpagespace in
1340 loop (pageno+1) pdimno pdim x y rowh' pdims
1342 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1343 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1345 | Csplit (c, _) ->
1346 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1347 let rec loop pageno pdimno pdim y pdims =
1348 if pageno = state.pagecount
1349 then ()
1350 else
1351 let pdimno, ((_, w, h, _) as pdim), pdims =
1352 match pdims with
1353 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1354 pdimno+1, pdim, rest
1355 | _ ->
1356 pdimno, pdim, pdims
1358 let cw = w / c in
1359 let rec loop1 n x y =
1360 if n = c then y else (
1361 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1362 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1365 let y = loop1 0 0 y in
1366 loop (pageno+1) pdimno pdim y pdims
1368 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1369 conf.columns <- Csplit (c, a);
1372 let represent () =
1373 docolumns conf.columns;
1374 state.maxy <- calcheight ();
1375 if state.reprf == noreprf
1376 then (
1377 match state.mode with
1378 | Birdseye (_, _, pageno, _, _) ->
1379 let y, h = getpageyh pageno in
1380 let top = (state.winh - h) / 2 in
1381 gotoy (max 0 (y - top))
1382 | _ -> gotoanchor state.anchor
1384 else (
1385 state.reprf ();
1386 state.reprf <- noreprf;
1390 let reshape w h =
1391 GlDraw.viewport 0 0 w h;
1392 let firsttime = state.geomcmds == firstgeomcmds in
1393 if not firsttime && nogeomcmds state.geomcmds
1394 then state.anchor <- getanchor ();
1396 state.winw <- w;
1397 let w = wadjsb (truncate (float w *. conf.zoom)) in
1398 let w = max w 2 in
1399 state.winh <- h;
1400 setfontsize fstate.fontsize;
1401 GlMat.mode `modelview;
1402 GlMat.load_identity ();
1404 GlMat.mode `projection;
1405 GlMat.load_identity ();
1406 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1407 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1408 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1410 let relx =
1411 if conf.zoom <= 1.0
1412 then 0.0
1413 else float state.x /. float state.w
1415 invalidate "geometry"
1416 (fun () ->
1417 state.w <- w;
1418 if not firsttime
1419 then state.x <- truncate (relx *. float w);
1420 let w =
1421 match conf.columns with
1422 | Csingle _ -> w
1423 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1424 | Csplit (c, _) -> w * c
1426 wcmd "geometry %d %d %d"
1427 w (stateh h) (FMTE.to_int conf.fitmodel)
1431 let enttext () =
1432 let len = String.length state.text in
1433 let x0 = if conf.leftscroll then conf.scrollbw else 0 in
1434 let drawstring s =
1435 let hscrollh =
1436 match state.mode with
1437 | Textentry _ | View | LinkNav _ ->
1438 let h, _, _ = state.uioh#scrollpw in
1440 | _ -> 0
1442 let rect x w =
1443 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1444 (x+.w) (float (state.winh - hscrollh))
1447 let w = float (wadjsb state.winw - 1) in
1448 if state.progress >= 0.0 && state.progress < 1.0
1449 then (
1450 GlDraw.color (0.3, 0.3, 0.3);
1451 let w1 = w *. state.progress in
1452 rect (float x0) w1;
1453 GlDraw.color (0.0, 0.0, 0.0);
1454 rect (float x0+.w1) (float x0+.w-.w1)
1456 else (
1457 GlDraw.color (0.0, 0.0, 0.0);
1458 rect (float x0) w;
1461 GlDraw.color (1.0, 1.0, 1.0);
1462 drawstring fstate.fontsize
1463 (if conf.leftscroll then x0 else x0 + if len > 0 then 8 else 2)
1464 (state.winh - hscrollh - 5) s;
1466 let s =
1467 match state.mode with
1468 | Textentry ((prefix, text, _, _, _, _), _) ->
1469 let s =
1470 if len > 0
1471 then
1472 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1473 else
1474 Printf.sprintf "%s%s_" prefix text
1478 | _ -> state.text
1480 let s =
1481 if state.newerrmsgs
1482 then (
1483 if not (istextentry state.mode) && state.uioh#eformsgs
1484 then
1485 let s1 = "(press 'e' to review error messasges)" in
1486 if nonemptystr s then s ^ " " ^ s1 else s1
1487 else s
1489 else s
1491 if nonemptystr s
1492 then drawstring s
1495 let gctiles () =
1496 let len = Queue.length state.tilelru in
1497 let layout = lazy (
1498 match state.throttle with
1499 | None ->
1500 if conf.preload
1501 then preloadlayout state.y
1502 else state.layout
1503 | Some (layout, _, _) ->
1504 layout
1505 ) in
1506 let rec loop qpos =
1507 if state.memused <= conf.memlimit
1508 then ()
1509 else (
1510 if qpos < len
1511 then
1512 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1513 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1514 let (_, pw, ph, _) = getpagedim n in
1516 gen = state.gen
1517 && colorspace = conf.colorspace
1518 && angle = conf.angle
1519 && pagew = pw
1520 && pageh = ph
1521 && (
1522 let x = col*conf.tilew
1523 and y = row*conf.tileh in
1524 tilevisible (Lazy.force_val layout) n x y
1526 then Queue.push lruitem state.tilelru
1527 else (
1528 freepbo p;
1529 wcmd "freetile %s" (~> p);
1530 state.memused <- state.memused - s;
1531 state.uioh#infochanged Memused;
1532 Hashtbl.remove state.tilemap k;
1534 loop (qpos+1)
1537 loop 0
1540 let logcurrently = function
1541 | Idle -> dolog "Idle"
1542 | Loading (l, gen) ->
1543 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1544 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1545 dolog
1546 "Tiling %d[%d,%d] page=%s cs=%s angle"
1547 l.pageno col row (~> pageopaque)
1548 (CSTE.to_string colorspace)
1550 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1551 angle gen conf.angle state.gen
1552 tilew tileh
1553 conf.tilew conf.tileh
1555 | Outlining _ ->
1556 dolog "outlining"
1559 let splitatspace =
1560 let r = Str.regexp " " in
1561 fun s -> Str.bounded_split r s 2;
1564 let onpagerect pageno f =
1565 let b =
1566 match conf.columns with
1567 | Cmulti (_, b) -> b
1568 | Csingle b -> b
1569 | Csplit (_, b) -> b
1571 if pageno >= 0 && pageno < Array.length b
1572 then
1573 let (_, _, _, (w, h, _, _)) = b.(pageno) in
1574 f w h
1577 let gotopagexy1 pageno x y =
1578 let _,w1,h1,leftx = getpagedim pageno in
1579 let top = y /. (float h1) in
1580 let left = x /. (float w1) in
1581 let py, w, h = getpageywh pageno in
1582 let wh = state.winh - hscrollh () in
1583 let x = left *. (float w) in
1584 let x = leftx + state.x + truncate x in
1585 let sx =
1586 if x < 0 || x >= wadjsb state.winw
1587 then state.x - x
1588 else state.x
1590 let pdy = truncate (top *. float h) in
1591 let y' = py + pdy in
1592 let dy = y' - state.y in
1593 let sy =
1594 if x != state.x || not (dy > 0 && dy < wh)
1595 then (
1596 if conf.presentation
1597 then
1598 if abs (py - y') > wh
1599 then y'
1600 else py
1601 else y';
1603 else state.y
1605 if state.x != sx || state.y != sy
1606 then (
1607 let x, y =
1608 if !wtmode
1609 then (
1610 let ww = wadjsb state.winw in
1611 let qx = sx / ww
1612 and qy = pdy / wh in
1613 let x = qx * ww
1614 and y = py + qy * wh in
1615 let x = if -x + ww > w1 then -(w1-ww) else x
1616 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1617 let y =
1618 if conf.presentation
1619 then
1620 if abs (py - y') > wh
1621 then y'
1622 else py
1623 else y';
1625 (x, y)
1627 else (sx, sy)
1629 state.x <- x;
1630 gotoy_and_clear_text y;
1632 else gotoy_and_clear_text state.y;
1635 let gotopagexy pageno x y =
1636 match state.mode with
1637 | Birdseye _ -> gotopage pageno 0.0
1638 | _ -> gotopagexy1 pageno x y
1641 let act cmds =
1642 (* dolog "%S" cmds; *)
1643 let cl = splitatspace cmds in
1644 let scan s fmt f =
1645 try Scanf.sscanf s fmt f
1646 with exn ->
1647 dolog "error processing '%S': %s" cmds (exntos exn);
1648 exit 1
1650 let addoutline outline =
1651 match state.currently with
1652 | Outlining outlines ->
1653 state.currently <- Outlining (outline :: outlines)
1654 | Idle -> state.currently <- Outlining [outline]
1655 | currently ->
1656 dolog "invalid outlining state";
1657 logcurrently currently
1659 match cl with
1660 | "clear" :: [] ->
1661 state.uioh#infochanged Pdim;
1662 state.pdims <- [];
1664 | "clearrects" :: [] ->
1665 state.rects <- state.rects1;
1666 G.postRedisplay "clearrects";
1668 | "continue" :: args :: [] ->
1669 let n = scan args "%u" (fun n -> n) in
1670 state.pagecount <- n;
1671 begin match state.currently with
1672 | Outlining l ->
1673 state.currently <- Idle;
1674 state.outlines <- Array.of_list (List.rev l)
1675 | _ -> ()
1676 end;
1678 let cur, cmds = state.geomcmds in
1679 if emptystr cur
1680 then failwith "umpossible";
1682 begin match List.rev cmds with
1683 | [] ->
1684 state.geomcmds <- "", [];
1685 state.throttle <- None;
1686 represent ();
1687 | (s, f) :: rest ->
1688 f ();
1689 state.geomcmds <- s, List.rev rest;
1690 end;
1691 if conf.maxwait = None && not !wtmode
1692 then G.postRedisplay "continue";
1694 | "title" :: args :: [] ->
1695 Wsi.settitle args
1697 | "msg" :: args :: [] ->
1698 showtext ' ' args
1700 | "vmsg" :: args :: [] ->
1701 if conf.verbose
1702 then showtext ' ' args
1704 | "emsg" :: args :: [] ->
1705 Buffer.add_string state.errmsgs args;
1706 state.newerrmsgs <- true;
1707 G.postRedisplay "error message"
1709 | "progress" :: args :: [] ->
1710 let progress, text =
1711 scan args "%f %n"
1712 (fun f pos ->
1713 f, String.sub args pos (String.length args - pos))
1715 state.text <- text;
1716 state.progress <- progress;
1717 G.postRedisplay "progress"
1719 | "firstmatch" :: args :: [] ->
1720 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1721 scan args "%u %d %f %f %f %f %f %f %f %f"
1722 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1723 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1725 let y = (getpagey pageno) + truncate y0 in
1726 addnav ();
1727 gotoy y;
1728 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1730 | "match" :: args :: [] ->
1731 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1732 scan args "%u %d %f %f %f %f %f %f %f %f"
1733 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1734 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1736 state.rects1 <-
1737 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1739 | "page" :: args :: [] ->
1740 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1741 let pageopaque = ~< pageopaques in
1742 begin match state.currently with
1743 | Loading (l, gen) ->
1744 vlog "page %d took %f sec" l.pageno t;
1745 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1746 begin match state.throttle with
1747 | None ->
1748 let preloadedpages =
1749 if conf.preload
1750 then preloadlayout state.y
1751 else state.layout
1753 let evict () =
1754 let set =
1755 List.fold_left (fun s l -> IntSet.add l.pageno s)
1756 IntSet.empty preloadedpages
1758 let evictedpages =
1759 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1760 if not (IntSet.mem pageno set)
1761 then (
1762 wcmd "freepage %s" (~> opaque);
1763 key :: accu
1765 else accu
1766 ) state.pagemap []
1768 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1770 evict ();
1771 state.currently <- Idle;
1772 if gen = state.gen
1773 then (
1774 tilepage l.pageno pageopaque state.layout;
1775 load state.layout;
1776 load preloadedpages;
1777 if pagevisible state.layout l.pageno
1778 && layoutready state.layout
1779 then G.postRedisplay "page";
1782 | Some (layout, _, _) ->
1783 state.currently <- Idle;
1784 tilepage l.pageno pageopaque layout;
1785 load state.layout
1786 end;
1788 | _ ->
1789 dolog "Inconsistent loading state";
1790 logcurrently state.currently;
1791 exit 1
1794 | "tile" :: args :: [] ->
1795 let (x, y, opaques, size, t) =
1796 scan args "%u %u %s %u %f"
1797 (fun x y p size t -> (x, y, p, size, t))
1799 let opaque = ~< opaques in
1800 begin match state.currently with
1801 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1802 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1804 unmappbo opaque;
1805 if tilew != conf.tilew || tileh != conf.tileh
1806 then (
1807 wcmd "freetile %s" (~> opaque);
1808 state.currently <- Idle;
1809 load state.layout;
1811 else (
1812 puttileopaque l col row gen cs angle opaque size t;
1813 state.memused <- state.memused + size;
1814 state.uioh#infochanged Memused;
1815 gctiles ();
1816 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1817 opaque, size) state.tilelru;
1819 let layout =
1820 match state.throttle with
1821 | None -> state.layout
1822 | Some (layout, _, _) -> layout
1825 state.currently <- Idle;
1826 if gen = state.gen
1827 && conf.colorspace = cs
1828 && conf.angle = angle
1829 && tilevisible layout l.pageno x y
1830 then conttiling l.pageno pageopaque;
1832 begin match state.throttle with
1833 | None ->
1834 preload state.layout;
1835 if gen = state.gen
1836 && conf.colorspace = cs
1837 && conf.angle = angle
1838 && tilevisible state.layout l.pageno x y
1839 && (not !wtmode || layoutready state.layout)
1840 then G.postRedisplay "tile nothrottle";
1842 | Some (layout, y, _) ->
1843 let ready = layoutready layout in
1844 if ready
1845 then (
1846 state.y <- y;
1847 state.layout <- layout;
1848 state.throttle <- None;
1849 G.postRedisplay "throttle";
1851 else load layout;
1852 end;
1855 | _ ->
1856 dolog "Inconsistent tiling state";
1857 logcurrently state.currently;
1858 exit 1
1861 | "pdim" :: args :: [] ->
1862 let (n, w, h, _) as pdim =
1863 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1865 let pdim =
1866 match conf.fitmodel, conf.columns with
1867 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
1868 | _ -> pdim
1870 state.uioh#infochanged Pdim;
1871 state.pdims <- pdim :: state.pdims
1873 | "o" :: args :: [] ->
1874 let (l, n, t, h, pos) =
1875 scan args "%u %u %d %u %n"
1876 (fun l n t h pos -> l, n, t, h, pos)
1878 let s = String.sub args pos (String.length args - pos) in
1879 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1881 | "ou" :: args :: [] ->
1882 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1883 let s = String.sub args pos len in
1884 let pos2 = pos + len + 1 in
1885 let uri = String.sub args pos2 (String.length args - pos2) in
1886 addoutline (s, l, Ouri uri)
1888 | "on" :: args :: [] ->
1889 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1890 let s = String.sub args pos (String.length args - pos) in
1891 addoutline (s, l, Onone)
1893 | "a" :: args :: [] ->
1894 let (n, l, t) =
1895 scan args "%u %d %d" (fun n l t -> n, l, t)
1897 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
1899 | "info" :: args :: [] ->
1900 state.docinfo <- (1, args) :: state.docinfo
1902 | "infoend" :: [] ->
1903 state.uioh#infochanged Docinfo;
1904 state.docinfo <- List.rev state.docinfo
1906 | _ ->
1907 error "unknown cmd `%S'" cmds
1910 let onhist cb =
1911 let rc = cb.rc in
1912 let action = function
1913 | HCprev -> cbget cb ~-1
1914 | HCnext -> cbget cb 1
1915 | HCfirst -> cbget cb ~-(cb.rc)
1916 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1917 and cancel () = cb.rc <- rc
1918 in (action, cancel)
1921 let search pattern forward =
1922 match conf.columns with
1923 | Csplit _ ->
1924 showtext '!' "searching does not work properly in split columns mode"
1925 | _ ->
1926 if nonemptystr pattern
1927 then
1928 let pn, py =
1929 match state.layout with
1930 | [] -> 0, 0
1931 | l :: _ ->
1932 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1934 wcmd "search %d %d %d %d,%s\000"
1935 (btod conf.icase) pn py (btod forward) pattern;
1938 let intentry text key =
1939 let c =
1940 if key >= 32 && key < 127
1941 then Char.chr key
1942 else '\000'
1944 match c with
1945 | '0' .. '9' ->
1946 let text = addchar text c in
1947 TEcont text
1949 | _ ->
1950 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1951 TEcont text
1954 let linknentry text key =
1955 let c =
1956 if key >= 32 && key < 127
1957 then Char.chr key
1958 else '\000'
1960 match c with
1961 | 'a' .. 'z' ->
1962 let text = addchar text c in
1963 TEcont text
1965 | _ ->
1966 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1967 TEcont text
1970 let linkndone f s =
1971 if nonemptystr s
1972 then (
1973 let n =
1974 let l = String.length s in
1975 let rec loop pos n = if pos = l then n else
1976 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1977 loop (pos+1) (n*26 + m)
1978 in loop 0 0
1980 let rec loop n = function
1981 | [] -> ()
1982 | l :: rest ->
1983 match getopaque l.pageno with
1984 | None -> loop n rest
1985 | Some opaque ->
1986 let m = getlinkcount opaque in
1987 if n < m
1988 then (
1989 let under = getlink opaque n in
1990 f under
1992 else loop (n-m) rest
1994 loop n state.layout;
1998 let textentry text key =
1999 if key land 0xff00 = 0xff00
2000 then TEcont text
2001 else TEcont (text ^ toutf8 key)
2004 let reqlayout angle fitmodel =
2005 match state.throttle with
2006 | None ->
2007 if nogeomcmds state.geomcmds
2008 then state.anchor <- getanchor ();
2009 conf.angle <- angle mod 360;
2010 if conf.angle != 0
2011 then (
2012 match state.mode with
2013 | LinkNav _ -> state.mode <- View
2014 | _ -> ()
2016 conf.fitmodel <- fitmodel;
2017 invalidate "reqlayout"
2018 (fun () ->
2019 wcmd "reqlayout %d %d %d"
2020 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2022 | _ -> ()
2025 let settrim trimmargins trimfuzz =
2026 if nogeomcmds state.geomcmds
2027 then state.anchor <- getanchor ();
2028 conf.trimmargins <- trimmargins;
2029 conf.trimfuzz <- trimfuzz;
2030 let x0, y0, x1, y1 = trimfuzz in
2031 invalidate "settrim"
2032 (fun () ->
2033 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2034 flushpages ();
2037 let setzoom zoom =
2038 match state.throttle with
2039 | None ->
2040 let zoom = max 0.0001 zoom in
2041 if zoom <> conf.zoom
2042 then (
2043 state.prevzoom <- (conf.zoom, state.x);
2044 conf.zoom <- zoom;
2045 reshape state.winw state.winh;
2046 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2049 | Some (layout, y, started) ->
2050 let time =
2051 match conf.maxwait with
2052 | None -> 0.0
2053 | Some t -> t
2055 let dt = now () -. started in
2056 if dt > time
2057 then (
2058 state.y <- y;
2059 load layout;
2063 let setcolumns mode columns coverA coverB =
2064 state.prevcolumns <- Some (conf.columns, conf.zoom);
2065 if columns < 0
2066 then (
2067 if isbirdseye mode
2068 then showtext '!' "split mode doesn't work in bird's eye"
2069 else (
2070 conf.columns <- Csplit (-columns, [||]);
2071 state.x <- 0;
2072 conf.zoom <- 1.0;
2075 else (
2076 if columns < 2
2077 then (
2078 conf.columns <- Csingle [||];
2079 state.x <- 0;
2080 setzoom 1.0;
2082 else (
2083 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2084 conf.zoom <- 1.0;
2087 reshape state.winw state.winh;
2090 let resetmstate () =
2091 state.mstate <- Mnone;
2092 Wsi.setcursor Wsi.CURSOR_INHERIT;
2095 let enterbirdseye () =
2096 let zoom = float conf.thumbw /. float state.winw in
2097 let birdseyepageno =
2098 let cy = state.winh / 2 in
2099 let fold = function
2100 | [] -> 0
2101 | l :: rest ->
2102 let rec fold best = function
2103 | [] -> best.pageno
2104 | l :: rest ->
2105 let d = cy - (l.pagedispy + l.pagevh/2)
2106 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2107 if abs d < abs dbest
2108 then fold l rest
2109 else best.pageno
2110 in fold l rest
2112 fold state.layout
2114 state.mode <- Birdseye (
2115 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2117 resetmstate ();
2118 conf.zoom <- zoom;
2119 conf.presentation <- false;
2120 conf.interpagespace <- 10;
2121 conf.hlinks <- false;
2122 conf.fitmodel <- FitProportional;
2123 state.x <- 0;
2124 conf.maxwait <- None;
2125 conf.columns <- (
2126 match conf.beyecolumns with
2127 | Some c ->
2128 conf.zoom <- 1.0;
2129 Cmulti ((c, 0, 0), [||])
2130 | None -> Csingle [||]
2132 if conf.verbose
2133 then
2134 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2135 (100.0*.zoom)
2136 else
2137 state.text <- ""
2139 reshape state.winw state.winh;
2142 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2143 state.mode <- View;
2144 conf.zoom <- c.zoom;
2145 conf.presentation <- c.presentation;
2146 conf.interpagespace <- c.interpagespace;
2147 conf.maxwait <- c.maxwait;
2148 conf.hlinks <- c.hlinks;
2149 conf.fitmodel <- c.fitmodel;
2150 conf.beyecolumns <- (
2151 match conf.columns with
2152 | Cmulti ((c, _, _), _) -> Some c
2153 | Csingle _ -> None
2154 | Csplit _ -> failwith "leaving bird's eye split mode"
2156 conf.columns <- (
2157 match c.columns with
2158 | Cmulti (c, _) -> Cmulti (c, [||])
2159 | Csingle _ -> Csingle [||]
2160 | Csplit (c, _) -> Csplit (c, [||])
2162 if conf.verbose
2163 then
2164 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2165 (100.0*.conf.zoom)
2167 reshape state.winw state.winh;
2168 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2169 state.x <- leftx;
2172 let togglebirdseye () =
2173 match state.mode with
2174 | Birdseye vals -> leavebirdseye vals true
2175 | View -> enterbirdseye ()
2176 | _ -> ()
2179 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2180 let pageno = max 0 (pageno - incr) in
2181 let rec loop = function
2182 | [] -> gotopage1 pageno 0
2183 | l :: _ when l.pageno = pageno ->
2184 if l.pagedispy >= 0 && l.pagey = 0
2185 then G.postRedisplay "upbirdseye"
2186 else gotopage1 pageno 0
2187 | _ :: rest -> loop rest
2189 loop state.layout;
2190 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2193 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2194 let pageno = min (state.pagecount - 1) (pageno + incr) in
2195 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2196 let rec loop = function
2197 | [] ->
2198 let y, h = getpageyh pageno in
2199 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2200 gotoy (clamp dy)
2201 | l :: _ when l.pageno = pageno ->
2202 if l.pagevh != l.pageh
2203 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2204 else G.postRedisplay "downbirdseye"
2205 | _ :: rest -> loop rest
2207 loop state.layout
2210 let boundastep h step =
2211 if step < 0
2212 then bound step ~-h 0
2213 else bound step 0 h
2216 let optentry mode _ key =
2217 let btos b = if b then "on" else "off" in
2218 if key >= 32 && key < 127
2219 then
2220 let c = Char.chr key in
2221 match c with
2222 | 's' ->
2223 let ondone s =
2224 try conf.scrollstep <- int_of_string s with exc ->
2225 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2227 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
2229 | 'A' ->
2230 let ondone s =
2232 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2233 if state.autoscroll <> None
2234 then state.autoscroll <- Some conf.autoscrollstep
2235 with exc ->
2236 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2238 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
2240 | 'C' ->
2241 let ondone s =
2243 let n, a, b = multicolumns_of_string s in
2244 setcolumns mode n a b;
2245 with exc ->
2246 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
2248 TEswitch ("columns: ", "", None, textentry, ondone, true)
2250 | 'Z' ->
2251 let ondone s =
2253 let zoom = float (int_of_string s) /. 100.0 in
2254 setzoom zoom
2255 with exc ->
2256 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2258 TEswitch ("zoom: ", "", None, intentry, ondone, true)
2260 | 't' ->
2261 let ondone s =
2263 conf.thumbw <- bound (int_of_string s) 2 4096;
2264 state.text <-
2265 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2266 begin match mode with
2267 | Birdseye beye ->
2268 leavebirdseye beye false;
2269 enterbirdseye ();
2270 | _ -> ();
2272 with exc ->
2273 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2275 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
2277 | 'R' ->
2278 let ondone s =
2279 match try
2280 Some (int_of_string s)
2281 with exc ->
2282 state.text <- Printf.sprintf "bad integer `%s': %s"
2283 s (exntos exc);
2284 None
2285 with
2286 | Some angle -> reqlayout angle conf.fitmodel
2287 | None -> ()
2289 TEswitch ("rotation: ", "", None, intentry, ondone, true)
2291 | 'i' ->
2292 conf.icase <- not conf.icase;
2293 TEdone ("case insensitive search " ^ (btos conf.icase))
2295 | 'p' ->
2296 conf.preload <- not conf.preload;
2297 gotoy state.y;
2298 TEdone ("preload " ^ (btos conf.preload))
2300 | 'v' ->
2301 conf.verbose <- not conf.verbose;
2302 TEdone ("verbose " ^ (btos conf.verbose))
2304 | 'd' ->
2305 conf.debug <- not conf.debug;
2306 TEdone ("debug " ^ (btos conf.debug))
2308 | 'h' ->
2309 conf.maxhfit <- not conf.maxhfit;
2310 state.maxy <- calcheight ();
2311 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2313 | 'c' ->
2314 conf.crophack <- not conf.crophack;
2315 TEdone ("crophack " ^ btos conf.crophack)
2317 | 'a' ->
2318 let s =
2319 match conf.maxwait with
2320 | None ->
2321 conf.maxwait <- Some infinity;
2322 "always wait for page to complete"
2323 | Some _ ->
2324 conf.maxwait <- None;
2325 "show placeholder if page is not ready"
2327 TEdone s
2329 | 'f' ->
2330 conf.underinfo <- not conf.underinfo;
2331 TEdone ("underinfo " ^ btos conf.underinfo)
2333 | 'P' ->
2334 conf.savebmarks <- not conf.savebmarks;
2335 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2337 | 'S' ->
2338 let ondone s =
2340 let pageno, py =
2341 match state.layout with
2342 | [] -> 0, 0
2343 | l :: _ ->
2344 l.pageno, l.pagey
2346 conf.interpagespace <- int_of_string s;
2347 docolumns conf.columns;
2348 state.maxy <- calcheight ();
2349 let y = getpagey pageno in
2350 gotoy (y + py)
2351 with exc ->
2352 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2354 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
2356 | 'l' ->
2357 let fm =
2358 match conf.fitmodel with
2359 | FitProportional -> FitWidth
2360 | _ -> FitProportional
2362 reqlayout conf.angle fm;
2363 TEdone ("proportional display " ^ btos (fm == FitProportional))
2365 | 'T' ->
2366 settrim (not conf.trimmargins) conf.trimfuzz;
2367 TEdone ("trim margins " ^ btos conf.trimmargins)
2369 | 'I' ->
2370 conf.invert <- not conf.invert;
2371 TEdone ("invert colors " ^ btos conf.invert)
2373 | 'x' ->
2374 let ondone s =
2375 cbput state.hists.sel s;
2376 conf.selcmd <- s;
2378 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2379 textentry, ondone, true)
2381 | 'M' ->
2382 if conf.pax == None
2383 then conf.pax <- Some (ref (0.0, 0, 0))
2384 else conf.pax <- None;
2385 TEdone ("PAX " ^ btos (conf.pax != None))
2387 | _ ->
2388 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2389 TEstop
2390 else
2391 TEcont state.text
2394 class type lvsource = object
2395 method getitemcount : int
2396 method getitem : int -> (string * int)
2397 method hasaction : int -> bool
2398 method exit :
2399 uioh:uioh ->
2400 cancel:bool ->
2401 active:int ->
2402 first:int ->
2403 pan:int ->
2404 uioh option
2405 method getactive : int
2406 method getfirst : int
2407 method getpan : int
2408 method getminfo : (int * int) array
2409 end;;
2411 class virtual lvsourcebase = object
2412 val mutable m_active = 0
2413 val mutable m_first = 0
2414 val mutable m_pan = 0
2415 method getactive = m_active
2416 method getfirst = m_first
2417 method getpan = m_pan
2418 method getminfo : (int * int) array = [||]
2419 end;;
2421 let withoutlastutf8 s =
2422 let len = String.length s in
2423 if len = 0
2424 then s
2425 else
2426 let rec find pos =
2427 if pos = 0
2428 then pos
2429 else
2430 let b = Char.code s.[pos] in
2431 if b land 0b11000000 = 0b11000000
2432 then pos
2433 else find (pos-1)
2435 let first =
2436 if Char.code s.[len-1] land 0x80 = 0
2437 then len-1
2438 else find (len-1)
2440 String.sub s 0 first;
2443 let textentrykeyboard
2444 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2445 let key =
2446 if key >= 0xffb0 && key <= 0xffb9
2447 then key - 0xffb0 + 48 else key
2449 let enttext te =
2450 state.mode <- Textentry (te, onleave);
2451 state.text <- "";
2452 enttext ();
2453 G.postRedisplay "textentrykeyboard enttext";
2455 let histaction cmd =
2456 match opthist with
2457 | None -> ()
2458 | Some (action, _) ->
2459 state.mode <- Textentry (
2460 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2462 G.postRedisplay "textentry histaction"
2464 match key with
2465 | 0xff08 -> (* backspace *)
2466 if emptystr text && cancelonempty
2467 then (
2468 onleave Cancel;
2469 G.postRedisplay "textentrykeyboard after cancel";
2471 else
2472 let s = withoutlastutf8 text in
2473 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2475 | 0xff0d | 0xff8d -> (* (kp) enter *)
2476 ondone text;
2477 onleave Confirm;
2478 G.postRedisplay "textentrykeyboard after confirm"
2480 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
2481 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
2482 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
2483 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
2485 | 0xff1b -> (* escape*)
2486 if emptystr text
2487 then (
2488 begin match opthist with
2489 | None -> ()
2490 | Some (_, onhistcancel) -> onhistcancel ()
2491 end;
2492 onleave Cancel;
2493 state.text <- "";
2494 G.postRedisplay "textentrykeyboard after cancel2"
2496 else (
2497 enttext (c, "", opthist, onkey, ondone, cancelonempty)
2500 | 0xff9f | 0xffff -> () (* delete *)
2502 | _ when key != 0
2503 && key land 0xff00 != 0xff00 (* keyboard *)
2504 && key land 0xfe00 != 0xfe00 (* xkb *)
2505 && key land 0xfd00 != 0xfd00 (* 3270 *)
2507 begin match onkey text key with
2508 | TEdone text ->
2509 ondone text;
2510 onleave Confirm;
2511 G.postRedisplay "textentrykeyboard after confirm2";
2513 | TEcont text ->
2514 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2516 | TEstop ->
2517 onleave Cancel;
2518 G.postRedisplay "textentrykeyboard after cancel3"
2520 | TEswitch te ->
2521 state.mode <- Textentry (te, onleave);
2522 G.postRedisplay "textentrykeyboard switch";
2523 end;
2525 | _ ->
2526 vlog "unhandled key %s" (Wsi.keyname key)
2529 let firstof first active =
2530 if first > active || abs (first - active) > fstate.maxrows - 1
2531 then max 0 (active - (fstate.maxrows/2))
2532 else first
2535 let calcfirst first active =
2536 if active > first
2537 then
2538 let rows = active - first in
2539 if rows > fstate.maxrows then active - fstate.maxrows else first
2540 else active
2543 let scrollph y maxy =
2544 let sh = float (maxy + state.winh) /. float state.winh in
2545 let sh = float state.winh /. sh in
2546 let sh = max sh (float conf.scrollh) in
2548 let percent = float y /. float maxy in
2549 let position = (float state.winh -. sh) *. percent in
2551 let position =
2552 if position +. sh > float state.winh
2553 then float state.winh -. sh
2554 else position
2556 position, sh;
2559 let coe s = (s :> uioh);;
2561 class listview ?(helpmode=false) ~(source:lvsource) ~trusted ~modehash =
2562 object (self)
2563 val m_pan = source#getpan
2564 val m_first = source#getfirst
2565 val m_active = source#getactive
2566 val m_qsearch = ""
2567 val m_prev_uioh = state.uioh
2569 method private elemunder y =
2570 if y < 0
2571 then None
2572 else
2573 let n = y / (fstate.fontsize+1) in
2574 if m_first + n < source#getitemcount
2575 then (
2576 if source#hasaction (m_first + n)
2577 then Some (m_first + n)
2578 else None
2580 else None
2582 method display =
2583 Gl.enable `blend;
2584 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2585 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2586 filledrect 0. 0. (float state.winw) (float state.winh);
2587 GlDraw.color (1., 1., 1.);
2588 Gl.enable `texture_2d;
2589 let fs = fstate.fontsize in
2590 let nfs = fs + 1 in
2591 let ww = fstate.wwidth in
2592 let tabw = 30.0*.ww in
2593 let itemcount = source#getitemcount in
2594 let minfo = source#getminfo in
2595 let x0, x1 =
2596 if conf.leftscroll
2597 then float conf.scrollbw, float (state.winw - 1)
2598 else 0.0, float (state.winw - conf.scrollbw - 1)
2600 let rec loop row =
2601 if (row - m_first) > fstate.maxrows
2602 then ()
2603 else (
2604 if row >= 0 && row < itemcount
2605 then (
2606 let (s, level) = source#getitem row in
2607 let y = (row - m_first) * nfs in
2608 let x = 5.0 +. float (level + m_pan) *. ww in
2609 if row = m_active
2610 then (
2611 Gl.disable `texture_2d;
2612 let alpha = if source#hasaction row then 0.9 else 0.3 in
2613 GlDraw.color (1., 1., 1.) ~alpha;
2614 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2615 Gl.enable `texture_2d;
2616 GlDraw.color (1., 1., 1.);
2618 let drawtabularstring s =
2619 let drawstr x s = drawstring1 fs (truncate (x +. x0)) (y+nfs) s in
2620 if trusted
2621 then
2622 let x = if helpmode && row > 0 then x +. ww else x in
2623 let tabpos = try String.index s '\t' with Not_found -> -1 in
2624 if tabpos > 0
2625 then
2626 let len = String.length s - tabpos - 1 in
2627 let s1 = String.sub s 0 tabpos
2628 and s2 = String.sub s (tabpos + 1) len in
2629 let nx = drawstr x s1 in
2630 let sw = nx -. x in
2631 let x = x +. (max tabw sw) in
2632 drawstr x s2
2633 else
2634 let len = String.length s - 2 in
2635 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2636 then
2637 let s = String.sub s 2 len in
2638 let x = if not helpmode then x +. ww else x in
2639 GlDraw.color (1.2, 1.2, 1.2);
2640 let vinc = drawstring1 (fs+fs/4)
2641 (truncate (x -. ww)) (y+nfs) s in
2642 GlDraw.color (1., 1., 1.);
2643 vinc +. (float fs *. 0.8)
2644 else
2645 drawstr x s
2646 else
2647 drawstr x s
2649 let _ = drawtabularstring s in
2650 loop (row+1)
2654 loop m_first;
2655 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2656 let rec loop row =
2657 if (row - m_first) > fstate.maxrows
2658 then ()
2659 else (
2660 if row >= 0 && row < itemcount
2661 then (
2662 let (s, level) = source#getitem row in
2663 let y = (row - m_first) * nfs in
2664 let x = 5.0 +. float (level + m_pan) *. ww in
2665 let (first, last) = minfo.(row) in
2666 let prefix = String.sub s 0 first in
2667 let suffix = String.sub s first (last - first) in
2668 let w1 = measurestr fstate.fontsize prefix in
2669 let w2 = measurestr fstate.fontsize suffix in
2670 let x0 = x +. w1
2671 and y0 = (float (y+2)) in
2672 let x1 = x0 +. w2
2673 and y1 = (float (y+fs+3)) in
2674 filledrect x0 y0 x1 y1;
2675 loop (row+1)
2679 Gl.disable `texture_2d;
2680 if Array.length minfo > 0 then loop m_first;
2681 Gl.disable `blend;
2683 method updownlevel incr =
2684 let len = source#getitemcount in
2685 let curlevel =
2686 if m_active >= 0 && m_active < len
2687 then snd (source#getitem m_active)
2688 else -1
2690 let rec flow i =
2691 if i = len then i-1 else if i = -1 then 0 else
2692 let _, l = source#getitem i in
2693 if l != curlevel then i else flow (i+incr)
2695 let active = flow m_active in
2696 let first = calcfirst m_first active in
2697 G.postRedisplay "outline updownlevel";
2698 {< m_active = active; m_first = first >}
2700 method private key1 key mask =
2701 let set1 active first qsearch =
2702 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2704 let search active pattern incr =
2705 let active = if active = -1 then m_first else active in
2706 let dosearch re =
2707 let rec loop n =
2708 if n >= 0 && n < source#getitemcount
2709 then (
2710 let s, _ = source#getitem n in
2712 (try ignore (Str.search_forward re s 0); true
2713 with Not_found -> false)
2714 then Some n
2715 else loop (n + incr)
2717 else None
2719 loop active
2722 let re = Str.regexp_case_fold pattern in
2723 dosearch re
2724 with Failure s ->
2725 state.text <- s;
2726 None
2728 let itemcount = source#getitemcount in
2729 let find start incr =
2730 let rec find i =
2731 if i = -1 || i = itemcount
2732 then -1
2733 else (
2734 if source#hasaction i
2735 then i
2736 else find (i + incr)
2739 find start
2741 let set active first =
2742 let first = bound first 0 (itemcount - fstate.maxrows) in
2743 state.text <- "";
2744 coe {< m_active = active; m_first = first; m_qsearch = "" >}
2746 let navigate incr =
2747 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2748 let active, first =
2749 let incr1 = if incr > 0 then 1 else -1 in
2750 if isvisible m_first m_active
2751 then
2752 let next =
2753 let next = m_active + incr in
2754 let next =
2755 if next < 0 || next >= itemcount
2756 then -1
2757 else find next incr1
2759 if abs (m_active - next) > fstate.maxrows
2760 then -1
2761 else next
2763 if next = -1
2764 then
2765 let first = m_first + incr in
2766 let first = bound first 0 (itemcount - fstate.maxrows) in
2767 let next =
2768 let next = m_active + incr in
2769 let next = bound next 0 (itemcount - 1) in
2770 find next ~-incr1
2772 let active =
2773 if next = -1
2774 then m_active
2775 else (
2776 if isvisible first next
2777 then next
2778 else m_active
2781 active, first
2782 else
2783 let first = min next m_first in
2784 let first =
2785 if abs (next - first) > fstate.maxrows
2786 then first + incr
2787 else first
2789 next, first
2790 else
2791 let first = m_first + incr in
2792 let first = bound first 0 (itemcount - 1) in
2793 let active =
2794 let next = m_active + incr in
2795 let next = bound next 0 (itemcount - 1) in
2796 let next = find next incr1 in
2797 let active =
2798 if next = -1 || abs (m_active - first) > fstate.maxrows
2799 then (
2800 let active = if m_active = -1 then next else m_active in
2801 active
2803 else next
2805 if isvisible first active
2806 then active
2807 else -1
2809 active, first
2811 G.postRedisplay "listview navigate";
2812 set active first;
2814 match key with
2815 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
2816 let incr = if key = 0x72 then -1 else 1 in
2817 let active, first =
2818 match search (m_active + incr) m_qsearch incr with
2819 | None ->
2820 state.text <- m_qsearch ^ " [not found]";
2821 m_active, m_first
2822 | Some active ->
2823 state.text <- m_qsearch;
2824 active, firstof m_first active
2826 G.postRedisplay "listview ctrl-r/s";
2827 set1 active first m_qsearch;
2829 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
2830 if m_active >= 0 && m_active < source#getitemcount
2831 then (
2832 let s, _ = source#getitem m_active in
2833 selstring s;
2835 coe self
2837 | 0xff08 -> (* backspace *)
2838 if emptystr m_qsearch
2839 then coe self
2840 else (
2841 let qsearch = withoutlastutf8 m_qsearch in
2842 if emptystr qsearch
2843 then (
2844 state.text <- "";
2845 G.postRedisplay "listview empty qsearch";
2846 set1 m_active m_first "";
2848 else
2849 let active, first =
2850 match search m_active qsearch ~-1 with
2851 | None ->
2852 state.text <- qsearch ^ " [not found]";
2853 m_active, m_first
2854 | Some active ->
2855 state.text <- qsearch;
2856 active, firstof m_first active
2858 G.postRedisplay "listview backspace qsearch";
2859 set1 active first qsearch
2862 | key when (key != 0 && key land 0xff00 != 0xff00) ->
2863 let pattern = m_qsearch ^ toutf8 key in
2864 let active, first =
2865 match search m_active pattern 1 with
2866 | None ->
2867 state.text <- pattern ^ " [not found]";
2868 m_active, m_first
2869 | Some active ->
2870 state.text <- pattern;
2871 active, firstof m_first active
2873 G.postRedisplay "listview qsearch add";
2874 set1 active first pattern;
2876 | 0xff1b -> (* escape *)
2877 state.text <- "";
2878 if emptystr m_qsearch
2879 then (
2880 G.postRedisplay "list view escape";
2881 begin
2882 match
2883 source#exit (coe self) true m_active m_first m_pan
2884 with
2885 | None -> m_prev_uioh
2886 | Some uioh -> uioh
2889 else (
2890 G.postRedisplay "list view kill qsearch";
2891 coe {< m_qsearch = "" >}
2894 | 0xff0d | 0xff8d -> (* (kp) enter *)
2895 state.text <- "";
2896 let self = {< m_qsearch = "" >} in
2897 let opt =
2898 G.postRedisplay "listview enter";
2899 if m_active >= 0 && m_active < source#getitemcount
2900 then (
2901 source#exit (coe self) false m_active m_first m_pan;
2903 else (
2904 source#exit (coe self) true m_active m_first m_pan;
2907 begin match opt with
2908 | None -> m_prev_uioh
2909 | Some uioh -> uioh
2912 | 0xff9f | 0xffff -> (* (kp) delete *)
2913 coe self
2915 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
2916 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
2917 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
2918 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
2920 | 0xff53 | 0xff98 -> (* (kp) right *)
2921 state.text <- "";
2922 G.postRedisplay "listview right";
2923 coe {< m_pan = m_pan - 1 >}
2925 | 0xff51 | 0xff96 -> (* (kp) left *)
2926 state.text <- "";
2927 G.postRedisplay "listview left";
2928 coe {< m_pan = m_pan + 1 >}
2930 | 0xff50 | 0xff95 -> (* (kp) home *)
2931 let active = find 0 1 in
2932 G.postRedisplay "listview home";
2933 set active 0;
2935 | 0xff57 | 0xff9c -> (* (kp) end *)
2936 let first = max 0 (itemcount - fstate.maxrows) in
2937 let active = find (itemcount - 1) ~-1 in
2938 G.postRedisplay "listview end";
2939 set active first;
2941 | key when (key = 0 || key land 0xff00 = 0xff00) ->
2942 coe self
2944 | _ ->
2945 dolog "listview unknown key %#x" key; coe self
2947 method key key mask =
2948 match state.mode with
2949 | Textentry te -> textentrykeyboard key mask te; coe self
2950 | _ -> self#key1 key mask
2952 method button button down x y _ =
2953 let opt =
2954 match button with
2955 | 1 when x > state.winw - conf.scrollbw ->
2956 G.postRedisplay "listview scroll";
2957 if down
2958 then
2959 let _, position, sh = self#scrollph in
2960 if y > truncate position && y < truncate (position +. sh)
2961 then (
2962 state.mstate <- Mscrolly;
2963 Some (coe self)
2965 else
2966 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
2967 let first = truncate (s *. float source#getitemcount) in
2968 let first = min source#getitemcount first in
2969 Some (coe {< m_first = first; m_active = first >})
2970 else (
2971 state.mstate <- Mnone;
2972 Some (coe self);
2974 | 1 when not down ->
2975 begin match self#elemunder y with
2976 | Some n ->
2977 G.postRedisplay "listview click";
2978 source#exit (coe {< m_active = n >}) false n m_first m_pan
2979 | _ ->
2980 Some (coe self)
2982 | n when (n == 4 || n == 5) && not down ->
2983 let len = source#getitemcount in
2984 let first =
2985 if n = 5 && m_first + fstate.maxrows >= len
2986 then
2987 m_first
2988 else
2989 let first = m_first + (if n == 4 then -1 else 1) in
2990 bound first 0 (len - 1)
2992 G.postRedisplay "listview wheel";
2993 Some (coe {< m_first = first >})
2994 | n when (n = 6 || n = 7) && not down ->
2995 let inc = if n = 7 then -1 else 1 in
2996 G.postRedisplay "listview hwheel";
2997 Some (coe {< m_pan = m_pan + inc >})
2998 | _ ->
2999 Some (coe self)
3001 match opt with
3002 | None -> m_prev_uioh
3003 | Some uioh -> uioh
3005 method multiclick _ x y = self#button 1 true x y
3007 method motion _ y =
3008 match state.mstate with
3009 | Mscrolly ->
3010 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3011 let first = truncate (s *. float source#getitemcount) in
3012 let first = min source#getitemcount first in
3013 G.postRedisplay "listview motion";
3014 coe {< m_first = first; m_active = first >}
3015 | _ -> coe self
3017 method pmotion x y =
3018 if x < state.winw - conf.scrollbw
3019 then
3020 let n =
3021 match self#elemunder y with
3022 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3023 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3025 let o =
3026 if n != m_active
3027 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3028 else self
3030 coe o
3031 else (
3032 Wsi.setcursor Wsi.CURSOR_INHERIT;
3033 coe self
3036 method infochanged _ = ()
3038 method scrollpw = (0, 0.0, 0.0)
3039 method scrollph =
3040 let nfs = fstate.fontsize + 1 in
3041 let y = m_first * nfs in
3042 let itemcount = source#getitemcount in
3043 let maxi = max 0 (itemcount - fstate.maxrows) in
3044 let maxy = maxi * nfs in
3045 let p, h = scrollph y maxy in
3046 conf.scrollbw, p, h
3048 method modehash = modehash
3049 method eformsgs = false
3050 end;;
3052 class outlinelistview ~source =
3053 let settext autonarrow s =
3054 if autonarrow
3055 then
3056 let ss = source#statestr in
3057 state.text <-
3058 if emptystr ss
3059 then "[" ^ s ^ "]"
3060 else "{" ^ ss ^ "} [" ^ s ^ "]"
3061 else state.text <- s
3063 object (self)
3064 inherit listview
3065 ~helpmode:false
3066 ~source:(source :> lvsource)
3067 ~trusted:false
3068 ~modehash:(findkeyhash conf "outline")
3069 as super
3071 val m_autonarrow = false
3073 method display = super#display
3075 method key key mask =
3076 let maxrows =
3077 if emptystr state.text
3078 then fstate.maxrows
3079 else fstate.maxrows - 2
3081 let calcfirst first active =
3082 if active > first
3083 then
3084 let rows = active - first in
3085 if rows > maxrows then active - maxrows else first
3086 else active
3088 let navigate incr =
3089 let active = m_active + incr in
3090 let active = bound active 0 (source#getitemcount - 1) in
3091 let first = calcfirst m_first active in
3092 G.postRedisplay "outline navigate";
3093 coe {< m_active = active; m_first = first >}
3095 let navscroll first =
3096 let active =
3097 let dist = m_active - first in
3098 if dist < 0
3099 then first
3100 else (
3101 if dist < maxrows
3102 then m_active
3103 else first + maxrows
3106 G.postRedisplay "outline navscroll";
3107 coe {< m_first = first; m_active = active >}
3109 let ctrl = Wsi.withctrl mask in
3110 match key with
3111 | 97 when ctrl -> (* ctrl-a *)
3112 let text =
3113 if m_autonarrow
3114 then (source#denarrow; "")
3115 else (
3116 let pattern = source#renarrow in
3117 if nonemptystr m_qsearch
3118 then (source#narrow m_qsearch; m_qsearch)
3119 else pattern
3122 settext (not m_autonarrow) text;
3123 G.postRedisplay "toggle auto narrowing";
3124 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3126 | 47 when emptystr m_qsearch && not m_autonarrow -> (* / *)
3127 settext true "";
3128 G.postRedisplay "toggle auto narrowing";
3129 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3131 | 110 when ctrl -> (* ctrl-n *)
3132 source#narrow m_qsearch;
3133 if not m_autonarrow
3134 then source#add_narrow_pattern m_qsearch;
3135 G.postRedisplay "outline ctrl-n";
3136 coe {< m_first = 0; m_active = 0 >}
3138 | 83 when ctrl -> (* ctrl-S *)
3139 let active = source#calcactive (getanchor ()) in
3140 let first = firstof m_first active in
3141 G.postRedisplay "outline ctrl-s";
3142 coe {< m_first = first; m_active = active >}
3144 | 117 when ctrl -> (* ctrl-u *)
3145 G.postRedisplay "outline ctrl-u";
3146 if m_autonarrow && nonemptystr m_qsearch
3147 then (
3148 ignore (source#renarrow);
3149 settext m_autonarrow "";
3150 coe {< m_first = 0; m_active = 0; m_qsearch = "" >}
3152 else (
3153 source#del_narrow_pattern;
3154 let pattern = source#renarrow in
3155 let text =
3156 if emptystr pattern then "" else "Narrowed to " ^ pattern
3158 settext m_autonarrow text;
3159 coe {< m_first = 0; m_active = 0; m_qsearch = "" >}
3162 | 108 when ctrl -> (* ctrl-l *)
3163 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3164 G.postRedisplay "outline ctrl-l";
3165 coe {< m_first = first >}
3167 | 0xff09 when m_autonarrow -> (* tab *)
3168 if nonemptystr m_qsearch
3169 then (
3170 G.postRedisplay "outline list view tab";
3171 source#add_narrow_pattern m_qsearch;
3172 settext true "";
3173 coe {< m_qsearch = "" >}
3175 else coe self
3177 | 0xff1b when m_autonarrow -> (* escape *)
3178 if nonemptystr m_qsearch
3179 then source#add_narrow_pattern m_qsearch;
3180 super#key key mask
3182 | 0xff0d | 0xff8d when m_autonarrow -> (* (kp) enter *)
3183 if nonemptystr m_qsearch
3184 then source#add_narrow_pattern m_qsearch;
3185 super#key key mask
3187 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
3188 let pattern = m_qsearch ^ toutf8 key in
3189 G.postRedisplay "outlinelistview autonarrow add";
3190 source#narrow pattern;
3191 settext true pattern;
3192 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3194 | key when m_autonarrow && key = 0xff08 -> (* backspace *)
3195 if emptystr m_qsearch
3196 then coe self
3197 else
3198 let pattern = withoutlastutf8 m_qsearch in
3199 G.postRedisplay "outlinelistview autonarrow backspace";
3200 ignore (source#renarrow);
3201 source#narrow pattern;
3202 settext true pattern;
3203 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3205 | 0xff9f | 0xffff -> (* (kp) delete *)
3206 source#remove m_active;
3207 G.postRedisplay "outline delete";
3208 let active = max 0 (m_active-1) in
3209 coe {< m_first = firstof m_first active;
3210 m_active = active >}
3212 | 0xff52 | 0xff97 when ctrl -> (* ctrl-(kp) up *)
3213 navscroll (max 0 (m_first - 1))
3215 | 0xff54 | 0xff99 when ctrl -> (* ctrl-(kp) down *)
3216 navscroll (min (source#getitemcount - 1) (m_first + 1))
3218 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3219 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3220 | 0xff55 | 0xff9a -> (* (kp) prior *)
3221 navigate ~-(fstate.maxrows)
3222 | 0xff56 | 0xff9b -> (* (kp) next *)
3223 navigate fstate.maxrows
3225 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3226 let o =
3227 if ctrl
3228 then (
3229 G.postRedisplay "outline ctrl right";
3230 {< m_pan = m_pan + 1 >}
3232 else self#updownlevel 1
3234 coe o
3236 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3237 let o =
3238 if ctrl
3239 then (
3240 G.postRedisplay "outline ctrl left";
3241 {< m_pan = m_pan - 1 >}
3243 else self#updownlevel ~-1
3245 coe o
3247 | 0xff50 | 0xff95 -> (* (kp) home *)
3248 G.postRedisplay "outline home";
3249 coe {< m_first = 0; m_active = 0 >}
3251 | 0xff57 | 0xff9c -> (* (kp) end *)
3252 let active = source#getitemcount - 1 in
3253 let first = max 0 (active - fstate.maxrows) in
3254 G.postRedisplay "outline end";
3255 coe {< m_active = active; m_first = first >}
3257 | _ -> super#key key mask
3260 let gotounder under =
3261 let getpath filename =
3262 let path =
3263 if nonemptystr filename
3264 then
3265 if Filename.is_relative filename
3266 then
3267 let dir = Filename.dirname state.path in
3268 let dir =
3269 if Filename.is_implicit dir
3270 then Filename.concat (Sys.getcwd ()) dir
3271 else dir
3273 Filename.concat dir filename
3274 else filename
3275 else ""
3277 if Sys.file_exists path
3278 then path
3279 else ""
3281 match under with
3282 | Ulinkgoto (pageno, top) ->
3283 if pageno >= 0
3284 then (
3285 addnav ();
3286 gotopage1 pageno top;
3289 | Ulinkuri s ->
3290 gotouri s
3292 | Uremote (filename, pageno) ->
3293 let path = getpath filename in
3294 if nonemptystr path
3295 then (
3296 if conf.riani
3297 then
3298 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
3299 try popen command []
3300 with exn ->
3301 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
3302 flush stderr;
3303 else
3304 let anchor = getanchor () in
3305 let ranchor = state.path, state.password, anchor, state.origin in
3306 state.origin <- "";
3307 state.anchor <- (pageno, 0.0, 0.0);
3308 state.ranchors <- ranchor :: state.ranchors;
3309 opendoc path "";
3311 else showtext '!' ("Could not find " ^ filename)
3313 | Uremotedest (filename, destname) ->
3314 let path = getpath filename in
3315 if nonemptystr path
3316 then (
3317 if conf.riani
3318 then
3319 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
3320 try popen command []
3321 with exn ->
3322 Printf.eprintf
3323 "failed to execute `%s': %s\n" command (exntos exn);
3324 flush stderr;
3325 else
3326 let anchor = getanchor () in
3327 let ranchor = state.path, state.password, anchor, state.origin in
3328 state.origin <- "";
3329 state.nameddest <- destname;
3330 state.ranchors <- ranchor :: state.ranchors;
3331 opendoc path "";
3333 else showtext '!' ("Could not find " ^ filename)
3335 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
3338 let gotooutline (_, _, kind) =
3339 match kind with
3340 | Onone -> ()
3341 | Oanchor anchor ->
3342 let (pageno, y, _) = anchor in
3343 let y = getanchory
3344 (if conf.presentation then (pageno, y, 1.0) else anchor)
3346 addnav ();
3347 gotoghyll y
3348 | Ouri uri -> gotounder (Ulinkuri uri)
3349 | Olaunch cmd -> gotounder (Ulaunch cmd)
3350 | Oremote remote -> gotounder (Uremote remote)
3351 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
3354 let outlinesource usebookmarks =
3355 let empty = [||] in
3356 (object (self)
3357 inherit lvsourcebase
3358 val mutable m_items = empty
3359 val mutable m_minfo = empty
3360 val mutable m_orig_items = empty
3361 val mutable m_orig_minfo = empty
3362 val mutable m_narrow_patterns = []
3363 val mutable m_hadremovals = false
3364 val mutable m_gen = -1
3366 method getitemcount =
3367 Array.length m_items + (if m_hadremovals then 1 else 0)
3369 method getitem n =
3370 if n == Array.length m_items && m_hadremovals
3371 then
3372 ("[Confirm removal]", 0)
3373 else
3374 let s, n, _ = m_items.(n) in
3375 (s, n)
3377 method exit ~uioh ~cancel ~active ~first ~pan =
3378 ignore (uioh, first);
3379 let confrimremoval = m_hadremovals && active = Array.length m_items in
3380 let items, minfo =
3381 if m_narrow_patterns = []
3382 then m_orig_items, m_orig_minfo
3383 else m_items, m_minfo
3385 if not cancel
3386 then (
3387 if not confrimremoval
3388 then (
3389 gotooutline m_items.(active);
3390 m_items <- items;
3391 m_minfo <- minfo;
3393 else (
3394 state.bookmarks <- Array.to_list m_items;
3395 m_orig_items <- m_items;
3396 m_orig_minfo <- m_minfo;
3399 else (
3400 m_items <- items;
3401 m_minfo <- minfo;
3403 m_pan <- pan;
3404 None
3406 method hasaction _ = true
3408 method greetmsg =
3409 if Array.length m_items != Array.length m_orig_items
3410 then
3411 let s =
3412 match m_narrow_patterns with
3413 | one :: [] -> one
3414 | many -> String.concat "\xe2\x80\xa6" (List.rev many)
3416 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
3417 else ""
3419 method statestr =
3420 match m_narrow_patterns with
3421 | [] -> ""
3422 | one :: [] -> one
3423 | head :: _ -> "\xe2\x80\xa6" ^ head
3425 method narrow pattern =
3426 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3427 match reopt with
3428 | None -> ()
3429 | Some re ->
3430 let rec loop accu minfo n =
3431 if n = -1
3432 then (
3433 m_items <- Array.of_list accu;
3434 m_minfo <- Array.of_list minfo;
3436 else
3437 let (s, _, _) as o = m_items.(n) in
3438 let accu, minfo =
3439 let first =
3440 try Str.search_forward re s 0
3441 with Not_found -> -1
3443 if first >= 0
3444 then o :: accu, (first, Str.match_end ()) :: minfo
3445 else accu, minfo
3447 loop accu minfo (n-1)
3449 loop [] [] (Array.length m_items - 1)
3451 method getminfo = m_minfo
3453 method denarrow =
3454 m_orig_items <- (
3455 if usebookmarks
3456 then Array.of_list state.bookmarks
3457 else state.outlines
3459 m_minfo <- m_orig_minfo;
3460 m_items <- m_orig_items
3462 method remove m =
3463 if usebookmarks
3464 then
3465 if m >= 0 && m < Array.length m_items
3466 then (
3467 m_hadremovals <- true;
3468 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3469 let n = if n >= m then n+1 else n in
3470 m_items.(n)
3474 method add_narrow_pattern pattern =
3475 m_narrow_patterns <- pattern :: m_narrow_patterns
3477 method del_narrow_pattern =
3478 match m_narrow_patterns with
3479 | _ :: rest -> m_narrow_patterns <- rest
3480 | [] -> ()
3482 method renarrow =
3483 self#denarrow;
3484 match m_narrow_patterns with
3485 | pattern :: [] -> self#narrow pattern; pattern
3486 | list ->
3487 List.fold_left (fun accu pattern ->
3488 self#narrow pattern;
3489 pattern ^ "\xe2\x80\xa6" ^ accu) "" list
3491 method calcactive anchor =
3492 let rely = getanchory anchor in
3493 let rec loop n best bestd =
3494 if n = Array.length m_items
3495 then best
3496 else
3497 let _, _, kind = m_items.(n) in
3498 match kind with
3499 | Oanchor anchor ->
3500 let orely = getanchory anchor in
3501 let d = abs (orely - rely) in
3502 if d < bestd
3503 then loop (n+1) n d
3504 else loop (n+1) best bestd
3505 | Onone | Oremote _ | Olaunch _ | Oremotedest _ | Ouri _ ->
3506 loop (n+1) best bestd
3508 loop 0 ~-1 max_int
3510 method reset anchor items =
3511 m_hadremovals <- false;
3512 if state.gen != m_gen
3513 then (
3514 m_orig_items <- items;
3515 m_items <- items;
3516 m_narrow_patterns <- [];
3517 m_minfo <- empty;
3518 m_orig_minfo <- empty;
3519 m_gen <- state.gen;
3521 else (
3522 if items != m_orig_items
3523 then (
3524 m_orig_items <- items;
3525 if m_narrow_patterns == []
3526 then m_items <- items;
3529 let active = self#calcactive anchor in
3530 m_active <- active;
3531 m_first <- firstof m_first active
3532 end)
3535 let enterselector usebookmarks =
3536 resetmstate ();
3537 let source = outlinesource usebookmarks in
3538 fun errmsg ->
3539 let outlines =
3540 if usebookmarks
3541 then Array.of_list state.bookmarks
3542 else state.outlines
3544 if Array.length outlines = 0
3545 then (
3546 showtext ' ' errmsg;
3548 else (
3549 state.text <- source#greetmsg;
3550 Wsi.setcursor Wsi.CURSOR_INHERIT;
3551 let anchor = getanchor () in
3552 source#reset anchor outlines;
3553 state.uioh <- coe (new outlinelistview ~source);
3554 G.postRedisplay "enter selector";
3558 let enteroutlinemode =
3559 let f = enterselector false in
3560 fun () -> f "Document has no outline";
3563 let enterbookmarkmode =
3564 let f = enterselector true in
3565 fun () -> f "Document has no bookmarks (yet)";
3568 let makecheckers () =
3569 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3570 following to say:
3571 converted by Issac Trotts. July 25, 2002 *)
3572 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3573 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3574 let id = GlTex.gen_texture () in
3575 GlTex.bind_texture `texture_2d id;
3576 GlPix.store (`unpack_alignment 1);
3577 GlTex.image2d image;
3578 List.iter (GlTex.parameter ~target:`texture_2d)
3579 [ `mag_filter `nearest; `min_filter `nearest ];
3583 let setcheckers enabled =
3584 match state.checkerstexid with
3585 | None ->
3586 if enabled then state.checkerstexid <- Some (makecheckers ())
3588 | Some checkerstexid ->
3589 if not enabled
3590 then (
3591 GlTex.delete_texture checkerstexid;
3592 state.checkerstexid <- None;
3596 let describe_location () =
3597 let fn = page_of_y state.y in
3598 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
3599 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3600 let percent =
3601 if maxy <= 0
3602 then 100.
3603 else (100. *. (float state.y /. float maxy))
3605 if fn = ln
3606 then
3607 Printf.sprintf "page %d of %d [%.2f%%]"
3608 (fn+1) state.pagecount percent
3609 else
3610 Printf.sprintf
3611 "pages %d-%d of %d [%.2f%%]"
3612 (fn+1) (ln+1) state.pagecount percent
3615 let setpresentationmode v =
3616 let n = page_of_y state.y in
3617 state.anchor <- (n, 0.0, 1.0);
3618 conf.presentation <- v;
3619 if conf.fitmodel = FitPage
3620 then reqlayout conf.angle conf.fitmodel;
3621 represent ();
3624 let enterinfomode =
3625 let btos b = if b then "\xe2\x88\x9a" else "" in
3626 let showextended = ref false in
3627 let leave mode = function
3628 | Confirm -> state.mode <- mode
3629 | Cancel -> state.mode <- mode in
3630 let src =
3631 (object
3632 val mutable m_first_time = true
3633 val mutable m_l = []
3634 val mutable m_a = [||]
3635 val mutable m_prev_uioh = nouioh
3636 val mutable m_prev_mode = View
3638 inherit lvsourcebase
3640 method reset prev_mode prev_uioh =
3641 m_a <- Array.of_list (List.rev m_l);
3642 m_l <- [];
3643 m_prev_mode <- prev_mode;
3644 m_prev_uioh <- prev_uioh;
3645 if m_first_time
3646 then (
3647 let rec loop n =
3648 if n >= Array.length m_a
3649 then ()
3650 else
3651 match m_a.(n) with
3652 | _, _, _, Action _ -> m_active <- n
3653 | _ -> loop (n+1)
3655 loop 0;
3656 m_first_time <- false;
3659 method int name get set =
3660 m_l <-
3661 (name, `int get, 1, Action (
3662 fun u ->
3663 let ondone s =
3664 try set (int_of_string s)
3665 with exn ->
3666 state.text <- Printf.sprintf "bad integer `%s': %s"
3667 s (exntos exn)
3669 state.text <- "";
3670 let te = name ^ ": ", "", None, intentry, ondone, true in
3671 state.mode <- Textentry (te, leave m_prev_mode);
3673 )) :: m_l
3675 method int_with_suffix name get set =
3676 m_l <-
3677 (name, `intws get, 1, Action (
3678 fun u ->
3679 let ondone s =
3680 try set (int_of_string_with_suffix s)
3681 with exn ->
3682 state.text <- Printf.sprintf "bad integer `%s': %s"
3683 s (exntos exn)
3685 state.text <- "";
3686 let te =
3687 name ^ ": ", "", None, intentry_with_suffix, ondone, true
3689 state.mode <- Textentry (te, leave m_prev_mode);
3691 )) :: m_l
3693 method bool ?(offset=1) ?(btos=btos) name get set =
3694 m_l <-
3695 (name, `bool (btos, get), offset, Action (
3696 fun u ->
3697 let v = get () in
3698 set (not v);
3700 )) :: m_l
3702 method color name get set =
3703 m_l <-
3704 (name, `color get, 1, Action (
3705 fun u ->
3706 let invalid = (nan, nan, nan) in
3707 let ondone s =
3708 let c =
3709 try color_of_string s
3710 with exn ->
3711 state.text <- Printf.sprintf "bad color `%s': %s"
3712 s (exntos exn);
3713 invalid
3715 if c <> invalid
3716 then set c;
3718 let te = name ^ ": ", "", None, textentry, ondone, true in
3719 state.text <- color_to_string (get ());
3720 state.mode <- Textentry (te, leave m_prev_mode);
3722 )) :: m_l
3724 method string name get set =
3725 m_l <-
3726 (name, `string get, 1, Action (
3727 fun u ->
3728 let ondone s = set s in
3729 let te = name ^ ": ", "", None, textentry, ondone, true in
3730 state.mode <- Textentry (te, leave m_prev_mode);
3732 )) :: m_l
3734 method colorspace name get set =
3735 m_l <-
3736 (name, `string get, 1, Action (
3737 fun _ ->
3738 let source =
3739 (object
3740 inherit lvsourcebase
3742 initializer
3743 m_active <- CSTE.to_int conf.colorspace;
3744 m_first <- 0;
3746 method getitemcount =
3747 Array.length CSTE.names
3748 method getitem n =
3749 (CSTE.names.(n), 0)
3750 method exit ~uioh ~cancel ~active ~first ~pan =
3751 ignore (uioh, first, pan);
3752 if not cancel then set active;
3753 None
3754 method hasaction _ = true
3755 end)
3757 state.text <- "";
3758 let modehash = findkeyhash conf "info" in
3759 coe (new listview ~helpmode:false ~source ~trusted:true ~modehash)
3760 )) :: m_l
3762 method paxmark name get set =
3763 m_l <-
3764 (name, `string get, 1, Action (
3765 fun _ ->
3766 let source =
3767 (object
3768 inherit lvsourcebase
3770 initializer
3771 m_active <- MTE.to_int conf.paxmark;
3772 m_first <- 0;
3774 method getitemcount = Array.length MTE.names
3775 method getitem n = (MTE.names.(n), 0)
3776 method exit ~uioh ~cancel ~active ~first ~pan =
3777 ignore (uioh, first, pan);
3778 if not cancel then set active;
3779 None
3780 method hasaction _ = true
3781 end)
3783 state.text <- "";
3784 let modehash = findkeyhash conf "info" in
3785 coe (new listview ~helpmode:false ~source ~trusted:true ~modehash)
3786 )) :: m_l
3788 method fitmodel name get set =
3789 m_l <-
3790 (name, `string get, 1, Action (
3791 fun _ ->
3792 let source =
3793 (object
3794 inherit lvsourcebase
3796 initializer
3797 m_active <- FMTE.to_int conf.fitmodel;
3798 m_first <- 0;
3800 method getitemcount = Array.length FMTE.names
3801 method getitem n = (FMTE.names.(n), 0)
3802 method exit ~uioh ~cancel ~active ~first ~pan =
3803 ignore (uioh, first, pan);
3804 if not cancel then set active;
3805 None
3806 method hasaction _ = true
3807 end)
3809 state.text <- "";
3810 let modehash = findkeyhash conf "info" in
3811 coe (new listview ~helpmode:false ~source ~trusted:true ~modehash)
3812 )) :: m_l
3814 method caption s offset =
3815 m_l <- (s, `empty, offset, Noaction) :: m_l
3817 method caption2 s f offset =
3818 m_l <- (s, `string f, offset, Noaction) :: m_l
3820 method getitemcount = Array.length m_a
3822 method getitem n =
3823 let tostr = function
3824 | `int f -> string_of_int (f ())
3825 | `intws f -> string_with_suffix_of_int (f ())
3826 | `string f -> f ()
3827 | `color f -> color_to_string (f ())
3828 | `bool (btos, f) -> btos (f ())
3829 | `empty -> ""
3831 let name, t, offset, _ = m_a.(n) in
3832 ((let s = tostr t in
3833 if nonemptystr s
3834 then Printf.sprintf "%s\t%s" name s
3835 else name),
3836 offset)
3838 method exit ~uioh ~cancel ~active ~first ~pan =
3839 let uiohopt =
3840 if not cancel
3841 then (
3842 let uioh =
3843 match m_a.(active) with
3844 | _, _, _, Action f -> f uioh
3845 | _ -> uioh
3847 Some uioh
3849 else None
3851 m_active <- active;
3852 m_first <- first;
3853 m_pan <- pan;
3854 uiohopt
3856 method hasaction n =
3857 match m_a.(n) with
3858 | _, _, _, Action _ -> true
3859 | _ -> false
3860 end)
3862 let rec fillsrc prevmode prevuioh =
3863 let sep () = src#caption "" 0 in
3864 let colorp name get set =
3865 src#string name
3866 (fun () -> color_to_string (get ()))
3867 (fun v ->
3869 let c = color_of_string v in
3870 set c
3871 with exn ->
3872 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
3875 let oldmode = state.mode in
3876 let birdseye = isbirdseye state.mode in
3878 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3880 src#bool "presentation mode"
3881 (fun () -> conf.presentation)
3882 (fun v -> setpresentationmode v);
3884 src#bool "ignore case in searches"
3885 (fun () -> conf.icase)
3886 (fun v -> conf.icase <- v);
3888 src#bool "preload"
3889 (fun () -> conf.preload)
3890 (fun v -> conf.preload <- v);
3892 src#bool "highlight links"
3893 (fun () -> conf.hlinks)
3894 (fun v -> conf.hlinks <- v);
3896 src#bool "under info"
3897 (fun () -> conf.underinfo)
3898 (fun v -> conf.underinfo <- v);
3900 src#bool "persistent bookmarks"
3901 (fun () -> conf.savebmarks)
3902 (fun v -> conf.savebmarks <- v);
3904 src#fitmodel "fit model"
3905 (fun () -> FMTE.to_string conf.fitmodel)
3906 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3908 src#bool "trim margins"
3909 (fun () -> conf.trimmargins)
3910 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3912 src#bool "persistent location"
3913 (fun () -> conf.jumpback)
3914 (fun v -> conf.jumpback <- v);
3916 sep ();
3917 src#int "inter-page space"
3918 (fun () -> conf.interpagespace)
3919 (fun n ->
3920 conf.interpagespace <- n;
3921 docolumns conf.columns;
3922 let pageno, py =
3923 match state.layout with
3924 | [] -> 0, 0
3925 | l :: _ ->
3926 l.pageno, l.pagey
3928 state.maxy <- calcheight ();
3929 let y = getpagey pageno in
3930 gotoy (y + py)
3933 src#int "page bias"
3934 (fun () -> conf.pagebias)
3935 (fun v -> conf.pagebias <- v);
3937 src#int "scroll step"
3938 (fun () -> conf.scrollstep)
3939 (fun n -> conf.scrollstep <- n);
3941 src#int "horizontal scroll step"
3942 (fun () -> conf.hscrollstep)
3943 (fun v -> conf.hscrollstep <- v);
3945 src#int "auto scroll step"
3946 (fun () ->
3947 match state.autoscroll with
3948 | Some step -> step
3949 | _ -> conf.autoscrollstep)
3950 (fun n ->
3951 let n = boundastep state.winh n in
3952 if state.autoscroll <> None
3953 then state.autoscroll <- Some n;
3954 conf.autoscrollstep <- n);
3956 src#int "zoom"
3957 (fun () -> truncate (conf.zoom *. 100.))
3958 (fun v -> setzoom ((float v) /. 100.));
3960 src#int "rotation"
3961 (fun () -> conf.angle)
3962 (fun v -> reqlayout v conf.fitmodel);
3964 src#int "scroll bar width"
3965 (fun () -> conf.scrollbw)
3966 (fun v ->
3967 conf.scrollbw <- v;
3968 reshape state.winw state.winh;
3971 src#int "scroll handle height"
3972 (fun () -> conf.scrollh)
3973 (fun v -> conf.scrollh <- v;);
3975 src#int "thumbnail width"
3976 (fun () -> conf.thumbw)
3977 (fun v ->
3978 conf.thumbw <- min 4096 v;
3979 match oldmode with
3980 | Birdseye beye ->
3981 leavebirdseye beye false;
3982 enterbirdseye ()
3983 | _ -> ()
3986 let mode = state.mode in
3987 src#string "columns"
3988 (fun () ->
3989 match conf.columns with
3990 | Csingle _ -> "1"
3991 | Cmulti (multi, _) -> multicolumns_to_string multi
3992 | Csplit (count, _) -> "-" ^ string_of_int count
3994 (fun v ->
3995 let n, a, b = multicolumns_of_string v in
3996 setcolumns mode n a b);
3998 sep ();
3999 src#caption "Pixmap cache" 0;
4000 src#int_with_suffix "size (advisory)"
4001 (fun () -> conf.memlimit)
4002 (fun v -> conf.memlimit <- v);
4004 src#caption2 "used"
4005 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4006 (string_with_suffix_of_int state.memused)
4007 (Hashtbl.length state.tilemap)) 1;
4009 sep ();
4010 src#caption "Layout" 0;
4011 src#caption2 "Dimension"
4012 (fun () ->
4013 Printf.sprintf "%dx%d (virtual %dx%d)"
4014 state.winw state.winh
4015 state.w state.maxy)
4017 if conf.debug
4018 then
4019 src#caption2 "Position" (fun () ->
4020 Printf.sprintf "%dx%d" state.x state.y
4022 else
4023 src#caption2 "Position" (fun () -> describe_location ()) 1
4026 sep ();
4027 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4028 "Save these parameters as global defaults at exit"
4029 (fun () -> conf.bedefault)
4030 (fun v -> conf.bedefault <- v)
4033 sep ();
4034 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4035 src#bool ~offset:0 ~btos "Extended parameters"
4036 (fun () -> !showextended)
4037 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4038 if !showextended
4039 then (
4040 src#bool "checkers"
4041 (fun () -> conf.checkers)
4042 (fun v -> conf.checkers <- v; setcheckers v);
4043 src#bool "update cursor"
4044 (fun () -> conf.updatecurs)
4045 (fun v -> conf.updatecurs <- v);
4046 src#bool "scroll-bar on the left"
4047 (fun () -> conf.leftscroll)
4048 (fun v -> conf.leftscroll <- v);
4049 src#bool "verbose"
4050 (fun () -> conf.verbose)
4051 (fun v -> conf.verbose <- v);
4052 src#bool "invert colors"
4053 (fun () -> conf.invert)
4054 (fun v -> conf.invert <- v);
4055 src#bool "max fit"
4056 (fun () -> conf.maxhfit)
4057 (fun v -> conf.maxhfit <- v);
4058 src#bool "redirect stderr"
4059 (fun () -> conf.redirectstderr)
4060 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4061 src#bool "pax mode"
4062 (fun () -> conf.pax != None)
4063 (fun v ->
4064 if v
4065 then conf.pax <- Some (ref (now (), 0, 0))
4066 else conf.pax <- None);
4067 src#string "uri launcher"
4068 (fun () -> conf.urilauncher)
4069 (fun v -> conf.urilauncher <- v);
4070 src#string "path launcher"
4071 (fun () -> conf.pathlauncher)
4072 (fun v -> conf.pathlauncher <- v);
4073 src#string "tile size"
4074 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4075 (fun v ->
4077 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4078 conf.tilew <- max 64 w;
4079 conf.tileh <- max 64 h;
4080 flushtiles ();
4081 with exn ->
4082 state.text <- Printf.sprintf "bad tile size `%s': %s"
4083 v (exntos exn)
4085 src#int "texture count"
4086 (fun () -> conf.texcount)
4087 (fun v ->
4088 if realloctexts v
4089 then conf.texcount <- v
4090 else showtext '!' " Failed to set texture count please retry later"
4092 src#int "slice height"
4093 (fun () -> conf.sliceheight)
4094 (fun v ->
4095 conf.sliceheight <- v;
4096 wcmd "sliceh %d" conf.sliceheight;
4098 src#int "anti-aliasing level"
4099 (fun () -> conf.aalevel)
4100 (fun v ->
4101 conf.aalevel <- bound v 0 8;
4102 state.anchor <- getanchor ();
4103 opendoc state.path state.password;
4105 src#string "page scroll scaling factor"
4106 (fun () -> string_of_float conf.pgscale)
4107 (fun v ->
4109 let s = float_of_string v in
4110 conf.pgscale <- s
4111 with exn ->
4112 state.text <- Printf.sprintf
4113 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4116 src#int "ui font size"
4117 (fun () -> fstate.fontsize)
4118 (fun v -> setfontsize (bound v 5 100));
4119 src#int "hint font size"
4120 (fun () -> conf.hfsize)
4121 (fun v -> conf.hfsize <- bound v 5 100);
4122 colorp "background color"
4123 (fun () -> conf.bgcolor)
4124 (fun v -> conf.bgcolor <- v);
4125 src#bool "crop hack"
4126 (fun () -> conf.crophack)
4127 (fun v -> conf.crophack <- v);
4128 src#string "trim fuzz"
4129 (fun () -> irect_to_string conf.trimfuzz)
4130 (fun v ->
4132 conf.trimfuzz <- irect_of_string v;
4133 if conf.trimmargins
4134 then settrim true conf.trimfuzz;
4135 with exn ->
4136 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4138 src#string "throttle"
4139 (fun () ->
4140 match conf.maxwait with
4141 | None -> "show place holder if page is not ready"
4142 | Some time ->
4143 if time = infinity
4144 then "wait for page to fully render"
4145 else
4146 "wait " ^ string_of_float time
4147 ^ " seconds before showing placeholder"
4149 (fun v ->
4151 let f = float_of_string v in
4152 if f <= 0.0
4153 then conf.maxwait <- None
4154 else conf.maxwait <- Some f
4155 with exn ->
4156 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4158 src#string "ghyll scroll"
4159 (fun () ->
4160 match conf.ghyllscroll with
4161 | None -> ""
4162 | Some nab -> ghyllscroll_to_string nab
4164 (fun v ->
4165 try conf.ghyllscroll <- ghyllscroll_of_string v
4166 with exn ->
4167 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4169 src#string "selection command"
4170 (fun () -> conf.selcmd)
4171 (fun v -> conf.selcmd <- v);
4172 src#string "synctex command"
4173 (fun () -> conf.stcmd)
4174 (fun v -> conf.stcmd <- v);
4175 src#string "pax command"
4176 (fun () -> conf.paxcmd)
4177 (fun v -> conf.paxcmd <- v);
4178 src#colorspace "color space"
4179 (fun () -> CSTE.to_string conf.colorspace)
4180 (fun v ->
4181 conf.colorspace <- CSTE.of_int v;
4182 wcmd "cs %d" v;
4183 load state.layout;
4185 src#paxmark "pax mark method"
4186 (fun () -> MTE.to_string conf.paxmark)
4187 (fun v -> conf.paxmark <- MTE.of_int v);
4188 if pbousable ()
4189 then
4190 src#bool "use PBO"
4191 (fun () -> conf.usepbo)
4192 (fun v -> conf.usepbo <- v);
4193 src#bool "mouse wheel scrolls pages"
4194 (fun () -> conf.wheelbypage)
4195 (fun v -> conf.wheelbypage <- v);
4196 src#bool "open remote links in a new instance"
4197 (fun () -> conf.riani)
4198 (fun v -> conf.riani <- v);
4201 sep ();
4202 src#caption "Document" 0;
4203 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4204 src#caption2 "Pages"
4205 (fun () -> string_of_int state.pagecount) 1;
4206 src#caption2 "Dimensions"
4207 (fun () -> string_of_int (List.length state.pdims)) 1;
4208 if conf.trimmargins
4209 then (
4210 sep ();
4211 src#caption "Trimmed margins" 0;
4212 src#caption2 "Dimensions"
4213 (fun () -> string_of_int (List.length state.pdims)) 1;
4216 sep ();
4217 src#caption "OpenGL" 0;
4218 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4219 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4221 sep ();
4222 src#caption "Location" 0;
4223 if nonemptystr state.origin
4224 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4225 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4227 src#reset prevmode prevuioh;
4229 fun () ->
4230 state.text <- "";
4231 resetmstate ();
4232 let prevmode = state.mode
4233 and prevuioh = state.uioh in
4234 fillsrc prevmode prevuioh;
4235 let source = (src :> lvsource) in
4236 let modehash = findkeyhash conf "info" in
4237 state.uioh <- coe (object (self)
4238 inherit listview ~helpmode:false ~source ~trusted:true ~modehash as super
4239 val mutable m_prevmemused = 0
4240 method infochanged = function
4241 | Memused ->
4242 if m_prevmemused != state.memused
4243 then (
4244 m_prevmemused <- state.memused;
4245 G.postRedisplay "memusedchanged";
4247 | Pdim -> G.postRedisplay "pdimchanged"
4248 | Docinfo -> fillsrc prevmode prevuioh
4250 method key key mask =
4251 if not (Wsi.withctrl mask)
4252 then
4253 match key with
4254 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4255 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4256 | _ -> super#key key mask
4257 else super#key key mask
4258 end);
4259 G.postRedisplay "info";
4262 let enterhelpmode =
4263 let source =
4264 (object
4265 inherit lvsourcebase
4266 method getitemcount = Array.length state.help
4267 method getitem n =
4268 let s, l, _ = state.help.(n) in
4269 (s, l)
4271 method exit ~uioh ~cancel ~active ~first ~pan =
4272 let optuioh =
4273 if not cancel
4274 then (
4275 match state.help.(active) with
4276 | _, _, Action f -> Some (f uioh)
4277 | _ -> Some (uioh)
4279 else None
4281 m_active <- active;
4282 m_first <- first;
4283 m_pan <- pan;
4284 optuioh
4286 method hasaction n =
4287 match state.help.(n) with
4288 | _, _, Action _ -> true
4289 | _ -> false
4291 initializer
4292 m_active <- -1
4293 end)
4294 in fun () ->
4295 let modehash = findkeyhash conf "help" in
4296 resetmstate ();
4297 state.uioh <- coe (new listview
4298 ~helpmode:true ~source ~trusted:true ~modehash);
4299 G.postRedisplay "help";
4302 let entermsgsmode =
4303 let msgsource =
4304 let re = Str.regexp "[\r\n]" in
4305 (object
4306 inherit lvsourcebase
4307 val mutable m_items = [||]
4309 method getitemcount = 1 + Array.length m_items
4311 method getitem n =
4312 if n = 0
4313 then "[Clear]", 0
4314 else m_items.(n-1), 0
4316 method exit ~uioh ~cancel ~active ~first ~pan =
4317 ignore uioh;
4318 if not cancel
4319 then (
4320 if active = 0
4321 then Buffer.clear state.errmsgs;
4323 m_active <- active;
4324 m_first <- first;
4325 m_pan <- pan;
4326 None
4328 method hasaction n =
4329 n = 0
4331 method reset =
4332 state.newerrmsgs <- false;
4333 let l = Str.split re (Buffer.contents state.errmsgs) in
4334 m_items <- Array.of_list l
4336 initializer
4337 m_active <- 0
4338 end)
4339 in fun () ->
4340 state.text <- "";
4341 resetmstate ();
4342 msgsource#reset;
4343 let source = (msgsource :> lvsource) in
4344 let modehash = findkeyhash conf "listview" in
4345 state.uioh <- coe (object
4346 inherit listview ~helpmode:false ~source ~trusted:false ~modehash as super
4347 method display =
4348 if state.newerrmsgs
4349 then msgsource#reset;
4350 super#display
4351 end);
4352 G.postRedisplay "msgs";
4355 let quickbookmark ?title () =
4356 match state.layout with
4357 | [] -> ()
4358 | l :: _ ->
4359 let title =
4360 match title with
4361 | None ->
4362 let tm = Unix.localtime (now ()) in
4363 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4364 (l.pageno+1)
4365 tm.Unix.tm_mday
4366 tm.Unix.tm_mon
4367 (tm.Unix.tm_year + 1900)
4368 tm.Unix.tm_hour
4369 tm.Unix.tm_min
4370 | Some title -> title
4372 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4375 let setautoscrollspeed step goingdown =
4376 let incr = max 1 ((abs step) / 2) in
4377 let incr = if goingdown then incr else -incr in
4378 let astep = boundastep state.winh (step + incr) in
4379 state.autoscroll <- Some astep;
4382 let canpan () =
4383 match conf.columns with
4384 | Csplit _ -> true
4385 | _ -> state.x != 0 || conf.zoom > 1.0
4388 let panbound x = bound x (-state.w) (wadjsb state.winw);;
4390 let existsinrow pageno (columns, coverA, coverB) p =
4391 let last = ((pageno - coverA) mod columns) + columns in
4392 let rec any = function
4393 | [] -> false
4394 | l :: rest ->
4395 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4396 then p l
4397 else (
4398 if not (p l)
4399 then (if l.pageno = last then false else any rest)
4400 else true
4403 any state.layout
4406 let nextpage () =
4407 match state.layout with
4408 | [] ->
4409 let pageno = page_of_y state.y in
4410 gotoghyll (getpagey (pageno+1))
4411 | l :: rest ->
4412 match conf.columns with
4413 | Csingle _ ->
4414 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4415 then
4416 let y = clamp (pgscale state.winh) in
4417 gotoghyll y
4418 else
4419 let pageno = min (l.pageno+1) (state.pagecount-1) in
4420 gotoghyll (getpagey pageno)
4421 | Cmulti ((c, _, _) as cl, _) ->
4422 if conf.presentation
4423 && (existsinrow l.pageno cl
4424 (fun l -> l.pageh > l.pagey + l.pagevh))
4425 then
4426 let y = clamp (pgscale state.winh) in
4427 gotoghyll y
4428 else
4429 let pageno = min (l.pageno+c) (state.pagecount-1) in
4430 gotoghyll (getpagey pageno)
4431 | Csplit (n, _) ->
4432 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4433 then
4434 let pagey, pageh = getpageyh l.pageno in
4435 let pagey = pagey + pageh * l.pagecol in
4436 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4437 gotoghyll (pagey + pageh + ips)
4440 let prevpage () =
4441 match state.layout with
4442 | [] ->
4443 let pageno = page_of_y state.y in
4444 gotoghyll (getpagey (pageno-1))
4445 | l :: _ ->
4446 match conf.columns with
4447 | Csingle _ ->
4448 if conf.presentation && l.pagey != 0
4449 then
4450 gotoghyll (clamp (pgscale ~-(state.winh)))
4451 else
4452 let pageno = max 0 (l.pageno-1) in
4453 gotoghyll (getpagey pageno)
4454 | Cmulti ((c, _, coverB) as cl, _) ->
4455 if conf.presentation &&
4456 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4457 then
4458 gotoghyll (clamp (pgscale ~-(state.winh)))
4459 else
4460 let decr =
4461 if l.pageno = state.pagecount - coverB
4462 then 1
4463 else c
4465 let pageno = max 0 (l.pageno-decr) in
4466 gotoghyll (getpagey pageno)
4467 | Csplit (n, _) ->
4468 let y =
4469 if l.pagecol = 0
4470 then
4471 if l.pageno = 0
4472 then l.pagey
4473 else
4474 let pageno = max 0 (l.pageno-1) in
4475 let pagey, pageh = getpageyh pageno in
4476 pagey + (n-1)*pageh
4477 else
4478 let pagey, pageh = getpageyh l.pageno in
4479 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4481 gotoghyll y
4484 let viewkeyboard key mask =
4485 let enttext te =
4486 let mode = state.mode in
4487 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4488 state.text <- "";
4489 enttext ();
4490 G.postRedisplay "view:enttext"
4492 let ctrl = Wsi.withctrl mask in
4493 let key =
4494 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4496 match key with
4497 | 81 -> (* Q *)
4498 exit 0
4500 | 0xff63 -> (* insert *)
4501 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4502 then (
4503 state.mode <- LinkNav (Ltgendir 0);
4504 gotoy state.y;
4506 else showtext '!' "Keyboard link navigation does not work under rotation"
4508 | 0xff1b | 113 -> (* escape / q *)
4509 begin match state.mstate with
4510 | Mzoomrect _ ->
4511 resetmstate ();
4512 G.postRedisplay "kill zoom rect";
4513 | _ ->
4514 begin match state.mode with
4515 | LinkNav _ ->
4516 state.mode <- View;
4517 G.postRedisplay "esc leave linknav"
4518 | _ ->
4519 match state.ranchors with
4520 | [] -> raise Quit
4521 | (path, password, anchor, origin) :: rest ->
4522 state.ranchors <- rest;
4523 state.anchor <- anchor;
4524 state.origin <- origin;
4525 state.nameddest <- "";
4526 opendoc path password
4527 end;
4528 end;
4530 | 0xff08 -> (* backspace *)
4531 gotoghyll (getnav ~-1)
4533 | 111 -> (* o *)
4534 enteroutlinemode ()
4536 | 117 -> (* u *)
4537 state.rects <- [];
4538 state.text <- "";
4539 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
4540 G.postRedisplay "dehighlight";
4542 | 47 | 63 -> (* / ? *)
4543 let ondone isforw s =
4544 cbput state.hists.pat s;
4545 state.searchpattern <- s;
4546 search s isforw
4548 let s = String.create 1 in
4549 s.[0] <- Char.chr key;
4550 enttext (s, "", Some (onhist state.hists.pat),
4551 textentry, ondone (key = 47), true)
4553 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4554 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4555 setzoom (conf.zoom +. incr)
4557 | 43 | 0xffab -> (* + *)
4558 let ondone s =
4559 let n =
4560 try int_of_string s with exc ->
4561 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4562 max_int
4564 if n != max_int
4565 then (
4566 conf.pagebias <- n;
4567 state.text <- "page bias is now " ^ string_of_int n;
4570 enttext ("page bias: ", "", None, intentry, ondone, true)
4572 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4573 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4574 setzoom (max 0.01 (conf.zoom -. decr))
4576 | 45 | 0xffad -> (* - *)
4577 let ondone msg = state.text <- msg in
4578 enttext (
4579 "option [acfhilpstvxACFPRSZTISM]: ", "", None,
4580 optentry state.mode, ondone, true
4583 | 48 when ctrl -> (* ctrl-0 *)
4584 if conf.zoom = 1.0
4585 then (
4586 state.x <- 0;
4587 gotoy state.y
4589 else setzoom 1.0
4591 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
4592 let cols =
4593 match conf.columns with
4594 | Csingle _ | Cmulti _ -> 1
4595 | Csplit (n, _) -> n
4597 let h = state.winh -
4598 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4600 let zoom = zoomforh state.winw h (vscrollw ()) cols in
4601 if zoom > 0.0 && (key = 50 || zoom < 1.0)
4602 then setzoom zoom
4604 | 51 when ctrl -> (* ctrl-3 *)
4605 let fm =
4606 match conf.fitmodel with
4607 | FitWidth -> FitProportional
4608 | FitProportional -> FitPage
4609 | FitPage -> FitWidth
4611 state.text <- "fit model: " ^ FMTE.to_string fm;
4612 reqlayout conf.angle fm
4614 | 0xffc6 -> (* f9 *)
4615 togglebirdseye ()
4617 | 57 when ctrl -> (* ctrl-9 *)
4618 togglebirdseye ()
4620 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4621 when not ctrl -> (* 0..9 *)
4622 let ondone s =
4623 let n =
4624 try int_of_string s with exc ->
4625 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4628 if n >= 0
4629 then (
4630 addnav ();
4631 cbput state.hists.pag (string_of_int n);
4632 gotopage1 (n + conf.pagebias - 1) 0;
4635 let pageentry text key =
4636 match Char.unsafe_chr key with
4637 | 'g' -> TEdone text
4638 | _ -> intentry text key
4640 let text = "x" in text.[0] <- Char.chr key;
4641 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4643 | 98 -> (* b *)
4644 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4645 reshape state.winw state.winh;
4647 | 66 -> (* B *)
4648 state.bzoom <- not state.bzoom;
4649 state.rects <- [];
4650 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4652 | 108 -> (* l *)
4653 conf.hlinks <- not conf.hlinks;
4654 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4655 G.postRedisplay "toggle highlightlinks";
4657 | 70 -> (* F *)
4658 state.glinks <- true;
4659 let mode = state.mode in
4660 state.mode <- Textentry (
4661 (":", "", None, linknentry, linkndone gotounder, false),
4662 (fun _ ->
4663 state.glinks <- false;
4664 state.mode <- mode)
4666 state.text <- "";
4667 G.postRedisplay "view:linkent(F)"
4669 | 121 -> (* y *)
4670 state.glinks <- true;
4671 let mode = state.mode in
4672 state.mode <- Textentry (
4674 ":", "", None, linknentry, linkndone (fun under ->
4675 selstring (undertext under);
4676 ), false
4678 fun _ ->
4679 state.glinks <- false;
4680 state.mode <- mode
4682 state.text <- "";
4683 G.postRedisplay "view:linkent"
4685 | 97 -> (* a *)
4686 begin match state.autoscroll with
4687 | Some step ->
4688 conf.autoscrollstep <- step;
4689 state.autoscroll <- None
4690 | None ->
4691 if conf.autoscrollstep = 0
4692 then state.autoscroll <- Some 1
4693 else state.autoscroll <- Some conf.autoscrollstep
4696 | 112 when ctrl -> (* ctrl-p *)
4697 launchpath ()
4699 | 80 -> (* P *)
4700 setpresentationmode (not conf.presentation);
4701 showtext ' ' ("presentation mode " ^
4702 if conf.presentation then "on" else "off");
4704 | 102 -> (* f *)
4705 if List.mem Wsi.Fullscreen state.winstate
4706 then Wsi.reshape conf.cwinw conf.cwinh
4707 else Wsi.fullscreen ()
4709 | 112 | 78 -> (* p|N *)
4710 search state.searchpattern false
4712 | 110 | 0xffc0 -> (* n|F3 *)
4713 search state.searchpattern true
4715 | 116 -> (* t *)
4716 begin match state.layout with
4717 | [] -> ()
4718 | l :: _ ->
4719 gotoghyll (getpagey l.pageno)
4722 | 32 -> (* space *)
4723 nextpage ()
4725 | 0xff9f | 0xffff -> (* delete *)
4726 prevpage ()
4728 | 61 -> (* = *)
4729 showtext ' ' (describe_location ());
4731 | 119 -> (* w *)
4732 begin match state.layout with
4733 | [] -> ()
4734 | l :: _ ->
4735 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
4736 G.postRedisplay "w"
4739 | 39 -> (* ' *)
4740 enterbookmarkmode ()
4742 | 104 | 0xffbe -> (* h|F1 *)
4743 enterhelpmode ()
4745 | 105 -> (* i *)
4746 enterinfomode ()
4748 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
4749 entermsgsmode ()
4751 | 109 -> (* m *)
4752 let ondone s =
4753 match state.layout with
4754 | l :: _ ->
4755 if nonemptystr s
4756 then
4757 state.bookmarks <-
4758 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4759 | _ -> ()
4761 enttext ("bookmark: ", "", None, textentry, ondone, true)
4763 | 126 -> (* ~ *)
4764 quickbookmark ();
4765 showtext ' ' "Quick bookmark added";
4767 | 122 -> (* z *)
4768 begin match state.layout with
4769 | l :: _ ->
4770 let rect = getpdimrect l.pagedimno in
4771 let w, h =
4772 if conf.crophack
4773 then
4774 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4775 truncate (1.2 *. (rect.(3) -. rect.(0))))
4776 else
4777 (truncate (rect.(1) -. rect.(0)),
4778 truncate (rect.(3) -. rect.(0)))
4780 let w = truncate ((float w)*.conf.zoom)
4781 and h = truncate ((float h)*.conf.zoom) in
4782 if w != 0 && h != 0
4783 then (
4784 state.anchor <- getanchor ();
4785 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
4787 G.postRedisplay "z";
4789 | [] -> ()
4792 | 120 -> (* x *)
4793 state.roam ()
4794 | 60 | 62 -> (* < > *)
4795 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
4797 | 91 | 93 -> (* [ ] *)
4798 conf.colorscale <-
4799 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
4801 G.postRedisplay "brightness";
4803 | 99 when state.mode = View -> (* [alt-]c *)
4804 if Wsi.withalt mask
4805 then (
4806 if conf.zoom > 1.0
4807 then
4808 let m = (wadjsb state.winw - state.w) / 2 in
4809 state.x <- m;
4810 gotoy_and_clear_text state.y
4812 else
4813 let (c, a, b), z =
4814 match state.prevcolumns with
4815 | None -> (1, 0, 0), 1.0
4816 | Some (columns, z) ->
4817 let cab =
4818 match columns with
4819 | Csplit (c, _) -> -c, 0, 0
4820 | Cmulti ((c, a, b), _) -> c, a, b
4821 | Csingle _ -> 1, 0, 0
4823 cab, z
4825 setcolumns View c a b;
4826 setzoom z
4828 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask
4829 -> (* ctrl-shift- (kp) [up|down] *)
4830 let zoom, x = state.prevzoom in
4831 setzoom zoom;
4832 state.x <- x;
4834 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
4835 begin match state.autoscroll with
4836 | None ->
4837 begin match state.mode with
4838 | Birdseye beye -> upbirdseye 1 beye
4839 | _ ->
4840 if ctrl
4841 then gotoy_and_clear_text (clamp ~-(state.winh/2))
4842 else (
4843 if not (Wsi.withshift mask) && conf.presentation
4844 then prevpage ()
4845 else gotoghyll1 true (clamp (-conf.scrollstep))
4848 | Some n ->
4849 setautoscrollspeed n false
4852 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
4853 begin match state.autoscroll with
4854 | None ->
4855 begin match state.mode with
4856 | Birdseye beye -> downbirdseye 1 beye
4857 | _ ->
4858 if ctrl
4859 then gotoy_and_clear_text (clamp (state.winh/2))
4860 else (
4861 if not (Wsi.withshift mask) && conf.presentation
4862 then nextpage ()
4863 else gotoghyll1 true (clamp (conf.scrollstep))
4866 | Some n ->
4867 setautoscrollspeed n true
4870 | 0xff51 | 0xff53 | 0xff96 | 0xff98
4871 when not (Wsi.withalt mask) -> (* (kp) left / right *)
4872 if canpan ()
4873 then
4874 let dx =
4875 if ctrl
4876 then state.winw / 2
4877 else conf.hscrollstep
4879 let dx = if key = 0xff51 || key = 0xff96 then dx else -dx in
4880 state.x <- panbound (state.x + dx);
4881 gotoy_and_clear_text state.y
4882 else (
4883 state.text <- "";
4884 G.postRedisplay "left/right"
4887 | 0xff55 | 0xff9a -> (* (kp) prior *)
4888 let y =
4889 if ctrl
4890 then
4891 match state.layout with
4892 | [] -> state.y
4893 | l :: _ -> state.y - l.pagey
4894 else
4895 clamp (pgscale (-state.winh))
4897 gotoghyll y
4899 | 0xff56 | 0xff9b -> (* (kp) next *)
4900 let y =
4901 if ctrl
4902 then
4903 match List.rev state.layout with
4904 | [] -> state.y
4905 | l :: _ -> getpagey l.pageno
4906 else
4907 clamp (pgscale state.winh)
4909 gotoghyll y
4911 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
4912 gotoghyll 0
4913 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
4914 gotoghyll (clamp state.maxy)
4916 | 0xff53 | 0xff98
4917 when Wsi.withalt mask -> (* alt-(kp) right *)
4918 gotoghyll (getnav 1)
4919 | 0xff51 | 0xff96
4920 when Wsi.withalt mask -> (* alt-(kp) left *)
4921 gotoghyll (getnav ~-1)
4923 | 114 -> (* r *)
4924 reload ()
4926 | 118 when conf.debug -> (* v *)
4927 state.rects <- [];
4928 List.iter (fun l ->
4929 match getopaque l.pageno with
4930 | None -> ()
4931 | Some opaque ->
4932 let x0, y0, x1, y1 = pagebbox opaque in
4933 let a,b = float x0, float y0 in
4934 let c,d = float x1, float y0 in
4935 let e,f = float x1, float y1 in
4936 let h,j = float x0, float y1 in
4937 let rect = (a,b,c,d,e,f,h,j) in
4938 debugrect rect;
4939 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4940 ) state.layout;
4941 G.postRedisplay "v";
4943 | 124 -> (* | *)
4944 let mode = state.mode in
4945 let cmd = ref "" in
4946 let onleave = function
4947 | Cancel -> state.mode <- mode
4948 | Confirm ->
4949 List.iter (fun l ->
4950 match getopaque l.pageno with
4951 | Some opaque -> pipesel opaque !cmd
4952 | None -> ()) state.layout;
4953 state.mode <- mode
4955 let ondone s =
4956 cbput state.hists.sel s;
4957 cmd := s
4959 let te =
4960 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
4962 G.postRedisplay "|";
4963 state.mode <- Textentry (te, onleave);
4965 | _ ->
4966 vlog "huh? %s" (Wsi.keyname key)
4969 let linknavkeyboard key mask linknav =
4970 let getpage pageno =
4971 let rec loop = function
4972 | [] -> None
4973 | l :: _ when l.pageno = pageno -> Some l
4974 | _ :: rest -> loop rest
4975 in loop state.layout
4977 let doexact (pageno, n) =
4978 match getopaque pageno, getpage pageno with
4979 | Some opaque, Some l ->
4980 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
4981 then
4982 let under = getlink opaque n in
4983 G.postRedisplay "link gotounder";
4984 gotounder under;
4985 state.mode <- View;
4986 else
4987 let opt, dir =
4988 match key with
4989 | 0xff50 -> (* home *)
4990 Some (findlink opaque LDfirst), -1
4992 | 0xff57 -> (* end *)
4993 Some (findlink opaque LDlast), 1
4995 | 0xff51 -> (* left *)
4996 Some (findlink opaque (LDleft n)), -1
4998 | 0xff53 -> (* right *)
4999 Some (findlink opaque (LDright n)), 1
5001 | 0xff52 -> (* up *)
5002 Some (findlink opaque (LDup n)), -1
5004 | 0xff54 -> (* down *)
5005 Some (findlink opaque (LDdown n)), 1
5007 | _ -> None, 0
5009 let pwl l dir =
5010 begin match findpwl l.pageno dir with
5011 | Pwlnotfound -> ()
5012 | Pwl pageno ->
5013 let notfound dir =
5014 state.mode <- LinkNav (Ltgendir dir);
5015 let y, h = getpageyh pageno in
5016 let y =
5017 if dir < 0
5018 then y + h - state.winh
5019 else y
5021 gotoy y
5023 begin match getopaque pageno, getpage pageno with
5024 | Some opaque, Some _ ->
5025 let link =
5026 let ld = if dir > 0 then LDfirst else LDlast in
5027 findlink opaque ld
5029 begin match link with
5030 | Lfound m ->
5031 showlinktype (getlink opaque m);
5032 state.mode <- LinkNav (Ltexact (pageno, m));
5033 G.postRedisplay "linknav jpage";
5034 | _ -> notfound dir
5035 end;
5036 | _ -> notfound dir
5037 end;
5038 end;
5040 begin match opt with
5041 | Some Lnotfound -> pwl l dir;
5042 | Some (Lfound m) ->
5043 if m = n
5044 then pwl l dir
5045 else (
5046 let _, y0, _, y1 = getlinkrect opaque m in
5047 if y0 < l.pagey
5048 then gotopage1 l.pageno y0
5049 else (
5050 let d = fstate.fontsize + 1 in
5051 if y1 - l.pagey > l.pagevh - d
5052 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5053 else G.postRedisplay "linknav";
5055 showlinktype (getlink opaque m);
5056 state.mode <- LinkNav (Ltexact (l.pageno, m));
5059 | None -> viewkeyboard key mask
5060 end;
5061 | _ -> viewkeyboard key mask
5063 if key = 0xff63
5064 then (
5065 state.mode <- View;
5066 G.postRedisplay "leave linknav"
5068 else
5069 match linknav with
5070 | Ltgendir _ -> viewkeyboard key mask
5071 | Ltexact exact -> doexact exact
5074 let keyboard key mask =
5075 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5076 then wcmd "interrupt"
5077 else state.uioh <- state.uioh#key key mask
5080 let birdseyekeyboard key mask
5081 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5082 let incr =
5083 match conf.columns with
5084 | Csingle _ -> 1
5085 | Cmulti ((c, _, _), _) -> c
5086 | Csplit _ -> failwith "bird's eye split mode"
5088 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5089 match key with
5090 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5091 let y, h = getpageyh pageno in
5092 let top = (state.winh - h) / 2 in
5093 gotoy (max 0 (y - top))
5094 | 0xff0d (* enter *)
5095 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5096 | 0xff1b -> leavebirdseye beye true (* escape *)
5097 | 0xff52 -> upbirdseye incr beye (* up *)
5098 | 0xff54 -> downbirdseye incr beye (* down *)
5099 | 0xff51 -> upbirdseye 1 beye (* left *)
5100 | 0xff53 -> downbirdseye 1 beye (* right *)
5102 | 0xff55 -> (* prior *)
5103 begin match state.layout with
5104 | l :: _ ->
5105 if l.pagey != 0
5106 then (
5107 state.mode <- Birdseye (
5108 oconf, leftx, l.pageno, hooverpageno, anchor
5110 gotopage1 l.pageno 0;
5112 else (
5113 let layout = layout (state.y-state.winh) (pgh state.layout) in
5114 match layout with
5115 | [] -> gotoy (clamp (-state.winh))
5116 | l :: _ ->
5117 state.mode <- Birdseye (
5118 oconf, leftx, l.pageno, hooverpageno, anchor
5120 gotopage1 l.pageno 0
5123 | [] -> gotoy (clamp (-state.winh))
5124 end;
5126 | 0xff56 -> (* next *)
5127 begin match List.rev state.layout with
5128 | l :: _ ->
5129 let layout = layout (state.y + (pgh state.layout)) state.winh in
5130 begin match layout with
5131 | [] ->
5132 let incr = l.pageh - l.pagevh in
5133 if incr = 0
5134 then (
5135 state.mode <-
5136 Birdseye (
5137 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5139 G.postRedisplay "birdseye pagedown";
5141 else gotoy (clamp (incr + conf.interpagespace*2));
5143 | l :: _ ->
5144 state.mode <-
5145 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5146 gotopage1 l.pageno 0;
5149 | [] -> gotoy (clamp state.winh)
5150 end;
5152 | 0xff50 -> (* home *)
5153 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5154 gotopage1 0 0
5156 | 0xff57 -> (* end *)
5157 let pageno = state.pagecount - 1 in
5158 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5159 if not (pagevisible state.layout pageno)
5160 then
5161 let h =
5162 match List.rev state.pdims with
5163 | [] -> state.winh
5164 | (_, _, h, _) :: _ -> h
5166 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5167 else G.postRedisplay "birdseye end";
5168 | _ -> viewkeyboard key mask
5171 let drawpage l =
5172 let color =
5173 match state.mode with
5174 | Textentry _ -> scalecolor 0.4
5175 | LinkNav _
5176 | View -> scalecolor 1.0
5177 | Birdseye (_, _, pageno, hooverpageno, _) ->
5178 if l.pageno = hooverpageno
5179 then scalecolor 0.9
5180 else (
5181 if l.pageno = pageno
5182 then scalecolor 1.0
5183 else scalecolor 0.8
5186 drawtiles l color;
5189 let postdrawpage l linkindexbase =
5190 match getopaque l.pageno with
5191 | Some opaque ->
5192 if tileready l l.pagex l.pagey
5193 then
5194 let x = l.pagedispx - l.pagex
5195 and y = l.pagedispy - l.pagey in
5196 let hlmask =
5197 match conf.columns with
5198 | Csingle _ | Cmulti _ ->
5199 (if conf.hlinks then 1 else 0)
5200 + (if state.glinks
5201 && not (isbirdseye state.mode) then 2 else 0)
5202 | _ -> 0
5204 let s =
5205 match state.mode with
5206 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5207 | _ -> ""
5209 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5210 else 0
5211 | _ -> 0
5214 let scrollindicator () =
5215 let sbw, ph, sh = state.uioh#scrollph in
5216 let sbh, pw, sw = state.uioh#scrollpw in
5218 let x0,x1 =
5219 if conf.leftscroll
5220 then (0, sbw)
5221 else (state.winw - sbw), state.winw
5224 GlDraw.color (0.64, 0.64, 0.64);
5225 filledrect (float x0) 0. (float x1) (float state.winh);
5226 filledrect
5227 0. (float (state.winh - sbh))
5228 (float (wadjsb state.winw - 1)) (float state.winh)
5230 GlDraw.color (0.0, 0.0, 0.0);
5232 filledrect (float x0) ph (float x1) (ph +. sh);
5233 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5236 let showsel () =
5237 match state.mstate with
5238 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5241 | Msel ((x0, y0), (x1, y1)) ->
5242 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5243 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< "", -1, 0, 0) in
5244 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< "", -1, 0, 0) in
5245 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5248 let showrects = function [] -> () | rects ->
5249 Gl.enable `blend;
5250 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5251 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5252 List.iter
5253 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5254 List.iter (fun l ->
5255 if l.pageno = pageno
5256 then (
5257 let dx = float (l.pagedispx - l.pagex) in
5258 let dy = float (l.pagedispy - l.pagey) in
5259 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5260 Raw.sets_float state.vraw ~pos:0
5261 [| x0+.dx; y0+.dy;
5262 x1+.dx; y1+.dy;
5263 x3+.dx; y3+.dy;
5264 x2+.dx; y2+.dy |];
5265 GlArray.vertex `two state.vraw;
5266 GlArray.draw_arrays `triangle_strip 0 4;
5268 ) state.layout
5269 ) rects
5271 Gl.disable `blend;
5274 let display () =
5275 GlClear.color (scalecolor2 conf.bgcolor);
5276 GlClear.clear [`color];
5277 List.iter drawpage state.layout;
5278 let rects =
5279 match state.mode with
5280 | LinkNav (Ltexact (pageno, linkno)) ->
5281 begin match getopaque pageno with
5282 | Some opaque ->
5283 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5284 (pageno, 5, (
5285 float x0, float y0,
5286 float x1, float y0,
5287 float x1, float y1,
5288 float x0, float y1)
5289 ) :: state.rects
5290 | None -> state.rects
5292 | _ -> state.rects
5294 showrects rects;
5295 let rec postloop linkindexbase = function
5296 | l :: rest ->
5297 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5298 postloop linkindexbase rest
5299 | [] -> ()
5301 showsel ();
5302 postloop 0 state.layout;
5303 state.uioh#display;
5304 begin match state.mstate with
5305 | Mzoomrect ((x0, y0), (x1, y1)) ->
5306 Gl.enable `blend;
5307 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5308 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5309 filledrect (float x0) (float y0) (float x1) (float y1);
5310 Gl.disable `blend;
5311 | _ -> ()
5312 end;
5313 enttext ();
5314 scrollindicator ();
5315 Wsi.swapb ();
5318 let zoomrect x y x1 y1 =
5319 let x0 = min x x1
5320 and x1 = max x x1
5321 and y0 = min y y1 in
5322 gotoy (state.y + y0);
5323 state.anchor <- getanchor ();
5324 let zoom = (float state.w) /. float (x1 - x0) in
5325 let margin =
5326 match conf.fitmodel, conf.columns with
5327 | FitPage, Csplit _ ->
5328 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5330 | _, _ ->
5331 let adjw = wadjsb state.winw in
5332 if state.w < adjw
5333 then (adjw - state.w) / 2
5334 else 0
5336 state.x <- (state.x + margin) - x0;
5337 setzoom zoom;
5338 resetmstate ();
5341 let zoomblock x y =
5342 let g opaque l px py =
5343 match rectofblock opaque px py with
5344 | Some a ->
5345 let x0 = a.(0) -. 20. in
5346 let x1 = a.(1) +. 20. in
5347 let y0 = a.(2) -. 20. in
5348 let zoom = (float state.w) /. (x1 -. x0) in
5349 let pagey = getpagey l.pageno in
5350 gotoy_and_clear_text (pagey + truncate y0);
5351 state.anchor <- getanchor ();
5352 let margin = (state.w - l.pagew)/2 in
5353 state.x <- -truncate x0 - margin;
5354 setzoom zoom;
5355 None
5356 | None -> None
5358 match conf.columns with
5359 | Csplit _ ->
5360 showtext '!' "block zooming does not work properly in split columns mode"
5361 | _ -> onppundermouse g x y ()
5364 let scrollx x =
5365 let winw = wadjsb state.winw - 1 in
5366 let s = float x /. float winw in
5367 let destx = truncate (float (state.w + winw) *. s) in
5368 state.x <- winw - destx;
5369 gotoy_and_clear_text state.y;
5370 state.mstate <- Mscrollx;
5373 let scrolly y =
5374 let s = float y /. float state.winh in
5375 let desty = truncate (float (state.maxy - state.winh) *. s) in
5376 gotoy_and_clear_text desty;
5377 state.mstate <- Mscrolly;
5380 let viewmulticlick clicks x y mask =
5381 let g opaque l px py =
5382 let mark =
5383 match clicks with
5384 | 2 -> Mark_word
5385 | 3 -> Mark_line
5386 | 4 -> Mark_block
5387 | _ -> Mark_page
5389 if markunder opaque px py mark
5390 then (
5391 Some (fun () ->
5392 let dopipe cmd =
5393 match getopaque l.pageno with
5394 | None -> ()
5395 | Some opaque -> pipesel opaque cmd
5397 state.roam <- (fun () -> dopipe conf.paxcmd);
5398 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5401 else None
5403 G.postRedisplay "viewmulticlick";
5404 onppundermouse g x y (fun () -> showtext '!' "Nothing to select") ();
5407 let canselect () =
5408 match conf.columns with
5409 | Csplit _ -> false
5410 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5413 let viewmouse button down x y mask =
5414 match button with
5415 | n when (n == 4 || n == 5) && not down ->
5416 if Wsi.withctrl mask
5417 then (
5418 match state.mstate with
5419 | Mzoom (oldn, i) ->
5420 if oldn = n
5421 then (
5422 if i = 2
5423 then
5424 let incr =
5425 match n with
5426 | 5 ->
5427 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5428 | _ ->
5429 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5431 let zoom = conf.zoom -. incr in
5432 setzoom zoom;
5433 state.mstate <- Mzoom (n, 0);
5434 else
5435 state.mstate <- Mzoom (n, i+1);
5437 else state.mstate <- Mzoom (n, 0)
5439 | _ -> state.mstate <- Mzoom (n, 0)
5441 else (
5442 match state.autoscroll with
5443 | Some step -> setautoscrollspeed step (n=4)
5444 | None ->
5445 if conf.wheelbypage || conf.presentation
5446 then (
5447 if n = 4
5448 then prevpage ()
5449 else nextpage ()
5451 else
5452 let incr =
5453 if n = 4
5454 then -conf.scrollstep
5455 else conf.scrollstep
5457 let incr = incr * 2 in
5458 let y = clamp incr in
5459 gotoy_and_clear_text y
5462 | n when (n = 6 || n = 7) && not down && canpan () ->
5463 state.x <-
5464 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5465 gotoy_and_clear_text state.y
5467 | 1 when Wsi.withshift mask ->
5468 state.mstate <- Mnone;
5469 if not down
5470 then (
5471 match unproject x y with
5472 | Some (pageno, ux, uy) ->
5473 let cmd = Printf.sprintf
5474 "%s %s %d %d %d"
5475 conf.stcmd state.path pageno ux uy
5477 popen cmd []
5478 | None -> ()
5481 | 1 when Wsi.withctrl mask ->
5482 if down
5483 then (
5484 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5485 state.mstate <- Mpan (x, y)
5487 else
5488 state.mstate <- Mnone
5490 | 3 ->
5491 if down
5492 then (
5493 Wsi.setcursor Wsi.CURSOR_CYCLE;
5494 let p = (x, y) in
5495 state.mstate <- Mzoomrect (p, p)
5497 else (
5498 match state.mstate with
5499 | Mzoomrect ((x0, y0), _) ->
5500 if abs (x-x0) > 10 && abs (y - y0) > 10
5501 then zoomrect x0 y0 x y
5502 else (
5503 resetmstate ();
5504 G.postRedisplay "kill accidental zoom rect";
5506 | _ ->
5507 resetmstate ()
5510 | 1 when x > state.winw - vscrollw () ->
5511 if down
5512 then
5513 let _, position, sh = state.uioh#scrollph in
5514 if y > truncate position && y < truncate (position +. sh)
5515 then state.mstate <- Mscrolly
5516 else scrolly y
5517 else
5518 state.mstate <- Mnone
5520 | 1 when y > state.winh - hscrollh () ->
5521 if down
5522 then
5523 let _, position, sw = state.uioh#scrollpw in
5524 if x > truncate position && x < truncate (position +. sw)
5525 then state.mstate <- Mscrollx
5526 else scrollx x
5527 else
5528 state.mstate <- Mnone
5530 | 1 when state.bzoom -> if not down then zoomblock x y
5532 | 1 ->
5533 let dest = if down then getunder x y else Unone in
5534 begin match dest with
5535 | Ulinkgoto _
5536 | Ulinkuri _
5537 | Uremote _ | Uremotedest _
5538 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5539 gotounder dest
5541 | Unone when down ->
5542 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5543 state.mstate <- Mpan (x, y);
5545 | Unone | Utext _ ->
5546 if down
5547 then (
5548 if canselect ()
5549 then (
5550 state.mstate <- Msel ((x, y), (x, y));
5551 G.postRedisplay "mouse select";
5554 else (
5555 match state.mstate with
5556 | Mnone -> ()
5558 | Mzoom _ | Mscrollx | Mscrolly ->
5559 state.mstate <- Mnone
5561 | Mzoomrect ((x0, y0), _) ->
5562 zoomrect x0 y0 x y
5564 | Mpan _ ->
5565 Wsi.setcursor Wsi.CURSOR_INHERIT;
5566 state.mstate <- Mnone
5568 | Msel ((x0, y0), (x1, y1)) ->
5569 let rec loop = function
5570 | [] -> ()
5571 | l :: rest ->
5572 let inside =
5573 let a0 = l.pagedispy in
5574 let a1 = a0 + l.pagevh in
5575 let b0 = l.pagedispx in
5576 let b1 = b0 + l.pagevw in
5577 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5578 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5580 if inside
5581 then
5582 match getopaque l.pageno with
5583 | Some opaque ->
5584 let dosel cmd () =
5585 match Ne.res Unix.pipe with
5586 | Ne.Exn exn ->
5587 showtext '!'
5588 (Printf.sprintf
5589 "can not create sel pipe: %s"
5590 (exntos exn));
5591 | Ne.Res (r, w) ->
5592 let clo what fd =
5593 Ne.clo fd (fun msg ->
5594 dolog "%s close failed: %s" what msg)
5596 let popened =
5597 try popen cmd [r, 0; w, -1]; true
5598 with exn ->
5599 dolog "can not execute %S: %s"
5600 cmd (exntos exn);
5601 false
5603 if popened
5604 then (
5605 copysel w opaque;
5606 G.postRedisplay "copysel";
5608 else clo "Msel pipe/w" w;
5609 clo "Msel pipe/r" r;
5611 dosel conf.selcmd ();
5612 state.roam <- dosel conf.paxcmd;
5613 | None -> ()
5614 else loop rest
5616 loop state.layout;
5617 resetmstate ();
5621 | _ -> ()
5624 let birdseyemouse button down x y mask
5625 (conf, leftx, _, hooverpageno, anchor) =
5626 match button with
5627 | 1 when down ->
5628 let rec loop = function
5629 | [] -> ()
5630 | l :: rest ->
5631 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5632 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5633 then (
5634 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5636 else loop rest
5638 loop state.layout
5639 | 3 -> ()
5640 | _ -> viewmouse button down x y mask
5643 let uioh = object
5644 method display = ()
5646 method key key mask =
5647 begin match state.mode with
5648 | Textentry textentry -> textentrykeyboard key mask textentry
5649 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5650 | View -> viewkeyboard key mask
5651 | LinkNav linknav -> linknavkeyboard key mask linknav
5652 end;
5653 state.uioh
5655 method button button bstate x y mask =
5656 begin match state.mode with
5657 | LinkNav _
5658 | View -> viewmouse button bstate x y mask
5659 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5660 | Textentry _ -> ()
5661 end;
5662 state.uioh
5664 method multiclick clicks x y mask =
5665 begin match state.mode with
5666 | LinkNav _
5667 | View -> viewmulticlick clicks x y mask
5668 | Birdseye _
5669 | Textentry _ -> ()
5670 end;
5671 state.uioh
5673 method motion x y =
5674 begin match state.mode with
5675 | Textentry _ -> ()
5676 | View | Birdseye _ | LinkNav _ ->
5677 match state.mstate with
5678 | Mzoom _ | Mnone -> ()
5680 | Mpan (x0, y0) ->
5681 let dx = x - x0
5682 and dy = y0 - y in
5683 state.mstate <- Mpan (x, y);
5684 if canpan ()
5685 then state.x <- panbound (state.x + dx);
5686 let y = clamp dy in
5687 gotoy_and_clear_text y
5689 | Msel (a, _) ->
5690 state.mstate <- Msel (a, (x, y));
5691 G.postRedisplay "motion select";
5693 | Mscrolly ->
5694 let y = min state.winh (max 0 y) in
5695 scrolly y
5697 | Mscrollx ->
5698 let x = min state.winw (max 0 x) in
5699 scrollx x
5701 | Mzoomrect (p0, _) ->
5702 state.mstate <- Mzoomrect (p0, (x, y));
5703 G.postRedisplay "motion zoomrect";
5704 end;
5705 state.uioh
5707 method pmotion x y =
5708 begin match state.mode with
5709 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5710 let rec loop = function
5711 | [] ->
5712 if hooverpageno != -1
5713 then (
5714 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5715 G.postRedisplay "pmotion birdseye no hoover";
5717 | l :: rest ->
5718 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5719 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5720 then (
5721 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5722 G.postRedisplay "pmotion birdseye hoover";
5724 else loop rest
5726 loop state.layout
5728 | Textentry _ -> ()
5730 | LinkNav _
5731 | View ->
5732 match state.mstate with
5733 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5735 | Mnone ->
5736 updateunder x y;
5737 if canselect ()
5738 then
5739 match conf.pax with
5740 | None -> ()
5741 | Some r ->
5742 let past, _, _ = !r in
5743 let now = now () in
5744 let delta = now -. past in
5745 if delta > 0.01
5746 then paxunder x y
5747 else r := (now, x, y)
5748 end;
5749 state.uioh
5751 method infochanged _ = ()
5753 method scrollph =
5754 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
5755 let p, h =
5756 if maxy = 0
5757 then 0.0, float state.winh
5758 else scrollph state.y maxy
5760 vscrollw (), p, h
5762 method scrollpw =
5763 let winw = wadjsb state.winw in
5764 let fwinw = float winw in
5765 let sw =
5766 let sw = fwinw /. float state.w in
5767 let sw = fwinw *. sw in
5768 max sw (float conf.scrollh)
5770 let position =
5771 let maxx = state.w + winw in
5772 let x = winw - state.x in
5773 let percent = float x /. float maxx in
5774 (fwinw -. sw) *. percent
5776 hscrollh (), position, sw
5778 method modehash =
5779 let modename =
5780 match state.mode with
5781 | LinkNav _ -> "links"
5782 | Textentry _ -> "textentry"
5783 | Birdseye _ -> "birdseye"
5784 | View -> "view"
5786 findkeyhash conf modename
5788 method eformsgs = true
5789 end;;
5791 let adderrmsg src msg =
5792 Buffer.add_string state.errmsgs msg;
5793 state.newerrmsgs <- true;
5794 G.postRedisplay src
5797 let adderrfmt src fmt =
5798 Format.kprintf (fun s -> adderrmsg src s) fmt;
5801 let ract cmds =
5802 let cl = splitatspace cmds in
5803 let scan s fmt f =
5804 try Scanf.sscanf s fmt f
5805 with exn ->
5806 adderrfmt "remote exec"
5807 "error processing '%S': %s\n" cmds (exntos exn)
5809 match cl with
5810 | "reload" :: [] -> reload ()
5811 | "goto" :: args :: [] ->
5812 scan args "%u %f %f"
5813 (fun pageno x y ->
5814 let cmd, _ = state.geomcmds in
5815 if emptystr cmd
5816 then gotopagexy pageno x y
5817 else
5818 let f prevf () =
5819 gotopagexy pageno x y;
5820 prevf ()
5822 state.reprf <- f state.reprf
5824 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
5825 | "gotor" :: args :: [] ->
5826 scan args "%S %u"
5827 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
5828 | "gotord" :: args :: [] ->
5829 scan args "%S %S"
5830 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
5831 | "rect" :: args :: [] ->
5832 scan args "%u %u %f %f %f %f"
5833 (fun pageno color x0 y0 x1 y1 ->
5834 onpagerect pageno (fun w h ->
5835 let _,w1,h1,_ = getpagedim pageno in
5836 let sw = float w1 /. float w
5837 and sh = float h1 /. float h in
5838 let x0s = x0 *. sw
5839 and x1s = x1 *. sw
5840 and y0s = y0 *. sh
5841 and y1s = y1 *. sh in
5842 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
5843 debugrect rect;
5844 state.rects <- (pageno, color, rect) :: state.rects;
5845 G.postRedisplay "rect";
5848 | "activatewin" :: [] -> Wsi.activatewin ()
5849 | "quit" :: [] -> raise Quit
5850 | _ ->
5851 adderrfmt "remote command"
5852 "error processing remote command: %S\n" cmds;
5855 let remote =
5856 let scratch = String.create 80 in
5857 let buf = Buffer.create 80 in
5858 fun fd ->
5859 let rec tempfr () =
5860 try Some (Unix.read fd scratch 0 80)
5861 with
5862 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
5863 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
5864 | exn -> raise exn
5866 match tempfr () with
5867 | None -> Some fd
5868 | Some n ->
5869 if n = 0
5870 then (
5871 Unix.close fd;
5872 if Buffer.length buf > 0
5873 then (
5874 let s = Buffer.contents buf in
5875 Buffer.clear buf;
5876 ract s;
5878 None
5880 else
5881 let rec eat ppos =
5882 let nlpos =
5884 let pos = String.index_from scratch ppos '\n' in
5885 if pos >= n then -1 else pos
5886 with Not_found -> -1
5888 if nlpos >= 0
5889 then (
5890 Buffer.add_substring buf scratch ppos (nlpos-ppos);
5891 let s = Buffer.contents buf in
5892 Buffer.clear buf;
5893 ract s;
5894 eat (nlpos+1);
5896 else (
5897 Buffer.add_substring buf scratch ppos (n-ppos);
5898 Some fd
5900 in eat 0
5903 let remoteopen path =
5904 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
5905 with exn ->
5906 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
5907 None
5910 let () =
5911 let trimcachepath = ref "" in
5912 let rcmdpath = ref "" in
5913 let pageno = ref None in
5914 selfexec := Sys.executable_name;
5915 Arg.parse
5916 (Arg.align
5917 [("-p", Arg.String (fun s -> state.password <- s),
5918 "<password> Set password");
5920 ("-f", Arg.String
5921 (fun s ->
5922 Config.fontpath := s;
5923 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
5925 "<path> Set path to the user interface font");
5927 ("-c", Arg.String
5928 (fun s ->
5929 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
5930 Config.confpath := s),
5931 "<path> Set path to the configuration file");
5933 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
5934 "<page-number> Jump to page");
5936 ("-tcf", Arg.String (fun s -> trimcachepath := s),
5937 "<path> Set path to the trim cache file");
5939 ("-dest", Arg.String (fun s -> state.nameddest <- s),
5940 "<named-destination> Set named destination");
5942 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
5943 ("-cxack", Arg.Set cxack, " Cut corners");
5945 ("-remote", Arg.String (fun s -> rcmdpath := s),
5946 "<path> Set path to the remote commands source");
5948 ("-origin", Arg.String (fun s -> state.origin <- s),
5949 "<original-path> Set original path");
5951 ("-v", Arg.Unit (fun () ->
5952 Printf.printf
5953 "%s\nconfiguration path: %s\n"
5954 (version ())
5955 Config.defconfpath
5957 exit 0), " Print version and exit");
5960 (fun s -> state.path <- s)
5961 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
5963 if !wtmode
5964 then selfexec := !selfexec ^ " -wtmode";
5966 if emptystr state.path
5967 then (prerr_endline "file name missing"; exit 1);
5969 if not (Config.load ())
5970 then prerr_endline "failed to load configuration";
5971 begin match !pageno with
5972 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
5973 | None -> ()
5974 end;
5976 let wsfd, winw, winh = Wsi.init (object (self)
5977 val mutable m_hack = false
5978 val mutable m_clicks = 0
5979 val mutable m_click_x = 0
5980 val mutable m_click_y = 0
5981 val mutable m_lastclicktime = infinity
5983 method private cleanup =
5984 state.roam <- noroam;
5985 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
5986 method expose = if not m_hack then G.postRedisplay "expose"
5987 method visible = G.postRedisplay "visible"
5988 method display = m_hack <- false; display ()
5989 method reshape w h =
5990 self#cleanup;
5991 m_hack <- w < state.winw && h < state.winh;
5992 reshape w h
5993 method mouse b d x y m =
5994 if d && canselect ()
5995 then (
5996 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
5997 m_click_x <- x;
5998 m_click_y <- y;
5999 if b = 1
6000 then (
6001 let t = now () in
6002 if abs x - m_click_x > 10
6003 || abs y - m_click_y > 10
6004 || abs_float (t -. m_lastclicktime) > 0.3
6005 then m_clicks <- 0;
6006 m_clicks <- m_clicks + 1;
6007 m_lastclicktime <- t;
6008 if m_clicks = 1
6009 then (
6010 self#cleanup;
6011 G.postRedisplay "cleanup";
6012 state.uioh <- state.uioh#button b d x y m;
6014 else state.uioh <- state.uioh#multiclick m_clicks x y m
6016 else (
6017 self#cleanup;
6018 m_clicks <- 0;
6019 m_lastclicktime <- infinity;
6020 state.uioh <- state.uioh#button b d x y m
6023 else (
6024 state.uioh <- state.uioh#button b d x y m
6026 method motion x y =
6027 state.mpos <- (x, y);
6028 state.uioh <- state.uioh#motion x y
6029 method pmotion x y =
6030 state.mpos <- (x, y);
6031 state.uioh <- state.uioh#pmotion x y
6032 method key k m =
6033 let mascm = m land (
6034 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6035 ) in
6036 let keyboard k m =
6037 let x = state.x and y = state.y in
6038 keyboard k m;
6039 if x != state.x || y != state.y then self#cleanup
6041 match state.keystate with
6042 | KSnone ->
6043 let km = k, mascm in
6044 begin
6045 match
6046 let modehash = state.uioh#modehash in
6047 try Hashtbl.find modehash km
6048 with Not_found ->
6049 try Hashtbl.find (findkeyhash conf "global") km
6050 with Not_found -> KMinsrt (k, m)
6051 with
6052 | KMinsrt (k, m) -> keyboard k m
6053 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6054 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6056 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6057 List.iter (fun (k, m) -> keyboard k m) insrt;
6058 state.keystate <- KSnone
6059 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6060 state.keystate <- KSinto (keys, insrt)
6061 | _ ->
6062 state.keystate <- KSnone
6064 method enter x y =
6065 state.mpos <- (x, y);
6066 state.uioh <- state.uioh#pmotion x y
6067 method leave = state.mpos <- (-1, -1)
6068 method winstate wsl = state.winstate <- wsl; m_hack <- false
6069 method quit = raise Quit
6070 end) conf.cwinw conf.cwinh (platform = Posx) in
6072 state.wsfd <- wsfd;
6074 if not (
6075 List.exists GlMisc.check_extension
6076 [ "GL_ARB_texture_rectangle"
6077 ; "GL_EXT_texture_recangle"
6078 ; "GL_NV_texture_rectangle" ]
6080 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6082 if (
6083 let r = GlMisc.get_string `renderer in
6084 let p = "Mesa DRI Intel(" in
6085 let l = String.length p in
6086 String.length r > l && String.sub r 0 l = p
6088 then (
6089 defconf.sliceheight <- 1024;
6090 defconf.texcount <- 32;
6091 defconf.usepbo <- true;
6094 let cr, sw =
6095 match Ne.res Unix.pipe with
6096 | Ne.Exn exn ->
6097 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
6098 exit 1
6099 | Ne.Res rw -> rw
6100 and sr, cw =
6101 match Ne.res Unix.pipe with
6102 | Ne.Exn exn ->
6103 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
6104 exit 1
6105 | Ne.Res rw -> rw
6108 cloexec cr;
6109 cloexec sw;
6110 cloexec sr;
6111 cloexec cw;
6113 setcheckers conf.checkers;
6114 redirectstderr ();
6115 if conf.redirectstderr
6116 then
6117 at_exit (fun () ->
6118 let s = Buffer.contents state.errmsgs ^
6119 (match state.errfd with
6120 | Some fd ->
6121 let s = String.create (80*24) in
6122 let n =
6124 let r, _, _ = Unix.select [fd] [] [] 0.0 in
6125 if List.mem fd r
6126 then Unix.read fd s 0 (String.length s)
6127 else 0
6128 with _ -> 0
6130 if n = 0
6131 then ""
6132 else String.sub s 0 n
6133 | None -> ""
6136 try ignore (Unix.write state.stderr s 0 (String.length s))
6137 with exn -> print_endline (exntos exn)
6141 init (cr, cw) (
6142 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6143 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6144 !Config.fontpath, !trimcachepath,
6145 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
6147 List.iter GlArray.enable [`texture_coord; `vertex];
6148 state.sr <- sr;
6149 state.sw <- sw;
6150 state.text <- "Opening " ^ (mbtoutf8 state.path);
6151 reshape winw winh;
6152 opendoc state.path state.password;
6153 state.uioh <- uioh;
6154 display ();
6155 Wsi.mapwin ();
6156 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6157 let optrfd =
6158 ref (
6159 if nonemptystr !rcmdpath
6160 then remoteopen !rcmdpath
6161 else None
6165 let rec loop deadline =
6166 let r =
6167 match state.errfd with
6168 | None -> [state.sr; state.wsfd]
6169 | Some fd -> [state.sr; state.wsfd; fd]
6171 let r =
6172 match !optrfd with
6173 | None -> r
6174 | Some fd -> fd :: r
6176 if state.redisplay
6177 then (
6178 state.redisplay <- false;
6179 display ();
6181 let timeout =
6182 let now = now () in
6183 if deadline > now
6184 then (
6185 if deadline = infinity
6186 then ~-.1.0
6187 else max 0.0 (deadline -. now)
6189 else 0.0
6191 let r, _, _ =
6192 try Unix.select r [] [] timeout
6193 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6195 begin match r with
6196 | [] ->
6197 state.ghyll None;
6198 let newdeadline =
6199 if state.ghyll == noghyll
6200 then
6201 match state.autoscroll with
6202 | Some step when step != 0 ->
6203 let y = state.y + step in
6204 let y =
6205 if y < 0
6206 then state.maxy
6207 else if y >= state.maxy then 0 else y
6209 gotoy y;
6210 if state.mode = View
6211 then state.text <- "";
6212 deadline +. 0.01
6213 | _ -> infinity
6214 else deadline +. 0.01
6216 loop newdeadline
6218 | l ->
6219 let rec checkfds = function
6220 | [] -> ()
6221 | fd :: rest when fd = state.sr ->
6222 let cmd = readcmd state.sr in
6223 act cmd;
6224 checkfds rest
6226 | fd :: rest when fd = state.wsfd ->
6227 Wsi.readresp fd;
6228 checkfds rest
6230 | fd :: rest when Some fd = !optrfd ->
6231 begin match remote fd with
6232 | None -> optrfd := remoteopen !rcmdpath;
6233 | opt -> optrfd := opt
6234 end;
6235 checkfds rest
6237 | fd :: rest ->
6238 let s = String.create 80 in
6239 let n = tempfailureretry (Unix.read fd s 0) 80 in
6240 if conf.redirectstderr
6241 then (
6242 Buffer.add_substring state.errmsgs s 0 n;
6243 state.newerrmsgs <- true;
6244 state.redisplay <- true;
6246 else (
6247 prerr_string (String.sub s 0 n);
6248 flush stderr;
6250 checkfds rest
6252 checkfds l;
6253 let newdeadline =
6254 let deadline1 =
6255 if deadline = infinity
6256 then now () +. 0.01
6257 else deadline
6259 match state.autoscroll with
6260 | Some step when step != 0 -> deadline1
6261 | _ -> if state.ghyll == noghyll then infinity else deadline1
6263 loop newdeadline
6264 end;
6267 loop infinity;
6268 with Quit ->
6269 Config.save leavebirdseye;