Compress
[llpp.git] / main.ml
blob73731048452b2cdfc414c849971a73c73de0e39a
1 open Utils;;
2 open Config;;
3 open Glutils;;
4 open Listview;;
6 external init : Unix.file_descr -> initparams -> unit = "ml_init";;
7 external seltext : opaque -> (int * int * int * int) -> unit = "ml_seltext";;
8 external hassel : opaque -> bool = "ml_hassel";;
9 external getpdimrect : int -> float array = "ml_getpdimrect";;
10 external whatsunder : opaque -> int -> int -> under = "ml_whatsunder";;
11 external markunder : opaque -> int -> int -> mark -> bool = "ml_markunder";;
12 external clearmark : opaque -> unit = "ml_clearmark";;
13 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
14 external getmaxw : unit -> float = "ml_getmaxw";;
15 external postprocess :
16 opaque -> int -> int -> int -> (int * string * int) -> int
17 = "ml_postprocess";;
18 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
19 external setaalevel : int -> unit = "ml_setaalevel";;
20 external realloctexts : int -> bool = "ml_realloctexts";;
21 external findlink : opaque -> linkdir -> link = "ml_findlink";;
22 external getlink : opaque -> int -> under = "ml_getlink";;
23 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
24 external getlinkcount : opaque -> int = "ml_getlinkcount";;
25 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links";;
26 external getpbo : width -> height -> colorspace -> opaque = "ml_getpbo";;
27 external freepbo : opaque -> unit = "ml_freepbo";;
28 external unmappbo : opaque -> unit = "ml_unmappbo";;
29 external bousable : unit -> bool = "ml_bo_usable";;
30 external unproject :
31 opaque -> int -> int -> (int * int) option = "ml_unproject";;
32 external project :
33 opaque -> int -> int -> float -> float -> (float * float) = "ml_project";;
34 external drawtile :
35 tileparams -> opaque -> unit = "ml_drawtile";;
36 external rectofblock :
37 opaque -> int -> int -> float array option = "ml_rectofblock";;
38 external begintiles : unit -> unit = "ml_begintiles";;
39 external endtiles : unit -> unit = "ml_endtiles";;
40 external addannot : opaque -> int -> int -> string -> unit = "ml_addannot";;
41 external modannot : opaque -> slinkindex -> string -> unit = "ml_modannot";;
42 external delannot : opaque -> slinkindex -> unit = "ml_delannot";;
43 external hasunsavedchanges : unit -> bool = "ml_hasunsavedchanges";;
44 external savedoc : string -> unit = "ml_savedoc";;
45 external getannotcontents :
46 opaque -> slinkindex -> string = "ml_getannotcontents";;
47 external drawprect :
48 opaque -> int -> int -> float array -> unit = "ml_drawprect";;
49 external wcmd : Unix.file_descr -> bytes -> int -> unit = "ml_wcmd";;
50 external rcmd : Unix.file_descr -> string = "ml_rcmd";;
51 external uritolocation :
52 string -> (pageno * float * float) = "ml_uritolocation";;
53 external isexternallink : string -> bool = "ml_isexternallink";;
55 (* copysel _will_ close the supplied descriptor *)
56 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
58 let selfexec = ref E.s;;
59 let ignoredoctitlte = ref false;;
60 let opengl_has_pbo = ref false;;
61 let layouth = ref ~-1;;
63 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
64 dolog {|rect {
65 x0,y0=(% f, % f)
66 x1,y1=(% f, % f)
67 x2,y2=(% f, % f)
68 x3,y3=(% f, % f)
69 }|} x0 y0 x1 y1 x2 y2 x3 y3;
72 let pgscale h = truncate (float h *. conf.pgscale);;
74 let hscrollh () =
75 if state.uioh#alwaysscrolly || ((conf.scrollb land scrollbhv != 0)
76 && (state.w > state.winw))
77 then conf.scrollbw
78 else 0
81 let setfontsize n =
82 fstate.fontsize <- n;
83 fstate.wwidth <- measurestr fstate.fontsize "w";
84 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
87 let vlog fmt =
88 if conf.verbose
89 then dolog fmt
90 else Printf.kprintf ignore fmt
93 let launchpath () =
94 if emptystr conf.pathlauncher
95 then dolog "%s" state.path
96 else (
97 let command =
98 Str.global_replace Utils.Re.percent state.path conf.pathlauncher in
99 match spawn command [] with
100 | _pid -> ()
101 | exception exn ->
102 dolog "failed to execute `%s': %s" command @@ exntos exn
106 let getopaque pageno =
107 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
108 with Not_found -> None
111 let pagetranslatepoint l x y =
112 let dy = y - l.pagedispy in
113 let y = dy + l.pagey in
114 let dx = x - l.pagedispx in
115 let x = dx + l.pagex in
116 (x, y);
119 let onppundermouse g x y d =
120 let rec f = function
121 | l :: rest ->
122 begin match getopaque l.pageno with
123 | Some opaque ->
124 let x0 = l.pagedispx in
125 let x1 = x0 + l.pagevw in
126 let y0 = l.pagedispy in
127 let y1 = y0 + l.pagevh in
128 if y >= y0 && y <= y1 && x >= x0 && x <= x1
129 then
130 let px, py = pagetranslatepoint l x y in
131 match g opaque l px py with
132 | Some res -> res
133 | None -> f rest
134 else f rest
135 | _ ->
136 f rest
138 | [] -> d
140 f state.layout
143 let getunder x y =
144 let g opaque l px py =
145 if state.bzoom
146 then (
147 match rectofblock opaque px py with
148 | Some [|x0;x1;y0;y1|] ->
149 let rect = (x0, y0, x1, y0, x1, y1, x0, y1) in
150 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
151 state.rects <- [l.pageno, color, rect];
152 postRedisplay "getunder";
153 | _ -> ()
155 let under = whatsunder opaque px py in
156 if under = Unone then None else Some under
158 onppundermouse g x y Unone
161 let unproject x y =
162 let g opaque l x y =
163 match unproject opaque x y with
164 | Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
165 | None -> None
167 onppundermouse g x y None;
170 let showtext c s =
171 state.text <- Printf.sprintf "%c%s" c s;
172 postRedisplay "showtext";
175 let impmsg fmt =
176 Format.ksprintf (fun s -> showtext '!' s) fmt;
179 let pipesel opaque cmd =
180 if hassel opaque
181 then pipef ~closew:false "pipesel"
182 (fun w ->
183 copysel w opaque;
184 postRedisplay "pipesel"
185 ) cmd
188 let paxunder x y =
189 let g opaque l px py =
190 if markunder opaque px py conf.paxmark
191 then (
192 Some (fun () ->
193 match getopaque l.pageno with
194 | None -> ()
195 | Some opaque -> pipesel opaque conf.paxcmd
198 else None
200 postRedisplay "paxunder";
201 if conf.paxmark = Mark_page
202 then
203 List.iter (fun l ->
204 match getopaque l.pageno with
205 | None -> ()
206 | Some opaque -> clearmark opaque) state.layout;
207 state.roam <- onppundermouse g x y (fun () -> impmsg "whoopsie daisy");
210 let undertext = function
211 | Unone -> "none"
212 | Ulinkuri s -> s
213 | Utext s -> "font: " ^ s
214 | Uannotation (opaque, slinkindex) ->
215 "annotation: " ^ getannotcontents opaque slinkindex
218 let updateunder x y =
219 match getunder x y with
220 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
221 | Ulinkuri uri ->
222 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
223 Wsi.setcursor Wsi.CURSOR_INFO
224 | Utext s ->
225 if conf.underinfo then showtext 'f' ("ont: " ^ s);
226 Wsi.setcursor Wsi.CURSOR_TEXT
227 | Uannotation _ ->
228 if conf.underinfo then showtext 'a' "nnotation";
229 Wsi.setcursor Wsi.CURSOR_INFO
232 let showlinktype under =
233 if conf.underinfo && under != Unone
234 then showtext ' ' @@ undertext under
237 let intentry_with_suffix text key =
238 let text =
239 match [@warning "-4"] key with
240 | Keys.Ascii ('0'..'9' as c) -> addchar text c
241 | Keys.Ascii ('k' | 'm' | 'g' | 'K' | 'M' | 'G' as c) ->
242 addchar text @@ asciilower c
243 | _ ->
244 state.text <- Printf.sprintf "invalid key";
245 text
247 TEcont text
250 let wcmd fmt =
251 let b = Buffer.create 16 in
252 Printf.kbprintf
253 (fun b ->
254 let b = Buffer.to_bytes b in
255 wcmd state.ss b @@ Bytes.length b
256 ) b fmt
259 let nogeomcmds cmds =
260 match cmds with
261 | s, [] -> emptystr s
262 | _ -> false
265 let layoutN ((columns, coverA, coverB), b) x y sw sh =
266 let rec fold accu n =
267 if n = Array.length b
268 then accu
269 else
270 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
271 if (vy - y) > sh &&
272 (n = coverA - 1
273 || n = state.pagecount - coverB
274 || (n - coverA) mod columns = columns - 1)
275 then accu
276 else
277 let accu =
278 if vy + h > y
279 then
280 let pagey = max 0 (y - vy) in
281 let pagedispy = if pagey > 0 then 0 else vy - y in
282 let pagedispx, pagex =
283 let pdx =
284 if n = coverA - 1 || n = state.pagecount - coverB
285 then x + (sw - w) / 2
286 else dx + xoff + x
288 if pdx < 0
289 then 0, -pdx
290 else pdx, 0
292 let pagevw =
293 let vw = sw - pagedispx in
294 let pw = w - pagex in
295 min vw pw
297 let pagevh = min (h - pagey) (sh - pagedispy) in
298 if pagevw > 0 && pagevh > 0
299 then
300 let e =
301 { pageno = n
302 ; pagedimno = pdimno
303 ; pagew = w
304 ; pageh = h
305 ; pagex = pagex
306 ; pagey = pagey
307 ; pagevw = pagevw
308 ; pagevh = pagevh
309 ; pagedispx = pagedispx
310 ; pagedispy = pagedispy
311 ; pagecol = 0
314 e :: accu
315 else
316 accu
317 else
318 accu
320 fold accu (n+1)
322 if Array.length b = 0
323 then []
324 else List.rev (fold [] (page_of_y y))
327 let layoutS (columns, b) x y sw sh =
328 let rec fold accu n =
329 if n = Array.length b
330 then accu
331 else
332 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
333 if (vy - y) > sh
334 then accu
335 else
336 let accu =
337 if vy + pageh > y
338 then
339 let x = xoff + x in
340 let pagey = max 0 (y - vy) in
341 let pagedispy = if pagey > 0 then 0 else vy - y in
342 let pagedispx, pagex =
343 if px = 0
344 then (
345 if x < 0
346 then 0, -x
347 else x, 0
349 else (
350 let px = px - x in
351 if px < 0
352 then -px, 0
353 else 0, px
356 let pagecolw = pagew/columns in
357 let pagedispx =
358 if pagecolw < sw
359 then pagedispx + ((sw - pagecolw) / 2)
360 else pagedispx
362 let pagevw =
363 let vw = sw - pagedispx in
364 let pw = pagew - pagex in
365 min vw pw
367 let pagevw = min pagevw pagecolw in
368 let pagevh = min (pageh - pagey) (sh - pagedispy) in
369 if pagevw > 0 && pagevh > 0
370 then
371 let e =
372 { pageno = n/columns
373 ; pagedimno = pdimno
374 ; pagew = pagew
375 ; pageh = pageh
376 ; pagex = pagex
377 ; pagey = pagey
378 ; pagevw = pagevw
379 ; pagevh = pagevh
380 ; pagedispx = pagedispx
381 ; pagedispy = pagedispy
382 ; pagecol = n mod columns
385 e :: accu
386 else
387 accu
388 else
389 accu
391 fold accu (n+1)
393 List.rev (fold [] 0)
396 let layout x y sw sh =
397 if nogeomcmds state.geomcmds
398 then
399 match conf.columns with
400 | Csingle b -> layoutN ((1, 0, 0), b) x y sw sh
401 | Cmulti c -> layoutN c x y sw sh
402 | Csplit s -> layoutS s x y sw sh
403 else []
406 let maxy () = state.maxy - if conf.maxhfit then state.winh else 0;;
408 let clamp incr = bound (state.y + incr) 0 @@ maxy ();;
410 let itertiles l f =
411 let tilex = l.pagex mod conf.tilew in
412 let tiley = l.pagey mod conf.tileh in
414 let col = l.pagex / conf.tilew in
415 let row = l.pagey / conf.tileh in
417 let rec rowloop row y0 dispy h =
418 if h = 0
419 then ()
420 else (
421 let dh = conf.tileh - y0 in
422 let dh = min h dh in
423 let rec colloop col x0 dispx w =
424 if w = 0
425 then ()
426 else (
427 let dw = conf.tilew - x0 in
428 let dw = min w dw in
429 f col row dispx dispy x0 y0 dw dh;
430 colloop (col+1) 0 (dispx+dw) (w-dw)
433 colloop col tilex l.pagedispx l.pagevw;
434 rowloop (row+1) 0 (dispy+dh) (h-dh)
437 if l.pagevw > 0 && l.pagevh > 0
438 then rowloop row tiley l.pagedispy l.pagevh;
441 let gettileopaque l col row =
442 let key =
443 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
445 try Some (Hashtbl.find state.tilemap key)
446 with Not_found -> None
449 let puttileopaque l col row gen colorspace angle opaque size elapsed =
450 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
451 Hashtbl.add state.tilemap key (opaque, size, elapsed)
454 let drawtiles l color =
455 GlDraw.color color;
456 begintiles ();
457 let f col row x y tilex tiley w h =
458 match gettileopaque l col row with
459 | Some (opaque, _, t) ->
460 let params = x, y, w, h, tilex, tiley in
461 if conf.invert
462 then GlTex.env (`mode `blend);
463 drawtile params opaque;
464 if conf.invert
465 then GlTex.env (`mode `modulate);
466 if conf.debug
467 then (
468 endtiles ();
469 let s = Printf.sprintf
470 "%d[%d,%d] %f sec"
471 l.pageno col row t
473 let w = measurestr fstate.fontsize s in
474 GlDraw.color (0.0, 0.0, 0.0);
475 filledrect (float (x-2))
476 (float (y-2))
477 (float (x+2) +. w)
478 (float (y + fstate.fontsize + 2));
479 GlDraw.color color;
480 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
481 begintiles ();
484 | None ->
485 endtiles ();
486 let w =
487 let lw = state.winw - x in
488 min lw w
489 and h =
490 let lh = state.winh - y in
491 min lh h
493 if conf.invert
494 then GlTex.env (`mode `blend);
495 begin match state.checkerstexid with
496 | Some id ->
497 Gl.enable `texture_2d;
498 GlTex.bind_texture ~target:`texture_2d id;
499 let x0 = float x
500 and y0 = float y
501 and x1 = float (x+w)
502 and y1 = float (y+h) in
504 let tw = float w /. 16.0
505 and th = float h /. 16.0 in
506 let tx0 = float tilex /. 16.0
507 and ty0 = float tiley /. 16.0 in
508 let tx1 = tx0 +. tw
509 and ty1 = ty0 +. th in
510 Raw.sets_float Glutils.vraw ~pos:0
511 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
512 Raw.sets_float Glutils.traw ~pos:0
513 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
514 GlArray.vertex `two Glutils.vraw;
515 GlArray.tex_coord `two Glutils.traw;
516 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
517 Gl.disable `texture_2d;
519 | None ->
520 GlDraw.color (1.0, 1.0, 1.0);
521 filledrect (float x) (float y) (float (x+w)) (float (y+h));
522 end;
523 if conf.invert
524 then GlTex.env (`mode `modulate);
525 if w > 128 && h > fstate.fontsize + 10
526 then (
527 let c = if conf.invert then 1.0 else 0.0 in
528 GlDraw.color (c, c, c);
529 let c, r =
530 if conf.verbose
531 then (col*conf.tilew, row*conf.tileh)
532 else col, row
534 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
536 GlDraw.color color;
537 begintiles ();
539 itertiles l f;
540 endtiles ();
543 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
545 let tilevisible1 l x y =
546 let ax0 = l.pagex
547 and ax1 = l.pagex + l.pagevw
548 and ay0 = l.pagey
549 and ay1 = l.pagey + l.pagevh in
551 let bx0 = x
552 and by0 = y in
553 let bx1 = min (bx0 + conf.tilew) l.pagew
554 and by1 = min (by0 + conf.tileh) l.pageh in
556 let rx0 = max ax0 bx0
557 and ry0 = max ay0 by0
558 and rx1 = min ax1 bx1
559 and ry1 = min ay1 by1 in
561 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
562 nonemptyintersection
565 let tilevisible layout n x y =
566 let rec findpageinlayout m = function
567 | l :: rest when l.pageno = n ->
568 tilevisible1 l x y || (
569 match conf.columns with
570 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
571 | Csplit _ | Csingle _ | Cmulti _ -> false
573 | _ :: rest -> findpageinlayout 0 rest
574 | [] -> false
576 findpageinlayout 0 layout;
579 let tileready l x y =
580 tilevisible1 l x y &&
581 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
584 let tilepage n p layout =
585 let rec loop = function
586 | l :: rest ->
587 if l.pageno = n
588 then
589 let f col row _ _ _ _ _ _ =
590 if state.currently = Idle
591 then
592 match gettileopaque l col row with
593 | Some _ -> ()
594 | None ->
595 let x = col*conf.tilew
596 and y = row*conf.tileh in
597 let w =
598 let w = l.pagew - x in
599 min w conf.tilew
601 let h =
602 let h = l.pageh - y in
603 min h conf.tileh
605 let pbo =
606 if conf.usepbo
607 then getpbo w h conf.colorspace
608 else ~< "0"
610 wcmd "tile %s %d %d %d %d %s"
611 (~> p) x y w h (~> pbo);
612 state.currently <-
613 Tiling (
614 l, p, conf.colorspace, conf.angle,
615 state.gen, col, row, conf.tilew, conf.tileh
618 itertiles l f;
619 else
620 loop rest
622 | [] -> ()
624 if nogeomcmds state.geomcmds
625 then loop layout;
628 let preloadlayout x y sw sh =
629 let y = if y < sh then 0 else y - sh in
630 let x = min 0 (x + sw) in
631 let h = sh*3 in
632 let w = sw*3 in
633 layout x y w h;
636 let load pages =
637 let rec loop pages =
638 if state.currently != Idle
639 then ()
640 else
641 match pages with
642 | l :: rest ->
643 begin match getopaque l.pageno with
644 | None ->
645 wcmd "page %d %d" l.pageno l.pagedimno;
646 state.currently <- Loading (l, state.gen);
647 | Some opaque ->
648 tilepage l.pageno opaque pages;
649 loop rest
650 end;
651 | _ -> ()
653 if nogeomcmds state.geomcmds
654 then loop pages
657 let preload pages =
658 load pages;
659 if conf.preload && state.currently = Idle
660 then load (preloadlayout state.x state.y state.winw state.winh);
663 let layoutready layout =
664 let rec fold all ls =
665 all && match ls with
666 | l :: rest ->
667 let seen = ref false in
668 let allvisible = ref true in
669 let foo col row _ _ _ _ _ _ =
670 seen := true;
671 allvisible := !allvisible &&
672 begin match gettileopaque l col row with
673 | Some _ -> true
674 | None -> false
677 itertiles l foo;
678 fold (!seen && !allvisible) rest
679 | [] -> true
681 let alltilesvisible = fold true layout in
682 alltilesvisible;
685 let gotoxy x y =
686 let y = bound y 0 state.maxy in
687 let y, layout =
688 let layout = layout x y state.winw state.winh in
689 postRedisplay "gotoxy ready";
690 y, layout
692 state.x <- x;
693 state.y <- y;
694 state.layout <- layout;
695 begin match state.mode with
696 | LinkNav ln ->
697 begin match ln with
698 | Ltexact (pageno, linkno) ->
699 let rec loop = function
700 | [] ->
701 state.lnava <- Some (pageno, linkno);
702 state.mode <- LinkNav (Ltgendir 0)
703 | l :: _ when l.pageno = pageno ->
704 begin match getopaque pageno with
705 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
706 | Some opaque ->
707 let x0, y0, x1, y1 = getlinkrect opaque linkno in
708 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
709 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
710 then state.mode <- LinkNav (Ltgendir 0)
712 | _ :: rest -> loop rest
714 loop layout
715 | Ltnotready _ | Ltgendir _ -> ()
717 | Birdseye _ | Textentry _ | View -> ()
718 end;
719 begin match state.mode with
720 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
721 if not (pagevisible layout pageno)
722 then (
723 match state.layout with
724 | [] -> ()
725 | l :: _ ->
726 state.mode <- Birdseye (
727 conf, leftx, l.pageno, hooverpageno, anchor
730 | LinkNav lt ->
731 begin match lt with
732 | Ltnotready (_, dir)
733 | Ltgendir dir ->
734 let linknav =
735 let rec loop = function
736 | [] -> lt
737 | l :: rest ->
738 match getopaque l.pageno with
739 | None -> Ltnotready (l.pageno, dir)
740 | Some opaque ->
741 let link =
742 let ld =
743 if dir = 0
744 then LDfirstvisible (l.pagex, l.pagey, dir)
745 else (
746 if dir > 0 then LDfirst else LDlast
749 findlink opaque ld
751 match link with
752 | Lnotfound -> loop rest
753 | Lfound n ->
754 showlinktype (getlink opaque n);
755 Ltexact (l.pageno, n)
757 loop state.layout
759 state.mode <- LinkNav linknav
760 | Ltexact _ -> ()
762 | Textentry _ | View -> ()
763 end;
764 preload layout;
765 if conf.updatecurs
766 then (
767 let mx, my = state.mpos in
768 updateunder mx my;
772 let conttiling pageno opaque =
773 tilepage pageno opaque
774 (if conf.preload
775 then preloadlayout state.x state.y state.winw state.winh
776 else state.layout)
779 let gotoxy x y =
780 if not conf.verbose then state.text <- E.s;
781 gotoxy x y;
784 let getanchory (n, top, dtop) =
785 let y, h = getpageyh n in
786 if conf.presentation
787 then
788 let ips = calcips h in
789 y + truncate (top*.float h -. dtop*.float ips) + ips;
790 else
791 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
794 let gotoanchor anchor =
795 gotoxy state.x (getanchory anchor);
798 let addnav () =
799 getanchor () |> cbput state.hists.nav;
802 let addnavnorc () =
803 getanchor () |> cbput_dont_update_rc state.hists.nav;
806 let getnav dir =
807 let anchor = cbgetc state.hists.nav dir in
808 getanchory anchor;
811 let gotopage n top =
812 let y, h = getpageyh n in
813 let y = y + (truncate (top *. float h)) in
814 gotoxy state.x y
817 let gotopage1 n top =
818 let y = getpagey n in
819 let y = y + top in
820 gotoxy state.x y
823 let invalidate s f =
824 Glutils.redisplay := false;
825 state.layout <- [];
826 state.pdims <- [];
827 state.rects <- [];
828 state.rects1 <- [];
829 match state.geomcmds with
830 | ps, [] when emptystr ps ->
831 f ();
832 state.geomcmds <- s, [];
834 | ps, [] ->
835 state.geomcmds <- ps, [s, f];
837 | ps, (s', _) :: rest when s' = s ->
838 state.geomcmds <- ps, ((s, f) :: rest);
840 | ps, cmds ->
841 state.geomcmds <- ps, ((s, f) :: cmds);
844 let flushpages () =
845 Hashtbl.iter (fun _ opaque ->
846 wcmd "freepage %s" (~> opaque);
847 ) state.pagemap;
848 Hashtbl.clear state.pagemap;
851 let flushtiles () =
852 if not (Queue.is_empty state.tilelru)
853 then (
854 Queue.iter (fun (k, p, s) ->
855 wcmd "freetile %s" (~> p);
856 state.memused <- state.memused - s;
857 Hashtbl.remove state.tilemap k;
858 ) state.tilelru;
859 state.uioh#infochanged Memused;
860 Queue.clear state.tilelru;
862 load state.layout;
865 let stateh h =
866 let h = truncate (float h*.conf.zoom) in
867 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
868 h - d
871 let fillhelp () =
872 state.help <-
873 let sl = keystostrlist conf in
874 let rec loop accu =
875 function | [] -> accu
876 | s :: rest -> loop ((s, 0, Noaction) :: accu) rest
877 in Help.makehelp conf.urilauncher
878 @ (("", 0, Noaction) :: loop [] sl) |> Array.of_list
881 let opendoc path password =
882 state.path <- path;
883 state.password <- password;
884 state.gen <- state.gen + 1;
885 state.docinfo <- [];
886 state.outlines <- [||];
888 flushpages ();
889 setaalevel conf.aalevel;
890 let titlepath =
891 if emptystr state.origin
892 then path
893 else state.origin
895 Wsi.settitle ("llpp " ^ mbtoutf8 (Filename.basename titlepath));
896 wcmd "open %d %d %s\000%s\000%s\000"
897 (btod conf.usedoccss) !layouth
898 path password conf.css;
899 invalidate "reqlayout"
900 (fun () ->
901 wcmd "reqlayout %d %d %d %s\000"
902 conf.angle (FMTE.to_int conf.fitmodel)
903 (stateh state.winh) state.nameddest
905 fillhelp ();
908 let reload () =
909 state.anchor <- getanchor ();
910 opendoc state.path state.password;
913 let scalecolor c =
914 let c = c *. conf.colorscale in
915 (c, c, c);
918 let scalecolor2 (r, g, b) =
919 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
922 let docolumns columns =
923 match columns with
924 | Csingle _ ->
925 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
926 let rec loop pageno pdimno pdim y ph pdims =
927 if pageno = state.pagecount
928 then ()
929 else
930 let pdimno, ((_, w, h, xoff) as pdim), pdims =
931 match pdims with
932 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
933 pdimno+1, pdim, rest
934 | _ ->
935 pdimno, pdim, pdims
937 let x = max 0 (((state.winw - w) / 2) - xoff) in
938 let y =
939 y + (if conf.presentation
940 then (if pageno = 0 then calcips h else calcips ph + calcips h)
941 else (if pageno = 0 then 0 else conf.interpagespace)
944 a.(pageno) <- (pdimno, x, y, pdim);
945 loop (pageno+1) pdimno pdim (y + h) h pdims
947 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
948 conf.columns <- Csingle a;
950 | Cmulti ((columns, coverA, coverB), _) ->
951 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
952 let rec loop pageno pdimno pdim x y rowh pdims =
953 let rec fixrow m =
954 if m = pageno then () else
955 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
956 if h < rowh
957 then (
958 let y = y + (rowh - h) / 2 in
959 a.(m) <- (pdimno, x, y, pdim);
961 fixrow (m+1)
963 if pageno = state.pagecount
964 then fixrow (((pageno - 1) / columns) * columns)
965 else
966 let pdimno, ((_, w, h, xoff) as pdim), pdims =
967 match pdims with
968 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
969 pdimno+1, pdim, rest
970 | _ ->
971 pdimno, pdim, pdims
973 let x, y, rowh' =
974 if pageno = coverA - 1 || pageno = state.pagecount - coverB
975 then (
976 let x = (state.winw - w) / 2 in
977 let ips =
978 if conf.presentation then calcips h else conf.interpagespace in
979 x, y + ips + rowh, h
981 else (
982 if (pageno - coverA) mod columns = 0
983 then (
984 let x = max 0 (state.winw - state.w) / 2 in
985 let y =
986 if conf.presentation
987 then
988 let ips = calcips h in
989 y + (if pageno = 0 then 0 else calcips rowh + ips)
990 else
991 y + (if pageno = 0 then 0 else conf.interpagespace)
993 x, y + rowh, h
995 else x, y, max rowh h
998 let y =
999 if pageno > 1 && (pageno - coverA) mod columns = 0
1000 then (
1001 let y =
1002 if pageno = columns && conf.presentation
1003 then (
1004 let ips = calcips rowh in
1005 for i = 0 to pred columns
1007 let (pdimno, x, y, pdim) = a.(i) in
1008 a.(i) <- (pdimno, x, y+ips, pdim)
1009 done;
1010 y+ips;
1012 else y
1014 fixrow (pageno - columns);
1017 else y
1019 a.(pageno) <- (pdimno, x, y, pdim);
1020 let x = x + w + xoff*2 + conf.interpagespace in
1021 loop (pageno+1) pdimno pdim x y rowh' pdims
1023 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1024 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1026 | Csplit (c, _) ->
1027 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1028 let rec loop pageno pdimno pdim y pdims =
1029 if pageno = state.pagecount
1030 then ()
1031 else
1032 let pdimno, ((_, w, h, _) as pdim), pdims =
1033 match pdims with
1034 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1035 pdimno+1, pdim, rest
1036 | _ ->
1037 pdimno, pdim, pdims
1039 let cw = w / c in
1040 let rec loop1 n x y =
1041 if n = c then y else (
1042 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1043 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1046 let y = loop1 0 0 y in
1047 loop (pageno+1) pdimno pdim y pdims
1049 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1050 conf.columns <- Csplit (c, a);
1053 let represent () =
1054 docolumns conf.columns;
1055 state.maxy <- calcheight ();
1056 if state.reprf == noreprf
1057 then (
1058 match state.mode with
1059 | Birdseye (_, _, pageno, _, _) ->
1060 let y, h = getpageyh pageno in
1061 let top = (state.winh - h) / 2 in
1062 gotoxy state.x (max 0 (y - top))
1063 | Textentry _ | View | LinkNav _ ->
1064 let y = getanchory state.anchor in
1065 let y = min y (state.maxy - state.winh) in
1066 gotoxy state.x y;
1068 else (
1069 state.reprf ();
1070 state.reprf <- noreprf;
1074 let reshape ?(firsttime=false) w h =
1075 GlDraw.viewport ~x:0 ~y:0 ~w ~h;
1076 if not firsttime && nogeomcmds state.geomcmds
1077 then state.anchor <- getanchor ();
1079 state.winw <- w;
1080 let w = truncate (float w *. conf.zoom) in
1081 let w = max w 2 in
1082 state.winh <- h;
1083 setfontsize fstate.fontsize;
1084 GlMat.mode `modelview;
1085 GlMat.load_identity ();
1087 GlMat.mode `projection;
1088 GlMat.load_identity ();
1089 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1090 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1091 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1093 let relx =
1094 if conf.zoom <= 1.0
1095 then 0.0
1096 else float state.x /. float state.w
1098 invalidate "geometry"
1099 (fun () ->
1100 state.w <- w;
1101 if not firsttime
1102 then state.x <- truncate (relx *. float w);
1103 let w =
1104 match conf.columns with
1105 | Csingle _ -> w
1106 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1107 | Csplit (c, _) -> w * c
1109 wcmd "geometry %d %d %d"
1110 w (stateh h) (FMTE.to_int conf.fitmodel)
1114 let gctiles () =
1115 let len = Queue.length state.tilelru in
1116 let layout = lazy (if conf.preload
1117 then preloadlayout state.x state.y state.winw state.winh
1118 else state.layout) in
1119 let rec loop qpos =
1120 if state.memused > conf.memlimit
1121 then (
1122 if qpos < len
1123 then
1124 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1125 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1126 let (_, pw, ph, _) = getpagedim n in
1127 if gen = state.gen
1128 && colorspace = conf.colorspace
1129 && angle = conf.angle
1130 && pagew = pw
1131 && pageh = ph
1132 && (
1133 let x = col*conf.tilew
1134 and y = row*conf.tileh in
1135 tilevisible (Lazy.force_val layout) n x y
1137 then Queue.push lruitem state.tilelru
1138 else (
1139 freepbo p;
1140 wcmd "freetile %s" (~> p);
1141 state.memused <- state.memused - s;
1142 state.uioh#infochanged Memused;
1143 Hashtbl.remove state.tilemap k;
1145 loop (qpos+1)
1148 loop 0
1151 let onpagerect pageno f =
1152 let b =
1153 match conf.columns with
1154 | Cmulti (_, b) -> b
1155 | Csingle b -> b
1156 | Csplit (_, b) -> b
1158 if pageno >= 0 && pageno < Array.length b
1159 then
1160 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1161 f w h
1164 let gotopagexy1 pageno x y =
1165 let _,w1,h1,leftx = getpagedim pageno in
1166 let top = y /. (float h1) in
1167 let left = x /. (float w1) in
1168 let py, w, h = getpageywh pageno in
1169 let wh = state.winh in
1170 let x = left *. (float w) in
1171 let x = leftx + state.x + truncate x in
1172 let sx =
1173 if x < 0 || x >= state.winw
1174 then state.x - x
1175 else state.x
1177 let pdy = truncate (top *. float h) in
1178 let y' = py + pdy in
1179 let dy = y' - state.y in
1180 let sy =
1181 if x != state.x || not (dy > 0 && dy < wh)
1182 then (
1183 if conf.presentation
1184 then
1185 if abs (py - y') > wh
1186 then y'
1187 else py
1188 else y';
1190 else state.y
1192 if state.x != sx || state.y != sy
1193 then gotoxy sx sy
1194 else gotoxy state.x state.y;
1197 let gotopagexy pageno x y =
1198 match state.mode with
1199 | Birdseye _ -> gotopage pageno 0.0
1200 | Textentry _ | View | LinkNav _ -> gotopagexy1 pageno x y
1203 let getpassword () =
1204 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1205 if emptystr passcmd
1206 then E.s
1207 else getcmdoutput
1208 (fun s ->
1209 impmsg "error getting password: %s" s;
1210 dolog "%s" s) passcmd;
1213 let pgoto opaque pageno x y =
1214 let pdimno = getpdimno pageno in
1215 let x, y = project opaque pageno pdimno x y in
1216 gotopagexy pageno x y;
1219 let act cmds =
1220 (* dolog "%S" cmds; *)
1221 let spl = splitatchar cmds ' ' in
1222 let scan s fmt f =
1223 try Scanf.sscanf s fmt f
1224 with exn ->
1225 dolog "error processing '%S': %s" cmds @@ exntos exn;
1226 exit 1
1228 let addoutline outline =
1229 match state.currently with
1230 | Outlining outlines -> state.currently <- Outlining (outline :: outlines)
1231 | Idle -> state.currently <- Outlining [outline]
1232 | Loading _ | Tiling _ ->
1233 dolog "invalid outlining state";
1234 logcurrently state.currently
1236 match spl with
1237 | "clear", "" ->
1238 state.pdims <- [];
1239 state.uioh#infochanged Pdim;
1241 | "clearrects", "" ->
1242 state.rects <- state.rects1;
1243 postRedisplay "clearrects";
1245 | "continue", args ->
1246 let n = scan args "%u" (fun n -> n) in
1247 state.pagecount <- n;
1248 begin match state.currently with
1249 | Outlining l ->
1250 state.currently <- Idle;
1251 state.outlines <- Array.of_list (List.rev l)
1252 | Idle | Loading _ | Tiling _ -> ()
1253 end;
1255 let cur, cmds = state.geomcmds in
1256 if emptystr cur
1257 then failwith "umpossible";
1259 begin match List.rev cmds with
1260 | [] ->
1261 state.geomcmds <- E.s, [];
1262 represent ();
1263 | (s, f) :: rest ->
1264 f ();
1265 state.geomcmds <- s, List.rev rest;
1266 end;
1267 postRedisplay "continue";
1269 | "msg", args ->
1270 showtext ' ' args
1272 | "vmsg", args ->
1273 if conf.verbose
1274 then showtext ' ' args
1276 | "emsg", args ->
1277 Buffer.add_string state.errmsgs args;
1278 state.newerrmsgs <- true;
1279 postRedisplay "error message"
1281 | "progress", args ->
1282 let progress, text =
1283 scan args "%f %n"
1284 (fun f pos ->
1285 f, String.sub args pos (String.length args - pos))
1287 state.text <- text;
1288 state.progress <- progress;
1289 postRedisplay "progress"
1291 | "firstmatch", args ->
1292 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1293 scan args "%u %d %f %f %f %f %f %f %f %f"
1294 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1295 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1297 let y = (getpagey pageno) + truncate y0 in
1298 let x =
1299 if (state.x < - truncate x0) || (state.x > state.winw - truncate x1)
1300 then state.winw/2 - truncate (x0 /. 2. +. x1 /. 2.)
1301 else state.x
1303 addnav ();
1304 gotoxy x y;
1305 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1306 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1308 | "match", args ->
1309 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1310 scan args "%u %d %f %f %f %f %f %f %f %f"
1311 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1312 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1314 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1315 state.rects1 <-
1316 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1318 | "page", args ->
1319 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1320 let pageopaque = ~< pageopaques in
1321 begin match state.currently with
1322 | Loading (l, gen) ->
1323 vlog "page %d took %f sec" l.pageno t;
1324 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1325 let preloadedpages =
1326 if conf.preload
1327 then preloadlayout state.x state.y state.winw state.winh
1328 else state.layout
1330 let evict () =
1331 let set = List.fold_left (fun s l -> IntSet.add l.pageno s)
1332 IntSet.empty preloadedpages
1334 let evictedpages =
1335 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1336 if not (IntSet.mem pageno set)
1337 then (
1338 wcmd "freepage %s" (~> opaque);
1339 key :: accu
1341 else accu
1342 ) state.pagemap []
1344 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1346 evict ();
1347 state.currently <- Idle;
1348 if gen = state.gen
1349 then (
1350 tilepage l.pageno pageopaque state.layout;
1351 load state.layout;
1352 load preloadedpages;
1353 let visible = pagevisible state.layout l.pageno in
1354 if visible
1355 then (
1356 match state.mode with
1357 | LinkNav (Ltnotready (pageno, dir)) ->
1358 if pageno = l.pageno
1359 then (
1360 let link =
1361 let ld =
1362 if dir = 0
1363 then LDfirstvisible (l.pagex, l.pagey, dir)
1364 else (
1365 if dir > 0 then LDfirst else LDlast
1368 findlink pageopaque ld
1370 match link with
1371 | Lnotfound -> ()
1372 | Lfound n ->
1373 showlinktype (getlink pageopaque n);
1374 state.mode <- LinkNav (Ltexact (l.pageno, n))
1376 | LinkNav (Ltgendir _)
1377 | LinkNav (Ltexact _)
1378 | View
1379 | Birdseye _
1380 | Textentry _ -> ()
1383 if visible && layoutready state.layout
1384 then (
1385 postRedisplay "page";
1389 | Idle | Tiling _ | Outlining _ ->
1390 dolog "Inconsistent loading state";
1391 logcurrently state.currently;
1392 exit 1
1395 | "tile" , args ->
1396 let (x, y, opaques, size, t) =
1397 scan args "%u %u %s %u %f"
1398 (fun x y p size t -> (x, y, p, size, t))
1400 let opaque = ~< opaques in
1401 begin match state.currently with
1402 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1403 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1405 unmappbo opaque;
1406 if tilew != conf.tilew || tileh != conf.tileh
1407 then (
1408 wcmd "freetile %s" (~> opaque);
1409 state.currently <- Idle;
1410 load state.layout;
1412 else (
1413 puttileopaque l col row gen cs angle opaque size t;
1414 state.memused <- state.memused + size;
1415 state.uioh#infochanged Memused;
1416 gctiles ();
1417 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1418 opaque, size) state.tilelru;
1420 state.currently <- Idle;
1421 if gen = state.gen
1422 && conf.colorspace = cs
1423 && conf.angle = angle
1424 && tilevisible state.layout l.pageno x y
1425 then conttiling l.pageno pageopaque;
1427 preload state.layout;
1428 if gen = state.gen
1429 && conf.colorspace = cs
1430 && conf.angle = angle
1431 && tilevisible state.layout l.pageno x y
1432 && layoutready state.layout
1433 then postRedisplay "tile nothrottle";
1436 | Idle | Loading _ | Outlining _ ->
1437 dolog "Inconsistent tiling state";
1438 logcurrently state.currently;
1439 exit 1
1442 | "pdim", args ->
1443 let (n, w, h, _) as pdim =
1444 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1446 let pdim =
1447 match conf.fitmodel with
1448 | FitWidth -> pdim
1449 | FitPage | FitProportional ->
1450 match conf.columns with
1451 | Csplit _ -> (n, w, h, 0)
1452 | Csingle _ | Cmulti _ -> pdim
1454 state.pdims <- pdim :: state.pdims;
1455 state.uioh#infochanged Pdim
1457 | "o", args ->
1458 let (l, n, t, h, pos) =
1459 scan args "%u %u %d %u %n"
1460 (fun l n t h pos -> l, n, t, h, pos)
1462 let s = String.sub args pos (String.length args - pos) in
1463 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1465 | "ou", args ->
1466 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1467 let s = String.sub args pos len in
1468 let pos2 = pos + len + 1 in
1469 let uri = String.sub args pos2 (String.length args - pos2) in
1470 addoutline (s, l, Ouri uri)
1472 | "on", args ->
1473 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1474 let s = String.sub args pos (String.length args - pos) in
1475 addoutline (s, l, Onone)
1477 | "a", args ->
1478 let (n, l, t) =
1479 scan args "%u %d %d" (fun n l t -> n, l, t)
1481 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
1483 | "info", args ->
1484 let c, v = splitatchar args '\t' in
1485 let s =
1486 if nonemptystr v
1487 then
1488 if c = "Title"
1489 then (
1490 conf.title <- v;
1491 if not !ignoredoctitlte
1492 then Wsi.settitle v;
1493 args
1495 else
1496 if let len = String.length c in
1497 len > 6 && ((String.sub c (len-4) 4) = "date")
1498 then (
1499 if String.length v >= 7 && v.[0] = 'D' && v.[1] = ':'
1500 then
1501 let b = Buffer.create 10 in
1502 Printf.bprintf b "%s\t" c;
1503 let sub p l c =
1505 Buffer.add_substring b v p l;
1506 Buffer.add_char b c;
1507 with exn -> Buffer.add_string b @@ exntos exn
1509 sub 2 4 '/';
1510 sub 6 2 '/';
1511 sub 8 2 ' ';
1512 sub 10 2 ':';
1513 sub 12 2 ':';
1514 sub 14 2 ' ';
1515 Buffer.add_char b '[';
1516 Buffer.add_string b v;
1517 Buffer.add_char b ']';
1518 Buffer.contents b
1519 else args
1521 else args
1522 else args
1524 state.docinfo <- (1, s) :: state.docinfo
1526 | "infoend", "" ->
1527 state.docinfo <- List.rev state.docinfo;
1528 state.uioh#infochanged Docinfo
1530 | "pass", args ->
1531 if args = "fail"
1532 then Wsi.settitle "Wrong password";
1533 let password = getpassword () in
1534 if emptystr password
1535 then error "document is password protected"
1536 else opendoc state.path password
1538 | _ ->
1539 error "unknown cmd `%S'" cmds
1542 let onhist cb =
1543 let rc = cb.rc in
1544 let action = function
1545 | HCprev -> cbget cb ~-1
1546 | HCnext -> cbget cb 1
1547 | HCfirst -> cbget cb ~-(cb.rc)
1548 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1549 and cancel () = cb.rc <- rc
1550 in (action, cancel)
1553 let search pattern forward =
1554 match conf.columns with
1555 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1556 | Csingle _ | Cmulti _ ->
1557 if nonemptystr pattern
1558 then
1559 let pn, py =
1560 match state.layout with
1561 | [] -> 0, 0
1562 | l :: _ ->
1563 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1565 wcmd "search %d %d %d %d,%s\000"
1566 (btod conf.icase) pn py (btod forward) pattern;
1569 let intentry text key =
1570 let text =
1571 if emptystr text && key = Keys.Ascii '-'
1572 then addchar text '-'
1573 else
1574 match [@warning "-4"] key with
1575 | Keys.Ascii ('0'..'9' as c) -> addchar text c
1576 | _ ->
1577 state.text <- "invalid key";
1578 text
1580 TEcont text
1583 let linknact f s =
1584 if nonemptystr s
1585 then (
1586 let n =
1587 let l = String.length s in
1588 let rec loop pos n =
1589 if pos = l
1590 then n
1591 else
1592 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1593 loop (pos+1) (n*26 + m)
1594 in loop 0 0
1596 let rec loop n = function
1597 | [] -> ()
1598 | l :: rest ->
1599 match getopaque l.pageno with
1600 | None -> loop n rest
1601 | Some opaque ->
1602 let m = getlinkcount opaque in
1603 if n < m
1604 then (
1605 let under = getlink opaque n in
1606 f under
1608 else loop (n-m) rest
1610 loop n state.layout;
1614 let linknentry text =
1615 function [@warning "-4"]
1616 | Keys.Ascii c ->
1617 let text = addchar text c in
1618 linknact (fun under -> state.text <- undertext under) text;
1619 TEcont text
1620 | _ ->
1621 state.text <- Printf.sprintf "invalid key";
1622 TEcont text
1625 let textentry text = function [@warning "-4"]
1626 | Keys.Ascii c -> TEcont (addchar text c)
1627 | Keys.Code c -> TEcont (text ^ toutf8 c)
1628 | _ -> TEcont text
1631 let reqlayout angle fitmodel =
1632 if nogeomcmds state.geomcmds
1633 then state.anchor <- getanchor ();
1634 conf.angle <- angle mod 360;
1635 if conf.angle != 0
1636 then (
1637 match state.mode with
1638 | LinkNav _ -> state.mode <- View
1639 | Birdseye _ | Textentry _ | View -> ()
1641 conf.fitmodel <- fitmodel;
1642 invalidate
1643 "reqlayout"
1644 (fun () ->
1645 wcmd "reqlayout %d %d %d"
1646 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
1650 let settrim trimmargins trimfuzz =
1651 if nogeomcmds state.geomcmds
1652 then state.anchor <- getanchor ();
1653 conf.trimmargins <- trimmargins;
1654 conf.trimfuzz <- trimfuzz;
1655 let x0, y0, x1, y1 = trimfuzz in
1656 invalidate
1657 "settrim" (fun () ->
1658 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
1659 flushpages ();
1662 let setzoom zoom =
1663 let zoom = max 0.0001 zoom in
1664 if zoom <> conf.zoom
1665 then (
1666 state.prevzoom <- (conf.zoom, state.x);
1667 conf.zoom <- zoom;
1668 reshape state.winw state.winh;
1669 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
1673 let pivotzoom ?(vw=min state.w state.winw)
1674 ?(vh=min (state.maxy-state.y) state.winh)
1675 ?(x=vw/2) ?(y=vh/2) zoom =
1676 let w = float state.w /. zoom in
1677 let hw = w /. 2.0 in
1678 let ratio = float vh /. float vw in
1679 let hh = hw *. ratio in
1680 let x0 = float x -. hw
1681 and y0 = float y -. hh in
1682 gotoxy (state.x - truncate x0) (state.y + truncate y0);
1683 setzoom zoom;
1686 let pivotzoom ?vw ?vh ?x ?y zoom =
1687 if nogeomcmds state.geomcmds
1688 then
1689 if zoom > 1.0
1690 then pivotzoom ?vw ?vh ?x ?y zoom
1691 else setzoom zoom
1694 let setcolumns mode columns coverA coverB =
1695 state.prevcolumns <- Some (conf.columns, conf.zoom);
1696 if columns < 0
1697 then (
1698 if isbirdseye mode
1699 then impmsg "split mode doesn't work in bird's eye"
1700 else (
1701 conf.columns <- Csplit (-columns, E.a);
1702 state.x <- 0;
1703 conf.zoom <- 1.0;
1706 else (
1707 if columns < 2
1708 then (
1709 conf.columns <- Csingle E.a;
1710 state.x <- 0;
1711 setzoom 1.0;
1713 else (
1714 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
1715 conf.zoom <- 1.0;
1718 reshape state.winw state.winh;
1721 let resetmstate () =
1722 state.mstate <- Mnone;
1723 Wsi.setcursor Wsi.CURSOR_INHERIT;
1726 let enterbirdseye () =
1727 let zoom = float conf.thumbw /. float state.winw in
1728 let birdseyepageno =
1729 let cy = state.winh / 2 in
1730 let fold = function
1731 | [] -> 0
1732 | l :: rest ->
1733 let rec fold best = function
1734 | [] -> best.pageno
1735 | l :: rest ->
1736 let d = cy - (l.pagedispy + l.pagevh/2)
1737 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1738 if abs d < abs dbest
1739 then fold l rest
1740 else best.pageno
1741 in fold l rest
1743 fold state.layout
1745 state.mode <-
1746 Birdseye (
1747 { conf with zoom = conf.zoom },
1748 state.x, birdseyepageno, -1, getanchor ()
1750 resetmstate ();
1751 conf.zoom <- zoom;
1752 conf.presentation <- false;
1753 conf.interpagespace <- 10;
1754 conf.hlinks <- false;
1755 conf.fitmodel <- FitPage;
1756 state.x <- 0;
1757 conf.columns <- (
1758 match conf.beyecolumns with
1759 | Some c ->
1760 conf.zoom <- 1.0;
1761 Cmulti ((c, 0, 0), E.a)
1762 | None -> Csingle E.a
1764 if conf.verbose
1765 then
1766 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1767 (100.0*.zoom)
1768 else
1769 state.text <- E.s
1771 reshape state.winw state.winh;
1774 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1775 state.mode <- View;
1776 conf.zoom <- c.zoom;
1777 conf.presentation <- c.presentation;
1778 conf.interpagespace <- c.interpagespace;
1779 conf.hlinks <- c.hlinks;
1780 conf.fitmodel <- c.fitmodel;
1781 conf.beyecolumns <- (
1782 match conf.columns with
1783 | Cmulti ((c, _, _), _) -> Some c
1784 | Csingle _ -> None
1785 | Csplit _ -> failwith "leaving bird's eye split mode"
1787 conf.columns <- (
1788 match c.columns with
1789 | Cmulti (c, _) -> Cmulti (c, E.a)
1790 | Csingle _ -> Csingle E.a
1791 | Csplit (c, _) -> Csplit (c, E.a)
1793 if conf.verbose
1794 then
1795 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1796 (100.0*.conf.zoom)
1798 reshape state.winw state.winh;
1799 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
1800 state.x <- leftx;
1803 let togglebirdseye () =
1804 match state.mode with
1805 | Birdseye vals -> leavebirdseye vals true
1806 | View -> enterbirdseye ()
1807 | Textentry _ | LinkNav _ -> ()
1810 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
1811 let pageno = max 0 (pageno - incr) in
1812 let rec loop = function
1813 | [] -> gotopage1 pageno 0
1814 | l :: _ when l.pageno = pageno ->
1815 if l.pagedispy >= 0 && l.pagey = 0
1816 then postRedisplay "upbirdseye"
1817 else gotopage1 pageno 0
1818 | _ :: rest -> loop rest
1820 loop state.layout;
1821 state.text <- E.s;
1822 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1825 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
1826 let pageno = min (state.pagecount - 1) (pageno + incr) in
1827 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1828 let rec loop = function
1829 | [] ->
1830 let y, h = getpageyh pageno in
1831 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
1832 gotoxy state.x (clamp dy)
1833 | l :: _ when l.pageno = pageno ->
1834 if l.pagevh != l.pageh
1835 then gotoxy state.x (clamp (l.pageh - l.pagevh + conf.interpagespace))
1836 else postRedisplay "downbirdseye"
1837 | _ :: rest -> loop rest
1839 loop state.layout;
1840 state.text <- E.s;
1843 let optentry mode _ key =
1844 let btos b = if b then "on" else "off" in
1845 match [@warning "-4"] key with
1846 | Keys.Ascii 'C' ->
1847 let ondone s =
1849 let n, a, b = multicolumns_of_string s in
1850 setcolumns mode n a b;
1851 with exn ->
1852 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exn
1854 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
1856 | Keys.Ascii 'Z' ->
1857 let ondone s =
1859 let zoom = float (int_of_string s) /. 100.0 in
1860 pivotzoom zoom
1861 with exn ->
1862 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
1864 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
1866 | Keys.Ascii 'i' ->
1867 conf.icase <- not conf.icase;
1868 TEdone ("case insensitive search " ^ (btos conf.icase))
1870 | Keys.Ascii 'v' ->
1871 conf.verbose <- not conf.verbose;
1872 TEdone ("verbose " ^ (btos conf.verbose))
1874 | Keys.Ascii 'd' ->
1875 conf.debug <- not conf.debug;
1876 TEdone ("debug " ^ (btos conf.debug))
1878 | Keys.Ascii 'f' ->
1879 conf.underinfo <- not conf.underinfo;
1880 TEdone ("underinfo " ^ btos conf.underinfo)
1882 | Keys.Ascii 'l' ->
1883 let fm =
1884 match conf.fitmodel with
1885 | FitProportional -> FitWidth
1886 | FitWidth | FitPage -> FitProportional
1888 reqlayout conf.angle fm;
1889 TEdone ("proportional display " ^ btos (fm == FitProportional))
1891 | Keys.Ascii 'T' ->
1892 settrim (not conf.trimmargins) conf.trimfuzz;
1893 TEdone ("trim margins " ^ btos conf.trimmargins)
1895 | Keys.Ascii 'I' ->
1896 conf.invert <- not conf.invert;
1897 TEdone ("invert colors " ^ btos conf.invert)
1899 | Keys.Ascii 'x' ->
1900 let ondone s =
1901 cbput state.hists.sel s;
1902 conf.selcmd <- s;
1904 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
1905 textentry, ondone, true)
1907 | Keys.Ascii 'M' ->
1908 if conf.pax == None
1909 then conf.pax <- Some 0.0
1910 else conf.pax <- None;
1911 TEdone ("PAX " ^ btos (conf.pax != None))
1913 | (Keys.Ascii c) ->
1914 state.text <- Printf.sprintf "bad option %d `%c'" (Char.code c) c;
1915 TEstop
1917 | _ ->
1918 TEcont state.text
1921 let adderrmsg src msg =
1922 Buffer.add_string state.errmsgs msg;
1923 state.newerrmsgs <- true;
1924 postRedisplay src
1927 let adderrfmt src fmt =
1928 Format.ksprintf (fun s -> adderrmsg src s) fmt;
1931 class outlinelistview ~zebra ~source =
1932 let settext autonarrow s =
1933 if autonarrow
1934 then
1935 let ss = source#statestr in
1936 state.text <-
1937 if emptystr ss
1938 then "[" ^ s ^ "]"
1939 else "{" ^ ss ^ "} [" ^ s ^ "]"
1940 else state.text <- s
1942 object (self)
1943 inherit listview
1944 ~zebra
1945 ~helpmode:false
1946 ~source:(source :> lvsource)
1947 ~trusted:false
1948 ~modehash:(findkeyhash conf "outline")
1949 as super
1951 val m_autonarrow = false
1953 method! key key mask =
1954 let maxrows =
1955 if emptystr state.text
1956 then fstate.maxrows
1957 else fstate.maxrows - 2
1959 let calcfirst first active =
1960 if active > first
1961 then
1962 let rows = active - first in
1963 if rows > maxrows then active - maxrows else first
1964 else active
1966 let navigate incr =
1967 let active = m_active + incr in
1968 let active = bound active 0 (source#getitemcount - 1) in
1969 let first = calcfirst m_first active in
1970 postRedisplay "outline navigate";
1971 coe {< m_active = active; m_first = first >}
1973 let navscroll first =
1974 let active =
1975 let dist = m_active - first in
1976 if dist < 0
1977 then first
1978 else (
1979 if dist < maxrows
1980 then m_active
1981 else first + maxrows
1984 postRedisplay "outline navscroll";
1985 coe {< m_first = first; m_active = active >}
1987 let ctrl = Wsi.withctrl mask in
1988 let open Keys in
1989 match Wsi.kc2kt key with
1990 | Ascii 'a' when ctrl ->
1991 let text =
1992 if m_autonarrow
1993 then (source#denarrow; E.s)
1994 else (
1995 let pattern = source#renarrow in
1996 if nonemptystr m_qsearch
1997 then (source#narrow m_qsearch; m_qsearch)
1998 else pattern
2001 settext (not m_autonarrow) text;
2002 postRedisplay "toggle auto narrowing";
2003 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
2005 | Ascii '/' when emptystr m_qsearch && not m_autonarrow ->
2006 settext true E.s;
2007 postRedisplay "toggle auto narrowing";
2008 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
2010 | Ascii 'n' when ctrl ->
2011 source#narrow m_qsearch;
2012 if not m_autonarrow
2013 then source#add_narrow_pattern m_qsearch;
2014 postRedisplay "outline ctrl-n";
2015 coe {< m_first = 0; m_active = 0 >}
2017 | Ascii 'S' when ctrl ->
2018 let active = source#calcactive (getanchor ()) in
2019 let first = firstof m_first active in
2020 postRedisplay "outline ctrl-s";
2021 coe {< m_first = first; m_active = active >}
2023 | Ascii 'u' when ctrl ->
2024 postRedisplay "outline ctrl-u";
2025 if m_autonarrow && nonemptystr m_qsearch
2026 then (
2027 ignore (source#renarrow);
2028 settext m_autonarrow E.s;
2029 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
2031 else (
2032 source#del_narrow_pattern;
2033 let pattern = source#renarrow in
2034 let text =
2035 if emptystr pattern then E.s else "Narrowed to " ^ pattern
2037 settext m_autonarrow text;
2038 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
2041 | Ascii 'l' when ctrl ->
2042 let first = max 0 (m_active - (fstate.maxrows / 2)) in
2043 postRedisplay "outline ctrl-l";
2044 coe {< m_first = first >}
2046 | Ascii '\t' when m_autonarrow ->
2047 if nonemptystr m_qsearch
2048 then (
2049 postRedisplay "outline list view tab";
2050 source#add_narrow_pattern m_qsearch;
2051 settext true E.s;
2052 coe {< m_qsearch = E.s >}
2054 else coe self
2056 | Escape when m_autonarrow ->
2057 if nonemptystr m_qsearch
2058 then source#add_narrow_pattern m_qsearch;
2059 super#key key mask
2061 | Enter when m_autonarrow ->
2062 if nonemptystr m_qsearch
2063 then source#add_narrow_pattern m_qsearch;
2064 super#key key mask
2066 | (Ascii _ | Code _) when m_autonarrow ->
2067 let pattern = m_qsearch ^ toutf8 key in
2068 postRedisplay "outlinelistview autonarrow add";
2069 source#narrow pattern;
2070 settext true pattern;
2071 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
2073 | Backspace when m_autonarrow ->
2074 if emptystr m_qsearch
2075 then coe self
2076 else
2077 let pattern = withoutlastutf8 m_qsearch in
2078 postRedisplay "outlinelistview autonarrow backspace";
2079 ignore (source#renarrow);
2080 source#narrow pattern;
2081 settext true pattern;
2082 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
2084 | Up when ctrl ->
2085 navscroll (max 0 (m_first - 1))
2087 | Down when ctrl ->
2088 navscroll (min (source#getitemcount - 1) (m_first + 1))
2090 | Up -> navigate ~-1
2091 | Down -> navigate 1
2092 | Prior -> navigate ~-(fstate.maxrows)
2093 | Next -> navigate fstate.maxrows
2095 | Right ->
2096 let o =
2097 if ctrl
2098 then (
2099 postRedisplay "outline ctrl right";
2100 {< m_pan = m_pan + 1 >}
2102 else self#updownlevel 1
2104 coe o
2106 | Left ->
2107 let o =
2108 if ctrl
2109 then (
2110 postRedisplay "outline ctrl left";
2111 {< m_pan = m_pan - 1 >}
2113 else self#updownlevel ~-1
2115 coe o
2117 | Home ->
2118 postRedisplay "outline home";
2119 coe {< m_first = 0; m_active = 0 >}
2121 | End ->
2122 let active = source#getitemcount - 1 in
2123 let first = max 0 (active - fstate.maxrows) in
2124 postRedisplay "outline end";
2125 coe {< m_active = active; m_first = first >}
2127 | Delete|Escape|Insert|Enter|Ascii _|Code _|Ctrl _|Backspace|Fn _ ->
2128 super#key key mask
2129 end;;
2131 let genhistoutlines () =
2132 Config.gethist ()
2133 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
2134 compare c2.lastvisit c1.lastvisit)
2135 |> List.map (fun ((path, c, _, _, _, origin) as hist) ->
2136 let path = if nonemptystr origin then origin else path in
2137 let base = mbtoutf8 @@ Filename.basename path in
2138 (base ^ "\000" ^ c.title, 1, Ohistory hist)
2142 let gotohist (path, c, bookmarks, x, anchor, origin) =
2143 Config.save leavebirdseye;
2144 state.anchor <- anchor;
2145 state.bookmarks <- bookmarks;
2146 state.origin <- origin;
2147 state.x <- x;
2148 setconf conf c;
2149 let x0, y0, x1, y1 = conf.trimfuzz in
2150 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
2151 reshape ~firsttime:true state.winw state.winh;
2152 opendoc path origin;
2153 setzoom c.zoom;
2156 let setcheckers enabled =
2157 match state.checkerstexid with
2158 | None ->
2159 if enabled then state.checkerstexid <- Some (makecheckers ())
2161 | Some checkerstexid ->
2162 if not enabled
2163 then (
2164 GlTex.delete_texture checkerstexid;
2165 state.checkerstexid <- None;
2169 let describe_layout layout =
2170 let d =
2171 match layout with
2172 | [] -> "Page 0"
2173 | l :: [] -> Printf.sprintf "Page %d" (l.pageno+1)
2174 | l :: rest ->
2175 let rangestr a b =
2176 if a.pageno = b.pageno then Printf.sprintf "%d" (a.pageno+1)
2177 else
2178 let sep = if a.pageno+1 = b.pageno then ", " else Unisyms.ellipsis in
2179 Printf.sprintf "%d%s%d" (a.pageno+1) sep (b.pageno+1)
2181 let rec fold s la lb = function
2182 | [] -> Printf.sprintf "%s %s" s (rangestr la lb)
2183 | l :: rest when l.pageno = succ lb.pageno -> fold s la l rest
2184 | l :: rest -> fold (s ^ " " ^ rangestr la lb ^ ",") l l rest
2186 fold "Pages" l l rest
2188 let percent =
2189 let maxy = maxy () in
2190 if maxy <= 0
2191 then 100.
2192 else 100. *. (float state.y /. float maxy)
2194 Printf.sprintf "%s of %d [%.2f%%]" d state.pagecount percent
2197 let setpresentationmode v =
2198 let n = page_of_y state.y in
2199 state.anchor <- (n, 0.0, 1.0);
2200 conf.presentation <- v;
2201 if conf.fitmodel = FitPage
2202 then reqlayout conf.angle conf.fitmodel;
2203 represent ();
2206 let enterinfomode =
2207 let btos b = if b then Unisyms.radical else E.s in
2208 let showextended = ref false in
2209 let showcolors = ref false in
2210 let leave mode _ = state.mode <- mode in
2211 let src =
2212 (object
2213 val mutable m_l = []
2214 val mutable m_a = E.a
2215 val mutable m_prev_uioh = nouioh
2216 val mutable m_prev_mode = View
2218 inherit lvsourcebase
2220 method reset prev_mode prev_uioh =
2221 m_a <- Array.of_list (List.rev m_l);
2222 m_l <- [];
2223 m_prev_mode <- prev_mode;
2224 m_prev_uioh <- prev_uioh;
2226 method int name get set =
2227 m_l <-
2228 (name, `int get, 1,
2229 Action (
2230 fun u ->
2231 let ondone s =
2232 try set (int_of_string s)
2233 with exn ->
2234 state.text <- Printf.sprintf "bad integer `%s': %s"
2235 s @@ exntos exn
2237 state.text <- E.s;
2238 let te = name ^ ": ", E.s, None, intentry, ondone, true in
2239 state.mode <- Textentry (te, leave m_prev_mode);
2241 )) :: m_l
2243 method int_with_suffix name get set =
2244 m_l <-
2245 (name, `intws get, 1,
2246 Action (
2247 fun u ->
2248 let ondone s =
2249 try set (int_of_string_with_suffix s)
2250 with exn ->
2251 state.text <- Printf.sprintf "bad integer `%s': %s"
2252 s @@ exntos exn
2254 state.text <- E.s;
2255 let te =
2256 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
2258 state.mode <- Textentry (te, leave m_prev_mode);
2260 )) :: m_l
2262 method bool ?(offset=1) ?(btos=btos) name get set =
2263 m_l <-
2264 (name, `bool (btos, get), offset, Action (
2265 fun u ->
2266 let v = get () in
2267 set (not v);
2269 )) :: m_l
2271 method color name get set =
2272 m_l <-
2273 (name, `color get, 1,
2274 Action (
2275 fun u ->
2276 let invalid = (nan, nan, nan) in
2277 let ondone s =
2278 let c =
2279 try color_of_string s
2280 with exn ->
2281 state.text <- Printf.sprintf "bad color `%s': %s"
2282 s @@ exntos exn;
2283 invalid
2285 if c <> invalid
2286 then set c;
2288 let te = name ^ ": ", E.s, None, textentry, ondone, true in
2289 state.text <- color_to_string (get ());
2290 state.mode <- Textentry (te, leave m_prev_mode);
2292 )) :: m_l
2294 method string name get set =
2295 m_l <-
2296 (name, `string get, 1,
2297 Action (
2298 fun u ->
2299 let ondone s = set s in
2300 let te = name ^ ": ", E.s, None, textentry, ondone, true in
2301 state.mode <- Textentry (te, leave m_prev_mode);
2303 )) :: m_l
2305 method colorspace name get set =
2306 m_l <-
2307 (name, `string get, 1,
2308 Action (
2309 fun _ ->
2310 let source =
2311 (object
2312 inherit lvsourcebase
2314 initializer
2315 m_active <- CSTE.to_int conf.colorspace;
2316 m_first <- 0;
2318 method getitemcount =
2319 Array.length CSTE.names
2320 method getitem n =
2321 (CSTE.names.(n), 0)
2322 method exit ~uioh ~cancel ~active ~first ~pan =
2323 ignore (uioh, first, pan);
2324 if not cancel then set active;
2325 None
2326 method hasaction _ = true
2327 end)
2329 state.text <- E.s;
2330 let modehash = findkeyhash conf "info" in
2331 coe (new listview ~zebra:false ~helpmode:false
2332 ~source ~trusted:true ~modehash)
2333 )) :: m_l
2335 method paxmark name get set =
2336 m_l <-
2337 (name, `string get, 1,
2338 Action (
2339 fun _ ->
2340 let source =
2341 (object
2342 inherit lvsourcebase
2344 initializer
2345 m_active <- MTE.to_int conf.paxmark;
2346 m_first <- 0;
2348 method getitemcount = Array.length MTE.names
2349 method getitem n = (MTE.names.(n), 0)
2350 method exit ~uioh ~cancel ~active ~first ~pan =
2351 ignore (uioh, first, pan);
2352 if not cancel then set active;
2353 None
2354 method hasaction _ = true
2355 end)
2357 state.text <- E.s;
2358 let modehash = findkeyhash conf "info" in
2359 coe (new listview ~zebra:false ~helpmode:false
2360 ~source ~trusted:true ~modehash)
2361 )) :: m_l
2363 method fitmodel name get set =
2364 m_l <-
2365 (name, `string get, 1,
2366 Action (
2367 fun _ ->
2368 let source =
2369 (object
2370 inherit lvsourcebase
2372 initializer
2373 m_active <- FMTE.to_int conf.fitmodel;
2374 m_first <- 0;
2376 method getitemcount = Array.length FMTE.names
2377 method getitem n = (FMTE.names.(n), 0)
2378 method exit ~uioh ~cancel ~active ~first ~pan =
2379 ignore (uioh, first, pan);
2380 if not cancel then set active;
2381 None
2382 method hasaction _ = true
2383 end)
2385 state.text <- E.s;
2386 let modehash = findkeyhash conf "info" in
2387 coe (new listview ~zebra:false ~helpmode:false
2388 ~source ~trusted:true ~modehash)
2389 )) :: m_l
2391 method caption s offset =
2392 m_l <- (s, `empty, offset, Noaction) :: m_l
2394 method caption2 s f offset =
2395 m_l <- (s, `string f, offset, Noaction) :: m_l
2397 method getitemcount = Array.length m_a
2399 method getitem n =
2400 let tostr = function
2401 | `int f -> string_of_int (f ())
2402 | `intws f -> string_with_suffix_of_int (f ())
2403 | `string f -> f ()
2404 | `color f -> color_to_string (f ())
2405 | `bool (btos, f) -> btos (f ())
2406 | `empty -> E.s
2408 let name, t, offset, _ = m_a.(n) in
2409 ((let s = tostr t in
2410 if nonemptystr s
2411 then Printf.sprintf "%s\t%s" name s
2412 else name),
2413 offset)
2415 method exit ~uioh ~cancel ~active ~first ~pan =
2416 let uiohopt =
2417 if not cancel
2418 then (
2419 let uioh =
2420 match m_a.(active) with
2421 | _, _, _, Action f -> f uioh
2422 | _, _, _, Noaction -> uioh
2424 Some uioh
2426 else None
2428 m_active <- active;
2429 m_first <- first;
2430 m_pan <- pan;
2431 uiohopt
2433 method hasaction n =
2434 match m_a.(n) with
2435 | _, _, _, Action _ -> true
2436 | _, _, _, Noaction -> false
2438 initializer m_active <- 1
2439 end)
2441 let rec fillsrc prevmode prevuioh =
2442 let sep () = src#caption E.s 0 in
2443 let colorp name get set =
2444 src#string name
2445 (fun () -> color_to_string (get ()))
2446 (fun v ->
2448 let c = color_of_string v in
2449 set c
2450 with exn ->
2451 state.text <-
2452 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
2455 let rgba name get set =
2456 src#string name
2457 (fun () -> rgba_to_string (get ()))
2458 (fun v ->
2460 let c = rgba_of_string v in
2461 set c
2462 with exn ->
2463 state.text <-
2464 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
2467 let oldmode = state.mode in
2468 let birdseye = isbirdseye state.mode in
2470 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
2472 src#bool "presentation mode"
2473 (fun () -> conf.presentation)
2474 (fun v -> setpresentationmode v);
2476 src#bool "ignore case in searches"
2477 (fun () -> conf.icase)
2478 (fun v -> conf.icase <- v);
2480 src#bool "preload"
2481 (fun () -> conf.preload)
2482 (fun v -> conf.preload <- v);
2484 src#bool "highlight links"
2485 (fun () -> conf.hlinks)
2486 (fun v -> conf.hlinks <- v);
2488 src#bool "under info"
2489 (fun () -> conf.underinfo)
2490 (fun v -> conf.underinfo <- v);
2492 src#fitmodel "fit model"
2493 (fun () -> FMTE.to_string conf.fitmodel)
2494 (fun v -> reqlayout conf.angle (FMTE.of_int v));
2496 src#bool "trim margins"
2497 (fun () -> conf.trimmargins)
2498 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
2500 sep ();
2501 src#int "inter-page space"
2502 (fun () -> conf.interpagespace)
2503 (fun n ->
2504 conf.interpagespace <- n;
2505 docolumns conf.columns;
2506 let pageno, py =
2507 match state.layout with
2508 | [] -> 0, 0
2509 | l :: _ ->
2510 l.pageno, l.pagey
2512 state.maxy <- calcheight ();
2513 let y = getpagey pageno in
2514 gotoxy state.x (y + py)
2517 src#int "page bias"
2518 (fun () -> conf.pagebias)
2519 (fun v -> conf.pagebias <- v);
2521 src#int "scroll step"
2522 (fun () -> conf.scrollstep)
2523 (fun n -> conf.scrollstep <- n);
2525 src#int "horizontal scroll step"
2526 (fun () -> conf.hscrollstep)
2527 (fun v -> conf.hscrollstep <- v);
2529 src#int "auto scroll step"
2530 (fun () ->
2531 match state.autoscroll with
2532 | Some step -> step
2533 | _ -> conf.autoscrollstep)
2534 (fun n ->
2535 let n = boundastep state.winh n in
2536 if state.autoscroll <> None
2537 then state.autoscroll <- Some n;
2538 conf.autoscrollstep <- n);
2540 src#int "zoom"
2541 (fun () -> truncate (conf.zoom *. 100.))
2542 (fun v -> pivotzoom ((float v) /. 100.));
2544 src#int "rotation"
2545 (fun () -> conf.angle)
2546 (fun v -> reqlayout v conf.fitmodel);
2548 src#int "scroll bar width"
2549 (fun () -> conf.scrollbw)
2550 (fun v ->
2551 conf.scrollbw <- v;
2552 reshape state.winw state.winh;
2555 src#int "scroll handle height"
2556 (fun () -> conf.scrollh)
2557 (fun v -> conf.scrollh <- v;);
2559 src#int "thumbnail width"
2560 (fun () -> conf.thumbw)
2561 (fun v ->
2562 conf.thumbw <- min 4096 v;
2563 match oldmode with
2564 | Birdseye beye ->
2565 leavebirdseye beye false;
2566 enterbirdseye ()
2567 | Textentry _
2568 | View
2569 | LinkNav _ -> ()
2572 let mode = state.mode in
2573 src#string "columns"
2574 (fun () ->
2575 match conf.columns with
2576 | Csingle _ -> "1"
2577 | Cmulti (multi, _) -> multicolumns_to_string multi
2578 | Csplit (count, _) -> "-" ^ string_of_int count
2580 (fun v ->
2581 let n, a, b = multicolumns_of_string v in
2582 setcolumns mode n a b);
2584 sep ();
2585 src#caption "Pixmap cache" 0;
2586 src#int_with_suffix "size (advisory)"
2587 (fun () -> conf.memlimit)
2588 (fun v -> conf.memlimit <- v);
2590 src#caption2 "used"
2591 (fun () ->
2592 Printf.sprintf "%s bytes, %d tiles"
2593 (string_with_suffix_of_int state.memused)
2594 (Hashtbl.length state.tilemap)) 1;
2596 sep ();
2597 src#caption "Layout" 0;
2598 src#caption2 "Dimension"
2599 (fun () ->
2600 Printf.sprintf "%dx%d (virtual %dx%d)"
2601 state.winw state.winh
2602 state.w state.maxy)
2604 if conf.debug
2605 then
2606 src#caption2 "Position" (fun () ->
2607 Printf.sprintf "%dx%d" state.x state.y
2609 else
2610 src#caption2 "Position" (fun () -> describe_layout state.layout) 1;
2612 sep ();
2613 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
2614 "Save these parameters as global defaults at exit"
2615 (fun () -> conf.bedefault)
2616 (fun v -> conf.bedefault <- v);
2618 sep ();
2619 let btos b = if b then Unisyms.lguillemet else Unisyms.rguillemet in
2620 src#bool ~offset:0 ~btos "Extended parameters"
2621 (fun () -> !showextended)
2622 (fun v -> showextended := v; fillsrc prevmode prevuioh);
2623 if !showextended
2624 then (
2625 src#bool "checkers"
2626 (fun () -> conf.checkers)
2627 (fun v -> conf.checkers <- v; setcheckers v);
2628 src#bool "update cursor"
2629 (fun () -> conf.updatecurs)
2630 (fun v -> conf.updatecurs <- v);
2631 src#bool "scroll-bar on the left"
2632 (fun () -> conf.leftscroll)
2633 (fun v -> conf.leftscroll <- v);
2634 src#bool "verbose"
2635 (fun () -> conf.verbose)
2636 (fun v -> conf.verbose <- v);
2637 src#bool "invert colors"
2638 (fun () -> conf.invert)
2639 (fun v -> conf.invert <- v);
2640 src#bool "max fit"
2641 (fun () -> conf.maxhfit)
2642 (fun v -> conf.maxhfit <- v);
2643 src#bool "pax mode"
2644 (fun () -> conf.pax != None)
2645 (fun v ->
2646 if v
2647 then conf.pax <- Some (now ())
2648 else conf.pax <- None);
2649 src#string "uri launcher"
2650 (fun () -> conf.urilauncher)
2651 (fun v -> conf.urilauncher <- v);
2652 src#string "path launcher"
2653 (fun () -> conf.pathlauncher)
2654 (fun v -> conf.pathlauncher <- v);
2655 src#string "tile size"
2656 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
2657 (fun v ->
2659 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
2660 conf.tilew <- max 64 w;
2661 conf.tileh <- max 64 h;
2662 flushtiles ();
2663 with exn ->
2664 state.text <- Printf.sprintf "bad tile size `%s': %s"
2665 v @@ exntos exn
2667 src#int "texture count"
2668 (fun () -> conf.texcount)
2669 (fun v ->
2670 if realloctexts v
2671 then conf.texcount <- v
2672 else impmsg "failed to set texture count please retry later"
2674 src#int "slice height"
2675 (fun () -> conf.sliceheight)
2676 (fun v ->
2677 conf.sliceheight <- v;
2678 wcmd "sliceh %d" conf.sliceheight;
2680 src#int "anti-aliasing level"
2681 (fun () -> conf.aalevel)
2682 (fun v ->
2683 conf.aalevel <- bound v 0 8;
2684 state.anchor <- getanchor ();
2685 opendoc state.path state.password;
2687 src#string "page scroll scaling factor"
2688 (fun () -> string_of_float conf.pgscale)
2689 (fun v ->
2691 let s = float_of_string v in
2692 conf.pgscale <- s
2693 with exn ->
2694 state.text <- Printf.sprintf
2695 "bad page scroll scaling factor `%s': %s" v
2696 @@ exntos exn
2699 src#int "ui font size"
2700 (fun () -> fstate.fontsize)
2701 (fun v -> setfontsize (bound v 5 100));
2702 src#int "hint font size"
2703 (fun () -> conf.hfsize)
2704 (fun v -> conf.hfsize <- bound v 5 100);
2705 src#string "trim fuzz"
2706 (fun () -> irect_to_string conf.trimfuzz)
2707 (fun v ->
2709 conf.trimfuzz <- irect_of_string v;
2710 if conf.trimmargins
2711 then settrim true conf.trimfuzz;
2712 with exn ->
2713 state.text <- Printf.sprintf "bad irect `%s': %s" v
2714 @@ exntos exn
2716 src#string "selection command"
2717 (fun () -> conf.selcmd)
2718 (fun v -> conf.selcmd <- v);
2719 src#string "synctex command"
2720 (fun () -> conf.stcmd)
2721 (fun v -> conf.stcmd <- v);
2722 src#string "pax command"
2723 (fun () -> conf.paxcmd)
2724 (fun v -> conf.paxcmd <- v);
2725 src#string "ask password command"
2726 (fun () -> conf.passcmd)
2727 (fun v -> conf.passcmd <- v);
2728 src#string "save path command"
2729 (fun () -> conf.savecmd)
2730 (fun v -> conf.savecmd <- v);
2731 src#colorspace "color space"
2732 (fun () -> CSTE.to_string conf.colorspace)
2733 (fun v ->
2734 conf.colorspace <- CSTE.of_int v;
2735 wcmd "cs %d" v;
2736 load state.layout;
2738 src#paxmark "pax mark method"
2739 (fun () -> MTE.to_string conf.paxmark)
2740 (fun v -> conf.paxmark <- MTE.of_int v);
2741 if bousable () && !opengl_has_pbo
2742 then
2743 src#bool "use PBO"
2744 (fun () -> conf.usepbo)
2745 (fun v -> conf.usepbo <- v);
2746 src#bool "mouse wheel scrolls pages"
2747 (fun () -> conf.wheelbypage)
2748 (fun v -> conf.wheelbypage <- v);
2749 src#bool "open remote links in a new instance"
2750 (fun () -> conf.riani)
2751 (fun v -> conf.riani <- v);
2752 src#bool "edit annotations inline"
2753 (fun () -> conf.annotinline)
2754 (fun v -> conf.annotinline <- v);
2755 src#bool "coarse positioning in presentation mode"
2756 (fun () -> conf.coarseprespos)
2757 (fun v -> conf.coarseprespos <- v);
2758 src#bool "use document CSS"
2759 (fun () -> conf.usedoccss)
2760 (fun v ->
2761 conf.usedoccss <- v;
2762 state.anchor <- getanchor ();
2763 opendoc state.path state.password;
2765 src#bool ~btos "colors"
2766 (fun () -> !showcolors)
2767 (fun v -> showcolors := v; fillsrc prevmode prevuioh);
2768 if !showcolors
2769 then (
2770 colorp " background"
2771 (fun () -> conf.bgcolor)
2772 (fun v -> conf.bgcolor <- v);
2773 rgba " scrollbar"
2774 (fun () -> conf.sbarcolor)
2775 (fun v -> conf.sbarcolor <- v);
2776 rgba " scrollbar handle"
2777 (fun () -> conf.sbarhndlcolor)
2778 (fun v -> conf.sbarhndlcolor <- v);
2782 sep ();
2783 src#caption "Document" 0;
2784 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
2785 src#caption2 "Pages"
2786 (fun () -> string_of_int state.pagecount) 1;
2787 src#caption2 "Dimensions"
2788 (fun () -> string_of_int (List.length state.pdims)) 1;
2789 if nonemptystr conf.css
2790 then src#caption2 "CSS" (fun () -> conf.css) 1;
2791 if conf.trimmargins
2792 then (
2793 sep ();
2794 src#caption "Trimmed margins" 0;
2795 src#caption2 "Dimensions"
2796 (fun () -> string_of_int (List.length state.pdims)) 1;
2799 sep ();
2800 src#caption "OpenGL" 0;
2801 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
2802 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
2804 sep ();
2805 src#caption "Location" 0;
2806 if nonemptystr state.origin
2807 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
2808 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
2810 src#reset prevmode prevuioh;
2812 fun () ->
2813 state.text <- E.s;
2814 resetmstate ();
2815 let prevmode = state.mode
2816 and prevuioh = state.uioh in
2817 fillsrc prevmode prevuioh;
2818 let source = (src :> lvsource) in
2819 let modehash = findkeyhash conf "info" in
2820 state.uioh <-
2821 coe (object (self)
2822 inherit listview ~zebra:false ~helpmode:false
2823 ~source ~trusted:true ~modehash as super
2824 val mutable m_prevmemused = 0
2825 method! infochanged = function
2826 | Memused ->
2827 if m_prevmemused != state.memused
2828 then (
2829 m_prevmemused <- state.memused;
2830 postRedisplay "memusedchanged";
2832 | Pdim -> postRedisplay "pdimchanged"
2833 | Docinfo -> fillsrc prevmode prevuioh
2835 method! key key mask =
2836 if not (Wsi.withctrl mask)
2837 then
2838 match [@warning "-4"] Wsi.kc2kt key with
2839 | Keys.Left -> coe (self#updownlevel ~-1)
2840 | Keys.Right -> coe (self#updownlevel 1)
2841 | _ -> super#key key mask
2842 else super#key key mask
2843 end);
2844 postRedisplay "info";
2847 let enterhelpmode =
2848 let source =
2849 (object
2850 inherit lvsourcebase
2851 method getitemcount = Array.length state.help
2852 method getitem n =
2853 let s, l, _ = state.help.(n) in
2854 (s, l)
2856 method exit ~uioh ~cancel ~active ~first ~pan =
2857 let optuioh =
2858 if not cancel
2859 then (
2860 match state.help.(active) with
2861 | _, _, Action f -> Some (f uioh)
2862 | _, _, Noaction -> Some uioh
2864 else None
2866 m_active <- active;
2867 m_first <- first;
2868 m_pan <- pan;
2869 optuioh
2871 method hasaction n =
2872 match state.help.(n) with
2873 | _, _, Action _ -> true
2874 | _, _, Noaction -> false
2876 initializer
2877 m_active <- -1
2878 end)
2879 in fun () ->
2880 let modehash = findkeyhash conf "help" in
2881 resetmstate ();
2882 state.uioh <- coe (new listview
2883 ~zebra:false ~helpmode:true
2884 ~source ~trusted:true ~modehash);
2885 postRedisplay "help";
2888 let entermsgsmode =
2889 let msgsource =
2890 (object
2891 inherit lvsourcebase
2892 val mutable m_items = E.a
2894 method getitemcount = 1 + Array.length m_items
2896 method getitem n =
2897 if n = 0
2898 then "[Clear]", 0
2899 else m_items.(n-1), 0
2901 method exit ~uioh ~cancel ~active ~first ~pan =
2902 ignore uioh;
2903 if not cancel
2904 then (
2905 if active = 0
2906 then Buffer.clear state.errmsgs;
2908 m_active <- active;
2909 m_first <- first;
2910 m_pan <- pan;
2911 None
2913 method hasaction n =
2914 n = 0
2916 method reset =
2917 state.newerrmsgs <- false;
2918 let l = Str.split Utils.Re.crlf (Buffer.contents state.errmsgs) in
2919 m_items <- Array.of_list l
2921 initializer
2922 m_active <- 0
2923 end)
2924 in fun () ->
2925 state.text <- E.s;
2926 resetmstate ();
2927 msgsource#reset;
2928 let source = (msgsource :> lvsource) in
2929 let modehash = findkeyhash conf "listview" in
2930 state.uioh <-
2931 coe (object
2932 inherit listview ~zebra:false ~helpmode:false
2933 ~source ~trusted:false ~modehash as super
2934 method! display =
2935 if state.newerrmsgs
2936 then msgsource#reset;
2937 super#display
2938 end);
2939 postRedisplay "msgs";
2942 let getusertext s =
2943 let editor = getenvwithdef "EDITOR" E.s in
2944 if emptystr editor
2945 then E.s
2946 else
2947 let tmppath = Filename.temp_file "llpp" "note" in
2948 if nonemptystr s
2949 then (
2950 let oc = open_out tmppath in
2951 output_string oc s;
2952 close_out oc;
2954 let execstr = editor ^ " " ^ tmppath in
2955 let s =
2956 match spawn execstr [] with
2957 | exception exn ->
2958 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
2960 | pid ->
2961 match Unix.waitpid [] pid with
2962 | exception exn ->
2963 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
2965 | (_pid, status) ->
2966 match status with
2967 | Unix.WEXITED 0 -> filecontents tmppath
2968 | Unix.WEXITED n ->
2969 impmsg "editor process(%s) exited abnormally: %d" execstr n;
2971 | Unix.WSIGNALED n ->
2972 impmsg "editor process(%s) was killed by signal %d" execstr n;
2974 | Unix.WSTOPPED n ->
2975 impmsg "editor(%s) process was stopped by signal %d" execstr n;
2978 match Unix.unlink tmppath with
2979 | exception exn ->
2980 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
2982 | () -> s
2985 let enterannotmode opaque slinkindex =
2986 let msgsource =
2987 (object
2988 inherit lvsourcebase
2989 val mutable m_text = E.s
2990 val mutable m_items = E.a
2992 method getitemcount = Array.length m_items
2994 method getitem n =
2995 let label, _func = m_items.(n) in
2996 label, 0
2998 method exit ~uioh ~cancel ~active ~first ~pan =
2999 ignore (uioh, first, pan);
3000 if not cancel
3001 then (
3002 let _label, func = m_items.(active) in
3003 func ()
3005 None
3007 method hasaction n = nonemptystr @@ fst m_items.(n)
3009 method reset s =
3010 let rec split accu b i =
3011 let p = b+i in
3012 if p = String.length s
3013 then (String.sub s b (p-b), unit) :: accu
3014 else
3015 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
3016 then
3017 let ss = if i = 0 then E.s else String.sub s b i in
3018 split ((ss, unit)::accu) (p+1) 0
3019 else
3020 split accu b (i+1)
3022 let cleanup () =
3023 wcmd "freepage %s" (~> opaque);
3024 let keys =
3025 Hashtbl.fold (fun key opaque' accu ->
3026 if opaque' = opaque'
3027 then key :: accu else accu) state.pagemap []
3029 List.iter (Hashtbl.remove state.pagemap) keys;
3030 flushtiles ();
3031 gotoxy state.x state.y
3033 let dele () =
3034 delannot opaque slinkindex;
3035 cleanup ();
3037 let edit inline () =
3038 let update s =
3039 if emptystr s
3040 then dele ()
3041 else (
3042 modannot opaque slinkindex s;
3043 cleanup ();
3046 if inline
3047 then
3048 let mode = state.mode in
3049 state.mode <-
3050 Textentry (
3051 ("annotation: ", m_text, None, textentry, update, true),
3052 fun _ -> state.mode <- mode);
3053 state.text <- E.s;
3054 enttext ();
3055 else
3056 let s = getusertext m_text in
3057 update s
3059 m_text <- s;
3060 m_items <-
3061 ( "[Copy]", fun () -> selstring conf.selcmd m_text)
3062 :: ("[Delete]", dele)
3063 :: ("[Edit]", edit conf.annotinline)
3064 :: (E.s, unit)
3065 :: split [] 0 0 |> List.rev |> Array.of_list
3067 initializer
3068 m_active <- 0
3069 end)
3071 state.text <- E.s;
3072 let s = getannotcontents opaque slinkindex in
3073 resetmstate ();
3074 msgsource#reset s;
3075 let source = (msgsource :> lvsource) in
3076 let modehash = findkeyhash conf "listview" in
3077 state.uioh <- coe (object
3078 inherit listview ~zebra:false ~helpmode:false
3079 ~source ~trusted:false ~modehash
3080 end);
3081 postRedisplay "enterannotmode";
3084 let gotoremote spec =
3085 let filename, dest = splitatchar spec '#' in
3086 let getpath filename =
3087 let path =
3088 if nonemptystr filename
3089 then
3090 if Filename.is_relative filename
3091 then
3092 let dir = Filename.dirname state.path in
3093 let dir =
3094 if Filename.is_implicit dir
3095 then Filename.concat (Sys.getcwd ()) dir
3096 else dir
3098 Filename.concat dir filename
3099 else filename
3100 else E.s
3102 if Sys.file_exists path
3103 then path
3104 else E.s
3106 let path = getpath filename in
3107 let dospawn lcmd =
3108 if conf.riani
3109 then
3110 let cmd = Lazy.force_val lcmd in
3111 match spawn cmd with
3112 | _pid -> ()
3113 | exception exn -> dolog "failed to execute `%s': %s" cmd @@ exntos exn
3114 else
3115 let anchor = getanchor () in
3116 let ranchor = state.path, state.password, anchor, state.origin in
3117 state.origin <- E.s;
3118 state.ranchors <- ranchor :: state.ranchors;
3119 opendoc path E.s;
3121 if substratis spec 0 "page="
3122 then
3123 match Scanf.sscanf spec "page=%d" (fun n -> n) with
3124 | pageno ->
3125 state.anchor <- (pageno, 0.0, 0.0);
3126 dospawn @@ lazy (Printf.sprintf "%s -page %d %S" !selfexec pageno path);
3127 | exception exn ->
3128 adderrfmt "error parsing remote destination" "page: %s" @@ exntos exn
3129 else (
3130 state.nameddest <- dest;
3131 dospawn @@ lazy (!selfexec ^ " " ^ path ^ " -dest " ^ dest)
3135 let gotounder = function
3136 | Ulinkuri s when isexternallink s ->
3137 if substratis s 0 "file://"
3138 then gotoremote @@ String.sub s 7 (String.length s - 7)
3139 else Help.gotouri conf.urilauncher s
3140 | Ulinkuri s ->
3141 let pageno, x, y = uritolocation s in
3142 addnav ();
3143 gotopagexy pageno x y
3144 | Utext _ | Unone -> ()
3145 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
3148 let gotooutline (_, _, kind) =
3149 match kind with
3150 | Onone -> ()
3151 | Oanchor anchor ->
3152 let (pageno, y, _) = anchor in
3153 let y = getanchory
3154 (if conf.presentation then (pageno, y, 1.0) else anchor)
3156 addnav ();
3157 gotoxy state.x y
3158 | Ouri uri -> gotounder (Ulinkuri uri)
3159 | Olaunch _cmd -> failwith "gotounder (Ulaunch cmd)"
3160 | Oremote _remote -> failwith "gotounder (Uremote remote)"
3161 | Ohistory hist -> gotohist hist
3162 | Oremotedest _remotedest -> failwith "gotounder (Uremotedest remotedest)"
3165 class outlinesoucebase fetchoutlines = object (self)
3166 inherit lvsourcebase
3167 val mutable m_items = E.a
3168 val mutable m_minfo = E.a
3169 val mutable m_orig_items = E.a
3170 val mutable m_orig_minfo = E.a
3171 val mutable m_narrow_patterns = []
3172 val mutable m_gen = -1
3174 method getitemcount = Array.length m_items
3176 method getitem n =
3177 let s, n, _ = m_items.(n) in
3178 (s, n+0)
3180 method exit ~(uioh:uioh) ~cancel ~active ~(first:int) ~pan : uioh option =
3181 ignore (uioh, first);
3182 let items, minfo =
3183 if m_narrow_patterns = []
3184 then m_orig_items, m_orig_minfo
3185 else m_items, m_minfo
3187 m_pan <- pan;
3188 if not cancel
3189 then (
3190 m_items <- items;
3191 m_minfo <- minfo;
3192 gotooutline m_items.(active);
3194 else (
3195 m_items <- items;
3196 m_minfo <- minfo;
3198 None
3200 method hasaction (_:int) = true
3202 method greetmsg =
3203 if Array.length m_items != Array.length m_orig_items
3204 then
3205 let s =
3206 match m_narrow_patterns with
3207 | one :: [] -> one
3208 | many -> String.concat Unisyms.ellipsis (List.rev many)
3210 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
3211 else E.s
3213 method statestr =
3214 match m_narrow_patterns with
3215 | [] -> E.s
3216 | one :: [] -> one
3217 | head :: _ -> Unisyms.ellipsis ^ head
3219 method narrow pattern =
3220 match Str.regexp_case_fold pattern with
3221 | exception _ -> ()
3222 | re ->
3223 let rec loop accu minfo n =
3224 if n = -1
3225 then (
3226 m_items <- Array.of_list accu;
3227 m_minfo <- Array.of_list minfo;
3229 else
3230 let (s, _, _) as o = m_items.(n) in
3231 let accu, minfo =
3232 match Str.search_forward re s 0 with
3233 | exception Not_found -> accu, minfo
3234 | first -> o :: accu, (first, Str.match_end ()) :: minfo
3236 loop accu minfo (n-1)
3238 loop [] [] (Array.length m_items - 1)
3240 method! getminfo = m_minfo
3242 method denarrow =
3243 m_orig_items <- fetchoutlines ();
3244 m_minfo <- m_orig_minfo;
3245 m_items <- m_orig_items
3247 method add_narrow_pattern pattern =
3248 m_narrow_patterns <- pattern :: m_narrow_patterns
3250 method del_narrow_pattern =
3251 match m_narrow_patterns with
3252 | _ :: rest -> m_narrow_patterns <- rest
3253 | [] -> ()
3255 method renarrow =
3256 self#denarrow;
3257 match m_narrow_patterns with
3258 | pattern :: [] -> self#narrow pattern; pattern
3259 | list ->
3260 List.fold_left (fun accu pattern ->
3261 self#narrow pattern;
3262 pattern ^ Unisyms.ellipsis ^ accu) E.s list
3264 method calcactive (_:anchor) = 0
3266 method reset anchor items =
3267 if state.gen != m_gen
3268 then (
3269 m_orig_items <- items;
3270 m_items <- items;
3271 m_narrow_patterns <- [];
3272 m_minfo <- E.a;
3273 m_orig_minfo <- E.a;
3274 m_gen <- state.gen;
3276 else (
3277 if items != m_orig_items
3278 then (
3279 m_orig_items <- items;
3280 if m_narrow_patterns == []
3281 then m_items <- items;
3284 let active = self#calcactive anchor in
3285 m_active <- active;
3286 m_first <- firstof m_first active
3290 let outlinesource fetchoutlines =
3291 (object
3292 inherit outlinesoucebase fetchoutlines
3293 method! calcactive anchor =
3294 let rely = getanchory anchor in
3295 let rec loop n best bestd =
3296 if n = Array.length m_items
3297 then best
3298 else
3299 let _, _, kind = m_items.(n) in
3300 match kind with
3301 | Oanchor anchor ->
3302 let orely = getanchory anchor in
3303 let d = abs (orely - rely) in
3304 if d < bestd
3305 then loop (n+1) n d
3306 else loop (n+1) best bestd
3307 | Onone | Oremote _ | Olaunch _
3308 | Oremotedest _ | Ouri _ | Ohistory _ ->
3309 loop (n+1) best bestd
3311 loop 0 ~-1 max_int
3312 end)
3315 let enteroutlinemode, enterbookmarkmode, enterhistmode =
3316 let mkselector sourcetype =
3317 let fetchoutlines () =
3318 match sourcetype with
3319 | `bookmarks -> Array.of_list state.bookmarks
3320 | `outlines -> state.outlines
3321 | `history -> genhistoutlines () |> Array.of_list
3323 let source =
3324 if sourcetype = `history
3325 then new outlinesoucebase fetchoutlines
3326 else outlinesource fetchoutlines
3328 (fun errmsg ->
3329 let outlines = fetchoutlines () in
3330 if Array.length outlines = 0
3331 then showtext ' ' errmsg
3332 else (
3333 resetmstate ();
3334 Wsi.setcursor Wsi.CURSOR_INHERIT;
3335 let anchor = getanchor () in
3336 source#reset anchor outlines;
3337 state.text <- source#greetmsg;
3338 state.uioh <-
3339 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
3340 postRedisplay "enter selector";
3344 let mkenter sourcetype errmsg =
3345 let enter = mkselector sourcetype in
3346 fun () -> enter errmsg
3348 ( mkenter `outlines "document has no outline"
3349 , mkenter `bookmarks "document has no bookmarks (yet)"
3350 , mkenter `history "history is empty" )
3353 let quickbookmark ?title () =
3354 match state.layout with
3355 | [] -> ()
3356 | l :: _ ->
3357 let title =
3358 match title with
3359 | None ->
3360 Unix.(
3361 let tm = localtime (now ()) in
3362 Printf.sprintf
3363 "Quick (page %d) (bookmarked on %02d/%02d/%d at %02d:%02d)"
3364 (l.pageno+1)
3365 tm.tm_mday (tm.tm_mon+1) (tm.tm_year+1900) tm.tm_hour tm.tm_min
3367 | Some title -> title
3369 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
3372 let setautoscrollspeed step goingdown =
3373 let incr = max 1 ((abs step) / 2) in
3374 let incr = if goingdown then incr else -incr in
3375 let astep = boundastep state.winh (step + incr) in
3376 state.autoscroll <- Some astep;
3379 let canpan () =
3380 match conf.columns with
3381 | Csplit _ -> true
3382 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
3385 let panbound x = bound x (-state.w) state.winw;;
3387 let existsinrow pageno (columns, coverA, coverB) p =
3388 let last = ((pageno - coverA) mod columns) + columns in
3389 let rec any = function
3390 | [] -> false
3391 | l :: rest ->
3392 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
3393 then p l
3394 else (
3395 if not (p l)
3396 then (if l.pageno = last then false else any rest)
3397 else true
3400 any state.layout
3403 let nextpage () =
3404 match state.layout with
3405 | [] ->
3406 let pageno = page_of_y state.y in
3407 gotoxy state.x (getpagey (pageno+1))
3408 | l :: rest ->
3409 match conf.columns with
3410 | Csingle _ ->
3411 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
3412 then
3413 let y = clamp (pgscale state.winh) in
3414 gotoxy state.x y
3415 else
3416 let pageno = min (l.pageno+1) (state.pagecount-1) in
3417 gotoxy state.x (getpagey pageno)
3418 | Cmulti ((c, _, _) as cl, _) ->
3419 if conf.presentation
3420 && (existsinrow l.pageno cl
3421 (fun l -> l.pageh > l.pagey + l.pagevh))
3422 then
3423 let y = clamp (pgscale state.winh) in
3424 gotoxy state.x y
3425 else
3426 let pageno = min (l.pageno+c) (state.pagecount-1) in
3427 gotoxy state.x (getpagey pageno)
3428 | Csplit (n, _) ->
3429 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
3430 then
3431 let pagey, pageh = getpageyh l.pageno in
3432 let pagey = pagey + pageh * l.pagecol in
3433 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
3434 gotoxy state.x (pagey + pageh + ips)
3437 let prevpage () =
3438 match state.layout with
3439 | [] ->
3440 let pageno = page_of_y state.y in
3441 gotoxy state.x (getpagey (pageno-1))
3442 | l :: _ ->
3443 match conf.columns with
3444 | Csingle _ ->
3445 if conf.presentation && l.pagey != 0
3446 then
3447 gotoxy state.x (clamp (pgscale ~-(state.winh)))
3448 else
3449 let pageno = max 0 (l.pageno-1) in
3450 gotoxy state.x (getpagey pageno)
3451 | Cmulti ((c, _, coverB) as cl, _) ->
3452 if conf.presentation &&
3453 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
3454 then
3455 gotoxy state.x (clamp (pgscale ~-(state.winh)))
3456 else
3457 let decr =
3458 if l.pageno = state.pagecount - coverB
3459 then 1
3460 else c
3462 let pageno = max 0 (l.pageno-decr) in
3463 gotoxy state.x (getpagey pageno)
3464 | Csplit (n, _) ->
3465 let y =
3466 if l.pagecol = 0
3467 then
3468 if l.pageno = 0
3469 then l.pagey
3470 else
3471 let pageno = max 0 (l.pageno-1) in
3472 let pagey, pageh = getpageyh pageno in
3473 pagey + (n-1)*pageh
3474 else
3475 let pagey, pageh = getpageyh l.pageno in
3476 pagey + pageh * (l.pagecol-1) - conf.interpagespace
3478 gotoxy state.x y
3481 let save () =
3482 if emptystr conf.savecmd
3483 then adderrmsg "savepath-command is empty"
3484 "don't know where to save modified document"
3485 else
3486 let savecmd = Str.global_replace Utils.Re.percent state.path conf.savecmd in
3487 let path =
3488 getcmdoutput
3489 (fun exn ->
3490 adderrfmt savecmd "failed to produce path to the saved copy: %s" exn)
3491 savecmd
3493 if nonemptystr path
3494 then
3495 let tmp = path ^ ".tmp" in
3496 savedoc tmp;
3497 Unix.rename tmp path;
3500 let viewkeyboard key mask =
3501 let enttext te =
3502 let mode = state.mode in
3503 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
3504 state.text <- E.s;
3505 enttext ();
3506 postRedisplay "view:enttext"
3508 let ctrl = Wsi.withctrl mask in
3509 let open Keys in
3510 match Wsi.kc2kt key with
3511 | Ascii 'S' -> state.slideshow <- state.slideshow lxor 1
3513 | Ascii 'Q' -> exit 0
3515 | Ascii 'W' ->
3516 if hasunsavedchanges ()
3517 then save ()
3519 | Insert ->
3520 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
3521 then (
3522 state.mode <- (
3523 match state.lnava with
3524 | None -> LinkNav (Ltgendir 0)
3525 | Some pn -> LinkNav (Ltexact pn)
3527 gotoxy state.x state.y;
3529 else impmsg "keyboard link navigation does not work under rotation"
3531 | Escape | Ascii 'q' ->
3532 begin match state.mstate with
3533 | Mzoomrect _ ->
3534 resetmstate ();
3535 postRedisplay "kill rect";
3536 | Msel _
3537 | Mpan _
3538 | Mscrolly | Mscrollx
3539 | Mzoom _
3540 | Mnone ->
3541 begin match state.mode with
3542 | LinkNav ln ->
3543 begin match ln with
3544 | Ltexact pl -> state.lnava <- Some pl
3545 | Ltgendir _ | Ltnotready _ -> state.lnava <- None
3546 end;
3547 state.mode <- View;
3548 postRedisplay "esc leave linknav"
3549 | Birdseye _ | Textentry _ | View ->
3550 match state.ranchors with
3551 | [] -> raise Quit
3552 | (path, password, anchor, origin) :: rest ->
3553 state.ranchors <- rest;
3554 state.anchor <- anchor;
3555 state.origin <- origin;
3556 state.nameddest <- E.s;
3557 opendoc path password
3558 end;
3559 end;
3561 | Backspace ->
3562 addnavnorc ();
3563 gotoxy state.x (getnav ~-1)
3565 | Ascii 'o' ->
3566 enteroutlinemode ()
3568 | Ascii 'H' ->
3569 enterhistmode ()
3571 | Ascii 'u' ->
3572 state.rects <- [];
3573 state.text <- E.s;
3574 Hashtbl.iter (fun _ opaque ->
3575 clearmark opaque;
3576 Hashtbl.clear state.prects) state.pagemap;
3577 postRedisplay "dehighlight";
3579 | Ascii (('/' | '?') as c) ->
3580 let ondone isforw s =
3581 cbput state.hists.pat s;
3582 state.searchpattern <- s;
3583 search s isforw
3585 let s = String.make 1 c in
3586 enttext (s, E.s, Some (onhist state.hists.pat),
3587 textentry, ondone (c = '/'), true)
3589 | Ascii '+' | Ascii '=' when ctrl ->
3590 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
3591 pivotzoom (conf.zoom +. incr)
3593 | Ascii '+' ->
3594 let ondone s =
3595 let n =
3596 try int_of_string s with exn ->
3597 state.text <-
3598 Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
3599 max_int
3601 if n != max_int
3602 then (
3603 conf.pagebias <- n;
3604 state.text <- "page bias is now " ^ string_of_int n;
3607 enttext ("page bias: ", E.s, None, intentry, ondone, true)
3609 | Ascii '-' when ctrl ->
3610 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
3611 pivotzoom (max 0.01 (conf.zoom -. decr))
3613 | Ascii '-' ->
3614 let ondone msg = state.text <- msg in
3615 enttext ("option: ", E.s, None,
3616 optentry state.mode, ondone, true)
3618 | Ascii '0' when ctrl ->
3619 if conf.zoom = 1.0
3620 then gotoxy 0 state.y
3621 else setzoom 1.0
3623 | Ascii ('1'|'2' as c) when ctrl && conf.fitmodel != FitPage ->
3624 let cols =
3625 match conf.columns with
3626 | Csingle _ | Cmulti _ -> 1
3627 | Csplit (n, _) -> n
3629 let h = state.winh -
3630 conf.interpagespace lsl (if conf.presentation then 1 else 0)
3632 let zoom = zoomforh state.winw h 0 cols in
3633 if zoom > 0.0 && (c = '2' || zoom < 1.0)
3634 then setzoom zoom
3636 | Ascii '3' when ctrl ->
3637 let fm =
3638 match conf.fitmodel with
3639 | FitWidth -> FitProportional
3640 | FitProportional -> FitPage
3641 | FitPage -> FitWidth
3643 state.text <- "fit model: " ^ FMTE.to_string fm;
3644 reqlayout conf.angle fm
3646 | Ascii '4' when ctrl ->
3647 let zoom = getmaxw () /. float state.winw in
3648 if zoom > 0.0 then setzoom zoom
3650 | Fn 9 ->
3651 togglebirdseye ()
3653 | Ascii '9' when ctrl ->
3654 togglebirdseye ()
3656 | Ascii ('0'..'9' as c) when not ctrl ->
3657 let ondone s =
3658 let n =
3659 try int_of_string s with exn ->
3660 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
3663 if n >= 0
3664 then (
3665 addnav ();
3666 cbput state.hists.pag (string_of_int n);
3667 gotopage1 (n + conf.pagebias - 1) 0;
3670 let pageentry text = function [@warning "-4"]
3671 | Keys.Ascii 'g' -> TEdone text
3672 | key -> intentry text key
3674 let text = String.make 1 c in
3675 enttext (":", text, Some (onhist state.hists.pag),
3676 pageentry, ondone, true)
3678 | Ascii 'b' ->
3679 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
3680 postRedisplay "toggle scrollbar";
3682 | Ascii 'B' ->
3683 state.bzoom <- not state.bzoom;
3684 state.rects <- [];
3685 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
3687 | Ascii 'l' ->
3688 conf.hlinks <- not conf.hlinks;
3689 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
3690 postRedisplay "toggle highlightlinks";
3692 | Ascii 'F' ->
3693 if conf.angle mod 360 = 0
3694 then (
3695 state.glinks <- true;
3696 let mode = state.mode in
3697 state.mode <-
3698 Textentry (
3699 (":", E.s, None, linknentry, linknact gotounder, false),
3700 (fun _ ->
3701 state.glinks <- false;
3702 state.mode <- mode)
3704 state.text <- E.s;
3705 postRedisplay "view:linkent(F)"
3707 else impmsg "hint mode does not work under rotation"
3709 | Ascii 'y' ->
3710 state.glinks <- true;
3711 let mode = state.mode in
3712 state.mode <-
3713 Textentry (
3714 (":", E.s, None, linknentry,
3715 linknact (fun under ->
3716 selstring conf.selcmd (undertext under)), false),
3717 (fun _ ->
3718 state.glinks <- false;
3719 state.mode <- mode)
3721 state.text <- E.s;
3722 postRedisplay "view:linkent"
3724 | Ascii 'a' ->
3725 begin match state.autoscroll with
3726 | Some step ->
3727 conf.autoscrollstep <- step;
3728 state.autoscroll <- None
3729 | None ->
3730 state.autoscroll <- Some conf.autoscrollstep;
3731 state.slideshow <- state.slideshow land lnot 2
3734 | Ascii 'p' when ctrl ->
3735 launchpath () (* XXX where do error messages go? *)
3737 | Ascii 'P' ->
3738 setpresentationmode (not conf.presentation);
3739 showtext ' ' ("presentation mode " ^
3740 if conf.presentation then "on" else "off");
3742 | Ascii 'f' ->
3743 if List.mem Wsi.Fullscreen state.winstate
3744 then Wsi.reshape conf.cwinw conf.cwinh
3745 else Wsi.fullscreen ()
3747 | Ascii ('p'|'N') ->
3748 search state.searchpattern false
3750 | Ascii 'n' | Fn 3 ->
3751 search state.searchpattern true
3753 | Ascii 't' ->
3754 begin match state.layout with
3755 | [] -> ()
3756 | l :: _ ->
3757 gotoxy state.x (getpagey l.pageno)
3760 | Ascii ' ' ->
3761 nextpage ()
3763 | Delete ->
3764 prevpage ()
3766 | Ascii '=' ->
3767 showtext ' ' (describe_layout state.layout);
3769 | Ascii 'w' ->
3770 begin match state.layout with
3771 | [] -> ()
3772 | l :: _ ->
3773 Wsi.reshape l.pagew l.pageh;
3774 postRedisplay "w"
3777 | Ascii '\'' ->
3778 enterbookmarkmode ()
3780 | Ascii 'h' | Fn 1 ->
3781 enterhelpmode ()
3783 | Ascii 'i' ->
3784 enterinfomode ()
3786 | Ascii 'e' when Buffer.length state.errmsgs > 0 ->
3787 entermsgsmode ()
3789 | Ascii 'm' ->
3790 let ondone s =
3791 match state.layout with
3792 | l :: _ ->
3793 if nonemptystr s
3794 then
3795 state.bookmarks <-
3796 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
3797 | _ -> ()
3799 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
3801 | Ascii '~' ->
3802 quickbookmark ();
3803 showtext ' ' "Quick bookmark added";
3805 | Ascii 'x' -> state.roam ()
3807 | Ascii ('<'|'>' as c) ->
3808 reqlayout
3809 (conf.angle + (if c = '>' then 30 else -30)) conf.fitmodel
3811 | Ascii ('['|']' as c) ->
3812 conf.colorscale <-
3813 bound (conf.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0;
3814 postRedisplay "brightness";
3816 | Ascii 'c' when state.mode = View ->
3817 if Wsi.withalt mask
3818 then (
3819 if conf.zoom > 1.0
3820 then
3821 let m = (state.winw - state.w) / 2 in
3822 gotoxy m state.y
3824 else
3825 let (c, a, b), z =
3826 match state.prevcolumns with
3827 | None -> (1, 0, 0), 1.0
3828 | Some (columns, z) ->
3829 let cab =
3830 match columns with
3831 | Csplit (c, _) -> -c, 0, 0
3832 | Cmulti ((c, a, b), _) -> c, a, b
3833 | Csingle _ -> 1, 0, 0
3835 cab, z
3837 setcolumns View c a b;
3838 setzoom z
3840 | Down | Up when ctrl && Wsi.withshift mask ->
3841 let zoom, x = state.prevzoom in
3842 setzoom zoom;
3843 state.x <- x;
3845 | Ascii 'k' | Up ->
3846 begin match state.autoscroll with
3847 | None ->
3848 begin match state.mode with
3849 | Birdseye beye -> upbirdseye 1 beye
3850 | Textentry _ | View | LinkNav _ ->
3851 if ctrl
3852 then gotoxy state.x (clamp ~-(state.winh/2))
3853 else (
3854 if not (Wsi.withshift mask) && conf.presentation
3855 then prevpage ()
3856 else gotoxy state.x (clamp (-conf.scrollstep))
3859 | Some n ->
3860 setautoscrollspeed n false
3863 | Ascii 'j' | Down ->
3864 begin match state.autoscroll with
3865 | None ->
3866 begin match state.mode with
3867 | Birdseye beye -> downbirdseye 1 beye
3868 | Textentry _ | View | LinkNav _ ->
3869 if ctrl
3870 then gotoxy state.x (clamp (state.winh/2))
3871 else (
3872 if not (Wsi.withshift mask) && conf.presentation
3873 then nextpage ()
3874 else gotoxy state.x (clamp (conf.scrollstep))
3877 | Some n ->
3878 setautoscrollspeed n true
3881 | Left | Right when not (Wsi.withalt mask) ->
3882 if canpan ()
3883 then
3884 let dx =
3885 if ctrl
3886 then state.winw / 2
3887 else conf.hscrollstep
3889 let dx =
3890 let pv = Wsi.kc2kt key in
3891 if pv = Keys.Left then dx else -dx
3893 gotoxy (panbound (state.x + dx)) state.y
3894 else (
3895 state.text <- E.s;
3896 postRedisplay "left/right"
3899 | Prior ->
3900 let y =
3901 if ctrl
3902 then
3903 match state.layout with
3904 | [] -> state.y
3905 | l :: _ -> state.y - l.pagey
3906 else
3907 clamp (pgscale (-state.winh))
3909 gotoxy state.x y
3911 | Next ->
3912 let y =
3913 if ctrl
3914 then
3915 match List.rev state.layout with
3916 | [] -> state.y
3917 | l :: _ -> getpagey l.pageno
3918 else
3919 clamp (pgscale state.winh)
3921 gotoxy state.x y
3923 | Ascii 'g' | Home ->
3924 addnav ();
3925 gotoxy 0 0
3926 | Ascii 'G' | End ->
3927 addnav ();
3928 gotoxy 0 (clamp state.maxy)
3930 | Right when Wsi.withalt mask ->
3931 addnavnorc ();
3932 gotoxy state.x (getnav 1)
3933 | Left when Wsi.withalt mask ->
3934 addnavnorc ();
3935 gotoxy state.x (getnav ~-1)
3937 | Ascii 'r' ->
3938 reload ()
3940 | Ascii 'v' when conf.debug ->
3941 state.rects <- [];
3942 List.iter (fun l ->
3943 match getopaque l.pageno with
3944 | None -> ()
3945 | Some opaque ->
3946 let x0, y0, x1, y1 = pagebbox opaque in
3947 let rect = (float x0, float y0,
3948 float x1, float y0,
3949 float x1, float y1,
3950 float x0, float y1) in
3951 debugrect rect;
3952 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
3953 state.rects <- (l.pageno, color, rect) :: state.rects;
3954 ) state.layout;
3955 postRedisplay "v";
3957 | Ascii '|' ->
3958 let mode = state.mode in
3959 let cmd = ref E.s in
3960 let onleave = function
3961 | Cancel -> state.mode <- mode
3962 | Confirm ->
3963 List.iter (fun l ->
3964 match getopaque l.pageno with
3965 | Some opaque -> pipesel opaque !cmd
3966 | None -> ()) state.layout;
3967 state.mode <- mode
3969 let ondone s =
3970 cbput state.hists.sel s;
3971 cmd := s
3973 let te =
3974 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
3976 postRedisplay "|";
3977 state.mode <- Textentry (te, onleave);
3979 | (Ascii _|Fn _|Enter|Left|Right|Code _|Ctrl _) ->
3980 vlog "huh? %s" (Wsi.keyname key)
3983 let linknavkeyboard key mask linknav =
3984 let pv = Wsi.kc2kt key in
3985 let getpage pageno =
3986 let rec loop = function
3987 | [] -> None
3988 | l :: _ when l.pageno = pageno -> Some l
3989 | _ :: rest -> loop rest
3990 in loop state.layout
3992 let doexact (pageno, n) =
3993 match getopaque pageno, getpage pageno with
3994 | Some opaque, Some l ->
3995 if pv = Keys.Enter
3996 then
3997 let under = getlink opaque n in
3998 postRedisplay "link gotounder";
3999 gotounder under;
4000 state.mode <- View;
4001 else
4002 let opt, dir =
4003 let open Keys in
4004 match pv with
4005 | Home -> Some (findlink opaque LDfirst), -1
4006 | End -> Some (findlink opaque LDlast), 1
4007 | Left -> Some (findlink opaque (LDleft n)), -1
4008 | Right -> Some (findlink opaque (LDright n)), 1
4009 | Up -> Some (findlink opaque (LDup n)), -1
4010 | Down -> Some (findlink opaque (LDdown n)), 1
4012 | Delete|Escape|Insert|Enter|Next|Prior|Ascii _
4013 | Code _|Fn _|Ctrl _|Backspace -> None, 0
4015 let pwl l dir =
4016 begin match findpwl l.pageno dir with
4017 | Pwlnotfound -> ()
4018 | Pwl pageno ->
4019 let notfound dir =
4020 state.mode <- LinkNav (Ltgendir dir);
4021 let y, h = getpageyh pageno in
4022 let y =
4023 if dir < 0
4024 then y + h - state.winh
4025 else y
4027 gotoxy state.x y
4029 begin match getopaque pageno, getpage pageno with
4030 | Some opaque, Some _ ->
4031 let link =
4032 let ld = if dir > 0 then LDfirst else LDlast in
4033 findlink opaque ld
4035 begin match link with
4036 | Lfound m ->
4037 showlinktype (getlink opaque m);
4038 state.mode <- LinkNav (Ltexact (pageno, m));
4039 postRedisplay "linknav jpage";
4040 | Lnotfound -> notfound dir
4041 end;
4042 | _ -> notfound dir
4043 end;
4044 end;
4046 begin match opt with
4047 | Some Lnotfound -> pwl l dir;
4048 | Some (Lfound m) ->
4049 if m = n
4050 then pwl l dir
4051 else (
4052 let _, y0, _, y1 = getlinkrect opaque m in
4053 if y0 < l.pagey
4054 then gotopage1 l.pageno y0
4055 else (
4056 let d = fstate.fontsize + 1 in
4057 if y1 - l.pagey > l.pagevh - d
4058 then gotopage1 l.pageno (y1 - state.winh + d)
4059 else postRedisplay "linknav";
4061 showlinktype (getlink opaque m);
4062 state.mode <- LinkNav (Ltexact (l.pageno, m));
4065 | None -> viewkeyboard key mask
4066 end;
4067 | _ -> viewkeyboard key mask
4069 if pv = Keys.Insert
4070 then (
4071 begin match linknav with
4072 | Ltexact pa -> state.lnava <- Some pa
4073 | Ltgendir _ | Ltnotready _ -> ()
4074 end;
4075 state.mode <- View;
4076 postRedisplay "leave linknav"
4078 else
4079 match linknav with
4080 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
4081 | Ltexact exact -> doexact exact
4084 let keyboard key mask =
4085 if (key = Char.code 'g' && Wsi.withctrl mask) && not (istextentry state.mode)
4086 then wcmd "interrupt"
4087 else state.uioh <- state.uioh#key key mask
4090 let birdseyekeyboard key mask
4091 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
4092 let incr =
4093 match conf.columns with
4094 | Csingle _ -> 1
4095 | Cmulti ((c, _, _), _) -> c
4096 | Csplit _ -> failwith "bird's eye split mode"
4098 let pgh layout = List.fold_left
4099 (fun m l -> max l.pageh m) state.winh layout in
4100 let open Keys in
4101 match Wsi.kc2kt key with
4102 | Ascii 'l' when Wsi.withctrl mask ->
4103 let y, h = getpageyh pageno in
4104 let top = (state.winh - h) / 2 in
4105 gotoxy state.x (max 0 (y - top))
4106 | Enter -> leavebirdseye beye false
4107 | Escape -> leavebirdseye beye true
4108 | Up -> upbirdseye incr beye
4109 | Down -> downbirdseye incr beye
4110 | Left -> upbirdseye 1 beye
4111 | Right -> downbirdseye 1 beye
4113 | Prior ->
4114 begin match state.layout with
4115 | l :: _ ->
4116 if l.pagey != 0
4117 then (
4118 state.mode <- Birdseye (
4119 oconf, leftx, l.pageno, hooverpageno, anchor
4121 gotopage1 l.pageno 0;
4123 else (
4124 let layout = layout state.x (state.y-state.winh)
4125 state.winw
4126 (pgh state.layout) in
4127 match layout with
4128 | [] -> gotoxy state.x (clamp (-state.winh))
4129 | l :: _ ->
4130 state.mode <- Birdseye (
4131 oconf, leftx, l.pageno, hooverpageno, anchor
4133 gotopage1 l.pageno 0
4136 | [] -> gotoxy state.x (clamp (-state.winh))
4137 end;
4139 | Next ->
4140 begin match List.rev state.layout with
4141 | l :: _ ->
4142 let layout = layout state.x
4143 (state.y + (pgh state.layout))
4144 state.winw state.winh in
4145 begin match layout with
4146 | [] ->
4147 let incr = l.pageh - l.pagevh in
4148 if incr = 0
4149 then (
4150 state.mode <-
4151 Birdseye (
4152 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
4154 postRedisplay "birdseye pagedown";
4156 else gotoxy state.x (clamp (incr + conf.interpagespace*2));
4158 | l :: _ ->
4159 state.mode <-
4160 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
4161 gotopage1 l.pageno 0;
4164 | [] -> gotoxy state.x (clamp state.winh)
4165 end;
4167 | Home ->
4168 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
4169 gotopage1 0 0
4171 | End ->
4172 let pageno = state.pagecount - 1 in
4173 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
4174 if not (pagevisible state.layout pageno)
4175 then
4176 let h =
4177 match List.rev state.pdims with
4178 | [] -> state.winh
4179 | (_, _, h, _) :: _ -> h
4181 gotoxy
4182 state.x
4183 (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
4184 else postRedisplay "birdseye end";
4186 | Delete|Insert|Ascii _|Code _|Ctrl _|Fn _|Backspace -> viewkeyboard key mask
4189 let drawpage l =
4190 let color =
4191 match state.mode with
4192 | Textentry _ -> scalecolor 0.4
4193 | LinkNav _ | View -> scalecolor 1.0
4194 | Birdseye (_, _, pageno, hooverpageno, _) ->
4195 if l.pageno = hooverpageno
4196 then scalecolor 0.9
4197 else (
4198 if l.pageno = pageno
4199 then (
4200 let c = scalecolor 1.0 in
4201 GlDraw.color c;
4202 GlDraw.line_width 3.0;
4203 let dispx = l.pagedispx in
4204 linerect
4205 (float (dispx-1)) (float (l.pagedispy-1))
4206 (float (dispx+l.pagevw+1))
4207 (float (l.pagedispy+l.pagevh+1));
4208 GlDraw.line_width 1.0;
4211 else scalecolor 0.8
4214 drawtiles l color;
4217 let postdrawpage l linkindexbase =
4218 match getopaque l.pageno with
4219 | Some opaque ->
4220 if tileready l l.pagex l.pagey
4221 then
4222 let x = l.pagedispx - l.pagex
4223 and y = l.pagedispy - l.pagey in
4224 let hlmask =
4225 match conf.columns with
4226 | Csingle _ | Cmulti _ ->
4227 (if conf.hlinks then 1 else 0)
4228 + (if state.glinks
4229 && not (isbirdseye state.mode) then 2 else 0)
4230 | Csplit _ -> 0
4232 let s =
4233 match state.mode with
4234 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
4235 | Textentry _
4236 | Birdseye _
4237 | View
4238 | LinkNav _ -> E.s
4240 Hashtbl.find_all state.prects l.pageno |>
4241 List.iter (fun vals -> drawprect opaque x y vals);
4242 let n = postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize) in
4243 if n < 0
4244 then (Glutils.redisplay := true; 0)
4245 else n
4246 else 0
4247 | _ -> 0
4250 let scrollindicator () =
4251 let sbw, ph, sh = state.uioh#scrollph in
4252 let sbh, pw, sw = state.uioh#scrollpw in
4254 let x0,x1,hx0 =
4255 if conf.leftscroll
4256 then (0, sbw, sbw)
4257 else ((state.winw - sbw), state.winw, 0)
4260 Gl.enable `blend;
4261 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
4262 let (r, g, b, alpha) = conf.sbarcolor in
4263 GlDraw.color (r, g, b) ~alpha;
4264 filledrect (float x0) 0. (float x1) (float state.winh);
4265 filledrect
4266 (float hx0) (float (state.winh - sbh))
4267 (float (hx0 + state.winw)) (float state.winh);
4268 let (r, g, b, alpha) = conf.sbarhndlcolor in
4269 GlDraw.color (r, g, b) ~alpha;
4271 filledrect (float x0) ph (float x1) (ph +. sh);
4272 let pw = pw +. float hx0 in
4273 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
4274 Gl.disable `blend;
4277 let showsel () =
4278 match state.mstate with
4279 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
4282 | Msel ((x0, y0), (x1, y1)) ->
4283 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
4284 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
4285 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
4286 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
4289 let showrects =
4290 function [] -> ()
4291 | rects ->
4292 Gl.enable `blend;
4293 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
4294 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
4295 List.iter
4296 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
4297 List.iter (fun l ->
4298 if l.pageno = pageno
4299 then (
4300 let dx = float (l.pagedispx - l.pagex) in
4301 let dy = float (l.pagedispy - l.pagey) in
4302 let r, g, b, alpha = c in
4303 GlDraw.color (r, g, b) ~alpha;
4304 filledrect2 (x0+.dx) (y0+.dy)
4305 (x1+.dx) (y1+.dy)
4306 (x3+.dx) (y3+.dy)
4307 (x2+.dx) (y2+.dy);
4309 ) state.layout
4310 ) rects;
4311 Gl.disable `blend;
4314 let display () =
4315 GlDraw.color (scalecolor2 conf.bgcolor);
4316 GlClear.color (scalecolor2 conf.bgcolor);
4317 GlClear.clear [`color];
4318 List.iter drawpage state.layout;
4319 let rects =
4320 match state.mode with
4321 | LinkNav (Ltexact (pageno, linkno)) ->
4322 begin match getopaque pageno with
4323 | Some opaque ->
4324 let x0, y0, x1, y1 = getlinkrect opaque linkno in
4325 let color = (0.0, 0.0, 0.5, 0.5) in
4326 (pageno, color,
4327 (float x0, float y0,
4328 float x1, float y0,
4329 float x1, float y1,
4330 float x0, float y1)
4331 ) :: state.rects
4332 | None -> state.rects
4334 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
4335 | Birdseye _
4336 | Textentry _
4337 | View -> state.rects
4339 showrects rects;
4340 let rec postloop linkindexbase = function
4341 | l :: rest ->
4342 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
4343 postloop linkindexbase rest
4344 | [] -> ()
4346 showsel ();
4347 postloop 0 state.layout;
4348 state.uioh#display;
4349 begin match state.mstate with
4350 | Mzoomrect ((x0, y0), (x1, y1)) ->
4351 Gl.enable `blend;
4352 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
4353 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
4354 filledrect (float x0) (float y0) (float x1) (float y1);
4355 Gl.disable `blend;
4356 | Msel _
4357 | Mpan _
4358 | Mscrolly | Mscrollx
4359 | Mzoom _
4360 | Mnone -> ()
4361 end;
4362 enttext ();
4363 scrollindicator ();
4364 Wsi.swapb ();
4367 let zoomrect x y x1 y1 =
4368 let x0 = min x x1
4369 and x1 = max x x1
4370 and y0 = min y y1 in
4371 let zoom = (float state.w) /. float (x1 - x0) in
4372 let margin =
4373 let simple () =
4374 if state.w < state.winw
4375 then (state.winw - state.w) / 2
4376 else 0
4378 match conf.fitmodel with
4379 | FitWidth | FitProportional -> simple ()
4380 | FitPage ->
4381 match conf.columns with
4382 | Csplit _ ->
4383 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
4384 | Cmulti _ | Csingle _ -> simple ()
4386 gotoxy ((state.x + margin) - x0) (state.y + y0);
4387 state.anchor <- getanchor ();
4388 setzoom zoom;
4389 resetmstate ();
4392 let annot inline x y =
4393 match unproject x y with
4394 | Some (opaque, n, ux, uy) ->
4395 let add text =
4396 addannot opaque ux uy text;
4397 wcmd "freepage %s" (~> opaque);
4398 Hashtbl.remove state.pagemap (n, state.gen);
4399 flushtiles ();
4400 gotoxy state.x state.y
4402 if inline
4403 then
4404 let ondone s = add s in
4405 let mode = state.mode in
4406 state.mode <- Textentry (
4407 ("annotation: ", E.s, None, textentry, ondone, true),
4408 fun _ -> state.mode <- mode);
4409 state.text <- E.s;
4410 enttext ();
4411 postRedisplay "annot"
4412 else
4413 add @@ getusertext E.s
4414 | _ -> ()
4417 let zoomblock x y =
4418 let g opaque l px py =
4419 match rectofblock opaque px py with
4420 | Some a ->
4421 let x0 = a.(0) -. 20. in
4422 let x1 = a.(1) +. 20. in
4423 let y0 = a.(2) -. 20. in
4424 let zoom = (float state.w) /. (x1 -. x0) in
4425 let pagey = getpagey l.pageno in
4426 let margin = (state.w - l.pagew)/2 in
4427 let nx = -truncate x0 - margin in
4428 gotoxy nx (pagey + truncate y0);
4429 state.anchor <- getanchor ();
4430 setzoom zoom;
4431 None
4432 | None -> None
4434 match conf.columns with
4435 | Csplit _ ->
4436 impmsg "block zooming does not work properly in split columns mode"
4437 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
4440 let scrollx x =
4441 let winw = state.winw - 1 in
4442 let s = float x /. float winw in
4443 let destx = truncate (float (state.w + winw) *. s) in
4444 gotoxy (winw - destx) state.y;
4445 state.mstate <- Mscrollx;
4448 let scrolly y =
4449 let s = float y /. float state.winh in
4450 let desty = truncate (s *. float (maxy ())) in
4451 gotoxy state.x desty;
4452 state.mstate <- Mscrolly;
4455 let viewmulticlick clicks x y mask =
4456 let g opaque l px py =
4457 let mark =
4458 match clicks with
4459 | 2 -> Mark_word
4460 | 3 -> Mark_line
4461 | 4 -> Mark_block
4462 | _ -> Mark_page
4464 if markunder opaque px py mark
4465 then (
4466 Some (fun () ->
4467 let dopipe cmd =
4468 match getopaque l.pageno with
4469 | None -> ()
4470 | Some opaque -> pipesel opaque cmd
4472 state.roam <- (fun () -> dopipe conf.paxcmd);
4473 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
4476 else None
4478 postRedisplay "viewmulticlick";
4479 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
4482 let canselect () =
4483 match conf.columns with
4484 | Csplit _ -> false
4485 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
4488 let viewmouse button down x y mask =
4489 match button with
4490 | n when (n == 4 || n == 5) && not down ->
4491 if Wsi.withctrl mask
4492 then (
4493 let incr =
4494 if n = 5
4495 then if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
4496 else if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
4498 let fx, fy =
4499 match state.mstate with
4500 | Mzoom (oldn, _, pos) when n = oldn -> pos
4501 | Mzoomrect _ | Mnone | Mpan _
4502 | Msel _ | Mscrollx | Mscrolly | Mzoom _ -> (x, y)
4504 let zoom = conf.zoom -. incr in
4505 state.mstate <- Mzoom (n, 0, (x, y));
4506 if false && abs (fx - x) > 5 || abs (fy - y) > 5
4507 then pivotzoom ~x ~y zoom
4508 else pivotzoom zoom
4510 else (
4511 match state.autoscroll with
4512 | Some step -> setautoscrollspeed step (n=4)
4513 | None ->
4514 if conf.wheelbypage || conf.presentation
4515 then (
4516 if n = 4
4517 then prevpage ()
4518 else nextpage ()
4520 else
4521 let incr =
4522 if n = 4
4523 then -conf.scrollstep
4524 else conf.scrollstep
4526 let incr = incr * 2 in
4527 let y = clamp incr in
4528 gotoxy state.x y
4531 | n when (n = 6 || n = 7) && not down && canpan () ->
4532 let x =
4533 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep) in
4534 gotoxy x state.y
4536 | 1 when Wsi.withshift mask ->
4537 state.mstate <- Mnone;
4538 if not down
4539 then (
4540 match unproject x y with
4541 | None -> ()
4542 | Some (_, pageno, ux, uy) ->
4543 let cmd = Printf.sprintf
4544 "%s %s %d %d %d"
4545 conf.stcmd state.path pageno ux uy
4547 match spawn cmd [] with
4548 | exception exn ->
4549 impmsg "execution of synctex command(%S) failed: %S"
4550 conf.stcmd @@ exntos exn
4551 | _pid -> ()
4554 | 1 when Wsi.withctrl mask ->
4555 if down
4556 then (
4557 Wsi.setcursor Wsi.CURSOR_FLEUR;
4558 state.mstate <- Mpan (x, y)
4560 else
4561 state.mstate <- Mnone
4563 | 3 ->
4564 if down
4565 then (
4566 if Wsi.withshift mask
4567 then (
4568 annot conf.annotinline x y;
4569 postRedisplay "addannot"
4571 else
4572 let p = (x, y) in
4573 Wsi.setcursor Wsi.CURSOR_CYCLE;
4574 state.mstate <- Mzoomrect (p, p)
4576 else (
4577 match state.mstate with
4578 | Mzoomrect ((x0, y0), _) ->
4579 if abs (x-x0) > 10 && abs (y - y0) > 10
4580 then zoomrect x0 y0 x y
4581 else (
4582 resetmstate ();
4583 postRedisplay "kill accidental zoom rect";
4585 | Msel _
4586 | Mpan _
4587 | Mscrolly | Mscrollx
4588 | Mzoom _
4589 | Mnone -> resetmstate ()
4592 | 1 when vscrollhit x ->
4593 if down
4594 then
4595 let _, position, sh = state.uioh#scrollph in
4596 if y > truncate position && y < truncate (position +. sh)
4597 then state.mstate <- Mscrolly
4598 else scrolly y
4599 else
4600 state.mstate <- Mnone
4602 | 1 when y > state.winh - hscrollh () ->
4603 if down
4604 then
4605 let _, position, sw = state.uioh#scrollpw in
4606 if x > truncate position && x < truncate (position +. sw)
4607 then state.mstate <- Mscrollx
4608 else scrollx x
4609 else
4610 state.mstate <- Mnone
4612 | 1 when state.bzoom -> if not down then zoomblock x y
4614 | 1 ->
4615 let dest = if down then getunder x y else Unone in
4616 begin match dest with
4617 | Ulinkuri _ ->
4618 gotounder dest
4620 | Unone when down ->
4621 Wsi.setcursor Wsi.CURSOR_FLEUR;
4622 state.mstate <- Mpan (x, y);
4624 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4626 | Unone | Utext _ ->
4627 if down
4628 then (
4629 if canselect ()
4630 then (
4631 state.mstate <- Msel ((x, y), (x, y));
4632 postRedisplay "mouse select";
4635 else (
4636 match state.mstate with
4637 | Mnone -> ()
4639 | Mzoom _ | Mscrollx | Mscrolly ->
4640 state.mstate <- Mnone
4642 | Mzoomrect ((x0, y0), _) ->
4643 zoomrect x0 y0 x y
4645 | Mpan _ ->
4646 Wsi.setcursor Wsi.CURSOR_INHERIT;
4647 state.mstate <- Mnone
4649 | Msel ((x0, y0), (x1, y1)) ->
4650 let rec loop = function
4651 | [] -> ()
4652 | l :: rest ->
4653 let inside =
4654 let a0 = l.pagedispy in
4655 let a1 = a0 + l.pagevh in
4656 let b0 = l.pagedispx in
4657 let b1 = b0 + l.pagevw in
4658 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
4659 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
4661 if inside
4662 then
4663 match getopaque l.pageno with
4664 | Some opaque ->
4665 let dosel cmd () =
4666 pipef ~closew:false "Msel"
4667 (fun w ->
4668 copysel w opaque;
4669 postRedisplay "Msel") cmd
4671 dosel conf.selcmd ();
4672 state.roam <- dosel conf.paxcmd;
4673 | None -> ()
4674 else loop rest
4676 loop state.layout;
4677 resetmstate ();
4681 | _ -> ()
4684 let birdseyemouse button down x y mask
4685 (conf, leftx, _, hooverpageno, anchor) =
4686 match button with
4687 | 1 when down ->
4688 let rec loop = function
4689 | [] -> ()
4690 | l :: rest ->
4691 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4692 && x > l.pagedispx && x < l.pagedispx + l.pagevw
4693 then (
4694 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
4696 else loop rest
4698 loop state.layout
4699 | 3 -> ()
4700 | _ -> viewmouse button down x y mask
4703 let uioh = object
4704 method display = ()
4706 method key key mask =
4707 begin match state.mode with
4708 | Textentry textentry -> textentrykeyboard key mask textentry
4709 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
4710 | View -> viewkeyboard key mask
4711 | LinkNav linknav -> linknavkeyboard key mask linknav
4712 end;
4713 state.uioh
4715 method button button bstate x y mask =
4716 begin match state.mode with
4717 | LinkNav _ | View -> viewmouse button bstate x y mask
4718 | Birdseye beye -> birdseyemouse button bstate x y mask beye
4719 | Textentry _ -> ()
4720 end;
4721 state.uioh
4723 method multiclick clicks x y mask =
4724 begin match state.mode with
4725 | LinkNav _ | View -> viewmulticlick clicks x y mask
4726 | Birdseye _ | Textentry _ -> ()
4727 end;
4728 state.uioh
4730 method motion x y =
4731 begin match state.mode with
4732 | Textentry _ -> ()
4733 | View | Birdseye _ | LinkNav _ ->
4734 match state.mstate with
4735 | Mzoom _ | Mnone -> ()
4737 | Mpan (x0, y0) ->
4738 let dx = x - x0
4739 and dy = y0 - y in
4740 state.mstate <- Mpan (x, y);
4741 let x = if canpan () then panbound (state.x + dx) else state.x in
4742 let y = clamp dy in
4743 gotoxy x y
4745 | Msel (a, _) ->
4746 state.mstate <- Msel (a, (x, y));
4747 postRedisplay "motion select";
4749 | Mscrolly ->
4750 let y = min state.winh (max 0 y) in
4751 scrolly y
4753 | Mscrollx ->
4754 let x = min state.winw (max 0 x) in
4755 scrollx x
4757 | Mzoomrect (p0, _) ->
4758 state.mstate <- Mzoomrect (p0, (x, y));
4759 postRedisplay "motion zoomrect";
4760 end;
4761 state.uioh
4763 method pmotion x y =
4764 begin match state.mode with
4765 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
4766 let rec loop = function
4767 | [] ->
4768 if hooverpageno != -1
4769 then (
4770 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
4771 postRedisplay "pmotion birdseye no hoover";
4773 | l :: rest ->
4774 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4775 && x > l.pagedispx && x < l.pagedispx + l.pagevw
4776 then (
4777 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
4778 postRedisplay "pmotion birdseye hoover";
4780 else loop rest
4782 loop state.layout
4784 | Textentry _ -> ()
4786 | LinkNav _ | View ->
4787 match state.mstate with
4788 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
4789 | Mnone ->
4790 updateunder x y;
4791 if canselect ()
4792 then
4793 match conf.pax with
4794 | None -> ()
4795 | Some past ->
4796 let now = now () in
4797 let delta = now -. past in
4798 if delta > 0.01
4799 then paxunder x y
4800 else conf.pax <- Some now
4801 end;
4802 state.uioh
4804 method infochanged _ = ()
4806 method scrollph =
4807 let maxy = maxy () in
4808 let p, h =
4809 if maxy = 0
4810 then 0.0, float state.winh
4811 else scrollph state.y maxy
4813 vscrollw (), p, h
4815 method scrollpw =
4816 let fwinw = float (state.winw - vscrollw ()) in
4817 let sw =
4818 let sw = fwinw /. float state.w in
4819 let sw = fwinw *. sw in
4820 max sw (float conf.scrollh)
4822 let position =
4823 let maxx = state.w + state.winw in
4824 let x = state.winw - state.x in
4825 let percent = float x /. float maxx in
4826 (fwinw -. sw) *. percent
4828 hscrollh (), position, sw
4830 method modehash =
4831 let modename =
4832 match state.mode with
4833 | LinkNav _ -> "links"
4834 | Textentry _ -> "textentry"
4835 | Birdseye _ -> "birdseye"
4836 | View -> "view"
4838 findkeyhash conf modename
4840 method eformsgs = true
4841 method alwaysscrolly = false
4842 method scroll dx dy =
4843 let x = if canpan () then panbound (state.x + dx) else state.x in
4844 gotoxy x (clamp (2 * dy));
4845 state.uioh
4846 method zoom z x y =
4847 pivotzoom ~x ~y (conf.zoom *. exp z);
4848 end;;
4850 let addrect pageno r g b a x0 y0 x1 y1 =
4851 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
4854 let ract cmds =
4855 let cl = splitatchar cmds ' ' in
4856 let scan s fmt f =
4857 try Scanf.sscanf s fmt f
4858 with exn -> adderrfmt "remote exec" "error processing '%S': %s\n"
4859 cmds @@ exntos exn
4861 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
4862 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
4863 s pageno r g b a x0 y0 x1 y1;
4864 onpagerect
4865 pageno
4866 (fun w h ->
4867 let _,w1,h1,_ = getpagedim pageno in
4868 let sw = float w1 /. float w
4869 and sh = float h1 /. float h in
4870 let x0s = x0 *. sw
4871 and x1s = x1 *. sw
4872 and y0s = y0 *. sh
4873 and y1s = y1 *. sh in
4874 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
4875 let color = (r, g, b, a) in
4876 if conf.verbose then debugrect rect;
4877 state.rects <- (pageno, color, rect) :: state.rects;
4878 postRedisplay s;
4881 match cl with
4882 | "reload", "" -> reload ()
4883 | "goto", args ->
4884 scan args "%u %f %f"
4885 (fun pageno x y ->
4886 let cmd, _ = state.geomcmds in
4887 if emptystr cmd
4888 then gotopagexy pageno x y
4889 else
4890 let f prevf () =
4891 gotopagexy pageno x y;
4892 prevf ()
4894 state.reprf <- f state.reprf
4896 | "goto1", args -> scan args "%u %f" gotopage
4897 | "gotor", args -> scan args "%S" gotoremote
4898 | "rect", args ->
4899 scan args "%u %u %f %f %f %f"
4900 (fun pageno c x0 y0 x1 y1 ->
4901 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
4902 rectx "rect" pageno color x0 y0 x1 y1;
4904 | "prect", args ->
4905 scan args "%u %f %f %f %f %f %f %f %f"
4906 (fun pageno r g b alpha x0 y0 x1 y1 ->
4907 addrect pageno r g b alpha x0 y0 x1 y1;
4908 postRedisplay "prect"
4910 | "pgoto", args ->
4911 scan args "%u %f %f"
4912 (fun pageno x y ->
4913 let optopaque =
4914 match getopaque pageno with
4915 | Some opaque -> opaque
4916 | None -> ~< E.s
4918 pgoto optopaque pageno x y;
4919 let rec fixx = function
4920 | [] -> ()
4921 | l :: rest ->
4922 if l.pageno = pageno
4923 then gotoxy (state.x - l.pagedispx) state.y
4924 else fixx rest
4926 let layout =
4927 let mult =
4928 match conf.columns with
4929 | Csingle _ | Csplit _ -> 1
4930 | Cmulti ((n, _, _), _) -> n
4932 layout 0 state.y (state.winw * mult) state.winh
4934 fixx layout
4936 | "activatewin", "" -> Wsi.activatewin ()
4937 | "quit", "" -> raise Quit
4938 | "keys", keys ->
4939 begin try
4940 let l = Config.keys_of_string keys in
4941 List.iter (fun (k, m) -> keyboard k m) l
4942 with exn -> adderrfmt "error processing keys" "`%S': %s\n"
4943 cmds @@ exntos exn
4945 | "clearrects", "" ->
4946 Hashtbl.clear state.prects;
4947 postRedisplay "clearrects"
4948 | _ ->
4949 adderrfmt "remote command"
4950 "error processing remote command: %S\n" cmds;
4953 let remote =
4954 let scratch = Bytes.create 80 in
4955 let buf = Buffer.create 80 in
4956 fun fd ->
4957 match tempfailureretry (Unix.read fd scratch 0) 80 with
4958 | exception Unix.Unix_error (Unix.EAGAIN, _, _) -> None
4959 | 0 ->
4960 Unix.close fd;
4961 if Buffer.length buf > 0
4962 then (
4963 let s = Buffer.contents buf in
4964 Buffer.clear buf;
4965 ract s;
4967 None
4968 | n ->
4969 let rec eat ppos =
4970 let nlpos =
4971 match Bytes.index_from scratch ppos '\n' with
4972 | pos -> if pos >= n then -1 else pos
4973 | exception Not_found -> -1
4975 if nlpos >= 0
4976 then (
4977 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
4978 let s = Buffer.contents buf in
4979 Buffer.clear buf;
4980 ract s;
4981 eat (nlpos+1);
4983 else (
4984 Buffer.add_subbytes buf scratch ppos (n-ppos);
4985 Some fd
4987 in eat 0
4990 let remoteopen path =
4991 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
4992 with exn ->
4993 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
4994 None
4997 let () =
4998 let gcconfig = ref false in
4999 let trimcachepath = ref E.s in
5000 let rcmdpath = ref E.s in
5001 let pageno = ref None in
5002 let openlast = ref false in
5003 let doreap = ref false in
5004 let csspath = ref None in
5005 selfexec := Sys.executable_name;
5006 Arg.parse
5007 (Arg.align
5008 [("-p", Arg.String (fun s -> state.password <- s),
5009 "<password> Set password");
5011 ("-f", Arg.String
5012 (fun s ->
5013 Config.fontpath := s;
5014 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
5016 "<path> Set path to the user interface font");
5018 ("-c", Arg.String
5019 (fun s ->
5020 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
5021 Config.confpath := s),
5022 "<path> Set path to the configuration file");
5024 ("-last", Arg.Set openlast, " Open last document");
5026 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
5027 "<page-number> Jump to page");
5029 ("-tcf", Arg.String (fun s -> trimcachepath := s),
5030 "<path> Set path to the trim cache file");
5032 ("-dest", Arg.String (fun s -> state.nameddest <- s),
5033 "<named-destination> Set named destination");
5035 ("-remote", Arg.String (fun s -> rcmdpath := s),
5036 "<path> Set path to the source of remote commands");
5038 ("-gc", Arg.Set gcconfig, " Collect config garbage");
5040 ("-v", Arg.Unit (fun () ->
5041 Printf.printf
5042 "%s\nconfiguration file: %s\n"
5043 (Help.version ())
5044 Config.defconfpath;
5045 exit 0), " Print version and exit");
5047 ("-css", Arg.String (fun s -> csspath := Some s),
5048 "<path> Set path to the style sheet to use with EPUB/HTML");
5050 ("-origin", Arg.String (fun s -> state.origin <- s),
5051 "<origin> <undocumented>");
5053 ("-no-title", Arg.Set ignoredoctitlte, " ignore document title");
5054 ("-layout-height", Arg.Set_int layouth,
5055 "<height> layout height html/epub/etc (-1, 0, N)");
5058 (fun s -> state.path <- s)
5059 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:");
5061 let histmode = emptystr state.path && not !openlast in
5063 if not (Config.load !openlast)
5064 then dolog "failed to load configuration";
5066 begin match !pageno with
5067 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
5068 | None -> ()
5069 end;
5071 fillhelp ();
5072 if !gcconfig
5073 then (
5074 Config.gc ();
5075 exit 0
5078 let mu =
5079 object (self)
5080 val mutable m_clicks = 0
5081 val mutable m_click_x = 0
5082 val mutable m_click_y = 0
5083 val mutable m_lastclicktime = infinity
5085 method private cleanup =
5086 state.roam <- noroam;
5087 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
5088 method expose = postRedisplay "expose"
5089 method visible v =
5090 let name =
5091 match v with
5092 | Wsi.Unobscured -> "unobscured"
5093 | Wsi.PartiallyObscured -> "partiallyobscured"
5094 | Wsi.FullyObscured -> "fullyobscured"
5096 vlog "visibility change %s" name
5097 method display = display ()
5098 method map mapped = vlog "mapped %b" mapped
5099 method reshape w h =
5100 self#cleanup;
5101 reshape w h
5102 method mouse b d x y m =
5103 if d && canselect ()
5104 then (
5106 * http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx
5108 m_click_x <- x;
5109 m_click_y <- y;
5110 if b = 1
5111 then (
5112 let t = now () in
5113 if abs x - m_click_x > 10
5114 || abs y - m_click_y > 10
5115 || abs_float (t -. m_lastclicktime) > 0.3
5116 then m_clicks <- 0;
5117 m_clicks <- m_clicks + 1;
5118 m_lastclicktime <- t;
5119 if m_clicks = 1
5120 then (
5121 self#cleanup;
5122 postRedisplay "cleanup";
5123 state.uioh <- state.uioh#button b d x y m;
5125 else state.uioh <- state.uioh#multiclick m_clicks x y m
5127 else (
5128 self#cleanup;
5129 m_clicks <- 0;
5130 m_lastclicktime <- infinity;
5131 state.uioh <- state.uioh#button b d x y m
5134 else (
5135 state.uioh <- state.uioh#button b d x y m
5137 method motion x y =
5138 state.mpos <- (x, y);
5139 state.uioh <- state.uioh#motion x y
5140 method pmotion x y =
5141 state.mpos <- (x, y);
5142 state.uioh <- state.uioh#pmotion x y
5143 method key k m =
5144 vlog "k=%#x m=%#x" k m;
5145 let mascm = m land (
5146 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
5147 ) in
5148 let keyboard k m =
5149 let x = state.x and y = state.y in
5150 keyboard k m;
5151 if x != state.x || y != state.y then self#cleanup
5153 match state.keystate with
5154 | KSnone ->
5155 let km = k, mascm in
5156 begin
5157 match
5158 let modehash = state.uioh#modehash in
5159 try Hashtbl.find modehash km
5160 with Not_found ->
5161 try Hashtbl.find (findkeyhash conf "global") km
5162 with Not_found -> KMinsrt (k, m)
5163 with
5164 | KMinsrt (k, m) -> keyboard k m
5165 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
5166 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
5168 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
5169 List.iter (fun (k, m) -> keyboard k m) insrt;
5170 state.keystate <- KSnone
5171 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
5172 state.keystate <- KSinto (keys, insrt)
5173 | KSinto _ -> state.keystate <- KSnone
5175 method enter x y =
5176 state.mpos <- (x, y);
5177 state.uioh <- state.uioh#pmotion x y
5178 method leave = state.mpos <- (-1, -1)
5179 method winstate wsl = state.winstate <- wsl
5180 method quit : 'a. 'a = raise Quit
5181 method scroll dx dy = state.uioh <- state.uioh#scroll dx dy
5182 method zoom z x y = state.uioh#zoom z x y
5183 method opendoc path =
5184 state.mode <- View;
5185 state.uioh <- uioh;
5186 postRedisplay "opendoc";
5187 opendoc path state.password
5190 let wsfd, winw, winh = Wsi.init mu conf.cwinw conf.cwinh platform in
5191 state.wsfd <- wsfd;
5193 if not @@ List.exists GlMisc.check_extension
5194 [ "GL_ARB_texture_rectangle"
5195 ; "GL_EXT_texture_recangle"
5196 ; "GL_NV_texture_rectangle" ]
5197 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
5199 if substratis (GlMisc.get_string `renderer) 0 "Mesa DRI Intel("
5200 then (
5201 defconf.sliceheight <- 1024;
5202 defconf.texcount <- 32;
5203 defconf.usepbo <- true;
5206 let cs, ss =
5207 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
5208 | exception exn ->
5209 dolog "socketpair failed: %s" @@ exntos exn;
5210 exit 1
5211 | (r, w) ->
5212 cloexec r;
5213 cloexec w;
5214 r, w
5217 setcheckers conf.checkers;
5219 opengl_has_pbo := GlMisc.check_extension "GL_ARB_pixel_buffer_object";
5221 begin match !csspath with
5222 | None -> ()
5223 | Some "" -> conf.css <- E.s
5224 | Some path ->
5225 let css = filecontents path in
5226 let l = String.length css in
5227 conf.css <-
5228 if substratis css (l-2) "\r\n"
5229 then String.sub css 0 (l-2)
5230 else (if css.[l-1] = '\n'
5231 then String.sub css 0 (l-1)
5232 else css);
5233 end;
5234 init cs (
5235 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
5236 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
5237 !Config.fontpath, !trimcachepath, !opengl_has_pbo
5239 List.iter GlArray.enable [`texture_coord; `vertex];
5240 state.ss <- ss;
5241 reshape ~firsttime:true winw winh;
5242 state.uioh <- uioh;
5243 if histmode
5244 then (
5245 Wsi.settitle "llpp (history)";
5246 enterhistmode ();
5248 else (
5249 state.text <- "Opening " ^ (mbtoutf8 state.path);
5250 opendoc state.path state.password;
5252 display ();
5253 Wsi.mapwin ();
5254 Wsi.setcursor Wsi.CURSOR_INHERIT;
5255 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
5257 let rec reap () =
5258 match Unix.waitpid [Unix.WNOHANG] ~-1 with
5259 | exception (Unix.Unix_error (Unix.ECHILD, _, _)) -> ()
5260 | exception exn -> dolog "Unix.waitpid: %s" @@ exntos exn
5261 | 0, _ -> ()
5262 | _pid, _status -> reap ()
5264 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
5266 let optrfd =
5267 ref (
5268 if nonemptystr !rcmdpath
5269 then remoteopen !rcmdpath
5270 else None
5274 let rec loop deadline =
5275 if !doreap
5276 then (
5277 doreap := false;
5278 reap ()
5280 let r = [state.ss; state.wsfd] in
5281 let r =
5282 match !optrfd with
5283 | None -> r
5284 | Some fd -> fd :: r
5286 if !redisplay
5287 then (
5288 Glutils.redisplay := false;
5289 display ();
5291 let timeout =
5292 let now = now () in
5293 if deadline > now
5294 then (
5295 if deadline = infinity
5296 then ~-.1.0
5297 else max 0.0 (deadline -. now)
5299 else 0.0
5301 let r, _, _ =
5302 try Unix.select r [] [] timeout
5303 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
5305 begin match r with
5306 | [] ->
5307 let newdeadline =
5308 match state.autoscroll with
5309 | Some step when step != 0 ->
5310 if state.slideshow land 1 = 1
5311 then (
5312 if state.slideshow land 2 = 0
5313 then state.slideshow <- state.slideshow lor 2
5314 else if step < 0 then prevpage () else nextpage ();
5315 deadline +. (float (abs step))
5317 else
5318 let y = state.y + step in
5319 let fy = if conf.maxhfit then state.winh else 0 in
5320 let y =
5321 if y < 0
5322 then state.maxy - fy
5323 else if y >= state.maxy - fy then 0 else y
5325 gotoxy state.x y;
5326 deadline +. 0.01
5327 | _ -> infinity
5329 loop newdeadline
5331 | l ->
5332 let rec checkfds = function
5333 | [] -> ()
5334 | fd :: rest when fd = state.ss ->
5335 let cmd = rcmd state.ss in
5336 act cmd;
5337 checkfds rest
5339 | fd :: rest when fd = state.wsfd ->
5340 Wsi.readresp fd;
5341 checkfds rest
5343 | fd :: rest when Some fd = !optrfd ->
5344 begin match remote fd with
5345 | None -> optrfd := remoteopen !rcmdpath;
5346 | opt -> optrfd := opt
5347 end;
5348 checkfds rest
5350 | _ :: rest ->
5351 dolog "select returned unknown descriptor";
5352 checkfds rest
5354 checkfds l;
5355 let newdeadline =
5356 let deadline1 =
5357 if deadline = infinity
5358 then now () +. 0.01
5359 else deadline
5361 match state.autoscroll with
5362 | Some step when step != 0 -> deadline1
5363 | _ -> infinity
5365 loop newdeadline
5366 end;
5368 match loop infinity with
5369 | exception Quit ->
5370 Config.save leavebirdseye;
5371 if hasunsavedchanges ()
5372 then save ()
5373 | _ -> error "umpossible - infinity reached"