Condense and dim
[llpp.git] / main.ml
blob276163c48dce2ff3c6f25498f18e70080366c61f
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 = 17.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 helpmode
2610 then GlDraw.color
2611 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2613 if row = m_active
2614 then (
2615 Gl.disable `texture_2d;
2616 let alpha = if source#hasaction row then 0.9 else 0.3 in
2617 GlDraw.color (1., 1., 1.) ~alpha;
2618 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2619 Gl.enable `texture_2d;
2620 GlDraw.color (1., 1., 1.);
2622 let drawtabularstring s =
2623 let drawstr x s = drawstring1 fs (truncate (x +. x0)) (y+nfs) s in
2624 if trusted
2625 then
2626 let x = if helpmode && row > 0 then x +. ww else x in
2627 let tabpos = try String.index s '\t' with Not_found -> -1 in
2628 if tabpos > 0
2629 then
2630 let len = String.length s - tabpos - 1 in
2631 let s1 = String.sub s 0 tabpos
2632 and s2 = String.sub s (tabpos + 1) len in
2633 let nx = drawstr x s1 in
2634 let sw = nx -. x in
2635 let x = x +. (max tabw sw) in
2636 drawstr x s2
2637 else
2638 let len = String.length s - 2 in
2639 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2640 then
2641 let s = String.sub s 2 len in
2642 let x = if not helpmode then x +. ww else x in
2643 GlDraw.color (1.2, 1.2, 1.2);
2644 let vinc = drawstring1 (fs+fs/4)
2645 (truncate (x -. ww)) (y+nfs) s in
2646 GlDraw.color (1., 1., 1.);
2647 vinc +. (float fs *. 0.8)
2648 else
2649 drawstr x s
2650 else
2651 drawstr x s
2653 let _ = drawtabularstring s in
2654 loop (row+1)
2658 loop m_first;
2659 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2660 let rec loop row =
2661 if (row - m_first) > fstate.maxrows
2662 then ()
2663 else (
2664 if row >= 0 && row < itemcount
2665 then (
2666 let (s, level) = source#getitem row in
2667 let y = (row - m_first) * nfs in
2668 let x = 5.0 +. float (level + m_pan) *. ww in
2669 let (first, last) = minfo.(row) in
2670 let prefix = String.sub s 0 first in
2671 let suffix = String.sub s first (last - first) in
2672 let w1 = measurestr fstate.fontsize prefix in
2673 let w2 = measurestr fstate.fontsize suffix in
2674 let x0 = x +. w1
2675 and y0 = (float (y+2)) in
2676 let x1 = x0 +. w2
2677 and y1 = (float (y+fs+3)) in
2678 filledrect x0 y0 x1 y1;
2679 loop (row+1)
2683 Gl.disable `texture_2d;
2684 if Array.length minfo > 0 then loop m_first;
2685 Gl.disable `blend;
2687 method updownlevel incr =
2688 let len = source#getitemcount in
2689 let curlevel =
2690 if m_active >= 0 && m_active < len
2691 then snd (source#getitem m_active)
2692 else -1
2694 let rec flow i =
2695 if i = len then i-1 else if i = -1 then 0 else
2696 let _, l = source#getitem i in
2697 if l != curlevel then i else flow (i+incr)
2699 let active = flow m_active in
2700 let first = calcfirst m_first active in
2701 G.postRedisplay "outline updownlevel";
2702 {< m_active = active; m_first = first >}
2704 method private key1 key mask =
2705 let set1 active first qsearch =
2706 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2708 let search active pattern incr =
2709 let active = if active = -1 then m_first else active in
2710 let dosearch re =
2711 let rec loop n =
2712 if n >= 0 && n < source#getitemcount
2713 then (
2714 let s, _ = source#getitem n in
2716 (try ignore (Str.search_forward re s 0); true
2717 with Not_found -> false)
2718 then Some n
2719 else loop (n + incr)
2721 else None
2723 loop active
2726 let re = Str.regexp_case_fold pattern in
2727 dosearch re
2728 with Failure s ->
2729 state.text <- s;
2730 None
2732 let itemcount = source#getitemcount in
2733 let find start incr =
2734 let rec find i =
2735 if i = -1 || i = itemcount
2736 then -1
2737 else (
2738 if source#hasaction i
2739 then i
2740 else find (i + incr)
2743 find start
2745 let set active first =
2746 let first = bound first 0 (itemcount - fstate.maxrows) in
2747 state.text <- "";
2748 coe {< m_active = active; m_first = first; m_qsearch = "" >}
2750 let navigate incr =
2751 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2752 let active, first =
2753 let incr1 = if incr > 0 then 1 else -1 in
2754 if isvisible m_first m_active
2755 then
2756 let next =
2757 let next = m_active + incr in
2758 let next =
2759 if next < 0 || next >= itemcount
2760 then -1
2761 else find next incr1
2763 if abs (m_active - next) > fstate.maxrows
2764 then -1
2765 else next
2767 if next = -1
2768 then
2769 let first = m_first + incr in
2770 let first = bound first 0 (itemcount - fstate.maxrows) in
2771 let next =
2772 let next = m_active + incr in
2773 let next = bound next 0 (itemcount - 1) in
2774 find next ~-incr1
2776 let active =
2777 if next = -1
2778 then m_active
2779 else (
2780 if isvisible first next
2781 then next
2782 else m_active
2785 active, first
2786 else
2787 let first = min next m_first in
2788 let first =
2789 if abs (next - first) > fstate.maxrows
2790 then first + incr
2791 else first
2793 next, first
2794 else
2795 let first = m_first + incr in
2796 let first = bound first 0 (itemcount - 1) in
2797 let active =
2798 let next = m_active + incr in
2799 let next = bound next 0 (itemcount - 1) in
2800 let next = find next incr1 in
2801 let active =
2802 if next = -1 || abs (m_active - first) > fstate.maxrows
2803 then (
2804 let active = if m_active = -1 then next else m_active in
2805 active
2807 else next
2809 if isvisible first active
2810 then active
2811 else -1
2813 active, first
2815 G.postRedisplay "listview navigate";
2816 set active first;
2818 match key with
2819 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
2820 let incr = if key = 0x72 then -1 else 1 in
2821 let active, first =
2822 match search (m_active + incr) m_qsearch incr with
2823 | None ->
2824 state.text <- m_qsearch ^ " [not found]";
2825 m_active, m_first
2826 | Some active ->
2827 state.text <- m_qsearch;
2828 active, firstof m_first active
2830 G.postRedisplay "listview ctrl-r/s";
2831 set1 active first m_qsearch;
2833 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
2834 if m_active >= 0 && m_active < source#getitemcount
2835 then (
2836 let s, _ = source#getitem m_active in
2837 selstring s;
2839 coe self
2841 | 0xff08 -> (* backspace *)
2842 if emptystr m_qsearch
2843 then coe self
2844 else (
2845 let qsearch = withoutlastutf8 m_qsearch in
2846 if emptystr qsearch
2847 then (
2848 state.text <- "";
2849 G.postRedisplay "listview empty qsearch";
2850 set1 m_active m_first "";
2852 else
2853 let active, first =
2854 match search m_active qsearch ~-1 with
2855 | None ->
2856 state.text <- qsearch ^ " [not found]";
2857 m_active, m_first
2858 | Some active ->
2859 state.text <- qsearch;
2860 active, firstof m_first active
2862 G.postRedisplay "listview backspace qsearch";
2863 set1 active first qsearch
2866 | key when (key != 0 && key land 0xff00 != 0xff00) ->
2867 let pattern = m_qsearch ^ toutf8 key in
2868 let active, first =
2869 match search m_active pattern 1 with
2870 | None ->
2871 state.text <- pattern ^ " [not found]";
2872 m_active, m_first
2873 | Some active ->
2874 state.text <- pattern;
2875 active, firstof m_first active
2877 G.postRedisplay "listview qsearch add";
2878 set1 active first pattern;
2880 | 0xff1b -> (* escape *)
2881 state.text <- "";
2882 if emptystr m_qsearch
2883 then (
2884 G.postRedisplay "list view escape";
2885 begin
2886 match
2887 source#exit (coe self) true m_active m_first m_pan
2888 with
2889 | None -> m_prev_uioh
2890 | Some uioh -> uioh
2893 else (
2894 G.postRedisplay "list view kill qsearch";
2895 coe {< m_qsearch = "" >}
2898 | 0xff0d | 0xff8d -> (* (kp) enter *)
2899 state.text <- "";
2900 let self = {< m_qsearch = "" >} in
2901 let opt =
2902 G.postRedisplay "listview enter";
2903 if m_active >= 0 && m_active < source#getitemcount
2904 then (
2905 source#exit (coe self) false m_active m_first m_pan;
2907 else (
2908 source#exit (coe self) true m_active m_first m_pan;
2911 begin match opt with
2912 | None -> m_prev_uioh
2913 | Some uioh -> uioh
2916 | 0xff9f | 0xffff -> (* (kp) delete *)
2917 coe self
2919 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
2920 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
2921 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
2922 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
2924 | 0xff53 | 0xff98 -> (* (kp) right *)
2925 state.text <- "";
2926 G.postRedisplay "listview right";
2927 coe {< m_pan = m_pan - 1 >}
2929 | 0xff51 | 0xff96 -> (* (kp) left *)
2930 state.text <- "";
2931 G.postRedisplay "listview left";
2932 coe {< m_pan = m_pan + 1 >}
2934 | 0xff50 | 0xff95 -> (* (kp) home *)
2935 let active = find 0 1 in
2936 G.postRedisplay "listview home";
2937 set active 0;
2939 | 0xff57 | 0xff9c -> (* (kp) end *)
2940 let first = max 0 (itemcount - fstate.maxrows) in
2941 let active = find (itemcount - 1) ~-1 in
2942 G.postRedisplay "listview end";
2943 set active first;
2945 | key when (key = 0 || key land 0xff00 = 0xff00) ->
2946 coe self
2948 | _ ->
2949 dolog "listview unknown key %#x" key; coe self
2951 method key key mask =
2952 match state.mode with
2953 | Textentry te -> textentrykeyboard key mask te; coe self
2954 | _ -> self#key1 key mask
2956 method button button down x y _ =
2957 let opt =
2958 match button with
2959 | 1 when x > state.winw - conf.scrollbw ->
2960 G.postRedisplay "listview scroll";
2961 if down
2962 then
2963 let _, position, sh = self#scrollph in
2964 if y > truncate position && y < truncate (position +. sh)
2965 then (
2966 state.mstate <- Mscrolly;
2967 Some (coe self)
2969 else
2970 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
2971 let first = truncate (s *. float source#getitemcount) in
2972 let first = min source#getitemcount first in
2973 Some (coe {< m_first = first; m_active = first >})
2974 else (
2975 state.mstate <- Mnone;
2976 Some (coe self);
2978 | 1 when not down ->
2979 begin match self#elemunder y with
2980 | Some n ->
2981 G.postRedisplay "listview click";
2982 source#exit (coe {< m_active = n >}) false n m_first m_pan
2983 | _ ->
2984 Some (coe self)
2986 | n when (n == 4 || n == 5) && not down ->
2987 let len = source#getitemcount in
2988 let first =
2989 if n = 5 && m_first + fstate.maxrows >= len
2990 then
2991 m_first
2992 else
2993 let first = m_first + (if n == 4 then -1 else 1) in
2994 bound first 0 (len - 1)
2996 G.postRedisplay "listview wheel";
2997 Some (coe {< m_first = first >})
2998 | n when (n = 6 || n = 7) && not down ->
2999 let inc = if n = 7 then -1 else 1 in
3000 G.postRedisplay "listview hwheel";
3001 Some (coe {< m_pan = m_pan + inc >})
3002 | _ ->
3003 Some (coe self)
3005 match opt with
3006 | None -> m_prev_uioh
3007 | Some uioh -> uioh
3009 method multiclick _ x y = self#button 1 true x y
3011 method motion _ y =
3012 match state.mstate with
3013 | Mscrolly ->
3014 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3015 let first = truncate (s *. float source#getitemcount) in
3016 let first = min source#getitemcount first in
3017 G.postRedisplay "listview motion";
3018 coe {< m_first = first; m_active = first >}
3019 | _ -> coe self
3021 method pmotion x y =
3022 if x < state.winw - conf.scrollbw
3023 then
3024 let n =
3025 match self#elemunder y with
3026 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3027 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3029 let o =
3030 if n != m_active
3031 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3032 else self
3034 coe o
3035 else (
3036 Wsi.setcursor Wsi.CURSOR_INHERIT;
3037 coe self
3040 method infochanged _ = ()
3042 method scrollpw = (0, 0.0, 0.0)
3043 method scrollph =
3044 let nfs = fstate.fontsize + 1 in
3045 let y = m_first * nfs in
3046 let itemcount = source#getitemcount in
3047 let maxi = max 0 (itemcount - fstate.maxrows) in
3048 let maxy = maxi * nfs in
3049 let p, h = scrollph y maxy in
3050 conf.scrollbw, p, h
3052 method modehash = modehash
3053 method eformsgs = false
3054 end;;
3056 class outlinelistview ~source =
3057 let settext autonarrow s =
3058 if autonarrow
3059 then
3060 let ss = source#statestr in
3061 state.text <-
3062 if emptystr ss
3063 then "[" ^ s ^ "]"
3064 else "{" ^ ss ^ "} [" ^ s ^ "]"
3065 else state.text <- s
3067 object (self)
3068 inherit listview
3069 ~helpmode:false
3070 ~source:(source :> lvsource)
3071 ~trusted:false
3072 ~modehash:(findkeyhash conf "outline")
3073 as super
3075 val m_autonarrow = false
3077 method display = super#display
3079 method key key mask =
3080 let maxrows =
3081 if emptystr state.text
3082 then fstate.maxrows
3083 else fstate.maxrows - 2
3085 let calcfirst first active =
3086 if active > first
3087 then
3088 let rows = active - first in
3089 if rows > maxrows then active - maxrows else first
3090 else active
3092 let navigate incr =
3093 let active = m_active + incr in
3094 let active = bound active 0 (source#getitemcount - 1) in
3095 let first = calcfirst m_first active in
3096 G.postRedisplay "outline navigate";
3097 coe {< m_active = active; m_first = first >}
3099 let navscroll first =
3100 let active =
3101 let dist = m_active - first in
3102 if dist < 0
3103 then first
3104 else (
3105 if dist < maxrows
3106 then m_active
3107 else first + maxrows
3110 G.postRedisplay "outline navscroll";
3111 coe {< m_first = first; m_active = active >}
3113 let ctrl = Wsi.withctrl mask in
3114 match key with
3115 | 97 when ctrl -> (* ctrl-a *)
3116 let text =
3117 if m_autonarrow
3118 then (source#denarrow; "")
3119 else (
3120 let pattern = source#renarrow in
3121 if nonemptystr m_qsearch
3122 then (source#narrow m_qsearch; m_qsearch)
3123 else pattern
3126 settext (not m_autonarrow) text;
3127 G.postRedisplay "toggle auto narrowing";
3128 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3130 | 47 when emptystr m_qsearch && not m_autonarrow -> (* / *)
3131 settext true "";
3132 G.postRedisplay "toggle auto narrowing";
3133 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3135 | 110 when ctrl -> (* ctrl-n *)
3136 source#narrow m_qsearch;
3137 if not m_autonarrow
3138 then source#add_narrow_pattern m_qsearch;
3139 G.postRedisplay "outline ctrl-n";
3140 coe {< m_first = 0; m_active = 0 >}
3142 | 83 when ctrl -> (* ctrl-S *)
3143 let active = source#calcactive (getanchor ()) in
3144 let first = firstof m_first active in
3145 G.postRedisplay "outline ctrl-s";
3146 coe {< m_first = first; m_active = active >}
3148 | 117 when ctrl -> (* ctrl-u *)
3149 G.postRedisplay "outline ctrl-u";
3150 if m_autonarrow && nonemptystr m_qsearch
3151 then (
3152 ignore (source#renarrow);
3153 settext m_autonarrow "";
3154 coe {< m_first = 0; m_active = 0; m_qsearch = "" >}
3156 else (
3157 source#del_narrow_pattern;
3158 let pattern = source#renarrow in
3159 let text =
3160 if emptystr pattern then "" else "Narrowed to " ^ pattern
3162 settext m_autonarrow text;
3163 coe {< m_first = 0; m_active = 0; m_qsearch = "" >}
3166 | 108 when ctrl -> (* ctrl-l *)
3167 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3168 G.postRedisplay "outline ctrl-l";
3169 coe {< m_first = first >}
3171 | 0xff09 when m_autonarrow -> (* tab *)
3172 if nonemptystr m_qsearch
3173 then (
3174 G.postRedisplay "outline list view tab";
3175 source#add_narrow_pattern m_qsearch;
3176 settext true "";
3177 coe {< m_qsearch = "" >}
3179 else coe self
3181 | 0xff1b when m_autonarrow -> (* escape *)
3182 if nonemptystr m_qsearch
3183 then source#add_narrow_pattern m_qsearch;
3184 super#key key mask
3186 | 0xff0d | 0xff8d when m_autonarrow -> (* (kp) enter *)
3187 if nonemptystr m_qsearch
3188 then source#add_narrow_pattern m_qsearch;
3189 super#key key mask
3191 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
3192 let pattern = m_qsearch ^ toutf8 key in
3193 G.postRedisplay "outlinelistview autonarrow add";
3194 source#narrow pattern;
3195 settext true pattern;
3196 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3198 | key when m_autonarrow && key = 0xff08 -> (* backspace *)
3199 if emptystr m_qsearch
3200 then coe self
3201 else
3202 let pattern = withoutlastutf8 m_qsearch in
3203 G.postRedisplay "outlinelistview autonarrow backspace";
3204 ignore (source#renarrow);
3205 source#narrow pattern;
3206 settext true pattern;
3207 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3209 | 0xff9f | 0xffff -> (* (kp) delete *)
3210 source#remove m_active;
3211 G.postRedisplay "outline delete";
3212 let active = max 0 (m_active-1) in
3213 coe {< m_first = firstof m_first active;
3214 m_active = active >}
3216 | 0xff52 | 0xff97 when ctrl -> (* ctrl-(kp) up *)
3217 navscroll (max 0 (m_first - 1))
3219 | 0xff54 | 0xff99 when ctrl -> (* ctrl-(kp) down *)
3220 navscroll (min (source#getitemcount - 1) (m_first + 1))
3222 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3223 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3224 | 0xff55 | 0xff9a -> (* (kp) prior *)
3225 navigate ~-(fstate.maxrows)
3226 | 0xff56 | 0xff9b -> (* (kp) next *)
3227 navigate fstate.maxrows
3229 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3230 let o =
3231 if ctrl
3232 then (
3233 G.postRedisplay "outline ctrl right";
3234 {< m_pan = m_pan + 1 >}
3236 else self#updownlevel 1
3238 coe o
3240 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3241 let o =
3242 if ctrl
3243 then (
3244 G.postRedisplay "outline ctrl left";
3245 {< m_pan = m_pan - 1 >}
3247 else self#updownlevel ~-1
3249 coe o
3251 | 0xff50 | 0xff95 -> (* (kp) home *)
3252 G.postRedisplay "outline home";
3253 coe {< m_first = 0; m_active = 0 >}
3255 | 0xff57 | 0xff9c -> (* (kp) end *)
3256 let active = source#getitemcount - 1 in
3257 let first = max 0 (active - fstate.maxrows) in
3258 G.postRedisplay "outline end";
3259 coe {< m_active = active; m_first = first >}
3261 | _ -> super#key key mask
3264 let gotounder under =
3265 let getpath filename =
3266 let path =
3267 if nonemptystr filename
3268 then
3269 if Filename.is_relative filename
3270 then
3271 let dir = Filename.dirname state.path in
3272 let dir =
3273 if Filename.is_implicit dir
3274 then Filename.concat (Sys.getcwd ()) dir
3275 else dir
3277 Filename.concat dir filename
3278 else filename
3279 else ""
3281 if Sys.file_exists path
3282 then path
3283 else ""
3285 match under with
3286 | Ulinkgoto (pageno, top) ->
3287 if pageno >= 0
3288 then (
3289 addnav ();
3290 gotopage1 pageno top;
3293 | Ulinkuri s ->
3294 gotouri s
3296 | Uremote (filename, pageno) ->
3297 let path = getpath filename in
3298 if nonemptystr path
3299 then (
3300 if conf.riani
3301 then
3302 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
3303 try popen command []
3304 with exn ->
3305 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
3306 flush stderr;
3307 else
3308 let anchor = getanchor () in
3309 let ranchor = state.path, state.password, anchor, state.origin in
3310 state.origin <- "";
3311 state.anchor <- (pageno, 0.0, 0.0);
3312 state.ranchors <- ranchor :: state.ranchors;
3313 opendoc path "";
3315 else showtext '!' ("Could not find " ^ filename)
3317 | Uremotedest (filename, destname) ->
3318 let path = getpath filename in
3319 if nonemptystr path
3320 then (
3321 if conf.riani
3322 then
3323 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
3324 try popen command []
3325 with exn ->
3326 Printf.eprintf
3327 "failed to execute `%s': %s\n" command (exntos exn);
3328 flush stderr;
3329 else
3330 let anchor = getanchor () in
3331 let ranchor = state.path, state.password, anchor, state.origin in
3332 state.origin <- "";
3333 state.nameddest <- destname;
3334 state.ranchors <- ranchor :: state.ranchors;
3335 opendoc path "";
3337 else showtext '!' ("Could not find " ^ filename)
3339 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
3342 let gotooutline (_, _, kind) =
3343 match kind with
3344 | Onone -> ()
3345 | Oanchor anchor ->
3346 let (pageno, y, _) = anchor in
3347 let y = getanchory
3348 (if conf.presentation then (pageno, y, 1.0) else anchor)
3350 addnav ();
3351 gotoghyll y
3352 | Ouri uri -> gotounder (Ulinkuri uri)
3353 | Olaunch cmd -> gotounder (Ulaunch cmd)
3354 | Oremote remote -> gotounder (Uremote remote)
3355 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
3358 let outlinesource usebookmarks =
3359 let empty = [||] in
3360 (object (self)
3361 inherit lvsourcebase
3362 val mutable m_items = empty
3363 val mutable m_minfo = empty
3364 val mutable m_orig_items = empty
3365 val mutable m_orig_minfo = empty
3366 val mutable m_narrow_patterns = []
3367 val mutable m_hadremovals = false
3368 val mutable m_gen = -1
3370 method getitemcount =
3371 Array.length m_items + (if m_hadremovals then 1 else 0)
3373 method getitem n =
3374 if n == Array.length m_items && m_hadremovals
3375 then
3376 ("[Confirm removal]", 0)
3377 else
3378 let s, n, _ = m_items.(n) in
3379 (s, n)
3381 method exit ~uioh ~cancel ~active ~first ~pan =
3382 ignore (uioh, first);
3383 let confrimremoval = m_hadremovals && active = Array.length m_items in
3384 let items, minfo =
3385 if m_narrow_patterns = []
3386 then m_orig_items, m_orig_minfo
3387 else m_items, m_minfo
3389 if not cancel
3390 then (
3391 if not confrimremoval
3392 then (
3393 gotooutline m_items.(active);
3394 m_items <- items;
3395 m_minfo <- minfo;
3397 else (
3398 state.bookmarks <- Array.to_list m_items;
3399 m_orig_items <- m_items;
3400 m_orig_minfo <- m_minfo;
3403 else (
3404 m_items <- items;
3405 m_minfo <- minfo;
3407 m_pan <- pan;
3408 None
3410 method hasaction _ = true
3412 method greetmsg =
3413 if Array.length m_items != Array.length m_orig_items
3414 then
3415 let s =
3416 match m_narrow_patterns with
3417 | one :: [] -> one
3418 | many -> String.concat "\xe2\x80\xa6" (List.rev many)
3420 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
3421 else ""
3423 method statestr =
3424 match m_narrow_patterns with
3425 | [] -> ""
3426 | one :: [] -> one
3427 | head :: _ -> "\xe2\x80\xa6" ^ head
3429 method narrow pattern =
3430 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3431 match reopt with
3432 | None -> ()
3433 | Some re ->
3434 let rec loop accu minfo n =
3435 if n = -1
3436 then (
3437 m_items <- Array.of_list accu;
3438 m_minfo <- Array.of_list minfo;
3440 else
3441 let (s, _, _) as o = m_items.(n) in
3442 let accu, minfo =
3443 let first =
3444 try Str.search_forward re s 0
3445 with Not_found -> -1
3447 if first >= 0
3448 then o :: accu, (first, Str.match_end ()) :: minfo
3449 else accu, minfo
3451 loop accu minfo (n-1)
3453 loop [] [] (Array.length m_items - 1)
3455 method getminfo = m_minfo
3457 method denarrow =
3458 m_orig_items <- (
3459 if usebookmarks
3460 then Array.of_list state.bookmarks
3461 else state.outlines
3463 m_minfo <- m_orig_minfo;
3464 m_items <- m_orig_items
3466 method remove m =
3467 if usebookmarks
3468 then
3469 if m >= 0 && m < Array.length m_items
3470 then (
3471 m_hadremovals <- true;
3472 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3473 let n = if n >= m then n+1 else n in
3474 m_items.(n)
3478 method add_narrow_pattern pattern =
3479 m_narrow_patterns <- pattern :: m_narrow_patterns
3481 method del_narrow_pattern =
3482 match m_narrow_patterns with
3483 | _ :: rest -> m_narrow_patterns <- rest
3484 | [] -> ()
3486 method renarrow =
3487 self#denarrow;
3488 match m_narrow_patterns with
3489 | pattern :: [] -> self#narrow pattern; pattern
3490 | list ->
3491 List.fold_left (fun accu pattern ->
3492 self#narrow pattern;
3493 pattern ^ "\xe2\x80\xa6" ^ accu) "" list
3495 method calcactive anchor =
3496 let rely = getanchory anchor in
3497 let rec loop n best bestd =
3498 if n = Array.length m_items
3499 then best
3500 else
3501 let _, _, kind = m_items.(n) in
3502 match kind with
3503 | Oanchor anchor ->
3504 let orely = getanchory anchor in
3505 let d = abs (orely - rely) in
3506 if d < bestd
3507 then loop (n+1) n d
3508 else loop (n+1) best bestd
3509 | Onone | Oremote _ | Olaunch _ | Oremotedest _ | Ouri _ ->
3510 loop (n+1) best bestd
3512 loop 0 ~-1 max_int
3514 method reset anchor items =
3515 m_hadremovals <- false;
3516 if state.gen != m_gen
3517 then (
3518 m_orig_items <- items;
3519 m_items <- items;
3520 m_narrow_patterns <- [];
3521 m_minfo <- empty;
3522 m_orig_minfo <- empty;
3523 m_gen <- state.gen;
3525 else (
3526 if items != m_orig_items
3527 then (
3528 m_orig_items <- items;
3529 if m_narrow_patterns == []
3530 then m_items <- items;
3533 let active = self#calcactive anchor in
3534 m_active <- active;
3535 m_first <- firstof m_first active
3536 end)
3539 let enterselector usebookmarks =
3540 resetmstate ();
3541 let source = outlinesource usebookmarks in
3542 fun errmsg ->
3543 let outlines =
3544 if usebookmarks
3545 then Array.of_list state.bookmarks
3546 else state.outlines
3548 if Array.length outlines = 0
3549 then (
3550 showtext ' ' errmsg;
3552 else (
3553 state.text <- source#greetmsg;
3554 Wsi.setcursor Wsi.CURSOR_INHERIT;
3555 let anchor = getanchor () in
3556 source#reset anchor outlines;
3557 state.uioh <- coe (new outlinelistview ~source);
3558 G.postRedisplay "enter selector";
3562 let enteroutlinemode =
3563 let f = enterselector false in
3564 fun () -> f "Document has no outline";
3567 let enterbookmarkmode =
3568 let f = enterselector true in
3569 fun () -> f "Document has no bookmarks (yet)";
3572 let makecheckers () =
3573 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3574 following to say:
3575 converted by Issac Trotts. July 25, 2002 *)
3576 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3577 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3578 let id = GlTex.gen_texture () in
3579 GlTex.bind_texture `texture_2d id;
3580 GlPix.store (`unpack_alignment 1);
3581 GlTex.image2d image;
3582 List.iter (GlTex.parameter ~target:`texture_2d)
3583 [ `mag_filter `nearest; `min_filter `nearest ];
3587 let setcheckers enabled =
3588 match state.checkerstexid with
3589 | None ->
3590 if enabled then state.checkerstexid <- Some (makecheckers ())
3592 | Some checkerstexid ->
3593 if not enabled
3594 then (
3595 GlTex.delete_texture checkerstexid;
3596 state.checkerstexid <- None;
3600 let describe_location () =
3601 let fn = page_of_y state.y in
3602 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
3603 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3604 let percent =
3605 if maxy <= 0
3606 then 100.
3607 else (100. *. (float state.y /. float maxy))
3609 if fn = ln
3610 then
3611 Printf.sprintf "page %d of %d [%.2f%%]"
3612 (fn+1) state.pagecount percent
3613 else
3614 Printf.sprintf
3615 "pages %d-%d of %d [%.2f%%]"
3616 (fn+1) (ln+1) state.pagecount percent
3619 let setpresentationmode v =
3620 let n = page_of_y state.y in
3621 state.anchor <- (n, 0.0, 1.0);
3622 conf.presentation <- v;
3623 if conf.fitmodel = FitPage
3624 then reqlayout conf.angle conf.fitmodel;
3625 represent ();
3628 let enterinfomode =
3629 let btos b = if b then "\xe2\x88\x9a" else "" in
3630 let showextended = ref false in
3631 let leave mode = function
3632 | Confirm -> state.mode <- mode
3633 | Cancel -> state.mode <- mode in
3634 let src =
3635 (object
3636 val mutable m_first_time = true
3637 val mutable m_l = []
3638 val mutable m_a = [||]
3639 val mutable m_prev_uioh = nouioh
3640 val mutable m_prev_mode = View
3642 inherit lvsourcebase
3644 method reset prev_mode prev_uioh =
3645 m_a <- Array.of_list (List.rev m_l);
3646 m_l <- [];
3647 m_prev_mode <- prev_mode;
3648 m_prev_uioh <- prev_uioh;
3649 if m_first_time
3650 then (
3651 let rec loop n =
3652 if n >= Array.length m_a
3653 then ()
3654 else
3655 match m_a.(n) with
3656 | _, _, _, Action _ -> m_active <- n
3657 | _ -> loop (n+1)
3659 loop 0;
3660 m_first_time <- false;
3663 method int name get set =
3664 m_l <-
3665 (name, `int get, 1, Action (
3666 fun u ->
3667 let ondone s =
3668 try set (int_of_string s)
3669 with exn ->
3670 state.text <- Printf.sprintf "bad integer `%s': %s"
3671 s (exntos exn)
3673 state.text <- "";
3674 let te = name ^ ": ", "", None, intentry, ondone, true in
3675 state.mode <- Textentry (te, leave m_prev_mode);
3677 )) :: m_l
3679 method int_with_suffix name get set =
3680 m_l <-
3681 (name, `intws get, 1, Action (
3682 fun u ->
3683 let ondone s =
3684 try set (int_of_string_with_suffix s)
3685 with exn ->
3686 state.text <- Printf.sprintf "bad integer `%s': %s"
3687 s (exntos exn)
3689 state.text <- "";
3690 let te =
3691 name ^ ": ", "", None, intentry_with_suffix, ondone, true
3693 state.mode <- Textentry (te, leave m_prev_mode);
3695 )) :: m_l
3697 method bool ?(offset=1) ?(btos=btos) name get set =
3698 m_l <-
3699 (name, `bool (btos, get), offset, Action (
3700 fun u ->
3701 let v = get () in
3702 set (not v);
3704 )) :: m_l
3706 method color name get set =
3707 m_l <-
3708 (name, `color get, 1, Action (
3709 fun u ->
3710 let invalid = (nan, nan, nan) in
3711 let ondone s =
3712 let c =
3713 try color_of_string s
3714 with exn ->
3715 state.text <- Printf.sprintf "bad color `%s': %s"
3716 s (exntos exn);
3717 invalid
3719 if c <> invalid
3720 then set c;
3722 let te = name ^ ": ", "", None, textentry, ondone, true in
3723 state.text <- color_to_string (get ());
3724 state.mode <- Textentry (te, leave m_prev_mode);
3726 )) :: m_l
3728 method string name get set =
3729 m_l <-
3730 (name, `string get, 1, Action (
3731 fun u ->
3732 let ondone s = set s in
3733 let te = name ^ ": ", "", None, textentry, ondone, true in
3734 state.mode <- Textentry (te, leave m_prev_mode);
3736 )) :: m_l
3738 method colorspace name get set =
3739 m_l <-
3740 (name, `string get, 1, Action (
3741 fun _ ->
3742 let source =
3743 (object
3744 inherit lvsourcebase
3746 initializer
3747 m_active <- CSTE.to_int conf.colorspace;
3748 m_first <- 0;
3750 method getitemcount =
3751 Array.length CSTE.names
3752 method getitem n =
3753 (CSTE.names.(n), 0)
3754 method exit ~uioh ~cancel ~active ~first ~pan =
3755 ignore (uioh, first, pan);
3756 if not cancel then set active;
3757 None
3758 method hasaction _ = true
3759 end)
3761 state.text <- "";
3762 let modehash = findkeyhash conf "info" in
3763 coe (new listview ~helpmode:false ~source ~trusted:true ~modehash)
3764 )) :: m_l
3766 method paxmark name get set =
3767 m_l <-
3768 (name, `string get, 1, Action (
3769 fun _ ->
3770 let source =
3771 (object
3772 inherit lvsourcebase
3774 initializer
3775 m_active <- MTE.to_int conf.paxmark;
3776 m_first <- 0;
3778 method getitemcount = Array.length MTE.names
3779 method getitem n = (MTE.names.(n), 0)
3780 method exit ~uioh ~cancel ~active ~first ~pan =
3781 ignore (uioh, first, pan);
3782 if not cancel then set active;
3783 None
3784 method hasaction _ = true
3785 end)
3787 state.text <- "";
3788 let modehash = findkeyhash conf "info" in
3789 coe (new listview ~helpmode:false ~source ~trusted:true ~modehash)
3790 )) :: m_l
3792 method fitmodel name get set =
3793 m_l <-
3794 (name, `string get, 1, Action (
3795 fun _ ->
3796 let source =
3797 (object
3798 inherit lvsourcebase
3800 initializer
3801 m_active <- FMTE.to_int conf.fitmodel;
3802 m_first <- 0;
3804 method getitemcount = Array.length FMTE.names
3805 method getitem n = (FMTE.names.(n), 0)
3806 method exit ~uioh ~cancel ~active ~first ~pan =
3807 ignore (uioh, first, pan);
3808 if not cancel then set active;
3809 None
3810 method hasaction _ = true
3811 end)
3813 state.text <- "";
3814 let modehash = findkeyhash conf "info" in
3815 coe (new listview ~helpmode:false ~source ~trusted:true ~modehash)
3816 )) :: m_l
3818 method caption s offset =
3819 m_l <- (s, `empty, offset, Noaction) :: m_l
3821 method caption2 s f offset =
3822 m_l <- (s, `string f, offset, Noaction) :: m_l
3824 method getitemcount = Array.length m_a
3826 method getitem n =
3827 let tostr = function
3828 | `int f -> string_of_int (f ())
3829 | `intws f -> string_with_suffix_of_int (f ())
3830 | `string f -> f ()
3831 | `color f -> color_to_string (f ())
3832 | `bool (btos, f) -> btos (f ())
3833 | `empty -> ""
3835 let name, t, offset, _ = m_a.(n) in
3836 ((let s = tostr t in
3837 if nonemptystr s
3838 then Printf.sprintf "%s\t%s" name s
3839 else name),
3840 offset)
3842 method exit ~uioh ~cancel ~active ~first ~pan =
3843 let uiohopt =
3844 if not cancel
3845 then (
3846 let uioh =
3847 match m_a.(active) with
3848 | _, _, _, Action f -> f uioh
3849 | _ -> uioh
3851 Some uioh
3853 else None
3855 m_active <- active;
3856 m_first <- first;
3857 m_pan <- pan;
3858 uiohopt
3860 method hasaction n =
3861 match m_a.(n) with
3862 | _, _, _, Action _ -> true
3863 | _ -> false
3864 end)
3866 let rec fillsrc prevmode prevuioh =
3867 let sep () = src#caption "" 0 in
3868 let colorp name get set =
3869 src#string name
3870 (fun () -> color_to_string (get ()))
3871 (fun v ->
3873 let c = color_of_string v in
3874 set c
3875 with exn ->
3876 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
3879 let oldmode = state.mode in
3880 let birdseye = isbirdseye state.mode in
3882 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3884 src#bool "presentation mode"
3885 (fun () -> conf.presentation)
3886 (fun v -> setpresentationmode v);
3888 src#bool "ignore case in searches"
3889 (fun () -> conf.icase)
3890 (fun v -> conf.icase <- v);
3892 src#bool "preload"
3893 (fun () -> conf.preload)
3894 (fun v -> conf.preload <- v);
3896 src#bool "highlight links"
3897 (fun () -> conf.hlinks)
3898 (fun v -> conf.hlinks <- v);
3900 src#bool "under info"
3901 (fun () -> conf.underinfo)
3902 (fun v -> conf.underinfo <- v);
3904 src#bool "persistent bookmarks"
3905 (fun () -> conf.savebmarks)
3906 (fun v -> conf.savebmarks <- v);
3908 src#fitmodel "fit model"
3909 (fun () -> FMTE.to_string conf.fitmodel)
3910 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3912 src#bool "trim margins"
3913 (fun () -> conf.trimmargins)
3914 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3916 src#bool "persistent location"
3917 (fun () -> conf.jumpback)
3918 (fun v -> conf.jumpback <- v);
3920 sep ();
3921 src#int "inter-page space"
3922 (fun () -> conf.interpagespace)
3923 (fun n ->
3924 conf.interpagespace <- n;
3925 docolumns conf.columns;
3926 let pageno, py =
3927 match state.layout with
3928 | [] -> 0, 0
3929 | l :: _ ->
3930 l.pageno, l.pagey
3932 state.maxy <- calcheight ();
3933 let y = getpagey pageno in
3934 gotoy (y + py)
3937 src#int "page bias"
3938 (fun () -> conf.pagebias)
3939 (fun v -> conf.pagebias <- v);
3941 src#int "scroll step"
3942 (fun () -> conf.scrollstep)
3943 (fun n -> conf.scrollstep <- n);
3945 src#int "horizontal scroll step"
3946 (fun () -> conf.hscrollstep)
3947 (fun v -> conf.hscrollstep <- v);
3949 src#int "auto scroll step"
3950 (fun () ->
3951 match state.autoscroll with
3952 | Some step -> step
3953 | _ -> conf.autoscrollstep)
3954 (fun n ->
3955 let n = boundastep state.winh n in
3956 if state.autoscroll <> None
3957 then state.autoscroll <- Some n;
3958 conf.autoscrollstep <- n);
3960 src#int "zoom"
3961 (fun () -> truncate (conf.zoom *. 100.))
3962 (fun v -> setzoom ((float v) /. 100.));
3964 src#int "rotation"
3965 (fun () -> conf.angle)
3966 (fun v -> reqlayout v conf.fitmodel);
3968 src#int "scroll bar width"
3969 (fun () -> conf.scrollbw)
3970 (fun v ->
3971 conf.scrollbw <- v;
3972 reshape state.winw state.winh;
3975 src#int "scroll handle height"
3976 (fun () -> conf.scrollh)
3977 (fun v -> conf.scrollh <- v;);
3979 src#int "thumbnail width"
3980 (fun () -> conf.thumbw)
3981 (fun v ->
3982 conf.thumbw <- min 4096 v;
3983 match oldmode with
3984 | Birdseye beye ->
3985 leavebirdseye beye false;
3986 enterbirdseye ()
3987 | _ -> ()
3990 let mode = state.mode in
3991 src#string "columns"
3992 (fun () ->
3993 match conf.columns with
3994 | Csingle _ -> "1"
3995 | Cmulti (multi, _) -> multicolumns_to_string multi
3996 | Csplit (count, _) -> "-" ^ string_of_int count
3998 (fun v ->
3999 let n, a, b = multicolumns_of_string v in
4000 setcolumns mode n a b);
4002 sep ();
4003 src#caption "Pixmap cache" 0;
4004 src#int_with_suffix "size (advisory)"
4005 (fun () -> conf.memlimit)
4006 (fun v -> conf.memlimit <- v);
4008 src#caption2 "used"
4009 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4010 (string_with_suffix_of_int state.memused)
4011 (Hashtbl.length state.tilemap)) 1;
4013 sep ();
4014 src#caption "Layout" 0;
4015 src#caption2 "Dimension"
4016 (fun () ->
4017 Printf.sprintf "%dx%d (virtual %dx%d)"
4018 state.winw state.winh
4019 state.w state.maxy)
4021 if conf.debug
4022 then
4023 src#caption2 "Position" (fun () ->
4024 Printf.sprintf "%dx%d" state.x state.y
4026 else
4027 src#caption2 "Position" (fun () -> describe_location ()) 1
4030 sep ();
4031 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4032 "Save these parameters as global defaults at exit"
4033 (fun () -> conf.bedefault)
4034 (fun v -> conf.bedefault <- v)
4037 sep ();
4038 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4039 src#bool ~offset:0 ~btos "Extended parameters"
4040 (fun () -> !showextended)
4041 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4042 if !showextended
4043 then (
4044 src#bool "checkers"
4045 (fun () -> conf.checkers)
4046 (fun v -> conf.checkers <- v; setcheckers v);
4047 src#bool "update cursor"
4048 (fun () -> conf.updatecurs)
4049 (fun v -> conf.updatecurs <- v);
4050 src#bool "scroll-bar on the left"
4051 (fun () -> conf.leftscroll)
4052 (fun v -> conf.leftscroll <- v);
4053 src#bool "verbose"
4054 (fun () -> conf.verbose)
4055 (fun v -> conf.verbose <- v);
4056 src#bool "invert colors"
4057 (fun () -> conf.invert)
4058 (fun v -> conf.invert <- v);
4059 src#bool "max fit"
4060 (fun () -> conf.maxhfit)
4061 (fun v -> conf.maxhfit <- v);
4062 src#bool "redirect stderr"
4063 (fun () -> conf.redirectstderr)
4064 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4065 src#bool "pax mode"
4066 (fun () -> conf.pax != None)
4067 (fun v ->
4068 if v
4069 then conf.pax <- Some (ref (now (), 0, 0))
4070 else conf.pax <- None);
4071 src#string "uri launcher"
4072 (fun () -> conf.urilauncher)
4073 (fun v -> conf.urilauncher <- v);
4074 src#string "path launcher"
4075 (fun () -> conf.pathlauncher)
4076 (fun v -> conf.pathlauncher <- v);
4077 src#string "tile size"
4078 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4079 (fun v ->
4081 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4082 conf.tilew <- max 64 w;
4083 conf.tileh <- max 64 h;
4084 flushtiles ();
4085 with exn ->
4086 state.text <- Printf.sprintf "bad tile size `%s': %s"
4087 v (exntos exn)
4089 src#int "texture count"
4090 (fun () -> conf.texcount)
4091 (fun v ->
4092 if realloctexts v
4093 then conf.texcount <- v
4094 else showtext '!' " Failed to set texture count please retry later"
4096 src#int "slice height"
4097 (fun () -> conf.sliceheight)
4098 (fun v ->
4099 conf.sliceheight <- v;
4100 wcmd "sliceh %d" conf.sliceheight;
4102 src#int "anti-aliasing level"
4103 (fun () -> conf.aalevel)
4104 (fun v ->
4105 conf.aalevel <- bound v 0 8;
4106 state.anchor <- getanchor ();
4107 opendoc state.path state.password;
4109 src#string "page scroll scaling factor"
4110 (fun () -> string_of_float conf.pgscale)
4111 (fun v ->
4113 let s = float_of_string v in
4114 conf.pgscale <- s
4115 with exn ->
4116 state.text <- Printf.sprintf
4117 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4120 src#int "ui font size"
4121 (fun () -> fstate.fontsize)
4122 (fun v -> setfontsize (bound v 5 100));
4123 src#int "hint font size"
4124 (fun () -> conf.hfsize)
4125 (fun v -> conf.hfsize <- bound v 5 100);
4126 colorp "background color"
4127 (fun () -> conf.bgcolor)
4128 (fun v -> conf.bgcolor <- v);
4129 src#bool "crop hack"
4130 (fun () -> conf.crophack)
4131 (fun v -> conf.crophack <- v);
4132 src#string "trim fuzz"
4133 (fun () -> irect_to_string conf.trimfuzz)
4134 (fun v ->
4136 conf.trimfuzz <- irect_of_string v;
4137 if conf.trimmargins
4138 then settrim true conf.trimfuzz;
4139 with exn ->
4140 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4142 src#string "throttle"
4143 (fun () ->
4144 match conf.maxwait with
4145 | None -> "show place holder if page is not ready"
4146 | Some time ->
4147 if time = infinity
4148 then "wait for page to fully render"
4149 else
4150 "wait " ^ string_of_float time
4151 ^ " seconds before showing placeholder"
4153 (fun v ->
4155 let f = float_of_string v in
4156 if f <= 0.0
4157 then conf.maxwait <- None
4158 else conf.maxwait <- Some f
4159 with exn ->
4160 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4162 src#string "ghyll scroll"
4163 (fun () ->
4164 match conf.ghyllscroll with
4165 | None -> ""
4166 | Some nab -> ghyllscroll_to_string nab
4168 (fun v ->
4169 try conf.ghyllscroll <- ghyllscroll_of_string v
4170 with exn ->
4171 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4173 src#string "selection command"
4174 (fun () -> conf.selcmd)
4175 (fun v -> conf.selcmd <- v);
4176 src#string "synctex command"
4177 (fun () -> conf.stcmd)
4178 (fun v -> conf.stcmd <- v);
4179 src#string "pax command"
4180 (fun () -> conf.paxcmd)
4181 (fun v -> conf.paxcmd <- v);
4182 src#colorspace "color space"
4183 (fun () -> CSTE.to_string conf.colorspace)
4184 (fun v ->
4185 conf.colorspace <- CSTE.of_int v;
4186 wcmd "cs %d" v;
4187 load state.layout;
4189 src#paxmark "pax mark method"
4190 (fun () -> MTE.to_string conf.paxmark)
4191 (fun v -> conf.paxmark <- MTE.of_int v);
4192 if pbousable ()
4193 then
4194 src#bool "use PBO"
4195 (fun () -> conf.usepbo)
4196 (fun v -> conf.usepbo <- v);
4197 src#bool "mouse wheel scrolls pages"
4198 (fun () -> conf.wheelbypage)
4199 (fun v -> conf.wheelbypage <- v);
4200 src#bool "open remote links in a new instance"
4201 (fun () -> conf.riani)
4202 (fun v -> conf.riani <- v);
4205 sep ();
4206 src#caption "Document" 0;
4207 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4208 src#caption2 "Pages"
4209 (fun () -> string_of_int state.pagecount) 1;
4210 src#caption2 "Dimensions"
4211 (fun () -> string_of_int (List.length state.pdims)) 1;
4212 if conf.trimmargins
4213 then (
4214 sep ();
4215 src#caption "Trimmed margins" 0;
4216 src#caption2 "Dimensions"
4217 (fun () -> string_of_int (List.length state.pdims)) 1;
4220 sep ();
4221 src#caption "OpenGL" 0;
4222 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4223 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4225 sep ();
4226 src#caption "Location" 0;
4227 if nonemptystr state.origin
4228 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4229 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4231 src#reset prevmode prevuioh;
4233 fun () ->
4234 state.text <- "";
4235 resetmstate ();
4236 let prevmode = state.mode
4237 and prevuioh = state.uioh in
4238 fillsrc prevmode prevuioh;
4239 let source = (src :> lvsource) in
4240 let modehash = findkeyhash conf "info" in
4241 state.uioh <- coe (object (self)
4242 inherit listview ~helpmode:false ~source ~trusted:true ~modehash as super
4243 val mutable m_prevmemused = 0
4244 method infochanged = function
4245 | Memused ->
4246 if m_prevmemused != state.memused
4247 then (
4248 m_prevmemused <- state.memused;
4249 G.postRedisplay "memusedchanged";
4251 | Pdim -> G.postRedisplay "pdimchanged"
4252 | Docinfo -> fillsrc prevmode prevuioh
4254 method key key mask =
4255 if not (Wsi.withctrl mask)
4256 then
4257 match key with
4258 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4259 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4260 | _ -> super#key key mask
4261 else super#key key mask
4262 end);
4263 G.postRedisplay "info";
4266 let enterhelpmode =
4267 let source =
4268 (object
4269 inherit lvsourcebase
4270 method getitemcount = Array.length state.help
4271 method getitem n =
4272 let s, l, _ = state.help.(n) in
4273 (s, l)
4275 method exit ~uioh ~cancel ~active ~first ~pan =
4276 let optuioh =
4277 if not cancel
4278 then (
4279 match state.help.(active) with
4280 | _, _, Action f -> Some (f uioh)
4281 | _ -> Some (uioh)
4283 else None
4285 m_active <- active;
4286 m_first <- first;
4287 m_pan <- pan;
4288 optuioh
4290 method hasaction n =
4291 match state.help.(n) with
4292 | _, _, Action _ -> true
4293 | _ -> false
4295 initializer
4296 m_active <- -1
4297 end)
4298 in fun () ->
4299 let modehash = findkeyhash conf "help" in
4300 resetmstate ();
4301 state.uioh <- coe (new listview
4302 ~helpmode:true ~source ~trusted:true ~modehash);
4303 G.postRedisplay "help";
4306 let entermsgsmode =
4307 let msgsource =
4308 let re = Str.regexp "[\r\n]" in
4309 (object
4310 inherit lvsourcebase
4311 val mutable m_items = [||]
4313 method getitemcount = 1 + Array.length m_items
4315 method getitem n =
4316 if n = 0
4317 then "[Clear]", 0
4318 else m_items.(n-1), 0
4320 method exit ~uioh ~cancel ~active ~first ~pan =
4321 ignore uioh;
4322 if not cancel
4323 then (
4324 if active = 0
4325 then Buffer.clear state.errmsgs;
4327 m_active <- active;
4328 m_first <- first;
4329 m_pan <- pan;
4330 None
4332 method hasaction n =
4333 n = 0
4335 method reset =
4336 state.newerrmsgs <- false;
4337 let l = Str.split re (Buffer.contents state.errmsgs) in
4338 m_items <- Array.of_list l
4340 initializer
4341 m_active <- 0
4342 end)
4343 in fun () ->
4344 state.text <- "";
4345 resetmstate ();
4346 msgsource#reset;
4347 let source = (msgsource :> lvsource) in
4348 let modehash = findkeyhash conf "listview" in
4349 state.uioh <- coe (object
4350 inherit listview ~helpmode:false ~source ~trusted:false ~modehash as super
4351 method display =
4352 if state.newerrmsgs
4353 then msgsource#reset;
4354 super#display
4355 end);
4356 G.postRedisplay "msgs";
4359 let quickbookmark ?title () =
4360 match state.layout with
4361 | [] -> ()
4362 | l :: _ ->
4363 let title =
4364 match title with
4365 | None ->
4366 let tm = Unix.localtime (now ()) in
4367 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4368 (l.pageno+1)
4369 tm.Unix.tm_mday
4370 tm.Unix.tm_mon
4371 (tm.Unix.tm_year + 1900)
4372 tm.Unix.tm_hour
4373 tm.Unix.tm_min
4374 | Some title -> title
4376 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4379 let setautoscrollspeed step goingdown =
4380 let incr = max 1 ((abs step) / 2) in
4381 let incr = if goingdown then incr else -incr in
4382 let astep = boundastep state.winh (step + incr) in
4383 state.autoscroll <- Some astep;
4386 let canpan () =
4387 match conf.columns with
4388 | Csplit _ -> true
4389 | _ -> state.x != 0 || conf.zoom > 1.0
4392 let panbound x = bound x (-state.w) (wadjsb state.winw);;
4394 let existsinrow pageno (columns, coverA, coverB) p =
4395 let last = ((pageno - coverA) mod columns) + columns in
4396 let rec any = function
4397 | [] -> false
4398 | l :: rest ->
4399 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4400 then p l
4401 else (
4402 if not (p l)
4403 then (if l.pageno = last then false else any rest)
4404 else true
4407 any state.layout
4410 let nextpage () =
4411 match state.layout with
4412 | [] ->
4413 let pageno = page_of_y state.y in
4414 gotoghyll (getpagey (pageno+1))
4415 | l :: rest ->
4416 match conf.columns with
4417 | Csingle _ ->
4418 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4419 then
4420 let y = clamp (pgscale state.winh) in
4421 gotoghyll y
4422 else
4423 let pageno = min (l.pageno+1) (state.pagecount-1) in
4424 gotoghyll (getpagey pageno)
4425 | Cmulti ((c, _, _) as cl, _) ->
4426 if conf.presentation
4427 && (existsinrow l.pageno cl
4428 (fun l -> l.pageh > l.pagey + l.pagevh))
4429 then
4430 let y = clamp (pgscale state.winh) in
4431 gotoghyll y
4432 else
4433 let pageno = min (l.pageno+c) (state.pagecount-1) in
4434 gotoghyll (getpagey pageno)
4435 | Csplit (n, _) ->
4436 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4437 then
4438 let pagey, pageh = getpageyh l.pageno in
4439 let pagey = pagey + pageh * l.pagecol in
4440 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4441 gotoghyll (pagey + pageh + ips)
4444 let prevpage () =
4445 match state.layout with
4446 | [] ->
4447 let pageno = page_of_y state.y in
4448 gotoghyll (getpagey (pageno-1))
4449 | l :: _ ->
4450 match conf.columns with
4451 | Csingle _ ->
4452 if conf.presentation && l.pagey != 0
4453 then
4454 gotoghyll (clamp (pgscale ~-(state.winh)))
4455 else
4456 let pageno = max 0 (l.pageno-1) in
4457 gotoghyll (getpagey pageno)
4458 | Cmulti ((c, _, coverB) as cl, _) ->
4459 if conf.presentation &&
4460 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4461 then
4462 gotoghyll (clamp (pgscale ~-(state.winh)))
4463 else
4464 let decr =
4465 if l.pageno = state.pagecount - coverB
4466 then 1
4467 else c
4469 let pageno = max 0 (l.pageno-decr) in
4470 gotoghyll (getpagey pageno)
4471 | Csplit (n, _) ->
4472 let y =
4473 if l.pagecol = 0
4474 then
4475 if l.pageno = 0
4476 then l.pagey
4477 else
4478 let pageno = max 0 (l.pageno-1) in
4479 let pagey, pageh = getpageyh pageno in
4480 pagey + (n-1)*pageh
4481 else
4482 let pagey, pageh = getpageyh l.pageno in
4483 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4485 gotoghyll y
4488 let viewkeyboard key mask =
4489 let enttext te =
4490 let mode = state.mode in
4491 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4492 state.text <- "";
4493 enttext ();
4494 G.postRedisplay "view:enttext"
4496 let ctrl = Wsi.withctrl mask in
4497 let key =
4498 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4500 match key with
4501 | 81 -> (* Q *)
4502 exit 0
4504 | 0xff63 -> (* insert *)
4505 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4506 then (
4507 state.mode <- LinkNav (Ltgendir 0);
4508 gotoy state.y;
4510 else showtext '!' "Keyboard link navigation does not work under rotation"
4512 | 0xff1b | 113 -> (* escape / q *)
4513 begin match state.mstate with
4514 | Mzoomrect _ ->
4515 resetmstate ();
4516 G.postRedisplay "kill zoom rect";
4517 | _ ->
4518 begin match state.mode with
4519 | LinkNav _ ->
4520 state.mode <- View;
4521 G.postRedisplay "esc leave linknav"
4522 | _ ->
4523 match state.ranchors with
4524 | [] -> raise Quit
4525 | (path, password, anchor, origin) :: rest ->
4526 state.ranchors <- rest;
4527 state.anchor <- anchor;
4528 state.origin <- origin;
4529 state.nameddest <- "";
4530 opendoc path password
4531 end;
4532 end;
4534 | 0xff08 -> (* backspace *)
4535 gotoghyll (getnav ~-1)
4537 | 111 -> (* o *)
4538 enteroutlinemode ()
4540 | 117 -> (* u *)
4541 state.rects <- [];
4542 state.text <- "";
4543 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
4544 G.postRedisplay "dehighlight";
4546 | 47 | 63 -> (* / ? *)
4547 let ondone isforw s =
4548 cbput state.hists.pat s;
4549 state.searchpattern <- s;
4550 search s isforw
4552 let s = String.create 1 in
4553 s.[0] <- Char.chr key;
4554 enttext (s, "", Some (onhist state.hists.pat),
4555 textentry, ondone (key = 47), true)
4557 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4558 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4559 setzoom (conf.zoom +. incr)
4561 | 43 | 0xffab -> (* + *)
4562 let ondone s =
4563 let n =
4564 try int_of_string s with exc ->
4565 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4566 max_int
4568 if n != max_int
4569 then (
4570 conf.pagebias <- n;
4571 state.text <- "page bias is now " ^ string_of_int n;
4574 enttext ("page bias: ", "", None, intentry, ondone, true)
4576 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4577 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4578 setzoom (max 0.01 (conf.zoom -. decr))
4580 | 45 | 0xffad -> (* - *)
4581 let ondone msg = state.text <- msg in
4582 enttext (
4583 "option [acfhilpstvxACFPRSZTISM]: ", "", None,
4584 optentry state.mode, ondone, true
4587 | 48 when ctrl -> (* ctrl-0 *)
4588 if conf.zoom = 1.0
4589 then (
4590 state.x <- 0;
4591 gotoy state.y
4593 else setzoom 1.0
4595 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
4596 let cols =
4597 match conf.columns with
4598 | Csingle _ | Cmulti _ -> 1
4599 | Csplit (n, _) -> n
4601 let h = state.winh -
4602 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4604 let zoom = zoomforh state.winw h (vscrollw ()) cols in
4605 if zoom > 0.0 && (key = 50 || zoom < 1.0)
4606 then setzoom zoom
4608 | 51 when ctrl -> (* ctrl-3 *)
4609 let fm =
4610 match conf.fitmodel with
4611 | FitWidth -> FitProportional
4612 | FitProportional -> FitPage
4613 | FitPage -> FitWidth
4615 state.text <- "fit model: " ^ FMTE.to_string fm;
4616 reqlayout conf.angle fm
4618 | 0xffc6 -> (* f9 *)
4619 togglebirdseye ()
4621 | 57 when ctrl -> (* ctrl-9 *)
4622 togglebirdseye ()
4624 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4625 when not ctrl -> (* 0..9 *)
4626 let ondone s =
4627 let n =
4628 try int_of_string s with exc ->
4629 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4632 if n >= 0
4633 then (
4634 addnav ();
4635 cbput state.hists.pag (string_of_int n);
4636 gotopage1 (n + conf.pagebias - 1) 0;
4639 let pageentry text key =
4640 match Char.unsafe_chr key with
4641 | 'g' -> TEdone text
4642 | _ -> intentry text key
4644 let text = "x" in text.[0] <- Char.chr key;
4645 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4647 | 98 -> (* b *)
4648 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4649 reshape state.winw state.winh;
4651 | 66 -> (* B *)
4652 state.bzoom <- not state.bzoom;
4653 state.rects <- [];
4654 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4656 | 108 -> (* l *)
4657 conf.hlinks <- not conf.hlinks;
4658 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4659 G.postRedisplay "toggle highlightlinks";
4661 | 70 -> (* F *)
4662 state.glinks <- true;
4663 let mode = state.mode in
4664 state.mode <- Textentry (
4665 (":", "", None, linknentry, linkndone gotounder, false),
4666 (fun _ ->
4667 state.glinks <- false;
4668 state.mode <- mode)
4670 state.text <- "";
4671 G.postRedisplay "view:linkent(F)"
4673 | 121 -> (* y *)
4674 state.glinks <- true;
4675 let mode = state.mode in
4676 state.mode <- Textentry (
4678 ":", "", None, linknentry, linkndone (fun under ->
4679 selstring (undertext under);
4680 ), false
4682 fun _ ->
4683 state.glinks <- false;
4684 state.mode <- mode
4686 state.text <- "";
4687 G.postRedisplay "view:linkent"
4689 | 97 -> (* a *)
4690 begin match state.autoscroll with
4691 | Some step ->
4692 conf.autoscrollstep <- step;
4693 state.autoscroll <- None
4694 | None ->
4695 if conf.autoscrollstep = 0
4696 then state.autoscroll <- Some 1
4697 else state.autoscroll <- Some conf.autoscrollstep
4700 | 112 when ctrl -> (* ctrl-p *)
4701 launchpath ()
4703 | 80 -> (* P *)
4704 setpresentationmode (not conf.presentation);
4705 showtext ' ' ("presentation mode " ^
4706 if conf.presentation then "on" else "off");
4708 | 102 -> (* f *)
4709 if List.mem Wsi.Fullscreen state.winstate
4710 then Wsi.reshape conf.cwinw conf.cwinh
4711 else Wsi.fullscreen ()
4713 | 112 | 78 -> (* p|N *)
4714 search state.searchpattern false
4716 | 110 | 0xffc0 -> (* n|F3 *)
4717 search state.searchpattern true
4719 | 116 -> (* t *)
4720 begin match state.layout with
4721 | [] -> ()
4722 | l :: _ ->
4723 gotoghyll (getpagey l.pageno)
4726 | 32 -> (* space *)
4727 nextpage ()
4729 | 0xff9f | 0xffff -> (* delete *)
4730 prevpage ()
4732 | 61 -> (* = *)
4733 showtext ' ' (describe_location ());
4735 | 119 -> (* w *)
4736 begin match state.layout with
4737 | [] -> ()
4738 | l :: _ ->
4739 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
4740 G.postRedisplay "w"
4743 | 39 -> (* ' *)
4744 enterbookmarkmode ()
4746 | 104 | 0xffbe -> (* h|F1 *)
4747 enterhelpmode ()
4749 | 105 -> (* i *)
4750 enterinfomode ()
4752 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
4753 entermsgsmode ()
4755 | 109 -> (* m *)
4756 let ondone s =
4757 match state.layout with
4758 | l :: _ ->
4759 if nonemptystr s
4760 then
4761 state.bookmarks <-
4762 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4763 | _ -> ()
4765 enttext ("bookmark: ", "", None, textentry, ondone, true)
4767 | 126 -> (* ~ *)
4768 quickbookmark ();
4769 showtext ' ' "Quick bookmark added";
4771 | 122 -> (* z *)
4772 begin match state.layout with
4773 | l :: _ ->
4774 let rect = getpdimrect l.pagedimno in
4775 let w, h =
4776 if conf.crophack
4777 then
4778 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4779 truncate (1.2 *. (rect.(3) -. rect.(0))))
4780 else
4781 (truncate (rect.(1) -. rect.(0)),
4782 truncate (rect.(3) -. rect.(0)))
4784 let w = truncate ((float w)*.conf.zoom)
4785 and h = truncate ((float h)*.conf.zoom) in
4786 if w != 0 && h != 0
4787 then (
4788 state.anchor <- getanchor ();
4789 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
4791 G.postRedisplay "z";
4793 | [] -> ()
4796 | 120 -> (* x *)
4797 state.roam ()
4798 | 60 | 62 -> (* < > *)
4799 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
4801 | 91 | 93 -> (* [ ] *)
4802 conf.colorscale <-
4803 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
4805 G.postRedisplay "brightness";
4807 | 99 when state.mode = View -> (* [alt-]c *)
4808 if Wsi.withalt mask
4809 then (
4810 if conf.zoom > 1.0
4811 then
4812 let m = (wadjsb state.winw - state.w) / 2 in
4813 state.x <- m;
4814 gotoy_and_clear_text state.y
4816 else
4817 let (c, a, b), z =
4818 match state.prevcolumns with
4819 | None -> (1, 0, 0), 1.0
4820 | Some (columns, z) ->
4821 let cab =
4822 match columns with
4823 | Csplit (c, _) -> -c, 0, 0
4824 | Cmulti ((c, a, b), _) -> c, a, b
4825 | Csingle _ -> 1, 0, 0
4827 cab, z
4829 setcolumns View c a b;
4830 setzoom z
4832 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask
4833 -> (* ctrl-shift- (kp) [up|down] *)
4834 let zoom, x = state.prevzoom in
4835 setzoom zoom;
4836 state.x <- x;
4838 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
4839 begin match state.autoscroll with
4840 | None ->
4841 begin match state.mode with
4842 | Birdseye beye -> upbirdseye 1 beye
4843 | _ ->
4844 if ctrl
4845 then gotoy_and_clear_text (clamp ~-(state.winh/2))
4846 else (
4847 if not (Wsi.withshift mask) && conf.presentation
4848 then prevpage ()
4849 else gotoghyll1 true (clamp (-conf.scrollstep))
4852 | Some n ->
4853 setautoscrollspeed n false
4856 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
4857 begin match state.autoscroll with
4858 | None ->
4859 begin match state.mode with
4860 | Birdseye beye -> downbirdseye 1 beye
4861 | _ ->
4862 if ctrl
4863 then gotoy_and_clear_text (clamp (state.winh/2))
4864 else (
4865 if not (Wsi.withshift mask) && conf.presentation
4866 then nextpage ()
4867 else gotoghyll1 true (clamp (conf.scrollstep))
4870 | Some n ->
4871 setautoscrollspeed n true
4874 | 0xff51 | 0xff53 | 0xff96 | 0xff98
4875 when not (Wsi.withalt mask) -> (* (kp) left / right *)
4876 if canpan ()
4877 then
4878 let dx =
4879 if ctrl
4880 then state.winw / 2
4881 else conf.hscrollstep
4883 let dx = if key = 0xff51 || key = 0xff96 then dx else -dx in
4884 state.x <- panbound (state.x + dx);
4885 gotoy_and_clear_text state.y
4886 else (
4887 state.text <- "";
4888 G.postRedisplay "left/right"
4891 | 0xff55 | 0xff9a -> (* (kp) prior *)
4892 let y =
4893 if ctrl
4894 then
4895 match state.layout with
4896 | [] -> state.y
4897 | l :: _ -> state.y - l.pagey
4898 else
4899 clamp (pgscale (-state.winh))
4901 gotoghyll y
4903 | 0xff56 | 0xff9b -> (* (kp) next *)
4904 let y =
4905 if ctrl
4906 then
4907 match List.rev state.layout with
4908 | [] -> state.y
4909 | l :: _ -> getpagey l.pageno
4910 else
4911 clamp (pgscale state.winh)
4913 gotoghyll y
4915 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
4916 gotoghyll 0
4917 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
4918 gotoghyll (clamp state.maxy)
4920 | 0xff53 | 0xff98
4921 when Wsi.withalt mask -> (* alt-(kp) right *)
4922 gotoghyll (getnav 1)
4923 | 0xff51 | 0xff96
4924 when Wsi.withalt mask -> (* alt-(kp) left *)
4925 gotoghyll (getnav ~-1)
4927 | 114 -> (* r *)
4928 reload ()
4930 | 118 when conf.debug -> (* v *)
4931 state.rects <- [];
4932 List.iter (fun l ->
4933 match getopaque l.pageno with
4934 | None -> ()
4935 | Some opaque ->
4936 let x0, y0, x1, y1 = pagebbox opaque in
4937 let a,b = float x0, float y0 in
4938 let c,d = float x1, float y0 in
4939 let e,f = float x1, float y1 in
4940 let h,j = float x0, float y1 in
4941 let rect = (a,b,c,d,e,f,h,j) in
4942 debugrect rect;
4943 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4944 ) state.layout;
4945 G.postRedisplay "v";
4947 | 124 -> (* | *)
4948 let mode = state.mode in
4949 let cmd = ref "" in
4950 let onleave = function
4951 | Cancel -> state.mode <- mode
4952 | Confirm ->
4953 List.iter (fun l ->
4954 match getopaque l.pageno with
4955 | Some opaque -> pipesel opaque !cmd
4956 | None -> ()) state.layout;
4957 state.mode <- mode
4959 let ondone s =
4960 cbput state.hists.sel s;
4961 cmd := s
4963 let te =
4964 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
4966 G.postRedisplay "|";
4967 state.mode <- Textentry (te, onleave);
4969 | _ ->
4970 vlog "huh? %s" (Wsi.keyname key)
4973 let linknavkeyboard key mask linknav =
4974 let getpage pageno =
4975 let rec loop = function
4976 | [] -> None
4977 | l :: _ when l.pageno = pageno -> Some l
4978 | _ :: rest -> loop rest
4979 in loop state.layout
4981 let doexact (pageno, n) =
4982 match getopaque pageno, getpage pageno with
4983 | Some opaque, Some l ->
4984 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
4985 then
4986 let under = getlink opaque n in
4987 G.postRedisplay "link gotounder";
4988 gotounder under;
4989 state.mode <- View;
4990 else
4991 let opt, dir =
4992 match key with
4993 | 0xff50 -> (* home *)
4994 Some (findlink opaque LDfirst), -1
4996 | 0xff57 -> (* end *)
4997 Some (findlink opaque LDlast), 1
4999 | 0xff51 -> (* left *)
5000 Some (findlink opaque (LDleft n)), -1
5002 | 0xff53 -> (* right *)
5003 Some (findlink opaque (LDright n)), 1
5005 | 0xff52 -> (* up *)
5006 Some (findlink opaque (LDup n)), -1
5008 | 0xff54 -> (* down *)
5009 Some (findlink opaque (LDdown n)), 1
5011 | _ -> None, 0
5013 let pwl l dir =
5014 begin match findpwl l.pageno dir with
5015 | Pwlnotfound -> ()
5016 | Pwl pageno ->
5017 let notfound dir =
5018 state.mode <- LinkNav (Ltgendir dir);
5019 let y, h = getpageyh pageno in
5020 let y =
5021 if dir < 0
5022 then y + h - state.winh
5023 else y
5025 gotoy y
5027 begin match getopaque pageno, getpage pageno with
5028 | Some opaque, Some _ ->
5029 let link =
5030 let ld = if dir > 0 then LDfirst else LDlast in
5031 findlink opaque ld
5033 begin match link with
5034 | Lfound m ->
5035 showlinktype (getlink opaque m);
5036 state.mode <- LinkNav (Ltexact (pageno, m));
5037 G.postRedisplay "linknav jpage";
5038 | _ -> notfound dir
5039 end;
5040 | _ -> notfound dir
5041 end;
5042 end;
5044 begin match opt with
5045 | Some Lnotfound -> pwl l dir;
5046 | Some (Lfound m) ->
5047 if m = n
5048 then pwl l dir
5049 else (
5050 let _, y0, _, y1 = getlinkrect opaque m in
5051 if y0 < l.pagey
5052 then gotopage1 l.pageno y0
5053 else (
5054 let d = fstate.fontsize + 1 in
5055 if y1 - l.pagey > l.pagevh - d
5056 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5057 else G.postRedisplay "linknav";
5059 showlinktype (getlink opaque m);
5060 state.mode <- LinkNav (Ltexact (l.pageno, m));
5063 | None -> viewkeyboard key mask
5064 end;
5065 | _ -> viewkeyboard key mask
5067 if key = 0xff63
5068 then (
5069 state.mode <- View;
5070 G.postRedisplay "leave linknav"
5072 else
5073 match linknav with
5074 | Ltgendir _ -> viewkeyboard key mask
5075 | Ltexact exact -> doexact exact
5078 let keyboard key mask =
5079 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5080 then wcmd "interrupt"
5081 else state.uioh <- state.uioh#key key mask
5084 let birdseyekeyboard key mask
5085 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5086 let incr =
5087 match conf.columns with
5088 | Csingle _ -> 1
5089 | Cmulti ((c, _, _), _) -> c
5090 | Csplit _ -> failwith "bird's eye split mode"
5092 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5093 match key with
5094 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5095 let y, h = getpageyh pageno in
5096 let top = (state.winh - h) / 2 in
5097 gotoy (max 0 (y - top))
5098 | 0xff0d (* enter *)
5099 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5100 | 0xff1b -> leavebirdseye beye true (* escape *)
5101 | 0xff52 -> upbirdseye incr beye (* up *)
5102 | 0xff54 -> downbirdseye incr beye (* down *)
5103 | 0xff51 -> upbirdseye 1 beye (* left *)
5104 | 0xff53 -> downbirdseye 1 beye (* right *)
5106 | 0xff55 -> (* prior *)
5107 begin match state.layout with
5108 | l :: _ ->
5109 if l.pagey != 0
5110 then (
5111 state.mode <- Birdseye (
5112 oconf, leftx, l.pageno, hooverpageno, anchor
5114 gotopage1 l.pageno 0;
5116 else (
5117 let layout = layout (state.y-state.winh) (pgh state.layout) in
5118 match layout with
5119 | [] -> gotoy (clamp (-state.winh))
5120 | l :: _ ->
5121 state.mode <- Birdseye (
5122 oconf, leftx, l.pageno, hooverpageno, anchor
5124 gotopage1 l.pageno 0
5127 | [] -> gotoy (clamp (-state.winh))
5128 end;
5130 | 0xff56 -> (* next *)
5131 begin match List.rev state.layout with
5132 | l :: _ ->
5133 let layout = layout (state.y + (pgh state.layout)) state.winh in
5134 begin match layout with
5135 | [] ->
5136 let incr = l.pageh - l.pagevh in
5137 if incr = 0
5138 then (
5139 state.mode <-
5140 Birdseye (
5141 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5143 G.postRedisplay "birdseye pagedown";
5145 else gotoy (clamp (incr + conf.interpagespace*2));
5147 | l :: _ ->
5148 state.mode <-
5149 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5150 gotopage1 l.pageno 0;
5153 | [] -> gotoy (clamp state.winh)
5154 end;
5156 | 0xff50 -> (* home *)
5157 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5158 gotopage1 0 0
5160 | 0xff57 -> (* end *)
5161 let pageno = state.pagecount - 1 in
5162 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5163 if not (pagevisible state.layout pageno)
5164 then
5165 let h =
5166 match List.rev state.pdims with
5167 | [] -> state.winh
5168 | (_, _, h, _) :: _ -> h
5170 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5171 else G.postRedisplay "birdseye end";
5172 | _ -> viewkeyboard key mask
5175 let drawpage l =
5176 let color =
5177 match state.mode with
5178 | Textentry _ -> scalecolor 0.4
5179 | LinkNav _
5180 | View -> scalecolor 1.0
5181 | Birdseye (_, _, pageno, hooverpageno, _) ->
5182 if l.pageno = hooverpageno
5183 then scalecolor 0.9
5184 else (
5185 if l.pageno = pageno
5186 then scalecolor 1.0
5187 else scalecolor 0.8
5190 drawtiles l color;
5193 let postdrawpage l linkindexbase =
5194 match getopaque l.pageno with
5195 | Some opaque ->
5196 if tileready l l.pagex l.pagey
5197 then
5198 let x = l.pagedispx - l.pagex
5199 and y = l.pagedispy - l.pagey in
5200 let hlmask =
5201 match conf.columns with
5202 | Csingle _ | Cmulti _ ->
5203 (if conf.hlinks then 1 else 0)
5204 + (if state.glinks
5205 && not (isbirdseye state.mode) then 2 else 0)
5206 | _ -> 0
5208 let s =
5209 match state.mode with
5210 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5211 | _ -> ""
5213 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5214 else 0
5215 | _ -> 0
5218 let scrollindicator () =
5219 let sbw, ph, sh = state.uioh#scrollph in
5220 let sbh, pw, sw = state.uioh#scrollpw in
5222 let x0,x1 =
5223 if conf.leftscroll
5224 then (0, sbw)
5225 else (state.winw - sbw), state.winw
5228 GlDraw.color (0.64, 0.64, 0.64);
5229 filledrect (float x0) 0. (float x1) (float state.winh);
5230 filledrect
5231 0. (float (state.winh - sbh))
5232 (float (wadjsb state.winw - 1)) (float state.winh)
5234 GlDraw.color (0.0, 0.0, 0.0);
5236 filledrect (float x0) ph (float x1) (ph +. sh);
5237 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5240 let showsel () =
5241 match state.mstate with
5242 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5245 | Msel ((x0, y0), (x1, y1)) ->
5246 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5247 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< "", -1, 0, 0) in
5248 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< "", -1, 0, 0) in
5249 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5252 let showrects = function [] -> () | rects ->
5253 Gl.enable `blend;
5254 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5255 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5256 List.iter
5257 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5258 List.iter (fun l ->
5259 if l.pageno = pageno
5260 then (
5261 let dx = float (l.pagedispx - l.pagex) in
5262 let dy = float (l.pagedispy - l.pagey) in
5263 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5264 Raw.sets_float state.vraw ~pos:0
5265 [| x0+.dx; y0+.dy;
5266 x1+.dx; y1+.dy;
5267 x3+.dx; y3+.dy;
5268 x2+.dx; y2+.dy |];
5269 GlArray.vertex `two state.vraw;
5270 GlArray.draw_arrays `triangle_strip 0 4;
5272 ) state.layout
5273 ) rects
5275 Gl.disable `blend;
5278 let display () =
5279 GlClear.color (scalecolor2 conf.bgcolor);
5280 GlClear.clear [`color];
5281 List.iter drawpage state.layout;
5282 let rects =
5283 match state.mode with
5284 | LinkNav (Ltexact (pageno, linkno)) ->
5285 begin match getopaque pageno with
5286 | Some opaque ->
5287 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5288 (pageno, 5, (
5289 float x0, float y0,
5290 float x1, float y0,
5291 float x1, float y1,
5292 float x0, float y1)
5293 ) :: state.rects
5294 | None -> state.rects
5296 | _ -> state.rects
5298 showrects rects;
5299 let rec postloop linkindexbase = function
5300 | l :: rest ->
5301 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5302 postloop linkindexbase rest
5303 | [] -> ()
5305 showsel ();
5306 postloop 0 state.layout;
5307 state.uioh#display;
5308 begin match state.mstate with
5309 | Mzoomrect ((x0, y0), (x1, y1)) ->
5310 Gl.enable `blend;
5311 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5312 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5313 filledrect (float x0) (float y0) (float x1) (float y1);
5314 Gl.disable `blend;
5315 | _ -> ()
5316 end;
5317 enttext ();
5318 scrollindicator ();
5319 Wsi.swapb ();
5322 let zoomrect x y x1 y1 =
5323 let x0 = min x x1
5324 and x1 = max x x1
5325 and y0 = min y y1 in
5326 gotoy (state.y + y0);
5327 state.anchor <- getanchor ();
5328 let zoom = (float state.w) /. float (x1 - x0) in
5329 let margin =
5330 match conf.fitmodel, conf.columns with
5331 | FitPage, Csplit _ ->
5332 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5334 | _, _ ->
5335 let adjw = wadjsb state.winw in
5336 if state.w < adjw
5337 then (adjw - state.w) / 2
5338 else 0
5340 state.x <- (state.x + margin) - x0;
5341 setzoom zoom;
5342 resetmstate ();
5345 let zoomblock x y =
5346 let g opaque l px py =
5347 match rectofblock opaque px py with
5348 | Some a ->
5349 let x0 = a.(0) -. 20. in
5350 let x1 = a.(1) +. 20. in
5351 let y0 = a.(2) -. 20. in
5352 let zoom = (float state.w) /. (x1 -. x0) in
5353 let pagey = getpagey l.pageno in
5354 gotoy_and_clear_text (pagey + truncate y0);
5355 state.anchor <- getanchor ();
5356 let margin = (state.w - l.pagew)/2 in
5357 state.x <- -truncate x0 - margin;
5358 setzoom zoom;
5359 None
5360 | None -> None
5362 match conf.columns with
5363 | Csplit _ ->
5364 showtext '!' "block zooming does not work properly in split columns mode"
5365 | _ -> onppundermouse g x y ()
5368 let scrollx x =
5369 let winw = wadjsb state.winw - 1 in
5370 let s = float x /. float winw in
5371 let destx = truncate (float (state.w + winw) *. s) in
5372 state.x <- winw - destx;
5373 gotoy_and_clear_text state.y;
5374 state.mstate <- Mscrollx;
5377 let scrolly y =
5378 let s = float y /. float state.winh in
5379 let desty = truncate (float (state.maxy - state.winh) *. s) in
5380 gotoy_and_clear_text desty;
5381 state.mstate <- Mscrolly;
5384 let viewmulticlick clicks x y mask =
5385 let g opaque l px py =
5386 let mark =
5387 match clicks with
5388 | 2 -> Mark_word
5389 | 3 -> Mark_line
5390 | 4 -> Mark_block
5391 | _ -> Mark_page
5393 if markunder opaque px py mark
5394 then (
5395 Some (fun () ->
5396 let dopipe cmd =
5397 match getopaque l.pageno with
5398 | None -> ()
5399 | Some opaque -> pipesel opaque cmd
5401 state.roam <- (fun () -> dopipe conf.paxcmd);
5402 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5405 else None
5407 G.postRedisplay "viewmulticlick";
5408 onppundermouse g x y (fun () -> showtext '!' "Nothing to select") ();
5411 let canselect () =
5412 match conf.columns with
5413 | Csplit _ -> false
5414 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5417 let viewmouse button down x y mask =
5418 match button with
5419 | n when (n == 4 || n == 5) && not down ->
5420 if Wsi.withctrl mask
5421 then (
5422 match state.mstate with
5423 | Mzoom (oldn, i) ->
5424 if oldn = n
5425 then (
5426 if i = 2
5427 then
5428 let incr =
5429 match n with
5430 | 5 ->
5431 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5432 | _ ->
5433 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5435 let zoom = conf.zoom -. incr in
5436 setzoom zoom;
5437 state.mstate <- Mzoom (n, 0);
5438 else
5439 state.mstate <- Mzoom (n, i+1);
5441 else state.mstate <- Mzoom (n, 0)
5443 | _ -> state.mstate <- Mzoom (n, 0)
5445 else (
5446 match state.autoscroll with
5447 | Some step -> setautoscrollspeed step (n=4)
5448 | None ->
5449 if conf.wheelbypage || conf.presentation
5450 then (
5451 if n = 4
5452 then prevpage ()
5453 else nextpage ()
5455 else
5456 let incr =
5457 if n = 4
5458 then -conf.scrollstep
5459 else conf.scrollstep
5461 let incr = incr * 2 in
5462 let y = clamp incr in
5463 gotoy_and_clear_text y
5466 | n when (n = 6 || n = 7) && not down && canpan () ->
5467 state.x <-
5468 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5469 gotoy_and_clear_text state.y
5471 | 1 when Wsi.withshift mask ->
5472 state.mstate <- Mnone;
5473 if not down
5474 then (
5475 match unproject x y with
5476 | Some (pageno, ux, uy) ->
5477 let cmd = Printf.sprintf
5478 "%s %s %d %d %d"
5479 conf.stcmd state.path pageno ux uy
5481 popen cmd []
5482 | None -> ()
5485 | 1 when Wsi.withctrl mask ->
5486 if down
5487 then (
5488 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5489 state.mstate <- Mpan (x, y)
5491 else
5492 state.mstate <- Mnone
5494 | 3 ->
5495 if down
5496 then (
5497 Wsi.setcursor Wsi.CURSOR_CYCLE;
5498 let p = (x, y) in
5499 state.mstate <- Mzoomrect (p, p)
5501 else (
5502 match state.mstate with
5503 | Mzoomrect ((x0, y0), _) ->
5504 if abs (x-x0) > 10 && abs (y - y0) > 10
5505 then zoomrect x0 y0 x y
5506 else (
5507 resetmstate ();
5508 G.postRedisplay "kill accidental zoom rect";
5510 | _ ->
5511 resetmstate ()
5514 | 1 when x > state.winw - vscrollw () ->
5515 if down
5516 then
5517 let _, position, sh = state.uioh#scrollph in
5518 if y > truncate position && y < truncate (position +. sh)
5519 then state.mstate <- Mscrolly
5520 else scrolly y
5521 else
5522 state.mstate <- Mnone
5524 | 1 when y > state.winh - hscrollh () ->
5525 if down
5526 then
5527 let _, position, sw = state.uioh#scrollpw in
5528 if x > truncate position && x < truncate (position +. sw)
5529 then state.mstate <- Mscrollx
5530 else scrollx x
5531 else
5532 state.mstate <- Mnone
5534 | 1 when state.bzoom -> if not down then zoomblock x y
5536 | 1 ->
5537 let dest = if down then getunder x y else Unone in
5538 begin match dest with
5539 | Ulinkgoto _
5540 | Ulinkuri _
5541 | Uremote _ | Uremotedest _
5542 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5543 gotounder dest
5545 | Unone when down ->
5546 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5547 state.mstate <- Mpan (x, y);
5549 | Unone | Utext _ ->
5550 if down
5551 then (
5552 if canselect ()
5553 then (
5554 state.mstate <- Msel ((x, y), (x, y));
5555 G.postRedisplay "mouse select";
5558 else (
5559 match state.mstate with
5560 | Mnone -> ()
5562 | Mzoom _ | Mscrollx | Mscrolly ->
5563 state.mstate <- Mnone
5565 | Mzoomrect ((x0, y0), _) ->
5566 zoomrect x0 y0 x y
5568 | Mpan _ ->
5569 Wsi.setcursor Wsi.CURSOR_INHERIT;
5570 state.mstate <- Mnone
5572 | Msel ((x0, y0), (x1, y1)) ->
5573 let rec loop = function
5574 | [] -> ()
5575 | l :: rest ->
5576 let inside =
5577 let a0 = l.pagedispy in
5578 let a1 = a0 + l.pagevh in
5579 let b0 = l.pagedispx in
5580 let b1 = b0 + l.pagevw in
5581 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5582 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5584 if inside
5585 then
5586 match getopaque l.pageno with
5587 | Some opaque ->
5588 let dosel cmd () =
5589 match Ne.res Unix.pipe with
5590 | Ne.Exn exn ->
5591 showtext '!'
5592 (Printf.sprintf
5593 "can not create sel pipe: %s"
5594 (exntos exn));
5595 | Ne.Res (r, w) ->
5596 let clo what fd =
5597 Ne.clo fd (fun msg ->
5598 dolog "%s close failed: %s" what msg)
5600 let popened =
5601 try popen cmd [r, 0; w, -1]; true
5602 with exn ->
5603 dolog "can not execute %S: %s"
5604 cmd (exntos exn);
5605 false
5607 if popened
5608 then (
5609 copysel w opaque;
5610 G.postRedisplay "copysel";
5612 else clo "Msel pipe/w" w;
5613 clo "Msel pipe/r" r;
5615 dosel conf.selcmd ();
5616 state.roam <- dosel conf.paxcmd;
5617 | None -> ()
5618 else loop rest
5620 loop state.layout;
5621 resetmstate ();
5625 | _ -> ()
5628 let birdseyemouse button down x y mask
5629 (conf, leftx, _, hooverpageno, anchor) =
5630 match button with
5631 | 1 when down ->
5632 let rec loop = function
5633 | [] -> ()
5634 | l :: rest ->
5635 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5636 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5637 then (
5638 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5640 else loop rest
5642 loop state.layout
5643 | 3 -> ()
5644 | _ -> viewmouse button down x y mask
5647 let uioh = object
5648 method display = ()
5650 method key key mask =
5651 begin match state.mode with
5652 | Textentry textentry -> textentrykeyboard key mask textentry
5653 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5654 | View -> viewkeyboard key mask
5655 | LinkNav linknav -> linknavkeyboard key mask linknav
5656 end;
5657 state.uioh
5659 method button button bstate x y mask =
5660 begin match state.mode with
5661 | LinkNav _
5662 | View -> viewmouse button bstate x y mask
5663 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5664 | Textentry _ -> ()
5665 end;
5666 state.uioh
5668 method multiclick clicks x y mask =
5669 begin match state.mode with
5670 | LinkNav _
5671 | View -> viewmulticlick clicks x y mask
5672 | Birdseye _
5673 | Textentry _ -> ()
5674 end;
5675 state.uioh
5677 method motion x y =
5678 begin match state.mode with
5679 | Textentry _ -> ()
5680 | View | Birdseye _ | LinkNav _ ->
5681 match state.mstate with
5682 | Mzoom _ | Mnone -> ()
5684 | Mpan (x0, y0) ->
5685 let dx = x - x0
5686 and dy = y0 - y in
5687 state.mstate <- Mpan (x, y);
5688 if canpan ()
5689 then state.x <- panbound (state.x + dx);
5690 let y = clamp dy in
5691 gotoy_and_clear_text y
5693 | Msel (a, _) ->
5694 state.mstate <- Msel (a, (x, y));
5695 G.postRedisplay "motion select";
5697 | Mscrolly ->
5698 let y = min state.winh (max 0 y) in
5699 scrolly y
5701 | Mscrollx ->
5702 let x = min state.winw (max 0 x) in
5703 scrollx x
5705 | Mzoomrect (p0, _) ->
5706 state.mstate <- Mzoomrect (p0, (x, y));
5707 G.postRedisplay "motion zoomrect";
5708 end;
5709 state.uioh
5711 method pmotion x y =
5712 begin match state.mode with
5713 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5714 let rec loop = function
5715 | [] ->
5716 if hooverpageno != -1
5717 then (
5718 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5719 G.postRedisplay "pmotion birdseye no hoover";
5721 | l :: rest ->
5722 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5723 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5724 then (
5725 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5726 G.postRedisplay "pmotion birdseye hoover";
5728 else loop rest
5730 loop state.layout
5732 | Textentry _ -> ()
5734 | LinkNav _
5735 | View ->
5736 match state.mstate with
5737 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5739 | Mnone ->
5740 updateunder x y;
5741 if canselect ()
5742 then
5743 match conf.pax with
5744 | None -> ()
5745 | Some r ->
5746 let past, _, _ = !r in
5747 let now = now () in
5748 let delta = now -. past in
5749 if delta > 0.01
5750 then paxunder x y
5751 else r := (now, x, y)
5752 end;
5753 state.uioh
5755 method infochanged _ = ()
5757 method scrollph =
5758 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
5759 let p, h =
5760 if maxy = 0
5761 then 0.0, float state.winh
5762 else scrollph state.y maxy
5764 vscrollw (), p, h
5766 method scrollpw =
5767 let winw = wadjsb state.winw in
5768 let fwinw = float winw in
5769 let sw =
5770 let sw = fwinw /. float state.w in
5771 let sw = fwinw *. sw in
5772 max sw (float conf.scrollh)
5774 let position =
5775 let maxx = state.w + winw in
5776 let x = winw - state.x in
5777 let percent = float x /. float maxx in
5778 (fwinw -. sw) *. percent
5780 hscrollh (), position, sw
5782 method modehash =
5783 let modename =
5784 match state.mode with
5785 | LinkNav _ -> "links"
5786 | Textentry _ -> "textentry"
5787 | Birdseye _ -> "birdseye"
5788 | View -> "view"
5790 findkeyhash conf modename
5792 method eformsgs = true
5793 end;;
5795 let adderrmsg src msg =
5796 Buffer.add_string state.errmsgs msg;
5797 state.newerrmsgs <- true;
5798 G.postRedisplay src
5801 let adderrfmt src fmt =
5802 Format.kprintf (fun s -> adderrmsg src s) fmt;
5805 let ract cmds =
5806 let cl = splitatspace cmds in
5807 let scan s fmt f =
5808 try Scanf.sscanf s fmt f
5809 with exn ->
5810 adderrfmt "remote exec"
5811 "error processing '%S': %s\n" cmds (exntos exn)
5813 match cl with
5814 | "reload" :: [] -> reload ()
5815 | "goto" :: args :: [] ->
5816 scan args "%u %f %f"
5817 (fun pageno x y ->
5818 let cmd, _ = state.geomcmds in
5819 if emptystr cmd
5820 then gotopagexy pageno x y
5821 else
5822 let f prevf () =
5823 gotopagexy pageno x y;
5824 prevf ()
5826 state.reprf <- f state.reprf
5828 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
5829 | "gotor" :: args :: [] ->
5830 scan args "%S %u"
5831 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
5832 | "gotord" :: args :: [] ->
5833 scan args "%S %S"
5834 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
5835 | "rect" :: args :: [] ->
5836 scan args "%u %u %f %f %f %f"
5837 (fun pageno color x0 y0 x1 y1 ->
5838 onpagerect pageno (fun w h ->
5839 let _,w1,h1,_ = getpagedim pageno in
5840 let sw = float w1 /. float w
5841 and sh = float h1 /. float h in
5842 let x0s = x0 *. sw
5843 and x1s = x1 *. sw
5844 and y0s = y0 *. sh
5845 and y1s = y1 *. sh in
5846 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
5847 debugrect rect;
5848 state.rects <- (pageno, color, rect) :: state.rects;
5849 G.postRedisplay "rect";
5852 | "activatewin" :: [] -> Wsi.activatewin ()
5853 | "quit" :: [] -> raise Quit
5854 | _ ->
5855 adderrfmt "remote command"
5856 "error processing remote command: %S\n" cmds;
5859 let remote =
5860 let scratch = String.create 80 in
5861 let buf = Buffer.create 80 in
5862 fun fd ->
5863 let rec tempfr () =
5864 try Some (Unix.read fd scratch 0 80)
5865 with
5866 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
5867 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
5868 | exn -> raise exn
5870 match tempfr () with
5871 | None -> Some fd
5872 | Some n ->
5873 if n = 0
5874 then (
5875 Unix.close fd;
5876 if Buffer.length buf > 0
5877 then (
5878 let s = Buffer.contents buf in
5879 Buffer.clear buf;
5880 ract s;
5882 None
5884 else
5885 let rec eat ppos =
5886 let nlpos =
5888 let pos = String.index_from scratch ppos '\n' in
5889 if pos >= n then -1 else pos
5890 with Not_found -> -1
5892 if nlpos >= 0
5893 then (
5894 Buffer.add_substring buf scratch ppos (nlpos-ppos);
5895 let s = Buffer.contents buf in
5896 Buffer.clear buf;
5897 ract s;
5898 eat (nlpos+1);
5900 else (
5901 Buffer.add_substring buf scratch ppos (n-ppos);
5902 Some fd
5904 in eat 0
5907 let remoteopen path =
5908 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
5909 with exn ->
5910 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
5911 None
5914 let () =
5915 let trimcachepath = ref "" in
5916 let rcmdpath = ref "" in
5917 let pageno = ref None in
5918 selfexec := Sys.executable_name;
5919 Arg.parse
5920 (Arg.align
5921 [("-p", Arg.String (fun s -> state.password <- s),
5922 "<password> Set password");
5924 ("-f", Arg.String
5925 (fun s ->
5926 Config.fontpath := s;
5927 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
5929 "<path> Set path to the user interface font");
5931 ("-c", Arg.String
5932 (fun s ->
5933 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
5934 Config.confpath := s),
5935 "<path> Set path to the configuration file");
5937 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
5938 "<page-number> Jump to page");
5940 ("-tcf", Arg.String (fun s -> trimcachepath := s),
5941 "<path> Set path to the trim cache file");
5943 ("-dest", Arg.String (fun s -> state.nameddest <- s),
5944 "<named-destination> Set named destination");
5946 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
5947 ("-cxack", Arg.Set cxack, " Cut corners");
5949 ("-remote", Arg.String (fun s -> rcmdpath := s),
5950 "<path> Set path to the remote commands source");
5952 ("-origin", Arg.String (fun s -> state.origin <- s),
5953 "<original-path> Set original path");
5955 ("-v", Arg.Unit (fun () ->
5956 Printf.printf
5957 "%s\nconfiguration path: %s\n"
5958 (version ())
5959 Config.defconfpath
5961 exit 0), " Print version and exit");
5964 (fun s -> state.path <- s)
5965 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
5967 if !wtmode
5968 then selfexec := !selfexec ^ " -wtmode";
5970 if emptystr state.path
5971 then (prerr_endline "file name missing"; exit 1);
5973 if not (Config.load ())
5974 then prerr_endline "failed to load configuration";
5975 begin match !pageno with
5976 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
5977 | None -> ()
5978 end;
5980 let wsfd, winw, winh = Wsi.init (object (self)
5981 val mutable m_hack = false
5982 val mutable m_clicks = 0
5983 val mutable m_click_x = 0
5984 val mutable m_click_y = 0
5985 val mutable m_lastclicktime = infinity
5987 method private cleanup =
5988 state.roam <- noroam;
5989 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
5990 method expose = if not m_hack then G.postRedisplay "expose"
5991 method visible = G.postRedisplay "visible"
5992 method display = m_hack <- false; display ()
5993 method reshape w h =
5994 self#cleanup;
5995 m_hack <- w < state.winw && h < state.winh;
5996 reshape w h
5997 method mouse b d x y m =
5998 if d && canselect ()
5999 then (
6000 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
6001 m_click_x <- x;
6002 m_click_y <- y;
6003 if b = 1
6004 then (
6005 let t = now () in
6006 if abs x - m_click_x > 10
6007 || abs y - m_click_y > 10
6008 || abs_float (t -. m_lastclicktime) > 0.3
6009 then m_clicks <- 0;
6010 m_clicks <- m_clicks + 1;
6011 m_lastclicktime <- t;
6012 if m_clicks = 1
6013 then (
6014 self#cleanup;
6015 G.postRedisplay "cleanup";
6016 state.uioh <- state.uioh#button b d x y m;
6018 else state.uioh <- state.uioh#multiclick m_clicks x y m
6020 else (
6021 self#cleanup;
6022 m_clicks <- 0;
6023 m_lastclicktime <- infinity;
6024 state.uioh <- state.uioh#button b d x y m
6027 else (
6028 state.uioh <- state.uioh#button b d x y m
6030 method motion x y =
6031 state.mpos <- (x, y);
6032 state.uioh <- state.uioh#motion x y
6033 method pmotion x y =
6034 state.mpos <- (x, y);
6035 state.uioh <- state.uioh#pmotion x y
6036 method key k m =
6037 let mascm = m land (
6038 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6039 ) in
6040 let keyboard k m =
6041 let x = state.x and y = state.y in
6042 keyboard k m;
6043 if x != state.x || y != state.y then self#cleanup
6045 match state.keystate with
6046 | KSnone ->
6047 let km = k, mascm in
6048 begin
6049 match
6050 let modehash = state.uioh#modehash in
6051 try Hashtbl.find modehash km
6052 with Not_found ->
6053 try Hashtbl.find (findkeyhash conf "global") km
6054 with Not_found -> KMinsrt (k, m)
6055 with
6056 | KMinsrt (k, m) -> keyboard k m
6057 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6058 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6060 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6061 List.iter (fun (k, m) -> keyboard k m) insrt;
6062 state.keystate <- KSnone
6063 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6064 state.keystate <- KSinto (keys, insrt)
6065 | _ ->
6066 state.keystate <- KSnone
6068 method enter x y =
6069 state.mpos <- (x, y);
6070 state.uioh <- state.uioh#pmotion x y
6071 method leave = state.mpos <- (-1, -1)
6072 method winstate wsl = state.winstate <- wsl; m_hack <- false
6073 method quit = raise Quit
6074 end) conf.cwinw conf.cwinh (platform = Posx) in
6076 state.wsfd <- wsfd;
6078 if not (
6079 List.exists GlMisc.check_extension
6080 [ "GL_ARB_texture_rectangle"
6081 ; "GL_EXT_texture_recangle"
6082 ; "GL_NV_texture_rectangle" ]
6084 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6086 if (
6087 let r = GlMisc.get_string `renderer in
6088 let p = "Mesa DRI Intel(" in
6089 let l = String.length p in
6090 String.length r > l && String.sub r 0 l = p
6092 then (
6093 defconf.sliceheight <- 1024;
6094 defconf.texcount <- 32;
6095 defconf.usepbo <- true;
6098 let cr, sw =
6099 match Ne.res Unix.pipe with
6100 | Ne.Exn exn ->
6101 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
6102 exit 1
6103 | Ne.Res rw -> rw
6104 and sr, cw =
6105 match Ne.res Unix.pipe with
6106 | Ne.Exn exn ->
6107 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
6108 exit 1
6109 | Ne.Res rw -> rw
6112 cloexec cr;
6113 cloexec sw;
6114 cloexec sr;
6115 cloexec cw;
6117 setcheckers conf.checkers;
6118 redirectstderr ();
6119 if conf.redirectstderr
6120 then
6121 at_exit (fun () ->
6122 let s = Buffer.contents state.errmsgs ^
6123 (match state.errfd with
6124 | Some fd ->
6125 let s = String.create (80*24) in
6126 let n =
6128 let r, _, _ = Unix.select [fd] [] [] 0.0 in
6129 if List.mem fd r
6130 then Unix.read fd s 0 (String.length s)
6131 else 0
6132 with _ -> 0
6134 if n = 0
6135 then ""
6136 else String.sub s 0 n
6137 | None -> ""
6140 try ignore (Unix.write state.stderr s 0 (String.length s))
6141 with exn -> print_endline (exntos exn)
6145 init (cr, cw) (
6146 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6147 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6148 !Config.fontpath, !trimcachepath,
6149 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
6151 List.iter GlArray.enable [`texture_coord; `vertex];
6152 state.sr <- sr;
6153 state.sw <- sw;
6154 state.text <- "Opening " ^ (mbtoutf8 state.path);
6155 reshape winw winh;
6156 opendoc state.path state.password;
6157 state.uioh <- uioh;
6158 display ();
6159 Wsi.mapwin ();
6160 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6161 let optrfd =
6162 ref (
6163 if nonemptystr !rcmdpath
6164 then remoteopen !rcmdpath
6165 else None
6169 let rec loop deadline =
6170 let r =
6171 match state.errfd with
6172 | None -> [state.sr; state.wsfd]
6173 | Some fd -> [state.sr; state.wsfd; fd]
6175 let r =
6176 match !optrfd with
6177 | None -> r
6178 | Some fd -> fd :: r
6180 if state.redisplay
6181 then (
6182 state.redisplay <- false;
6183 display ();
6185 let timeout =
6186 let now = now () in
6187 if deadline > now
6188 then (
6189 if deadline = infinity
6190 then ~-.1.0
6191 else max 0.0 (deadline -. now)
6193 else 0.0
6195 let r, _, _ =
6196 try Unix.select r [] [] timeout
6197 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6199 begin match r with
6200 | [] ->
6201 state.ghyll None;
6202 let newdeadline =
6203 if state.ghyll == noghyll
6204 then
6205 match state.autoscroll with
6206 | Some step when step != 0 ->
6207 let y = state.y + step in
6208 let y =
6209 if y < 0
6210 then state.maxy
6211 else if y >= state.maxy then 0 else y
6213 gotoy y;
6214 if state.mode = View
6215 then state.text <- "";
6216 deadline +. 0.01
6217 | _ -> infinity
6218 else deadline +. 0.01
6220 loop newdeadline
6222 | l ->
6223 let rec checkfds = function
6224 | [] -> ()
6225 | fd :: rest when fd = state.sr ->
6226 let cmd = readcmd state.sr in
6227 act cmd;
6228 checkfds rest
6230 | fd :: rest when fd = state.wsfd ->
6231 Wsi.readresp fd;
6232 checkfds rest
6234 | fd :: rest when Some fd = !optrfd ->
6235 begin match remote fd with
6236 | None -> optrfd := remoteopen !rcmdpath;
6237 | opt -> optrfd := opt
6238 end;
6239 checkfds rest
6241 | fd :: rest ->
6242 let s = String.create 80 in
6243 let n = tempfailureretry (Unix.read fd s 0) 80 in
6244 if conf.redirectstderr
6245 then (
6246 Buffer.add_substring state.errmsgs s 0 n;
6247 state.newerrmsgs <- true;
6248 state.redisplay <- true;
6250 else (
6251 prerr_string (String.sub s 0 n);
6252 flush stderr;
6254 checkfds rest
6256 checkfds l;
6257 let newdeadline =
6258 let deadline1 =
6259 if deadline = infinity
6260 then now () +. 0.01
6261 else deadline
6263 match state.autoscroll with
6264 | Some step when step != 0 -> deadline1
6265 | _ -> if state.ghyll == noghyll then infinity else deadline1
6267 loop newdeadline
6268 end;
6271 loop infinity;
6272 with Quit ->
6273 Config.save leavebirdseye;