Update
[llpp.git] / main.ml
blobe79cc9cb0a5b62b40a3c27defb22babf02fb9200
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 = Str.global_replace percentsre state.path conf.pathlauncher in
98 match spawn command [] with
99 | _pid -> ()
100 | exception exn ->
101 dolog "failed to execute `%s': %s" command @@ exntos exn
105 let getopaque pageno =
106 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
107 with Not_found -> None
110 let pagetranslatepoint l x y =
111 let dy = y - l.pagedispy in
112 let y = dy + l.pagey in
113 let dx = x - l.pagedispx in
114 let x = dx + l.pagex in
115 (x, y);
118 let onppundermouse g x y d =
119 let rec f = function
120 | l :: rest ->
121 begin match getopaque l.pageno with
122 | Some opaque ->
123 let x0 = l.pagedispx in
124 let x1 = x0 + l.pagevw in
125 let y0 = l.pagedispy in
126 let y1 = y0 + l.pagevh in
127 if y >= y0 && y <= y1 && x >= x0 && x <= x1
128 then
129 let px, py = pagetranslatepoint l x y in
130 match g opaque l px py with
131 | Some res -> res
132 | None -> f rest
133 else f rest
134 | _ ->
135 f rest
137 | [] -> d
139 f state.layout
142 let getunder x y =
143 let g opaque l px py =
144 if state.bzoom
145 then (
146 match rectofblock opaque px py with
147 | Some [|x0;x1;y0;y1|] ->
148 let rect = (x0, y0, x1, y0, x1, y1, x0, y1) in
149 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
150 state.rects <- [l.pageno, color, rect];
151 postRedisplay "getunder";
152 | _ -> ()
154 let under = whatsunder opaque px py in
155 if under = Unone then None else Some under
157 onppundermouse g x y Unone
160 let unproject x y =
161 let g opaque l x y =
162 match unproject opaque x y with
163 | Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
164 | None -> None
166 onppundermouse g x y None;
169 let showtext c s =
170 state.text <- Printf.sprintf "%c%s" c s;
171 postRedisplay "showtext";
174 let impmsg fmt =
175 Format.ksprintf (fun s -> showtext '!' s) fmt;
178 let pipesel opaque cmd =
179 if hassel opaque
180 then pipef ~closew:false "pipesel"
181 (fun w ->
182 copysel w opaque;
183 postRedisplay "pipesel"
184 ) cmd
187 let paxunder x y =
188 let g opaque l px py =
189 if markunder opaque px py conf.paxmark
190 then (
191 Some (fun () ->
192 match getopaque l.pageno with
193 | None -> ()
194 | Some opaque -> pipesel opaque conf.paxcmd
197 else None
199 postRedisplay "paxunder";
200 if conf.paxmark = Mark_page
201 then
202 List.iter (fun l ->
203 match getopaque l.pageno with
204 | None -> ()
205 | Some opaque -> clearmark opaque) state.layout;
206 state.roam <- onppundermouse g x y (fun () -> impmsg "whoopsie daisy");
209 let undertext = function
210 | Unone -> "none"
211 | Ulinkuri s -> s
212 | Utext s -> "font: " ^ s
213 | Uannotation (opaque, slinkindex) ->
214 "annotation: " ^ getannotcontents opaque slinkindex
217 let updateunder x y =
218 match getunder x y with
219 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
220 | Ulinkuri uri ->
221 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
222 Wsi.setcursor Wsi.CURSOR_INFO
223 | Utext s ->
224 if conf.underinfo then showtext 'f' ("ont: " ^ s);
225 Wsi.setcursor Wsi.CURSOR_TEXT
226 | Uannotation _ ->
227 if conf.underinfo then showtext 'a' "nnotation";
228 Wsi.setcursor Wsi.CURSOR_INFO
231 let showlinktype under =
232 if conf.underinfo && under != Unone
233 then showtext ' ' @@ undertext under
236 let intentry_with_suffix text key =
237 let text =
238 match [@warning "-4"] key with
239 | Keys.Ascii ('0'..'9' as c) -> addchar text c
240 | Keys.Ascii ('k' | 'm' | 'g' | 'K' | 'M' | 'G' as c) ->
241 addchar text @@ asciilower c
242 | _ ->
243 state.text <- Printf.sprintf "invalid key";
244 text
246 TEcont text
249 let wcmd fmt =
250 let b = Buffer.create 16 in
251 Printf.kbprintf
252 (fun b ->
253 let b = Buffer.to_bytes b in
254 wcmd state.ss b @@ Bytes.length b
255 ) b fmt
258 let nogeomcmds cmds =
259 match cmds with
260 | s, [] -> emptystr s
261 | _ -> false
264 let layoutN ((columns, coverA, coverB), b) x y sw sh =
265 let rec fold accu n =
266 if n = Array.length b
267 then accu
268 else
269 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
270 if (vy - y) > sh &&
271 (n = coverA - 1
272 || n = state.pagecount - coverB
273 || (n - coverA) mod columns = columns - 1)
274 then accu
275 else
276 let accu =
277 if vy + h > y
278 then
279 let pagey = max 0 (y - vy) in
280 let pagedispy = if pagey > 0 then 0 else vy - y in
281 let pagedispx, pagex =
282 let pdx =
283 if n = coverA - 1 || n = state.pagecount - coverB
284 then x + (sw - w) / 2
285 else dx + xoff + x
287 if pdx < 0
288 then 0, -pdx
289 else pdx, 0
291 let pagevw =
292 let vw = sw - pagedispx in
293 let pw = w - pagex in
294 min vw pw
296 let pagevh = min (h - pagey) (sh - pagedispy) in
297 if pagevw > 0 && pagevh > 0
298 then
299 let e =
300 { pageno = n
301 ; pagedimno = pdimno
302 ; pagew = w
303 ; pageh = h
304 ; pagex = pagex
305 ; pagey = pagey
306 ; pagevw = pagevw
307 ; pagevh = pagevh
308 ; pagedispx = pagedispx
309 ; pagedispy = pagedispy
310 ; pagecol = 0
313 e :: accu
314 else
315 accu
316 else
317 accu
319 fold accu (n+1)
321 if Array.length b = 0
322 then []
323 else List.rev (fold [] (page_of_y y))
326 let layoutS (columns, b) x y sw sh =
327 let rec fold accu n =
328 if n = Array.length b
329 then accu
330 else
331 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
332 if (vy - y) > sh
333 then accu
334 else
335 let accu =
336 if vy + pageh > y
337 then
338 let x = xoff + x in
339 let pagey = max 0 (y - vy) in
340 let pagedispy = if pagey > 0 then 0 else vy - y in
341 let pagedispx, pagex =
342 if px = 0
343 then (
344 if x < 0
345 then 0, -x
346 else x, 0
348 else (
349 let px = px - x in
350 if px < 0
351 then -px, 0
352 else 0, px
355 let pagecolw = pagew/columns in
356 let pagedispx =
357 if pagecolw < sw
358 then pagedispx + ((sw - pagecolw) / 2)
359 else pagedispx
361 let pagevw =
362 let vw = sw - pagedispx in
363 let pw = pagew - pagex in
364 min vw pw
366 let pagevw = min pagevw pagecolw in
367 let pagevh = min (pageh - pagey) (sh - pagedispy) in
368 if pagevw > 0 && pagevh > 0
369 then
370 let e =
371 { pageno = n/columns
372 ; pagedimno = pdimno
373 ; pagew = pagew
374 ; pageh = pageh
375 ; pagex = pagex
376 ; pagey = pagey
377 ; pagevw = pagevw
378 ; pagevh = pagevh
379 ; pagedispx = pagedispx
380 ; pagedispy = pagedispy
381 ; pagecol = n mod columns
384 e :: accu
385 else
386 accu
387 else
388 accu
390 fold accu (n+1)
392 List.rev (fold [] 0)
395 let layout x y sw sh =
396 if nogeomcmds state.geomcmds
397 then
398 match conf.columns with
399 | Csingle b -> layoutN ((1, 0, 0), b) x y sw sh
400 | Cmulti c -> layoutN c x y sw sh
401 | Csplit s -> layoutS s x y sw sh
402 else []
405 let maxy () = state.maxy - if conf.maxhfit then state.winh else 0;;
407 let clamp incr = bound (state.y + incr) 0 @@ maxy ();;
409 let itertiles l f =
410 let tilex = l.pagex mod conf.tilew in
411 let tiley = l.pagey mod conf.tileh in
413 let col = l.pagex / conf.tilew in
414 let row = l.pagey / conf.tileh in
416 let rec rowloop row y0 dispy h =
417 if h = 0
418 then ()
419 else (
420 let dh = conf.tileh - y0 in
421 let dh = min h dh in
422 let rec colloop col x0 dispx w =
423 if w = 0
424 then ()
425 else (
426 let dw = conf.tilew - x0 in
427 let dw = min w dw in
428 f col row dispx dispy x0 y0 dw dh;
429 colloop (col+1) 0 (dispx+dw) (w-dw)
432 colloop col tilex l.pagedispx l.pagevw;
433 rowloop (row+1) 0 (dispy+dh) (h-dh)
436 if l.pagevw > 0 && l.pagevh > 0
437 then rowloop row tiley l.pagedispy l.pagevh;
440 let gettileopaque l col row =
441 let key =
442 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
444 try Some (Hashtbl.find state.tilemap key)
445 with Not_found -> None
448 let puttileopaque l col row gen colorspace angle opaque size elapsed =
449 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
450 Hashtbl.add state.tilemap key (opaque, size, elapsed)
453 let drawtiles l color =
454 GlDraw.color color;
455 begintiles ();
456 let f col row x y tilex tiley w h =
457 match gettileopaque l col row with
458 | Some (opaque, _, t) ->
459 let params = x, y, w, h, tilex, tiley in
460 if conf.invert
461 then GlTex.env (`mode `blend);
462 drawtile params opaque;
463 if conf.invert
464 then GlTex.env (`mode `modulate);
465 if conf.debug
466 then (
467 endtiles ();
468 let s = Printf.sprintf
469 "%d[%d,%d] %f sec"
470 l.pageno col row t
472 let w = measurestr fstate.fontsize s in
473 GlDraw.color (0.0, 0.0, 0.0);
474 filledrect (float (x-2))
475 (float (y-2))
476 (float (x+2) +. w)
477 (float (y + fstate.fontsize + 2));
478 GlDraw.color color;
479 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
480 begintiles ();
483 | None ->
484 endtiles ();
485 let w =
486 let lw = state.winw - x in
487 min lw w
488 and h =
489 let lh = state.winh - y in
490 min lh h
492 if conf.invert
493 then GlTex.env (`mode `blend);
494 begin match state.checkerstexid with
495 | Some id ->
496 Gl.enable `texture_2d;
497 GlTex.bind_texture ~target:`texture_2d id;
498 let x0 = float x
499 and y0 = float y
500 and x1 = float (x+w)
501 and y1 = float (y+h) in
503 let tw = float w /. 16.0
504 and th = float h /. 16.0 in
505 let tx0 = float tilex /. 16.0
506 and ty0 = float tiley /. 16.0 in
507 let tx1 = tx0 +. tw
508 and ty1 = ty0 +. th in
509 Raw.sets_float Glutils.vraw ~pos:0
510 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
511 Raw.sets_float Glutils.traw ~pos:0
512 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
513 GlArray.vertex `two Glutils.vraw;
514 GlArray.tex_coord `two Glutils.traw;
515 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
516 Gl.disable `texture_2d;
518 | None ->
519 GlDraw.color (1.0, 1.0, 1.0);
520 filledrect (float x) (float y) (float (x+w)) (float (y+h));
521 end;
522 if conf.invert
523 then GlTex.env (`mode `modulate);
524 if w > 128 && h > fstate.fontsize + 10
525 then (
526 let c = if conf.invert then 1.0 else 0.0 in
527 GlDraw.color (c, c, c);
528 let c, r =
529 if conf.verbose
530 then (col*conf.tilew, row*conf.tileh)
531 else col, row
533 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
535 GlDraw.color color;
536 begintiles ();
538 itertiles l f;
539 endtiles ();
542 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
544 let tilevisible1 l x y =
545 let ax0 = l.pagex
546 and ax1 = l.pagex + l.pagevw
547 and ay0 = l.pagey
548 and ay1 = l.pagey + l.pagevh in
550 let bx0 = x
551 and by0 = y in
552 let bx1 = min (bx0 + conf.tilew) l.pagew
553 and by1 = min (by0 + conf.tileh) l.pageh in
555 let rx0 = max ax0 bx0
556 and ry0 = max ay0 by0
557 and rx1 = min ax1 bx1
558 and ry1 = min ay1 by1 in
560 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
561 nonemptyintersection
564 let tilevisible layout n x y =
565 let rec findpageinlayout m = function
566 | l :: rest when l.pageno = n ->
567 tilevisible1 l x y || (
568 match conf.columns with
569 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
570 | Csplit _ | Csingle _ | Cmulti _ -> false
572 | _ :: rest -> findpageinlayout 0 rest
573 | [] -> false
575 findpageinlayout 0 layout;
578 let tileready l x y =
579 tilevisible1 l x y &&
580 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
583 let tilepage n p layout =
584 let rec loop = function
585 | l :: rest ->
586 if l.pageno = n
587 then
588 let f col row _ _ _ _ _ _ =
589 if state.currently = Idle
590 then
591 match gettileopaque l col row with
592 | Some _ -> ()
593 | None ->
594 let x = col*conf.tilew
595 and y = row*conf.tileh in
596 let w =
597 let w = l.pagew - x in
598 min w conf.tilew
600 let h =
601 let h = l.pageh - y in
602 min h conf.tileh
604 let pbo =
605 if conf.usepbo
606 then getpbo w h conf.colorspace
607 else ~< "0"
609 wcmd "tile %s %d %d %d %d %s"
610 (~> p) x y w h (~> pbo);
611 state.currently <-
612 Tiling (
613 l, p, conf.colorspace, conf.angle,
614 state.gen, col, row, conf.tilew, conf.tileh
617 itertiles l f;
618 else
619 loop rest
621 | [] -> ()
623 if nogeomcmds state.geomcmds
624 then loop layout;
627 let preloadlayout x y sw sh =
628 let y = if y < sh then 0 else y - sh in
629 let x = min 0 (x + sw) in
630 let h = sh*3 in
631 let w = sw*3 in
632 layout x y w h;
635 let load pages =
636 let rec loop pages =
637 if state.currently != Idle
638 then ()
639 else
640 match pages with
641 | l :: rest ->
642 begin match getopaque l.pageno with
643 | None ->
644 wcmd "page %d %d" l.pageno l.pagedimno;
645 state.currently <- Loading (l, state.gen);
646 | Some opaque ->
647 tilepage l.pageno opaque pages;
648 loop rest
649 end;
650 | _ -> ()
652 if nogeomcmds state.geomcmds
653 then loop pages
656 let preload pages =
657 load pages;
658 if conf.preload && state.currently = Idle
659 then load (preloadlayout state.x state.y state.winw state.winh);
662 let layoutready layout =
663 let rec fold all ls =
664 all && match ls with
665 | l :: rest ->
666 let seen = ref false in
667 let allvisible = ref true in
668 let foo col row _ _ _ _ _ _ =
669 seen := true;
670 allvisible := !allvisible &&
671 begin match gettileopaque l col row with
672 | Some _ -> true
673 | None -> false
676 itertiles l foo;
677 fold (!seen && !allvisible) rest
678 | [] -> true
680 let alltilesvisible = fold true layout in
681 alltilesvisible;
684 let gotoxy x y =
685 let y = bound y 0 state.maxy in
686 let y, layout =
687 let layout = layout x y state.winw state.winh in
688 postRedisplay "gotoxy ready";
689 y, layout
691 state.x <- x;
692 state.y <- y;
693 state.layout <- layout;
694 begin match state.mode with
695 | LinkNav ln ->
696 begin match ln with
697 | Ltexact (pageno, linkno) ->
698 let rec loop = function
699 | [] ->
700 state.lnava <- Some (pageno, linkno);
701 state.mode <- LinkNav (Ltgendir 0)
702 | l :: _ when l.pageno = pageno ->
703 begin match getopaque pageno with
704 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
705 | Some opaque ->
706 let x0, y0, x1, y1 = getlinkrect opaque linkno in
707 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
708 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
709 then state.mode <- LinkNav (Ltgendir 0)
711 | _ :: rest -> loop rest
713 loop layout
714 | Ltnotready _ | Ltgendir _ -> ()
716 | Birdseye _ | Textentry _ | View -> ()
717 end;
718 begin match state.mode with
719 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
720 if not (pagevisible layout pageno)
721 then (
722 match state.layout with
723 | [] -> ()
724 | l :: _ ->
725 state.mode <- Birdseye (
726 conf, leftx, l.pageno, hooverpageno, anchor
729 | LinkNav lt ->
730 begin match lt with
731 | Ltnotready (_, dir)
732 | Ltgendir dir ->
733 let linknav =
734 let rec loop = function
735 | [] -> lt
736 | l :: rest ->
737 match getopaque l.pageno with
738 | None -> Ltnotready (l.pageno, dir)
739 | Some opaque ->
740 let link =
741 let ld =
742 if dir = 0
743 then LDfirstvisible (l.pagex, l.pagey, dir)
744 else (
745 if dir > 0 then LDfirst else LDlast
748 findlink opaque ld
750 match link with
751 | Lnotfound -> loop rest
752 | Lfound n ->
753 showlinktype (getlink opaque n);
754 Ltexact (l.pageno, n)
756 loop state.layout
758 state.mode <- LinkNav linknav
759 | Ltexact _ -> ()
761 | Textentry _ | View -> ()
762 end;
763 preload layout;
764 if conf.updatecurs
765 then (
766 let mx, my = state.mpos in
767 updateunder mx my;
771 let conttiling pageno opaque =
772 tilepage pageno opaque
773 (if conf.preload
774 then preloadlayout state.x state.y state.winw state.winh
775 else state.layout)
778 let gotoxy x y =
779 if not conf.verbose then state.text <- E.s;
780 gotoxy x y;
783 let getanchory (n, top, dtop) =
784 let y, h = getpageyh n in
785 if conf.presentation
786 then
787 let ips = calcips h in
788 y + truncate (top*.float h -. dtop*.float ips) + ips;
789 else
790 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
793 let gotoanchor anchor =
794 gotoxy state.x (getanchory anchor);
797 let addnav () =
798 getanchor () |> cbput state.hists.nav;
801 let addnavnorc () =
802 getanchor () |> cbput_dont_update_rc state.hists.nav;
805 let getnav dir =
806 let anchor = cbgetc state.hists.nav dir in
807 getanchory anchor;
810 let gotopage n top =
811 let y, h = getpageyh n in
812 let y = y + (truncate (top *. float h)) in
813 gotoxy state.x y
816 let gotopage1 n top =
817 let y = getpagey n in
818 let y = y + top in
819 gotoxy state.x y
822 let invalidate s f =
823 Glutils.redisplay := false;
824 state.layout <- [];
825 state.pdims <- [];
826 state.rects <- [];
827 state.rects1 <- [];
828 match state.geomcmds with
829 | ps, [] when emptystr ps ->
830 f ();
831 state.geomcmds <- s, [];
833 | ps, [] ->
834 state.geomcmds <- ps, [s, f];
836 | ps, (s', _) :: rest when s' = s ->
837 state.geomcmds <- ps, ((s, f) :: rest);
839 | ps, cmds ->
840 state.geomcmds <- ps, ((s, f) :: cmds);
843 let flushpages () =
844 Hashtbl.iter (fun _ opaque ->
845 wcmd "freepage %s" (~> opaque);
846 ) state.pagemap;
847 Hashtbl.clear state.pagemap;
850 let flushtiles () =
851 if not (Queue.is_empty state.tilelru)
852 then (
853 Queue.iter (fun (k, p, s) ->
854 wcmd "freetile %s" (~> p);
855 state.memused <- state.memused - s;
856 Hashtbl.remove state.tilemap k;
857 ) state.tilelru;
858 state.uioh#infochanged Memused;
859 Queue.clear state.tilelru;
861 load state.layout;
864 let stateh h =
865 let h = truncate (float h*.conf.zoom) in
866 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
867 h - d
870 let fillhelp () =
871 state.help <-
872 let sl = keystostrlist conf in
873 let rec loop accu =
874 function | [] -> accu
875 | s :: rest -> loop ((s, 0, Noaction) :: accu) rest
876 in Help.makehelp conf.urilauncher
877 @ (("", 0, Noaction) :: loop [] sl) |> Array.of_list
880 let opendoc path password =
881 state.path <- path;
882 state.password <- password;
883 state.gen <- state.gen + 1;
884 state.docinfo <- [];
885 state.outlines <- [||];
887 flushpages ();
888 setaalevel conf.aalevel;
889 let titlepath =
890 if emptystr state.origin
891 then path
892 else state.origin
894 Wsi.settitle ("llpp " ^ mbtoutf8 (Filename.basename titlepath));
895 wcmd "open %d %d %s\000%s\000%s\000"
896 (btod conf.usedoccss) !layouth
897 path password conf.css;
898 invalidate "reqlayout"
899 (fun () ->
900 wcmd "reqlayout %d %d %d %s\000"
901 conf.angle (FMTE.to_int conf.fitmodel)
902 (stateh state.winh) state.nameddest
904 fillhelp ();
907 let reload () =
908 state.anchor <- getanchor ();
909 opendoc state.path state.password;
912 let scalecolor c =
913 let c = c *. conf.colorscale in
914 (c, c, c);
917 let scalecolor2 (r, g, b) =
918 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
921 let docolumns columns =
922 match columns with
923 | Csingle _ ->
924 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
925 let rec loop pageno pdimno pdim y ph pdims =
926 if pageno = state.pagecount
927 then ()
928 else
929 let pdimno, ((_, w, h, xoff) as pdim), pdims =
930 match pdims with
931 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
932 pdimno+1, pdim, rest
933 | _ ->
934 pdimno, pdim, pdims
936 let x = max 0 (((state.winw - w) / 2) - xoff) in
937 let y =
938 y + (if conf.presentation
939 then (if pageno = 0 then calcips h else calcips ph + calcips h)
940 else (if pageno = 0 then 0 else conf.interpagespace)
943 a.(pageno) <- (pdimno, x, y, pdim);
944 loop (pageno+1) pdimno pdim (y + h) h pdims
946 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
947 conf.columns <- Csingle a;
949 | Cmulti ((columns, coverA, coverB), _) ->
950 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
951 let rec loop pageno pdimno pdim x y rowh pdims =
952 let rec fixrow m =
953 if m = pageno then () else
954 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
955 if h < rowh
956 then (
957 let y = y + (rowh - h) / 2 in
958 a.(m) <- (pdimno, x, y, pdim);
960 fixrow (m+1)
962 if pageno = state.pagecount
963 then fixrow (((pageno - 1) / columns) * columns)
964 else
965 let pdimno, ((_, w, h, xoff) as pdim), pdims =
966 match pdims with
967 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
968 pdimno+1, pdim, rest
969 | _ ->
970 pdimno, pdim, pdims
972 let x, y, rowh' =
973 if pageno = coverA - 1 || pageno = state.pagecount - coverB
974 then (
975 let x = (state.winw - w) / 2 in
976 let ips =
977 if conf.presentation then calcips h else conf.interpagespace in
978 x, y + ips + rowh, h
980 else (
981 if (pageno - coverA) mod columns = 0
982 then (
983 let x = max 0 (state.winw - state.w) / 2 in
984 let y =
985 if conf.presentation
986 then
987 let ips = calcips h in
988 y + (if pageno = 0 then 0 else calcips rowh + ips)
989 else
990 y + (if pageno = 0 then 0 else conf.interpagespace)
992 x, y + rowh, h
994 else x, y, max rowh h
997 let y =
998 if pageno > 1 && (pageno - coverA) mod columns = 0
999 then (
1000 let y =
1001 if pageno = columns && conf.presentation
1002 then (
1003 let ips = calcips rowh in
1004 for i = 0 to pred columns
1006 let (pdimno, x, y, pdim) = a.(i) in
1007 a.(i) <- (pdimno, x, y+ips, pdim)
1008 done;
1009 y+ips;
1011 else y
1013 fixrow (pageno - columns);
1016 else y
1018 a.(pageno) <- (pdimno, x, y, pdim);
1019 let x = x + w + xoff*2 + conf.interpagespace in
1020 loop (pageno+1) pdimno pdim x y rowh' pdims
1022 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1023 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1025 | Csplit (c, _) ->
1026 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1027 let rec loop pageno pdimno pdim y pdims =
1028 if pageno = state.pagecount
1029 then ()
1030 else
1031 let pdimno, ((_, w, h, _) as pdim), pdims =
1032 match pdims with
1033 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1034 pdimno+1, pdim, rest
1035 | _ ->
1036 pdimno, pdim, pdims
1038 let cw = w / c in
1039 let rec loop1 n x y =
1040 if n = c then y else (
1041 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1042 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1045 let y = loop1 0 0 y in
1046 loop (pageno+1) pdimno pdim y pdims
1048 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1049 conf.columns <- Csplit (c, a);
1052 let represent () =
1053 docolumns conf.columns;
1054 state.maxy <- calcheight ();
1055 if state.reprf == noreprf
1056 then (
1057 match state.mode with
1058 | Birdseye (_, _, pageno, _, _) ->
1059 let y, h = getpageyh pageno in
1060 let top = (state.winh - h) / 2 in
1061 gotoxy state.x (max 0 (y - top))
1062 | Textentry _ | View | LinkNav _ ->
1063 let y = getanchory state.anchor in
1064 let y = min y (state.maxy - state.winh) in
1065 gotoxy state.x y;
1067 else (
1068 state.reprf ();
1069 state.reprf <- noreprf;
1073 let reshape ?(firsttime=false) w h =
1074 GlDraw.viewport ~x:0 ~y:0 ~w ~h;
1075 if not firsttime && nogeomcmds state.geomcmds
1076 then state.anchor <- getanchor ();
1078 state.winw <- w;
1079 let w = truncate (float w *. conf.zoom) in
1080 let w = max w 2 in
1081 state.winh <- h;
1082 setfontsize fstate.fontsize;
1083 GlMat.mode `modelview;
1084 GlMat.load_identity ();
1086 GlMat.mode `projection;
1087 GlMat.load_identity ();
1088 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1089 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1090 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1092 let relx =
1093 if conf.zoom <= 1.0
1094 then 0.0
1095 else float state.x /. float state.w
1097 invalidate "geometry"
1098 (fun () ->
1099 state.w <- w;
1100 if not firsttime
1101 then state.x <- truncate (relx *. float w);
1102 let w =
1103 match conf.columns with
1104 | Csingle _ -> w
1105 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1106 | Csplit (c, _) -> w * c
1108 wcmd "geometry %d %d %d"
1109 w (stateh h) (FMTE.to_int conf.fitmodel)
1113 let gctiles () =
1114 let len = Queue.length state.tilelru in
1115 let layout = lazy (if conf.preload
1116 then preloadlayout state.x state.y state.winw state.winh
1117 else state.layout) in
1118 let rec loop qpos =
1119 if state.memused > conf.memlimit
1120 then (
1121 if qpos < len
1122 then
1123 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1124 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1125 let (_, pw, ph, _) = getpagedim n in
1126 if gen = state.gen
1127 && colorspace = conf.colorspace
1128 && angle = conf.angle
1129 && pagew = pw
1130 && pageh = ph
1131 && (
1132 let x = col*conf.tilew
1133 and y = row*conf.tileh in
1134 tilevisible (Lazy.force_val layout) n x y
1136 then Queue.push lruitem state.tilelru
1137 else (
1138 freepbo p;
1139 wcmd "freetile %s" (~> p);
1140 state.memused <- state.memused - s;
1141 state.uioh#infochanged Memused;
1142 Hashtbl.remove state.tilemap k;
1144 loop (qpos+1)
1147 loop 0
1150 let onpagerect pageno f =
1151 let b =
1152 match conf.columns with
1153 | Cmulti (_, b) -> b
1154 | Csingle b -> b
1155 | Csplit (_, b) -> b
1157 if pageno >= 0 && pageno < Array.length b
1158 then
1159 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1160 f w h
1163 let gotopagexy1 pageno x y =
1164 let _,w1,h1,leftx = getpagedim pageno in
1165 let top = y /. (float h1) in
1166 let left = x /. (float w1) in
1167 let py, w, h = getpageywh pageno in
1168 let wh = state.winh in
1169 let x = left *. (float w) in
1170 let x = leftx + state.x + truncate x in
1171 let sx =
1172 if x < 0 || x >= state.winw
1173 then state.x - x
1174 else state.x
1176 let pdy = truncate (top *. float h) in
1177 let y' = py + pdy in
1178 let dy = y' - state.y in
1179 let sy =
1180 if x != state.x || not (dy > 0 && dy < wh)
1181 then (
1182 if conf.presentation
1183 then
1184 if abs (py - y') > wh
1185 then y'
1186 else py
1187 else y';
1189 else state.y
1191 if state.x != sx || state.y != sy
1192 then gotoxy sx sy
1193 else gotoxy state.x state.y;
1196 let gotopagexy pageno x y =
1197 match state.mode with
1198 | Birdseye _ -> gotopage pageno 0.0
1199 | Textentry _ | View | LinkNav _ -> gotopagexy1 pageno x y
1202 let getpassword () =
1203 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1204 if emptystr passcmd
1205 then E.s
1206 else getcmdoutput
1207 (fun s ->
1208 impmsg "error getting password: %s" s;
1209 dolog "%s" s) passcmd;
1212 let pgoto opaque pageno x y =
1213 let pdimno = getpdimno pageno in
1214 let x, y = project opaque pageno pdimno x y in
1215 gotopagexy pageno x y;
1218 let act cmds =
1219 (* dolog "%S" cmds; *)
1220 let spl = splitatchar cmds ' ' in
1221 let scan s fmt f =
1222 try Scanf.sscanf s fmt f
1223 with exn ->
1224 dolog "error processing '%S': %s" cmds @@ exntos exn;
1225 exit 1
1227 let addoutline outline =
1228 match state.currently with
1229 | Outlining outlines -> state.currently <- Outlining (outline :: outlines)
1230 | Idle -> state.currently <- Outlining [outline]
1231 | Loading _ | Tiling _ ->
1232 dolog "invalid outlining state";
1233 logcurrently state.currently
1235 match spl with
1236 | "clear", "" ->
1237 state.pdims <- [];
1238 state.uioh#infochanged Pdim;
1240 | "clearrects", "" ->
1241 state.rects <- state.rects1;
1242 postRedisplay "clearrects";
1244 | "continue", args ->
1245 let n = scan args "%u" (fun n -> n) in
1246 state.pagecount <- n;
1247 begin match state.currently with
1248 | Outlining l ->
1249 state.currently <- Idle;
1250 state.outlines <- Array.of_list (List.rev l)
1251 | Idle | Loading _ | Tiling _ -> ()
1252 end;
1254 let cur, cmds = state.geomcmds in
1255 if emptystr cur
1256 then failwith "umpossible";
1258 begin match List.rev cmds with
1259 | [] ->
1260 state.geomcmds <- E.s, [];
1261 represent ();
1262 | (s, f) :: rest ->
1263 f ();
1264 state.geomcmds <- s, List.rev rest;
1265 end;
1266 postRedisplay "continue";
1268 | "msg", args ->
1269 showtext ' ' args
1271 | "vmsg", args ->
1272 if conf.verbose
1273 then showtext ' ' args
1275 | "emsg", args ->
1276 Buffer.add_string state.errmsgs args;
1277 state.newerrmsgs <- true;
1278 postRedisplay "error message"
1280 | "progress", args ->
1281 let progress, text =
1282 scan args "%f %n"
1283 (fun f pos ->
1284 f, String.sub args pos (String.length args - pos))
1286 state.text <- text;
1287 state.progress <- progress;
1288 postRedisplay "progress"
1290 | "firstmatch", args ->
1291 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1292 scan args "%u %d %f %f %f %f %f %f %f %f"
1293 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1294 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1296 let y = (getpagey pageno) + truncate y0 in
1297 let x =
1298 if (state.x < - truncate x0) || (state.x > state.winw - truncate x1)
1299 then state.winw/2 - truncate (x0 /. 2. +. x1 /. 2.)
1300 else state.x
1302 addnav ();
1303 gotoxy x y;
1304 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1305 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1307 | "match", args ->
1308 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1309 scan args "%u %d %f %f %f %f %f %f %f %f"
1310 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1311 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1313 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1314 state.rects1 <-
1315 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1317 | "page", args ->
1318 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1319 let pageopaque = ~< pageopaques in
1320 begin match state.currently with
1321 | Loading (l, gen) ->
1322 vlog "page %d took %f sec" l.pageno t;
1323 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1324 let preloadedpages =
1325 if conf.preload
1326 then preloadlayout state.x state.y state.winw state.winh
1327 else state.layout
1329 let evict () =
1330 let set = List.fold_left (fun s l -> IntSet.add l.pageno s)
1331 IntSet.empty preloadedpages
1333 let evictedpages =
1334 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1335 if not (IntSet.mem pageno set)
1336 then (
1337 wcmd "freepage %s" (~> opaque);
1338 key :: accu
1340 else accu
1341 ) state.pagemap []
1343 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1345 evict ();
1346 state.currently <- Idle;
1347 if gen = state.gen
1348 then (
1349 tilepage l.pageno pageopaque state.layout;
1350 load state.layout;
1351 load preloadedpages;
1352 let visible = pagevisible state.layout l.pageno in
1353 if visible
1354 then (
1355 match state.mode with
1356 | LinkNav (Ltnotready (pageno, dir)) ->
1357 if pageno = l.pageno
1358 then (
1359 let link =
1360 let ld =
1361 if dir = 0
1362 then LDfirstvisible (l.pagex, l.pagey, dir)
1363 else (
1364 if dir > 0 then LDfirst else LDlast
1367 findlink pageopaque ld
1369 match link with
1370 | Lnotfound -> ()
1371 | Lfound n ->
1372 showlinktype (getlink pageopaque n);
1373 state.mode <- LinkNav (Ltexact (l.pageno, n))
1375 | LinkNav (Ltgendir _)
1376 | LinkNav (Ltexact _)
1377 | View
1378 | Birdseye _
1379 | Textentry _ -> ()
1382 if visible && layoutready state.layout
1383 then (
1384 postRedisplay "page";
1388 | Idle | Tiling _ | Outlining _ ->
1389 dolog "Inconsistent loading state";
1390 logcurrently state.currently;
1391 exit 1
1394 | "tile" , args ->
1395 let (x, y, opaques, size, t) =
1396 scan args "%u %u %s %u %f"
1397 (fun x y p size t -> (x, y, p, size, t))
1399 let opaque = ~< opaques in
1400 begin match state.currently with
1401 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1402 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1404 unmappbo opaque;
1405 if tilew != conf.tilew || tileh != conf.tileh
1406 then (
1407 wcmd "freetile %s" (~> opaque);
1408 state.currently <- Idle;
1409 load state.layout;
1411 else (
1412 puttileopaque l col row gen cs angle opaque size t;
1413 state.memused <- state.memused + size;
1414 state.uioh#infochanged Memused;
1415 gctiles ();
1416 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1417 opaque, size) state.tilelru;
1419 state.currently <- Idle;
1420 if gen = state.gen
1421 && conf.colorspace = cs
1422 && conf.angle = angle
1423 && tilevisible state.layout l.pageno x y
1424 then conttiling l.pageno pageopaque;
1426 preload state.layout;
1427 if gen = state.gen
1428 && conf.colorspace = cs
1429 && conf.angle = angle
1430 && tilevisible state.layout l.pageno x y
1431 && layoutready state.layout
1432 then postRedisplay "tile nothrottle";
1435 | Idle | Loading _ | Outlining _ ->
1436 dolog "Inconsistent tiling state";
1437 logcurrently state.currently;
1438 exit 1
1441 | "pdim", args ->
1442 let (n, w, h, _) as pdim =
1443 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1445 let pdim =
1446 match conf.fitmodel with
1447 | FitWidth -> pdim
1448 | FitPage | FitProportional ->
1449 match conf.columns with
1450 | Csplit _ -> (n, w, h, 0)
1451 | Csingle _ | Cmulti _ -> pdim
1453 state.pdims <- pdim :: state.pdims;
1454 state.uioh#infochanged Pdim
1456 | "o", args ->
1457 let (l, n, t, h, pos) =
1458 scan args "%u %u %d %u %n"
1459 (fun l n t h pos -> l, n, t, h, pos)
1461 let s = String.sub args pos (String.length args - pos) in
1462 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1464 | "ou", args ->
1465 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1466 let s = String.sub args pos len in
1467 let pos2 = pos + len + 1 in
1468 let uri = String.sub args pos2 (String.length args - pos2) in
1469 addoutline (s, l, Ouri uri)
1471 | "on", args ->
1472 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1473 let s = String.sub args pos (String.length args - pos) in
1474 addoutline (s, l, Onone)
1476 | "a", args ->
1477 let (n, l, t) =
1478 scan args "%u %d %d" (fun n l t -> n, l, t)
1480 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
1482 | "info", args ->
1483 let c, v = splitatchar args '\t' in
1484 let s =
1485 if nonemptystr v
1486 then
1487 if c = "Title"
1488 then (
1489 conf.title <- v;
1490 if not !ignoredoctitlte
1491 then Wsi.settitle v;
1492 args
1494 else
1495 if let len = String.length c in
1496 len > 6 && ((String.sub c (len-4) 4) = "date")
1497 then (
1498 if String.length v >= 7 && v.[0] = 'D' && v.[1] = ':'
1499 then
1500 let b = Buffer.create 10 in
1501 Printf.bprintf b "%s\t" c;
1502 let sub p l c =
1504 Buffer.add_substring b v p l;
1505 Buffer.add_char b c;
1506 with exn -> Buffer.add_string b @@ exntos exn
1508 sub 2 4 '/';
1509 sub 6 2 '/';
1510 sub 8 2 ' ';
1511 sub 10 2 ':';
1512 sub 12 2 ':';
1513 sub 14 2 ' ';
1514 Buffer.add_char b '[';
1515 Buffer.add_string b v;
1516 Buffer.add_char b ']';
1517 Buffer.contents b
1518 else args
1520 else args
1521 else args
1523 state.docinfo <- (1, s) :: state.docinfo
1525 | "infoend", "" ->
1526 state.docinfo <- List.rev state.docinfo;
1527 state.uioh#infochanged Docinfo
1529 | "pass", args ->
1530 if args = "fail"
1531 then Wsi.settitle "Wrong password";
1532 let password = getpassword () in
1533 if emptystr password
1534 then error "document is password protected"
1535 else opendoc state.path password
1537 | _ ->
1538 error "unknown cmd `%S'" cmds
1541 let onhist cb =
1542 let rc = cb.rc in
1543 let action = function
1544 | HCprev -> cbget cb ~-1
1545 | HCnext -> cbget cb 1
1546 | HCfirst -> cbget cb ~-(cb.rc)
1547 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1548 and cancel () = cb.rc <- rc
1549 in (action, cancel)
1552 let search pattern forward =
1553 match conf.columns with
1554 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1555 | Csingle _ | Cmulti _ ->
1556 if nonemptystr pattern
1557 then
1558 let pn, py =
1559 match state.layout with
1560 | [] -> 0, 0
1561 | l :: _ ->
1562 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1564 wcmd "search %d %d %d %d,%s\000"
1565 (btod conf.icase) pn py (btod forward) pattern;
1568 let intentry text key =
1569 let text =
1570 if emptystr text && key = Keys.Ascii '-'
1571 then addchar text '-'
1572 else
1573 match [@warning "-4"] key with
1574 | Keys.Ascii ('0'..'9' as c) -> addchar text c
1575 | _ ->
1576 state.text <- "invalid key";
1577 text
1579 TEcont text
1582 let linknact f s =
1583 if nonemptystr s
1584 then (
1585 let n =
1586 let l = String.length s in
1587 let rec loop pos n =
1588 if pos = l
1589 then n
1590 else
1591 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1592 loop (pos+1) (n*26 + m)
1593 in loop 0 0
1595 let rec loop n = function
1596 | [] -> ()
1597 | l :: rest ->
1598 match getopaque l.pageno with
1599 | None -> loop n rest
1600 | Some opaque ->
1601 let m = getlinkcount opaque in
1602 if n < m
1603 then (
1604 let under = getlink opaque n in
1605 f under
1607 else loop (n-m) rest
1609 loop n state.layout;
1613 let linknentry text =
1614 function [@warning "-4"]
1615 | Keys.Ascii c ->
1616 let text = addchar text c in
1617 linknact (fun under -> state.text <- undertext under) text;
1618 TEcont text
1619 | _ ->
1620 state.text <- Printf.sprintf "invalid key";
1621 TEcont text
1624 let textentry text = function [@warning "-4"]
1625 | Keys.Ascii c -> TEcont (addchar text c)
1626 | Keys.Code c -> TEcont (text ^ toutf8 c)
1627 | _ -> TEcont text
1630 let reqlayout angle fitmodel =
1631 if nogeomcmds state.geomcmds
1632 then state.anchor <- getanchor ();
1633 conf.angle <- angle mod 360;
1634 if conf.angle != 0
1635 then (
1636 match state.mode with
1637 | LinkNav _ -> state.mode <- View
1638 | Birdseye _ | Textentry _ | View -> ()
1640 conf.fitmodel <- fitmodel;
1641 invalidate
1642 "reqlayout"
1643 (fun () ->
1644 wcmd "reqlayout %d %d %d"
1645 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
1649 let settrim trimmargins trimfuzz =
1650 if nogeomcmds state.geomcmds
1651 then state.anchor <- getanchor ();
1652 conf.trimmargins <- trimmargins;
1653 conf.trimfuzz <- trimfuzz;
1654 let x0, y0, x1, y1 = trimfuzz in
1655 invalidate
1656 "settrim" (fun () ->
1657 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
1658 flushpages ();
1661 let setzoom zoom =
1662 let zoom = max 0.0001 zoom in
1663 if zoom <> conf.zoom
1664 then (
1665 state.prevzoom <- (conf.zoom, state.x);
1666 conf.zoom <- zoom;
1667 reshape state.winw state.winh;
1668 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
1672 let pivotzoom ?(vw=min state.w state.winw)
1673 ?(vh=min (state.maxy-state.y) state.winh)
1674 ?(x=vw/2) ?(y=vh/2) zoom =
1675 let w = float state.w /. zoom in
1676 let hw = w /. 2.0 in
1677 let ratio = float vh /. float vw in
1678 let hh = hw *. ratio in
1679 let x0 = float x -. hw
1680 and y0 = float y -. hh in
1681 gotoxy (state.x - truncate x0) (state.y + truncate y0);
1682 setzoom zoom;
1685 let pivotzoom ?vw ?vh ?x ?y zoom =
1686 if nogeomcmds state.geomcmds
1687 then
1688 if zoom > 1.0
1689 then pivotzoom ?vw ?vh ?x ?y zoom
1690 else setzoom zoom
1693 let setcolumns mode columns coverA coverB =
1694 state.prevcolumns <- Some (conf.columns, conf.zoom);
1695 if columns < 0
1696 then (
1697 if isbirdseye mode
1698 then impmsg "split mode doesn't work in bird's eye"
1699 else (
1700 conf.columns <- Csplit (-columns, E.a);
1701 state.x <- 0;
1702 conf.zoom <- 1.0;
1705 else (
1706 if columns < 2
1707 then (
1708 conf.columns <- Csingle E.a;
1709 state.x <- 0;
1710 setzoom 1.0;
1712 else (
1713 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
1714 conf.zoom <- 1.0;
1717 reshape state.winw state.winh;
1720 let resetmstate () =
1721 state.mstate <- Mnone;
1722 Wsi.setcursor Wsi.CURSOR_INHERIT;
1725 let enterbirdseye () =
1726 let zoom = float conf.thumbw /. float state.winw in
1727 let birdseyepageno =
1728 let cy = state.winh / 2 in
1729 let fold = function
1730 | [] -> 0
1731 | l :: rest ->
1732 let rec fold best = function
1733 | [] -> best.pageno
1734 | l :: rest ->
1735 let d = cy - (l.pagedispy + l.pagevh/2)
1736 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1737 if abs d < abs dbest
1738 then fold l rest
1739 else best.pageno
1740 in fold l rest
1742 fold state.layout
1744 state.mode <-
1745 Birdseye (
1746 { conf with zoom = conf.zoom },
1747 state.x, birdseyepageno, -1, getanchor ()
1749 resetmstate ();
1750 conf.zoom <- zoom;
1751 conf.presentation <- false;
1752 conf.interpagespace <- 10;
1753 conf.hlinks <- false;
1754 conf.fitmodel <- FitPage;
1755 state.x <- 0;
1756 conf.columns <- (
1757 match conf.beyecolumns with
1758 | Some c ->
1759 conf.zoom <- 1.0;
1760 Cmulti ((c, 0, 0), E.a)
1761 | None -> Csingle E.a
1763 if conf.verbose
1764 then
1765 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1766 (100.0*.zoom)
1767 else
1768 state.text <- E.s
1770 reshape state.winw state.winh;
1773 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1774 state.mode <- View;
1775 conf.zoom <- c.zoom;
1776 conf.presentation <- c.presentation;
1777 conf.interpagespace <- c.interpagespace;
1778 conf.hlinks <- c.hlinks;
1779 conf.fitmodel <- c.fitmodel;
1780 conf.beyecolumns <- (
1781 match conf.columns with
1782 | Cmulti ((c, _, _), _) -> Some c
1783 | Csingle _ -> None
1784 | Csplit _ -> failwith "leaving bird's eye split mode"
1786 conf.columns <- (
1787 match c.columns with
1788 | Cmulti (c, _) -> Cmulti (c, E.a)
1789 | Csingle _ -> Csingle E.a
1790 | Csplit (c, _) -> Csplit (c, E.a)
1792 if conf.verbose
1793 then
1794 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1795 (100.0*.conf.zoom)
1797 reshape state.winw state.winh;
1798 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
1799 state.x <- leftx;
1802 let togglebirdseye () =
1803 match state.mode with
1804 | Birdseye vals -> leavebirdseye vals true
1805 | View -> enterbirdseye ()
1806 | Textentry _ | LinkNav _ -> ()
1809 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
1810 let pageno = max 0 (pageno - incr) in
1811 let rec loop = function
1812 | [] -> gotopage1 pageno 0
1813 | l :: _ when l.pageno = pageno ->
1814 if l.pagedispy >= 0 && l.pagey = 0
1815 then postRedisplay "upbirdseye"
1816 else gotopage1 pageno 0
1817 | _ :: rest -> loop rest
1819 loop state.layout;
1820 state.text <- E.s;
1821 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1824 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
1825 let pageno = min (state.pagecount - 1) (pageno + incr) in
1826 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1827 let rec loop = function
1828 | [] ->
1829 let y, h = getpageyh pageno in
1830 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
1831 gotoxy state.x (clamp dy)
1832 | l :: _ when l.pageno = pageno ->
1833 if l.pagevh != l.pageh
1834 then gotoxy state.x (clamp (l.pageh - l.pagevh + conf.interpagespace))
1835 else postRedisplay "downbirdseye"
1836 | _ :: rest -> loop rest
1838 loop state.layout;
1839 state.text <- E.s;
1842 let optentry mode _ key =
1843 let btos b = if b then "on" else "off" in
1844 match [@warning "-4"] key with
1845 | Keys.Ascii 'C' ->
1846 let ondone s =
1848 let n, a, b = multicolumns_of_string s in
1849 setcolumns mode n a b;
1850 with exn ->
1851 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exn
1853 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
1855 | Keys.Ascii 'Z' ->
1856 let ondone s =
1858 let zoom = float (int_of_string s) /. 100.0 in
1859 pivotzoom zoom
1860 with exn ->
1861 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
1863 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
1865 | Keys.Ascii 'i' ->
1866 conf.icase <- not conf.icase;
1867 TEdone ("case insensitive search " ^ (btos conf.icase))
1869 | Keys.Ascii 'v' ->
1870 conf.verbose <- not conf.verbose;
1871 TEdone ("verbose " ^ (btos conf.verbose))
1873 | Keys.Ascii 'd' ->
1874 conf.debug <- not conf.debug;
1875 TEdone ("debug " ^ (btos conf.debug))
1877 | Keys.Ascii 'f' ->
1878 conf.underinfo <- not conf.underinfo;
1879 TEdone ("underinfo " ^ btos conf.underinfo)
1881 | Keys.Ascii 'l' ->
1882 let fm =
1883 match conf.fitmodel with
1884 | FitProportional -> FitWidth
1885 | FitWidth | FitPage -> FitProportional
1887 reqlayout conf.angle fm;
1888 TEdone ("proportional display " ^ btos (fm == FitProportional))
1890 | Keys.Ascii 'T' ->
1891 settrim (not conf.trimmargins) conf.trimfuzz;
1892 TEdone ("trim margins " ^ btos conf.trimmargins)
1894 | Keys.Ascii 'I' ->
1895 conf.invert <- not conf.invert;
1896 TEdone ("invert colors " ^ btos conf.invert)
1898 | Keys.Ascii 'x' ->
1899 let ondone s =
1900 cbput state.hists.sel s;
1901 conf.selcmd <- s;
1903 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
1904 textentry, ondone, true)
1906 | Keys.Ascii 'M' ->
1907 if conf.pax == None
1908 then conf.pax <- Some 0.0
1909 else conf.pax <- None;
1910 TEdone ("PAX " ^ btos (conf.pax != None))
1912 | (Keys.Ascii c) ->
1913 state.text <- Printf.sprintf "bad option %d `%c'" (Char.code c) c;
1914 TEstop
1916 | _ ->
1917 TEcont state.text
1920 let adderrmsg src msg =
1921 Buffer.add_string state.errmsgs msg;
1922 state.newerrmsgs <- true;
1923 postRedisplay src
1926 let adderrfmt src fmt =
1927 Format.ksprintf (fun s -> adderrmsg src s) fmt;
1930 class outlinelistview ~zebra ~source =
1931 let settext autonarrow s =
1932 if autonarrow
1933 then
1934 let ss = source#statestr in
1935 state.text <-
1936 if emptystr ss
1937 then "[" ^ s ^ "]"
1938 else "{" ^ ss ^ "} [" ^ s ^ "]"
1939 else state.text <- s
1941 object (self)
1942 inherit listview
1943 ~zebra
1944 ~helpmode:false
1945 ~source:(source :> lvsource)
1946 ~trusted:false
1947 ~modehash:(findkeyhash conf "outline")
1948 as super
1950 val m_autonarrow = false
1952 method! key key mask =
1953 let maxrows =
1954 if emptystr state.text
1955 then fstate.maxrows
1956 else fstate.maxrows - 2
1958 let calcfirst first active =
1959 if active > first
1960 then
1961 let rows = active - first in
1962 if rows > maxrows then active - maxrows else first
1963 else active
1965 let navigate incr =
1966 let active = m_active + incr in
1967 let active = bound active 0 (source#getitemcount - 1) in
1968 let first = calcfirst m_first active in
1969 postRedisplay "outline navigate";
1970 coe {< m_active = active; m_first = first >}
1972 let navscroll first =
1973 let active =
1974 let dist = m_active - first in
1975 if dist < 0
1976 then first
1977 else (
1978 if dist < maxrows
1979 then m_active
1980 else first + maxrows
1983 postRedisplay "outline navscroll";
1984 coe {< m_first = first; m_active = active >}
1986 let ctrl = Wsi.withctrl mask in
1987 let open Keys in
1988 match Wsi.kc2kt key with
1989 | Ascii 'a' when ctrl ->
1990 let text =
1991 if m_autonarrow
1992 then (source#denarrow; E.s)
1993 else (
1994 let pattern = source#renarrow in
1995 if nonemptystr m_qsearch
1996 then (source#narrow m_qsearch; m_qsearch)
1997 else pattern
2000 settext (not m_autonarrow) text;
2001 postRedisplay "toggle auto narrowing";
2002 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
2004 | Ascii '/' when emptystr m_qsearch && not m_autonarrow ->
2005 settext true E.s;
2006 postRedisplay "toggle auto narrowing";
2007 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
2009 | Ascii 'n' when ctrl ->
2010 source#narrow m_qsearch;
2011 if not m_autonarrow
2012 then source#add_narrow_pattern m_qsearch;
2013 postRedisplay "outline ctrl-n";
2014 coe {< m_first = 0; m_active = 0 >}
2016 | Ascii 'S' when ctrl ->
2017 let active = source#calcactive (getanchor ()) in
2018 let first = firstof m_first active in
2019 postRedisplay "outline ctrl-s";
2020 coe {< m_first = first; m_active = active >}
2022 | Ascii 'u' when ctrl ->
2023 postRedisplay "outline ctrl-u";
2024 if m_autonarrow && nonemptystr m_qsearch
2025 then (
2026 ignore (source#renarrow);
2027 settext m_autonarrow E.s;
2028 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
2030 else (
2031 source#del_narrow_pattern;
2032 let pattern = source#renarrow in
2033 let text =
2034 if emptystr pattern then E.s else "Narrowed to " ^ pattern
2036 settext m_autonarrow text;
2037 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
2040 | Ascii 'l' when ctrl ->
2041 let first = max 0 (m_active - (fstate.maxrows / 2)) in
2042 postRedisplay "outline ctrl-l";
2043 coe {< m_first = first >}
2045 | Ascii '\t' when m_autonarrow ->
2046 if nonemptystr m_qsearch
2047 then (
2048 postRedisplay "outline list view tab";
2049 source#add_narrow_pattern m_qsearch;
2050 settext true E.s;
2051 coe {< m_qsearch = E.s >}
2053 else coe self
2055 | Escape when m_autonarrow ->
2056 if nonemptystr m_qsearch
2057 then source#add_narrow_pattern m_qsearch;
2058 super#key key mask
2060 | Enter when m_autonarrow ->
2061 if nonemptystr m_qsearch
2062 then source#add_narrow_pattern m_qsearch;
2063 super#key key mask
2065 | (Ascii _ | Code _) when m_autonarrow ->
2066 let pattern = m_qsearch ^ toutf8 key in
2067 postRedisplay "outlinelistview autonarrow add";
2068 source#narrow pattern;
2069 settext true pattern;
2070 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
2072 | Backspace when m_autonarrow ->
2073 if emptystr m_qsearch
2074 then coe self
2075 else
2076 let pattern = withoutlastutf8 m_qsearch in
2077 postRedisplay "outlinelistview autonarrow backspace";
2078 ignore (source#renarrow);
2079 source#narrow pattern;
2080 settext true pattern;
2081 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
2083 | Up when ctrl ->
2084 navscroll (max 0 (m_first - 1))
2086 | Down when ctrl ->
2087 navscroll (min (source#getitemcount - 1) (m_first + 1))
2089 | Up -> navigate ~-1
2090 | Down -> navigate 1
2091 | Prior -> navigate ~-(fstate.maxrows)
2092 | Next -> navigate fstate.maxrows
2094 | Right ->
2095 let o =
2096 if ctrl
2097 then (
2098 postRedisplay "outline ctrl right";
2099 {< m_pan = m_pan + 1 >}
2101 else self#updownlevel 1
2103 coe o
2105 | Left ->
2106 let o =
2107 if ctrl
2108 then (
2109 postRedisplay "outline ctrl left";
2110 {< m_pan = m_pan - 1 >}
2112 else self#updownlevel ~-1
2114 coe o
2116 | Home ->
2117 postRedisplay "outline home";
2118 coe {< m_first = 0; m_active = 0 >}
2120 | End ->
2121 let active = source#getitemcount - 1 in
2122 let first = max 0 (active - fstate.maxrows) in
2123 postRedisplay "outline end";
2124 coe {< m_active = active; m_first = first >}
2126 | Delete|Escape|Insert|Enter|Ascii _|Code _|Ctrl _|Backspace|Fn _ ->
2127 super#key key mask
2128 end;;
2130 let genhistoutlines () =
2131 Config.gethist ()
2132 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
2133 compare c2.lastvisit c1.lastvisit)
2134 |> List.map (fun ((path, c, _, _, _, origin) as hist) ->
2135 let path = if nonemptystr origin then origin else path in
2136 let base = mbtoutf8 @@ Filename.basename path in
2137 (base ^ "\000" ^ c.title, 1, Ohistory hist)
2141 let gotohist (path, c, bookmarks, x, anchor, origin) =
2142 Config.save leavebirdseye;
2143 state.anchor <- anchor;
2144 state.bookmarks <- bookmarks;
2145 state.origin <- origin;
2146 state.x <- x;
2147 setconf conf c;
2148 let x0, y0, x1, y1 = conf.trimfuzz in
2149 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
2150 reshape ~firsttime:true state.winw state.winh;
2151 opendoc path origin;
2152 setzoom c.zoom;
2155 let setcheckers enabled =
2156 match state.checkerstexid with
2157 | None ->
2158 if enabled then state.checkerstexid <- Some (makecheckers ())
2160 | Some checkerstexid ->
2161 if not enabled
2162 then (
2163 GlTex.delete_texture checkerstexid;
2164 state.checkerstexid <- None;
2168 let describe_layout layout =
2169 let d =
2170 match layout with
2171 | [] -> "Page 0"
2172 | l :: [] -> Printf.sprintf "Page %d" (l.pageno+1)
2173 | l :: rest ->
2174 let rangestr a b =
2175 if a.pageno = b.pageno then Printf.sprintf "%d" (a.pageno+1)
2176 else
2177 let sep = if a.pageno+1 = b.pageno then ", " else Unisyms.ellipsis in
2178 Printf.sprintf "%d%s%d" (a.pageno+1) sep (b.pageno+1)
2180 let rec fold s la lb = function
2181 | [] -> Printf.sprintf "%s %s" s (rangestr la lb)
2182 | l :: rest when l.pageno = succ lb.pageno -> fold s la l rest
2183 | l :: rest -> fold (s ^ " " ^ rangestr la lb ^ ",") l l rest
2185 fold "Pages" l l rest
2187 let percent =
2188 let maxy = maxy () in
2189 if maxy <= 0
2190 then 100.
2191 else 100. *. (float state.y /. float maxy)
2193 Printf.sprintf "%s of %d [%.2f%%]" d state.pagecount percent
2196 let setpresentationmode v =
2197 let n = page_of_y state.y in
2198 state.anchor <- (n, 0.0, 1.0);
2199 conf.presentation <- v;
2200 if conf.fitmodel = FitPage
2201 then reqlayout conf.angle conf.fitmodel;
2202 represent ();
2205 let enterinfomode =
2206 let btos b = if b then Unisyms.radical else E.s in
2207 let showextended = ref false in
2208 let showcolors = ref false in
2209 let leave mode _ = state.mode <- mode in
2210 let src =
2211 (object
2212 val mutable m_l = []
2213 val mutable m_a = E.a
2214 val mutable m_prev_uioh = nouioh
2215 val mutable m_prev_mode = View
2217 inherit lvsourcebase
2219 method reset prev_mode prev_uioh =
2220 m_a <- Array.of_list (List.rev m_l);
2221 m_l <- [];
2222 m_prev_mode <- prev_mode;
2223 m_prev_uioh <- prev_uioh;
2225 method int name get set =
2226 m_l <-
2227 (name, `int get, 1,
2228 Action (
2229 fun u ->
2230 let ondone s =
2231 try set (int_of_string s)
2232 with exn ->
2233 state.text <- Printf.sprintf "bad integer `%s': %s"
2234 s @@ exntos exn
2236 state.text <- E.s;
2237 let te = name ^ ": ", E.s, None, intentry, ondone, true in
2238 state.mode <- Textentry (te, leave m_prev_mode);
2240 )) :: m_l
2242 method int_with_suffix name get set =
2243 m_l <-
2244 (name, `intws get, 1,
2245 Action (
2246 fun u ->
2247 let ondone s =
2248 try set (int_of_string_with_suffix s)
2249 with exn ->
2250 state.text <- Printf.sprintf "bad integer `%s': %s"
2251 s @@ exntos exn
2253 state.text <- E.s;
2254 let te =
2255 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
2257 state.mode <- Textentry (te, leave m_prev_mode);
2259 )) :: m_l
2261 method bool ?(offset=1) ?(btos=btos) name get set =
2262 m_l <-
2263 (name, `bool (btos, get), offset, Action (
2264 fun u ->
2265 let v = get () in
2266 set (not v);
2268 )) :: m_l
2270 method color name get set =
2271 m_l <-
2272 (name, `color get, 1,
2273 Action (
2274 fun u ->
2275 let invalid = (nan, nan, nan) in
2276 let ondone s =
2277 let c =
2278 try color_of_string s
2279 with exn ->
2280 state.text <- Printf.sprintf "bad color `%s': %s"
2281 s @@ exntos exn;
2282 invalid
2284 if c <> invalid
2285 then set c;
2287 let te = name ^ ": ", E.s, None, textentry, ondone, true in
2288 state.text <- color_to_string (get ());
2289 state.mode <- Textentry (te, leave m_prev_mode);
2291 )) :: m_l
2293 method string name get set =
2294 m_l <-
2295 (name, `string get, 1,
2296 Action (
2297 fun u ->
2298 let ondone s = set s in
2299 let te = name ^ ": ", E.s, None, textentry, ondone, true in
2300 state.mode <- Textentry (te, leave m_prev_mode);
2302 )) :: m_l
2304 method colorspace name get set =
2305 m_l <-
2306 (name, `string get, 1,
2307 Action (
2308 fun _ ->
2309 let source =
2310 (object
2311 inherit lvsourcebase
2313 initializer
2314 m_active <- CSTE.to_int conf.colorspace;
2315 m_first <- 0;
2317 method getitemcount =
2318 Array.length CSTE.names
2319 method getitem n =
2320 (CSTE.names.(n), 0)
2321 method exit ~uioh ~cancel ~active ~first ~pan =
2322 ignore (uioh, first, pan);
2323 if not cancel then set active;
2324 None
2325 method hasaction _ = true
2326 end)
2328 state.text <- E.s;
2329 let modehash = findkeyhash conf "info" in
2330 coe (new listview ~zebra:false ~helpmode:false
2331 ~source ~trusted:true ~modehash)
2332 )) :: m_l
2334 method paxmark name get set =
2335 m_l <-
2336 (name, `string get, 1,
2337 Action (
2338 fun _ ->
2339 let source =
2340 (object
2341 inherit lvsourcebase
2343 initializer
2344 m_active <- MTE.to_int conf.paxmark;
2345 m_first <- 0;
2347 method getitemcount = Array.length MTE.names
2348 method getitem n = (MTE.names.(n), 0)
2349 method exit ~uioh ~cancel ~active ~first ~pan =
2350 ignore (uioh, first, pan);
2351 if not cancel then set active;
2352 None
2353 method hasaction _ = true
2354 end)
2356 state.text <- E.s;
2357 let modehash = findkeyhash conf "info" in
2358 coe (new listview ~zebra:false ~helpmode:false
2359 ~source ~trusted:true ~modehash)
2360 )) :: m_l
2362 method fitmodel name get set =
2363 m_l <-
2364 (name, `string get, 1,
2365 Action (
2366 fun _ ->
2367 let source =
2368 (object
2369 inherit lvsourcebase
2371 initializer
2372 m_active <- FMTE.to_int conf.fitmodel;
2373 m_first <- 0;
2375 method getitemcount = Array.length FMTE.names
2376 method getitem n = (FMTE.names.(n), 0)
2377 method exit ~uioh ~cancel ~active ~first ~pan =
2378 ignore (uioh, first, pan);
2379 if not cancel then set active;
2380 None
2381 method hasaction _ = true
2382 end)
2384 state.text <- E.s;
2385 let modehash = findkeyhash conf "info" in
2386 coe (new listview ~zebra:false ~helpmode:false
2387 ~source ~trusted:true ~modehash)
2388 )) :: m_l
2390 method caption s offset =
2391 m_l <- (s, `empty, offset, Noaction) :: m_l
2393 method caption2 s f offset =
2394 m_l <- (s, `string f, offset, Noaction) :: m_l
2396 method getitemcount = Array.length m_a
2398 method getitem n =
2399 let tostr = function
2400 | `int f -> string_of_int (f ())
2401 | `intws f -> string_with_suffix_of_int (f ())
2402 | `string f -> f ()
2403 | `color f -> color_to_string (f ())
2404 | `bool (btos, f) -> btos (f ())
2405 | `empty -> E.s
2407 let name, t, offset, _ = m_a.(n) in
2408 ((let s = tostr t in
2409 if nonemptystr s
2410 then Printf.sprintf "%s\t%s" name s
2411 else name),
2412 offset)
2414 method exit ~uioh ~cancel ~active ~first ~pan =
2415 let uiohopt =
2416 if not cancel
2417 then (
2418 let uioh =
2419 match m_a.(active) with
2420 | _, _, _, Action f -> f uioh
2421 | _, _, _, Noaction -> uioh
2423 Some uioh
2425 else None
2427 m_active <- active;
2428 m_first <- first;
2429 m_pan <- pan;
2430 uiohopt
2432 method hasaction n =
2433 match m_a.(n) with
2434 | _, _, _, Action _ -> true
2435 | _, _, _, Noaction -> false
2437 initializer m_active <- 1
2438 end)
2440 let rec fillsrc prevmode prevuioh =
2441 let sep () = src#caption E.s 0 in
2442 let colorp name get set =
2443 src#string name
2444 (fun () -> color_to_string (get ()))
2445 (fun v ->
2447 let c = color_of_string v in
2448 set c
2449 with exn ->
2450 state.text <-
2451 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
2454 let rgba name get set =
2455 src#string name
2456 (fun () -> rgba_to_string (get ()))
2457 (fun v ->
2459 let c = rgba_of_string v in
2460 set c
2461 with exn ->
2462 state.text <-
2463 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
2466 let oldmode = state.mode in
2467 let birdseye = isbirdseye state.mode in
2469 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
2471 src#bool "presentation mode"
2472 (fun () -> conf.presentation)
2473 (fun v -> setpresentationmode v);
2475 src#bool "ignore case in searches"
2476 (fun () -> conf.icase)
2477 (fun v -> conf.icase <- v);
2479 src#bool "preload"
2480 (fun () -> conf.preload)
2481 (fun v -> conf.preload <- v);
2483 src#bool "highlight links"
2484 (fun () -> conf.hlinks)
2485 (fun v -> conf.hlinks <- v);
2487 src#bool "under info"
2488 (fun () -> conf.underinfo)
2489 (fun v -> conf.underinfo <- v);
2491 src#fitmodel "fit model"
2492 (fun () -> FMTE.to_string conf.fitmodel)
2493 (fun v -> reqlayout conf.angle (FMTE.of_int v));
2495 src#bool "trim margins"
2496 (fun () -> conf.trimmargins)
2497 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
2499 sep ();
2500 src#int "inter-page space"
2501 (fun () -> conf.interpagespace)
2502 (fun n ->
2503 conf.interpagespace <- n;
2504 docolumns conf.columns;
2505 let pageno, py =
2506 match state.layout with
2507 | [] -> 0, 0
2508 | l :: _ ->
2509 l.pageno, l.pagey
2511 state.maxy <- calcheight ();
2512 let y = getpagey pageno in
2513 gotoxy state.x (y + py)
2516 src#int "page bias"
2517 (fun () -> conf.pagebias)
2518 (fun v -> conf.pagebias <- v);
2520 src#int "scroll step"
2521 (fun () -> conf.scrollstep)
2522 (fun n -> conf.scrollstep <- n);
2524 src#int "horizontal scroll step"
2525 (fun () -> conf.hscrollstep)
2526 (fun v -> conf.hscrollstep <- v);
2528 src#int "auto scroll step"
2529 (fun () ->
2530 match state.autoscroll with
2531 | Some step -> step
2532 | _ -> conf.autoscrollstep)
2533 (fun n ->
2534 let n = boundastep state.winh n in
2535 if state.autoscroll <> None
2536 then state.autoscroll <- Some n;
2537 conf.autoscrollstep <- n);
2539 src#int "zoom"
2540 (fun () -> truncate (conf.zoom *. 100.))
2541 (fun v -> pivotzoom ((float v) /. 100.));
2543 src#int "rotation"
2544 (fun () -> conf.angle)
2545 (fun v -> reqlayout v conf.fitmodel);
2547 src#int "scroll bar width"
2548 (fun () -> conf.scrollbw)
2549 (fun v ->
2550 conf.scrollbw <- v;
2551 reshape state.winw state.winh;
2554 src#int "scroll handle height"
2555 (fun () -> conf.scrollh)
2556 (fun v -> conf.scrollh <- v;);
2558 src#int "thumbnail width"
2559 (fun () -> conf.thumbw)
2560 (fun v ->
2561 conf.thumbw <- min 4096 v;
2562 match oldmode with
2563 | Birdseye beye ->
2564 leavebirdseye beye false;
2565 enterbirdseye ()
2566 | Textentry _
2567 | View
2568 | LinkNav _ -> ()
2571 let mode = state.mode in
2572 src#string "columns"
2573 (fun () ->
2574 match conf.columns with
2575 | Csingle _ -> "1"
2576 | Cmulti (multi, _) -> multicolumns_to_string multi
2577 | Csplit (count, _) -> "-" ^ string_of_int count
2579 (fun v ->
2580 let n, a, b = multicolumns_of_string v in
2581 setcolumns mode n a b);
2583 sep ();
2584 src#caption "Pixmap cache" 0;
2585 src#int_with_suffix "size (advisory)"
2586 (fun () -> conf.memlimit)
2587 (fun v -> conf.memlimit <- v);
2589 src#caption2 "used"
2590 (fun () ->
2591 Printf.sprintf "%s bytes, %d tiles"
2592 (string_with_suffix_of_int state.memused)
2593 (Hashtbl.length state.tilemap)) 1;
2595 sep ();
2596 src#caption "Layout" 0;
2597 src#caption2 "Dimension"
2598 (fun () ->
2599 Printf.sprintf "%dx%d (virtual %dx%d)"
2600 state.winw state.winh
2601 state.w state.maxy)
2603 if conf.debug
2604 then
2605 src#caption2 "Position" (fun () ->
2606 Printf.sprintf "%dx%d" state.x state.y
2608 else
2609 src#caption2 "Position" (fun () -> describe_layout state.layout) 1;
2611 sep ();
2612 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
2613 "Save these parameters as global defaults at exit"
2614 (fun () -> conf.bedefault)
2615 (fun v -> conf.bedefault <- v);
2617 sep ();
2618 let btos b = if b then Unisyms.lguillemet else Unisyms.rguillemet in
2619 src#bool ~offset:0 ~btos "Extended parameters"
2620 (fun () -> !showextended)
2621 (fun v -> showextended := v; fillsrc prevmode prevuioh);
2622 if !showextended
2623 then (
2624 src#bool "checkers"
2625 (fun () -> conf.checkers)
2626 (fun v -> conf.checkers <- v; setcheckers v);
2627 src#bool "update cursor"
2628 (fun () -> conf.updatecurs)
2629 (fun v -> conf.updatecurs <- v);
2630 src#bool "scroll-bar on the left"
2631 (fun () -> conf.leftscroll)
2632 (fun v -> conf.leftscroll <- v);
2633 src#bool "verbose"
2634 (fun () -> conf.verbose)
2635 (fun v -> conf.verbose <- v);
2636 src#bool "invert colors"
2637 (fun () -> conf.invert)
2638 (fun v -> conf.invert <- v);
2639 src#bool "max fit"
2640 (fun () -> conf.maxhfit)
2641 (fun v -> conf.maxhfit <- v);
2642 src#bool "pax mode"
2643 (fun () -> conf.pax != None)
2644 (fun v ->
2645 if v
2646 then conf.pax <- Some (now ())
2647 else conf.pax <- None);
2648 src#string "uri launcher"
2649 (fun () -> conf.urilauncher)
2650 (fun v -> conf.urilauncher <- v);
2651 src#string "path launcher"
2652 (fun () -> conf.pathlauncher)
2653 (fun v -> conf.pathlauncher <- v);
2654 src#string "tile size"
2655 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
2656 (fun v ->
2658 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
2659 conf.tilew <- max 64 w;
2660 conf.tileh <- max 64 h;
2661 flushtiles ();
2662 with exn ->
2663 state.text <- Printf.sprintf "bad tile size `%s': %s"
2664 v @@ exntos exn
2666 src#int "texture count"
2667 (fun () -> conf.texcount)
2668 (fun v ->
2669 if realloctexts v
2670 then conf.texcount <- v
2671 else impmsg "failed to set texture count please retry later"
2673 src#int "slice height"
2674 (fun () -> conf.sliceheight)
2675 (fun v ->
2676 conf.sliceheight <- v;
2677 wcmd "sliceh %d" conf.sliceheight;
2679 src#int "anti-aliasing level"
2680 (fun () -> conf.aalevel)
2681 (fun v ->
2682 conf.aalevel <- bound v 0 8;
2683 state.anchor <- getanchor ();
2684 opendoc state.path state.password;
2686 src#string "page scroll scaling factor"
2687 (fun () -> string_of_float conf.pgscale)
2688 (fun v ->
2690 let s = float_of_string v in
2691 conf.pgscale <- s
2692 with exn ->
2693 state.text <- Printf.sprintf
2694 "bad page scroll scaling factor `%s': %s" v
2695 @@ exntos exn
2698 src#int "ui font size"
2699 (fun () -> fstate.fontsize)
2700 (fun v -> setfontsize (bound v 5 100));
2701 src#int "hint font size"
2702 (fun () -> conf.hfsize)
2703 (fun v -> conf.hfsize <- bound v 5 100);
2704 src#string "trim fuzz"
2705 (fun () -> irect_to_string conf.trimfuzz)
2706 (fun v ->
2708 conf.trimfuzz <- irect_of_string v;
2709 if conf.trimmargins
2710 then settrim true conf.trimfuzz;
2711 with exn ->
2712 state.text <- Printf.sprintf "bad irect `%s': %s" v
2713 @@ exntos exn
2715 src#string "selection command"
2716 (fun () -> conf.selcmd)
2717 (fun v -> conf.selcmd <- v);
2718 src#string "synctex command"
2719 (fun () -> conf.stcmd)
2720 (fun v -> conf.stcmd <- v);
2721 src#string "pax command"
2722 (fun () -> conf.paxcmd)
2723 (fun v -> conf.paxcmd <- v);
2724 src#string "ask password command"
2725 (fun () -> conf.passcmd)
2726 (fun v -> conf.passcmd <- v);
2727 src#string "save path command"
2728 (fun () -> conf.savecmd)
2729 (fun v -> conf.savecmd <- v);
2730 src#colorspace "color space"
2731 (fun () -> CSTE.to_string conf.colorspace)
2732 (fun v ->
2733 conf.colorspace <- CSTE.of_int v;
2734 wcmd "cs %d" v;
2735 load state.layout;
2737 src#paxmark "pax mark method"
2738 (fun () -> MTE.to_string conf.paxmark)
2739 (fun v -> conf.paxmark <- MTE.of_int v);
2740 if bousable () && !opengl_has_pbo
2741 then
2742 src#bool "use PBO"
2743 (fun () -> conf.usepbo)
2744 (fun v -> conf.usepbo <- v);
2745 src#bool "mouse wheel scrolls pages"
2746 (fun () -> conf.wheelbypage)
2747 (fun v -> conf.wheelbypage <- v);
2748 src#bool "open remote links in a new instance"
2749 (fun () -> conf.riani)
2750 (fun v -> conf.riani <- v);
2751 src#bool "edit annotations inline"
2752 (fun () -> conf.annotinline)
2753 (fun v -> conf.annotinline <- v);
2754 src#bool "coarse positioning in presentation mode"
2755 (fun () -> conf.coarseprespos)
2756 (fun v -> conf.coarseprespos <- v);
2757 src#bool "use document CSS"
2758 (fun () -> conf.usedoccss)
2759 (fun v ->
2760 conf.usedoccss <- v;
2761 state.anchor <- getanchor ();
2762 opendoc state.path state.password;
2764 src#bool ~btos "colors"
2765 (fun () -> !showcolors)
2766 (fun v -> showcolors := v; fillsrc prevmode prevuioh);
2767 if !showcolors
2768 then (
2769 colorp " background"
2770 (fun () -> conf.bgcolor)
2771 (fun v -> conf.bgcolor <- v);
2772 rgba " scrollbar"
2773 (fun () -> conf.sbarcolor)
2774 (fun v -> conf.sbarcolor <- v);
2775 rgba " scrollbar handle"
2776 (fun () -> conf.sbarhndlcolor)
2777 (fun v -> conf.sbarhndlcolor <- v);
2781 sep ();
2782 src#caption "Document" 0;
2783 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
2784 src#caption2 "Pages"
2785 (fun () -> string_of_int state.pagecount) 1;
2786 src#caption2 "Dimensions"
2787 (fun () -> string_of_int (List.length state.pdims)) 1;
2788 if nonemptystr conf.css
2789 then src#caption2 "CSS" (fun () -> conf.css) 1;
2790 if conf.trimmargins
2791 then (
2792 sep ();
2793 src#caption "Trimmed margins" 0;
2794 src#caption2 "Dimensions"
2795 (fun () -> string_of_int (List.length state.pdims)) 1;
2798 sep ();
2799 src#caption "OpenGL" 0;
2800 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
2801 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
2803 sep ();
2804 src#caption "Location" 0;
2805 if nonemptystr state.origin
2806 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
2807 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
2809 src#reset prevmode prevuioh;
2811 fun () ->
2812 state.text <- E.s;
2813 resetmstate ();
2814 let prevmode = state.mode
2815 and prevuioh = state.uioh in
2816 fillsrc prevmode prevuioh;
2817 let source = (src :> lvsource) in
2818 let modehash = findkeyhash conf "info" in
2819 state.uioh <-
2820 coe (object (self)
2821 inherit listview ~zebra:false ~helpmode:false
2822 ~source ~trusted:true ~modehash as super
2823 val mutable m_prevmemused = 0
2824 method! infochanged = function
2825 | Memused ->
2826 if m_prevmemused != state.memused
2827 then (
2828 m_prevmemused <- state.memused;
2829 postRedisplay "memusedchanged";
2831 | Pdim -> postRedisplay "pdimchanged"
2832 | Docinfo -> fillsrc prevmode prevuioh
2834 method! key key mask =
2835 if not (Wsi.withctrl mask)
2836 then
2837 match [@warning "-4"] Wsi.kc2kt key with
2838 | Keys.Left -> coe (self#updownlevel ~-1)
2839 | Keys.Right -> coe (self#updownlevel 1)
2840 | _ -> super#key key mask
2841 else super#key key mask
2842 end);
2843 postRedisplay "info";
2846 let enterhelpmode =
2847 let source =
2848 (object
2849 inherit lvsourcebase
2850 method getitemcount = Array.length state.help
2851 method getitem n =
2852 let s, l, _ = state.help.(n) in
2853 (s, l)
2855 method exit ~uioh ~cancel ~active ~first ~pan =
2856 let optuioh =
2857 if not cancel
2858 then (
2859 match state.help.(active) with
2860 | _, _, Action f -> Some (f uioh)
2861 | _, _, Noaction -> Some uioh
2863 else None
2865 m_active <- active;
2866 m_first <- first;
2867 m_pan <- pan;
2868 optuioh
2870 method hasaction n =
2871 match state.help.(n) with
2872 | _, _, Action _ -> true
2873 | _, _, Noaction -> false
2875 initializer
2876 m_active <- -1
2877 end)
2878 in fun () ->
2879 let modehash = findkeyhash conf "help" in
2880 resetmstate ();
2881 state.uioh <- coe (new listview
2882 ~zebra:false ~helpmode:true
2883 ~source ~trusted:true ~modehash);
2884 postRedisplay "help";
2887 let entermsgsmode =
2888 let msgsource =
2889 (object
2890 inherit lvsourcebase
2891 val mutable m_items = E.a
2893 method getitemcount = 1 + Array.length m_items
2895 method getitem n =
2896 if n = 0
2897 then "[Clear]", 0
2898 else m_items.(n-1), 0
2900 method exit ~uioh ~cancel ~active ~first ~pan =
2901 ignore uioh;
2902 if not cancel
2903 then (
2904 if active = 0
2905 then Buffer.clear state.errmsgs;
2907 m_active <- active;
2908 m_first <- first;
2909 m_pan <- pan;
2910 None
2912 method hasaction n =
2913 n = 0
2915 method reset =
2916 state.newerrmsgs <- false;
2917 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
2918 m_items <- Array.of_list l
2920 initializer
2921 m_active <- 0
2922 end)
2923 in fun () ->
2924 state.text <- E.s;
2925 resetmstate ();
2926 msgsource#reset;
2927 let source = (msgsource :> lvsource) in
2928 let modehash = findkeyhash conf "listview" in
2929 state.uioh <-
2930 coe (object
2931 inherit listview ~zebra:false ~helpmode:false
2932 ~source ~trusted:false ~modehash as super
2933 method! display =
2934 if state.newerrmsgs
2935 then msgsource#reset;
2936 super#display
2937 end);
2938 postRedisplay "msgs";
2941 let getusertext s =
2942 let editor = getenvwithdef "EDITOR" E.s in
2943 if emptystr editor
2944 then E.s
2945 else
2946 let tmppath = Filename.temp_file "llpp" "note" in
2947 if nonemptystr s
2948 then (
2949 let oc = open_out tmppath in
2950 output_string oc s;
2951 close_out oc;
2953 let execstr = editor ^ " " ^ tmppath in
2954 let s =
2955 match spawn execstr [] with
2956 | exception exn ->
2957 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
2959 | pid ->
2960 match Unix.waitpid [] pid with
2961 | exception exn ->
2962 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
2964 | (_pid, status) ->
2965 match status with
2966 | Unix.WEXITED 0 -> filecontents tmppath
2967 | Unix.WEXITED n ->
2968 impmsg "editor process(%s) exited abnormally: %d" execstr n;
2970 | Unix.WSIGNALED n ->
2971 impmsg "editor process(%s) was killed by signal %d" execstr n;
2973 | Unix.WSTOPPED n ->
2974 impmsg "editor(%s) process was stopped by signal %d" execstr n;
2977 match Unix.unlink tmppath with
2978 | exception exn ->
2979 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
2981 | () -> s
2984 let enterannotmode opaque slinkindex =
2985 let msgsource =
2986 (object
2987 inherit lvsourcebase
2988 val mutable m_text = E.s
2989 val mutable m_items = E.a
2991 method getitemcount = Array.length m_items
2993 method getitem n =
2994 let label, _func = m_items.(n) in
2995 label, 0
2997 method exit ~uioh ~cancel ~active ~first ~pan =
2998 ignore (uioh, first, pan);
2999 if not cancel
3000 then (
3001 let _label, func = m_items.(active) in
3002 func ()
3004 None
3006 method hasaction n = nonemptystr @@ fst m_items.(n)
3008 method reset s =
3009 let rec split accu b i =
3010 let p = b+i in
3011 if p = String.length s
3012 then (String.sub s b (p-b), unit) :: accu
3013 else
3014 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
3015 then
3016 let ss = if i = 0 then E.s else String.sub s b i in
3017 split ((ss, unit)::accu) (p+1) 0
3018 else
3019 split accu b (i+1)
3021 let cleanup () =
3022 wcmd "freepage %s" (~> opaque);
3023 let keys =
3024 Hashtbl.fold (fun key opaque' accu ->
3025 if opaque' = opaque'
3026 then key :: accu else accu) state.pagemap []
3028 List.iter (Hashtbl.remove state.pagemap) keys;
3029 flushtiles ();
3030 gotoxy state.x state.y
3032 let dele () =
3033 delannot opaque slinkindex;
3034 cleanup ();
3036 let edit inline () =
3037 let update s =
3038 if emptystr s
3039 then dele ()
3040 else (
3041 modannot opaque slinkindex s;
3042 cleanup ();
3045 if inline
3046 then
3047 let mode = state.mode in
3048 state.mode <-
3049 Textentry (
3050 ("annotation: ", m_text, None, textentry, update, true),
3051 fun _ -> state.mode <- mode);
3052 state.text <- E.s;
3053 enttext ();
3054 else
3055 let s = getusertext m_text in
3056 update s
3058 m_text <- s;
3059 m_items <-
3060 ( "[Copy]", fun () -> selstring conf.selcmd m_text)
3061 :: ("[Delete]", dele)
3062 :: ("[Edit]", edit conf.annotinline)
3063 :: (E.s, unit)
3064 :: split [] 0 0 |> List.rev |> Array.of_list
3066 initializer
3067 m_active <- 0
3068 end)
3070 state.text <- E.s;
3071 let s = getannotcontents opaque slinkindex in
3072 resetmstate ();
3073 msgsource#reset s;
3074 let source = (msgsource :> lvsource) in
3075 let modehash = findkeyhash conf "listview" in
3076 state.uioh <- coe (object
3077 inherit listview ~zebra:false ~helpmode:false
3078 ~source ~trusted:false ~modehash
3079 end);
3080 postRedisplay "enterannotmode";
3083 let gotoremote spec =
3084 let filename, dest = splitatchar spec '#' in
3085 let getpath filename =
3086 let path =
3087 if nonemptystr filename
3088 then
3089 if Filename.is_relative filename
3090 then
3091 let dir = Filename.dirname state.path in
3092 let dir =
3093 if Filename.is_implicit dir
3094 then Filename.concat (Sys.getcwd ()) dir
3095 else dir
3097 Filename.concat dir filename
3098 else filename
3099 else E.s
3101 if Sys.file_exists path
3102 then path
3103 else E.s
3105 let path = getpath filename in
3106 let dospawn lcmd =
3107 if conf.riani
3108 then
3109 let cmd = Lazy.force_val lcmd in
3110 match spawn cmd with
3111 | _pid -> ()
3112 | exception exn -> dolog "failed to execute `%s': %s" cmd @@ exntos exn
3113 else
3114 let anchor = getanchor () in
3115 let ranchor = state.path, state.password, anchor, state.origin in
3116 state.origin <- E.s;
3117 state.ranchors <- ranchor :: state.ranchors;
3118 opendoc path E.s;
3120 if substratis spec 0 "page="
3121 then
3122 match Scanf.sscanf spec "page=%d" (fun n -> n) with
3123 | pageno ->
3124 state.anchor <- (pageno, 0.0, 0.0);
3125 dospawn @@ lazy (Printf.sprintf "%s -page %d %S" !selfexec pageno path);
3126 | exception exn ->
3127 adderrfmt "error parsing remote destination" "page: %s" @@ exntos exn
3128 else (
3129 state.nameddest <- dest;
3130 dospawn @@ lazy (!selfexec ^ " " ^ path ^ " -dest " ^ dest)
3134 let gotounder = function
3135 | Ulinkuri s when isexternallink s ->
3136 if substratis s 0 "file://"
3137 then gotoremote @@ String.sub s 7 (String.length s - 7)
3138 else Help.gotouri conf.urilauncher s
3139 | Ulinkuri s ->
3140 let pageno, x, y = uritolocation s in
3141 addnav ();
3142 gotopagexy pageno x y
3143 | Utext _ | Unone -> ()
3144 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
3147 let gotooutline (_, _, kind) =
3148 match kind with
3149 | Onone -> ()
3150 | Oanchor anchor ->
3151 let (pageno, y, _) = anchor in
3152 let y = getanchory
3153 (if conf.presentation then (pageno, y, 1.0) else anchor)
3155 addnav ();
3156 gotoxy state.x y
3157 | Ouri uri -> gotounder (Ulinkuri uri)
3158 | Olaunch _cmd -> failwith "gotounder (Ulaunch cmd)"
3159 | Oremote _remote -> failwith "gotounder (Uremote remote)"
3160 | Ohistory hist -> gotohist hist
3161 | Oremotedest _remotedest -> failwith "gotounder (Uremotedest remotedest)"
3164 class outlinesoucebase fetchoutlines = object (self)
3165 inherit lvsourcebase
3166 val mutable m_items = E.a
3167 val mutable m_minfo = E.a
3168 val mutable m_orig_items = E.a
3169 val mutable m_orig_minfo = E.a
3170 val mutable m_narrow_patterns = []
3171 val mutable m_gen = -1
3173 method getitemcount = Array.length m_items
3175 method getitem n =
3176 let s, n, _ = m_items.(n) in
3177 (s, n+0)
3179 method exit ~(uioh:uioh) ~cancel ~active ~(first:int) ~pan : uioh option =
3180 ignore (uioh, first);
3181 let items, minfo =
3182 if m_narrow_patterns = []
3183 then m_orig_items, m_orig_minfo
3184 else m_items, m_minfo
3186 m_pan <- pan;
3187 if not cancel
3188 then (
3189 m_items <- items;
3190 m_minfo <- minfo;
3191 gotooutline m_items.(active);
3193 else (
3194 m_items <- items;
3195 m_minfo <- minfo;
3197 None
3199 method hasaction (_:int) = true
3201 method greetmsg =
3202 if Array.length m_items != Array.length m_orig_items
3203 then
3204 let s =
3205 match m_narrow_patterns with
3206 | one :: [] -> one
3207 | many -> String.concat Unisyms.ellipsis (List.rev many)
3209 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
3210 else E.s
3212 method statestr =
3213 match m_narrow_patterns with
3214 | [] -> E.s
3215 | one :: [] -> one
3216 | head :: _ -> Unisyms.ellipsis ^ head
3218 method narrow pattern =
3219 match Str.regexp_case_fold pattern with
3220 | exception _ -> ()
3221 | re ->
3222 let rec loop accu minfo n =
3223 if n = -1
3224 then (
3225 m_items <- Array.of_list accu;
3226 m_minfo <- Array.of_list minfo;
3228 else
3229 let (s, _, _) as o = m_items.(n) in
3230 let accu, minfo =
3231 match Str.search_forward re s 0 with
3232 | exception Not_found -> accu, minfo
3233 | first -> o :: accu, (first, Str.match_end ()) :: minfo
3235 loop accu minfo (n-1)
3237 loop [] [] (Array.length m_items - 1)
3239 method! getminfo = m_minfo
3241 method denarrow =
3242 m_orig_items <- fetchoutlines ();
3243 m_minfo <- m_orig_minfo;
3244 m_items <- m_orig_items
3246 method add_narrow_pattern pattern =
3247 m_narrow_patterns <- pattern :: m_narrow_patterns
3249 method del_narrow_pattern =
3250 match m_narrow_patterns with
3251 | _ :: rest -> m_narrow_patterns <- rest
3252 | [] -> ()
3254 method renarrow =
3255 self#denarrow;
3256 match m_narrow_patterns with
3257 | pattern :: [] -> self#narrow pattern; pattern
3258 | list ->
3259 List.fold_left (fun accu pattern ->
3260 self#narrow pattern;
3261 pattern ^ Unisyms.ellipsis ^ accu) E.s list
3263 method calcactive (_:anchor) = 0
3265 method reset anchor items =
3266 if state.gen != m_gen
3267 then (
3268 m_orig_items <- items;
3269 m_items <- items;
3270 m_narrow_patterns <- [];
3271 m_minfo <- E.a;
3272 m_orig_minfo <- E.a;
3273 m_gen <- state.gen;
3275 else (
3276 if items != m_orig_items
3277 then (
3278 m_orig_items <- items;
3279 if m_narrow_patterns == []
3280 then m_items <- items;
3283 let active = self#calcactive anchor in
3284 m_active <- active;
3285 m_first <- firstof m_first active
3289 let outlinesource fetchoutlines =
3290 (object
3291 inherit outlinesoucebase fetchoutlines
3292 method! calcactive anchor =
3293 let rely = getanchory anchor in
3294 let rec loop n best bestd =
3295 if n = Array.length m_items
3296 then best
3297 else
3298 let _, _, kind = m_items.(n) in
3299 match kind with
3300 | Oanchor anchor ->
3301 let orely = getanchory anchor in
3302 let d = abs (orely - rely) in
3303 if d < bestd
3304 then loop (n+1) n d
3305 else loop (n+1) best bestd
3306 | Onone | Oremote _ | Olaunch _
3307 | Oremotedest _ | Ouri _ | Ohistory _ ->
3308 loop (n+1) best bestd
3310 loop 0 ~-1 max_int
3311 end)
3314 let enteroutlinemode, enterbookmarkmode, enterhistmode =
3315 let mkselector sourcetype =
3316 let fetchoutlines () =
3317 match sourcetype with
3318 | `bookmarks -> Array.of_list state.bookmarks
3319 | `outlines -> state.outlines
3320 | `history -> genhistoutlines () |> Array.of_list
3322 let source =
3323 if sourcetype = `history
3324 then new outlinesoucebase fetchoutlines
3325 else outlinesource fetchoutlines
3327 (fun errmsg ->
3328 let outlines = fetchoutlines () in
3329 if Array.length outlines = 0
3330 then showtext ' ' errmsg
3331 else (
3332 resetmstate ();
3333 Wsi.setcursor Wsi.CURSOR_INHERIT;
3334 let anchor = getanchor () in
3335 source#reset anchor outlines;
3336 state.text <- source#greetmsg;
3337 state.uioh <-
3338 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
3339 postRedisplay "enter selector";
3343 let mkenter sourcetype errmsg =
3344 let enter = mkselector sourcetype in
3345 fun () -> enter errmsg
3347 ( mkenter `outlines "document has no outline"
3348 , mkenter `bookmarks "document has no bookmarks (yet)"
3349 , mkenter `history "history is empty" )
3352 let quickbookmark ?title () =
3353 match state.layout with
3354 | [] -> ()
3355 | l :: _ ->
3356 let title =
3357 match title with
3358 | None ->
3359 Unix.(
3360 let tm = localtime (now ()) in
3361 Printf.sprintf
3362 "Quick (page %d) (bookmarked on %02d/%02d/%d at %02d:%02d)"
3363 (l.pageno+1)
3364 tm.tm_mday (tm.tm_mon+1) (tm.tm_year+1900) tm.tm_hour tm.tm_min
3366 | Some title -> title
3368 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
3371 let setautoscrollspeed step goingdown =
3372 let incr = max 1 ((abs step) / 2) in
3373 let incr = if goingdown then incr else -incr in
3374 let astep = boundastep state.winh (step + incr) in
3375 state.autoscroll <- Some astep;
3378 let canpan () =
3379 match conf.columns with
3380 | Csplit _ -> true
3381 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
3384 let panbound x = bound x (-state.w) state.winw;;
3386 let existsinrow pageno (columns, coverA, coverB) p =
3387 let last = ((pageno - coverA) mod columns) + columns in
3388 let rec any = function
3389 | [] -> false
3390 | l :: rest ->
3391 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
3392 then p l
3393 else (
3394 if not (p l)
3395 then (if l.pageno = last then false else any rest)
3396 else true
3399 any state.layout
3402 let nextpage () =
3403 match state.layout with
3404 | [] ->
3405 let pageno = page_of_y state.y in
3406 gotoxy state.x (getpagey (pageno+1))
3407 | l :: rest ->
3408 match conf.columns with
3409 | Csingle _ ->
3410 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
3411 then
3412 let y = clamp (pgscale state.winh) in
3413 gotoxy state.x y
3414 else
3415 let pageno = min (l.pageno+1) (state.pagecount-1) in
3416 gotoxy state.x (getpagey pageno)
3417 | Cmulti ((c, _, _) as cl, _) ->
3418 if conf.presentation
3419 && (existsinrow l.pageno cl
3420 (fun l -> l.pageh > l.pagey + l.pagevh))
3421 then
3422 let y = clamp (pgscale state.winh) in
3423 gotoxy state.x y
3424 else
3425 let pageno = min (l.pageno+c) (state.pagecount-1) in
3426 gotoxy state.x (getpagey pageno)
3427 | Csplit (n, _) ->
3428 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
3429 then
3430 let pagey, pageh = getpageyh l.pageno in
3431 let pagey = pagey + pageh * l.pagecol in
3432 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
3433 gotoxy state.x (pagey + pageh + ips)
3436 let prevpage () =
3437 match state.layout with
3438 | [] ->
3439 let pageno = page_of_y state.y in
3440 gotoxy state.x (getpagey (pageno-1))
3441 | l :: _ ->
3442 match conf.columns with
3443 | Csingle _ ->
3444 if conf.presentation && l.pagey != 0
3445 then
3446 gotoxy state.x (clamp (pgscale ~-(state.winh)))
3447 else
3448 let pageno = max 0 (l.pageno-1) in
3449 gotoxy state.x (getpagey pageno)
3450 | Cmulti ((c, _, coverB) as cl, _) ->
3451 if conf.presentation &&
3452 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
3453 then
3454 gotoxy state.x (clamp (pgscale ~-(state.winh)))
3455 else
3456 let decr =
3457 if l.pageno = state.pagecount - coverB
3458 then 1
3459 else c
3461 let pageno = max 0 (l.pageno-decr) in
3462 gotoxy state.x (getpagey pageno)
3463 | Csplit (n, _) ->
3464 let y =
3465 if l.pagecol = 0
3466 then
3467 if l.pageno = 0
3468 then l.pagey
3469 else
3470 let pageno = max 0 (l.pageno-1) in
3471 let pagey, pageh = getpageyh pageno in
3472 pagey + (n-1)*pageh
3473 else
3474 let pagey, pageh = getpageyh l.pageno in
3475 pagey + pageh * (l.pagecol-1) - conf.interpagespace
3477 gotoxy state.x y
3480 let save () =
3481 if emptystr conf.savecmd
3482 then adderrmsg "savepath-command is empty"
3483 "don't know where to save modified document"
3484 else
3485 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
3486 let path =
3487 getcmdoutput
3488 (fun exn ->
3489 adderrfmt savecmd "failed to produce path to the saved copy: %s" exn)
3490 savecmd
3492 if nonemptystr path
3493 then
3494 let tmp = path ^ ".tmp" in
3495 savedoc tmp;
3496 Unix.rename tmp path;
3499 let viewkeyboard key mask =
3500 let enttext te =
3501 let mode = state.mode in
3502 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
3503 state.text <- E.s;
3504 enttext ();
3505 postRedisplay "view:enttext"
3507 let ctrl = Wsi.withctrl mask in
3508 let open Keys in
3509 match Wsi.kc2kt key with
3510 | Ascii 'S' -> state.slideshow <- state.slideshow lxor 1
3512 | Ascii 'Q' -> exit 0
3514 | Ascii 'W' ->
3515 if hasunsavedchanges ()
3516 then save ()
3518 | Insert ->
3519 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
3520 then (
3521 state.mode <- (
3522 match state.lnava with
3523 | None -> LinkNav (Ltgendir 0)
3524 | Some pn -> LinkNav (Ltexact pn)
3526 gotoxy state.x state.y;
3528 else impmsg "keyboard link navigation does not work under rotation"
3530 | Escape | Ascii 'q' ->
3531 begin match state.mstate with
3532 | Mzoomrect _ ->
3533 resetmstate ();
3534 postRedisplay "kill rect";
3535 | Msel _
3536 | Mpan _
3537 | Mscrolly | Mscrollx
3538 | Mzoom _
3539 | Mnone ->
3540 begin match state.mode with
3541 | LinkNav ln ->
3542 begin match ln with
3543 | Ltexact pl -> state.lnava <- Some pl
3544 | Ltgendir _ | Ltnotready _ -> state.lnava <- None
3545 end;
3546 state.mode <- View;
3547 postRedisplay "esc leave linknav"
3548 | Birdseye _ | Textentry _ | View ->
3549 match state.ranchors with
3550 | [] -> raise Quit
3551 | (path, password, anchor, origin) :: rest ->
3552 state.ranchors <- rest;
3553 state.anchor <- anchor;
3554 state.origin <- origin;
3555 state.nameddest <- E.s;
3556 opendoc path password
3557 end;
3558 end;
3560 | Backspace ->
3561 addnavnorc ();
3562 gotoxy state.x (getnav ~-1)
3564 | Ascii 'o' ->
3565 enteroutlinemode ()
3567 | Ascii 'H' ->
3568 enterhistmode ()
3570 | Ascii 'u' ->
3571 state.rects <- [];
3572 state.text <- E.s;
3573 Hashtbl.iter (fun _ opaque ->
3574 clearmark opaque;
3575 Hashtbl.clear state.prects) state.pagemap;
3576 postRedisplay "dehighlight";
3578 | Ascii (('/' | '?') as c) ->
3579 let ondone isforw s =
3580 cbput state.hists.pat s;
3581 state.searchpattern <- s;
3582 search s isforw
3584 let s = String.make 1 c in
3585 enttext (s, E.s, Some (onhist state.hists.pat),
3586 textentry, ondone (c = '/'), true)
3588 | Ascii '+' | Ascii '=' when ctrl ->
3589 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
3590 pivotzoom (conf.zoom +. incr)
3592 | Ascii '+' ->
3593 let ondone s =
3594 let n =
3595 try int_of_string s with exn ->
3596 state.text <-
3597 Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
3598 max_int
3600 if n != max_int
3601 then (
3602 conf.pagebias <- n;
3603 state.text <- "page bias is now " ^ string_of_int n;
3606 enttext ("page bias: ", E.s, None, intentry, ondone, true)
3608 | Ascii '-' when ctrl ->
3609 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
3610 pivotzoom (max 0.01 (conf.zoom -. decr))
3612 | Ascii '-' ->
3613 let ondone msg = state.text <- msg in
3614 enttext ("option: ", E.s, None,
3615 optentry state.mode, ondone, true)
3617 | Ascii '0' when ctrl ->
3618 if conf.zoom = 1.0
3619 then gotoxy 0 state.y
3620 else setzoom 1.0
3622 | Ascii ('1'|'2' as c) when ctrl && conf.fitmodel != FitPage ->
3623 let cols =
3624 match conf.columns with
3625 | Csingle _ | Cmulti _ -> 1
3626 | Csplit (n, _) -> n
3628 let h = state.winh -
3629 conf.interpagespace lsl (if conf.presentation then 1 else 0)
3631 let zoom = zoomforh state.winw h 0 cols in
3632 if zoom > 0.0 && (c = '2' || zoom < 1.0)
3633 then setzoom zoom
3635 | Ascii '3' when ctrl ->
3636 let fm =
3637 match conf.fitmodel with
3638 | FitWidth -> FitProportional
3639 | FitProportional -> FitPage
3640 | FitPage -> FitWidth
3642 state.text <- "fit model: " ^ FMTE.to_string fm;
3643 reqlayout conf.angle fm
3645 | Ascii '4' when ctrl ->
3646 let zoom = getmaxw () /. float state.winw in
3647 if zoom > 0.0 then setzoom zoom
3649 | Fn 9 ->
3650 togglebirdseye ()
3652 | Ascii '9' when ctrl ->
3653 togglebirdseye ()
3655 | Ascii ('0'..'9' as c) when not ctrl ->
3656 let ondone s =
3657 let n =
3658 try int_of_string s with exn ->
3659 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
3662 if n >= 0
3663 then (
3664 addnav ();
3665 cbput state.hists.pag (string_of_int n);
3666 gotopage1 (n + conf.pagebias - 1) 0;
3669 let pageentry text = function [@warning "-4"]
3670 | Keys.Ascii 'g' -> TEdone text
3671 | key -> intentry text key
3673 let text = String.make 1 c in
3674 enttext (":", text, Some (onhist state.hists.pag),
3675 pageentry, ondone, true)
3677 | Ascii 'b' ->
3678 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
3679 postRedisplay "toggle scrollbar";
3681 | Ascii 'B' ->
3682 state.bzoom <- not state.bzoom;
3683 state.rects <- [];
3684 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
3686 | Ascii 'l' ->
3687 conf.hlinks <- not conf.hlinks;
3688 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
3689 postRedisplay "toggle highlightlinks";
3691 | Ascii 'F' ->
3692 if conf.angle mod 360 = 0
3693 then (
3694 state.glinks <- true;
3695 let mode = state.mode in
3696 state.mode <-
3697 Textentry (
3698 (":", E.s, None, linknentry, linknact gotounder, false),
3699 (fun _ ->
3700 state.glinks <- false;
3701 state.mode <- mode)
3703 state.text <- E.s;
3704 postRedisplay "view:linkent(F)"
3706 else impmsg "hint mode does not work under rotation"
3708 | Ascii 'y' ->
3709 state.glinks <- true;
3710 let mode = state.mode in
3711 state.mode <-
3712 Textentry (
3713 (":", E.s, None, linknentry,
3714 linknact (fun under ->
3715 selstring conf.selcmd (undertext under)), false),
3716 (fun _ ->
3717 state.glinks <- false;
3718 state.mode <- mode)
3720 state.text <- E.s;
3721 postRedisplay "view:linkent"
3723 | Ascii 'a' ->
3724 begin match state.autoscroll with
3725 | Some step ->
3726 conf.autoscrollstep <- step;
3727 state.autoscroll <- None
3728 | None ->
3729 state.autoscroll <- Some conf.autoscrollstep;
3730 state.slideshow <- state.slideshow land lnot 2
3733 | Ascii 'p' when ctrl ->
3734 launchpath () (* XXX where do error messages go? *)
3736 | Ascii 'P' ->
3737 setpresentationmode (not conf.presentation);
3738 showtext ' ' ("presentation mode " ^
3739 if conf.presentation then "on" else "off");
3741 | Ascii 'f' ->
3742 if List.mem Wsi.Fullscreen state.winstate
3743 then Wsi.reshape conf.cwinw conf.cwinh
3744 else Wsi.fullscreen ()
3746 | Ascii ('p'|'N') ->
3747 search state.searchpattern false
3749 | Ascii 'n' | Fn 3 ->
3750 search state.searchpattern true
3752 | Ascii 't' ->
3753 begin match state.layout with
3754 | [] -> ()
3755 | l :: _ ->
3756 gotoxy state.x (getpagey l.pageno)
3759 | Ascii ' ' ->
3760 nextpage ()
3762 | Delete ->
3763 prevpage ()
3765 | Ascii '=' ->
3766 showtext ' ' (describe_layout state.layout);
3768 | Ascii 'w' ->
3769 begin match state.layout with
3770 | [] -> ()
3771 | l :: _ ->
3772 Wsi.reshape l.pagew l.pageh;
3773 postRedisplay "w"
3776 | Ascii '\'' ->
3777 enterbookmarkmode ()
3779 | Ascii 'h' | Fn 1 ->
3780 enterhelpmode ()
3782 | Ascii 'i' ->
3783 enterinfomode ()
3785 | Ascii 'e' when Buffer.length state.errmsgs > 0 ->
3786 entermsgsmode ()
3788 | Ascii 'm' ->
3789 let ondone s =
3790 match state.layout with
3791 | l :: _ ->
3792 if nonemptystr s
3793 then
3794 state.bookmarks <-
3795 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
3796 | _ -> ()
3798 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
3800 | Ascii '~' ->
3801 quickbookmark ();
3802 showtext ' ' "Quick bookmark added";
3804 | Ascii 'x' -> state.roam ()
3806 | Ascii ('<'|'>' as c) ->
3807 reqlayout
3808 (conf.angle + (if c = '>' then 30 else -30)) conf.fitmodel
3810 | Ascii ('['|']' as c) ->
3811 conf.colorscale <-
3812 bound (conf.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0;
3813 postRedisplay "brightness";
3815 | Ascii 'c' when state.mode = View ->
3816 if Wsi.withalt mask
3817 then (
3818 if conf.zoom > 1.0
3819 then
3820 let m = (state.winw - state.w) / 2 in
3821 gotoxy m state.y
3823 else
3824 let (c, a, b), z =
3825 match state.prevcolumns with
3826 | None -> (1, 0, 0), 1.0
3827 | Some (columns, z) ->
3828 let cab =
3829 match columns with
3830 | Csplit (c, _) -> -c, 0, 0
3831 | Cmulti ((c, a, b), _) -> c, a, b
3832 | Csingle _ -> 1, 0, 0
3834 cab, z
3836 setcolumns View c a b;
3837 setzoom z
3839 | Down | Up when ctrl && Wsi.withshift mask ->
3840 let zoom, x = state.prevzoom in
3841 setzoom zoom;
3842 state.x <- x;
3844 | Ascii 'k' | Up ->
3845 begin match state.autoscroll with
3846 | None ->
3847 begin match state.mode with
3848 | Birdseye beye -> upbirdseye 1 beye
3849 | Textentry _ | View | LinkNav _ ->
3850 if ctrl
3851 then gotoxy state.x (clamp ~-(state.winh/2))
3852 else (
3853 if not (Wsi.withshift mask) && conf.presentation
3854 then prevpage ()
3855 else gotoxy state.x (clamp (-conf.scrollstep))
3858 | Some n ->
3859 setautoscrollspeed n false
3862 | Ascii 'j' | Down ->
3863 begin match state.autoscroll with
3864 | None ->
3865 begin match state.mode with
3866 | Birdseye beye -> downbirdseye 1 beye
3867 | Textentry _ | View | LinkNav _ ->
3868 if ctrl
3869 then gotoxy state.x (clamp (state.winh/2))
3870 else (
3871 if not (Wsi.withshift mask) && conf.presentation
3872 then nextpage ()
3873 else gotoxy state.x (clamp (conf.scrollstep))
3876 | Some n ->
3877 setautoscrollspeed n true
3880 | Left | Right when not (Wsi.withalt mask) ->
3881 if canpan ()
3882 then
3883 let dx =
3884 if ctrl
3885 then state.winw / 2
3886 else conf.hscrollstep
3888 let dx =
3889 let pv = Wsi.kc2kt key in
3890 if pv = Keys.Left then dx else -dx
3892 gotoxy (panbound (state.x + dx)) state.y
3893 else (
3894 state.text <- E.s;
3895 postRedisplay "left/right"
3898 | Prior ->
3899 let y =
3900 if ctrl
3901 then
3902 match state.layout with
3903 | [] -> state.y
3904 | l :: _ -> state.y - l.pagey
3905 else
3906 clamp (pgscale (-state.winh))
3908 gotoxy state.x y
3910 | Next ->
3911 let y =
3912 if ctrl
3913 then
3914 match List.rev state.layout with
3915 | [] -> state.y
3916 | l :: _ -> getpagey l.pageno
3917 else
3918 clamp (pgscale state.winh)
3920 gotoxy state.x y
3922 | Ascii 'g' | Home ->
3923 addnav ();
3924 gotoxy 0 0
3925 | Ascii 'G' | End ->
3926 addnav ();
3927 gotoxy 0 (clamp state.maxy)
3929 | Right when Wsi.withalt mask ->
3930 addnavnorc ();
3931 gotoxy state.x (getnav 1)
3932 | Left when Wsi.withalt mask ->
3933 addnavnorc ();
3934 gotoxy state.x (getnav ~-1)
3936 | Ascii 'r' ->
3937 reload ()
3939 | Ascii 'v' when conf.debug ->
3940 state.rects <- [];
3941 List.iter (fun l ->
3942 match getopaque l.pageno with
3943 | None -> ()
3944 | Some opaque ->
3945 let x0, y0, x1, y1 = pagebbox opaque in
3946 let rect = (float x0, float y0,
3947 float x1, float y0,
3948 float x1, float y1,
3949 float x0, float y1) in
3950 debugrect rect;
3951 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
3952 state.rects <- (l.pageno, color, rect) :: state.rects;
3953 ) state.layout;
3954 postRedisplay "v";
3956 | Ascii '|' ->
3957 let mode = state.mode in
3958 let cmd = ref E.s in
3959 let onleave = function
3960 | Cancel -> state.mode <- mode
3961 | Confirm ->
3962 List.iter (fun l ->
3963 match getopaque l.pageno with
3964 | Some opaque -> pipesel opaque !cmd
3965 | None -> ()) state.layout;
3966 state.mode <- mode
3968 let ondone s =
3969 cbput state.hists.sel s;
3970 cmd := s
3972 let te =
3973 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
3975 postRedisplay "|";
3976 state.mode <- Textentry (te, onleave);
3978 | (Ascii _|Fn _|Enter|Left|Right|Code _|Ctrl _) ->
3979 vlog "huh? %s" (Wsi.keyname key)
3982 let linknavkeyboard key mask linknav =
3983 let pv = Wsi.kc2kt key in
3984 let getpage pageno =
3985 let rec loop = function
3986 | [] -> None
3987 | l :: _ when l.pageno = pageno -> Some l
3988 | _ :: rest -> loop rest
3989 in loop state.layout
3991 let doexact (pageno, n) =
3992 match getopaque pageno, getpage pageno with
3993 | Some opaque, Some l ->
3994 if pv = Keys.Enter
3995 then
3996 let under = getlink opaque n in
3997 postRedisplay "link gotounder";
3998 gotounder under;
3999 state.mode <- View;
4000 else
4001 let opt, dir =
4002 let open Keys in
4003 match pv with
4004 | Home -> Some (findlink opaque LDfirst), -1
4005 | End -> Some (findlink opaque LDlast), 1
4006 | Left -> Some (findlink opaque (LDleft n)), -1
4007 | Right -> Some (findlink opaque (LDright n)), 1
4008 | Up -> Some (findlink opaque (LDup n)), -1
4009 | Down -> Some (findlink opaque (LDdown n)), 1
4011 | Delete|Escape|Insert|Enter|Next|Prior|Ascii _
4012 | Code _|Fn _|Ctrl _|Backspace -> None, 0
4014 let pwl l dir =
4015 begin match findpwl l.pageno dir with
4016 | Pwlnotfound -> ()
4017 | Pwl pageno ->
4018 let notfound dir =
4019 state.mode <- LinkNav (Ltgendir dir);
4020 let y, h = getpageyh pageno in
4021 let y =
4022 if dir < 0
4023 then y + h - state.winh
4024 else y
4026 gotoxy state.x y
4028 begin match getopaque pageno, getpage pageno with
4029 | Some opaque, Some _ ->
4030 let link =
4031 let ld = if dir > 0 then LDfirst else LDlast in
4032 findlink opaque ld
4034 begin match link with
4035 | Lfound m ->
4036 showlinktype (getlink opaque m);
4037 state.mode <- LinkNav (Ltexact (pageno, m));
4038 postRedisplay "linknav jpage";
4039 | Lnotfound -> notfound dir
4040 end;
4041 | _ -> notfound dir
4042 end;
4043 end;
4045 begin match opt with
4046 | Some Lnotfound -> pwl l dir;
4047 | Some (Lfound m) ->
4048 if m = n
4049 then pwl l dir
4050 else (
4051 let _, y0, _, y1 = getlinkrect opaque m in
4052 if y0 < l.pagey
4053 then gotopage1 l.pageno y0
4054 else (
4055 let d = fstate.fontsize + 1 in
4056 if y1 - l.pagey > l.pagevh - d
4057 then gotopage1 l.pageno (y1 - state.winh + d)
4058 else postRedisplay "linknav";
4060 showlinktype (getlink opaque m);
4061 state.mode <- LinkNav (Ltexact (l.pageno, m));
4064 | None -> viewkeyboard key mask
4065 end;
4066 | _ -> viewkeyboard key mask
4068 if pv = Keys.Insert
4069 then (
4070 begin match linknav with
4071 | Ltexact pa -> state.lnava <- Some pa
4072 | Ltgendir _ | Ltnotready _ -> ()
4073 end;
4074 state.mode <- View;
4075 postRedisplay "leave linknav"
4077 else
4078 match linknav with
4079 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
4080 | Ltexact exact -> doexact exact
4083 let keyboard key mask =
4084 if (key = Char.code 'g' && Wsi.withctrl mask) && not (istextentry state.mode)
4085 then wcmd "interrupt"
4086 else state.uioh <- state.uioh#key key mask
4089 let birdseyekeyboard key mask
4090 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
4091 let incr =
4092 match conf.columns with
4093 | Csingle _ -> 1
4094 | Cmulti ((c, _, _), _) -> c
4095 | Csplit _ -> failwith "bird's eye split mode"
4097 let pgh layout = List.fold_left
4098 (fun m l -> max l.pageh m) state.winh layout in
4099 let open Keys in
4100 match Wsi.kc2kt key with
4101 | Ascii 'l' when Wsi.withctrl mask ->
4102 let y, h = getpageyh pageno in
4103 let top = (state.winh - h) / 2 in
4104 gotoxy state.x (max 0 (y - top))
4105 | Enter -> leavebirdseye beye false
4106 | Escape -> leavebirdseye beye true
4107 | Up -> upbirdseye incr beye
4108 | Down -> downbirdseye incr beye
4109 | Left -> upbirdseye 1 beye
4110 | Right -> downbirdseye 1 beye
4112 | Prior ->
4113 begin match state.layout with
4114 | l :: _ ->
4115 if l.pagey != 0
4116 then (
4117 state.mode <- Birdseye (
4118 oconf, leftx, l.pageno, hooverpageno, anchor
4120 gotopage1 l.pageno 0;
4122 else (
4123 let layout = layout state.x (state.y-state.winh)
4124 state.winw
4125 (pgh state.layout) in
4126 match layout with
4127 | [] -> gotoxy state.x (clamp (-state.winh))
4128 | l :: _ ->
4129 state.mode <- Birdseye (
4130 oconf, leftx, l.pageno, hooverpageno, anchor
4132 gotopage1 l.pageno 0
4135 | [] -> gotoxy state.x (clamp (-state.winh))
4136 end;
4138 | Next ->
4139 begin match List.rev state.layout with
4140 | l :: _ ->
4141 let layout = layout state.x
4142 (state.y + (pgh state.layout))
4143 state.winw state.winh in
4144 begin match layout with
4145 | [] ->
4146 let incr = l.pageh - l.pagevh in
4147 if incr = 0
4148 then (
4149 state.mode <-
4150 Birdseye (
4151 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
4153 postRedisplay "birdseye pagedown";
4155 else gotoxy state.x (clamp (incr + conf.interpagespace*2));
4157 | l :: _ ->
4158 state.mode <-
4159 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
4160 gotopage1 l.pageno 0;
4163 | [] -> gotoxy state.x (clamp state.winh)
4164 end;
4166 | Home ->
4167 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
4168 gotopage1 0 0
4170 | End ->
4171 let pageno = state.pagecount - 1 in
4172 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
4173 if not (pagevisible state.layout pageno)
4174 then
4175 let h =
4176 match List.rev state.pdims with
4177 | [] -> state.winh
4178 | (_, _, h, _) :: _ -> h
4180 gotoxy
4181 state.x
4182 (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
4183 else postRedisplay "birdseye end";
4185 | Delete|Insert|Ascii _|Code _|Ctrl _|Fn _|Backspace -> viewkeyboard key mask
4188 let drawpage l =
4189 let color =
4190 match state.mode with
4191 | Textentry _ -> scalecolor 0.4
4192 | LinkNav _ | View -> scalecolor 1.0
4193 | Birdseye (_, _, pageno, hooverpageno, _) ->
4194 if l.pageno = hooverpageno
4195 then scalecolor 0.9
4196 else (
4197 if l.pageno = pageno
4198 then (
4199 let c = scalecolor 1.0 in
4200 GlDraw.color c;
4201 GlDraw.line_width 3.0;
4202 let dispx = l.pagedispx in
4203 linerect
4204 (float (dispx-1)) (float (l.pagedispy-1))
4205 (float (dispx+l.pagevw+1))
4206 (float (l.pagedispy+l.pagevh+1));
4207 GlDraw.line_width 1.0;
4210 else scalecolor 0.8
4213 drawtiles l color;
4216 let postdrawpage l linkindexbase =
4217 match getopaque l.pageno with
4218 | Some opaque ->
4219 if tileready l l.pagex l.pagey
4220 then
4221 let x = l.pagedispx - l.pagex
4222 and y = l.pagedispy - l.pagey in
4223 let hlmask =
4224 match conf.columns with
4225 | Csingle _ | Cmulti _ ->
4226 (if conf.hlinks then 1 else 0)
4227 + (if state.glinks
4228 && not (isbirdseye state.mode) then 2 else 0)
4229 | Csplit _ -> 0
4231 let s =
4232 match state.mode with
4233 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
4234 | Textentry _
4235 | Birdseye _
4236 | View
4237 | LinkNav _ -> E.s
4239 Hashtbl.find_all state.prects l.pageno |>
4240 List.iter (fun vals -> drawprect opaque x y vals);
4241 let n = postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize) in
4242 if n < 0
4243 then (Glutils.redisplay := true; 0)
4244 else n
4245 else 0
4246 | _ -> 0
4249 let scrollindicator () =
4250 let sbw, ph, sh = state.uioh#scrollph in
4251 let sbh, pw, sw = state.uioh#scrollpw in
4253 let x0,x1,hx0 =
4254 if conf.leftscroll
4255 then (0, sbw, sbw)
4256 else ((state.winw - sbw), state.winw, 0)
4259 Gl.enable `blend;
4260 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
4261 let (r, g, b, alpha) = conf.sbarcolor in
4262 GlDraw.color (r, g, b) ~alpha;
4263 filledrect (float x0) 0. (float x1) (float state.winh);
4264 filledrect
4265 (float hx0) (float (state.winh - sbh))
4266 (float (hx0 + state.winw)) (float state.winh);
4267 let (r, g, b, alpha) = conf.sbarhndlcolor in
4268 GlDraw.color (r, g, b) ~alpha;
4270 filledrect (float x0) ph (float x1) (ph +. sh);
4271 let pw = pw +. float hx0 in
4272 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
4273 Gl.disable `blend;
4276 let showsel () =
4277 match state.mstate with
4278 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
4281 | Msel ((x0, y0), (x1, y1)) ->
4282 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
4283 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
4284 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
4285 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
4288 let showrects =
4289 function [] -> ()
4290 | rects ->
4291 Gl.enable `blend;
4292 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
4293 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
4294 List.iter
4295 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
4296 List.iter (fun l ->
4297 if l.pageno = pageno
4298 then (
4299 let dx = float (l.pagedispx - l.pagex) in
4300 let dy = float (l.pagedispy - l.pagey) in
4301 let r, g, b, alpha = c in
4302 GlDraw.color (r, g, b) ~alpha;
4303 filledrect2 (x0+.dx) (y0+.dy)
4304 (x1+.dx) (y1+.dy)
4305 (x3+.dx) (y3+.dy)
4306 (x2+.dx) (y2+.dy);
4308 ) state.layout
4309 ) rects;
4310 Gl.disable `blend;
4313 let display () =
4314 GlDraw.color (scalecolor2 conf.bgcolor);
4315 GlClear.color (scalecolor2 conf.bgcolor);
4316 GlClear.clear [`color];
4317 List.iter drawpage state.layout;
4318 let rects =
4319 match state.mode with
4320 | LinkNav (Ltexact (pageno, linkno)) ->
4321 begin match getopaque pageno with
4322 | Some opaque ->
4323 let x0, y0, x1, y1 = getlinkrect opaque linkno in
4324 let color = (0.0, 0.0, 0.5, 0.5) in
4325 (pageno, color,
4326 (float x0, float y0,
4327 float x1, float y0,
4328 float x1, float y1,
4329 float x0, float y1)
4330 ) :: state.rects
4331 | None -> state.rects
4333 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
4334 | Birdseye _
4335 | Textentry _
4336 | View -> state.rects
4338 showrects rects;
4339 let rec postloop linkindexbase = function
4340 | l :: rest ->
4341 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
4342 postloop linkindexbase rest
4343 | [] -> ()
4345 showsel ();
4346 postloop 0 state.layout;
4347 state.uioh#display;
4348 begin match state.mstate with
4349 | Mzoomrect ((x0, y0), (x1, y1)) ->
4350 Gl.enable `blend;
4351 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
4352 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
4353 filledrect (float x0) (float y0) (float x1) (float y1);
4354 Gl.disable `blend;
4355 | Msel _
4356 | Mpan _
4357 | Mscrolly | Mscrollx
4358 | Mzoom _
4359 | Mnone -> ()
4360 end;
4361 enttext ();
4362 scrollindicator ();
4363 Wsi.swapb ();
4366 let zoomrect x y x1 y1 =
4367 let x0 = min x x1
4368 and x1 = max x x1
4369 and y0 = min y y1 in
4370 let zoom = (float state.w) /. float (x1 - x0) in
4371 let margin =
4372 let simple () =
4373 if state.w < state.winw
4374 then (state.winw - state.w) / 2
4375 else 0
4377 match conf.fitmodel with
4378 | FitWidth | FitProportional -> simple ()
4379 | FitPage ->
4380 match conf.columns with
4381 | Csplit _ ->
4382 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
4383 | Cmulti _ | Csingle _ -> simple ()
4385 gotoxy ((state.x + margin) - x0) (state.y + y0);
4386 state.anchor <- getanchor ();
4387 setzoom zoom;
4388 resetmstate ();
4391 let annot inline x y =
4392 match unproject x y with
4393 | Some (opaque, n, ux, uy) ->
4394 let add text =
4395 addannot opaque ux uy text;
4396 wcmd "freepage %s" (~> opaque);
4397 Hashtbl.remove state.pagemap (n, state.gen);
4398 flushtiles ();
4399 gotoxy state.x state.y
4401 if inline
4402 then
4403 let ondone s = add s in
4404 let mode = state.mode in
4405 state.mode <- Textentry (
4406 ("annotation: ", E.s, None, textentry, ondone, true),
4407 fun _ -> state.mode <- mode);
4408 state.text <- E.s;
4409 enttext ();
4410 postRedisplay "annot"
4411 else
4412 add @@ getusertext E.s
4413 | _ -> ()
4416 let zoomblock x y =
4417 let g opaque l px py =
4418 match rectofblock opaque px py with
4419 | Some a ->
4420 let x0 = a.(0) -. 20. in
4421 let x1 = a.(1) +. 20. in
4422 let y0 = a.(2) -. 20. in
4423 let zoom = (float state.w) /. (x1 -. x0) in
4424 let pagey = getpagey l.pageno in
4425 let margin = (state.w - l.pagew)/2 in
4426 let nx = -truncate x0 - margin in
4427 gotoxy nx (pagey + truncate y0);
4428 state.anchor <- getanchor ();
4429 setzoom zoom;
4430 None
4431 | None -> None
4433 match conf.columns with
4434 | Csplit _ ->
4435 impmsg "block zooming does not work properly in split columns mode"
4436 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
4439 let scrollx x =
4440 let winw = state.winw - 1 in
4441 let s = float x /. float winw in
4442 let destx = truncate (float (state.w + winw) *. s) in
4443 gotoxy (winw - destx) state.y;
4444 state.mstate <- Mscrollx;
4447 let scrolly y =
4448 let s = float y /. float state.winh in
4449 let desty = truncate (s *. float (maxy ())) in
4450 gotoxy state.x desty;
4451 state.mstate <- Mscrolly;
4454 let viewmulticlick clicks x y mask =
4455 let g opaque l px py =
4456 let mark =
4457 match clicks with
4458 | 2 -> Mark_word
4459 | 3 -> Mark_line
4460 | 4 -> Mark_block
4461 | _ -> Mark_page
4463 if markunder opaque px py mark
4464 then (
4465 Some (fun () ->
4466 let dopipe cmd =
4467 match getopaque l.pageno with
4468 | None -> ()
4469 | Some opaque -> pipesel opaque cmd
4471 state.roam <- (fun () -> dopipe conf.paxcmd);
4472 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
4475 else None
4477 postRedisplay "viewmulticlick";
4478 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
4481 let canselect () =
4482 match conf.columns with
4483 | Csplit _ -> false
4484 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
4487 let viewmouse button down x y mask =
4488 match button with
4489 | n when (n == 4 || n == 5) && not down ->
4490 if Wsi.withctrl mask
4491 then (
4492 let incr =
4493 if n = 5
4494 then if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
4495 else if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
4497 let fx, fy =
4498 match state.mstate with
4499 | Mzoom (oldn, _, pos) when n = oldn -> pos
4500 | Mzoomrect _ | Mnone | Mpan _
4501 | Msel _ | Mscrollx | Mscrolly | Mzoom _ -> (x, y)
4503 let zoom = conf.zoom -. incr in
4504 state.mstate <- Mzoom (n, 0, (x, y));
4505 if false && abs (fx - x) > 5 || abs (fy - y) > 5
4506 then pivotzoom ~x ~y zoom
4507 else pivotzoom zoom
4509 else (
4510 match state.autoscroll with
4511 | Some step -> setautoscrollspeed step (n=4)
4512 | None ->
4513 if conf.wheelbypage || conf.presentation
4514 then (
4515 if n = 4
4516 then prevpage ()
4517 else nextpage ()
4519 else
4520 let incr =
4521 if n = 4
4522 then -conf.scrollstep
4523 else conf.scrollstep
4525 let incr = incr * 2 in
4526 let y = clamp incr in
4527 gotoxy state.x y
4530 | n when (n = 6 || n = 7) && not down && canpan () ->
4531 let x =
4532 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep) in
4533 gotoxy x state.y
4535 | 1 when Wsi.withshift mask ->
4536 state.mstate <- Mnone;
4537 if not down
4538 then (
4539 match unproject x y with
4540 | None -> ()
4541 | Some (_, pageno, ux, uy) ->
4542 let cmd = Printf.sprintf
4543 "%s %s %d %d %d"
4544 conf.stcmd state.path pageno ux uy
4546 match spawn cmd [] with
4547 | exception exn ->
4548 impmsg "execution of synctex command(%S) failed: %S"
4549 conf.stcmd @@ exntos exn
4550 | _pid -> ()
4553 | 1 when Wsi.withctrl mask ->
4554 if down
4555 then (
4556 Wsi.setcursor Wsi.CURSOR_FLEUR;
4557 state.mstate <- Mpan (x, y)
4559 else
4560 state.mstate <- Mnone
4562 | 3 ->
4563 if down
4564 then (
4565 if Wsi.withshift mask
4566 then (
4567 annot conf.annotinline x y;
4568 postRedisplay "addannot"
4570 else
4571 let p = (x, y) in
4572 Wsi.setcursor Wsi.CURSOR_CYCLE;
4573 state.mstate <- Mzoomrect (p, p)
4575 else (
4576 match state.mstate with
4577 | Mzoomrect ((x0, y0), _) ->
4578 if abs (x-x0) > 10 && abs (y - y0) > 10
4579 then zoomrect x0 y0 x y
4580 else (
4581 resetmstate ();
4582 postRedisplay "kill accidental zoom rect";
4584 | Msel _
4585 | Mpan _
4586 | Mscrolly | Mscrollx
4587 | Mzoom _
4588 | Mnone -> resetmstate ()
4591 | 1 when vscrollhit x ->
4592 if down
4593 then
4594 let _, position, sh = state.uioh#scrollph in
4595 if y > truncate position && y < truncate (position +. sh)
4596 then state.mstate <- Mscrolly
4597 else scrolly y
4598 else
4599 state.mstate <- Mnone
4601 | 1 when y > state.winh - hscrollh () ->
4602 if down
4603 then
4604 let _, position, sw = state.uioh#scrollpw in
4605 if x > truncate position && x < truncate (position +. sw)
4606 then state.mstate <- Mscrollx
4607 else scrollx x
4608 else
4609 state.mstate <- Mnone
4611 | 1 when state.bzoom -> if not down then zoomblock x y
4613 | 1 ->
4614 let dest = if down then getunder x y else Unone in
4615 begin match dest with
4616 | Ulinkuri _ ->
4617 gotounder dest
4619 | Unone when down ->
4620 Wsi.setcursor Wsi.CURSOR_FLEUR;
4621 state.mstate <- Mpan (x, y);
4623 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4625 | Unone | Utext _ ->
4626 if down
4627 then (
4628 if canselect ()
4629 then (
4630 state.mstate <- Msel ((x, y), (x, y));
4631 postRedisplay "mouse select";
4634 else (
4635 match state.mstate with
4636 | Mnone -> ()
4638 | Mzoom _ | Mscrollx | Mscrolly ->
4639 state.mstate <- Mnone
4641 | Mzoomrect ((x0, y0), _) ->
4642 zoomrect x0 y0 x y
4644 | Mpan _ ->
4645 Wsi.setcursor Wsi.CURSOR_INHERIT;
4646 state.mstate <- Mnone
4648 | Msel ((x0, y0), (x1, y1)) ->
4649 let rec loop = function
4650 | [] -> ()
4651 | l :: rest ->
4652 let inside =
4653 let a0 = l.pagedispy in
4654 let a1 = a0 + l.pagevh in
4655 let b0 = l.pagedispx in
4656 let b1 = b0 + l.pagevw in
4657 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
4658 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
4660 if inside
4661 then
4662 match getopaque l.pageno with
4663 | Some opaque ->
4664 let dosel cmd () =
4665 pipef ~closew:false "Msel"
4666 (fun w ->
4667 copysel w opaque;
4668 postRedisplay "Msel") cmd
4670 dosel conf.selcmd ();
4671 state.roam <- dosel conf.paxcmd;
4672 | None -> ()
4673 else loop rest
4675 loop state.layout;
4676 resetmstate ();
4680 | _ -> ()
4683 let birdseyemouse button down x y mask
4684 (conf, leftx, _, hooverpageno, anchor) =
4685 match button with
4686 | 1 when down ->
4687 let rec loop = function
4688 | [] -> ()
4689 | l :: rest ->
4690 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4691 && x > l.pagedispx && x < l.pagedispx + l.pagevw
4692 then (
4693 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
4695 else loop rest
4697 loop state.layout
4698 | 3 -> ()
4699 | _ -> viewmouse button down x y mask
4702 let uioh = object
4703 method display = ()
4705 method key key mask =
4706 begin match state.mode with
4707 | Textentry textentry -> textentrykeyboard key mask textentry
4708 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
4709 | View -> viewkeyboard key mask
4710 | LinkNav linknav -> linknavkeyboard key mask linknav
4711 end;
4712 state.uioh
4714 method button button bstate x y mask =
4715 begin match state.mode with
4716 | LinkNav _ | View -> viewmouse button bstate x y mask
4717 | Birdseye beye -> birdseyemouse button bstate x y mask beye
4718 | Textentry _ -> ()
4719 end;
4720 state.uioh
4722 method multiclick clicks x y mask =
4723 begin match state.mode with
4724 | LinkNav _ | View -> viewmulticlick clicks x y mask
4725 | Birdseye _ | Textentry _ -> ()
4726 end;
4727 state.uioh
4729 method motion x y =
4730 begin match state.mode with
4731 | Textentry _ -> ()
4732 | View | Birdseye _ | LinkNav _ ->
4733 match state.mstate with
4734 | Mzoom _ | Mnone -> ()
4736 | Mpan (x0, y0) ->
4737 let dx = x - x0
4738 and dy = y0 - y in
4739 state.mstate <- Mpan (x, y);
4740 let x = if canpan () then panbound (state.x + dx) else state.x in
4741 let y = clamp dy in
4742 gotoxy x y
4744 | Msel (a, _) ->
4745 state.mstate <- Msel (a, (x, y));
4746 postRedisplay "motion select";
4748 | Mscrolly ->
4749 let y = min state.winh (max 0 y) in
4750 scrolly y
4752 | Mscrollx ->
4753 let x = min state.winw (max 0 x) in
4754 scrollx x
4756 | Mzoomrect (p0, _) ->
4757 state.mstate <- Mzoomrect (p0, (x, y));
4758 postRedisplay "motion zoomrect";
4759 end;
4760 state.uioh
4762 method pmotion x y =
4763 begin match state.mode with
4764 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
4765 let rec loop = function
4766 | [] ->
4767 if hooverpageno != -1
4768 then (
4769 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
4770 postRedisplay "pmotion birdseye no hoover";
4772 | l :: rest ->
4773 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4774 && x > l.pagedispx && x < l.pagedispx + l.pagevw
4775 then (
4776 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
4777 postRedisplay "pmotion birdseye hoover";
4779 else loop rest
4781 loop state.layout
4783 | Textentry _ -> ()
4785 | LinkNav _ | View ->
4786 match state.mstate with
4787 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
4788 | Mnone ->
4789 updateunder x y;
4790 if canselect ()
4791 then
4792 match conf.pax with
4793 | None -> ()
4794 | Some past ->
4795 let now = now () in
4796 let delta = now -. past in
4797 if delta > 0.01
4798 then paxunder x y
4799 else conf.pax <- Some now
4800 end;
4801 state.uioh
4803 method infochanged _ = ()
4805 method scrollph =
4806 let maxy = maxy () in
4807 let p, h =
4808 if maxy = 0
4809 then 0.0, float state.winh
4810 else scrollph state.y maxy
4812 vscrollw (), p, h
4814 method scrollpw =
4815 let fwinw = float (state.winw - vscrollw ()) in
4816 let sw =
4817 let sw = fwinw /. float state.w in
4818 let sw = fwinw *. sw in
4819 max sw (float conf.scrollh)
4821 let position =
4822 let maxx = state.w + state.winw in
4823 let x = state.winw - state.x in
4824 let percent = float x /. float maxx in
4825 (fwinw -. sw) *. percent
4827 hscrollh (), position, sw
4829 method modehash =
4830 let modename =
4831 match state.mode with
4832 | LinkNav _ -> "links"
4833 | Textentry _ -> "textentry"
4834 | Birdseye _ -> "birdseye"
4835 | View -> "view"
4837 findkeyhash conf modename
4839 method eformsgs = true
4840 method alwaysscrolly = false
4841 method scroll dx dy =
4842 let x = if canpan () then panbound (state.x + dx) else state.x in
4843 gotoxy x (clamp (2 * dy));
4844 state.uioh
4845 method zoom z x y =
4846 pivotzoom ~x ~y (conf.zoom *. exp z);
4847 end;;
4849 let addrect pageno r g b a x0 y0 x1 y1 =
4850 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
4853 let ract cmds =
4854 let cl = splitatchar cmds ' ' in
4855 let scan s fmt f =
4856 try Scanf.sscanf s fmt f
4857 with exn -> adderrfmt "remote exec" "error processing '%S': %s\n"
4858 cmds @@ exntos exn
4860 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
4861 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
4862 s pageno r g b a x0 y0 x1 y1;
4863 onpagerect
4864 pageno
4865 (fun w h ->
4866 let _,w1,h1,_ = getpagedim pageno in
4867 let sw = float w1 /. float w
4868 and sh = float h1 /. float h in
4869 let x0s = x0 *. sw
4870 and x1s = x1 *. sw
4871 and y0s = y0 *. sh
4872 and y1s = y1 *. sh in
4873 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
4874 let color = (r, g, b, a) in
4875 if conf.verbose then debugrect rect;
4876 state.rects <- (pageno, color, rect) :: state.rects;
4877 postRedisplay s;
4880 match cl with
4881 | "reload", "" -> reload ()
4882 | "goto", args ->
4883 scan args "%u %f %f"
4884 (fun pageno x y ->
4885 let cmd, _ = state.geomcmds in
4886 if emptystr cmd
4887 then gotopagexy pageno x y
4888 else
4889 let f prevf () =
4890 gotopagexy pageno x y;
4891 prevf ()
4893 state.reprf <- f state.reprf
4895 | "goto1", args -> scan args "%u %f" gotopage
4896 | "gotor", args -> scan args "%S" gotoremote
4897 | "rect", args ->
4898 scan args "%u %u %f %f %f %f"
4899 (fun pageno c x0 y0 x1 y1 ->
4900 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
4901 rectx "rect" pageno color x0 y0 x1 y1;
4903 | "prect", args ->
4904 scan args "%u %f %f %f %f %f %f %f %f"
4905 (fun pageno r g b alpha x0 y0 x1 y1 ->
4906 addrect pageno r g b alpha x0 y0 x1 y1;
4907 postRedisplay "prect"
4909 | "pgoto", args ->
4910 scan args "%u %f %f"
4911 (fun pageno x y ->
4912 let optopaque =
4913 match getopaque pageno with
4914 | Some opaque -> opaque
4915 | None -> ~< E.s
4917 pgoto optopaque pageno x y;
4918 let rec fixx = function
4919 | [] -> ()
4920 | l :: rest ->
4921 if l.pageno = pageno
4922 then gotoxy (state.x - l.pagedispx) state.y
4923 else fixx rest
4925 let layout =
4926 let mult =
4927 match conf.columns with
4928 | Csingle _ | Csplit _ -> 1
4929 | Cmulti ((n, _, _), _) -> n
4931 layout 0 state.y (state.winw * mult) state.winh
4933 fixx layout
4935 | "activatewin", "" -> Wsi.activatewin ()
4936 | "quit", "" -> raise Quit
4937 | "keys", keys ->
4938 begin try
4939 let l = Config.keys_of_string keys in
4940 List.iter (fun (k, m) -> keyboard k m) l
4941 with exn -> adderrfmt "error processing keys" "`%S': %s\n"
4942 cmds @@ exntos exn
4944 | "clearrects", "" ->
4945 Hashtbl.clear state.prects;
4946 postRedisplay "clearrects"
4947 | _ ->
4948 adderrfmt "remote command"
4949 "error processing remote command: %S\n" cmds;
4952 let remote =
4953 let scratch = Bytes.create 80 in
4954 let buf = Buffer.create 80 in
4955 fun fd ->
4956 match tempfailureretry (Unix.read fd scratch 0) 80 with
4957 | exception Unix.Unix_error (Unix.EAGAIN, _, _) -> None
4958 | 0 ->
4959 Unix.close fd;
4960 if Buffer.length buf > 0
4961 then (
4962 let s = Buffer.contents buf in
4963 Buffer.clear buf;
4964 ract s;
4966 None
4967 | n ->
4968 let rec eat ppos =
4969 let nlpos =
4970 match Bytes.index_from scratch ppos '\n' with
4971 | pos -> if pos >= n then -1 else pos
4972 | exception Not_found -> -1
4974 if nlpos >= 0
4975 then (
4976 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
4977 let s = Buffer.contents buf in
4978 Buffer.clear buf;
4979 ract s;
4980 eat (nlpos+1);
4982 else (
4983 Buffer.add_subbytes buf scratch ppos (n-ppos);
4984 Some fd
4986 in eat 0
4989 let remoteopen path =
4990 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
4991 with exn ->
4992 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
4993 None
4996 let () =
4997 let gcconfig = ref false in
4998 let trimcachepath = ref E.s in
4999 let rcmdpath = ref E.s in
5000 let pageno = ref None in
5001 let openlast = ref false in
5002 let doreap = ref false in
5003 let csspath = ref None in
5004 selfexec := Sys.executable_name;
5005 Arg.parse
5006 (Arg.align
5007 [("-p", Arg.String (fun s -> state.password <- s),
5008 "<password> Set password");
5010 ("-f", Arg.String
5011 (fun s ->
5012 Config.fontpath := s;
5013 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
5015 "<path> Set path to the user interface font");
5017 ("-c", Arg.String
5018 (fun s ->
5019 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
5020 Config.confpath := s),
5021 "<path> Set path to the configuration file");
5023 ("-last", Arg.Set openlast, " Open last document");
5025 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
5026 "<page-number> Jump to page");
5028 ("-tcf", Arg.String (fun s -> trimcachepath := s),
5029 "<path> Set path to the trim cache file");
5031 ("-dest", Arg.String (fun s -> state.nameddest <- s),
5032 "<named-destination> Set named destination");
5034 ("-remote", Arg.String (fun s -> rcmdpath := s),
5035 "<path> Set path to the source of remote commands");
5037 ("-gc", Arg.Set gcconfig, " Collect config garbage");
5039 ("-v", Arg.Unit (fun () ->
5040 Printf.printf
5041 "%s\nconfiguration file: %s\n"
5042 (Help.version ())
5043 Config.defconfpath;
5044 exit 0), " Print version and exit");
5046 ("-css", Arg.String (fun s -> csspath := Some s),
5047 "<path> Set path to the style sheet to use with EPUB/HTML");
5049 ("-origin", Arg.String (fun s -> state.origin <- s),
5050 "<origin> <undocumented>");
5052 ("-no-title", Arg.Set ignoredoctitlte, " ignore document title");
5053 ("-layout-height", Arg.Set_int layouth,
5054 "<height> layout height html/epub/etc (-1, 0, N)");
5057 (fun s -> state.path <- s)
5058 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:");
5060 let histmode = emptystr state.path && not !openlast in
5062 if not (Config.load !openlast)
5063 then dolog "failed to load configuration";
5065 begin match !pageno with
5066 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
5067 | None -> ()
5068 end;
5070 fillhelp ();
5071 if !gcconfig
5072 then (
5073 Config.gc ();
5074 exit 0
5077 let mu =
5078 object (self)
5079 val mutable m_clicks = 0
5080 val mutable m_click_x = 0
5081 val mutable m_click_y = 0
5082 val mutable m_lastclicktime = infinity
5084 method private cleanup =
5085 state.roam <- noroam;
5086 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
5087 method expose = postRedisplay "expose"
5088 method visible v =
5089 let name =
5090 match v with
5091 | Wsi.Unobscured -> "unobscured"
5092 | Wsi.PartiallyObscured -> "partiallyobscured"
5093 | Wsi.FullyObscured -> "fullyobscured"
5095 vlog "visibility change %s" name
5096 method display = display ()
5097 method map mapped = vlog "mapped %b" mapped
5098 method reshape w h =
5099 self#cleanup;
5100 reshape w h
5101 method mouse b d x y m =
5102 if d && canselect ()
5103 then (
5105 * http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx
5107 m_click_x <- x;
5108 m_click_y <- y;
5109 if b = 1
5110 then (
5111 let t = now () in
5112 if abs x - m_click_x > 10
5113 || abs y - m_click_y > 10
5114 || abs_float (t -. m_lastclicktime) > 0.3
5115 then m_clicks <- 0;
5116 m_clicks <- m_clicks + 1;
5117 m_lastclicktime <- t;
5118 if m_clicks = 1
5119 then (
5120 self#cleanup;
5121 postRedisplay "cleanup";
5122 state.uioh <- state.uioh#button b d x y m;
5124 else state.uioh <- state.uioh#multiclick m_clicks x y m
5126 else (
5127 self#cleanup;
5128 m_clicks <- 0;
5129 m_lastclicktime <- infinity;
5130 state.uioh <- state.uioh#button b d x y m
5133 else (
5134 state.uioh <- state.uioh#button b d x y m
5136 method motion x y =
5137 state.mpos <- (x, y);
5138 state.uioh <- state.uioh#motion x y
5139 method pmotion x y =
5140 state.mpos <- (x, y);
5141 state.uioh <- state.uioh#pmotion x y
5142 method key k m =
5143 vlog "k=%#x m=%#x" k m;
5144 let mascm = m land (
5145 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
5146 ) in
5147 let keyboard k m =
5148 let x = state.x and y = state.y in
5149 keyboard k m;
5150 if x != state.x || y != state.y then self#cleanup
5152 match state.keystate with
5153 | KSnone ->
5154 let km = k, mascm in
5155 begin
5156 match
5157 let modehash = state.uioh#modehash in
5158 try Hashtbl.find modehash km
5159 with Not_found ->
5160 try Hashtbl.find (findkeyhash conf "global") km
5161 with Not_found -> KMinsrt (k, m)
5162 with
5163 | KMinsrt (k, m) -> keyboard k m
5164 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
5165 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
5167 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
5168 List.iter (fun (k, m) -> keyboard k m) insrt;
5169 state.keystate <- KSnone
5170 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
5171 state.keystate <- KSinto (keys, insrt)
5172 | KSinto _ -> state.keystate <- KSnone
5174 method enter x y =
5175 state.mpos <- (x, y);
5176 state.uioh <- state.uioh#pmotion x y
5177 method leave = state.mpos <- (-1, -1)
5178 method winstate wsl = state.winstate <- wsl
5179 method quit : 'a. 'a = raise Quit
5180 method scroll dx dy = state.uioh <- state.uioh#scroll dx dy
5181 method zoom z x y = state.uioh#zoom z x y
5182 method opendoc path =
5183 state.mode <- View;
5184 state.uioh <- uioh;
5185 postRedisplay "opendoc";
5186 opendoc path state.password
5189 let wsfd, winw, winh = Wsi.init mu conf.cwinw conf.cwinh platform in
5190 state.wsfd <- wsfd;
5192 if not @@ List.exists GlMisc.check_extension
5193 [ "GL_ARB_texture_rectangle"
5194 ; "GL_EXT_texture_recangle"
5195 ; "GL_NV_texture_rectangle" ]
5196 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
5198 if substratis (GlMisc.get_string `renderer) 0 "Mesa DRI Intel("
5199 then (
5200 defconf.sliceheight <- 1024;
5201 defconf.texcount <- 32;
5202 defconf.usepbo <- true;
5205 let cs, ss =
5206 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
5207 | exception exn ->
5208 dolog "socketpair failed: %s" @@ exntos exn;
5209 exit 1
5210 | (r, w) ->
5211 cloexec r;
5212 cloexec w;
5213 r, w
5216 setcheckers conf.checkers;
5218 opengl_has_pbo := GlMisc.check_extension "GL_ARB_pixel_buffer_object";
5220 begin match !csspath with
5221 | None -> ()
5222 | Some "" -> conf.css <- E.s
5223 | Some path ->
5224 let css = filecontents path in
5225 let l = String.length css in
5226 conf.css <-
5227 if substratis css (l-2) "\r\n"
5228 then String.sub css 0 (l-2)
5229 else (if css.[l-1] = '\n'
5230 then String.sub css 0 (l-1)
5231 else css);
5232 end;
5233 init cs (
5234 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
5235 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
5236 !Config.fontpath, !trimcachepath, !opengl_has_pbo
5238 List.iter GlArray.enable [`texture_coord; `vertex];
5239 state.ss <- ss;
5240 reshape ~firsttime:true winw winh;
5241 state.uioh <- uioh;
5242 if histmode
5243 then (
5244 Wsi.settitle "llpp (history)";
5245 enterhistmode ();
5247 else (
5248 state.text <- "Opening " ^ (mbtoutf8 state.path);
5249 opendoc state.path state.password;
5251 display ();
5252 Wsi.mapwin ();
5253 Wsi.setcursor Wsi.CURSOR_INHERIT;
5254 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
5256 let rec reap () =
5257 match Unix.waitpid [Unix.WNOHANG] ~-1 with
5258 | exception (Unix.Unix_error (Unix.ECHILD, _, _)) -> ()
5259 | exception exn -> dolog "Unix.waitpid: %s" @@ exntos exn
5260 | 0, _ -> ()
5261 | _pid, _status -> reap ()
5263 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
5265 let optrfd =
5266 ref (
5267 if nonemptystr !rcmdpath
5268 then remoteopen !rcmdpath
5269 else None
5273 let rec loop deadline =
5274 if !doreap
5275 then (
5276 doreap := false;
5277 reap ()
5279 let r = [state.ss; state.wsfd] in
5280 let r =
5281 match !optrfd with
5282 | None -> r
5283 | Some fd -> fd :: r
5285 if !redisplay
5286 then (
5287 Glutils.redisplay := false;
5288 display ();
5290 let timeout =
5291 let now = now () in
5292 if deadline > now
5293 then (
5294 if deadline = infinity
5295 then ~-.1.0
5296 else max 0.0 (deadline -. now)
5298 else 0.0
5300 let r, _, _ =
5301 try Unix.select r [] [] timeout
5302 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
5304 begin match r with
5305 | [] ->
5306 let newdeadline =
5307 match state.autoscroll with
5308 | Some step when step != 0 ->
5309 if state.slideshow land 1 = 1
5310 then (
5311 if state.slideshow land 2 = 0
5312 then state.slideshow <- state.slideshow lor 2
5313 else if step < 0 then prevpage () else nextpage ();
5314 deadline +. (float (abs step))
5316 else
5317 let y = state.y + step in
5318 let fy = if conf.maxhfit then state.winh else 0 in
5319 let y =
5320 if y < 0
5321 then state.maxy - fy
5322 else if y >= state.maxy - fy then 0 else y
5324 gotoxy state.x y;
5325 deadline +. 0.01
5326 | _ -> infinity
5328 loop newdeadline
5330 | l ->
5331 let rec checkfds = function
5332 | [] -> ()
5333 | fd :: rest when fd = state.ss ->
5334 let cmd = rcmd state.ss in
5335 act cmd;
5336 checkfds rest
5338 | fd :: rest when fd = state.wsfd ->
5339 Wsi.readresp fd;
5340 checkfds rest
5342 | fd :: rest when Some fd = !optrfd ->
5343 begin match remote fd with
5344 | None -> optrfd := remoteopen !rcmdpath;
5345 | opt -> optrfd := opt
5346 end;
5347 checkfds rest
5349 | _ :: rest ->
5350 dolog "select returned unknown descriptor";
5351 checkfds rest
5353 checkfds l;
5354 let newdeadline =
5355 let deadline1 =
5356 if deadline = infinity
5357 then now () +. 0.01
5358 else deadline
5360 match state.autoscroll with
5361 | Some step when step != 0 -> deadline1
5362 | _ -> infinity
5364 loop newdeadline
5365 end;
5367 match loop infinity with
5368 | exception Quit ->
5369 Config.save leavebirdseye;
5370 if hasunsavedchanges ()
5371 then save ()
5372 | _ -> error "umpossible - infinity reached"