Update
[llpp.git] / main.ml
blob15a34c8d5bfd32887a840a9fc1c2649a6b8bf9bb
1 open Utils;;
2 open Config;;
3 open Glutils;;
4 open Listview;;
6 external init : Unix.file_descr -> initparams -> unit = "ml_init";;
7 external seltext : opaque -> (int * int * int * int) -> unit = "ml_seltext";;
8 external hassel : opaque -> bool = "ml_hassel";;
9 external getpdimrect : int -> float array = "ml_getpdimrect";;
10 external whatsunder : opaque -> int -> int -> under = "ml_whatsunder";;
11 external markunder : opaque -> int -> int -> mark -> bool = "ml_markunder";;
12 external clearmark : opaque -> unit = "ml_clearmark";;
13 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
14 external getmaxw : unit -> float = "ml_getmaxw";;
15 external postprocess :
16 opaque -> int -> int -> int -> (int * string * int) -> int
17 = "ml_postprocess";;
18 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
19 external setaalevel : int -> unit = "ml_setaalevel";;
20 external realloctexts : int -> bool = "ml_realloctexts";;
21 external findlink : opaque -> linkdir -> link = "ml_findlink";;
22 external getlink : opaque -> int -> under = "ml_getlink";;
23 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
24 external getlinkcount : opaque -> int = "ml_getlinkcount";;
25 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links";;
26 external getpbo : width -> height -> colorspace -> opaque = "ml_getpbo";;
27 external freepbo : opaque -> unit = "ml_freepbo";;
28 external unmappbo : opaque -> unit = "ml_unmappbo";;
29 external bousable : unit -> bool = "ml_bo_usable";;
30 external unproject :
31 opaque -> int -> int -> (int * int) option = "ml_unproject";;
32 external project :
33 opaque -> int -> int -> float -> float -> (float * float) = "ml_project";;
34 external drawtile :
35 tileparams -> opaque -> unit = "ml_drawtile";;
36 external rectofblock :
37 opaque -> int -> int -> float array option = "ml_rectofblock";;
38 external begintiles : unit -> unit = "ml_begintiles";;
39 external endtiles : unit -> unit = "ml_endtiles";;
40 external addannot : opaque -> int -> int -> string -> unit = "ml_addannot";;
41 external modannot : opaque -> slinkindex -> string -> unit = "ml_modannot";;
42 external delannot : opaque -> slinkindex -> unit = "ml_delannot";;
43 external hasunsavedchanges : unit -> bool = "ml_hasunsavedchanges";;
44 external savedoc : string -> unit = "ml_savedoc";;
45 external getannotcontents :
46 opaque -> slinkindex -> string = "ml_getannotcontents";;
47 external drawprect :
48 opaque -> int -> int -> float array -> unit = "ml_drawprect";;
49 external wcmd : Unix.file_descr -> bytes -> int -> unit = "ml_wcmd";;
50 external rcmd : Unix.file_descr -> string = "ml_rcmd";;
51 external uritolocation :
52 string -> (pageno * float * float) = "ml_uritolocation";;
53 external isexternallink : string -> bool = "ml_isexternallink";;
55 (* copysel _will_ close the supplied descriptor *)
56 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
58 let selfexec = ref E.s;;
59 let ignoredoctitlte = ref false;;
60 let opengl_has_pbo = ref false;;
61 let layouth = ref ~-1;;
63 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
64 dolog {|rect {
65 x0,y0=(% f, % f)
66 x1,y1=(% f, % f)
67 x2,y2=(% f, % f)
68 x3,y3=(% f, % f)
69 }|} x0 y0 x1 y1 x2 y2 x3 y3;
72 let pgscale h = truncate (float h *. conf.pgscale);;
74 let hscrollh () =
75 if state.uioh#alwaysscrolly || ((conf.scrollb land scrollbhv != 0)
76 && (state.w > state.winw))
77 then conf.scrollbw
78 else 0
81 let setfontsize n =
82 fstate.fontsize <- n;
83 fstate.wwidth <- measurestr fstate.fontsize "w";
84 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
87 let vlog fmt =
88 if conf.verbose
89 then dolog fmt
90 else Printf.kprintf ignore fmt
93 let launchpath () =
94 if emptystr conf.pathlauncher
95 then dolog "%s" state.path
96 else (
97 let command =
98 Str.global_replace Utils.Re.percent state.path conf.pathlauncher in
99 match spawn command [] with
100 | _pid -> ()
101 | exception exn ->
102 dolog "failed to execute `%s': %s" command @@ exntos exn
106 let getopaque pageno =
107 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
108 with Not_found -> None
111 let pagetranslatepoint l x y =
112 let dy = y - l.pagedispy in
113 let y = dy + l.pagey in
114 let dx = x - l.pagedispx in
115 let x = dx + l.pagex in
116 (x, y);
119 let onppundermouse g x y d =
120 let rec f = function
121 | l :: rest ->
122 begin match getopaque l.pageno with
123 | Some opaque ->
124 let x0 = l.pagedispx in
125 let x1 = x0 + l.pagevw in
126 let y0 = l.pagedispy in
127 let y1 = y0 + l.pagevh in
128 if y >= y0 && y <= y1 && x >= x0 && x <= x1
129 then
130 let px, py = pagetranslatepoint l x y in
131 match g opaque l px py with
132 | Some res -> res
133 | None -> f rest
134 else f rest
135 | _ -> 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 = Format.ksprintf (fun s -> showtext '!' s) fmt;;
176 let pipesel opaque cmd =
177 if hassel opaque
178 then pipef ~closew:false "pipesel"
179 (fun w ->
180 copysel w opaque;
181 postRedisplay "pipesel"
182 ) cmd
185 let paxunder x y =
186 let g opaque l px py =
187 if markunder opaque px py conf.paxmark
188 then (
189 Some (fun () ->
190 match getopaque l.pageno with
191 | None -> ()
192 | Some opaque -> pipesel opaque conf.paxcmd
195 else None
197 postRedisplay "paxunder";
198 if conf.paxmark = Mark_page
199 then
200 List.iter (fun l ->
201 match getopaque l.pageno with
202 | None -> ()
203 | Some opaque -> clearmark opaque) state.layout;
204 state.roam <- onppundermouse g x y (fun () -> impmsg "whoopsie daisy");
207 let undertext = function
208 | Unone -> "none"
209 | Ulinkuri s -> s
210 | Utext s -> "font: " ^ s
211 | Uannotation (opaque, slinkindex) ->
212 "annotation: " ^ getannotcontents opaque slinkindex
215 let updateunder x y =
216 match getunder x y with
217 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
218 | Ulinkuri uri ->
219 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
220 Wsi.setcursor Wsi.CURSOR_INFO
221 | Utext s ->
222 if conf.underinfo then showtext 'f' ("ont: " ^ s);
223 Wsi.setcursor Wsi.CURSOR_TEXT
224 | Uannotation _ ->
225 if conf.underinfo then showtext 'a' "nnotation";
226 Wsi.setcursor Wsi.CURSOR_INFO
229 let showlinktype under =
230 if conf.underinfo && under != Unone
231 then showtext ' ' @@ undertext under
234 let intentry_with_suffix text key =
235 let text =
236 match [@warning "-4"] key with
237 | Keys.Ascii ('0'..'9' as c) -> addchar text c
238 | Keys.Ascii ('k' | 'm' | 'g' | 'K' | 'M' | 'G' as c) ->
239 addchar text @@ asciilower c
240 | _ ->
241 state.text <- "invalid key";
242 text
244 TEcont text
247 let wcmd fmt =
248 let b = Buffer.create 16 in
249 Printf.kbprintf
250 (fun b ->
251 let b = Buffer.to_bytes b in
252 wcmd state.ss b @@ Bytes.length b
253 ) b fmt
256 let nogeomcmds cmds =
257 match cmds with
258 | s, [] -> emptystr s
259 | _ -> false
262 let layoutN ((columns, coverA, coverB), b) x y sw sh =
263 let rec fold accu n =
264 if n = Array.length b
265 then accu
266 else
267 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
268 if (vy - y) > sh &&
269 (n = coverA - 1
270 || n = state.pagecount - coverB
271 || (n - coverA) mod columns = columns - 1)
272 then accu
273 else
274 let accu =
275 if vy + h > y
276 then
277 let pagey = max 0 (y - vy) in
278 let pagedispy = if pagey > 0 then 0 else vy - y in
279 let pagedispx, pagex =
280 let pdx =
281 if n = coverA - 1 || n = state.pagecount - coverB
282 then x + (sw - w) / 2
283 else dx + xoff + x
285 if pdx < 0
286 then 0, -pdx
287 else pdx, 0
289 let pagevw =
290 let vw = sw - pagedispx in
291 let pw = w - pagex in
292 min vw pw
294 let pagevh = min (h - pagey) (sh - pagedispy) in
295 if pagevw > 0 && pagevh > 0
296 then
297 let e =
298 { pageno = n
299 ; pagedimno = pdimno
300 ; pagew = w
301 ; pageh = h
302 ; pagex = pagex
303 ; pagey = pagey
304 ; pagevw = pagevw
305 ; pagevh = pagevh
306 ; pagedispx = pagedispx
307 ; pagedispy = pagedispy
308 ; pagecol = 0
311 e :: accu
312 else
313 accu
314 else
315 accu
317 fold accu (n+1)
319 if Array.length b = 0
320 then []
321 else List.rev (fold [] (page_of_y y))
324 let layoutS (columns, b) x y sw sh =
325 let rec fold accu n =
326 if n = Array.length b
327 then accu
328 else
329 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
330 if (vy - y) > sh
331 then accu
332 else
333 let accu =
334 if vy + pageh > y
335 then
336 let x = xoff + x in
337 let pagey = max 0 (y - vy) in
338 let pagedispy = if pagey > 0 then 0 else vy - y in
339 let pagedispx, pagex =
340 if px = 0
341 then (
342 if x < 0
343 then 0, -x
344 else x, 0
346 else (
347 let px = px - x in
348 if px < 0
349 then -px, 0
350 else 0, px
353 let pagecolw = pagew/columns in
354 let pagedispx =
355 if pagecolw < sw
356 then pagedispx + ((sw - pagecolw) / 2)
357 else pagedispx
359 let pagevw =
360 let vw = sw - pagedispx in
361 let pw = pagew - pagex in
362 min vw pw
364 let pagevw = min pagevw pagecolw in
365 let pagevh = min (pageh - pagey) (sh - pagedispy) in
366 if pagevw > 0 && pagevh > 0
367 then
368 let e =
369 { pageno = n/columns
370 ; pagedimno = pdimno
371 ; pagew = pagew
372 ; pageh = pageh
373 ; pagex = pagex
374 ; pagey = pagey
375 ; pagevw = pagevw
376 ; pagevh = pagevh
377 ; pagedispx = pagedispx
378 ; pagedispy = pagedispy
379 ; pagecol = n mod columns
382 e :: accu
383 else
384 accu
385 else
386 accu
388 fold accu (n+1)
390 List.rev (fold [] 0)
393 let layout x y sw sh =
394 if nogeomcmds state.geomcmds
395 then
396 match conf.columns with
397 | Csingle b -> layoutN ((1, 0, 0), b) x y sw sh
398 | Cmulti c -> layoutN c x y sw sh
399 | Csplit s -> layoutS s x y sw sh
400 else []
403 let maxy () = state.maxy - if conf.maxhfit then state.winh else 0;;
404 let clamp incr = bound (state.y + incr) 0 @@ maxy ();;
406 let itertiles l f =
407 let tilex = l.pagex mod conf.tilew in
408 let tiley = l.pagey mod conf.tileh in
410 let col = l.pagex / conf.tilew in
411 let row = l.pagey / conf.tileh in
413 let rec rowloop row y0 dispy h =
414 if h = 0
415 then ()
416 else (
417 let dh = conf.tileh - y0 in
418 let dh = min h dh in
419 let rec colloop col x0 dispx w =
420 if w != 0
421 then
422 let dw = conf.tilew - x0 in
423 let dw = min w dw in
424 f col row dispx dispy x0 y0 dw dh;
425 colloop (col+1) 0 (dispx+dw) (w-dw)
427 colloop col tilex l.pagedispx l.pagevw;
428 rowloop (row+1) 0 (dispy+dh) (h-dh)
431 if l.pagevw > 0 && l.pagevh > 0
432 then rowloop row tiley l.pagedispy l.pagevh;
435 let gettileopaque l col row =
436 let key = l.pageno, state.gen, conf.colorspace,
437 conf.angle, l.pagew, l.pageh, col, row in
438 try Some (Hashtbl.find state.tilemap key)
439 with Not_found -> None
442 let puttileopaque l col row gen colorspace angle opaque size elapsed =
443 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
444 Hashtbl.add state.tilemap key (opaque, size, elapsed)
447 let drawtiles l color =
448 GlDraw.color color;
449 begintiles ();
450 let f col row x y tilex tiley w h =
451 match gettileopaque l col row with
452 | Some (opaque, _, t) ->
453 let params = x, y, w, h, tilex, tiley in
454 if conf.invert
455 then GlTex.env (`mode `blend);
456 drawtile params opaque;
457 if conf.invert
458 then GlTex.env (`mode `modulate);
459 if conf.debug
460 then (
461 endtiles ();
462 let s = Printf.sprintf
463 "%d[%d,%d] %f sec"
464 l.pageno col row t
466 let w = measurestr fstate.fontsize s in
467 GlDraw.color (0.0, 0.0, 0.0);
468 filledrect
469 (float (x-2))
470 (float (y-2))
471 (float (x+2) +. w)
472 (float (y + fstate.fontsize + 2));
473 GlDraw.color color;
474 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
475 begintiles ();
478 | None ->
479 endtiles ();
480 let w =
481 let lw = state.winw - x in
482 min lw w
483 and h =
484 let lh = state.winh - y in
485 min lh h
487 if conf.invert
488 then GlTex.env (`mode `blend);
489 begin match state.checkerstexid with
490 | Some id ->
491 Gl.enable `texture_2d;
492 GlTex.bind_texture ~target:`texture_2d id;
493 let x0 = float x
494 and y0 = float y
495 and x1 = float (x+w)
496 and y1 = float (y+h) in
498 let tw = float w /. 16.0
499 and th = float h /. 16.0 in
500 let tx0 = float tilex /. 16.0
501 and ty0 = float tiley /. 16.0 in
502 let tx1 = tx0 +. tw
503 and ty1 = ty0 +. th in
504 Raw.sets_float Glutils.vraw ~pos:0
505 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
506 Raw.sets_float Glutils.traw ~pos:0
507 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
508 GlArray.vertex `two Glutils.vraw;
509 GlArray.tex_coord `two Glutils.traw;
510 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
511 Gl.disable `texture_2d;
513 | None ->
514 GlDraw.color (1.0, 1.0, 1.0);
515 filledrect (float x) (float y) (float (x+w)) (float (y+h));
516 end;
517 if conf.invert
518 then GlTex.env (`mode `modulate);
519 if w > 128 && h > fstate.fontsize + 10
520 then (
521 let c = if conf.invert then 1.0 else 0.0 in
522 GlDraw.color (c, c, c);
523 let c, r =
524 if conf.verbose
525 then (col*conf.tilew, row*conf.tileh)
526 else col, row
528 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
530 GlDraw.color color;
531 begintiles ();
533 itertiles l f;
534 endtiles ();
537 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
539 let tilevisible1 l x y =
540 let ax0 = l.pagex
541 and ax1 = l.pagex + l.pagevw
542 and ay0 = l.pagey
543 and ay1 = l.pagey + l.pagevh in
545 let bx0 = x
546 and by0 = y in
547 let bx1 = min (bx0 + conf.tilew) l.pagew
548 and by1 = min (by0 + conf.tileh) l.pageh in
550 let rx0 = max ax0 bx0
551 and ry0 = max ay0 by0
552 and rx1 = min ax1 bx1
553 and ry1 = min ay1 by1 in
555 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
556 nonemptyintersection
559 let tilevisible layout n x y =
560 let rec findpageinlayout m = function
561 | l :: rest when l.pageno = n ->
562 tilevisible1 l x y || (
563 match conf.columns with
564 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
565 | Csplit _ | Csingle _ | Cmulti _ -> false
567 | _ :: rest -> findpageinlayout 0 rest
568 | [] -> false
570 findpageinlayout 0 layout;
573 let tileready l x y =
574 tilevisible1 l x y &&
575 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
578 let tilepage n p layout =
579 let rec loop = function
580 | l :: rest ->
581 if l.pageno = n
582 then
583 let f col row _ _ _ _ _ _ =
584 if state.currently = Idle
585 then
586 match gettileopaque l col row with
587 | Some _ -> ()
588 | None ->
589 let x = col*conf.tilew
590 and y = row*conf.tileh in
591 let w =
592 let w = l.pagew - x in
593 min w conf.tilew
595 let h =
596 let h = l.pageh - y in
597 min h conf.tileh
599 let pbo =
600 if conf.usepbo
601 then getpbo w h conf.colorspace
602 else ~< "0"
604 wcmd "tile %s %d %d %d %d %s" (~> p) x y w h (~> pbo);
605 state.currently <-
606 Tiling (
607 l, p, conf.colorspace, conf.angle,
608 state.gen, col, row, conf.tilew, conf.tileh
611 itertiles l f;
612 else
613 loop rest
615 | [] -> ()
617 if nogeomcmds state.geomcmds
618 then loop layout;
621 let preloadlayout x y sw sh =
622 let y = if y < sh then 0 else y - sh in
623 let x = min 0 (x + sw) in
624 let h = sh*3 in
625 let w = sw*3 in
626 layout x y w h;
629 let load pages =
630 let rec loop pages =
631 if state.currently = Idle
632 then
633 match pages with
634 | l :: rest ->
635 begin match getopaque l.pageno with
636 | None ->
637 wcmd "page %d %d" l.pageno l.pagedimno;
638 state.currently <- Loading (l, state.gen);
639 | Some opaque ->
640 tilepage l.pageno opaque pages;
641 loop rest
642 end;
643 | _ -> ()
645 if nogeomcmds state.geomcmds
646 then loop pages
649 let preload pages =
650 load pages;
651 if conf.preload && state.currently = Idle
652 then load (preloadlayout state.x state.y state.winw state.winh);
655 let layoutready layout =
656 let rec fold all ls =
657 all && match ls with
658 | l :: rest ->
659 let seen = ref false in
660 let allvisible = ref true in
661 let foo col row _ _ _ _ _ _ =
662 seen := true;
663 allvisible := !allvisible &&
664 begin match gettileopaque l col row with
665 | Some _ -> true
666 | None -> false
669 itertiles l foo;
670 fold (!seen && !allvisible) rest
671 | [] -> true
673 let alltilesvisible = fold true layout in
674 alltilesvisible;
677 let gotoxy x y =
678 let y = bound y 0 state.maxy in
679 let y, layout =
680 let layout = layout x y state.winw state.winh in
681 postRedisplay "gotoxy ready";
682 y, layout
684 state.x <- x;
685 state.y <- y;
686 state.layout <- layout;
687 begin match state.mode with
688 | LinkNav ln ->
689 begin match ln with
690 | Ltexact (pageno, linkno) ->
691 let rec loop = function
692 | [] ->
693 state.lnava <- Some (pageno, linkno);
694 state.mode <- LinkNav (Ltgendir 0)
695 | l :: _ when l.pageno = pageno ->
696 begin match getopaque pageno with
697 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
698 | Some opaque ->
699 let x0, y0, x1, y1 = getlinkrect opaque linkno in
700 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
701 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
702 then state.mode <- LinkNav (Ltgendir 0)
704 | _ :: rest -> loop rest
706 loop layout
707 | Ltnotready _ | Ltgendir _ -> ()
709 | Birdseye _ | Textentry _ | View -> ()
710 end;
711 begin match state.mode with
712 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
713 if not (pagevisible layout pageno)
714 then (
715 match state.layout with
716 | [] -> ()
717 | l :: _ ->
718 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno, anchor)
720 | LinkNav lt ->
721 begin match lt with
722 | Ltnotready (_, dir)
723 | Ltgendir dir ->
724 let linknav =
725 let rec loop = function
726 | [] -> lt
727 | l :: rest ->
728 match getopaque l.pageno with
729 | None -> Ltnotready (l.pageno, dir)
730 | Some opaque ->
731 let link =
732 let ld =
733 if dir = 0
734 then LDfirstvisible (l.pagex, l.pagey, dir)
735 else (
736 if dir > 0 then LDfirst else LDlast
739 findlink opaque ld
741 match link with
742 | Lnotfound -> loop rest
743 | Lfound n ->
744 showlinktype (getlink opaque n);
745 Ltexact (l.pageno, n)
747 loop state.layout
749 state.mode <- LinkNav linknav
750 | Ltexact _ -> ()
752 | Textentry _ | View -> ()
753 end;
754 preload layout;
755 if conf.updatecurs
756 then (
757 let mx, my = state.mpos in
758 updateunder mx my;
762 let conttiling pageno opaque =
763 tilepage pageno opaque
764 (if conf.preload
765 then preloadlayout state.x state.y state.winw state.winh
766 else state.layout)
769 let gotoxy x y =
770 if not conf.verbose then state.text <- E.s;
771 gotoxy x y;
774 let getanchory (n, top, dtop) =
775 let y, h = getpageyh n in
776 if conf.presentation
777 then
778 let ips = calcips h in
779 y + truncate (top*.float h -. dtop*.float ips) + ips;
780 else y + truncate (top*.float h -. dtop*.float conf.interpagespace)
783 let gotoanchor anchor = gotoxy state.x (getanchory anchor);;
784 let addnav () = getanchor () |> cbput state.hists.nav;;
785 let addnavnorc () = getanchor () |> cbput_dont_update_rc state.hists.nav;;
787 let getnav dir =
788 let anchor = cbgetc state.hists.nav dir in
789 getanchory anchor;
792 let gotopage n top =
793 let y, h = getpageyh n in
794 let y = y + (truncate (top *. float h)) in
795 gotoxy state.x y
798 let gotopage1 n top =
799 let y = getpagey n in
800 let y = y + top in
801 gotoxy state.x y
804 let invalidate s f =
805 Glutils.redisplay := false;
806 state.layout <- [];
807 state.pdims <- [];
808 state.rects <- [];
809 state.rects1 <- [];
810 match state.geomcmds with
811 | ps, [] when emptystr ps ->
812 f ();
813 state.geomcmds <- s, [];
814 | ps, [] -> state.geomcmds <- ps, [s, f];
815 | ps, (s', _) :: rest when s' = s -> state.geomcmds <- ps, ((s, f) :: rest);
816 | ps, cmds -> state.geomcmds <- ps, ((s, f) :: cmds);
819 let flushpages () =
820 Hashtbl.iter (fun _ opaque -> wcmd "freepage %s" (~> opaque)) state.pagemap;
821 Hashtbl.clear state.pagemap;
824 let flushtiles () =
825 if not (Queue.is_empty state.tilelru)
826 then (
827 Queue.iter (fun (k, p, s) ->
828 wcmd "freetile %s" (~> p);
829 state.memused <- state.memused - s;
830 Hashtbl.remove state.tilemap k;
831 ) state.tilelru;
832 state.uioh#infochanged Memused;
833 Queue.clear state.tilelru;
835 load state.layout;
838 let stateh h =
839 let h = truncate (float h*.conf.zoom) in
840 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
841 h - d
844 let fillhelp () =
845 state.help <-
846 let sl = keystostrlist conf in
847 let rec loop accu =
848 function | [] -> accu
849 | s :: rest -> loop ((s, 0, Noaction) :: accu) rest
850 in Help.makehelp conf.urilauncher
851 @ (("", 0, Noaction) :: loop [] sl) |> Array.of_list
854 let opendoc path password =
855 state.path <- path;
856 state.password <- password;
857 state.gen <- state.gen + 1;
858 state.docinfo <- [];
859 state.outlines <- [||];
861 flushpages ();
862 setaalevel conf.aalevel;
863 let titlepath =
864 if emptystr state.origin
865 then path
866 else state.origin
868 Wsi.settitle ("llpp " ^ mbtoutf8 (Filename.basename titlepath));
869 wcmd "open %d %d %s\000%s\000%s\000"
870 (btod conf.usedoccss) !layouth
871 path password conf.css;
872 invalidate "reqlayout"
873 (fun () ->
874 wcmd "reqlayout %d %d %d %s\000"
875 conf.angle (FMTE.to_int conf.fitmodel)
876 (stateh state.winh) state.nameddest
878 fillhelp ();
881 let reload () =
882 state.anchor <- getanchor ();
883 opendoc state.path state.password;
886 let scalecolor c =
887 let c = c *. conf.colorscale in
888 (c, c, c);
891 let scalecolor2 (r, g, b) =
892 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
895 let docolumns columns =
896 match columns with
897 | Csingle _ ->
898 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
899 let rec loop pageno pdimno pdim y ph pdims =
900 if pageno = state.pagecount
901 then ()
902 else
903 let pdimno, ((_, w, h, xoff) as pdim), pdims =
904 match pdims with
905 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
906 pdimno+1, pdim, rest
907 | _ ->
908 pdimno, pdim, pdims
910 let x = max 0 (((state.winw - w) / 2) - xoff) in
911 let y =
912 y + (if conf.presentation
913 then (if pageno = 0 then calcips h else calcips ph + calcips h)
914 else (if pageno = 0 then 0 else conf.interpagespace))
916 a.(pageno) <- (pdimno, x, y, pdim);
917 loop (pageno+1) pdimno pdim (y + h) h pdims
919 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
920 conf.columns <- Csingle a;
922 | Cmulti ((columns, coverA, coverB), _) ->
923 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
924 let rec loop pageno pdimno pdim x y rowh pdims =
925 let rec fixrow m =
926 if m = pageno then () else
927 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
928 if h < rowh
929 then (
930 let y = y + (rowh - h) / 2 in
931 a.(m) <- (pdimno, x, y, pdim);
933 fixrow (m+1)
935 if pageno = state.pagecount
936 then fixrow (((pageno - 1) / columns) * columns)
937 else
938 let pdimno, ((_, w, h, xoff) as pdim), pdims =
939 match pdims with
940 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
941 pdimno+1, pdim, rest
942 | _ -> pdimno, pdim, pdims
944 let x, y, rowh' =
945 if pageno = coverA - 1 || pageno = state.pagecount - coverB
946 then (
947 let x = (state.winw - w) / 2 in
948 let ips =
949 if conf.presentation then calcips h else conf.interpagespace in
950 x, y + ips + rowh, h
952 else (
953 if (pageno - coverA) mod columns = 0
954 then (
955 let x = max 0 (state.winw - state.w) / 2 in
956 let y =
957 if conf.presentation
958 then
959 let ips = calcips h in
960 y + (if pageno = 0 then 0 else calcips rowh + ips)
961 else
962 y + (if pageno = 0 then 0 else conf.interpagespace)
964 x, y + rowh, h
966 else x, y, max rowh h
969 let y =
970 if pageno > 1 && (pageno - coverA) mod columns = 0
971 then (
972 let y =
973 if pageno = columns && conf.presentation
974 then (
975 let ips = calcips rowh in
976 for i = 0 to pred columns
978 let (pdimno, x, y, pdim) = a.(i) in
979 a.(i) <- (pdimno, x, y+ips, pdim)
980 done;
981 y+ips;
983 else y
985 fixrow (pageno - columns);
988 else y
990 a.(pageno) <- (pdimno, x, y, pdim);
991 let x = x + w + xoff*2 + conf.interpagespace in
992 loop (pageno+1) pdimno pdim x y rowh' pdims
994 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
995 conf.columns <- Cmulti ((columns, coverA, coverB), a);
997 | Csplit (c, _) ->
998 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
999 let rec loop pageno pdimno pdim y pdims =
1000 if pageno != state.pagecount
1001 then
1002 let pdimno, ((_, w, h, _) as pdim), pdims =
1003 match pdims with
1004 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1005 pdimno+1, pdim, rest
1006 | _ -> pdimno, pdim, pdims
1008 let cw = w / c in
1009 let rec loop1 n x y =
1010 if n = c then y else (
1011 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1012 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1015 let y = loop1 0 0 y in
1016 loop (pageno+1) pdimno pdim y pdims
1018 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1019 conf.columns <- Csplit (c, a);
1022 let represent () =
1023 docolumns conf.columns;
1024 state.maxy <- calcheight ();
1025 if state.reprf == noreprf
1026 then (
1027 match state.mode with
1028 | Birdseye (_, _, pageno, _, _) ->
1029 let y, h = getpageyh pageno in
1030 let top = (state.winh - h) / 2 in
1031 gotoxy state.x (max 0 (y - top))
1032 | Textentry _ | View | LinkNav _ ->
1033 let y = getanchory state.anchor in
1034 let y = min y (state.maxy - state.winh) in
1035 gotoxy state.x y;
1037 else (
1038 state.reprf ();
1039 state.reprf <- noreprf;
1043 let reshape ?(firsttime=false) w h =
1044 GlDraw.viewport ~x:0 ~y:0 ~w ~h;
1045 if not firsttime && nogeomcmds state.geomcmds
1046 then state.anchor <- getanchor ();
1048 state.winw <- w;
1049 let w = truncate (float w *. conf.zoom) in
1050 let w = max w 2 in
1051 state.winh <- h;
1052 setfontsize fstate.fontsize;
1053 GlMat.mode `modelview;
1054 GlMat.load_identity ();
1056 GlMat.mode `projection;
1057 GlMat.load_identity ();
1058 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1059 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1060 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1062 let relx =
1063 if conf.zoom <= 1.0
1064 then 0.0
1065 else float state.x /. float state.w
1067 invalidate "geometry"
1068 (fun () ->
1069 state.w <- w;
1070 if not firsttime
1071 then state.x <- truncate (relx *. float w);
1072 let w =
1073 match conf.columns with
1074 | Csingle _ -> w
1075 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1076 | Csplit (c, _) -> w * c
1078 wcmd "geometry %d %d %d" w (stateh h) (FMTE.to_int conf.fitmodel)
1082 let gctiles () =
1083 let len = Queue.length state.tilelru in
1084 let layout = lazy (if conf.preload
1085 then preloadlayout state.x state.y state.winw state.winh
1086 else state.layout) in
1087 let rec loop qpos =
1088 if state.memused > conf.memlimit
1089 then (
1090 if qpos < len
1091 then
1092 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1093 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1094 let (_, pw, ph, _) = getpagedim n in
1095 if gen = state.gen
1096 && colorspace = conf.colorspace
1097 && angle = conf.angle
1098 && pagew = pw
1099 && pageh = ph
1100 && (
1101 let x = col*conf.tilew and y = row*conf.tileh in
1102 tilevisible (Lazy.force_val layout) n x y
1104 then Queue.push lruitem state.tilelru
1105 else (
1106 freepbo p;
1107 wcmd "freetile %s" (~> p);
1108 state.memused <- state.memused - s;
1109 state.uioh#infochanged Memused;
1110 Hashtbl.remove state.tilemap k;
1112 loop (qpos+1)
1115 loop 0
1118 let onpagerect pageno f =
1119 let b =
1120 match conf.columns with
1121 | Cmulti (_, b) -> b
1122 | Csingle b -> b
1123 | Csplit (_, b) -> b
1125 if pageno >= 0 && pageno < Array.length b
1126 then
1127 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1128 f w h
1131 let gotopagexy1 pageno x y =
1132 let _,w1,h1,leftx = getpagedim pageno in
1133 let top = y /. (float h1) in
1134 let left = x /. (float w1) in
1135 let py, w, h = getpageywh pageno in
1136 let wh = state.winh in
1137 let x = left *. (float w) in
1138 let x = leftx + state.x + truncate x in
1139 let sx =
1140 if x < 0 || x >= state.winw
1141 then state.x - x
1142 else state.x
1144 let pdy = truncate (top *. float h) in
1145 let y' = py + pdy in
1146 let dy = y' - state.y in
1147 let sy =
1148 if x != state.x || not (dy > 0 && dy < wh)
1149 then (
1150 if conf.presentation
1151 then
1152 if abs (py - y') > wh
1153 then y'
1154 else py
1155 else y';
1157 else state.y
1159 if state.x != sx || state.y != sy
1160 then gotoxy sx sy
1161 else gotoxy state.x state.y;
1164 let gotopagexy pageno x y =
1165 match state.mode with
1166 | Birdseye _ -> gotopage pageno 0.0
1167 | Textentry _ | View | LinkNav _ -> gotopagexy1 pageno x y
1170 let getpassword () =
1171 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1172 if emptystr passcmd
1173 then E.s
1174 else getcmdoutput
1175 (fun s ->
1176 impmsg "error getting password: %s" s;
1177 dolog "%s" s) passcmd;
1180 let pgoto opaque pageno x y =
1181 let pdimno = getpdimno pageno in
1182 let x, y = project opaque pageno pdimno x y in
1183 gotopagexy pageno x y;
1186 let act cmds =
1187 (* dolog "%S" cmds; *)
1188 let spl = splitatchar cmds ' ' in
1189 let scan s fmt f =
1190 try Scanf.sscanf s fmt f
1191 with exn ->
1192 dolog "error processing '%S': %s" cmds @@ exntos exn;
1193 exit 1
1195 let addoutline outline =
1196 match state.currently with
1197 | Outlining outlines -> state.currently <- Outlining (outline :: outlines)
1198 | Idle -> state.currently <- Outlining [outline]
1199 | Loading _ | Tiling _ ->
1200 dolog "invalid outlining state";
1201 logcurrently state.currently
1203 match spl with
1204 | "clear", "" ->
1205 state.pdims <- [];
1206 state.uioh#infochanged Pdim;
1208 | "clearrects", "" ->
1209 state.rects <- state.rects1;
1210 postRedisplay "clearrects";
1212 | "continue", args ->
1213 let n = scan args "%u" (fun n -> n) in
1214 state.pagecount <- n;
1215 begin match state.currently with
1216 | Outlining l ->
1217 state.currently <- Idle;
1218 state.outlines <- Array.of_list (List.rev l)
1219 | Idle | Loading _ | Tiling _ -> ()
1220 end;
1222 let cur, cmds = state.geomcmds in
1223 if emptystr cur
1224 then failwith "umpossible";
1226 begin match List.rev cmds with
1227 | [] ->
1228 state.geomcmds <- E.s, [];
1229 represent ();
1230 | (s, f) :: rest ->
1231 f ();
1232 state.geomcmds <- s, List.rev rest;
1233 end;
1234 postRedisplay "continue";
1236 | "msg", args ->
1237 showtext ' ' args
1239 | "vmsg", args ->
1240 if conf.verbose
1241 then showtext ' ' args
1243 | "emsg", args ->
1244 Buffer.add_string state.errmsgs args;
1245 state.newerrmsgs <- true;
1246 postRedisplay "error message"
1248 | "progress", args ->
1249 let progress, text =
1250 scan args "%f %n"
1251 (fun f pos ->
1252 f, String.sub args pos (String.length args - pos))
1254 state.text <- text;
1255 state.progress <- progress;
1256 postRedisplay "progress"
1258 | "firstmatch", args ->
1259 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1260 scan args "%u %d %f %f %f %f %f %f %f %f"
1261 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1262 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1264 let y = (getpagey pageno) + truncate y0 in
1265 let x =
1266 if (state.x < - truncate x0) || (state.x > state.winw - truncate x1)
1267 then state.winw/2 - truncate (x0 /. 2. +. x1 /. 2.)
1268 else state.x
1270 addnav ();
1271 gotoxy x y;
1272 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1273 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1275 | "match", args ->
1276 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1277 scan args "%u %d %f %f %f %f %f %f %f %f"
1278 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1279 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1281 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1282 state.rects1 <-
1283 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1285 | "page", args ->
1286 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1287 let pageopaque = ~< pageopaques in
1288 begin match state.currently with
1289 | Loading (l, gen) ->
1290 vlog "page %d took %f sec" l.pageno t;
1291 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1292 let preloadedpages =
1293 if conf.preload
1294 then preloadlayout state.x state.y state.winw state.winh
1295 else state.layout
1297 let evict () =
1298 let set = List.fold_left (fun s l -> IntSet.add l.pageno s)
1299 IntSet.empty preloadedpages
1301 let evictedpages =
1302 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1303 if not (IntSet.mem pageno set)
1304 then (
1305 wcmd "freepage %s" (~> opaque);
1306 key :: accu
1308 else accu
1309 ) state.pagemap []
1311 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1313 evict ();
1314 state.currently <- Idle;
1315 if gen = state.gen
1316 then (
1317 tilepage l.pageno pageopaque state.layout;
1318 load state.layout;
1319 load preloadedpages;
1320 let visible = pagevisible state.layout l.pageno in
1321 if visible
1322 then (
1323 match state.mode with
1324 | LinkNav (Ltnotready (pageno, dir)) ->
1325 if pageno = l.pageno
1326 then (
1327 let link =
1328 let ld =
1329 if dir = 0
1330 then LDfirstvisible (l.pagex, l.pagey, dir)
1331 else (
1332 if dir > 0 then LDfirst else LDlast
1335 findlink pageopaque ld
1337 match link with
1338 | Lnotfound -> ()
1339 | Lfound n ->
1340 showlinktype (getlink pageopaque n);
1341 state.mode <- LinkNav (Ltexact (l.pageno, n))
1343 | LinkNav (Ltgendir _)
1344 | LinkNav (Ltexact _)
1345 | View
1346 | Birdseye _
1347 | Textentry _ -> ()
1350 if visible && layoutready state.layout
1351 then (
1352 postRedisplay "page";
1356 | Idle | Tiling _ | Outlining _ ->
1357 dolog "Inconsistent loading state";
1358 logcurrently state.currently;
1359 exit 1
1362 | "tile" , args ->
1363 let (x, y, opaques, size, t) =
1364 scan args "%u %u %s %u %f"
1365 (fun x y p size t -> (x, y, p, size, t))
1367 let opaque = ~< opaques in
1368 begin match state.currently with
1369 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1370 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1372 unmappbo opaque;
1373 if tilew != conf.tilew || tileh != conf.tileh
1374 then (
1375 wcmd "freetile %s" (~> opaque);
1376 state.currently <- Idle;
1377 load state.layout;
1379 else (
1380 puttileopaque l col row gen cs angle opaque size t;
1381 state.memused <- state.memused + size;
1382 state.uioh#infochanged Memused;
1383 gctiles ();
1384 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1385 opaque, size) state.tilelru;
1387 state.currently <- Idle;
1388 if gen = state.gen
1389 && conf.colorspace = cs
1390 && conf.angle = angle
1391 && tilevisible state.layout l.pageno x y
1392 then conttiling l.pageno pageopaque;
1394 preload state.layout;
1395 if gen = state.gen
1396 && conf.colorspace = cs
1397 && conf.angle = angle
1398 && tilevisible state.layout l.pageno x y
1399 && layoutready state.layout
1400 then postRedisplay "tile nothrottle";
1403 | Idle | Loading _ | Outlining _ ->
1404 dolog "Inconsistent tiling state";
1405 logcurrently state.currently;
1406 exit 1
1409 | "pdim", args ->
1410 let (n, w, h, _) as pdim =
1411 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1413 let pdim =
1414 match conf.fitmodel with
1415 | FitWidth -> pdim
1416 | FitPage | FitProportional ->
1417 match conf.columns with
1418 | Csplit _ -> (n, w, h, 0)
1419 | Csingle _ | Cmulti _ -> pdim
1421 state.pdims <- pdim :: state.pdims;
1422 state.uioh#infochanged Pdim
1424 | "o", args ->
1425 let (l, n, t, h, pos) =
1426 scan args "%u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
1428 let s = String.sub args pos (String.length args - pos) in
1429 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1431 | "ou", args ->
1432 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1433 let s = String.sub args pos len in
1434 let pos2 = pos + len + 1 in
1435 let uri = String.sub args pos2 (String.length args - pos2) in
1436 addoutline (s, l, Ouri uri)
1438 | "on", args ->
1439 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1440 let s = String.sub args pos (String.length args - pos) in
1441 addoutline (s, l, Onone)
1443 | "a", args ->
1444 let (n, l, t) = scan args "%u %d %d" (fun n l t -> n, l, t) in
1445 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
1447 | "info", args ->
1448 let c, v = splitatchar args '\t' in
1449 let s =
1450 if nonemptystr v
1451 then
1452 if c = "Title"
1453 then (
1454 conf.title <- v;
1455 if not !ignoredoctitlte
1456 then Wsi.settitle v;
1457 args
1459 else
1460 if let len = String.length c in
1461 len > 6 && ((String.sub c (len-4) 4) = "date")
1462 then (
1463 if String.length v >= 7 && v.[0] = 'D' && v.[1] = ':'
1464 then
1465 let b = Buffer.create 10 in
1466 Printf.bprintf b "%s\t" c;
1467 let sub p l c =
1469 Buffer.add_substring b v p l;
1470 Buffer.add_char b c;
1471 with exn -> Buffer.add_string b @@ exntos exn
1473 sub 2 4 '/';
1474 sub 6 2 '/';
1475 sub 8 2 ' ';
1476 sub 10 2 ':';
1477 sub 12 2 ':';
1478 sub 14 2 ' ';
1479 Buffer.add_char b '[';
1480 Buffer.add_string b v;
1481 Buffer.add_char b ']';
1482 Buffer.contents b
1483 else args
1485 else args
1486 else args
1488 state.docinfo <- (1, s) :: state.docinfo
1490 | "infoend", "" ->
1491 state.docinfo <- List.rev state.docinfo;
1492 state.uioh#infochanged Docinfo
1494 | "pass", args ->
1495 if args = "fail"
1496 then Wsi.settitle "Wrong password";
1497 let password = getpassword () in
1498 if emptystr password
1499 then error "document is password protected"
1500 else opendoc state.path password
1502 | _ -> error "unknown cmd `%S'" cmds
1505 let onhist cb =
1506 let rc = cb.rc in
1507 let action = function
1508 | HCprev -> cbget cb ~-1
1509 | HCnext -> cbget cb 1
1510 | HCfirst -> cbget cb ~-(cb.rc)
1511 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1512 and cancel () = cb.rc <- rc
1513 in (action, cancel)
1516 let search pattern forward =
1517 match conf.columns with
1518 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1519 | Csingle _ | Cmulti _ ->
1520 if nonemptystr pattern
1521 then
1522 let pn, py =
1523 match state.layout with
1524 | [] -> 0, 0
1525 | l :: _ -> l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1527 wcmd "search %d %d %d %d,%s\000"
1528 (btod conf.icase) pn py (btod forward) pattern;
1531 let intentry text key =
1532 let text =
1533 if emptystr text && key = Keys.Ascii '-'
1534 then addchar text '-'
1535 else
1536 match [@warning "-4"] key with
1537 | Keys.Ascii ('0'..'9' as c) -> addchar text c
1538 | _ ->
1539 state.text <- "invalid key";
1540 text
1542 TEcont text
1545 let linknact f s =
1546 if nonemptystr s
1547 then (
1548 let n =
1549 let l = String.length s in
1550 let rec loop pos n =
1551 if pos = l
1552 then n
1553 else
1554 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1555 loop (pos+1) (n*26 + m)
1556 in loop 0 0
1558 let rec loop n = function
1559 | [] -> ()
1560 | l :: rest ->
1561 match getopaque l.pageno with
1562 | None -> loop n rest
1563 | Some opaque ->
1564 let m = getlinkcount opaque in
1565 if n < m
1566 then (
1567 let under = getlink opaque n in
1568 f under
1570 else loop (n-m) rest
1572 loop n state.layout;
1576 let linknentry text key = match [@warning "-4"] key with
1577 | Keys.Ascii c ->
1578 let text = addchar text c in
1579 linknact (fun under -> state.text <- undertext under) text;
1580 TEcont text
1581 | _ ->
1582 state.text <- Printf.sprintf "invalid key";
1583 TEcont text
1586 let textentry text key = match [@warning "-4"] key with
1587 | Keys.Ascii c -> TEcont (addchar text c)
1588 | Keys.Code c -> TEcont (text ^ toutf8 c)
1589 | _ -> TEcont text
1592 let reqlayout angle fitmodel =
1593 if nogeomcmds state.geomcmds
1594 then state.anchor <- getanchor ();
1595 conf.angle <- angle mod 360;
1596 if conf.angle != 0
1597 then (
1598 match state.mode with
1599 | LinkNav _ -> state.mode <- View
1600 | Birdseye _ | Textentry _ | View -> ()
1602 conf.fitmodel <- fitmodel;
1603 invalidate "reqlayout"
1604 (fun () ->
1605 wcmd "reqlayout %d %d %d"
1606 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
1610 let settrim trimmargins trimfuzz =
1611 if nogeomcmds state.geomcmds
1612 then state.anchor <- getanchor ();
1613 conf.trimmargins <- trimmargins;
1614 conf.trimfuzz <- trimfuzz;
1615 let x0, y0, x1, y1 = trimfuzz in
1616 invalidate "settrim"
1617 (fun () -> wcmd "settrim %d %d %d %d %d"
1618 (btod conf.trimmargins) x0 y0 x1 y1);
1619 flushpages ();
1622 let setzoom zoom =
1623 let zoom = max 0.0001 zoom in
1624 if zoom <> conf.zoom
1625 then (
1626 state.prevzoom <- (conf.zoom, state.x);
1627 conf.zoom <- zoom;
1628 reshape state.winw state.winh;
1629 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
1633 let pivotzoom ?(vw=min state.w state.winw)
1634 ?(vh=min (state.maxy-state.y) state.winh)
1635 ?(x=vw/2) ?(y=vh/2) zoom =
1636 let w = float state.w /. zoom in
1637 let hw = w /. 2.0 in
1638 let ratio = float vh /. float vw in
1639 let hh = hw *. ratio in
1640 let x0 = float x -. hw
1641 and y0 = float y -. hh in
1642 gotoxy (state.x - truncate x0) (state.y + truncate y0);
1643 setzoom zoom;
1646 let pivotzoom ?vw ?vh ?x ?y zoom =
1647 if nogeomcmds state.geomcmds
1648 then
1649 if zoom > 1.0
1650 then pivotzoom ?vw ?vh ?x ?y zoom
1651 else setzoom zoom
1654 let setcolumns mode columns coverA coverB =
1655 state.prevcolumns <- Some (conf.columns, conf.zoom);
1656 if columns < 0
1657 then (
1658 if isbirdseye mode
1659 then impmsg "split mode doesn't work in bird's eye"
1660 else (
1661 conf.columns <- Csplit (-columns, E.a);
1662 state.x <- 0;
1663 conf.zoom <- 1.0;
1666 else (
1667 if columns < 2
1668 then (
1669 conf.columns <- Csingle E.a;
1670 state.x <- 0;
1671 setzoom 1.0;
1673 else (
1674 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
1675 conf.zoom <- 1.0;
1678 reshape state.winw state.winh;
1681 let resetmstate () =
1682 state.mstate <- Mnone;
1683 Wsi.setcursor Wsi.CURSOR_INHERIT;
1686 let enterbirdseye () =
1687 let zoom = float conf.thumbw /. float state.winw in
1688 let birdseyepageno =
1689 let cy = state.winh / 2 in
1690 let fold = function
1691 | [] -> 0
1692 | l :: rest ->
1693 let rec fold best = function
1694 | [] -> best.pageno
1695 | l :: rest ->
1696 let d = cy - (l.pagedispy + l.pagevh/2)
1697 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1698 if abs d < abs dbest
1699 then fold l rest
1700 else best.pageno
1701 in fold l rest
1703 fold state.layout
1705 state.mode <-
1706 Birdseye (
1707 { conf with zoom = conf.zoom },
1708 state.x, birdseyepageno, -1, getanchor ()
1710 resetmstate ();
1711 conf.zoom <- zoom;
1712 conf.presentation <- false;
1713 conf.interpagespace <- 10;
1714 conf.hlinks <- false;
1715 conf.fitmodel <- FitPage;
1716 state.x <- 0;
1717 conf.columns <- (
1718 match conf.beyecolumns with
1719 | Some c ->
1720 conf.zoom <- 1.0;
1721 Cmulti ((c, 0, 0), E.a)
1722 | None -> Csingle E.a
1724 if conf.verbose
1725 then
1726 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1727 (100.0*.zoom)
1728 else state.text <- E.s;
1729 reshape state.winw state.winh;
1732 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1733 state.mode <- View;
1734 conf.zoom <- c.zoom;
1735 conf.presentation <- c.presentation;
1736 conf.interpagespace <- c.interpagespace;
1737 conf.hlinks <- c.hlinks;
1738 conf.fitmodel <- c.fitmodel;
1739 conf.beyecolumns <- (
1740 match conf.columns with
1741 | Cmulti ((c, _, _), _) -> Some c
1742 | Csingle _ -> None
1743 | Csplit _ -> failwith "leaving bird's eye split mode"
1745 conf.columns <- (
1746 match c.columns with
1747 | Cmulti (c, _) -> Cmulti (c, E.a)
1748 | Csingle _ -> Csingle E.a
1749 | Csplit (c, _) -> Csplit (c, E.a)
1751 if conf.verbose
1752 then
1753 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1754 (100.0*.conf.zoom);
1755 reshape state.winw state.winh;
1756 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
1757 state.x <- leftx;
1760 let togglebirdseye () =
1761 match state.mode with
1762 | Birdseye vals -> leavebirdseye vals true
1763 | View -> enterbirdseye ()
1764 | Textentry _ | LinkNav _ -> ()
1767 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
1768 let pageno = max 0 (pageno - incr) in
1769 let rec loop = function
1770 | [] -> gotopage1 pageno 0
1771 | l :: _ when l.pageno = pageno ->
1772 if l.pagedispy >= 0 && l.pagey = 0
1773 then postRedisplay "upbirdseye"
1774 else gotopage1 pageno 0
1775 | _ :: rest -> loop rest
1777 loop state.layout;
1778 state.text <- E.s;
1779 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1782 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
1783 let pageno = min (state.pagecount - 1) (pageno + incr) in
1784 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1785 let rec loop = function
1786 | [] ->
1787 let y, h = getpageyh pageno in
1788 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
1789 gotoxy state.x (clamp dy)
1790 | l :: _ when l.pageno = pageno ->
1791 if l.pagevh != l.pageh
1792 then gotoxy state.x (clamp (l.pageh - l.pagevh + conf.interpagespace))
1793 else postRedisplay "downbirdseye"
1794 | _ :: rest -> loop rest
1796 loop state.layout;
1797 state.text <- E.s;
1800 let optentry mode _ key =
1801 let btos b = if b then "on" else "off" in
1802 match [@warning "-4"] key with
1803 | Keys.Ascii 'C' ->
1804 let ondone s =
1806 let n, a, b = multicolumns_of_string s in
1807 setcolumns mode n a b;
1808 with exn ->
1809 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exn
1811 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
1813 | Keys.Ascii 'Z' ->
1814 let ondone s =
1816 let zoom = float (int_of_string s) /. 100.0 in
1817 pivotzoom zoom
1818 with exn ->
1819 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
1821 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
1823 | Keys.Ascii 'i' ->
1824 conf.icase <- not conf.icase;
1825 TEdone ("case insensitive search " ^ (btos conf.icase))
1827 | Keys.Ascii 'v' ->
1828 conf.verbose <- not conf.verbose;
1829 TEdone ("verbose " ^ (btos conf.verbose))
1831 | Keys.Ascii 'd' ->
1832 conf.debug <- not conf.debug;
1833 TEdone ("debug " ^ (btos conf.debug))
1835 | Keys.Ascii 'f' ->
1836 conf.underinfo <- not conf.underinfo;
1837 TEdone ("underinfo " ^ btos conf.underinfo)
1839 | Keys.Ascii 'T' ->
1840 settrim (not conf.trimmargins) conf.trimfuzz;
1841 TEdone ("trim margins " ^ btos conf.trimmargins)
1843 | Keys.Ascii 'I' ->
1844 conf.invert <- not conf.invert;
1845 TEdone ("invert colors " ^ btos conf.invert)
1847 | Keys.Ascii 'x' ->
1848 let ondone s =
1849 cbput state.hists.sel s;
1850 conf.selcmd <- s;
1852 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
1853 textentry, ondone, true)
1855 | Keys.Ascii 'M' ->
1856 if conf.pax == None
1857 then conf.pax <- Some 0.0
1858 else conf.pax <- None;
1859 TEdone ("PAX " ^ btos (conf.pax != None))
1861 | (Keys.Ascii c) ->
1862 state.text <- Printf.sprintf "bad option %d `%c'" (Char.code c) c;
1863 TEstop
1865 | _ -> TEcont state.text
1868 let adderrmsg src msg =
1869 Buffer.add_string state.errmsgs msg;
1870 state.newerrmsgs <- true;
1871 postRedisplay src
1874 let adderrfmt src fmt = Format.ksprintf (fun s -> adderrmsg src s) fmt;;
1876 class outlinelistview ~zebra ~source =
1877 let settext autonarrow s =
1878 if autonarrow
1879 then
1880 let ss = source#statestr in
1881 state.text <-
1882 if emptystr ss
1883 then "[" ^ s ^ "]"
1884 else "{" ^ ss ^ "} [" ^ s ^ "]"
1885 else state.text <- s
1887 object (self)
1888 inherit listview
1889 ~zebra
1890 ~helpmode:false
1891 ~source:(source :> lvsource)
1892 ~trusted:false
1893 ~modehash:(findkeyhash conf "outline")
1894 as super
1896 val m_autonarrow = false
1898 method! key key mask =
1899 let maxrows =
1900 if emptystr state.text
1901 then fstate.maxrows
1902 else fstate.maxrows - 2
1904 let calcfirst first active =
1905 if active > first
1906 then
1907 let rows = active - first in
1908 if rows > maxrows then active - maxrows else first
1909 else active
1911 let navigate incr =
1912 let active = m_active + incr in
1913 let active = bound active 0 (source#getitemcount - 1) in
1914 let first = calcfirst m_first active in
1915 postRedisplay "outline navigate";
1916 coe {< m_active = active; m_first = first >}
1918 let navscroll first =
1919 let active =
1920 let dist = m_active - first in
1921 if dist < 0
1922 then first
1923 else (
1924 if dist < maxrows
1925 then m_active
1926 else first + maxrows
1929 postRedisplay "outline navscroll";
1930 coe {< m_first = first; m_active = active >}
1932 let ctrl = Wsi.withctrl mask in
1933 let open Keys in
1934 match Wsi.kc2kt key with
1935 | Ascii 'a' when ctrl ->
1936 let text =
1937 if m_autonarrow
1938 then (
1939 source#denarrow;
1942 else (
1943 let pattern = source#renarrow in
1944 if nonemptystr m_qsearch
1945 then (source#narrow m_qsearch; m_qsearch)
1946 else pattern
1949 settext (not m_autonarrow) text;
1950 postRedisplay "toggle auto narrowing";
1951 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
1953 | Ascii '/' when emptystr m_qsearch && not m_autonarrow ->
1954 settext true E.s;
1955 postRedisplay "toggle auto narrowing";
1956 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
1958 | Ascii 'n' when ctrl ->
1959 source#narrow m_qsearch;
1960 if not m_autonarrow
1961 then source#add_narrow_pattern m_qsearch;
1962 postRedisplay "outline ctrl-n";
1963 coe {< m_first = 0; m_active = 0 >}
1965 | Ascii 'S' when ctrl ->
1966 let active = source#calcactive (getanchor ()) in
1967 let first = firstof m_first active in
1968 postRedisplay "outline ctrl-s";
1969 coe {< m_first = first; m_active = active >}
1971 | Ascii 'u' when ctrl ->
1972 postRedisplay "outline ctrl-u";
1973 if m_autonarrow && nonemptystr m_qsearch
1974 then (
1975 ignore (source#renarrow);
1976 settext m_autonarrow E.s;
1977 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
1979 else (
1980 source#del_narrow_pattern;
1981 let pattern = source#renarrow in
1982 let text =
1983 if emptystr pattern then E.s else "Narrowed to " ^ pattern
1985 settext m_autonarrow text;
1986 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
1989 | Ascii 'l' when ctrl ->
1990 let first = max 0 (m_active - (fstate.maxrows / 2)) in
1991 postRedisplay "outline ctrl-l";
1992 coe {< m_first = first >}
1994 | Ascii '\t' when m_autonarrow ->
1995 if nonemptystr m_qsearch
1996 then (
1997 postRedisplay "outline list view tab";
1998 source#add_narrow_pattern m_qsearch;
1999 settext true E.s;
2000 coe {< m_qsearch = E.s >}
2002 else coe self
2004 | Escape when m_autonarrow ->
2005 if nonemptystr m_qsearch
2006 then source#add_narrow_pattern m_qsearch;
2007 super#key key mask
2009 | Enter when m_autonarrow ->
2010 if nonemptystr m_qsearch
2011 then source#add_narrow_pattern m_qsearch;
2012 super#key key mask
2014 | (Ascii _ | Code _) when m_autonarrow ->
2015 let pattern = m_qsearch ^ toutf8 key in
2016 postRedisplay "outlinelistview autonarrow add";
2017 source#narrow pattern;
2018 settext true pattern;
2019 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
2021 | Backspace when m_autonarrow ->
2022 if emptystr m_qsearch
2023 then coe self
2024 else
2025 let pattern = withoutlastutf8 m_qsearch in
2026 postRedisplay "outlinelistview autonarrow backspace";
2027 ignore (source#renarrow);
2028 source#narrow pattern;
2029 settext true pattern;
2030 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
2032 | Up when ctrl -> navscroll (max 0 (m_first - 1))
2034 | Down when ctrl ->
2035 navscroll (min (source#getitemcount - 1) (m_first + 1))
2037 | Up -> navigate ~-1
2038 | Down -> navigate 1
2039 | Prior -> navigate ~-(fstate.maxrows)
2040 | Next -> navigate fstate.maxrows
2042 | Right ->
2043 let o =
2044 if ctrl
2045 then (
2046 postRedisplay "outline ctrl right";
2047 {< m_pan = m_pan + 1 >}
2049 else self#updownlevel 1
2051 coe o
2053 | Left ->
2054 let o =
2055 if ctrl
2056 then (
2057 postRedisplay "outline ctrl left";
2058 {< m_pan = m_pan - 1 >}
2060 else self#updownlevel ~-1
2062 coe o
2064 | Home ->
2065 postRedisplay "outline home";
2066 coe {< m_first = 0; m_active = 0 >}
2068 | End ->
2069 let active = source#getitemcount - 1 in
2070 let first = max 0 (active - fstate.maxrows) in
2071 postRedisplay "outline end";
2072 coe {< m_active = active; m_first = first >}
2074 | Delete|Escape|Insert|Enter|Ascii _|Code _|Ctrl _|Backspace|Fn _ ->
2075 super#key key mask
2076 end;;
2078 let genhistoutlines () =
2079 Config.gethist ()
2080 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
2081 compare c2.lastvisit c1.lastvisit)
2082 |> List.map (fun ((path, c, _, _, _, origin) as hist) ->
2083 let path = if nonemptystr origin then origin else path in
2084 let base = mbtoutf8 @@ Filename.basename path in
2085 (base ^ "\000" ^ c.title, 1, Ohistory hist)
2089 let gotohist (path, c, bookmarks, x, anchor, origin) =
2090 Config.save leavebirdseye;
2091 state.anchor <- anchor;
2092 state.bookmarks <- bookmarks;
2093 state.origin <- origin;
2094 state.x <- x;
2095 setconf conf c;
2096 let x0, y0, x1, y1 = conf.trimfuzz in
2097 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
2098 reshape ~firsttime:true state.winw state.winh;
2099 opendoc path origin;
2100 setzoom c.zoom;
2103 let setcheckers enabled =
2104 match state.checkerstexid with
2105 | None -> if enabled then state.checkerstexid <- Some (makecheckers ())
2107 | Some checkerstexid ->
2108 if not enabled
2109 then (
2110 GlTex.delete_texture checkerstexid;
2111 state.checkerstexid <- None;
2115 let describe_layout layout =
2116 let d =
2117 match layout with
2118 | [] -> "Page 0"
2119 | l :: [] -> Printf.sprintf "Page %d" (l.pageno+1)
2120 | l :: rest ->
2121 let rangestr a b =
2122 if a.pageno = b.pageno then Printf.sprintf "%d" (a.pageno+1)
2123 else Printf.sprintf "%d%s%d" (a.pageno+1)
2124 (if a.pageno+1 = b.pageno then ", " else Utf8syms.ellipsis)
2125 (b.pageno+1)
2127 let rec fold s la lb = function
2128 | [] -> Printf.sprintf "%s %s" s (rangestr la lb)
2129 | l :: rest when l.pageno = succ lb.pageno -> fold s la l rest
2130 | l :: rest -> fold (s ^ " " ^ rangestr la lb ^ ",") l l rest
2132 fold "Pages" l l rest
2134 let percent =
2135 let maxy = maxy () in
2136 if maxy <= 0
2137 then 100.
2138 else 100. *. (float state.y /. float maxy)
2140 Printf.sprintf "%s of %d [%.2f%%]" d state.pagecount percent
2143 let setpresentationmode v =
2144 let n = page_of_y state.y in
2145 state.anchor <- (n, 0.0, 1.0);
2146 conf.presentation <- v;
2147 if conf.fitmodel = FitPage
2148 then reqlayout conf.angle conf.fitmodel;
2149 represent ();
2152 let enterinfomode =
2153 let btos b = if b then Utf8syms.radical else E.s in
2154 let showextended = ref false in
2155 let showcolors = ref false in
2156 let leave mode _ = state.mode <- mode in
2157 let src =
2158 (object
2159 val mutable m_l = []
2160 val mutable m_a = E.a
2161 val mutable m_prev_uioh = nouioh
2162 val mutable m_prev_mode = View
2164 inherit lvsourcebase
2166 method reset prev_mode prev_uioh =
2167 m_a <- Array.of_list (List.rev m_l);
2168 m_l <- [];
2169 m_prev_mode <- prev_mode;
2170 m_prev_uioh <- prev_uioh;
2172 method int name get set =
2173 m_l <-
2174 (name, `int get, 1,
2175 Action (
2176 fun u ->
2177 let ondone s =
2178 try set (int_of_string s)
2179 with exn ->
2180 state.text <- Printf.sprintf "bad integer `%s': %s"
2181 s @@ exntos exn
2183 state.text <- E.s;
2184 let te = name ^ ": ", E.s, None, intentry, ondone, true in
2185 state.mode <- Textentry (te, leave m_prev_mode);
2187 )) :: m_l
2189 method int_with_suffix name get set =
2190 m_l <-
2191 (name, `intws get, 1,
2192 Action (
2193 fun u ->
2194 let ondone s =
2195 try set (int_of_string_with_suffix s)
2196 with exn ->
2197 state.text <- Printf.sprintf "bad integer `%s': %s"
2198 s @@ exntos exn
2200 state.text <- E.s;
2201 let te =
2202 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
2204 state.mode <- Textentry (te, leave m_prev_mode);
2206 )) :: m_l
2208 method bool ?(offset=1) ?(btos=btos) name get set =
2209 m_l <-
2210 (name, `bool (btos, get), offset, Action (
2211 fun u ->
2212 let v = get () in
2213 set (not v);
2215 )) :: m_l
2217 method color name get set =
2218 m_l <-
2219 (name, `color get, 1,
2220 Action (
2221 fun u ->
2222 let invalid = (nan, nan, nan) in
2223 let ondone s =
2224 let c =
2225 try color_of_string s
2226 with exn ->
2227 state.text <- Printf.sprintf "bad color `%s': %s"
2228 s @@ exntos exn;
2229 invalid
2231 if c <> invalid
2232 then set c;
2234 let te = name ^ ": ", E.s, None, textentry, ondone, true in
2235 state.text <- color_to_string (get ());
2236 state.mode <- Textentry (te, leave m_prev_mode);
2238 )) :: m_l
2240 method string name get set =
2241 m_l <-
2242 (name, `string get, 1,
2243 Action (
2244 fun u ->
2245 let ondone s = set s in
2246 let te = name ^ ": ", E.s, None, textentry, ondone, true in
2247 state.mode <- Textentry (te, leave m_prev_mode);
2249 )) :: m_l
2251 method colorspace name get set =
2252 m_l <-
2253 (name, `string get, 1,
2254 Action (
2255 fun _ ->
2256 let source =
2257 (object
2258 inherit lvsourcebase
2260 initializer
2261 m_active <- CSTE.to_int conf.colorspace;
2262 m_first <- 0;
2264 method getitemcount =
2265 Array.length CSTE.names
2266 method getitem n =
2267 (CSTE.names.(n), 0)
2268 method exit ~uioh ~cancel ~active ~first ~pan =
2269 ignore (uioh, first, pan);
2270 if not cancel then set active;
2271 None
2272 method hasaction _ = true
2273 end)
2275 state.text <- E.s;
2276 let modehash = findkeyhash conf "info" in
2277 coe (new listview ~zebra:false ~helpmode:false
2278 ~source ~trusted:true ~modehash)
2279 )) :: m_l
2281 method paxmark name get set =
2282 m_l <-
2283 (name, `string get, 1,
2284 Action (
2285 fun _ ->
2286 let source =
2287 (object
2288 inherit lvsourcebase
2290 initializer
2291 m_active <- MTE.to_int conf.paxmark;
2292 m_first <- 0;
2294 method getitemcount = Array.length MTE.names
2295 method getitem n = (MTE.names.(n), 0)
2296 method exit ~uioh ~cancel ~active ~first ~pan =
2297 ignore (uioh, first, pan);
2298 if not cancel then set active;
2299 None
2300 method hasaction _ = true
2301 end)
2303 state.text <- E.s;
2304 let modehash = findkeyhash conf "info" in
2305 coe (new listview ~zebra:false ~helpmode:false
2306 ~source ~trusted:true ~modehash)
2307 )) :: m_l
2309 method fitmodel name get set =
2310 m_l <-
2311 (name, `string get, 1,
2312 Action (
2313 fun _ ->
2314 let source =
2315 (object
2316 inherit lvsourcebase
2318 initializer
2319 m_active <- FMTE.to_int conf.fitmodel;
2320 m_first <- 0;
2322 method getitemcount = Array.length FMTE.names
2323 method getitem n = (FMTE.names.(n), 0)
2324 method exit ~uioh ~cancel ~active ~first ~pan =
2325 ignore (uioh, first, pan);
2326 if not cancel then set active;
2327 None
2328 method hasaction _ = true
2329 end)
2331 state.text <- E.s;
2332 let modehash = findkeyhash conf "info" in
2333 coe (new listview ~zebra:false ~helpmode:false
2334 ~source ~trusted:true ~modehash)
2335 )) :: m_l
2337 method caption s offset =
2338 m_l <- (s, `empty, offset, Noaction) :: m_l
2340 method caption2 s f offset =
2341 m_l <- (s, `string f, offset, Noaction) :: m_l
2343 method getitemcount = Array.length m_a
2345 method getitem n =
2346 let tostr = function
2347 | `int f -> string_of_int (f ())
2348 | `intws f -> string_with_suffix_of_int (f ())
2349 | `string f -> f ()
2350 | `color f -> color_to_string (f ())
2351 | `bool (btos, f) -> btos (f ())
2352 | `empty -> E.s
2354 let name, t, offset, _ = m_a.(n) in
2355 ((let s = tostr t in
2356 if nonemptystr s
2357 then Printf.sprintf "%s\t%s" name s
2358 else name),
2359 offset)
2361 method exit ~uioh ~cancel ~active ~first ~pan =
2362 let uiohopt =
2363 if not cancel
2364 then (
2365 let uioh =
2366 match m_a.(active) with
2367 | _, _, _, Action f -> f uioh
2368 | _, _, _, Noaction -> uioh
2370 Some uioh
2372 else None
2374 m_active <- active;
2375 m_first <- first;
2376 m_pan <- pan;
2377 uiohopt
2379 method hasaction n =
2380 match m_a.(n) with
2381 | _, _, _, Action _ -> true
2382 | _, _, _, Noaction -> false
2384 initializer m_active <- 1
2385 end)
2387 let rec fillsrc prevmode prevuioh =
2388 let sep () = src#caption E.s 0 in
2389 let colorp name get set =
2390 src#string name
2391 (fun () -> color_to_string (get ()))
2392 (fun v ->
2393 try set @@ color_of_string v
2394 with exn ->
2395 state.text <-
2396 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
2399 let rgba name get set =
2400 src#string name
2401 (fun () -> get () |> rgba_to_string)
2402 (fun v ->
2403 try set @@ rgba_of_string v
2404 with exn ->
2405 state.text <-
2406 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
2409 let oldmode = state.mode in
2410 let birdseye = isbirdseye state.mode in
2412 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
2414 src#bool "presentation mode"
2415 (fun () -> conf.presentation)
2416 (fun v -> setpresentationmode v);
2418 src#bool "ignore case in searches"
2419 (fun () -> conf.icase)
2420 (fun v -> conf.icase <- v);
2422 src#bool "preload"
2423 (fun () -> conf.preload)
2424 (fun v -> conf.preload <- v);
2426 src#bool "highlight links"
2427 (fun () -> conf.hlinks)
2428 (fun v -> conf.hlinks <- v);
2430 src#bool "under info"
2431 (fun () -> conf.underinfo)
2432 (fun v -> conf.underinfo <- v);
2434 src#fitmodel "fit model"
2435 (fun () -> FMTE.to_string conf.fitmodel)
2436 (fun v -> reqlayout conf.angle (FMTE.of_int v));
2438 src#bool "trim margins"
2439 (fun () -> conf.trimmargins)
2440 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
2442 sep ();
2443 src#int "inter-page space"
2444 (fun () -> conf.interpagespace)
2445 (fun n ->
2446 conf.interpagespace <- n;
2447 docolumns conf.columns;
2448 let pageno, py =
2449 match state.layout with
2450 | [] -> 0, 0
2451 | l :: _ -> l.pageno, l.pagey
2453 state.maxy <- calcheight ();
2454 let y = getpagey pageno in
2455 gotoxy state.x (y + py)
2458 src#int "page bias"
2459 (fun () -> conf.pagebias)
2460 (fun v -> conf.pagebias <- v);
2462 src#int "scroll step"
2463 (fun () -> conf.scrollstep)
2464 (fun n -> conf.scrollstep <- n);
2466 src#int "horizontal scroll step"
2467 (fun () -> conf.hscrollstep)
2468 (fun v -> conf.hscrollstep <- v);
2470 src#int "auto scroll step"
2471 (fun () ->
2472 match state.autoscroll with
2473 | Some step -> step
2474 | _ -> conf.autoscrollstep)
2475 (fun n ->
2476 let n = boundastep state.winh n in
2477 if state.autoscroll <> None
2478 then state.autoscroll <- Some n;
2479 conf.autoscrollstep <- n);
2481 src#int "zoom"
2482 (fun () -> truncate (conf.zoom *. 100.))
2483 (fun v -> pivotzoom ((float v) /. 100.));
2485 src#int "rotation"
2486 (fun () -> conf.angle)
2487 (fun v -> reqlayout v conf.fitmodel);
2489 src#int "scroll bar width"
2490 (fun () -> conf.scrollbw)
2491 (fun v ->
2492 conf.scrollbw <- v;
2493 reshape state.winw state.winh;
2496 src#int "scroll handle height"
2497 (fun () -> conf.scrollh)
2498 (fun v -> conf.scrollh <- v;);
2500 src#int "thumbnail width"
2501 (fun () -> conf.thumbw)
2502 (fun v ->
2503 conf.thumbw <- min 4096 v;
2504 match oldmode with
2505 | Birdseye beye ->
2506 leavebirdseye beye false;
2507 enterbirdseye ()
2508 | Textentry _
2509 | View
2510 | LinkNav _ -> ()
2513 let mode = state.mode in
2514 src#string "columns"
2515 (fun () ->
2516 match conf.columns with
2517 | Csingle _ -> "1"
2518 | Cmulti (multi, _) -> multicolumns_to_string multi
2519 | Csplit (count, _) -> "-" ^ string_of_int count
2521 (fun v ->
2522 let n, a, b = multicolumns_of_string v in
2523 setcolumns mode n a b);
2525 sep ();
2526 src#caption "Pixmap cache" 0;
2527 src#int_with_suffix "size (advisory)"
2528 (fun () -> conf.memlimit)
2529 (fun v -> conf.memlimit <- v);
2531 src#caption2 "used"
2532 (fun () ->
2533 Printf.sprintf "%s bytes, %d tiles"
2534 (string_with_suffix_of_int state.memused)
2535 (Hashtbl.length state.tilemap)) 1;
2537 sep ();
2538 src#caption "Layout" 0;
2539 src#caption2 "Dimension"
2540 (fun () -> Printf.sprintf "%dx%d (virtual %dx%d)"
2541 state.winw state.winh
2542 state.w state.maxy)
2544 if conf.debug
2545 then src#caption2 "Position" (fun () ->
2546 Printf.sprintf "%dx%d" state.x state.y
2548 else src#caption2 "Position" (fun () -> describe_layout state.layout) 1;
2550 sep ();
2551 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
2552 "Save these parameters as global defaults at exit"
2553 (fun () -> conf.bedefault)
2554 (fun v -> conf.bedefault <- v);
2556 sep ();
2557 let btos b = Utf8syms.(if b then lguillemet else rguillemet) in
2558 src#bool ~offset:0 ~btos "Extended parameters"
2559 (fun () -> !showextended)
2560 (fun v -> showextended := v; fillsrc prevmode prevuioh);
2561 if !showextended
2562 then (
2563 src#bool "checkers"
2564 (fun () -> conf.checkers)
2565 (fun v -> conf.checkers <- v; setcheckers v);
2566 src#bool "update cursor"
2567 (fun () -> conf.updatecurs)
2568 (fun v -> conf.updatecurs <- v);
2569 src#bool "scroll-bar on the left"
2570 (fun () -> conf.leftscroll)
2571 (fun v -> conf.leftscroll <- v);
2572 src#bool "verbose"
2573 (fun () -> conf.verbose)
2574 (fun v -> conf.verbose <- v);
2575 src#bool "invert colors"
2576 (fun () -> conf.invert)
2577 (fun v -> conf.invert <- v);
2578 src#bool "max fit"
2579 (fun () -> conf.maxhfit)
2580 (fun v -> conf.maxhfit <- v);
2581 src#bool "pax mode"
2582 (fun () -> conf.pax != None)
2583 (fun v ->
2584 if v
2585 then conf.pax <- Some (now ())
2586 else conf.pax <- None);
2587 src#string "uri launcher"
2588 (fun () -> conf.urilauncher)
2589 (fun v -> conf.urilauncher <- v);
2590 src#string "path launcher"
2591 (fun () -> conf.pathlauncher)
2592 (fun v -> conf.pathlauncher <- v);
2593 src#string "tile size"
2594 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
2595 (fun v ->
2597 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
2598 conf.tilew <- max 64 w;
2599 conf.tileh <- max 64 h;
2600 flushtiles ();
2601 with exn ->
2602 state.text <- Printf.sprintf "bad tile size `%s': %s"
2603 v @@ exntos exn
2605 src#int "texture count"
2606 (fun () -> conf.texcount)
2607 (fun v ->
2608 if realloctexts v
2609 then conf.texcount <- v
2610 else impmsg "failed to set texture count please retry later"
2612 src#int "slice height"
2613 (fun () -> conf.sliceheight)
2614 (fun v ->
2615 conf.sliceheight <- v;
2616 wcmd "sliceh %d" conf.sliceheight;
2618 src#int "anti-aliasing level"
2619 (fun () -> conf.aalevel)
2620 (fun v ->
2621 conf.aalevel <- bound v 0 8;
2622 state.anchor <- getanchor ();
2623 opendoc state.path state.password;
2625 src#string "page scroll scaling factor"
2626 (fun () -> string_of_float conf.pgscale)
2627 (fun v ->
2628 try conf.pgscale <- float_of_string v
2629 with exn ->
2630 state.text <-
2631 Printf.sprintf "bad page scroll scaling factor `%s': %s" v
2632 @@ exntos exn
2634 src#int "ui font size"
2635 (fun () -> fstate.fontsize)
2636 (fun v -> setfontsize (bound v 5 100));
2637 src#int "hint font size"
2638 (fun () -> conf.hfsize)
2639 (fun v -> conf.hfsize <- bound v 5 100);
2640 src#string "trim fuzz"
2641 (fun () -> irect_to_string conf.trimfuzz)
2642 (fun v ->
2644 conf.trimfuzz <- irect_of_string v;
2645 if conf.trimmargins
2646 then settrim true conf.trimfuzz;
2647 with exn ->
2648 state.text <- Printf.sprintf "bad irect `%s': %s" v
2649 @@ exntos exn
2651 src#string "selection command"
2652 (fun () -> conf.selcmd)
2653 (fun v -> conf.selcmd <- v);
2654 src#string "synctex command"
2655 (fun () -> conf.stcmd)
2656 (fun v -> conf.stcmd <- v);
2657 src#string "pax command"
2658 (fun () -> conf.paxcmd)
2659 (fun v -> conf.paxcmd <- v);
2660 src#string "ask password command"
2661 (fun () -> conf.passcmd)
2662 (fun v -> conf.passcmd <- v);
2663 src#string "save path command"
2664 (fun () -> conf.savecmd)
2665 (fun v -> conf.savecmd <- v);
2666 src#colorspace "color space"
2667 (fun () -> CSTE.to_string conf.colorspace)
2668 (fun v ->
2669 conf.colorspace <- CSTE.of_int v;
2670 wcmd "cs %d" v;
2671 load state.layout;
2673 src#paxmark "pax mark method"
2674 (fun () -> MTE.to_string conf.paxmark)
2675 (fun v -> conf.paxmark <- MTE.of_int v);
2676 if bousable () && !opengl_has_pbo
2677 then
2678 src#bool "use PBO"
2679 (fun () -> conf.usepbo)
2680 (fun v -> conf.usepbo <- v);
2681 src#bool "mouse wheel scrolls pages"
2682 (fun () -> conf.wheelbypage)
2683 (fun v -> conf.wheelbypage <- v);
2684 src#bool "open remote links in a new instance"
2685 (fun () -> conf.riani)
2686 (fun v -> conf.riani <- v);
2687 src#bool "edit annotations inline"
2688 (fun () -> conf.annotinline)
2689 (fun v -> conf.annotinline <- v);
2690 src#bool "coarse positioning in presentation mode"
2691 (fun () -> conf.coarseprespos)
2692 (fun v -> conf.coarseprespos <- v);
2693 src#bool "use document CSS"
2694 (fun () -> conf.usedoccss)
2695 (fun v ->
2696 conf.usedoccss <- v;
2697 state.anchor <- getanchor ();
2698 opendoc state.path state.password;
2700 src#bool ~btos "colors"
2701 (fun () -> !showcolors)
2702 (fun v -> showcolors := v; fillsrc prevmode prevuioh);
2703 if !showcolors
2704 then (
2705 colorp " background"
2706 (fun () -> conf.bgcolor)
2707 (fun v -> conf.bgcolor <- v);
2708 rgba " scrollbar"
2709 (fun () -> conf.sbarcolor)
2710 (fun v -> conf.sbarcolor <- v);
2711 rgba " scrollbar handle"
2712 (fun () -> conf.sbarhndlcolor)
2713 (fun v -> conf.sbarhndlcolor <- v);
2717 sep ();
2718 src#caption "Document" 0;
2719 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
2720 src#caption2 "Pages"
2721 (fun () -> string_of_int state.pagecount) 1;
2722 src#caption2 "Dimensions"
2723 (fun () -> string_of_int (List.length state.pdims)) 1;
2724 if nonemptystr conf.css
2725 then src#caption2 "CSS" (fun () -> conf.css) 1;
2726 if conf.trimmargins
2727 then (
2728 sep ();
2729 src#caption "Trimmed margins" 0;
2730 src#caption2 "Dimensions"
2731 (fun () -> string_of_int (List.length state.pdims)) 1;
2734 sep ();
2735 src#caption "OpenGL" 0;
2736 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
2737 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
2739 sep ();
2740 src#caption "Location" 0;
2741 if nonemptystr state.origin
2742 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
2743 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
2745 src#reset prevmode prevuioh;
2747 fun () ->
2748 state.text <- E.s;
2749 resetmstate ();
2750 let prevmode = state.mode
2751 and prevuioh = state.uioh in
2752 fillsrc prevmode prevuioh;
2753 let source = (src :> lvsource) in
2754 let modehash = findkeyhash conf "info" in
2755 state.uioh <-
2756 coe (object (self)
2757 inherit listview ~zebra:false ~helpmode:false
2758 ~source ~trusted:true ~modehash as super
2759 val mutable m_prevmemused = 0
2760 method! infochanged = function
2761 | Memused ->
2762 if m_prevmemused != state.memused
2763 then (
2764 m_prevmemused <- state.memused;
2765 postRedisplay "memusedchanged";
2767 | Pdim -> postRedisplay "pdimchanged"
2768 | Docinfo -> fillsrc prevmode prevuioh
2770 method! key key mask =
2771 if not (Wsi.withctrl mask)
2772 then
2773 match [@warning "-4"] Wsi.kc2kt key with
2774 | Keys.Left -> coe (self#updownlevel ~-1)
2775 | Keys.Right -> coe (self#updownlevel 1)
2776 | _ -> super#key key mask
2777 else super#key key mask
2778 end);
2779 postRedisplay "info";
2782 let enterhelpmode =
2783 let source =
2784 (object
2785 inherit lvsourcebase
2786 method getitemcount = Array.length state.help
2787 method getitem n =
2788 let s, l, _ = state.help.(n) in
2789 (s, l)
2791 method exit ~uioh ~cancel ~active ~first ~pan =
2792 let optuioh =
2793 if not cancel
2794 then (
2795 match state.help.(active) with
2796 | _, _, Action f -> Some (f uioh)
2797 | _, _, Noaction -> Some uioh
2799 else None
2801 m_active <- active;
2802 m_first <- first;
2803 m_pan <- pan;
2804 optuioh
2806 method hasaction n =
2807 match state.help.(n) with
2808 | _, _, Action _ -> true
2809 | _, _, Noaction -> false
2811 initializer
2812 m_active <- -1
2813 end)
2814 in fun () ->
2815 let modehash = findkeyhash conf "help" in
2816 resetmstate ();
2817 state.uioh <- coe (new listview
2818 ~zebra:false ~helpmode:true
2819 ~source ~trusted:true ~modehash);
2820 postRedisplay "help";
2823 let entermsgsmode =
2824 let msgsource =
2825 (object
2826 inherit lvsourcebase
2827 val mutable m_items = E.a
2829 method getitemcount = 1 + Array.length m_items
2831 method getitem n =
2832 if n = 0
2833 then "[Clear]", 0
2834 else m_items.(n-1), 0
2836 method exit ~uioh ~cancel ~active ~first ~pan =
2837 ignore uioh;
2838 if not cancel
2839 then (
2840 if active = 0
2841 then Buffer.clear state.errmsgs;
2843 m_active <- active;
2844 m_first <- first;
2845 m_pan <- pan;
2846 None
2848 method hasaction n =
2849 n = 0
2851 method reset =
2852 state.newerrmsgs <- false;
2853 let l = Str.split Utils.Re.crlf (Buffer.contents state.errmsgs) in
2854 m_items <- Array.of_list l
2856 initializer
2857 m_active <- 0
2858 end)
2859 in fun () ->
2860 state.text <- E.s;
2861 resetmstate ();
2862 msgsource#reset;
2863 let source = (msgsource :> lvsource) in
2864 let modehash = findkeyhash conf "listview" in
2865 state.uioh <-
2866 coe (object
2867 inherit listview ~zebra:false ~helpmode:false
2868 ~source ~trusted:false ~modehash as super
2869 method! display =
2870 if state.newerrmsgs
2871 then msgsource#reset;
2872 super#display
2873 end);
2874 postRedisplay "msgs";
2877 let getusertext s =
2878 let editor = getenvwithdef "EDITOR" E.s in
2879 if emptystr editor
2880 then E.s
2881 else
2882 let tmppath = Filename.temp_file "llpp" "note" in
2883 if nonemptystr s
2884 then (
2885 let oc = open_out tmppath in
2886 output_string oc s;
2887 close_out oc;
2889 let execstr = editor ^ " " ^ tmppath in
2890 let s =
2891 match spawn execstr [] with
2892 | exception exn ->
2893 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
2895 | pid ->
2896 match Unix.waitpid [] pid with
2897 | exception exn ->
2898 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
2900 | (_pid, status) ->
2901 match status with
2902 | Unix.WEXITED 0 -> filecontents tmppath
2903 | Unix.WEXITED n ->
2904 impmsg "editor process(%s) exited abnormally: %d" execstr n;
2906 | Unix.WSIGNALED n ->
2907 impmsg "editor process(%s) was killed by signal %d" execstr n;
2909 | Unix.WSTOPPED n ->
2910 impmsg "editor(%s) process was stopped by signal %d" execstr n;
2913 match Unix.unlink tmppath with
2914 | exception exn ->
2915 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
2917 | () -> s
2920 let enterannotmode opaque slinkindex =
2921 let msgsource =
2922 (object
2923 inherit lvsourcebase
2924 val mutable m_text = E.s
2925 val mutable m_items = E.a
2927 method getitemcount = Array.length m_items
2929 method getitem n =
2930 let label, _func = m_items.(n) in
2931 label, 0
2933 method exit ~uioh ~cancel ~active ~first ~pan =
2934 ignore (uioh, first, pan);
2935 if not cancel
2936 then (
2937 let _label, func = m_items.(active) in
2938 func ()
2940 None
2942 method hasaction n = nonemptystr @@ fst m_items.(n)
2944 method reset s =
2945 let rec split accu b i =
2946 let p = b+i in
2947 if p = String.length s
2948 then (String.sub s b (p-b), unit) :: accu
2949 else
2950 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
2951 then
2952 let ss = if i = 0 then E.s else String.sub s b i in
2953 split ((ss, unit)::accu) (p+1) 0
2954 else split accu b (i+1)
2956 let cleanup () =
2957 wcmd "freepage %s" (~> opaque);
2958 let keys =
2959 Hashtbl.fold (fun key opaque' accu ->
2960 if opaque' = opaque'
2961 then key :: accu else accu) state.pagemap []
2963 List.iter (Hashtbl.remove state.pagemap) keys;
2964 flushtiles ();
2965 gotoxy state.x state.y
2967 let dele () =
2968 delannot opaque slinkindex;
2969 cleanup ();
2971 let edit inline () =
2972 let update s =
2973 if emptystr s
2974 then dele ()
2975 else (
2976 modannot opaque slinkindex s;
2977 cleanup ();
2980 if inline
2981 then
2982 let mode = state.mode in
2983 state.mode <-
2984 Textentry (
2985 ("annotation: ", m_text, None, textentry, update, true),
2986 fun _ -> state.mode <- mode
2988 state.text <- E.s;
2989 enttext ();
2990 else
2991 let s = getusertext m_text in
2992 update s
2994 m_text <- s;
2995 m_items <-
2996 ( "[Copy]", fun () -> selstring conf.selcmd m_text)
2997 :: ("[Delete]", dele)
2998 :: ("[Edit]", edit conf.annotinline)
2999 :: (E.s, unit)
3000 :: split [] 0 0 |> List.rev |> Array.of_list
3002 initializer
3003 m_active <- 0
3004 end)
3006 state.text <- E.s;
3007 let s = getannotcontents opaque slinkindex in
3008 resetmstate ();
3009 msgsource#reset s;
3010 let source = (msgsource :> lvsource) in
3011 let modehash = findkeyhash conf "listview" in
3012 state.uioh <- coe (object
3013 inherit listview ~zebra:false ~helpmode:false
3014 ~source ~trusted:false ~modehash
3015 end);
3016 postRedisplay "enterannotmode";
3019 let gotoremote spec =
3020 let filename, dest = splitatchar spec '#' in
3021 let getpath filename =
3022 let path =
3023 if nonemptystr filename
3024 then
3025 if Filename.is_relative filename
3026 then
3027 let dir = Filename.dirname state.path in
3028 let dir =
3029 if Filename.is_implicit dir
3030 then Filename.concat (Sys.getcwd ()) dir
3031 else dir
3033 Filename.concat dir filename
3034 else filename
3035 else E.s
3037 if Sys.file_exists path
3038 then path
3039 else E.s
3041 let path = getpath filename in
3042 let dospawn lcmd =
3043 if conf.riani
3044 then
3045 let cmd = Lazy.force_val lcmd in
3046 match spawn cmd with
3047 | _pid -> ()
3048 | exception exn -> dolog "failed to execute `%s': %s" cmd @@ exntos exn
3049 else
3050 let anchor = getanchor () in
3051 let ranchor = state.path, state.password, anchor, state.origin in
3052 state.origin <- E.s;
3053 state.ranchors <- ranchor :: state.ranchors;
3054 opendoc path E.s;
3056 if substratis spec 0 "page="
3057 then
3058 match Scanf.sscanf spec "page=%d" (fun n -> n) with
3059 | pageno ->
3060 state.anchor <- (pageno, 0.0, 0.0);
3061 dospawn @@ lazy (Printf.sprintf "%s -page %d %S" !selfexec pageno path);
3062 | exception exn ->
3063 adderrfmt "error parsing remote destination" "page: %s" @@ exntos exn
3064 else (
3065 state.nameddest <- dest;
3066 dospawn @@ lazy (!selfexec ^ " " ^ path ^ " -dest " ^ dest)
3070 let gotounder = function
3071 | Ulinkuri s when isexternallink s ->
3072 if substratis s 0 "file://"
3073 then gotoremote @@ String.sub s 7 (String.length s - 7)
3074 else Help.gotouri conf.urilauncher s
3075 | Ulinkuri s ->
3076 let pageno, x, y = uritolocation s in
3077 addnav ();
3078 gotopagexy pageno x y
3079 | Utext _ | Unone -> ()
3080 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
3083 let gotooutline (_, _, kind) =
3084 match kind with
3085 | Onone -> ()
3086 | Oanchor anchor ->
3087 let (pageno, y, _) = anchor in
3088 let y = getanchory
3089 (if conf.presentation then (pageno, y, 1.0) else anchor)
3091 addnav ();
3092 gotoxy state.x y
3093 | Ouri uri -> gotounder (Ulinkuri uri)
3094 | Olaunch _cmd -> failwith "gotounder (Ulaunch cmd)"
3095 | Oremote _remote -> failwith "gotounder (Uremote remote)"
3096 | Ohistory hist -> gotohist hist
3097 | Oremotedest _remotedest -> failwith "gotounder (Uremotedest remotedest)"
3100 class outlinesoucebase fetchoutlines = object (self)
3101 inherit lvsourcebase
3102 val mutable m_items = E.a
3103 val mutable m_minfo = E.a
3104 val mutable m_orig_items = E.a
3105 val mutable m_orig_minfo = E.a
3106 val mutable m_narrow_patterns = []
3107 val mutable m_gen = -1
3109 method getitemcount = Array.length m_items
3111 method getitem n =
3112 let s, n, _ = m_items.(n) in
3113 (s, n+0)
3115 method exit ~(uioh:uioh) ~cancel ~active ~(first:int) ~pan : uioh option =
3116 ignore (uioh, first);
3117 let items, minfo =
3118 if m_narrow_patterns = []
3119 then m_orig_items, m_orig_minfo
3120 else m_items, m_minfo
3122 m_pan <- pan;
3123 if not cancel
3124 then (
3125 m_items <- items;
3126 m_minfo <- minfo;
3127 gotooutline m_items.(active);
3129 else (
3130 m_items <- items;
3131 m_minfo <- minfo;
3133 None
3135 method hasaction (_:int) = true
3137 method greetmsg =
3138 if Array.length m_items != Array.length m_orig_items
3139 then
3140 let s =
3141 match m_narrow_patterns with
3142 | one :: [] -> one
3143 | many -> String.concat Utf8syms.ellipsis (List.rev many)
3145 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
3146 else E.s
3148 method statestr =
3149 match m_narrow_patterns with
3150 | [] -> E.s
3151 | one :: [] -> one
3152 | head :: _ -> Utf8syms.ellipsis ^ head
3154 method narrow pattern =
3155 match Str.regexp_case_fold pattern with
3156 | exception _ -> ()
3157 | re ->
3158 let rec loop accu minfo n =
3159 if n = -1
3160 then (
3161 m_items <- Array.of_list accu;
3162 m_minfo <- Array.of_list minfo;
3164 else
3165 let (s, _, _) as o = m_items.(n) in
3166 let accu, minfo =
3167 match Str.search_forward re s 0 with
3168 | exception Not_found -> accu, minfo
3169 | first -> o :: accu, (first, Str.match_end ()) :: minfo
3171 loop accu minfo (n-1)
3173 loop [] [] (Array.length m_items - 1)
3175 method! getminfo = m_minfo
3177 method denarrow =
3178 m_orig_items <- fetchoutlines ();
3179 m_minfo <- m_orig_minfo;
3180 m_items <- m_orig_items
3182 method add_narrow_pattern pattern =
3183 m_narrow_patterns <- pattern :: m_narrow_patterns
3185 method del_narrow_pattern =
3186 match m_narrow_patterns with
3187 | _ :: rest -> m_narrow_patterns <- rest
3188 | [] -> ()
3190 method renarrow =
3191 self#denarrow;
3192 match m_narrow_patterns with
3193 | pattern :: [] -> self#narrow pattern; pattern
3194 | list ->
3195 List.fold_left (fun accu pattern ->
3196 self#narrow pattern;
3197 pattern ^ Utf8syms.ellipsis ^ accu) E.s list
3199 method calcactive (_:anchor) = 0
3201 method reset anchor items =
3202 if state.gen != m_gen
3203 then (
3204 m_orig_items <- items;
3205 m_items <- items;
3206 m_narrow_patterns <- [];
3207 m_minfo <- E.a;
3208 m_orig_minfo <- E.a;
3209 m_gen <- state.gen;
3211 else (
3212 if items != m_orig_items
3213 then (
3214 m_orig_items <- items;
3215 if m_narrow_patterns == []
3216 then m_items <- items;
3219 let active = self#calcactive anchor in
3220 m_active <- active;
3221 m_first <- firstof m_first active
3225 let outlinesource fetchoutlines =
3226 (object
3227 inherit outlinesoucebase fetchoutlines
3228 method! calcactive anchor =
3229 let rely = getanchory anchor in
3230 let rec loop n best bestd =
3231 if n = Array.length m_items
3232 then best
3233 else
3234 let _, _, kind = m_items.(n) in
3235 match kind with
3236 | Oanchor anchor ->
3237 let orely = getanchory anchor in
3238 let d = abs (orely - rely) in
3239 if d < bestd
3240 then loop (n+1) n d
3241 else loop (n+1) best bestd
3242 | Onone | Oremote _ | Olaunch _
3243 | Oremotedest _ | Ouri _ | Ohistory _ ->
3244 loop (n+1) best bestd
3246 loop 0 ~-1 max_int
3247 end)
3250 let enteroutlinemode, enterbookmarkmode, enterhistmode =
3251 let mkselector sourcetype =
3252 let fetchoutlines () =
3253 match sourcetype with
3254 | `bookmarks -> Array.of_list state.bookmarks
3255 | `outlines -> state.outlines
3256 | `history -> genhistoutlines () |> Array.of_list
3258 let source =
3259 if sourcetype = `history
3260 then new outlinesoucebase fetchoutlines
3261 else outlinesource fetchoutlines
3263 (fun errmsg ->
3264 let outlines = fetchoutlines () in
3265 if Array.length outlines = 0
3266 then showtext ' ' errmsg
3267 else (
3268 resetmstate ();
3269 Wsi.setcursor Wsi.CURSOR_INHERIT;
3270 let anchor = getanchor () in
3271 source#reset anchor outlines;
3272 state.text <- source#greetmsg;
3273 state.uioh <-
3274 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
3275 postRedisplay "enter selector";
3279 let mkenter sourcetype errmsg = fun () -> mkselector sourcetype errmsg in
3280 ( mkenter `outlines "document has no outline"
3281 , mkenter `bookmarks "document has no bookmarks (yet)"
3282 , mkenter `history "history is empty" )
3285 let quickbookmark ?title () =
3286 match state.layout with
3287 | [] -> ()
3288 | l :: _ ->
3289 let title =
3290 match title with
3291 | None ->
3292 Unix.(
3293 let tm = localtime (now ()) in
3294 Printf.sprintf
3295 "Quick (page %d) (bookmarked on %02d/%02d/%d at %02d:%02d)"
3296 (l.pageno+1)
3297 tm.tm_mday (tm.tm_mon+1) (tm.tm_year+1900) tm.tm_hour tm.tm_min
3299 | Some title -> title
3301 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
3304 let setautoscrollspeed step goingdown =
3305 let incr = max 1 ((abs step) / 2) in
3306 let incr = if goingdown then incr else -incr in
3307 let astep = boundastep state.winh (step + incr) in
3308 state.autoscroll <- Some astep;
3311 let canpan () =
3312 match conf.columns with
3313 | Csplit _ -> true
3314 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
3317 let panbound x = bound x (-state.w) state.winw;;
3319 let existsinrow pageno (columns, coverA, coverB) p =
3320 let last = ((pageno - coverA) mod columns) + columns in
3321 let rec any = function
3322 | [] -> false
3323 | l :: rest ->
3324 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
3325 then p l
3326 else (
3327 if not (p l)
3328 then (if l.pageno = last then false else any rest)
3329 else true
3332 any state.layout
3335 let nextpage () =
3336 match state.layout with
3337 | [] ->
3338 let pageno = page_of_y state.y in
3339 gotoxy state.x (getpagey (pageno+1))
3340 | l :: rest ->
3341 match conf.columns with
3342 | Csingle _ ->
3343 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
3344 then
3345 let y = clamp (pgscale state.winh) in
3346 gotoxy state.x y
3347 else
3348 let pageno = min (l.pageno+1) (state.pagecount-1) in
3349 gotoxy state.x (getpagey pageno)
3350 | Cmulti ((c, _, _) as cl, _) ->
3351 if conf.presentation
3352 && (existsinrow l.pageno cl
3353 (fun l -> l.pageh > l.pagey + l.pagevh))
3354 then
3355 let y = clamp (pgscale state.winh) in
3356 gotoxy state.x y
3357 else
3358 let pageno = min (l.pageno+c) (state.pagecount-1) in
3359 gotoxy state.x (getpagey pageno)
3360 | Csplit (n, _) ->
3361 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
3362 then
3363 let pagey, pageh = getpageyh l.pageno in
3364 let pagey = pagey + pageh * l.pagecol in
3365 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
3366 gotoxy state.x (pagey + pageh + ips)
3369 let prevpage () =
3370 match state.layout with
3371 | [] ->
3372 let pageno = page_of_y state.y in
3373 gotoxy state.x (getpagey (pageno-1))
3374 | l :: _ ->
3375 match conf.columns with
3376 | Csingle _ ->
3377 if conf.presentation && l.pagey != 0
3378 then
3379 gotoxy state.x (clamp (pgscale ~-(state.winh)))
3380 else
3381 let pageno = max 0 (l.pageno-1) in
3382 gotoxy state.x (getpagey pageno)
3383 | Cmulti ((c, _, coverB) as cl, _) ->
3384 if conf.presentation &&
3385 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
3386 then
3387 gotoxy state.x (clamp (pgscale ~-(state.winh)))
3388 else
3389 let decr =
3390 if l.pageno = state.pagecount - coverB
3391 then 1
3392 else c
3394 let pageno = max 0 (l.pageno-decr) in
3395 gotoxy state.x (getpagey pageno)
3396 | Csplit (n, _) ->
3397 let y =
3398 if l.pagecol = 0
3399 then
3400 if l.pageno = 0
3401 then l.pagey
3402 else
3403 let pageno = max 0 (l.pageno-1) in
3404 let pagey, pageh = getpageyh pageno in
3405 pagey + (n-1)*pageh
3406 else
3407 let pagey, pageh = getpageyh l.pageno in
3408 pagey + pageh * (l.pagecol-1) - conf.interpagespace
3410 gotoxy state.x y
3413 let save () =
3414 if emptystr conf.savecmd
3415 then adderrmsg "savepath-command is empty"
3416 "don't know where to save modified document"
3417 else
3418 let savecmd = Str.global_replace Utils.Re.percent state.path conf.savecmd in
3419 let path =
3420 getcmdoutput
3421 (fun exn ->
3422 adderrfmt savecmd "failed to produce path to the saved copy: %s" exn)
3423 savecmd
3425 if nonemptystr path
3426 then
3427 let tmp = path ^ ".tmp" in
3428 savedoc tmp;
3429 Unix.rename tmp path;
3432 let viewkeyboard key mask =
3433 let enttext te =
3434 let mode = state.mode in
3435 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
3436 state.text <- E.s;
3437 enttext ();
3438 postRedisplay "view:enttext"
3440 let ctrl = Wsi.withctrl mask in
3441 let open Keys in
3442 match Wsi.kc2kt key with
3443 | Ascii 'S' -> state.slideshow <- state.slideshow lxor 1
3445 | Ascii 'Q' -> exit 0
3447 | Ascii 'W' ->
3448 if hasunsavedchanges ()
3449 then save ()
3451 | Insert ->
3452 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
3453 then (
3454 state.mode <- (
3455 match state.lnava with
3456 | None -> LinkNav (Ltgendir 0)
3457 | Some pn -> LinkNav (Ltexact pn)
3459 gotoxy state.x state.y;
3461 else impmsg "keyboard link navigation does not work under rotation"
3463 | Escape | Ascii 'q' ->
3464 begin match state.mstate with
3465 | Mzoomrect _ ->
3466 resetmstate ();
3467 postRedisplay "kill rect";
3468 | Msel _
3469 | Mpan _
3470 | Mscrolly | Mscrollx
3471 | Mzoom _
3472 | Mnone ->
3473 begin match state.mode with
3474 | LinkNav ln ->
3475 begin match ln with
3476 | Ltexact pl -> state.lnava <- Some pl
3477 | Ltgendir _ | Ltnotready _ -> state.lnava <- None
3478 end;
3479 state.mode <- View;
3480 postRedisplay "esc leave linknav"
3481 | Birdseye _ | Textentry _ | View ->
3482 match state.ranchors with
3483 | [] -> raise Quit
3484 | (path, password, anchor, origin) :: rest ->
3485 state.ranchors <- rest;
3486 state.anchor <- anchor;
3487 state.origin <- origin;
3488 state.nameddest <- E.s;
3489 opendoc path password
3490 end;
3491 end;
3493 | Backspace ->
3494 addnavnorc ();
3495 gotoxy state.x (getnav ~-1)
3497 | Ascii 'o' ->
3498 enteroutlinemode ()
3500 | Ascii 'H' ->
3501 enterhistmode ()
3503 | Ascii 'u' ->
3504 state.rects <- [];
3505 state.text <- E.s;
3506 Hashtbl.iter (fun _ opaque ->
3507 clearmark opaque;
3508 Hashtbl.clear state.prects) state.pagemap;
3509 postRedisplay "dehighlight";
3511 | Ascii (('/' | '?') as c) ->
3512 let ondone isforw s =
3513 cbput state.hists.pat s;
3514 state.searchpattern <- s;
3515 search s isforw
3517 let s = String.make 1 c in
3518 enttext (s, E.s, Some (onhist state.hists.pat),
3519 textentry, ondone (c = '/'), true)
3521 | Ascii '+' | Ascii '=' when ctrl ->
3522 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
3523 pivotzoom (conf.zoom +. incr)
3525 | Ascii '+' ->
3526 let ondone s =
3527 let n =
3528 try int_of_string s with exn ->
3529 state.text <-
3530 Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
3531 max_int
3533 if n != max_int
3534 then (
3535 conf.pagebias <- n;
3536 state.text <- "page bias is now " ^ string_of_int n;
3539 enttext ("page bias: ", E.s, None, intentry, ondone, true)
3541 | Ascii '-' when ctrl ->
3542 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
3543 pivotzoom (max 0.01 (conf.zoom -. decr))
3545 | Ascii '-' ->
3546 let ondone msg = state.text <- msg in
3547 enttext ("option: ", E.s, None,
3548 optentry state.mode, ondone, true)
3550 | Ascii '0' when ctrl ->
3551 if conf.zoom = 1.0
3552 then gotoxy 0 state.y
3553 else setzoom 1.0
3555 | Ascii ('1'|'2' as c) when ctrl && conf.fitmodel != FitPage ->
3556 let cols =
3557 match conf.columns with
3558 | Csingle _ | Cmulti _ -> 1
3559 | Csplit (n, _) -> n
3561 let h = state.winh -
3562 conf.interpagespace lsl (if conf.presentation then 1 else 0)
3564 let zoom = zoomforh state.winw h 0 cols in
3565 if zoom > 0.0 && (c = '2' || zoom < 1.0)
3566 then setzoom zoom
3568 | Ascii '3' when ctrl ->
3569 let fm =
3570 match conf.fitmodel with
3571 | FitWidth -> FitProportional
3572 | FitProportional -> FitPage
3573 | FitPage -> FitWidth
3575 state.text <- "fit model: " ^ FMTE.to_string fm;
3576 reqlayout conf.angle fm
3578 | Ascii '4' when ctrl ->
3579 let zoom = getmaxw () /. float state.winw in
3580 if zoom > 0.0 then setzoom zoom
3582 | Fn 9 | Ascii '9' when ctrl -> togglebirdseye ()
3584 | Ascii ('0'..'9' as c) when not ctrl ->
3585 let ondone s =
3586 let n =
3587 try int_of_string s with exn ->
3588 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
3591 if n >= 0
3592 then (
3593 addnav ();
3594 cbput state.hists.pag (string_of_int n);
3595 gotopage1 (n + conf.pagebias - 1) 0;
3598 let pageentry text = function [@warning "-4"]
3599 | Keys.Ascii 'g' -> TEdone text
3600 | key -> intentry text key
3602 let text = String.make 1 c in
3603 enttext (":", text, Some (onhist state.hists.pag),
3604 pageentry, ondone, true)
3606 | Ascii 'b' ->
3607 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
3608 postRedisplay "toggle scrollbar";
3610 | Ascii 'B' ->
3611 state.bzoom <- not state.bzoom;
3612 state.rects <- [];
3613 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
3615 | Ascii 'l' ->
3616 conf.hlinks <- not conf.hlinks;
3617 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
3618 postRedisplay "toggle highlightlinks";
3620 | Ascii 'F' ->
3621 if conf.angle mod 360 = 0
3622 then (
3623 state.glinks <- true;
3624 let mode = state.mode in
3625 state.mode <-
3626 Textentry (
3627 (":", E.s, None, linknentry, linknact gotounder, false),
3628 (fun _ ->
3629 state.glinks <- false;
3630 state.mode <- mode)
3632 state.text <- E.s;
3633 postRedisplay "view:linkent(F)"
3635 else impmsg "hint mode does not work under rotation"
3637 | Ascii 'y' ->
3638 state.glinks <- true;
3639 let mode = state.mode in
3640 state.mode <-
3641 Textentry (
3642 (":", E.s, None, linknentry,
3643 linknact (fun under ->
3644 selstring conf.selcmd (undertext under)), false),
3645 (fun _ ->
3646 state.glinks <- false;
3647 state.mode <- mode)
3649 state.text <- E.s;
3650 postRedisplay "view:linkent"
3652 | Ascii 'a' ->
3653 begin match state.autoscroll with
3654 | Some step ->
3655 conf.autoscrollstep <- step;
3656 state.autoscroll <- None
3657 | None ->
3658 state.autoscroll <- Some conf.autoscrollstep;
3659 state.slideshow <- state.slideshow land lnot 2
3662 | Ascii 'p' when ctrl ->
3663 launchpath () (* XXX where do error messages go? *)
3665 | Ascii 'P' ->
3666 setpresentationmode (not conf.presentation);
3667 showtext ' ' ("presentation mode " ^
3668 if conf.presentation then "on" else "off");
3670 | Ascii 'f' ->
3671 if List.mem Wsi.Fullscreen state.winstate
3672 then Wsi.reshape conf.cwinw conf.cwinh
3673 else Wsi.fullscreen ()
3675 | Ascii ('p'|'N') ->
3676 search state.searchpattern false
3678 | Ascii 'n' | Fn 3 ->
3679 search state.searchpattern true
3681 | Ascii 't' ->
3682 begin match state.layout with
3683 | [] -> ()
3684 | l :: _ -> gotoxy state.x (getpagey l.pageno)
3687 | Ascii ' ' -> nextpage ()
3688 | Delete -> prevpage ()
3689 | Ascii '=' -> showtext ' ' (describe_layout state.layout);
3691 | Ascii 'w' ->
3692 begin match state.layout with
3693 | [] -> ()
3694 | l :: _ ->
3695 Wsi.reshape l.pagew l.pageh;
3696 postRedisplay "w"
3699 | Ascii '\'' -> enterbookmarkmode ()
3700 | Ascii 'h' | Fn 1 -> enterhelpmode ()
3701 | Ascii 'i' -> enterinfomode ()
3702 | Ascii 'e' when Buffer.length state.errmsgs > 0 -> entermsgsmode ()
3704 | Ascii 'm' ->
3705 let ondone s =
3706 match state.layout with
3707 | l :: _ when nonemptystr s ->
3708 state.bookmarks <- (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
3709 | _ -> ()
3711 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
3713 | Ascii '~' ->
3714 quickbookmark ();
3715 showtext ' ' "Quick bookmark added";
3717 | Ascii 'x' -> state.roam ()
3719 | Ascii ('<'|'>' as c) ->
3720 reqlayout (conf.angle + (if c = '>' then 30 else -30)) conf.fitmodel
3722 | Ascii ('['|']' as c) ->
3723 conf.colorscale <-
3724 bound (conf.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0;
3725 postRedisplay "brightness";
3727 | Ascii 'c' when state.mode = View ->
3728 if Wsi.withalt mask
3729 then (
3730 if conf.zoom > 1.0
3731 then
3732 let m = (state.winw - state.w) / 2 in
3733 gotoxy m state.y
3735 else
3736 let (c, a, b), z =
3737 match state.prevcolumns with
3738 | None -> (1, 0, 0), 1.0
3739 | Some (columns, z) ->
3740 let cab =
3741 match columns with
3742 | Csplit (c, _) -> -c, 0, 0
3743 | Cmulti ((c, a, b), _) -> c, a, b
3744 | Csingle _ -> 1, 0, 0
3746 cab, z
3748 setcolumns View c a b;
3749 setzoom z
3751 | Down | Up when ctrl && Wsi.withshift mask ->
3752 let zoom, x = state.prevzoom in
3753 setzoom zoom;
3754 state.x <- x;
3756 | Ascii 'k' | Up ->
3757 begin match state.autoscroll with
3758 | None ->
3759 begin match state.mode with
3760 | Birdseye beye -> upbirdseye 1 beye
3761 | Textentry _ | View | LinkNav _ ->
3762 if ctrl
3763 then gotoxy state.x (clamp ~-(state.winh/2))
3764 else (
3765 if not (Wsi.withshift mask) && conf.presentation
3766 then prevpage ()
3767 else gotoxy state.x (clamp (-conf.scrollstep))
3770 | Some n -> setautoscrollspeed n false
3773 | Ascii 'j' | Down ->
3774 begin match state.autoscroll with
3775 | None ->
3776 begin match state.mode with
3777 | Birdseye beye -> downbirdseye 1 beye
3778 | Textentry _ | View | LinkNav _ ->
3779 if ctrl
3780 then gotoxy state.x (clamp (state.winh/2))
3781 else (
3782 if not (Wsi.withshift mask) && conf.presentation
3783 then nextpage ()
3784 else gotoxy state.x (clamp (conf.scrollstep))
3787 | Some n -> setautoscrollspeed n true
3790 | Left | Right when not (Wsi.withalt mask) ->
3791 if canpan ()
3792 then
3793 let dx =
3794 if ctrl
3795 then state.winw / 2
3796 else conf.hscrollstep
3798 let dx =
3799 let pv = Wsi.kc2kt key in
3800 if pv = Keys.Left then dx else -dx
3802 gotoxy (panbound (state.x + dx)) state.y
3803 else (
3804 state.text <- E.s;
3805 postRedisplay "left/right"
3808 | Prior ->
3809 let y =
3810 if ctrl
3811 then
3812 match state.layout with
3813 | [] -> state.y
3814 | l :: _ -> state.y - l.pagey
3815 else clamp (pgscale (-state.winh))
3817 gotoxy state.x y
3819 | Next ->
3820 let y =
3821 if ctrl
3822 then
3823 match List.rev state.layout with
3824 | [] -> state.y
3825 | l :: _ -> getpagey l.pageno
3826 else clamp (pgscale state.winh)
3828 gotoxy state.x y
3830 | Ascii 'g' | Home ->
3831 addnav ();
3832 gotoxy 0 0
3833 | Ascii 'G' | End ->
3834 addnav ();
3835 gotoxy 0 (clamp state.maxy)
3837 | Right when Wsi.withalt mask ->
3838 addnavnorc ();
3839 gotoxy state.x (getnav 1)
3840 | Left when Wsi.withalt mask ->
3841 addnavnorc ();
3842 gotoxy state.x (getnav ~-1)
3844 | Ascii 'r' ->
3845 reload ()
3847 | Ascii 'v' when conf.debug ->
3848 state.rects <- [];
3849 List.iter (fun l ->
3850 match getopaque l.pageno with
3851 | None -> ()
3852 | Some opaque ->
3853 let x0, y0, x1, y1 = pagebbox opaque in
3854 let rect = (float x0, float y0,
3855 float x1, float y0,
3856 float x1, float y1,
3857 float x0, float y1) in
3858 debugrect rect;
3859 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
3860 state.rects <- (l.pageno, color, rect) :: state.rects;
3861 ) state.layout;
3862 postRedisplay "v";
3864 | Ascii '|' ->
3865 let mode = state.mode in
3866 let cmd = ref E.s in
3867 let onleave = function
3868 | Cancel -> state.mode <- mode
3869 | Confirm ->
3870 List.iter (fun l ->
3871 match getopaque l.pageno with
3872 | Some opaque -> pipesel opaque !cmd
3873 | None -> ()) state.layout;
3874 state.mode <- mode
3876 let ondone s =
3877 cbput state.hists.sel s;
3878 cmd := s
3880 let te =
3881 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
3883 postRedisplay "|";
3884 state.mode <- Textentry (te, onleave);
3886 | (Ascii _|Fn _|Enter|Left|Right|Code _|Ctrl _) ->
3887 vlog "huh? %s" (Wsi.keyname key)
3890 let linknavkeyboard key mask linknav =
3891 let pv = Wsi.kc2kt key in
3892 let getpage pageno =
3893 let rec loop = function
3894 | [] -> None
3895 | l :: _ when l.pageno = pageno -> Some l
3896 | _ :: rest -> loop rest
3897 in loop state.layout
3899 let doexact (pageno, n) =
3900 match getopaque pageno, getpage pageno with
3901 | Some opaque, Some l ->
3902 if pv = Keys.Enter
3903 then
3904 let under = getlink opaque n in
3905 postRedisplay "link gotounder";
3906 gotounder under;
3907 state.mode <- View;
3908 else
3909 let opt, dir =
3910 let open Keys in
3911 match pv with
3912 | Home -> Some (findlink opaque LDfirst), -1
3913 | End -> Some (findlink opaque LDlast), 1
3914 | Left -> Some (findlink opaque (LDleft n)), -1
3915 | Right -> Some (findlink opaque (LDright n)), 1
3916 | Up -> Some (findlink opaque (LDup n)), -1
3917 | Down -> Some (findlink opaque (LDdown n)), 1
3918 | Delete|Escape|Insert|Enter|Next|Prior|Ascii _
3919 | Code _|Fn _|Ctrl _|Backspace -> None, 0
3921 let pwl l dir =
3922 begin match findpwl l.pageno dir with
3923 | Pwlnotfound -> ()
3924 | Pwl pageno ->
3925 let notfound dir =
3926 state.mode <- LinkNav (Ltgendir dir);
3927 let y, h = getpageyh pageno in
3928 let y =
3929 if dir < 0
3930 then y + h - state.winh
3931 else y
3933 gotoxy state.x y
3935 begin match getopaque pageno, getpage pageno with
3936 | Some opaque, Some _ ->
3937 let link =
3938 let ld = if dir > 0 then LDfirst else LDlast in
3939 findlink opaque ld
3941 begin match link with
3942 | Lfound m ->
3943 showlinktype (getlink opaque m);
3944 state.mode <- LinkNav (Ltexact (pageno, m));
3945 postRedisplay "linknav jpage";
3946 | Lnotfound -> notfound dir
3947 end;
3948 | _ -> notfound dir
3949 end;
3950 end;
3952 begin match opt with
3953 | Some Lnotfound -> pwl l dir;
3954 | Some (Lfound m) ->
3955 if m = n
3956 then pwl l dir
3957 else (
3958 let _, y0, _, y1 = getlinkrect opaque m in
3959 if y0 < l.pagey
3960 then gotopage1 l.pageno y0
3961 else (
3962 let d = fstate.fontsize + 1 in
3963 if y1 - l.pagey > l.pagevh - d
3964 then gotopage1 l.pageno (y1 - state.winh + d)
3965 else postRedisplay "linknav";
3967 showlinktype (getlink opaque m);
3968 state.mode <- LinkNav (Ltexact (l.pageno, m));
3971 | None -> viewkeyboard key mask
3972 end;
3973 | _ -> viewkeyboard key mask
3975 if pv = Keys.Insert
3976 then (
3977 begin match linknav with
3978 | Ltexact pa -> state.lnava <- Some pa
3979 | Ltgendir _ | Ltnotready _ -> ()
3980 end;
3981 state.mode <- View;
3982 postRedisplay "leave linknav"
3984 else
3985 match linknav with
3986 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
3987 | Ltexact exact -> doexact exact
3990 let keyboard key mask =
3991 if (key = Char.code 'g' && Wsi.withctrl mask) && not (istextentry state.mode)
3992 then wcmd "interrupt"
3993 else state.uioh <- state.uioh#key key mask
3996 let birdseyekeyboard key mask
3997 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
3998 let incr =
3999 match conf.columns with
4000 | Csingle _ -> 1
4001 | Cmulti ((c, _, _), _) -> c
4002 | Csplit _ -> failwith "bird's eye split mode"
4004 let pgh layout = List.fold_left
4005 (fun m l -> max l.pageh m) state.winh layout in
4006 let open Keys in
4007 match Wsi.kc2kt key with
4008 | Ascii 'l' when Wsi.withctrl mask ->
4009 let y, h = getpageyh pageno in
4010 let top = (state.winh - h) / 2 in
4011 gotoxy state.x (max 0 (y - top))
4012 | Enter -> leavebirdseye beye false
4013 | Escape -> leavebirdseye beye true
4014 | Up -> upbirdseye incr beye
4015 | Down -> downbirdseye incr beye
4016 | Left -> upbirdseye 1 beye
4017 | Right -> downbirdseye 1 beye
4019 | Prior ->
4020 begin match state.layout with
4021 | l :: _ ->
4022 if l.pagey != 0
4023 then (
4024 state.mode <- Birdseye (
4025 oconf, leftx, l.pageno, hooverpageno, anchor
4027 gotopage1 l.pageno 0;
4029 else (
4030 let layout = layout state.x (state.y-state.winh)
4031 state.winw
4032 (pgh state.layout) in
4033 match layout with
4034 | [] -> gotoxy state.x (clamp (-state.winh))
4035 | l :: _ ->
4036 state.mode <- Birdseye (
4037 oconf, leftx, l.pageno, hooverpageno, anchor
4039 gotopage1 l.pageno 0
4042 | [] -> gotoxy state.x (clamp (-state.winh))
4043 end;
4045 | Next ->
4046 begin match List.rev state.layout with
4047 | l :: _ ->
4048 let layout = layout state.x
4049 (state.y + (pgh state.layout))
4050 state.winw state.winh in
4051 begin match layout with
4052 | [] ->
4053 let incr = l.pageh - l.pagevh in
4054 if incr = 0
4055 then (
4056 state.mode <-
4057 Birdseye (
4058 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
4060 postRedisplay "birdseye pagedown";
4062 else gotoxy state.x (clamp (incr + conf.interpagespace*2));
4064 | l :: _ ->
4065 state.mode <-
4066 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
4067 gotopage1 l.pageno 0;
4070 | [] -> gotoxy state.x (clamp state.winh)
4071 end;
4073 | Home ->
4074 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
4075 gotopage1 0 0
4077 | End ->
4078 let pageno = state.pagecount - 1 in
4079 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
4080 if not (pagevisible state.layout pageno)
4081 then
4082 let h =
4083 match List.rev state.pdims with
4084 | [] -> state.winh
4085 | (_, _, h, _) :: _ -> h
4087 gotoxy
4088 state.x
4089 (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
4090 else postRedisplay "birdseye end";
4092 | Delete|Insert|Ascii _|Code _|Ctrl _|Fn _|Backspace -> viewkeyboard key mask
4095 let drawpage l =
4096 let color =
4097 match state.mode with
4098 | Textentry _ -> scalecolor 0.4
4099 | LinkNav _ | View -> scalecolor 1.0
4100 | Birdseye (_, _, pageno, hooverpageno, _) ->
4101 if l.pageno = hooverpageno
4102 then scalecolor 0.9
4103 else (
4104 if l.pageno = pageno
4105 then (
4106 let c = scalecolor 1.0 in
4107 GlDraw.color c;
4108 GlDraw.line_width 3.0;
4109 let dispx = l.pagedispx in
4110 linerect
4111 (float (dispx-1)) (float (l.pagedispy-1))
4112 (float (dispx+l.pagevw+1))
4113 (float (l.pagedispy+l.pagevh+1));
4114 GlDraw.line_width 1.0;
4117 else scalecolor 0.8
4120 drawtiles l color;
4123 let postdrawpage l linkindexbase =
4124 match getopaque l.pageno with
4125 | Some opaque ->
4126 if tileready l l.pagex l.pagey
4127 then
4128 let x = l.pagedispx - l.pagex
4129 and y = l.pagedispy - l.pagey in
4130 let hlmask =
4131 match conf.columns with
4132 | Csingle _ | Cmulti _ ->
4133 (if conf.hlinks then 1 else 0)
4134 + (if state.glinks
4135 && not (isbirdseye state.mode) then 2 else 0)
4136 | Csplit _ -> 0
4138 let s =
4139 match state.mode with
4140 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
4141 | Textentry _
4142 | Birdseye _
4143 | View
4144 | LinkNav _ -> E.s
4146 Hashtbl.find_all state.prects l.pageno |>
4147 List.iter (fun vals -> drawprect opaque x y vals);
4148 let n = postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize) in
4149 if n < 0
4150 then (Glutils.redisplay := true; 0)
4151 else n
4152 else 0
4153 | _ -> 0
4156 let scrollindicator () =
4157 let sbw, ph, sh = state.uioh#scrollph in
4158 let sbh, pw, sw = state.uioh#scrollpw in
4160 let x0,x1,hx0 =
4161 if conf.leftscroll
4162 then (0, sbw, sbw)
4163 else ((state.winw - sbw), state.winw, 0)
4166 Gl.enable `blend;
4167 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
4168 let (r, g, b, alpha) = conf.sbarcolor in
4169 GlDraw.color (r, g, b) ~alpha;
4170 filledrect (float x0) 0. (float x1) (float state.winh);
4171 filledrect
4172 (float hx0) (float (state.winh - sbh))
4173 (float (hx0 + state.winw)) (float state.winh);
4174 let (r, g, b, alpha) = conf.sbarhndlcolor in
4175 GlDraw.color (r, g, b) ~alpha;
4177 filledrect (float x0) ph (float x1) (ph +. sh);
4178 let pw = pw +. float hx0 in
4179 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
4180 Gl.disable `blend;
4183 let showsel () =
4184 match state.mstate with
4185 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ -> ()
4186 | Msel ((x0, y0), (x1, y1)) ->
4187 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
4188 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
4189 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
4190 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
4193 let showrects = function
4194 | [] -> ()
4195 | rects ->
4196 Gl.enable `blend;
4197 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
4198 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
4199 List.iter
4200 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
4201 List.iter (fun l ->
4202 if l.pageno = pageno
4203 then (
4204 let dx = float (l.pagedispx - l.pagex) in
4205 let dy = float (l.pagedispy - l.pagey) in
4206 let r, g, b, alpha = c in
4207 GlDraw.color (r, g, b) ~alpha;
4208 filledrect2
4209 (x0+.dx) (y0+.dy)
4210 (x1+.dx) (y1+.dy)
4211 (x3+.dx) (y3+.dy)
4212 (x2+.dx) (y2+.dy);
4214 ) state.layout
4215 ) rects;
4216 Gl.disable `blend;
4219 let display () =
4220 GlDraw.color (scalecolor2 conf.bgcolor);
4221 GlClear.color (scalecolor2 conf.bgcolor);
4222 GlClear.clear [`color];
4223 List.iter drawpage state.layout;
4224 let rects =
4225 match state.mode with
4226 | LinkNav (Ltexact (pageno, linkno)) ->
4227 begin match getopaque pageno with
4228 | Some opaque ->
4229 let x0, y0, x1, y1 = getlinkrect opaque linkno in
4230 let color = (0.0, 0.0, 0.5, 0.5) in
4231 (pageno, color,
4232 (float x0, float y0,
4233 float x1, float y0,
4234 float x1, float y1,
4235 float x0, float y1)
4236 ) :: state.rects
4237 | None -> state.rects
4239 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
4240 | Birdseye _
4241 | Textentry _
4242 | View -> state.rects
4244 showrects rects;
4245 let rec postloop linkindexbase = function
4246 | l :: rest ->
4247 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
4248 postloop linkindexbase rest
4249 | [] -> ()
4251 showsel ();
4252 postloop 0 state.layout;
4253 state.uioh#display;
4254 begin match state.mstate with
4255 | Mzoomrect ((x0, y0), (x1, y1)) ->
4256 Gl.enable `blend;
4257 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
4258 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
4259 filledrect (float x0) (float y0) (float x1) (float y1);
4260 Gl.disable `blend;
4261 | Msel _
4262 | Mpan _
4263 | Mscrolly | Mscrollx
4264 | Mzoom _
4265 | Mnone -> ()
4266 end;
4267 enttext ();
4268 scrollindicator ();
4269 Wsi.swapb ();
4272 let zoomrect x y x1 y1 =
4273 let x0 = min x x1
4274 and x1 = max x x1
4275 and y0 = min y y1 in
4276 let zoom = (float state.w) /. float (x1 - x0) in
4277 let margin =
4278 let simple () =
4279 if state.w < state.winw
4280 then (state.winw - state.w) / 2
4281 else 0
4283 match conf.fitmodel with
4284 | FitWidth | FitProportional -> simple ()
4285 | FitPage ->
4286 match conf.columns with
4287 | Csplit _ ->
4288 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
4289 | Cmulti _ | Csingle _ -> simple ()
4291 gotoxy ((state.x + margin) - x0) (state.y + y0);
4292 state.anchor <- getanchor ();
4293 setzoom zoom;
4294 resetmstate ();
4297 let annot inline x y =
4298 match unproject x y with
4299 | Some (opaque, n, ux, uy) ->
4300 let add text =
4301 addannot opaque ux uy text;
4302 wcmd "freepage %s" (~> opaque);
4303 Hashtbl.remove state.pagemap (n, state.gen);
4304 flushtiles ();
4305 gotoxy state.x state.y
4307 if inline
4308 then
4309 let ondone s = add s in
4310 let mode = state.mode in
4311 state.mode <- Textentry (
4312 ("annotation: ", E.s, None, textentry, ondone, true),
4313 fun _ -> state.mode <- mode);
4314 state.text <- E.s;
4315 enttext ();
4316 postRedisplay "annot"
4317 else add @@ getusertext E.s
4318 | _ -> ()
4321 let zoomblock x y =
4322 let g opaque l px py =
4323 match rectofblock opaque px py with
4324 | Some a ->
4325 let x0 = a.(0) -. 20. in
4326 let x1 = a.(1) +. 20. in
4327 let y0 = a.(2) -. 20. in
4328 let zoom = (float state.w) /. (x1 -. x0) in
4329 let pagey = getpagey l.pageno in
4330 let margin = (state.w - l.pagew)/2 in
4331 let nx = -truncate x0 - margin in
4332 gotoxy nx (pagey + truncate y0);
4333 state.anchor <- getanchor ();
4334 setzoom zoom;
4335 None
4336 | None -> None
4338 match conf.columns with
4339 | Csplit _ ->
4340 impmsg "block zooming does not work properly in split columns mode"
4341 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
4344 let scrollx x =
4345 let winw = state.winw - 1 in
4346 let s = float x /. float winw in
4347 let destx = truncate (float (state.w + winw) *. s) in
4348 gotoxy (winw - destx) state.y;
4349 state.mstate <- Mscrollx;
4352 let scrolly y =
4353 let s = float y /. float state.winh in
4354 let desty = truncate (s *. float (maxy ())) in
4355 gotoxy state.x desty;
4356 state.mstate <- Mscrolly;
4359 let viewmulticlick clicks x y mask =
4360 let g opaque l px py =
4361 let mark =
4362 match clicks with
4363 | 2 -> Mark_word
4364 | 3 -> Mark_line
4365 | 4 -> Mark_block
4366 | _ -> Mark_page
4368 if markunder opaque px py mark
4369 then (
4370 Some (fun () ->
4371 let dopipe cmd =
4372 match getopaque l.pageno with
4373 | None -> ()
4374 | Some opaque -> pipesel opaque cmd
4376 state.roam <- (fun () -> dopipe conf.paxcmd);
4377 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
4380 else None
4382 postRedisplay "viewmulticlick";
4383 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
4386 let canselect () =
4387 match conf.columns with
4388 | Csplit _ -> false
4389 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
4392 let viewmouse button down x y mask =
4393 match button with
4394 | n when (n == 4 || n == 5) && not down ->
4395 if Wsi.withctrl mask
4396 then (
4397 let incr =
4398 if n = 5
4399 then if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
4400 else if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
4402 let fx, fy =
4403 match state.mstate with
4404 | Mzoom (oldn, _, pos) when n = oldn -> pos
4405 | Mzoomrect _ | Mnone | Mpan _
4406 | Msel _ | Mscrollx | Mscrolly | Mzoom _ -> (x, y)
4408 let zoom = conf.zoom -. incr in
4409 state.mstate <- Mzoom (n, 0, (x, y));
4410 if false && abs (fx - x) > 5 || abs (fy - y) > 5
4411 then pivotzoom ~x ~y zoom
4412 else pivotzoom zoom
4414 else (
4415 match state.autoscroll with
4416 | Some step -> setautoscrollspeed step (n=4)
4417 | None ->
4418 if conf.wheelbypage || conf.presentation
4419 then (
4420 if n = 4
4421 then prevpage ()
4422 else nextpage ()
4424 else
4425 let incr = if n = 4 then -conf.scrollstep else conf.scrollstep in
4426 let incr = incr * 2 in
4427 let y = clamp incr in
4428 gotoxy state.x y
4431 | n when (n = 6 || n = 7) && not down && canpan () ->
4432 let x =
4433 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep) in
4434 gotoxy x state.y
4436 | 1 when Wsi.withshift mask ->
4437 state.mstate <- Mnone;
4438 if not down
4439 then (
4440 match unproject x y with
4441 | None -> ()
4442 | Some (_, pageno, ux, uy) ->
4443 let cmd = Printf.sprintf
4444 "%s %s %d %d %d"
4445 conf.stcmd state.path pageno ux uy
4447 match spawn cmd [] with
4448 | exception exn ->
4449 impmsg "execution of synctex command(%S) failed: %S"
4450 conf.stcmd @@ exntos exn
4451 | _pid -> ()
4454 | 1 when Wsi.withctrl mask ->
4455 if down
4456 then (
4457 Wsi.setcursor Wsi.CURSOR_FLEUR;
4458 state.mstate <- Mpan (x, y)
4460 else state.mstate <- Mnone
4462 | 3 ->
4463 if down
4464 then (
4465 if Wsi.withshift mask
4466 then (
4467 annot conf.annotinline x y;
4468 postRedisplay "addannot"
4470 else
4471 let p = (x, y) in
4472 Wsi.setcursor Wsi.CURSOR_CYCLE;
4473 state.mstate <- Mzoomrect (p, p)
4475 else (
4476 match state.mstate with
4477 | Mzoomrect ((x0, y0), _) ->
4478 if abs (x-x0) > 10 && abs (y - y0) > 10
4479 then zoomrect x0 y0 x y
4480 else (
4481 resetmstate ();
4482 postRedisplay "kill accidental zoom rect";
4484 | Msel _
4485 | Mpan _
4486 | Mscrolly | Mscrollx
4487 | Mzoom _
4488 | Mnone -> resetmstate ()
4491 | 1 when vscrollhit x ->
4492 if down
4493 then
4494 let _, position, sh = state.uioh#scrollph in
4495 if y > truncate position && y < truncate (position +. sh)
4496 then state.mstate <- Mscrolly
4497 else scrolly y
4498 else state.mstate <- Mnone
4500 | 1 when y > state.winh - hscrollh () ->
4501 if down
4502 then
4503 let _, position, sw = state.uioh#scrollpw in
4504 if x > truncate position && x < truncate (position +. sw)
4505 then state.mstate <- Mscrollx
4506 else scrollx x
4507 else state.mstate <- Mnone
4509 | 1 when state.bzoom -> if not down then zoomblock x y
4511 | 1 ->
4512 let dest = if down then getunder x y else Unone in
4513 begin match dest with
4514 | Ulinkuri _ -> gotounder dest
4515 | Unone when down ->
4516 Wsi.setcursor Wsi.CURSOR_FLEUR;
4517 state.mstate <- Mpan (x, y);
4518 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4519 | Unone | Utext _ ->
4520 if down
4521 then (
4522 if canselect ()
4523 then (
4524 state.mstate <- Msel ((x, y), (x, y));
4525 postRedisplay "mouse select";
4528 else (
4529 match state.mstate with
4530 | Mnone -> ()
4531 | Mzoom _ | Mscrollx | Mscrolly -> state.mstate <- Mnone
4532 | Mzoomrect ((x0, y0), _) -> zoomrect x0 y0 x y
4533 | Mpan _ ->
4534 Wsi.setcursor Wsi.CURSOR_INHERIT;
4535 state.mstate <- Mnone
4536 | Msel ((x0, y0), (x1, y1)) ->
4537 let rec loop = function
4538 | [] -> ()
4539 | l :: rest ->
4540 let inside =
4541 let a0 = l.pagedispy in
4542 let a1 = a0 + l.pagevh in
4543 let b0 = l.pagedispx in
4544 let b1 = b0 + l.pagevw in
4545 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
4546 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
4548 if inside
4549 then
4550 match getopaque l.pageno with
4551 | Some opaque ->
4552 let dosel cmd () =
4553 pipef ~closew:false "Msel"
4554 (fun w ->
4555 copysel w opaque;
4556 postRedisplay "Msel") cmd
4558 dosel conf.selcmd ();
4559 state.roam <- dosel conf.paxcmd;
4560 | None -> ()
4561 else loop rest
4563 loop state.layout;
4564 resetmstate ();
4567 | _ -> ()
4570 let birdseyemouse button down x y mask
4571 (conf, leftx, _, hooverpageno, anchor) =
4572 match button with
4573 | 1 when down ->
4574 let rec loop = function
4575 | [] -> ()
4576 | l :: rest ->
4577 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4578 && x > l.pagedispx && x < l.pagedispx + l.pagevw
4579 then (
4580 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
4582 else loop rest
4584 loop state.layout
4585 | 3 -> ()
4586 | _ -> viewmouse button down x y mask
4589 let uioh = object
4590 method display = ()
4592 method key key mask =
4593 begin match state.mode with
4594 | Textentry textentry -> textentrykeyboard key mask textentry
4595 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
4596 | View -> viewkeyboard key mask
4597 | LinkNav linknav -> linknavkeyboard key mask linknav
4598 end;
4599 state.uioh
4601 method button button bstate x y mask =
4602 begin match state.mode with
4603 | LinkNav _ | View -> viewmouse button bstate x y mask
4604 | Birdseye beye -> birdseyemouse button bstate x y mask beye
4605 | Textentry _ -> ()
4606 end;
4607 state.uioh
4609 method multiclick clicks x y mask =
4610 begin match state.mode with
4611 | LinkNav _ | View -> viewmulticlick clicks x y mask
4612 | Birdseye _ | Textentry _ -> ()
4613 end;
4614 state.uioh
4616 method motion x y =
4617 begin match state.mode with
4618 | Textentry _ -> ()
4619 | View | Birdseye _ | LinkNav _ ->
4620 match state.mstate with
4621 | Mzoom _ | Mnone -> ()
4622 | Mpan (x0, y0) ->
4623 let dx = x - x0
4624 and dy = y0 - y in
4625 state.mstate <- Mpan (x, y);
4626 let x = if canpan () then panbound (state.x + dx) else state.x in
4627 let y = clamp dy in
4628 gotoxy x y
4630 | Msel (a, _) ->
4631 state.mstate <- Msel (a, (x, y));
4632 postRedisplay "motion select";
4634 | Mscrolly ->
4635 let y = min state.winh (max 0 y) in
4636 scrolly y
4638 | Mscrollx ->
4639 let x = min state.winw (max 0 x) in
4640 scrollx x
4642 | Mzoomrect (p0, _) ->
4643 state.mstate <- Mzoomrect (p0, (x, y));
4644 postRedisplay "motion zoomrect";
4645 end;
4646 state.uioh
4648 method pmotion x y =
4649 begin match state.mode with
4650 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
4651 let rec loop = function
4652 | [] ->
4653 if hooverpageno != -1
4654 then (
4655 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
4656 postRedisplay "pmotion birdseye no hoover";
4658 | l :: rest ->
4659 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4660 && x > l.pagedispx && x < l.pagedispx + l.pagevw
4661 then (
4662 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
4663 postRedisplay "pmotion birdseye hoover";
4665 else loop rest
4667 loop state.layout
4669 | Textentry _ -> ()
4671 | LinkNav _ | View ->
4672 match state.mstate with
4673 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
4674 | Mnone ->
4675 updateunder x y;
4676 if canselect ()
4677 then
4678 match conf.pax with
4679 | None -> ()
4680 | Some past ->
4681 let now = now () in
4682 let delta = now -. past in
4683 if delta > 0.01
4684 then paxunder x y
4685 else conf.pax <- Some now
4686 end;
4687 state.uioh
4689 method infochanged _ = ()
4691 method scrollph =
4692 let maxy = maxy () in
4693 let p, h =
4694 if maxy = 0
4695 then 0.0, float state.winh
4696 else scrollph state.y maxy
4698 vscrollw (), p, h
4700 method scrollpw =
4701 let fwinw = float (state.winw - vscrollw ()) in
4702 let sw =
4703 let sw = fwinw /. float state.w in
4704 let sw = fwinw *. sw in
4705 max sw (float conf.scrollh)
4707 let position =
4708 let maxx = state.w + state.winw in
4709 let x = state.winw - state.x in
4710 let percent = float x /. float maxx in
4711 (fwinw -. sw) *. percent
4713 hscrollh (), position, sw
4715 method modehash =
4716 let modename =
4717 match state.mode with
4718 | LinkNav _ -> "links"
4719 | Textentry _ -> "textentry"
4720 | Birdseye _ -> "birdseye"
4721 | View -> "view"
4723 findkeyhash conf modename
4725 method eformsgs = true
4726 method alwaysscrolly = false
4727 method scroll dx dy =
4728 let x = if canpan () then panbound (state.x + dx) else state.x in
4729 gotoxy x (clamp (2 * dy));
4730 state.uioh
4731 method zoom z x y =
4732 pivotzoom ~x ~y (conf.zoom *. exp z);
4733 end;;
4735 let addrect pageno r g b a x0 y0 x1 y1 =
4736 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
4739 let ract cmds =
4740 let cl = splitatchar cmds ' ' in
4741 let scan s fmt f =
4742 try Scanf.sscanf s fmt f
4743 with exn -> adderrfmt "remote exec" "error processing '%S': %s\n"
4744 cmds @@ exntos exn
4746 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
4747 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
4748 s pageno r g b a x0 y0 x1 y1;
4749 onpagerect
4750 pageno
4751 (fun w h ->
4752 let _,w1,h1,_ = getpagedim pageno in
4753 let sw = float w1 /. float w
4754 and sh = float h1 /. float h in
4755 let x0s = x0 *. sw
4756 and x1s = x1 *. sw
4757 and y0s = y0 *. sh
4758 and y1s = y1 *. sh in
4759 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
4760 let color = (r, g, b, a) in
4761 if conf.verbose then debugrect rect;
4762 state.rects <- (pageno, color, rect) :: state.rects;
4763 postRedisplay s;
4766 match cl with
4767 | "reload", "" -> reload ()
4768 | "goto", args ->
4769 scan args "%u %f %f"
4770 (fun pageno x y ->
4771 let cmd, _ = state.geomcmds in
4772 if emptystr cmd
4773 then gotopagexy pageno x y
4774 else
4775 let f prevf () =
4776 gotopagexy pageno x y;
4777 prevf ()
4779 state.reprf <- f state.reprf
4781 | "goto1", args -> scan args "%u %f" gotopage
4782 | "gotor", args -> scan args "%S" gotoremote
4783 | "rect", args ->
4784 scan args "%u %u %f %f %f %f"
4785 (fun pageno c x0 y0 x1 y1 ->
4786 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
4787 rectx "rect" pageno color x0 y0 x1 y1;
4789 | "prect", args ->
4790 scan args "%u %f %f %f %f %f %f %f %f"
4791 (fun pageno r g b alpha x0 y0 x1 y1 ->
4792 addrect pageno r g b alpha x0 y0 x1 y1;
4793 postRedisplay "prect"
4795 | "pgoto", args ->
4796 scan args "%u %f %f"
4797 (fun pageno x y ->
4798 let optopaque =
4799 match getopaque pageno with
4800 | Some opaque -> opaque
4801 | None -> ~< E.s
4803 pgoto optopaque pageno x y;
4804 let rec fixx = function
4805 | [] -> ()
4806 | l :: rest ->
4807 if l.pageno = pageno
4808 then gotoxy (state.x - l.pagedispx) state.y
4809 else fixx rest
4811 let layout =
4812 let mult =
4813 match conf.columns with
4814 | Csingle _ | Csplit _ -> 1
4815 | Cmulti ((n, _, _), _) -> n
4817 layout 0 state.y (state.winw * mult) state.winh
4819 fixx layout
4821 | "activatewin", "" -> Wsi.activatewin ()
4822 | "quit", "" -> raise Quit
4823 | "keys", keys ->
4824 begin try
4825 let l = Config.keys_of_string keys in
4826 List.iter (fun (k, m) -> keyboard k m) l
4827 with exn -> adderrfmt "error processing keys" "`%S': %s\n"
4828 cmds @@ exntos exn
4830 | "clearrects", "" ->
4831 Hashtbl.clear state.prects;
4832 postRedisplay "clearrects"
4833 | _ ->
4834 adderrfmt "remote command"
4835 "error processing remote command: %S\n" cmds;
4838 let remote =
4839 let scratch = Bytes.create 80 in
4840 let buf = Buffer.create 80 in
4841 fun fd ->
4842 match tempfailureretry (Unix.read fd scratch 0) 80 with
4843 | exception Unix.Unix_error (Unix.EAGAIN, _, _) -> None
4844 | 0 ->
4845 Unix.close fd;
4846 if Buffer.length buf > 0
4847 then (
4848 let s = Buffer.contents buf in
4849 Buffer.clear buf;
4850 ract s;
4852 None
4853 | n ->
4854 let rec eat ppos =
4855 let nlpos =
4856 match Bytes.index_from scratch ppos '\n' with
4857 | pos -> if pos >= n then -1 else pos
4858 | exception Not_found -> -1
4860 if nlpos >= 0
4861 then (
4862 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
4863 let s = Buffer.contents buf in
4864 Buffer.clear buf;
4865 ract s;
4866 eat (nlpos+1);
4868 else (
4869 Buffer.add_subbytes buf scratch ppos (n-ppos);
4870 Some fd
4872 in eat 0
4875 let remoteopen path =
4876 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
4877 with exn ->
4878 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
4879 None
4882 let () =
4883 let gcconfig = ref false in
4884 let trimcachepath = ref E.s in
4885 let rcmdpath = ref E.s in
4886 let pageno = ref None in
4887 let openlast = ref false in
4888 let doreap = ref false in
4889 let csspath = ref None in
4890 selfexec := Sys.executable_name;
4891 Arg.parse
4892 (Arg.align
4893 [("-p", Arg.String (fun s -> state.password <- s),
4894 "<password> Set password");
4896 ("-f", Arg.String
4897 (fun s ->
4898 Config.fontpath := s;
4899 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
4901 "<path> Set path to the user interface font");
4903 ("-c", Arg.String
4904 (fun s ->
4905 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
4906 Config.confpath := s),
4907 "<path> Set path to the configuration file");
4909 ("-last", Arg.Set openlast, " Open last document");
4911 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
4912 "<page-number> Jump to page");
4914 ("-tcf", Arg.String (fun s -> trimcachepath := s),
4915 "<path> Set path to the trim cache file");
4917 ("-dest", Arg.String (fun s -> state.nameddest <- s),
4918 "<named-destination> Set named destination");
4920 ("-remote", Arg.String (fun s -> rcmdpath := s),
4921 "<path> Set path to the source of remote commands");
4923 ("-gc", Arg.Set gcconfig, " Collect config garbage");
4925 ("-v", Arg.Unit (fun () ->
4926 Printf.printf
4927 "%s\nconfiguration file: %s\n"
4928 (Help.version ())
4929 Config.defconfpath;
4930 exit 0), " Print version and exit");
4932 ("-css", Arg.String (fun s -> csspath := Some s),
4933 "<path> Set path to the style sheet to use with EPUB/HTML");
4935 ("-origin", Arg.String (fun s -> state.origin <- s),
4936 "<origin> <undocumented>");
4938 ("-no-title", Arg.Set ignoredoctitlte, " ignore document title");
4939 ("-layout-height", Arg.Set_int layouth,
4940 "<height> layout height html/epub/etc (-1, 0, N)");
4943 (fun s -> state.path <- s)
4944 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:");
4946 let histmode = emptystr state.path && not !openlast in
4948 if not (Config.load !openlast)
4949 then dolog "failed to load configuration";
4951 begin match !pageno with
4952 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
4953 | None -> ()
4954 end;
4956 fillhelp ();
4957 if !gcconfig
4958 then (
4959 Config.gc ();
4960 exit 0
4963 let mu =
4964 object (self)
4965 val mutable m_clicks = 0
4966 val mutable m_click_x = 0
4967 val mutable m_click_y = 0
4968 val mutable m_lastclicktime = infinity
4970 method private cleanup =
4971 state.roam <- noroam;
4972 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
4973 method expose = postRedisplay "expose"
4974 method visible v =
4975 let name =
4976 match v with
4977 | Wsi.Unobscured -> "unobscured"
4978 | Wsi.PartiallyObscured -> "partiallyobscured"
4979 | Wsi.FullyObscured -> "fullyobscured"
4981 vlog "visibility change %s" name
4982 method display = display ()
4983 method map mapped = vlog "mapped %b" mapped
4984 method reshape w h =
4985 self#cleanup;
4986 reshape w h
4987 method mouse b d x y m =
4988 if d && canselect ()
4989 then (
4991 * http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx
4993 m_click_x <- x;
4994 m_click_y <- y;
4995 if b = 1
4996 then (
4997 let t = now () in
4998 if abs x - m_click_x > 10
4999 || abs y - m_click_y > 10
5000 || abs_float (t -. m_lastclicktime) > 0.3
5001 then m_clicks <- 0;
5002 m_clicks <- m_clicks + 1;
5003 m_lastclicktime <- t;
5004 if m_clicks = 1
5005 then (
5006 self#cleanup;
5007 postRedisplay "cleanup";
5008 state.uioh <- state.uioh#button b d x y m;
5010 else state.uioh <- state.uioh#multiclick m_clicks x y m
5012 else (
5013 self#cleanup;
5014 m_clicks <- 0;
5015 m_lastclicktime <- infinity;
5016 state.uioh <- state.uioh#button b d x y m
5019 else state.uioh <- state.uioh#button b d x y m
5020 method motion x y =
5021 state.mpos <- (x, y);
5022 state.uioh <- state.uioh#motion x y
5023 method pmotion x y =
5024 state.mpos <- (x, y);
5025 state.uioh <- state.uioh#pmotion x y
5026 method key k m =
5027 vlog "k=%#x m=%#x" k m;
5028 let mascm = m land (
5029 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
5030 ) in
5031 let keyboard k m =
5032 let x = state.x and y = state.y in
5033 keyboard k m;
5034 if x != state.x || y != state.y then self#cleanup
5036 match state.keystate with
5037 | KSnone ->
5038 let km = k, mascm in
5039 begin
5040 match
5041 let modehash = state.uioh#modehash in
5042 try Hashtbl.find modehash km
5043 with Not_found ->
5044 try Hashtbl.find (findkeyhash conf "global") km
5045 with Not_found -> KMinsrt (k, m)
5046 with
5047 | KMinsrt (k, m) -> keyboard k m
5048 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
5049 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
5051 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
5052 List.iter (fun (k, m) -> keyboard k m) insrt;
5053 state.keystate <- KSnone
5054 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
5055 state.keystate <- KSinto (keys, insrt)
5056 | KSinto _ -> state.keystate <- KSnone
5058 method enter x y =
5059 state.mpos <- (x, y);
5060 state.uioh <- state.uioh#pmotion x y
5061 method leave = state.mpos <- (-1, -1)
5062 method winstate wsl = state.winstate <- wsl
5063 method quit : 'a. 'a = raise Quit
5064 method scroll dx dy = state.uioh <- state.uioh#scroll dx dy
5065 method zoom z x y = state.uioh#zoom z x y
5066 method opendoc path =
5067 state.mode <- View;
5068 state.uioh <- uioh;
5069 postRedisplay "opendoc";
5070 opendoc path state.password
5073 let wsfd, winw, winh = Wsi.init mu conf.cwinw conf.cwinh platform in
5074 state.wsfd <- wsfd;
5076 if not @@ List.exists GlMisc.check_extension
5077 [ "GL_ARB_texture_rectangle"
5078 ; "GL_EXT_texture_recangle"
5079 ; "GL_NV_texture_rectangle" ]
5080 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
5082 if substratis (GlMisc.get_string `renderer) 0 "Mesa DRI Intel("
5083 then (
5084 defconf.sliceheight <- 1024;
5085 defconf.texcount <- 32;
5086 defconf.usepbo <- true;
5089 let cs, ss =
5090 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
5091 | exception exn ->
5092 dolog "socketpair failed: %s" @@ exntos exn;
5093 exit 1
5094 | (r, w) ->
5095 cloexec r;
5096 cloexec w;
5097 r, w
5100 setcheckers conf.checkers;
5102 opengl_has_pbo := GlMisc.check_extension "GL_ARB_pixel_buffer_object";
5104 begin match !csspath with
5105 | None -> ()
5106 | Some "" -> conf.css <- E.s
5107 | Some path ->
5108 let css = filecontents path in
5109 let l = String.length css in
5110 conf.css <-
5111 if substratis css (l-2) "\r\n"
5112 then String.sub css 0 (l-2)
5113 else (if css.[l-1] = '\n' then String.sub css 0 (l-1) else css)
5114 end;
5115 init cs (
5116 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
5117 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
5118 !Config.fontpath, !trimcachepath, !opengl_has_pbo
5120 List.iter GlArray.enable [`texture_coord; `vertex];
5121 state.ss <- ss;
5122 reshape ~firsttime:true winw winh;
5123 state.uioh <- uioh;
5124 if histmode
5125 then (
5126 Wsi.settitle "llpp (history)";
5127 enterhistmode ();
5129 else (
5130 state.text <- "Opening " ^ (mbtoutf8 state.path);
5131 opendoc state.path state.password;
5133 display ();
5134 Wsi.mapwin ();
5135 Wsi.setcursor Wsi.CURSOR_INHERIT;
5136 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
5138 let rec reap () =
5139 match Unix.waitpid [Unix.WNOHANG] ~-1 with
5140 | exception (Unix.Unix_error (Unix.ECHILD, _, _)) -> ()
5141 | exception exn -> dolog "Unix.waitpid: %s" @@ exntos exn
5142 | 0, _ -> ()
5143 | _pid, _status -> reap ()
5145 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
5147 let optrfd =
5148 ref (if nonemptystr !rcmdpath then remoteopen !rcmdpath else None)
5151 let rec loop deadline =
5152 if !doreap
5153 then (
5154 doreap := false;
5155 reap ()
5157 let r = [state.ss; state.wsfd] in
5158 let r =
5159 match !optrfd with
5160 | None -> r
5161 | Some fd -> fd :: r
5163 if !redisplay
5164 then (
5165 Glutils.redisplay := false;
5166 display ();
5168 let timeout =
5169 let now = now () in
5170 if deadline > now
5171 then (
5172 if deadline = infinity
5173 then ~-.1.0
5174 else max 0.0 (deadline -. now)
5176 else 0.0
5178 let r, _, _ =
5179 try Unix.select r [] [] timeout
5180 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
5182 begin match r with
5183 | [] ->
5184 let newdeadline =
5185 match state.autoscroll with
5186 | Some step when step != 0 ->
5187 if state.slideshow land 1 = 1
5188 then (
5189 if state.slideshow land 2 = 0
5190 then state.slideshow <- state.slideshow lor 2
5191 else if step < 0 then prevpage () else nextpage ();
5192 deadline +. (float (abs step))
5194 else
5195 let y = state.y + step in
5196 let fy = if conf.maxhfit then state.winh else 0 in
5197 let y =
5198 if y < 0
5199 then state.maxy - fy
5200 else if y >= state.maxy - fy then 0 else y
5202 gotoxy state.x y;
5203 deadline +. 0.01
5204 | _ -> infinity
5206 loop newdeadline
5208 | l ->
5209 let rec checkfds = function
5210 | [] -> ()
5211 | fd :: rest when fd = state.ss ->
5212 let cmd = rcmd state.ss in
5213 act cmd;
5214 checkfds rest
5216 | fd :: rest when fd = state.wsfd ->
5217 Wsi.readresp fd;
5218 checkfds rest
5220 | fd :: rest when Some fd = !optrfd ->
5221 begin match remote fd with
5222 | None -> optrfd := remoteopen !rcmdpath;
5223 | opt -> optrfd := opt
5224 end;
5225 checkfds rest
5227 | _ :: rest ->
5228 dolog "select returned unknown descriptor";
5229 checkfds rest
5231 checkfds l;
5232 let newdeadline =
5233 let deadline1 =
5234 if deadline = infinity
5235 then now () +. 0.01
5236 else deadline
5238 match state.autoscroll with
5239 | Some step when step != 0 -> deadline1
5240 | _ -> infinity
5242 loop newdeadline
5243 end;
5245 match loop infinity with
5246 | exception Quit ->
5247 Config.save leavebirdseye;
5248 if hasunsavedchanges ()
5249 then save ()
5250 | _ -> error "umpossible - infinity reached"