M-h M-C-\
[llpp.git] / main.ml
blob6f840dc9d4d8acfd50d5ad00284e0dd6dae6310f
1 open Utils;;
2 open Config;;
4 exception Quit;;
6 external init : Unix.file_descr -> initparams -> unit = "ml_init";;
7 external seltext : opaque -> (int * int * int * int) -> unit = "ml_seltext";;
8 external hassel : opaque -> bool = "ml_hassel";;
9 external getpdimrect : int -> float array = "ml_getpdimrect";;
10 external whatsunder : opaque -> int -> int -> under = "ml_whatsunder";;
11 external markunder : opaque -> int -> int -> mark -> bool = "ml_markunder";;
12 external clearmark : opaque -> unit = "ml_clearmark";;
13 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
14 external getmaxw : unit -> float = "ml_getmaxw";;
15 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
16 external measurestr : int -> string -> float = "ml_measure_string";;
17 external postprocess : opaque -> int -> int -> int -> (int * string * int)
18 -> int = "ml_postprocess";;
19 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
20 external setaalevel : int -> unit = "ml_setaalevel";;
21 external realloctexts : int -> bool = "ml_realloctexts";;
22 external findlink : opaque -> linkdir -> link = "ml_findlink";;
23 external getlink : opaque -> int -> under = "ml_getlink";;
24 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
25 external getlinkcount : opaque -> int = "ml_getlinkcount";;
26 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links";;
27 external getpbo : width -> height -> colorspace -> opaque = "ml_getpbo";;
28 external freepbo : opaque -> unit = "ml_freepbo";;
29 external unmappbo : opaque -> unit = "ml_unmappbo";;
30 external bousable : unit -> bool = "ml_bo_usable";;
31 external unproject : opaque -> int -> int
32 -> (int * int) option = "ml_unproject";;
33 external project : opaque -> int -> int -> float -> float
34 -> (float * float) = "ml_project";;
35 external drawtile : tileparams -> opaque
36 -> unit = "ml_drawtile";;
37 external rectofblock : opaque -> int -> int
38 -> float array option = "ml_rectofblock";;
39 external begintiles : unit -> unit = "ml_begintiles";;
40 external endtiles : unit -> unit = "ml_endtiles";;
41 external addannot : opaque -> int -> int -> string -> unit = "ml_addannot";;
42 external modannot : opaque -> slinkindex -> string -> unit = "ml_modannot";;
43 external delannot : opaque -> slinkindex -> unit = "ml_delannot";;
44 external hasunsavedchanges : unit -> bool = "ml_hasunsavedchanges";;
45 external savedoc : string -> unit = "ml_savedoc";;
46 external getannotcontents : opaque -> slinkindex
47 -> string = "ml_getannotcontents";;
48 external drawprect : opaque -> int -> int -> float array
49 -> unit = "ml_drawprect";;
50 external wcmd : Unix.file_descr -> bytes -> int -> unit = "ml_wcmd";;
51 external rcmd : Unix.file_descr -> string = "ml_rcmd";;
52 external uritolocation : string
53 -> (pageno * float * float) = "ml_uritolocation";;
54 external isexternallink : string -> bool = "ml_isexternallink";;
56 (* copysel _will_ close the supplied descriptor *)
57 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
59 let selfexec = ref E.s;;
60 let opengl_has_pbo = ref false;;
62 let drawstring size x y s =
63 Gl.enable `blend;
64 Gl.enable `texture_2d;
65 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
66 ignore (drawstr size x y s);
67 Gl.disable `blend;
68 Gl.disable `texture_2d;
71 let drawstring1 size x y s =
72 drawstr size x y s;
75 let drawstring2 size x y fmt =
76 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
79 module UniSyms = struct
80 let ellipsis = "\xe2\x80\xa6";;
81 let radical = "\xe2\x88\x9a";;
82 let lguillemet = "\xc2\xab";;
83 let rguillemet = "\xc2\xbb";;
84 end;;
86 let _debugl l =
87 dolog {|l %d dim=%d {
88 WxH %dx%d
89 vWxH %dx%d
90 pagex,y %d,%d
91 dispx,y %d,%d
92 column %d
93 }|}
94 l.pageno l.pagedimno
95 l.pagew l.pageh
96 l.pagevw l.pagevh
97 l.pagex l.pagey
98 l.pagedispx l.pagedispy
99 l.pagecol
102 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
103 dolog {|rect {
104 x0,y0=(% f, % f)
105 x1,y1=(% f, % f)
106 x2,y2=(% f, % f)
107 x3,y3=(% f, % f)
108 }|} x0 y0 x1 y1 x2 y2 x3 y3;
111 let isbirdseye = function
112 | Birdseye _ -> true
113 | Textentry _ | View | LinkNav _ -> false
116 let istextentry = function
117 | Textentry _ -> true
118 | Birdseye _ | View | LinkNav _ -> false
121 let wtmode = ref false;;
122 let cxack = ref false;;
124 let pgscale h = truncate (float h *. conf.pgscale);;
126 let hscrollh () =
127 if state.uioh#alwaysscrolly || ((conf.scrollb land scrollbhv != 0)
128 && (state.w > state.winw))
129 then conf.scrollbw
130 else 0
133 let vscrollw () =
134 if state.uioh#alwaysscrolly || ((conf.scrollb land scrollbvv != 0)
135 && (state.maxy > state.winh))
136 then conf.scrollbw
137 else 0
140 let vscrollhit x =
141 if conf.leftscroll
142 then x < vscrollw ()
143 else x > state.winw - vscrollw ()
146 let setfontsize n =
147 fstate.fontsize <- n;
148 fstate.wwidth <- measurestr fstate.fontsize "w";
149 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
152 let vlog fmt =
153 if conf.verbose
154 then dolog fmt
155 else Printf.kprintf ignore fmt
158 let launchpath () =
159 if emptystr conf.pathlauncher
160 then dolog "%s" state.path
161 else (
162 let command = Str.global_replace percentsre state.path conf.pathlauncher in
163 match spawn command [] with
164 | _pid -> ()
165 | exception exn ->
166 dolog "failed to execute `%s': %s" command @@ exntos exn
170 module G =
171 struct
172 let postRedisplay who =
173 vlog "redisplay for [%S]" who;
174 state.redisplay <- true;
176 end;;
178 let getopaque pageno =
179 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
180 with Not_found -> None
183 let pagetranslatepoint l x y =
184 let dy = y - l.pagedispy in
185 let y = dy + l.pagey in
186 let dx = x - l.pagedispx in
187 let x = dx + l.pagex in
188 (x, y);
191 let onppundermouse g x y d =
192 let rec f = function
193 | l :: rest ->
194 begin match getopaque l.pageno with
195 | Some opaque ->
196 let x0 = l.pagedispx in
197 let x1 = x0 + l.pagevw in
198 let y0 = l.pagedispy in
199 let y1 = y0 + l.pagevh in
200 if y >= y0 && y <= y1 && x >= x0 && x <= x1
201 then
202 let px, py = pagetranslatepoint l x y in
203 match g opaque l px py with
204 | Some res -> res
205 | None -> f rest
206 else f rest
207 | _ ->
208 f rest
210 | [] -> d
212 f state.layout
215 let getunder x y =
216 let g opaque l px py =
217 if state.bzoom
218 then (
219 match rectofblock opaque px py with
220 | Some [|x0;x1;y0;y1|] ->
221 let rect = (x0, y0, x1, y0, x1, y1, x0, y1) in
222 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
223 state.rects <- [l.pageno, color, rect];
224 G.postRedisplay "getunder";
225 | _ -> ()
227 let under = whatsunder opaque px py in
228 if under = Unone then None else Some under
230 onppundermouse g x y Unone
233 let unproject x y =
234 let g opaque l x y =
235 match unproject opaque x y with
236 | Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
237 | None -> None
239 onppundermouse g x y None;
242 let showtext c s =
243 state.text <- Printf.sprintf "%c%s" c s;
244 G.postRedisplay "showtext";
247 let impmsg fmt =
248 Format.ksprintf (fun s -> showtext '!' s) fmt;
251 let pipef ?(closew=true) cap f cmd =
252 match Unix.pipe () with
253 | exception exn -> dolog "%s cannot create pipe: %S" cap @@ exntos exn
254 | (r, w) ->
255 begin match spawn cmd [r, 0; w, -1] with
256 | exception exn -> dolog "%s: cannot execute %S: %s" cap cmd @@ exntos exn
257 | _pid -> f w
258 end;
259 Ne.clo r (dolog "%s failed to close r: %s" cap);
260 if closew then Ne.clo w (dolog "%s failed to close w: %s" cap);
263 let pipesel opaque cmd =
264 if hassel opaque
265 then pipef ~closew:false "pipesel"
266 (fun w ->
267 copysel w opaque;
268 G.postRedisplay "pipesel"
269 ) cmd
272 let paxunder x y =
273 let g opaque l px py =
274 if markunder opaque px py conf.paxmark
275 then (
276 Some (fun () ->
277 match getopaque l.pageno with
278 | None -> ()
279 | Some opaque -> pipesel opaque conf.paxcmd
282 else None
284 G.postRedisplay "paxunder";
285 if conf.paxmark = Mark_page
286 then
287 List.iter (fun l ->
288 match getopaque l.pageno with
289 | None -> ()
290 | Some opaque -> clearmark opaque) state.layout;
291 state.roam <- onppundermouse g x y (fun () -> impmsg "whoopsie daisy");
294 let selstring s =
295 pipef
296 "selstring" (fun w ->
298 let l = String.length s in
299 let bytes = Bytes.unsafe_of_string s in
300 let n = tempfailureretry (Unix.write w bytes 0) l in
301 if n != l
302 then impmsg "failed to write %d characters to sel pipe, wrote %d" l n;
303 with exn -> impmsg "failed to write to sel pipe: %s" @@ exntos exn
304 ) conf.selcmd
307 let undertext = function
308 | Unone -> "none"
309 | Ulinkuri s -> s
310 | Utext s -> "font: " ^ s
311 | Uannotation (opaque, slinkindex) ->
312 "annotation: " ^ getannotcontents opaque slinkindex
315 let updateunder x y =
316 match getunder x y with
317 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
318 | Ulinkuri uri ->
319 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
320 Wsi.setcursor Wsi.CURSOR_INFO
321 | Utext s ->
322 if conf.underinfo then showtext 'f' ("ont: " ^ s);
323 Wsi.setcursor Wsi.CURSOR_TEXT
324 | Uannotation _ ->
325 if conf.underinfo then showtext 'a' "nnotation";
326 Wsi.setcursor Wsi.CURSOR_INFO
329 let showlinktype under =
330 if conf.underinfo && under != Unone
331 then showtext ' ' @@ undertext under
334 let [@warning "-4"] intentry_with_suffix text key =
335 let text =
336 match key with
337 | Keys.Ascii ('0'..'9' as c) -> addchar text c
338 | Keys.Ascii ('k' | 'm' | 'g' | 'K' | 'M' | 'G' as c) ->
339 addchar text @@ asciilower c
340 | _ ->
341 state.text <- Printf.sprintf "invalid key";
342 text
344 TEcont text
347 let wcmd fmt =
348 let b = Buffer.create 16 in
349 Printf.kbprintf
350 (fun b ->
351 let b = Buffer.to_bytes b in
352 wcmd state.ss b @@ Bytes.length b
353 ) b fmt
356 let nogeomcmds cmds =
357 match cmds with
358 | s, [] -> emptystr s
359 | _ -> false
362 let layoutN ((columns, coverA, coverB), b) x y sw sh =
363 let rec fold accu n =
364 if n = Array.length b
365 then accu
366 else
367 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
368 if (vy - y) > sh &&
369 (n = coverA - 1
370 || n = state.pagecount - coverB
371 || (n - coverA) mod columns = columns - 1)
372 then accu
373 else
374 let accu =
375 if vy + h > y
376 then
377 let pagey = max 0 (y - vy) in
378 let pagedispy = if pagey > 0 then 0 else vy - y in
379 let pagedispx, pagex =
380 let pdx =
381 if n = coverA - 1 || n = state.pagecount - coverB
382 then x + (sw - w) / 2
383 else dx + xoff + x
385 if pdx < 0
386 then 0, -pdx
387 else pdx, 0
389 let pagevw =
390 let vw = sw - pagedispx in
391 let pw = w - pagex in
392 min vw pw
394 let pagevh = min (h - pagey) (sh - pagedispy) in
395 if pagevw > 0 && pagevh > 0
396 then
397 let e =
398 { pageno = n
399 ; pagedimno = pdimno
400 ; pagew = w
401 ; pageh = h
402 ; pagex = pagex
403 ; pagey = pagey
404 ; pagevw = pagevw
405 ; pagevh = pagevh
406 ; pagedispx = pagedispx
407 ; pagedispy = pagedispy
408 ; pagecol = 0
411 e :: accu
412 else
413 accu
414 else
415 accu
417 fold accu (n+1)
419 if Array.length b = 0
420 then []
421 else List.rev (fold [] (page_of_y y))
424 let layoutS (columns, b) x y sw sh =
425 let rec fold accu n =
426 if n = Array.length b
427 then accu
428 else
429 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
430 if (vy - y) > sh
431 then accu
432 else
433 let accu =
434 if vy + pageh > y
435 then
436 let x = xoff + x in
437 let pagey = max 0 (y - vy) in
438 let pagedispy = if pagey > 0 then 0 else vy - y in
439 let pagedispx, pagex =
440 if px = 0
441 then (
442 if x < 0
443 then 0, -x
444 else x, 0
446 else (
447 let px = px - x in
448 if px < 0
449 then -px, 0
450 else 0, px
453 let pagecolw = pagew/columns in
454 let pagedispx =
455 if pagecolw < sw
456 then pagedispx + ((sw - pagecolw) / 2)
457 else pagedispx
459 let pagevw =
460 let vw = sw - pagedispx in
461 let pw = pagew - pagex in
462 min vw pw
464 let pagevw = min pagevw pagecolw in
465 let pagevh = min (pageh - pagey) (sh - pagedispy) in
466 if pagevw > 0 && pagevh > 0
467 then
468 let e =
469 { pageno = n/columns
470 ; pagedimno = pdimno
471 ; pagew = pagew
472 ; pageh = pageh
473 ; pagex = pagex
474 ; pagey = pagey
475 ; pagevw = pagevw
476 ; pagevh = pagevh
477 ; pagedispx = pagedispx
478 ; pagedispy = pagedispy
479 ; pagecol = n mod columns
482 e :: accu
483 else
484 accu
485 else
486 accu
488 fold accu (n+1)
490 List.rev (fold [] 0)
493 let layout x y sw sh =
494 if nogeomcmds state.geomcmds
495 then
496 match conf.columns with
497 | Csingle b -> layoutN ((1, 0, 0), b) x y sw sh
498 | Cmulti c -> layoutN c x y sw sh
499 | Csplit s -> layoutS s x y sw sh
500 else []
503 let maxy () = state.maxy - if conf.maxhfit then state.winh else 0;;
505 let clamp incr = bound (state.y + incr) 0 @@ maxy ();;
507 let itertiles l f =
508 let tilex = l.pagex mod conf.tilew in
509 let tiley = l.pagey mod conf.tileh in
511 let col = l.pagex / conf.tilew in
512 let row = l.pagey / conf.tileh in
514 let rec rowloop row y0 dispy h =
515 if h = 0
516 then ()
517 else (
518 let dh = conf.tileh - y0 in
519 let dh = min h dh in
520 let rec colloop col x0 dispx w =
521 if w = 0
522 then ()
523 else (
524 let dw = conf.tilew - x0 in
525 let dw = min w dw in
526 f col row dispx dispy x0 y0 dw dh;
527 colloop (col+1) 0 (dispx+dw) (w-dw)
530 colloop col tilex l.pagedispx l.pagevw;
531 rowloop (row+1) 0 (dispy+dh) (h-dh)
534 if l.pagevw > 0 && l.pagevh > 0
535 then rowloop row tiley l.pagedispy l.pagevh;
538 let gettileopaque l col row =
539 let key =
540 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
542 try Some (Hashtbl.find state.tilemap key)
543 with Not_found -> None
546 let puttileopaque l col row gen colorspace angle opaque size elapsed =
547 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
548 Hashtbl.add state.tilemap key (opaque, size, elapsed)
551 let filledrect2 x0 y0 x1 y1 x2 y2 x3 y3 =
552 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x1; y1; x2; y2; x3; y3 |];
553 GlArray.vertex `two state.vraw;
554 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
557 let filledrect1 x0 y0 x1 y1 = filledrect2 x0 y0 x0 y1 x1 y0 x1 y1;;
559 let filledrect x0 y0 x1 y1 =
560 GlArray.disable `texture_coord;
561 filledrect1 x0 y0 x1 y1;
562 GlArray.enable `texture_coord;
565 let linerect x0 y0 x1 y1 =
566 GlArray.disable `texture_coord;
567 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
568 GlArray.vertex `two state.vraw;
569 GlArray.draw_arrays `line_loop ~first:0 ~count:4;
570 GlArray.enable `texture_coord;
573 let drawtiles l color =
574 GlDraw.color color;
575 begintiles ();
576 let f col row x y tilex tiley w h =
577 match gettileopaque l col row with
578 | Some (opaque, _, t) ->
579 let params = x, y, w, h, tilex, tiley in
580 if conf.invert
581 then GlTex.env (`mode `blend);
582 drawtile params opaque;
583 if conf.invert
584 then GlTex.env (`mode `modulate);
585 if conf.debug
586 then (
587 endtiles ();
588 let s = Printf.sprintf
589 "%d[%d,%d] %f sec"
590 l.pageno col row t
592 let w = measurestr fstate.fontsize s in
593 GlDraw.color (0.0, 0.0, 0.0);
594 filledrect (float (x-2))
595 (float (y-2))
596 (float (x+2) +. w)
597 (float (y + fstate.fontsize + 2));
598 GlDraw.color color;
599 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
600 begintiles ();
603 | None ->
604 endtiles ();
605 let w =
606 let lw = state.winw - x in
607 min lw w
608 and h =
609 let lh = state.winh - y in
610 min lh h
612 if conf.invert
613 then GlTex.env (`mode `blend);
614 begin match state.checkerstexid with
615 | Some id ->
616 Gl.enable `texture_2d;
617 GlTex.bind_texture ~target:`texture_2d id;
618 let x0 = float x
619 and y0 = float y
620 and x1 = float (x+w)
621 and y1 = float (y+h) in
623 let tw = float w /. 16.0
624 and th = float h /. 16.0 in
625 let tx0 = float tilex /. 16.0
626 and ty0 = float tiley /. 16.0 in
627 let tx1 = tx0 +. tw
628 and ty1 = ty0 +. th in
629 Raw.sets_float state.vraw ~pos:0
630 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
631 Raw.sets_float state.traw ~pos:0
632 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
633 GlArray.vertex `two state.vraw;
634 GlArray.tex_coord `two state.traw;
635 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
636 Gl.disable `texture_2d;
638 | None ->
639 GlDraw.color (1.0, 1.0, 1.0);
640 filledrect (float x) (float y) (float (x+w)) (float (y+h));
641 end;
642 if conf.invert
643 then GlTex.env (`mode `modulate);
644 if w > 128 && h > fstate.fontsize + 10
645 then (
646 let c = if conf.invert then 1.0 else 0.0 in
647 GlDraw.color (c, c, c);
648 let c, r =
649 if conf.verbose
650 then (col*conf.tilew, row*conf.tileh)
651 else col, row
653 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
655 GlDraw.color color;
656 begintiles ();
658 itertiles l f;
659 endtiles ();
662 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
664 let tilevisible1 l x y =
665 let ax0 = l.pagex
666 and ax1 = l.pagex + l.pagevw
667 and ay0 = l.pagey
668 and ay1 = l.pagey + l.pagevh in
670 let bx0 = x
671 and by0 = y in
672 let bx1 = min (bx0 + conf.tilew) l.pagew
673 and by1 = min (by0 + conf.tileh) l.pageh in
675 let rx0 = max ax0 bx0
676 and ry0 = max ay0 by0
677 and rx1 = min ax1 bx1
678 and ry1 = min ay1 by1 in
680 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
681 nonemptyintersection
684 let tilevisible layout n x y =
685 let rec findpageinlayout m = function
686 | l :: rest when l.pageno = n ->
687 tilevisible1 l x y || (
688 match conf.columns with
689 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
690 | Csplit _ | Csingle _ | Cmulti _ -> false
692 | _ :: rest -> findpageinlayout 0 rest
693 | [] -> false
695 findpageinlayout 0 layout;
698 let tileready l x y =
699 tilevisible1 l x y &&
700 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
703 let tilepage n p layout =
704 let rec loop = function
705 | l :: rest ->
706 if l.pageno = n
707 then
708 let f col row _ _ _ _ _ _ =
709 if state.currently = Idle
710 then
711 match gettileopaque l col row with
712 | Some _ -> ()
713 | None ->
714 let x = col*conf.tilew
715 and y = row*conf.tileh in
716 let w =
717 let w = l.pagew - x in
718 min w conf.tilew
720 let h =
721 let h = l.pageh - y in
722 min h conf.tileh
724 let pbo =
725 if conf.usepbo
726 then getpbo w h conf.colorspace
727 else ~< "0"
729 wcmd "tile %s %d %d %d %d %s"
730 (~> p) x y w h (~> pbo);
731 state.currently <-
732 Tiling (
733 l, p, conf.colorspace, conf.angle,
734 state.gen, col, row, conf.tilew, conf.tileh
737 itertiles l f;
738 else
739 loop rest
741 | [] -> ()
743 if nogeomcmds state.geomcmds
744 then loop layout;
747 let preloadlayout x y sw sh =
748 let y = if y < sh then 0 else y - sh in
749 let x = min 0 (x + sw) in
750 let h = sh*3 in
751 let w = sw*3 in
752 layout x y w h;
755 let load pages =
756 let rec loop pages =
757 if state.currently != Idle
758 then ()
759 else
760 match pages with
761 | l :: rest ->
762 begin match getopaque l.pageno with
763 | None ->
764 wcmd "page %d %d" l.pageno l.pagedimno;
765 state.currently <- Loading (l, state.gen);
766 | Some opaque ->
767 tilepage l.pageno opaque pages;
768 loop rest
769 end;
770 | _ -> ()
772 if nogeomcmds state.geomcmds
773 then loop pages
776 let preload pages =
777 load pages;
778 if conf.preload && state.currently = Idle
779 then load (preloadlayout state.x state.y state.winw state.winh);
782 let layoutready layout =
783 let rec fold all ls =
784 all && match ls with
785 | l :: rest ->
786 let seen = ref false in
787 let allvisible = ref true in
788 let foo col row _ _ _ _ _ _ =
789 seen := true;
790 allvisible := !allvisible &&
791 begin match gettileopaque l col row with
792 | Some _ -> true
793 | None -> false
796 itertiles l foo;
797 fold (!seen && !allvisible) rest
798 | [] -> true
800 let alltilesvisible = fold true layout in
801 alltilesvisible;
804 let gotoxy x y =
805 let y = bound y 0 state.maxy in
806 let y, layout, proceed =
807 match conf.maxwait with
808 | Some time when state.ghyll == noghyll ->
809 begin match state.throttle with
810 | None ->
811 let layout = layout x y state.winw state.winh in
812 let ready = layoutready layout in
813 if not ready
814 then (
815 load layout;
816 state.throttle <- Some (layout, y, now ());
818 else G.postRedisplay "gotoxy showall (None)";
819 y, layout, ready
820 | Some (_, _, started) ->
821 let dt = now () -. started in
822 if dt > time
823 then (
824 state.throttle <- None;
825 let layout = layout x y state.winw state.winh in
826 load layout;
827 G.postRedisplay "maxwait";
828 y, layout, true
830 else -1, [], false
833 | _ ->
834 let layout = layout x y state.winw state.winh in
835 if not !wtmode || layoutready layout
836 then G.postRedisplay "gotoxy ready";
837 y, layout, true
839 if proceed
840 then (
841 state.x <- x;
842 state.y <- y;
843 state.layout <- layout;
844 begin match state.mode with
845 | LinkNav ln ->
846 begin match ln with
847 | Ltexact (pageno, linkno) ->
848 let rec loop = function
849 | [] ->
850 state.lnava <- Some (pageno, linkno);
851 state.mode <- LinkNav (Ltgendir 0)
852 | l :: _ when l.pageno = pageno ->
853 begin match getopaque pageno with
854 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
855 | Some opaque ->
856 let x0, y0, x1, y1 = getlinkrect opaque linkno in
857 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
858 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
859 then state.mode <- LinkNav (Ltgendir 0)
861 | _ :: rest -> loop rest
863 loop layout
864 | Ltnotready _ | Ltgendir _ -> ()
866 | Birdseye _ | Textentry _ | View -> ()
867 end;
868 begin match state.mode with
869 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
870 if not (pagevisible layout pageno)
871 then (
872 match state.layout with
873 | [] -> ()
874 | l :: _ ->
875 state.mode <- Birdseye (
876 conf, leftx, l.pageno, hooverpageno, anchor
879 | LinkNav lt ->
880 begin match lt with
881 | Ltnotready (_, dir)
882 | Ltgendir dir ->
883 let linknav =
884 let rec loop = function
885 | [] -> lt
886 | l :: rest ->
887 match getopaque l.pageno with
888 | None -> Ltnotready (l.pageno, dir)
889 | Some opaque ->
890 let link =
891 let ld =
892 if dir = 0
893 then LDfirstvisible (l.pagex, l.pagey, dir)
894 else (
895 if dir > 0 then LDfirst else LDlast
898 findlink opaque ld
900 match link with
901 | Lnotfound -> loop rest
902 | Lfound n ->
903 showlinktype (getlink opaque n);
904 Ltexact (l.pageno, n)
906 loop state.layout
908 state.mode <- LinkNav linknav
909 | Ltexact _ -> ()
911 | Textentry _ | View -> ()
912 end;
913 preload layout;
915 state.ghyll <- noghyll;
916 if conf.updatecurs
917 then (
918 let mx, my = state.mpos in
919 updateunder mx my;
923 let conttiling pageno opaque =
924 tilepage pageno opaque
925 (if conf.preload
926 then preloadlayout state.x state.y state.winw state.winh
927 else state.layout)
930 let gotoxy_and_clear_text x y =
931 if not conf.verbose then state.text <- E.s;
932 gotoxy x y;
935 let getanchory (n, top, dtop) =
936 let y, h = getpageyh n in
937 if conf.presentation
938 then
939 let ips = calcips h in
940 y + truncate (top*.float h -. dtop*.float ips) + ips;
941 else
942 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
945 let gotoanchor anchor =
946 gotoxy state.x (getanchory anchor);
949 let addnav () =
950 cbput state.hists.nav (getanchor ());
953 let getnav dir =
954 let anchor = cbgetc state.hists.nav dir in
955 getanchory anchor;
958 let gotoghyll1 single y =
959 let scroll f n a b =
960 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
961 let snake f a b =
962 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
963 if f < a
964 then s (float f /. float a)
965 else (
966 if f > b
967 then 1.0 -. s ((float (f-b) /. float (n-b)))
968 else 1.0
971 snake f a b
972 and summa n a b =
973 let ins = float a *. 0.5
974 and outs = float (n-b) *. 0.5 in
975 let ones = b - a in
976 ins +. outs +. float ones
978 let rec set nab y sy =
979 let (_N, _A, _B), y =
980 if single
981 then
982 let scl = if y > sy then 2 else -2 in
983 let _N, _, _ = nab in
984 (_N,0,_N), y+conf.scrollstep*scl
985 else nab,y in
986 let sum = summa _N _A _B in
987 let dy = float (y - sy) in
988 state.ghyll <- (
989 let rec gf n y1 o =
990 if n >= _N
991 then state.ghyll <- noghyll
992 else
993 let go n =
994 let s = scroll n _N _A _B in
995 let y1 = y1 +. ((s *. dy) /. sum) in
996 gotoxy_and_clear_text state.x (truncate y1);
997 state.ghyll <- gf (n+1) y1;
999 match o with
1000 | None -> go n
1001 | Some y' when single -> set nab y' state.y
1002 | Some y' -> set (_N/2, 1, 1) y' state.y
1004 gf 0 (float state.y)
1007 match conf.ghyllscroll with
1008 | Some nab when not conf.presentation ->
1009 if state.ghyll == noghyll
1010 then set nab y state.y
1011 else state.ghyll (Some y)
1012 | _ ->
1013 gotoxy_and_clear_text state.x y
1016 let gotoghyll = gotoghyll1 false;;
1018 let gotopage n top =
1019 let y, h = getpageyh n in
1020 let y = y + (truncate (top *. float h)) in
1021 gotoghyll y
1024 let gotopage1 n top =
1025 let y = getpagey n in
1026 let y = y + top in
1027 gotoghyll y
1030 let invalidate ?(redisplay=false) s f =
1031 state.redisplay <- redisplay;
1032 state.layout <- [];
1033 state.pdims <- [];
1034 state.rects <- [];
1035 state.rects1 <- [];
1036 match state.geomcmds with
1037 | ps, [] when emptystr ps ->
1038 f ();
1039 state.geomcmds <- s, [];
1041 | ps, [] ->
1042 state.geomcmds <- ps, [s, f];
1044 | ps, (s', _) :: rest when s' = s ->
1045 state.geomcmds <- ps, ((s, f) :: rest);
1047 | ps, cmds ->
1048 state.geomcmds <- ps, ((s, f) :: cmds);
1051 let flushpages () =
1052 Hashtbl.iter (fun _ opaque ->
1053 wcmd "freepage %s" (~> opaque);
1054 ) state.pagemap;
1055 Hashtbl.clear state.pagemap;
1058 let flushtiles () =
1059 if not (Queue.is_empty state.tilelru)
1060 then (
1061 Queue.iter (fun (k, p, s) ->
1062 wcmd "freetile %s" (~> p);
1063 state.memused <- state.memused - s;
1064 Hashtbl.remove state.tilemap k;
1065 ) state.tilelru;
1066 state.uioh#infochanged Memused;
1067 Queue.clear state.tilelru;
1069 load state.layout;
1072 let stateh h =
1073 let h = truncate (float h*.conf.zoom) in
1074 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1075 h - d
1078 let opendoc path password =
1079 state.path <- path;
1080 state.password <- password;
1081 state.gen <- state.gen + 1;
1082 state.docinfo <- [];
1083 state.outlines <- [||];
1085 flushpages ();
1086 setaalevel conf.aalevel;
1087 let titlepath =
1088 if emptystr state.origin
1089 then path
1090 else state.origin
1092 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1093 wcmd "open %d %d %d %s\000%s\000%s\000"
1094 (btod !wtmode) (btod !cxack) (btod conf.usedoccss)
1095 path password conf.css;
1096 invalidate "reqlayout"
1097 (fun () ->
1098 wcmd "reqlayout %d %d %d %s\000"
1099 conf.angle (FMTE.to_int conf.fitmodel)
1100 (stateh state.winh) state.nameddest
1102 state.help <-
1103 let sl = keystostrlist conf in
1104 let rec loop accu =
1105 function | [] -> accu
1106 | s :: rest -> loop ((s, 0, Noaction) :: accu) rest
1107 in makehelp () @ (("", 0, Noaction) :: loop [] sl) |> Array.of_list
1110 let reload () =
1111 state.anchor <- getanchor ();
1112 opendoc state.path state.password;
1115 let scalecolor c =
1116 let c = c *. conf.colorscale in
1117 (c, c, c);
1120 let scalecolor2 (r, g, b) =
1121 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1124 let docolumns columns =
1125 match columns with
1126 | Csingle _ ->
1127 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1128 let rec loop pageno pdimno pdim y ph pdims =
1129 if pageno = state.pagecount
1130 then ()
1131 else
1132 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1133 match pdims with
1134 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1135 pdimno+1, pdim, rest
1136 | _ ->
1137 pdimno, pdim, pdims
1139 let x = max 0 (((state.winw - w) / 2) - xoff) in
1140 let y =
1141 y + (if conf.presentation
1142 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1143 else (if pageno = 0 then 0 else conf.interpagespace)
1146 a.(pageno) <- (pdimno, x, y, pdim);
1147 loop (pageno+1) pdimno pdim (y + h) h pdims
1149 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1150 conf.columns <- Csingle a;
1152 | Cmulti ((columns, coverA, coverB), _) ->
1153 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1154 let rec loop pageno pdimno pdim x y rowh pdims =
1155 let rec fixrow m =
1156 if m = pageno then () else
1157 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1158 if h < rowh
1159 then (
1160 let y = y + (rowh - h) / 2 in
1161 a.(m) <- (pdimno, x, y, pdim);
1163 fixrow (m+1)
1165 if pageno = state.pagecount
1166 then fixrow (((pageno - 1) / columns) * columns)
1167 else
1168 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1169 match pdims with
1170 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1171 pdimno+1, pdim, rest
1172 | _ ->
1173 pdimno, pdim, pdims
1175 let x, y, rowh' =
1176 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1177 then (
1178 let x = (state.winw - w) / 2 in
1179 let ips =
1180 if conf.presentation then calcips h else conf.interpagespace in
1181 x, y + ips + rowh, h
1183 else (
1184 if (pageno - coverA) mod columns = 0
1185 then (
1186 let x = max 0 (state.winw - state.w) / 2 in
1187 let y =
1188 if conf.presentation
1189 then
1190 let ips = calcips h in
1191 y + (if pageno = 0 then 0 else calcips rowh + ips)
1192 else
1193 y + (if pageno = 0 then 0 else conf.interpagespace)
1195 x, y + rowh, h
1197 else x, y, max rowh h
1200 let y =
1201 if pageno > 1 && (pageno - coverA) mod columns = 0
1202 then (
1203 let y =
1204 if pageno = columns && conf.presentation
1205 then (
1206 let ips = calcips rowh in
1207 for i = 0 to pred columns
1209 let (pdimno, x, y, pdim) = a.(i) in
1210 a.(i) <- (pdimno, x, y+ips, pdim)
1211 done;
1212 y+ips;
1214 else y
1216 fixrow (pageno - columns);
1219 else y
1221 a.(pageno) <- (pdimno, x, y, pdim);
1222 let x = x + w + xoff*2 + conf.interpagespace in
1223 loop (pageno+1) pdimno pdim x y rowh' pdims
1225 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1226 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1228 | Csplit (c, _) ->
1229 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1230 let rec loop pageno pdimno pdim y pdims =
1231 if pageno = state.pagecount
1232 then ()
1233 else
1234 let pdimno, ((_, w, h, _) as pdim), pdims =
1235 match pdims with
1236 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1237 pdimno+1, pdim, rest
1238 | _ ->
1239 pdimno, pdim, pdims
1241 let cw = w / c in
1242 let rec loop1 n x y =
1243 if n = c then y else (
1244 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1245 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1248 let y = loop1 0 0 y in
1249 loop (pageno+1) pdimno pdim y pdims
1251 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1252 conf.columns <- Csplit (c, a);
1255 let represent () =
1256 docolumns conf.columns;
1257 state.maxy <- calcheight ();
1258 if state.reprf == noreprf
1259 then (
1260 match state.mode with
1261 | Birdseye (_, _, pageno, _, _) ->
1262 let y, h = getpageyh pageno in
1263 let top = (state.winh - h) / 2 in
1264 gotoxy state.x (max 0 (y - top))
1265 | Textentry _ | View | LinkNav _ ->
1266 let y = getanchory state.anchor in
1267 let y = min y (state.maxy - state.winh) in
1268 gotoxy state.x y;
1270 else (
1271 state.reprf ();
1272 state.reprf <- noreprf;
1276 let reshape ?(firsttime=false) w h =
1277 GlDraw.viewport ~x:0 ~y:0 ~w ~h;
1278 if not firsttime && nogeomcmds state.geomcmds
1279 then state.anchor <- getanchor ();
1281 state.winw <- w;
1282 let w = truncate (float w *. conf.zoom) in
1283 let w = max w 2 in
1284 state.winh <- h;
1285 setfontsize fstate.fontsize;
1286 GlMat.mode `modelview;
1287 GlMat.load_identity ();
1289 GlMat.mode `projection;
1290 GlMat.load_identity ();
1291 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1292 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1293 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1295 let relx =
1296 if conf.zoom <= 1.0
1297 then 0.0
1298 else float state.x /. float state.w
1300 invalidate ~redisplay:true "geometry"
1301 (fun () ->
1302 state.w <- w;
1303 if not firsttime
1304 then state.x <- truncate (relx *. float w);
1305 let w =
1306 match conf.columns with
1307 | Csingle _ -> w
1308 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1309 | Csplit (c, _) -> w * c
1311 wcmd "geometry %d %d %d"
1312 w (stateh h) (FMTE.to_int conf.fitmodel)
1316 let enttext () =
1317 let len = String.length state.text in
1318 let x0 = if conf.leftscroll then vscrollw () else 0 in
1319 let drawstring s =
1320 let hscrollh =
1321 match state.mode with
1322 | Textentry _ | View | LinkNav _ ->
1323 let h, _, _ = state.uioh#scrollpw in
1325 | Birdseye _ -> 0
1327 let rect x w =
1328 filledrect
1329 x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1330 (x+.w) (float (state.winh - hscrollh))
1333 let w = float (state.winw - 1 - vscrollw ()) in
1334 if state.progress >= 0.0 && state.progress < 1.0
1335 then (
1336 GlDraw.color (0.3, 0.3, 0.3);
1337 let w1 = w *. state.progress in
1338 rect (float x0) w1;
1339 GlDraw.color (0.0, 0.0, 0.0);
1340 rect (float x0+.w1) (float x0+.w-.w1)
1342 else (
1343 GlDraw.color (0.0, 0.0, 0.0);
1344 rect (float x0) w;
1347 GlDraw.color (1.0, 1.0, 1.0);
1348 drawstring
1349 fstate.fontsize
1350 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1351 (state.winh - hscrollh - 5) s;
1353 let s =
1354 match state.mode with
1355 | Textentry ((prefix, text, _, _, _, _), _) ->
1356 let s =
1357 if len > 0
1358 then Printf.sprintf "%s%s_ [%s]" prefix text state.text
1359 else Printf.sprintf "%s%s_" prefix text
1363 | Birdseye _ | View | LinkNav _ -> state.text
1365 let s =
1366 if state.newerrmsgs
1367 then (
1368 if not (istextentry state.mode) && state.uioh#eformsgs
1369 then
1370 let s1 = "(press 'e' to review error messasges)" in
1371 if nonemptystr s then s ^ " " ^ s1 else s1
1372 else s
1374 else s
1376 if nonemptystr s
1377 then drawstring s
1380 let gctiles () =
1381 let len = Queue.length state.tilelru in
1382 let layout = lazy (
1383 match state.throttle with
1384 | None ->
1385 if conf.preload
1386 then preloadlayout state.x state.y state.winw state.winh
1387 else state.layout
1388 | Some (layout, _, _) ->
1389 layout
1390 ) in
1391 let rec loop qpos =
1392 if state.memused > conf.memlimit
1393 then (
1394 if qpos < len
1395 then
1396 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1397 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1398 let (_, pw, ph, _) = getpagedim n in
1399 if gen = state.gen
1400 && colorspace = conf.colorspace
1401 && angle = conf.angle
1402 && pagew = pw
1403 && pageh = ph
1404 && (
1405 let x = col*conf.tilew
1406 and y = row*conf.tileh in
1407 tilevisible (Lazy.force_val layout) n x y
1409 then Queue.push lruitem state.tilelru
1410 else (
1411 freepbo p;
1412 wcmd "freetile %s" (~> p);
1413 state.memused <- state.memused - s;
1414 state.uioh#infochanged Memused;
1415 Hashtbl.remove state.tilemap k;
1417 loop (qpos+1)
1420 loop 0
1423 let onpagerect pageno f =
1424 let b =
1425 match conf.columns with
1426 | Cmulti (_, b) -> b
1427 | Csingle b -> b
1428 | Csplit (_, b) -> b
1430 if pageno >= 0 && pageno < Array.length b
1431 then
1432 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1433 f w h
1436 let gotopagexy1 wtmode pageno x y =
1437 let _,w1,h1,leftx = getpagedim pageno in
1438 let top = y /. (float h1) in
1439 let left = x /. (float w1) in
1440 let py, w, h = getpageywh pageno in
1441 let wh = state.winh in
1442 let x = left *. (float w) in
1443 let x = leftx + state.x + truncate x in
1444 let sx =
1445 if x < 0 || x >= state.winw
1446 then state.x - x
1447 else state.x
1449 let pdy = truncate (top *. float h) in
1450 let y' = py + pdy in
1451 let dy = y' - state.y in
1452 let sy =
1453 if x != state.x || not (dy > 0 && dy < wh)
1454 then (
1455 if conf.presentation
1456 then
1457 if abs (py - y') > wh
1458 then y'
1459 else py
1460 else y';
1462 else state.y
1464 if state.x != sx || state.y != sy
1465 then (
1466 let x, y =
1467 if wtmode
1468 then (
1469 let ww = state.winw in
1470 let qx = sx / ww
1471 and qy = pdy / wh in
1472 let x = qx * ww
1473 and y = py + qy * wh in
1474 let x = if -x + ww > w1 then -(w1-ww) else x
1475 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1476 let y =
1477 if conf.presentation
1478 then
1479 if abs (py - y') > wh
1480 then y'
1481 else py
1482 else y';
1484 (x, y)
1486 else (sx, sy)
1488 gotoxy_and_clear_text x y;
1490 else gotoxy_and_clear_text state.x state.y;
1493 let gotopagexy wtmode pageno x y =
1494 match state.mode with
1495 | Birdseye _ -> gotopage pageno 0.0
1496 | Textentry _ | View | LinkNav _ -> gotopagexy1 wtmode pageno x y
1499 let getpassword () =
1500 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1501 if emptystr passcmd
1502 then E.s
1503 else getcmdoutput
1504 (fun s ->
1505 impmsg "error getting password: %s" s;
1506 dolog "%s" s) passcmd;
1509 let pgoto opaque pageno x y =
1510 let pdimno = getpdimno pageno in
1511 let x, y = project opaque pageno pdimno x y in
1512 gotopagexy false pageno x y;
1515 let act cmds =
1516 (* dolog "%S" cmds; *)
1517 let spl = splitatchar cmds ' ' in
1518 let scan s fmt f =
1519 try Scanf.sscanf s fmt f
1520 with exn ->
1521 dolog "error processing '%S': %s" cmds @@ exntos exn;
1522 exit 1
1524 let addoutline outline =
1525 match state.currently with
1526 | Outlining outlines -> state.currently <- Outlining (outline :: outlines)
1527 | Idle -> state.currently <- Outlining [outline]
1528 | Loading _ | Tiling _ ->
1529 dolog "invalid outlining state";
1530 logcurrently state.currently
1532 match spl with
1533 | "clear", "" ->
1534 state.pdims <- [];
1535 state.uioh#infochanged Pdim;
1537 | "clearrects", "" ->
1538 state.rects <- state.rects1;
1539 G.postRedisplay "clearrects";
1541 | "continue", args ->
1542 let n = scan args "%u" (fun n -> n) in
1543 state.pagecount <- n;
1544 begin match state.currently with
1545 | Outlining l ->
1546 state.currently <- Idle;
1547 state.outlines <- Array.of_list (List.rev l)
1548 | Idle | Loading _ | Tiling _ -> ()
1549 end;
1551 let cur, cmds = state.geomcmds in
1552 if emptystr cur
1553 then failwith "umpossible";
1555 begin match List.rev cmds with
1556 | [] ->
1557 state.geomcmds <- E.s, [];
1558 state.throttle <- None;
1559 represent ();
1560 | (s, f) :: rest ->
1561 f ();
1562 state.geomcmds <- s, List.rev rest;
1563 end;
1564 if conf.maxwait = None && not !wtmode
1565 then G.postRedisplay "continue";
1567 | "msg", args ->
1568 showtext ' ' args
1570 | "vmsg", args ->
1571 if conf.verbose
1572 then showtext ' ' args
1574 | "emsg", args ->
1575 Buffer.add_string state.errmsgs args;
1576 state.newerrmsgs <- true;
1577 G.postRedisplay "error message"
1579 | "progress", args ->
1580 let progress, text =
1581 scan args "%f %n"
1582 (fun f pos ->
1583 f, String.sub args pos (String.length args - pos))
1585 state.text <- text;
1586 state.progress <- progress;
1587 G.postRedisplay "progress"
1589 | "firstmatch", args ->
1590 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1591 scan args "%u %d %f %f %f %f %f %f %f %f"
1592 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1593 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1595 let y = (getpagey pageno) + truncate y0 in
1596 let x =
1597 if conf.zoom > 1.0
1598 then state.winw/2
1599 else state.x
1601 addnav ();
1602 gotoxy x y;
1603 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1604 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1606 | "match", args ->
1607 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1608 scan args "%u %d %f %f %f %f %f %f %f %f"
1609 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1610 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1612 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1613 state.rects1 <-
1614 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1616 | "page", args ->
1617 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1618 let pageopaque = ~< pageopaques in
1619 begin match state.currently with
1620 | Loading (l, gen) ->
1621 vlog "page %d took %f sec" l.pageno t;
1622 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1623 begin match state.throttle with
1624 | None ->
1625 let preloadedpages =
1626 if conf.preload
1627 then preloadlayout state.x state.y state.winw state.winh
1628 else state.layout
1630 let evict () =
1631 let set =
1632 List.fold_left (fun s l -> IntSet.add l.pageno s)
1633 IntSet.empty preloadedpages
1635 let evictedpages =
1636 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1637 if not (IntSet.mem pageno set)
1638 then (
1639 wcmd "freepage %s" (~> opaque);
1640 key :: accu
1642 else accu
1643 ) state.pagemap []
1645 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1647 evict ();
1648 state.currently <- Idle;
1649 if gen = state.gen
1650 then (
1651 tilepage l.pageno pageopaque state.layout;
1652 load state.layout;
1653 load preloadedpages;
1654 let visible = pagevisible state.layout l.pageno in
1655 if visible
1656 then (
1657 match state.mode with
1658 | LinkNav (Ltnotready (pageno, dir)) ->
1659 if pageno = l.pageno
1660 then (
1661 let link =
1662 let ld =
1663 if dir = 0
1664 then LDfirstvisible (l.pagex, l.pagey, dir)
1665 else (
1666 if dir > 0 then LDfirst else LDlast
1669 findlink pageopaque ld
1671 match link with
1672 | Lnotfound -> ()
1673 | Lfound n ->
1674 showlinktype (getlink pageopaque n);
1675 state.mode <- LinkNav (Ltexact (l.pageno, n))
1677 | LinkNav (Ltgendir _)
1678 | LinkNav (Ltexact _)
1679 | View
1680 | Birdseye _
1681 | Textentry _ -> ()
1684 if visible && layoutready state.layout
1685 then (
1686 G.postRedisplay "page";
1690 | Some (layout, _, _) ->
1691 state.currently <- Idle;
1692 tilepage l.pageno pageopaque layout;
1693 load state.layout
1694 end;
1696 | Idle | Tiling _ | Outlining _ ->
1697 dolog "Inconsistent loading state";
1698 logcurrently state.currently;
1699 exit 1
1702 | "tile" , args ->
1703 let (x, y, opaques, size, t) =
1704 scan args "%u %u %s %u %f"
1705 (fun x y p size t -> (x, y, p, size, t))
1707 let opaque = ~< opaques in
1708 begin match state.currently with
1709 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1710 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1712 unmappbo opaque;
1713 if tilew != conf.tilew || tileh != conf.tileh
1714 then (
1715 wcmd "freetile %s" (~> opaque);
1716 state.currently <- Idle;
1717 load state.layout;
1719 else (
1720 puttileopaque l col row gen cs angle opaque size t;
1721 state.memused <- state.memused + size;
1722 state.uioh#infochanged Memused;
1723 gctiles ();
1724 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1725 opaque, size) state.tilelru;
1727 let layout =
1728 match state.throttle with
1729 | None -> state.layout
1730 | Some (layout, _, _) -> layout
1733 state.currently <- Idle;
1734 if gen = state.gen
1735 && conf.colorspace = cs
1736 && conf.angle = angle
1737 && tilevisible layout l.pageno x y
1738 then conttiling l.pageno pageopaque;
1740 begin match state.throttle with
1741 | None ->
1742 preload state.layout;
1743 if gen = state.gen
1744 && conf.colorspace = cs
1745 && conf.angle = angle
1746 && tilevisible state.layout l.pageno x y
1747 && (not !wtmode || layoutready state.layout)
1748 then G.postRedisplay "tile nothrottle";
1750 | Some (layout, y, _) ->
1751 let ready = layoutready layout in
1752 if ready
1753 then (
1754 state.y <- y;
1755 state.layout <- layout;
1756 state.throttle <- None;
1757 G.postRedisplay "throttle";
1759 else load layout;
1760 end;
1763 | Idle | Loading _ | Outlining _ ->
1764 dolog "Inconsistent tiling state";
1765 logcurrently state.currently;
1766 exit 1
1769 | "pdim", args ->
1770 let (n, w, h, _) as pdim =
1771 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1773 let pdim =
1774 match conf.fitmodel with
1775 | FitWidth -> pdim
1776 | FitPage | FitProportional ->
1777 match conf.columns with
1778 | Csplit _ -> (n, w, h, 0)
1779 | Csingle _ | Cmulti _ -> pdim
1781 state.pdims <- pdim :: state.pdims;
1782 state.uioh#infochanged Pdim
1784 | "o", args ->
1785 let (l, n, t, h, pos) =
1786 scan args "%u %u %d %u %n"
1787 (fun l n t h pos -> l, n, t, h, pos)
1789 let s = String.sub args pos (String.length args - pos) in
1790 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1792 | "ou", args ->
1793 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1794 let s = String.sub args pos len in
1795 let pos2 = pos + len + 1 in
1796 let uri = String.sub args pos2 (String.length args - pos2) in
1797 addoutline (s, l, Ouri uri)
1799 | "on", args ->
1800 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1801 let s = String.sub args pos (String.length args - pos) in
1802 addoutline (s, l, Onone)
1804 | "a", args ->
1805 let (n, l, t) =
1806 scan args "%u %d %d" (fun n l t -> n, l, t)
1808 state.reprf <- (fun () -> gotopagexy !wtmode n (float l) (float t))
1810 | "info", args ->
1811 let c, v = splitatchar args '\t' in
1812 let s =
1813 if nonemptystr v
1814 then
1815 if c = "Title"
1816 then (
1817 conf.title <- v;
1818 Wsi.settitle v;
1819 args
1821 else
1822 if let len = String.length c in
1823 len > 6 && ((String.sub c (len-4) 4) = "date")
1824 then (
1825 if String.length v >= 7 && v.[0] = 'D' && v.[1] = ':'
1826 then
1827 let b = Buffer.create 10 in
1828 Printf.bprintf b "%s\t" c;
1829 let sub p l c =
1831 Buffer.add_substring b v p l;
1832 Buffer.add_char b c;
1833 with exn -> Buffer.add_string b @@ exntos exn
1835 sub 2 4 '/';
1836 sub 6 2 '/';
1837 sub 8 2 ' ';
1838 sub 10 2 ':';
1839 sub 12 2 ':';
1840 sub 14 2 ' ';
1841 Buffer.add_char b '[';
1842 Buffer.add_string b v;
1843 Buffer.add_char b ']';
1844 Buffer.contents b
1845 else args
1847 else args
1848 else args
1850 state.docinfo <- (1, s) :: state.docinfo
1852 | "infoend", "" ->
1853 state.docinfo <- List.rev state.docinfo;
1854 state.uioh#infochanged Docinfo
1856 | "pass", args ->
1857 if args = "fail"
1858 then Wsi.settitle "Wrong password";
1859 let password = getpassword () in
1860 if emptystr password
1861 then error "document is password protected"
1862 else opendoc state.path password
1864 | _ ->
1865 error "unknown cmd `%S'" cmds
1868 let onhist cb =
1869 let rc = cb.rc in
1870 let action = function
1871 | HCprev -> cbget cb ~-1
1872 | HCnext -> cbget cb 1
1873 | HCfirst -> cbget cb ~-(cb.rc)
1874 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1875 and cancel () = cb.rc <- rc
1876 in (action, cancel)
1879 let search pattern forward =
1880 match conf.columns with
1881 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1882 | Csingle _ | Cmulti _ ->
1883 if nonemptystr pattern
1884 then
1885 let pn, py =
1886 match state.layout with
1887 | [] -> 0, 0
1888 | l :: _ ->
1889 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1891 wcmd "search %d %d %d %d,%s\000"
1892 (btod conf.icase) pn py (btod forward) pattern;
1895 let [@warning "-4"] intentry text key =
1896 let text =
1897 match key with
1898 | Keys.Ascii ('0'..'9' as c) -> addchar text c
1899 | _ ->
1900 state.text <- "invalid key";
1901 text
1903 TEcont text
1906 let linknact f s =
1907 if nonemptystr s
1908 then (
1909 let n =
1910 let l = String.length s in
1911 let rec loop pos n =
1912 if pos = l
1913 then n
1914 else
1915 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1916 loop (pos+1) (n*26 + m)
1917 in loop 0 0
1919 let rec loop n = function
1920 | [] -> ()
1921 | l :: rest ->
1922 match getopaque l.pageno with
1923 | None -> loop n rest
1924 | Some opaque ->
1925 let m = getlinkcount opaque in
1926 if n < m
1927 then (
1928 let under = getlink opaque n in
1929 f under
1931 else loop (n-m) rest
1933 loop n state.layout;
1937 let [@warning "-4"] linknentry text = function
1938 | Keys.Ascii c ->
1939 let text = addchar text c in
1940 linknact (fun under -> state.text <- undertext under) text;
1941 TEcont text
1942 | _ ->
1943 state.text <- Printf.sprintf "invalid key";
1944 TEcont text
1947 let [@warning "-4"] textentry text = function
1948 | Keys.Ascii c -> TEcont (addchar text c)
1949 | Keys.Code c -> TEcont (text ^ toutf8 c)
1950 | _ -> TEcont text
1953 let reqlayout angle fitmodel =
1954 match state.throttle with
1955 | None ->
1956 if nogeomcmds state.geomcmds
1957 then state.anchor <- getanchor ();
1958 conf.angle <- angle mod 360;
1959 if conf.angle != 0
1960 then (
1961 match state.mode with
1962 | LinkNav _ -> state.mode <- View
1963 | Birdseye _ | Textentry _ | View -> ()
1965 conf.fitmodel <- fitmodel;
1966 invalidate
1967 "reqlayout"
1968 (fun () ->
1969 wcmd "reqlayout %d %d %d"
1970 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
1972 | _ -> ()
1975 let settrim trimmargins trimfuzz =
1976 if nogeomcmds state.geomcmds
1977 then state.anchor <- getanchor ();
1978 conf.trimmargins <- trimmargins;
1979 conf.trimfuzz <- trimfuzz;
1980 let x0, y0, x1, y1 = trimfuzz in
1981 invalidate
1982 "settrim" (fun () ->
1983 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
1984 flushpages ();
1987 let setzoom zoom =
1988 match state.throttle with
1989 | None ->
1990 let zoom = max 0.0001 zoom in
1991 if zoom <> conf.zoom
1992 then (
1993 state.prevzoom <- (conf.zoom, state.x);
1994 conf.zoom <- zoom;
1995 reshape state.winw state.winh;
1996 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
1999 | Some (layout, y, started) ->
2000 let time =
2001 match conf.maxwait with
2002 | None -> 0.0
2003 | Some t -> t
2005 let dt = now () -. started in
2006 if dt > time
2007 then (
2008 state.y <- y;
2009 load layout;
2013 let pivotzoom ?(vw=min state.w state.winw)
2014 ?(vh=min (state.maxy-state.y) state.winh)
2015 ?(x=vw/2) ?(y=vh/2) zoom =
2016 let w = float state.w /. zoom in
2017 let hw = w /. 2.0 in
2018 let ratio = float vh /. float vw in
2019 let hh = hw *. ratio in
2020 let x0 = if zoom < 1.0 then 0.0 else float x -. hw in
2021 let y0 = float y -. hh in
2022 gotoxy (state.x - truncate x0) (state.y + truncate y0);
2023 setzoom zoom;
2026 let pivotzoom ?vw ?vh ?x ?y zoom =
2027 if nogeomcmds state.geomcmds then pivotzoom ?vw ?vh ?x ?y zoom
2030 let setcolumns mode columns coverA coverB =
2031 state.prevcolumns <- Some (conf.columns, conf.zoom);
2032 if columns < 0
2033 then (
2034 if isbirdseye mode
2035 then impmsg "split mode doesn't work in bird's eye"
2036 else (
2037 conf.columns <- Csplit (-columns, E.a);
2038 state.x <- 0;
2039 conf.zoom <- 1.0;
2042 else (
2043 if columns < 2
2044 then (
2045 conf.columns <- Csingle E.a;
2046 state.x <- 0;
2047 setzoom 1.0;
2049 else (
2050 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2051 conf.zoom <- 1.0;
2054 reshape state.winw state.winh;
2057 let resetmstate () =
2058 state.mstate <- Mnone;
2059 Wsi.setcursor Wsi.CURSOR_INHERIT;
2062 let enterbirdseye () =
2063 let zoom = float conf.thumbw /. float state.winw in
2064 let birdseyepageno =
2065 let cy = state.winh / 2 in
2066 let fold = function
2067 | [] -> 0
2068 | l :: rest ->
2069 let rec fold best = function
2070 | [] -> best.pageno
2071 | l :: rest ->
2072 let d = cy - (l.pagedispy + l.pagevh/2)
2073 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2074 if abs d < abs dbest
2075 then fold l rest
2076 else best.pageno
2077 in fold l rest
2079 fold state.layout
2081 state.mode <-
2082 Birdseye (
2083 { conf with zoom = conf.zoom },
2084 state.x, birdseyepageno, -1, getanchor ()
2086 resetmstate ();
2087 conf.zoom <- zoom;
2088 conf.presentation <- false;
2089 conf.interpagespace <- 10;
2090 conf.hlinks <- false;
2091 conf.fitmodel <- FitPage;
2092 state.x <- 0;
2093 conf.maxwait <- None;
2094 conf.columns <- (
2095 match conf.beyecolumns with
2096 | Some c ->
2097 conf.zoom <- 1.0;
2098 Cmulti ((c, 0, 0), E.a)
2099 | None -> Csingle E.a
2101 if conf.verbose
2102 then
2103 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2104 (100.0*.zoom)
2105 else
2106 state.text <- E.s
2108 reshape state.winw state.winh;
2111 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2112 state.mode <- View;
2113 conf.zoom <- c.zoom;
2114 conf.presentation <- c.presentation;
2115 conf.interpagespace <- c.interpagespace;
2116 conf.maxwait <- c.maxwait;
2117 conf.hlinks <- c.hlinks;
2118 conf.fitmodel <- c.fitmodel;
2119 conf.beyecolumns <- (
2120 match conf.columns with
2121 | Cmulti ((c, _, _), _) -> Some c
2122 | Csingle _ -> None
2123 | Csplit _ -> failwith "leaving bird's eye split mode"
2125 conf.columns <- (
2126 match c.columns with
2127 | Cmulti (c, _) -> Cmulti (c, E.a)
2128 | Csingle _ -> Csingle E.a
2129 | Csplit (c, _) -> Csplit (c, E.a)
2131 if conf.verbose
2132 then
2133 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2134 (100.0*.conf.zoom)
2136 reshape state.winw state.winh;
2137 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2138 state.x <- leftx;
2141 let togglebirdseye () =
2142 match state.mode with
2143 | Birdseye vals -> leavebirdseye vals true
2144 | View -> enterbirdseye ()
2145 | Textentry _ | LinkNav _ -> ()
2148 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2149 let pageno = max 0 (pageno - incr) in
2150 let rec loop = function
2151 | [] -> gotopage1 pageno 0
2152 | l :: _ when l.pageno = pageno ->
2153 if l.pagedispy >= 0 && l.pagey = 0
2154 then G.postRedisplay "upbirdseye"
2155 else gotopage1 pageno 0
2156 | _ :: rest -> loop rest
2158 loop state.layout;
2159 state.text <- E.s;
2160 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2163 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2164 let pageno = min (state.pagecount - 1) (pageno + incr) in
2165 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2166 let rec loop = function
2167 | [] ->
2168 let y, h = getpageyh pageno in
2169 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2170 gotoxy state.x (clamp dy)
2171 | l :: _ when l.pageno = pageno ->
2172 if l.pagevh != l.pageh
2173 then gotoxy state.x (clamp (l.pageh - l.pagevh + conf.interpagespace))
2174 else G.postRedisplay "downbirdseye"
2175 | _ :: rest -> loop rest
2177 loop state.layout;
2178 state.text <- E.s;
2181 let [@warning "-4"] optentry mode _ key =
2182 let btos b = if b then "on" else "off" in
2183 match key with
2184 | Keys.Ascii 's' ->
2185 let ondone s =
2186 try conf.scrollstep <- int_of_string s with exn ->
2187 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2189 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2191 | Keys.Ascii 'A' ->
2192 let ondone s =
2194 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2195 if state.autoscroll <> None
2196 then state.autoscroll <- Some conf.autoscrollstep
2197 with exn ->
2198 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2200 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2202 | Keys.Ascii 'C' ->
2203 let ondone s =
2205 let n, a, b = multicolumns_of_string s in
2206 setcolumns mode n a b;
2207 with exn ->
2208 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exn
2210 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2212 | Keys.Ascii 'Z' ->
2213 let ondone s =
2215 let zoom = float (int_of_string s) /. 100.0 in
2216 pivotzoom zoom
2217 with exn ->
2218 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2220 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2222 | Keys.Ascii 't' ->
2223 let ondone s =
2225 conf.thumbw <- bound (int_of_string s) 2 4096;
2226 state.text <-
2227 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2228 begin match mode with
2229 | Birdseye beye ->
2230 leavebirdseye beye false;
2231 enterbirdseye ();
2232 | Textentry _ | View | LinkNav _ -> ();
2234 with exn ->
2235 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2237 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2239 | Keys.Ascii 'R' ->
2240 let ondone s =
2241 match int_of_string s with
2242 | angle -> reqlayout angle conf.fitmodel
2243 | exception exn ->
2244 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2246 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2248 | Keys.Ascii 'i' ->
2249 conf.icase <- not conf.icase;
2250 TEdone ("case insensitive search " ^ (btos conf.icase))
2252 | Keys.Ascii 'p' ->
2253 conf.preload <- not conf.preload;
2254 gotoxy state.x state.y;
2255 TEdone ("preload " ^ (btos conf.preload))
2257 | Keys.Ascii 'v' ->
2258 conf.verbose <- not conf.verbose;
2259 TEdone ("verbose " ^ (btos conf.verbose))
2261 | Keys.Ascii 'd' ->
2262 conf.debug <- not conf.debug;
2263 TEdone ("debug " ^ (btos conf.debug))
2265 | Keys.Ascii 'h' ->
2266 conf.maxhfit <- not conf.maxhfit;
2267 state.maxy <- calcheight ();
2268 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2270 | Keys.Ascii 'c' ->
2271 conf.crophack <- not conf.crophack;
2272 TEdone ("crophack " ^ btos conf.crophack)
2274 | Keys.Ascii 'a' ->
2275 let s =
2276 match conf.maxwait with
2277 | None ->
2278 conf.maxwait <- Some infinity;
2279 "always wait for page to complete"
2280 | Some _ ->
2281 conf.maxwait <- None;
2282 "show placeholder if page is not ready"
2284 TEdone s
2286 | Keys.Ascii 'f' ->
2287 conf.underinfo <- not conf.underinfo;
2288 TEdone ("underinfo " ^ btos conf.underinfo)
2290 | Keys.Ascii 'P' ->
2291 conf.savebmarks <- not conf.savebmarks;
2292 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2294 | Keys.Ascii 'S' ->
2295 let ondone s =
2297 let pageno, py =
2298 match state.layout with
2299 | [] -> 0, 0
2300 | l :: _ ->
2301 l.pageno, l.pagey
2303 conf.interpagespace <- int_of_string s;
2304 docolumns conf.columns;
2305 state.maxy <- calcheight ();
2306 let y = getpagey pageno in
2307 gotoxy state.x (y + py)
2308 with exn ->
2309 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2311 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2313 | Keys.Ascii 'l' ->
2314 let fm =
2315 match conf.fitmodel with
2316 | FitProportional -> FitWidth
2317 | FitWidth | FitPage -> FitProportional
2319 reqlayout conf.angle fm;
2320 TEdone ("proportional display " ^ btos (fm == FitProportional))
2322 | Keys.Ascii 'T' ->
2323 settrim (not conf.trimmargins) conf.trimfuzz;
2324 TEdone ("trim margins " ^ btos conf.trimmargins)
2326 | Keys.Ascii 'I' ->
2327 conf.invert <- not conf.invert;
2328 TEdone ("invert colors " ^ btos conf.invert)
2330 | Keys.Ascii 'x' ->
2331 let ondone s =
2332 cbput state.hists.sel s;
2333 conf.selcmd <- s;
2335 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2336 textentry, ondone, true)
2338 | Keys.Ascii 'M' ->
2339 if conf.pax == None
2340 then conf.pax <- Some (ref (0.0, 0, 0))
2341 else conf.pax <- None;
2342 TEdone ("PAX " ^ btos (conf.pax != None))
2344 | (Keys.Ascii c) ->
2345 state.text <- Printf.sprintf "bad option %d `%c'"
2346 (Char.code c) c;
2347 TEstop
2349 | _ ->
2350 TEcont state.text
2353 class type lvsource =
2354 object
2355 method getitemcount : int
2356 method getitem : int -> (string * int)
2357 method hasaction : int -> bool
2358 method exit : uioh:uioh ->
2359 cancel:bool ->
2360 active:int ->
2361 first:int ->
2362 pan:int ->
2363 uioh option
2364 method getactive : int
2365 method getfirst : int
2366 method getpan : int
2367 method getminfo : (int * int) array
2368 end;;
2370 class virtual lvsourcebase = object
2371 val mutable m_active = 0
2372 val mutable m_first = 0
2373 val mutable m_pan = 0
2374 method getactive = m_active
2375 method getfirst = m_first
2376 method getpan = m_pan
2377 method getminfo : (int * int) array = E.a
2378 end;;
2380 let [@warning "-4"]
2381 textentrykeyboard
2382 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2383 state.text <- E.s;
2384 let enttext te =
2385 state.mode <- Textentry (te, onleave);
2386 enttext ();
2387 G.postRedisplay "textentrykeyboard enttext";
2389 let histaction cmd =
2390 match opthist with
2391 | None -> ()
2392 | Some (action, _) ->
2393 state.mode <-
2394 Textentry (
2395 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2397 G.postRedisplay "textentry histaction"
2399 let open Keys in
2400 let kt = Wsi.kc2kt key in
2401 match kt with
2402 | Backspace ->
2403 if emptystr text && cancelonempty
2404 then (
2405 onleave Cancel;
2406 G.postRedisplay "textentrykeyboard after cancel";
2408 else
2409 let s = withoutlastutf8 text in
2410 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2412 | Enter ->
2413 ondone text;
2414 onleave Confirm;
2415 G.postRedisplay "textentrykeyboard after confirm"
2417 | Up -> histaction HCprev
2418 | Down -> histaction HCnext
2419 | Home -> histaction HCfirst
2420 | End -> histaction HClast
2422 | Escape ->
2423 if emptystr text
2424 then (
2425 begin match opthist with
2426 | None -> ()
2427 | Some (_, onhistcancel) -> onhistcancel ()
2428 end;
2429 onleave Cancel;
2430 state.text <- E.s;
2431 G.postRedisplay "textentrykeyboard after cancel2"
2433 else (
2434 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2437 | Delete -> ()
2439 | Code _ | Ascii _ ->
2440 begin match onkey text kt with
2441 | TEdone text ->
2442 ondone text;
2443 onleave Confirm;
2444 G.postRedisplay "textentrykeyboard after confirm2";
2446 | TEcont text ->
2447 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2449 | TEstop ->
2450 onleave Cancel;
2451 G.postRedisplay "textentrykeyboard after cancel3"
2453 | TEswitch te ->
2454 state.mode <- Textentry (te, onleave);
2455 G.postRedisplay "textentrykeyboard switch";
2457 | _ -> vlog "unhandled key"
2460 let firstof first active =
2461 if first > active || abs (first - active) > fstate.maxrows - 1
2462 then max 0 (active - (fstate.maxrows/2))
2463 else first
2466 let calcfirst first active =
2467 if active > first
2468 then
2469 let rows = active - first in
2470 if rows > fstate.maxrows then active - fstate.maxrows else first
2471 else active
2474 let scrollph y maxy =
2475 let sh = float (maxy + state.winh) /. float state.winh in
2476 let sh = float state.winh /. sh in
2477 let sh = max sh (float conf.scrollh) in
2479 let percent = float y /. float maxy in
2480 let position = (float state.winh -. sh) *. percent in
2482 let position =
2483 if position +. sh > float state.winh
2484 then float state.winh -. sh
2485 else position
2487 position, sh;
2490 let adderrmsg src msg =
2491 Buffer.add_string state.errmsgs msg;
2492 state.newerrmsgs <- true;
2493 G.postRedisplay src
2496 let adderrfmt src fmt =
2497 Format.ksprintf (fun s -> adderrmsg src s) fmt;
2500 let coe s = (s :> uioh);;
2502 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2503 object (self)
2504 val m_pan = source#getpan
2505 val m_first = source#getfirst
2506 val m_active = source#getactive
2507 val m_qsearch = E.s
2508 val m_prev_uioh = state.uioh
2510 method private elemunder y =
2511 if y < 0
2512 then None
2513 else
2514 let n = y / (fstate.fontsize+1) in
2515 if m_first + n < source#getitemcount
2516 then (
2517 if source#hasaction (m_first + n)
2518 then Some (m_first + n)
2519 else None
2521 else None
2523 method display =
2524 Gl.enable `blend;
2525 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2526 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2527 filledrect 0. 0. (float state.winw) (float state.winh);
2528 GlDraw.color (1., 1., 1.);
2529 Gl.enable `texture_2d;
2530 let fs = fstate.fontsize in
2531 let nfs = fs + 1 in
2532 let hw = state.winw/3 in
2533 let ww = fstate.wwidth in
2534 let tabw = 17.0*.ww in
2535 let itemcount = source#getitemcount in
2536 let minfo = source#getminfo in
2537 if conf.leftscroll
2538 then (
2539 GlMat.push ();
2540 GlMat.translate ~x:(float conf.scrollbw) ();
2542 let x0 = 0.0 and x1 = float (state.winw - conf.scrollbw - 1) in
2543 let rec loop row =
2544 if (row - m_first) > fstate.maxrows
2545 then ()
2546 else (
2547 if row >= 0 && row < itemcount
2548 then (
2549 let (s, level) = source#getitem row in
2550 let y = (row - m_first) * nfs in
2551 let x = 5.0 +. (float (level + m_pan)) *. ww in
2552 if helpmode
2553 then GlDraw.color
2554 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2556 if row = m_active
2557 then (
2558 Gl.disable `texture_2d;
2559 let alpha = if source#hasaction row then 0.9 else 0.3 in
2560 GlDraw.color (1., 1., 1.) ~alpha;
2561 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2562 Gl.enable `texture_2d;
2564 let c =
2565 if zebra && row land 1 = 1
2566 then 0.8
2567 else 1.0
2569 GlDraw.color (c,c,c);
2570 let drawtabularstring s =
2571 let drawstr x s =
2572 let x' = truncate (x0 +. x) in
2573 let s1, s2 = splitatchar s '\000' in
2574 if emptystr s2
2575 then drawstring1 fs x' (y+nfs) s
2576 else
2577 let rec e s =
2578 if emptystr s
2579 then s
2580 else
2581 let s' = withoutlastutf8 s in
2582 let s = s' ^ UniSyms.ellipsis in
2583 let w = measurestr fs s in
2584 if float x' +. w +. ww < float (hw + x')
2585 then s
2586 else e s'
2588 let s1 =
2589 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2590 then e s1
2591 else s1
2593 ignore (drawstring1 fs x' (y+nfs) s1);
2594 drawstring1 fs (hw + x') (y+nfs) s2
2596 if trusted
2597 then
2598 let x = if helpmode && row > 0 then x +. ww else x in
2599 let s1, s2 = splitatchar s '\t' in
2600 if nonemptystr s2
2601 then
2602 let nx = drawstr x s1 in
2603 let sw = nx -. x in
2604 let x = x +. (max tabw sw) in
2605 drawstr x s2
2606 else
2607 let len = String.length s - 2 in
2608 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2609 then
2610 let s = String.sub s 2 len in
2611 let x = if not helpmode then x +. ww else x in
2612 GlDraw.color (1.2, 1.2, 1.2);
2613 let vinc = drawstring1 (fs+fs/4)
2614 (truncate (x -. ww)) (y+nfs) s in
2615 GlDraw.color (1., 1., 1.);
2616 vinc +. (float fs *. 0.8)
2617 else
2618 drawstr x s
2619 else
2620 drawstr x s
2622 ignore (drawtabularstring s);
2623 loop (row+1)
2627 loop m_first;
2628 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2629 let xadj = 5.0 in
2630 let rec loop row =
2631 if (row - m_first) <= fstate.maxrows
2632 then
2633 if row >= 0 && row < itemcount
2634 then (
2635 let (s, level) = source#getitem row in
2636 let pos0 = nindex s '\000' in
2637 let y = (row - m_first) * nfs in
2638 let x = float (level + m_pan) *. ww in
2639 let (first, last) = minfo.(row) in
2640 let prefix =
2641 if pos0 > 0 && first > pos0
2642 then String.sub s (pos0+1) (first-pos0-1)
2643 else String.sub s 0 first
2645 let suffix = String.sub s first (last - first) in
2646 let w1 = measurestr fstate.fontsize prefix in
2647 let w2 = measurestr fstate.fontsize suffix in
2648 let x = x +. if conf.leftscroll then xadj else 5.0 in
2649 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2650 let x0 = x +. w1
2651 and y0 = float (y+2) in
2652 let x1 = x0 +. w2
2653 and y1 = float (y+fs+3) in
2654 filledrect x0 y0 x1 y1;
2655 loop (row+1)
2658 Gl.disable `texture_2d;
2659 if Array.length minfo > 0 then loop m_first;
2660 Gl.disable `blend;
2661 if conf.leftscroll
2662 then GlMat.pop ()
2664 method updownlevel incr =
2665 let len = source#getitemcount in
2666 let curlevel =
2667 if m_active >= 0 && m_active < len
2668 then snd (source#getitem m_active)
2669 else -1
2671 let rec flow i =
2672 if i = len then i-1 else if i = -1 then 0 else
2673 let _, l = source#getitem i in
2674 if l != curlevel then i else flow (i+incr)
2676 let active = flow m_active in
2677 let first = calcfirst m_first active in
2678 G.postRedisplay "outline updownlevel";
2679 {< m_active = active; m_first = first >}
2681 method private key1 key mask =
2682 let set1 active first qsearch =
2683 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2685 let search active pattern incr =
2686 let active = if active = -1 then m_first else active in
2687 let dosearch re =
2688 let rec loop n =
2689 if n >= 0 && n < source#getitemcount
2690 then (
2691 let s, _ = source#getitem n in
2692 match Str.search_forward re s 0 with
2693 | exception Not_found -> loop (n + incr)
2694 | _ -> Some n
2696 else None
2698 loop active
2700 let qpat = Str.quote pattern in
2701 match Str.regexp_case_fold qpat with
2702 | s -> dosearch s
2703 | exception exn ->
2704 adderrfmt "listview key1" "regexp_case_fold for `%S' failed: %S\n"
2705 qpat @@ Printexc.to_string exn;
2706 None
2708 let itemcount = source#getitemcount in
2709 let find start incr =
2710 let rec find i =
2711 if i = -1 || i = itemcount
2712 then -1
2713 else (
2714 if source#hasaction i
2715 then i
2716 else find (i + incr)
2719 find start
2721 let set active first =
2722 let first = bound first 0 (itemcount - fstate.maxrows) in
2723 state.text <- E.s;
2724 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2726 let navigate incr =
2727 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2728 let active, first =
2729 let incr1 = if incr > 0 then 1 else -1 in
2730 if isvisible m_first m_active
2731 then
2732 let next =
2733 let next = m_active + incr in
2734 let next =
2735 if next < 0 || next >= itemcount
2736 then -1
2737 else find next incr1
2739 if abs (m_active - next) > fstate.maxrows
2740 then -1
2741 else next
2743 if next = -1
2744 then
2745 let first = m_first + incr in
2746 let first = bound first 0 (itemcount - fstate.maxrows) in
2747 let next =
2748 let next = m_active + incr in
2749 let next = bound next 0 (itemcount - 1) in
2750 find next ~-incr1
2752 let active =
2753 if next = -1
2754 then m_active
2755 else (
2756 if isvisible first next
2757 then next
2758 else m_active
2761 active, first
2762 else
2763 let first = min next m_first in
2764 let first =
2765 if abs (next - first) > fstate.maxrows
2766 then first + incr
2767 else first
2769 next, first
2770 else
2771 let first = m_first + incr in
2772 let first = bound first 0 (itemcount - 1) in
2773 let active =
2774 let next = m_active + incr in
2775 let next = bound next 0 (itemcount - 1) in
2776 let next = find next incr1 in
2777 let active =
2778 if next = -1 || abs (m_active - first) > fstate.maxrows
2779 then (
2780 let active = if m_active = -1 then next else m_active in
2781 active
2783 else next
2785 if isvisible first active
2786 then active
2787 else -1
2789 active, first
2791 G.postRedisplay "listview navigate";
2792 set active first;
2794 let open Keys in
2795 let kt = Wsi.kc2kt key in
2796 match [@warning "-4"] kt with
2797 | Ascii (('r'|'s') as c) when Wsi.withctrl mask ->
2798 let incr = if c = 'r' then -1 else 1 in
2799 let active, first =
2800 match search (m_active + incr) m_qsearch incr with
2801 | None ->
2802 state.text <- m_qsearch ^ " [not found]";
2803 m_active, m_first
2804 | Some active ->
2805 state.text <- m_qsearch;
2806 active, firstof m_first active
2808 G.postRedisplay "listview ctrl-r/s";
2809 set1 active first m_qsearch;
2811 | Insert when Wsi.withctrl mask ->
2812 if m_active >= 0 && m_active < source#getitemcount
2813 then (
2814 let s, _ = source#getitem m_active in
2815 selstring s;
2817 coe self
2819 | Backspace ->
2820 if emptystr m_qsearch
2821 then coe self
2822 else (
2823 let qsearch = withoutlastutf8 m_qsearch in
2824 if emptystr qsearch
2825 then (
2826 state.text <- E.s;
2827 G.postRedisplay "listview empty qsearch";
2828 set1 m_active m_first E.s;
2830 else
2831 let active, first =
2832 match search m_active qsearch ~-1 with
2833 | None ->
2834 state.text <- qsearch ^ " [not found]";
2835 m_active, m_first
2836 | Some active ->
2837 state.text <- qsearch;
2838 active, firstof m_first active
2840 G.postRedisplay "listview backspace qsearch";
2841 set1 active first qsearch
2844 | Ascii _ | Code _ ->
2845 let utf8 =
2846 match [@warning "-4"] kt with
2847 | Ascii c -> String.make 1 c
2848 | Code code -> toutf8 code
2849 | _ -> assert false
2851 let pattern = m_qsearch ^ utf8 in
2852 let active, first =
2853 match search m_active pattern 1 with
2854 | None ->
2855 state.text <- pattern ^ " [not found]";
2856 m_active, m_first
2857 | Some active ->
2858 state.text <- pattern;
2859 active, firstof m_first active
2861 G.postRedisplay "listview qsearch add";
2862 set1 active first pattern;
2864 | Escape ->
2865 state.text <- E.s;
2866 if emptystr m_qsearch
2867 then (
2868 G.postRedisplay "list view escape";
2869 let mx, my = state.mpos in
2870 updateunder mx my;
2871 match source#exit ~uioh:(coe self) ~cancel:true ~active:m_active
2872 ~first:m_first ~pan:m_pan with
2873 | None -> m_prev_uioh
2874 | Some uioh -> uioh
2876 else (
2877 G.postRedisplay "list view kill qsearch";
2878 coe {< m_qsearch = E.s >}
2881 | Enter ->
2882 state.text <- E.s;
2883 let self = {< m_qsearch = E.s >} in
2884 let opt =
2885 G.postRedisplay "listview enter";
2886 let cancel = not (m_active >= 0 && m_active < source#getitemcount) in
2887 source#exit ~uioh:(coe self) ~cancel
2888 ~active:m_active ~first:m_first ~pan:m_pan;
2890 begin match opt with
2891 | None -> m_prev_uioh
2892 | Some uioh -> uioh
2895 | Delete ->
2896 coe self
2898 | Up -> navigate ~-1
2899 | Down -> navigate 1
2900 | Prior -> navigate ~-(fstate.maxrows)
2901 | Next -> navigate fstate.maxrows
2903 | Right ->
2904 state.text <- E.s;
2905 G.postRedisplay "listview right";
2906 coe {< m_pan = m_pan - 1 >}
2908 | Left ->
2909 state.text <- E.s;
2910 G.postRedisplay "listview left";
2911 coe {< m_pan = m_pan + 1 >}
2913 | Home ->
2914 let active = find 0 1 in
2915 G.postRedisplay "listview home";
2916 set active 0;
2918 | End ->
2919 let first = max 0 (itemcount - fstate.maxrows) in
2920 let active = find (itemcount - 1) ~-1 in
2921 G.postRedisplay "listview end";
2922 set active first;
2924 | _ -> coe self
2926 method key key mask =
2927 match state.mode with
2928 | Textentry te ->
2929 textentrykeyboard key mask te;
2930 coe self
2931 | Birdseye _ | View | LinkNav _ -> self#key1 key mask
2933 method button button down x y _ =
2934 let opt =
2935 match button with
2936 | 1 when vscrollhit x ->
2937 G.postRedisplay "listview scroll";
2938 if down
2939 then
2940 let _, position, sh = self#scrollph in
2941 if y > truncate position && y < truncate (position +. sh)
2942 then (
2943 state.mstate <- Mscrolly;
2944 Some (coe self)
2946 else
2947 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
2948 let first = truncate (s *. float source#getitemcount) in
2949 let first = min source#getitemcount first in
2950 Some (coe {< m_first = first; m_active = first >})
2951 else (
2952 state.mstate <- Mnone;
2953 Some (coe self);
2955 | 1 when down ->
2956 begin match self#elemunder y with
2957 | Some n ->
2958 G.postRedisplay "listview click";
2959 source#exit ~uioh:(coe {< m_active = n >})
2960 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
2961 | _ ->
2962 Some (coe self)
2964 | n when (n == 4 || n == 5) && not down ->
2965 let len = source#getitemcount in
2966 let first =
2967 if n = 5 && m_first + fstate.maxrows >= len
2968 then
2969 m_first
2970 else
2971 let first = m_first + (if n == 4 then -1 else 1) in
2972 bound first 0 (len - 1)
2974 G.postRedisplay "listview wheel";
2975 Some (coe {< m_first = first >})
2976 | n when (n = 6 || n = 7) && not down ->
2977 let inc = if n = 7 then -1 else 1 in
2978 G.postRedisplay "listview hwheel";
2979 Some (coe {< m_pan = m_pan + inc >})
2980 | _ ->
2981 Some (coe self)
2983 match opt with
2984 | None -> m_prev_uioh
2985 | Some uioh -> uioh
2987 method multiclick _ x y = self#button 1 true x y
2989 method motion _ y =
2990 match state.mstate with
2991 | Mscrolly ->
2992 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
2993 let first = truncate (s *. float source#getitemcount) in
2994 let first = min source#getitemcount first in
2995 G.postRedisplay "listview motion";
2996 coe {< m_first = first; m_active = first >}
2997 | Msel _
2998 | Mpan _
2999 | Mscrollx
3000 | Mzoom _
3001 | Mzoomrect _
3002 | Mnone -> coe self
3004 method pmotion x y =
3005 if x < state.winw - conf.scrollbw
3006 then
3007 let n =
3008 match self#elemunder y with
3009 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3010 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3012 let o =
3013 if n != m_active
3014 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3015 else self
3017 coe o
3018 else (
3019 Wsi.setcursor Wsi.CURSOR_INHERIT;
3020 coe self
3023 method infochanged _ = ()
3025 method scrollpw = (0, 0.0, 0.0)
3026 method scrollph =
3027 let nfs = fstate.fontsize + 1 in
3028 let y = m_first * nfs in
3029 let itemcount = source#getitemcount in
3030 let maxi = max 0 (itemcount - fstate.maxrows) in
3031 let maxy = maxi * nfs in
3032 let p, h = scrollph y maxy in
3033 conf.scrollbw, p, h
3035 method modehash = modehash
3036 method eformsgs = false
3037 method alwaysscrolly = true
3038 end;;
3040 class outlinelistview ~zebra ~source =
3041 let settext autonarrow s =
3042 if autonarrow
3043 then
3044 let ss = source#statestr in
3045 state.text <-
3046 if emptystr ss
3047 then "[" ^ s ^ "]"
3048 else "{" ^ ss ^ "} [" ^ s ^ "]"
3049 else state.text <- s
3051 object (self)
3052 inherit listview
3053 ~zebra
3054 ~helpmode:false
3055 ~source:(source :> lvsource)
3056 ~trusted:false
3057 ~modehash:(findkeyhash conf "outline")
3058 as super
3060 val m_autonarrow = false
3062 method! key key mask =
3063 let maxrows =
3064 if emptystr state.text
3065 then fstate.maxrows
3066 else fstate.maxrows - 2
3068 let calcfirst first active =
3069 if active > first
3070 then
3071 let rows = active - first in
3072 if rows > maxrows then active - maxrows else first
3073 else active
3075 let navigate incr =
3076 let active = m_active + incr in
3077 let active = bound active 0 (source#getitemcount - 1) in
3078 let first = calcfirst m_first active in
3079 G.postRedisplay "outline navigate";
3080 coe {< m_active = active; m_first = first >}
3082 let navscroll first =
3083 let active =
3084 let dist = m_active - first in
3085 if dist < 0
3086 then first
3087 else (
3088 if dist < maxrows
3089 then m_active
3090 else first + maxrows
3093 G.postRedisplay "outline navscroll";
3094 coe {< m_first = first; m_active = active >}
3096 let ctrl = Wsi.withctrl mask in
3097 let open Keys in
3098 match Wsi.kc2kt key with
3099 | Ascii 'a' when ctrl ->
3100 let text =
3101 if m_autonarrow
3102 then (source#denarrow; E.s)
3103 else (
3104 let pattern = source#renarrow in
3105 if nonemptystr m_qsearch
3106 then (source#narrow m_qsearch; m_qsearch)
3107 else pattern
3110 settext (not m_autonarrow) text;
3111 G.postRedisplay "toggle auto narrowing";
3112 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3114 | Ascii '/' when emptystr m_qsearch && not m_autonarrow ->
3115 settext true E.s;
3116 G.postRedisplay "toggle auto narrowing";
3117 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3119 | Ascii 'n' when ctrl ->
3120 source#narrow m_qsearch;
3121 if not m_autonarrow
3122 then source#add_narrow_pattern m_qsearch;
3123 G.postRedisplay "outline ctrl-n";
3124 coe {< m_first = 0; m_active = 0 >}
3126 | Ascii 'S' when ctrl ->
3127 let active = source#calcactive (getanchor ()) in
3128 let first = firstof m_first active in
3129 G.postRedisplay "outline ctrl-s";
3130 coe {< m_first = first; m_active = active >}
3132 | Ascii 'u' when ctrl ->
3133 G.postRedisplay "outline ctrl-u";
3134 if m_autonarrow && nonemptystr m_qsearch
3135 then (
3136 ignore (source#renarrow);
3137 settext m_autonarrow E.s;
3138 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3140 else (
3141 source#del_narrow_pattern;
3142 let pattern = source#renarrow in
3143 let text =
3144 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3146 settext m_autonarrow text;
3147 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3150 | Ascii 'l' when ctrl ->
3151 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3152 G.postRedisplay "outline ctrl-l";
3153 coe {< m_first = first >}
3155 | Ascii '\t' when m_autonarrow ->
3156 if nonemptystr m_qsearch
3157 then (
3158 G.postRedisplay "outline list view tab";
3159 source#add_narrow_pattern m_qsearch;
3160 settext true E.s;
3161 coe {< m_qsearch = E.s >}
3163 else coe self
3165 | Escape when m_autonarrow ->
3166 if nonemptystr m_qsearch
3167 then source#add_narrow_pattern m_qsearch;
3168 super#key key mask
3170 | Enter when m_autonarrow ->
3171 if nonemptystr m_qsearch
3172 then source#add_narrow_pattern m_qsearch;
3173 super#key key mask
3175 | (Ascii _ | Code _) when m_autonarrow ->
3176 let pattern = m_qsearch ^ toutf8 key in
3177 G.postRedisplay "outlinelistview autonarrow add";
3178 source#narrow pattern;
3179 settext true pattern;
3180 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3182 | Backspace when m_autonarrow ->
3183 if emptystr m_qsearch
3184 then coe self
3185 else
3186 let pattern = withoutlastutf8 m_qsearch in
3187 G.postRedisplay "outlinelistview autonarrow backspace";
3188 ignore (source#renarrow);
3189 source#narrow pattern;
3190 settext true pattern;
3191 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3193 | Up when ctrl ->
3194 navscroll (max 0 (m_first - 1))
3196 | Down when ctrl ->
3197 navscroll (min (source#getitemcount - 1) (m_first + 1))
3199 | Up -> navigate ~-1
3200 | Down -> navigate 1
3201 | Prior -> navigate ~-(fstate.maxrows)
3202 | Next -> navigate fstate.maxrows
3204 | Right ->
3205 let o =
3206 if ctrl
3207 then (
3208 G.postRedisplay "outline ctrl right";
3209 {< m_pan = m_pan + 1 >}
3211 else self#updownlevel 1
3213 coe o
3215 | Left ->
3216 let o =
3217 if ctrl
3218 then (
3219 G.postRedisplay "outline ctrl left";
3220 {< m_pan = m_pan - 1 >}
3222 else self#updownlevel ~-1
3224 coe o
3226 | Home ->
3227 G.postRedisplay "outline home";
3228 coe {< m_first = 0; m_active = 0 >}
3230 | End ->
3231 let active = source#getitemcount - 1 in
3232 let first = max 0 (active - fstate.maxrows) in
3233 G.postRedisplay "outline end";
3234 coe {< m_active = active; m_first = first >}
3236 | Delete|Escape|Insert|Enter|Ascii _|Code _|Ctrl _|Backspace|Fn _ ->
3237 super#key key mask
3238 end;;
3240 let genhistoutlines () =
3241 Config.gethist ()
3242 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
3243 compare c2.lastvisit c1.lastvisit)
3244 |> List.map (fun ((path, c, _, _, _, origin) as hist) ->
3245 let path = if nonemptystr origin then origin else path in
3246 let base = mbtoutf8 @@ Filename.basename path in
3247 (base ^ "\000" ^ c.title, 1, Ohistory hist)
3251 let gotohist (path, c, bookmarks, x, anchor, origin) =
3252 Config.save leavebirdseye;
3253 state.anchor <- anchor;
3254 state.bookmarks <- bookmarks;
3255 state.origin <- origin;
3256 state.x <- x;
3257 setconf conf c;
3258 let x0, y0, x1, y1 = conf.trimfuzz in
3259 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3260 reshape ~firsttime:true state.winw state.winh;
3261 opendoc path origin;
3262 setzoom c.zoom;
3265 let makecheckers () =
3266 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3267 following to say:
3268 converted by Issac Trotts. July 25, 2002 *)
3269 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3270 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3271 let id = GlTex.gen_texture () in
3272 GlTex.bind_texture ~target:`texture_2d id;
3273 GlPix.store (`unpack_alignment 1);
3274 GlTex.image2d image;
3275 List.iter (GlTex.parameter ~target:`texture_2d)
3276 [ `mag_filter `nearest; `min_filter `nearest ];
3280 let setcheckers enabled =
3281 match state.checkerstexid with
3282 | None ->
3283 if enabled then state.checkerstexid <- Some (makecheckers ())
3285 | Some checkerstexid ->
3286 if not enabled
3287 then (
3288 GlTex.delete_texture checkerstexid;
3289 state.checkerstexid <- None;
3293 let describe_location () =
3294 let fn = page_of_y state.y in
3295 let ln = page_of_y (state.y + state.winh - 1) in
3296 let maxy = maxy () in
3297 let percent =
3298 if maxy <= 0
3299 then 100.
3300 else 100. *. (float state.y /. float maxy)
3302 if fn = ln
3303 then
3304 Printf.sprintf "page %d of %d [%.2f%%]"
3305 (fn+1) state.pagecount percent
3306 else
3307 Printf.sprintf
3308 "pages %d-%d of %d [%.2f%%]"
3309 (fn+1) (ln+1) state.pagecount percent
3312 let setpresentationmode v =
3313 let n = page_of_y state.y in
3314 state.anchor <- (n, 0.0, 1.0);
3315 conf.presentation <- v;
3316 if conf.fitmodel = FitPage
3317 then reqlayout conf.angle conf.fitmodel;
3318 represent ();
3321 let setbgcol (r, g, b) =
3322 let col =
3323 let r = r *. 255.0 |> truncate
3324 and g = g *. 255.0 |> truncate
3325 and b = b *. 255.0 |> truncate in
3326 r lsl 16 |> (lor) (g lsl 8) |> (lor) b
3328 Wsi.setwinbgcol col;
3331 let enterinfomode =
3332 let btos b = if b then UniSyms.radical else E.s in
3333 let showextended = ref false in
3334 let leave mode _ = state.mode <- mode in
3335 let src =
3336 (object
3337 val mutable m_l = []
3338 val mutable m_a = E.a
3339 val mutable m_prev_uioh = nouioh
3340 val mutable m_prev_mode = View
3342 inherit lvsourcebase
3344 method reset prev_mode prev_uioh =
3345 m_a <- Array.of_list (List.rev m_l);
3346 m_l <- [];
3347 m_prev_mode <- prev_mode;
3348 m_prev_uioh <- prev_uioh;
3350 method int name get set =
3351 m_l <-
3352 (name, `int get, 1,
3353 Action (
3354 fun u ->
3355 let ondone s =
3356 try set (int_of_string s)
3357 with exn ->
3358 state.text <- Printf.sprintf "bad integer `%s': %s"
3359 s @@ exntos exn
3361 state.text <- E.s;
3362 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3363 state.mode <- Textentry (te, leave m_prev_mode);
3365 )) :: m_l
3367 method int_with_suffix name get set =
3368 m_l <-
3369 (name, `intws get, 1,
3370 Action (
3371 fun u ->
3372 let ondone s =
3373 try set (int_of_string_with_suffix s)
3374 with exn ->
3375 state.text <- Printf.sprintf "bad integer `%s': %s"
3376 s @@ exntos exn
3378 state.text <- E.s;
3379 let te =
3380 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3382 state.mode <- Textentry (te, leave m_prev_mode);
3384 )) :: m_l
3386 method bool ?(offset=1) ?(btos=btos) name get set =
3387 m_l <-
3388 (name, `bool (btos, get), offset, Action (
3389 fun u ->
3390 let v = get () in
3391 set (not v);
3393 )) :: m_l
3395 method color name get set =
3396 m_l <-
3397 (name, `color get, 1,
3398 Action (
3399 fun u ->
3400 let invalid = (nan, nan, nan) in
3401 let ondone s =
3402 let c =
3403 try color_of_string s
3404 with exn ->
3405 state.text <- Printf.sprintf "bad color `%s': %s"
3406 s @@ exntos exn;
3407 invalid
3409 if c <> invalid
3410 then set c;
3412 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3413 state.text <- color_to_string (get ());
3414 state.mode <- Textentry (te, leave m_prev_mode);
3416 )) :: m_l
3418 method string name get set =
3419 m_l <-
3420 (name, `string get, 1,
3421 Action (
3422 fun u ->
3423 let ondone s = set s in
3424 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3425 state.mode <- Textentry (te, leave m_prev_mode);
3427 )) :: m_l
3429 method colorspace name get set =
3430 m_l <-
3431 (name, `string get, 1,
3432 Action (
3433 fun _ ->
3434 let source =
3435 (object
3436 inherit lvsourcebase
3438 initializer
3439 m_active <- CSTE.to_int conf.colorspace;
3440 m_first <- 0;
3442 method getitemcount =
3443 Array.length CSTE.names
3444 method getitem n =
3445 (CSTE.names.(n), 0)
3446 method exit ~uioh ~cancel ~active ~first ~pan =
3447 ignore (uioh, first, pan);
3448 if not cancel then set active;
3449 None
3450 method hasaction _ = true
3451 end)
3453 state.text <- E.s;
3454 let modehash = findkeyhash conf "info" in
3455 coe (new listview ~zebra:false ~helpmode:false
3456 ~source ~trusted:true ~modehash)
3457 )) :: m_l
3459 method paxmark name get set =
3460 m_l <-
3461 (name, `string get, 1,
3462 Action (
3463 fun _ ->
3464 let source =
3465 (object
3466 inherit lvsourcebase
3468 initializer
3469 m_active <- MTE.to_int conf.paxmark;
3470 m_first <- 0;
3472 method getitemcount = Array.length MTE.names
3473 method getitem n = (MTE.names.(n), 0)
3474 method exit ~uioh ~cancel ~active ~first ~pan =
3475 ignore (uioh, first, pan);
3476 if not cancel then set active;
3477 None
3478 method hasaction _ = true
3479 end)
3481 state.text <- E.s;
3482 let modehash = findkeyhash conf "info" in
3483 coe (new listview ~zebra:false ~helpmode:false
3484 ~source ~trusted:true ~modehash)
3485 )) :: m_l
3487 method fitmodel name get set =
3488 m_l <-
3489 (name, `string get, 1,
3490 Action (
3491 fun _ ->
3492 let source =
3493 (object
3494 inherit lvsourcebase
3496 initializer
3497 m_active <- FMTE.to_int conf.fitmodel;
3498 m_first <- 0;
3500 method getitemcount = Array.length FMTE.names
3501 method getitem n = (FMTE.names.(n), 0)
3502 method exit ~uioh ~cancel ~active ~first ~pan =
3503 ignore (uioh, first, pan);
3504 if not cancel then set active;
3505 None
3506 method hasaction _ = true
3507 end)
3509 state.text <- E.s;
3510 let modehash = findkeyhash conf "info" in
3511 coe (new listview ~zebra:false ~helpmode:false
3512 ~source ~trusted:true ~modehash)
3513 )) :: m_l
3515 method caption s offset =
3516 m_l <- (s, `empty, offset, Noaction) :: m_l
3518 method caption2 s f offset =
3519 m_l <- (s, `string f, offset, Noaction) :: m_l
3521 method getitemcount = Array.length m_a
3523 method getitem n =
3524 let tostr = function
3525 | `int f -> string_of_int (f ())
3526 | `intws f -> string_with_suffix_of_int (f ())
3527 | `string f -> f ()
3528 | `color f -> color_to_string (f ())
3529 | `bool (btos, f) -> btos (f ())
3530 | `empty -> E.s
3532 let name, t, offset, _ = m_a.(n) in
3533 ((let s = tostr t in
3534 if nonemptystr s
3535 then Printf.sprintf "%s\t%s" name s
3536 else name),
3537 offset)
3539 method exit ~uioh ~cancel ~active ~first ~pan =
3540 let uiohopt =
3541 if not cancel
3542 then (
3543 let uioh =
3544 match m_a.(active) with
3545 | _, _, _, Action f -> f uioh
3546 | _, _, _, Noaction -> uioh
3548 Some uioh
3550 else None
3552 m_active <- active;
3553 m_first <- first;
3554 m_pan <- pan;
3555 uiohopt
3557 method hasaction n =
3558 match m_a.(n) with
3559 | _, _, _, Action _ -> true
3560 | _, _, _, Noaction -> false
3562 initializer m_active <- 1
3563 end)
3565 let rec fillsrc prevmode prevuioh =
3566 let sep () = src#caption E.s 0 in
3567 let colorp name get set =
3568 src#string name
3569 (fun () -> color_to_string (get ()))
3570 (fun v ->
3572 let c = color_of_string v in
3573 set c
3574 with exn ->
3575 state.text <-
3576 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3579 let oldmode = state.mode in
3580 let birdseye = isbirdseye state.mode in
3582 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3584 src#bool "presentation mode"
3585 (fun () -> conf.presentation)
3586 (fun v -> setpresentationmode v);
3588 src#bool "ignore case in searches"
3589 (fun () -> conf.icase)
3590 (fun v -> conf.icase <- v);
3592 src#bool "preload"
3593 (fun () -> conf.preload)
3594 (fun v -> conf.preload <- v);
3596 src#bool "highlight links"
3597 (fun () -> conf.hlinks)
3598 (fun v -> conf.hlinks <- v);
3600 src#bool "under info"
3601 (fun () -> conf.underinfo)
3602 (fun v -> conf.underinfo <- v);
3604 src#bool "persistent bookmarks"
3605 (fun () -> conf.savebmarks)
3606 (fun v -> conf.savebmarks <- v);
3608 src#fitmodel "fit model"
3609 (fun () -> FMTE.to_string conf.fitmodel)
3610 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3612 src#bool "trim margins"
3613 (fun () -> conf.trimmargins)
3614 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3616 src#bool "persistent location"
3617 (fun () -> conf.jumpback)
3618 (fun v -> conf.jumpback <- v);
3620 sep ();
3621 src#int "inter-page space"
3622 (fun () -> conf.interpagespace)
3623 (fun n ->
3624 conf.interpagespace <- n;
3625 docolumns conf.columns;
3626 let pageno, py =
3627 match state.layout with
3628 | [] -> 0, 0
3629 | l :: _ ->
3630 l.pageno, l.pagey
3632 state.maxy <- calcheight ();
3633 let y = getpagey pageno in
3634 gotoxy state.x (y + py)
3637 src#int "page bias"
3638 (fun () -> conf.pagebias)
3639 (fun v -> conf.pagebias <- v);
3641 src#int "scroll step"
3642 (fun () -> conf.scrollstep)
3643 (fun n -> conf.scrollstep <- n);
3645 src#int "horizontal scroll step"
3646 (fun () -> conf.hscrollstep)
3647 (fun v -> conf.hscrollstep <- v);
3649 src#int "auto scroll step"
3650 (fun () ->
3651 match state.autoscroll with
3652 | Some step -> step
3653 | _ -> conf.autoscrollstep)
3654 (fun n ->
3655 let n = boundastep state.winh n in
3656 if state.autoscroll <> None
3657 then state.autoscroll <- Some n;
3658 conf.autoscrollstep <- n);
3660 src#int "zoom"
3661 (fun () -> truncate (conf.zoom *. 100.))
3662 (fun v -> pivotzoom ((float v) /. 100.));
3664 src#int "rotation"
3665 (fun () -> conf.angle)
3666 (fun v -> reqlayout v conf.fitmodel);
3668 src#int "scroll bar width"
3669 (fun () -> conf.scrollbw)
3670 (fun v ->
3671 conf.scrollbw <- v;
3672 reshape state.winw state.winh;
3675 src#int "scroll handle height"
3676 (fun () -> conf.scrollh)
3677 (fun v -> conf.scrollh <- v;);
3679 src#int "thumbnail width"
3680 (fun () -> conf.thumbw)
3681 (fun v ->
3682 conf.thumbw <- min 4096 v;
3683 match oldmode with
3684 | Birdseye beye ->
3685 leavebirdseye beye false;
3686 enterbirdseye ()
3687 | Textentry _
3688 | View
3689 | LinkNav _ -> ()
3692 let mode = state.mode in
3693 src#string "columns"
3694 (fun () ->
3695 match conf.columns with
3696 | Csingle _ -> "1"
3697 | Cmulti (multi, _) -> multicolumns_to_string multi
3698 | Csplit (count, _) -> "-" ^ string_of_int count
3700 (fun v ->
3701 let n, a, b = multicolumns_of_string v in
3702 setcolumns mode n a b);
3704 sep ();
3705 src#caption "Pixmap cache" 0;
3706 src#int_with_suffix "size (advisory)"
3707 (fun () -> conf.memlimit)
3708 (fun v -> conf.memlimit <- v);
3710 src#caption2 "used"
3711 (fun () ->
3712 Printf.sprintf "%s bytes, %d tiles"
3713 (string_with_suffix_of_int state.memused)
3714 (Hashtbl.length state.tilemap)) 1;
3716 sep ();
3717 src#caption "Layout" 0;
3718 src#caption2 "Dimension"
3719 (fun () ->
3720 Printf.sprintf "%dx%d (virtual %dx%d)"
3721 state.winw state.winh
3722 state.w state.maxy)
3724 if conf.debug
3725 then
3726 src#caption2 "Position" (fun () ->
3727 Printf.sprintf "%dx%d" state.x state.y
3729 else
3730 src#caption2 "Position" (fun () -> describe_location ()) 1
3733 sep ();
3734 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3735 "Save these parameters as global defaults at exit"
3736 (fun () -> conf.bedefault)
3737 (fun v -> conf.bedefault <- v)
3740 sep ();
3741 let btos b = if b then UniSyms.lguillemet else UniSyms.rguillemet in
3742 src#bool ~offset:0 ~btos "Extended parameters"
3743 (fun () -> !showextended)
3744 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3745 if !showextended
3746 then (
3747 src#bool "checkers"
3748 (fun () -> conf.checkers)
3749 (fun v -> conf.checkers <- v; setcheckers v);
3750 src#bool "update cursor"
3751 (fun () -> conf.updatecurs)
3752 (fun v -> conf.updatecurs <- v);
3753 src#bool "scroll-bar on the left"
3754 (fun () -> conf.leftscroll)
3755 (fun v -> conf.leftscroll <- v);
3756 src#bool "verbose"
3757 (fun () -> conf.verbose)
3758 (fun v -> conf.verbose <- v);
3759 src#bool "invert colors"
3760 (fun () -> conf.invert)
3761 (fun v -> conf.invert <- v);
3762 src#bool "max fit"
3763 (fun () -> conf.maxhfit)
3764 (fun v -> conf.maxhfit <- v);
3765 src#bool "pax mode"
3766 (fun () -> conf.pax != None)
3767 (fun v ->
3768 if v
3769 then conf.pax <- Some (ref (now (), 0, 0))
3770 else conf.pax <- None);
3771 src#string "uri launcher"
3772 (fun () -> conf.urilauncher)
3773 (fun v -> conf.urilauncher <- v);
3774 src#string "path launcher"
3775 (fun () -> conf.pathlauncher)
3776 (fun v -> conf.pathlauncher <- v);
3777 src#string "tile size"
3778 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3779 (fun v ->
3781 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3782 conf.tilew <- max 64 w;
3783 conf.tileh <- max 64 h;
3784 flushtiles ();
3785 with exn ->
3786 state.text <- Printf.sprintf "bad tile size `%s': %s"
3787 v @@ exntos exn
3789 src#int "texture count"
3790 (fun () -> conf.texcount)
3791 (fun v ->
3792 if realloctexts v
3793 then conf.texcount <- v
3794 else impmsg "failed to set texture count please retry later"
3796 src#int "slice height"
3797 (fun () -> conf.sliceheight)
3798 (fun v ->
3799 conf.sliceheight <- v;
3800 wcmd "sliceh %d" conf.sliceheight;
3802 src#int "anti-aliasing level"
3803 (fun () -> conf.aalevel)
3804 (fun v ->
3805 conf.aalevel <- bound v 0 8;
3806 state.anchor <- getanchor ();
3807 opendoc state.path state.password;
3809 src#string "page scroll scaling factor"
3810 (fun () -> string_of_float conf.pgscale)
3811 (fun v ->
3813 let s = float_of_string v in
3814 conf.pgscale <- s
3815 with exn ->
3816 state.text <- Printf.sprintf
3817 "bad page scroll scaling factor `%s': %s" v
3818 @@ exntos exn
3821 src#int "ui font size"
3822 (fun () -> fstate.fontsize)
3823 (fun v -> setfontsize (bound v 5 100));
3824 src#int "hint font size"
3825 (fun () -> conf.hfsize)
3826 (fun v -> conf.hfsize <- bound v 5 100);
3827 colorp "background color"
3828 (fun () -> conf.bgcolor)
3829 (fun v -> conf.bgcolor <- v; setbgcol v);
3830 src#bool "crop hack"
3831 (fun () -> conf.crophack)
3832 (fun v -> conf.crophack <- v);
3833 src#string "trim fuzz"
3834 (fun () -> irect_to_string conf.trimfuzz)
3835 (fun v ->
3837 conf.trimfuzz <- irect_of_string v;
3838 if conf.trimmargins
3839 then settrim true conf.trimfuzz;
3840 with exn ->
3841 state.text <- Printf.sprintf "bad irect `%s': %s" v
3842 @@ exntos exn
3844 src#string "throttle"
3845 (fun () ->
3846 match conf.maxwait with
3847 | None -> "show place holder if page is not ready"
3848 | Some time ->
3849 if time = infinity
3850 then "wait for page to fully render"
3851 else
3852 "wait " ^ string_of_float time
3853 ^ " seconds before showing placeholder"
3855 (fun v ->
3857 let f = float_of_string v in
3858 if f <= 0.0
3859 then conf.maxwait <- None
3860 else conf.maxwait <- Some f
3861 with exn ->
3862 state.text <- Printf.sprintf "bad time `%s': %s" v
3863 @@ exntos exn
3865 src#string "ghyll scroll"
3866 (fun () ->
3867 match conf.ghyllscroll with
3868 | None -> E.s
3869 | Some nab -> ghyllscroll_to_string nab
3871 (fun v ->
3872 try conf.ghyllscroll <- ghyllscroll_of_string v
3873 with
3874 | Failure msg ->
3875 state.text <- Printf.sprintf "bad ghyll `%s': %s" v msg
3876 | exn ->
3877 state.text <- Printf.sprintf "bad ghyll `%s': %s" v
3878 @@ exntos exn
3880 src#string "selection command"
3881 (fun () -> conf.selcmd)
3882 (fun v -> conf.selcmd <- v);
3883 src#string "synctex command"
3884 (fun () -> conf.stcmd)
3885 (fun v -> conf.stcmd <- v);
3886 src#string "pax command"
3887 (fun () -> conf.paxcmd)
3888 (fun v -> conf.paxcmd <- v);
3889 src#string "ask password command"
3890 (fun () -> conf.passcmd)
3891 (fun v -> conf.passcmd <- v);
3892 src#string "save path command"
3893 (fun () -> conf.savecmd)
3894 (fun v -> conf.savecmd <- v);
3895 src#colorspace "color space"
3896 (fun () -> CSTE.to_string conf.colorspace)
3897 (fun v ->
3898 conf.colorspace <- CSTE.of_int v;
3899 wcmd "cs %d" v;
3900 load state.layout;
3902 src#paxmark "pax mark method"
3903 (fun () -> MTE.to_string conf.paxmark)
3904 (fun v -> conf.paxmark <- MTE.of_int v);
3905 if bousable () && !opengl_has_pbo
3906 then
3907 src#bool "use PBO"
3908 (fun () -> conf.usepbo)
3909 (fun v -> conf.usepbo <- v);
3910 src#bool "mouse wheel scrolls pages"
3911 (fun () -> conf.wheelbypage)
3912 (fun v -> conf.wheelbypage <- v);
3913 src#bool "open remote links in a new instance"
3914 (fun () -> conf.riani)
3915 (fun v -> conf.riani <- v);
3916 src#bool "edit annotations inline"
3917 (fun () -> conf.annotinline)
3918 (fun v -> conf.annotinline <- v);
3919 src#bool "coarse positioning in presentation mode"
3920 (fun () -> conf.coarseprespos)
3921 (fun v -> conf.coarseprespos <- v);
3922 src#bool "use document CSS"
3923 (fun () -> conf.usedoccss)
3924 (fun v ->
3925 conf.usedoccss <- v;
3926 state.anchor <- getanchor ();
3927 opendoc state.path state.password;
3931 sep ();
3932 src#caption "Document" 0;
3933 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3934 src#caption2 "Pages"
3935 (fun () -> string_of_int state.pagecount) 1;
3936 src#caption2 "Dimensions"
3937 (fun () -> string_of_int (List.length state.pdims)) 1;
3938 if nonemptystr conf.css
3939 then src#caption2 "CSS" (fun () -> conf.css) 1;
3940 if conf.trimmargins
3941 then (
3942 sep ();
3943 src#caption "Trimmed margins" 0;
3944 src#caption2 "Dimensions"
3945 (fun () -> string_of_int (List.length state.pdims)) 1;
3948 sep ();
3949 src#caption "OpenGL" 0;
3950 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
3951 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
3953 sep ();
3954 src#caption "Location" 0;
3955 if nonemptystr state.origin
3956 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
3957 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
3959 src#reset prevmode prevuioh;
3961 fun () ->
3962 state.text <- E.s;
3963 resetmstate ();
3964 let prevmode = state.mode
3965 and prevuioh = state.uioh in
3966 fillsrc prevmode prevuioh;
3967 let source = (src :> lvsource) in
3968 let modehash = findkeyhash conf "info" in
3969 state.uioh <-
3970 coe (object (self)
3971 inherit listview ~zebra:false ~helpmode:false
3972 ~source ~trusted:true ~modehash as super
3973 val mutable m_prevmemused = 0
3974 method! infochanged = function
3975 | Memused ->
3976 if m_prevmemused != state.memused
3977 then (
3978 m_prevmemused <- state.memused;
3979 G.postRedisplay "memusedchanged";
3981 | Pdim -> G.postRedisplay "pdimchanged"
3982 | Docinfo -> fillsrc prevmode prevuioh
3984 method! key key mask =
3985 if not (Wsi.withctrl mask)
3986 then
3987 match [@warning "-4"] Wsi.kc2kt key with
3988 | Keys.Left -> coe (self#updownlevel ~-1)
3989 | Keys.Right -> coe (self#updownlevel 1)
3990 | _ -> super#key key mask
3991 else super#key key mask
3992 end);
3993 G.postRedisplay "info";
3996 let enterhelpmode =
3997 let source =
3998 (object
3999 inherit lvsourcebase
4000 method getitemcount = Array.length state.help
4001 method getitem n =
4002 let s, l, _ = state.help.(n) in
4003 (s, l)
4005 method exit ~uioh ~cancel ~active ~first ~pan =
4006 let optuioh =
4007 if not cancel
4008 then (
4009 match state.help.(active) with
4010 | _, _, Action f -> Some (f uioh)
4011 | _, _, Noaction -> Some uioh
4013 else None
4015 m_active <- active;
4016 m_first <- first;
4017 m_pan <- pan;
4018 optuioh
4020 method hasaction n =
4021 match state.help.(n) with
4022 | _, _, Action _ -> true
4023 | _, _, Noaction -> false
4025 initializer
4026 m_active <- -1
4027 end)
4028 in fun () ->
4029 let modehash = findkeyhash conf "help" in
4030 resetmstate ();
4031 state.uioh <- coe (new listview
4032 ~zebra:false ~helpmode:true
4033 ~source ~trusted:true ~modehash);
4034 G.postRedisplay "help";
4037 let entermsgsmode =
4038 let msgsource =
4039 (object
4040 inherit lvsourcebase
4041 val mutable m_items = E.a
4043 method getitemcount = 1 + Array.length m_items
4045 method getitem n =
4046 if n = 0
4047 then "[Clear]", 0
4048 else m_items.(n-1), 0
4050 method exit ~uioh ~cancel ~active ~first ~pan =
4051 ignore uioh;
4052 if not cancel
4053 then (
4054 if active = 0
4055 then Buffer.clear state.errmsgs;
4057 m_active <- active;
4058 m_first <- first;
4059 m_pan <- pan;
4060 None
4062 method hasaction n =
4063 n = 0
4065 method reset =
4066 state.newerrmsgs <- false;
4067 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
4068 m_items <- Array.of_list l
4070 initializer
4071 m_active <- 0
4072 end)
4073 in fun () ->
4074 state.text <- E.s;
4075 resetmstate ();
4076 msgsource#reset;
4077 let source = (msgsource :> lvsource) in
4078 let modehash = findkeyhash conf "listview" in
4079 state.uioh <-
4080 coe (object
4081 inherit listview ~zebra:false ~helpmode:false
4082 ~source ~trusted:false ~modehash as super
4083 method! display =
4084 if state.newerrmsgs
4085 then msgsource#reset;
4086 super#display
4087 end);
4088 G.postRedisplay "msgs";
4091 let getusertext s =
4092 let editor = getenvwithdef "EDITOR" E.s in
4093 if emptystr editor
4094 then E.s
4095 else
4096 let tmppath = Filename.temp_file "llpp" "note" in
4097 if nonemptystr s
4098 then (
4099 let oc = open_out tmppath in
4100 output_string oc s;
4101 close_out oc;
4103 let execstr = editor ^ " " ^ tmppath in
4104 let s =
4105 match spawn execstr [] with
4106 | exception exn ->
4107 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
4109 | pid ->
4110 match Unix.waitpid [] pid with
4111 | exception exn ->
4112 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
4114 | (_pid, status) ->
4115 match status with
4116 | Unix.WEXITED 0 -> filecontents tmppath
4117 | Unix.WEXITED n ->
4118 impmsg "editor process(%s) exited abnormally: %d" execstr n;
4120 | Unix.WSIGNALED n ->
4121 impmsg "editor process(%s) was killed by signal %d" execstr n;
4123 | Unix.WSTOPPED n ->
4124 impmsg "editor(%s) process was stopped by signal %d" execstr n;
4127 match Unix.unlink tmppath with
4128 | exception exn ->
4129 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
4131 | () -> s
4134 let enterannotmode opaque slinkindex =
4135 let msgsource =
4136 (object
4137 inherit lvsourcebase
4138 val mutable m_text = E.s
4139 val mutable m_items = E.a
4141 method getitemcount = Array.length m_items
4143 method getitem n =
4144 let label, _func = m_items.(n) in
4145 label, 0
4147 method exit ~uioh ~cancel ~active ~first ~pan =
4148 ignore (uioh, first, pan);
4149 if not cancel
4150 then (
4151 let _label, func = m_items.(active) in
4152 func ()
4154 None
4156 method hasaction n = nonemptystr @@ fst m_items.(n)
4158 method reset s =
4159 let rec split accu b i =
4160 let p = b+i in
4161 if p = String.length s
4162 then (String.sub s b (p-b), unit) :: accu
4163 else
4164 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4165 then
4166 let ss = if i = 0 then E.s else String.sub s b i in
4167 split ((ss, unit)::accu) (p+1) 0
4168 else
4169 split accu b (i+1)
4171 let cleanup () =
4172 wcmd "freepage %s" (~> opaque);
4173 let keys =
4174 Hashtbl.fold (fun key opaque' accu ->
4175 if opaque' = opaque'
4176 then key :: accu else accu) state.pagemap []
4178 List.iter (Hashtbl.remove state.pagemap) keys;
4179 flushtiles ();
4180 gotoxy state.x state.y
4182 let dele () =
4183 delannot opaque slinkindex;
4184 cleanup ();
4186 let edit inline () =
4187 let update s =
4188 if emptystr s
4189 then dele ()
4190 else (
4191 modannot opaque slinkindex s;
4192 cleanup ();
4195 if inline
4196 then
4197 let mode = state.mode in
4198 state.mode <-
4199 Textentry (
4200 ("annotation: ", m_text, None, textentry, update, true),
4201 fun _ -> state.mode <- mode);
4202 state.text <- E.s;
4203 enttext ();
4204 else
4205 let s = getusertext m_text in
4206 update s
4208 m_text <- s;
4209 m_items <-
4210 ( "[Copy]", fun () -> selstring m_text)
4211 :: ("[Delete]", dele)
4212 :: ("[Edit]", edit conf.annotinline)
4213 :: (E.s, unit)
4214 :: split [] 0 0 |> List.rev |> Array.of_list
4216 initializer
4217 m_active <- 0
4218 end)
4220 state.text <- E.s;
4221 let s = getannotcontents opaque slinkindex in
4222 resetmstate ();
4223 msgsource#reset s;
4224 let source = (msgsource :> lvsource) in
4225 let modehash = findkeyhash conf "listview" in
4226 state.uioh <- coe (object
4227 inherit listview ~zebra:false ~helpmode:false
4228 ~source ~trusted:false ~modehash
4229 end);
4230 G.postRedisplay "enterannotmode";
4233 let gotoremote spec =
4234 let filename, dest = splitatchar spec '#' in
4235 let getpath filename =
4236 let path =
4237 if nonemptystr filename
4238 then
4239 if Filename.is_relative filename
4240 then
4241 let dir = Filename.dirname state.path in
4242 let dir =
4243 if Filename.is_implicit dir
4244 then Filename.concat (Sys.getcwd ()) dir
4245 else dir
4247 Filename.concat dir filename
4248 else filename
4249 else E.s
4251 if Sys.file_exists path
4252 then path
4253 else E.s
4255 let path = getpath filename in
4256 let dospawn lcmd =
4257 if conf.riani
4258 then
4259 let cmd = Lazy.force_val lcmd in
4260 match spawn cmd with
4261 | _pid -> ()
4262 | exception exn -> dolog "failed to execute `%s': %s" cmd @@ exntos exn
4263 else
4264 let anchor = getanchor () in
4265 let ranchor = state.path, state.password, anchor, state.origin in
4266 state.origin <- E.s;
4267 state.ranchors <- ranchor :: state.ranchors;
4268 opendoc path E.s;
4270 if substratis spec 0 "page="
4271 then
4272 match Scanf.sscanf spec "page=%d" (fun n -> n) with
4273 | pageno ->
4274 state.anchor <- (pageno, 0.0, 0.0);
4275 dospawn @@ lazy (Printf.sprintf "%s -page %d %S" !selfexec pageno path);
4276 | exception exn ->
4277 adderrfmt "error parsing remote destination" "page: %s" @@ exntos exn
4278 else (
4279 state.nameddest <- dest;
4280 dospawn @@ lazy (!selfexec ^ " " ^ path ^ " -dest " ^ dest)
4284 let gotounder = function
4285 | Ulinkuri s when isexternallink s ->
4286 if substratis s 0 "file://"
4287 then gotoremote @@ String.sub s 7 (String.length s - 7)
4288 else gotouri s
4289 | Ulinkuri s ->
4290 let pageno, x, y = uritolocation s in
4291 addnav ();
4292 gotopagexy !wtmode pageno x y
4293 | Utext _ | Unone -> ()
4294 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4297 let gotooutline (_, _, kind) =
4298 match kind with
4299 | Onone -> ()
4300 | Oanchor anchor ->
4301 let (pageno, y, _) = anchor in
4302 let y = getanchory
4303 (if conf.presentation then (pageno, y, 1.0) else anchor)
4305 addnav ();
4306 gotoghyll y
4307 | Ouri uri -> gotounder (Ulinkuri uri)
4308 | Olaunch _cmd -> failwith "gotounder (Ulaunch cmd)"
4309 | Oremote _remote -> failwith "gotounder (Uremote remote)"
4310 | Ohistory hist -> gotohist hist
4311 | Oremotedest _remotedest -> failwith "gotounder (Uremotedest remotedest)"
4314 class outlinesoucebase fetchoutlines = object (self)
4315 inherit lvsourcebase
4316 val mutable m_items = E.a
4317 val mutable m_minfo = E.a
4318 val mutable m_orig_items = E.a
4319 val mutable m_orig_minfo = E.a
4320 val mutable m_narrow_patterns = []
4321 val mutable m_gen = -1
4323 method getitemcount = Array.length m_items
4325 method getitem n =
4326 let s, n, _ = m_items.(n) in
4327 (s, n+0)
4329 method exit ~(uioh:uioh) ~cancel ~active ~(first:int) ~pan : uioh option =
4330 ignore (uioh, first);
4331 let items, minfo =
4332 if m_narrow_patterns = []
4333 then m_orig_items, m_orig_minfo
4334 else m_items, m_minfo
4336 m_pan <- pan;
4337 if not cancel
4338 then (
4339 m_items <- items;
4340 m_minfo <- minfo;
4341 gotooutline m_items.(active);
4343 else (
4344 m_items <- items;
4345 m_minfo <- minfo;
4347 None
4349 method hasaction (_:int) = true
4351 method greetmsg =
4352 if Array.length m_items != Array.length m_orig_items
4353 then
4354 let s =
4355 match m_narrow_patterns with
4356 | one :: [] -> one
4357 | many -> String.concat UniSyms.ellipsis (List.rev many)
4359 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4360 else E.s
4362 method statestr =
4363 match m_narrow_patterns with
4364 | [] -> E.s
4365 | one :: [] -> one
4366 | head :: _ -> UniSyms.ellipsis ^ head
4368 method narrow pattern =
4369 match Str.regexp_case_fold pattern with
4370 | exception _ -> ()
4371 | re ->
4372 let rec loop accu minfo n =
4373 if n = -1
4374 then (
4375 m_items <- Array.of_list accu;
4376 m_minfo <- Array.of_list minfo;
4378 else
4379 let (s, _, _) as o = m_items.(n) in
4380 let accu, minfo =
4381 match Str.search_forward re s 0 with
4382 | exception Not_found -> accu, minfo
4383 | first -> o :: accu, (first, Str.match_end ()) :: minfo
4385 loop accu minfo (n-1)
4387 loop [] [] (Array.length m_items - 1)
4389 method! getminfo = m_minfo
4391 method denarrow =
4392 m_orig_items <- fetchoutlines ();
4393 m_minfo <- m_orig_minfo;
4394 m_items <- m_orig_items
4396 method add_narrow_pattern pattern =
4397 m_narrow_patterns <- pattern :: m_narrow_patterns
4399 method del_narrow_pattern =
4400 match m_narrow_patterns with
4401 | _ :: rest -> m_narrow_patterns <- rest
4402 | [] -> ()
4404 method renarrow =
4405 self#denarrow;
4406 match m_narrow_patterns with
4407 | pattern :: [] -> self#narrow pattern; pattern
4408 | list ->
4409 List.fold_left (fun accu pattern ->
4410 self#narrow pattern;
4411 pattern ^ UniSyms.ellipsis ^ accu) E.s list
4413 method calcactive (_:anchor) = 0
4415 method reset anchor items =
4416 if state.gen != m_gen
4417 then (
4418 m_orig_items <- items;
4419 m_items <- items;
4420 m_narrow_patterns <- [];
4421 m_minfo <- E.a;
4422 m_orig_minfo <- E.a;
4423 m_gen <- state.gen;
4425 else (
4426 if items != m_orig_items
4427 then (
4428 m_orig_items <- items;
4429 if m_narrow_patterns == []
4430 then m_items <- items;
4433 let active = self#calcactive anchor in
4434 m_active <- active;
4435 m_first <- firstof m_first active
4439 let outlinesource fetchoutlines =
4440 (object
4441 inherit outlinesoucebase fetchoutlines
4442 method! calcactive anchor =
4443 let rely = getanchory anchor in
4444 let rec loop n best bestd =
4445 if n = Array.length m_items
4446 then best
4447 else
4448 let _, _, kind = m_items.(n) in
4449 match kind with
4450 | Oanchor anchor ->
4451 let orely = getanchory anchor in
4452 let d = abs (orely - rely) in
4453 if d < bestd
4454 then loop (n+1) n d
4455 else loop (n+1) best bestd
4456 | Onone | Oremote _ | Olaunch _
4457 | Oremotedest _ | Ouri _ | Ohistory _ ->
4458 loop (n+1) best bestd
4460 loop 0 ~-1 max_int
4461 end)
4464 let enteroutlinemode, enterbookmarkmode, enterhistmode =
4465 let mkselector sourcetype =
4466 let fetchoutlines () =
4467 match sourcetype with
4468 | `bookmarks -> Array.of_list state.bookmarks
4469 | `outlines -> state.outlines
4470 | `history -> genhistoutlines () |> Array.of_list
4472 let source =
4473 if sourcetype = `history
4474 then new outlinesoucebase fetchoutlines
4475 else outlinesource fetchoutlines
4477 (fun errmsg ->
4478 let outlines = fetchoutlines () in
4479 if Array.length outlines = 0
4480 then showtext ' ' errmsg
4481 else (
4482 resetmstate ();
4483 Wsi.setcursor Wsi.CURSOR_INHERIT;
4484 let anchor = getanchor () in
4485 source#reset anchor outlines;
4486 state.text <- source#greetmsg;
4487 state.uioh <-
4488 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4489 G.postRedisplay "enter selector";
4493 let mkenter sourcetype errmsg =
4494 let enter = mkselector sourcetype in
4495 fun () -> enter errmsg
4497 ( mkenter `outlines "document has no outline"
4498 , mkenter `bookmarks "document has no bookmarks (yet)"
4499 , mkenter `history "history is empty" )
4502 let quickbookmark ?title () =
4503 match state.layout with
4504 | [] -> ()
4505 | l :: _ ->
4506 let title =
4507 match title with
4508 | None ->
4509 Unix.(
4510 let tm = localtime (now ()) in
4511 Printf.sprintf
4512 "Quick (page %d) (bookmarked on %02d/%02d/%d at %02d:%02d)"
4513 (l.pageno+1)
4514 tm.tm_mday (tm.tm_mon+1) (tm.tm_year+1900) tm.tm_hour tm.tm_min
4516 | Some title -> title
4518 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4521 let setautoscrollspeed step goingdown =
4522 let incr = max 1 ((abs step) / 2) in
4523 let incr = if goingdown then incr else -incr in
4524 let astep = boundastep state.winh (step + incr) in
4525 state.autoscroll <- Some astep;
4528 let canpan () =
4529 match conf.columns with
4530 | Csplit _ -> true
4531 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4534 let panbound x = bound x (-state.w) state.winw;;
4536 let existsinrow pageno (columns, coverA, coverB) p =
4537 let last = ((pageno - coverA) mod columns) + columns in
4538 let rec any = function
4539 | [] -> false
4540 | l :: rest ->
4541 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4542 then p l
4543 else (
4544 if not (p l)
4545 then (if l.pageno = last then false else any rest)
4546 else true
4549 any state.layout
4552 let nextpage () =
4553 match state.layout with
4554 | [] ->
4555 let pageno = page_of_y state.y in
4556 gotoghyll (getpagey (pageno+1))
4557 | l :: rest ->
4558 match conf.columns with
4559 | Csingle _ ->
4560 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4561 then
4562 let y = clamp (pgscale state.winh) in
4563 gotoghyll y
4564 else
4565 let pageno = min (l.pageno+1) (state.pagecount-1) in
4566 gotoghyll (getpagey pageno)
4567 | Cmulti ((c, _, _) as cl, _) ->
4568 if conf.presentation
4569 && (existsinrow l.pageno cl
4570 (fun l -> l.pageh > l.pagey + l.pagevh))
4571 then
4572 let y = clamp (pgscale state.winh) in
4573 gotoghyll y
4574 else
4575 let pageno = min (l.pageno+c) (state.pagecount-1) in
4576 gotoghyll (getpagey pageno)
4577 | Csplit (n, _) ->
4578 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4579 then
4580 let pagey, pageh = getpageyh l.pageno in
4581 let pagey = pagey + pageh * l.pagecol in
4582 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4583 gotoghyll (pagey + pageh + ips)
4586 let prevpage () =
4587 match state.layout with
4588 | [] ->
4589 let pageno = page_of_y state.y in
4590 gotoghyll (getpagey (pageno-1))
4591 | l :: _ ->
4592 match conf.columns with
4593 | Csingle _ ->
4594 if conf.presentation && l.pagey != 0
4595 then
4596 gotoghyll (clamp (pgscale ~-(state.winh)))
4597 else
4598 let pageno = max 0 (l.pageno-1) in
4599 gotoghyll (getpagey pageno)
4600 | Cmulti ((c, _, coverB) as cl, _) ->
4601 if conf.presentation &&
4602 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4603 then
4604 gotoghyll (clamp (pgscale ~-(state.winh)))
4605 else
4606 let decr =
4607 if l.pageno = state.pagecount - coverB
4608 then 1
4609 else c
4611 let pageno = max 0 (l.pageno-decr) in
4612 gotoghyll (getpagey pageno)
4613 | Csplit (n, _) ->
4614 let y =
4615 if l.pagecol = 0
4616 then
4617 if l.pageno = 0
4618 then l.pagey
4619 else
4620 let pageno = max 0 (l.pageno-1) in
4621 let pagey, pageh = getpageyh pageno in
4622 pagey + (n-1)*pageh
4623 else
4624 let pagey, pageh = getpageyh l.pageno in
4625 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4627 gotoghyll y
4630 let save () =
4631 if emptystr conf.savecmd
4632 then adderrmsg "savepath-command is empty"
4633 "don't know where to save modified document"
4634 else
4635 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
4636 let path =
4637 getcmdoutput
4638 (fun exn ->
4639 adderrfmt savecmd "failed to produce path to the saved copy: %s" exn)
4640 savecmd
4642 if nonemptystr path
4643 then
4644 let tmp = path ^ ".tmp" in
4645 savedoc tmp;
4646 Unix.rename tmp path;
4649 let viewkeyboard key mask =
4650 let enttext te =
4651 let mode = state.mode in
4652 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4653 state.text <- E.s;
4654 enttext ();
4655 G.postRedisplay "view:enttext"
4657 let ctrl = Wsi.withctrl mask in
4658 let open Keys in
4659 match Wsi.kc2kt key with
4660 | Ascii 'Q' -> exit 0
4662 | Ascii 'W' ->
4663 if hasunsavedchanges ()
4664 then save ()
4666 | Insert ->
4667 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4668 then (
4669 state.mode <- (
4670 match state.lnava with
4671 | None -> LinkNav (Ltgendir 0)
4672 | Some pn -> LinkNav (Ltexact pn)
4674 gotoxy state.x state.y;
4676 else impmsg "keyboard link navigation does not work under rotation"
4678 | Escape | Ascii 'q' ->
4679 begin match state.mstate with
4680 | Mzoomrect _ ->
4681 resetmstate ();
4682 G.postRedisplay "kill rect";
4683 | Msel _
4684 | Mpan _
4685 | Mscrolly | Mscrollx
4686 | Mzoom _
4687 | Mnone ->
4688 begin match state.mode with
4689 | LinkNav ln ->
4690 begin match ln with
4691 | Ltexact pl -> state.lnava <- Some pl
4692 | Ltgendir _ | Ltnotready _ -> state.lnava <- None
4693 end;
4694 state.mode <- View;
4695 G.postRedisplay "esc leave linknav"
4696 | Birdseye _ | Textentry _ | View ->
4697 match state.ranchors with
4698 | [] -> raise Quit
4699 | (path, password, anchor, origin) :: rest ->
4700 state.ranchors <- rest;
4701 state.anchor <- anchor;
4702 state.origin <- origin;
4703 state.nameddest <- E.s;
4704 opendoc path password
4705 end;
4706 end;
4708 | Backspace ->
4709 gotoghyll (getnav ~-1)
4711 | Ascii 'o' ->
4712 enteroutlinemode ()
4714 | Ascii 'H' ->
4715 enterhistmode ()
4717 | Ascii 'u' ->
4718 state.rects <- [];
4719 state.text <- E.s;
4720 Hashtbl.iter (fun _ opaque ->
4721 clearmark opaque;
4722 Hashtbl.clear state.prects) state.pagemap;
4723 G.postRedisplay "dehighlight";
4725 | (Ascii '/' | Ascii '?') as pv ->
4726 let ondone isforw s =
4727 cbput state.hists.pat s;
4728 state.searchpattern <- s;
4729 search s isforw
4731 let s = String.make 1 (Char.chr key) in
4732 enttext (s, E.s, Some (onhist state.hists.pat),
4733 textentry, ondone (pv = Ascii '/'), true)
4735 | Ascii '+' | Ascii '=' when ctrl ->
4736 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4737 pivotzoom (conf.zoom +. incr)
4739 | Ascii '+' ->
4740 let ondone s =
4741 let n =
4742 try int_of_string s with exn ->
4743 state.text <-
4744 Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4745 max_int
4747 if n != max_int
4748 then (
4749 conf.pagebias <- n;
4750 state.text <- "page bias is now " ^ string_of_int n;
4753 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4755 | Ascii '-' when ctrl ->
4756 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4757 pivotzoom (max 0.01 (conf.zoom -. decr))
4759 | Ascii '-' ->
4760 let ondone msg = state.text <- msg in
4761 enttext (
4762 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4763 optentry state.mode, ondone, true
4766 | Ascii '0' when ctrl ->
4767 if conf.zoom = 1.0
4768 then gotoxy 0 state.y
4769 else setzoom 1.0
4771 | Ascii ('1'|'2' as c) when ctrl && conf.fitmodel != FitPage ->
4772 let cols =
4773 match conf.columns with
4774 | Csingle _ | Cmulti _ -> 1
4775 | Csplit (n, _) -> n
4777 let h = state.winh -
4778 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4780 let zoom = zoomforh state.winw h 0 cols in
4781 if zoom > 0.0 && (c = '2' || zoom < 1.0)
4782 then setzoom zoom
4784 | Ascii '3' when ctrl ->
4785 let fm =
4786 match conf.fitmodel with
4787 | FitWidth -> FitProportional
4788 | FitProportional -> FitPage
4789 | FitPage -> FitWidth
4791 state.text <- "fit model: " ^ FMTE.to_string fm;
4792 reqlayout conf.angle fm
4794 | Ascii '4' when ctrl ->
4795 let zoom = getmaxw () /. float state.winw in
4796 if zoom > 0.0 then setzoom zoom
4798 | Fn 9 ->
4799 togglebirdseye ()
4801 | Ascii '9' when ctrl ->
4802 togglebirdseye ()
4804 | Ascii ('0'..'9') when not ctrl ->
4805 let ondone s =
4806 let n =
4807 try int_of_string s with exn ->
4808 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4811 if n >= 0
4812 then (
4813 addnav ();
4814 cbput state.hists.pag (string_of_int n);
4815 gotopage1 (n + conf.pagebias - 1) 0;
4818 let [@warning "-4"] pageentry text = function
4819 | Keys.Ascii 'g' -> TEdone text
4820 | key -> intentry text key
4822 let text = String.make 1 (Char.chr key) in
4823 enttext (":", text, Some (onhist state.hists.pag),
4824 pageentry, ondone, true)
4826 | Ascii 'b' ->
4827 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4828 G.postRedisplay "toggle scrollbar";
4830 | Ascii 'B' ->
4831 state.bzoom <- not state.bzoom;
4832 state.rects <- [];
4833 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4835 | Ascii 'l' ->
4836 conf.hlinks <- not conf.hlinks;
4837 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4838 G.postRedisplay "toggle highlightlinks";
4840 | Ascii 'F' ->
4841 if conf.angle mod 360 = 0
4842 then (
4843 state.glinks <- true;
4844 let mode = state.mode in
4845 state.mode <-
4846 Textentry (
4847 (":", E.s, None, linknentry, linknact gotounder, false),
4848 (fun _ ->
4849 state.glinks <- false;
4850 state.mode <- mode)
4852 state.text <- E.s;
4853 G.postRedisplay "view:linkent(F)"
4855 else impmsg "hint mode does not work under rotation"
4857 | Ascii 'y' ->
4858 state.glinks <- true;
4859 let mode = state.mode in
4860 state.mode <-
4861 Textentry (
4862 (":", E.s, None, linknentry,
4863 linknact (fun under -> selstring (undertext under)), false),
4864 (fun _ ->
4865 state.glinks <- false;
4866 state.mode <- mode)
4868 state.text <- E.s;
4869 G.postRedisplay "view:linkent"
4871 | Ascii 'a' ->
4872 begin match state.autoscroll with
4873 | Some step ->
4874 conf.autoscrollstep <- step;
4875 state.autoscroll <- None
4876 | None ->
4877 if conf.autoscrollstep = 0
4878 then state.autoscroll <- Some 1
4879 else state.autoscroll <- Some conf.autoscrollstep
4882 | Ascii 'p' when ctrl ->
4883 launchpath () (* XXX where do error messages go? *)
4885 | Ascii 'P' ->
4886 setpresentationmode (not conf.presentation);
4887 showtext ' ' ("presentation mode " ^
4888 if conf.presentation then "on" else "off");
4890 | Ascii 'f' ->
4891 if List.mem Wsi.Fullscreen state.winstate
4892 then Wsi.reshape conf.cwinw conf.cwinh
4893 else Wsi.fullscreen ()
4895 | Ascii ('p'|'N') ->
4896 search state.searchpattern false
4898 | Ascii 'n' | Fn 3 ->
4899 search state.searchpattern true
4901 | Ascii 't' ->
4902 begin match state.layout with
4903 | [] -> ()
4904 | l :: _ ->
4905 gotoghyll (getpagey l.pageno)
4908 | Ascii ' ' ->
4909 nextpage ()
4911 | Delete ->
4912 prevpage ()
4914 | Ascii '=' ->
4915 showtext ' ' (describe_location ());
4917 | Ascii 'w' ->
4918 begin match state.layout with
4919 | [] -> ()
4920 | l :: _ ->
4921 Wsi.reshape l.pagew l.pageh;
4922 G.postRedisplay "w"
4925 | Ascii '\'' ->
4926 enterbookmarkmode ()
4928 | Ascii 'h' | Fn 1 ->
4929 enterhelpmode ()
4931 | Ascii 'i' ->
4932 enterinfomode ()
4934 | Ascii 'e' when Buffer.length state.errmsgs > 0 ->
4935 entermsgsmode ()
4937 | Ascii 'm' ->
4938 let ondone s =
4939 match state.layout with
4940 | l :: _ ->
4941 if nonemptystr s
4942 then
4943 state.bookmarks <-
4944 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4945 | _ -> ()
4947 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
4949 | Ascii '~' ->
4950 quickbookmark ();
4951 showtext ' ' "Quick bookmark added";
4953 | Ascii 'z' ->
4954 begin match state.layout with
4955 | l :: _ ->
4956 let rect = getpdimrect l.pagedimno in
4957 let w, h =
4958 if conf.crophack
4959 then
4960 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4961 truncate (1.2 *. (rect.(3) -. rect.(0))))
4962 else
4963 (truncate (rect.(1) -. rect.(0)),
4964 truncate (rect.(3) -. rect.(0)))
4966 let w = truncate ((float w)*.conf.zoom)
4967 and h = truncate ((float h)*.conf.zoom) in
4968 if w != 0 && h != 0
4969 then (
4970 state.anchor <- getanchor ();
4971 Wsi.reshape w (h + conf.interpagespace)
4973 G.postRedisplay "z";
4975 | [] -> ()
4978 | Ascii 'x' -> state.roam ()
4980 | Ascii ('<'|'>' as c) ->
4981 reqlayout
4982 (conf.angle + (if c = '>' then 30 else -30)) conf.fitmodel
4984 | Ascii ('['|']' as c) ->
4985 conf.colorscale <-
4986 bound (conf.colorscale +. (if c = '>' then 0.1 else -0.1)) 0.0 1.0;
4987 G.postRedisplay "brightness";
4989 | Ascii 'c' when state.mode = View ->
4990 if Wsi.withalt mask
4991 then (
4992 if conf.zoom > 1.0
4993 then
4994 let m = (state.winw - state.w) / 2 in
4995 gotoxy_and_clear_text m state.y
4997 else
4998 let (c, a, b), z =
4999 match state.prevcolumns with
5000 | None -> (1, 0, 0), 1.0
5001 | Some (columns, z) ->
5002 let cab =
5003 match columns with
5004 | Csplit (c, _) -> -c, 0, 0
5005 | Cmulti ((c, a, b), _) -> c, a, b
5006 | Csingle _ -> 1, 0, 0
5008 cab, z
5010 setcolumns View c a b;
5011 setzoom z
5013 | Down | Up when ctrl && Wsi.withshift mask ->
5014 let zoom, x = state.prevzoom in
5015 setzoom zoom;
5016 state.x <- x;
5018 | Ascii 'k' | Up ->
5019 begin match state.autoscroll with
5020 | None ->
5021 begin match state.mode with
5022 | Birdseye beye -> upbirdseye 1 beye
5023 | Textentry _ | View | LinkNav _ ->
5024 if ctrl
5025 then gotoxy_and_clear_text state.x (clamp ~-(state.winh/2))
5026 else (
5027 if not (Wsi.withshift mask) && conf.presentation
5028 then prevpage ()
5029 else gotoghyll1 true (clamp (-conf.scrollstep))
5032 | Some n ->
5033 setautoscrollspeed n false
5036 | Ascii 'j' | Down ->
5037 begin match state.autoscroll with
5038 | None ->
5039 begin match state.mode with
5040 | Birdseye beye -> downbirdseye 1 beye
5041 | Textentry _ | View | LinkNav _ ->
5042 if ctrl
5043 then gotoxy_and_clear_text state.x (clamp (state.winh/2))
5044 else (
5045 if not (Wsi.withshift mask) && conf.presentation
5046 then nextpage ()
5047 else gotoghyll1 true (clamp (conf.scrollstep))
5050 | Some n ->
5051 setautoscrollspeed n true
5054 | Left | Right when not (Wsi.withalt mask) ->
5055 if canpan ()
5056 then
5057 let dx =
5058 if ctrl
5059 then state.winw / 2
5060 else conf.hscrollstep
5062 let dx =
5063 let pv = Wsi.kc2kt key in
5064 if pv = Keys.Left then dx else -dx
5066 gotoxy_and_clear_text (panbound (state.x + dx)) state.y
5067 else (
5068 state.text <- E.s;
5069 G.postRedisplay "left/right"
5072 | Prior ->
5073 let y =
5074 if ctrl
5075 then
5076 match state.layout with
5077 | [] -> state.y
5078 | l :: _ -> state.y - l.pagey
5079 else
5080 clamp (pgscale (-state.winh))
5082 gotoghyll y
5084 | Next ->
5085 let y =
5086 if ctrl
5087 then
5088 match List.rev state.layout with
5089 | [] -> state.y
5090 | l :: _ -> getpagey l.pageno
5091 else
5092 clamp (pgscale state.winh)
5094 gotoghyll y
5096 | Ascii 'g' | Home ->
5097 addnav ();
5098 gotoghyll 0
5099 | Ascii 'G' | End ->
5100 addnav ();
5101 gotoghyll (clamp state.maxy)
5103 | Right when Wsi.withalt mask ->
5104 gotoghyll (getnav 1)
5105 | Left when Wsi.withalt mask ->
5106 gotoghyll (getnav ~-1)
5108 | Ascii 'r' ->
5109 reload ()
5111 | Ascii 'v' when conf.debug ->
5112 state.rects <- [];
5113 List.iter (fun l ->
5114 match getopaque l.pageno with
5115 | None -> ()
5116 | Some opaque ->
5117 let x0, y0, x1, y1 = pagebbox opaque in
5118 let rect = (float x0, float y0,
5119 float x1, float y0,
5120 float x1, float y1,
5121 float x0, float y1) in
5122 debugrect rect;
5123 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
5124 state.rects <- (l.pageno, color, rect) :: state.rects;
5125 ) state.layout;
5126 G.postRedisplay "v";
5128 | Ascii '|' ->
5129 let mode = state.mode in
5130 let cmd = ref E.s in
5131 let onleave = function
5132 | Cancel -> state.mode <- mode
5133 | Confirm ->
5134 List.iter (fun l ->
5135 match getopaque l.pageno with
5136 | Some opaque -> pipesel opaque !cmd
5137 | None -> ()) state.layout;
5138 state.mode <- mode
5140 let ondone s =
5141 cbput state.hists.sel s;
5142 cmd := s
5144 let te =
5145 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5147 G.postRedisplay "|";
5148 state.mode <- Textentry (te, onleave);
5150 | (Ascii _|Fn _|Enter|Left|Right|Code _|Ctrl _) ->
5151 vlog "huh? %s" (Wsi.keyname key)
5154 let linknavkeyboard key mask linknav =
5155 let pv = Wsi.kc2kt key in
5156 let getpage pageno =
5157 let rec loop = function
5158 | [] -> None
5159 | l :: _ when l.pageno = pageno -> Some l
5160 | _ :: rest -> loop rest
5161 in loop state.layout
5163 let doexact (pageno, n) =
5164 match getopaque pageno, getpage pageno with
5165 | Some opaque, Some l ->
5166 if pv = Keys.Enter
5167 then
5168 let under = getlink opaque n in
5169 G.postRedisplay "link gotounder";
5170 gotounder under;
5171 state.mode <- View;
5172 else
5173 let opt, dir =
5174 let open Keys in
5175 match pv with
5176 | Home -> Some (findlink opaque LDfirst), -1
5177 | End -> Some (findlink opaque LDlast), 1
5178 | Left -> Some (findlink opaque (LDleft n)), -1
5179 | Right -> Some (findlink opaque (LDright n)), 1
5180 | Up -> Some (findlink opaque (LDup n)), -1
5181 | Down -> Some (findlink opaque (LDdown n)), 1
5183 | Delete|Escape|Insert|Enter|Next|Prior|Ascii _
5184 | Code _|Fn _|Ctrl _|Backspace -> None, 0
5186 let pwl l dir =
5187 begin match findpwl l.pageno dir with
5188 | Pwlnotfound -> ()
5189 | Pwl pageno ->
5190 let notfound dir =
5191 state.mode <- LinkNav (Ltgendir dir);
5192 let y, h = getpageyh pageno in
5193 let y =
5194 if dir < 0
5195 then y + h - state.winh
5196 else y
5198 gotoxy state.x y
5200 begin match getopaque pageno, getpage pageno with
5201 | Some opaque, Some _ ->
5202 let link =
5203 let ld = if dir > 0 then LDfirst else LDlast in
5204 findlink opaque ld
5206 begin match link with
5207 | Lfound m ->
5208 showlinktype (getlink opaque m);
5209 state.mode <- LinkNav (Ltexact (pageno, m));
5210 G.postRedisplay "linknav jpage";
5211 | Lnotfound -> notfound dir
5212 end;
5213 | _ -> notfound dir
5214 end;
5215 end;
5217 begin match opt with
5218 | Some Lnotfound -> pwl l dir;
5219 | Some (Lfound m) ->
5220 if m = n
5221 then pwl l dir
5222 else (
5223 let _, y0, _, y1 = getlinkrect opaque m in
5224 if y0 < l.pagey
5225 then gotopage1 l.pageno y0
5226 else (
5227 let d = fstate.fontsize + 1 in
5228 if y1 - l.pagey > l.pagevh - d
5229 then gotopage1 l.pageno (y1 - state.winh + d)
5230 else G.postRedisplay "linknav";
5232 showlinktype (getlink opaque m);
5233 state.mode <- LinkNav (Ltexact (l.pageno, m));
5236 | None -> viewkeyboard key mask
5237 end;
5238 | _ -> viewkeyboard key mask
5240 if pv = Keys.Insert
5241 then (
5242 begin match linknav with
5243 | Ltexact pa -> state.lnava <- Some pa
5244 | Ltgendir _ | Ltnotready _ -> ()
5245 end;
5246 state.mode <- View;
5247 G.postRedisplay "leave linknav"
5249 else
5250 match linknav with
5251 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5252 | Ltexact exact -> doexact exact
5255 let keyboard key mask =
5256 if (key = Char.code 'g' && Wsi.withctrl mask) && not (istextentry state.mode)
5257 then wcmd "interrupt"
5258 else state.uioh <- state.uioh#key key mask
5261 let birdseyekeyboard key mask
5262 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5263 let incr =
5264 match conf.columns with
5265 | Csingle _ -> 1
5266 | Cmulti ((c, _, _), _) -> c
5267 | Csplit _ -> failwith "bird's eye split mode"
5269 let pgh layout = List.fold_left
5270 (fun m l -> max l.pageh m) state.winh layout in
5271 let open Keys in
5272 match Wsi.kc2kt key with
5273 | Ascii 'l' when Wsi.withctrl mask ->
5274 let y, h = getpageyh pageno in
5275 let top = (state.winh - h) / 2 in
5276 gotoxy state.x (max 0 (y - top))
5277 | Enter -> leavebirdseye beye false
5278 | Escape -> leavebirdseye beye true
5279 | Up -> upbirdseye incr beye
5280 | Down -> downbirdseye incr beye
5281 | Left -> upbirdseye 1 beye
5282 | Right -> downbirdseye 1 beye
5284 | Prior ->
5285 begin match state.layout with
5286 | l :: _ ->
5287 if l.pagey != 0
5288 then (
5289 state.mode <- Birdseye (
5290 oconf, leftx, l.pageno, hooverpageno, anchor
5292 gotopage1 l.pageno 0;
5294 else (
5295 let layout = layout state.x (state.y-state.winh)
5296 state.winw
5297 (pgh state.layout) in
5298 match layout with
5299 | [] -> gotoxy state.x (clamp (-state.winh))
5300 | l :: _ ->
5301 state.mode <- Birdseye (
5302 oconf, leftx, l.pageno, hooverpageno, anchor
5304 gotopage1 l.pageno 0
5307 | [] -> gotoxy state.x (clamp (-state.winh))
5308 end;
5310 | Next ->
5311 begin match List.rev state.layout with
5312 | l :: _ ->
5313 let layout = layout state.x
5314 (state.y + (pgh state.layout))
5315 state.winw state.winh in
5316 begin match layout with
5317 | [] ->
5318 let incr = l.pageh - l.pagevh in
5319 if incr = 0
5320 then (
5321 state.mode <-
5322 Birdseye (
5323 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5325 G.postRedisplay "birdseye pagedown";
5327 else gotoxy state.x (clamp (incr + conf.interpagespace*2));
5329 | l :: _ ->
5330 state.mode <-
5331 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5332 gotopage1 l.pageno 0;
5335 | [] -> gotoxy state.x (clamp state.winh)
5336 end;
5338 | Home ->
5339 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5340 gotopage1 0 0
5342 | End ->
5343 let pageno = state.pagecount - 1 in
5344 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5345 if not (pagevisible state.layout pageno)
5346 then
5347 let h =
5348 match List.rev state.pdims with
5349 | [] -> state.winh
5350 | (_, _, h, _) :: _ -> h
5352 gotoxy
5353 state.x
5354 (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5355 else G.postRedisplay "birdseye end";
5357 | Delete|Insert|Ascii _|Code _|Ctrl _|Fn _|Backspace -> viewkeyboard key mask
5360 let drawpage l =
5361 let color =
5362 match state.mode with
5363 | Textentry _ -> scalecolor 0.4
5364 | LinkNav _ | View -> scalecolor 1.0
5365 | Birdseye (_, _, pageno, hooverpageno, _) ->
5366 if l.pageno = hooverpageno
5367 then scalecolor 0.9
5368 else (
5369 if l.pageno = pageno
5370 then (
5371 let c = scalecolor 1.0 in
5372 GlDraw.color c;
5373 GlDraw.line_width 3.0;
5374 let dispx = l.pagedispx in
5375 linerect
5376 (float (dispx-1)) (float (l.pagedispy-1))
5377 (float (dispx+l.pagevw+1))
5378 (float (l.pagedispy+l.pagevh+1))
5380 GlDraw.line_width 1.0;
5383 else scalecolor 0.8
5386 drawtiles l color;
5389 let postdrawpage l linkindexbase =
5390 match getopaque l.pageno with
5391 | Some opaque ->
5392 if tileready l l.pagex l.pagey
5393 then
5394 let x = l.pagedispx - l.pagex
5395 and y = l.pagedispy - l.pagey in
5396 let hlmask =
5397 match conf.columns with
5398 | Csingle _ | Cmulti _ ->
5399 (if conf.hlinks then 1 else 0)
5400 + (if state.glinks
5401 && not (isbirdseye state.mode) then 2 else 0)
5402 | Csplit _ -> 0
5404 let s =
5405 match state.mode with
5406 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5407 | Textentry _
5408 | Birdseye _
5409 | View
5410 | LinkNav _ -> E.s
5412 Hashtbl.find_all state.prects l.pageno |>
5413 List.iter (fun vals -> drawprect opaque x y vals);
5414 let n = postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize) in
5415 if n < 0
5416 then (state.redisplay <- true; 0)
5417 else n
5418 else 0
5419 | _ -> 0
5422 let scrollindicator () =
5423 let sbw, ph, sh = state.uioh#scrollph in
5424 let sbh, pw, sw = state.uioh#scrollpw in
5426 let x0,x1,hx0 =
5427 if conf.leftscroll
5428 then (0, sbw, sbw)
5429 else ((state.winw - sbw), state.winw, 0)
5432 Gl.enable `blend;
5433 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5434 GlDraw.color (0.64, 0.64, 0.64) ~alpha:0.7;
5435 filledrect (float x0) 0. (float x1) (float state.winh);
5436 filledrect
5437 (float hx0) (float (state.winh - sbh))
5438 (float (hx0 + state.winw)) (float state.winh)
5440 GlDraw.color (0.0, 0.0, 0.0) ~alpha:0.7;
5442 filledrect (float x0) ph (float x1) (ph +. sh);
5443 let pw = pw +. float hx0 in
5444 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5445 Gl.disable `blend;
5448 let showsel () =
5449 match state.mstate with
5450 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5453 | Msel ((x0, y0), (x1, y1)) ->
5454 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5455 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5456 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5457 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5460 let showrects =
5461 function [] -> ()
5462 | rects ->
5463 Gl.enable `blend;
5464 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5465 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5466 List.iter
5467 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5468 List.iter (fun l ->
5469 if l.pageno = pageno
5470 then (
5471 let dx = float (l.pagedispx - l.pagex) in
5472 let dy = float (l.pagedispy - l.pagey) in
5473 let r, g, b, alpha = c in
5474 GlDraw.color (r, g, b) ~alpha;
5475 filledrect2 (x0+.dx) (y0+.dy)
5476 (x1+.dx) (y1+.dy)
5477 (x3+.dx) (y3+.dy)
5478 (x2+.dx) (y2+.dy);
5480 ) state.layout
5481 ) rects
5483 Gl.disable `blend;
5486 let display () =
5487 GlDraw.color (scalecolor2 conf.bgcolor);
5488 GlClear.color (scalecolor2 conf.bgcolor);
5489 GlClear.clear [`color];
5490 List.iter drawpage state.layout;
5491 let rects =
5492 match state.mode with
5493 | LinkNav (Ltexact (pageno, linkno)) ->
5494 begin match getopaque pageno with
5495 | Some opaque ->
5496 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5497 let color = (0.0, 0.0, 0.5, 0.5) in
5498 (pageno, color, (
5499 float x0, float y0,
5500 float x1, float y0,
5501 float x1, float y1,
5502 float x0, float y1)
5503 ) :: state.rects
5504 | None -> state.rects
5506 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5507 | Birdseye _
5508 | Textentry _
5509 | View -> state.rects
5511 showrects rects;
5512 let rec postloop linkindexbase = function
5513 | l :: rest ->
5514 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5515 postloop linkindexbase rest
5516 | [] -> ()
5518 showsel ();
5519 postloop 0 state.layout;
5520 state.uioh#display;
5521 begin match state.mstate with
5522 | Mzoomrect ((x0, y0), (x1, y1)) ->
5523 Gl.enable `blend;
5524 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5525 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5526 filledrect (float x0) (float y0) (float x1) (float y1);
5527 Gl.disable `blend;
5528 | Msel _
5529 | Mpan _
5530 | Mscrolly | Mscrollx
5531 | Mzoom _
5532 | Mnone -> ()
5533 end;
5534 enttext ();
5535 scrollindicator ();
5536 Wsi.swapb ();
5539 let zoomrect x y x1 y1 =
5540 let x0 = min x x1
5541 and x1 = max x x1
5542 and y0 = min y y1 in
5543 let zoom = (float state.w) /. float (x1 - x0) in
5544 let margin =
5545 let simple () =
5546 if state.w < state.winw
5547 then (state.winw - state.w) / 2
5548 else 0
5550 match conf.fitmodel with
5551 | FitWidth | FitProportional -> simple ()
5552 | FitPage ->
5553 match conf.columns with
5554 | Csplit _ ->
5555 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5556 | Cmulti _ | Csingle _ -> simple ()
5558 gotoxy ((state.x + margin) - x0) (state.y + y0);
5559 state.anchor <- getanchor ();
5560 setzoom zoom;
5561 resetmstate ();
5564 let annot inline x y =
5565 match unproject x y with
5566 | Some (opaque, n, ux, uy) ->
5567 let add text =
5568 addannot opaque ux uy text;
5569 wcmd "freepage %s" (~> opaque);
5570 Hashtbl.remove state.pagemap (n, state.gen);
5571 flushtiles ();
5572 gotoxy state.x state.y
5574 if inline
5575 then
5576 let ondone s = add s in
5577 let mode = state.mode in
5578 state.mode <- Textentry (
5579 ("annotation: ", E.s, None, textentry, ondone, true),
5580 fun _ -> state.mode <- mode);
5581 state.text <- E.s;
5582 enttext ();
5583 G.postRedisplay "annot"
5584 else
5585 add @@ getusertext E.s
5586 | _ -> ()
5589 let zoomblock x y =
5590 let g opaque l px py =
5591 match rectofblock opaque px py with
5592 | Some a ->
5593 let x0 = a.(0) -. 20. in
5594 let x1 = a.(1) +. 20. in
5595 let y0 = a.(2) -. 20. in
5596 let zoom = (float state.w) /. (x1 -. x0) in
5597 let pagey = getpagey l.pageno in
5598 let margin = (state.w - l.pagew)/2 in
5599 let nx = -truncate x0 - margin in
5600 gotoxy_and_clear_text nx (pagey + truncate y0);
5601 state.anchor <- getanchor ();
5602 setzoom zoom;
5603 None
5604 | None -> None
5606 match conf.columns with
5607 | Csplit _ ->
5608 impmsg "block zooming does not work properly in split columns mode"
5609 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5612 let scrollx x =
5613 let winw = state.winw - 1 in
5614 let s = float x /. float winw in
5615 let destx = truncate (float (state.w + winw) *. s) in
5616 gotoxy_and_clear_text (winw - destx) state.y;
5617 state.mstate <- Mscrollx;
5620 let scrolly y =
5621 let s = float y /. float state.winh in
5622 let desty = truncate (s *. float (maxy ())) in
5623 gotoxy_and_clear_text state.x desty;
5624 state.mstate <- Mscrolly;
5627 let viewmulticlick clicks x y mask =
5628 let g opaque l px py =
5629 let mark =
5630 match clicks with
5631 | 2 -> Mark_word
5632 | 3 -> Mark_line
5633 | 4 -> Mark_block
5634 | _ -> Mark_page
5636 if markunder opaque px py mark
5637 then (
5638 Some (fun () ->
5639 let dopipe cmd =
5640 match getopaque l.pageno with
5641 | None -> ()
5642 | Some opaque -> pipesel opaque cmd
5644 state.roam <- (fun () -> dopipe conf.paxcmd);
5645 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5648 else None
5650 G.postRedisplay "viewmulticlick";
5651 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
5654 let canselect () =
5655 match conf.columns with
5656 | Csplit _ -> false
5657 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5660 let viewmouse button down x y mask =
5661 match button with
5662 | n when (n == 4 || n == 5) && not down ->
5663 if Wsi.withctrl mask
5664 then (
5665 match state.mstate with
5666 | Mzoom (oldn, i, (ftx, fty)) ->
5667 let recenter =
5668 if false
5669 then abs (ftx - x) > 5 || abs (fty - y) > 5
5670 else false
5672 if oldn = n
5673 then (
5674 if i = 2
5675 then
5676 let incr =
5677 match n with
5678 | 5 ->
5679 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5680 | _ ->
5681 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5683 let zoom = conf.zoom -. incr in
5684 if recenter
5685 then pivotzoom ~x ~y zoom
5686 else pivotzoom zoom;
5687 state.mstate <- Mzoom (n, 0, (x, y));
5688 else
5689 state.mstate <- Mzoom (n, i+1, (ftx, fty));
5691 else state.mstate <- Mzoom (n, 0, (ftx, fty))
5693 | Msel _
5694 | Mpan _
5695 | Mscrolly | Mscrollx
5696 | Mzoomrect _
5697 | Mnone -> state.mstate <- Mzoom (n, 0, (0, 0))
5699 else (
5700 match state.autoscroll with
5701 | Some step -> setautoscrollspeed step (n=4)
5702 | None ->
5703 if conf.wheelbypage || conf.presentation
5704 then (
5705 if n = 4
5706 then prevpage ()
5707 else nextpage ()
5709 else
5710 let incr =
5711 if n = 4
5712 then -conf.scrollstep
5713 else conf.scrollstep
5715 let incr = incr * 2 in
5716 let y = clamp incr in
5717 gotoxy_and_clear_text state.x y
5720 | n when (n = 6 || n = 7) && not down && canpan () ->
5721 let x =
5722 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep) in
5723 gotoxy_and_clear_text x state.y
5725 | 1 when Wsi.withshift mask ->
5726 state.mstate <- Mnone;
5727 if not down
5728 then (
5729 match unproject x y with
5730 | None -> ()
5731 | Some (_, pageno, ux, uy) ->
5732 let cmd = Printf.sprintf
5733 "%s %s %d %d %d"
5734 conf.stcmd state.path pageno ux uy
5736 match spawn cmd [] with
5737 | exception exn ->
5738 impmsg "execution of synctex command(%S) failed: %S"
5739 conf.stcmd @@ exntos exn
5740 | _pid -> ()
5743 | 1 when Wsi.withctrl mask ->
5744 if down
5745 then (
5746 Wsi.setcursor Wsi.CURSOR_FLEUR;
5747 state.mstate <- Mpan (x, y)
5749 else
5750 state.mstate <- Mnone
5752 | 3 ->
5753 if down
5754 then (
5755 if Wsi.withshift mask
5756 then (
5757 annot conf.annotinline x y;
5758 G.postRedisplay "addannot"
5760 else
5761 let p = (x, y) in
5762 Wsi.setcursor Wsi.CURSOR_CYCLE;
5763 state.mstate <- Mzoomrect (p, p)
5765 else (
5766 match state.mstate with
5767 | Mzoomrect ((x0, y0), _) ->
5768 if abs (x-x0) > 10 && abs (y - y0) > 10
5769 then zoomrect x0 y0 x y
5770 else (
5771 resetmstate ();
5772 G.postRedisplay "kill accidental zoom rect";
5774 | Msel _
5775 | Mpan _
5776 | Mscrolly | Mscrollx
5777 | Mzoom _
5778 | Mnone -> resetmstate ()
5781 | 1 when vscrollhit x ->
5782 if down
5783 then
5784 let _, position, sh = state.uioh#scrollph in
5785 if y > truncate position && y < truncate (position +. sh)
5786 then state.mstate <- Mscrolly
5787 else scrolly y
5788 else
5789 state.mstate <- Mnone
5791 | 1 when y > state.winh - hscrollh () ->
5792 if down
5793 then
5794 let _, position, sw = state.uioh#scrollpw in
5795 if x > truncate position && x < truncate (position +. sw)
5796 then state.mstate <- Mscrollx
5797 else scrollx x
5798 else
5799 state.mstate <- Mnone
5801 | 1 when state.bzoom -> if not down then zoomblock x y
5803 | 1 ->
5804 let dest = if down then getunder x y else Unone in
5805 begin match dest with
5806 | Ulinkuri _ ->
5807 gotounder dest
5809 | Unone when down ->
5810 Wsi.setcursor Wsi.CURSOR_FLEUR;
5811 state.mstate <- Mpan (x, y);
5813 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5815 | Unone | Utext _ ->
5816 if down
5817 then (
5818 if canselect ()
5819 then (
5820 state.mstate <- Msel ((x, y), (x, y));
5821 G.postRedisplay "mouse select";
5824 else (
5825 match state.mstate with
5826 | Mnone -> ()
5828 | Mzoom _ | Mscrollx | Mscrolly ->
5829 state.mstate <- Mnone
5831 | Mzoomrect ((x0, y0), _) ->
5832 zoomrect x0 y0 x y
5834 | Mpan _ ->
5835 Wsi.setcursor Wsi.CURSOR_INHERIT;
5836 state.mstate <- Mnone
5838 | Msel ((x0, y0), (x1, y1)) ->
5839 let rec loop = function
5840 | [] -> ()
5841 | l :: rest ->
5842 let inside =
5843 let a0 = l.pagedispy in
5844 let a1 = a0 + l.pagevh in
5845 let b0 = l.pagedispx in
5846 let b1 = b0 + l.pagevw in
5847 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5848 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5850 if inside
5851 then
5852 match getopaque l.pageno with
5853 | Some opaque ->
5854 let dosel cmd () =
5855 pipef ~closew:false "Msel"
5856 (fun w ->
5857 copysel w opaque;
5858 G.postRedisplay "Msel") cmd
5860 dosel conf.selcmd ();
5861 state.roam <- dosel conf.paxcmd;
5862 | None -> ()
5863 else loop rest
5865 loop state.layout;
5866 resetmstate ();
5870 | _ -> ()
5873 let birdseyemouse button down x y mask
5874 (conf, leftx, _, hooverpageno, anchor) =
5875 match button with
5876 | 1 when down ->
5877 let rec loop = function
5878 | [] -> ()
5879 | l :: rest ->
5880 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5881 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5882 then (
5883 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5885 else loop rest
5887 loop state.layout
5888 | 3 -> ()
5889 | _ -> viewmouse button down x y mask
5892 let uioh = object
5893 method display = ()
5895 method key key mask =
5896 begin match state.mode with
5897 | Textentry textentry -> textentrykeyboard key mask textentry
5898 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5899 | View -> viewkeyboard key mask
5900 | LinkNav linknav -> linknavkeyboard key mask linknav
5901 end;
5902 state.uioh
5904 method button button bstate x y mask =
5905 begin match state.mode with
5906 | LinkNav _ | View -> viewmouse button bstate x y mask
5907 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5908 | Textentry _ -> ()
5909 end;
5910 state.uioh
5912 method multiclick clicks x y mask =
5913 begin match state.mode with
5914 | LinkNav _ | View -> viewmulticlick clicks x y mask
5915 | Birdseye _ | Textentry _ -> ()
5916 end;
5917 state.uioh
5919 method motion x y =
5920 begin match state.mode with
5921 | Textentry _ -> ()
5922 | View | Birdseye _ | LinkNav _ ->
5923 match state.mstate with
5924 | Mzoom _ | Mnone -> ()
5926 | Mpan (x0, y0) ->
5927 let dx = x - x0
5928 and dy = y0 - y in
5929 state.mstate <- Mpan (x, y);
5930 let x = if canpan () then panbound (state.x + dx) else state.x in
5931 let y = clamp dy in
5932 gotoxy_and_clear_text x y
5934 | Msel (a, _) ->
5935 state.mstate <- Msel (a, (x, y));
5936 G.postRedisplay "motion select";
5938 | Mscrolly ->
5939 let y = min state.winh (max 0 y) in
5940 scrolly y
5942 | Mscrollx ->
5943 let x = min state.winw (max 0 x) in
5944 scrollx x
5946 | Mzoomrect (p0, _) ->
5947 state.mstate <- Mzoomrect (p0, (x, y));
5948 G.postRedisplay "motion zoomrect";
5949 end;
5950 state.uioh
5952 method pmotion x y =
5953 begin match state.mode with
5954 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5955 let rec loop = function
5956 | [] ->
5957 if hooverpageno != -1
5958 then (
5959 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5960 G.postRedisplay "pmotion birdseye no hoover";
5962 | l :: rest ->
5963 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5964 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5965 then (
5966 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5967 G.postRedisplay "pmotion birdseye hoover";
5969 else loop rest
5971 loop state.layout
5973 | Textentry _ -> ()
5975 | LinkNav _ | View ->
5976 match state.mstate with
5977 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
5978 | Mnone ->
5979 updateunder x y;
5980 if canselect ()
5981 then
5982 match conf.pax with
5983 | None -> ()
5984 | Some r ->
5985 let past, _, _ = !r in
5986 let now = now () in
5987 let delta = now -. past in
5988 if delta > 0.01
5989 then paxunder x y
5990 else r := (now, x, y)
5991 end;
5992 state.uioh
5994 method infochanged _ = ()
5996 method scrollph =
5997 let maxy = maxy () in
5998 let p, h =
5999 if maxy = 0
6000 then 0.0, float state.winh
6001 else scrollph state.y maxy
6003 vscrollw (), p, h
6005 method scrollpw =
6006 let fwinw = float (state.winw - vscrollw ()) in
6007 let sw =
6008 let sw = fwinw /. float state.w in
6009 let sw = fwinw *. sw in
6010 max sw (float conf.scrollh)
6012 let position =
6013 let maxx = state.w + state.winw in
6014 let x = state.winw - state.x in
6015 let percent = float x /. float maxx in
6016 (fwinw -. sw) *. percent
6018 hscrollh (), position, sw
6020 method modehash =
6021 let modename =
6022 match state.mode with
6023 | LinkNav _ -> "links"
6024 | Textentry _ -> "textentry"
6025 | Birdseye _ -> "birdseye"
6026 | View -> "view"
6028 findkeyhash conf modename
6030 method eformsgs = true
6031 method alwaysscrolly = false
6032 end;;
6034 let addrect pageno r g b a x0 y0 x1 y1 =
6035 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
6038 let ract cmds =
6039 let cl = splitatchar cmds ' ' in
6040 let scan s fmt f =
6041 try Scanf.sscanf s fmt f
6042 with exn -> adderrfmt "remote exec" "error processing '%S': %s\n"
6043 cmds @@ exntos exn
6045 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
6046 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
6047 s pageno r g b a x0 y0 x1 y1;
6048 onpagerect
6049 pageno
6050 (fun w h ->
6051 let _,w1,h1,_ = getpagedim pageno in
6052 let sw = float w1 /. float w
6053 and sh = float h1 /. float h in
6054 let x0s = x0 *. sw
6055 and x1s = x1 *. sw
6056 and y0s = y0 *. sh
6057 and y1s = y1 *. sh in
6058 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6059 let color = (r, g, b, a) in
6060 if conf.verbose then debugrect rect;
6061 state.rects <- (pageno, color, rect) :: state.rects;
6062 G.postRedisplay s;
6065 match cl with
6066 | "reload", "" -> reload ()
6067 | "goto", args ->
6068 scan args "%u %f %f"
6069 (fun pageno x y ->
6070 let cmd, _ = state.geomcmds in
6071 if emptystr cmd
6072 then gotopagexy !wtmode pageno x y
6073 else
6074 let f prevf () =
6075 gotopagexy !wtmode pageno x y;
6076 prevf ()
6078 state.reprf <- f state.reprf
6080 | "goto1", args -> scan args "%u %f" gotopage
6081 | "gotor", args ->
6082 scan args "%S %u"
6083 (fun _filename _pageno ->
6084 failwith "gotounder (Uremote (filename, pageno))")
6085 | "gotord", args ->
6086 scan args "%S %S"
6087 (fun _filename _dest ->
6088 failwith "gotounder (Uremotedest (filename, dest))")
6089 | "rect", args ->
6090 scan args "%u %u %f %f %f %f"
6091 (fun pageno c x0 y0 x1 y1 ->
6092 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
6093 rectx "rect" pageno color x0 y0 x1 y1;
6095 | "prect", args ->
6096 scan args "%u %f %f %f %f %f %f %f %f"
6097 (fun pageno r g b alpha x0 y0 x1 y1 ->
6098 addrect pageno r g b alpha x0 y0 x1 y1;
6099 G.postRedisplay "prect"
6101 | "pgoto", args ->
6102 scan args "%u %f %f"
6103 (fun pageno x y ->
6104 let optopaque =
6105 match getopaque pageno with
6106 | Some opaque -> opaque
6107 | None -> ~< E.s
6109 pgoto optopaque pageno x y;
6110 let rec fixx = function
6111 | [] -> ()
6112 | l :: rest ->
6113 if l.pageno = pageno
6114 then gotoxy (state.x - l.pagedispx) state.y
6115 else fixx rest
6117 let layout =
6118 let mult =
6119 match conf.columns with
6120 | Csingle _ | Csplit _ -> 1
6121 | Cmulti ((n, _, _), _) -> n
6123 layout 0 state.y (state.winw * mult) state.winh
6125 fixx layout
6127 | "activatewin", "" -> Wsi.activatewin ()
6128 | "quit", "" -> raise Quit
6129 | "keys", keys ->
6130 begin try
6131 let l = Config.keys_of_string keys in
6132 List.iter (fun (k, m) -> keyboard k m) l
6133 with exn -> adderrfmt "error processing keys" "`%S': %s\n"
6134 cmds @@ exntos exn
6136 | "clearrects", "" ->
6137 Hashtbl.clear state.prects;
6138 G.postRedisplay "clearrects"
6139 | _ ->
6140 adderrfmt "remote command"
6141 "error processing remote command: %S\n" cmds;
6144 let remote =
6145 let scratch = Bytes.create 80 in
6146 let buf = Buffer.create 80 in
6147 fun fd ->
6148 match tempfailureretry (Unix.read fd scratch 0) 80 with
6149 | exception Unix.Unix_error (Unix.EAGAIN, _, _) -> None
6150 | 0 ->
6151 Unix.close fd;
6152 if Buffer.length buf > 0
6153 then (
6154 let s = Buffer.contents buf in
6155 Buffer.clear buf;
6156 ract s;
6158 None
6159 | n ->
6160 let rec eat ppos =
6161 let nlpos =
6162 match Bytes.index_from scratch ppos '\n' with
6163 | pos -> if pos >= n then -1 else pos
6164 | exception Not_found -> -1
6166 if nlpos >= 0
6167 then (
6168 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6169 let s = Buffer.contents buf in
6170 Buffer.clear buf;
6171 ract s;
6172 eat (nlpos+1);
6174 else (
6175 Buffer.add_subbytes buf scratch ppos (n-ppos);
6176 Some fd
6178 in eat 0
6181 let remoteopen path =
6182 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6183 with exn ->
6184 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
6185 None
6188 let () =
6189 let gcconfig = ref E.s in
6190 let trimcachepath = ref E.s in
6191 let rcmdpath = ref E.s in
6192 let pageno = ref None in
6193 let rootwid = ref 0 in
6194 let openlast = ref false in
6195 let doreap = ref false in
6196 let csspath = ref None in
6197 selfexec := Sys.executable_name;
6198 Arg.parse
6199 (Arg.align
6200 [("-p", Arg.String (fun s -> state.password <- s),
6201 "<password> Set password");
6203 ("-f", Arg.String
6204 (fun s ->
6205 Config.fontpath := s;
6206 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6208 "<path> Set path to the user interface font");
6210 ("-c", Arg.String
6211 (fun s ->
6212 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6213 Config.confpath := s),
6214 "<path> Set path to the configuration file");
6216 ("-last", Arg.Set openlast, " Open last document");
6218 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6219 "<page-number> Jump to page");
6221 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6222 "<path> Set path to the trim cache file");
6224 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6225 "<named-destination> Set named destination");
6227 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6228 ("-cxack", Arg.Set cxack, " Cut corners");
6230 ("-remote", Arg.String (fun s -> rcmdpath := s),
6231 "<path> Set path to the source of remote commands");
6233 ("-gc", Arg.Set_string gcconfig,
6234 "<path> Collect garbage with the help of a script");
6236 ("-v", Arg.Unit (fun () ->
6237 Printf.printf
6238 "%s\nconfiguration path: %s\n"
6239 (version ())
6240 Config.defconfpath;
6241 exit 0), " Print version and exit");
6243 ("-css", Arg.String (fun s -> csspath := Some s),
6244 "<path> Set path to the style sheet to use with EPUB/HTML");
6246 ("-embed", Arg.Set_int rootwid, "<window-id> Embed into window");
6248 ("-origin", Arg.String (fun s -> state.origin <- s),
6249 "<origin> <undocumented>");
6252 (fun s -> state.path <- s)
6253 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:");
6255 if !wtmode
6256 then selfexec := !selfexec ^ " -wtmode";
6258 let histmode = emptystr state.path && not !openlast in
6260 if not (Config.load !openlast)
6261 then dolog "failed to load configuration";
6263 begin match !pageno with
6264 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6265 | None -> ()
6266 end;
6268 if nonemptystr !gcconfig
6269 then (
6270 let (c, s) =
6271 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6272 | exception exn -> error "socketpair for gc failed: %s" @@ exntos exn
6273 | fds -> fds
6275 match spawn !gcconfig [(c, 0); (c, 1); (s, -1)] with
6276 | exception exn -> error "failed to execute gc script: %s" @@ exntos exn
6277 | _pid ->
6278 Config.gc s;
6279 exit 0
6282 let mu =
6283 object (self)
6284 val mutable m_clicks = 0
6285 val mutable m_click_x = 0
6286 val mutable m_click_y = 0
6287 val mutable m_lastclicktime = infinity
6289 method private cleanup =
6290 state.roam <- noroam;
6291 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6292 method expose = G.postRedisplay "expose"
6293 method visible v =
6294 let name =
6295 match v with
6296 | Wsi.Unobscured -> "unobscured"
6297 | Wsi.PartiallyObscured -> "partiallyobscured"
6298 | Wsi.FullyObscured -> "fullyobscured"
6300 vlog "visibility change %s" name
6301 method display = display ()
6302 method map mapped = vlog "mapped %b" mapped
6303 method reshape w h =
6304 self#cleanup;
6305 reshape w h
6306 method mouse b d x y m =
6307 if d && canselect ()
6308 then (
6310 * http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx
6312 m_click_x <- x;
6313 m_click_y <- y;
6314 if b = 1
6315 then (
6316 let t = now () in
6317 if abs x - m_click_x > 10
6318 || abs y - m_click_y > 10
6319 || abs_float (t -. m_lastclicktime) > 0.3
6320 then m_clicks <- 0;
6321 m_clicks <- m_clicks + 1;
6322 m_lastclicktime <- t;
6323 if m_clicks = 1
6324 then (
6325 self#cleanup;
6326 G.postRedisplay "cleanup";
6327 state.uioh <- state.uioh#button b d x y m;
6329 else state.uioh <- state.uioh#multiclick m_clicks x y m
6331 else (
6332 self#cleanup;
6333 m_clicks <- 0;
6334 m_lastclicktime <- infinity;
6335 state.uioh <- state.uioh#button b d x y m
6338 else (
6339 state.uioh <- state.uioh#button b d x y m
6341 method motion x y =
6342 state.mpos <- (x, y);
6343 state.uioh <- state.uioh#motion x y
6344 method pmotion x y =
6345 state.mpos <- (x, y);
6346 state.uioh <- state.uioh#pmotion x y
6347 method key k m =
6348 vlog "k=%#x m=%#x" k m;
6349 let mascm = m land (
6350 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6351 ) in
6352 let keyboard k m =
6353 let x = state.x and y = state.y in
6354 keyboard k m;
6355 if x != state.x || y != state.y then self#cleanup
6357 match state.keystate with
6358 | KSnone ->
6359 let km = k, mascm in
6360 begin
6361 match
6362 let modehash = state.uioh#modehash in
6363 try Hashtbl.find modehash km
6364 with Not_found ->
6365 try Hashtbl.find (findkeyhash conf "global") km
6366 with Not_found -> KMinsrt (k, m)
6367 with
6368 | KMinsrt (k, m) -> keyboard k m
6369 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6370 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6372 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6373 List.iter (fun (k, m) -> keyboard k m) insrt;
6374 state.keystate <- KSnone
6375 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6376 state.keystate <- KSinto (keys, insrt)
6377 | KSinto _ -> state.keystate <- KSnone
6379 method enter x y =
6380 state.mpos <- (x, y);
6381 state.uioh <- state.uioh#pmotion x y
6382 method leave = state.mpos <- (-1, -1)
6383 method winstate wsl = state.winstate <- wsl
6384 method quit = raise Quit
6387 let wsfd, winw, winh = Wsi.init mu !rootwid conf.cwinw conf.cwinh platform in
6389 setbgcol conf.bgcolor;
6390 state.wsfd <- wsfd;
6392 if not @@ List.exists GlMisc.check_extension
6393 [ "GL_ARB_texture_rectangle"
6394 ; "GL_EXT_texture_recangle"
6395 ; "GL_NV_texture_rectangle" ]
6396 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
6398 if substratis (GlMisc.get_string `renderer) 0 "Mesa DRI Intel("
6399 then (
6400 defconf.sliceheight <- 1024;
6401 defconf.texcount <- 32;
6402 defconf.usepbo <- true;
6405 let cs, ss =
6406 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6407 | exception exn ->
6408 dolog "socketpair failed: %s" @@ exntos exn;
6409 exit 1
6410 | (r, w) ->
6411 cloexec r;
6412 cloexec w;
6413 r, w
6416 setcheckers conf.checkers;
6418 opengl_has_pbo := GlMisc.check_extension "GL_ARB_pixel_buffer_object";
6420 begin match !csspath with
6421 | None -> ()
6422 | Some "" -> conf.css <- E.s
6423 | Some path ->
6424 let css = filecontents path in
6425 let l = String.length css in
6426 conf.css <-
6427 if substratis css (l-2) "\r\n"
6428 then String.sub css 0 (l-2)
6429 else (if css.[l-1] = '\n'
6430 then String.sub css 0 (l-1)
6431 else css);
6432 end;
6433 init cs (
6434 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6435 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6436 !Config.fontpath, !trimcachepath, !opengl_has_pbo
6438 List.iter GlArray.enable [`texture_coord; `vertex];
6439 state.ss <- ss;
6440 reshape ~firsttime:true winw winh;
6441 state.uioh <- uioh;
6442 if histmode
6443 then (
6444 Wsi.settitle "llpp (history)";
6445 enterhistmode ();
6447 else (
6448 state.text <- "Opening " ^ (mbtoutf8 state.path);
6449 opendoc state.path state.password;
6451 display ();
6452 Wsi.mapwin ();
6453 Wsi.setcursor Wsi.CURSOR_INHERIT;
6454 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6456 let rec reap () =
6457 match Unix.waitpid [Unix.WNOHANG] ~-1 with
6458 | exception (Unix.Unix_error (Unix.ECHILD, _, _)) -> ()
6459 | exception exn -> dolog "Unix.waitpid: %s" @@ exntos exn
6460 | 0, _ -> ()
6461 | _pid, _status -> reap ()
6463 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
6465 let optrfd =
6466 ref (
6467 if nonemptystr !rcmdpath
6468 then remoteopen !rcmdpath
6469 else None
6473 let rec loop deadline =
6474 if !doreap
6475 then (
6476 doreap := false;
6477 reap ()
6479 let r = [state.ss; state.wsfd] in
6480 let r =
6481 match !optrfd with
6482 | None -> r
6483 | Some fd -> fd :: r
6485 if state.redisplay
6486 then (
6487 state.redisplay <- false;
6488 display ();
6490 let timeout =
6491 let now = now () in
6492 if deadline > now
6493 then (
6494 if deadline = infinity
6495 then ~-.1.0
6496 else max 0.0 (deadline -. now)
6498 else 0.0
6500 let r, _, _ =
6501 try Unix.select r [] [] timeout
6502 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6504 begin match r with
6505 | [] ->
6506 state.ghyll None;
6507 let newdeadline =
6508 if state.ghyll == noghyll
6509 then
6510 match state.autoscroll with
6511 | Some step when step != 0 ->
6512 let y = state.y + step in
6513 let fy = if conf.maxhfit then state.winh else 0 in
6514 let y =
6515 if y < 0
6516 then state.maxy - fy
6517 else if y >= state.maxy - fy then 0 else y
6519 if state.mode = View
6520 then gotoxy_and_clear_text state.x y
6521 else gotoxy state.x y;
6522 deadline +. 0.01
6523 | _ -> infinity
6524 else deadline +. 0.01
6526 loop newdeadline
6528 | l ->
6529 let rec checkfds = function
6530 | [] -> ()
6531 | fd :: rest when fd = state.ss ->
6532 let cmd = rcmd state.ss in
6533 act cmd;
6534 checkfds rest
6536 | fd :: rest when fd = state.wsfd ->
6537 Wsi.readresp fd;
6538 checkfds rest
6540 | fd :: rest when Some fd = !optrfd ->
6541 begin match remote fd with
6542 | None -> optrfd := remoteopen !rcmdpath;
6543 | opt -> optrfd := opt
6544 end;
6545 checkfds rest
6547 | _ :: rest ->
6548 dolog "select returned unknown descriptor";
6549 checkfds rest
6551 checkfds l;
6552 let newdeadline =
6553 let deadline1 =
6554 if deadline = infinity
6555 then now () +. 0.01
6556 else deadline
6558 match state.autoscroll with
6559 | Some step when step != 0 -> deadline1
6560 | _ -> if state.ghyll == noghyll then infinity else deadline1
6562 loop newdeadline
6563 end;
6565 match loop infinity with
6566 | exception Quit ->
6567 Config.save leavebirdseye;
6568 if hasunsavedchanges ()
6569 then save ()
6570 | _ -> error "umpossible - infinity reached"