Cosmetics
[llpp.git] / main.ml
blob9e506ad177fbe294b6d8567428829aede1ebc311
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 copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
10 external getpdimrect : int -> float array = "ml_getpdimrect";;
11 external whatsunder : opaque -> int -> int -> under = "ml_whatsunder";;
12 external markunder : opaque -> int -> int -> mark -> bool = "ml_markunder";;
13 external clearmark : opaque -> unit = "ml_clearmark";;
14 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
15 external getmaxw : unit -> float = "ml_getmaxw";;
16 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
17 external measurestr : int -> string -> float = "ml_measure_string";;
18 external postprocess :
19 opaque -> int -> int -> int -> (int * string * int) -> int
20 = "ml_postprocess";;
21 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
22 external setaalevel : int -> unit = "ml_setaalevel";;
23 external realloctexts : int -> bool = "ml_realloctexts";;
24 external findlink : opaque -> linkdir -> link = "ml_findlink";;
25 external getlink : opaque -> int -> under = "ml_getlink";;
26 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
27 external getlinkcount : opaque -> int = "ml_getlinkcount";;
28 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links";;
29 external getpbo : width -> height -> colorspace -> opaque = "ml_getpbo";;
30 external freepbo : opaque -> unit = "ml_freepbo";;
31 external unmappbo : opaque -> unit = "ml_unmappbo";;
32 external bousable : unit -> bool = "ml_bo_usable";;
33 external unproject : opaque -> int -> int -> (int * int) option
34 = "ml_unproject";;
35 external project : opaque -> int -> int -> float -> float -> (float * float)
36 = "ml_project";;
37 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
38 external rectofblock : opaque -> int -> int -> float array option
39 = "ml_rectofblock";;
40 external begintiles : unit -> unit = "ml_begintiles";;
41 external endtiles : unit -> unit = "ml_endtiles";;
42 external addannot : opaque -> int -> int -> string -> unit = "ml_addannot";;
43 external modannot : opaque -> slinkindex -> string -> unit = "ml_modannot";;
44 external delannot : opaque -> slinkindex -> unit = "ml_delannot";;
45 external hasunsavedchanges : unit -> bool = "ml_hasunsavedchanges";;
46 external savedoc : string -> unit = "ml_savedoc";;
47 external getannotcontents : opaque -> slinkindex -> string
48 = "ml_getannotcontents";;
49 external drawprect : opaque -> int -> int -> float array -> unit
50 = "ml_drawprect";;
51 external wcmd : Unix.file_descr -> bytes -> int -> unit = "ml_wcmd";;
52 external rcmd : Unix.file_descr -> string = "ml_rcmd";;
53 external uritolocation : string -> (pageno * float * float)
54 = "ml_uritolocation";;
55 external isexternallink : string -> bool = "ml_isexternallink";;
57 let selfexec = ref E.s;;
58 let opengl_has_pbo = ref false;;
60 let drawstring size x y s =
61 Gl.enable `blend;
62 Gl.enable `texture_2d;
63 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
64 ignore (drawstr size x y s);
65 Gl.disable `blend;
66 Gl.disable `texture_2d;
69 let drawstring1 size x y s =
70 drawstr size x y s;
73 let drawstring2 size x y fmt =
74 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
77 module UniSyms = struct
78 let ellipsis = "\xe2\x80\xa6";;
79 let radical = "\xe2\x88\x9a";;
80 let lguillemet = "\xc2\xab";;
81 let rguillemet = "\xc2\xbb";;
82 end;;
84 let _debugl l =
85 dolog {|l %d dim=%d {
86 WxH %dx%d
87 vWxH %dx%d
88 pagex,y %d,%d
89 dispx,y %d,%d
90 column %d
91 }|}
92 l.pageno l.pagedimno
93 l.pagew l.pageh
94 l.pagevw l.pagevh
95 l.pagex l.pagey
96 l.pagedispx l.pagedispy
97 l.pagecol
100 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
101 dolog {|rect {
102 x0,y0=(% f, % f)
103 x1,y1=(% f, % f)
104 x2,y2=(% f, % f)
105 x3,y3=(% f, % f)
106 }|} x0 y0 x1 y1 x2 y2 x3 y3;
109 let isbirdseye = function
110 | Birdseye _ -> true
111 | Textentry _ | View | LinkNav _ -> false
114 let istextentry = function
115 | Textentry _ -> true
116 | Birdseye _ | View | LinkNav _ -> false
119 let wtmode = ref false;;
120 let cxack = ref false;;
122 let pgscale h = truncate (float h *. conf.pgscale);;
124 let hscrollh () =
125 if state.uioh#alwaysscrolly || ((conf.scrollb land scrollbhv != 0)
126 && (state.w > state.winw))
127 then conf.scrollbw
128 else 0
131 let vscrollw () =
132 if state.uioh#alwaysscrolly || ((conf.scrollb land scrollbvv != 0)
133 && (state.maxy > state.winh))
134 then conf.scrollbw
135 else 0
138 let vscrollhit x =
139 if conf.leftscroll
140 then x < vscrollw ()
141 else x > state.winw - vscrollw ()
144 let setfontsize n =
145 fstate.fontsize <- n;
146 fstate.wwidth <- measurestr fstate.fontsize "w";
147 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
150 let vlog fmt =
151 if conf.verbose
152 then dolog fmt
153 else Printf.kprintf ignore fmt
156 let launchpath () =
157 if emptystr conf.pathlauncher
158 then dolog "%s" state.path
159 else (
160 let command = Str.global_replace percentsre state.path conf.pathlauncher in
161 match spawn command [] with
162 | _pid -> ()
163 | exception exn ->
164 dolog "failed to execute `%s': %s" command @@ exntos exn
168 module G =
169 struct
170 let postRedisplay who =
171 vlog "redisplay for [%S]" who;
172 state.redisplay <- true;
174 end;;
176 let getopaque pageno =
177 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
178 with Not_found -> None
181 let pagetranslatepoint l x y =
182 let dy = y - l.pagedispy in
183 let y = dy + l.pagey in
184 let dx = x - l.pagedispx in
185 let x = dx + l.pagex in
186 (x, y);
189 let onppundermouse g x y d =
190 let rec f = function
191 | l :: rest ->
192 begin match getopaque l.pageno with
193 | Some opaque ->
194 let x0 = l.pagedispx in
195 let x1 = x0 + l.pagevw in
196 let y0 = l.pagedispy in
197 let y1 = y0 + l.pagevh in
198 if y >= y0 && y <= y1 && x >= x0 && x <= x1
199 then
200 let px, py = pagetranslatepoint l x y in
201 match g opaque l px py with
202 | Some res -> res
203 | None -> f rest
204 else f rest
205 | _ ->
206 f rest
208 | [] -> d
210 f state.layout
213 let getunder x y =
214 let g opaque l px py =
215 if state.bzoom
216 then (
217 match rectofblock opaque px py with
218 | Some [|x0;x1;y0;y1|] ->
219 let rect = (x0, y0, x1, y0, x1, y1, x0, y1) in
220 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
221 state.rects <- [l.pageno, color, rect];
222 G.postRedisplay "getunder";
223 | _otherwise -> ()
225 let under = whatsunder opaque px py in
226 if under = Unone then None else Some under
228 onppundermouse g x y Unone
231 let unproject x y =
232 let g opaque l x y =
233 match unproject opaque x y with
234 | Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
235 | None -> None
237 onppundermouse g x y None;
240 let showtext c s =
241 state.text <- Printf.sprintf "%c%s" c s;
242 G.postRedisplay "showtext";
245 let impmsg fmt =
246 Format.ksprintf (fun s -> showtext '!' s) fmt;
249 let pipesel opaque cmd =
250 if hassel opaque
251 then
252 match Unix.pipe () with
253 | exception exn -> dolog "pipesel cannot create pipe: %S" @@ exntos exn;
254 | (r, w) ->
255 let doclose what fd =
256 Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
258 begin match spawn cmd [r, 0; w, -1] with
259 | exception exn ->
260 doclose "pipesel pipe/w" w;
261 dolog "cannot execute %S: %s" cmd @@ exntos exn
262 | _pid ->
263 copysel w opaque;
264 G.postRedisplay "pipesel";
265 end;
266 doclose "pipesel pipe/r" r;
269 let paxunder x y =
270 let g opaque l px py =
271 if markunder opaque px py conf.paxmark
272 then (
273 Some (fun () ->
274 match getopaque l.pageno with
275 | None -> ()
276 | Some opaque -> pipesel opaque conf.paxcmd
279 else None
281 G.postRedisplay "paxunder";
282 if conf.paxmark = Mark_page
283 then
284 List.iter (fun l ->
285 match getopaque l.pageno with
286 | None -> ()
287 | Some opaque -> clearmark opaque) state.layout;
288 state.roam <- onppundermouse g x y (fun () -> impmsg "whoopsie daisy");
291 let selstring s =
292 match Unix.pipe () with
293 | exception exn -> impmsg "pipe failed: %s" @@ exntos exn
294 | (r, w) ->
295 let clo cap fd =
296 Ne.clo fd (fun msg -> impmsg "failed to close %s: %s" cap msg)
298 begin match spawn conf.selcmd [r, 0; w, -1] with
299 | exception exn ->
300 clo "selstring pipe/w" w;
301 impmsg "failed to execute %s: %s" conf.selcmd @@ exntos exn
302 | _pid ->
304 let l = String.length s in
305 let bytes = Bytes.unsafe_of_string s in
306 let n = tempfailureretry (Unix.write w bytes 0) l in
307 if n != l
308 then impmsg "failed to write %d characters to sel pipe, wrote %d" l n
309 with exn -> impmsg "failed to write to sel pipe: %s" @@ exntos exn
310 end;
311 clo "selstring pipe/r" r;
314 let undertext = function
315 | Unone -> "none"
316 | Ulinkuri s -> s
317 | Utext s -> "font: " ^ s
318 | Uannotation (opaque, slinkindex) ->
319 "annotation: " ^ getannotcontents opaque slinkindex
322 let updateunder x y =
323 match getunder x y with
324 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
325 | Ulinkuri uri ->
326 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
327 Wsi.setcursor Wsi.CURSOR_INFO
328 | Utext s ->
329 if conf.underinfo then showtext 'f' ("ont: " ^ s);
330 Wsi.setcursor Wsi.CURSOR_TEXT
331 | Uannotation _ ->
332 if conf.underinfo then showtext 'a' "nnotation";
333 Wsi.setcursor Wsi.CURSOR_INFO
336 let showlinktype under =
337 if conf.underinfo && under != Unone
338 then showtext ' ' @@ undertext under
341 let [@warning "-4"] intentry_with_suffix text key =
342 let text =
343 match key with
344 | Keys.Ascii ('0'..'9' as c) -> addchar text c
345 | Keys.Ascii ('k' | 'm' | 'g' | 'K' | 'M' | 'G' as c) ->
346 addchar text @@ asciilower c
347 | _ ->
348 state.text <- Printf.sprintf "invalid key";
349 text
351 TEcont text
354 let wcmd fmt =
355 let b = Buffer.create 16 in
356 Printf.kbprintf
357 (fun b ->
358 let b = Buffer.to_bytes b in
359 wcmd state.ss b @@ Bytes.length b
360 ) b fmt
363 let nogeomcmds cmds =
364 match cmds with
365 | s, [] -> emptystr s
366 | _ -> false
369 let layoutN ((columns, coverA, coverB), b) x y sw sh =
370 let rec fold accu n =
371 if n = Array.length b
372 then accu
373 else
374 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
375 if (vy - y) > sh &&
376 (n = coverA - 1
377 || n = state.pagecount - coverB
378 || (n - coverA) mod columns = columns - 1)
379 then accu
380 else
381 let accu =
382 if vy + h > y
383 then
384 let pagey = max 0 (y - vy) in
385 let pagedispy = if pagey > 0 then 0 else vy - y in
386 let pagedispx, pagex =
387 let pdx =
388 if n = coverA - 1 || n = state.pagecount - coverB
389 then x + (sw - w) / 2
390 else dx + xoff + x
392 if pdx < 0
393 then 0, -pdx
394 else pdx, 0
396 let pagevw =
397 let vw = sw - pagedispx in
398 let pw = w - pagex in
399 min vw pw
401 let pagevh = min (h - pagey) (sh - pagedispy) in
402 if pagevw > 0 && pagevh > 0
403 then
404 let e =
405 { pageno = n
406 ; pagedimno = pdimno
407 ; pagew = w
408 ; pageh = h
409 ; pagex = pagex
410 ; pagey = pagey
411 ; pagevw = pagevw
412 ; pagevh = pagevh
413 ; pagedispx = pagedispx
414 ; pagedispy = pagedispy
415 ; pagecol = 0
418 e :: accu
419 else
420 accu
421 else
422 accu
424 fold accu (n+1)
426 if Array.length b = 0
427 then []
428 else List.rev (fold [] (page_of_y y))
431 let layoutS (columns, b) x y sw sh =
432 let rec fold accu n =
433 if n = Array.length b
434 then accu
435 else
436 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
437 if (vy - y) > sh
438 then accu
439 else
440 let accu =
441 if vy + pageh > y
442 then
443 let x = xoff + x in
444 let pagey = max 0 (y - vy) in
445 let pagedispy = if pagey > 0 then 0 else vy - y in
446 let pagedispx, pagex =
447 if px = 0
448 then (
449 if x < 0
450 then 0, -x
451 else x, 0
453 else (
454 let px = px - x in
455 if px < 0
456 then -px, 0
457 else 0, px
460 let pagecolw = pagew/columns in
461 let pagedispx =
462 if pagecolw < sw
463 then pagedispx + ((sw - pagecolw) / 2)
464 else pagedispx
466 let pagevw =
467 let vw = sw - pagedispx in
468 let pw = pagew - pagex in
469 min vw pw
471 let pagevw = min pagevw pagecolw in
472 let pagevh = min (pageh - pagey) (sh - pagedispy) in
473 if pagevw > 0 && pagevh > 0
474 then
475 let e =
476 { pageno = n/columns
477 ; pagedimno = pdimno
478 ; pagew = pagew
479 ; pageh = pageh
480 ; pagex = pagex
481 ; pagey = pagey
482 ; pagevw = pagevw
483 ; pagevh = pagevh
484 ; pagedispx = pagedispx
485 ; pagedispy = pagedispy
486 ; pagecol = n mod columns
489 e :: accu
490 else
491 accu
492 else
493 accu
495 fold accu (n+1)
497 List.rev (fold [] 0)
500 let layout x y sw sh =
501 if nogeomcmds state.geomcmds
502 then
503 match conf.columns with
504 | Csingle b -> layoutN ((1, 0, 0), b) x y sw sh
505 | Cmulti c -> layoutN c x y sw sh
506 | Csplit s -> layoutS s x y sw sh
507 else []
510 let clamp incr =
511 let y = state.y + incr in
512 let y = max 0 y in
513 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
517 let itertiles l f =
518 let tilex = l.pagex mod conf.tilew in
519 let tiley = l.pagey mod conf.tileh in
521 let col = l.pagex / conf.tilew in
522 let row = l.pagey / conf.tileh in
524 let rec rowloop row y0 dispy h =
525 if h = 0
526 then ()
527 else (
528 let dh = conf.tileh - y0 in
529 let dh = min h dh in
530 let rec colloop col x0 dispx w =
531 if w = 0
532 then ()
533 else (
534 let dw = conf.tilew - x0 in
535 let dw = min w dw in
536 f col row dispx dispy x0 y0 dw dh;
537 colloop (col+1) 0 (dispx+dw) (w-dw)
540 colloop col tilex l.pagedispx l.pagevw;
541 rowloop (row+1) 0 (dispy+dh) (h-dh)
544 if l.pagevw > 0 && l.pagevh > 0
545 then rowloop row tiley l.pagedispy l.pagevh;
548 let gettileopaque l col row =
549 let key =
550 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
552 try Some (Hashtbl.find state.tilemap key)
553 with Not_found -> None
556 let puttileopaque l col row gen colorspace angle opaque size elapsed =
557 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
558 Hashtbl.add state.tilemap key (opaque, size, elapsed)
561 let filledrect2 x0 y0 x1 y1 x2 y2 x3 y3 =
562 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x1; y1; x2; y2; x3; y3 |];
563 GlArray.vertex `two state.vraw;
564 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
567 let filledrect1 x0 y0 x1 y1 = filledrect2 x0 y0 x0 y1 x1 y0 x1 y1;;
569 let filledrect x0 y0 x1 y1 =
570 GlArray.disable `texture_coord;
571 filledrect1 x0 y0 x1 y1;
572 GlArray.enable `texture_coord;
575 let linerect x0 y0 x1 y1 =
576 GlArray.disable `texture_coord;
577 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
578 GlArray.vertex `two state.vraw;
579 GlArray.draw_arrays `line_loop ~first:0 ~count:4;
580 GlArray.enable `texture_coord;
583 let drawtiles l color =
584 GlDraw.color color;
585 begintiles ();
586 let f col row x y tilex tiley w h =
587 match gettileopaque l col row with
588 | Some (opaque, _, t) ->
589 let params = x, y, w, h, tilex, tiley in
590 if conf.invert
591 then GlTex.env (`mode `blend);
592 drawtile params opaque;
593 if conf.invert
594 then GlTex.env (`mode `modulate);
595 if conf.debug
596 then (
597 endtiles ();
598 let s = Printf.sprintf
599 "%d[%d,%d] %f sec"
600 l.pageno col row t
602 let w = measurestr fstate.fontsize s in
603 GlDraw.color (0.0, 0.0, 0.0);
604 filledrect (float (x-2))
605 (float (y-2))
606 (float (x+2) +. w)
607 (float (y + fstate.fontsize + 2));
608 GlDraw.color color;
609 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
610 begintiles ();
613 | None ->
614 endtiles ();
615 let w =
616 let lw = state.winw - x in
617 min lw w
618 and h =
619 let lh = state.winh - y in
620 min lh h
622 if conf.invert
623 then GlTex.env (`mode `blend);
624 begin match state.checkerstexid with
625 | Some id ->
626 Gl.enable `texture_2d;
627 GlTex.bind_texture ~target:`texture_2d id;
628 let x0 = float x
629 and y0 = float y
630 and x1 = float (x+w)
631 and y1 = float (y+h) in
633 let tw = float w /. 16.0
634 and th = float h /. 16.0 in
635 let tx0 = float tilex /. 16.0
636 and ty0 = float tiley /. 16.0 in
637 let tx1 = tx0 +. tw
638 and ty1 = ty0 +. th in
639 Raw.sets_float state.vraw ~pos:0
640 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
641 Raw.sets_float state.traw ~pos:0
642 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
643 GlArray.vertex `two state.vraw;
644 GlArray.tex_coord `two state.traw;
645 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
646 Gl.disable `texture_2d;
648 | None ->
649 GlDraw.color (1.0, 1.0, 1.0);
650 filledrect (float x) (float y) (float (x+w)) (float (y+h));
651 end;
652 if conf.invert
653 then GlTex.env (`mode `modulate);
654 if w > 128 && h > fstate.fontsize + 10
655 then (
656 let c = if conf.invert then 1.0 else 0.0 in
657 GlDraw.color (c, c, c);
658 let c, r =
659 if conf.verbose
660 then (col*conf.tilew, row*conf.tileh)
661 else col, row
663 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
665 GlDraw.color color;
666 begintiles ();
668 itertiles l f;
669 endtiles ();
672 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
674 let tilevisible1 l x y =
675 let ax0 = l.pagex
676 and ax1 = l.pagex + l.pagevw
677 and ay0 = l.pagey
678 and ay1 = l.pagey + l.pagevh in
680 let bx0 = x
681 and by0 = y in
682 let bx1 = min (bx0 + conf.tilew) l.pagew
683 and by1 = min (by0 + conf.tileh) l.pageh in
685 let rx0 = max ax0 bx0
686 and ry0 = max ay0 by0
687 and rx1 = min ax1 bx1
688 and ry1 = min ay1 by1 in
690 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
691 nonemptyintersection
694 let tilevisible layout n x y =
695 let rec findpageinlayout m = function
696 | l :: rest when l.pageno = n ->
697 tilevisible1 l x y || (
698 match conf.columns with
699 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
700 | Csplit _ | Csingle _ | Cmulti _ -> false
702 | _ :: rest -> findpageinlayout 0 rest
703 | [] -> false
705 findpageinlayout 0 layout;
708 let tileready l x y =
709 tilevisible1 l x y &&
710 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
713 let tilepage n p layout =
714 let rec loop = function
715 | l :: rest ->
716 if l.pageno = n
717 then
718 let f col row _ _ _ _ _ _ =
719 if state.currently = Idle
720 then
721 match gettileopaque l col row with
722 | Some _ -> ()
723 | None ->
724 let x = col*conf.tilew
725 and y = row*conf.tileh in
726 let w =
727 let w = l.pagew - x in
728 min w conf.tilew
730 let h =
731 let h = l.pageh - y in
732 min h conf.tileh
734 let pbo =
735 if conf.usepbo
736 then getpbo w h conf.colorspace
737 else ~< "0"
739 wcmd "tile %s %d %d %d %d %s"
740 (~> p) x y w h (~> pbo);
741 state.currently <-
742 Tiling (
743 l, p, conf.colorspace, conf.angle,
744 state.gen, col, row, conf.tilew, conf.tileh
747 itertiles l f;
748 else
749 loop rest
751 | [] -> ()
753 if nogeomcmds state.geomcmds
754 then loop layout;
757 let preloadlayout x y sw sh =
758 let y = if y < sh then 0 else y - sh in
759 let x = min 0 (x + sw) in
760 let h = sh*3 in
761 let w = sw*3 in
762 layout x y w h;
765 let load pages =
766 let rec loop pages =
767 if state.currently != Idle
768 then ()
769 else
770 match pages with
771 | l :: rest ->
772 begin match getopaque l.pageno with
773 | None ->
774 wcmd "page %d %d" l.pageno l.pagedimno;
775 state.currently <- Loading (l, state.gen);
776 | Some opaque ->
777 tilepage l.pageno opaque pages;
778 loop rest
779 end;
780 | _ -> ()
782 if nogeomcmds state.geomcmds
783 then loop pages
786 let preload pages =
787 load pages;
788 if conf.preload && state.currently = Idle
789 then load (preloadlayout state.x state.y state.winw state.winh);
792 let layoutready layout =
793 let rec fold all ls =
794 all && match ls with
795 | l :: rest ->
796 let seen = ref false in
797 let allvisible = ref true in
798 let foo col row _ _ _ _ _ _ =
799 seen := true;
800 allvisible := !allvisible &&
801 begin match gettileopaque l col row with
802 | Some _ -> true
803 | None -> false
806 itertiles l foo;
807 fold (!seen && !allvisible) rest
808 | [] -> true
810 let alltilesvisible = fold true layout in
811 alltilesvisible;
814 let gotoxy x y =
815 let y = bound y 0 state.maxy in
816 let y, layout, proceed =
817 match conf.maxwait with
818 | Some time when state.ghyll == noghyll ->
819 begin match state.throttle with
820 | None ->
821 let layout = layout x y state.winw state.winh in
822 let ready = layoutready layout in
823 if not ready
824 then (
825 load layout;
826 state.throttle <- Some (layout, y, now ());
828 else G.postRedisplay "gotoxy showall (None)";
829 y, layout, ready
830 | Some (_, _, started) ->
831 let dt = now () -. started in
832 if dt > time
833 then (
834 state.throttle <- None;
835 let layout = layout x y state.winw state.winh in
836 load layout;
837 G.postRedisplay "maxwait";
838 y, layout, true
840 else -1, [], false
843 | _ ->
844 let layout = layout x y state.winw state.winh in
845 if not !wtmode || layoutready layout
846 then G.postRedisplay "gotoxy ready";
847 y, layout, true
849 if proceed
850 then (
851 state.x <- x;
852 state.y <- y;
853 state.layout <- layout;
854 begin match state.mode with
855 | LinkNav ln ->
856 begin match ln with
857 | Ltexact (pageno, linkno) ->
858 let rec loop = function
859 | [] ->
860 state.lnava <- Some (pageno, linkno);
861 state.mode <- LinkNav (Ltgendir 0)
862 | l :: _ when l.pageno = pageno ->
863 begin match getopaque pageno with
864 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
865 | Some opaque ->
866 let x0, y0, x1, y1 = getlinkrect opaque linkno in
867 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
868 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
869 then state.mode <- LinkNav (Ltgendir 0)
871 | _ :: rest -> loop rest
873 loop layout
874 | Ltnotready _ | Ltgendir _ -> ()
876 | Birdseye _ | Textentry _ | View -> ()
877 end;
878 begin match state.mode with
879 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
880 if not (pagevisible layout pageno)
881 then (
882 match state.layout with
883 | [] -> ()
884 | l :: _ ->
885 state.mode <- Birdseye (
886 conf, leftx, l.pageno, hooverpageno, anchor
889 | LinkNav lt ->
890 begin match lt with
891 | Ltnotready (_, dir)
892 | Ltgendir dir ->
893 let linknav =
894 let rec loop = function
895 | [] -> lt
896 | l :: rest ->
897 match getopaque l.pageno with
898 | None -> Ltnotready (l.pageno, dir)
899 | Some opaque ->
900 let link =
901 let ld =
902 if dir = 0
903 then LDfirstvisible (l.pagex, l.pagey, dir)
904 else (
905 if dir > 0 then LDfirst else LDlast
908 findlink opaque ld
910 match link with
911 | Lnotfound -> loop rest
912 | Lfound n ->
913 showlinktype (getlink opaque n);
914 Ltexact (l.pageno, n)
916 loop state.layout
918 state.mode <- LinkNav linknav
919 | Ltexact _ -> ()
921 | Textentry _ | View -> ()
922 end;
923 preload layout;
925 state.ghyll <- noghyll;
926 if conf.updatecurs
927 then (
928 let mx, my = state.mpos in
929 updateunder mx my;
933 let conttiling pageno opaque =
934 tilepage pageno opaque
935 (if conf.preload
936 then preloadlayout state.x state.y state.winw state.winh
937 else state.layout)
940 let gotoxy_and_clear_text x y =
941 if not conf.verbose then state.text <- E.s;
942 gotoxy x y;
945 let getanchory (n, top, dtop) =
946 let y, h = getpageyh n in
947 if conf.presentation
948 then
949 let ips = calcips h in
950 y + truncate (top*.float h -. dtop*.float ips) + ips;
951 else
952 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
955 let gotoanchor anchor =
956 gotoxy state.x (getanchory anchor);
959 let addnav () =
960 cbput state.hists.nav (getanchor ());
963 let getnav dir =
964 let anchor = cbgetc state.hists.nav dir in
965 getanchory anchor;
968 let gotoghyll1 single y =
969 let scroll f n a b =
970 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
971 let snake f a b =
972 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
973 if f < a
974 then s (float f /. float a)
975 else (
976 if f > b
977 then 1.0 -. s ((float (f-b) /. float (n-b)))
978 else 1.0
981 snake f a b
982 and summa n a b =
983 let ins = float a *. 0.5
984 and outs = float (n-b) *. 0.5 in
985 let ones = b - a in
986 ins +. outs +. float ones
988 let rec set nab y sy =
989 let (_N, _A, _B), y =
990 if single
991 then
992 let scl = if y > sy then 2 else -2 in
993 let _N, _, _ = nab in
994 (_N,0,_N), y+conf.scrollstep*scl
995 else nab,y in
996 let sum = summa _N _A _B in
997 let dy = float (y - sy) in
998 state.ghyll <- (
999 let rec gf n y1 o =
1000 if n >= _N
1001 then state.ghyll <- noghyll
1002 else
1003 let go n =
1004 let s = scroll n _N _A _B in
1005 let y1 = y1 +. ((s *. dy) /. sum) in
1006 gotoxy_and_clear_text state.x (truncate y1);
1007 state.ghyll <- gf (n+1) y1;
1009 match o with
1010 | None -> go n
1011 | Some y' when single -> set nab y' state.y
1012 | Some y' -> set (_N/2, 1, 1) y' state.y
1014 gf 0 (float state.y)
1017 match conf.ghyllscroll with
1018 | Some nab when not conf.presentation ->
1019 if state.ghyll == noghyll
1020 then set nab y state.y
1021 else state.ghyll (Some y)
1022 | _ ->
1023 gotoxy_and_clear_text state.x y
1026 let gotoghyll = gotoghyll1 false;;
1028 let gotopage n top =
1029 let y, h = getpageyh n in
1030 let y = y + (truncate (top *. float h)) in
1031 gotoghyll y
1034 let gotopage1 n top =
1035 let y = getpagey n in
1036 let y = y + top in
1037 gotoghyll y
1040 let invalidate s f =
1041 state.redisplay <- false;
1042 state.layout <- [];
1043 state.pdims <- [];
1044 state.rects <- [];
1045 state.rects1 <- [];
1046 match state.geomcmds with
1047 | ps, [] when emptystr ps ->
1048 f ();
1049 state.geomcmds <- s, [];
1051 | ps, [] ->
1052 state.geomcmds <- ps, [s, f];
1054 | ps, (s', _) :: rest when s' = s ->
1055 state.geomcmds <- ps, ((s, f) :: rest);
1057 | ps, cmds ->
1058 state.geomcmds <- ps, ((s, f) :: cmds);
1061 let flushpages () =
1062 Hashtbl.iter (fun _ opaque ->
1063 wcmd "freepage %s" (~> opaque);
1064 ) state.pagemap;
1065 Hashtbl.clear state.pagemap;
1068 let flushtiles () =
1069 if not (Queue.is_empty state.tilelru)
1070 then (
1071 Queue.iter (fun (k, p, s) ->
1072 wcmd "freetile %s" (~> p);
1073 state.memused <- state.memused - s;
1074 Hashtbl.remove state.tilemap k;
1075 ) state.tilelru;
1076 state.uioh#infochanged Memused;
1077 Queue.clear state.tilelru;
1079 load state.layout;
1082 let stateh h =
1083 let h = truncate (float h*.conf.zoom) in
1084 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1085 h - d
1088 let opendoc path password =
1089 state.path <- path;
1090 state.password <- password;
1091 state.gen <- state.gen + 1;
1092 state.docinfo <- [];
1093 state.outlines <- [||];
1095 flushpages ();
1096 setaalevel conf.aalevel;
1097 let titlepath =
1098 if emptystr state.origin
1099 then path
1100 else state.origin
1102 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1103 wcmd "open %d %d %d %s\000%s\000%s\000"
1104 (btod !wtmode) (btod !cxack) (btod conf.usedoccss)
1105 path password conf.css;
1106 invalidate "reqlayout"
1107 (fun () ->
1108 wcmd "reqlayout %d %d %d %s\000"
1109 conf.angle (FMTE.to_int conf.fitmodel)
1110 (stateh state.winh) state.nameddest
1112 state.help <-
1113 let sl = keystostrlist conf in
1114 let rec loop accu =
1115 function | [] -> accu
1116 | s :: rest -> loop ((s, 0, Noaction) :: accu) rest
1117 in makehelp () @ (("", 0, Noaction) :: loop [] sl) |> Array.of_list
1120 let reload () =
1121 state.anchor <- getanchor ();
1122 opendoc state.path state.password;
1125 let scalecolor c =
1126 let c = c *. conf.colorscale in
1127 (c, c, c);
1130 let scalecolor2 (r, g, b) =
1131 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1134 let docolumns columns =
1135 match columns with
1136 | Csingle _ ->
1137 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1138 let rec loop pageno pdimno pdim y ph pdims =
1139 if pageno = state.pagecount
1140 then ()
1141 else
1142 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1143 match pdims with
1144 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1145 pdimno+1, pdim, rest
1146 | _ ->
1147 pdimno, pdim, pdims
1149 let x = max 0 (((state.winw - w) / 2) - xoff) in
1150 let y =
1151 y + (if conf.presentation
1152 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1153 else (if pageno = 0 then 0 else conf.interpagespace)
1156 a.(pageno) <- (pdimno, x, y, pdim);
1157 loop (pageno+1) pdimno pdim (y + h) h pdims
1159 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1160 conf.columns <- Csingle a;
1162 | Cmulti ((columns, coverA, coverB), _) ->
1163 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1164 let rec loop pageno pdimno pdim x y rowh pdims =
1165 let rec fixrow m =
1166 if m = pageno then () else
1167 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1168 if h < rowh
1169 then (
1170 let y = y + (rowh - h) / 2 in
1171 a.(m) <- (pdimno, x, y, pdim);
1173 fixrow (m+1)
1175 if pageno = state.pagecount
1176 then fixrow (((pageno - 1) / columns) * columns)
1177 else
1178 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1179 match pdims with
1180 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1181 pdimno+1, pdim, rest
1182 | _ ->
1183 pdimno, pdim, pdims
1185 let x, y, rowh' =
1186 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1187 then (
1188 let x = (state.winw - w) / 2 in
1189 let ips =
1190 if conf.presentation then calcips h else conf.interpagespace in
1191 x, y + ips + rowh, h
1193 else (
1194 if (pageno - coverA) mod columns = 0
1195 then (
1196 let x = max 0 (state.winw - state.w) / 2 in
1197 let y =
1198 if conf.presentation
1199 then
1200 let ips = calcips h in
1201 y + (if pageno = 0 then 0 else calcips rowh + ips)
1202 else
1203 y + (if pageno = 0 then 0 else conf.interpagespace)
1205 x, y + rowh, h
1207 else x, y, max rowh h
1210 let y =
1211 if pageno > 1 && (pageno - coverA) mod columns = 0
1212 then (
1213 let y =
1214 if pageno = columns && conf.presentation
1215 then (
1216 let ips = calcips rowh in
1217 for i = 0 to pred columns
1219 let (pdimno, x, y, pdim) = a.(i) in
1220 a.(i) <- (pdimno, x, y+ips, pdim)
1221 done;
1222 y+ips;
1224 else y
1226 fixrow (pageno - columns);
1229 else y
1231 a.(pageno) <- (pdimno, x, y, pdim);
1232 let x = x + w + xoff*2 + conf.interpagespace in
1233 loop (pageno+1) pdimno pdim x y rowh' pdims
1235 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1236 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1238 | Csplit (c, _) ->
1239 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1240 let rec loop pageno pdimno pdim y pdims =
1241 if pageno = state.pagecount
1242 then ()
1243 else
1244 let pdimno, ((_, w, h, _) as pdim), pdims =
1245 match pdims with
1246 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1247 pdimno+1, pdim, rest
1248 | _ ->
1249 pdimno, pdim, pdims
1251 let cw = w / c in
1252 let rec loop1 n x y =
1253 if n = c then y else (
1254 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1255 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1258 let y = loop1 0 0 y in
1259 loop (pageno+1) pdimno pdim y pdims
1261 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1262 conf.columns <- Csplit (c, a);
1265 let represent () =
1266 docolumns conf.columns;
1267 state.maxy <- calcheight ();
1268 if state.reprf == noreprf
1269 then (
1270 match state.mode with
1271 | Birdseye (_, _, pageno, _, _) ->
1272 let y, h = getpageyh pageno in
1273 let top = (state.winh - h) / 2 in
1274 gotoxy state.x (max 0 (y - top))
1275 | Textentry _ | View | LinkNav _ ->
1276 let y = getanchory state.anchor in
1277 let y = min y (state.maxy - state.winh) in
1278 gotoxy state.x y;
1280 else (
1281 state.reprf ();
1282 state.reprf <- noreprf;
1286 let reshape ?(firsttime=false) w h =
1287 GlDraw.viewport ~x:0 ~y:0 ~w ~h;
1288 if not firsttime && nogeomcmds state.geomcmds
1289 then state.anchor <- getanchor ();
1291 state.winw <- w;
1292 let w = truncate (float w *. conf.zoom) in
1293 let w = max w 2 in
1294 state.winh <- h;
1295 setfontsize fstate.fontsize;
1296 GlMat.mode `modelview;
1297 GlMat.load_identity ();
1299 GlMat.mode `projection;
1300 GlMat.load_identity ();
1301 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1302 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1303 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1305 let relx =
1306 if conf.zoom <= 1.0
1307 then 0.0
1308 else float state.x /. float state.w
1310 invalidate "geometry"
1311 (fun () ->
1312 state.w <- w;
1313 if not firsttime
1314 then state.x <- truncate (relx *. float w);
1315 let w =
1316 match conf.columns with
1317 | Csingle _ -> w
1318 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1319 | Csplit (c, _) -> w * c
1321 wcmd "geometry %d %d %d"
1322 w (stateh h) (FMTE.to_int conf.fitmodel)
1326 let enttext () =
1327 let len = String.length state.text in
1328 let x0 = if conf.leftscroll then vscrollw () else 0 in
1329 let drawstring s =
1330 let hscrollh =
1331 match state.mode with
1332 | Textentry _ | View | LinkNav _ ->
1333 let h, _, _ = state.uioh#scrollpw in
1335 | Birdseye _ -> 0
1337 let rect x w =
1338 filledrect
1339 x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1340 (x+.w) (float (state.winh - hscrollh))
1343 let w = float (state.winw - 1 - vscrollw ()) in
1344 if state.progress >= 0.0 && state.progress < 1.0
1345 then (
1346 GlDraw.color (0.3, 0.3, 0.3);
1347 let w1 = w *. state.progress in
1348 rect (float x0) w1;
1349 GlDraw.color (0.0, 0.0, 0.0);
1350 rect (float x0+.w1) (float x0+.w-.w1)
1352 else (
1353 GlDraw.color (0.0, 0.0, 0.0);
1354 rect (float x0) w;
1357 GlDraw.color (1.0, 1.0, 1.0);
1358 drawstring
1359 fstate.fontsize
1360 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1361 (state.winh - hscrollh - 5) s;
1363 let s =
1364 match state.mode with
1365 | Textentry ((prefix, text, _, _, _, _), _) ->
1366 let s =
1367 if len > 0
1368 then Printf.sprintf "%s%s_ [%s]" prefix text state.text
1369 else Printf.sprintf "%s%s_" prefix text
1373 | Birdseye _ | View | LinkNav _ -> state.text
1375 let s =
1376 if state.newerrmsgs
1377 then (
1378 if not (istextentry state.mode) && state.uioh#eformsgs
1379 then
1380 let s1 = "(press 'e' to review error messasges)" in
1381 if nonemptystr s then s ^ " " ^ s1 else s1
1382 else s
1384 else s
1386 if nonemptystr s
1387 then drawstring s
1390 let gctiles () =
1391 let len = Queue.length state.tilelru in
1392 let layout = lazy (
1393 match state.throttle with
1394 | None ->
1395 if conf.preload
1396 then preloadlayout state.x state.y state.winw state.winh
1397 else state.layout
1398 | Some (layout, _, _) ->
1399 layout
1400 ) in
1401 let rec loop qpos =
1402 if state.memused > conf.memlimit
1403 then (
1404 if qpos < len
1405 then
1406 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1407 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1408 let (_, pw, ph, _) = getpagedim n in
1409 if gen = state.gen
1410 && colorspace = conf.colorspace
1411 && angle = conf.angle
1412 && pagew = pw
1413 && pageh = ph
1414 && (
1415 let x = col*conf.tilew
1416 and y = row*conf.tileh in
1417 tilevisible (Lazy.force_val layout) n x y
1419 then Queue.push lruitem state.tilelru
1420 else (
1421 freepbo p;
1422 wcmd "freetile %s" (~> p);
1423 state.memused <- state.memused - s;
1424 state.uioh#infochanged Memused;
1425 Hashtbl.remove state.tilemap k;
1427 loop (qpos+1)
1430 loop 0
1433 let onpagerect pageno f =
1434 let b =
1435 match conf.columns with
1436 | Cmulti (_, b) -> b
1437 | Csingle b -> b
1438 | Csplit (_, b) -> b
1440 if pageno >= 0 && pageno < Array.length b
1441 then
1442 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1443 f w h
1446 let gotopagexy1 wtmode pageno x y =
1447 let _,w1,h1,leftx = getpagedim pageno in
1448 let top = y /. (float h1) in
1449 let left = x /. (float w1) in
1450 let py, w, h = getpageywh pageno in
1451 let wh = state.winh in
1452 let x = left *. (float w) in
1453 let x = leftx + state.x + truncate x in
1454 let sx =
1455 if x < 0 || x >= state.winw
1456 then state.x - x
1457 else state.x
1459 let pdy = truncate (top *. float h) in
1460 let y' = py + pdy in
1461 let dy = y' - state.y in
1462 let sy =
1463 if x != state.x || not (dy > 0 && dy < wh)
1464 then (
1465 if conf.presentation
1466 then
1467 if abs (py - y') > wh
1468 then y'
1469 else py
1470 else y';
1472 else state.y
1474 if state.x != sx || state.y != sy
1475 then (
1476 let x, y =
1477 if wtmode
1478 then (
1479 let ww = state.winw in
1480 let qx = sx / ww
1481 and qy = pdy / wh in
1482 let x = qx * ww
1483 and y = py + qy * wh in
1484 let x = if -x + ww > w1 then -(w1-ww) else x
1485 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1486 let y =
1487 if conf.presentation
1488 then
1489 if abs (py - y') > wh
1490 then y'
1491 else py
1492 else y';
1494 (x, y)
1496 else (sx, sy)
1498 gotoxy_and_clear_text x y;
1500 else gotoxy_and_clear_text state.x state.y;
1503 let gotopagexy wtmode pageno x y =
1504 match state.mode with
1505 | Birdseye _ -> gotopage pageno 0.0
1506 | Textentry _ | View | LinkNav _ -> gotopagexy1 wtmode pageno x y
1509 let getpassword () =
1510 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1511 if emptystr passcmd
1512 then E.s
1513 else getcmdoutput
1514 (fun s ->
1515 impmsg "error getting password: %s" s;
1516 dolog "%s" s) passcmd;
1519 let pgoto opaque pageno x y =
1520 let pdimno = getpdimno pageno in
1521 let x, y = project opaque pageno pdimno x y in
1522 gotopagexy false pageno x y;
1525 let act cmds =
1526 (* dolog "%S" cmds; *)
1527 let spl = splitatchar cmds ' ' in
1528 let scan s fmt f =
1529 try Scanf.sscanf s fmt f
1530 with exn ->
1531 dolog "error processing '%S': %s" cmds @@ exntos exn;
1532 exit 1
1534 let addoutline outline =
1535 match state.currently with
1536 | Outlining outlines -> state.currently <- Outlining (outline :: outlines)
1537 | Idle -> state.currently <- Outlining [outline]
1538 | Loading _ | Tiling _ ->
1539 dolog "invalid outlining state";
1540 logcurrently state.currently
1542 match spl with
1543 | "clear", "" ->
1544 state.pdims <- [];
1545 state.uioh#infochanged Pdim;
1547 | "clearrects", "" ->
1548 state.rects <- state.rects1;
1549 G.postRedisplay "clearrects";
1551 | "continue", args ->
1552 let n = scan args "%u" (fun n -> n) in
1553 state.pagecount <- n;
1554 begin match state.currently with
1555 | Outlining l ->
1556 state.currently <- Idle;
1557 state.outlines <- Array.of_list (List.rev l)
1558 | Idle | Loading _ | Tiling _ -> ()
1559 end;
1561 let cur, cmds = state.geomcmds in
1562 if emptystr cur
1563 then failwith "umpossible";
1565 begin match List.rev cmds with
1566 | [] ->
1567 state.geomcmds <- E.s, [];
1568 state.throttle <- None;
1569 represent ();
1570 | (s, f) :: rest ->
1571 f ();
1572 state.geomcmds <- s, List.rev rest;
1573 end;
1574 if conf.maxwait = None && not !wtmode
1575 then G.postRedisplay "continue";
1577 | "msg", args ->
1578 showtext ' ' args
1580 | "vmsg", args ->
1581 if conf.verbose
1582 then showtext ' ' args
1584 | "emsg", args ->
1585 Buffer.add_string state.errmsgs args;
1586 state.newerrmsgs <- true;
1587 G.postRedisplay "error message"
1589 | "progress", args ->
1590 let progress, text =
1591 scan args "%f %n"
1592 (fun f pos ->
1593 f, String.sub args pos (String.length args - pos))
1595 state.text <- text;
1596 state.progress <- progress;
1597 G.postRedisplay "progress"
1599 | "firstmatch", args ->
1600 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1601 scan args "%u %d %f %f %f %f %f %f %f %f"
1602 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1603 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1605 let y = (getpagey pageno) + truncate y0 in
1606 let x =
1607 if conf.zoom > 1.0
1608 then state.winw/2
1609 else state.x
1611 addnav ();
1612 gotoxy x y;
1613 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1614 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1616 | "match", args ->
1617 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1618 scan args "%u %d %f %f %f %f %f %f %f %f"
1619 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1620 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1622 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1623 state.rects1 <-
1624 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1626 | "page", args ->
1627 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1628 let pageopaque = ~< pageopaques in
1629 begin match state.currently with
1630 | Loading (l, gen) ->
1631 vlog "page %d took %f sec" l.pageno t;
1632 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1633 begin match state.throttle with
1634 | None ->
1635 let preloadedpages =
1636 if conf.preload
1637 then preloadlayout state.x state.y state.winw state.winh
1638 else state.layout
1640 let evict () =
1641 let set =
1642 List.fold_left (fun s l -> IntSet.add l.pageno s)
1643 IntSet.empty preloadedpages
1645 let evictedpages =
1646 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1647 if not (IntSet.mem pageno set)
1648 then (
1649 wcmd "freepage %s" (~> opaque);
1650 key :: accu
1652 else accu
1653 ) state.pagemap []
1655 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1657 evict ();
1658 state.currently <- Idle;
1659 if gen = state.gen
1660 then (
1661 tilepage l.pageno pageopaque state.layout;
1662 load state.layout;
1663 load preloadedpages;
1664 let visible = pagevisible state.layout l.pageno in
1665 if visible
1666 then (
1667 match state.mode with
1668 | LinkNav (Ltnotready (pageno, dir)) ->
1669 if pageno = l.pageno
1670 then (
1671 let link =
1672 let ld =
1673 if dir = 0
1674 then LDfirstvisible (l.pagex, l.pagey, dir)
1675 else (
1676 if dir > 0 then LDfirst else LDlast
1679 findlink pageopaque ld
1681 match link with
1682 | Lnotfound -> ()
1683 | Lfound n ->
1684 showlinktype (getlink pageopaque n);
1685 state.mode <- LinkNav (Ltexact (l.pageno, n))
1687 | LinkNav (Ltgendir _)
1688 | LinkNav (Ltexact _)
1689 | View
1690 | Birdseye _
1691 | Textentry _ -> ()
1694 if visible && layoutready state.layout
1695 then (
1696 G.postRedisplay "page";
1700 | Some (layout, _, _) ->
1701 state.currently <- Idle;
1702 tilepage l.pageno pageopaque layout;
1703 load state.layout
1704 end;
1706 | Idle | Tiling _ | Outlining _ ->
1707 dolog "Inconsistent loading state";
1708 logcurrently state.currently;
1709 exit 1
1712 | "tile" , args ->
1713 let (x, y, opaques, size, t) =
1714 scan args "%u %u %s %u %f"
1715 (fun x y p size t -> (x, y, p, size, t))
1717 let opaque = ~< opaques in
1718 begin match state.currently with
1719 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1720 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1722 unmappbo opaque;
1723 if tilew != conf.tilew || tileh != conf.tileh
1724 then (
1725 wcmd "freetile %s" (~> opaque);
1726 state.currently <- Idle;
1727 load state.layout;
1729 else (
1730 puttileopaque l col row gen cs angle opaque size t;
1731 state.memused <- state.memused + size;
1732 state.uioh#infochanged Memused;
1733 gctiles ();
1734 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1735 opaque, size) state.tilelru;
1737 let layout =
1738 match state.throttle with
1739 | None -> state.layout
1740 | Some (layout, _, _) -> layout
1743 state.currently <- Idle;
1744 if gen = state.gen
1745 && conf.colorspace = cs
1746 && conf.angle = angle
1747 && tilevisible layout l.pageno x y
1748 then conttiling l.pageno pageopaque;
1750 begin match state.throttle with
1751 | None ->
1752 preload state.layout;
1753 if gen = state.gen
1754 && conf.colorspace = cs
1755 && conf.angle = angle
1756 && tilevisible state.layout l.pageno x y
1757 && (not !wtmode || layoutready state.layout)
1758 then G.postRedisplay "tile nothrottle";
1760 | Some (layout, y, _) ->
1761 let ready = layoutready layout in
1762 if ready
1763 then (
1764 state.y <- y;
1765 state.layout <- layout;
1766 state.throttle <- None;
1767 G.postRedisplay "throttle";
1769 else load layout;
1770 end;
1773 | Idle | Loading _ | Outlining _ ->
1774 dolog "Inconsistent tiling state";
1775 logcurrently state.currently;
1776 exit 1
1779 | "pdim", args ->
1780 let (n, w, h, _) as pdim =
1781 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1783 let pdim =
1784 match conf.fitmodel with
1785 | FitWidth -> pdim
1786 | FitPage | FitProportional ->
1787 match conf.columns with
1788 | Csplit _ -> (n, w, h, 0)
1789 | Csingle _ | Cmulti _ -> pdim
1791 state.pdims <- pdim :: state.pdims;
1792 state.uioh#infochanged Pdim
1794 | "o", args ->
1795 let (l, n, t, h, pos) =
1796 scan args "%u %u %d %u %n"
1797 (fun l n t h pos -> l, n, t, h, pos)
1799 let s = String.sub args pos (String.length args - pos) in
1800 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1802 | "ou", args ->
1803 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1804 let s = String.sub args pos len in
1805 let pos2 = pos + len + 1 in
1806 let uri = String.sub args pos2 (String.length args - pos2) in
1807 addoutline (s, l, Ouri uri)
1809 | "on", args ->
1810 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1811 let s = String.sub args pos (String.length args - pos) in
1812 addoutline (s, l, Onone)
1814 | "a", args ->
1815 let (n, l, t) =
1816 scan args "%u %d %d" (fun n l t -> n, l, t)
1818 state.reprf <- (fun () -> gotopagexy !wtmode n (float l) (float t))
1820 | "info", args ->
1821 let c, v = splitatchar args '\t' in
1822 let s =
1823 if nonemptystr v
1824 then
1825 if c = "Title"
1826 then (
1827 conf.title <- v;
1828 Wsi.settitle v;
1829 args
1831 else
1832 if let len = String.length c in
1833 len > 6 && ((String.sub c (len-4) 4) = "date")
1834 then (
1835 if String.length v >= 7 && v.[0] = 'D' && v.[1] = ':'
1836 then
1837 let b = Buffer.create 10 in
1838 Printf.bprintf b "%s\t" c;
1839 let sub p l c =
1841 Buffer.add_substring b v p l;
1842 Buffer.add_char b c;
1843 with exn -> Buffer.add_string b @@ exntos exn
1845 sub 2 4 '/';
1846 sub 6 2 '/';
1847 sub 8 2 ' ';
1848 sub 10 2 ':';
1849 sub 12 2 ':';
1850 sub 14 2 ' ';
1851 Buffer.add_char b '[';
1852 Buffer.add_string b v;
1853 Buffer.add_char b ']';
1854 Buffer.contents b
1855 else args
1857 else args
1858 else args
1860 state.docinfo <- (1, s) :: state.docinfo
1862 | "infoend", "" ->
1863 state.docinfo <- List.rev state.docinfo;
1864 state.uioh#infochanged Docinfo
1866 | "pass", args ->
1867 if args = "fail"
1868 then Wsi.settitle "Wrong password";
1869 let password = getpassword () in
1870 if emptystr password
1871 then error "document is password protected"
1872 else opendoc state.path password
1874 | _ ->
1875 error "unknown cmd `%S'" cmds
1878 let onhist cb =
1879 let rc = cb.rc in
1880 let action = function
1881 | HCprev -> cbget cb ~-1
1882 | HCnext -> cbget cb 1
1883 | HCfirst -> cbget cb ~-(cb.rc)
1884 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1885 and cancel () = cb.rc <- rc
1886 in (action, cancel)
1889 let search pattern forward =
1890 match conf.columns with
1891 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1892 | Csingle _ | Cmulti _ ->
1893 if nonemptystr pattern
1894 then
1895 let pn, py =
1896 match state.layout with
1897 | [] -> 0, 0
1898 | l :: _ ->
1899 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1901 wcmd "search %d %d %d %d,%s\000"
1902 (btod conf.icase) pn py (btod forward) pattern;
1905 let [@warning "-4"] intentry text key =
1906 let text =
1907 match key with
1908 | Keys.Ascii ('0'..'9' as c) -> addchar text c
1909 | _ ->
1910 state.text <- "invalid key";
1911 text
1913 TEcont text
1916 let linknact f s =
1917 if nonemptystr s
1918 then (
1919 let n =
1920 let l = String.length s in
1921 let rec loop pos n =
1922 if pos = l
1923 then n
1924 else
1925 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1926 loop (pos+1) (n*26 + m)
1927 in loop 0 0
1929 let rec loop n = function
1930 | [] -> ()
1931 | l :: rest ->
1932 match getopaque l.pageno with
1933 | None -> loop n rest
1934 | Some opaque ->
1935 let m = getlinkcount opaque in
1936 if n < m
1937 then (
1938 let under = getlink opaque n in
1939 f under
1941 else loop (n-m) rest
1943 loop n state.layout;
1947 let [@warning "-4"] linknentry text = function
1948 | Keys.Ascii c ->
1949 let text = addchar text c in
1950 linknact (fun under -> state.text <- undertext under) text;
1951 TEcont text
1952 | _ ->
1953 state.text <- Printf.sprintf "invalid key";
1954 TEcont text
1957 let [@warning "-4"] textentry text = function
1958 | Keys.Ascii c -> TEcont (addchar text c)
1959 | Keys.Code c -> TEcont (text ^ toutf8 c)
1960 | _ -> TEcont text
1963 let reqlayout angle fitmodel =
1964 match state.throttle with
1965 | None ->
1966 if nogeomcmds state.geomcmds
1967 then state.anchor <- getanchor ();
1968 conf.angle <- angle mod 360;
1969 if conf.angle != 0
1970 then (
1971 match state.mode with
1972 | LinkNav _ -> state.mode <- View
1973 | Birdseye _ | Textentry _ | View -> ()
1975 conf.fitmodel <- fitmodel;
1976 invalidate
1977 "reqlayout"
1978 (fun () ->
1979 wcmd "reqlayout %d %d %d"
1980 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
1982 | _ -> ()
1985 let settrim trimmargins trimfuzz =
1986 if nogeomcmds state.geomcmds
1987 then state.anchor <- getanchor ();
1988 conf.trimmargins <- trimmargins;
1989 conf.trimfuzz <- trimfuzz;
1990 let x0, y0, x1, y1 = trimfuzz in
1991 invalidate
1992 "settrim" (fun () ->
1993 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
1994 flushpages ();
1997 let setzoom zoom =
1998 match state.throttle with
1999 | None ->
2000 let zoom = max 0.0001 zoom in
2001 if zoom <> conf.zoom
2002 then (
2003 state.prevzoom <- (conf.zoom, state.x);
2004 conf.zoom <- zoom;
2005 reshape state.winw state.winh;
2006 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2009 | Some (layout, y, started) ->
2010 let time =
2011 match conf.maxwait with
2012 | None -> 0.0
2013 | Some t -> t
2015 let dt = now () -. started in
2016 if dt > time
2017 then (
2018 state.y <- y;
2019 load layout;
2023 let pivotzoom ?(vw=min state.w state.winw)
2024 ?(vh=min (state.maxy-state.y) state.winh)
2025 ?(x=vw/2) ?(y=vh/2) zoom =
2026 let w = float state.w /. zoom in
2027 let hw = w /. 2.0 in
2028 let ratio = float vh /. float vw in
2029 let hh = hw *. ratio in
2030 let x0 = if zoom < 1.0 then 0.0 else float x -. hw in
2031 let y0 = float y -. hh in
2032 gotoxy (state.x - truncate x0) (state.y + truncate y0);
2033 setzoom zoom;
2036 let pivotzoom ?vw ?vh ?x ?y zoom =
2037 if nogeomcmds state.geomcmds then pivotzoom ?vw ?vh ?x ?y zoom
2040 let setcolumns mode columns coverA coverB =
2041 state.prevcolumns <- Some (conf.columns, conf.zoom);
2042 if columns < 0
2043 then (
2044 if isbirdseye mode
2045 then impmsg "split mode doesn't work in bird's eye"
2046 else (
2047 conf.columns <- Csplit (-columns, E.a);
2048 state.x <- 0;
2049 conf.zoom <- 1.0;
2052 else (
2053 if columns < 2
2054 then (
2055 conf.columns <- Csingle E.a;
2056 state.x <- 0;
2057 setzoom 1.0;
2059 else (
2060 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2061 conf.zoom <- 1.0;
2064 reshape state.winw state.winh;
2067 let resetmstate () =
2068 state.mstate <- Mnone;
2069 Wsi.setcursor Wsi.CURSOR_INHERIT;
2072 let enterbirdseye () =
2073 let zoom = float conf.thumbw /. float state.winw in
2074 let birdseyepageno =
2075 let cy = state.winh / 2 in
2076 let fold = function
2077 | [] -> 0
2078 | l :: rest ->
2079 let rec fold best = function
2080 | [] -> best.pageno
2081 | l :: rest ->
2082 let d = cy - (l.pagedispy + l.pagevh/2)
2083 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2084 if abs d < abs dbest
2085 then fold l rest
2086 else best.pageno
2087 in fold l rest
2089 fold state.layout
2091 state.mode <-
2092 Birdseye (
2093 { conf with zoom = conf.zoom },
2094 state.x, birdseyepageno, -1, getanchor ()
2096 resetmstate ();
2097 conf.zoom <- zoom;
2098 conf.presentation <- false;
2099 conf.interpagespace <- 10;
2100 conf.hlinks <- false;
2101 conf.fitmodel <- FitPage;
2102 state.x <- 0;
2103 conf.maxwait <- None;
2104 conf.columns <- (
2105 match conf.beyecolumns with
2106 | Some c ->
2107 conf.zoom <- 1.0;
2108 Cmulti ((c, 0, 0), E.a)
2109 | None -> Csingle E.a
2111 if conf.verbose
2112 then
2113 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2114 (100.0*.zoom)
2115 else
2116 state.text <- E.s
2118 reshape state.winw state.winh;
2121 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2122 state.mode <- View;
2123 conf.zoom <- c.zoom;
2124 conf.presentation <- c.presentation;
2125 conf.interpagespace <- c.interpagespace;
2126 conf.maxwait <- c.maxwait;
2127 conf.hlinks <- c.hlinks;
2128 conf.fitmodel <- c.fitmodel;
2129 conf.beyecolumns <- (
2130 match conf.columns with
2131 | Cmulti ((c, _, _), _) -> Some c
2132 | Csingle _ -> None
2133 | Csplit _ -> failwith "leaving bird's eye split mode"
2135 conf.columns <- (
2136 match c.columns with
2137 | Cmulti (c, _) -> Cmulti (c, E.a)
2138 | Csingle _ -> Csingle E.a
2139 | Csplit (c, _) -> Csplit (c, E.a)
2141 if conf.verbose
2142 then
2143 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2144 (100.0*.conf.zoom)
2146 reshape state.winw state.winh;
2147 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2148 state.x <- leftx;
2151 let togglebirdseye () =
2152 match state.mode with
2153 | Birdseye vals -> leavebirdseye vals true
2154 | View -> enterbirdseye ()
2155 | Textentry _ | LinkNav _ -> ()
2158 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2159 let pageno = max 0 (pageno - incr) in
2160 let rec loop = function
2161 | [] -> gotopage1 pageno 0
2162 | l :: _ when l.pageno = pageno ->
2163 if l.pagedispy >= 0 && l.pagey = 0
2164 then G.postRedisplay "upbirdseye"
2165 else gotopage1 pageno 0
2166 | _ :: rest -> loop rest
2168 loop state.layout;
2169 state.text <- E.s;
2170 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2173 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2174 let pageno = min (state.pagecount - 1) (pageno + incr) in
2175 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2176 let rec loop = function
2177 | [] ->
2178 let y, h = getpageyh pageno in
2179 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2180 gotoxy state.x (clamp dy)
2181 | l :: _ when l.pageno = pageno ->
2182 if l.pagevh != l.pageh
2183 then gotoxy state.x (clamp (l.pageh - l.pagevh + conf.interpagespace))
2184 else G.postRedisplay "downbirdseye"
2185 | _ :: rest -> loop rest
2187 loop state.layout;
2188 state.text <- E.s;
2191 let [@warning "-4"] optentry mode _ key =
2192 let btos b = if b then "on" else "off" in
2193 match key with
2194 | Keys.Ascii 's' ->
2195 let ondone s =
2196 try conf.scrollstep <- int_of_string s with exn ->
2197 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2199 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2201 | Keys.Ascii 'A' ->
2202 let ondone s =
2204 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2205 if state.autoscroll <> None
2206 then state.autoscroll <- Some conf.autoscrollstep
2207 with exn ->
2208 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2210 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2212 | Keys.Ascii 'C' ->
2213 let ondone s =
2215 let n, a, b = multicolumns_of_string s in
2216 setcolumns mode n a b;
2217 with exn ->
2218 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exn
2220 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2222 | Keys.Ascii 'Z' ->
2223 let ondone s =
2225 let zoom = float (int_of_string s) /. 100.0 in
2226 pivotzoom zoom
2227 with exn ->
2228 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2230 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2232 | Keys.Ascii 't' ->
2233 let ondone s =
2235 conf.thumbw <- bound (int_of_string s) 2 4096;
2236 state.text <-
2237 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2238 begin match mode with
2239 | Birdseye beye ->
2240 leavebirdseye beye false;
2241 enterbirdseye ();
2242 | Textentry _ | View | LinkNav _ -> ();
2244 with exn ->
2245 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2247 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2249 | Keys.Ascii 'R' ->
2250 let ondone s =
2251 match int_of_string s with
2252 | angle -> reqlayout angle conf.fitmodel
2253 | exception exn ->
2254 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2256 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2258 | Keys.Ascii 'i' ->
2259 conf.icase <- not conf.icase;
2260 TEdone ("case insensitive search " ^ (btos conf.icase))
2262 | Keys.Ascii 'p' ->
2263 conf.preload <- not conf.preload;
2264 gotoxy state.x state.y;
2265 TEdone ("preload " ^ (btos conf.preload))
2267 | Keys.Ascii 'v' ->
2268 conf.verbose <- not conf.verbose;
2269 TEdone ("verbose " ^ (btos conf.verbose))
2271 | Keys.Ascii 'd' ->
2272 conf.debug <- not conf.debug;
2273 TEdone ("debug " ^ (btos conf.debug))
2275 | Keys.Ascii 'h' ->
2276 conf.maxhfit <- not conf.maxhfit;
2277 state.maxy <- calcheight ();
2278 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2280 | Keys.Ascii 'c' ->
2281 conf.crophack <- not conf.crophack;
2282 TEdone ("crophack " ^ btos conf.crophack)
2284 | Keys.Ascii 'a' ->
2285 let s =
2286 match conf.maxwait with
2287 | None ->
2288 conf.maxwait <- Some infinity;
2289 "always wait for page to complete"
2290 | Some _ ->
2291 conf.maxwait <- None;
2292 "show placeholder if page is not ready"
2294 TEdone s
2296 | Keys.Ascii 'f' ->
2297 conf.underinfo <- not conf.underinfo;
2298 TEdone ("underinfo " ^ btos conf.underinfo)
2300 | Keys.Ascii 'P' ->
2301 conf.savebmarks <- not conf.savebmarks;
2302 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2304 | Keys.Ascii 'S' ->
2305 let ondone s =
2307 let pageno, py =
2308 match state.layout with
2309 | [] -> 0, 0
2310 | l :: _ ->
2311 l.pageno, l.pagey
2313 conf.interpagespace <- int_of_string s;
2314 docolumns conf.columns;
2315 state.maxy <- calcheight ();
2316 let y = getpagey pageno in
2317 gotoxy state.x (y + py)
2318 with exn ->
2319 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2321 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2323 | Keys.Ascii 'l' ->
2324 let fm =
2325 match conf.fitmodel with
2326 | FitProportional -> FitWidth
2327 | FitWidth | FitPage -> FitProportional
2329 reqlayout conf.angle fm;
2330 TEdone ("proportional display " ^ btos (fm == FitProportional))
2332 | Keys.Ascii 'T' ->
2333 settrim (not conf.trimmargins) conf.trimfuzz;
2334 TEdone ("trim margins " ^ btos conf.trimmargins)
2336 | Keys.Ascii 'I' ->
2337 conf.invert <- not conf.invert;
2338 TEdone ("invert colors " ^ btos conf.invert)
2340 | Keys.Ascii 'x' ->
2341 let ondone s =
2342 cbput state.hists.sel s;
2343 conf.selcmd <- s;
2345 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2346 textentry, ondone, true)
2348 | Keys.Ascii 'M' ->
2349 if conf.pax == None
2350 then conf.pax <- Some (ref (0.0, 0, 0))
2351 else conf.pax <- None;
2352 TEdone ("PAX " ^ btos (conf.pax != None))
2354 | (Keys.Ascii c) ->
2355 state.text <- Printf.sprintf "bad option %d `%c'"
2356 (Char.code c) c;
2357 TEstop
2359 | _ ->
2360 TEcont state.text
2363 class type lvsource =
2364 object
2365 method getitemcount : int
2366 method getitem : int -> (string * int)
2367 method hasaction : int -> bool
2368 method exit : uioh:uioh ->
2369 cancel:bool ->
2370 active:int ->
2371 first:int ->
2372 pan:int ->
2373 uioh option
2374 method getactive : int
2375 method getfirst : int
2376 method getpan : int
2377 method getminfo : (int * int) array
2378 end;;
2380 class virtual lvsourcebase = object
2381 val mutable m_active = 0
2382 val mutable m_first = 0
2383 val mutable m_pan = 0
2384 method getactive = m_active
2385 method getfirst = m_first
2386 method getpan = m_pan
2387 method getminfo : (int * int) array = E.a
2388 end;;
2390 let [@warning "-4"] textentrykeyboard
2391 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2392 state.text <- E.s;
2393 let enttext te =
2394 state.mode <- Textentry (te, onleave);
2395 enttext ();
2396 G.postRedisplay "textentrykeyboard enttext";
2398 let histaction cmd =
2399 match opthist with
2400 | None -> ()
2401 | Some (action, _) ->
2402 state.mode <-
2403 Textentry (
2404 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2406 G.postRedisplay "textentry histaction"
2408 let open Keys in let kt = Wsi.kc2kt key in
2409 match kt with
2410 | Backspace ->
2411 if emptystr text && cancelonempty
2412 then (
2413 onleave Cancel;
2414 G.postRedisplay "textentrykeyboard after cancel";
2416 else
2417 let s = withoutlastutf8 text in
2418 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2420 | Enter ->
2421 ondone text;
2422 onleave Confirm;
2423 G.postRedisplay "textentrykeyboard after confirm"
2425 | Up -> histaction HCprev
2426 | Down -> histaction HCnext
2427 | Home -> histaction HCfirst
2428 | End -> histaction HClast
2430 | Escape ->
2431 if emptystr text
2432 then (
2433 begin match opthist with
2434 | None -> ()
2435 | Some (_, onhistcancel) -> onhistcancel ()
2436 end;
2437 onleave Cancel;
2438 state.text <- E.s;
2439 G.postRedisplay "textentrykeyboard after cancel2"
2441 else (
2442 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2445 | Delete -> ()
2447 | Code _ | Ascii _ ->
2448 begin match onkey text kt with
2449 | TEdone text ->
2450 ondone text;
2451 onleave Confirm;
2452 G.postRedisplay "textentrykeyboard after confirm2";
2454 | TEcont text ->
2455 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2457 | TEstop ->
2458 onleave Cancel;
2459 G.postRedisplay "textentrykeyboard after cancel3"
2461 | TEswitch te ->
2462 state.mode <- Textentry (te, onleave);
2463 G.postRedisplay "textentrykeyboard switch";
2465 | _ -> vlog "unhandled key"
2468 let firstof first active =
2469 if first > active || abs (first - active) > fstate.maxrows - 1
2470 then max 0 (active - (fstate.maxrows/2))
2471 else first
2474 let calcfirst first active =
2475 if active > first
2476 then
2477 let rows = active - first in
2478 if rows > fstate.maxrows then active - fstate.maxrows else first
2479 else active
2482 let scrollph y maxy =
2483 let sh = float (maxy + state.winh) /. float state.winh in
2484 let sh = float state.winh /. sh in
2485 let sh = max sh (float conf.scrollh) in
2487 let percent = float y /. float maxy in
2488 let position = (float state.winh -. sh) *. percent in
2490 let position =
2491 if position +. sh > float state.winh
2492 then float state.winh -. sh
2493 else position
2495 position, sh;
2498 let adderrmsg src msg =
2499 Buffer.add_string state.errmsgs msg;
2500 state.newerrmsgs <- true;
2501 G.postRedisplay src
2504 let adderrfmt src fmt =
2505 Format.ksprintf (fun s -> adderrmsg src s) fmt;
2508 let coe s = (s :> uioh);;
2510 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2511 object (self)
2512 val m_pan = source#getpan
2513 val m_first = source#getfirst
2514 val m_active = source#getactive
2515 val m_qsearch = E.s
2516 val m_prev_uioh = state.uioh
2518 method private elemunder y =
2519 if y < 0
2520 then None
2521 else
2522 let n = y / (fstate.fontsize+1) in
2523 if m_first + n < source#getitemcount
2524 then (
2525 if source#hasaction (m_first + n)
2526 then Some (m_first + n)
2527 else None
2529 else None
2531 method display =
2532 Gl.enable `blend;
2533 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2534 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2535 filledrect 0. 0. (float state.winw) (float state.winh);
2536 GlDraw.color (1., 1., 1.);
2537 Gl.enable `texture_2d;
2538 let fs = fstate.fontsize in
2539 let nfs = fs + 1 in
2540 let hw = state.winw/3 in
2541 let ww = fstate.wwidth in
2542 let tabw = 17.0*.ww in
2543 let itemcount = source#getitemcount in
2544 let minfo = source#getminfo in
2545 if conf.leftscroll
2546 then (
2547 GlMat.push ();
2548 GlMat.translate ~x:(float conf.scrollbw) ();
2550 let x0 = 0.0 and x1 = float (state.winw - conf.scrollbw - 1) in
2551 let rec loop row =
2552 if (row - m_first) > fstate.maxrows
2553 then ()
2554 else (
2555 if row >= 0 && row < itemcount
2556 then (
2557 let (s, level) = source#getitem row in
2558 let y = (row - m_first) * nfs in
2559 let x = 5.0 +. (float (level + m_pan)) *. ww in
2560 if helpmode
2561 then GlDraw.color
2562 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2564 if row = m_active
2565 then (
2566 Gl.disable `texture_2d;
2567 let alpha = if source#hasaction row then 0.9 else 0.3 in
2568 GlDraw.color (1., 1., 1.) ~alpha;
2569 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2570 Gl.enable `texture_2d;
2572 let c =
2573 if zebra && row land 1 = 1
2574 then 0.8
2575 else 1.0
2577 GlDraw.color (c,c,c);
2578 let drawtabularstring s =
2579 let drawstr x s =
2580 let x' = truncate (x0 +. x) in
2581 let s1, s2 = splitatchar s '\000' in
2582 if emptystr s2
2583 then drawstring1 fs x' (y+nfs) s
2584 else
2585 let rec e s =
2586 if emptystr s
2587 then s
2588 else
2589 let s' = withoutlastutf8 s in
2590 let s = s' ^ UniSyms.ellipsis in
2591 let w = measurestr fs s in
2592 if float x' +. w +. ww < float (hw + x')
2593 then s
2594 else e s'
2596 let s1 =
2597 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2598 then e s1
2599 else s1
2601 ignore (drawstring1 fs x' (y+nfs) s1);
2602 drawstring1 fs (hw + x') (y+nfs) s2
2604 if trusted
2605 then
2606 let x = if helpmode && row > 0 then x +. ww else x in
2607 let s1, s2 = splitatchar s '\t' in
2608 if nonemptystr s2
2609 then
2610 let nx = drawstr x s1 in
2611 let sw = nx -. x in
2612 let x = x +. (max tabw sw) in
2613 drawstr x s2
2614 else
2615 let len = String.length s - 2 in
2616 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2617 then
2618 let s = String.sub s 2 len in
2619 let x = if not helpmode then x +. ww else x in
2620 GlDraw.color (1.2, 1.2, 1.2);
2621 let vinc = drawstring1 (fs+fs/4)
2622 (truncate (x -. ww)) (y+nfs) s in
2623 GlDraw.color (1., 1., 1.);
2624 vinc +. (float fs *. 0.8)
2625 else
2626 drawstr x s
2627 else
2628 drawstr x s
2630 ignore (drawtabularstring s);
2631 loop (row+1)
2635 loop m_first;
2636 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2637 let xadj = 5.0 in
2638 let rec loop row =
2639 if (row - m_first) <= fstate.maxrows
2640 then
2641 if row >= 0 && row < itemcount
2642 then (
2643 let (s, level) = source#getitem row in
2644 let pos0 = nindex s '\000' in
2645 let y = (row - m_first) * nfs in
2646 let x = float (level + m_pan) *. ww in
2647 let (first, last) = minfo.(row) in
2648 let prefix =
2649 if pos0 > 0 && first > pos0
2650 then String.sub s (pos0+1) (first-pos0-1)
2651 else String.sub s 0 first
2653 let suffix = String.sub s first (last - first) in
2654 let w1 = measurestr fstate.fontsize prefix in
2655 let w2 = measurestr fstate.fontsize suffix in
2656 let x = x +. if conf.leftscroll then xadj else 5.0 in
2657 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2658 let x0 = x +. w1
2659 and y0 = float (y+2) in
2660 let x1 = x0 +. w2
2661 and y1 = float (y+fs+3) in
2662 filledrect x0 y0 x1 y1;
2663 loop (row+1)
2666 Gl.disable `texture_2d;
2667 if Array.length minfo > 0 then loop m_first;
2668 Gl.disable `blend;
2669 if conf.leftscroll
2670 then GlMat.pop ()
2672 method updownlevel incr =
2673 let len = source#getitemcount in
2674 let curlevel =
2675 if m_active >= 0 && m_active < len
2676 then snd (source#getitem m_active)
2677 else -1
2679 let rec flow i =
2680 if i = len then i-1 else if i = -1 then 0 else
2681 let _, l = source#getitem i in
2682 if l != curlevel then i else flow (i+incr)
2684 let active = flow m_active in
2685 let first = calcfirst m_first active in
2686 G.postRedisplay "outline updownlevel";
2687 {< m_active = active; m_first = first >}
2689 method private key1 key mask =
2690 let set1 active first qsearch =
2691 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2693 let search active pattern incr =
2694 let active = if active = -1 then m_first else active in
2695 let dosearch re =
2696 let rec loop n =
2697 if n >= 0 && n < source#getitemcount
2698 then (
2699 let s, _ = source#getitem n in
2700 match Str.search_forward re s 0 with
2701 | exception Not_found -> loop (n + incr)
2702 | _ -> Some n
2704 else None
2706 loop active
2708 let qpat = Str.quote pattern in
2709 match Str.regexp_case_fold qpat with
2710 | s -> dosearch s
2711 | exception exn ->
2712 adderrfmt "listview key1" "regexp_case_fold for `%S' failed: %S\n"
2713 qpat @@ Printexc.to_string exn;
2714 None
2716 let itemcount = source#getitemcount in
2717 let find start incr =
2718 let rec find i =
2719 if i = -1 || i = itemcount
2720 then -1
2721 else (
2722 if source#hasaction i
2723 then i
2724 else find (i + incr)
2727 find start
2729 let set active first =
2730 let first = bound first 0 (itemcount - fstate.maxrows) in
2731 state.text <- E.s;
2732 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2734 let navigate incr =
2735 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2736 let active, first =
2737 let incr1 = if incr > 0 then 1 else -1 in
2738 if isvisible m_first m_active
2739 then
2740 let next =
2741 let next = m_active + incr in
2742 let next =
2743 if next < 0 || next >= itemcount
2744 then -1
2745 else find next incr1
2747 if abs (m_active - next) > fstate.maxrows
2748 then -1
2749 else next
2751 if next = -1
2752 then
2753 let first = m_first + incr in
2754 let first = bound first 0 (itemcount - fstate.maxrows) in
2755 let next =
2756 let next = m_active + incr in
2757 let next = bound next 0 (itemcount - 1) in
2758 find next ~-incr1
2760 let active =
2761 if next = -1
2762 then m_active
2763 else (
2764 if isvisible first next
2765 then next
2766 else m_active
2769 active, first
2770 else
2771 let first = min next m_first in
2772 let first =
2773 if abs (next - first) > fstate.maxrows
2774 then first + incr
2775 else first
2777 next, first
2778 else
2779 let first = m_first + incr in
2780 let first = bound first 0 (itemcount - 1) in
2781 let active =
2782 let next = m_active + incr in
2783 let next = bound next 0 (itemcount - 1) in
2784 let next = find next incr1 in
2785 let active =
2786 if next = -1 || abs (m_active - first) > fstate.maxrows
2787 then (
2788 let active = if m_active = -1 then next else m_active in
2789 active
2791 else next
2793 if isvisible first active
2794 then active
2795 else -1
2797 active, first
2799 G.postRedisplay "listview navigate";
2800 set active first;
2802 let open Keys in
2803 let kt = Wsi.kc2kt key in
2804 match [@warning "-4"] kt with
2805 | Ascii (('r'|'s') as c) when Wsi.withctrl mask ->
2806 let incr = if c = 'r' then -1 else 1 in
2807 let active, first =
2808 match search (m_active + incr) m_qsearch incr with
2809 | None ->
2810 state.text <- m_qsearch ^ " [not found]";
2811 m_active, m_first
2812 | Some active ->
2813 state.text <- m_qsearch;
2814 active, firstof m_first active
2816 G.postRedisplay "listview ctrl-r/s";
2817 set1 active first m_qsearch;
2819 | Insert when Wsi.withctrl mask ->
2820 if m_active >= 0 && m_active < source#getitemcount
2821 then (
2822 let s, _ = source#getitem m_active in
2823 selstring s;
2825 coe self
2827 | Backspace ->
2828 if emptystr m_qsearch
2829 then coe self
2830 else (
2831 let qsearch = withoutlastutf8 m_qsearch in
2832 if emptystr qsearch
2833 then (
2834 state.text <- E.s;
2835 G.postRedisplay "listview empty qsearch";
2836 set1 m_active m_first E.s;
2838 else
2839 let active, first =
2840 match search m_active qsearch ~-1 with
2841 | None ->
2842 state.text <- qsearch ^ " [not found]";
2843 m_active, m_first
2844 | Some active ->
2845 state.text <- qsearch;
2846 active, firstof m_first active
2848 G.postRedisplay "listview backspace qsearch";
2849 set1 active first qsearch
2852 | Ascii _ | Code _ ->
2853 let utf8 =
2854 match [@warning "-4"] kt with
2855 | Ascii c -> String.make 1 c
2856 | Code code -> toutf8 code
2857 | _ -> assert false
2859 let pattern = m_qsearch ^ utf8 in
2860 let active, first =
2861 match search m_active pattern 1 with
2862 | None ->
2863 state.text <- pattern ^ " [not found]";
2864 m_active, m_first
2865 | Some active ->
2866 state.text <- pattern;
2867 active, firstof m_first active
2869 G.postRedisplay "listview qsearch add";
2870 set1 active first pattern;
2872 | Escape ->
2873 state.text <- E.s;
2874 if emptystr m_qsearch
2875 then (
2876 G.postRedisplay "list view escape";
2877 let mx, my = state.mpos in
2878 updateunder mx my;
2879 match source#exit ~uioh:(coe self) ~cancel:true ~active:m_active
2880 ~first:m_first ~pan:m_pan with
2881 | None -> m_prev_uioh
2882 | Some uioh -> uioh
2884 else (
2885 G.postRedisplay "list view kill qsearch";
2886 coe {< m_qsearch = E.s >}
2889 | Enter ->
2890 state.text <- E.s;
2891 let self = {< m_qsearch = E.s >} in
2892 let opt =
2893 G.postRedisplay "listview enter";
2894 let cancel = not (m_active >= 0 && m_active < source#getitemcount) in
2895 source#exit ~uioh:(coe self) ~cancel
2896 ~active:m_active ~first:m_first ~pan:m_pan;
2898 begin match opt with
2899 | None -> m_prev_uioh
2900 | Some uioh -> uioh
2903 | Delete ->
2904 coe self
2906 | Up -> navigate ~-1
2907 | Down -> navigate 1
2908 | Prior -> navigate ~-(fstate.maxrows)
2909 | Next -> navigate fstate.maxrows
2911 | Right ->
2912 state.text <- E.s;
2913 G.postRedisplay "listview right";
2914 coe {< m_pan = m_pan - 1 >}
2916 | Left ->
2917 state.text <- E.s;
2918 G.postRedisplay "listview left";
2919 coe {< m_pan = m_pan + 1 >}
2921 | Home ->
2922 let active = find 0 1 in
2923 G.postRedisplay "listview home";
2924 set active 0;
2926 | End ->
2927 let first = max 0 (itemcount - fstate.maxrows) in
2928 let active = find (itemcount - 1) ~-1 in
2929 G.postRedisplay "listview end";
2930 set active first;
2932 | _ -> coe self
2934 method key key mask =
2935 match state.mode with
2936 | Textentry te ->
2937 textentrykeyboard key mask te;
2938 coe self
2939 | Birdseye _ | View | LinkNav _ -> self#key1 key mask
2941 method button button down x y _ =
2942 let opt =
2943 match button with
2944 | 1 when vscrollhit x ->
2945 G.postRedisplay "listview scroll";
2946 if down
2947 then
2948 let _, position, sh = self#scrollph in
2949 if y > truncate position && y < truncate (position +. sh)
2950 then (
2951 state.mstate <- Mscrolly;
2952 Some (coe self)
2954 else
2955 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
2956 let first = truncate (s *. float source#getitemcount) in
2957 let first = min source#getitemcount first in
2958 Some (coe {< m_first = first; m_active = first >})
2959 else (
2960 state.mstate <- Mnone;
2961 Some (coe self);
2963 | 1 when down ->
2964 begin match self#elemunder y with
2965 | Some n ->
2966 G.postRedisplay "listview click";
2967 source#exit ~uioh:(coe {< m_active = n >})
2968 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
2969 | _ ->
2970 Some (coe self)
2972 | n when (n == 4 || n == 5) && not down ->
2973 let len = source#getitemcount in
2974 let first =
2975 if n = 5 && m_first + fstate.maxrows >= len
2976 then
2977 m_first
2978 else
2979 let first = m_first + (if n == 4 then -1 else 1) in
2980 bound first 0 (len - 1)
2982 G.postRedisplay "listview wheel";
2983 Some (coe {< m_first = first >})
2984 | n when (n = 6 || n = 7) && not down ->
2985 let inc = if n = 7 then -1 else 1 in
2986 G.postRedisplay "listview hwheel";
2987 Some (coe {< m_pan = m_pan + inc >})
2988 | _ ->
2989 Some (coe self)
2991 match opt with
2992 | None -> m_prev_uioh
2993 | Some uioh -> uioh
2995 method multiclick _ x y = self#button 1 true x y
2997 method motion _ y =
2998 match state.mstate with
2999 | Mscrolly ->
3000 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3001 let first = truncate (s *. float source#getitemcount) in
3002 let first = min source#getitemcount first in
3003 G.postRedisplay "listview motion";
3004 coe {< m_first = first; m_active = first >}
3005 | Msel _
3006 | Mpan _
3007 | Mscrollx
3008 | Mzoom _
3009 | Mzoomrect _
3010 | Mnone -> coe self
3012 method pmotion x y =
3013 if x < state.winw - conf.scrollbw
3014 then
3015 let n =
3016 match self#elemunder y with
3017 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3018 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3020 let o =
3021 if n != m_active
3022 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3023 else self
3025 coe o
3026 else (
3027 Wsi.setcursor Wsi.CURSOR_INHERIT;
3028 coe self
3031 method infochanged _ = ()
3033 method scrollpw = (0, 0.0, 0.0)
3034 method scrollph =
3035 let nfs = fstate.fontsize + 1 in
3036 let y = m_first * nfs in
3037 let itemcount = source#getitemcount in
3038 let maxi = max 0 (itemcount - fstate.maxrows) in
3039 let maxy = maxi * nfs in
3040 let p, h = scrollph y maxy in
3041 conf.scrollbw, p, h
3043 method modehash = modehash
3044 method eformsgs = false
3045 method alwaysscrolly = true
3046 end;;
3048 class outlinelistview ~zebra ~source =
3049 let settext autonarrow s =
3050 if autonarrow
3051 then
3052 let ss = source#statestr in
3053 state.text <-
3054 if emptystr ss
3055 then "[" ^ s ^ "]"
3056 else "{" ^ ss ^ "} [" ^ s ^ "]"
3057 else state.text <- s
3059 object (self)
3060 inherit listview
3061 ~zebra
3062 ~helpmode:false
3063 ~source:(source :> lvsource)
3064 ~trusted:false
3065 ~modehash:(findkeyhash conf "outline")
3066 as super
3068 val m_autonarrow = false
3070 method! key key mask =
3071 let maxrows =
3072 if emptystr state.text
3073 then fstate.maxrows
3074 else fstate.maxrows - 2
3076 let calcfirst first active =
3077 if active > first
3078 then
3079 let rows = active - first in
3080 if rows > maxrows then active - maxrows else first
3081 else active
3083 let navigate incr =
3084 let active = m_active + incr in
3085 let active = bound active 0 (source#getitemcount - 1) in
3086 let first = calcfirst m_first active in
3087 G.postRedisplay "outline navigate";
3088 coe {< m_active = active; m_first = first >}
3090 let navscroll first =
3091 let active =
3092 let dist = m_active - first in
3093 if dist < 0
3094 then first
3095 else (
3096 if dist < maxrows
3097 then m_active
3098 else first + maxrows
3101 G.postRedisplay "outline navscroll";
3102 coe {< m_first = first; m_active = active >}
3104 let ctrl = Wsi.withctrl mask in
3105 let open Keys in
3106 match Wsi.kc2kt key with
3107 | Ascii 'a' when ctrl ->
3108 let text =
3109 if m_autonarrow
3110 then (source#denarrow; E.s)
3111 else (
3112 let pattern = source#renarrow in
3113 if nonemptystr m_qsearch
3114 then (source#narrow m_qsearch; m_qsearch)
3115 else pattern
3118 settext (not m_autonarrow) text;
3119 G.postRedisplay "toggle auto narrowing";
3120 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3122 | Ascii '/' when emptystr m_qsearch && not m_autonarrow ->
3123 settext true E.s;
3124 G.postRedisplay "toggle auto narrowing";
3125 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3127 | Ascii 'n' when ctrl ->
3128 source#narrow m_qsearch;
3129 if not m_autonarrow
3130 then source#add_narrow_pattern m_qsearch;
3131 G.postRedisplay "outline ctrl-n";
3132 coe {< m_first = 0; m_active = 0 >}
3134 | Ascii 'S' when ctrl ->
3135 let active = source#calcactive (getanchor ()) in
3136 let first = firstof m_first active in
3137 G.postRedisplay "outline ctrl-s";
3138 coe {< m_first = first; m_active = active >}
3140 | Ascii 'u' when ctrl ->
3141 G.postRedisplay "outline ctrl-u";
3142 if m_autonarrow && nonemptystr m_qsearch
3143 then (
3144 ignore (source#renarrow);
3145 settext m_autonarrow E.s;
3146 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3148 else (
3149 source#del_narrow_pattern;
3150 let pattern = source#renarrow in
3151 let text =
3152 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3154 settext m_autonarrow text;
3155 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3158 | Ascii 'l' when ctrl ->
3159 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3160 G.postRedisplay "outline ctrl-l";
3161 coe {< m_first = first >}
3163 | Ascii '\t' when m_autonarrow ->
3164 if nonemptystr m_qsearch
3165 then (
3166 G.postRedisplay "outline list view tab";
3167 source#add_narrow_pattern m_qsearch;
3168 settext true E.s;
3169 coe {< m_qsearch = E.s >}
3171 else coe self
3173 | Escape when m_autonarrow ->
3174 if nonemptystr m_qsearch
3175 then source#add_narrow_pattern m_qsearch;
3176 super#key key mask
3178 | Enter when m_autonarrow ->
3179 if nonemptystr m_qsearch
3180 then source#add_narrow_pattern m_qsearch;
3181 super#key key mask
3183 | _ when m_autonarrow ->
3184 let pattern = m_qsearch ^ toutf8 key in
3185 G.postRedisplay "outlinelistview autonarrow add";
3186 source#narrow pattern;
3187 settext true pattern;
3188 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3190 | key when m_autonarrow && key = Backspace ->
3191 if emptystr m_qsearch
3192 then coe self
3193 else
3194 let pattern = withoutlastutf8 m_qsearch in
3195 G.postRedisplay "outlinelistview autonarrow backspace";
3196 ignore (source#renarrow);
3197 source#narrow pattern;
3198 settext true pattern;
3199 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3201 | Up when ctrl ->
3202 navscroll (max 0 (m_first - 1))
3204 | Down when ctrl ->
3205 navscroll (min (source#getitemcount - 1) (m_first + 1))
3207 | Up -> navigate ~-1
3208 | Down -> navigate 1
3209 | Prior -> navigate ~-(fstate.maxrows)
3210 | Next -> navigate fstate.maxrows
3212 | Right ->
3213 let o =
3214 if ctrl
3215 then (
3216 G.postRedisplay "outline ctrl right";
3217 {< m_pan = m_pan + 1 >}
3219 else self#updownlevel 1
3221 coe o
3223 | Left ->
3224 let o =
3225 if ctrl
3226 then (
3227 G.postRedisplay "outline ctrl left";
3228 {< m_pan = m_pan - 1 >}
3230 else self#updownlevel ~-1
3232 coe o
3234 | Home ->
3235 G.postRedisplay "outline home";
3236 coe {< m_first = 0; m_active = 0 >}
3238 | End ->
3239 let active = source#getitemcount - 1 in
3240 let first = max 0 (active - fstate.maxrows) in
3241 G.postRedisplay "outline end";
3242 coe {< m_active = active; m_first = first >}
3244 | Delete|Escape|Insert|Enter|Ascii _|Code _|Backspace|Fn _ ->
3245 super#key key mask
3246 end;;
3248 let genhistoutlines () =
3249 Config.gethist ()
3250 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
3251 compare c2.lastvisit c1.lastvisit)
3252 |> List.map (fun ((path, c, _, _, _, origin) as hist) ->
3253 let path = if nonemptystr origin then origin else path in
3254 let base = mbtoutf8 @@ Filename.basename path in
3255 (base ^ "\000" ^ c.title, 1, Ohistory hist)
3259 let gotohist (path, c, bookmarks, x, anchor, origin) =
3260 Config.save leavebirdseye;
3261 state.anchor <- anchor;
3262 state.bookmarks <- bookmarks;
3263 state.origin <- origin;
3264 state.x <- x;
3265 setconf conf c;
3266 let x0, y0, x1, y1 = conf.trimfuzz in
3267 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3268 reshape ~firsttime:true state.winw state.winh;
3269 opendoc path origin;
3270 setzoom c.zoom;
3273 let makecheckers () =
3274 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3275 following to say:
3276 converted by Issac Trotts. July 25, 2002 *)
3277 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3278 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3279 let id = GlTex.gen_texture () in
3280 GlTex.bind_texture ~target:`texture_2d id;
3281 GlPix.store (`unpack_alignment 1);
3282 GlTex.image2d image;
3283 List.iter (GlTex.parameter ~target:`texture_2d)
3284 [ `mag_filter `nearest; `min_filter `nearest ];
3288 let setcheckers enabled =
3289 match state.checkerstexid with
3290 | None ->
3291 if enabled then state.checkerstexid <- Some (makecheckers ())
3293 | Some checkerstexid ->
3294 if not enabled
3295 then (
3296 GlTex.delete_texture checkerstexid;
3297 state.checkerstexid <- None;
3301 let describe_location () =
3302 let fn = page_of_y state.y in
3303 let ln = page_of_y (state.y + state.winh - 1) in
3304 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3305 let percent =
3306 if maxy <= 0
3307 then 100.
3308 else (100. *. (float state.y /. float maxy))
3310 if fn = ln
3311 then
3312 Printf.sprintf "page %d of %d [%.2f%%]"
3313 (fn+1) state.pagecount percent
3314 else
3315 Printf.sprintf
3316 "pages %d-%d of %d [%.2f%%]"
3317 (fn+1) (ln+1) state.pagecount percent
3320 let setpresentationmode v =
3321 let n = page_of_y state.y in
3322 state.anchor <- (n, 0.0, 1.0);
3323 conf.presentation <- v;
3324 if conf.fitmodel = FitPage
3325 then reqlayout conf.angle conf.fitmodel;
3326 represent ();
3329 let setbgcol (r, g, b) =
3330 let col =
3331 let r = r *. 255.0 |> truncate
3332 and g = g *. 255.0 |> truncate
3333 and b = b *. 255.0 |> truncate in
3334 r lsl 16 |> (lor) (g lsl 8) |> (lor) b
3336 Wsi.setwinbgcol col;
3339 let enterinfomode =
3340 let btos b = if b then UniSyms.radical else E.s in
3341 let showextended = ref false in
3342 let leave mode _ = state.mode <- mode in
3343 let src =
3344 (object
3345 val mutable m_l = []
3346 val mutable m_a = E.a
3347 val mutable m_prev_uioh = nouioh
3348 val mutable m_prev_mode = View
3350 inherit lvsourcebase
3352 method reset prev_mode prev_uioh =
3353 m_a <- Array.of_list (List.rev m_l);
3354 m_l <- [];
3355 m_prev_mode <- prev_mode;
3356 m_prev_uioh <- prev_uioh;
3358 method int name get set =
3359 m_l <-
3360 (name, `int get, 1,
3361 Action (
3362 fun u ->
3363 let ondone s =
3364 try set (int_of_string s)
3365 with exn ->
3366 state.text <- Printf.sprintf "bad integer `%s': %s"
3367 s @@ exntos exn
3369 state.text <- E.s;
3370 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3371 state.mode <- Textentry (te, leave m_prev_mode);
3373 )) :: m_l
3375 method int_with_suffix name get set =
3376 m_l <-
3377 (name, `intws get, 1,
3378 Action (
3379 fun u ->
3380 let ondone s =
3381 try set (int_of_string_with_suffix s)
3382 with exn ->
3383 state.text <- Printf.sprintf "bad integer `%s': %s"
3384 s @@ exntos exn
3386 state.text <- E.s;
3387 let te =
3388 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3390 state.mode <- Textentry (te, leave m_prev_mode);
3392 )) :: m_l
3394 method bool ?(offset=1) ?(btos=btos) name get set =
3395 m_l <-
3396 (name, `bool (btos, get), offset, Action (
3397 fun u ->
3398 let v = get () in
3399 set (not v);
3401 )) :: m_l
3403 method color name get set =
3404 m_l <-
3405 (name, `color get, 1,
3406 Action (
3407 fun u ->
3408 let invalid = (nan, nan, nan) in
3409 let ondone s =
3410 let c =
3411 try color_of_string s
3412 with exn ->
3413 state.text <- Printf.sprintf "bad color `%s': %s"
3414 s @@ exntos exn;
3415 invalid
3417 if c <> invalid
3418 then set c;
3420 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3421 state.text <- color_to_string (get ());
3422 state.mode <- Textentry (te, leave m_prev_mode);
3424 )) :: m_l
3426 method string name get set =
3427 m_l <-
3428 (name, `string get, 1,
3429 Action (
3430 fun u ->
3431 let ondone s = set s in
3432 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3433 state.mode <- Textentry (te, leave m_prev_mode);
3435 )) :: m_l
3437 method colorspace name get set =
3438 m_l <-
3439 (name, `string get, 1,
3440 Action (
3441 fun _ ->
3442 let source =
3443 (object
3444 inherit lvsourcebase
3446 initializer
3447 m_active <- CSTE.to_int conf.colorspace;
3448 m_first <- 0;
3450 method getitemcount =
3451 Array.length CSTE.names
3452 method getitem n =
3453 (CSTE.names.(n), 0)
3454 method exit ~uioh ~cancel ~active ~first ~pan =
3455 ignore (uioh, first, pan);
3456 if not cancel then set active;
3457 None
3458 method hasaction _ = true
3459 end)
3461 state.text <- E.s;
3462 let modehash = findkeyhash conf "info" in
3463 coe (new listview ~zebra:false ~helpmode:false
3464 ~source ~trusted:true ~modehash)
3465 )) :: m_l
3467 method paxmark name get set =
3468 m_l <-
3469 (name, `string get, 1,
3470 Action (
3471 fun _ ->
3472 let source =
3473 (object
3474 inherit lvsourcebase
3476 initializer
3477 m_active <- MTE.to_int conf.paxmark;
3478 m_first <- 0;
3480 method getitemcount = Array.length MTE.names
3481 method getitem n = (MTE.names.(n), 0)
3482 method exit ~uioh ~cancel ~active ~first ~pan =
3483 ignore (uioh, first, pan);
3484 if not cancel then set active;
3485 None
3486 method hasaction _ = true
3487 end)
3489 state.text <- E.s;
3490 let modehash = findkeyhash conf "info" in
3491 coe (new listview ~zebra:false ~helpmode:false
3492 ~source ~trusted:true ~modehash)
3493 )) :: m_l
3495 method fitmodel name get set =
3496 m_l <-
3497 (name, `string get, 1,
3498 Action (
3499 fun _ ->
3500 let source =
3501 (object
3502 inherit lvsourcebase
3504 initializer
3505 m_active <- FMTE.to_int conf.fitmodel;
3506 m_first <- 0;
3508 method getitemcount = Array.length FMTE.names
3509 method getitem n = (FMTE.names.(n), 0)
3510 method exit ~uioh ~cancel ~active ~first ~pan =
3511 ignore (uioh, first, pan);
3512 if not cancel then set active;
3513 None
3514 method hasaction _ = true
3515 end)
3517 state.text <- E.s;
3518 let modehash = findkeyhash conf "info" in
3519 coe (new listview ~zebra:false ~helpmode:false
3520 ~source ~trusted:true ~modehash)
3521 )) :: m_l
3523 method caption s offset =
3524 m_l <- (s, `empty, offset, Noaction) :: m_l
3526 method caption2 s f offset =
3527 m_l <- (s, `string f, offset, Noaction) :: m_l
3529 method getitemcount = Array.length m_a
3531 method getitem n =
3532 let tostr = function
3533 | `int f -> string_of_int (f ())
3534 | `intws f -> string_with_suffix_of_int (f ())
3535 | `string f -> f ()
3536 | `color f -> color_to_string (f ())
3537 | `bool (btos, f) -> btos (f ())
3538 | `empty -> E.s
3540 let name, t, offset, _ = m_a.(n) in
3541 ((let s = tostr t in
3542 if nonemptystr s
3543 then Printf.sprintf "%s\t%s" name s
3544 else name),
3545 offset)
3547 method exit ~uioh ~cancel ~active ~first ~pan =
3548 let uiohopt =
3549 if not cancel
3550 then (
3551 let uioh =
3552 match m_a.(active) with
3553 | _, _, _, Action f -> f uioh
3554 | _, _, _, Noaction -> uioh
3556 Some uioh
3558 else None
3560 m_active <- active;
3561 m_first <- first;
3562 m_pan <- pan;
3563 uiohopt
3565 method hasaction n =
3566 match m_a.(n) with
3567 | _, _, _, Action _ -> true
3568 | _, _, _, Noaction -> false
3570 initializer m_active <- 1
3571 end)
3573 let rec fillsrc prevmode prevuioh =
3574 let sep () = src#caption E.s 0 in
3575 let colorp name get set =
3576 src#string name
3577 (fun () -> color_to_string (get ()))
3578 (fun v ->
3580 let c = color_of_string v in
3581 set c
3582 with exn ->
3583 state.text <-
3584 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3587 let oldmode = state.mode in
3588 let birdseye = isbirdseye state.mode in
3590 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3592 src#bool "presentation mode"
3593 (fun () -> conf.presentation)
3594 (fun v -> setpresentationmode v);
3596 src#bool "ignore case in searches"
3597 (fun () -> conf.icase)
3598 (fun v -> conf.icase <- v);
3600 src#bool "preload"
3601 (fun () -> conf.preload)
3602 (fun v -> conf.preload <- v);
3604 src#bool "highlight links"
3605 (fun () -> conf.hlinks)
3606 (fun v -> conf.hlinks <- v);
3608 src#bool "under info"
3609 (fun () -> conf.underinfo)
3610 (fun v -> conf.underinfo <- v);
3612 src#bool "persistent bookmarks"
3613 (fun () -> conf.savebmarks)
3614 (fun v -> conf.savebmarks <- v);
3616 src#fitmodel "fit model"
3617 (fun () -> FMTE.to_string conf.fitmodel)
3618 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3620 src#bool "trim margins"
3621 (fun () -> conf.trimmargins)
3622 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3624 src#bool "persistent location"
3625 (fun () -> conf.jumpback)
3626 (fun v -> conf.jumpback <- v);
3628 sep ();
3629 src#int "inter-page space"
3630 (fun () -> conf.interpagespace)
3631 (fun n ->
3632 conf.interpagespace <- n;
3633 docolumns conf.columns;
3634 let pageno, py =
3635 match state.layout with
3636 | [] -> 0, 0
3637 | l :: _ ->
3638 l.pageno, l.pagey
3640 state.maxy <- calcheight ();
3641 let y = getpagey pageno in
3642 gotoxy state.x (y + py)
3645 src#int "page bias"
3646 (fun () -> conf.pagebias)
3647 (fun v -> conf.pagebias <- v);
3649 src#int "scroll step"
3650 (fun () -> conf.scrollstep)
3651 (fun n -> conf.scrollstep <- n);
3653 src#int "horizontal scroll step"
3654 (fun () -> conf.hscrollstep)
3655 (fun v -> conf.hscrollstep <- v);
3657 src#int "auto scroll step"
3658 (fun () ->
3659 match state.autoscroll with
3660 | Some step -> step
3661 | _ -> conf.autoscrollstep)
3662 (fun n ->
3663 let n = boundastep state.winh n in
3664 if state.autoscroll <> None
3665 then state.autoscroll <- Some n;
3666 conf.autoscrollstep <- n);
3668 src#int "zoom"
3669 (fun () -> truncate (conf.zoom *. 100.))
3670 (fun v -> pivotzoom ((float v) /. 100.));
3672 src#int "rotation"
3673 (fun () -> conf.angle)
3674 (fun v -> reqlayout v conf.fitmodel);
3676 src#int "scroll bar width"
3677 (fun () -> conf.scrollbw)
3678 (fun v ->
3679 conf.scrollbw <- v;
3680 reshape state.winw state.winh;
3683 src#int "scroll handle height"
3684 (fun () -> conf.scrollh)
3685 (fun v -> conf.scrollh <- v;);
3687 src#int "thumbnail width"
3688 (fun () -> conf.thumbw)
3689 (fun v ->
3690 conf.thumbw <- min 4096 v;
3691 match oldmode with
3692 | Birdseye beye ->
3693 leavebirdseye beye false;
3694 enterbirdseye ()
3695 | Textentry _
3696 | View
3697 | LinkNav _ -> ()
3700 let mode = state.mode in
3701 src#string "columns"
3702 (fun () ->
3703 match conf.columns with
3704 | Csingle _ -> "1"
3705 | Cmulti (multi, _) -> multicolumns_to_string multi
3706 | Csplit (count, _) -> "-" ^ string_of_int count
3708 (fun v ->
3709 let n, a, b = multicolumns_of_string v in
3710 setcolumns mode n a b);
3712 sep ();
3713 src#caption "Pixmap cache" 0;
3714 src#int_with_suffix "size (advisory)"
3715 (fun () -> conf.memlimit)
3716 (fun v -> conf.memlimit <- v);
3718 src#caption2 "used"
3719 (fun () ->
3720 Printf.sprintf "%s bytes, %d tiles"
3721 (string_with_suffix_of_int state.memused)
3722 (Hashtbl.length state.tilemap)) 1;
3724 sep ();
3725 src#caption "Layout" 0;
3726 src#caption2 "Dimension"
3727 (fun () ->
3728 Printf.sprintf "%dx%d (virtual %dx%d)"
3729 state.winw state.winh
3730 state.w state.maxy)
3732 if conf.debug
3733 then
3734 src#caption2 "Position" (fun () ->
3735 Printf.sprintf "%dx%d" state.x state.y
3737 else
3738 src#caption2 "Position" (fun () -> describe_location ()) 1
3741 sep ();
3742 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3743 "Save these parameters as global defaults at exit"
3744 (fun () -> conf.bedefault)
3745 (fun v -> conf.bedefault <- v)
3748 sep ();
3749 let btos b = if b then UniSyms.lguillemet else UniSyms.rguillemet in
3750 src#bool ~offset:0 ~btos "Extended parameters"
3751 (fun () -> !showextended)
3752 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3753 if !showextended
3754 then (
3755 src#bool "checkers"
3756 (fun () -> conf.checkers)
3757 (fun v -> conf.checkers <- v; setcheckers v);
3758 src#bool "update cursor"
3759 (fun () -> conf.updatecurs)
3760 (fun v -> conf.updatecurs <- v);
3761 src#bool "scroll-bar on the left"
3762 (fun () -> conf.leftscroll)
3763 (fun v -> conf.leftscroll <- v);
3764 src#bool "verbose"
3765 (fun () -> conf.verbose)
3766 (fun v -> conf.verbose <- v);
3767 src#bool "invert colors"
3768 (fun () -> conf.invert)
3769 (fun v -> conf.invert <- v);
3770 src#bool "max fit"
3771 (fun () -> conf.maxhfit)
3772 (fun v -> conf.maxhfit <- v);
3773 src#bool "pax mode"
3774 (fun () -> conf.pax != None)
3775 (fun v ->
3776 if v
3777 then conf.pax <- Some (ref (now (), 0, 0))
3778 else conf.pax <- None);
3779 src#string "uri launcher"
3780 (fun () -> conf.urilauncher)
3781 (fun v -> conf.urilauncher <- v);
3782 src#string "path launcher"
3783 (fun () -> conf.pathlauncher)
3784 (fun v -> conf.pathlauncher <- v);
3785 src#string "tile size"
3786 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3787 (fun v ->
3789 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3790 conf.tilew <- max 64 w;
3791 conf.tileh <- max 64 h;
3792 flushtiles ();
3793 with exn ->
3794 state.text <- Printf.sprintf "bad tile size `%s': %s"
3795 v @@ exntos exn
3797 src#int "texture count"
3798 (fun () -> conf.texcount)
3799 (fun v ->
3800 if realloctexts v
3801 then conf.texcount <- v
3802 else impmsg "failed to set texture count please retry later"
3804 src#int "slice height"
3805 (fun () -> conf.sliceheight)
3806 (fun v ->
3807 conf.sliceheight <- v;
3808 wcmd "sliceh %d" conf.sliceheight;
3810 src#int "anti-aliasing level"
3811 (fun () -> conf.aalevel)
3812 (fun v ->
3813 conf.aalevel <- bound v 0 8;
3814 state.anchor <- getanchor ();
3815 opendoc state.path state.password;
3817 src#string "page scroll scaling factor"
3818 (fun () -> string_of_float conf.pgscale)
3819 (fun v ->
3821 let s = float_of_string v in
3822 conf.pgscale <- s
3823 with exn ->
3824 state.text <- Printf.sprintf
3825 "bad page scroll scaling factor `%s': %s" v
3826 @@ exntos exn
3829 src#int "ui font size"
3830 (fun () -> fstate.fontsize)
3831 (fun v -> setfontsize (bound v 5 100));
3832 src#int "hint font size"
3833 (fun () -> conf.hfsize)
3834 (fun v -> conf.hfsize <- bound v 5 100);
3835 colorp "background color"
3836 (fun () -> conf.bgcolor)
3837 (fun v -> conf.bgcolor <- v; setbgcol v);
3838 src#bool "crop hack"
3839 (fun () -> conf.crophack)
3840 (fun v -> conf.crophack <- v);
3841 src#string "trim fuzz"
3842 (fun () -> irect_to_string conf.trimfuzz)
3843 (fun v ->
3845 conf.trimfuzz <- irect_of_string v;
3846 if conf.trimmargins
3847 then settrim true conf.trimfuzz;
3848 with exn ->
3849 state.text <- Printf.sprintf "bad irect `%s': %s" v
3850 @@ exntos exn
3852 src#string "throttle"
3853 (fun () ->
3854 match conf.maxwait with
3855 | None -> "show place holder if page is not ready"
3856 | Some time ->
3857 if time = infinity
3858 then "wait for page to fully render"
3859 else
3860 "wait " ^ string_of_float time
3861 ^ " seconds before showing placeholder"
3863 (fun v ->
3865 let f = float_of_string v in
3866 if f <= 0.0
3867 then conf.maxwait <- None
3868 else conf.maxwait <- Some f
3869 with exn ->
3870 state.text <- Printf.sprintf "bad time `%s': %s" v
3871 @@ exntos exn
3873 src#string "ghyll scroll"
3874 (fun () ->
3875 match conf.ghyllscroll with
3876 | None -> E.s
3877 | Some nab -> ghyllscroll_to_string nab
3879 (fun v ->
3880 try conf.ghyllscroll <- ghyllscroll_of_string v
3881 with
3882 | Failure msg ->
3883 state.text <- Printf.sprintf "bad ghyll `%s': %s" v msg
3884 | exn ->
3885 state.text <- Printf.sprintf "bad ghyll `%s': %s" v
3886 @@ exntos exn
3888 src#string "selection command"
3889 (fun () -> conf.selcmd)
3890 (fun v -> conf.selcmd <- v);
3891 src#string "synctex command"
3892 (fun () -> conf.stcmd)
3893 (fun v -> conf.stcmd <- v);
3894 src#string "pax command"
3895 (fun () -> conf.paxcmd)
3896 (fun v -> conf.paxcmd <- v);
3897 src#string "ask password command"
3898 (fun () -> conf.passcmd)
3899 (fun v -> conf.passcmd <- v);
3900 src#string "save path command"
3901 (fun () -> conf.savecmd)
3902 (fun v -> conf.savecmd <- v);
3903 src#colorspace "color space"
3904 (fun () -> CSTE.to_string conf.colorspace)
3905 (fun v ->
3906 conf.colorspace <- CSTE.of_int v;
3907 wcmd "cs %d" v;
3908 load state.layout;
3910 src#paxmark "pax mark method"
3911 (fun () -> MTE.to_string conf.paxmark)
3912 (fun v -> conf.paxmark <- MTE.of_int v);
3913 if bousable () && !opengl_has_pbo
3914 then
3915 src#bool "use PBO"
3916 (fun () -> conf.usepbo)
3917 (fun v -> conf.usepbo <- v);
3918 src#bool "mouse wheel scrolls pages"
3919 (fun () -> conf.wheelbypage)
3920 (fun v -> conf.wheelbypage <- v);
3921 src#bool "open remote links in a new instance"
3922 (fun () -> conf.riani)
3923 (fun v -> conf.riani <- v);
3924 src#bool "edit annotations inline"
3925 (fun () -> conf.annotinline)
3926 (fun v -> conf.annotinline <- v);
3927 src#bool "coarse positioning in presentation mode"
3928 (fun () -> conf.coarseprespos)
3929 (fun v -> conf.coarseprespos <- v);
3930 src#bool "use document CSS"
3931 (fun () -> conf.usedoccss)
3932 (fun v ->
3933 conf.usedoccss <- v;
3934 state.anchor <- getanchor ();
3935 opendoc state.path state.password;
3939 sep ();
3940 src#caption "Document" 0;
3941 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3942 src#caption2 "Pages"
3943 (fun () -> string_of_int state.pagecount) 1;
3944 src#caption2 "Dimensions"
3945 (fun () -> string_of_int (List.length state.pdims)) 1;
3946 if nonemptystr conf.css
3947 then src#caption2 "CSS" (fun () -> conf.css) 1;
3948 if conf.trimmargins
3949 then (
3950 sep ();
3951 src#caption "Trimmed margins" 0;
3952 src#caption2 "Dimensions"
3953 (fun () -> string_of_int (List.length state.pdims)) 1;
3956 sep ();
3957 src#caption "OpenGL" 0;
3958 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
3959 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
3961 sep ();
3962 src#caption "Location" 0;
3963 if nonemptystr state.origin
3964 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
3965 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
3967 src#reset prevmode prevuioh;
3969 fun () ->
3970 state.text <- E.s;
3971 resetmstate ();
3972 let prevmode = state.mode
3973 and prevuioh = state.uioh in
3974 fillsrc prevmode prevuioh;
3975 let source = (src :> lvsource) in
3976 let modehash = findkeyhash conf "info" in
3977 state.uioh <-
3978 coe (object (self)
3979 inherit listview ~zebra:false ~helpmode:false
3980 ~source ~trusted:true ~modehash as super
3981 val mutable m_prevmemused = 0
3982 method! infochanged = function
3983 | Memused ->
3984 if m_prevmemused != state.memused
3985 then (
3986 m_prevmemused <- state.memused;
3987 G.postRedisplay "memusedchanged";
3989 | Pdim -> G.postRedisplay "pdimchanged"
3990 | Docinfo -> fillsrc prevmode prevuioh
3992 method! key key mask =
3993 if not (Wsi.withctrl mask)
3994 then
3995 match [@warning "-4"] Wsi.kc2kt key with
3996 | Keys.Left -> coe (self#updownlevel ~-1)
3997 | Keys.Right -> coe (self#updownlevel 1)
3998 | _ -> super#key key mask
3999 else super#key key mask
4000 end);
4001 G.postRedisplay "info";
4004 let enterhelpmode =
4005 let source =
4006 (object
4007 inherit lvsourcebase
4008 method getitemcount = Array.length state.help
4009 method getitem n =
4010 let s, l, _ = state.help.(n) in
4011 (s, l)
4013 method exit ~uioh ~cancel ~active ~first ~pan =
4014 let optuioh =
4015 if not cancel
4016 then (
4017 match state.help.(active) with
4018 | _, _, Action f -> Some (f uioh)
4019 | _, _, Noaction -> Some uioh
4021 else None
4023 m_active <- active;
4024 m_first <- first;
4025 m_pan <- pan;
4026 optuioh
4028 method hasaction n =
4029 match state.help.(n) with
4030 | _, _, Action _ -> true
4031 | _, _, Noaction -> false
4033 initializer
4034 m_active <- -1
4035 end)
4036 in fun () ->
4037 let modehash = findkeyhash conf "help" in
4038 resetmstate ();
4039 state.uioh <- coe (new listview
4040 ~zebra:false ~helpmode:true
4041 ~source ~trusted:true ~modehash);
4042 G.postRedisplay "help";
4045 let entermsgsmode =
4046 let msgsource =
4047 (object
4048 inherit lvsourcebase
4049 val mutable m_items = E.a
4051 method getitemcount = 1 + Array.length m_items
4053 method getitem n =
4054 if n = 0
4055 then "[Clear]", 0
4056 else m_items.(n-1), 0
4058 method exit ~uioh ~cancel ~active ~first ~pan =
4059 ignore uioh;
4060 if not cancel
4061 then (
4062 if active = 0
4063 then Buffer.clear state.errmsgs;
4065 m_active <- active;
4066 m_first <- first;
4067 m_pan <- pan;
4068 None
4070 method hasaction n =
4071 n = 0
4073 method reset =
4074 state.newerrmsgs <- false;
4075 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
4076 m_items <- Array.of_list l
4078 initializer
4079 m_active <- 0
4080 end)
4081 in fun () ->
4082 state.text <- E.s;
4083 resetmstate ();
4084 msgsource#reset;
4085 let source = (msgsource :> lvsource) in
4086 let modehash = findkeyhash conf "listview" in
4087 state.uioh <-
4088 coe (object
4089 inherit listview ~zebra:false ~helpmode:false
4090 ~source ~trusted:false ~modehash as super
4091 method! display =
4092 if state.newerrmsgs
4093 then msgsource#reset;
4094 super#display
4095 end);
4096 G.postRedisplay "msgs";
4099 let getusertext s =
4100 let editor = getenvwithdef "EDITOR" E.s in
4101 if emptystr editor
4102 then E.s
4103 else
4104 let tmppath = Filename.temp_file "llpp" "note" in
4105 if nonemptystr s
4106 then (
4107 let oc = open_out tmppath in
4108 output_string oc s;
4109 close_out oc;
4111 let execstr = editor ^ " " ^ tmppath in
4112 let s =
4113 match spawn execstr [] with
4114 | exception exn ->
4115 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
4117 | pid ->
4118 match Unix.waitpid [] pid with
4119 | exception exn ->
4120 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
4122 | (_pid, status) ->
4123 match status with
4124 | Unix.WEXITED 0 -> filecontents tmppath
4125 | Unix.WEXITED n ->
4126 impmsg "editor process(%s) exited abnormally: %d" execstr n;
4128 | Unix.WSIGNALED n ->
4129 impmsg "editor process(%s) was killed by signal %d" execstr n;
4131 | Unix.WSTOPPED n ->
4132 impmsg "editor(%s) process was stopped by signal %d" execstr n;
4135 match Unix.unlink tmppath with
4136 | exception exn ->
4137 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
4139 | () -> s
4142 let enterannotmode opaque slinkindex =
4143 let msgsource =
4144 (object
4145 inherit lvsourcebase
4146 val mutable m_text = E.s
4147 val mutable m_items = E.a
4149 method getitemcount = Array.length m_items
4151 method getitem n =
4152 let label, _func = m_items.(n) in
4153 label, 0
4155 method exit ~uioh ~cancel ~active ~first ~pan =
4156 ignore (uioh, first, pan);
4157 if not cancel
4158 then (
4159 let _label, func = m_items.(active) in
4160 func ()
4162 None
4164 method hasaction n = nonemptystr @@ fst m_items.(n)
4166 method reset s =
4167 let rec split accu b i =
4168 let p = b+i in
4169 if p = String.length s
4170 then (String.sub s b (p-b), unit) :: accu
4171 else
4172 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4173 then
4174 let ss = if i = 0 then E.s else String.sub s b i in
4175 split ((ss, unit)::accu) (p+1) 0
4176 else
4177 split accu b (i+1)
4179 let cleanup () =
4180 wcmd "freepage %s" (~> opaque);
4181 let keys =
4182 Hashtbl.fold (fun key opaque' accu ->
4183 if opaque' = opaque'
4184 then key :: accu else accu) state.pagemap []
4186 List.iter (Hashtbl.remove state.pagemap) keys;
4187 flushtiles ();
4188 gotoxy state.x state.y
4190 let dele () =
4191 delannot opaque slinkindex;
4192 cleanup ();
4194 let edit inline () =
4195 let update s =
4196 if emptystr s
4197 then dele ()
4198 else (
4199 modannot opaque slinkindex s;
4200 cleanup ();
4203 if inline
4204 then
4205 let mode = state.mode in
4206 state.mode <-
4207 Textentry (
4208 ("annotation: ", m_text, None, textentry, update, true),
4209 fun _ -> state.mode <- mode);
4210 state.text <- E.s;
4211 enttext ();
4212 else
4213 let s = getusertext m_text in
4214 update s
4216 m_text <- s;
4217 m_items <-
4218 ( "[Copy]", fun () -> selstring m_text)
4219 :: ("[Delete]", dele)
4220 :: ("[Edit]", edit conf.annotinline)
4221 :: (E.s, unit)
4222 :: split [] 0 0 |> List.rev |> Array.of_list
4224 initializer
4225 m_active <- 0
4226 end)
4228 state.text <- E.s;
4229 let s = getannotcontents opaque slinkindex in
4230 resetmstate ();
4231 msgsource#reset s;
4232 let source = (msgsource :> lvsource) in
4233 let modehash = findkeyhash conf "listview" in
4234 state.uioh <- coe (object
4235 inherit listview ~zebra:false ~helpmode:false
4236 ~source ~trusted:false ~modehash
4237 end);
4238 G.postRedisplay "enterannotmode";
4241 let gotoremote spec =
4242 let filename, dest = splitatchar spec '#' in
4243 let getpath filename =
4244 let path =
4245 if nonemptystr filename
4246 then
4247 if Filename.is_relative filename
4248 then
4249 let dir = Filename.dirname state.path in
4250 let dir =
4251 if Filename.is_implicit dir
4252 then Filename.concat (Sys.getcwd ()) dir
4253 else dir
4255 Filename.concat dir filename
4256 else filename
4257 else E.s
4259 if Sys.file_exists path
4260 then path
4261 else E.s
4263 let path = getpath filename in
4264 let dospawn lcmd =
4265 if conf.riani
4266 then
4267 let cmd = Lazy.force_val lcmd in
4268 match spawn cmd with
4269 | _pid -> ()
4270 | exception exn -> dolog "failed to execute `%s': %s" cmd @@ exntos exn
4271 else
4272 let anchor = getanchor () in
4273 let ranchor = state.path, state.password, anchor, state.origin in
4274 state.origin <- E.s;
4275 state.ranchors <- ranchor :: state.ranchors;
4276 opendoc path E.s;
4278 if substratis spec 0 "page="
4279 then
4280 match Scanf.sscanf spec "page=%d" (fun n -> n) with
4281 | pageno ->
4282 state.anchor <- (pageno, 0.0, 0.0);
4283 dospawn @@ lazy (Printf.sprintf "%s -page %d %S" !selfexec pageno path);
4284 | exception exn ->
4285 adderrfmt "error parsing remote destination" "page: %s" @@ exntos exn
4286 else (
4287 state.nameddest <- dest;
4288 dospawn @@ lazy (!selfexec ^ " " ^ path ^ " -dest " ^ dest)
4292 let gotounder = function
4293 | Ulinkuri s when isexternallink s ->
4294 if substratis s 0 "file://"
4295 then gotoremote @@ String.sub s 7 (String.length s - 7)
4296 else gotouri s
4297 | Ulinkuri s ->
4298 let pageno, x, y = uritolocation s in
4299 addnav ();
4300 gotopagexy !wtmode pageno x y
4301 | Utext _ | Unone -> ()
4302 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4305 let gotooutline (_, _, kind) =
4306 match kind with
4307 | Onone -> ()
4308 | Oanchor anchor ->
4309 let (pageno, y, _) = anchor in
4310 let y = getanchory
4311 (if conf.presentation then (pageno, y, 1.0) else anchor)
4313 addnav ();
4314 gotoghyll y
4315 | Ouri uri -> gotounder (Ulinkuri uri)
4316 | Olaunch _cmd -> failwith "gotounder (Ulaunch cmd)"
4317 | Oremote _remote -> failwith "gotounder (Uremote remote)"
4318 | Ohistory hist -> gotohist hist
4319 | Oremotedest _remotedest -> failwith "gotounder (Uremotedest remotedest)"
4322 class outlinesoucebase fetchoutlines = object (self)
4323 inherit lvsourcebase
4324 val mutable m_items = E.a
4325 val mutable m_minfo = E.a
4326 val mutable m_orig_items = E.a
4327 val mutable m_orig_minfo = E.a
4328 val mutable m_narrow_patterns = []
4329 val mutable m_gen = -1
4331 method getitemcount = Array.length m_items
4333 method getitem n =
4334 let s, n, _ = m_items.(n) in
4335 (s, n+0)
4337 method exit ~(uioh:uioh) ~cancel ~active ~(first:int) ~pan : uioh option =
4338 ignore (uioh, first);
4339 let items, minfo =
4340 if m_narrow_patterns = []
4341 then m_orig_items, m_orig_minfo
4342 else m_items, m_minfo
4344 m_pan <- pan;
4345 if not cancel
4346 then (
4347 m_items <- items;
4348 m_minfo <- minfo;
4349 gotooutline m_items.(active);
4351 else (
4352 m_items <- items;
4353 m_minfo <- minfo;
4355 None
4357 method hasaction (_:int) = true
4359 method greetmsg =
4360 if Array.length m_items != Array.length m_orig_items
4361 then
4362 let s =
4363 match m_narrow_patterns with
4364 | one :: [] -> one
4365 | many -> String.concat UniSyms.ellipsis (List.rev many)
4367 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4368 else E.s
4370 method statestr =
4371 match m_narrow_patterns with
4372 | [] -> E.s
4373 | one :: [] -> one
4374 | head :: _ -> UniSyms.ellipsis ^ head
4376 method narrow pattern =
4377 match Str.regexp_case_fold pattern with
4378 | exception _ -> ()
4379 | re ->
4380 let rec loop accu minfo n =
4381 if n = -1
4382 then (
4383 m_items <- Array.of_list accu;
4384 m_minfo <- Array.of_list minfo;
4386 else
4387 let (s, _, _) as o = m_items.(n) in
4388 let accu, minfo =
4389 match Str.search_forward re s 0 with
4390 | exception Not_found -> accu, minfo
4391 | first -> o :: accu, (first, Str.match_end ()) :: minfo
4393 loop accu minfo (n-1)
4395 loop [] [] (Array.length m_items - 1)
4397 method! getminfo = m_minfo
4399 method denarrow =
4400 m_orig_items <- fetchoutlines ();
4401 m_minfo <- m_orig_minfo;
4402 m_items <- m_orig_items
4404 method add_narrow_pattern pattern =
4405 m_narrow_patterns <- pattern :: m_narrow_patterns
4407 method del_narrow_pattern =
4408 match m_narrow_patterns with
4409 | _ :: rest -> m_narrow_patterns <- rest
4410 | [] -> ()
4412 method renarrow =
4413 self#denarrow;
4414 match m_narrow_patterns with
4415 | pattern :: [] -> self#narrow pattern; pattern
4416 | list ->
4417 List.fold_left (fun accu pattern ->
4418 self#narrow pattern;
4419 pattern ^ UniSyms.ellipsis ^ accu) E.s list
4421 method calcactive (_:anchor) = 0
4423 method reset anchor items =
4424 if state.gen != m_gen
4425 then (
4426 m_orig_items <- items;
4427 m_items <- items;
4428 m_narrow_patterns <- [];
4429 m_minfo <- E.a;
4430 m_orig_minfo <- E.a;
4431 m_gen <- state.gen;
4433 else (
4434 if items != m_orig_items
4435 then (
4436 m_orig_items <- items;
4437 if m_narrow_patterns == []
4438 then m_items <- items;
4441 let active = self#calcactive anchor in
4442 m_active <- active;
4443 m_first <- firstof m_first active
4447 let outlinesource fetchoutlines =
4448 (object
4449 inherit outlinesoucebase fetchoutlines
4450 method! calcactive anchor =
4451 let rely = getanchory anchor in
4452 let rec loop n best bestd =
4453 if n = Array.length m_items
4454 then best
4455 else
4456 let _, _, kind = m_items.(n) in
4457 match kind with
4458 | Oanchor anchor ->
4459 let orely = getanchory anchor in
4460 let d = abs (orely - rely) in
4461 if d < bestd
4462 then loop (n+1) n d
4463 else loop (n+1) best bestd
4464 | Onone | Oremote _ | Olaunch _
4465 | Oremotedest _ | Ouri _ | Ohistory _ ->
4466 loop (n+1) best bestd
4468 loop 0 ~-1 max_int
4469 end)
4472 let enteroutlinemode, enterbookmarkmode, enterhistmode =
4473 let mkselector sourcetype =
4474 let fetchoutlines () =
4475 match sourcetype with
4476 | `bookmarks -> Array.of_list state.bookmarks
4477 | `outlines -> state.outlines
4478 | `history -> genhistoutlines () |> Array.of_list
4480 let source =
4481 if sourcetype = `history
4482 then new outlinesoucebase fetchoutlines
4483 else outlinesource fetchoutlines
4485 (fun errmsg ->
4486 let outlines = fetchoutlines () in
4487 if Array.length outlines = 0
4488 then showtext ' ' errmsg
4489 else (
4490 resetmstate ();
4491 Wsi.setcursor Wsi.CURSOR_INHERIT;
4492 let anchor = getanchor () in
4493 source#reset anchor outlines;
4494 state.text <- source#greetmsg;
4495 state.uioh <-
4496 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4497 G.postRedisplay "enter selector";
4501 let mkenter sourcetype errmsg =
4502 let enter = mkselector sourcetype in
4503 fun () -> enter errmsg
4505 ( mkenter `outlines "document has no outline"
4506 , mkenter `bookmarks "document has no bookmarks (yet)"
4507 , mkenter `history "history is empty" )
4510 let quickbookmark ?title () =
4511 match state.layout with
4512 | [] -> ()
4513 | l :: _ ->
4514 let title =
4515 match title with
4516 | None ->
4517 let open Unix in
4518 let tm = localtime (now ()) in
4519 Printf.sprintf
4520 "Quick (page %d) (bookmarked at %02d/%02d/%d %02d:%02d)"
4521 (l.pageno+1)
4522 tm.tm_mday (tm.tm_mon+1) (tm.tm_year+1900) tm.tm_hour tm.tm_min
4523 | Some title -> title
4525 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4528 let setautoscrollspeed step goingdown =
4529 let incr = max 1 ((abs step) / 2) in
4530 let incr = if goingdown then incr else -incr in
4531 let astep = boundastep state.winh (step + incr) in
4532 state.autoscroll <- Some astep;
4535 let canpan () =
4536 match conf.columns with
4537 | Csplit _ -> true
4538 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4541 let panbound x = bound x (-state.w) state.winw;;
4543 let existsinrow pageno (columns, coverA, coverB) p =
4544 let last = ((pageno - coverA) mod columns) + columns in
4545 let rec any = function
4546 | [] -> false
4547 | l :: rest ->
4548 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4549 then p l
4550 else (
4551 if not (p l)
4552 then (if l.pageno = last then false else any rest)
4553 else true
4556 any state.layout
4559 let nextpage () =
4560 match state.layout with
4561 | [] ->
4562 let pageno = page_of_y state.y in
4563 gotoghyll (getpagey (pageno+1))
4564 | l :: rest ->
4565 match conf.columns with
4566 | Csingle _ ->
4567 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4568 then
4569 let y = clamp (pgscale state.winh) in
4570 gotoghyll y
4571 else
4572 let pageno = min (l.pageno+1) (state.pagecount-1) in
4573 gotoghyll (getpagey pageno)
4574 | Cmulti ((c, _, _) as cl, _) ->
4575 if conf.presentation
4576 && (existsinrow l.pageno cl
4577 (fun l -> l.pageh > l.pagey + l.pagevh))
4578 then
4579 let y = clamp (pgscale state.winh) in
4580 gotoghyll y
4581 else
4582 let pageno = min (l.pageno+c) (state.pagecount-1) in
4583 gotoghyll (getpagey pageno)
4584 | Csplit (n, _) ->
4585 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4586 then
4587 let pagey, pageh = getpageyh l.pageno in
4588 let pagey = pagey + pageh * l.pagecol in
4589 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4590 gotoghyll (pagey + pageh + ips)
4593 let prevpage () =
4594 match state.layout with
4595 | [] ->
4596 let pageno = page_of_y state.y in
4597 gotoghyll (getpagey (pageno-1))
4598 | l :: _ ->
4599 match conf.columns with
4600 | Csingle _ ->
4601 if conf.presentation && l.pagey != 0
4602 then
4603 gotoghyll (clamp (pgscale ~-(state.winh)))
4604 else
4605 let pageno = max 0 (l.pageno-1) in
4606 gotoghyll (getpagey pageno)
4607 | Cmulti ((c, _, coverB) as cl, _) ->
4608 if conf.presentation &&
4609 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4610 then
4611 gotoghyll (clamp (pgscale ~-(state.winh)))
4612 else
4613 let decr =
4614 if l.pageno = state.pagecount - coverB
4615 then 1
4616 else c
4618 let pageno = max 0 (l.pageno-decr) in
4619 gotoghyll (getpagey pageno)
4620 | Csplit (n, _) ->
4621 let y =
4622 if l.pagecol = 0
4623 then
4624 if l.pageno = 0
4625 then l.pagey
4626 else
4627 let pageno = max 0 (l.pageno-1) in
4628 let pagey, pageh = getpageyh pageno in
4629 pagey + (n-1)*pageh
4630 else
4631 let pagey, pageh = getpageyh l.pageno in
4632 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4634 gotoghyll y
4637 let save () =
4638 if emptystr conf.savecmd
4639 then error "don't know where to save modified document"
4640 else
4641 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
4642 let path =
4643 getcmdoutput
4644 (fun s -> error "failed to obtain path to the saved copy: %s" s)
4645 savecmd
4647 if nonemptystr path
4648 then
4649 let tmp = path ^ ".tmp" in
4650 savedoc tmp;
4651 Unix.rename tmp path;
4654 let viewkeyboard key mask =
4655 let enttext te =
4656 let mode = state.mode in
4657 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4658 state.text <- E.s;
4659 enttext ();
4660 G.postRedisplay "view:enttext"
4662 let ctrl = Wsi.withctrl mask in
4663 let open Keys in
4664 match Wsi.kc2kt key with
4665 | Ascii 'Q' -> exit 0
4667 | Ascii 'W' ->
4668 if hasunsavedchanges ()
4669 then save ()
4671 | Insert ->
4672 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4673 then (
4674 state.mode <- (
4675 match state.lnava with
4676 | None -> LinkNav (Ltgendir 0)
4677 | Some pn -> LinkNav (Ltexact pn)
4679 gotoxy state.x state.y;
4681 else impmsg "keyboard link navigation does not work under rotation"
4683 | Escape | Ascii 'q' ->
4684 begin match state.mstate with
4685 | Mzoomrect _ ->
4686 resetmstate ();
4687 G.postRedisplay "kill rect";
4688 | Msel _
4689 | Mpan _
4690 | Mscrolly | Mscrollx
4691 | Mzoom _
4692 | Mnone ->
4693 begin match state.mode with
4694 | LinkNav ln ->
4695 begin match ln with
4696 | Ltexact pl -> state.lnava <- Some pl
4697 | Ltgendir _ | Ltnotready _ -> state.lnava <- None
4698 end;
4699 state.mode <- View;
4700 G.postRedisplay "esc leave linknav"
4701 | Birdseye _ | Textentry _ | View ->
4702 match state.ranchors with
4703 | [] -> raise Quit
4704 | (path, password, anchor, origin) :: rest ->
4705 state.ranchors <- rest;
4706 state.anchor <- anchor;
4707 state.origin <- origin;
4708 state.nameddest <- E.s;
4709 opendoc path password
4710 end;
4711 end;
4713 | Backspace ->
4714 gotoghyll (getnav ~-1)
4716 | Ascii 'o' ->
4717 enteroutlinemode ()
4719 | Ascii 'H' ->
4720 enterhistmode ()
4722 | Ascii 'u' ->
4723 state.rects <- [];
4724 state.text <- E.s;
4725 Hashtbl.iter (fun _ opaque ->
4726 clearmark opaque;
4727 Hashtbl.clear state.prects) state.pagemap;
4728 G.postRedisplay "dehighlight";
4730 | (Ascii '/' | Ascii '?') as pv ->
4731 let ondone isforw s =
4732 cbput state.hists.pat s;
4733 state.searchpattern <- s;
4734 search s isforw
4736 let s = String.make 1 (Char.chr key) in
4737 enttext (s, E.s, Some (onhist state.hists.pat),
4738 textentry, ondone (pv = Ascii '/'), true)
4740 | Ascii '+' | Ascii '=' when ctrl ->
4741 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4742 pivotzoom (conf.zoom +. incr)
4744 | Ascii '+' ->
4745 let ondone s =
4746 let n =
4747 try int_of_string s with exn ->
4748 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4749 max_int
4751 if n != max_int
4752 then (
4753 conf.pagebias <- n;
4754 state.text <- "page bias is now " ^ string_of_int n;
4757 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4759 | Ascii '-' when ctrl ->
4760 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4761 pivotzoom (max 0.01 (conf.zoom -. decr))
4763 | Ascii '-' ->
4764 let ondone msg = state.text <- msg in
4765 enttext (
4766 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4767 optentry state.mode, ondone, true
4770 | Ascii '0' when ctrl ->
4771 if conf.zoom = 1.0
4772 then gotoxy 0 state.y
4773 else setzoom 1.0
4775 | Ascii ('1'|'2' as c)
4776 when ctrl && conf.fitmodel != FitPage ->
4777 let cols =
4778 match conf.columns with
4779 | Csingle _ | Cmulti _ -> 1
4780 | Csplit (n, _) -> n
4782 let h = state.winh -
4783 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4785 let zoom = zoomforh state.winw h 0 cols in
4786 if zoom > 0.0 && (c = '2' || zoom < 1.0)
4787 then setzoom zoom
4789 | Ascii '3' when ctrl ->
4790 let fm =
4791 match conf.fitmodel with
4792 | FitWidth -> FitProportional
4793 | FitProportional -> FitPage
4794 | FitPage -> FitWidth
4796 state.text <- "fit model: " ^ FMTE.to_string fm;
4797 reqlayout conf.angle fm
4799 | Ascii '4' when ctrl ->
4800 let zoom = getmaxw () /. float state.winw in
4801 if zoom > 0.0 then setzoom zoom
4803 | Fn 9 ->
4804 togglebirdseye ()
4806 | Ascii '9' when ctrl ->
4807 togglebirdseye ()
4809 | Ascii ('0'..'9')
4810 when not ctrl ->
4811 let ondone s =
4812 let n =
4813 try int_of_string s with exn ->
4814 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4817 if n >= 0
4818 then (
4819 addnav ();
4820 cbput state.hists.pag (string_of_int n);
4821 gotopage1 (n + conf.pagebias - 1) 0;
4824 let [@warning "-4"] pageentry text = function
4825 | Keys.Ascii 'g' -> TEdone text
4826 | key -> intentry text key
4828 let text = String.make 1 (Char.chr key) in
4829 enttext (":", text, Some (onhist state.hists.pag),
4830 pageentry, ondone, true)
4832 | Ascii 'b' ->
4833 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4834 G.postRedisplay "toggle scrollbar";
4836 | Ascii 'B' ->
4837 state.bzoom <- not state.bzoom;
4838 state.rects <- [];
4839 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4841 | Ascii 'l' ->
4842 conf.hlinks <- not conf.hlinks;
4843 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4844 G.postRedisplay "toggle highlightlinks";
4846 | Ascii 'F' ->
4847 if conf.angle mod 360 = 0
4848 then (
4849 state.glinks <- true;
4850 let mode = state.mode in
4851 state.mode <-
4852 Textentry (
4853 (":", E.s, None, linknentry, linknact gotounder, false),
4854 (fun _ ->
4855 state.glinks <- false;
4856 state.mode <- mode)
4858 state.text <- E.s;
4859 G.postRedisplay "view:linkent(F)"
4861 else impmsg "hint mode does not work under rotation"
4863 | Ascii 'y' ->
4864 state.glinks <- true;
4865 let mode = state.mode in
4866 state.mode <-
4867 Textentry (
4869 ":", E.s, None, linknentry, linknact (fun under ->
4870 selstring (undertext under);
4871 ), false
4873 fun _ ->
4874 state.glinks <- false;
4875 state.mode <- mode
4877 state.text <- E.s;
4878 G.postRedisplay "view:linkent"
4880 | Ascii 'a' ->
4881 begin match state.autoscroll with
4882 | Some step ->
4883 conf.autoscrollstep <- step;
4884 state.autoscroll <- None
4885 | None ->
4886 if conf.autoscrollstep = 0
4887 then state.autoscroll <- Some 1
4888 else state.autoscroll <- Some conf.autoscrollstep
4891 | Ascii 'p' when ctrl ->
4892 launchpath () (* XXX where do error messages go? *)
4894 | Ascii 'P' ->
4895 setpresentationmode (not conf.presentation);
4896 showtext ' ' ("presentation mode " ^
4897 if conf.presentation then "on" else "off");
4899 | Ascii 'f' ->
4900 if List.mem Wsi.Fullscreen state.winstate
4901 then Wsi.reshape conf.cwinw conf.cwinh
4902 else Wsi.fullscreen ()
4904 | Ascii ('p'|'N') ->
4905 search state.searchpattern false
4907 | Ascii 'n' | Fn 3 ->
4908 search state.searchpattern true
4910 | Ascii 't' ->
4911 begin match state.layout with
4912 | [] -> ()
4913 | l :: _ ->
4914 gotoghyll (getpagey l.pageno)
4917 | Ascii ' ' ->
4918 nextpage ()
4920 | Delete ->
4921 prevpage ()
4923 | Ascii '=' ->
4924 showtext ' ' (describe_location ());
4926 | Ascii 'w' ->
4927 begin match state.layout with
4928 | [] -> ()
4929 | l :: _ ->
4930 Wsi.reshape l.pagew l.pageh;
4931 G.postRedisplay "w"
4934 | Ascii '\'' ->
4935 enterbookmarkmode ()
4937 | Ascii 'h' | Fn 1 ->
4938 enterhelpmode ()
4940 | Ascii 'i' ->
4941 enterinfomode ()
4943 | Ascii 'e' when Buffer.length state.errmsgs > 0 ->
4944 entermsgsmode ()
4946 | Ascii 'm' ->
4947 let ondone s =
4948 match state.layout with
4949 | l :: _ ->
4950 if nonemptystr s
4951 then
4952 state.bookmarks <-
4953 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4954 | _ -> ()
4956 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
4958 | Ascii '~' ->
4959 quickbookmark ();
4960 showtext ' ' "Quick bookmark added";
4962 | Ascii 'z' ->
4963 begin match state.layout with
4964 | l :: _ ->
4965 let rect = getpdimrect l.pagedimno in
4966 let w, h =
4967 if conf.crophack
4968 then
4969 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4970 truncate (1.2 *. (rect.(3) -. rect.(0))))
4971 else
4972 (truncate (rect.(1) -. rect.(0)),
4973 truncate (rect.(3) -. rect.(0)))
4975 let w = truncate ((float w)*.conf.zoom)
4976 and h = truncate ((float h)*.conf.zoom) in
4977 if w != 0 && h != 0
4978 then (
4979 state.anchor <- getanchor ();
4980 Wsi.reshape w (h + conf.interpagespace)
4982 G.postRedisplay "z";
4984 | [] -> ()
4987 | Ascii 'x' -> state.roam ()
4989 | Ascii ('<'|'>' as c) ->
4990 reqlayout
4991 (conf.angle + (if c = '>' then 30 else -30)) conf.fitmodel
4993 | Ascii ('['|']' as c) ->
4994 conf.colorscale <-
4995 bound (conf.colorscale +. (if c = '>' then 0.1 else -0.1)) 0.0 1.0;
4996 G.postRedisplay "brightness";
4998 | Ascii 'c' when state.mode = View ->
4999 if Wsi.withalt mask
5000 then (
5001 if conf.zoom > 1.0
5002 then
5003 let m = (state.winw - state.w) / 2 in
5004 gotoxy_and_clear_text m state.y
5006 else
5007 let (c, a, b), z =
5008 match state.prevcolumns with
5009 | None -> (1, 0, 0), 1.0
5010 | Some (columns, z) ->
5011 let cab =
5012 match columns with
5013 | Csplit (c, _) -> -c, 0, 0
5014 | Cmulti ((c, a, b), _) -> c, a, b
5015 | Csingle _ -> 1, 0, 0
5017 cab, z
5019 setcolumns View c a b;
5020 setzoom z
5022 | Down | Up when ctrl && Wsi.withshift mask ->
5023 let zoom, x = state.prevzoom in
5024 setzoom zoom;
5025 state.x <- x;
5027 | Ascii 'k' | Up ->
5028 begin match state.autoscroll with
5029 | None ->
5030 begin match state.mode with
5031 | Birdseye beye -> upbirdseye 1 beye
5032 | Textentry _ | View | LinkNav _ ->
5033 if ctrl
5034 then gotoxy_and_clear_text state.x (clamp ~-(state.winh/2))
5035 else (
5036 if not (Wsi.withshift mask) && conf.presentation
5037 then prevpage ()
5038 else gotoghyll1 true (clamp (-conf.scrollstep))
5041 | Some n ->
5042 setautoscrollspeed n false
5045 | Ascii 'j' | Down ->
5046 begin match state.autoscroll with
5047 | None ->
5048 begin match state.mode with
5049 | Birdseye beye -> downbirdseye 1 beye
5050 | Textentry _ | View | LinkNav _ ->
5051 if ctrl
5052 then gotoxy_and_clear_text state.x (clamp (state.winh/2))
5053 else (
5054 if not (Wsi.withshift mask) && conf.presentation
5055 then nextpage ()
5056 else gotoghyll1 true (clamp (conf.scrollstep))
5059 | Some n ->
5060 setautoscrollspeed n true
5063 | Left | Right when not (Wsi.withalt mask) ->
5064 if canpan ()
5065 then
5066 let dx =
5067 if ctrl
5068 then state.winw / 2
5069 else conf.hscrollstep
5071 let dx =
5072 let pv = Wsi.kc2kt key in
5073 if pv = Keys.Left then dx else -dx
5075 gotoxy_and_clear_text (panbound (state.x + dx)) state.y
5076 else (
5077 state.text <- E.s;
5078 G.postRedisplay "left/right"
5081 | Prior ->
5082 let y =
5083 if ctrl
5084 then
5085 match state.layout with
5086 | [] -> state.y
5087 | l :: _ -> state.y - l.pagey
5088 else
5089 clamp (pgscale (-state.winh))
5091 gotoghyll y
5093 | Next ->
5094 let y =
5095 if ctrl
5096 then
5097 match List.rev state.layout with
5098 | [] -> state.y
5099 | l :: _ -> getpagey l.pageno
5100 else
5101 clamp (pgscale state.winh)
5103 gotoghyll y
5105 | Ascii 'g' | Home ->
5106 addnav ();
5107 gotoghyll 0
5108 | Ascii 'G' | End ->
5109 addnav ();
5110 gotoghyll (clamp state.maxy)
5112 | Right when Wsi.withalt mask ->
5113 gotoghyll (getnav 1)
5114 | Left when Wsi.withalt mask ->
5115 gotoghyll (getnav ~-1)
5117 | Ascii 'r' ->
5118 reload ()
5120 | Ascii 'v' when conf.debug ->
5121 state.rects <- [];
5122 List.iter (fun l ->
5123 match getopaque l.pageno with
5124 | None -> ()
5125 | Some opaque ->
5126 let x0, y0, x1, y1 = pagebbox opaque in
5127 let rect = (float x0, float y0,
5128 float x1, float y0,
5129 float x1, float y1,
5130 float x0, float y1) in
5131 debugrect rect;
5132 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
5133 state.rects <- (l.pageno, color, rect) :: state.rects;
5134 ) state.layout;
5135 G.postRedisplay "v";
5137 | Ascii '|' ->
5138 let mode = state.mode in
5139 let cmd = ref E.s in
5140 let onleave = function
5141 | Cancel -> state.mode <- mode
5142 | Confirm ->
5143 List.iter (fun l ->
5144 match getopaque l.pageno with
5145 | Some opaque -> pipesel opaque !cmd
5146 | None -> ()) state.layout;
5147 state.mode <- mode
5149 let ondone s =
5150 cbput state.hists.sel s;
5151 cmd := s
5153 let te =
5154 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5156 G.postRedisplay "|";
5157 state.mode <- Textentry (te, onleave);
5159 | (Ascii _|Fn _|Enter|Left|Right|Code _) ->
5160 vlog "huh? %s" (Wsi.keyname key)
5163 let linknavkeyboard key mask linknav =
5164 let pv = Wsi.kc2kt key in
5165 let getpage pageno =
5166 let rec loop = function
5167 | [] -> None
5168 | l :: _ when l.pageno = pageno -> Some l
5169 | _ :: rest -> loop rest
5170 in loop state.layout
5172 let doexact (pageno, n) =
5173 match getopaque pageno, getpage pageno with
5174 | Some opaque, Some l ->
5175 if pv = Keys.Enter
5176 then
5177 let under = getlink opaque n in
5178 G.postRedisplay "link gotounder";
5179 gotounder under;
5180 state.mode <- View;
5181 else
5182 let opt, dir =
5183 let open Keys in
5184 match pv with
5185 | Home ->
5186 Some (findlink opaque LDfirst), -1
5188 | End ->
5189 Some (findlink opaque LDlast), 1
5191 | Left ->
5192 Some (findlink opaque (LDleft n)), -1
5194 | Right ->
5195 Some (findlink opaque (LDright n)), 1
5197 | Up ->
5198 Some (findlink opaque (LDup n)), -1
5200 | Down ->
5201 Some (findlink opaque (LDdown n)), 1
5203 | Delete|Escape|Insert|Enter|Next|Prior|Ascii _
5204 | Code _|Fn _|Backspace -> None, 0
5206 let pwl l dir =
5207 begin match findpwl l.pageno dir with
5208 | Pwlnotfound -> ()
5209 | Pwl pageno ->
5210 let notfound dir =
5211 state.mode <- LinkNav (Ltgendir dir);
5212 let y, h = getpageyh pageno in
5213 let y =
5214 if dir < 0
5215 then y + h - state.winh
5216 else y
5218 gotoxy state.x y
5220 begin match getopaque pageno, getpage pageno with
5221 | Some opaque, Some _ ->
5222 let link =
5223 let ld = if dir > 0 then LDfirst else LDlast in
5224 findlink opaque ld
5226 begin match link with
5227 | Lfound m ->
5228 showlinktype (getlink opaque m);
5229 state.mode <- LinkNav (Ltexact (pageno, m));
5230 G.postRedisplay "linknav jpage";
5231 | Lnotfound -> notfound dir
5232 end;
5233 | _ -> notfound dir
5234 end;
5235 end;
5237 begin match opt with
5238 | Some Lnotfound -> pwl l dir;
5239 | Some (Lfound m) ->
5240 if m = n
5241 then pwl l dir
5242 else (
5243 let _, y0, _, y1 = getlinkrect opaque m in
5244 if y0 < l.pagey
5245 then gotopage1 l.pageno y0
5246 else (
5247 let d = fstate.fontsize + 1 in
5248 if y1 - l.pagey > l.pagevh - d
5249 then gotopage1 l.pageno (y1 - state.winh + d)
5250 else G.postRedisplay "linknav";
5252 showlinktype (getlink opaque m);
5253 state.mode <- LinkNav (Ltexact (l.pageno, m));
5256 | None -> viewkeyboard key mask
5257 end;
5258 | _ -> viewkeyboard key mask
5260 if pv = Keys.Insert
5261 then (
5262 begin match linknav with
5263 | Ltexact pa -> state.lnava <- Some pa
5264 | Ltgendir _ | Ltnotready _ -> ()
5265 end;
5266 state.mode <- View;
5267 G.postRedisplay "leave linknav"
5269 else
5270 match linknav with
5271 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5272 | Ltexact exact -> doexact exact
5275 let keyboard key mask =
5276 if (key = Char.code 'g' && Wsi.withctrl mask) && not (istextentry state.mode)
5277 then wcmd "interrupt"
5278 else state.uioh <- state.uioh#key key mask
5281 let birdseyekeyboard key mask
5282 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5283 let incr =
5284 match conf.columns with
5285 | Csingle _ -> 1
5286 | Cmulti ((c, _, _), _) -> c
5287 | Csplit _ -> failwith "bird's eye split mode"
5289 let pgh layout = List.fold_left
5290 (fun m l -> max l.pageh m) state.winh layout in
5291 let open Keys in
5292 match Wsi.kc2kt key with
5293 | Ascii 'l' when Wsi.withctrl mask ->
5294 let y, h = getpageyh pageno in
5295 let top = (state.winh - h) / 2 in
5296 gotoxy state.x (max 0 (y - top))
5297 | Enter -> leavebirdseye beye false
5298 | Escape -> leavebirdseye beye true
5299 | Up -> upbirdseye incr beye
5300 | Down -> downbirdseye incr beye
5301 | Left -> upbirdseye 1 beye
5302 | Right -> downbirdseye 1 beye
5304 | Prior ->
5305 begin match state.layout with
5306 | l :: _ ->
5307 if l.pagey != 0
5308 then (
5309 state.mode <- Birdseye (
5310 oconf, leftx, l.pageno, hooverpageno, anchor
5312 gotopage1 l.pageno 0;
5314 else (
5315 let layout = layout state.x (state.y-state.winh)
5316 state.winw
5317 (pgh state.layout) in
5318 match layout with
5319 | [] -> gotoxy state.x (clamp (-state.winh))
5320 | l :: _ ->
5321 state.mode <- Birdseye (
5322 oconf, leftx, l.pageno, hooverpageno, anchor
5324 gotopage1 l.pageno 0
5327 | [] -> gotoxy state.x (clamp (-state.winh))
5328 end;
5330 | Next ->
5331 begin match List.rev state.layout with
5332 | l :: _ ->
5333 let layout = layout state.x
5334 (state.y + (pgh state.layout))
5335 state.winw state.winh in
5336 begin match layout with
5337 | [] ->
5338 let incr = l.pageh - l.pagevh in
5339 if incr = 0
5340 then (
5341 state.mode <-
5342 Birdseye (
5343 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5345 G.postRedisplay "birdseye pagedown";
5347 else gotoxy state.x (clamp (incr + conf.interpagespace*2));
5349 | l :: _ ->
5350 state.mode <-
5351 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5352 gotopage1 l.pageno 0;
5355 | [] -> gotoxy state.x (clamp state.winh)
5356 end;
5358 | Home ->
5359 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5360 gotopage1 0 0
5362 | End ->
5363 let pageno = state.pagecount - 1 in
5364 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5365 if not (pagevisible state.layout pageno)
5366 then
5367 let h =
5368 match List.rev state.pdims with
5369 | [] -> state.winh
5370 | (_, _, h, _) :: _ -> h
5372 gotoxy
5373 state.x
5374 (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5375 else G.postRedisplay "birdseye end";
5377 | Delete|Insert|Ascii _|Code _|Fn _|Backspace -> viewkeyboard key mask
5380 let drawpage l =
5381 let color =
5382 match state.mode with
5383 | Textentry _ -> scalecolor 0.4
5384 | LinkNav _ | View -> scalecolor 1.0
5385 | Birdseye (_, _, pageno, hooverpageno, _) ->
5386 if l.pageno = hooverpageno
5387 then scalecolor 0.9
5388 else (
5389 if l.pageno = pageno
5390 then (
5391 let c = scalecolor 1.0 in
5392 GlDraw.color c;
5393 GlDraw.line_width 3.0;
5394 let dispx = l.pagedispx in
5395 linerect
5396 (float (dispx-1)) (float (l.pagedispy-1))
5397 (float (dispx+l.pagevw+1))
5398 (float (l.pagedispy+l.pagevh+1))
5400 GlDraw.line_width 1.0;
5403 else scalecolor 0.8
5406 drawtiles l color;
5409 let postdrawpage l linkindexbase =
5410 match getopaque l.pageno with
5411 | Some opaque ->
5412 if tileready l l.pagex l.pagey
5413 then
5414 let x = l.pagedispx - l.pagex
5415 and y = l.pagedispy - l.pagey in
5416 let hlmask =
5417 match conf.columns with
5418 | Csingle _ | Cmulti _ ->
5419 (if conf.hlinks then 1 else 0)
5420 + (if state.glinks
5421 && not (isbirdseye state.mode) then 2 else 0)
5422 | Csplit _ -> 0
5424 let s =
5425 match state.mode with
5426 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5427 | Textentry _
5428 | Birdseye _
5429 | View
5430 | LinkNav _ -> E.s
5432 Hashtbl.find_all state.prects l.pageno |>
5433 List.iter (fun vals -> drawprect opaque x y vals);
5434 let n = postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize) in
5435 if n < 0
5436 then (state.redisplay <- true; 0)
5437 else n
5438 else 0
5439 | _ -> 0
5442 let scrollindicator () =
5443 let sbw, ph, sh = state.uioh#scrollph in
5444 let sbh, pw, sw = state.uioh#scrollpw in
5446 let x0,x1,hx0 =
5447 if conf.leftscroll
5448 then (0, sbw, sbw)
5449 else ((state.winw - sbw), state.winw, 0)
5452 Gl.enable `blend;
5453 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5454 GlDraw.color (0.64, 0.64, 0.64) ~alpha:0.7;
5455 filledrect (float x0) 0. (float x1) (float state.winh);
5456 filledrect
5457 (float hx0) (float (state.winh - sbh))
5458 (float (hx0 + state.winw)) (float state.winh)
5460 GlDraw.color (0.0, 0.0, 0.0) ~alpha:0.7;
5462 filledrect (float x0) ph (float x1) (ph +. sh);
5463 let pw = pw +. float hx0 in
5464 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5465 Gl.disable `blend;
5468 let showsel () =
5469 match state.mstate with
5470 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5473 | Msel ((x0, y0), (x1, y1)) ->
5474 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5475 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5476 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5477 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5480 let showrects =
5481 function [] -> ()
5482 | rects ->
5483 Gl.enable `blend;
5484 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5485 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5486 List.iter
5487 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5488 List.iter (fun l ->
5489 if l.pageno = pageno
5490 then (
5491 let dx = float (l.pagedispx - l.pagex) in
5492 let dy = float (l.pagedispy - l.pagey) in
5493 let r, g, b, alpha = c in
5494 GlDraw.color (r, g, b) ~alpha;
5495 filledrect2 (x0+.dx) (y0+.dy)
5496 (x1+.dx) (y1+.dy)
5497 (x3+.dx) (y3+.dy)
5498 (x2+.dx) (y2+.dy);
5500 ) state.layout
5501 ) rects
5503 Gl.disable `blend;
5506 let display () =
5507 begin match conf.columns, state.layout with
5508 | Csingle _, _ :: _ ->
5509 GlDraw.color (scalecolor2 conf.bgcolor);
5510 let y =
5511 List.fold_left (fun y l ->
5512 let x0 = 0 in
5513 let y0 = y in
5514 let x1 = l.pagedispx in
5515 let y1 = (l.pagedispy + l.pagevh) in
5516 filledrect (float x0) (float y0) (float x1) (float y1);
5517 let x0 = x1 + l.pagevw in
5518 let x1 = state.winw in
5519 filledrect1 (float x0) (float y0) (float x1) (float y1);
5520 if y != l.pagedispy
5521 then (
5522 let x0 = 0
5523 and x1 = state.winw in
5524 let y0 = y
5525 and y1 = l.pagedispy in
5526 filledrect1 (float x0) (float y0) (float x1) (float y1);
5528 l.pagedispy + l.pagevh) 0 state.layout
5530 let x0 = 0
5531 and x1 = state.winw in
5532 let y0 = y
5533 and y1 = state.winh in
5534 filledrect1 (float x0) (float y0) (float x1) (float y1)
5535 | (Cmulti _ | Csplit _), _ | Csingle _, [] ->
5536 GlClear.color (scalecolor2 conf.bgcolor);
5537 GlClear.clear [`color];
5538 end;
5539 List.iter drawpage state.layout;
5540 let rects =
5541 match state.mode with
5542 | LinkNav (Ltexact (pageno, linkno)) ->
5543 begin match getopaque pageno with
5544 | Some opaque ->
5545 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5546 let color = (0.0, 0.0, 0.5, 0.5) in
5547 (pageno, color, (
5548 float x0, float y0,
5549 float x1, float y0,
5550 float x1, float y1,
5551 float x0, float y1)
5552 ) :: state.rects
5553 | None -> state.rects
5555 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5556 | Birdseye _
5557 | Textentry _
5558 | View -> state.rects
5560 showrects rects;
5561 let rec postloop linkindexbase = function
5562 | l :: rest ->
5563 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5564 postloop linkindexbase rest
5565 | [] -> ()
5567 showsel ();
5568 postloop 0 state.layout;
5569 state.uioh#display;
5570 begin match state.mstate with
5571 | Mzoomrect ((x0, y0), (x1, y1)) ->
5572 Gl.enable `blend;
5573 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5574 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5575 filledrect (float x0) (float y0) (float x1) (float y1);
5576 Gl.disable `blend;
5577 | Msel _
5578 | Mpan _
5579 | Mscrolly | Mscrollx
5580 | Mzoom _
5581 | Mnone -> ()
5582 end;
5583 enttext ();
5584 scrollindicator ();
5585 Wsi.swapb ();
5588 let zoomrect x y x1 y1 =
5589 let x0 = min x x1
5590 and x1 = max x x1
5591 and y0 = min y y1 in
5592 let zoom = (float state.w) /. float (x1 - x0) in
5593 let margin =
5594 let simple () =
5595 if state.w < state.winw
5596 then (state.winw - state.w) / 2
5597 else 0
5599 match conf.fitmodel with
5600 | FitWidth | FitProportional -> simple ()
5601 | FitPage ->
5602 match conf.columns with
5603 | Csplit _ ->
5604 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5605 | Cmulti _ | Csingle _ -> simple ()
5607 gotoxy ((state.x + margin) - x0) (state.y + y0);
5608 state.anchor <- getanchor ();
5609 setzoom zoom;
5610 resetmstate ();
5613 let annot inline x y =
5614 match unproject x y with
5615 | Some (opaque, n, ux, uy) ->
5616 let add text =
5617 addannot opaque ux uy text;
5618 wcmd "freepage %s" (~> opaque);
5619 Hashtbl.remove state.pagemap (n, state.gen);
5620 flushtiles ();
5621 gotoxy state.x state.y
5623 if inline
5624 then
5625 let ondone s = add s in
5626 let mode = state.mode in
5627 state.mode <- Textentry (
5628 ("annotation: ", E.s, None, textentry, ondone, true),
5629 fun _ -> state.mode <- mode);
5630 state.text <- E.s;
5631 enttext ();
5632 G.postRedisplay "annot"
5633 else
5634 add @@ getusertext E.s
5635 | _ -> ()
5638 let zoomblock x y =
5639 let g opaque l px py =
5640 match rectofblock opaque px py with
5641 | Some a ->
5642 let x0 = a.(0) -. 20. in
5643 let x1 = a.(1) +. 20. in
5644 let y0 = a.(2) -. 20. in
5645 let zoom = (float state.w) /. (x1 -. x0) in
5646 let pagey = getpagey l.pageno in
5647 let margin = (state.w - l.pagew)/2 in
5648 let nx = -truncate x0 - margin in
5649 gotoxy_and_clear_text nx (pagey + truncate y0);
5650 state.anchor <- getanchor ();
5651 setzoom zoom;
5652 None
5653 | None -> None
5655 match conf.columns with
5656 | Csplit _ ->
5657 impmsg "block zooming does not work properly in split columns mode"
5658 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5661 let scrollx x =
5662 let winw = state.winw - 1 in
5663 let s = float x /. float winw in
5664 let destx = truncate (float (state.w + winw) *. s) in
5665 gotoxy_and_clear_text (winw - destx) state.y;
5666 state.mstate <- Mscrollx;
5669 let scrolly y =
5670 let s = float y /. float state.winh in
5671 let desty = truncate (float (state.maxy -
5672 (if conf.maxhfit then state.winh else 0))
5673 *. s) in
5674 gotoxy_and_clear_text state.x desty;
5675 state.mstate <- Mscrolly;
5678 let viewmulticlick clicks x y mask =
5679 let g opaque l px py =
5680 let mark =
5681 match clicks with
5682 | 2 -> Mark_word
5683 | 3 -> Mark_line
5684 | 4 -> Mark_block
5685 | _ -> Mark_page
5687 if markunder opaque px py mark
5688 then (
5689 Some (fun () ->
5690 let dopipe cmd =
5691 match getopaque l.pageno with
5692 | None -> ()
5693 | Some opaque -> pipesel opaque cmd
5695 state.roam <- (fun () -> dopipe conf.paxcmd);
5696 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5699 else None
5701 G.postRedisplay "viewmulticlick";
5702 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
5705 let canselect () =
5706 match conf.columns with
5707 | Csplit _ -> false
5708 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5711 let viewmouse button down x y mask =
5712 match button with
5713 | n when (n == 4 || n == 5) && not down ->
5714 if Wsi.withctrl mask
5715 then (
5716 match state.mstate with
5717 | Mzoom (oldn, i, (ftx, fty)) ->
5718 let recenter =
5719 if false
5720 then abs (ftx - x) > 5 || abs (fty - y) > 5
5721 else false
5723 if oldn = n
5724 then (
5725 if i = 2
5726 then
5727 let incr =
5728 match n with
5729 | 5 ->
5730 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5731 | _ ->
5732 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5734 let zoom = conf.zoom -. incr in
5735 if recenter
5736 then pivotzoom ~x ~y zoom
5737 else pivotzoom zoom;
5738 state.mstate <- Mzoom (n, 0, (x, y));
5739 else
5740 state.mstate <- Mzoom (n, i+1, (ftx, fty));
5742 else state.mstate <- Mzoom (n, 0, (ftx, fty))
5744 | Msel _
5745 | Mpan _
5746 | Mscrolly | Mscrollx
5747 | Mzoomrect _
5748 | Mnone -> state.mstate <- Mzoom (n, 0, (0, 0))
5750 else (
5751 match state.autoscroll with
5752 | Some step -> setautoscrollspeed step (n=4)
5753 | None ->
5754 if conf.wheelbypage || conf.presentation
5755 then (
5756 if n = 4
5757 then prevpage ()
5758 else nextpage ()
5760 else
5761 let incr =
5762 if n = 4
5763 then -conf.scrollstep
5764 else conf.scrollstep
5766 let incr = incr * 2 in
5767 let y = clamp incr in
5768 gotoxy_and_clear_text state.x y
5771 | n when (n = 6 || n = 7) && not down && canpan () ->
5772 let x =
5773 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep) in
5774 gotoxy_and_clear_text x state.y
5776 | 1 when Wsi.withshift mask ->
5777 state.mstate <- Mnone;
5778 if not down
5779 then (
5780 match unproject x y with
5781 | None -> ()
5782 | Some (_, pageno, ux, uy) ->
5783 let cmd = Printf.sprintf
5784 "%s %s %d %d %d"
5785 conf.stcmd state.path pageno ux uy
5787 match spawn cmd [] with
5788 | exception exn ->
5789 impmsg "execution of synctex command(%S) failed: %S"
5790 conf.stcmd @@ exntos exn
5791 | _pid -> ()
5794 | 1 when Wsi.withctrl mask ->
5795 if down
5796 then (
5797 Wsi.setcursor Wsi.CURSOR_FLEUR;
5798 state.mstate <- Mpan (x, y)
5800 else
5801 state.mstate <- Mnone
5803 | 3 ->
5804 if down
5805 then (
5806 if Wsi.withshift mask
5807 then (
5808 annot conf.annotinline x y;
5809 G.postRedisplay "addannot"
5811 else
5812 let p = (x, y) in
5813 Wsi.setcursor Wsi.CURSOR_CYCLE;
5814 state.mstate <- Mzoomrect (p, p)
5816 else (
5817 match state.mstate with
5818 | Mzoomrect ((x0, y0), _) ->
5819 if abs (x-x0) > 10 && abs (y - y0) > 10
5820 then zoomrect x0 y0 x y
5821 else (
5822 resetmstate ();
5823 G.postRedisplay "kill accidental zoom rect";
5825 | Msel _
5826 | Mpan _
5827 | Mscrolly | Mscrollx
5828 | Mzoom _
5829 | Mnone -> resetmstate ()
5832 | 1 when vscrollhit x ->
5833 if down
5834 then
5835 let _, position, sh = state.uioh#scrollph in
5836 if y > truncate position && y < truncate (position +. sh)
5837 then state.mstate <- Mscrolly
5838 else scrolly y
5839 else
5840 state.mstate <- Mnone
5842 | 1 when y > state.winh - hscrollh () ->
5843 if down
5844 then
5845 let _, position, sw = state.uioh#scrollpw in
5846 if x > truncate position && x < truncate (position +. sw)
5847 then state.mstate <- Mscrollx
5848 else scrollx x
5849 else
5850 state.mstate <- Mnone
5852 | 1 when state.bzoom -> if not down then zoomblock x y
5854 | 1 ->
5855 let dest = if down then getunder x y else Unone in
5856 begin match dest with
5857 | Ulinkuri _ ->
5858 gotounder dest
5860 | Unone when down ->
5861 Wsi.setcursor Wsi.CURSOR_FLEUR;
5862 state.mstate <- Mpan (x, y);
5864 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5866 | Unone | Utext _ ->
5867 if down
5868 then (
5869 if canselect ()
5870 then (
5871 state.mstate <- Msel ((x, y), (x, y));
5872 G.postRedisplay "mouse select";
5875 else (
5876 match state.mstate with
5877 | Mnone -> ()
5879 | Mzoom _ | Mscrollx | Mscrolly ->
5880 state.mstate <- Mnone
5882 | Mzoomrect ((x0, y0), _) ->
5883 zoomrect x0 y0 x y
5885 | Mpan _ ->
5886 Wsi.setcursor Wsi.CURSOR_INHERIT;
5887 state.mstate <- Mnone
5889 | Msel ((x0, y0), (x1, y1)) ->
5890 let rec loop = function
5891 | [] -> ()
5892 | l :: rest ->
5893 let inside =
5894 let a0 = l.pagedispy in
5895 let a1 = a0 + l.pagevh in
5896 let b0 = l.pagedispx in
5897 let b1 = b0 + l.pagevw in
5898 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5899 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5901 if inside
5902 then
5903 match getopaque l.pageno with
5904 | Some opaque ->
5905 let dosel cmd () =
5906 match Unix.pipe () with
5907 | exception exn ->
5908 impmsg "cannot create sel pipe: %s" @@
5909 exntos exn;
5910 | (r, w) ->
5911 let clo what fd =
5912 Ne.clo fd (fun msg ->
5913 dolog "%s close failed: %s" what msg)
5915 begin match spawn cmd [r, 0; w, -1] with
5916 | exception exn ->
5917 clo "Msel pipe/w" w;
5918 dolog "cannot execute %S: %s" cmd @@ exntos exn
5919 | _pid ->
5920 copysel w opaque;
5921 G.postRedisplay "copysel";
5922 end;
5923 clo "Msel pipe/r" r;
5925 dosel conf.selcmd ();
5926 state.roam <- dosel conf.paxcmd;
5927 | None -> ()
5928 else loop rest
5930 loop state.layout;
5931 resetmstate ();
5935 | _ -> ()
5938 let birdseyemouse button down x y mask
5939 (conf, leftx, _, hooverpageno, anchor) =
5940 match button with
5941 | 1 when down ->
5942 let rec loop = function
5943 | [] -> ()
5944 | l :: rest ->
5945 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5946 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5947 then (
5948 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5950 else loop rest
5952 loop state.layout
5953 | 3 -> ()
5954 | _ -> viewmouse button down x y mask
5957 let uioh = object
5958 method display = ()
5960 method key key mask =
5961 begin match state.mode with
5962 | Textentry textentry -> textentrykeyboard key mask textentry
5963 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5964 | View -> viewkeyboard key mask
5965 | LinkNav linknav -> linknavkeyboard key mask linknav
5966 end;
5967 state.uioh
5969 method button button bstate x y mask =
5970 begin match state.mode with
5971 | LinkNav _ | View -> viewmouse button bstate x y mask
5972 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5973 | Textentry _ -> ()
5974 end;
5975 state.uioh
5977 method multiclick clicks x y mask =
5978 begin match state.mode with
5979 | LinkNav _ | View -> viewmulticlick clicks x y mask
5980 | Birdseye _ | Textentry _ -> ()
5981 end;
5982 state.uioh
5984 method motion x y =
5985 begin match state.mode with
5986 | Textentry _ -> ()
5987 | View | Birdseye _ | LinkNav _ ->
5988 match state.mstate with
5989 | Mzoom _ | Mnone -> ()
5991 | Mpan (x0, y0) ->
5992 let dx = x - x0
5993 and dy = y0 - y in
5994 state.mstate <- Mpan (x, y);
5995 let x = if canpan () then panbound (state.x + dx) else state.x in
5996 let y = clamp dy in
5997 gotoxy_and_clear_text x y
5999 | Msel (a, _) ->
6000 state.mstate <- Msel (a, (x, y));
6001 G.postRedisplay "motion select";
6003 | Mscrolly ->
6004 let y = min state.winh (max 0 y) in
6005 scrolly y
6007 | Mscrollx ->
6008 let x = min state.winw (max 0 x) in
6009 scrollx x
6011 | Mzoomrect (p0, _) ->
6012 state.mstate <- Mzoomrect (p0, (x, y));
6013 G.postRedisplay "motion zoomrect";
6014 end;
6015 state.uioh
6017 method pmotion x y =
6018 begin match state.mode with
6019 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6020 let rec loop = function
6021 | [] ->
6022 if hooverpageno != -1
6023 then (
6024 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6025 G.postRedisplay "pmotion birdseye no hoover";
6027 | l :: rest ->
6028 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6029 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6030 then (
6031 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6032 G.postRedisplay "pmotion birdseye hoover";
6034 else loop rest
6036 loop state.layout
6038 | Textentry _ -> ()
6040 | LinkNav _ | View ->
6041 match state.mstate with
6042 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
6043 | Mnone ->
6044 updateunder x y;
6045 if canselect ()
6046 then
6047 match conf.pax with
6048 | None -> ()
6049 | Some r ->
6050 let past, _, _ = !r in
6051 let now = now () in
6052 let delta = now -. past in
6053 if delta > 0.01
6054 then paxunder x y
6055 else r := (now, x, y)
6056 end;
6057 state.uioh
6059 method infochanged _ = ()
6061 method scrollph =
6062 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6063 let p, h =
6064 if maxy = 0
6065 then 0.0, float state.winh
6066 else scrollph state.y maxy
6068 vscrollw (), p, h
6070 method scrollpw =
6071 let fwinw = float (state.winw - vscrollw ()) in
6072 let sw =
6073 let sw = fwinw /. float state.w in
6074 let sw = fwinw *. sw in
6075 max sw (float conf.scrollh)
6077 let position =
6078 let maxx = state.w + state.winw in
6079 let x = state.winw - state.x in
6080 let percent = float x /. float maxx in
6081 (fwinw -. sw) *. percent
6083 hscrollh (), position, sw
6085 method modehash =
6086 let modename =
6087 match state.mode with
6088 | LinkNav _ -> "links"
6089 | Textentry _ -> "textentry"
6090 | Birdseye _ -> "birdseye"
6091 | View -> "view"
6093 findkeyhash conf modename
6095 method eformsgs = true
6096 method alwaysscrolly = false
6097 end;;
6099 let addrect pageno r g b a x0 y0 x1 y1 =
6100 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
6103 let ract cmds =
6104 let cl = splitatchar cmds ' ' in
6105 let scan s fmt f =
6106 try Scanf.sscanf s fmt f
6107 with exn -> adderrfmt "remote exec" "error processing '%S': %s\n"
6108 cmds @@ exntos exn
6110 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
6111 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
6112 s pageno r g b a x0 y0 x1 y1;
6113 onpagerect
6114 pageno
6115 (fun w h ->
6116 let _,w1,h1,_ = getpagedim pageno in
6117 let sw = float w1 /. float w
6118 and sh = float h1 /. float h in
6119 let x0s = x0 *. sw
6120 and x1s = x1 *. sw
6121 and y0s = y0 *. sh
6122 and y1s = y1 *. sh in
6123 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6124 let color = (r, g, b, a) in
6125 if conf.verbose then debugrect rect;
6126 state.rects <- (pageno, color, rect) :: state.rects;
6127 G.postRedisplay s;
6130 match cl with
6131 | "reload", "" -> reload ()
6132 | "goto", args ->
6133 scan args "%u %f %f"
6134 (fun pageno x y ->
6135 let cmd, _ = state.geomcmds in
6136 if emptystr cmd
6137 then gotopagexy !wtmode pageno x y
6138 else
6139 let f prevf () =
6140 gotopagexy !wtmode pageno x y;
6141 prevf ()
6143 state.reprf <- f state.reprf
6145 | "goto1", args -> scan args "%u %f" gotopage
6146 | "gotor", args ->
6147 scan args "%S %u"
6148 (fun _filename _pageno ->
6149 failwith "gotounder (Uremote (filename, pageno))")
6150 | "gotord", args ->
6151 scan args "%S %S"
6152 (fun _filename _dest ->
6153 failwith "gotounder (Uremotedest (filename, dest))")
6154 | "rect", args ->
6155 scan args "%u %u %f %f %f %f"
6156 (fun pageno c x0 y0 x1 y1 ->
6157 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
6158 rectx "rect" pageno color x0 y0 x1 y1;
6160 | "prect", args ->
6161 scan args "%u %f %f %f %f %f %f %f %f"
6162 (fun pageno r g b alpha x0 y0 x1 y1 ->
6163 addrect pageno r g b alpha x0 y0 x1 y1;
6164 G.postRedisplay "prect"
6166 | "pgoto", args ->
6167 scan args "%u %f %f"
6168 (fun pageno x y ->
6169 let optopaque =
6170 match getopaque pageno with
6171 | Some opaque -> opaque
6172 | None -> ~< E.s
6174 pgoto optopaque pageno x y;
6175 let rec fixx = function
6176 | [] -> ()
6177 | l :: rest ->
6178 if l.pageno = pageno
6179 then gotoxy (state.x - l.pagedispx) state.y
6180 else fixx rest
6182 let layout =
6183 let mult =
6184 match conf.columns with
6185 | Csingle _ | Csplit _ -> 1
6186 | Cmulti ((n, _, _), _) -> n
6188 layout 0 state.y (state.winw * mult) state.winh
6190 fixx layout
6192 | "activatewin", "" -> Wsi.activatewin ()
6193 | "quit", "" -> raise Quit
6194 | "keys", keys ->
6195 begin try
6196 let l = Config.keys_of_string keys in
6197 List.iter (fun (k, m) -> keyboard k m) l
6198 with exn -> adderrfmt "error processing keys" "`%S': %s\n"
6199 cmds @@ exntos exn
6201 | "clearrects", "" ->
6202 Hashtbl.clear state.prects;
6203 G.postRedisplay "clearrects"
6204 | _ ->
6205 adderrfmt "remote command"
6206 "error processing remote command: %S\n" cmds;
6209 let remote =
6210 let scratch = Bytes.create 80 in
6211 let buf = Buffer.create 80 in
6212 fun fd ->
6213 match tempfailureretry (Unix.read fd scratch 0) 80 with
6214 | exception Unix.Unix_error (Unix.EAGAIN, _, _) -> None
6215 | 0 ->
6216 Unix.close fd;
6217 if Buffer.length buf > 0
6218 then (
6219 let s = Buffer.contents buf in
6220 Buffer.clear buf;
6221 ract s;
6223 None
6224 | n ->
6225 let rec eat ppos =
6226 let nlpos =
6227 match Bytes.index_from scratch ppos '\n' with
6228 | pos -> if pos >= n then -1 else pos
6229 | exception Not_found -> -1
6231 if nlpos >= 0
6232 then (
6233 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6234 let s = Buffer.contents buf in
6235 Buffer.clear buf;
6236 ract s;
6237 eat (nlpos+1);
6239 else (
6240 Buffer.add_subbytes buf scratch ppos (n-ppos);
6241 Some fd
6243 in eat 0
6246 let remoteopen path =
6247 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6248 with exn ->
6249 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
6250 None
6253 let () =
6254 let gcconfig = ref E.s in
6255 let trimcachepath = ref E.s in
6256 let rcmdpath = ref E.s in
6257 let pageno = ref None in
6258 let rootwid = ref 0 in
6259 let openlast = ref false in
6260 let nofc = ref false in
6261 let doreap = ref false in
6262 let csspath = ref None in
6263 selfexec := Sys.executable_name;
6264 Arg.parse
6265 (Arg.align
6266 [("-p", Arg.String (fun s -> state.password <- s),
6267 "<password> Set password");
6269 ("-f", Arg.String
6270 (fun s ->
6271 Config.fontpath := s;
6272 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6274 "<path> Set path to the user interface font");
6276 ("-c", Arg.String
6277 (fun s ->
6278 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6279 Config.confpath := s),
6280 "<path> Set path to the configuration file");
6282 ("-last", Arg.Set openlast, " Open last document");
6284 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6285 "<page-number> Jump to page");
6287 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6288 "<path> Set path to the trim cache file");
6290 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6291 "<named-destination> Set named destination");
6293 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6294 ("-cxack", Arg.Set cxack, " Cut corners");
6296 ("-remote", Arg.String (fun s -> rcmdpath := s),
6297 "<path> Set path to the source of remote commands");
6299 ("-gc", Arg.Set_string gcconfig,
6300 "<path> Collect garbage with the help of a script");
6302 ("-nofc", Arg.Set nofc, " Do not use fontconfig");
6304 ("-v", Arg.Unit (fun () ->
6305 Printf.printf
6306 "%s\nconfiguration path: %s\n"
6307 (version ())
6308 Config.defconfpath;
6309 exit 0), " Print version and exit");
6311 ("-css", Arg.String (fun s -> csspath := Some s),
6312 "<path> Set path to the style sheet to use with EPUB/HTML");
6314 ("-embed", Arg.Set_int rootwid, "<window-id> Embed into window");
6316 ("-origin", Arg.String (fun s -> state.origin <- s),
6317 "<origin> <undocumented>");
6320 (fun s -> state.path <- s)
6321 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:");
6323 if !wtmode
6324 then selfexec := !selfexec ^ " -wtmode";
6326 let histmode = emptystr state.path && not !openlast in
6328 if not (Config.load !openlast)
6329 then dolog "failed to load configuration";
6331 begin match !pageno with
6332 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6333 | None -> ()
6334 end;
6336 if nonemptystr !gcconfig
6337 then (
6338 let (c, s) =
6339 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6340 | exception exn -> error "socketpair for gc failed: %s" @@ exntos exn
6341 | fds -> fds
6343 match spawn !gcconfig [(c, 0); (c, 1); (s, -1)] with
6344 | exception exn -> error "failed to execute gc script: %s" @@ exntos exn
6345 | _pid ->
6346 Ne.clo c @@ (fun s -> error "failed to close gc fd %s" s);
6347 Config.gc s;
6348 exit 0
6351 let mu =
6352 object (self)
6353 val mutable m_clicks = 0
6354 val mutable m_click_x = 0
6355 val mutable m_click_y = 0
6356 val mutable m_lastclicktime = infinity
6358 method private cleanup =
6359 state.roam <- noroam;
6360 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6361 method expose = G.postRedisplay "expose"
6362 method visible v =
6363 let name =
6364 match v with
6365 | Wsi.Unobscured -> "unobscured"
6366 | Wsi.PartiallyObscured -> "partiallyobscured"
6367 | Wsi.FullyObscured -> "fullyobscured"
6369 vlog "visibility change %s" name
6370 method display = display ()
6371 method map mapped = vlog "mapped %b" mapped
6372 method reshape w h =
6373 self#cleanup;
6374 reshape w h
6375 method mouse b d x y m =
6376 if d && canselect ()
6377 then (
6379 * http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx
6381 m_click_x <- x;
6382 m_click_y <- y;
6383 if b = 1
6384 then (
6385 let t = now () in
6386 if abs x - m_click_x > 10
6387 || abs y - m_click_y > 10
6388 || abs_float (t -. m_lastclicktime) > 0.3
6389 then m_clicks <- 0;
6390 m_clicks <- m_clicks + 1;
6391 m_lastclicktime <- t;
6392 if m_clicks = 1
6393 then (
6394 self#cleanup;
6395 G.postRedisplay "cleanup";
6396 state.uioh <- state.uioh#button b d x y m;
6398 else state.uioh <- state.uioh#multiclick m_clicks x y m
6400 else (
6401 self#cleanup;
6402 m_clicks <- 0;
6403 m_lastclicktime <- infinity;
6404 state.uioh <- state.uioh#button b d x y m
6407 else (
6408 state.uioh <- state.uioh#button b d x y m
6410 method motion x y =
6411 state.mpos <- (x, y);
6412 state.uioh <- state.uioh#motion x y
6413 method pmotion x y =
6414 state.mpos <- (x, y);
6415 state.uioh <- state.uioh#pmotion x y
6416 method key k m =
6417 let mascm = m land (
6418 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6419 ) in
6420 let keyboard k m =
6421 let x = state.x and y = state.y in
6422 keyboard k m;
6423 if x != state.x || y != state.y then self#cleanup
6425 match state.keystate with
6426 | KSnone ->
6427 let km = k, mascm in
6428 begin
6429 match
6430 let modehash = state.uioh#modehash in
6431 try Hashtbl.find modehash km
6432 with Not_found ->
6433 try Hashtbl.find (findkeyhash conf "global") km
6434 with Not_found -> KMinsrt (k, m)
6435 with
6436 | KMinsrt (k, m) -> keyboard k m
6437 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6438 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6440 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6441 List.iter (fun (k, m) -> keyboard k m) insrt;
6442 state.keystate <- KSnone
6443 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6444 state.keystate <- KSinto (keys, insrt)
6445 | KSinto _ -> state.keystate <- KSnone
6447 method enter x y =
6448 state.mpos <- (x, y);
6449 state.uioh <- state.uioh#pmotion x y
6450 method leave = state.mpos <- (-1, -1)
6451 method winstate wsl = state.winstate <- wsl
6452 method quit = raise Quit
6455 let wsfd, winw, winh = Wsi.init mu !rootwid conf.cwinw conf.cwinh platform in
6457 setbgcol conf.bgcolor;
6458 state.wsfd <- wsfd;
6460 if not @@ List.exists GlMisc.check_extension
6461 [ "GL_ARB_texture_rectangle"
6462 ; "GL_EXT_texture_recangle"
6463 ; "GL_NV_texture_rectangle" ]
6464 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
6466 if substratis (GlMisc.get_string `renderer) 0 "Mesa DRI Intel("
6467 then (
6468 defconf.sliceheight <- 1024;
6469 defconf.texcount <- 32;
6470 defconf.usepbo <- true;
6473 let cs, ss =
6474 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6475 | exception exn ->
6476 dolog "socketpair failed: %s" @@ exntos exn;
6477 exit 1
6478 | (r, w) ->
6479 cloexec r;
6480 cloexec w;
6481 r, w
6484 setcheckers conf.checkers;
6486 opengl_has_pbo := GlMisc.check_extension "GL_ARB_pixel_buffer_object";
6488 begin match !csspath with
6489 | None -> ()
6490 | Some "" -> conf.css <- E.s
6491 | Some path ->
6492 let css = filecontents path in
6493 let l = String.length css in
6494 conf.css <-
6495 if substratis css (l-2) "\r\n"
6496 then String.sub css 0 (l-2)
6497 else (if css.[l-1] = '\n'
6498 then String.sub css 0 (l-1)
6499 else css);
6500 end;
6501 init cs (
6502 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6503 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6504 !Config.fontpath, !trimcachepath, !opengl_has_pbo, not !nofc
6506 List.iter GlArray.enable [`texture_coord; `vertex];
6507 state.ss <- ss;
6508 reshape ~firsttime:true winw winh;
6509 state.uioh <- uioh;
6510 if histmode
6511 then (
6512 Wsi.settitle "llpp (history)";
6513 enterhistmode ();
6515 else (
6516 state.text <- "Opening " ^ (mbtoutf8 state.path);
6517 opendoc state.path state.password;
6519 display ();
6520 Wsi.mapwin ();
6521 Wsi.setcursor Wsi.CURSOR_INHERIT;
6522 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6524 let rec reap () =
6525 match Unix.waitpid [Unix.WNOHANG] ~-1 with
6526 | exception (Unix.Unix_error (Unix.ECHILD, _, _)) -> ()
6527 | exception exn -> dolog "Unix.waitpid: %s" @@ exntos exn
6528 | 0, _ -> ()
6529 | _pid, _status -> reap ()
6531 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
6533 let optrfd =
6534 ref (
6535 if nonemptystr !rcmdpath
6536 then remoteopen !rcmdpath
6537 else None
6541 let rec loop deadline =
6542 if !doreap
6543 then (
6544 doreap := false;
6545 reap ()
6547 let r = [state.ss; state.wsfd] in
6548 let r =
6549 match !optrfd with
6550 | None -> r
6551 | Some fd -> fd :: r
6553 if state.redisplay
6554 then (
6555 state.redisplay <- false;
6556 display ();
6558 let timeout =
6559 let now = now () in
6560 if deadline > now
6561 then (
6562 if deadline = infinity
6563 then ~-.1.0
6564 else max 0.0 (deadline -. now)
6566 else 0.0
6568 let r, _, _ =
6569 try Unix.select r [] [] timeout
6570 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6572 begin match r with
6573 | [] ->
6574 state.ghyll None;
6575 let newdeadline =
6576 if state.ghyll == noghyll
6577 then
6578 match state.autoscroll with
6579 | Some step when step != 0 ->
6580 let y = state.y + step in
6581 let fy = if conf.maxhfit then state.winh else 0 in
6582 let y =
6583 if y < 0
6584 then state.maxy - fy
6585 else if y >= state.maxy - fy then 0 else y
6587 if state.mode = View
6588 then gotoxy_and_clear_text state.x y
6589 else gotoxy state.x y;
6590 deadline +. 0.01
6591 | _ -> infinity
6592 else deadline +. 0.01
6594 loop newdeadline
6596 | l ->
6597 let rec checkfds = function
6598 | [] -> ()
6599 | fd :: rest when fd = state.ss ->
6600 let cmd = rcmd state.ss in
6601 act cmd;
6602 checkfds rest
6604 | fd :: rest when fd = state.wsfd ->
6605 Wsi.readresp fd;
6606 checkfds rest
6608 | fd :: rest when Some fd = !optrfd ->
6609 begin match remote fd with
6610 | None -> optrfd := remoteopen !rcmdpath;
6611 | opt -> optrfd := opt
6612 end;
6613 checkfds rest
6615 | _ :: rest ->
6616 dolog "select returned unknown descriptor";
6617 checkfds rest
6619 checkfds l;
6620 let newdeadline =
6621 let deadline1 =
6622 if deadline = infinity
6623 then now () +. 0.01
6624 else deadline
6626 match state.autoscroll with
6627 | Some step when step != 0 -> deadline1
6628 | _ -> if state.ghyll == noghyll then infinity else deadline1
6630 loop newdeadline
6631 end;
6633 match loop infinity with
6634 | exception Quit ->
6635 Config.save leavebirdseye;
6636 if hasunsavedchanges ()
6637 then save ()
6638 | _ -> error "umpossible - infinity reached"